splice.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  1. /*
  2. * "splice": joining two ropes together by interweaving their strands.
  3. *
  4. * This is the "extended pipe" functionality, where a pipe is used as
  5. * an arbitrary in-memory buffer. Think of a pipe as a small kernel
  6. * buffer that you can use to transfer data from one end to the other.
  7. *
  8. * The traditional unix read/write is extended with a "splice()" operation
  9. * that transfers data buffers to or from a pipe buffer.
  10. *
  11. * Named by Larry McVoy, original implementation from Linus, extended by
  12. * Jens to support splicing to files, network, direct splicing, etc and
  13. * fixing lots of bugs.
  14. *
  15. * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk>
  16. * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
  17. * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
  18. *
  19. */
  20. #include <linux/fs.h>
  21. #include <linux/file.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/splice.h>
  24. #include <linux/mm_inline.h>
  25. #include <linux/swap.h>
  26. #include <linux/writeback.h>
  27. #include <linux/buffer_head.h>
  28. #include <linux/module.h>
  29. #include <linux/syscalls.h>
  30. #include <linux/uio.h>
  31. #include <linux/security.h>
  32. /*
  33. * Attempt to steal a page from a pipe buffer. This should perhaps go into
  34. * a vm helper function, it's already simplified quite a bit by the
  35. * addition of remove_mapping(). If success is returned, the caller may
  36. * attempt to reuse this page for another destination.
  37. */
  38. static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
  39. struct pipe_buffer *buf)
  40. {
  41. struct page *page = buf->page;
  42. struct address_space *mapping;
  43. lock_page(page);
  44. mapping = page_mapping(page);
  45. if (mapping) {
  46. WARN_ON(!PageUptodate(page));
  47. /*
  48. * At least for ext2 with nobh option, we need to wait on
  49. * writeback completing on this page, since we'll remove it
  50. * from the pagecache. Otherwise truncate wont wait on the
  51. * page, allowing the disk blocks to be reused by someone else
  52. * before we actually wrote our data to them. fs corruption
  53. * ensues.
  54. */
  55. wait_on_page_writeback(page);
  56. if (PagePrivate(page))
  57. try_to_release_page(page, GFP_KERNEL);
  58. /*
  59. * If we succeeded in removing the mapping, set LRU flag
  60. * and return good.
  61. */
  62. if (remove_mapping(mapping, page)) {
  63. buf->flags |= PIPE_BUF_FLAG_LRU;
  64. return 0;
  65. }
  66. }
  67. /*
  68. * Raced with truncate or failed to remove page from current
  69. * address space, unlock and return failure.
  70. */
  71. unlock_page(page);
  72. return 1;
  73. }
  74. static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
  75. struct pipe_buffer *buf)
  76. {
  77. page_cache_release(buf->page);
  78. buf->flags &= ~PIPE_BUF_FLAG_LRU;
  79. }
  80. /*
  81. * Check whether the contents of buf is OK to access. Since the content
  82. * is a page cache page, IO may be in flight.
  83. */
  84. static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
  85. struct pipe_buffer *buf)
  86. {
  87. struct page *page = buf->page;
  88. int err;
  89. if (!PageUptodate(page)) {
  90. lock_page(page);
  91. /*
  92. * Page got truncated/unhashed. This will cause a 0-byte
  93. * splice, if this is the first page.
  94. */
  95. if (!page->mapping) {
  96. err = -ENODATA;
  97. goto error;
  98. }
  99. /*
  100. * Uh oh, read-error from disk.
  101. */
  102. if (!PageUptodate(page)) {
  103. err = -EIO;
  104. goto error;
  105. }
  106. /*
  107. * Page is ok afterall, we are done.
  108. */
  109. unlock_page(page);
  110. }
  111. return 0;
  112. error:
  113. unlock_page(page);
  114. return err;
  115. }
  116. static const struct pipe_buf_operations page_cache_pipe_buf_ops = {
  117. .can_merge = 0,
  118. .map = generic_pipe_buf_map,
  119. .unmap = generic_pipe_buf_unmap,
  120. .confirm = page_cache_pipe_buf_confirm,
  121. .release = page_cache_pipe_buf_release,
  122. .steal = page_cache_pipe_buf_steal,
  123. .get = generic_pipe_buf_get,
  124. };
  125. static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
  126. struct pipe_buffer *buf)
  127. {
  128. if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
  129. return 1;
  130. buf->flags |= PIPE_BUF_FLAG_LRU;
  131. return generic_pipe_buf_steal(pipe, buf);
  132. }
  133. static const struct pipe_buf_operations user_page_pipe_buf_ops = {
  134. .can_merge = 0,
  135. .map = generic_pipe_buf_map,
  136. .unmap = generic_pipe_buf_unmap,
  137. .confirm = generic_pipe_buf_confirm,
  138. .release = page_cache_pipe_buf_release,
  139. .steal = user_page_pipe_buf_steal,
  140. .get = generic_pipe_buf_get,
  141. };
  142. /**
  143. * splice_to_pipe - fill passed data into a pipe
  144. * @pipe: pipe to fill
  145. * @spd: data to fill
  146. *
  147. * Description:
  148. * @spd contains a map of pages and len/offset tuples, along with
  149. * the struct pipe_buf_operations associated with these pages. This
  150. * function will link that data to the pipe.
  151. *
  152. */
  153. ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
  154. struct splice_pipe_desc *spd)
  155. {
  156. unsigned int spd_pages = spd->nr_pages;
  157. int ret, do_wakeup, page_nr;
  158. ret = 0;
  159. do_wakeup = 0;
  160. page_nr = 0;
  161. if (pipe->inode)
  162. mutex_lock(&pipe->inode->i_mutex);
  163. for (;;) {
  164. if (!pipe->readers) {
  165. send_sig(SIGPIPE, current, 0);
  166. if (!ret)
  167. ret = -EPIPE;
  168. break;
  169. }
  170. if (pipe->nrbufs < PIPE_BUFFERS) {
  171. int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1);
  172. struct pipe_buffer *buf = pipe->bufs + newbuf;
  173. buf->page = spd->pages[page_nr];
  174. buf->offset = spd->partial[page_nr].offset;
  175. buf->len = spd->partial[page_nr].len;
  176. buf->private = spd->partial[page_nr].private;
  177. buf->ops = spd->ops;
  178. if (spd->flags & SPLICE_F_GIFT)
  179. buf->flags |= PIPE_BUF_FLAG_GIFT;
  180. pipe->nrbufs++;
  181. page_nr++;
  182. ret += buf->len;
  183. if (pipe->inode)
  184. do_wakeup = 1;
  185. if (!--spd->nr_pages)
  186. break;
  187. if (pipe->nrbufs < PIPE_BUFFERS)
  188. continue;
  189. break;
  190. }
  191. if (spd->flags & SPLICE_F_NONBLOCK) {
  192. if (!ret)
  193. ret = -EAGAIN;
  194. break;
  195. }
  196. if (signal_pending(current)) {
  197. if (!ret)
  198. ret = -ERESTARTSYS;
  199. break;
  200. }
  201. if (do_wakeup) {
  202. smp_mb();
  203. if (waitqueue_active(&pipe->wait))
  204. wake_up_interruptible_sync(&pipe->wait);
  205. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  206. do_wakeup = 0;
  207. }
  208. pipe->waiting_writers++;
  209. pipe_wait(pipe);
  210. pipe->waiting_writers--;
  211. }
  212. if (pipe->inode) {
  213. mutex_unlock(&pipe->inode->i_mutex);
  214. if (do_wakeup) {
  215. smp_mb();
  216. if (waitqueue_active(&pipe->wait))
  217. wake_up_interruptible(&pipe->wait);
  218. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  219. }
  220. }
  221. while (page_nr < spd_pages)
  222. page_cache_release(spd->pages[page_nr++]);
  223. return ret;
  224. }
  225. static int
  226. __generic_file_splice_read(struct file *in, loff_t *ppos,
  227. struct pipe_inode_info *pipe, size_t len,
  228. unsigned int flags)
  229. {
  230. struct address_space *mapping = in->f_mapping;
  231. unsigned int loff, nr_pages, req_pages;
  232. struct page *pages[PIPE_BUFFERS];
  233. struct partial_page partial[PIPE_BUFFERS];
  234. struct page *page;
  235. pgoff_t index, end_index;
  236. loff_t isize;
  237. int error, page_nr;
  238. struct splice_pipe_desc spd = {
  239. .pages = pages,
  240. .partial = partial,
  241. .flags = flags,
  242. .ops = &page_cache_pipe_buf_ops,
  243. };
  244. index = *ppos >> PAGE_CACHE_SHIFT;
  245. loff = *ppos & ~PAGE_CACHE_MASK;
  246. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  247. nr_pages = min(req_pages, (unsigned)PIPE_BUFFERS);
  248. /*
  249. * Lookup the (hopefully) full range of pages we need.
  250. */
  251. spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages);
  252. index += spd.nr_pages;
  253. /*
  254. * If find_get_pages_contig() returned fewer pages than we needed,
  255. * readahead/allocate the rest and fill in the holes.
  256. */
  257. if (spd.nr_pages < nr_pages)
  258. page_cache_sync_readahead(mapping, &in->f_ra, in,
  259. index, req_pages - spd.nr_pages);
  260. error = 0;
  261. while (spd.nr_pages < nr_pages) {
  262. /*
  263. * Page could be there, find_get_pages_contig() breaks on
  264. * the first hole.
  265. */
  266. page = find_get_page(mapping, index);
  267. if (!page) {
  268. /*
  269. * page didn't exist, allocate one.
  270. */
  271. page = page_cache_alloc_cold(mapping);
  272. if (!page)
  273. break;
  274. error = add_to_page_cache_lru(page, mapping, index,
  275. GFP_KERNEL);
  276. if (unlikely(error)) {
  277. page_cache_release(page);
  278. if (error == -EEXIST)
  279. continue;
  280. break;
  281. }
  282. /*
  283. * add_to_page_cache() locks the page, unlock it
  284. * to avoid convoluting the logic below even more.
  285. */
  286. unlock_page(page);
  287. }
  288. pages[spd.nr_pages++] = page;
  289. index++;
  290. }
  291. /*
  292. * Now loop over the map and see if we need to start IO on any
  293. * pages, fill in the partial map, etc.
  294. */
  295. index = *ppos >> PAGE_CACHE_SHIFT;
  296. nr_pages = spd.nr_pages;
  297. spd.nr_pages = 0;
  298. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  299. unsigned int this_len;
  300. if (!len)
  301. break;
  302. /*
  303. * this_len is the max we'll use from this page
  304. */
  305. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  306. page = pages[page_nr];
  307. if (PageReadahead(page))
  308. page_cache_async_readahead(mapping, &in->f_ra, in,
  309. page, index, req_pages - page_nr);
  310. /*
  311. * If the page isn't uptodate, we may need to start io on it
  312. */
  313. if (!PageUptodate(page)) {
  314. /*
  315. * If in nonblock mode then dont block on waiting
  316. * for an in-flight io page
  317. */
  318. if (flags & SPLICE_F_NONBLOCK) {
  319. if (TestSetPageLocked(page))
  320. break;
  321. } else
  322. lock_page(page);
  323. /*
  324. * page was truncated, stop here. if this isn't the
  325. * first page, we'll just complete what we already
  326. * added
  327. */
  328. if (!page->mapping) {
  329. unlock_page(page);
  330. break;
  331. }
  332. /*
  333. * page was already under io and is now done, great
  334. */
  335. if (PageUptodate(page)) {
  336. unlock_page(page);
  337. goto fill_it;
  338. }
  339. /*
  340. * need to read in the page
  341. */
  342. error = mapping->a_ops->readpage(in, page);
  343. if (unlikely(error)) {
  344. /*
  345. * We really should re-lookup the page here,
  346. * but it complicates things a lot. Instead
  347. * lets just do what we already stored, and
  348. * we'll get it the next time we are called.
  349. */
  350. if (error == AOP_TRUNCATED_PAGE)
  351. error = 0;
  352. break;
  353. }
  354. }
  355. fill_it:
  356. /*
  357. * i_size must be checked after PageUptodate.
  358. */
  359. isize = i_size_read(mapping->host);
  360. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  361. if (unlikely(!isize || index > end_index))
  362. break;
  363. /*
  364. * if this is the last page, see if we need to shrink
  365. * the length and stop
  366. */
  367. if (end_index == index) {
  368. unsigned int plen;
  369. /*
  370. * max good bytes in this page
  371. */
  372. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  373. if (plen <= loff)
  374. break;
  375. /*
  376. * force quit after adding this page
  377. */
  378. this_len = min(this_len, plen - loff);
  379. len = this_len;
  380. }
  381. partial[page_nr].offset = loff;
  382. partial[page_nr].len = this_len;
  383. len -= this_len;
  384. loff = 0;
  385. spd.nr_pages++;
  386. index++;
  387. }
  388. /*
  389. * Release any pages at the end, if we quit early. 'page_nr' is how far
  390. * we got, 'nr_pages' is how many pages are in the map.
  391. */
  392. while (page_nr < nr_pages)
  393. page_cache_release(pages[page_nr++]);
  394. in->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT;
  395. if (spd.nr_pages)
  396. return splice_to_pipe(pipe, &spd);
  397. return error;
  398. }
  399. /**
  400. * generic_file_splice_read - splice data from file to a pipe
  401. * @in: file to splice from
  402. * @ppos: position in @in
  403. * @pipe: pipe to splice to
  404. * @len: number of bytes to splice
  405. * @flags: splice modifier flags
  406. *
  407. * Description:
  408. * Will read pages from given file and fill them into a pipe. Can be
  409. * used as long as the address_space operations for the source implements
  410. * a readpage() hook.
  411. *
  412. */
  413. ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
  414. struct pipe_inode_info *pipe, size_t len,
  415. unsigned int flags)
  416. {
  417. ssize_t spliced;
  418. int ret;
  419. loff_t isize, left;
  420. isize = i_size_read(in->f_mapping->host);
  421. if (unlikely(*ppos >= isize))
  422. return 0;
  423. left = isize - *ppos;
  424. if (unlikely(left < len))
  425. len = left;
  426. ret = 0;
  427. spliced = 0;
  428. while (len && !spliced) {
  429. ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
  430. if (ret < 0)
  431. break;
  432. else if (!ret) {
  433. if (spliced)
  434. break;
  435. if (flags & SPLICE_F_NONBLOCK) {
  436. ret = -EAGAIN;
  437. break;
  438. }
  439. }
  440. *ppos += ret;
  441. len -= ret;
  442. spliced += ret;
  443. }
  444. if (spliced)
  445. return spliced;
  446. return ret;
  447. }
  448. EXPORT_SYMBOL(generic_file_splice_read);
  449. /*
  450. * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
  451. * using sendpage(). Return the number of bytes sent.
  452. */
  453. static int pipe_to_sendpage(struct pipe_inode_info *pipe,
  454. struct pipe_buffer *buf, struct splice_desc *sd)
  455. {
  456. struct file *file = sd->u.file;
  457. loff_t pos = sd->pos;
  458. int ret, more;
  459. ret = buf->ops->confirm(pipe, buf);
  460. if (!ret) {
  461. more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
  462. ret = file->f_op->sendpage(file, buf->page, buf->offset,
  463. sd->len, &pos, more);
  464. }
  465. return ret;
  466. }
  467. /*
  468. * This is a little more tricky than the file -> pipe splicing. There are
  469. * basically three cases:
  470. *
  471. * - Destination page already exists in the address space and there
  472. * are users of it. For that case we have no other option that
  473. * copying the data. Tough luck.
  474. * - Destination page already exists in the address space, but there
  475. * are no users of it. Make sure it's uptodate, then drop it. Fall
  476. * through to last case.
  477. * - Destination page does not exist, we can add the pipe page to
  478. * the page cache and avoid the copy.
  479. *
  480. * If asked to move pages to the output file (SPLICE_F_MOVE is set in
  481. * sd->flags), we attempt to migrate pages from the pipe to the output
  482. * file address space page cache. This is possible if no one else has
  483. * the pipe page referenced outside of the pipe and page cache. If
  484. * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
  485. * a new page in the output file page cache and fill/dirty that.
  486. */
  487. static int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  488. struct splice_desc *sd)
  489. {
  490. struct file *file = sd->u.file;
  491. struct address_space *mapping = file->f_mapping;
  492. unsigned int offset, this_len;
  493. struct page *page;
  494. void *fsdata;
  495. int ret;
  496. /*
  497. * make sure the data in this buffer is uptodate
  498. */
  499. ret = buf->ops->confirm(pipe, buf);
  500. if (unlikely(ret))
  501. return ret;
  502. offset = sd->pos & ~PAGE_CACHE_MASK;
  503. this_len = sd->len;
  504. if (this_len + offset > PAGE_CACHE_SIZE)
  505. this_len = PAGE_CACHE_SIZE - offset;
  506. ret = pagecache_write_begin(file, mapping, sd->pos, this_len,
  507. AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
  508. if (unlikely(ret))
  509. goto out;
  510. if (buf->page != page) {
  511. /*
  512. * Careful, ->map() uses KM_USER0!
  513. */
  514. char *src = buf->ops->map(pipe, buf, 1);
  515. char *dst = kmap_atomic(page, KM_USER1);
  516. memcpy(dst + offset, src + buf->offset, this_len);
  517. flush_dcache_page(page);
  518. kunmap_atomic(dst, KM_USER1);
  519. buf->ops->unmap(pipe, buf, src);
  520. }
  521. ret = pagecache_write_end(file, mapping, sd->pos, this_len, this_len,
  522. page, fsdata);
  523. out:
  524. return ret;
  525. }
  526. /**
  527. * __splice_from_pipe - splice data from a pipe to given actor
  528. * @pipe: pipe to splice from
  529. * @sd: information to @actor
  530. * @actor: handler that splices the data
  531. *
  532. * Description:
  533. * This function does little more than loop over the pipe and call
  534. * @actor to do the actual moving of a single struct pipe_buffer to
  535. * the desired destination. See pipe_to_file, pipe_to_sendpage, or
  536. * pipe_to_user.
  537. *
  538. */
  539. ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
  540. splice_actor *actor)
  541. {
  542. int ret, do_wakeup, err;
  543. ret = 0;
  544. do_wakeup = 0;
  545. for (;;) {
  546. if (pipe->nrbufs) {
  547. struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  548. const struct pipe_buf_operations *ops = buf->ops;
  549. sd->len = buf->len;
  550. if (sd->len > sd->total_len)
  551. sd->len = sd->total_len;
  552. err = actor(pipe, buf, sd);
  553. if (err <= 0) {
  554. if (!ret && err != -ENODATA)
  555. ret = err;
  556. break;
  557. }
  558. ret += err;
  559. buf->offset += err;
  560. buf->len -= err;
  561. sd->len -= err;
  562. sd->pos += err;
  563. sd->total_len -= err;
  564. if (sd->len)
  565. continue;
  566. if (!buf->len) {
  567. buf->ops = NULL;
  568. ops->release(pipe, buf);
  569. pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
  570. pipe->nrbufs--;
  571. if (pipe->inode)
  572. do_wakeup = 1;
  573. }
  574. if (!sd->total_len)
  575. break;
  576. }
  577. if (pipe->nrbufs)
  578. continue;
  579. if (!pipe->writers)
  580. break;
  581. if (!pipe->waiting_writers) {
  582. if (ret)
  583. break;
  584. }
  585. if (sd->flags & SPLICE_F_NONBLOCK) {
  586. if (!ret)
  587. ret = -EAGAIN;
  588. break;
  589. }
  590. if (signal_pending(current)) {
  591. if (!ret)
  592. ret = -ERESTARTSYS;
  593. break;
  594. }
  595. if (do_wakeup) {
  596. smp_mb();
  597. if (waitqueue_active(&pipe->wait))
  598. wake_up_interruptible_sync(&pipe->wait);
  599. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  600. do_wakeup = 0;
  601. }
  602. pipe_wait(pipe);
  603. }
  604. if (do_wakeup) {
  605. smp_mb();
  606. if (waitqueue_active(&pipe->wait))
  607. wake_up_interruptible(&pipe->wait);
  608. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  609. }
  610. return ret;
  611. }
  612. EXPORT_SYMBOL(__splice_from_pipe);
  613. /**
  614. * splice_from_pipe - splice data from a pipe to a file
  615. * @pipe: pipe to splice from
  616. * @out: file to splice to
  617. * @ppos: position in @out
  618. * @len: how many bytes to splice
  619. * @flags: splice modifier flags
  620. * @actor: handler that splices the data
  621. *
  622. * Description:
  623. * See __splice_from_pipe. This function locks the input and output inodes,
  624. * otherwise it's identical to __splice_from_pipe().
  625. *
  626. */
  627. ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
  628. loff_t *ppos, size_t len, unsigned int flags,
  629. splice_actor *actor)
  630. {
  631. ssize_t ret;
  632. struct inode *inode = out->f_mapping->host;
  633. struct splice_desc sd = {
  634. .total_len = len,
  635. .flags = flags,
  636. .pos = *ppos,
  637. .u.file = out,
  638. };
  639. /*
  640. * The actor worker might be calling ->prepare_write and
  641. * ->commit_write. Most of the time, these expect i_mutex to
  642. * be held. Since this may result in an ABBA deadlock with
  643. * pipe->inode, we have to order lock acquiry here.
  644. */
  645. inode_double_lock(inode, pipe->inode);
  646. ret = __splice_from_pipe(pipe, &sd, actor);
  647. inode_double_unlock(inode, pipe->inode);
  648. return ret;
  649. }
  650. /**
  651. * generic_file_splice_write_nolock - generic_file_splice_write without mutexes
  652. * @pipe: pipe info
  653. * @out: file to write to
  654. * @ppos: position in @out
  655. * @len: number of bytes to splice
  656. * @flags: splice modifier flags
  657. *
  658. * Description:
  659. * Will either move or copy pages (determined by @flags options) from
  660. * the given pipe inode to the given file. The caller is responsible
  661. * for acquiring i_mutex on both inodes.
  662. *
  663. */
  664. ssize_t
  665. generic_file_splice_write_nolock(struct pipe_inode_info *pipe, struct file *out,
  666. loff_t *ppos, size_t len, unsigned int flags)
  667. {
  668. struct address_space *mapping = out->f_mapping;
  669. struct inode *inode = mapping->host;
  670. struct splice_desc sd = {
  671. .total_len = len,
  672. .flags = flags,
  673. .pos = *ppos,
  674. .u.file = out,
  675. };
  676. ssize_t ret;
  677. int err;
  678. err = remove_suid(out->f_path.dentry);
  679. if (unlikely(err))
  680. return err;
  681. ret = __splice_from_pipe(pipe, &sd, pipe_to_file);
  682. if (ret > 0) {
  683. unsigned long nr_pages;
  684. *ppos += ret;
  685. nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  686. /*
  687. * If file or inode is SYNC and we actually wrote some data,
  688. * sync it.
  689. */
  690. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  691. err = generic_osync_inode(inode, mapping,
  692. OSYNC_METADATA|OSYNC_DATA);
  693. if (err)
  694. ret = err;
  695. }
  696. balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
  697. }
  698. return ret;
  699. }
  700. EXPORT_SYMBOL(generic_file_splice_write_nolock);
  701. /**
  702. * generic_file_splice_write - splice data from a pipe to a file
  703. * @pipe: pipe info
  704. * @out: file to write to
  705. * @ppos: position in @out
  706. * @len: number of bytes to splice
  707. * @flags: splice modifier flags
  708. *
  709. * Description:
  710. * Will either move or copy pages (determined by @flags options) from
  711. * the given pipe inode to the given file.
  712. *
  713. */
  714. ssize_t
  715. generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  716. loff_t *ppos, size_t len, unsigned int flags)
  717. {
  718. struct address_space *mapping = out->f_mapping;
  719. struct inode *inode = mapping->host;
  720. int killsuid, killpriv;
  721. ssize_t ret;
  722. int err = 0;
  723. killpriv = security_inode_need_killpriv(out->f_path.dentry);
  724. killsuid = should_remove_suid(out->f_path.dentry);
  725. if (unlikely(killsuid || killpriv)) {
  726. mutex_lock(&inode->i_mutex);
  727. if (killpriv)
  728. err = security_inode_killpriv(out->f_path.dentry);
  729. if (!err && killsuid)
  730. err = __remove_suid(out->f_path.dentry, killsuid);
  731. mutex_unlock(&inode->i_mutex);
  732. if (err)
  733. return err;
  734. }
  735. ret = splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_file);
  736. if (ret > 0) {
  737. unsigned long nr_pages;
  738. *ppos += ret;
  739. nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  740. /*
  741. * If file or inode is SYNC and we actually wrote some data,
  742. * sync it.
  743. */
  744. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  745. mutex_lock(&inode->i_mutex);
  746. err = generic_osync_inode(inode, mapping,
  747. OSYNC_METADATA|OSYNC_DATA);
  748. mutex_unlock(&inode->i_mutex);
  749. if (err)
  750. ret = err;
  751. }
  752. balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
  753. }
  754. return ret;
  755. }
  756. EXPORT_SYMBOL(generic_file_splice_write);
  757. /**
  758. * generic_splice_sendpage - splice data from a pipe to a socket
  759. * @pipe: pipe to splice from
  760. * @out: socket to write to
  761. * @ppos: position in @out
  762. * @len: number of bytes to splice
  763. * @flags: splice modifier flags
  764. *
  765. * Description:
  766. * Will send @len bytes from the pipe to a network socket. No data copying
  767. * is involved.
  768. *
  769. */
  770. ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
  771. loff_t *ppos, size_t len, unsigned int flags)
  772. {
  773. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
  774. }
  775. EXPORT_SYMBOL(generic_splice_sendpage);
  776. /*
  777. * Attempt to initiate a splice from pipe to file.
  778. */
  779. static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  780. loff_t *ppos, size_t len, unsigned int flags)
  781. {
  782. int ret;
  783. if (unlikely(!out->f_op || !out->f_op->splice_write))
  784. return -EINVAL;
  785. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  786. return -EBADF;
  787. ret = rw_verify_area(WRITE, out, ppos, len);
  788. if (unlikely(ret < 0))
  789. return ret;
  790. ret = security_file_permission(out, MAY_WRITE);
  791. if (unlikely(ret < 0))
  792. return ret;
  793. return out->f_op->splice_write(pipe, out, ppos, len, flags);
  794. }
  795. /*
  796. * Attempt to initiate a splice from a file to a pipe.
  797. */
  798. static long do_splice_to(struct file *in, loff_t *ppos,
  799. struct pipe_inode_info *pipe, size_t len,
  800. unsigned int flags)
  801. {
  802. int ret;
  803. if (unlikely(!in->f_op || !in->f_op->splice_read))
  804. return -EINVAL;
  805. if (unlikely(!(in->f_mode & FMODE_READ)))
  806. return -EBADF;
  807. ret = rw_verify_area(READ, in, ppos, len);
  808. if (unlikely(ret < 0))
  809. return ret;
  810. ret = security_file_permission(in, MAY_READ);
  811. if (unlikely(ret < 0))
  812. return ret;
  813. return in->f_op->splice_read(in, ppos, pipe, len, flags);
  814. }
  815. /**
  816. * splice_direct_to_actor - splices data directly between two non-pipes
  817. * @in: file to splice from
  818. * @sd: actor information on where to splice to
  819. * @actor: handles the data splicing
  820. *
  821. * Description:
  822. * This is a special case helper to splice directly between two
  823. * points, without requiring an explicit pipe. Internally an allocated
  824. * pipe is cached in the process, and reused during the lifetime of
  825. * that process.
  826. *
  827. */
  828. ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
  829. splice_direct_actor *actor)
  830. {
  831. struct pipe_inode_info *pipe;
  832. long ret, bytes;
  833. umode_t i_mode;
  834. size_t len;
  835. int i, flags;
  836. /*
  837. * We require the input being a regular file, as we don't want to
  838. * randomly drop data for eg socket -> socket splicing. Use the
  839. * piped splicing for that!
  840. */
  841. i_mode = in->f_path.dentry->d_inode->i_mode;
  842. if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  843. return -EINVAL;
  844. /*
  845. * neither in nor out is a pipe, setup an internal pipe attached to
  846. * 'out' and transfer the wanted data from 'in' to 'out' through that
  847. */
  848. pipe = current->splice_pipe;
  849. if (unlikely(!pipe)) {
  850. pipe = alloc_pipe_info(NULL);
  851. if (!pipe)
  852. return -ENOMEM;
  853. /*
  854. * We don't have an immediate reader, but we'll read the stuff
  855. * out of the pipe right after the splice_to_pipe(). So set
  856. * PIPE_READERS appropriately.
  857. */
  858. pipe->readers = 1;
  859. current->splice_pipe = pipe;
  860. }
  861. /*
  862. * Do the splice.
  863. */
  864. ret = 0;
  865. bytes = 0;
  866. len = sd->total_len;
  867. flags = sd->flags;
  868. /*
  869. * Don't block on output, we have to drain the direct pipe.
  870. */
  871. sd->flags &= ~SPLICE_F_NONBLOCK;
  872. while (len) {
  873. size_t read_len;
  874. loff_t pos = sd->pos;
  875. ret = do_splice_to(in, &pos, pipe, len, flags);
  876. if (unlikely(ret <= 0))
  877. goto out_release;
  878. read_len = ret;
  879. sd->total_len = read_len;
  880. /*
  881. * NOTE: nonblocking mode only applies to the input. We
  882. * must not do the output in nonblocking mode as then we
  883. * could get stuck data in the internal pipe:
  884. */
  885. ret = actor(pipe, sd);
  886. if (unlikely(ret <= 0))
  887. goto out_release;
  888. bytes += ret;
  889. len -= ret;
  890. sd->pos = pos;
  891. if (ret < read_len)
  892. goto out_release;
  893. }
  894. pipe->nrbufs = pipe->curbuf = 0;
  895. return bytes;
  896. out_release:
  897. /*
  898. * If we did an incomplete transfer we must release
  899. * the pipe buffers in question:
  900. */
  901. for (i = 0; i < PIPE_BUFFERS; i++) {
  902. struct pipe_buffer *buf = pipe->bufs + i;
  903. if (buf->ops) {
  904. buf->ops->release(pipe, buf);
  905. buf->ops = NULL;
  906. }
  907. }
  908. pipe->nrbufs = pipe->curbuf = 0;
  909. /*
  910. * If we transferred some data, return the number of bytes:
  911. */
  912. if (bytes > 0)
  913. return bytes;
  914. return ret;
  915. }
  916. EXPORT_SYMBOL(splice_direct_to_actor);
  917. static int direct_splice_actor(struct pipe_inode_info *pipe,
  918. struct splice_desc *sd)
  919. {
  920. struct file *file = sd->u.file;
  921. return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
  922. }
  923. /**
  924. * do_splice_direct - splices data directly between two files
  925. * @in: file to splice from
  926. * @ppos: input file offset
  927. * @out: file to splice to
  928. * @len: number of bytes to splice
  929. * @flags: splice modifier flags
  930. *
  931. * Description:
  932. * For use by do_sendfile(). splice can easily emulate sendfile, but
  933. * doing it in the application would incur an extra system call
  934. * (splice in + splice out, as compared to just sendfile()). So this helper
  935. * can splice directly through a process-private pipe.
  936. *
  937. */
  938. long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
  939. size_t len, unsigned int flags)
  940. {
  941. struct splice_desc sd = {
  942. .len = len,
  943. .total_len = len,
  944. .flags = flags,
  945. .pos = *ppos,
  946. .u.file = out,
  947. };
  948. long ret;
  949. ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
  950. if (ret > 0)
  951. *ppos += ret;
  952. return ret;
  953. }
  954. /*
  955. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  956. * location, so checking ->i_pipe is not enough to verify that this is a
  957. * pipe.
  958. */
  959. static inline struct pipe_inode_info *pipe_info(struct inode *inode)
  960. {
  961. if (S_ISFIFO(inode->i_mode))
  962. return inode->i_pipe;
  963. return NULL;
  964. }
  965. /*
  966. * Determine where to splice to/from.
  967. */
  968. static long do_splice(struct file *in, loff_t __user *off_in,
  969. struct file *out, loff_t __user *off_out,
  970. size_t len, unsigned int flags)
  971. {
  972. struct pipe_inode_info *pipe;
  973. loff_t offset, *off;
  974. long ret;
  975. pipe = pipe_info(in->f_path.dentry->d_inode);
  976. if (pipe) {
  977. if (off_in)
  978. return -ESPIPE;
  979. if (off_out) {
  980. if (out->f_op->llseek == no_llseek)
  981. return -EINVAL;
  982. if (copy_from_user(&offset, off_out, sizeof(loff_t)))
  983. return -EFAULT;
  984. off = &offset;
  985. } else
  986. off = &out->f_pos;
  987. ret = do_splice_from(pipe, out, off, len, flags);
  988. if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
  989. ret = -EFAULT;
  990. return ret;
  991. }
  992. pipe = pipe_info(out->f_path.dentry->d_inode);
  993. if (pipe) {
  994. if (off_out)
  995. return -ESPIPE;
  996. if (off_in) {
  997. if (in->f_op->llseek == no_llseek)
  998. return -EINVAL;
  999. if (copy_from_user(&offset, off_in, sizeof(loff_t)))
  1000. return -EFAULT;
  1001. off = &offset;
  1002. } else
  1003. off = &in->f_pos;
  1004. ret = do_splice_to(in, off, pipe, len, flags);
  1005. if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
  1006. ret = -EFAULT;
  1007. return ret;
  1008. }
  1009. return -EINVAL;
  1010. }
  1011. /*
  1012. * Do a copy-from-user while holding the mmap_semaphore for reading, in a
  1013. * manner safe from deadlocking with simultaneous mmap() (grabbing mmap_sem
  1014. * for writing) and page faulting on the user memory pointed to by src.
  1015. * This assumes that we will very rarely hit the partial != 0 path, or this
  1016. * will not be a win.
  1017. */
  1018. static int copy_from_user_mmap_sem(void *dst, const void __user *src, size_t n)
  1019. {
  1020. int partial;
  1021. pagefault_disable();
  1022. partial = __copy_from_user_inatomic(dst, src, n);
  1023. pagefault_enable();
  1024. /*
  1025. * Didn't copy everything, drop the mmap_sem and do a faulting copy
  1026. */
  1027. if (unlikely(partial)) {
  1028. up_read(&current->mm->mmap_sem);
  1029. partial = copy_from_user(dst, src, n);
  1030. down_read(&current->mm->mmap_sem);
  1031. }
  1032. return partial;
  1033. }
  1034. /*
  1035. * Map an iov into an array of pages and offset/length tupples. With the
  1036. * partial_page structure, we can map several non-contiguous ranges into
  1037. * our ones pages[] map instead of splitting that operation into pieces.
  1038. * Could easily be exported as a generic helper for other users, in which
  1039. * case one would probably want to add a 'max_nr_pages' parameter as well.
  1040. */
  1041. static int get_iovec_page_array(const struct iovec __user *iov,
  1042. unsigned int nr_vecs, struct page **pages,
  1043. struct partial_page *partial, int aligned)
  1044. {
  1045. int buffers = 0, error = 0;
  1046. down_read(&current->mm->mmap_sem);
  1047. while (nr_vecs) {
  1048. unsigned long off, npages;
  1049. struct iovec entry;
  1050. void __user *base;
  1051. size_t len;
  1052. int i;
  1053. error = -EFAULT;
  1054. if (copy_from_user_mmap_sem(&entry, iov, sizeof(entry)))
  1055. break;
  1056. base = entry.iov_base;
  1057. len = entry.iov_len;
  1058. /*
  1059. * Sanity check this iovec. 0 read succeeds.
  1060. */
  1061. error = 0;
  1062. if (unlikely(!len))
  1063. break;
  1064. error = -EFAULT;
  1065. if (unlikely(!base))
  1066. break;
  1067. /*
  1068. * Get this base offset and number of pages, then map
  1069. * in the user pages.
  1070. */
  1071. off = (unsigned long) base & ~PAGE_MASK;
  1072. /*
  1073. * If asked for alignment, the offset must be zero and the
  1074. * length a multiple of the PAGE_SIZE.
  1075. */
  1076. error = -EINVAL;
  1077. if (aligned && (off || len & ~PAGE_MASK))
  1078. break;
  1079. npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1080. if (npages > PIPE_BUFFERS - buffers)
  1081. npages = PIPE_BUFFERS - buffers;
  1082. error = get_user_pages(current, current->mm,
  1083. (unsigned long) base, npages, 0, 0,
  1084. &pages[buffers], NULL);
  1085. if (unlikely(error <= 0))
  1086. break;
  1087. /*
  1088. * Fill this contiguous range into the partial page map.
  1089. */
  1090. for (i = 0; i < error; i++) {
  1091. const int plen = min_t(size_t, len, PAGE_SIZE - off);
  1092. partial[buffers].offset = off;
  1093. partial[buffers].len = plen;
  1094. off = 0;
  1095. len -= plen;
  1096. buffers++;
  1097. }
  1098. /*
  1099. * We didn't complete this iov, stop here since it probably
  1100. * means we have to move some of this into a pipe to
  1101. * be able to continue.
  1102. */
  1103. if (len)
  1104. break;
  1105. /*
  1106. * Don't continue if we mapped fewer pages than we asked for,
  1107. * or if we mapped the max number of pages that we have
  1108. * room for.
  1109. */
  1110. if (error < npages || buffers == PIPE_BUFFERS)
  1111. break;
  1112. nr_vecs--;
  1113. iov++;
  1114. }
  1115. up_read(&current->mm->mmap_sem);
  1116. if (buffers)
  1117. return buffers;
  1118. return error;
  1119. }
  1120. static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  1121. struct splice_desc *sd)
  1122. {
  1123. char *src;
  1124. int ret;
  1125. ret = buf->ops->confirm(pipe, buf);
  1126. if (unlikely(ret))
  1127. return ret;
  1128. /*
  1129. * See if we can use the atomic maps, by prefaulting in the
  1130. * pages and doing an atomic copy
  1131. */
  1132. if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) {
  1133. src = buf->ops->map(pipe, buf, 1);
  1134. ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset,
  1135. sd->len);
  1136. buf->ops->unmap(pipe, buf, src);
  1137. if (!ret) {
  1138. ret = sd->len;
  1139. goto out;
  1140. }
  1141. }
  1142. /*
  1143. * No dice, use slow non-atomic map and copy
  1144. */
  1145. src = buf->ops->map(pipe, buf, 0);
  1146. ret = sd->len;
  1147. if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len))
  1148. ret = -EFAULT;
  1149. buf->ops->unmap(pipe, buf, src);
  1150. out:
  1151. if (ret > 0)
  1152. sd->u.userptr += ret;
  1153. return ret;
  1154. }
  1155. /*
  1156. * For lack of a better implementation, implement vmsplice() to userspace
  1157. * as a simple copy of the pipes pages to the user iov.
  1158. */
  1159. static long vmsplice_to_user(struct file *file, const struct iovec __user *iov,
  1160. unsigned long nr_segs, unsigned int flags)
  1161. {
  1162. struct pipe_inode_info *pipe;
  1163. struct splice_desc sd;
  1164. ssize_t size;
  1165. int error;
  1166. long ret;
  1167. pipe = pipe_info(file->f_path.dentry->d_inode);
  1168. if (!pipe)
  1169. return -EBADF;
  1170. if (pipe->inode)
  1171. mutex_lock(&pipe->inode->i_mutex);
  1172. error = ret = 0;
  1173. while (nr_segs) {
  1174. void __user *base;
  1175. size_t len;
  1176. /*
  1177. * Get user address base and length for this iovec.
  1178. */
  1179. error = get_user(base, &iov->iov_base);
  1180. if (unlikely(error))
  1181. break;
  1182. error = get_user(len, &iov->iov_len);
  1183. if (unlikely(error))
  1184. break;
  1185. /*
  1186. * Sanity check this iovec. 0 read succeeds.
  1187. */
  1188. if (unlikely(!len))
  1189. break;
  1190. if (unlikely(!base)) {
  1191. error = -EFAULT;
  1192. break;
  1193. }
  1194. sd.len = 0;
  1195. sd.total_len = len;
  1196. sd.flags = flags;
  1197. sd.u.userptr = base;
  1198. sd.pos = 0;
  1199. size = __splice_from_pipe(pipe, &sd, pipe_to_user);
  1200. if (size < 0) {
  1201. if (!ret)
  1202. ret = size;
  1203. break;
  1204. }
  1205. ret += size;
  1206. if (size < len)
  1207. break;
  1208. nr_segs--;
  1209. iov++;
  1210. }
  1211. if (pipe->inode)
  1212. mutex_unlock(&pipe->inode->i_mutex);
  1213. if (!ret)
  1214. ret = error;
  1215. return ret;
  1216. }
  1217. /*
  1218. * vmsplice splices a user address range into a pipe. It can be thought of
  1219. * as splice-from-memory, where the regular splice is splice-from-file (or
  1220. * to file). In both cases the output is a pipe, naturally.
  1221. */
  1222. static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
  1223. unsigned long nr_segs, unsigned int flags)
  1224. {
  1225. struct pipe_inode_info *pipe;
  1226. struct page *pages[PIPE_BUFFERS];
  1227. struct partial_page partial[PIPE_BUFFERS];
  1228. struct splice_pipe_desc spd = {
  1229. .pages = pages,
  1230. .partial = partial,
  1231. .flags = flags,
  1232. .ops = &user_page_pipe_buf_ops,
  1233. };
  1234. pipe = pipe_info(file->f_path.dentry->d_inode);
  1235. if (!pipe)
  1236. return -EBADF;
  1237. spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
  1238. flags & SPLICE_F_GIFT);
  1239. if (spd.nr_pages <= 0)
  1240. return spd.nr_pages;
  1241. return splice_to_pipe(pipe, &spd);
  1242. }
  1243. /*
  1244. * Note that vmsplice only really supports true splicing _from_ user memory
  1245. * to a pipe, not the other way around. Splicing from user memory is a simple
  1246. * operation that can be supported without any funky alignment restrictions
  1247. * or nasty vm tricks. We simply map in the user memory and fill them into
  1248. * a pipe. The reverse isn't quite as easy, though. There are two possible
  1249. * solutions for that:
  1250. *
  1251. * - memcpy() the data internally, at which point we might as well just
  1252. * do a regular read() on the buffer anyway.
  1253. * - Lots of nasty vm tricks, that are neither fast nor flexible (it
  1254. * has restriction limitations on both ends of the pipe).
  1255. *
  1256. * Currently we punt and implement it as a normal copy, see pipe_to_user().
  1257. *
  1258. */
  1259. asmlinkage long sys_vmsplice(int fd, const struct iovec __user *iov,
  1260. unsigned long nr_segs, unsigned int flags)
  1261. {
  1262. struct file *file;
  1263. long error;
  1264. int fput;
  1265. if (unlikely(nr_segs > UIO_MAXIOV))
  1266. return -EINVAL;
  1267. else if (unlikely(!nr_segs))
  1268. return 0;
  1269. error = -EBADF;
  1270. file = fget_light(fd, &fput);
  1271. if (file) {
  1272. if (file->f_mode & FMODE_WRITE)
  1273. error = vmsplice_to_pipe(file, iov, nr_segs, flags);
  1274. else if (file->f_mode & FMODE_READ)
  1275. error = vmsplice_to_user(file, iov, nr_segs, flags);
  1276. fput_light(file, fput);
  1277. }
  1278. return error;
  1279. }
  1280. asmlinkage long sys_splice(int fd_in, loff_t __user *off_in,
  1281. int fd_out, loff_t __user *off_out,
  1282. size_t len, unsigned int flags)
  1283. {
  1284. long error;
  1285. struct file *in, *out;
  1286. int fput_in, fput_out;
  1287. if (unlikely(!len))
  1288. return 0;
  1289. error = -EBADF;
  1290. in = fget_light(fd_in, &fput_in);
  1291. if (in) {
  1292. if (in->f_mode & FMODE_READ) {
  1293. out = fget_light(fd_out, &fput_out);
  1294. if (out) {
  1295. if (out->f_mode & FMODE_WRITE)
  1296. error = do_splice(in, off_in,
  1297. out, off_out,
  1298. len, flags);
  1299. fput_light(out, fput_out);
  1300. }
  1301. }
  1302. fput_light(in, fput_in);
  1303. }
  1304. return error;
  1305. }
  1306. /*
  1307. * Make sure there's data to read. Wait for input if we can, otherwise
  1308. * return an appropriate error.
  1309. */
  1310. static int link_ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1311. {
  1312. int ret;
  1313. /*
  1314. * Check ->nrbufs without the inode lock first. This function
  1315. * is speculative anyways, so missing one is ok.
  1316. */
  1317. if (pipe->nrbufs)
  1318. return 0;
  1319. ret = 0;
  1320. mutex_lock(&pipe->inode->i_mutex);
  1321. while (!pipe->nrbufs) {
  1322. if (signal_pending(current)) {
  1323. ret = -ERESTARTSYS;
  1324. break;
  1325. }
  1326. if (!pipe->writers)
  1327. break;
  1328. if (!pipe->waiting_writers) {
  1329. if (flags & SPLICE_F_NONBLOCK) {
  1330. ret = -EAGAIN;
  1331. break;
  1332. }
  1333. }
  1334. pipe_wait(pipe);
  1335. }
  1336. mutex_unlock(&pipe->inode->i_mutex);
  1337. return ret;
  1338. }
  1339. /*
  1340. * Make sure there's writeable room. Wait for room if we can, otherwise
  1341. * return an appropriate error.
  1342. */
  1343. static int link_opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1344. {
  1345. int ret;
  1346. /*
  1347. * Check ->nrbufs without the inode lock first. This function
  1348. * is speculative anyways, so missing one is ok.
  1349. */
  1350. if (pipe->nrbufs < PIPE_BUFFERS)
  1351. return 0;
  1352. ret = 0;
  1353. mutex_lock(&pipe->inode->i_mutex);
  1354. while (pipe->nrbufs >= PIPE_BUFFERS) {
  1355. if (!pipe->readers) {
  1356. send_sig(SIGPIPE, current, 0);
  1357. ret = -EPIPE;
  1358. break;
  1359. }
  1360. if (flags & SPLICE_F_NONBLOCK) {
  1361. ret = -EAGAIN;
  1362. break;
  1363. }
  1364. if (signal_pending(current)) {
  1365. ret = -ERESTARTSYS;
  1366. break;
  1367. }
  1368. pipe->waiting_writers++;
  1369. pipe_wait(pipe);
  1370. pipe->waiting_writers--;
  1371. }
  1372. mutex_unlock(&pipe->inode->i_mutex);
  1373. return ret;
  1374. }
  1375. /*
  1376. * Link contents of ipipe to opipe.
  1377. */
  1378. static int link_pipe(struct pipe_inode_info *ipipe,
  1379. struct pipe_inode_info *opipe,
  1380. size_t len, unsigned int flags)
  1381. {
  1382. struct pipe_buffer *ibuf, *obuf;
  1383. int ret = 0, i = 0, nbuf;
  1384. /*
  1385. * Potential ABBA deadlock, work around it by ordering lock
  1386. * grabbing by inode address. Otherwise two different processes
  1387. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1388. */
  1389. inode_double_lock(ipipe->inode, opipe->inode);
  1390. do {
  1391. if (!opipe->readers) {
  1392. send_sig(SIGPIPE, current, 0);
  1393. if (!ret)
  1394. ret = -EPIPE;
  1395. break;
  1396. }
  1397. /*
  1398. * If we have iterated all input buffers or ran out of
  1399. * output room, break.
  1400. */
  1401. if (i >= ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS)
  1402. break;
  1403. ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
  1404. nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
  1405. /*
  1406. * Get a reference to this pipe buffer,
  1407. * so we can copy the contents over.
  1408. */
  1409. ibuf->ops->get(ipipe, ibuf);
  1410. obuf = opipe->bufs + nbuf;
  1411. *obuf = *ibuf;
  1412. /*
  1413. * Don't inherit the gift flag, we need to
  1414. * prevent multiple steals of this page.
  1415. */
  1416. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1417. if (obuf->len > len)
  1418. obuf->len = len;
  1419. opipe->nrbufs++;
  1420. ret += obuf->len;
  1421. len -= obuf->len;
  1422. i++;
  1423. } while (len);
  1424. inode_double_unlock(ipipe->inode, opipe->inode);
  1425. /*
  1426. * If we put data in the output pipe, wakeup any potential readers.
  1427. */
  1428. if (ret > 0) {
  1429. smp_mb();
  1430. if (waitqueue_active(&opipe->wait))
  1431. wake_up_interruptible(&opipe->wait);
  1432. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  1433. }
  1434. return ret;
  1435. }
  1436. /*
  1437. * This is a tee(1) implementation that works on pipes. It doesn't copy
  1438. * any data, it simply references the 'in' pages on the 'out' pipe.
  1439. * The 'flags' used are the SPLICE_F_* variants, currently the only
  1440. * applicable one is SPLICE_F_NONBLOCK.
  1441. */
  1442. static long do_tee(struct file *in, struct file *out, size_t len,
  1443. unsigned int flags)
  1444. {
  1445. struct pipe_inode_info *ipipe = pipe_info(in->f_path.dentry->d_inode);
  1446. struct pipe_inode_info *opipe = pipe_info(out->f_path.dentry->d_inode);
  1447. int ret = -EINVAL;
  1448. /*
  1449. * Duplicate the contents of ipipe to opipe without actually
  1450. * copying the data.
  1451. */
  1452. if (ipipe && opipe && ipipe != opipe) {
  1453. /*
  1454. * Keep going, unless we encounter an error. The ipipe/opipe
  1455. * ordering doesn't really matter.
  1456. */
  1457. ret = link_ipipe_prep(ipipe, flags);
  1458. if (!ret) {
  1459. ret = link_opipe_prep(opipe, flags);
  1460. if (!ret) {
  1461. ret = link_pipe(ipipe, opipe, len, flags);
  1462. if (!ret && (flags & SPLICE_F_NONBLOCK))
  1463. ret = -EAGAIN;
  1464. }
  1465. }
  1466. }
  1467. return ret;
  1468. }
  1469. asmlinkage long sys_tee(int fdin, int fdout, size_t len, unsigned int flags)
  1470. {
  1471. struct file *in;
  1472. int error, fput_in;
  1473. if (unlikely(!len))
  1474. return 0;
  1475. error = -EBADF;
  1476. in = fget_light(fdin, &fput_in);
  1477. if (in) {
  1478. if (in->f_mode & FMODE_READ) {
  1479. int fput_out;
  1480. struct file *out = fget_light(fdout, &fput_out);
  1481. if (out) {
  1482. if (out->f_mode & FMODE_WRITE)
  1483. error = do_tee(in, out, len, flags);
  1484. fput_light(out, fput_out);
  1485. }
  1486. }
  1487. fput_light(in, fput_in);
  1488. }
  1489. return error;
  1490. }