splice.c 39 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. if (pipe->inode)
  165. mutex_lock(&pipe->inode->i_mutex);
  166. for (;;) {
  167. if (!pipe->readers) {
  168. send_sig(SIGPIPE, current, 0);
  169. if (!ret)
  170. ret = -EPIPE;
  171. break;
  172. }
  173. if (pipe->nrbufs < PIPE_BUFFERS) {
  174. int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1);
  175. struct pipe_buffer *buf = pipe->bufs + newbuf;
  176. buf->page = spd->pages[page_nr];
  177. buf->offset = spd->partial[page_nr].offset;
  178. buf->len = spd->partial[page_nr].len;
  179. buf->private = spd->partial[page_nr].private;
  180. buf->ops = spd->ops;
  181. if (spd->flags & SPLICE_F_GIFT)
  182. buf->flags |= PIPE_BUF_FLAG_GIFT;
  183. pipe->nrbufs++;
  184. page_nr++;
  185. ret += buf->len;
  186. if (pipe->inode)
  187. do_wakeup = 1;
  188. if (!--spd->nr_pages)
  189. break;
  190. if (pipe->nrbufs < PIPE_BUFFERS)
  191. continue;
  192. break;
  193. }
  194. if (spd->flags & SPLICE_F_NONBLOCK) {
  195. if (!ret)
  196. ret = -EAGAIN;
  197. break;
  198. }
  199. if (signal_pending(current)) {
  200. if (!ret)
  201. ret = -ERESTARTSYS;
  202. break;
  203. }
  204. if (do_wakeup) {
  205. smp_mb();
  206. if (waitqueue_active(&pipe->wait))
  207. wake_up_interruptible_sync(&pipe->wait);
  208. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  209. do_wakeup = 0;
  210. }
  211. pipe->waiting_writers++;
  212. pipe_wait(pipe);
  213. pipe->waiting_writers--;
  214. }
  215. if (pipe->inode) {
  216. mutex_unlock(&pipe->inode->i_mutex);
  217. if (do_wakeup) {
  218. smp_mb();
  219. if (waitqueue_active(&pipe->wait))
  220. wake_up_interruptible(&pipe->wait);
  221. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  222. }
  223. }
  224. while (page_nr < spd_pages)
  225. spd->spd_release(spd, page_nr++);
  226. return ret;
  227. }
  228. static void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
  229. {
  230. page_cache_release(spd->pages[i]);
  231. }
  232. static int
  233. __generic_file_splice_read(struct file *in, loff_t *ppos,
  234. struct pipe_inode_info *pipe, size_t len,
  235. unsigned int flags)
  236. {
  237. struct address_space *mapping = in->f_mapping;
  238. unsigned int loff, nr_pages, req_pages;
  239. struct page *pages[PIPE_BUFFERS];
  240. struct partial_page partial[PIPE_BUFFERS];
  241. struct page *page;
  242. pgoff_t index, end_index;
  243. loff_t isize;
  244. int error, page_nr;
  245. struct splice_pipe_desc spd = {
  246. .pages = pages,
  247. .partial = partial,
  248. .flags = flags,
  249. .ops = &page_cache_pipe_buf_ops,
  250. .spd_release = spd_release_page,
  251. };
  252. index = *ppos >> PAGE_CACHE_SHIFT;
  253. loff = *ppos & ~PAGE_CACHE_MASK;
  254. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  255. nr_pages = min(req_pages, (unsigned)PIPE_BUFFERS);
  256. /*
  257. * Lookup the (hopefully) full range of pages we need.
  258. */
  259. spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages);
  260. index += spd.nr_pages;
  261. /*
  262. * If find_get_pages_contig() returned fewer pages than we needed,
  263. * readahead/allocate the rest and fill in the holes.
  264. */
  265. if (spd.nr_pages < nr_pages)
  266. page_cache_sync_readahead(mapping, &in->f_ra, in,
  267. index, req_pages - spd.nr_pages);
  268. error = 0;
  269. while (spd.nr_pages < nr_pages) {
  270. /*
  271. * Page could be there, find_get_pages_contig() breaks on
  272. * the first hole.
  273. */
  274. page = find_get_page(mapping, index);
  275. if (!page) {
  276. /*
  277. * page didn't exist, allocate one.
  278. */
  279. page = page_cache_alloc_cold(mapping);
  280. if (!page)
  281. break;
  282. error = add_to_page_cache_lru(page, mapping, index,
  283. mapping_gfp_mask(mapping));
  284. if (unlikely(error)) {
  285. page_cache_release(page);
  286. if (error == -EEXIST)
  287. continue;
  288. break;
  289. }
  290. /*
  291. * add_to_page_cache() locks the page, unlock it
  292. * to avoid convoluting the logic below even more.
  293. */
  294. unlock_page(page);
  295. }
  296. pages[spd.nr_pages++] = page;
  297. index++;
  298. }
  299. /*
  300. * Now loop over the map and see if we need to start IO on any
  301. * pages, fill in the partial map, etc.
  302. */
  303. index = *ppos >> PAGE_CACHE_SHIFT;
  304. nr_pages = spd.nr_pages;
  305. spd.nr_pages = 0;
  306. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  307. unsigned int this_len;
  308. if (!len)
  309. break;
  310. /*
  311. * this_len is the max we'll use from this page
  312. */
  313. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  314. page = pages[page_nr];
  315. if (PageReadahead(page))
  316. page_cache_async_readahead(mapping, &in->f_ra, in,
  317. page, index, req_pages - page_nr);
  318. /*
  319. * If the page isn't uptodate, we may need to start io on it
  320. */
  321. if (!PageUptodate(page)) {
  322. /*
  323. * If in nonblock mode then dont block on waiting
  324. * for an in-flight io page
  325. */
  326. if (flags & SPLICE_F_NONBLOCK) {
  327. if (!trylock_page(page)) {
  328. error = -EAGAIN;
  329. break;
  330. }
  331. } else
  332. lock_page(page);
  333. /*
  334. * Page was truncated, or invalidated by the
  335. * filesystem. Redo the find/create, but this time the
  336. * page is kept locked, so there's no chance of another
  337. * race with truncate/invalidate.
  338. */
  339. if (!page->mapping) {
  340. unlock_page(page);
  341. page = find_or_create_page(mapping, index,
  342. mapping_gfp_mask(mapping));
  343. if (!page) {
  344. error = -ENOMEM;
  345. break;
  346. }
  347. page_cache_release(pages[page_nr]);
  348. pages[page_nr] = page;
  349. }
  350. /*
  351. * page was already under io and is now done, great
  352. */
  353. if (PageUptodate(page)) {
  354. unlock_page(page);
  355. goto fill_it;
  356. }
  357. /*
  358. * need to read in the page
  359. */
  360. error = mapping->a_ops->readpage(in, page);
  361. if (unlikely(error)) {
  362. /*
  363. * We really should re-lookup the page here,
  364. * but it complicates things a lot. Instead
  365. * lets just do what we already stored, and
  366. * we'll get it the next time we are called.
  367. */
  368. if (error == AOP_TRUNCATED_PAGE)
  369. error = 0;
  370. break;
  371. }
  372. }
  373. fill_it:
  374. /*
  375. * i_size must be checked after PageUptodate.
  376. */
  377. isize = i_size_read(mapping->host);
  378. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  379. if (unlikely(!isize || index > end_index))
  380. break;
  381. /*
  382. * if this is the last page, see if we need to shrink
  383. * the length and stop
  384. */
  385. if (end_index == index) {
  386. unsigned int plen;
  387. /*
  388. * max good bytes in this page
  389. */
  390. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  391. if (plen <= loff)
  392. break;
  393. /*
  394. * force quit after adding this page
  395. */
  396. this_len = min(this_len, plen - loff);
  397. len = this_len;
  398. }
  399. partial[page_nr].offset = loff;
  400. partial[page_nr].len = this_len;
  401. len -= this_len;
  402. loff = 0;
  403. spd.nr_pages++;
  404. index++;
  405. }
  406. /*
  407. * Release any pages at the end, if we quit early. 'page_nr' is how far
  408. * we got, 'nr_pages' is how many pages are in the map.
  409. */
  410. while (page_nr < nr_pages)
  411. page_cache_release(pages[page_nr++]);
  412. in->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT;
  413. if (spd.nr_pages)
  414. return splice_to_pipe(pipe, &spd);
  415. return error;
  416. }
  417. /**
  418. * generic_file_splice_read - splice data from file to a pipe
  419. * @in: file to splice from
  420. * @ppos: position in @in
  421. * @pipe: pipe to splice to
  422. * @len: number of bytes to splice
  423. * @flags: splice modifier flags
  424. *
  425. * Description:
  426. * Will read pages from given file and fill them into a pipe. Can be
  427. * used as long as the address_space operations for the source implements
  428. * a readpage() hook.
  429. *
  430. */
  431. ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
  432. struct pipe_inode_info *pipe, size_t len,
  433. unsigned int flags)
  434. {
  435. loff_t isize, left;
  436. int ret;
  437. isize = i_size_read(in->f_mapping->host);
  438. if (unlikely(*ppos >= isize))
  439. return 0;
  440. left = isize - *ppos;
  441. if (unlikely(left < len))
  442. len = left;
  443. ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
  444. if (ret > 0)
  445. *ppos += ret;
  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 ->write_begin and
  641. * ->write_end. 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. * Outer lock must be inode->i_mutex, as pipe_wait() will
  646. * release and reacquire pipe->inode->i_mutex, AND inode must
  647. * never be a pipe.
  648. */
  649. WARN_ON(S_ISFIFO(inode->i_mode));
  650. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  651. if (pipe->inode)
  652. mutex_lock_nested(&pipe->inode->i_mutex, I_MUTEX_CHILD);
  653. ret = __splice_from_pipe(pipe, &sd, actor);
  654. if (pipe->inode)
  655. mutex_unlock(&pipe->inode->i_mutex);
  656. mutex_unlock(&inode->i_mutex);
  657. return ret;
  658. }
  659. /**
  660. * generic_file_splice_write_nolock - generic_file_splice_write without mutexes
  661. * @pipe: pipe info
  662. * @out: file to write to
  663. * @ppos: position in @out
  664. * @len: number of bytes to splice
  665. * @flags: splice modifier flags
  666. *
  667. * Description:
  668. * Will either move or copy pages (determined by @flags options) from
  669. * the given pipe inode to the given file. The caller is responsible
  670. * for acquiring i_mutex on both inodes.
  671. *
  672. */
  673. ssize_t
  674. generic_file_splice_write_nolock(struct pipe_inode_info *pipe, struct file *out,
  675. loff_t *ppos, size_t len, unsigned int flags)
  676. {
  677. struct address_space *mapping = out->f_mapping;
  678. struct inode *inode = mapping->host;
  679. struct splice_desc sd = {
  680. .total_len = len,
  681. .flags = flags,
  682. .pos = *ppos,
  683. .u.file = out,
  684. };
  685. ssize_t ret;
  686. int err;
  687. err = file_remove_suid(out);
  688. if (unlikely(err))
  689. return err;
  690. ret = __splice_from_pipe(pipe, &sd, pipe_to_file);
  691. if (ret > 0) {
  692. unsigned long nr_pages;
  693. *ppos += ret;
  694. nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  695. /*
  696. * If file or inode is SYNC and we actually wrote some data,
  697. * sync it.
  698. */
  699. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  700. err = generic_osync_inode(inode, mapping,
  701. OSYNC_METADATA|OSYNC_DATA);
  702. if (err)
  703. ret = err;
  704. }
  705. balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
  706. }
  707. return ret;
  708. }
  709. EXPORT_SYMBOL(generic_file_splice_write_nolock);
  710. /**
  711. * generic_file_splice_write - splice data from a pipe to a file
  712. * @pipe: pipe info
  713. * @out: file to write to
  714. * @ppos: position in @out
  715. * @len: number of bytes to splice
  716. * @flags: splice modifier flags
  717. *
  718. * Description:
  719. * Will either move or copy pages (determined by @flags options) from
  720. * the given pipe inode to the given file.
  721. *
  722. */
  723. ssize_t
  724. generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  725. loff_t *ppos, size_t len, unsigned int flags)
  726. {
  727. struct address_space *mapping = out->f_mapping;
  728. struct inode *inode = mapping->host;
  729. struct splice_desc sd = {
  730. .total_len = len,
  731. .flags = flags,
  732. .pos = *ppos,
  733. .u.file = out,
  734. };
  735. ssize_t ret;
  736. WARN_ON(S_ISFIFO(inode->i_mode));
  737. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  738. ret = file_remove_suid(out);
  739. if (likely(!ret)) {
  740. if (pipe->inode)
  741. mutex_lock_nested(&pipe->inode->i_mutex, I_MUTEX_CHILD);
  742. ret = __splice_from_pipe(pipe, &sd, pipe_to_file);
  743. if (pipe->inode)
  744. mutex_unlock(&pipe->inode->i_mutex);
  745. }
  746. mutex_unlock(&inode->i_mutex);
  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. if (pipe->inode)
  1155. mutex_lock(&pipe->inode->i_mutex);
  1156. error = ret = 0;
  1157. while (nr_segs) {
  1158. void __user *base;
  1159. size_t len;
  1160. /*
  1161. * Get user address base and length for this iovec.
  1162. */
  1163. error = get_user(base, &iov->iov_base);
  1164. if (unlikely(error))
  1165. break;
  1166. error = get_user(len, &iov->iov_len);
  1167. if (unlikely(error))
  1168. break;
  1169. /*
  1170. * Sanity check this iovec. 0 read succeeds.
  1171. */
  1172. if (unlikely(!len))
  1173. break;
  1174. if (unlikely(!base)) {
  1175. error = -EFAULT;
  1176. break;
  1177. }
  1178. if (unlikely(!access_ok(VERIFY_WRITE, base, len))) {
  1179. error = -EFAULT;
  1180. break;
  1181. }
  1182. sd.len = 0;
  1183. sd.total_len = len;
  1184. sd.flags = flags;
  1185. sd.u.userptr = base;
  1186. sd.pos = 0;
  1187. size = __splice_from_pipe(pipe, &sd, pipe_to_user);
  1188. if (size < 0) {
  1189. if (!ret)
  1190. ret = size;
  1191. break;
  1192. }
  1193. ret += size;
  1194. if (size < len)
  1195. break;
  1196. nr_segs--;
  1197. iov++;
  1198. }
  1199. if (pipe->inode)
  1200. mutex_unlock(&pipe->inode->i_mutex);
  1201. if (!ret)
  1202. ret = error;
  1203. return ret;
  1204. }
  1205. /*
  1206. * vmsplice splices a user address range into a pipe. It can be thought of
  1207. * as splice-from-memory, where the regular splice is splice-from-file (or
  1208. * to file). In both cases the output is a pipe, naturally.
  1209. */
  1210. static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
  1211. unsigned long nr_segs, unsigned int flags)
  1212. {
  1213. struct pipe_inode_info *pipe;
  1214. struct page *pages[PIPE_BUFFERS];
  1215. struct partial_page partial[PIPE_BUFFERS];
  1216. struct splice_pipe_desc spd = {
  1217. .pages = pages,
  1218. .partial = partial,
  1219. .flags = flags,
  1220. .ops = &user_page_pipe_buf_ops,
  1221. .spd_release = spd_release_page,
  1222. };
  1223. pipe = pipe_info(file->f_path.dentry->d_inode);
  1224. if (!pipe)
  1225. return -EBADF;
  1226. spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
  1227. flags & SPLICE_F_GIFT);
  1228. if (spd.nr_pages <= 0)
  1229. return spd.nr_pages;
  1230. return splice_to_pipe(pipe, &spd);
  1231. }
  1232. /*
  1233. * Note that vmsplice only really supports true splicing _from_ user memory
  1234. * to a pipe, not the other way around. Splicing from user memory is a simple
  1235. * operation that can be supported without any funky alignment restrictions
  1236. * or nasty vm tricks. We simply map in the user memory and fill them into
  1237. * a pipe. The reverse isn't quite as easy, though. There are two possible
  1238. * solutions for that:
  1239. *
  1240. * - memcpy() the data internally, at which point we might as well just
  1241. * do a regular read() on the buffer anyway.
  1242. * - Lots of nasty vm tricks, that are neither fast nor flexible (it
  1243. * has restriction limitations on both ends of the pipe).
  1244. *
  1245. * Currently we punt and implement it as a normal copy, see pipe_to_user().
  1246. *
  1247. */
  1248. SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
  1249. unsigned long, nr_segs, unsigned int, flags)
  1250. {
  1251. struct file *file;
  1252. long error;
  1253. int fput;
  1254. if (unlikely(nr_segs > UIO_MAXIOV))
  1255. return -EINVAL;
  1256. else if (unlikely(!nr_segs))
  1257. return 0;
  1258. error = -EBADF;
  1259. file = fget_light(fd, &fput);
  1260. if (file) {
  1261. if (file->f_mode & FMODE_WRITE)
  1262. error = vmsplice_to_pipe(file, iov, nr_segs, flags);
  1263. else if (file->f_mode & FMODE_READ)
  1264. error = vmsplice_to_user(file, iov, nr_segs, flags);
  1265. fput_light(file, fput);
  1266. }
  1267. return error;
  1268. }
  1269. SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
  1270. int, fd_out, loff_t __user *, off_out,
  1271. size_t, len, unsigned int, flags)
  1272. {
  1273. long error;
  1274. struct file *in, *out;
  1275. int fput_in, fput_out;
  1276. if (unlikely(!len))
  1277. return 0;
  1278. error = -EBADF;
  1279. in = fget_light(fd_in, &fput_in);
  1280. if (in) {
  1281. if (in->f_mode & FMODE_READ) {
  1282. out = fget_light(fd_out, &fput_out);
  1283. if (out) {
  1284. if (out->f_mode & FMODE_WRITE)
  1285. error = do_splice(in, off_in,
  1286. out, off_out,
  1287. len, flags);
  1288. fput_light(out, fput_out);
  1289. }
  1290. }
  1291. fput_light(in, fput_in);
  1292. }
  1293. return error;
  1294. }
  1295. /*
  1296. * Make sure there's data to read. Wait for input if we can, otherwise
  1297. * return an appropriate error.
  1298. */
  1299. static int link_ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1300. {
  1301. int ret;
  1302. /*
  1303. * Check ->nrbufs without the inode lock first. This function
  1304. * is speculative anyways, so missing one is ok.
  1305. */
  1306. if (pipe->nrbufs)
  1307. return 0;
  1308. ret = 0;
  1309. mutex_lock(&pipe->inode->i_mutex);
  1310. while (!pipe->nrbufs) {
  1311. if (signal_pending(current)) {
  1312. ret = -ERESTARTSYS;
  1313. break;
  1314. }
  1315. if (!pipe->writers)
  1316. break;
  1317. if (!pipe->waiting_writers) {
  1318. if (flags & SPLICE_F_NONBLOCK) {
  1319. ret = -EAGAIN;
  1320. break;
  1321. }
  1322. }
  1323. pipe_wait(pipe);
  1324. }
  1325. mutex_unlock(&pipe->inode->i_mutex);
  1326. return ret;
  1327. }
  1328. /*
  1329. * Make sure there's writeable room. Wait for room if we can, otherwise
  1330. * return an appropriate error.
  1331. */
  1332. static int link_opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1333. {
  1334. int ret;
  1335. /*
  1336. * Check ->nrbufs without the inode lock first. This function
  1337. * is speculative anyways, so missing one is ok.
  1338. */
  1339. if (pipe->nrbufs < PIPE_BUFFERS)
  1340. return 0;
  1341. ret = 0;
  1342. mutex_lock(&pipe->inode->i_mutex);
  1343. while (pipe->nrbufs >= PIPE_BUFFERS) {
  1344. if (!pipe->readers) {
  1345. send_sig(SIGPIPE, current, 0);
  1346. ret = -EPIPE;
  1347. break;
  1348. }
  1349. if (flags & SPLICE_F_NONBLOCK) {
  1350. ret = -EAGAIN;
  1351. break;
  1352. }
  1353. if (signal_pending(current)) {
  1354. ret = -ERESTARTSYS;
  1355. break;
  1356. }
  1357. pipe->waiting_writers++;
  1358. pipe_wait(pipe);
  1359. pipe->waiting_writers--;
  1360. }
  1361. mutex_unlock(&pipe->inode->i_mutex);
  1362. return ret;
  1363. }
  1364. /*
  1365. * Link contents of ipipe to opipe.
  1366. */
  1367. static int link_pipe(struct pipe_inode_info *ipipe,
  1368. struct pipe_inode_info *opipe,
  1369. size_t len, unsigned int flags)
  1370. {
  1371. struct pipe_buffer *ibuf, *obuf;
  1372. int ret = 0, i = 0, nbuf;
  1373. /*
  1374. * Potential ABBA deadlock, work around it by ordering lock
  1375. * grabbing by inode address. Otherwise two different processes
  1376. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1377. */
  1378. inode_double_lock(ipipe->inode, opipe->inode);
  1379. do {
  1380. if (!opipe->readers) {
  1381. send_sig(SIGPIPE, current, 0);
  1382. if (!ret)
  1383. ret = -EPIPE;
  1384. break;
  1385. }
  1386. /*
  1387. * If we have iterated all input buffers or ran out of
  1388. * output room, break.
  1389. */
  1390. if (i >= ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS)
  1391. break;
  1392. ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
  1393. nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
  1394. /*
  1395. * Get a reference to this pipe buffer,
  1396. * so we can copy the contents over.
  1397. */
  1398. ibuf->ops->get(ipipe, ibuf);
  1399. obuf = opipe->bufs + nbuf;
  1400. *obuf = *ibuf;
  1401. /*
  1402. * Don't inherit the gift flag, we need to
  1403. * prevent multiple steals of this page.
  1404. */
  1405. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1406. if (obuf->len > len)
  1407. obuf->len = len;
  1408. opipe->nrbufs++;
  1409. ret += obuf->len;
  1410. len -= obuf->len;
  1411. i++;
  1412. } while (len);
  1413. /*
  1414. * return EAGAIN if we have the potential of some data in the
  1415. * future, otherwise just return 0
  1416. */
  1417. if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
  1418. ret = -EAGAIN;
  1419. inode_double_unlock(ipipe->inode, opipe->inode);
  1420. /*
  1421. * If we put data in the output pipe, wakeup any potential readers.
  1422. */
  1423. if (ret > 0) {
  1424. smp_mb();
  1425. if (waitqueue_active(&opipe->wait))
  1426. wake_up_interruptible(&opipe->wait);
  1427. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  1428. }
  1429. return ret;
  1430. }
  1431. /*
  1432. * This is a tee(1) implementation that works on pipes. It doesn't copy
  1433. * any data, it simply references the 'in' pages on the 'out' pipe.
  1434. * The 'flags' used are the SPLICE_F_* variants, currently the only
  1435. * applicable one is SPLICE_F_NONBLOCK.
  1436. */
  1437. static long do_tee(struct file *in, struct file *out, size_t len,
  1438. unsigned int flags)
  1439. {
  1440. struct pipe_inode_info *ipipe = pipe_info(in->f_path.dentry->d_inode);
  1441. struct pipe_inode_info *opipe = pipe_info(out->f_path.dentry->d_inode);
  1442. int ret = -EINVAL;
  1443. /*
  1444. * Duplicate the contents of ipipe to opipe without actually
  1445. * copying the data.
  1446. */
  1447. if (ipipe && opipe && ipipe != opipe) {
  1448. /*
  1449. * Keep going, unless we encounter an error. The ipipe/opipe
  1450. * ordering doesn't really matter.
  1451. */
  1452. ret = link_ipipe_prep(ipipe, flags);
  1453. if (!ret) {
  1454. ret = link_opipe_prep(opipe, flags);
  1455. if (!ret)
  1456. ret = link_pipe(ipipe, opipe, len, flags);
  1457. }
  1458. }
  1459. return ret;
  1460. }
  1461. SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
  1462. {
  1463. struct file *in;
  1464. int error, fput_in;
  1465. if (unlikely(!len))
  1466. return 0;
  1467. error = -EBADF;
  1468. in = fget_light(fdin, &fput_in);
  1469. if (in) {
  1470. if (in->f_mode & FMODE_READ) {
  1471. int fput_out;
  1472. struct file *out = fget_light(fdout, &fput_out);
  1473. if (out) {
  1474. if (out->f_mode & FMODE_WRITE)
  1475. error = do_tee(in, out, len, flags);
  1476. fput_light(out, fput_out);
  1477. }
  1478. }
  1479. fput_light(in, fput_in);
  1480. }
  1481. return error;
  1482. }