splice.c 40 KB

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