splice.c 35 KB

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