splice.c 47 KB

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