splice.c 36 KB

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