splice.c 46 KB

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