splice.c 46 KB

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