splice.c 40 KB

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