splice.c 34 KB

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