splice.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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->page = NULL;
  80. buf->flags &= ~PIPE_BUF_FLAG_LRU;
  81. }
  82. static int page_cache_pipe_buf_pin(struct pipe_inode_info *info,
  83. struct pipe_buffer *buf)
  84. {
  85. struct page *page = buf->page;
  86. int err;
  87. if (!PageUptodate(page)) {
  88. lock_page(page);
  89. /*
  90. * Page got truncated/unhashed. This will cause a 0-byte
  91. * splice, if this is the first page.
  92. */
  93. if (!page->mapping) {
  94. err = -ENODATA;
  95. goto error;
  96. }
  97. /*
  98. * Uh oh, read-error from disk.
  99. */
  100. if (!PageUptodate(page)) {
  101. err = -EIO;
  102. goto error;
  103. }
  104. /*
  105. * Page is ok afterall, we are done.
  106. */
  107. unlock_page(page);
  108. }
  109. return 0;
  110. error:
  111. unlock_page(page);
  112. return err;
  113. }
  114. static struct pipe_buf_operations page_cache_pipe_buf_ops = {
  115. .can_merge = 0,
  116. .map = generic_pipe_buf_map,
  117. .unmap = generic_pipe_buf_unmap,
  118. .pin = page_cache_pipe_buf_pin,
  119. .release = page_cache_pipe_buf_release,
  120. .steal = page_cache_pipe_buf_steal,
  121. .get = generic_pipe_buf_get,
  122. };
  123. static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
  124. struct pipe_buffer *buf)
  125. {
  126. if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
  127. return 1;
  128. return 0;
  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 vm
  495. * side (LRU and page cache) and we can reuse it. The page
  496. * will also be looked 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 (ret == AOP_TRUNCATED_PAGE) {
  554. page_cache_release(page);
  555. goto find_page;
  556. } else if (ret)
  557. goto out;
  558. if (buf->page != page) {
  559. /*
  560. * Careful, ->map() uses KM_USER0!
  561. */
  562. char *src = buf->ops->map(info, buf, 1);
  563. char *dst = kmap_atomic(page, KM_USER1);
  564. memcpy(dst + offset, src + buf->offset, this_len);
  565. flush_dcache_page(page);
  566. kunmap_atomic(dst, KM_USER1);
  567. buf->ops->unmap(info, buf, src);
  568. }
  569. ret = mapping->a_ops->commit_write(file, page, offset, offset+this_len);
  570. if (!ret) {
  571. /*
  572. * Return the number of bytes written and mark page as
  573. * accessed, we are now done!
  574. */
  575. ret = this_len;
  576. mark_page_accessed(page);
  577. balance_dirty_pages_ratelimited(mapping);
  578. } else if (ret == AOP_TRUNCATED_PAGE) {
  579. page_cache_release(page);
  580. goto find_page;
  581. }
  582. out:
  583. page_cache_release(page);
  584. unlock_page(page);
  585. out_nomem:
  586. return ret;
  587. }
  588. /*
  589. * Pipe input worker. Most of this logic works like a regular pipe, the
  590. * key here is the 'actor' worker passed in that actually moves the data
  591. * to the wanted destination. See pipe_to_file/pipe_to_sendpage above.
  592. */
  593. ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
  594. loff_t *ppos, size_t len, unsigned int flags,
  595. splice_actor *actor)
  596. {
  597. int ret, do_wakeup, err;
  598. struct splice_desc sd;
  599. ret = 0;
  600. do_wakeup = 0;
  601. sd.total_len = len;
  602. sd.flags = flags;
  603. sd.file = out;
  604. sd.pos = *ppos;
  605. if (pipe->inode)
  606. mutex_lock(&pipe->inode->i_mutex);
  607. for (;;) {
  608. if (pipe->nrbufs) {
  609. struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  610. struct pipe_buf_operations *ops = buf->ops;
  611. sd.len = buf->len;
  612. if (sd.len > sd.total_len)
  613. sd.len = sd.total_len;
  614. err = actor(pipe, buf, &sd);
  615. if (err <= 0) {
  616. if (!ret && err != -ENODATA)
  617. ret = err;
  618. break;
  619. }
  620. ret += err;
  621. buf->offset += err;
  622. buf->len -= err;
  623. sd.len -= err;
  624. sd.pos += err;
  625. sd.total_len -= err;
  626. if (sd.len)
  627. continue;
  628. if (!buf->len) {
  629. buf->ops = NULL;
  630. ops->release(pipe, buf);
  631. pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
  632. pipe->nrbufs--;
  633. if (pipe->inode)
  634. do_wakeup = 1;
  635. }
  636. if (!sd.total_len)
  637. break;
  638. }
  639. if (pipe->nrbufs)
  640. continue;
  641. if (!pipe->writers)
  642. break;
  643. if (!pipe->waiting_writers) {
  644. if (ret)
  645. break;
  646. }
  647. if (flags & SPLICE_F_NONBLOCK) {
  648. if (!ret)
  649. ret = -EAGAIN;
  650. break;
  651. }
  652. if (signal_pending(current)) {
  653. if (!ret)
  654. ret = -ERESTARTSYS;
  655. break;
  656. }
  657. if (do_wakeup) {
  658. smp_mb();
  659. if (waitqueue_active(&pipe->wait))
  660. wake_up_interruptible_sync(&pipe->wait);
  661. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  662. do_wakeup = 0;
  663. }
  664. pipe_wait(pipe);
  665. }
  666. if (pipe->inode)
  667. mutex_unlock(&pipe->inode->i_mutex);
  668. if (do_wakeup) {
  669. smp_mb();
  670. if (waitqueue_active(&pipe->wait))
  671. wake_up_interruptible(&pipe->wait);
  672. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  673. }
  674. return ret;
  675. }
  676. /**
  677. * generic_file_splice_write - splice data from a pipe to a file
  678. * @pipe: pipe info
  679. * @out: file to write to
  680. * @len: number of bytes to splice
  681. * @flags: splice modifier flags
  682. *
  683. * Will either move or copy pages (determined by @flags options) from
  684. * the given pipe inode to the given file.
  685. *
  686. */
  687. ssize_t
  688. generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  689. loff_t *ppos, size_t len, unsigned int flags)
  690. {
  691. struct address_space *mapping = out->f_mapping;
  692. ssize_t ret;
  693. ret = splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_file);
  694. if (ret > 0) {
  695. struct inode *inode = mapping->host;
  696. *ppos += ret;
  697. /*
  698. * If file or inode is SYNC and we actually wrote some data,
  699. * sync it.
  700. */
  701. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  702. int err;
  703. mutex_lock(&inode->i_mutex);
  704. err = generic_osync_inode(inode, mapping,
  705. OSYNC_METADATA|OSYNC_DATA);
  706. mutex_unlock(&inode->i_mutex);
  707. if (err)
  708. ret = err;
  709. }
  710. }
  711. return ret;
  712. }
  713. EXPORT_SYMBOL(generic_file_splice_write);
  714. /**
  715. * generic_splice_sendpage - splice data from a pipe to a socket
  716. * @inode: pipe inode
  717. * @out: socket to write to
  718. * @len: number of bytes to splice
  719. * @flags: splice modifier flags
  720. *
  721. * Will send @len bytes from the pipe to a network socket. No data copying
  722. * is involved.
  723. *
  724. */
  725. ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
  726. loff_t *ppos, size_t len, unsigned int flags)
  727. {
  728. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
  729. }
  730. EXPORT_SYMBOL(generic_splice_sendpage);
  731. /*
  732. * Attempt to initiate a splice from pipe to file.
  733. */
  734. static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  735. loff_t *ppos, size_t len, unsigned int flags)
  736. {
  737. int ret;
  738. if (unlikely(!out->f_op || !out->f_op->splice_write))
  739. return -EINVAL;
  740. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  741. return -EBADF;
  742. ret = rw_verify_area(WRITE, out, ppos, len);
  743. if (unlikely(ret < 0))
  744. return ret;
  745. return out->f_op->splice_write(pipe, out, ppos, len, flags);
  746. }
  747. /*
  748. * Attempt to initiate a splice from a file to a pipe.
  749. */
  750. static long do_splice_to(struct file *in, loff_t *ppos,
  751. struct pipe_inode_info *pipe, size_t len,
  752. unsigned int flags)
  753. {
  754. loff_t isize, left;
  755. int ret;
  756. if (unlikely(!in->f_op || !in->f_op->splice_read))
  757. return -EINVAL;
  758. if (unlikely(!(in->f_mode & FMODE_READ)))
  759. return -EBADF;
  760. ret = rw_verify_area(READ, in, ppos, len);
  761. if (unlikely(ret < 0))
  762. return ret;
  763. isize = i_size_read(in->f_mapping->host);
  764. if (unlikely(*ppos >= isize))
  765. return 0;
  766. left = isize - *ppos;
  767. if (unlikely(left < len))
  768. len = left;
  769. return in->f_op->splice_read(in, ppos, pipe, len, flags);
  770. }
  771. long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
  772. size_t len, unsigned int flags)
  773. {
  774. struct pipe_inode_info *pipe;
  775. long ret, bytes;
  776. loff_t out_off;
  777. umode_t i_mode;
  778. int i;
  779. /*
  780. * We require the input being a regular file, as we don't want to
  781. * randomly drop data for eg socket -> socket splicing. Use the
  782. * piped splicing for that!
  783. */
  784. i_mode = in->f_dentry->d_inode->i_mode;
  785. if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  786. return -EINVAL;
  787. /*
  788. * neither in nor out is a pipe, setup an internal pipe attached to
  789. * 'out' and transfer the wanted data from 'in' to 'out' through that
  790. */
  791. pipe = current->splice_pipe;
  792. if (unlikely(!pipe)) {
  793. pipe = alloc_pipe_info(NULL);
  794. if (!pipe)
  795. return -ENOMEM;
  796. /*
  797. * We don't have an immediate reader, but we'll read the stuff
  798. * out of the pipe right after the splice_to_pipe(). So set
  799. * PIPE_READERS appropriately.
  800. */
  801. pipe->readers = 1;
  802. current->splice_pipe = pipe;
  803. }
  804. /*
  805. * Do the splice.
  806. */
  807. ret = 0;
  808. bytes = 0;
  809. out_off = 0;
  810. while (len) {
  811. size_t read_len, max_read_len;
  812. /*
  813. * Do at most PIPE_BUFFERS pages worth of transfer:
  814. */
  815. max_read_len = min(len, (size_t)(PIPE_BUFFERS*PAGE_SIZE));
  816. ret = do_splice_to(in, ppos, pipe, max_read_len, flags);
  817. if (unlikely(ret < 0))
  818. goto out_release;
  819. read_len = ret;
  820. /*
  821. * NOTE: nonblocking mode only applies to the input. We
  822. * must not do the output in nonblocking mode as then we
  823. * could get stuck data in the internal pipe:
  824. */
  825. ret = do_splice_from(pipe, out, &out_off, read_len,
  826. flags & ~SPLICE_F_NONBLOCK);
  827. if (unlikely(ret < 0))
  828. goto out_release;
  829. bytes += ret;
  830. len -= ret;
  831. /*
  832. * In nonblocking mode, if we got back a short read then
  833. * that was due to either an IO error or due to the
  834. * pagecache entry not being there. In the IO error case
  835. * the _next_ splice attempt will produce a clean IO error
  836. * return value (not a short read), so in both cases it's
  837. * correct to break out of the loop here:
  838. */
  839. if ((flags & SPLICE_F_NONBLOCK) && (read_len < max_read_len))
  840. break;
  841. }
  842. pipe->nrbufs = pipe->curbuf = 0;
  843. return bytes;
  844. out_release:
  845. /*
  846. * If we did an incomplete transfer we must release
  847. * the pipe buffers in question:
  848. */
  849. for (i = 0; i < PIPE_BUFFERS; i++) {
  850. struct pipe_buffer *buf = pipe->bufs + i;
  851. if (buf->ops) {
  852. buf->ops->release(pipe, buf);
  853. buf->ops = NULL;
  854. }
  855. }
  856. pipe->nrbufs = pipe->curbuf = 0;
  857. /*
  858. * If we transferred some data, return the number of bytes:
  859. */
  860. if (bytes > 0)
  861. return bytes;
  862. return ret;
  863. }
  864. EXPORT_SYMBOL(do_splice_direct);
  865. /*
  866. * Determine where to splice to/from.
  867. */
  868. static long do_splice(struct file *in, loff_t __user *off_in,
  869. struct file *out, loff_t __user *off_out,
  870. size_t len, unsigned int flags)
  871. {
  872. struct pipe_inode_info *pipe;
  873. loff_t offset, *off;
  874. long ret;
  875. pipe = in->f_dentry->d_inode->i_pipe;
  876. if (pipe) {
  877. if (off_in)
  878. return -ESPIPE;
  879. if (off_out) {
  880. if (out->f_op->llseek == no_llseek)
  881. return -EINVAL;
  882. if (copy_from_user(&offset, off_out, sizeof(loff_t)))
  883. return -EFAULT;
  884. off = &offset;
  885. } else
  886. off = &out->f_pos;
  887. ret = do_splice_from(pipe, out, off, len, flags);
  888. if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
  889. ret = -EFAULT;
  890. return ret;
  891. }
  892. pipe = out->f_dentry->d_inode->i_pipe;
  893. if (pipe) {
  894. if (off_out)
  895. return -ESPIPE;
  896. if (off_in) {
  897. if (in->f_op->llseek == no_llseek)
  898. return -EINVAL;
  899. if (copy_from_user(&offset, off_in, sizeof(loff_t)))
  900. return -EFAULT;
  901. off = &offset;
  902. } else
  903. off = &in->f_pos;
  904. ret = do_splice_to(in, off, pipe, len, flags);
  905. if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
  906. ret = -EFAULT;
  907. return ret;
  908. }
  909. return -EINVAL;
  910. }
  911. /*
  912. * Map an iov into an array of pages and offset/length tupples. With the
  913. * partial_page structure, we can map several non-contiguous ranges into
  914. * our ones pages[] map instead of splitting that operation into pieces.
  915. * Could easily be exported as a generic helper for other users, in which
  916. * case one would probably want to add a 'max_nr_pages' parameter as well.
  917. */
  918. static int get_iovec_page_array(const struct iovec __user *iov,
  919. unsigned int nr_vecs, struct page **pages,
  920. struct partial_page *partial, int aligned)
  921. {
  922. int buffers = 0, error = 0;
  923. /*
  924. * It's ok to take the mmap_sem for reading, even
  925. * across a "get_user()".
  926. */
  927. down_read(&current->mm->mmap_sem);
  928. while (nr_vecs) {
  929. unsigned long off, npages;
  930. void __user *base;
  931. size_t len;
  932. int i;
  933. /*
  934. * Get user address base and length for this iovec.
  935. */
  936. error = get_user(base, &iov->iov_base);
  937. if (unlikely(error))
  938. break;
  939. error = get_user(len, &iov->iov_len);
  940. if (unlikely(error))
  941. break;
  942. /*
  943. * Sanity check this iovec. 0 read succeeds.
  944. */
  945. if (unlikely(!len))
  946. break;
  947. error = -EFAULT;
  948. if (unlikely(!base))
  949. break;
  950. /*
  951. * Get this base offset and number of pages, then map
  952. * in the user pages.
  953. */
  954. off = (unsigned long) base & ~PAGE_MASK;
  955. /*
  956. * If asked for alignment, the offset must be zero and the
  957. * length a multiple of the PAGE_SIZE.
  958. */
  959. error = -EINVAL;
  960. if (aligned && (off || len & ~PAGE_MASK))
  961. break;
  962. npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  963. if (npages > PIPE_BUFFERS - buffers)
  964. npages = PIPE_BUFFERS - buffers;
  965. error = get_user_pages(current, current->mm,
  966. (unsigned long) base, npages, 0, 0,
  967. &pages[buffers], NULL);
  968. if (unlikely(error <= 0))
  969. break;
  970. /*
  971. * Fill this contiguous range into the partial page map.
  972. */
  973. for (i = 0; i < error; i++) {
  974. const int plen = min_t(size_t, len, PAGE_SIZE) - off;
  975. partial[buffers].offset = off;
  976. partial[buffers].len = plen;
  977. off = 0;
  978. len -= plen;
  979. buffers++;
  980. }
  981. /*
  982. * We didn't complete this iov, stop here since it probably
  983. * means we have to move some of this into a pipe to
  984. * be able to continue.
  985. */
  986. if (len)
  987. break;
  988. /*
  989. * Don't continue if we mapped fewer pages than we asked for,
  990. * or if we mapped the max number of pages that we have
  991. * room for.
  992. */
  993. if (error < npages || buffers == PIPE_BUFFERS)
  994. break;
  995. nr_vecs--;
  996. iov++;
  997. }
  998. up_read(&current->mm->mmap_sem);
  999. if (buffers)
  1000. return buffers;
  1001. return error;
  1002. }
  1003. /*
  1004. * vmsplice splices a user address range into a pipe. It can be thought of
  1005. * as splice-from-memory, where the regular splice is splice-from-file (or
  1006. * to file). In both cases the output is a pipe, naturally.
  1007. *
  1008. * Note that vmsplice only supports splicing _from_ user memory to a pipe,
  1009. * not the other way around. Splicing from user memory is a simple operation
  1010. * that can be supported without any funky alignment restrictions or nasty
  1011. * vm tricks. We simply map in the user memory and fill them into a pipe.
  1012. * The reverse isn't quite as easy, though. There are two possible solutions
  1013. * for that:
  1014. *
  1015. * - memcpy() the data internally, at which point we might as well just
  1016. * do a regular read() on the buffer anyway.
  1017. * - Lots of nasty vm tricks, that are neither fast nor flexible (it
  1018. * has restriction limitations on both ends of the pipe).
  1019. *
  1020. * Alas, it isn't here.
  1021. *
  1022. */
  1023. static long do_vmsplice(struct file *file, const struct iovec __user *iov,
  1024. unsigned long nr_segs, unsigned int flags)
  1025. {
  1026. struct pipe_inode_info *pipe = file->f_dentry->d_inode->i_pipe;
  1027. struct page *pages[PIPE_BUFFERS];
  1028. struct partial_page partial[PIPE_BUFFERS];
  1029. struct splice_pipe_desc spd = {
  1030. .pages = pages,
  1031. .partial = partial,
  1032. .flags = flags,
  1033. .ops = &user_page_pipe_buf_ops,
  1034. };
  1035. if (unlikely(!pipe))
  1036. return -EBADF;
  1037. if (unlikely(nr_segs > UIO_MAXIOV))
  1038. return -EINVAL;
  1039. else if (unlikely(!nr_segs))
  1040. return 0;
  1041. spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
  1042. flags & SPLICE_F_GIFT);
  1043. if (spd.nr_pages <= 0)
  1044. return spd.nr_pages;
  1045. return splice_to_pipe(pipe, &spd);
  1046. }
  1047. asmlinkage long sys_vmsplice(int fd, const struct iovec __user *iov,
  1048. unsigned long nr_segs, unsigned int flags)
  1049. {
  1050. struct file *file;
  1051. long error;
  1052. int fput;
  1053. error = -EBADF;
  1054. file = fget_light(fd, &fput);
  1055. if (file) {
  1056. if (file->f_mode & FMODE_WRITE)
  1057. error = do_vmsplice(file, iov, nr_segs, flags);
  1058. fput_light(file, fput);
  1059. }
  1060. return error;
  1061. }
  1062. asmlinkage long sys_splice(int fd_in, loff_t __user *off_in,
  1063. int fd_out, loff_t __user *off_out,
  1064. size_t len, unsigned int flags)
  1065. {
  1066. long error;
  1067. struct file *in, *out;
  1068. int fput_in, fput_out;
  1069. if (unlikely(!len))
  1070. return 0;
  1071. error = -EBADF;
  1072. in = fget_light(fd_in, &fput_in);
  1073. if (in) {
  1074. if (in->f_mode & FMODE_READ) {
  1075. out = fget_light(fd_out, &fput_out);
  1076. if (out) {
  1077. if (out->f_mode & FMODE_WRITE)
  1078. error = do_splice(in, off_in,
  1079. out, off_out,
  1080. len, flags);
  1081. fput_light(out, fput_out);
  1082. }
  1083. }
  1084. fput_light(in, fput_in);
  1085. }
  1086. return error;
  1087. }
  1088. /*
  1089. * Link contents of ipipe to opipe.
  1090. */
  1091. static int link_pipe(struct pipe_inode_info *ipipe,
  1092. struct pipe_inode_info *opipe,
  1093. size_t len, unsigned int flags)
  1094. {
  1095. struct pipe_buffer *ibuf, *obuf;
  1096. int ret, do_wakeup, i, ipipe_first;
  1097. ret = do_wakeup = ipipe_first = 0;
  1098. /*
  1099. * Potential ABBA deadlock, work around it by ordering lock
  1100. * grabbing by inode address. Otherwise two different processes
  1101. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1102. */
  1103. if (ipipe->inode < opipe->inode) {
  1104. ipipe_first = 1;
  1105. mutex_lock(&ipipe->inode->i_mutex);
  1106. mutex_lock(&opipe->inode->i_mutex);
  1107. } else {
  1108. mutex_lock(&opipe->inode->i_mutex);
  1109. mutex_lock(&ipipe->inode->i_mutex);
  1110. }
  1111. for (i = 0;; i++) {
  1112. if (!opipe->readers) {
  1113. send_sig(SIGPIPE, current, 0);
  1114. if (!ret)
  1115. ret = -EPIPE;
  1116. break;
  1117. }
  1118. if (ipipe->nrbufs - i) {
  1119. ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
  1120. /*
  1121. * If we have room, fill this buffer
  1122. */
  1123. if (opipe->nrbufs < PIPE_BUFFERS) {
  1124. int nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
  1125. /*
  1126. * Get a reference to this pipe buffer,
  1127. * so we can copy the contents over.
  1128. */
  1129. ibuf->ops->get(ipipe, ibuf);
  1130. obuf = opipe->bufs + nbuf;
  1131. *obuf = *ibuf;
  1132. /*
  1133. * Don't inherit the gift flag, we need to
  1134. * prevent multiple steals of this page.
  1135. */
  1136. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1137. if (obuf->len > len)
  1138. obuf->len = len;
  1139. opipe->nrbufs++;
  1140. do_wakeup = 1;
  1141. ret += obuf->len;
  1142. len -= obuf->len;
  1143. if (!len)
  1144. break;
  1145. if (opipe->nrbufs < PIPE_BUFFERS)
  1146. continue;
  1147. }
  1148. /*
  1149. * We have input available, but no output room.
  1150. * If we already copied data, return that. If we
  1151. * need to drop the opipe lock, it must be ordered
  1152. * last to avoid deadlocks.
  1153. */
  1154. if ((flags & SPLICE_F_NONBLOCK) || !ipipe_first) {
  1155. if (!ret)
  1156. ret = -EAGAIN;
  1157. break;
  1158. }
  1159. if (signal_pending(current)) {
  1160. if (!ret)
  1161. ret = -ERESTARTSYS;
  1162. break;
  1163. }
  1164. if (do_wakeup) {
  1165. smp_mb();
  1166. if (waitqueue_active(&opipe->wait))
  1167. wake_up_interruptible(&opipe->wait);
  1168. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  1169. do_wakeup = 0;
  1170. }
  1171. opipe->waiting_writers++;
  1172. pipe_wait(opipe);
  1173. opipe->waiting_writers--;
  1174. continue;
  1175. }
  1176. /*
  1177. * No input buffers, do the usual checks for available
  1178. * writers and blocking and wait if necessary
  1179. */
  1180. if (!ipipe->writers)
  1181. break;
  1182. if (!ipipe->waiting_writers) {
  1183. if (ret)
  1184. break;
  1185. }
  1186. /*
  1187. * pipe_wait() drops the ipipe mutex. To avoid deadlocks
  1188. * with another process, we can only safely do that if
  1189. * the ipipe lock is ordered last.
  1190. */
  1191. if ((flags & SPLICE_F_NONBLOCK) || ipipe_first) {
  1192. if (!ret)
  1193. ret = -EAGAIN;
  1194. break;
  1195. }
  1196. if (signal_pending(current)) {
  1197. if (!ret)
  1198. ret = -ERESTARTSYS;
  1199. break;
  1200. }
  1201. if (waitqueue_active(&ipipe->wait))
  1202. wake_up_interruptible_sync(&ipipe->wait);
  1203. kill_fasync(&ipipe->fasync_writers, SIGIO, POLL_OUT);
  1204. pipe_wait(ipipe);
  1205. }
  1206. mutex_unlock(&ipipe->inode->i_mutex);
  1207. mutex_unlock(&opipe->inode->i_mutex);
  1208. if (do_wakeup) {
  1209. smp_mb();
  1210. if (waitqueue_active(&opipe->wait))
  1211. wake_up_interruptible(&opipe->wait);
  1212. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  1213. }
  1214. return ret;
  1215. }
  1216. /*
  1217. * This is a tee(1) implementation that works on pipes. It doesn't copy
  1218. * any data, it simply references the 'in' pages on the 'out' pipe.
  1219. * The 'flags' used are the SPLICE_F_* variants, currently the only
  1220. * applicable one is SPLICE_F_NONBLOCK.
  1221. */
  1222. static long do_tee(struct file *in, struct file *out, size_t len,
  1223. unsigned int flags)
  1224. {
  1225. struct pipe_inode_info *ipipe = in->f_dentry->d_inode->i_pipe;
  1226. struct pipe_inode_info *opipe = out->f_dentry->d_inode->i_pipe;
  1227. /*
  1228. * Link ipipe to the two output pipes, consuming as we go along.
  1229. */
  1230. if (ipipe && opipe)
  1231. return link_pipe(ipipe, opipe, len, flags);
  1232. return -EINVAL;
  1233. }
  1234. asmlinkage long sys_tee(int fdin, int fdout, size_t len, unsigned int flags)
  1235. {
  1236. struct file *in;
  1237. int error, fput_in;
  1238. if (unlikely(!len))
  1239. return 0;
  1240. error = -EBADF;
  1241. in = fget_light(fdin, &fput_in);
  1242. if (in) {
  1243. if (in->f_mode & FMODE_READ) {
  1244. int fput_out;
  1245. struct file *out = fget_light(fdout, &fput_out);
  1246. if (out) {
  1247. if (out->f_mode & FMODE_WRITE)
  1248. error = do_tee(in, out, len, flags);
  1249. fput_light(out, fput_out);
  1250. }
  1251. }
  1252. fput_light(in, fput_in);
  1253. }
  1254. return error;
  1255. }