splice.c 33 KB

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