splice.c 38 KB

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