splice.c 46 KB

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