splice.c 47 KB

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