splice.c 33 KB

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