splice.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113
  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_HIGHUSER);
  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 *) kmap(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. kunmap(pages[i]);
  528. this_len = min_t(size_t, vec[i].iov_len, res);
  529. partial[i].offset = 0;
  530. partial[i].len = this_len;
  531. if (!this_len) {
  532. __free_page(pages[i]);
  533. pages[i] = NULL;
  534. nr_freed++;
  535. }
  536. res -= this_len;
  537. }
  538. spd.nr_pages -= nr_freed;
  539. res = splice_to_pipe(pipe, &spd);
  540. if (res > 0)
  541. *ppos += res;
  542. return res;
  543. err:
  544. for (i = 0; i < spd.nr_pages; i++) {
  545. kunmap(pages[i]);
  546. __free_page(pages[i]);
  547. }
  548. return error;
  549. }
  550. EXPORT_SYMBOL(default_file_splice_read);
  551. /*
  552. * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
  553. * using sendpage(). Return the number of bytes sent.
  554. */
  555. static int pipe_to_sendpage(struct pipe_inode_info *pipe,
  556. struct pipe_buffer *buf, struct splice_desc *sd)
  557. {
  558. struct file *file = sd->u.file;
  559. loff_t pos = sd->pos;
  560. int ret, more;
  561. ret = buf->ops->confirm(pipe, buf);
  562. if (!ret) {
  563. more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
  564. ret = file->f_op->sendpage(file, buf->page, buf->offset,
  565. sd->len, &pos, more);
  566. }
  567. return ret;
  568. }
  569. /*
  570. * This is a little more tricky than the file -> pipe splicing. There are
  571. * basically three cases:
  572. *
  573. * - Destination page already exists in the address space and there
  574. * are users of it. For that case we have no other option that
  575. * copying the data. Tough luck.
  576. * - Destination page already exists in the address space, but there
  577. * are no users of it. Make sure it's uptodate, then drop it. Fall
  578. * through to last case.
  579. * - Destination page does not exist, we can add the pipe page to
  580. * the page cache and avoid the copy.
  581. *
  582. * If asked to move pages to the output file (SPLICE_F_MOVE is set in
  583. * sd->flags), we attempt to migrate pages from the pipe to the output
  584. * file address space page cache. This is possible if no one else has
  585. * the pipe page referenced outside of the pipe and page cache. If
  586. * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
  587. * a new page in the output file page cache and fill/dirty that.
  588. */
  589. int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  590. struct splice_desc *sd)
  591. {
  592. struct file *file = sd->u.file;
  593. struct address_space *mapping = file->f_mapping;
  594. unsigned int offset, this_len;
  595. struct page *page;
  596. void *fsdata;
  597. int ret;
  598. /*
  599. * make sure the data in this buffer is uptodate
  600. */
  601. ret = buf->ops->confirm(pipe, buf);
  602. if (unlikely(ret))
  603. return ret;
  604. offset = sd->pos & ~PAGE_CACHE_MASK;
  605. this_len = sd->len;
  606. if (this_len + offset > PAGE_CACHE_SIZE)
  607. this_len = PAGE_CACHE_SIZE - offset;
  608. ret = pagecache_write_begin(file, mapping, sd->pos, this_len,
  609. AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
  610. if (unlikely(ret))
  611. goto out;
  612. if (buf->page != page) {
  613. /*
  614. * Careful, ->map() uses KM_USER0!
  615. */
  616. char *src = buf->ops->map(pipe, buf, 1);
  617. char *dst = kmap_atomic(page, KM_USER1);
  618. memcpy(dst + offset, src + buf->offset, this_len);
  619. flush_dcache_page(page);
  620. kunmap_atomic(dst, KM_USER1);
  621. buf->ops->unmap(pipe, buf, src);
  622. }
  623. ret = pagecache_write_end(file, mapping, sd->pos, this_len, this_len,
  624. page, fsdata);
  625. out:
  626. return ret;
  627. }
  628. EXPORT_SYMBOL(pipe_to_file);
  629. static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
  630. {
  631. smp_mb();
  632. if (waitqueue_active(&pipe->wait))
  633. wake_up_interruptible(&pipe->wait);
  634. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  635. }
  636. /**
  637. * splice_from_pipe_feed - feed available data from a pipe to a file
  638. * @pipe: pipe to splice from
  639. * @sd: information to @actor
  640. * @actor: handler that splices the data
  641. *
  642. * Description:
  643. * This function loops over the pipe and calls @actor to do the
  644. * actual moving of a single struct pipe_buffer to the desired
  645. * destination. It returns when there's no more buffers left in
  646. * the pipe or if the requested number of bytes (@sd->total_len)
  647. * have been copied. It returns a positive number (one) if the
  648. * pipe needs to be filled with more data, zero if the required
  649. * number of bytes have been copied and -errno on error.
  650. *
  651. * This, together with splice_from_pipe_{begin,end,next}, may be
  652. * used to implement the functionality of __splice_from_pipe() when
  653. * locking is required around copying the pipe buffers to the
  654. * destination.
  655. */
  656. int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
  657. splice_actor *actor)
  658. {
  659. int ret;
  660. while (pipe->nrbufs) {
  661. struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  662. const struct pipe_buf_operations *ops = buf->ops;
  663. sd->len = buf->len;
  664. if (sd->len > sd->total_len)
  665. sd->len = sd->total_len;
  666. ret = actor(pipe, buf, sd);
  667. if (ret <= 0) {
  668. if (ret == -ENODATA)
  669. ret = 0;
  670. return ret;
  671. }
  672. buf->offset += ret;
  673. buf->len -= ret;
  674. sd->num_spliced += ret;
  675. sd->len -= ret;
  676. sd->pos += ret;
  677. sd->total_len -= ret;
  678. if (!buf->len) {
  679. buf->ops = NULL;
  680. ops->release(pipe, buf);
  681. pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
  682. pipe->nrbufs--;
  683. if (pipe->inode)
  684. sd->need_wakeup = true;
  685. }
  686. if (!sd->total_len)
  687. return 0;
  688. }
  689. return 1;
  690. }
  691. EXPORT_SYMBOL(splice_from_pipe_feed);
  692. /**
  693. * splice_from_pipe_next - wait for some data to splice from
  694. * @pipe: pipe to splice from
  695. * @sd: information about the splice operation
  696. *
  697. * Description:
  698. * This function will wait for some data and return a positive
  699. * value (one) if pipe buffers are available. It will return zero
  700. * or -errno if no more data needs to be spliced.
  701. */
  702. int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
  703. {
  704. while (!pipe->nrbufs) {
  705. if (!pipe->writers)
  706. return 0;
  707. if (!pipe->waiting_writers && sd->num_spliced)
  708. return 0;
  709. if (sd->flags & SPLICE_F_NONBLOCK)
  710. return -EAGAIN;
  711. if (signal_pending(current))
  712. return -ERESTARTSYS;
  713. if (sd->need_wakeup) {
  714. wakeup_pipe_writers(pipe);
  715. sd->need_wakeup = false;
  716. }
  717. pipe_wait(pipe);
  718. }
  719. return 1;
  720. }
  721. EXPORT_SYMBOL(splice_from_pipe_next);
  722. /**
  723. * splice_from_pipe_begin - start splicing from pipe
  724. * @sd: information about the splice operation
  725. *
  726. * Description:
  727. * This function should be called before a loop containing
  728. * splice_from_pipe_next() and splice_from_pipe_feed() to
  729. * initialize the necessary fields of @sd.
  730. */
  731. void splice_from_pipe_begin(struct splice_desc *sd)
  732. {
  733. sd->num_spliced = 0;
  734. sd->need_wakeup = false;
  735. }
  736. EXPORT_SYMBOL(splice_from_pipe_begin);
  737. /**
  738. * splice_from_pipe_end - finish splicing from pipe
  739. * @pipe: pipe to splice from
  740. * @sd: information about the splice operation
  741. *
  742. * Description:
  743. * This function will wake up pipe writers if necessary. It should
  744. * be called after a loop containing splice_from_pipe_next() and
  745. * splice_from_pipe_feed().
  746. */
  747. void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
  748. {
  749. if (sd->need_wakeup)
  750. wakeup_pipe_writers(pipe);
  751. }
  752. EXPORT_SYMBOL(splice_from_pipe_end);
  753. /**
  754. * __splice_from_pipe - splice data from a pipe to given actor
  755. * @pipe: pipe to splice from
  756. * @sd: information to @actor
  757. * @actor: handler that splices the data
  758. *
  759. * Description:
  760. * This function does little more than loop over the pipe and call
  761. * @actor to do the actual moving of a single struct pipe_buffer to
  762. * the desired destination. See pipe_to_file, pipe_to_sendpage, or
  763. * pipe_to_user.
  764. *
  765. */
  766. ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
  767. splice_actor *actor)
  768. {
  769. int ret;
  770. splice_from_pipe_begin(sd);
  771. do {
  772. ret = splice_from_pipe_next(pipe, sd);
  773. if (ret > 0)
  774. ret = splice_from_pipe_feed(pipe, sd, actor);
  775. } while (ret > 0);
  776. splice_from_pipe_end(pipe, sd);
  777. return sd->num_spliced ? sd->num_spliced : ret;
  778. }
  779. EXPORT_SYMBOL(__splice_from_pipe);
  780. /**
  781. * splice_from_pipe - splice data from a pipe to a file
  782. * @pipe: pipe to splice from
  783. * @out: file to splice to
  784. * @ppos: position in @out
  785. * @len: how many bytes to splice
  786. * @flags: splice modifier flags
  787. * @actor: handler that splices the data
  788. *
  789. * Description:
  790. * See __splice_from_pipe. This function locks the pipe inode,
  791. * otherwise it's identical to __splice_from_pipe().
  792. *
  793. */
  794. ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
  795. loff_t *ppos, size_t len, unsigned int flags,
  796. splice_actor *actor)
  797. {
  798. ssize_t ret;
  799. struct splice_desc sd = {
  800. .total_len = len,
  801. .flags = flags,
  802. .pos = *ppos,
  803. .u.file = out,
  804. };
  805. pipe_lock(pipe);
  806. ret = __splice_from_pipe(pipe, &sd, actor);
  807. pipe_unlock(pipe);
  808. return ret;
  809. }
  810. /**
  811. * generic_file_splice_write - splice data from a pipe to a file
  812. * @pipe: pipe info
  813. * @out: file to write to
  814. * @ppos: position in @out
  815. * @len: number of bytes to splice
  816. * @flags: splice modifier flags
  817. *
  818. * Description:
  819. * Will either move or copy pages (determined by @flags options) from
  820. * the given pipe inode to the given file.
  821. *
  822. */
  823. ssize_t
  824. generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  825. loff_t *ppos, size_t len, unsigned int flags)
  826. {
  827. struct address_space *mapping = out->f_mapping;
  828. struct inode *inode = mapping->host;
  829. struct splice_desc sd = {
  830. .total_len = len,
  831. .flags = flags,
  832. .pos = *ppos,
  833. .u.file = out,
  834. };
  835. ssize_t ret;
  836. pipe_lock(pipe);
  837. splice_from_pipe_begin(&sd);
  838. do {
  839. ret = splice_from_pipe_next(pipe, &sd);
  840. if (ret <= 0)
  841. break;
  842. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  843. ret = file_remove_suid(out);
  844. if (!ret)
  845. ret = splice_from_pipe_feed(pipe, &sd, pipe_to_file);
  846. mutex_unlock(&inode->i_mutex);
  847. } while (ret > 0);
  848. splice_from_pipe_end(pipe, &sd);
  849. pipe_unlock(pipe);
  850. if (sd.num_spliced)
  851. ret = sd.num_spliced;
  852. if (ret > 0) {
  853. unsigned long nr_pages;
  854. *ppos += ret;
  855. nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  856. /*
  857. * If file or inode is SYNC and we actually wrote some data,
  858. * sync it.
  859. */
  860. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  861. int err;
  862. mutex_lock(&inode->i_mutex);
  863. err = generic_osync_inode(inode, mapping,
  864. OSYNC_METADATA|OSYNC_DATA);
  865. mutex_unlock(&inode->i_mutex);
  866. if (err)
  867. ret = err;
  868. }
  869. balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
  870. }
  871. return ret;
  872. }
  873. EXPORT_SYMBOL(generic_file_splice_write);
  874. static struct pipe_buffer *nth_pipe_buf(struct pipe_inode_info *pipe, int n)
  875. {
  876. return &pipe->bufs[(pipe->curbuf + n) % PIPE_BUFFERS];
  877. }
  878. static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
  879. struct file *out, loff_t *ppos,
  880. size_t len, unsigned int flags)
  881. {
  882. ssize_t ret = 0;
  883. ssize_t total_len = 0;
  884. int do_wakeup = 0;
  885. pipe_lock(pipe);
  886. while (len) {
  887. struct pipe_buffer *buf;
  888. void *data[PIPE_BUFFERS];
  889. struct iovec vec[PIPE_BUFFERS];
  890. unsigned int nr_pages = 0;
  891. unsigned int write_len = 0;
  892. unsigned int now_len = len;
  893. unsigned int this_len;
  894. int i;
  895. BUG_ON(pipe->nrbufs > PIPE_BUFFERS);
  896. for (i = 0; i < pipe->nrbufs && now_len; i++) {
  897. buf = nth_pipe_buf(pipe, i);
  898. ret = buf->ops->confirm(pipe, buf);
  899. if (ret)
  900. break;
  901. data[i] = buf->ops->map(pipe, buf, 0);
  902. this_len = min(buf->len, now_len);
  903. vec[i].iov_base = (void __user *) data[i] + buf->offset;
  904. vec[i].iov_len = this_len;
  905. now_len -= this_len;
  906. write_len += this_len;
  907. nr_pages++;
  908. }
  909. if (nr_pages) {
  910. ret = kernel_writev(out, vec, nr_pages, ppos);
  911. if (ret == 0)
  912. ret = -EIO;
  913. if (ret > 0) {
  914. len -= ret;
  915. total_len += ret;
  916. }
  917. }
  918. for (i = 0; i < nr_pages; i++) {
  919. buf = nth_pipe_buf(pipe, i);
  920. buf->ops->unmap(pipe, buf, data[i]);
  921. if (ret > 0) {
  922. this_len = min_t(unsigned, vec[i].iov_len, ret);
  923. buf->offset += this_len;
  924. buf->len -= this_len;
  925. ret -= this_len;
  926. }
  927. }
  928. if (ret < 0)
  929. break;
  930. while (pipe->nrbufs) {
  931. const struct pipe_buf_operations *ops;
  932. buf = nth_pipe_buf(pipe, 0);
  933. if (buf->len)
  934. break;
  935. ops = buf->ops;
  936. buf->ops = NULL;
  937. ops->release(pipe, buf);
  938. pipe->curbuf = (pipe->curbuf + 1) % PIPE_BUFFERS;
  939. pipe->nrbufs--;
  940. if (pipe->inode)
  941. do_wakeup = 1;
  942. }
  943. if (pipe->nrbufs)
  944. continue;
  945. if (!pipe->writers)
  946. break;
  947. if (!pipe->waiting_writers) {
  948. if (total_len)
  949. break;
  950. }
  951. if (flags & SPLICE_F_NONBLOCK) {
  952. ret = -EAGAIN;
  953. break;
  954. }
  955. if (signal_pending(current)) {
  956. ret = -ERESTARTSYS;
  957. break;
  958. }
  959. if (do_wakeup) {
  960. wakeup_pipe_writers(pipe);
  961. do_wakeup = 0;
  962. }
  963. pipe_wait(pipe);
  964. }
  965. pipe_unlock(pipe);
  966. if (do_wakeup)
  967. wakeup_pipe_writers(pipe);
  968. return total_len ? total_len : ret;
  969. }
  970. /**
  971. * generic_splice_sendpage - splice data from a pipe to a socket
  972. * @pipe: pipe to splice from
  973. * @out: socket to write to
  974. * @ppos: position in @out
  975. * @len: number of bytes to splice
  976. * @flags: splice modifier flags
  977. *
  978. * Description:
  979. * Will send @len bytes from the pipe to a network socket. No data copying
  980. * is involved.
  981. *
  982. */
  983. ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
  984. loff_t *ppos, size_t len, unsigned int flags)
  985. {
  986. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
  987. }
  988. EXPORT_SYMBOL(generic_splice_sendpage);
  989. /*
  990. * Attempt to initiate a splice from pipe to file.
  991. */
  992. static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  993. loff_t *ppos, size_t len, unsigned int flags)
  994. {
  995. ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
  996. loff_t *, size_t, unsigned int);
  997. int ret;
  998. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  999. return -EBADF;
  1000. if (unlikely(out->f_flags & O_APPEND))
  1001. return -EINVAL;
  1002. ret = rw_verify_area(WRITE, out, ppos, len);
  1003. if (unlikely(ret < 0))
  1004. return ret;
  1005. splice_write = out->f_op->splice_write;
  1006. if (!splice_write)
  1007. splice_write = default_file_splice_write;
  1008. return splice_write(pipe, out, ppos, len, flags);
  1009. }
  1010. /*
  1011. * Attempt to initiate a splice from a file to a pipe.
  1012. */
  1013. static long do_splice_to(struct file *in, loff_t *ppos,
  1014. struct pipe_inode_info *pipe, size_t len,
  1015. unsigned int flags)
  1016. {
  1017. ssize_t (*splice_read)(struct file *, loff_t *,
  1018. struct pipe_inode_info *, size_t, unsigned int);
  1019. int ret;
  1020. if (unlikely(!(in->f_mode & FMODE_READ)))
  1021. return -EBADF;
  1022. ret = rw_verify_area(READ, in, ppos, len);
  1023. if (unlikely(ret < 0))
  1024. return ret;
  1025. splice_read = in->f_op->splice_read;
  1026. if (!splice_read)
  1027. splice_read = default_file_splice_read;
  1028. return splice_read(in, ppos, pipe, len, flags);
  1029. }
  1030. /**
  1031. * splice_direct_to_actor - splices data directly between two non-pipes
  1032. * @in: file to splice from
  1033. * @sd: actor information on where to splice to
  1034. * @actor: handles the data splicing
  1035. *
  1036. * Description:
  1037. * This is a special case helper to splice directly between two
  1038. * points, without requiring an explicit pipe. Internally an allocated
  1039. * pipe is cached in the process, and reused during the lifetime of
  1040. * that process.
  1041. *
  1042. */
  1043. ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
  1044. splice_direct_actor *actor)
  1045. {
  1046. struct pipe_inode_info *pipe;
  1047. long ret, bytes;
  1048. umode_t i_mode;
  1049. size_t len;
  1050. int i, flags;
  1051. /*
  1052. * We require the input being a regular file, as we don't want to
  1053. * randomly drop data for eg socket -> socket splicing. Use the
  1054. * piped splicing for that!
  1055. */
  1056. i_mode = in->f_path.dentry->d_inode->i_mode;
  1057. if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  1058. return -EINVAL;
  1059. /*
  1060. * neither in nor out is a pipe, setup an internal pipe attached to
  1061. * 'out' and transfer the wanted data from 'in' to 'out' through that
  1062. */
  1063. pipe = current->splice_pipe;
  1064. if (unlikely(!pipe)) {
  1065. pipe = alloc_pipe_info(NULL);
  1066. if (!pipe)
  1067. return -ENOMEM;
  1068. /*
  1069. * We don't have an immediate reader, but we'll read the stuff
  1070. * out of the pipe right after the splice_to_pipe(). So set
  1071. * PIPE_READERS appropriately.
  1072. */
  1073. pipe->readers = 1;
  1074. current->splice_pipe = pipe;
  1075. }
  1076. /*
  1077. * Do the splice.
  1078. */
  1079. ret = 0;
  1080. bytes = 0;
  1081. len = sd->total_len;
  1082. flags = sd->flags;
  1083. /*
  1084. * Don't block on output, we have to drain the direct pipe.
  1085. */
  1086. sd->flags &= ~SPLICE_F_NONBLOCK;
  1087. while (len) {
  1088. size_t read_len;
  1089. loff_t pos = sd->pos, prev_pos = pos;
  1090. ret = do_splice_to(in, &pos, pipe, len, flags);
  1091. if (unlikely(ret <= 0))
  1092. goto out_release;
  1093. read_len = ret;
  1094. sd->total_len = read_len;
  1095. /*
  1096. * NOTE: nonblocking mode only applies to the input. We
  1097. * must not do the output in nonblocking mode as then we
  1098. * could get stuck data in the internal pipe:
  1099. */
  1100. ret = actor(pipe, sd);
  1101. if (unlikely(ret <= 0)) {
  1102. sd->pos = prev_pos;
  1103. goto out_release;
  1104. }
  1105. bytes += ret;
  1106. len -= ret;
  1107. sd->pos = pos;
  1108. if (ret < read_len) {
  1109. sd->pos = prev_pos + ret;
  1110. goto out_release;
  1111. }
  1112. }
  1113. done:
  1114. pipe->nrbufs = pipe->curbuf = 0;
  1115. file_accessed(in);
  1116. return bytes;
  1117. out_release:
  1118. /*
  1119. * If we did an incomplete transfer we must release
  1120. * the pipe buffers in question:
  1121. */
  1122. for (i = 0; i < PIPE_BUFFERS; i++) {
  1123. struct pipe_buffer *buf = pipe->bufs + i;
  1124. if (buf->ops) {
  1125. buf->ops->release(pipe, buf);
  1126. buf->ops = NULL;
  1127. }
  1128. }
  1129. if (!bytes)
  1130. bytes = ret;
  1131. goto done;
  1132. }
  1133. EXPORT_SYMBOL(splice_direct_to_actor);
  1134. static int direct_splice_actor(struct pipe_inode_info *pipe,
  1135. struct splice_desc *sd)
  1136. {
  1137. struct file *file = sd->u.file;
  1138. return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
  1139. }
  1140. /**
  1141. * do_splice_direct - splices data directly between two files
  1142. * @in: file to splice from
  1143. * @ppos: input file offset
  1144. * @out: file to splice to
  1145. * @len: number of bytes to splice
  1146. * @flags: splice modifier flags
  1147. *
  1148. * Description:
  1149. * For use by do_sendfile(). splice can easily emulate sendfile, but
  1150. * doing it in the application would incur an extra system call
  1151. * (splice in + splice out, as compared to just sendfile()). So this helper
  1152. * can splice directly through a process-private pipe.
  1153. *
  1154. */
  1155. long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
  1156. size_t len, unsigned int flags)
  1157. {
  1158. struct splice_desc sd = {
  1159. .len = len,
  1160. .total_len = len,
  1161. .flags = flags,
  1162. .pos = *ppos,
  1163. .u.file = out,
  1164. };
  1165. long ret;
  1166. ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
  1167. if (ret > 0)
  1168. *ppos = sd.pos;
  1169. return ret;
  1170. }
  1171. static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  1172. struct pipe_inode_info *opipe,
  1173. size_t len, unsigned int flags);
  1174. /*
  1175. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  1176. * location, so checking ->i_pipe is not enough to verify that this is a
  1177. * pipe.
  1178. */
  1179. static inline struct pipe_inode_info *pipe_info(struct inode *inode)
  1180. {
  1181. if (S_ISFIFO(inode->i_mode))
  1182. return inode->i_pipe;
  1183. return NULL;
  1184. }
  1185. /*
  1186. * Determine where to splice to/from.
  1187. */
  1188. static long do_splice(struct file *in, loff_t __user *off_in,
  1189. struct file *out, loff_t __user *off_out,
  1190. size_t len, unsigned int flags)
  1191. {
  1192. struct pipe_inode_info *ipipe;
  1193. struct pipe_inode_info *opipe;
  1194. loff_t offset, *off;
  1195. long ret;
  1196. ipipe = pipe_info(in->f_path.dentry->d_inode);
  1197. opipe = pipe_info(out->f_path.dentry->d_inode);
  1198. if (ipipe && opipe) {
  1199. if (off_in || off_out)
  1200. return -ESPIPE;
  1201. if (!(in->f_mode & FMODE_READ))
  1202. return -EBADF;
  1203. if (!(out->f_mode & FMODE_WRITE))
  1204. return -EBADF;
  1205. /* Splicing to self would be fun, but... */
  1206. if (ipipe == opipe)
  1207. return -EINVAL;
  1208. return splice_pipe_to_pipe(ipipe, opipe, len, flags);
  1209. }
  1210. if (ipipe) {
  1211. if (off_in)
  1212. return -ESPIPE;
  1213. if (off_out) {
  1214. if (out->f_op->llseek == no_llseek)
  1215. return -EINVAL;
  1216. if (copy_from_user(&offset, off_out, sizeof(loff_t)))
  1217. return -EFAULT;
  1218. off = &offset;
  1219. } else
  1220. off = &out->f_pos;
  1221. ret = do_splice_from(ipipe, out, off, len, flags);
  1222. if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
  1223. ret = -EFAULT;
  1224. return ret;
  1225. }
  1226. if (opipe) {
  1227. if (off_out)
  1228. return -ESPIPE;
  1229. if (off_in) {
  1230. if (in->f_op->llseek == no_llseek)
  1231. return -EINVAL;
  1232. if (copy_from_user(&offset, off_in, sizeof(loff_t)))
  1233. return -EFAULT;
  1234. off = &offset;
  1235. } else
  1236. off = &in->f_pos;
  1237. ret = do_splice_to(in, off, opipe, len, flags);
  1238. if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
  1239. ret = -EFAULT;
  1240. return ret;
  1241. }
  1242. return -EINVAL;
  1243. }
  1244. /*
  1245. * Map an iov into an array of pages and offset/length tupples. With the
  1246. * partial_page structure, we can map several non-contiguous ranges into
  1247. * our ones pages[] map instead of splitting that operation into pieces.
  1248. * Could easily be exported as a generic helper for other users, in which
  1249. * case one would probably want to add a 'max_nr_pages' parameter as well.
  1250. */
  1251. static int get_iovec_page_array(const struct iovec __user *iov,
  1252. unsigned int nr_vecs, struct page **pages,
  1253. struct partial_page *partial, int aligned)
  1254. {
  1255. int buffers = 0, error = 0;
  1256. while (nr_vecs) {
  1257. unsigned long off, npages;
  1258. struct iovec entry;
  1259. void __user *base;
  1260. size_t len;
  1261. int i;
  1262. error = -EFAULT;
  1263. if (copy_from_user(&entry, iov, sizeof(entry)))
  1264. break;
  1265. base = entry.iov_base;
  1266. len = entry.iov_len;
  1267. /*
  1268. * Sanity check this iovec. 0 read succeeds.
  1269. */
  1270. error = 0;
  1271. if (unlikely(!len))
  1272. break;
  1273. error = -EFAULT;
  1274. if (!access_ok(VERIFY_READ, base, len))
  1275. break;
  1276. /*
  1277. * Get this base offset and number of pages, then map
  1278. * in the user pages.
  1279. */
  1280. off = (unsigned long) base & ~PAGE_MASK;
  1281. /*
  1282. * If asked for alignment, the offset must be zero and the
  1283. * length a multiple of the PAGE_SIZE.
  1284. */
  1285. error = -EINVAL;
  1286. if (aligned && (off || len & ~PAGE_MASK))
  1287. break;
  1288. npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1289. if (npages > PIPE_BUFFERS - buffers)
  1290. npages = PIPE_BUFFERS - buffers;
  1291. error = get_user_pages_fast((unsigned long)base, npages,
  1292. 0, &pages[buffers]);
  1293. if (unlikely(error <= 0))
  1294. break;
  1295. /*
  1296. * Fill this contiguous range into the partial page map.
  1297. */
  1298. for (i = 0; i < error; i++) {
  1299. const int plen = min_t(size_t, len, PAGE_SIZE - off);
  1300. partial[buffers].offset = off;
  1301. partial[buffers].len = plen;
  1302. off = 0;
  1303. len -= plen;
  1304. buffers++;
  1305. }
  1306. /*
  1307. * We didn't complete this iov, stop here since it probably
  1308. * means we have to move some of this into a pipe to
  1309. * be able to continue.
  1310. */
  1311. if (len)
  1312. break;
  1313. /*
  1314. * Don't continue if we mapped fewer pages than we asked for,
  1315. * or if we mapped the max number of pages that we have
  1316. * room for.
  1317. */
  1318. if (error < npages || buffers == PIPE_BUFFERS)
  1319. break;
  1320. nr_vecs--;
  1321. iov++;
  1322. }
  1323. if (buffers)
  1324. return buffers;
  1325. return error;
  1326. }
  1327. static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  1328. struct splice_desc *sd)
  1329. {
  1330. char *src;
  1331. int ret;
  1332. ret = buf->ops->confirm(pipe, buf);
  1333. if (unlikely(ret))
  1334. return ret;
  1335. /*
  1336. * See if we can use the atomic maps, by prefaulting in the
  1337. * pages and doing an atomic copy
  1338. */
  1339. if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) {
  1340. src = buf->ops->map(pipe, buf, 1);
  1341. ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset,
  1342. sd->len);
  1343. buf->ops->unmap(pipe, buf, src);
  1344. if (!ret) {
  1345. ret = sd->len;
  1346. goto out;
  1347. }
  1348. }
  1349. /*
  1350. * No dice, use slow non-atomic map and copy
  1351. */
  1352. src = buf->ops->map(pipe, buf, 0);
  1353. ret = sd->len;
  1354. if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len))
  1355. ret = -EFAULT;
  1356. buf->ops->unmap(pipe, buf, src);
  1357. out:
  1358. if (ret > 0)
  1359. sd->u.userptr += ret;
  1360. return ret;
  1361. }
  1362. /*
  1363. * For lack of a better implementation, implement vmsplice() to userspace
  1364. * as a simple copy of the pipes pages to the user iov.
  1365. */
  1366. static long vmsplice_to_user(struct file *file, const struct iovec __user *iov,
  1367. unsigned long nr_segs, unsigned int flags)
  1368. {
  1369. struct pipe_inode_info *pipe;
  1370. struct splice_desc sd;
  1371. ssize_t size;
  1372. int error;
  1373. long ret;
  1374. pipe = pipe_info(file->f_path.dentry->d_inode);
  1375. if (!pipe)
  1376. return -EBADF;
  1377. pipe_lock(pipe);
  1378. error = ret = 0;
  1379. while (nr_segs) {
  1380. void __user *base;
  1381. size_t len;
  1382. /*
  1383. * Get user address base and length for this iovec.
  1384. */
  1385. error = get_user(base, &iov->iov_base);
  1386. if (unlikely(error))
  1387. break;
  1388. error = get_user(len, &iov->iov_len);
  1389. if (unlikely(error))
  1390. break;
  1391. /*
  1392. * Sanity check this iovec. 0 read succeeds.
  1393. */
  1394. if (unlikely(!len))
  1395. break;
  1396. if (unlikely(!base)) {
  1397. error = -EFAULT;
  1398. break;
  1399. }
  1400. if (unlikely(!access_ok(VERIFY_WRITE, base, len))) {
  1401. error = -EFAULT;
  1402. break;
  1403. }
  1404. sd.len = 0;
  1405. sd.total_len = len;
  1406. sd.flags = flags;
  1407. sd.u.userptr = base;
  1408. sd.pos = 0;
  1409. size = __splice_from_pipe(pipe, &sd, pipe_to_user);
  1410. if (size < 0) {
  1411. if (!ret)
  1412. ret = size;
  1413. break;
  1414. }
  1415. ret += size;
  1416. if (size < len)
  1417. break;
  1418. nr_segs--;
  1419. iov++;
  1420. }
  1421. pipe_unlock(pipe);
  1422. if (!ret)
  1423. ret = error;
  1424. return ret;
  1425. }
  1426. /*
  1427. * vmsplice splices a user address range into a pipe. It can be thought of
  1428. * as splice-from-memory, where the regular splice is splice-from-file (or
  1429. * to file). In both cases the output is a pipe, naturally.
  1430. */
  1431. static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
  1432. unsigned long nr_segs, unsigned int flags)
  1433. {
  1434. struct pipe_inode_info *pipe;
  1435. struct page *pages[PIPE_BUFFERS];
  1436. struct partial_page partial[PIPE_BUFFERS];
  1437. struct splice_pipe_desc spd = {
  1438. .pages = pages,
  1439. .partial = partial,
  1440. .flags = flags,
  1441. .ops = &user_page_pipe_buf_ops,
  1442. .spd_release = spd_release_page,
  1443. };
  1444. pipe = pipe_info(file->f_path.dentry->d_inode);
  1445. if (!pipe)
  1446. return -EBADF;
  1447. spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
  1448. flags & SPLICE_F_GIFT);
  1449. if (spd.nr_pages <= 0)
  1450. return spd.nr_pages;
  1451. return splice_to_pipe(pipe, &spd);
  1452. }
  1453. /*
  1454. * Note that vmsplice only really supports true splicing _from_ user memory
  1455. * to a pipe, not the other way around. Splicing from user memory is a simple
  1456. * operation that can be supported without any funky alignment restrictions
  1457. * or nasty vm tricks. We simply map in the user memory and fill them into
  1458. * a pipe. The reverse isn't quite as easy, though. There are two possible
  1459. * solutions for that:
  1460. *
  1461. * - memcpy() the data internally, at which point we might as well just
  1462. * do a regular read() on the buffer anyway.
  1463. * - Lots of nasty vm tricks, that are neither fast nor flexible (it
  1464. * has restriction limitations on both ends of the pipe).
  1465. *
  1466. * Currently we punt and implement it as a normal copy, see pipe_to_user().
  1467. *
  1468. */
  1469. SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
  1470. unsigned long, nr_segs, unsigned int, flags)
  1471. {
  1472. struct file *file;
  1473. long error;
  1474. int fput;
  1475. if (unlikely(nr_segs > UIO_MAXIOV))
  1476. return -EINVAL;
  1477. else if (unlikely(!nr_segs))
  1478. return 0;
  1479. error = -EBADF;
  1480. file = fget_light(fd, &fput);
  1481. if (file) {
  1482. if (file->f_mode & FMODE_WRITE)
  1483. error = vmsplice_to_pipe(file, iov, nr_segs, flags);
  1484. else if (file->f_mode & FMODE_READ)
  1485. error = vmsplice_to_user(file, iov, nr_segs, flags);
  1486. fput_light(file, fput);
  1487. }
  1488. return error;
  1489. }
  1490. SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
  1491. int, fd_out, loff_t __user *, off_out,
  1492. size_t, len, unsigned int, flags)
  1493. {
  1494. long error;
  1495. struct file *in, *out;
  1496. int fput_in, fput_out;
  1497. if (unlikely(!len))
  1498. return 0;
  1499. error = -EBADF;
  1500. in = fget_light(fd_in, &fput_in);
  1501. if (in) {
  1502. if (in->f_mode & FMODE_READ) {
  1503. out = fget_light(fd_out, &fput_out);
  1504. if (out) {
  1505. if (out->f_mode & FMODE_WRITE)
  1506. error = do_splice(in, off_in,
  1507. out, off_out,
  1508. len, flags);
  1509. fput_light(out, fput_out);
  1510. }
  1511. }
  1512. fput_light(in, fput_in);
  1513. }
  1514. return error;
  1515. }
  1516. /*
  1517. * Make sure there's data to read. Wait for input if we can, otherwise
  1518. * return an appropriate error.
  1519. */
  1520. static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1521. {
  1522. int ret;
  1523. /*
  1524. * Check ->nrbufs without the inode lock first. This function
  1525. * is speculative anyways, so missing one is ok.
  1526. */
  1527. if (pipe->nrbufs)
  1528. return 0;
  1529. ret = 0;
  1530. pipe_lock(pipe);
  1531. while (!pipe->nrbufs) {
  1532. if (signal_pending(current)) {
  1533. ret = -ERESTARTSYS;
  1534. break;
  1535. }
  1536. if (!pipe->writers)
  1537. break;
  1538. if (!pipe->waiting_writers) {
  1539. if (flags & SPLICE_F_NONBLOCK) {
  1540. ret = -EAGAIN;
  1541. break;
  1542. }
  1543. }
  1544. pipe_wait(pipe);
  1545. }
  1546. pipe_unlock(pipe);
  1547. return ret;
  1548. }
  1549. /*
  1550. * Make sure there's writeable room. Wait for room if we can, otherwise
  1551. * return an appropriate error.
  1552. */
  1553. static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1554. {
  1555. int ret;
  1556. /*
  1557. * Check ->nrbufs without the inode lock first. This function
  1558. * is speculative anyways, so missing one is ok.
  1559. */
  1560. if (pipe->nrbufs < PIPE_BUFFERS)
  1561. return 0;
  1562. ret = 0;
  1563. pipe_lock(pipe);
  1564. while (pipe->nrbufs >= PIPE_BUFFERS) {
  1565. if (!pipe->readers) {
  1566. send_sig(SIGPIPE, current, 0);
  1567. ret = -EPIPE;
  1568. break;
  1569. }
  1570. if (flags & SPLICE_F_NONBLOCK) {
  1571. ret = -EAGAIN;
  1572. break;
  1573. }
  1574. if (signal_pending(current)) {
  1575. ret = -ERESTARTSYS;
  1576. break;
  1577. }
  1578. pipe->waiting_writers++;
  1579. pipe_wait(pipe);
  1580. pipe->waiting_writers--;
  1581. }
  1582. pipe_unlock(pipe);
  1583. return ret;
  1584. }
  1585. /*
  1586. * Splice contents of ipipe to opipe.
  1587. */
  1588. static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  1589. struct pipe_inode_info *opipe,
  1590. size_t len, unsigned int flags)
  1591. {
  1592. struct pipe_buffer *ibuf, *obuf;
  1593. int ret = 0, nbuf;
  1594. bool input_wakeup = false;
  1595. retry:
  1596. ret = ipipe_prep(ipipe, flags);
  1597. if (ret)
  1598. return ret;
  1599. ret = opipe_prep(opipe, flags);
  1600. if (ret)
  1601. return ret;
  1602. /*
  1603. * Potential ABBA deadlock, work around it by ordering lock
  1604. * grabbing by pipe info address. Otherwise two different processes
  1605. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1606. */
  1607. pipe_double_lock(ipipe, opipe);
  1608. do {
  1609. if (!opipe->readers) {
  1610. send_sig(SIGPIPE, current, 0);
  1611. if (!ret)
  1612. ret = -EPIPE;
  1613. break;
  1614. }
  1615. if (!ipipe->nrbufs && !ipipe->writers)
  1616. break;
  1617. /*
  1618. * Cannot make any progress, because either the input
  1619. * pipe is empty or the output pipe is full.
  1620. */
  1621. if (!ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS) {
  1622. /* Already processed some buffers, break */
  1623. if (ret)
  1624. break;
  1625. if (flags & SPLICE_F_NONBLOCK) {
  1626. ret = -EAGAIN;
  1627. break;
  1628. }
  1629. /*
  1630. * We raced with another reader/writer and haven't
  1631. * managed to process any buffers. A zero return
  1632. * value means EOF, so retry instead.
  1633. */
  1634. pipe_unlock(ipipe);
  1635. pipe_unlock(opipe);
  1636. goto retry;
  1637. }
  1638. ibuf = ipipe->bufs + ipipe->curbuf;
  1639. nbuf = (opipe->curbuf + opipe->nrbufs) % PIPE_BUFFERS;
  1640. obuf = opipe->bufs + nbuf;
  1641. if (len >= ibuf->len) {
  1642. /*
  1643. * Simply move the whole buffer from ipipe to opipe
  1644. */
  1645. *obuf = *ibuf;
  1646. ibuf->ops = NULL;
  1647. opipe->nrbufs++;
  1648. ipipe->curbuf = (ipipe->curbuf + 1) % PIPE_BUFFERS;
  1649. ipipe->nrbufs--;
  1650. input_wakeup = true;
  1651. } else {
  1652. /*
  1653. * Get a reference to this pipe buffer,
  1654. * so we can copy the contents over.
  1655. */
  1656. ibuf->ops->get(ipipe, ibuf);
  1657. *obuf = *ibuf;
  1658. /*
  1659. * Don't inherit the gift flag, we need to
  1660. * prevent multiple steals of this page.
  1661. */
  1662. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1663. obuf->len = len;
  1664. opipe->nrbufs++;
  1665. ibuf->offset += obuf->len;
  1666. ibuf->len -= obuf->len;
  1667. }
  1668. ret += obuf->len;
  1669. len -= obuf->len;
  1670. } while (len);
  1671. pipe_unlock(ipipe);
  1672. pipe_unlock(opipe);
  1673. /*
  1674. * If we put data in the output pipe, wakeup any potential readers.
  1675. */
  1676. if (ret > 0) {
  1677. smp_mb();
  1678. if (waitqueue_active(&opipe->wait))
  1679. wake_up_interruptible(&opipe->wait);
  1680. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  1681. }
  1682. if (input_wakeup)
  1683. wakeup_pipe_writers(ipipe);
  1684. return ret;
  1685. }
  1686. /*
  1687. * Link contents of ipipe to opipe.
  1688. */
  1689. static int link_pipe(struct pipe_inode_info *ipipe,
  1690. struct pipe_inode_info *opipe,
  1691. size_t len, unsigned int flags)
  1692. {
  1693. struct pipe_buffer *ibuf, *obuf;
  1694. int ret = 0, i = 0, nbuf;
  1695. /*
  1696. * Potential ABBA deadlock, work around it by ordering lock
  1697. * grabbing by pipe info address. Otherwise two different processes
  1698. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1699. */
  1700. pipe_double_lock(ipipe, opipe);
  1701. do {
  1702. if (!opipe->readers) {
  1703. send_sig(SIGPIPE, current, 0);
  1704. if (!ret)
  1705. ret = -EPIPE;
  1706. break;
  1707. }
  1708. /*
  1709. * If we have iterated all input buffers or ran out of
  1710. * output room, break.
  1711. */
  1712. if (i >= ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS)
  1713. break;
  1714. ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
  1715. nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
  1716. /*
  1717. * Get a reference to this pipe buffer,
  1718. * so we can copy the contents over.
  1719. */
  1720. ibuf->ops->get(ipipe, ibuf);
  1721. obuf = opipe->bufs + nbuf;
  1722. *obuf = *ibuf;
  1723. /*
  1724. * Don't inherit the gift flag, we need to
  1725. * prevent multiple steals of this page.
  1726. */
  1727. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1728. if (obuf->len > len)
  1729. obuf->len = len;
  1730. opipe->nrbufs++;
  1731. ret += obuf->len;
  1732. len -= obuf->len;
  1733. i++;
  1734. } while (len);
  1735. /*
  1736. * return EAGAIN if we have the potential of some data in the
  1737. * future, otherwise just return 0
  1738. */
  1739. if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
  1740. ret = -EAGAIN;
  1741. pipe_unlock(ipipe);
  1742. pipe_unlock(opipe);
  1743. /*
  1744. * If we put data in the output pipe, wakeup any potential readers.
  1745. */
  1746. if (ret > 0) {
  1747. smp_mb();
  1748. if (waitqueue_active(&opipe->wait))
  1749. wake_up_interruptible(&opipe->wait);
  1750. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  1751. }
  1752. return ret;
  1753. }
  1754. /*
  1755. * This is a tee(1) implementation that works on pipes. It doesn't copy
  1756. * any data, it simply references the 'in' pages on the 'out' pipe.
  1757. * The 'flags' used are the SPLICE_F_* variants, currently the only
  1758. * applicable one is SPLICE_F_NONBLOCK.
  1759. */
  1760. static long do_tee(struct file *in, struct file *out, size_t len,
  1761. unsigned int flags)
  1762. {
  1763. struct pipe_inode_info *ipipe = pipe_info(in->f_path.dentry->d_inode);
  1764. struct pipe_inode_info *opipe = pipe_info(out->f_path.dentry->d_inode);
  1765. int ret = -EINVAL;
  1766. /*
  1767. * Duplicate the contents of ipipe to opipe without actually
  1768. * copying the data.
  1769. */
  1770. if (ipipe && opipe && ipipe != opipe) {
  1771. /*
  1772. * Keep going, unless we encounter an error. The ipipe/opipe
  1773. * ordering doesn't really matter.
  1774. */
  1775. ret = ipipe_prep(ipipe, flags);
  1776. if (!ret) {
  1777. ret = opipe_prep(opipe, flags);
  1778. if (!ret)
  1779. ret = link_pipe(ipipe, opipe, len, flags);
  1780. }
  1781. }
  1782. return ret;
  1783. }
  1784. SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
  1785. {
  1786. struct file *in;
  1787. int error, fput_in;
  1788. if (unlikely(!len))
  1789. return 0;
  1790. error = -EBADF;
  1791. in = fget_light(fdin, &fput_in);
  1792. if (in) {
  1793. if (in->f_mode & FMODE_READ) {
  1794. int fput_out;
  1795. struct file *out = fget_light(fdout, &fput_out);
  1796. if (out) {
  1797. if (out->f_mode & FMODE_WRITE)
  1798. error = do_tee(in, out, len, flags);
  1799. fput_light(out, fput_out);
  1800. }
  1801. }
  1802. fput_light(in, fput_in);
  1803. }
  1804. return error;
  1805. }