splice.c 34 KB

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