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