splice.c 47 KB

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