splice.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  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 *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_nomem;
  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;
  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_nomem:
  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. ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
  618. loff_t *ppos, size_t len, unsigned int flags,
  619. 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. if (pipe->inode)
  630. mutex_lock(&pipe->inode->i_mutex);
  631. for (;;) {
  632. if (pipe->nrbufs) {
  633. struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  634. struct pipe_buf_operations *ops = buf->ops;
  635. sd.len = buf->len;
  636. if (sd.len > sd.total_len)
  637. sd.len = sd.total_len;
  638. err = actor(pipe, buf, &sd);
  639. if (err <= 0) {
  640. if (!ret && err != -ENODATA)
  641. ret = err;
  642. break;
  643. }
  644. ret += err;
  645. buf->offset += err;
  646. buf->len -= err;
  647. sd.len -= err;
  648. sd.pos += err;
  649. sd.total_len -= err;
  650. if (sd.len)
  651. continue;
  652. if (!buf->len) {
  653. buf->ops = NULL;
  654. ops->release(pipe, buf);
  655. pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
  656. pipe->nrbufs--;
  657. if (pipe->inode)
  658. do_wakeup = 1;
  659. }
  660. if (!sd.total_len)
  661. break;
  662. }
  663. if (pipe->nrbufs)
  664. continue;
  665. if (!pipe->writers)
  666. break;
  667. if (!pipe->waiting_writers) {
  668. if (ret)
  669. break;
  670. }
  671. if (flags & SPLICE_F_NONBLOCK) {
  672. if (!ret)
  673. ret = -EAGAIN;
  674. break;
  675. }
  676. if (signal_pending(current)) {
  677. if (!ret)
  678. ret = -ERESTARTSYS;
  679. break;
  680. }
  681. if (do_wakeup) {
  682. smp_mb();
  683. if (waitqueue_active(&pipe->wait))
  684. wake_up_interruptible_sync(&pipe->wait);
  685. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  686. do_wakeup = 0;
  687. }
  688. pipe_wait(pipe);
  689. }
  690. if (pipe->inode)
  691. mutex_unlock(&pipe->inode->i_mutex);
  692. if (do_wakeup) {
  693. smp_mb();
  694. if (waitqueue_active(&pipe->wait))
  695. wake_up_interruptible(&pipe->wait);
  696. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  697. }
  698. return ret;
  699. }
  700. /**
  701. * generic_file_splice_write - splice data from a pipe to a file
  702. * @pipe: pipe info
  703. * @out: file to write to
  704. * @len: number of bytes to splice
  705. * @flags: splice modifier flags
  706. *
  707. * Will either move or copy pages (determined by @flags options) from
  708. * the given pipe inode to the given file.
  709. *
  710. */
  711. ssize_t
  712. generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  713. loff_t *ppos, size_t len, unsigned int flags)
  714. {
  715. struct address_space *mapping = out->f_mapping;
  716. ssize_t ret;
  717. ret = splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_file);
  718. if (ret > 0) {
  719. struct inode *inode = mapping->host;
  720. *ppos += ret;
  721. /*
  722. * If file or inode is SYNC and we actually wrote some data,
  723. * sync it.
  724. */
  725. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  726. int err;
  727. mutex_lock(&inode->i_mutex);
  728. err = generic_osync_inode(inode, mapping,
  729. OSYNC_METADATA|OSYNC_DATA);
  730. mutex_unlock(&inode->i_mutex);
  731. if (err)
  732. ret = err;
  733. }
  734. }
  735. return ret;
  736. }
  737. EXPORT_SYMBOL(generic_file_splice_write);
  738. /**
  739. * generic_splice_sendpage - splice data from a pipe to a socket
  740. * @inode: pipe inode
  741. * @out: socket to write to
  742. * @len: number of bytes to splice
  743. * @flags: splice modifier flags
  744. *
  745. * Will send @len bytes from the pipe to a network socket. No data copying
  746. * is involved.
  747. *
  748. */
  749. ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
  750. loff_t *ppos, size_t len, unsigned int flags)
  751. {
  752. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
  753. }
  754. EXPORT_SYMBOL(generic_splice_sendpage);
  755. /*
  756. * Attempt to initiate a splice from pipe to file.
  757. */
  758. static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  759. loff_t *ppos, size_t len, unsigned int flags)
  760. {
  761. int ret;
  762. if (unlikely(!out->f_op || !out->f_op->splice_write))
  763. return -EINVAL;
  764. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  765. return -EBADF;
  766. ret = rw_verify_area(WRITE, out, ppos, len);
  767. if (unlikely(ret < 0))
  768. return ret;
  769. return out->f_op->splice_write(pipe, out, ppos, len, flags);
  770. }
  771. /*
  772. * Attempt to initiate a splice from a file to a pipe.
  773. */
  774. static long do_splice_to(struct file *in, loff_t *ppos,
  775. struct pipe_inode_info *pipe, size_t len,
  776. unsigned int flags)
  777. {
  778. loff_t isize, left;
  779. int ret;
  780. if (unlikely(!in->f_op || !in->f_op->splice_read))
  781. return -EINVAL;
  782. if (unlikely(!(in->f_mode & FMODE_READ)))
  783. return -EBADF;
  784. ret = rw_verify_area(READ, in, ppos, len);
  785. if (unlikely(ret < 0))
  786. return ret;
  787. isize = i_size_read(in->f_mapping->host);
  788. if (unlikely(*ppos >= isize))
  789. return 0;
  790. left = isize - *ppos;
  791. if (unlikely(left < len))
  792. len = left;
  793. return in->f_op->splice_read(in, ppos, pipe, len, flags);
  794. }
  795. long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
  796. size_t len, unsigned int flags)
  797. {
  798. struct pipe_inode_info *pipe;
  799. long ret, bytes;
  800. loff_t out_off;
  801. umode_t i_mode;
  802. int i;
  803. /*
  804. * We require the input being a regular file, as we don't want to
  805. * randomly drop data for eg socket -> socket splicing. Use the
  806. * piped splicing for that!
  807. */
  808. i_mode = in->f_dentry->d_inode->i_mode;
  809. if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  810. return -EINVAL;
  811. /*
  812. * neither in nor out is a pipe, setup an internal pipe attached to
  813. * 'out' and transfer the wanted data from 'in' to 'out' through that
  814. */
  815. pipe = current->splice_pipe;
  816. if (unlikely(!pipe)) {
  817. pipe = alloc_pipe_info(NULL);
  818. if (!pipe)
  819. return -ENOMEM;
  820. /*
  821. * We don't have an immediate reader, but we'll read the stuff
  822. * out of the pipe right after the splice_to_pipe(). So set
  823. * PIPE_READERS appropriately.
  824. */
  825. pipe->readers = 1;
  826. current->splice_pipe = pipe;
  827. }
  828. /*
  829. * Do the splice.
  830. */
  831. ret = 0;
  832. bytes = 0;
  833. out_off = 0;
  834. while (len) {
  835. size_t read_len, max_read_len;
  836. /*
  837. * Do at most PIPE_BUFFERS pages worth of transfer:
  838. */
  839. max_read_len = min(len, (size_t)(PIPE_BUFFERS*PAGE_SIZE));
  840. ret = do_splice_to(in, ppos, pipe, max_read_len, flags);
  841. if (unlikely(ret < 0))
  842. goto out_release;
  843. read_len = ret;
  844. /*
  845. * NOTE: nonblocking mode only applies to the input. We
  846. * must not do the output in nonblocking mode as then we
  847. * could get stuck data in the internal pipe:
  848. */
  849. ret = do_splice_from(pipe, out, &out_off, read_len,
  850. flags & ~SPLICE_F_NONBLOCK);
  851. if (unlikely(ret < 0))
  852. goto out_release;
  853. bytes += ret;
  854. len -= ret;
  855. /*
  856. * In nonblocking mode, if we got back a short read then
  857. * that was due to either an IO error or due to the
  858. * pagecache entry not being there. In the IO error case
  859. * the _next_ splice attempt will produce a clean IO error
  860. * return value (not a short read), so in both cases it's
  861. * correct to break out of the loop here:
  862. */
  863. if ((flags & SPLICE_F_NONBLOCK) && (read_len < max_read_len))
  864. break;
  865. }
  866. pipe->nrbufs = pipe->curbuf = 0;
  867. return bytes;
  868. out_release:
  869. /*
  870. * If we did an incomplete transfer we must release
  871. * the pipe buffers in question:
  872. */
  873. for (i = 0; i < PIPE_BUFFERS; i++) {
  874. struct pipe_buffer *buf = pipe->bufs + i;
  875. if (buf->ops) {
  876. buf->ops->release(pipe, buf);
  877. buf->ops = NULL;
  878. }
  879. }
  880. pipe->nrbufs = pipe->curbuf = 0;
  881. /*
  882. * If we transferred some data, return the number of bytes:
  883. */
  884. if (bytes > 0)
  885. return bytes;
  886. return ret;
  887. }
  888. EXPORT_SYMBOL(do_splice_direct);
  889. /*
  890. * Determine where to splice to/from.
  891. */
  892. static long do_splice(struct file *in, loff_t __user *off_in,
  893. struct file *out, loff_t __user *off_out,
  894. size_t len, unsigned int flags)
  895. {
  896. struct pipe_inode_info *pipe;
  897. loff_t offset, *off;
  898. long ret;
  899. pipe = in->f_dentry->d_inode->i_pipe;
  900. if (pipe) {
  901. if (off_in)
  902. return -ESPIPE;
  903. if (off_out) {
  904. if (out->f_op->llseek == no_llseek)
  905. return -EINVAL;
  906. if (copy_from_user(&offset, off_out, sizeof(loff_t)))
  907. return -EFAULT;
  908. off = &offset;
  909. } else
  910. off = &out->f_pos;
  911. ret = do_splice_from(pipe, out, off, len, flags);
  912. if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
  913. ret = -EFAULT;
  914. return ret;
  915. }
  916. pipe = out->f_dentry->d_inode->i_pipe;
  917. if (pipe) {
  918. if (off_out)
  919. return -ESPIPE;
  920. if (off_in) {
  921. if (in->f_op->llseek == no_llseek)
  922. return -EINVAL;
  923. if (copy_from_user(&offset, off_in, sizeof(loff_t)))
  924. return -EFAULT;
  925. off = &offset;
  926. } else
  927. off = &in->f_pos;
  928. ret = do_splice_to(in, off, pipe, len, flags);
  929. if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
  930. ret = -EFAULT;
  931. return ret;
  932. }
  933. return -EINVAL;
  934. }
  935. /*
  936. * Map an iov into an array of pages and offset/length tupples. With the
  937. * partial_page structure, we can map several non-contiguous ranges into
  938. * our ones pages[] map instead of splitting that operation into pieces.
  939. * Could easily be exported as a generic helper for other users, in which
  940. * case one would probably want to add a 'max_nr_pages' parameter as well.
  941. */
  942. static int get_iovec_page_array(const struct iovec __user *iov,
  943. unsigned int nr_vecs, struct page **pages,
  944. struct partial_page *partial, int aligned)
  945. {
  946. int buffers = 0, error = 0;
  947. /*
  948. * It's ok to take the mmap_sem for reading, even
  949. * across a "get_user()".
  950. */
  951. down_read(&current->mm->mmap_sem);
  952. while (nr_vecs) {
  953. unsigned long off, npages;
  954. void __user *base;
  955. size_t len;
  956. int i;
  957. /*
  958. * Get user address base and length for this iovec.
  959. */
  960. error = get_user(base, &iov->iov_base);
  961. if (unlikely(error))
  962. break;
  963. error = get_user(len, &iov->iov_len);
  964. if (unlikely(error))
  965. break;
  966. /*
  967. * Sanity check this iovec. 0 read succeeds.
  968. */
  969. if (unlikely(!len))
  970. break;
  971. error = -EFAULT;
  972. if (unlikely(!base))
  973. break;
  974. /*
  975. * Get this base offset and number of pages, then map
  976. * in the user pages.
  977. */
  978. off = (unsigned long) base & ~PAGE_MASK;
  979. /*
  980. * If asked for alignment, the offset must be zero and the
  981. * length a multiple of the PAGE_SIZE.
  982. */
  983. error = -EINVAL;
  984. if (aligned && (off || len & ~PAGE_MASK))
  985. break;
  986. npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  987. if (npages > PIPE_BUFFERS - buffers)
  988. npages = PIPE_BUFFERS - buffers;
  989. error = get_user_pages(current, current->mm,
  990. (unsigned long) base, npages, 0, 0,
  991. &pages[buffers], NULL);
  992. if (unlikely(error <= 0))
  993. break;
  994. /*
  995. * Fill this contiguous range into the partial page map.
  996. */
  997. for (i = 0; i < error; i++) {
  998. const int plen = min_t(size_t, len, PAGE_SIZE - off);
  999. partial[buffers].offset = off;
  1000. partial[buffers].len = plen;
  1001. off = 0;
  1002. len -= plen;
  1003. buffers++;
  1004. }
  1005. /*
  1006. * We didn't complete this iov, stop here since it probably
  1007. * means we have to move some of this into a pipe to
  1008. * be able to continue.
  1009. */
  1010. if (len)
  1011. break;
  1012. /*
  1013. * Don't continue if we mapped fewer pages than we asked for,
  1014. * or if we mapped the max number of pages that we have
  1015. * room for.
  1016. */
  1017. if (error < npages || buffers == PIPE_BUFFERS)
  1018. break;
  1019. nr_vecs--;
  1020. iov++;
  1021. }
  1022. up_read(&current->mm->mmap_sem);
  1023. if (buffers)
  1024. return buffers;
  1025. return error;
  1026. }
  1027. /*
  1028. * vmsplice splices a user address range into a pipe. It can be thought of
  1029. * as splice-from-memory, where the regular splice is splice-from-file (or
  1030. * to file). In both cases the output is a pipe, naturally.
  1031. *
  1032. * Note that vmsplice only supports splicing _from_ user memory to a pipe,
  1033. * not the other way around. Splicing from user memory is a simple operation
  1034. * that can be supported without any funky alignment restrictions or nasty
  1035. * vm tricks. We simply map in the user memory and fill them into a pipe.
  1036. * The reverse isn't quite as easy, though. There are two possible solutions
  1037. * for that:
  1038. *
  1039. * - memcpy() the data internally, at which point we might as well just
  1040. * do a regular read() on the buffer anyway.
  1041. * - Lots of nasty vm tricks, that are neither fast nor flexible (it
  1042. * has restriction limitations on both ends of the pipe).
  1043. *
  1044. * Alas, it isn't here.
  1045. *
  1046. */
  1047. static long do_vmsplice(struct file *file, const struct iovec __user *iov,
  1048. unsigned long nr_segs, unsigned int flags)
  1049. {
  1050. struct pipe_inode_info *pipe = file->f_dentry->d_inode->i_pipe;
  1051. struct page *pages[PIPE_BUFFERS];
  1052. struct partial_page partial[PIPE_BUFFERS];
  1053. struct splice_pipe_desc spd = {
  1054. .pages = pages,
  1055. .partial = partial,
  1056. .flags = flags,
  1057. .ops = &user_page_pipe_buf_ops,
  1058. };
  1059. if (unlikely(!pipe))
  1060. return -EBADF;
  1061. if (unlikely(nr_segs > UIO_MAXIOV))
  1062. return -EINVAL;
  1063. else if (unlikely(!nr_segs))
  1064. return 0;
  1065. spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
  1066. flags & SPLICE_F_GIFT);
  1067. if (spd.nr_pages <= 0)
  1068. return spd.nr_pages;
  1069. return splice_to_pipe(pipe, &spd);
  1070. }
  1071. asmlinkage long sys_vmsplice(int fd, const struct iovec __user *iov,
  1072. unsigned long nr_segs, unsigned int flags)
  1073. {
  1074. struct file *file;
  1075. long error;
  1076. int fput;
  1077. error = -EBADF;
  1078. file = fget_light(fd, &fput);
  1079. if (file) {
  1080. if (file->f_mode & FMODE_WRITE)
  1081. error = do_vmsplice(file, iov, nr_segs, flags);
  1082. fput_light(file, fput);
  1083. }
  1084. return error;
  1085. }
  1086. asmlinkage long sys_splice(int fd_in, loff_t __user *off_in,
  1087. int fd_out, loff_t __user *off_out,
  1088. size_t len, unsigned int flags)
  1089. {
  1090. long error;
  1091. struct file *in, *out;
  1092. int fput_in, fput_out;
  1093. if (unlikely(!len))
  1094. return 0;
  1095. error = -EBADF;
  1096. in = fget_light(fd_in, &fput_in);
  1097. if (in) {
  1098. if (in->f_mode & FMODE_READ) {
  1099. out = fget_light(fd_out, &fput_out);
  1100. if (out) {
  1101. if (out->f_mode & FMODE_WRITE)
  1102. error = do_splice(in, off_in,
  1103. out, off_out,
  1104. len, flags);
  1105. fput_light(out, fput_out);
  1106. }
  1107. }
  1108. fput_light(in, fput_in);
  1109. }
  1110. return error;
  1111. }
  1112. /*
  1113. * Make sure there's data to read. Wait for input if we can, otherwise
  1114. * return an appropriate error.
  1115. */
  1116. static int link_ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1117. {
  1118. int ret;
  1119. /*
  1120. * Check ->nrbufs without the inode lock first. This function
  1121. * is speculative anyways, so missing one is ok.
  1122. */
  1123. if (pipe->nrbufs)
  1124. return 0;
  1125. ret = 0;
  1126. mutex_lock(&pipe->inode->i_mutex);
  1127. while (!pipe->nrbufs) {
  1128. if (signal_pending(current)) {
  1129. ret = -ERESTARTSYS;
  1130. break;
  1131. }
  1132. if (!pipe->writers)
  1133. break;
  1134. if (!pipe->waiting_writers) {
  1135. if (flags & SPLICE_F_NONBLOCK) {
  1136. ret = -EAGAIN;
  1137. break;
  1138. }
  1139. }
  1140. pipe_wait(pipe);
  1141. }
  1142. mutex_unlock(&pipe->inode->i_mutex);
  1143. return ret;
  1144. }
  1145. /*
  1146. * Make sure there's writeable room. Wait for room if we can, otherwise
  1147. * return an appropriate error.
  1148. */
  1149. static int link_opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1150. {
  1151. int ret;
  1152. /*
  1153. * Check ->nrbufs without the inode lock first. This function
  1154. * is speculative anyways, so missing one is ok.
  1155. */
  1156. if (pipe->nrbufs < PIPE_BUFFERS)
  1157. return 0;
  1158. ret = 0;
  1159. mutex_lock(&pipe->inode->i_mutex);
  1160. while (pipe->nrbufs >= PIPE_BUFFERS) {
  1161. if (!pipe->readers) {
  1162. send_sig(SIGPIPE, current, 0);
  1163. ret = -EPIPE;
  1164. break;
  1165. }
  1166. if (flags & SPLICE_F_NONBLOCK) {
  1167. ret = -EAGAIN;
  1168. break;
  1169. }
  1170. if (signal_pending(current)) {
  1171. ret = -ERESTARTSYS;
  1172. break;
  1173. }
  1174. pipe->waiting_writers++;
  1175. pipe_wait(pipe);
  1176. pipe->waiting_writers--;
  1177. }
  1178. mutex_unlock(&pipe->inode->i_mutex);
  1179. return ret;
  1180. }
  1181. /*
  1182. * Link contents of ipipe to opipe.
  1183. */
  1184. static int link_pipe(struct pipe_inode_info *ipipe,
  1185. struct pipe_inode_info *opipe,
  1186. size_t len, unsigned int flags)
  1187. {
  1188. struct pipe_buffer *ibuf, *obuf;
  1189. int ret = 0, i = 0, nbuf;
  1190. /*
  1191. * Potential ABBA deadlock, work around it by ordering lock
  1192. * grabbing by inode address. Otherwise two different processes
  1193. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1194. */
  1195. if (ipipe->inode < opipe->inode) {
  1196. mutex_lock_nested(&ipipe->inode->i_mutex, I_MUTEX_PARENT);
  1197. mutex_lock_nested(&opipe->inode->i_mutex, I_MUTEX_CHILD);
  1198. } else {
  1199. mutex_lock_nested(&opipe->inode->i_mutex, I_MUTEX_PARENT);
  1200. mutex_lock_nested(&ipipe->inode->i_mutex, I_MUTEX_CHILD);
  1201. }
  1202. do {
  1203. if (!opipe->readers) {
  1204. send_sig(SIGPIPE, current, 0);
  1205. if (!ret)
  1206. ret = -EPIPE;
  1207. break;
  1208. }
  1209. /*
  1210. * If we have iterated all input buffers or ran out of
  1211. * output room, break.
  1212. */
  1213. if (i >= ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS)
  1214. break;
  1215. ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
  1216. nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
  1217. /*
  1218. * Get a reference to this pipe buffer,
  1219. * so we can copy the contents over.
  1220. */
  1221. ibuf->ops->get(ipipe, ibuf);
  1222. obuf = opipe->bufs + nbuf;
  1223. *obuf = *ibuf;
  1224. /*
  1225. * Don't inherit the gift flag, we need to
  1226. * prevent multiple steals of this page.
  1227. */
  1228. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1229. if (obuf->len > len)
  1230. obuf->len = len;
  1231. opipe->nrbufs++;
  1232. ret += obuf->len;
  1233. len -= obuf->len;
  1234. i++;
  1235. } while (len);
  1236. mutex_unlock(&ipipe->inode->i_mutex);
  1237. mutex_unlock(&opipe->inode->i_mutex);
  1238. /*
  1239. * If we put data in the output pipe, wakeup any potential readers.
  1240. */
  1241. if (ret > 0) {
  1242. smp_mb();
  1243. if (waitqueue_active(&opipe->wait))
  1244. wake_up_interruptible(&opipe->wait);
  1245. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  1246. }
  1247. return ret;
  1248. }
  1249. /*
  1250. * This is a tee(1) implementation that works on pipes. It doesn't copy
  1251. * any data, it simply references the 'in' pages on the 'out' pipe.
  1252. * The 'flags' used are the SPLICE_F_* variants, currently the only
  1253. * applicable one is SPLICE_F_NONBLOCK.
  1254. */
  1255. static long do_tee(struct file *in, struct file *out, size_t len,
  1256. unsigned int flags)
  1257. {
  1258. struct pipe_inode_info *ipipe = in->f_dentry->d_inode->i_pipe;
  1259. struct pipe_inode_info *opipe = out->f_dentry->d_inode->i_pipe;
  1260. int ret = -EINVAL;
  1261. /*
  1262. * Duplicate the contents of ipipe to opipe without actually
  1263. * copying the data.
  1264. */
  1265. if (ipipe && opipe && ipipe != opipe) {
  1266. /*
  1267. * Keep going, unless we encounter an error. The ipipe/opipe
  1268. * ordering doesn't really matter.
  1269. */
  1270. ret = link_ipipe_prep(ipipe, flags);
  1271. if (!ret) {
  1272. ret = link_opipe_prep(opipe, flags);
  1273. if (!ret) {
  1274. ret = link_pipe(ipipe, opipe, len, flags);
  1275. if (!ret && (flags & SPLICE_F_NONBLOCK))
  1276. ret = -EAGAIN;
  1277. }
  1278. }
  1279. }
  1280. return ret;
  1281. }
  1282. asmlinkage long sys_tee(int fdin, int fdout, size_t len, unsigned int flags)
  1283. {
  1284. struct file *in;
  1285. int error, fput_in;
  1286. if (unlikely(!len))
  1287. return 0;
  1288. error = -EBADF;
  1289. in = fget_light(fdin, &fput_in);
  1290. if (in) {
  1291. if (in->f_mode & FMODE_READ) {
  1292. int fput_out;
  1293. struct file *out = fget_light(fdout, &fput_out);
  1294. if (out) {
  1295. if (out->f_mode & FMODE_WRITE)
  1296. error = do_tee(in, out, len, flags);
  1297. fput_light(out, fput_out);
  1298. }
  1299. }
  1300. fput_light(in, fput_in);
  1301. }
  1302. return error;
  1303. }