splice.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043
  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/module.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. /*
  639. * Careful, ->map() uses KM_USER0!
  640. */
  641. char *src = buf->ops->map(pipe, buf, 1);
  642. char *dst = kmap_atomic(page, KM_USER1);
  643. memcpy(dst + offset, src + buf->offset, this_len);
  644. flush_dcache_page(page);
  645. kunmap_atomic(dst, KM_USER1);
  646. buf->ops->unmap(pipe, buf, src);
  647. }
  648. ret = pagecache_write_end(file, mapping, sd->pos, this_len, this_len,
  649. page, fsdata);
  650. out:
  651. return ret;
  652. }
  653. EXPORT_SYMBOL(pipe_to_file);
  654. static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
  655. {
  656. smp_mb();
  657. if (waitqueue_active(&pipe->wait))
  658. wake_up_interruptible(&pipe->wait);
  659. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  660. }
  661. /**
  662. * splice_from_pipe_feed - feed available data from a pipe to a file
  663. * @pipe: pipe to splice from
  664. * @sd: information to @actor
  665. * @actor: handler that splices the data
  666. *
  667. * Description:
  668. * This function loops over the pipe and calls @actor to do the
  669. * actual moving of a single struct pipe_buffer to the desired
  670. * destination. It returns when there's no more buffers left in
  671. * the pipe or if the requested number of bytes (@sd->total_len)
  672. * have been copied. It returns a positive number (one) if the
  673. * pipe needs to be filled with more data, zero if the required
  674. * number of bytes have been copied and -errno on error.
  675. *
  676. * This, together with splice_from_pipe_{begin,end,next}, may be
  677. * used to implement the functionality of __splice_from_pipe() when
  678. * locking is required around copying the pipe buffers to the
  679. * destination.
  680. */
  681. int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
  682. splice_actor *actor)
  683. {
  684. int ret;
  685. while (pipe->nrbufs) {
  686. struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  687. const struct pipe_buf_operations *ops = buf->ops;
  688. sd->len = buf->len;
  689. if (sd->len > sd->total_len)
  690. sd->len = sd->total_len;
  691. ret = buf->ops->confirm(pipe, buf);
  692. if (unlikely(ret)) {
  693. if (ret == -ENODATA)
  694. ret = 0;
  695. return ret;
  696. }
  697. ret = actor(pipe, buf, sd);
  698. if (ret <= 0)
  699. return ret;
  700. buf->offset += ret;
  701. buf->len -= ret;
  702. sd->num_spliced += ret;
  703. sd->len -= ret;
  704. sd->pos += ret;
  705. sd->total_len -= ret;
  706. if (!buf->len) {
  707. buf->ops = NULL;
  708. ops->release(pipe, buf);
  709. pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
  710. pipe->nrbufs--;
  711. if (pipe->inode)
  712. sd->need_wakeup = true;
  713. }
  714. if (!sd->total_len)
  715. return 0;
  716. }
  717. return 1;
  718. }
  719. EXPORT_SYMBOL(splice_from_pipe_feed);
  720. /**
  721. * splice_from_pipe_next - wait for some data to splice from
  722. * @pipe: pipe to splice from
  723. * @sd: information about the splice operation
  724. *
  725. * Description:
  726. * This function will wait for some data and return a positive
  727. * value (one) if pipe buffers are available. It will return zero
  728. * or -errno if no more data needs to be spliced.
  729. */
  730. int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
  731. {
  732. while (!pipe->nrbufs) {
  733. if (!pipe->writers)
  734. return 0;
  735. if (!pipe->waiting_writers && sd->num_spliced)
  736. return 0;
  737. if (sd->flags & SPLICE_F_NONBLOCK)
  738. return -EAGAIN;
  739. if (signal_pending(current))
  740. return -ERESTARTSYS;
  741. if (sd->need_wakeup) {
  742. wakeup_pipe_writers(pipe);
  743. sd->need_wakeup = false;
  744. }
  745. pipe_wait(pipe);
  746. }
  747. return 1;
  748. }
  749. EXPORT_SYMBOL(splice_from_pipe_next);
  750. /**
  751. * splice_from_pipe_begin - start splicing from pipe
  752. * @sd: information about the splice operation
  753. *
  754. * Description:
  755. * This function should be called before a loop containing
  756. * splice_from_pipe_next() and splice_from_pipe_feed() to
  757. * initialize the necessary fields of @sd.
  758. */
  759. void splice_from_pipe_begin(struct splice_desc *sd)
  760. {
  761. sd->num_spliced = 0;
  762. sd->need_wakeup = false;
  763. }
  764. EXPORT_SYMBOL(splice_from_pipe_begin);
  765. /**
  766. * splice_from_pipe_end - finish splicing from pipe
  767. * @pipe: pipe to splice from
  768. * @sd: information about the splice operation
  769. *
  770. * Description:
  771. * This function will wake up pipe writers if necessary. It should
  772. * be called after a loop containing splice_from_pipe_next() and
  773. * splice_from_pipe_feed().
  774. */
  775. void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
  776. {
  777. if (sd->need_wakeup)
  778. wakeup_pipe_writers(pipe);
  779. }
  780. EXPORT_SYMBOL(splice_from_pipe_end);
  781. /**
  782. * __splice_from_pipe - splice data from a pipe to given actor
  783. * @pipe: pipe to splice from
  784. * @sd: information to @actor
  785. * @actor: handler that splices the data
  786. *
  787. * Description:
  788. * This function does little more than loop over the pipe and call
  789. * @actor to do the actual moving of a single struct pipe_buffer to
  790. * the desired destination. See pipe_to_file, pipe_to_sendpage, or
  791. * pipe_to_user.
  792. *
  793. */
  794. ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
  795. splice_actor *actor)
  796. {
  797. int ret;
  798. splice_from_pipe_begin(sd);
  799. do {
  800. ret = splice_from_pipe_next(pipe, sd);
  801. if (ret > 0)
  802. ret = splice_from_pipe_feed(pipe, sd, actor);
  803. } while (ret > 0);
  804. splice_from_pipe_end(pipe, sd);
  805. return sd->num_spliced ? sd->num_spliced : ret;
  806. }
  807. EXPORT_SYMBOL(__splice_from_pipe);
  808. /**
  809. * splice_from_pipe - splice data from a pipe to a file
  810. * @pipe: pipe to splice from
  811. * @out: file to splice to
  812. * @ppos: position in @out
  813. * @len: how many bytes to splice
  814. * @flags: splice modifier flags
  815. * @actor: handler that splices the data
  816. *
  817. * Description:
  818. * See __splice_from_pipe. This function locks the pipe inode,
  819. * otherwise it's identical to __splice_from_pipe().
  820. *
  821. */
  822. ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
  823. loff_t *ppos, size_t len, unsigned int flags,
  824. splice_actor *actor)
  825. {
  826. ssize_t ret;
  827. struct splice_desc sd = {
  828. .total_len = len,
  829. .flags = flags,
  830. .pos = *ppos,
  831. .u.file = out,
  832. };
  833. pipe_lock(pipe);
  834. ret = __splice_from_pipe(pipe, &sd, actor);
  835. pipe_unlock(pipe);
  836. return ret;
  837. }
  838. /**
  839. * generic_file_splice_write - splice data from a pipe to a file
  840. * @pipe: pipe info
  841. * @out: file to write to
  842. * @ppos: position in @out
  843. * @len: number of bytes to splice
  844. * @flags: splice modifier flags
  845. *
  846. * Description:
  847. * Will either move or copy pages (determined by @flags options) from
  848. * the given pipe inode to the given file.
  849. *
  850. */
  851. ssize_t
  852. generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  853. loff_t *ppos, size_t len, unsigned int flags)
  854. {
  855. struct address_space *mapping = out->f_mapping;
  856. struct inode *inode = mapping->host;
  857. struct splice_desc sd = {
  858. .total_len = len,
  859. .flags = flags,
  860. .pos = *ppos,
  861. .u.file = out,
  862. };
  863. ssize_t ret;
  864. pipe_lock(pipe);
  865. splice_from_pipe_begin(&sd);
  866. do {
  867. ret = splice_from_pipe_next(pipe, &sd);
  868. if (ret <= 0)
  869. break;
  870. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  871. ret = file_remove_suid(out);
  872. if (!ret) {
  873. file_update_time(out);
  874. ret = splice_from_pipe_feed(pipe, &sd, pipe_to_file);
  875. }
  876. mutex_unlock(&inode->i_mutex);
  877. } while (ret > 0);
  878. splice_from_pipe_end(pipe, &sd);
  879. pipe_unlock(pipe);
  880. if (sd.num_spliced)
  881. ret = sd.num_spliced;
  882. if (ret > 0) {
  883. unsigned long nr_pages;
  884. int err;
  885. nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  886. err = generic_write_sync(out, *ppos, ret);
  887. if (err)
  888. ret = err;
  889. else
  890. *ppos += ret;
  891. balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
  892. }
  893. return ret;
  894. }
  895. EXPORT_SYMBOL(generic_file_splice_write);
  896. static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  897. struct splice_desc *sd)
  898. {
  899. int ret;
  900. void *data;
  901. data = buf->ops->map(pipe, buf, 0);
  902. ret = kernel_write(sd->u.file, data + buf->offset, sd->len, sd->pos);
  903. buf->ops->unmap(pipe, buf, data);
  904. return ret;
  905. }
  906. static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
  907. struct file *out, loff_t *ppos,
  908. size_t len, unsigned int flags)
  909. {
  910. ssize_t ret;
  911. ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
  912. if (ret > 0)
  913. *ppos += ret;
  914. return ret;
  915. }
  916. /**
  917. * generic_splice_sendpage - splice data from a pipe to a socket
  918. * @pipe: pipe to splice from
  919. * @out: socket to write to
  920. * @ppos: position in @out
  921. * @len: number of bytes to splice
  922. * @flags: splice modifier flags
  923. *
  924. * Description:
  925. * Will send @len bytes from the pipe to a network socket. No data copying
  926. * is involved.
  927. *
  928. */
  929. ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
  930. loff_t *ppos, size_t len, unsigned int flags)
  931. {
  932. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
  933. }
  934. EXPORT_SYMBOL(generic_splice_sendpage);
  935. /*
  936. * Attempt to initiate a splice from pipe to file.
  937. */
  938. static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  939. loff_t *ppos, size_t len, unsigned int flags)
  940. {
  941. ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
  942. loff_t *, size_t, unsigned int);
  943. int ret;
  944. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  945. return -EBADF;
  946. if (unlikely(out->f_flags & O_APPEND))
  947. return -EINVAL;
  948. ret = rw_verify_area(WRITE, out, ppos, len);
  949. if (unlikely(ret < 0))
  950. return ret;
  951. if (out->f_op && out->f_op->splice_write)
  952. splice_write = out->f_op->splice_write;
  953. else
  954. splice_write = default_file_splice_write;
  955. return splice_write(pipe, out, ppos, len, flags);
  956. }
  957. /*
  958. * Attempt to initiate a splice from a file to a pipe.
  959. */
  960. static long do_splice_to(struct file *in, loff_t *ppos,
  961. struct pipe_inode_info *pipe, size_t len,
  962. unsigned int flags)
  963. {
  964. ssize_t (*splice_read)(struct file *, loff_t *,
  965. struct pipe_inode_info *, size_t, unsigned int);
  966. int ret;
  967. if (unlikely(!(in->f_mode & FMODE_READ)))
  968. return -EBADF;
  969. ret = rw_verify_area(READ, in, ppos, len);
  970. if (unlikely(ret < 0))
  971. return ret;
  972. if (in->f_op && in->f_op->splice_read)
  973. splice_read = in->f_op->splice_read;
  974. else
  975. splice_read = default_file_splice_read;
  976. return splice_read(in, ppos, pipe, len, flags);
  977. }
  978. /**
  979. * splice_direct_to_actor - splices data directly between two non-pipes
  980. * @in: file to splice from
  981. * @sd: actor information on where to splice to
  982. * @actor: handles the data splicing
  983. *
  984. * Description:
  985. * This is a special case helper to splice directly between two
  986. * points, without requiring an explicit pipe. Internally an allocated
  987. * pipe is cached in the process, and reused during the lifetime of
  988. * that process.
  989. *
  990. */
  991. ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
  992. splice_direct_actor *actor)
  993. {
  994. struct pipe_inode_info *pipe;
  995. long ret, bytes;
  996. umode_t i_mode;
  997. size_t len;
  998. int i, flags;
  999. /*
  1000. * We require the input being a regular file, as we don't want to
  1001. * randomly drop data for eg socket -> socket splicing. Use the
  1002. * piped splicing for that!
  1003. */
  1004. i_mode = in->f_path.dentry->d_inode->i_mode;
  1005. if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  1006. return -EINVAL;
  1007. /*
  1008. * neither in nor out is a pipe, setup an internal pipe attached to
  1009. * 'out' and transfer the wanted data from 'in' to 'out' through that
  1010. */
  1011. pipe = current->splice_pipe;
  1012. if (unlikely(!pipe)) {
  1013. pipe = alloc_pipe_info(NULL);
  1014. if (!pipe)
  1015. return -ENOMEM;
  1016. /*
  1017. * We don't have an immediate reader, but we'll read the stuff
  1018. * out of the pipe right after the splice_to_pipe(). So set
  1019. * PIPE_READERS appropriately.
  1020. */
  1021. pipe->readers = 1;
  1022. current->splice_pipe = pipe;
  1023. }
  1024. /*
  1025. * Do the splice.
  1026. */
  1027. ret = 0;
  1028. bytes = 0;
  1029. len = sd->total_len;
  1030. flags = sd->flags;
  1031. /*
  1032. * Don't block on output, we have to drain the direct pipe.
  1033. */
  1034. sd->flags &= ~SPLICE_F_NONBLOCK;
  1035. while (len) {
  1036. size_t read_len;
  1037. loff_t pos = sd->pos, prev_pos = pos;
  1038. ret = do_splice_to(in, &pos, pipe, len, flags);
  1039. if (unlikely(ret <= 0))
  1040. goto out_release;
  1041. read_len = ret;
  1042. sd->total_len = read_len;
  1043. /*
  1044. * NOTE: nonblocking mode only applies to the input. We
  1045. * must not do the output in nonblocking mode as then we
  1046. * could get stuck data in the internal pipe:
  1047. */
  1048. ret = actor(pipe, sd);
  1049. if (unlikely(ret <= 0)) {
  1050. sd->pos = prev_pos;
  1051. goto out_release;
  1052. }
  1053. bytes += ret;
  1054. len -= ret;
  1055. sd->pos = pos;
  1056. if (ret < read_len) {
  1057. sd->pos = prev_pos + ret;
  1058. goto out_release;
  1059. }
  1060. }
  1061. done:
  1062. pipe->nrbufs = pipe->curbuf = 0;
  1063. file_accessed(in);
  1064. return bytes;
  1065. out_release:
  1066. /*
  1067. * If we did an incomplete transfer we must release
  1068. * the pipe buffers in question:
  1069. */
  1070. for (i = 0; i < pipe->buffers; i++) {
  1071. struct pipe_buffer *buf = pipe->bufs + i;
  1072. if (buf->ops) {
  1073. buf->ops->release(pipe, buf);
  1074. buf->ops = NULL;
  1075. }
  1076. }
  1077. if (!bytes)
  1078. bytes = ret;
  1079. goto done;
  1080. }
  1081. EXPORT_SYMBOL(splice_direct_to_actor);
  1082. static int direct_splice_actor(struct pipe_inode_info *pipe,
  1083. struct splice_desc *sd)
  1084. {
  1085. struct file *file = sd->u.file;
  1086. return do_splice_from(pipe, file, &file->f_pos, sd->total_len,
  1087. sd->flags);
  1088. }
  1089. /**
  1090. * do_splice_direct - splices data directly between two files
  1091. * @in: file to splice from
  1092. * @ppos: input file offset
  1093. * @out: file to splice to
  1094. * @len: number of bytes to splice
  1095. * @flags: splice modifier flags
  1096. *
  1097. * Description:
  1098. * For use by do_sendfile(). splice can easily emulate sendfile, but
  1099. * doing it in the application would incur an extra system call
  1100. * (splice in + splice out, as compared to just sendfile()). So this helper
  1101. * can splice directly through a process-private pipe.
  1102. *
  1103. */
  1104. long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
  1105. size_t len, unsigned int flags)
  1106. {
  1107. struct splice_desc sd = {
  1108. .len = len,
  1109. .total_len = len,
  1110. .flags = flags,
  1111. .pos = *ppos,
  1112. .u.file = out,
  1113. };
  1114. long ret;
  1115. ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
  1116. if (ret > 0)
  1117. *ppos = sd.pos;
  1118. return ret;
  1119. }
  1120. static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  1121. struct pipe_inode_info *opipe,
  1122. size_t len, unsigned int flags);
  1123. /*
  1124. * Determine where to splice to/from.
  1125. */
  1126. static long do_splice(struct file *in, loff_t __user *off_in,
  1127. struct file *out, loff_t __user *off_out,
  1128. size_t len, unsigned int flags)
  1129. {
  1130. struct pipe_inode_info *ipipe;
  1131. struct pipe_inode_info *opipe;
  1132. loff_t offset, *off;
  1133. long ret;
  1134. ipipe = get_pipe_info(in);
  1135. opipe = get_pipe_info(out);
  1136. if (ipipe && opipe) {
  1137. if (off_in || off_out)
  1138. return -ESPIPE;
  1139. if (!(in->f_mode & FMODE_READ))
  1140. return -EBADF;
  1141. if (!(out->f_mode & FMODE_WRITE))
  1142. return -EBADF;
  1143. /* Splicing to self would be fun, but... */
  1144. if (ipipe == opipe)
  1145. return -EINVAL;
  1146. return splice_pipe_to_pipe(ipipe, opipe, len, flags);
  1147. }
  1148. if (ipipe) {
  1149. if (off_in)
  1150. return -ESPIPE;
  1151. if (off_out) {
  1152. if (!(out->f_mode & FMODE_PWRITE))
  1153. return -EINVAL;
  1154. if (copy_from_user(&offset, off_out, sizeof(loff_t)))
  1155. return -EFAULT;
  1156. off = &offset;
  1157. } else
  1158. off = &out->f_pos;
  1159. ret = do_splice_from(ipipe, out, off, len, flags);
  1160. if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
  1161. ret = -EFAULT;
  1162. return ret;
  1163. }
  1164. if (opipe) {
  1165. if (off_out)
  1166. return -ESPIPE;
  1167. if (off_in) {
  1168. if (!(in->f_mode & FMODE_PREAD))
  1169. return -EINVAL;
  1170. if (copy_from_user(&offset, off_in, sizeof(loff_t)))
  1171. return -EFAULT;
  1172. off = &offset;
  1173. } else
  1174. off = &in->f_pos;
  1175. ret = do_splice_to(in, off, opipe, len, flags);
  1176. if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
  1177. ret = -EFAULT;
  1178. return ret;
  1179. }
  1180. return -EINVAL;
  1181. }
  1182. /*
  1183. * Map an iov into an array of pages and offset/length tupples. With the
  1184. * partial_page structure, we can map several non-contiguous ranges into
  1185. * our ones pages[] map instead of splitting that operation into pieces.
  1186. * Could easily be exported as a generic helper for other users, in which
  1187. * case one would probably want to add a 'max_nr_pages' parameter as well.
  1188. */
  1189. static int get_iovec_page_array(const struct iovec __user *iov,
  1190. unsigned int nr_vecs, struct page **pages,
  1191. struct partial_page *partial, int aligned,
  1192. unsigned int pipe_buffers)
  1193. {
  1194. int buffers = 0, error = 0;
  1195. while (nr_vecs) {
  1196. unsigned long off, npages;
  1197. struct iovec entry;
  1198. void __user *base;
  1199. size_t len;
  1200. int i;
  1201. error = -EFAULT;
  1202. if (copy_from_user(&entry, iov, sizeof(entry)))
  1203. break;
  1204. base = entry.iov_base;
  1205. len = entry.iov_len;
  1206. /*
  1207. * Sanity check this iovec. 0 read succeeds.
  1208. */
  1209. error = 0;
  1210. if (unlikely(!len))
  1211. break;
  1212. error = -EFAULT;
  1213. if (!access_ok(VERIFY_READ, base, len))
  1214. break;
  1215. /*
  1216. * Get this base offset and number of pages, then map
  1217. * in the user pages.
  1218. */
  1219. off = (unsigned long) base & ~PAGE_MASK;
  1220. /*
  1221. * If asked for alignment, the offset must be zero and the
  1222. * length a multiple of the PAGE_SIZE.
  1223. */
  1224. error = -EINVAL;
  1225. if (aligned && (off || len & ~PAGE_MASK))
  1226. break;
  1227. npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1228. if (npages > pipe_buffers - buffers)
  1229. npages = pipe_buffers - buffers;
  1230. error = get_user_pages_fast((unsigned long)base, npages,
  1231. 0, &pages[buffers]);
  1232. if (unlikely(error <= 0))
  1233. break;
  1234. /*
  1235. * Fill this contiguous range into the partial page map.
  1236. */
  1237. for (i = 0; i < error; i++) {
  1238. const int plen = min_t(size_t, len, PAGE_SIZE - off);
  1239. partial[buffers].offset = off;
  1240. partial[buffers].len = plen;
  1241. off = 0;
  1242. len -= plen;
  1243. buffers++;
  1244. }
  1245. /*
  1246. * We didn't complete this iov, stop here since it probably
  1247. * means we have to move some of this into a pipe to
  1248. * be able to continue.
  1249. */
  1250. if (len)
  1251. break;
  1252. /*
  1253. * Don't continue if we mapped fewer pages than we asked for,
  1254. * or if we mapped the max number of pages that we have
  1255. * room for.
  1256. */
  1257. if (error < npages || buffers == pipe_buffers)
  1258. break;
  1259. nr_vecs--;
  1260. iov++;
  1261. }
  1262. if (buffers)
  1263. return buffers;
  1264. return error;
  1265. }
  1266. static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  1267. struct splice_desc *sd)
  1268. {
  1269. char *src;
  1270. int ret;
  1271. /*
  1272. * See if we can use the atomic maps, by prefaulting in the
  1273. * pages and doing an atomic copy
  1274. */
  1275. if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) {
  1276. src = buf->ops->map(pipe, buf, 1);
  1277. ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset,
  1278. sd->len);
  1279. buf->ops->unmap(pipe, buf, src);
  1280. if (!ret) {
  1281. ret = sd->len;
  1282. goto out;
  1283. }
  1284. }
  1285. /*
  1286. * No dice, use slow non-atomic map and copy
  1287. */
  1288. src = buf->ops->map(pipe, buf, 0);
  1289. ret = sd->len;
  1290. if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len))
  1291. ret = -EFAULT;
  1292. buf->ops->unmap(pipe, buf, src);
  1293. out:
  1294. if (ret > 0)
  1295. sd->u.userptr += ret;
  1296. return ret;
  1297. }
  1298. /*
  1299. * For lack of a better implementation, implement vmsplice() to userspace
  1300. * as a simple copy of the pipes pages to the user iov.
  1301. */
  1302. static long vmsplice_to_user(struct file *file, const struct iovec __user *iov,
  1303. unsigned long nr_segs, unsigned int flags)
  1304. {
  1305. struct pipe_inode_info *pipe;
  1306. struct splice_desc sd;
  1307. ssize_t size;
  1308. int error;
  1309. long ret;
  1310. pipe = get_pipe_info(file);
  1311. if (!pipe)
  1312. return -EBADF;
  1313. pipe_lock(pipe);
  1314. error = ret = 0;
  1315. while (nr_segs) {
  1316. void __user *base;
  1317. size_t len;
  1318. /*
  1319. * Get user address base and length for this iovec.
  1320. */
  1321. error = get_user(base, &iov->iov_base);
  1322. if (unlikely(error))
  1323. break;
  1324. error = get_user(len, &iov->iov_len);
  1325. if (unlikely(error))
  1326. break;
  1327. /*
  1328. * Sanity check this iovec. 0 read succeeds.
  1329. */
  1330. if (unlikely(!len))
  1331. break;
  1332. if (unlikely(!base)) {
  1333. error = -EFAULT;
  1334. break;
  1335. }
  1336. if (unlikely(!access_ok(VERIFY_WRITE, base, len))) {
  1337. error = -EFAULT;
  1338. break;
  1339. }
  1340. sd.len = 0;
  1341. sd.total_len = len;
  1342. sd.flags = flags;
  1343. sd.u.userptr = base;
  1344. sd.pos = 0;
  1345. size = __splice_from_pipe(pipe, &sd, pipe_to_user);
  1346. if (size < 0) {
  1347. if (!ret)
  1348. ret = size;
  1349. break;
  1350. }
  1351. ret += size;
  1352. if (size < len)
  1353. break;
  1354. nr_segs--;
  1355. iov++;
  1356. }
  1357. pipe_unlock(pipe);
  1358. if (!ret)
  1359. ret = error;
  1360. return ret;
  1361. }
  1362. /*
  1363. * vmsplice splices a user address range into a pipe. It can be thought of
  1364. * as splice-from-memory, where the regular splice is splice-from-file (or
  1365. * to file). In both cases the output is a pipe, naturally.
  1366. */
  1367. static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
  1368. unsigned long nr_segs, unsigned int flags)
  1369. {
  1370. struct pipe_inode_info *pipe;
  1371. struct page *pages[PIPE_DEF_BUFFERS];
  1372. struct partial_page partial[PIPE_DEF_BUFFERS];
  1373. struct splice_pipe_desc spd = {
  1374. .pages = pages,
  1375. .partial = partial,
  1376. .flags = flags,
  1377. .ops = &user_page_pipe_buf_ops,
  1378. .spd_release = spd_release_page,
  1379. };
  1380. long ret;
  1381. pipe = get_pipe_info(file);
  1382. if (!pipe)
  1383. return -EBADF;
  1384. if (splice_grow_spd(pipe, &spd))
  1385. return -ENOMEM;
  1386. spd.nr_pages = get_iovec_page_array(iov, nr_segs, spd.pages,
  1387. spd.partial, flags & SPLICE_F_GIFT,
  1388. pipe->buffers);
  1389. if (spd.nr_pages <= 0)
  1390. ret = spd.nr_pages;
  1391. else
  1392. ret = splice_to_pipe(pipe, &spd);
  1393. splice_shrink_spd(pipe, &spd);
  1394. return ret;
  1395. }
  1396. /*
  1397. * Note that vmsplice only really supports true splicing _from_ user memory
  1398. * to a pipe, not the other way around. Splicing from user memory is a simple
  1399. * operation that can be supported without any funky alignment restrictions
  1400. * or nasty vm tricks. We simply map in the user memory and fill them into
  1401. * a pipe. The reverse isn't quite as easy, though. There are two possible
  1402. * solutions for that:
  1403. *
  1404. * - memcpy() the data internally, at which point we might as well just
  1405. * do a regular read() on the buffer anyway.
  1406. * - Lots of nasty vm tricks, that are neither fast nor flexible (it
  1407. * has restriction limitations on both ends of the pipe).
  1408. *
  1409. * Currently we punt and implement it as a normal copy, see pipe_to_user().
  1410. *
  1411. */
  1412. SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
  1413. unsigned long, nr_segs, unsigned int, flags)
  1414. {
  1415. struct file *file;
  1416. long error;
  1417. int fput;
  1418. if (unlikely(nr_segs > UIO_MAXIOV))
  1419. return -EINVAL;
  1420. else if (unlikely(!nr_segs))
  1421. return 0;
  1422. error = -EBADF;
  1423. file = fget_light(fd, &fput);
  1424. if (file) {
  1425. if (file->f_mode & FMODE_WRITE)
  1426. error = vmsplice_to_pipe(file, iov, nr_segs, flags);
  1427. else if (file->f_mode & FMODE_READ)
  1428. error = vmsplice_to_user(file, iov, nr_segs, flags);
  1429. fput_light(file, fput);
  1430. }
  1431. return error;
  1432. }
  1433. SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
  1434. int, fd_out, loff_t __user *, off_out,
  1435. size_t, len, unsigned int, flags)
  1436. {
  1437. long error;
  1438. struct file *in, *out;
  1439. int fput_in, fput_out;
  1440. if (unlikely(!len))
  1441. return 0;
  1442. error = -EBADF;
  1443. in = fget_light(fd_in, &fput_in);
  1444. if (in) {
  1445. if (in->f_mode & FMODE_READ) {
  1446. out = fget_light(fd_out, &fput_out);
  1447. if (out) {
  1448. if (out->f_mode & FMODE_WRITE)
  1449. error = do_splice(in, off_in,
  1450. out, off_out,
  1451. len, flags);
  1452. fput_light(out, fput_out);
  1453. }
  1454. }
  1455. fput_light(in, fput_in);
  1456. }
  1457. return error;
  1458. }
  1459. /*
  1460. * Make sure there's data to read. Wait for input if we can, otherwise
  1461. * return an appropriate error.
  1462. */
  1463. static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1464. {
  1465. int ret;
  1466. /*
  1467. * Check ->nrbufs without the inode lock first. This function
  1468. * is speculative anyways, so missing one is ok.
  1469. */
  1470. if (pipe->nrbufs)
  1471. return 0;
  1472. ret = 0;
  1473. pipe_lock(pipe);
  1474. while (!pipe->nrbufs) {
  1475. if (signal_pending(current)) {
  1476. ret = -ERESTARTSYS;
  1477. break;
  1478. }
  1479. if (!pipe->writers)
  1480. break;
  1481. if (!pipe->waiting_writers) {
  1482. if (flags & SPLICE_F_NONBLOCK) {
  1483. ret = -EAGAIN;
  1484. break;
  1485. }
  1486. }
  1487. pipe_wait(pipe);
  1488. }
  1489. pipe_unlock(pipe);
  1490. return ret;
  1491. }
  1492. /*
  1493. * Make sure there's writeable room. Wait for room if we can, otherwise
  1494. * return an appropriate error.
  1495. */
  1496. static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1497. {
  1498. int ret;
  1499. /*
  1500. * Check ->nrbufs without the inode lock first. This function
  1501. * is speculative anyways, so missing one is ok.
  1502. */
  1503. if (pipe->nrbufs < pipe->buffers)
  1504. return 0;
  1505. ret = 0;
  1506. pipe_lock(pipe);
  1507. while (pipe->nrbufs >= pipe->buffers) {
  1508. if (!pipe->readers) {
  1509. send_sig(SIGPIPE, current, 0);
  1510. ret = -EPIPE;
  1511. break;
  1512. }
  1513. if (flags & SPLICE_F_NONBLOCK) {
  1514. ret = -EAGAIN;
  1515. break;
  1516. }
  1517. if (signal_pending(current)) {
  1518. ret = -ERESTARTSYS;
  1519. break;
  1520. }
  1521. pipe->waiting_writers++;
  1522. pipe_wait(pipe);
  1523. pipe->waiting_writers--;
  1524. }
  1525. pipe_unlock(pipe);
  1526. return ret;
  1527. }
  1528. /*
  1529. * Splice contents of ipipe to opipe.
  1530. */
  1531. static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  1532. struct pipe_inode_info *opipe,
  1533. size_t len, unsigned int flags)
  1534. {
  1535. struct pipe_buffer *ibuf, *obuf;
  1536. int ret = 0, nbuf;
  1537. bool input_wakeup = false;
  1538. retry:
  1539. ret = ipipe_prep(ipipe, flags);
  1540. if (ret)
  1541. return ret;
  1542. ret = opipe_prep(opipe, flags);
  1543. if (ret)
  1544. return ret;
  1545. /*
  1546. * Potential ABBA deadlock, work around it by ordering lock
  1547. * grabbing by pipe info address. Otherwise two different processes
  1548. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1549. */
  1550. pipe_double_lock(ipipe, opipe);
  1551. do {
  1552. if (!opipe->readers) {
  1553. send_sig(SIGPIPE, current, 0);
  1554. if (!ret)
  1555. ret = -EPIPE;
  1556. break;
  1557. }
  1558. if (!ipipe->nrbufs && !ipipe->writers)
  1559. break;
  1560. /*
  1561. * Cannot make any progress, because either the input
  1562. * pipe is empty or the output pipe is full.
  1563. */
  1564. if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
  1565. /* Already processed some buffers, break */
  1566. if (ret)
  1567. break;
  1568. if (flags & SPLICE_F_NONBLOCK) {
  1569. ret = -EAGAIN;
  1570. break;
  1571. }
  1572. /*
  1573. * We raced with another reader/writer and haven't
  1574. * managed to process any buffers. A zero return
  1575. * value means EOF, so retry instead.
  1576. */
  1577. pipe_unlock(ipipe);
  1578. pipe_unlock(opipe);
  1579. goto retry;
  1580. }
  1581. ibuf = ipipe->bufs + ipipe->curbuf;
  1582. nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
  1583. obuf = opipe->bufs + nbuf;
  1584. if (len >= ibuf->len) {
  1585. /*
  1586. * Simply move the whole buffer from ipipe to opipe
  1587. */
  1588. *obuf = *ibuf;
  1589. ibuf->ops = NULL;
  1590. opipe->nrbufs++;
  1591. ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
  1592. ipipe->nrbufs--;
  1593. input_wakeup = true;
  1594. } else {
  1595. /*
  1596. * Get a reference to this pipe buffer,
  1597. * so we can copy the contents over.
  1598. */
  1599. ibuf->ops->get(ipipe, ibuf);
  1600. *obuf = *ibuf;
  1601. /*
  1602. * Don't inherit the gift flag, we need to
  1603. * prevent multiple steals of this page.
  1604. */
  1605. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1606. obuf->len = len;
  1607. opipe->nrbufs++;
  1608. ibuf->offset += obuf->len;
  1609. ibuf->len -= obuf->len;
  1610. }
  1611. ret += obuf->len;
  1612. len -= obuf->len;
  1613. } while (len);
  1614. pipe_unlock(ipipe);
  1615. pipe_unlock(opipe);
  1616. /*
  1617. * If we put data in the output pipe, wakeup any potential readers.
  1618. */
  1619. if (ret > 0)
  1620. wakeup_pipe_readers(opipe);
  1621. if (input_wakeup)
  1622. wakeup_pipe_writers(ipipe);
  1623. return ret;
  1624. }
  1625. /*
  1626. * Link contents of ipipe to opipe.
  1627. */
  1628. static int link_pipe(struct pipe_inode_info *ipipe,
  1629. struct pipe_inode_info *opipe,
  1630. size_t len, unsigned int flags)
  1631. {
  1632. struct pipe_buffer *ibuf, *obuf;
  1633. int ret = 0, i = 0, nbuf;
  1634. /*
  1635. * Potential ABBA deadlock, work around it by ordering lock
  1636. * grabbing by pipe info address. Otherwise two different processes
  1637. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1638. */
  1639. pipe_double_lock(ipipe, opipe);
  1640. do {
  1641. if (!opipe->readers) {
  1642. send_sig(SIGPIPE, current, 0);
  1643. if (!ret)
  1644. ret = -EPIPE;
  1645. break;
  1646. }
  1647. /*
  1648. * If we have iterated all input buffers or ran out of
  1649. * output room, break.
  1650. */
  1651. if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
  1652. break;
  1653. ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
  1654. nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
  1655. /*
  1656. * Get a reference to this pipe buffer,
  1657. * so we can copy the contents over.
  1658. */
  1659. ibuf->ops->get(ipipe, ibuf);
  1660. obuf = opipe->bufs + nbuf;
  1661. *obuf = *ibuf;
  1662. /*
  1663. * Don't inherit the gift flag, we need to
  1664. * prevent multiple steals of this page.
  1665. */
  1666. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1667. if (obuf->len > len)
  1668. obuf->len = len;
  1669. opipe->nrbufs++;
  1670. ret += obuf->len;
  1671. len -= obuf->len;
  1672. i++;
  1673. } while (len);
  1674. /*
  1675. * return EAGAIN if we have the potential of some data in the
  1676. * future, otherwise just return 0
  1677. */
  1678. if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
  1679. ret = -EAGAIN;
  1680. pipe_unlock(ipipe);
  1681. pipe_unlock(opipe);
  1682. /*
  1683. * If we put data in the output pipe, wakeup any potential readers.
  1684. */
  1685. if (ret > 0)
  1686. wakeup_pipe_readers(opipe);
  1687. return ret;
  1688. }
  1689. /*
  1690. * This is a tee(1) implementation that works on pipes. It doesn't copy
  1691. * any data, it simply references the 'in' pages on the 'out' pipe.
  1692. * The 'flags' used are the SPLICE_F_* variants, currently the only
  1693. * applicable one is SPLICE_F_NONBLOCK.
  1694. */
  1695. static long do_tee(struct file *in, struct file *out, size_t len,
  1696. unsigned int flags)
  1697. {
  1698. struct pipe_inode_info *ipipe = get_pipe_info(in);
  1699. struct pipe_inode_info *opipe = get_pipe_info(out);
  1700. int ret = -EINVAL;
  1701. /*
  1702. * Duplicate the contents of ipipe to opipe without actually
  1703. * copying the data.
  1704. */
  1705. if (ipipe && opipe && ipipe != opipe) {
  1706. /*
  1707. * Keep going, unless we encounter an error. The ipipe/opipe
  1708. * ordering doesn't really matter.
  1709. */
  1710. ret = ipipe_prep(ipipe, flags);
  1711. if (!ret) {
  1712. ret = opipe_prep(opipe, flags);
  1713. if (!ret)
  1714. ret = link_pipe(ipipe, opipe, len, flags);
  1715. }
  1716. }
  1717. return ret;
  1718. }
  1719. SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
  1720. {
  1721. struct file *in;
  1722. int error, fput_in;
  1723. if (unlikely(!len))
  1724. return 0;
  1725. error = -EBADF;
  1726. in = fget_light(fdin, &fput_in);
  1727. if (in) {
  1728. if (in->f_mode & FMODE_READ) {
  1729. int fput_out;
  1730. struct file *out = fget_light(fdout, &fput_out);
  1731. if (out) {
  1732. if (out->f_mode & FMODE_WRITE)
  1733. error = do_tee(in, out, len, flags);
  1734. fput_light(out, fput_out);
  1735. }
  1736. }
  1737. fput_light(in, fput_in);
  1738. }
  1739. return error;
  1740. }