splice.c 48 KB

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