splice.c 47 KB

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