splice.c 46 KB

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