splice.c 47 KB

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