splice.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  1. /*
  2. * "splice": joining two ropes together by interweaving their strands.
  3. *
  4. * This is the "extended pipe" functionality, where a pipe is used as
  5. * an arbitrary in-memory buffer. Think of a pipe as a small kernel
  6. * buffer that you can use to transfer data from one end to the other.
  7. *
  8. * The traditional unix read/write is extended with a "splice()" operation
  9. * that transfers data buffers to or from a pipe buffer.
  10. *
  11. * Named by Larry McVoy, original implementation from Linus, extended by
  12. * Jens to support splicing to files, network, direct splicing, etc and
  13. * fixing lots of bugs.
  14. *
  15. * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk>
  16. * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
  17. * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
  18. *
  19. */
  20. #include <linux/fs.h>
  21. #include <linux/file.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/splice.h>
  24. #include <linux/memcontrol.h>
  25. #include <linux/mm_inline.h>
  26. #include <linux/swap.h>
  27. #include <linux/writeback.h>
  28. #include <linux/export.h>
  29. #include <linux/syscalls.h>
  30. #include <linux/uio.h>
  31. #include <linux/security.h>
  32. #include <linux/gfp.h>
  33. #include <linux/socket.h>
  34. #include <linux/compat.h>
  35. /*
  36. * Attempt to steal a page from a pipe buffer. This should perhaps go into
  37. * a vm helper function, it's already simplified quite a bit by the
  38. * addition of remove_mapping(). If success is returned, the caller may
  39. * attempt to reuse this page for another destination.
  40. */
  41. static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
  42. struct pipe_buffer *buf)
  43. {
  44. struct page *page = buf->page;
  45. struct address_space *mapping;
  46. lock_page(page);
  47. mapping = page_mapping(page);
  48. if (mapping) {
  49. WARN_ON(!PageUptodate(page));
  50. /*
  51. * At least for ext2 with nobh option, we need to wait on
  52. * writeback completing on this page, since we'll remove it
  53. * from the pagecache. Otherwise truncate wont wait on the
  54. * page, allowing the disk blocks to be reused by someone else
  55. * before we actually wrote our data to them. fs corruption
  56. * ensues.
  57. */
  58. wait_on_page_writeback(page);
  59. if (page_has_private(page) &&
  60. !try_to_release_page(page, GFP_KERNEL))
  61. goto out_unlock;
  62. /*
  63. * If we succeeded in removing the mapping, set LRU flag
  64. * and return good.
  65. */
  66. if (remove_mapping(mapping, page)) {
  67. buf->flags |= PIPE_BUF_FLAG_LRU;
  68. return 0;
  69. }
  70. }
  71. /*
  72. * Raced with truncate or failed to remove page from current
  73. * address space, unlock and return failure.
  74. */
  75. out_unlock:
  76. unlock_page(page);
  77. return 1;
  78. }
  79. static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
  80. struct pipe_buffer *buf)
  81. {
  82. page_cache_release(buf->page);
  83. buf->flags &= ~PIPE_BUF_FLAG_LRU;
  84. }
  85. /*
  86. * Check whether the contents of buf is OK to access. Since the content
  87. * is a page cache page, IO may be in flight.
  88. */
  89. static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
  90. struct pipe_buffer *buf)
  91. {
  92. struct page *page = buf->page;
  93. int err;
  94. if (!PageUptodate(page)) {
  95. lock_page(page);
  96. /*
  97. * Page got truncated/unhashed. This will cause a 0-byte
  98. * splice, if this is the first page.
  99. */
  100. if (!page->mapping) {
  101. err = -ENODATA;
  102. goto error;
  103. }
  104. /*
  105. * Uh oh, read-error from disk.
  106. */
  107. if (!PageUptodate(page)) {
  108. err = -EIO;
  109. goto error;
  110. }
  111. /*
  112. * Page is ok afterall, we are done.
  113. */
  114. unlock_page(page);
  115. }
  116. return 0;
  117. error:
  118. unlock_page(page);
  119. return err;
  120. }
  121. const struct pipe_buf_operations page_cache_pipe_buf_ops = {
  122. .can_merge = 0,
  123. .map = generic_pipe_buf_map,
  124. .unmap = generic_pipe_buf_unmap,
  125. .confirm = page_cache_pipe_buf_confirm,
  126. .release = page_cache_pipe_buf_release,
  127. .steal = page_cache_pipe_buf_steal,
  128. .get = generic_pipe_buf_get,
  129. };
  130. static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
  131. struct pipe_buffer *buf)
  132. {
  133. if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
  134. return 1;
  135. buf->flags |= PIPE_BUF_FLAG_LRU;
  136. return generic_pipe_buf_steal(pipe, buf);
  137. }
  138. static const struct pipe_buf_operations user_page_pipe_buf_ops = {
  139. .can_merge = 0,
  140. .map = generic_pipe_buf_map,
  141. .unmap = generic_pipe_buf_unmap,
  142. .confirm = generic_pipe_buf_confirm,
  143. .release = page_cache_pipe_buf_release,
  144. .steal = user_page_pipe_buf_steal,
  145. .get = generic_pipe_buf_get,
  146. };
  147. static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
  148. {
  149. smp_mb();
  150. if (waitqueue_active(&pipe->wait))
  151. wake_up_interruptible(&pipe->wait);
  152. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  153. }
  154. /**
  155. * splice_to_pipe - fill passed data into a pipe
  156. * @pipe: pipe to fill
  157. * @spd: data to fill
  158. *
  159. * Description:
  160. * @spd contains a map of pages and len/offset tuples, along with
  161. * the struct pipe_buf_operations associated with these pages. This
  162. * function will link that data to the pipe.
  163. *
  164. */
  165. ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
  166. struct splice_pipe_desc *spd)
  167. {
  168. unsigned int spd_pages = spd->nr_pages;
  169. int ret, do_wakeup, page_nr;
  170. ret = 0;
  171. do_wakeup = 0;
  172. page_nr = 0;
  173. pipe_lock(pipe);
  174. for (;;) {
  175. if (!pipe->readers) {
  176. send_sig(SIGPIPE, current, 0);
  177. if (!ret)
  178. ret = -EPIPE;
  179. break;
  180. }
  181. if (pipe->nrbufs < pipe->buffers) {
  182. int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
  183. struct pipe_buffer *buf = pipe->bufs + newbuf;
  184. buf->page = spd->pages[page_nr];
  185. buf->offset = spd->partial[page_nr].offset;
  186. buf->len = spd->partial[page_nr].len;
  187. buf->private = spd->partial[page_nr].private;
  188. buf->ops = spd->ops;
  189. if (spd->flags & SPLICE_F_GIFT)
  190. buf->flags |= PIPE_BUF_FLAG_GIFT;
  191. pipe->nrbufs++;
  192. page_nr++;
  193. ret += buf->len;
  194. if (pipe->inode)
  195. do_wakeup = 1;
  196. if (!--spd->nr_pages)
  197. break;
  198. if (pipe->nrbufs < pipe->buffers)
  199. continue;
  200. break;
  201. }
  202. if (spd->flags & SPLICE_F_NONBLOCK) {
  203. if (!ret)
  204. ret = -EAGAIN;
  205. break;
  206. }
  207. if (signal_pending(current)) {
  208. if (!ret)
  209. ret = -ERESTARTSYS;
  210. break;
  211. }
  212. if (do_wakeup) {
  213. smp_mb();
  214. if (waitqueue_active(&pipe->wait))
  215. wake_up_interruptible_sync(&pipe->wait);
  216. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  217. do_wakeup = 0;
  218. }
  219. pipe->waiting_writers++;
  220. pipe_wait(pipe);
  221. pipe->waiting_writers--;
  222. }
  223. pipe_unlock(pipe);
  224. if (do_wakeup)
  225. wakeup_pipe_readers(pipe);
  226. while (page_nr < spd_pages)
  227. spd->spd_release(spd, page_nr++);
  228. return ret;
  229. }
  230. void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
  231. {
  232. page_cache_release(spd->pages[i]);
  233. }
  234. /*
  235. * Check if we need to grow the arrays holding pages and partial page
  236. * descriptions.
  237. */
  238. int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
  239. {
  240. unsigned int buffers = ACCESS_ONCE(pipe->buffers);
  241. spd->nr_pages_max = buffers;
  242. if (buffers <= PIPE_DEF_BUFFERS)
  243. return 0;
  244. spd->pages = kmalloc(buffers * sizeof(struct page *), GFP_KERNEL);
  245. spd->partial = kmalloc(buffers * sizeof(struct partial_page), GFP_KERNEL);
  246. if (spd->pages && spd->partial)
  247. return 0;
  248. kfree(spd->pages);
  249. kfree(spd->partial);
  250. return -ENOMEM;
  251. }
  252. void splice_shrink_spd(struct splice_pipe_desc *spd)
  253. {
  254. if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
  255. return;
  256. kfree(spd->pages);
  257. kfree(spd->partial);
  258. }
  259. static int
  260. __generic_file_splice_read(struct file *in, loff_t *ppos,
  261. struct pipe_inode_info *pipe, size_t len,
  262. unsigned int flags)
  263. {
  264. struct address_space *mapping = in->f_mapping;
  265. unsigned int loff, nr_pages, req_pages;
  266. struct page *pages[PIPE_DEF_BUFFERS];
  267. struct partial_page partial[PIPE_DEF_BUFFERS];
  268. struct page *page;
  269. pgoff_t index, end_index;
  270. loff_t isize;
  271. int error, page_nr;
  272. struct splice_pipe_desc spd = {
  273. .pages = pages,
  274. .partial = partial,
  275. .nr_pages_max = PIPE_DEF_BUFFERS,
  276. .flags = flags,
  277. .ops = &page_cache_pipe_buf_ops,
  278. .spd_release = spd_release_page,
  279. };
  280. if (splice_grow_spd(pipe, &spd))
  281. return -ENOMEM;
  282. index = *ppos >> PAGE_CACHE_SHIFT;
  283. loff = *ppos & ~PAGE_CACHE_MASK;
  284. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  285. nr_pages = min(req_pages, spd.nr_pages_max);
  286. /*
  287. * Lookup the (hopefully) full range of pages we need.
  288. */
  289. spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, spd.pages);
  290. index += spd.nr_pages;
  291. /*
  292. * If find_get_pages_contig() returned fewer pages than we needed,
  293. * readahead/allocate the rest and fill in the holes.
  294. */
  295. if (spd.nr_pages < nr_pages)
  296. page_cache_sync_readahead(mapping, &in->f_ra, in,
  297. index, req_pages - spd.nr_pages);
  298. error = 0;
  299. while (spd.nr_pages < nr_pages) {
  300. /*
  301. * Page could be there, find_get_pages_contig() breaks on
  302. * the first hole.
  303. */
  304. page = find_get_page(mapping, index);
  305. if (!page) {
  306. /*
  307. * page didn't exist, allocate one.
  308. */
  309. page = page_cache_alloc_cold(mapping);
  310. if (!page)
  311. break;
  312. error = add_to_page_cache_lru(page, mapping, index,
  313. GFP_KERNEL);
  314. if (unlikely(error)) {
  315. page_cache_release(page);
  316. if (error == -EEXIST)
  317. continue;
  318. break;
  319. }
  320. /*
  321. * add_to_page_cache() locks the page, unlock it
  322. * to avoid convoluting the logic below even more.
  323. */
  324. unlock_page(page);
  325. }
  326. spd.pages[spd.nr_pages++] = page;
  327. index++;
  328. }
  329. /*
  330. * Now loop over the map and see if we need to start IO on any
  331. * pages, fill in the partial map, etc.
  332. */
  333. index = *ppos >> PAGE_CACHE_SHIFT;
  334. nr_pages = spd.nr_pages;
  335. spd.nr_pages = 0;
  336. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  337. unsigned int this_len;
  338. if (!len)
  339. break;
  340. /*
  341. * this_len is the max we'll use from this page
  342. */
  343. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  344. page = spd.pages[page_nr];
  345. if (PageReadahead(page))
  346. page_cache_async_readahead(mapping, &in->f_ra, in,
  347. page, index, req_pages - page_nr);
  348. /*
  349. * If the page isn't uptodate, we may need to start io on it
  350. */
  351. if (!PageUptodate(page)) {
  352. lock_page(page);
  353. /*
  354. * Page was truncated, or invalidated by the
  355. * filesystem. Redo the find/create, but this time the
  356. * page is kept locked, so there's no chance of another
  357. * race with truncate/invalidate.
  358. */
  359. if (!page->mapping) {
  360. unlock_page(page);
  361. page = find_or_create_page(mapping, index,
  362. mapping_gfp_mask(mapping));
  363. if (!page) {
  364. error = -ENOMEM;
  365. break;
  366. }
  367. page_cache_release(spd.pages[page_nr]);
  368. spd.pages[page_nr] = page;
  369. }
  370. /*
  371. * page was already under io and is now done, great
  372. */
  373. if (PageUptodate(page)) {
  374. unlock_page(page);
  375. goto fill_it;
  376. }
  377. /*
  378. * need to read in the page
  379. */
  380. error = mapping->a_ops->readpage(in, page);
  381. if (unlikely(error)) {
  382. /*
  383. * We really should re-lookup the page here,
  384. * but it complicates things a lot. Instead
  385. * lets just do what we already stored, and
  386. * we'll get it the next time we are called.
  387. */
  388. if (error == AOP_TRUNCATED_PAGE)
  389. error = 0;
  390. break;
  391. }
  392. }
  393. fill_it:
  394. /*
  395. * i_size must be checked after PageUptodate.
  396. */
  397. isize = i_size_read(mapping->host);
  398. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  399. if (unlikely(!isize || index > end_index))
  400. break;
  401. /*
  402. * if this is the last page, see if we need to shrink
  403. * the length and stop
  404. */
  405. if (end_index == index) {
  406. unsigned int plen;
  407. /*
  408. * max good bytes in this page
  409. */
  410. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  411. if (plen <= loff)
  412. break;
  413. /*
  414. * force quit after adding this page
  415. */
  416. this_len = min(this_len, plen - loff);
  417. len = this_len;
  418. }
  419. spd.partial[page_nr].offset = loff;
  420. spd.partial[page_nr].len = this_len;
  421. len -= this_len;
  422. loff = 0;
  423. spd.nr_pages++;
  424. index++;
  425. }
  426. /*
  427. * Release any pages at the end, if we quit early. 'page_nr' is how far
  428. * we got, 'nr_pages' is how many pages are in the map.
  429. */
  430. while (page_nr < nr_pages)
  431. page_cache_release(spd.pages[page_nr++]);
  432. in->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT;
  433. if (spd.nr_pages)
  434. error = splice_to_pipe(pipe, &spd);
  435. splice_shrink_spd(&spd);
  436. return error;
  437. }
  438. /**
  439. * generic_file_splice_read - splice data from file to a pipe
  440. * @in: file to splice from
  441. * @ppos: position in @in
  442. * @pipe: pipe to splice to
  443. * @len: number of bytes to splice
  444. * @flags: splice modifier flags
  445. *
  446. * Description:
  447. * Will read pages from given file and fill them into a pipe. Can be
  448. * used as long as the address_space operations for the source implements
  449. * a readpage() hook.
  450. *
  451. */
  452. ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
  453. struct pipe_inode_info *pipe, size_t len,
  454. unsigned int flags)
  455. {
  456. loff_t isize, left;
  457. int ret;
  458. isize = i_size_read(in->f_mapping->host);
  459. if (unlikely(*ppos >= isize))
  460. return 0;
  461. left = isize - *ppos;
  462. if (unlikely(left < len))
  463. len = left;
  464. ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
  465. if (ret > 0) {
  466. *ppos += ret;
  467. file_accessed(in);
  468. }
  469. return ret;
  470. }
  471. EXPORT_SYMBOL(generic_file_splice_read);
  472. static const struct pipe_buf_operations default_pipe_buf_ops = {
  473. .can_merge = 0,
  474. .map = generic_pipe_buf_map,
  475. .unmap = generic_pipe_buf_unmap,
  476. .confirm = generic_pipe_buf_confirm,
  477. .release = generic_pipe_buf_release,
  478. .steal = generic_pipe_buf_steal,
  479. .get = generic_pipe_buf_get,
  480. };
  481. static ssize_t kernel_readv(struct file *file, const struct iovec *vec,
  482. unsigned long vlen, loff_t offset)
  483. {
  484. mm_segment_t old_fs;
  485. loff_t pos = offset;
  486. ssize_t res;
  487. old_fs = get_fs();
  488. set_fs(get_ds());
  489. /* The cast to a user pointer is valid due to the set_fs() */
  490. res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos);
  491. set_fs(old_fs);
  492. return res;
  493. }
  494. ssize_t kernel_write(struct file *file, const char *buf, size_t count,
  495. loff_t pos)
  496. {
  497. mm_segment_t old_fs;
  498. ssize_t res;
  499. old_fs = get_fs();
  500. set_fs(get_ds());
  501. /* The cast to a user pointer is valid due to the set_fs() */
  502. res = vfs_write(file, (__force const char __user *)buf, count, &pos);
  503. set_fs(old_fs);
  504. return res;
  505. }
  506. EXPORT_SYMBOL(kernel_write);
  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. while (!pipe->nrbufs) {
  738. if (!pipe->writers)
  739. return 0;
  740. if (!pipe->waiting_writers && sd->num_spliced)
  741. return 0;
  742. if (sd->flags & SPLICE_F_NONBLOCK)
  743. return -EAGAIN;
  744. if (signal_pending(current))
  745. return -ERESTARTSYS;
  746. if (sd->need_wakeup) {
  747. wakeup_pipe_writers(pipe);
  748. sd->need_wakeup = false;
  749. }
  750. pipe_wait(pipe);
  751. }
  752. return 1;
  753. }
  754. EXPORT_SYMBOL(splice_from_pipe_next);
  755. /**
  756. * splice_from_pipe_begin - start splicing from pipe
  757. * @sd: information about the splice operation
  758. *
  759. * Description:
  760. * This function should be called before a loop containing
  761. * splice_from_pipe_next() and splice_from_pipe_feed() to
  762. * initialize the necessary fields of @sd.
  763. */
  764. void splice_from_pipe_begin(struct splice_desc *sd)
  765. {
  766. sd->num_spliced = 0;
  767. sd->need_wakeup = false;
  768. }
  769. EXPORT_SYMBOL(splice_from_pipe_begin);
  770. /**
  771. * splice_from_pipe_end - finish splicing from pipe
  772. * @pipe: pipe to splice from
  773. * @sd: information about the splice operation
  774. *
  775. * Description:
  776. * This function will wake up pipe writers if necessary. It should
  777. * be called after a loop containing splice_from_pipe_next() and
  778. * splice_from_pipe_feed().
  779. */
  780. void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
  781. {
  782. if (sd->need_wakeup)
  783. wakeup_pipe_writers(pipe);
  784. }
  785. EXPORT_SYMBOL(splice_from_pipe_end);
  786. /**
  787. * __splice_from_pipe - splice data from a pipe to given actor
  788. * @pipe: pipe to splice from
  789. * @sd: information to @actor
  790. * @actor: handler that splices the data
  791. *
  792. * Description:
  793. * This function does little more than loop over the pipe and call
  794. * @actor to do the actual moving of a single struct pipe_buffer to
  795. * the desired destination. See pipe_to_file, pipe_to_sendpage, or
  796. * pipe_to_user.
  797. *
  798. */
  799. ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
  800. splice_actor *actor)
  801. {
  802. int ret;
  803. splice_from_pipe_begin(sd);
  804. do {
  805. ret = splice_from_pipe_next(pipe, sd);
  806. if (ret > 0)
  807. ret = splice_from_pipe_feed(pipe, sd, actor);
  808. } while (ret > 0);
  809. splice_from_pipe_end(pipe, sd);
  810. return sd->num_spliced ? sd->num_spliced : ret;
  811. }
  812. EXPORT_SYMBOL(__splice_from_pipe);
  813. /**
  814. * splice_from_pipe - splice data from a pipe to a file
  815. * @pipe: pipe to splice from
  816. * @out: file to splice to
  817. * @ppos: position in @out
  818. * @len: how many bytes to splice
  819. * @flags: splice modifier flags
  820. * @actor: handler that splices the data
  821. *
  822. * Description:
  823. * See __splice_from_pipe. This function locks the pipe inode,
  824. * otherwise it's identical to __splice_from_pipe().
  825. *
  826. */
  827. ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
  828. loff_t *ppos, size_t len, unsigned int flags,
  829. splice_actor *actor)
  830. {
  831. ssize_t ret;
  832. struct splice_desc sd = {
  833. .total_len = len,
  834. .flags = flags,
  835. .pos = *ppos,
  836. .u.file = out,
  837. };
  838. pipe_lock(pipe);
  839. ret = __splice_from_pipe(pipe, &sd, actor);
  840. pipe_unlock(pipe);
  841. return ret;
  842. }
  843. /**
  844. * generic_file_splice_write - splice data from a pipe to a file
  845. * @pipe: pipe info
  846. * @out: file to write to
  847. * @ppos: position in @out
  848. * @len: number of bytes to splice
  849. * @flags: splice modifier flags
  850. *
  851. * Description:
  852. * Will either move or copy pages (determined by @flags options) from
  853. * the given pipe inode to the given file.
  854. *
  855. */
  856. ssize_t
  857. generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  858. loff_t *ppos, size_t len, unsigned int flags)
  859. {
  860. struct address_space *mapping = out->f_mapping;
  861. struct inode *inode = mapping->host;
  862. struct splice_desc sd = {
  863. .total_len = len,
  864. .flags = flags,
  865. .pos = *ppos,
  866. .u.file = out,
  867. };
  868. ssize_t ret;
  869. sb_start_write(inode->i_sb);
  870. pipe_lock(pipe);
  871. splice_from_pipe_begin(&sd);
  872. do {
  873. ret = splice_from_pipe_next(pipe, &sd);
  874. if (ret <= 0)
  875. break;
  876. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  877. ret = file_remove_suid(out);
  878. if (!ret) {
  879. ret = file_update_time(out);
  880. if (!ret)
  881. ret = splice_from_pipe_feed(pipe, &sd,
  882. pipe_to_file);
  883. }
  884. mutex_unlock(&inode->i_mutex);
  885. } while (ret > 0);
  886. splice_from_pipe_end(pipe, &sd);
  887. pipe_unlock(pipe);
  888. if (sd.num_spliced)
  889. ret = sd.num_spliced;
  890. if (ret > 0) {
  891. int err;
  892. err = generic_write_sync(out, *ppos, ret);
  893. if (err)
  894. ret = err;
  895. else
  896. *ppos += ret;
  897. balance_dirty_pages_ratelimited(mapping);
  898. }
  899. sb_end_write(inode->i_sb);
  900. return ret;
  901. }
  902. EXPORT_SYMBOL(generic_file_splice_write);
  903. static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  904. struct splice_desc *sd)
  905. {
  906. int ret;
  907. void *data;
  908. data = buf->ops->map(pipe, buf, 0);
  909. ret = kernel_write(sd->u.file, data + buf->offset, sd->len, sd->pos);
  910. buf->ops->unmap(pipe, buf, data);
  911. return ret;
  912. }
  913. static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
  914. struct file *out, loff_t *ppos,
  915. size_t len, unsigned int flags)
  916. {
  917. ssize_t ret;
  918. ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
  919. if (ret > 0)
  920. *ppos += ret;
  921. return ret;
  922. }
  923. /**
  924. * generic_splice_sendpage - splice data from a pipe to a socket
  925. * @pipe: pipe to splice from
  926. * @out: socket to write to
  927. * @ppos: position in @out
  928. * @len: number of bytes to splice
  929. * @flags: splice modifier flags
  930. *
  931. * Description:
  932. * Will send @len bytes from the pipe to a network socket. No data copying
  933. * is involved.
  934. *
  935. */
  936. ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
  937. loff_t *ppos, size_t len, unsigned int flags)
  938. {
  939. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
  940. }
  941. EXPORT_SYMBOL(generic_splice_sendpage);
  942. /*
  943. * Attempt to initiate a splice from pipe to file.
  944. */
  945. static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  946. loff_t *ppos, size_t len, unsigned int flags)
  947. {
  948. ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
  949. loff_t *, size_t, unsigned int);
  950. int ret;
  951. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  952. return -EBADF;
  953. if (unlikely(out->f_flags & O_APPEND))
  954. return -EINVAL;
  955. ret = rw_verify_area(WRITE, out, ppos, len);
  956. if (unlikely(ret < 0))
  957. return ret;
  958. if (out->f_op && out->f_op->splice_write)
  959. splice_write = out->f_op->splice_write;
  960. else
  961. splice_write = default_file_splice_write;
  962. return splice_write(pipe, out, ppos, len, flags);
  963. }
  964. /*
  965. * Attempt to initiate a splice from a file to a pipe.
  966. */
  967. static long do_splice_to(struct file *in, loff_t *ppos,
  968. struct pipe_inode_info *pipe, size_t len,
  969. unsigned int flags)
  970. {
  971. ssize_t (*splice_read)(struct file *, loff_t *,
  972. struct pipe_inode_info *, size_t, unsigned int);
  973. int ret;
  974. if (unlikely(!(in->f_mode & FMODE_READ)))
  975. return -EBADF;
  976. ret = rw_verify_area(READ, in, ppos, len);
  977. if (unlikely(ret < 0))
  978. return ret;
  979. if (in->f_op && in->f_op->splice_read)
  980. splice_read = in->f_op->splice_read;
  981. else
  982. splice_read = default_file_splice_read;
  983. return splice_read(in, ppos, pipe, len, flags);
  984. }
  985. /**
  986. * splice_direct_to_actor - splices data directly between two non-pipes
  987. * @in: file to splice from
  988. * @sd: actor information on where to splice to
  989. * @actor: handles the data splicing
  990. *
  991. * Description:
  992. * This is a special case helper to splice directly between two
  993. * points, without requiring an explicit pipe. Internally an allocated
  994. * pipe is cached in the process, and reused during the lifetime of
  995. * that process.
  996. *
  997. */
  998. ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
  999. splice_direct_actor *actor)
  1000. {
  1001. struct pipe_inode_info *pipe;
  1002. long ret, bytes;
  1003. umode_t i_mode;
  1004. size_t len;
  1005. int i, flags;
  1006. /*
  1007. * We require the input being a regular file, as we don't want to
  1008. * randomly drop data for eg socket -> socket splicing. Use the
  1009. * piped splicing for that!
  1010. */
  1011. i_mode = file_inode(in)->i_mode;
  1012. if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  1013. return -EINVAL;
  1014. /*
  1015. * neither in nor out is a pipe, setup an internal pipe attached to
  1016. * 'out' and transfer the wanted data from 'in' to 'out' through that
  1017. */
  1018. pipe = current->splice_pipe;
  1019. if (unlikely(!pipe)) {
  1020. pipe = alloc_pipe_info(NULL);
  1021. if (!pipe)
  1022. return -ENOMEM;
  1023. /*
  1024. * We don't have an immediate reader, but we'll read the stuff
  1025. * out of the pipe right after the splice_to_pipe(). So set
  1026. * PIPE_READERS appropriately.
  1027. */
  1028. pipe->readers = 1;
  1029. current->splice_pipe = pipe;
  1030. }
  1031. /*
  1032. * Do the splice.
  1033. */
  1034. ret = 0;
  1035. bytes = 0;
  1036. len = sd->total_len;
  1037. flags = sd->flags;
  1038. /*
  1039. * Don't block on output, we have to drain the direct pipe.
  1040. */
  1041. sd->flags &= ~SPLICE_F_NONBLOCK;
  1042. while (len) {
  1043. size_t read_len;
  1044. loff_t pos = sd->pos, prev_pos = pos;
  1045. ret = do_splice_to(in, &pos, pipe, len, flags);
  1046. if (unlikely(ret <= 0))
  1047. goto out_release;
  1048. read_len = ret;
  1049. sd->total_len = read_len;
  1050. /*
  1051. * NOTE: nonblocking mode only applies to the input. We
  1052. * must not do the output in nonblocking mode as then we
  1053. * could get stuck data in the internal pipe:
  1054. */
  1055. ret = actor(pipe, sd);
  1056. if (unlikely(ret <= 0)) {
  1057. sd->pos = prev_pos;
  1058. goto out_release;
  1059. }
  1060. bytes += ret;
  1061. len -= ret;
  1062. sd->pos = pos;
  1063. if (ret < read_len) {
  1064. sd->pos = prev_pos + ret;
  1065. goto out_release;
  1066. }
  1067. }
  1068. done:
  1069. pipe->nrbufs = pipe->curbuf = 0;
  1070. file_accessed(in);
  1071. return bytes;
  1072. out_release:
  1073. /*
  1074. * If we did an incomplete transfer we must release
  1075. * the pipe buffers in question:
  1076. */
  1077. for (i = 0; i < pipe->buffers; i++) {
  1078. struct pipe_buffer *buf = pipe->bufs + i;
  1079. if (buf->ops) {
  1080. buf->ops->release(pipe, buf);
  1081. buf->ops = NULL;
  1082. }
  1083. }
  1084. if (!bytes)
  1085. bytes = ret;
  1086. goto done;
  1087. }
  1088. EXPORT_SYMBOL(splice_direct_to_actor);
  1089. static int direct_splice_actor(struct pipe_inode_info *pipe,
  1090. struct splice_desc *sd)
  1091. {
  1092. struct file *file = sd->u.file;
  1093. return do_splice_from(pipe, file, &file->f_pos, sd->total_len,
  1094. sd->flags);
  1095. }
  1096. /**
  1097. * do_splice_direct - splices data directly between two files
  1098. * @in: file to splice from
  1099. * @ppos: input file offset
  1100. * @out: file to splice to
  1101. * @len: number of bytes to splice
  1102. * @flags: splice modifier flags
  1103. *
  1104. * Description:
  1105. * For use by do_sendfile(). splice can easily emulate sendfile, but
  1106. * doing it in the application would incur an extra system call
  1107. * (splice in + splice out, as compared to just sendfile()). So this helper
  1108. * can splice directly through a process-private pipe.
  1109. *
  1110. */
  1111. long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
  1112. size_t len, unsigned int flags)
  1113. {
  1114. struct splice_desc sd = {
  1115. .len = len,
  1116. .total_len = len,
  1117. .flags = flags,
  1118. .pos = *ppos,
  1119. .u.file = out,
  1120. };
  1121. long ret;
  1122. ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
  1123. if (ret > 0)
  1124. *ppos = sd.pos;
  1125. return ret;
  1126. }
  1127. static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  1128. struct pipe_inode_info *opipe,
  1129. size_t len, unsigned int flags);
  1130. /*
  1131. * Determine where to splice to/from.
  1132. */
  1133. static long do_splice(struct file *in, loff_t __user *off_in,
  1134. struct file *out, loff_t __user *off_out,
  1135. size_t len, unsigned int flags)
  1136. {
  1137. struct pipe_inode_info *ipipe;
  1138. struct pipe_inode_info *opipe;
  1139. loff_t offset, *off;
  1140. long ret;
  1141. ipipe = get_pipe_info(in);
  1142. opipe = get_pipe_info(out);
  1143. if (ipipe && opipe) {
  1144. if (off_in || off_out)
  1145. return -ESPIPE;
  1146. if (!(in->f_mode & FMODE_READ))
  1147. return -EBADF;
  1148. if (!(out->f_mode & FMODE_WRITE))
  1149. return -EBADF;
  1150. /* Splicing to self would be fun, but... */
  1151. if (ipipe == opipe)
  1152. return -EINVAL;
  1153. return splice_pipe_to_pipe(ipipe, opipe, len, flags);
  1154. }
  1155. if (ipipe) {
  1156. if (off_in)
  1157. return -ESPIPE;
  1158. if (off_out) {
  1159. if (!(out->f_mode & FMODE_PWRITE))
  1160. return -EINVAL;
  1161. if (copy_from_user(&offset, off_out, sizeof(loff_t)))
  1162. return -EFAULT;
  1163. off = &offset;
  1164. } else
  1165. off = &out->f_pos;
  1166. ret = do_splice_from(ipipe, out, off, len, flags);
  1167. if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
  1168. ret = -EFAULT;
  1169. return ret;
  1170. }
  1171. if (opipe) {
  1172. if (off_out)
  1173. return -ESPIPE;
  1174. if (off_in) {
  1175. if (!(in->f_mode & FMODE_PREAD))
  1176. return -EINVAL;
  1177. if (copy_from_user(&offset, off_in, sizeof(loff_t)))
  1178. return -EFAULT;
  1179. off = &offset;
  1180. } else
  1181. off = &in->f_pos;
  1182. ret = do_splice_to(in, off, opipe, len, flags);
  1183. if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
  1184. ret = -EFAULT;
  1185. return ret;
  1186. }
  1187. return -EINVAL;
  1188. }
  1189. /*
  1190. * Map an iov into an array of pages and offset/length tupples. With the
  1191. * partial_page structure, we can map several non-contiguous ranges into
  1192. * our ones pages[] map instead of splitting that operation into pieces.
  1193. * Could easily be exported as a generic helper for other users, in which
  1194. * case one would probably want to add a 'max_nr_pages' parameter as well.
  1195. */
  1196. static int get_iovec_page_array(const struct iovec __user *iov,
  1197. unsigned int nr_vecs, struct page **pages,
  1198. struct partial_page *partial, bool aligned,
  1199. unsigned int pipe_buffers)
  1200. {
  1201. int buffers = 0, error = 0;
  1202. while (nr_vecs) {
  1203. unsigned long off, npages;
  1204. struct iovec entry;
  1205. void __user *base;
  1206. size_t len;
  1207. int i;
  1208. error = -EFAULT;
  1209. if (copy_from_user(&entry, iov, sizeof(entry)))
  1210. break;
  1211. base = entry.iov_base;
  1212. len = entry.iov_len;
  1213. /*
  1214. * Sanity check this iovec. 0 read succeeds.
  1215. */
  1216. error = 0;
  1217. if (unlikely(!len))
  1218. break;
  1219. error = -EFAULT;
  1220. if (!access_ok(VERIFY_READ, base, len))
  1221. break;
  1222. /*
  1223. * Get this base offset and number of pages, then map
  1224. * in the user pages.
  1225. */
  1226. off = (unsigned long) base & ~PAGE_MASK;
  1227. /*
  1228. * If asked for alignment, the offset must be zero and the
  1229. * length a multiple of the PAGE_SIZE.
  1230. */
  1231. error = -EINVAL;
  1232. if (aligned && (off || len & ~PAGE_MASK))
  1233. break;
  1234. npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1235. if (npages > pipe_buffers - buffers)
  1236. npages = pipe_buffers - buffers;
  1237. error = get_user_pages_fast((unsigned long)base, npages,
  1238. 0, &pages[buffers]);
  1239. if (unlikely(error <= 0))
  1240. break;
  1241. /*
  1242. * Fill this contiguous range into the partial page map.
  1243. */
  1244. for (i = 0; i < error; i++) {
  1245. const int plen = min_t(size_t, len, PAGE_SIZE - off);
  1246. partial[buffers].offset = off;
  1247. partial[buffers].len = plen;
  1248. off = 0;
  1249. len -= plen;
  1250. buffers++;
  1251. }
  1252. /*
  1253. * We didn't complete this iov, stop here since it probably
  1254. * means we have to move some of this into a pipe to
  1255. * be able to continue.
  1256. */
  1257. if (len)
  1258. break;
  1259. /*
  1260. * Don't continue if we mapped fewer pages than we asked for,
  1261. * or if we mapped the max number of pages that we have
  1262. * room for.
  1263. */
  1264. if (error < npages || buffers == pipe_buffers)
  1265. break;
  1266. nr_vecs--;
  1267. iov++;
  1268. }
  1269. if (buffers)
  1270. return buffers;
  1271. return error;
  1272. }
  1273. static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  1274. struct splice_desc *sd)
  1275. {
  1276. char *src;
  1277. int ret;
  1278. /*
  1279. * See if we can use the atomic maps, by prefaulting in the
  1280. * pages and doing an atomic copy
  1281. */
  1282. if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) {
  1283. src = buf->ops->map(pipe, buf, 1);
  1284. ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset,
  1285. sd->len);
  1286. buf->ops->unmap(pipe, buf, src);
  1287. if (!ret) {
  1288. ret = sd->len;
  1289. goto out;
  1290. }
  1291. }
  1292. /*
  1293. * No dice, use slow non-atomic map and copy
  1294. */
  1295. src = buf->ops->map(pipe, buf, 0);
  1296. ret = sd->len;
  1297. if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len))
  1298. ret = -EFAULT;
  1299. buf->ops->unmap(pipe, buf, src);
  1300. out:
  1301. if (ret > 0)
  1302. sd->u.userptr += ret;
  1303. return ret;
  1304. }
  1305. /*
  1306. * For lack of a better implementation, implement vmsplice() to userspace
  1307. * as a simple copy of the pipes pages to the user iov.
  1308. */
  1309. static long vmsplice_to_user(struct file *file, const struct iovec __user *iov,
  1310. unsigned long nr_segs, unsigned int flags)
  1311. {
  1312. struct pipe_inode_info *pipe;
  1313. struct splice_desc sd;
  1314. ssize_t size;
  1315. int error;
  1316. long ret;
  1317. pipe = get_pipe_info(file);
  1318. if (!pipe)
  1319. return -EBADF;
  1320. pipe_lock(pipe);
  1321. error = ret = 0;
  1322. while (nr_segs) {
  1323. void __user *base;
  1324. size_t len;
  1325. /*
  1326. * Get user address base and length for this iovec.
  1327. */
  1328. error = get_user(base, &iov->iov_base);
  1329. if (unlikely(error))
  1330. break;
  1331. error = get_user(len, &iov->iov_len);
  1332. if (unlikely(error))
  1333. break;
  1334. /*
  1335. * Sanity check this iovec. 0 read succeeds.
  1336. */
  1337. if (unlikely(!len))
  1338. break;
  1339. if (unlikely(!base)) {
  1340. error = -EFAULT;
  1341. break;
  1342. }
  1343. if (unlikely(!access_ok(VERIFY_WRITE, base, len))) {
  1344. error = -EFAULT;
  1345. break;
  1346. }
  1347. sd.len = 0;
  1348. sd.total_len = len;
  1349. sd.flags = flags;
  1350. sd.u.userptr = base;
  1351. sd.pos = 0;
  1352. size = __splice_from_pipe(pipe, &sd, pipe_to_user);
  1353. if (size < 0) {
  1354. if (!ret)
  1355. ret = size;
  1356. break;
  1357. }
  1358. ret += size;
  1359. if (size < len)
  1360. break;
  1361. nr_segs--;
  1362. iov++;
  1363. }
  1364. pipe_unlock(pipe);
  1365. if (!ret)
  1366. ret = error;
  1367. return ret;
  1368. }
  1369. /*
  1370. * vmsplice splices a user address range into a pipe. It can be thought of
  1371. * as splice-from-memory, where the regular splice is splice-from-file (or
  1372. * to file). In both cases the output is a pipe, naturally.
  1373. */
  1374. static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
  1375. unsigned long nr_segs, unsigned int flags)
  1376. {
  1377. struct pipe_inode_info *pipe;
  1378. struct page *pages[PIPE_DEF_BUFFERS];
  1379. struct partial_page partial[PIPE_DEF_BUFFERS];
  1380. struct splice_pipe_desc spd = {
  1381. .pages = pages,
  1382. .partial = partial,
  1383. .nr_pages_max = PIPE_DEF_BUFFERS,
  1384. .flags = flags,
  1385. .ops = &user_page_pipe_buf_ops,
  1386. .spd_release = spd_release_page,
  1387. };
  1388. long ret;
  1389. pipe = get_pipe_info(file);
  1390. if (!pipe)
  1391. return -EBADF;
  1392. if (splice_grow_spd(pipe, &spd))
  1393. return -ENOMEM;
  1394. spd.nr_pages = get_iovec_page_array(iov, nr_segs, spd.pages,
  1395. spd.partial, false,
  1396. spd.nr_pages_max);
  1397. if (spd.nr_pages <= 0)
  1398. ret = spd.nr_pages;
  1399. else
  1400. ret = splice_to_pipe(pipe, &spd);
  1401. splice_shrink_spd(&spd);
  1402. return ret;
  1403. }
  1404. /*
  1405. * Note that vmsplice only really supports true splicing _from_ user memory
  1406. * to a pipe, not the other way around. Splicing from user memory is a simple
  1407. * operation that can be supported without any funky alignment restrictions
  1408. * or nasty vm tricks. We simply map in the user memory and fill them into
  1409. * a pipe. The reverse isn't quite as easy, though. There are two possible
  1410. * solutions for that:
  1411. *
  1412. * - memcpy() the data internally, at which point we might as well just
  1413. * do a regular read() on the buffer anyway.
  1414. * - Lots of nasty vm tricks, that are neither fast nor flexible (it
  1415. * has restriction limitations on both ends of the pipe).
  1416. *
  1417. * Currently we punt and implement it as a normal copy, see pipe_to_user().
  1418. *
  1419. */
  1420. SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
  1421. unsigned long, nr_segs, unsigned int, flags)
  1422. {
  1423. struct fd f;
  1424. long error;
  1425. if (unlikely(nr_segs > UIO_MAXIOV))
  1426. return -EINVAL;
  1427. else if (unlikely(!nr_segs))
  1428. return 0;
  1429. error = -EBADF;
  1430. f = fdget(fd);
  1431. if (f.file) {
  1432. if (f.file->f_mode & FMODE_WRITE)
  1433. error = vmsplice_to_pipe(f.file, iov, nr_segs, flags);
  1434. else if (f.file->f_mode & FMODE_READ)
  1435. error = vmsplice_to_user(f.file, iov, nr_segs, flags);
  1436. fdput(f);
  1437. }
  1438. return error;
  1439. }
  1440. #ifdef CONFIG_COMPAT
  1441. COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
  1442. unsigned int, nr_segs, unsigned int, flags)
  1443. {
  1444. unsigned i;
  1445. struct iovec __user *iov;
  1446. if (nr_segs > UIO_MAXIOV)
  1447. return -EINVAL;
  1448. iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
  1449. for (i = 0; i < nr_segs; i++) {
  1450. struct compat_iovec v;
  1451. if (get_user(v.iov_base, &iov32[i].iov_base) ||
  1452. get_user(v.iov_len, &iov32[i].iov_len) ||
  1453. put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
  1454. put_user(v.iov_len, &iov[i].iov_len))
  1455. return -EFAULT;
  1456. }
  1457. return sys_vmsplice(fd, iov, nr_segs, flags);
  1458. }
  1459. #endif
  1460. SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
  1461. int, fd_out, loff_t __user *, off_out,
  1462. size_t, len, unsigned int, flags)
  1463. {
  1464. struct fd in, out;
  1465. long error;
  1466. if (unlikely(!len))
  1467. return 0;
  1468. error = -EBADF;
  1469. in = fdget(fd_in);
  1470. if (in.file) {
  1471. if (in.file->f_mode & FMODE_READ) {
  1472. out = fdget(fd_out);
  1473. if (out.file) {
  1474. if (out.file->f_mode & FMODE_WRITE)
  1475. error = do_splice(in.file, off_in,
  1476. out.file, off_out,
  1477. len, flags);
  1478. fdput(out);
  1479. }
  1480. }
  1481. fdput(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. obuf->len = len;
  1633. opipe->nrbufs++;
  1634. ibuf->offset += obuf->len;
  1635. ibuf->len -= obuf->len;
  1636. }
  1637. ret += obuf->len;
  1638. len -= obuf->len;
  1639. } while (len);
  1640. pipe_unlock(ipipe);
  1641. pipe_unlock(opipe);
  1642. /*
  1643. * If we put data in the output pipe, wakeup any potential readers.
  1644. */
  1645. if (ret > 0)
  1646. wakeup_pipe_readers(opipe);
  1647. if (input_wakeup)
  1648. wakeup_pipe_writers(ipipe);
  1649. return ret;
  1650. }
  1651. /*
  1652. * Link contents of ipipe to opipe.
  1653. */
  1654. static int link_pipe(struct pipe_inode_info *ipipe,
  1655. struct pipe_inode_info *opipe,
  1656. size_t len, unsigned int flags)
  1657. {
  1658. struct pipe_buffer *ibuf, *obuf;
  1659. int ret = 0, i = 0, nbuf;
  1660. /*
  1661. * Potential ABBA deadlock, work around it by ordering lock
  1662. * grabbing by pipe info address. Otherwise two different processes
  1663. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1664. */
  1665. pipe_double_lock(ipipe, opipe);
  1666. do {
  1667. if (!opipe->readers) {
  1668. send_sig(SIGPIPE, current, 0);
  1669. if (!ret)
  1670. ret = -EPIPE;
  1671. break;
  1672. }
  1673. /*
  1674. * If we have iterated all input buffers or ran out of
  1675. * output room, break.
  1676. */
  1677. if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
  1678. break;
  1679. ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
  1680. nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
  1681. /*
  1682. * Get a reference to this pipe buffer,
  1683. * so we can copy the contents over.
  1684. */
  1685. ibuf->ops->get(ipipe, ibuf);
  1686. obuf = opipe->bufs + nbuf;
  1687. *obuf = *ibuf;
  1688. /*
  1689. * Don't inherit the gift flag, we need to
  1690. * prevent multiple steals of this page.
  1691. */
  1692. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1693. if (obuf->len > len)
  1694. obuf->len = len;
  1695. opipe->nrbufs++;
  1696. ret += obuf->len;
  1697. len -= obuf->len;
  1698. i++;
  1699. } while (len);
  1700. /*
  1701. * return EAGAIN if we have the potential of some data in the
  1702. * future, otherwise just return 0
  1703. */
  1704. if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
  1705. ret = -EAGAIN;
  1706. pipe_unlock(ipipe);
  1707. pipe_unlock(opipe);
  1708. /*
  1709. * If we put data in the output pipe, wakeup any potential readers.
  1710. */
  1711. if (ret > 0)
  1712. wakeup_pipe_readers(opipe);
  1713. return ret;
  1714. }
  1715. /*
  1716. * This is a tee(1) implementation that works on pipes. It doesn't copy
  1717. * any data, it simply references the 'in' pages on the 'out' pipe.
  1718. * The 'flags' used are the SPLICE_F_* variants, currently the only
  1719. * applicable one is SPLICE_F_NONBLOCK.
  1720. */
  1721. static long do_tee(struct file *in, struct file *out, size_t len,
  1722. unsigned int flags)
  1723. {
  1724. struct pipe_inode_info *ipipe = get_pipe_info(in);
  1725. struct pipe_inode_info *opipe = get_pipe_info(out);
  1726. int ret = -EINVAL;
  1727. /*
  1728. * Duplicate the contents of ipipe to opipe without actually
  1729. * copying the data.
  1730. */
  1731. if (ipipe && opipe && ipipe != opipe) {
  1732. /*
  1733. * Keep going, unless we encounter an error. The ipipe/opipe
  1734. * ordering doesn't really matter.
  1735. */
  1736. ret = ipipe_prep(ipipe, flags);
  1737. if (!ret) {
  1738. ret = opipe_prep(opipe, flags);
  1739. if (!ret)
  1740. ret = link_pipe(ipipe, opipe, len, flags);
  1741. }
  1742. }
  1743. return ret;
  1744. }
  1745. SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
  1746. {
  1747. struct fd in;
  1748. int error;
  1749. if (unlikely(!len))
  1750. return 0;
  1751. error = -EBADF;
  1752. in = fdget(fdin);
  1753. if (in.file) {
  1754. if (in.file->f_mode & FMODE_READ) {
  1755. struct fd out = fdget(fdout);
  1756. if (out.file) {
  1757. if (out.file->f_mode & FMODE_WRITE)
  1758. error = do_tee(in.file, out.file,
  1759. len, flags);
  1760. fdput(out);
  1761. }
  1762. }
  1763. fdput(in);
  1764. }
  1765. return error;
  1766. }