splice.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111
  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/splice.h>
  24. #include <linux/memcontrol.h>
  25. #include <linux/mm_inline.h>
  26. #include <linux/swap.h>
  27. #include <linux/writeback.h>
  28. #include <linux/buffer_head.h>
  29. #include <linux/module.h>
  30. #include <linux/syscalls.h>
  31. #include <linux/uio.h>
  32. #include <linux/security.h>
  33. /*
  34. * Attempt to steal a page from a pipe buffer. This should perhaps go into
  35. * a vm helper function, it's already simplified quite a bit by the
  36. * addition of remove_mapping(). If success is returned, the caller may
  37. * attempt to reuse this page for another destination.
  38. */
  39. static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
  40. struct pipe_buffer *buf)
  41. {
  42. struct page *page = buf->page;
  43. struct address_space *mapping;
  44. lock_page(page);
  45. mapping = page_mapping(page);
  46. if (mapping) {
  47. WARN_ON(!PageUptodate(page));
  48. /*
  49. * At least for ext2 with nobh option, we need to wait on
  50. * writeback completing on this page, since we'll remove it
  51. * from the pagecache. Otherwise truncate wont wait on the
  52. * page, allowing the disk blocks to be reused by someone else
  53. * before we actually wrote our data to them. fs corruption
  54. * ensues.
  55. */
  56. wait_on_page_writeback(page);
  57. if (page_has_private(page) &&
  58. !try_to_release_page(page, GFP_KERNEL))
  59. goto out_unlock;
  60. /*
  61. * If we succeeded in removing the mapping, set LRU flag
  62. * and return good.
  63. */
  64. if (remove_mapping(mapping, page)) {
  65. buf->flags |= PIPE_BUF_FLAG_LRU;
  66. return 0;
  67. }
  68. }
  69. /*
  70. * Raced with truncate or failed to remove page from current
  71. * address space, unlock and return failure.
  72. */
  73. out_unlock:
  74. unlock_page(page);
  75. return 1;
  76. }
  77. static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
  78. struct pipe_buffer *buf)
  79. {
  80. page_cache_release(buf->page);
  81. buf->flags &= ~PIPE_BUF_FLAG_LRU;
  82. }
  83. /*
  84. * Check whether the contents of buf is OK to access. Since the content
  85. * is a page cache page, IO may be in flight.
  86. */
  87. static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
  88. struct pipe_buffer *buf)
  89. {
  90. struct page *page = buf->page;
  91. int err;
  92. if (!PageUptodate(page)) {
  93. lock_page(page);
  94. /*
  95. * Page got truncated/unhashed. This will cause a 0-byte
  96. * splice, if this is the first page.
  97. */
  98. if (!page->mapping) {
  99. err = -ENODATA;
  100. goto error;
  101. }
  102. /*
  103. * Uh oh, read-error from disk.
  104. */
  105. if (!PageUptodate(page)) {
  106. err = -EIO;
  107. goto error;
  108. }
  109. /*
  110. * Page is ok afterall, we are done.
  111. */
  112. unlock_page(page);
  113. }
  114. return 0;
  115. error:
  116. unlock_page(page);
  117. return err;
  118. }
  119. static const struct pipe_buf_operations page_cache_pipe_buf_ops = {
  120. .can_merge = 0,
  121. .map = generic_pipe_buf_map,
  122. .unmap = generic_pipe_buf_unmap,
  123. .confirm = page_cache_pipe_buf_confirm,
  124. .release = page_cache_pipe_buf_release,
  125. .steal = page_cache_pipe_buf_steal,
  126. .get = generic_pipe_buf_get,
  127. };
  128. static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
  129. struct pipe_buffer *buf)
  130. {
  131. if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
  132. return 1;
  133. buf->flags |= PIPE_BUF_FLAG_LRU;
  134. return generic_pipe_buf_steal(pipe, buf);
  135. }
  136. static const struct pipe_buf_operations user_page_pipe_buf_ops = {
  137. .can_merge = 0,
  138. .map = generic_pipe_buf_map,
  139. .unmap = generic_pipe_buf_unmap,
  140. .confirm = generic_pipe_buf_confirm,
  141. .release = page_cache_pipe_buf_release,
  142. .steal = user_page_pipe_buf_steal,
  143. .get = generic_pipe_buf_get,
  144. };
  145. /**
  146. * splice_to_pipe - fill passed data into a pipe
  147. * @pipe: pipe to fill
  148. * @spd: data to fill
  149. *
  150. * Description:
  151. * @spd contains a map of pages and len/offset tuples, along with
  152. * the struct pipe_buf_operations associated with these pages. This
  153. * function will link that data to the pipe.
  154. *
  155. */
  156. ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
  157. struct splice_pipe_desc *spd)
  158. {
  159. unsigned int spd_pages = spd->nr_pages;
  160. int ret, do_wakeup, page_nr;
  161. ret = 0;
  162. do_wakeup = 0;
  163. page_nr = 0;
  164. pipe_lock(pipe);
  165. for (;;) {
  166. if (!pipe->readers) {
  167. send_sig(SIGPIPE, current, 0);
  168. if (!ret)
  169. ret = -EPIPE;
  170. break;
  171. }
  172. if (pipe->nrbufs < PIPE_BUFFERS) {
  173. int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1);
  174. struct pipe_buffer *buf = pipe->bufs + newbuf;
  175. buf->page = spd->pages[page_nr];
  176. buf->offset = spd->partial[page_nr].offset;
  177. buf->len = spd->partial[page_nr].len;
  178. buf->private = spd->partial[page_nr].private;
  179. buf->ops = spd->ops;
  180. if (spd->flags & SPLICE_F_GIFT)
  181. buf->flags |= PIPE_BUF_FLAG_GIFT;
  182. pipe->nrbufs++;
  183. page_nr++;
  184. ret += buf->len;
  185. if (pipe->inode)
  186. do_wakeup = 1;
  187. if (!--spd->nr_pages)
  188. break;
  189. if (pipe->nrbufs < PIPE_BUFFERS)
  190. continue;
  191. break;
  192. }
  193. if (spd->flags & SPLICE_F_NONBLOCK) {
  194. if (!ret)
  195. ret = -EAGAIN;
  196. break;
  197. }
  198. if (signal_pending(current)) {
  199. if (!ret)
  200. ret = -ERESTARTSYS;
  201. break;
  202. }
  203. if (do_wakeup) {
  204. smp_mb();
  205. if (waitqueue_active(&pipe->wait))
  206. wake_up_interruptible_sync(&pipe->wait);
  207. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  208. do_wakeup = 0;
  209. }
  210. pipe->waiting_writers++;
  211. pipe_wait(pipe);
  212. pipe->waiting_writers--;
  213. }
  214. pipe_unlock(pipe);
  215. if (do_wakeup) {
  216. smp_mb();
  217. if (waitqueue_active(&pipe->wait))
  218. wake_up_interruptible(&pipe->wait);
  219. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  220. }
  221. while (page_nr < spd_pages)
  222. spd->spd_release(spd, page_nr++);
  223. return ret;
  224. }
  225. static void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
  226. {
  227. page_cache_release(spd->pages[i]);
  228. }
  229. static int
  230. __generic_file_splice_read(struct file *in, loff_t *ppos,
  231. struct pipe_inode_info *pipe, size_t len,
  232. unsigned int flags)
  233. {
  234. struct address_space *mapping = in->f_mapping;
  235. unsigned int loff, nr_pages, req_pages;
  236. struct page *pages[PIPE_BUFFERS];
  237. struct partial_page partial[PIPE_BUFFERS];
  238. struct page *page;
  239. pgoff_t index, end_index;
  240. loff_t isize;
  241. int error, page_nr;
  242. struct splice_pipe_desc spd = {
  243. .pages = pages,
  244. .partial = partial,
  245. .flags = flags,
  246. .ops = &page_cache_pipe_buf_ops,
  247. .spd_release = spd_release_page,
  248. };
  249. index = *ppos >> PAGE_CACHE_SHIFT;
  250. loff = *ppos & ~PAGE_CACHE_MASK;
  251. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  252. nr_pages = min(req_pages, (unsigned)PIPE_BUFFERS);
  253. /*
  254. * Lookup the (hopefully) full range of pages we need.
  255. */
  256. spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages);
  257. index += spd.nr_pages;
  258. /*
  259. * If find_get_pages_contig() returned fewer pages than we needed,
  260. * readahead/allocate the rest and fill in the holes.
  261. */
  262. if (spd.nr_pages < nr_pages)
  263. page_cache_sync_readahead(mapping, &in->f_ra, in,
  264. index, req_pages - spd.nr_pages);
  265. error = 0;
  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. * page didn't exist, allocate one.
  275. */
  276. page = page_cache_alloc_cold(mapping);
  277. if (!page)
  278. break;
  279. error = add_to_page_cache_lru(page, mapping, index,
  280. mapping_gfp_mask(mapping));
  281. if (unlikely(error)) {
  282. page_cache_release(page);
  283. if (error == -EEXIST)
  284. continue;
  285. break;
  286. }
  287. /*
  288. * add_to_page_cache() locks the page, unlock it
  289. * to avoid convoluting the logic below even more.
  290. */
  291. unlock_page(page);
  292. }
  293. pages[spd.nr_pages++] = page;
  294. index++;
  295. }
  296. /*
  297. * Now loop over the map and see if we need to start IO on any
  298. * pages, fill in the partial map, etc.
  299. */
  300. index = *ppos >> PAGE_CACHE_SHIFT;
  301. nr_pages = spd.nr_pages;
  302. spd.nr_pages = 0;
  303. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  304. unsigned int this_len;
  305. if (!len)
  306. break;
  307. /*
  308. * this_len is the max we'll use from this page
  309. */
  310. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  311. page = pages[page_nr];
  312. if (PageReadahead(page))
  313. page_cache_async_readahead(mapping, &in->f_ra, in,
  314. page, index, req_pages - page_nr);
  315. /*
  316. * If the page isn't uptodate, we may need to start io on it
  317. */
  318. if (!PageUptodate(page)) {
  319. /*
  320. * If in nonblock mode then dont block on waiting
  321. * for an in-flight io page
  322. */
  323. if (flags & SPLICE_F_NONBLOCK) {
  324. if (!trylock_page(page)) {
  325. error = -EAGAIN;
  326. break;
  327. }
  328. } else
  329. lock_page(page);
  330. /*
  331. * Page was truncated, or invalidated by the
  332. * filesystem. Redo the find/create, but this time the
  333. * page is kept locked, so there's no chance of another
  334. * race with truncate/invalidate.
  335. */
  336. if (!page->mapping) {
  337. unlock_page(page);
  338. page = find_or_create_page(mapping, index,
  339. mapping_gfp_mask(mapping));
  340. if (!page) {
  341. error = -ENOMEM;
  342. break;
  343. }
  344. page_cache_release(pages[page_nr]);
  345. pages[page_nr] = page;
  346. }
  347. /*
  348. * page was already under io and is now done, great
  349. */
  350. if (PageUptodate(page)) {
  351. unlock_page(page);
  352. goto fill_it;
  353. }
  354. /*
  355. * need to read in the page
  356. */
  357. error = mapping->a_ops->readpage(in, page);
  358. if (unlikely(error)) {
  359. /*
  360. * We really should re-lookup the page here,
  361. * but it complicates things a lot. Instead
  362. * lets just do what we already stored, and
  363. * we'll get it the next time we are called.
  364. */
  365. if (error == AOP_TRUNCATED_PAGE)
  366. error = 0;
  367. break;
  368. }
  369. }
  370. fill_it:
  371. /*
  372. * i_size must be checked after PageUptodate.
  373. */
  374. isize = i_size_read(mapping->host);
  375. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  376. if (unlikely(!isize || index > end_index))
  377. break;
  378. /*
  379. * if this is the last page, see if we need to shrink
  380. * the length and stop
  381. */
  382. if (end_index == index) {
  383. unsigned int plen;
  384. /*
  385. * max good bytes in this page
  386. */
  387. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  388. if (plen <= loff)
  389. break;
  390. /*
  391. * force quit after adding this page
  392. */
  393. this_len = min(this_len, plen - loff);
  394. len = this_len;
  395. }
  396. partial[page_nr].offset = loff;
  397. partial[page_nr].len = this_len;
  398. len -= this_len;
  399. loff = 0;
  400. spd.nr_pages++;
  401. index++;
  402. }
  403. /*
  404. * Release any pages at the end, if we quit early. 'page_nr' is how far
  405. * we got, 'nr_pages' is how many pages are in the map.
  406. */
  407. while (page_nr < nr_pages)
  408. page_cache_release(pages[page_nr++]);
  409. in->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT;
  410. if (spd.nr_pages)
  411. return splice_to_pipe(pipe, &spd);
  412. return error;
  413. }
  414. /**
  415. * generic_file_splice_read - splice data from file to a pipe
  416. * @in: file to splice from
  417. * @ppos: position in @in
  418. * @pipe: pipe to splice to
  419. * @len: number of bytes to splice
  420. * @flags: splice modifier flags
  421. *
  422. * Description:
  423. * Will read pages from given file and fill them into a pipe. Can be
  424. * used as long as the address_space operations for the source implements
  425. * a readpage() hook.
  426. *
  427. */
  428. ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
  429. struct pipe_inode_info *pipe, size_t len,
  430. unsigned int flags)
  431. {
  432. loff_t isize, left;
  433. int ret;
  434. isize = i_size_read(in->f_mapping->host);
  435. if (unlikely(*ppos >= isize))
  436. return 0;
  437. left = isize - *ppos;
  438. if (unlikely(left < len))
  439. len = left;
  440. ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
  441. if (ret > 0)
  442. *ppos += ret;
  443. return ret;
  444. }
  445. EXPORT_SYMBOL(generic_file_splice_read);
  446. static const struct pipe_buf_operations default_pipe_buf_ops = {
  447. .can_merge = 0,
  448. .map = generic_pipe_buf_map,
  449. .unmap = generic_pipe_buf_unmap,
  450. .confirm = generic_pipe_buf_confirm,
  451. .release = generic_pipe_buf_release,
  452. .steal = generic_pipe_buf_steal,
  453. .get = generic_pipe_buf_get,
  454. };
  455. static ssize_t kernel_readv(struct file *file, const struct iovec *vec,
  456. unsigned long vlen, loff_t offset)
  457. {
  458. mm_segment_t old_fs;
  459. loff_t pos = offset;
  460. ssize_t res;
  461. old_fs = get_fs();
  462. set_fs(get_ds());
  463. /* The cast to a user pointer is valid due to the set_fs() */
  464. res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos);
  465. set_fs(old_fs);
  466. return res;
  467. }
  468. static ssize_t kernel_writev(struct file *file, const struct iovec *vec,
  469. unsigned long vlen, loff_t *ppos)
  470. {
  471. mm_segment_t old_fs;
  472. ssize_t res;
  473. old_fs = get_fs();
  474. set_fs(get_ds());
  475. /* The cast to a user pointer is valid due to the set_fs() */
  476. res = vfs_writev(file, (const struct iovec __user *)vec, vlen, ppos);
  477. set_fs(old_fs);
  478. return res;
  479. }
  480. ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
  481. struct pipe_inode_info *pipe, size_t len,
  482. unsigned int flags)
  483. {
  484. unsigned int nr_pages;
  485. unsigned int nr_freed;
  486. size_t offset;
  487. struct page *pages[PIPE_BUFFERS];
  488. struct partial_page partial[PIPE_BUFFERS];
  489. struct iovec vec[PIPE_BUFFERS];
  490. pgoff_t index;
  491. ssize_t res;
  492. size_t this_len;
  493. int error;
  494. int i;
  495. struct splice_pipe_desc spd = {
  496. .pages = pages,
  497. .partial = partial,
  498. .flags = flags,
  499. .ops = &default_pipe_buf_ops,
  500. .spd_release = spd_release_page,
  501. };
  502. index = *ppos >> PAGE_CACHE_SHIFT;
  503. offset = *ppos & ~PAGE_CACHE_MASK;
  504. nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  505. for (i = 0; i < nr_pages && i < PIPE_BUFFERS && len; i++) {
  506. struct page *page;
  507. page = alloc_page(GFP_USER);
  508. error = -ENOMEM;
  509. if (!page)
  510. goto err;
  511. this_len = min_t(size_t, len, PAGE_CACHE_SIZE - offset);
  512. vec[i].iov_base = (void __user *) page_address(page);
  513. vec[i].iov_len = this_len;
  514. pages[i] = page;
  515. spd.nr_pages++;
  516. len -= this_len;
  517. offset = 0;
  518. }
  519. res = kernel_readv(in, vec, spd.nr_pages, *ppos);
  520. if (res < 0)
  521. goto err;
  522. error = 0;
  523. if (!res)
  524. goto err;
  525. nr_freed = 0;
  526. for (i = 0; i < spd.nr_pages; i++) {
  527. this_len = min_t(size_t, vec[i].iov_len, res);
  528. partial[i].offset = 0;
  529. partial[i].len = this_len;
  530. if (!this_len) {
  531. __free_page(pages[i]);
  532. pages[i] = NULL;
  533. nr_freed++;
  534. }
  535. res -= this_len;
  536. }
  537. spd.nr_pages -= nr_freed;
  538. res = splice_to_pipe(pipe, &spd);
  539. if (res > 0)
  540. *ppos += res;
  541. return res;
  542. err:
  543. for (i = 0; i < spd.nr_pages; i++)
  544. __free_page(pages[i]);
  545. return error;
  546. }
  547. EXPORT_SYMBOL(default_file_splice_read);
  548. /*
  549. * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
  550. * using sendpage(). Return the number of bytes sent.
  551. */
  552. static int pipe_to_sendpage(struct pipe_inode_info *pipe,
  553. struct pipe_buffer *buf, struct splice_desc *sd)
  554. {
  555. struct file *file = sd->u.file;
  556. loff_t pos = sd->pos;
  557. int ret, more;
  558. ret = buf->ops->confirm(pipe, buf);
  559. if (!ret) {
  560. more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
  561. ret = file->f_op->sendpage(file, buf->page, buf->offset,
  562. sd->len, &pos, more);
  563. }
  564. return ret;
  565. }
  566. /*
  567. * This is a little more tricky than the file -> pipe splicing. There are
  568. * basically three cases:
  569. *
  570. * - Destination page already exists in the address space and there
  571. * are users of it. For that case we have no other option that
  572. * copying the data. Tough luck.
  573. * - Destination page already exists in the address space, but there
  574. * are no users of it. Make sure it's uptodate, then drop it. Fall
  575. * through to last case.
  576. * - Destination page does not exist, we can add the pipe page to
  577. * the page cache and avoid the copy.
  578. *
  579. * If asked to move pages to the output file (SPLICE_F_MOVE is set in
  580. * sd->flags), we attempt to migrate pages from the pipe to the output
  581. * file address space page cache. This is possible if no one else has
  582. * the pipe page referenced outside of the pipe and page cache. If
  583. * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
  584. * a new page in the output file page cache and fill/dirty that.
  585. */
  586. int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  587. struct splice_desc *sd)
  588. {
  589. struct file *file = sd->u.file;
  590. struct address_space *mapping = file->f_mapping;
  591. unsigned int offset, this_len;
  592. struct page *page;
  593. void *fsdata;
  594. int ret;
  595. /*
  596. * make sure the data in this buffer is uptodate
  597. */
  598. ret = buf->ops->confirm(pipe, buf);
  599. if (unlikely(ret))
  600. return ret;
  601. offset = sd->pos & ~PAGE_CACHE_MASK;
  602. this_len = sd->len;
  603. if (this_len + offset > PAGE_CACHE_SIZE)
  604. this_len = PAGE_CACHE_SIZE - offset;
  605. ret = pagecache_write_begin(file, mapping, sd->pos, this_len,
  606. AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
  607. if (unlikely(ret))
  608. goto out;
  609. if (buf->page != page) {
  610. /*
  611. * Careful, ->map() uses KM_USER0!
  612. */
  613. char *src = buf->ops->map(pipe, buf, 1);
  614. char *dst = kmap_atomic(page, KM_USER1);
  615. memcpy(dst + offset, src + buf->offset, this_len);
  616. flush_dcache_page(page);
  617. kunmap_atomic(dst, KM_USER1);
  618. buf->ops->unmap(pipe, buf, src);
  619. }
  620. ret = pagecache_write_end(file, mapping, sd->pos, this_len, this_len,
  621. page, fsdata);
  622. out:
  623. return ret;
  624. }
  625. EXPORT_SYMBOL(pipe_to_file);
  626. static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
  627. {
  628. smp_mb();
  629. if (waitqueue_active(&pipe->wait))
  630. wake_up_interruptible(&pipe->wait);
  631. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  632. }
  633. /**
  634. * splice_from_pipe_feed - feed available data from a pipe to a file
  635. * @pipe: pipe to splice from
  636. * @sd: information to @actor
  637. * @actor: handler that splices the data
  638. *
  639. * Description:
  640. * This function loops over the pipe and calls @actor to do the
  641. * actual moving of a single struct pipe_buffer to the desired
  642. * destination. It returns when there's no more buffers left in
  643. * the pipe or if the requested number of bytes (@sd->total_len)
  644. * have been copied. It returns a positive number (one) if the
  645. * pipe needs to be filled with more data, zero if the required
  646. * number of bytes have been copied and -errno on error.
  647. *
  648. * This, together with splice_from_pipe_{begin,end,next}, may be
  649. * used to implement the functionality of __splice_from_pipe() when
  650. * locking is required around copying the pipe buffers to the
  651. * destination.
  652. */
  653. int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
  654. splice_actor *actor)
  655. {
  656. int ret;
  657. while (pipe->nrbufs) {
  658. struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  659. const struct pipe_buf_operations *ops = buf->ops;
  660. sd->len = buf->len;
  661. if (sd->len > sd->total_len)
  662. sd->len = sd->total_len;
  663. ret = actor(pipe, buf, sd);
  664. if (ret <= 0) {
  665. if (ret == -ENODATA)
  666. ret = 0;
  667. return ret;
  668. }
  669. buf->offset += ret;
  670. buf->len -= ret;
  671. sd->num_spliced += ret;
  672. sd->len -= ret;
  673. sd->pos += ret;
  674. sd->total_len -= ret;
  675. if (!buf->len) {
  676. buf->ops = NULL;
  677. ops->release(pipe, buf);
  678. pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
  679. pipe->nrbufs--;
  680. if (pipe->inode)
  681. sd->need_wakeup = true;
  682. }
  683. if (!sd->total_len)
  684. return 0;
  685. }
  686. return 1;
  687. }
  688. EXPORT_SYMBOL(splice_from_pipe_feed);
  689. /**
  690. * splice_from_pipe_next - wait for some data to splice from
  691. * @pipe: pipe to splice from
  692. * @sd: information about the splice operation
  693. *
  694. * Description:
  695. * This function will wait for some data and return a positive
  696. * value (one) if pipe buffers are available. It will return zero
  697. * or -errno if no more data needs to be spliced.
  698. */
  699. int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
  700. {
  701. while (!pipe->nrbufs) {
  702. if (!pipe->writers)
  703. return 0;
  704. if (!pipe->waiting_writers && sd->num_spliced)
  705. return 0;
  706. if (sd->flags & SPLICE_F_NONBLOCK)
  707. return -EAGAIN;
  708. if (signal_pending(current))
  709. return -ERESTARTSYS;
  710. if (sd->need_wakeup) {
  711. wakeup_pipe_writers(pipe);
  712. sd->need_wakeup = false;
  713. }
  714. pipe_wait(pipe);
  715. }
  716. return 1;
  717. }
  718. EXPORT_SYMBOL(splice_from_pipe_next);
  719. /**
  720. * splice_from_pipe_begin - start splicing from pipe
  721. * @sd: information about the splice operation
  722. *
  723. * Description:
  724. * This function should be called before a loop containing
  725. * splice_from_pipe_next() and splice_from_pipe_feed() to
  726. * initialize the necessary fields of @sd.
  727. */
  728. void splice_from_pipe_begin(struct splice_desc *sd)
  729. {
  730. sd->num_spliced = 0;
  731. sd->need_wakeup = false;
  732. }
  733. EXPORT_SYMBOL(splice_from_pipe_begin);
  734. /**
  735. * splice_from_pipe_end - finish splicing from pipe
  736. * @pipe: pipe to splice from
  737. * @sd: information about the splice operation
  738. *
  739. * Description:
  740. * This function will wake up pipe writers if necessary. It should
  741. * be called after a loop containing splice_from_pipe_next() and
  742. * splice_from_pipe_feed().
  743. */
  744. void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
  745. {
  746. if (sd->need_wakeup)
  747. wakeup_pipe_writers(pipe);
  748. }
  749. EXPORT_SYMBOL(splice_from_pipe_end);
  750. /**
  751. * __splice_from_pipe - splice data from a pipe to given actor
  752. * @pipe: pipe to splice from
  753. * @sd: information to @actor
  754. * @actor: handler that splices the data
  755. *
  756. * Description:
  757. * This function does little more than loop over the pipe and call
  758. * @actor to do the actual moving of a single struct pipe_buffer to
  759. * the desired destination. See pipe_to_file, pipe_to_sendpage, or
  760. * pipe_to_user.
  761. *
  762. */
  763. ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
  764. splice_actor *actor)
  765. {
  766. int ret;
  767. splice_from_pipe_begin(sd);
  768. do {
  769. ret = splice_from_pipe_next(pipe, sd);
  770. if (ret > 0)
  771. ret = splice_from_pipe_feed(pipe, sd, actor);
  772. } while (ret > 0);
  773. splice_from_pipe_end(pipe, sd);
  774. return sd->num_spliced ? sd->num_spliced : ret;
  775. }
  776. EXPORT_SYMBOL(__splice_from_pipe);
  777. /**
  778. * splice_from_pipe - splice data from a pipe to a file
  779. * @pipe: pipe to splice from
  780. * @out: file to splice to
  781. * @ppos: position in @out
  782. * @len: how many bytes to splice
  783. * @flags: splice modifier flags
  784. * @actor: handler that splices the data
  785. *
  786. * Description:
  787. * See __splice_from_pipe. This function locks the pipe inode,
  788. * otherwise it's identical to __splice_from_pipe().
  789. *
  790. */
  791. ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
  792. loff_t *ppos, size_t len, unsigned int flags,
  793. splice_actor *actor)
  794. {
  795. ssize_t ret;
  796. struct splice_desc sd = {
  797. .total_len = len,
  798. .flags = flags,
  799. .pos = *ppos,
  800. .u.file = out,
  801. };
  802. pipe_lock(pipe);
  803. ret = __splice_from_pipe(pipe, &sd, actor);
  804. pipe_unlock(pipe);
  805. return ret;
  806. }
  807. /**
  808. * generic_file_splice_write - splice data from a pipe to a file
  809. * @pipe: pipe info
  810. * @out: file to write to
  811. * @ppos: position in @out
  812. * @len: number of bytes to splice
  813. * @flags: splice modifier flags
  814. *
  815. * Description:
  816. * Will either move or copy pages (determined by @flags options) from
  817. * the given pipe inode to the given file.
  818. *
  819. */
  820. ssize_t
  821. generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  822. loff_t *ppos, size_t len, unsigned int flags)
  823. {
  824. struct address_space *mapping = out->f_mapping;
  825. struct inode *inode = mapping->host;
  826. struct splice_desc sd = {
  827. .total_len = len,
  828. .flags = flags,
  829. .pos = *ppos,
  830. .u.file = out,
  831. };
  832. ssize_t ret;
  833. pipe_lock(pipe);
  834. splice_from_pipe_begin(&sd);
  835. do {
  836. ret = splice_from_pipe_next(pipe, &sd);
  837. if (ret <= 0)
  838. break;
  839. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  840. ret = file_remove_suid(out);
  841. if (!ret)
  842. ret = splice_from_pipe_feed(pipe, &sd, pipe_to_file);
  843. mutex_unlock(&inode->i_mutex);
  844. } while (ret > 0);
  845. splice_from_pipe_end(pipe, &sd);
  846. pipe_unlock(pipe);
  847. if (sd.num_spliced)
  848. ret = sd.num_spliced;
  849. if (ret > 0) {
  850. unsigned long nr_pages;
  851. *ppos += ret;
  852. nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  853. /*
  854. * If file or inode is SYNC and we actually wrote some data,
  855. * sync it.
  856. */
  857. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  858. int err;
  859. mutex_lock(&inode->i_mutex);
  860. err = generic_osync_inode(inode, mapping,
  861. OSYNC_METADATA|OSYNC_DATA);
  862. mutex_unlock(&inode->i_mutex);
  863. if (err)
  864. ret = err;
  865. }
  866. balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
  867. }
  868. return ret;
  869. }
  870. EXPORT_SYMBOL(generic_file_splice_write);
  871. static struct pipe_buffer *nth_pipe_buf(struct pipe_inode_info *pipe, int n)
  872. {
  873. return &pipe->bufs[(pipe->curbuf + n) % PIPE_BUFFERS];
  874. }
  875. static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
  876. struct file *out, loff_t *ppos,
  877. size_t len, unsigned int flags)
  878. {
  879. ssize_t ret = 0;
  880. ssize_t total_len = 0;
  881. int do_wakeup = 0;
  882. pipe_lock(pipe);
  883. while (len) {
  884. struct pipe_buffer *buf;
  885. void *data[PIPE_BUFFERS];
  886. struct iovec vec[PIPE_BUFFERS];
  887. unsigned int nr_pages = 0;
  888. unsigned int write_len = 0;
  889. unsigned int now_len = len;
  890. unsigned int this_len;
  891. int i;
  892. BUG_ON(pipe->nrbufs > PIPE_BUFFERS);
  893. for (i = 0; i < pipe->nrbufs && now_len; i++) {
  894. buf = nth_pipe_buf(pipe, i);
  895. ret = buf->ops->confirm(pipe, buf);
  896. if (ret)
  897. break;
  898. data[i] = buf->ops->map(pipe, buf, 0);
  899. this_len = min(buf->len, now_len);
  900. vec[i].iov_base = (void __user *) data[i] + buf->offset;
  901. vec[i].iov_len = this_len;
  902. now_len -= this_len;
  903. write_len += this_len;
  904. nr_pages++;
  905. }
  906. if (nr_pages) {
  907. ret = kernel_writev(out, vec, nr_pages, ppos);
  908. if (ret == 0)
  909. ret = -EIO;
  910. if (ret > 0) {
  911. len -= ret;
  912. total_len += ret;
  913. }
  914. }
  915. for (i = 0; i < nr_pages; i++) {
  916. buf = nth_pipe_buf(pipe, i);
  917. buf->ops->unmap(pipe, buf, data[i]);
  918. if (ret > 0) {
  919. this_len = min_t(unsigned, vec[i].iov_len, ret);
  920. buf->offset += this_len;
  921. buf->len -= this_len;
  922. ret -= this_len;
  923. }
  924. }
  925. if (ret < 0)
  926. break;
  927. while (pipe->nrbufs) {
  928. const struct pipe_buf_operations *ops;
  929. buf = nth_pipe_buf(pipe, 0);
  930. if (buf->len)
  931. break;
  932. ops = buf->ops;
  933. buf->ops = NULL;
  934. ops->release(pipe, buf);
  935. pipe->curbuf = (pipe->curbuf + 1) % PIPE_BUFFERS;
  936. pipe->nrbufs--;
  937. if (pipe->inode)
  938. do_wakeup = 1;
  939. }
  940. if (pipe->nrbufs)
  941. continue;
  942. if (!pipe->writers)
  943. break;
  944. if (!pipe->waiting_writers) {
  945. if (total_len)
  946. break;
  947. }
  948. if (flags & SPLICE_F_NONBLOCK) {
  949. ret = -EAGAIN;
  950. break;
  951. }
  952. if (signal_pending(current)) {
  953. ret = -ERESTARTSYS;
  954. break;
  955. }
  956. if (do_wakeup) {
  957. wakeup_pipe_writers(pipe);
  958. do_wakeup = 0;
  959. }
  960. pipe_wait(pipe);
  961. }
  962. pipe_unlock(pipe);
  963. if (do_wakeup)
  964. wakeup_pipe_writers(pipe);
  965. return total_len ? total_len : ret;
  966. }
  967. /**
  968. * generic_splice_sendpage - splice data from a pipe to a socket
  969. * @pipe: pipe to splice from
  970. * @out: socket to write to
  971. * @ppos: position in @out
  972. * @len: number of bytes to splice
  973. * @flags: splice modifier flags
  974. *
  975. * Description:
  976. * Will send @len bytes from the pipe to a network socket. No data copying
  977. * is involved.
  978. *
  979. */
  980. ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
  981. loff_t *ppos, size_t len, unsigned int flags)
  982. {
  983. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
  984. }
  985. EXPORT_SYMBOL(generic_splice_sendpage);
  986. /*
  987. * Attempt to initiate a splice from pipe to file.
  988. */
  989. static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  990. loff_t *ppos, size_t len, unsigned int flags)
  991. {
  992. ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
  993. loff_t *, size_t, unsigned int);
  994. int ret;
  995. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  996. return -EBADF;
  997. if (unlikely(out->f_flags & O_APPEND))
  998. return -EINVAL;
  999. ret = rw_verify_area(WRITE, out, ppos, len);
  1000. if (unlikely(ret < 0))
  1001. return ret;
  1002. splice_write = out->f_op->splice_write;
  1003. if (!splice_write)
  1004. splice_write = default_file_splice_write;
  1005. return splice_write(pipe, out, ppos, len, flags);
  1006. }
  1007. /*
  1008. * Attempt to initiate a splice from a file to a pipe.
  1009. */
  1010. static long do_splice_to(struct file *in, loff_t *ppos,
  1011. struct pipe_inode_info *pipe, size_t len,
  1012. unsigned int flags)
  1013. {
  1014. ssize_t (*splice_read)(struct file *, loff_t *,
  1015. struct pipe_inode_info *, size_t, unsigned int);
  1016. int ret;
  1017. if (unlikely(!(in->f_mode & FMODE_READ)))
  1018. return -EBADF;
  1019. ret = rw_verify_area(READ, in, ppos, len);
  1020. if (unlikely(ret < 0))
  1021. return ret;
  1022. splice_read = in->f_op->splice_read;
  1023. if (!splice_read)
  1024. splice_read = default_file_splice_read;
  1025. return splice_read(in, ppos, pipe, len, flags);
  1026. }
  1027. /**
  1028. * splice_direct_to_actor - splices data directly between two non-pipes
  1029. * @in: file to splice from
  1030. * @sd: actor information on where to splice to
  1031. * @actor: handles the data splicing
  1032. *
  1033. * Description:
  1034. * This is a special case helper to splice directly between two
  1035. * points, without requiring an explicit pipe. Internally an allocated
  1036. * pipe is cached in the process, and reused during the lifetime of
  1037. * that process.
  1038. *
  1039. */
  1040. ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
  1041. splice_direct_actor *actor)
  1042. {
  1043. struct pipe_inode_info *pipe;
  1044. long ret, bytes;
  1045. umode_t i_mode;
  1046. size_t len;
  1047. int i, flags;
  1048. /*
  1049. * We require the input being a regular file, as we don't want to
  1050. * randomly drop data for eg socket -> socket splicing. Use the
  1051. * piped splicing for that!
  1052. */
  1053. i_mode = in->f_path.dentry->d_inode->i_mode;
  1054. if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  1055. return -EINVAL;
  1056. /*
  1057. * neither in nor out is a pipe, setup an internal pipe attached to
  1058. * 'out' and transfer the wanted data from 'in' to 'out' through that
  1059. */
  1060. pipe = current->splice_pipe;
  1061. if (unlikely(!pipe)) {
  1062. pipe = alloc_pipe_info(NULL);
  1063. if (!pipe)
  1064. return -ENOMEM;
  1065. /*
  1066. * We don't have an immediate reader, but we'll read the stuff
  1067. * out of the pipe right after the splice_to_pipe(). So set
  1068. * PIPE_READERS appropriately.
  1069. */
  1070. pipe->readers = 1;
  1071. current->splice_pipe = pipe;
  1072. }
  1073. /*
  1074. * Do the splice.
  1075. */
  1076. ret = 0;
  1077. bytes = 0;
  1078. len = sd->total_len;
  1079. flags = sd->flags;
  1080. /*
  1081. * Don't block on output, we have to drain the direct pipe.
  1082. */
  1083. sd->flags &= ~SPLICE_F_NONBLOCK;
  1084. while (len) {
  1085. size_t read_len;
  1086. loff_t pos = sd->pos, prev_pos = pos;
  1087. ret = do_splice_to(in, &pos, pipe, len, flags);
  1088. if (unlikely(ret <= 0))
  1089. goto out_release;
  1090. read_len = ret;
  1091. sd->total_len = read_len;
  1092. /*
  1093. * NOTE: nonblocking mode only applies to the input. We
  1094. * must not do the output in nonblocking mode as then we
  1095. * could get stuck data in the internal pipe:
  1096. */
  1097. ret = actor(pipe, sd);
  1098. if (unlikely(ret <= 0)) {
  1099. sd->pos = prev_pos;
  1100. goto out_release;
  1101. }
  1102. bytes += ret;
  1103. len -= ret;
  1104. sd->pos = pos;
  1105. if (ret < read_len) {
  1106. sd->pos = prev_pos + ret;
  1107. goto out_release;
  1108. }
  1109. }
  1110. done:
  1111. pipe->nrbufs = pipe->curbuf = 0;
  1112. file_accessed(in);
  1113. return bytes;
  1114. out_release:
  1115. /*
  1116. * If we did an incomplete transfer we must release
  1117. * the pipe buffers in question:
  1118. */
  1119. for (i = 0; i < PIPE_BUFFERS; i++) {
  1120. struct pipe_buffer *buf = pipe->bufs + i;
  1121. if (buf->ops) {
  1122. buf->ops->release(pipe, buf);
  1123. buf->ops = NULL;
  1124. }
  1125. }
  1126. if (!bytes)
  1127. bytes = ret;
  1128. goto done;
  1129. }
  1130. EXPORT_SYMBOL(splice_direct_to_actor);
  1131. static int direct_splice_actor(struct pipe_inode_info *pipe,
  1132. struct splice_desc *sd)
  1133. {
  1134. struct file *file = sd->u.file;
  1135. return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
  1136. }
  1137. /**
  1138. * do_splice_direct - splices data directly between two files
  1139. * @in: file to splice from
  1140. * @ppos: input file offset
  1141. * @out: file to splice to
  1142. * @len: number of bytes to splice
  1143. * @flags: splice modifier flags
  1144. *
  1145. * Description:
  1146. * For use by do_sendfile(). splice can easily emulate sendfile, but
  1147. * doing it in the application would incur an extra system call
  1148. * (splice in + splice out, as compared to just sendfile()). So this helper
  1149. * can splice directly through a process-private pipe.
  1150. *
  1151. */
  1152. long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
  1153. size_t len, unsigned int flags)
  1154. {
  1155. struct splice_desc sd = {
  1156. .len = len,
  1157. .total_len = len,
  1158. .flags = flags,
  1159. .pos = *ppos,
  1160. .u.file = out,
  1161. };
  1162. long ret;
  1163. ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
  1164. if (ret > 0)
  1165. *ppos = sd.pos;
  1166. return ret;
  1167. }
  1168. static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  1169. struct pipe_inode_info *opipe,
  1170. size_t len, unsigned int flags);
  1171. /*
  1172. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  1173. * location, so checking ->i_pipe is not enough to verify that this is a
  1174. * pipe.
  1175. */
  1176. static inline struct pipe_inode_info *pipe_info(struct inode *inode)
  1177. {
  1178. if (S_ISFIFO(inode->i_mode))
  1179. return inode->i_pipe;
  1180. return NULL;
  1181. }
  1182. /*
  1183. * Determine where to splice to/from.
  1184. */
  1185. static long do_splice(struct file *in, loff_t __user *off_in,
  1186. struct file *out, loff_t __user *off_out,
  1187. size_t len, unsigned int flags)
  1188. {
  1189. struct pipe_inode_info *ipipe;
  1190. struct pipe_inode_info *opipe;
  1191. loff_t offset, *off;
  1192. long ret;
  1193. ipipe = pipe_info(in->f_path.dentry->d_inode);
  1194. opipe = pipe_info(out->f_path.dentry->d_inode);
  1195. if (ipipe && opipe) {
  1196. if (off_in || off_out)
  1197. return -ESPIPE;
  1198. if (!(in->f_mode & FMODE_READ))
  1199. return -EBADF;
  1200. if (!(out->f_mode & FMODE_WRITE))
  1201. return -EBADF;
  1202. /* Splicing to self would be fun, but... */
  1203. if (ipipe == opipe)
  1204. return -EINVAL;
  1205. return splice_pipe_to_pipe(ipipe, opipe, len, flags);
  1206. }
  1207. if (ipipe) {
  1208. if (off_in)
  1209. return -ESPIPE;
  1210. if (off_out) {
  1211. if (out->f_op->llseek == no_llseek)
  1212. return -EINVAL;
  1213. if (copy_from_user(&offset, off_out, sizeof(loff_t)))
  1214. return -EFAULT;
  1215. off = &offset;
  1216. } else
  1217. off = &out->f_pos;
  1218. ret = do_splice_from(ipipe, out, off, len, flags);
  1219. if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
  1220. ret = -EFAULT;
  1221. return ret;
  1222. }
  1223. if (opipe) {
  1224. if (off_out)
  1225. return -ESPIPE;
  1226. if (off_in) {
  1227. if (in->f_op->llseek == no_llseek)
  1228. return -EINVAL;
  1229. if (copy_from_user(&offset, off_in, sizeof(loff_t)))
  1230. return -EFAULT;
  1231. off = &offset;
  1232. } else
  1233. off = &in->f_pos;
  1234. ret = do_splice_to(in, off, opipe, len, flags);
  1235. if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
  1236. ret = -EFAULT;
  1237. return ret;
  1238. }
  1239. return -EINVAL;
  1240. }
  1241. /*
  1242. * Map an iov into an array of pages and offset/length tupples. With the
  1243. * partial_page structure, we can map several non-contiguous ranges into
  1244. * our ones pages[] map instead of splitting that operation into pieces.
  1245. * Could easily be exported as a generic helper for other users, in which
  1246. * case one would probably want to add a 'max_nr_pages' parameter as well.
  1247. */
  1248. static int get_iovec_page_array(const struct iovec __user *iov,
  1249. unsigned int nr_vecs, struct page **pages,
  1250. struct partial_page *partial, int aligned)
  1251. {
  1252. int buffers = 0, error = 0;
  1253. while (nr_vecs) {
  1254. unsigned long off, npages;
  1255. struct iovec entry;
  1256. void __user *base;
  1257. size_t len;
  1258. int i;
  1259. error = -EFAULT;
  1260. if (copy_from_user(&entry, iov, sizeof(entry)))
  1261. break;
  1262. base = entry.iov_base;
  1263. len = entry.iov_len;
  1264. /*
  1265. * Sanity check this iovec. 0 read succeeds.
  1266. */
  1267. error = 0;
  1268. if (unlikely(!len))
  1269. break;
  1270. error = -EFAULT;
  1271. if (!access_ok(VERIFY_READ, base, len))
  1272. break;
  1273. /*
  1274. * Get this base offset and number of pages, then map
  1275. * in the user pages.
  1276. */
  1277. off = (unsigned long) base & ~PAGE_MASK;
  1278. /*
  1279. * If asked for alignment, the offset must be zero and the
  1280. * length a multiple of the PAGE_SIZE.
  1281. */
  1282. error = -EINVAL;
  1283. if (aligned && (off || len & ~PAGE_MASK))
  1284. break;
  1285. npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1286. if (npages > PIPE_BUFFERS - buffers)
  1287. npages = PIPE_BUFFERS - buffers;
  1288. error = get_user_pages_fast((unsigned long)base, npages,
  1289. 0, &pages[buffers]);
  1290. if (unlikely(error <= 0))
  1291. break;
  1292. /*
  1293. * Fill this contiguous range into the partial page map.
  1294. */
  1295. for (i = 0; i < error; i++) {
  1296. const int plen = min_t(size_t, len, PAGE_SIZE - off);
  1297. partial[buffers].offset = off;
  1298. partial[buffers].len = plen;
  1299. off = 0;
  1300. len -= plen;
  1301. buffers++;
  1302. }
  1303. /*
  1304. * We didn't complete this iov, stop here since it probably
  1305. * means we have to move some of this into a pipe to
  1306. * be able to continue.
  1307. */
  1308. if (len)
  1309. break;
  1310. /*
  1311. * Don't continue if we mapped fewer pages than we asked for,
  1312. * or if we mapped the max number of pages that we have
  1313. * room for.
  1314. */
  1315. if (error < npages || buffers == PIPE_BUFFERS)
  1316. break;
  1317. nr_vecs--;
  1318. iov++;
  1319. }
  1320. if (buffers)
  1321. return buffers;
  1322. return error;
  1323. }
  1324. static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  1325. struct splice_desc *sd)
  1326. {
  1327. char *src;
  1328. int ret;
  1329. ret = buf->ops->confirm(pipe, buf);
  1330. if (unlikely(ret))
  1331. return ret;
  1332. /*
  1333. * See if we can use the atomic maps, by prefaulting in the
  1334. * pages and doing an atomic copy
  1335. */
  1336. if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) {
  1337. src = buf->ops->map(pipe, buf, 1);
  1338. ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset,
  1339. sd->len);
  1340. buf->ops->unmap(pipe, buf, src);
  1341. if (!ret) {
  1342. ret = sd->len;
  1343. goto out;
  1344. }
  1345. }
  1346. /*
  1347. * No dice, use slow non-atomic map and copy
  1348. */
  1349. src = buf->ops->map(pipe, buf, 0);
  1350. ret = sd->len;
  1351. if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len))
  1352. ret = -EFAULT;
  1353. buf->ops->unmap(pipe, buf, src);
  1354. out:
  1355. if (ret > 0)
  1356. sd->u.userptr += ret;
  1357. return ret;
  1358. }
  1359. /*
  1360. * For lack of a better implementation, implement vmsplice() to userspace
  1361. * as a simple copy of the pipes pages to the user iov.
  1362. */
  1363. static long vmsplice_to_user(struct file *file, const struct iovec __user *iov,
  1364. unsigned long nr_segs, unsigned int flags)
  1365. {
  1366. struct pipe_inode_info *pipe;
  1367. struct splice_desc sd;
  1368. ssize_t size;
  1369. int error;
  1370. long ret;
  1371. pipe = pipe_info(file->f_path.dentry->d_inode);
  1372. if (!pipe)
  1373. return -EBADF;
  1374. pipe_lock(pipe);
  1375. error = ret = 0;
  1376. while (nr_segs) {
  1377. void __user *base;
  1378. size_t len;
  1379. /*
  1380. * Get user address base and length for this iovec.
  1381. */
  1382. error = get_user(base, &iov->iov_base);
  1383. if (unlikely(error))
  1384. break;
  1385. error = get_user(len, &iov->iov_len);
  1386. if (unlikely(error))
  1387. break;
  1388. /*
  1389. * Sanity check this iovec. 0 read succeeds.
  1390. */
  1391. if (unlikely(!len))
  1392. break;
  1393. if (unlikely(!base)) {
  1394. error = -EFAULT;
  1395. break;
  1396. }
  1397. if (unlikely(!access_ok(VERIFY_WRITE, base, len))) {
  1398. error = -EFAULT;
  1399. break;
  1400. }
  1401. sd.len = 0;
  1402. sd.total_len = len;
  1403. sd.flags = flags;
  1404. sd.u.userptr = base;
  1405. sd.pos = 0;
  1406. size = __splice_from_pipe(pipe, &sd, pipe_to_user);
  1407. if (size < 0) {
  1408. if (!ret)
  1409. ret = size;
  1410. break;
  1411. }
  1412. ret += size;
  1413. if (size < len)
  1414. break;
  1415. nr_segs--;
  1416. iov++;
  1417. }
  1418. pipe_unlock(pipe);
  1419. if (!ret)
  1420. ret = error;
  1421. return ret;
  1422. }
  1423. /*
  1424. * vmsplice splices a user address range into a pipe. It can be thought of
  1425. * as splice-from-memory, where the regular splice is splice-from-file (or
  1426. * to file). In both cases the output is a pipe, naturally.
  1427. */
  1428. static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
  1429. unsigned long nr_segs, unsigned int flags)
  1430. {
  1431. struct pipe_inode_info *pipe;
  1432. struct page *pages[PIPE_BUFFERS];
  1433. struct partial_page partial[PIPE_BUFFERS];
  1434. struct splice_pipe_desc spd = {
  1435. .pages = pages,
  1436. .partial = partial,
  1437. .flags = flags,
  1438. .ops = &user_page_pipe_buf_ops,
  1439. .spd_release = spd_release_page,
  1440. };
  1441. pipe = pipe_info(file->f_path.dentry->d_inode);
  1442. if (!pipe)
  1443. return -EBADF;
  1444. spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
  1445. flags & SPLICE_F_GIFT);
  1446. if (spd.nr_pages <= 0)
  1447. return spd.nr_pages;
  1448. return splice_to_pipe(pipe, &spd);
  1449. }
  1450. /*
  1451. * Note that vmsplice only really supports true splicing _from_ user memory
  1452. * to a pipe, not the other way around. Splicing from user memory is a simple
  1453. * operation that can be supported without any funky alignment restrictions
  1454. * or nasty vm tricks. We simply map in the user memory and fill them into
  1455. * a pipe. The reverse isn't quite as easy, though. There are two possible
  1456. * solutions for that:
  1457. *
  1458. * - memcpy() the data internally, at which point we might as well just
  1459. * do a regular read() on the buffer anyway.
  1460. * - Lots of nasty vm tricks, that are neither fast nor flexible (it
  1461. * has restriction limitations on both ends of the pipe).
  1462. *
  1463. * Currently we punt and implement it as a normal copy, see pipe_to_user().
  1464. *
  1465. */
  1466. SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
  1467. unsigned long, nr_segs, unsigned int, flags)
  1468. {
  1469. struct file *file;
  1470. long error;
  1471. int fput;
  1472. if (unlikely(nr_segs > UIO_MAXIOV))
  1473. return -EINVAL;
  1474. else if (unlikely(!nr_segs))
  1475. return 0;
  1476. error = -EBADF;
  1477. file = fget_light(fd, &fput);
  1478. if (file) {
  1479. if (file->f_mode & FMODE_WRITE)
  1480. error = vmsplice_to_pipe(file, iov, nr_segs, flags);
  1481. else if (file->f_mode & FMODE_READ)
  1482. error = vmsplice_to_user(file, iov, nr_segs, flags);
  1483. fput_light(file, fput);
  1484. }
  1485. return error;
  1486. }
  1487. SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
  1488. int, fd_out, loff_t __user *, off_out,
  1489. size_t, len, unsigned int, flags)
  1490. {
  1491. long error;
  1492. struct file *in, *out;
  1493. int fput_in, fput_out;
  1494. if (unlikely(!len))
  1495. return 0;
  1496. error = -EBADF;
  1497. in = fget_light(fd_in, &fput_in);
  1498. if (in) {
  1499. if (in->f_mode & FMODE_READ) {
  1500. out = fget_light(fd_out, &fput_out);
  1501. if (out) {
  1502. if (out->f_mode & FMODE_WRITE)
  1503. error = do_splice(in, off_in,
  1504. out, off_out,
  1505. len, flags);
  1506. fput_light(out, fput_out);
  1507. }
  1508. }
  1509. fput_light(in, fput_in);
  1510. }
  1511. return error;
  1512. }
  1513. /*
  1514. * Make sure there's data to read. Wait for input if we can, otherwise
  1515. * return an appropriate error.
  1516. */
  1517. static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1518. {
  1519. int ret;
  1520. /*
  1521. * Check ->nrbufs without the inode lock first. This function
  1522. * is speculative anyways, so missing one is ok.
  1523. */
  1524. if (pipe->nrbufs)
  1525. return 0;
  1526. ret = 0;
  1527. pipe_lock(pipe);
  1528. while (!pipe->nrbufs) {
  1529. if (signal_pending(current)) {
  1530. ret = -ERESTARTSYS;
  1531. break;
  1532. }
  1533. if (!pipe->writers)
  1534. break;
  1535. if (!pipe->waiting_writers) {
  1536. if (flags & SPLICE_F_NONBLOCK) {
  1537. ret = -EAGAIN;
  1538. break;
  1539. }
  1540. }
  1541. pipe_wait(pipe);
  1542. }
  1543. pipe_unlock(pipe);
  1544. return ret;
  1545. }
  1546. /*
  1547. * Make sure there's writeable room. Wait for room if we can, otherwise
  1548. * return an appropriate error.
  1549. */
  1550. static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1551. {
  1552. int ret;
  1553. /*
  1554. * Check ->nrbufs without the inode lock first. This function
  1555. * is speculative anyways, so missing one is ok.
  1556. */
  1557. if (pipe->nrbufs < PIPE_BUFFERS)
  1558. return 0;
  1559. ret = 0;
  1560. pipe_lock(pipe);
  1561. while (pipe->nrbufs >= PIPE_BUFFERS) {
  1562. if (!pipe->readers) {
  1563. send_sig(SIGPIPE, current, 0);
  1564. ret = -EPIPE;
  1565. break;
  1566. }
  1567. if (flags & SPLICE_F_NONBLOCK) {
  1568. ret = -EAGAIN;
  1569. break;
  1570. }
  1571. if (signal_pending(current)) {
  1572. ret = -ERESTARTSYS;
  1573. break;
  1574. }
  1575. pipe->waiting_writers++;
  1576. pipe_wait(pipe);
  1577. pipe->waiting_writers--;
  1578. }
  1579. pipe_unlock(pipe);
  1580. return ret;
  1581. }
  1582. /*
  1583. * Splice contents of ipipe to opipe.
  1584. */
  1585. static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  1586. struct pipe_inode_info *opipe,
  1587. size_t len, unsigned int flags)
  1588. {
  1589. struct pipe_buffer *ibuf, *obuf;
  1590. int ret = 0, nbuf;
  1591. bool input_wakeup = false;
  1592. retry:
  1593. ret = ipipe_prep(ipipe, flags);
  1594. if (ret)
  1595. return ret;
  1596. ret = opipe_prep(opipe, flags);
  1597. if (ret)
  1598. return ret;
  1599. /*
  1600. * Potential ABBA deadlock, work around it by ordering lock
  1601. * grabbing by pipe info address. Otherwise two different processes
  1602. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1603. */
  1604. pipe_double_lock(ipipe, opipe);
  1605. do {
  1606. if (!opipe->readers) {
  1607. send_sig(SIGPIPE, current, 0);
  1608. if (!ret)
  1609. ret = -EPIPE;
  1610. break;
  1611. }
  1612. if (!ipipe->nrbufs && !ipipe->writers)
  1613. break;
  1614. /*
  1615. * Cannot make any progress, because either the input
  1616. * pipe is empty or the output pipe is full.
  1617. */
  1618. if (!ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS) {
  1619. /* Already processed some buffers, break */
  1620. if (ret)
  1621. break;
  1622. if (flags & SPLICE_F_NONBLOCK) {
  1623. ret = -EAGAIN;
  1624. break;
  1625. }
  1626. /*
  1627. * We raced with another reader/writer and haven't
  1628. * managed to process any buffers. A zero return
  1629. * value means EOF, so retry instead.
  1630. */
  1631. pipe_unlock(ipipe);
  1632. pipe_unlock(opipe);
  1633. goto retry;
  1634. }
  1635. ibuf = ipipe->bufs + ipipe->curbuf;
  1636. nbuf = (opipe->curbuf + opipe->nrbufs) % PIPE_BUFFERS;
  1637. obuf = opipe->bufs + nbuf;
  1638. if (len >= ibuf->len) {
  1639. /*
  1640. * Simply move the whole buffer from ipipe to opipe
  1641. */
  1642. *obuf = *ibuf;
  1643. ibuf->ops = NULL;
  1644. opipe->nrbufs++;
  1645. ipipe->curbuf = (ipipe->curbuf + 1) % PIPE_BUFFERS;
  1646. ipipe->nrbufs--;
  1647. input_wakeup = true;
  1648. } else {
  1649. /*
  1650. * Get a reference to this pipe buffer,
  1651. * so we can copy the contents over.
  1652. */
  1653. ibuf->ops->get(ipipe, ibuf);
  1654. *obuf = *ibuf;
  1655. /*
  1656. * Don't inherit the gift flag, we need to
  1657. * prevent multiple steals of this page.
  1658. */
  1659. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1660. obuf->len = len;
  1661. opipe->nrbufs++;
  1662. ibuf->offset += obuf->len;
  1663. ibuf->len -= obuf->len;
  1664. }
  1665. ret += obuf->len;
  1666. len -= obuf->len;
  1667. } while (len);
  1668. pipe_unlock(ipipe);
  1669. pipe_unlock(opipe);
  1670. /*
  1671. * If we put data in the output pipe, wakeup any potential readers.
  1672. */
  1673. if (ret > 0) {
  1674. smp_mb();
  1675. if (waitqueue_active(&opipe->wait))
  1676. wake_up_interruptible(&opipe->wait);
  1677. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  1678. }
  1679. if (input_wakeup)
  1680. wakeup_pipe_writers(ipipe);
  1681. return ret;
  1682. }
  1683. /*
  1684. * Link contents of ipipe to opipe.
  1685. */
  1686. static int link_pipe(struct pipe_inode_info *ipipe,
  1687. struct pipe_inode_info *opipe,
  1688. size_t len, unsigned int flags)
  1689. {
  1690. struct pipe_buffer *ibuf, *obuf;
  1691. int ret = 0, i = 0, nbuf;
  1692. /*
  1693. * Potential ABBA deadlock, work around it by ordering lock
  1694. * grabbing by pipe info address. Otherwise two different processes
  1695. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1696. */
  1697. pipe_double_lock(ipipe, opipe);
  1698. do {
  1699. if (!opipe->readers) {
  1700. send_sig(SIGPIPE, current, 0);
  1701. if (!ret)
  1702. ret = -EPIPE;
  1703. break;
  1704. }
  1705. /*
  1706. * If we have iterated all input buffers or ran out of
  1707. * output room, break.
  1708. */
  1709. if (i >= ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS)
  1710. break;
  1711. ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
  1712. nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
  1713. /*
  1714. * Get a reference to this pipe buffer,
  1715. * so we can copy the contents over.
  1716. */
  1717. ibuf->ops->get(ipipe, ibuf);
  1718. obuf = opipe->bufs + nbuf;
  1719. *obuf = *ibuf;
  1720. /*
  1721. * Don't inherit the gift flag, we need to
  1722. * prevent multiple steals of this page.
  1723. */
  1724. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1725. if (obuf->len > len)
  1726. obuf->len = len;
  1727. opipe->nrbufs++;
  1728. ret += obuf->len;
  1729. len -= obuf->len;
  1730. i++;
  1731. } while (len);
  1732. /*
  1733. * return EAGAIN if we have the potential of some data in the
  1734. * future, otherwise just return 0
  1735. */
  1736. if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
  1737. ret = -EAGAIN;
  1738. pipe_unlock(ipipe);
  1739. pipe_unlock(opipe);
  1740. /*
  1741. * If we put data in the output pipe, wakeup any potential readers.
  1742. */
  1743. if (ret > 0) {
  1744. smp_mb();
  1745. if (waitqueue_active(&opipe->wait))
  1746. wake_up_interruptible(&opipe->wait);
  1747. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  1748. }
  1749. return ret;
  1750. }
  1751. /*
  1752. * This is a tee(1) implementation that works on pipes. It doesn't copy
  1753. * any data, it simply references the 'in' pages on the 'out' pipe.
  1754. * The 'flags' used are the SPLICE_F_* variants, currently the only
  1755. * applicable one is SPLICE_F_NONBLOCK.
  1756. */
  1757. static long do_tee(struct file *in, struct file *out, size_t len,
  1758. unsigned int flags)
  1759. {
  1760. struct pipe_inode_info *ipipe = pipe_info(in->f_path.dentry->d_inode);
  1761. struct pipe_inode_info *opipe = pipe_info(out->f_path.dentry->d_inode);
  1762. int ret = -EINVAL;
  1763. /*
  1764. * Duplicate the contents of ipipe to opipe without actually
  1765. * copying the data.
  1766. */
  1767. if (ipipe && opipe && ipipe != opipe) {
  1768. /*
  1769. * Keep going, unless we encounter an error. The ipipe/opipe
  1770. * ordering doesn't really matter.
  1771. */
  1772. ret = ipipe_prep(ipipe, flags);
  1773. if (!ret) {
  1774. ret = opipe_prep(opipe, flags);
  1775. if (!ret)
  1776. ret = link_pipe(ipipe, opipe, len, flags);
  1777. }
  1778. }
  1779. return ret;
  1780. }
  1781. SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
  1782. {
  1783. struct file *in;
  1784. int error, fput_in;
  1785. if (unlikely(!len))
  1786. return 0;
  1787. error = -EBADF;
  1788. in = fget_light(fdin, &fput_in);
  1789. if (in) {
  1790. if (in->f_mode & FMODE_READ) {
  1791. int fput_out;
  1792. struct file *out = fget_light(fdout, &fput_out);
  1793. if (out) {
  1794. if (out->f_mode & FMODE_WRITE)
  1795. error = do_tee(in, out, len, flags);
  1796. fput_light(out, fput_out);
  1797. }
  1798. }
  1799. fput_light(in, fput_in);
  1800. }
  1801. return error;
  1802. }