splice.c 47 KB

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