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