splice.c 35 KB

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