splice.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047
  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. .confirm = page_cache_pipe_buf_confirm,
  125. .release = page_cache_pipe_buf_release,
  126. .steal = page_cache_pipe_buf_steal,
  127. .get = generic_pipe_buf_get,
  128. };
  129. static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
  130. struct pipe_buffer *buf)
  131. {
  132. if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
  133. return 1;
  134. buf->flags |= PIPE_BUF_FLAG_LRU;
  135. return generic_pipe_buf_steal(pipe, buf);
  136. }
  137. static const struct pipe_buf_operations user_page_pipe_buf_ops = {
  138. .can_merge = 0,
  139. .confirm = generic_pipe_buf_confirm,
  140. .release = page_cache_pipe_buf_release,
  141. .steal = user_page_pipe_buf_steal,
  142. .get = generic_pipe_buf_get,
  143. };
  144. static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
  145. {
  146. smp_mb();
  147. if (waitqueue_active(&pipe->wait))
  148. wake_up_interruptible(&pipe->wait);
  149. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  150. }
  151. /**
  152. * splice_to_pipe - fill passed data into a pipe
  153. * @pipe: pipe to fill
  154. * @spd: data to fill
  155. *
  156. * Description:
  157. * @spd contains a map of pages and len/offset tuples, along with
  158. * the struct pipe_buf_operations associated with these pages. This
  159. * function will link that data to the pipe.
  160. *
  161. */
  162. ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
  163. struct splice_pipe_desc *spd)
  164. {
  165. unsigned int spd_pages = spd->nr_pages;
  166. int ret, do_wakeup, page_nr;
  167. if (!spd_pages)
  168. return 0;
  169. ret = 0;
  170. do_wakeup = 0;
  171. page_nr = 0;
  172. pipe_lock(pipe);
  173. for (;;) {
  174. if (!pipe->readers) {
  175. send_sig(SIGPIPE, current, 0);
  176. if (!ret)
  177. ret = -EPIPE;
  178. break;
  179. }
  180. if (pipe->nrbufs < pipe->buffers) {
  181. int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
  182. struct pipe_buffer *buf = pipe->bufs + newbuf;
  183. buf->page = spd->pages[page_nr];
  184. buf->offset = spd->partial[page_nr].offset;
  185. buf->len = spd->partial[page_nr].len;
  186. buf->private = spd->partial[page_nr].private;
  187. buf->ops = spd->ops;
  188. buf->flags = 0;
  189. if (spd->flags & SPLICE_F_GIFT)
  190. buf->flags |= PIPE_BUF_FLAG_GIFT;
  191. pipe->nrbufs++;
  192. page_nr++;
  193. ret += buf->len;
  194. if (pipe->files)
  195. do_wakeup = 1;
  196. if (!--spd->nr_pages)
  197. break;
  198. if (pipe->nrbufs < pipe->buffers)
  199. continue;
  200. break;
  201. }
  202. if (spd->flags & SPLICE_F_NONBLOCK) {
  203. if (!ret)
  204. ret = -EAGAIN;
  205. break;
  206. }
  207. if (signal_pending(current)) {
  208. if (!ret)
  209. ret = -ERESTARTSYS;
  210. break;
  211. }
  212. if (do_wakeup) {
  213. smp_mb();
  214. if (waitqueue_active(&pipe->wait))
  215. wake_up_interruptible_sync(&pipe->wait);
  216. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  217. do_wakeup = 0;
  218. }
  219. pipe->waiting_writers++;
  220. pipe_wait(pipe);
  221. pipe->waiting_writers--;
  222. }
  223. pipe_unlock(pipe);
  224. if (do_wakeup)
  225. wakeup_pipe_readers(pipe);
  226. while (page_nr < spd_pages)
  227. spd->spd_release(spd, page_nr++);
  228. return ret;
  229. }
  230. EXPORT_SYMBOL_GPL(splice_to_pipe);
  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. mapping_gfp_constraint(mapping, 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. if (IS_DAX(in->f_mapping->host))
  460. return default_file_splice_read(in, ppos, pipe, len, flags);
  461. isize = i_size_read(in->f_mapping->host);
  462. if (unlikely(*ppos >= isize))
  463. return 0;
  464. left = isize - *ppos;
  465. if (unlikely(left < len))
  466. len = left;
  467. ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
  468. if (ret > 0) {
  469. *ppos += ret;
  470. file_accessed(in);
  471. }
  472. return ret;
  473. }
  474. EXPORT_SYMBOL(generic_file_splice_read);
  475. static const struct pipe_buf_operations default_pipe_buf_ops = {
  476. .can_merge = 0,
  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. .confirm = generic_pipe_buf_confirm,
  491. .release = generic_pipe_buf_release,
  492. .steal = generic_pipe_buf_nosteal,
  493. .get = generic_pipe_buf_get,
  494. };
  495. EXPORT_SYMBOL(nosteal_pipe_buf_ops);
  496. static ssize_t kernel_readv(struct file *file, const struct iovec *vec,
  497. unsigned long vlen, loff_t offset)
  498. {
  499. mm_segment_t old_fs;
  500. loff_t pos = offset;
  501. ssize_t res;
  502. old_fs = get_fs();
  503. set_fs(get_ds());
  504. /* The cast to a user pointer is valid due to the set_fs() */
  505. res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos);
  506. set_fs(old_fs);
  507. return res;
  508. }
  509. ssize_t kernel_write(struct file *file, const char *buf, size_t count,
  510. loff_t pos)
  511. {
  512. mm_segment_t old_fs;
  513. ssize_t res;
  514. old_fs = get_fs();
  515. set_fs(get_ds());
  516. /* The cast to a user pointer is valid due to the set_fs() */
  517. res = vfs_write(file, (__force const char __user *)buf, count, &pos);
  518. set_fs(old_fs);
  519. return res;
  520. }
  521. EXPORT_SYMBOL(kernel_write);
  522. ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
  523. struct pipe_inode_info *pipe, size_t len,
  524. unsigned int flags)
  525. {
  526. unsigned int nr_pages;
  527. unsigned int nr_freed;
  528. size_t offset;
  529. struct page *pages[PIPE_DEF_BUFFERS];
  530. struct partial_page partial[PIPE_DEF_BUFFERS];
  531. struct iovec *vec, __vec[PIPE_DEF_BUFFERS];
  532. ssize_t res;
  533. size_t this_len;
  534. int error;
  535. int i;
  536. struct splice_pipe_desc spd = {
  537. .pages = pages,
  538. .partial = partial,
  539. .nr_pages_max = PIPE_DEF_BUFFERS,
  540. .flags = flags,
  541. .ops = &default_pipe_buf_ops,
  542. .spd_release = spd_release_page,
  543. };
  544. if (splice_grow_spd(pipe, &spd))
  545. return -ENOMEM;
  546. res = -ENOMEM;
  547. vec = __vec;
  548. if (spd.nr_pages_max > PIPE_DEF_BUFFERS) {
  549. vec = kmalloc(spd.nr_pages_max * sizeof(struct iovec), GFP_KERNEL);
  550. if (!vec)
  551. goto shrink_ret;
  552. }
  553. offset = *ppos & ~PAGE_CACHE_MASK;
  554. nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  555. for (i = 0; i < nr_pages && i < spd.nr_pages_max && len; i++) {
  556. struct page *page;
  557. page = alloc_page(GFP_USER);
  558. error = -ENOMEM;
  559. if (!page)
  560. goto err;
  561. this_len = min_t(size_t, len, PAGE_CACHE_SIZE - offset);
  562. vec[i].iov_base = (void __user *) page_address(page);
  563. vec[i].iov_len = this_len;
  564. spd.pages[i] = page;
  565. spd.nr_pages++;
  566. len -= this_len;
  567. offset = 0;
  568. }
  569. res = kernel_readv(in, vec, spd.nr_pages, *ppos);
  570. if (res < 0) {
  571. error = res;
  572. goto err;
  573. }
  574. error = 0;
  575. if (!res)
  576. goto err;
  577. nr_freed = 0;
  578. for (i = 0; i < spd.nr_pages; i++) {
  579. this_len = min_t(size_t, vec[i].iov_len, res);
  580. spd.partial[i].offset = 0;
  581. spd.partial[i].len = this_len;
  582. if (!this_len) {
  583. __free_page(spd.pages[i]);
  584. spd.pages[i] = NULL;
  585. nr_freed++;
  586. }
  587. res -= this_len;
  588. }
  589. spd.nr_pages -= nr_freed;
  590. res = splice_to_pipe(pipe, &spd);
  591. if (res > 0)
  592. *ppos += res;
  593. shrink_ret:
  594. if (vec != __vec)
  595. kfree(vec);
  596. splice_shrink_spd(&spd);
  597. return res;
  598. err:
  599. for (i = 0; i < spd.nr_pages; i++)
  600. __free_page(spd.pages[i]);
  601. res = error;
  602. goto shrink_ret;
  603. }
  604. EXPORT_SYMBOL(default_file_splice_read);
  605. /*
  606. * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
  607. * using sendpage(). Return the number of bytes sent.
  608. */
  609. static int pipe_to_sendpage(struct pipe_inode_info *pipe,
  610. struct pipe_buffer *buf, struct splice_desc *sd)
  611. {
  612. struct file *file = sd->u.file;
  613. loff_t pos = sd->pos;
  614. int more;
  615. if (!likely(file->f_op->sendpage))
  616. return -EINVAL;
  617. more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
  618. if (sd->len < sd->total_len && pipe->nrbufs > 1)
  619. more |= MSG_SENDPAGE_NOTLAST;
  620. return file->f_op->sendpage(file, buf->page, buf->offset,
  621. sd->len, &pos, more);
  622. }
  623. static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
  624. {
  625. smp_mb();
  626. if (waitqueue_active(&pipe->wait))
  627. wake_up_interruptible(&pipe->wait);
  628. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  629. }
  630. /**
  631. * splice_from_pipe_feed - feed available data from a pipe to a file
  632. * @pipe: pipe to splice from
  633. * @sd: information to @actor
  634. * @actor: handler that splices the data
  635. *
  636. * Description:
  637. * This function loops over the pipe and calls @actor to do the
  638. * actual moving of a single struct pipe_buffer to the desired
  639. * destination. It returns when there's no more buffers left in
  640. * the pipe or if the requested number of bytes (@sd->total_len)
  641. * have been copied. It returns a positive number (one) if the
  642. * pipe needs to be filled with more data, zero if the required
  643. * number of bytes have been copied and -errno on error.
  644. *
  645. * This, together with splice_from_pipe_{begin,end,next}, may be
  646. * used to implement the functionality of __splice_from_pipe() when
  647. * locking is required around copying the pipe buffers to the
  648. * destination.
  649. */
  650. static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
  651. splice_actor *actor)
  652. {
  653. int ret;
  654. while (pipe->nrbufs) {
  655. struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  656. const struct pipe_buf_operations *ops = buf->ops;
  657. sd->len = buf->len;
  658. if (sd->len > sd->total_len)
  659. sd->len = sd->total_len;
  660. ret = buf->ops->confirm(pipe, buf);
  661. if (unlikely(ret)) {
  662. if (ret == -ENODATA)
  663. ret = 0;
  664. return ret;
  665. }
  666. ret = actor(pipe, buf, sd);
  667. if (ret <= 0)
  668. return ret;
  669. buf->offset += ret;
  670. buf->len -= ret;
  671. sd->num_spliced += ret;
  672. sd->len -= ret;
  673. sd->pos += ret;
  674. sd->total_len -= ret;
  675. if (!buf->len) {
  676. buf->ops = NULL;
  677. ops->release(pipe, buf);
  678. pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
  679. pipe->nrbufs--;
  680. if (pipe->files)
  681. sd->need_wakeup = true;
  682. }
  683. if (!sd->total_len)
  684. return 0;
  685. }
  686. return 1;
  687. }
  688. /**
  689. * splice_from_pipe_next - wait for some data to splice from
  690. * @pipe: pipe to splice from
  691. * @sd: information about the splice operation
  692. *
  693. * Description:
  694. * This function will wait for some data and return a positive
  695. * value (one) if pipe buffers are available. It will return zero
  696. * or -errno if no more data needs to be spliced.
  697. */
  698. static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
  699. {
  700. /*
  701. * Check for signal early to make process killable when there are
  702. * always buffers available
  703. */
  704. if (signal_pending(current))
  705. return -ERESTARTSYS;
  706. while (!pipe->nrbufs) {
  707. if (!pipe->writers)
  708. return 0;
  709. if (!pipe->waiting_writers && sd->num_spliced)
  710. return 0;
  711. if (sd->flags & SPLICE_F_NONBLOCK)
  712. return -EAGAIN;
  713. if (signal_pending(current))
  714. return -ERESTARTSYS;
  715. if (sd->need_wakeup) {
  716. wakeup_pipe_writers(pipe);
  717. sd->need_wakeup = false;
  718. }
  719. pipe_wait(pipe);
  720. }
  721. return 1;
  722. }
  723. /**
  724. * splice_from_pipe_begin - start splicing from pipe
  725. * @sd: information about the splice operation
  726. *
  727. * Description:
  728. * This function should be called before a loop containing
  729. * splice_from_pipe_next() and splice_from_pipe_feed() to
  730. * initialize the necessary fields of @sd.
  731. */
  732. static void splice_from_pipe_begin(struct splice_desc *sd)
  733. {
  734. sd->num_spliced = 0;
  735. sd->need_wakeup = false;
  736. }
  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. static 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. /**
  753. * __splice_from_pipe - splice data from a pipe to given actor
  754. * @pipe: pipe to splice from
  755. * @sd: information to @actor
  756. * @actor: handler that splices the data
  757. *
  758. * Description:
  759. * This function does little more than loop over the pipe and call
  760. * @actor to do the actual moving of a single struct pipe_buffer to
  761. * the desired destination. See pipe_to_file, pipe_to_sendpage, or
  762. * pipe_to_user.
  763. *
  764. */
  765. ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
  766. splice_actor *actor)
  767. {
  768. int ret;
  769. splice_from_pipe_begin(sd);
  770. do {
  771. cond_resched();
  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. * iter_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. * This one is ->write_iter-based.
  822. *
  823. */
  824. ssize_t
  825. iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  826. loff_t *ppos, size_t len, unsigned int flags)
  827. {
  828. struct splice_desc sd = {
  829. .total_len = len,
  830. .flags = flags,
  831. .pos = *ppos,
  832. .u.file = out,
  833. };
  834. int nbufs = pipe->buffers;
  835. struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
  836. GFP_KERNEL);
  837. ssize_t ret;
  838. if (unlikely(!array))
  839. return -ENOMEM;
  840. pipe_lock(pipe);
  841. splice_from_pipe_begin(&sd);
  842. while (sd.total_len) {
  843. struct iov_iter from;
  844. size_t left;
  845. int n, idx;
  846. ret = splice_from_pipe_next(pipe, &sd);
  847. if (ret <= 0)
  848. break;
  849. if (unlikely(nbufs < pipe->buffers)) {
  850. kfree(array);
  851. nbufs = pipe->buffers;
  852. array = kcalloc(nbufs, sizeof(struct bio_vec),
  853. GFP_KERNEL);
  854. if (!array) {
  855. ret = -ENOMEM;
  856. break;
  857. }
  858. }
  859. /* build the vector */
  860. left = sd.total_len;
  861. for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) {
  862. struct pipe_buffer *buf = pipe->bufs + idx;
  863. size_t this_len = buf->len;
  864. if (this_len > left)
  865. this_len = left;
  866. if (idx == pipe->buffers - 1)
  867. idx = -1;
  868. ret = buf->ops->confirm(pipe, buf);
  869. if (unlikely(ret)) {
  870. if (ret == -ENODATA)
  871. ret = 0;
  872. goto done;
  873. }
  874. array[n].bv_page = buf->page;
  875. array[n].bv_len = this_len;
  876. array[n].bv_offset = buf->offset;
  877. left -= this_len;
  878. }
  879. iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n,
  880. sd.total_len - left);
  881. ret = vfs_iter_write(out, &from, &sd.pos);
  882. if (ret <= 0)
  883. break;
  884. sd.num_spliced += ret;
  885. sd.total_len -= ret;
  886. *ppos = sd.pos;
  887. /* dismiss the fully eaten buffers, adjust the partial one */
  888. while (ret) {
  889. struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  890. if (ret >= buf->len) {
  891. const struct pipe_buf_operations *ops = buf->ops;
  892. ret -= buf->len;
  893. buf->len = 0;
  894. buf->ops = NULL;
  895. ops->release(pipe, buf);
  896. pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
  897. pipe->nrbufs--;
  898. if (pipe->files)
  899. sd.need_wakeup = true;
  900. } else {
  901. buf->offset += ret;
  902. buf->len -= ret;
  903. ret = 0;
  904. }
  905. }
  906. }
  907. done:
  908. kfree(array);
  909. splice_from_pipe_end(pipe, &sd);
  910. pipe_unlock(pipe);
  911. if (sd.num_spliced)
  912. ret = sd.num_spliced;
  913. return ret;
  914. }
  915. EXPORT_SYMBOL(iter_file_splice_write);
  916. static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  917. struct splice_desc *sd)
  918. {
  919. int ret;
  920. void *data;
  921. loff_t tmp = sd->pos;
  922. data = kmap(buf->page);
  923. ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
  924. kunmap(buf->page);
  925. return ret;
  926. }
  927. static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
  928. struct file *out, loff_t *ppos,
  929. size_t len, unsigned int flags)
  930. {
  931. ssize_t ret;
  932. ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
  933. if (ret > 0)
  934. *ppos += ret;
  935. return ret;
  936. }
  937. /**
  938. * generic_splice_sendpage - splice data from a pipe to a socket
  939. * @pipe: pipe to splice from
  940. * @out: socket to write to
  941. * @ppos: position in @out
  942. * @len: number of bytes to splice
  943. * @flags: splice modifier flags
  944. *
  945. * Description:
  946. * Will send @len bytes from the pipe to a network socket. No data copying
  947. * is involved.
  948. *
  949. */
  950. ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
  951. loff_t *ppos, size_t len, unsigned int flags)
  952. {
  953. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
  954. }
  955. EXPORT_SYMBOL(generic_splice_sendpage);
  956. /*
  957. * Attempt to initiate a splice from pipe to file.
  958. */
  959. static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  960. loff_t *ppos, size_t len, unsigned int flags)
  961. {
  962. ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
  963. loff_t *, size_t, unsigned int);
  964. if (out->f_op->splice_write)
  965. splice_write = out->f_op->splice_write;
  966. else
  967. splice_write = default_file_splice_write;
  968. return splice_write(pipe, out, ppos, len, flags);
  969. }
  970. /*
  971. * Attempt to initiate a splice from a file to a pipe.
  972. */
  973. static long do_splice_to(struct file *in, loff_t *ppos,
  974. struct pipe_inode_info *pipe, size_t len,
  975. unsigned int flags)
  976. {
  977. ssize_t (*splice_read)(struct file *, loff_t *,
  978. struct pipe_inode_info *, size_t, unsigned int);
  979. int ret;
  980. if (unlikely(!(in->f_mode & FMODE_READ)))
  981. return -EBADF;
  982. ret = rw_verify_area(READ, in, ppos, len);
  983. if (unlikely(ret < 0))
  984. return ret;
  985. if (in->f_op->splice_read)
  986. splice_read = in->f_op->splice_read;
  987. else
  988. splice_read = default_file_splice_read;
  989. return splice_read(in, ppos, pipe, len, flags);
  990. }
  991. /**
  992. * splice_direct_to_actor - splices data directly between two non-pipes
  993. * @in: file to splice from
  994. * @sd: actor information on where to splice to
  995. * @actor: handles the data splicing
  996. *
  997. * Description:
  998. * This is a special case helper to splice directly between two
  999. * points, without requiring an explicit pipe. Internally an allocated
  1000. * pipe is cached in the process, and reused during the lifetime of
  1001. * that process.
  1002. *
  1003. */
  1004. ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
  1005. splice_direct_actor *actor)
  1006. {
  1007. struct pipe_inode_info *pipe;
  1008. long ret, bytes;
  1009. umode_t i_mode;
  1010. size_t len;
  1011. int i, flags, more;
  1012. /*
  1013. * We require the input being a regular file, as we don't want to
  1014. * randomly drop data for eg socket -> socket splicing. Use the
  1015. * piped splicing for that!
  1016. */
  1017. i_mode = file_inode(in)->i_mode;
  1018. if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  1019. return -EINVAL;
  1020. /*
  1021. * neither in nor out is a pipe, setup an internal pipe attached to
  1022. * 'out' and transfer the wanted data from 'in' to 'out' through that
  1023. */
  1024. pipe = current->splice_pipe;
  1025. if (unlikely(!pipe)) {
  1026. pipe = alloc_pipe_info();
  1027. if (!pipe)
  1028. return -ENOMEM;
  1029. /*
  1030. * We don't have an immediate reader, but we'll read the stuff
  1031. * out of the pipe right after the splice_to_pipe(). So set
  1032. * PIPE_READERS appropriately.
  1033. */
  1034. pipe->readers = 1;
  1035. current->splice_pipe = pipe;
  1036. }
  1037. /*
  1038. * Do the splice.
  1039. */
  1040. ret = 0;
  1041. bytes = 0;
  1042. len = sd->total_len;
  1043. flags = sd->flags;
  1044. /*
  1045. * Don't block on output, we have to drain the direct pipe.
  1046. */
  1047. sd->flags &= ~SPLICE_F_NONBLOCK;
  1048. more = sd->flags & SPLICE_F_MORE;
  1049. while (len) {
  1050. size_t read_len;
  1051. loff_t pos = sd->pos, prev_pos = pos;
  1052. ret = do_splice_to(in, &pos, pipe, len, flags);
  1053. if (unlikely(ret <= 0))
  1054. goto out_release;
  1055. read_len = ret;
  1056. sd->total_len = read_len;
  1057. /*
  1058. * If more data is pending, set SPLICE_F_MORE
  1059. * If this is the last data and SPLICE_F_MORE was not set
  1060. * initially, clears it.
  1061. */
  1062. if (read_len < len)
  1063. sd->flags |= SPLICE_F_MORE;
  1064. else if (!more)
  1065. sd->flags &= ~SPLICE_F_MORE;
  1066. /*
  1067. * NOTE: nonblocking mode only applies to the input. We
  1068. * must not do the output in nonblocking mode as then we
  1069. * could get stuck data in the internal pipe:
  1070. */
  1071. ret = actor(pipe, sd);
  1072. if (unlikely(ret <= 0)) {
  1073. sd->pos = prev_pos;
  1074. goto out_release;
  1075. }
  1076. bytes += ret;
  1077. len -= ret;
  1078. sd->pos = pos;
  1079. if (ret < read_len) {
  1080. sd->pos = prev_pos + ret;
  1081. goto out_release;
  1082. }
  1083. }
  1084. done:
  1085. pipe->nrbufs = pipe->curbuf = 0;
  1086. file_accessed(in);
  1087. return bytes;
  1088. out_release:
  1089. /*
  1090. * If we did an incomplete transfer we must release
  1091. * the pipe buffers in question:
  1092. */
  1093. for (i = 0; i < pipe->buffers; i++) {
  1094. struct pipe_buffer *buf = pipe->bufs + i;
  1095. if (buf->ops) {
  1096. buf->ops->release(pipe, buf);
  1097. buf->ops = NULL;
  1098. }
  1099. }
  1100. if (!bytes)
  1101. bytes = ret;
  1102. goto done;
  1103. }
  1104. EXPORT_SYMBOL(splice_direct_to_actor);
  1105. static int direct_splice_actor(struct pipe_inode_info *pipe,
  1106. struct splice_desc *sd)
  1107. {
  1108. struct file *file = sd->u.file;
  1109. return do_splice_from(pipe, file, sd->opos, sd->total_len,
  1110. sd->flags);
  1111. }
  1112. /**
  1113. * do_splice_direct - splices data directly between two files
  1114. * @in: file to splice from
  1115. * @ppos: input file offset
  1116. * @out: file to splice to
  1117. * @opos: output file offset
  1118. * @len: number of bytes to splice
  1119. * @flags: splice modifier flags
  1120. *
  1121. * Description:
  1122. * For use by do_sendfile(). splice can easily emulate sendfile, but
  1123. * doing it in the application would incur an extra system call
  1124. * (splice in + splice out, as compared to just sendfile()). So this helper
  1125. * can splice directly through a process-private pipe.
  1126. *
  1127. */
  1128. long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
  1129. loff_t *opos, size_t len, unsigned int flags)
  1130. {
  1131. struct splice_desc sd = {
  1132. .len = len,
  1133. .total_len = len,
  1134. .flags = flags,
  1135. .pos = *ppos,
  1136. .u.file = out,
  1137. .opos = opos,
  1138. };
  1139. long ret;
  1140. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  1141. return -EBADF;
  1142. if (unlikely(out->f_flags & O_APPEND))
  1143. return -EINVAL;
  1144. ret = rw_verify_area(WRITE, out, opos, len);
  1145. if (unlikely(ret < 0))
  1146. return ret;
  1147. ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
  1148. if (ret > 0)
  1149. *ppos = sd.pos;
  1150. return ret;
  1151. }
  1152. EXPORT_SYMBOL(do_splice_direct);
  1153. static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  1154. struct pipe_inode_info *opipe,
  1155. size_t len, unsigned int flags);
  1156. /*
  1157. * Determine where to splice to/from.
  1158. */
  1159. static long do_splice(struct file *in, loff_t __user *off_in,
  1160. struct file *out, loff_t __user *off_out,
  1161. size_t len, unsigned int flags)
  1162. {
  1163. struct pipe_inode_info *ipipe;
  1164. struct pipe_inode_info *opipe;
  1165. loff_t offset;
  1166. long ret;
  1167. ipipe = get_pipe_info(in);
  1168. opipe = get_pipe_info(out);
  1169. if (ipipe && opipe) {
  1170. if (off_in || off_out)
  1171. return -ESPIPE;
  1172. if (!(in->f_mode & FMODE_READ))
  1173. return -EBADF;
  1174. if (!(out->f_mode & FMODE_WRITE))
  1175. return -EBADF;
  1176. /* Splicing to self would be fun, but... */
  1177. if (ipipe == opipe)
  1178. return -EINVAL;
  1179. return splice_pipe_to_pipe(ipipe, opipe, len, flags);
  1180. }
  1181. if (ipipe) {
  1182. if (off_in)
  1183. return -ESPIPE;
  1184. if (off_out) {
  1185. if (!(out->f_mode & FMODE_PWRITE))
  1186. return -EINVAL;
  1187. if (copy_from_user(&offset, off_out, sizeof(loff_t)))
  1188. return -EFAULT;
  1189. } else {
  1190. offset = out->f_pos;
  1191. }
  1192. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  1193. return -EBADF;
  1194. if (unlikely(out->f_flags & O_APPEND))
  1195. return -EINVAL;
  1196. ret = rw_verify_area(WRITE, out, &offset, len);
  1197. if (unlikely(ret < 0))
  1198. return ret;
  1199. file_start_write(out);
  1200. ret = do_splice_from(ipipe, out, &offset, len, flags);
  1201. file_end_write(out);
  1202. if (!off_out)
  1203. out->f_pos = offset;
  1204. else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
  1205. ret = -EFAULT;
  1206. return ret;
  1207. }
  1208. if (opipe) {
  1209. if (off_out)
  1210. return -ESPIPE;
  1211. if (off_in) {
  1212. if (!(in->f_mode & FMODE_PREAD))
  1213. return -EINVAL;
  1214. if (copy_from_user(&offset, off_in, sizeof(loff_t)))
  1215. return -EFAULT;
  1216. } else {
  1217. offset = in->f_pos;
  1218. }
  1219. ret = do_splice_to(in, &offset, opipe, len, flags);
  1220. if (!off_in)
  1221. in->f_pos = offset;
  1222. else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
  1223. ret = -EFAULT;
  1224. return ret;
  1225. }
  1226. return -EINVAL;
  1227. }
  1228. /*
  1229. * Map an iov into an array of pages and offset/length tupples. With the
  1230. * partial_page structure, we can map several non-contiguous ranges into
  1231. * our ones pages[] map instead of splitting that operation into pieces.
  1232. * Could easily be exported as a generic helper for other users, in which
  1233. * case one would probably want to add a 'max_nr_pages' parameter as well.
  1234. */
  1235. static int get_iovec_page_array(const struct iovec __user *iov,
  1236. unsigned int nr_vecs, struct page **pages,
  1237. struct partial_page *partial, bool aligned,
  1238. unsigned int pipe_buffers)
  1239. {
  1240. int buffers = 0, error = 0;
  1241. while (nr_vecs) {
  1242. unsigned long off, npages;
  1243. struct iovec entry;
  1244. void __user *base;
  1245. size_t len;
  1246. int i;
  1247. error = -EFAULT;
  1248. if (copy_from_user(&entry, iov, sizeof(entry)))
  1249. break;
  1250. base = entry.iov_base;
  1251. len = entry.iov_len;
  1252. /*
  1253. * Sanity check this iovec. 0 read succeeds.
  1254. */
  1255. error = 0;
  1256. if (unlikely(!len))
  1257. break;
  1258. error = -EFAULT;
  1259. if (!access_ok(VERIFY_READ, base, len))
  1260. break;
  1261. /*
  1262. * Get this base offset and number of pages, then map
  1263. * in the user pages.
  1264. */
  1265. off = (unsigned long) base & ~PAGE_MASK;
  1266. /*
  1267. * If asked for alignment, the offset must be zero and the
  1268. * length a multiple of the PAGE_SIZE.
  1269. */
  1270. error = -EINVAL;
  1271. if (aligned && (off || len & ~PAGE_MASK))
  1272. break;
  1273. npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1274. if (npages > pipe_buffers - buffers)
  1275. npages = pipe_buffers - buffers;
  1276. error = get_user_pages_fast((unsigned long)base, npages,
  1277. 0, &pages[buffers]);
  1278. if (unlikely(error <= 0))
  1279. break;
  1280. /*
  1281. * Fill this contiguous range into the partial page map.
  1282. */
  1283. for (i = 0; i < error; i++) {
  1284. const int plen = min_t(size_t, len, PAGE_SIZE - off);
  1285. partial[buffers].offset = off;
  1286. partial[buffers].len = plen;
  1287. off = 0;
  1288. len -= plen;
  1289. buffers++;
  1290. }
  1291. /*
  1292. * We didn't complete this iov, stop here since it probably
  1293. * means we have to move some of this into a pipe to
  1294. * be able to continue.
  1295. */
  1296. if (len)
  1297. break;
  1298. /*
  1299. * Don't continue if we mapped fewer pages than we asked for,
  1300. * or if we mapped the max number of pages that we have
  1301. * room for.
  1302. */
  1303. if (error < npages || buffers == pipe_buffers)
  1304. break;
  1305. nr_vecs--;
  1306. iov++;
  1307. }
  1308. if (buffers)
  1309. return buffers;
  1310. return error;
  1311. }
  1312. static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  1313. struct splice_desc *sd)
  1314. {
  1315. int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
  1316. return n == sd->len ? n : -EFAULT;
  1317. }
  1318. /*
  1319. * For lack of a better implementation, implement vmsplice() to userspace
  1320. * as a simple copy of the pipes pages to the user iov.
  1321. */
  1322. static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov,
  1323. unsigned long nr_segs, unsigned int flags)
  1324. {
  1325. struct pipe_inode_info *pipe;
  1326. struct splice_desc sd;
  1327. long ret;
  1328. struct iovec iovstack[UIO_FASTIOV];
  1329. struct iovec *iov = iovstack;
  1330. struct iov_iter iter;
  1331. pipe = get_pipe_info(file);
  1332. if (!pipe)
  1333. return -EBADF;
  1334. ret = import_iovec(READ, uiov, nr_segs,
  1335. ARRAY_SIZE(iovstack), &iov, &iter);
  1336. if (ret < 0)
  1337. return ret;
  1338. sd.total_len = iov_iter_count(&iter);
  1339. sd.len = 0;
  1340. sd.flags = flags;
  1341. sd.u.data = &iter;
  1342. sd.pos = 0;
  1343. if (sd.total_len) {
  1344. pipe_lock(pipe);
  1345. ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
  1346. pipe_unlock(pipe);
  1347. }
  1348. kfree(iov);
  1349. return ret;
  1350. }
  1351. /*
  1352. * vmsplice splices a user address range into a pipe. It can be thought of
  1353. * as splice-from-memory, where the regular splice is splice-from-file (or
  1354. * to file). In both cases the output is a pipe, naturally.
  1355. */
  1356. static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
  1357. unsigned long nr_segs, unsigned int flags)
  1358. {
  1359. struct pipe_inode_info *pipe;
  1360. struct page *pages[PIPE_DEF_BUFFERS];
  1361. struct partial_page partial[PIPE_DEF_BUFFERS];
  1362. struct splice_pipe_desc spd = {
  1363. .pages = pages,
  1364. .partial = partial,
  1365. .nr_pages_max = PIPE_DEF_BUFFERS,
  1366. .flags = flags,
  1367. .ops = &user_page_pipe_buf_ops,
  1368. .spd_release = spd_release_page,
  1369. };
  1370. long ret;
  1371. pipe = get_pipe_info(file);
  1372. if (!pipe)
  1373. return -EBADF;
  1374. if (splice_grow_spd(pipe, &spd))
  1375. return -ENOMEM;
  1376. spd.nr_pages = get_iovec_page_array(iov, nr_segs, spd.pages,
  1377. spd.partial, false,
  1378. spd.nr_pages_max);
  1379. if (spd.nr_pages <= 0)
  1380. ret = spd.nr_pages;
  1381. else
  1382. ret = splice_to_pipe(pipe, &spd);
  1383. splice_shrink_spd(&spd);
  1384. return ret;
  1385. }
  1386. /*
  1387. * Note that vmsplice only really supports true splicing _from_ user memory
  1388. * to a pipe, not the other way around. Splicing from user memory is a simple
  1389. * operation that can be supported without any funky alignment restrictions
  1390. * or nasty vm tricks. We simply map in the user memory and fill them into
  1391. * a pipe. The reverse isn't quite as easy, though. There are two possible
  1392. * solutions for that:
  1393. *
  1394. * - memcpy() the data internally, at which point we might as well just
  1395. * do a regular read() on the buffer anyway.
  1396. * - Lots of nasty vm tricks, that are neither fast nor flexible (it
  1397. * has restriction limitations on both ends of the pipe).
  1398. *
  1399. * Currently we punt and implement it as a normal copy, see pipe_to_user().
  1400. *
  1401. */
  1402. SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
  1403. unsigned long, nr_segs, unsigned int, flags)
  1404. {
  1405. struct fd f;
  1406. long error;
  1407. if (unlikely(nr_segs > UIO_MAXIOV))
  1408. return -EINVAL;
  1409. else if (unlikely(!nr_segs))
  1410. return 0;
  1411. error = -EBADF;
  1412. f = fdget(fd);
  1413. if (f.file) {
  1414. if (f.file->f_mode & FMODE_WRITE)
  1415. error = vmsplice_to_pipe(f.file, iov, nr_segs, flags);
  1416. else if (f.file->f_mode & FMODE_READ)
  1417. error = vmsplice_to_user(f.file, iov, nr_segs, flags);
  1418. fdput(f);
  1419. }
  1420. return error;
  1421. }
  1422. #ifdef CONFIG_COMPAT
  1423. COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
  1424. unsigned int, nr_segs, unsigned int, flags)
  1425. {
  1426. unsigned i;
  1427. struct iovec __user *iov;
  1428. if (nr_segs > UIO_MAXIOV)
  1429. return -EINVAL;
  1430. iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
  1431. for (i = 0; i < nr_segs; i++) {
  1432. struct compat_iovec v;
  1433. if (get_user(v.iov_base, &iov32[i].iov_base) ||
  1434. get_user(v.iov_len, &iov32[i].iov_len) ||
  1435. put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
  1436. put_user(v.iov_len, &iov[i].iov_len))
  1437. return -EFAULT;
  1438. }
  1439. return sys_vmsplice(fd, iov, nr_segs, flags);
  1440. }
  1441. #endif
  1442. SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
  1443. int, fd_out, loff_t __user *, off_out,
  1444. size_t, len, unsigned int, flags)
  1445. {
  1446. struct fd in, out;
  1447. long error;
  1448. if (unlikely(!len))
  1449. return 0;
  1450. error = -EBADF;
  1451. in = fdget(fd_in);
  1452. if (in.file) {
  1453. if (in.file->f_mode & FMODE_READ) {
  1454. out = fdget(fd_out);
  1455. if (out.file) {
  1456. if (out.file->f_mode & FMODE_WRITE)
  1457. error = do_splice(in.file, off_in,
  1458. out.file, off_out,
  1459. len, flags);
  1460. fdput(out);
  1461. }
  1462. }
  1463. fdput(in);
  1464. }
  1465. return error;
  1466. }
  1467. /*
  1468. * Make sure there's data to read. Wait for input if we can, otherwise
  1469. * return an appropriate error.
  1470. */
  1471. static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1472. {
  1473. int ret;
  1474. /*
  1475. * Check ->nrbufs without the inode lock first. This function
  1476. * is speculative anyways, so missing one is ok.
  1477. */
  1478. if (pipe->nrbufs)
  1479. return 0;
  1480. ret = 0;
  1481. pipe_lock(pipe);
  1482. while (!pipe->nrbufs) {
  1483. if (signal_pending(current)) {
  1484. ret = -ERESTARTSYS;
  1485. break;
  1486. }
  1487. if (!pipe->writers)
  1488. break;
  1489. if (!pipe->waiting_writers) {
  1490. if (flags & SPLICE_F_NONBLOCK) {
  1491. ret = -EAGAIN;
  1492. break;
  1493. }
  1494. }
  1495. pipe_wait(pipe);
  1496. }
  1497. pipe_unlock(pipe);
  1498. return ret;
  1499. }
  1500. /*
  1501. * Make sure there's writeable room. Wait for room if we can, otherwise
  1502. * return an appropriate error.
  1503. */
  1504. static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1505. {
  1506. int ret;
  1507. /*
  1508. * Check ->nrbufs without the inode lock first. This function
  1509. * is speculative anyways, so missing one is ok.
  1510. */
  1511. if (pipe->nrbufs < pipe->buffers)
  1512. return 0;
  1513. ret = 0;
  1514. pipe_lock(pipe);
  1515. while (pipe->nrbufs >= pipe->buffers) {
  1516. if (!pipe->readers) {
  1517. send_sig(SIGPIPE, current, 0);
  1518. ret = -EPIPE;
  1519. break;
  1520. }
  1521. if (flags & SPLICE_F_NONBLOCK) {
  1522. ret = -EAGAIN;
  1523. break;
  1524. }
  1525. if (signal_pending(current)) {
  1526. ret = -ERESTARTSYS;
  1527. break;
  1528. }
  1529. pipe->waiting_writers++;
  1530. pipe_wait(pipe);
  1531. pipe->waiting_writers--;
  1532. }
  1533. pipe_unlock(pipe);
  1534. return ret;
  1535. }
  1536. /*
  1537. * Splice contents of ipipe to opipe.
  1538. */
  1539. static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  1540. struct pipe_inode_info *opipe,
  1541. size_t len, unsigned int flags)
  1542. {
  1543. struct pipe_buffer *ibuf, *obuf;
  1544. int ret = 0, nbuf;
  1545. bool input_wakeup = false;
  1546. retry:
  1547. ret = ipipe_prep(ipipe, flags);
  1548. if (ret)
  1549. return ret;
  1550. ret = opipe_prep(opipe, flags);
  1551. if (ret)
  1552. return ret;
  1553. /*
  1554. * Potential ABBA deadlock, work around it by ordering lock
  1555. * grabbing by pipe info address. Otherwise two different processes
  1556. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1557. */
  1558. pipe_double_lock(ipipe, opipe);
  1559. do {
  1560. if (!opipe->readers) {
  1561. send_sig(SIGPIPE, current, 0);
  1562. if (!ret)
  1563. ret = -EPIPE;
  1564. break;
  1565. }
  1566. if (!ipipe->nrbufs && !ipipe->writers)
  1567. break;
  1568. /*
  1569. * Cannot make any progress, because either the input
  1570. * pipe is empty or the output pipe is full.
  1571. */
  1572. if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
  1573. /* Already processed some buffers, break */
  1574. if (ret)
  1575. break;
  1576. if (flags & SPLICE_F_NONBLOCK) {
  1577. ret = -EAGAIN;
  1578. break;
  1579. }
  1580. /*
  1581. * We raced with another reader/writer and haven't
  1582. * managed to process any buffers. A zero return
  1583. * value means EOF, so retry instead.
  1584. */
  1585. pipe_unlock(ipipe);
  1586. pipe_unlock(opipe);
  1587. goto retry;
  1588. }
  1589. ibuf = ipipe->bufs + ipipe->curbuf;
  1590. nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
  1591. obuf = opipe->bufs + nbuf;
  1592. if (len >= ibuf->len) {
  1593. /*
  1594. * Simply move the whole buffer from ipipe to opipe
  1595. */
  1596. *obuf = *ibuf;
  1597. ibuf->ops = NULL;
  1598. opipe->nrbufs++;
  1599. ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
  1600. ipipe->nrbufs--;
  1601. input_wakeup = true;
  1602. } else {
  1603. /*
  1604. * Get a reference to this pipe buffer,
  1605. * so we can copy the contents over.
  1606. */
  1607. ibuf->ops->get(ipipe, ibuf);
  1608. *obuf = *ibuf;
  1609. /*
  1610. * Don't inherit the gift flag, we need to
  1611. * prevent multiple steals of this page.
  1612. */
  1613. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1614. obuf->len = len;
  1615. opipe->nrbufs++;
  1616. ibuf->offset += obuf->len;
  1617. ibuf->len -= obuf->len;
  1618. }
  1619. ret += obuf->len;
  1620. len -= obuf->len;
  1621. } while (len);
  1622. pipe_unlock(ipipe);
  1623. pipe_unlock(opipe);
  1624. /*
  1625. * If we put data in the output pipe, wakeup any potential readers.
  1626. */
  1627. if (ret > 0)
  1628. wakeup_pipe_readers(opipe);
  1629. if (input_wakeup)
  1630. wakeup_pipe_writers(ipipe);
  1631. return ret;
  1632. }
  1633. /*
  1634. * Link contents of ipipe to opipe.
  1635. */
  1636. static int link_pipe(struct pipe_inode_info *ipipe,
  1637. struct pipe_inode_info *opipe,
  1638. size_t len, unsigned int flags)
  1639. {
  1640. struct pipe_buffer *ibuf, *obuf;
  1641. int ret = 0, i = 0, nbuf;
  1642. /*
  1643. * Potential ABBA deadlock, work around it by ordering lock
  1644. * grabbing by pipe info address. Otherwise two different processes
  1645. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1646. */
  1647. pipe_double_lock(ipipe, opipe);
  1648. do {
  1649. if (!opipe->readers) {
  1650. send_sig(SIGPIPE, current, 0);
  1651. if (!ret)
  1652. ret = -EPIPE;
  1653. break;
  1654. }
  1655. /*
  1656. * If we have iterated all input buffers or ran out of
  1657. * output room, break.
  1658. */
  1659. if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
  1660. break;
  1661. ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
  1662. nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
  1663. /*
  1664. * Get a reference to this pipe buffer,
  1665. * so we can copy the contents over.
  1666. */
  1667. ibuf->ops->get(ipipe, ibuf);
  1668. obuf = opipe->bufs + nbuf;
  1669. *obuf = *ibuf;
  1670. /*
  1671. * Don't inherit the gift flag, we need to
  1672. * prevent multiple steals of this page.
  1673. */
  1674. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1675. if (obuf->len > len)
  1676. obuf->len = len;
  1677. opipe->nrbufs++;
  1678. ret += obuf->len;
  1679. len -= obuf->len;
  1680. i++;
  1681. } while (len);
  1682. /*
  1683. * return EAGAIN if we have the potential of some data in the
  1684. * future, otherwise just return 0
  1685. */
  1686. if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
  1687. ret = -EAGAIN;
  1688. pipe_unlock(ipipe);
  1689. pipe_unlock(opipe);
  1690. /*
  1691. * If we put data in the output pipe, wakeup any potential readers.
  1692. */
  1693. if (ret > 0)
  1694. wakeup_pipe_readers(opipe);
  1695. return ret;
  1696. }
  1697. /*
  1698. * This is a tee(1) implementation that works on pipes. It doesn't copy
  1699. * any data, it simply references the 'in' pages on the 'out' pipe.
  1700. * The 'flags' used are the SPLICE_F_* variants, currently the only
  1701. * applicable one is SPLICE_F_NONBLOCK.
  1702. */
  1703. static long do_tee(struct file *in, struct file *out, size_t len,
  1704. unsigned int flags)
  1705. {
  1706. struct pipe_inode_info *ipipe = get_pipe_info(in);
  1707. struct pipe_inode_info *opipe = get_pipe_info(out);
  1708. int ret = -EINVAL;
  1709. /*
  1710. * Duplicate the contents of ipipe to opipe without actually
  1711. * copying the data.
  1712. */
  1713. if (ipipe && opipe && ipipe != opipe) {
  1714. /*
  1715. * Keep going, unless we encounter an error. The ipipe/opipe
  1716. * ordering doesn't really matter.
  1717. */
  1718. ret = ipipe_prep(ipipe, flags);
  1719. if (!ret) {
  1720. ret = opipe_prep(opipe, flags);
  1721. if (!ret)
  1722. ret = link_pipe(ipipe, opipe, len, flags);
  1723. }
  1724. }
  1725. return ret;
  1726. }
  1727. SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
  1728. {
  1729. struct fd in;
  1730. int error;
  1731. if (unlikely(!len))
  1732. return 0;
  1733. error = -EBADF;
  1734. in = fdget(fdin);
  1735. if (in.file) {
  1736. if (in.file->f_mode & FMODE_READ) {
  1737. struct fd out = fdget(fdout);
  1738. if (out.file) {
  1739. if (out.file->f_mode & FMODE_WRITE)
  1740. error = do_tee(in.file, out.file,
  1741. len, flags);
  1742. fdput(out);
  1743. }
  1744. }
  1745. fdput(in);
  1746. }
  1747. return error;
  1748. }