splice.c 46 KB

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