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/export.h>
  29. #include <linux/syscalls.h>
  30. #include <linux/uio.h>
  31. #include <linux/security.h>
  32. #include <linux/gfp.h>
  33. #include <linux/socket.h>
  34. /*
  35. * Attempt to steal a page from a pipe buffer. This should perhaps go into
  36. * a vm helper function, it's already simplified quite a bit by the
  37. * addition of remove_mapping(). If success is returned, the caller may
  38. * attempt to reuse this page for another destination.
  39. */
  40. static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
  41. struct pipe_buffer *buf)
  42. {
  43. struct page *page = buf->page;
  44. struct address_space *mapping;
  45. lock_page(page);
  46. mapping = page_mapping(page);
  47. if (mapping) {
  48. WARN_ON(!PageUptodate(page));
  49. /*
  50. * At least for ext2 with nobh option, we need to wait on
  51. * writeback completing on this page, since we'll remove it
  52. * from the pagecache. Otherwise truncate wont wait on the
  53. * page, allowing the disk blocks to be reused by someone else
  54. * before we actually wrote our data to them. fs corruption
  55. * ensues.
  56. */
  57. wait_on_page_writeback(page);
  58. if (page_has_private(page) &&
  59. !try_to_release_page(page, GFP_KERNEL))
  60. goto out_unlock;
  61. /*
  62. * If we succeeded in removing the mapping, set LRU flag
  63. * and return good.
  64. */
  65. if (remove_mapping(mapping, page)) {
  66. buf->flags |= PIPE_BUF_FLAG_LRU;
  67. return 0;
  68. }
  69. }
  70. /*
  71. * Raced with truncate or failed to remove page from current
  72. * address space, unlock and return failure.
  73. */
  74. out_unlock:
  75. unlock_page(page);
  76. return 1;
  77. }
  78. static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
  79. struct pipe_buffer *buf)
  80. {
  81. page_cache_release(buf->page);
  82. buf->flags &= ~PIPE_BUF_FLAG_LRU;
  83. }
  84. /*
  85. * Check whether the contents of buf is OK to access. Since the content
  86. * is a page cache page, IO may be in flight.
  87. */
  88. static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
  89. struct pipe_buffer *buf)
  90. {
  91. struct page *page = buf->page;
  92. int err;
  93. if (!PageUptodate(page)) {
  94. lock_page(page);
  95. /*
  96. * Page got truncated/unhashed. This will cause a 0-byte
  97. * splice, if this is the first page.
  98. */
  99. if (!page->mapping) {
  100. err = -ENODATA;
  101. goto error;
  102. }
  103. /*
  104. * Uh oh, read-error from disk.
  105. */
  106. if (!PageUptodate(page)) {
  107. err = -EIO;
  108. goto error;
  109. }
  110. /*
  111. * Page is ok afterall, we are done.
  112. */
  113. unlock_page(page);
  114. }
  115. return 0;
  116. error:
  117. unlock_page(page);
  118. return err;
  119. }
  120. const struct pipe_buf_operations page_cache_pipe_buf_ops = {
  121. .can_merge = 0,
  122. .map = generic_pipe_buf_map,
  123. .unmap = generic_pipe_buf_unmap,
  124. .confirm = page_cache_pipe_buf_confirm,
  125. .release = page_cache_pipe_buf_release,
  126. .steal = page_cache_pipe_buf_steal,
  127. .get = generic_pipe_buf_get,
  128. };
  129. static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
  130. struct pipe_buffer *buf)
  131. {
  132. if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
  133. return 1;
  134. buf->flags |= PIPE_BUF_FLAG_LRU;
  135. return generic_pipe_buf_steal(pipe, buf);
  136. }
  137. static const struct pipe_buf_operations user_page_pipe_buf_ops = {
  138. .can_merge = 0,
  139. .map = generic_pipe_buf_map,
  140. .unmap = generic_pipe_buf_unmap,
  141. .confirm = generic_pipe_buf_confirm,
  142. .release = page_cache_pipe_buf_release,
  143. .steal = user_page_pipe_buf_steal,
  144. .get = generic_pipe_buf_get,
  145. };
  146. static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
  147. {
  148. smp_mb();
  149. if (waitqueue_active(&pipe->wait))
  150. wake_up_interruptible(&pipe->wait);
  151. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  152. }
  153. /**
  154. * splice_to_pipe - fill passed data into a pipe
  155. * @pipe: pipe to fill
  156. * @spd: data to fill
  157. *
  158. * Description:
  159. * @spd contains a map of pages and len/offset tuples, along with
  160. * the struct pipe_buf_operations associated with these pages. This
  161. * function will link that data to the pipe.
  162. *
  163. */
  164. ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
  165. struct splice_pipe_desc *spd)
  166. {
  167. unsigned int spd_pages = spd->nr_pages;
  168. int ret, do_wakeup, page_nr;
  169. ret = 0;
  170. do_wakeup = 0;
  171. page_nr = 0;
  172. pipe_lock(pipe);
  173. for (;;) {
  174. if (!pipe->readers) {
  175. send_sig(SIGPIPE, current, 0);
  176. if (!ret)
  177. ret = -EPIPE;
  178. break;
  179. }
  180. if (pipe->nrbufs < pipe->buffers) {
  181. int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
  182. struct pipe_buffer *buf = pipe->bufs + newbuf;
  183. buf->page = spd->pages[page_nr];
  184. buf->offset = spd->partial[page_nr].offset;
  185. buf->len = spd->partial[page_nr].len;
  186. buf->private = spd->partial[page_nr].private;
  187. buf->ops = spd->ops;
  188. if (spd->flags & SPLICE_F_GIFT)
  189. buf->flags |= PIPE_BUF_FLAG_GIFT;
  190. pipe->nrbufs++;
  191. page_nr++;
  192. ret += buf->len;
  193. if (pipe->inode)
  194. do_wakeup = 1;
  195. if (!--spd->nr_pages)
  196. break;
  197. if (pipe->nrbufs < pipe->buffers)
  198. continue;
  199. break;
  200. }
  201. if (spd->flags & SPLICE_F_NONBLOCK) {
  202. if (!ret)
  203. ret = -EAGAIN;
  204. break;
  205. }
  206. if (signal_pending(current)) {
  207. if (!ret)
  208. ret = -ERESTARTSYS;
  209. break;
  210. }
  211. if (do_wakeup) {
  212. smp_mb();
  213. if (waitqueue_active(&pipe->wait))
  214. wake_up_interruptible_sync(&pipe->wait);
  215. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  216. do_wakeup = 0;
  217. }
  218. pipe->waiting_writers++;
  219. pipe_wait(pipe);
  220. pipe->waiting_writers--;
  221. }
  222. pipe_unlock(pipe);
  223. if (do_wakeup)
  224. wakeup_pipe_readers(pipe);
  225. while (page_nr < spd_pages)
  226. spd->spd_release(spd, page_nr++);
  227. return ret;
  228. }
  229. void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
  230. {
  231. page_cache_release(spd->pages[i]);
  232. }
  233. /*
  234. * Check if we need to grow the arrays holding pages and partial page
  235. * descriptions.
  236. */
  237. int splice_grow_spd(struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
  238. {
  239. if (pipe->buffers <= PIPE_DEF_BUFFERS)
  240. return 0;
  241. spd->pages = kmalloc(pipe->buffers * sizeof(struct page *), GFP_KERNEL);
  242. spd->partial = kmalloc(pipe->buffers * sizeof(struct partial_page), GFP_KERNEL);
  243. if (spd->pages && spd->partial)
  244. return 0;
  245. kfree(spd->pages);
  246. kfree(spd->partial);
  247. return -ENOMEM;
  248. }
  249. void splice_shrink_spd(struct pipe_inode_info *pipe,
  250. struct splice_pipe_desc *spd)
  251. {
  252. if (pipe->buffers <= PIPE_DEF_BUFFERS)
  253. return;
  254. kfree(spd->pages);
  255. kfree(spd->partial);
  256. }
  257. static int
  258. __generic_file_splice_read(struct file *in, loff_t *ppos,
  259. struct pipe_inode_info *pipe, size_t len,
  260. unsigned int flags)
  261. {
  262. struct address_space *mapping = in->f_mapping;
  263. unsigned int loff, nr_pages, req_pages;
  264. struct page *pages[PIPE_DEF_BUFFERS];
  265. struct partial_page partial[PIPE_DEF_BUFFERS];
  266. struct page *page;
  267. pgoff_t index, end_index;
  268. loff_t isize;
  269. int error, page_nr;
  270. struct splice_pipe_desc spd = {
  271. .pages = pages,
  272. .partial = partial,
  273. .flags = flags,
  274. .ops = &page_cache_pipe_buf_ops,
  275. .spd_release = spd_release_page,
  276. };
  277. if (splice_grow_spd(pipe, &spd))
  278. return -ENOMEM;
  279. index = *ppos >> PAGE_CACHE_SHIFT;
  280. loff = *ppos & ~PAGE_CACHE_MASK;
  281. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  282. nr_pages = min(req_pages, pipe->buffers);
  283. /*
  284. * Lookup the (hopefully) full range of pages we need.
  285. */
  286. spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, spd.pages);
  287. index += spd.nr_pages;
  288. /*
  289. * If find_get_pages_contig() returned fewer pages than we needed,
  290. * readahead/allocate the rest and fill in the holes.
  291. */
  292. if (spd.nr_pages < nr_pages)
  293. page_cache_sync_readahead(mapping, &in->f_ra, in,
  294. index, req_pages - spd.nr_pages);
  295. error = 0;
  296. while (spd.nr_pages < nr_pages) {
  297. /*
  298. * Page could be there, find_get_pages_contig() breaks on
  299. * the first hole.
  300. */
  301. page = find_get_page(mapping, index);
  302. if (!page) {
  303. /*
  304. * page didn't exist, allocate one.
  305. */
  306. page = page_cache_alloc_cold(mapping);
  307. if (!page)
  308. break;
  309. error = add_to_page_cache_lru(page, mapping, index,
  310. GFP_KERNEL);
  311. if (unlikely(error)) {
  312. page_cache_release(page);
  313. if (error == -EEXIST)
  314. continue;
  315. break;
  316. }
  317. /*
  318. * add_to_page_cache() locks the page, unlock it
  319. * to avoid convoluting the logic below even more.
  320. */
  321. unlock_page(page);
  322. }
  323. spd.pages[spd.nr_pages++] = page;
  324. index++;
  325. }
  326. /*
  327. * Now loop over the map and see if we need to start IO on any
  328. * pages, fill in the partial map, etc.
  329. */
  330. index = *ppos >> PAGE_CACHE_SHIFT;
  331. nr_pages = spd.nr_pages;
  332. spd.nr_pages = 0;
  333. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  334. unsigned int this_len;
  335. if (!len)
  336. break;
  337. /*
  338. * this_len is the max we'll use from this page
  339. */
  340. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  341. page = spd.pages[page_nr];
  342. if (PageReadahead(page))
  343. page_cache_async_readahead(mapping, &in->f_ra, in,
  344. page, index, req_pages - page_nr);
  345. /*
  346. * If the page isn't uptodate, we may need to start io on it
  347. */
  348. if (!PageUptodate(page)) {
  349. lock_page(page);
  350. /*
  351. * Page was truncated, or invalidated by the
  352. * filesystem. Redo the find/create, but this time the
  353. * page is kept locked, so there's no chance of another
  354. * race with truncate/invalidate.
  355. */
  356. if (!page->mapping) {
  357. unlock_page(page);
  358. page = find_or_create_page(mapping, index,
  359. mapping_gfp_mask(mapping));
  360. if (!page) {
  361. error = -ENOMEM;
  362. break;
  363. }
  364. page_cache_release(spd.pages[page_nr]);
  365. spd.pages[page_nr] = page;
  366. }
  367. /*
  368. * page was already under io and is now done, great
  369. */
  370. if (PageUptodate(page)) {
  371. unlock_page(page);
  372. goto fill_it;
  373. }
  374. /*
  375. * need to read in the page
  376. */
  377. error = mapping->a_ops->readpage(in, page);
  378. if (unlikely(error)) {
  379. /*
  380. * We really should re-lookup the page here,
  381. * but it complicates things a lot. Instead
  382. * lets just do what we already stored, and
  383. * we'll get it the next time we are called.
  384. */
  385. if (error == AOP_TRUNCATED_PAGE)
  386. error = 0;
  387. break;
  388. }
  389. }
  390. fill_it:
  391. /*
  392. * i_size must be checked after PageUptodate.
  393. */
  394. isize = i_size_read(mapping->host);
  395. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  396. if (unlikely(!isize || index > end_index))
  397. break;
  398. /*
  399. * if this is the last page, see if we need to shrink
  400. * the length and stop
  401. */
  402. if (end_index == index) {
  403. unsigned int plen;
  404. /*
  405. * max good bytes in this page
  406. */
  407. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  408. if (plen <= loff)
  409. break;
  410. /*
  411. * force quit after adding this page
  412. */
  413. this_len = min(this_len, plen - loff);
  414. len = this_len;
  415. }
  416. spd.partial[page_nr].offset = loff;
  417. spd.partial[page_nr].len = this_len;
  418. len -= this_len;
  419. loff = 0;
  420. spd.nr_pages++;
  421. index++;
  422. }
  423. /*
  424. * Release any pages at the end, if we quit early. 'page_nr' is how far
  425. * we got, 'nr_pages' is how many pages are in the map.
  426. */
  427. while (page_nr < nr_pages)
  428. page_cache_release(spd.pages[page_nr++]);
  429. in->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT;
  430. if (spd.nr_pages)
  431. error = splice_to_pipe(pipe, &spd);
  432. splice_shrink_spd(pipe, &spd);
  433. return error;
  434. }
  435. /**
  436. * generic_file_splice_read - splice data from file to a pipe
  437. * @in: file to splice from
  438. * @ppos: position in @in
  439. * @pipe: pipe to splice to
  440. * @len: number of bytes to splice
  441. * @flags: splice modifier flags
  442. *
  443. * Description:
  444. * Will read pages from given file and fill them into a pipe. Can be
  445. * used as long as the address_space operations for the source implements
  446. * a readpage() hook.
  447. *
  448. */
  449. ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
  450. struct pipe_inode_info *pipe, size_t len,
  451. unsigned int flags)
  452. {
  453. loff_t isize, left;
  454. int ret;
  455. isize = i_size_read(in->f_mapping->host);
  456. if (unlikely(*ppos >= isize))
  457. return 0;
  458. left = isize - *ppos;
  459. if (unlikely(left < len))
  460. len = left;
  461. ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
  462. if (ret > 0) {
  463. *ppos += ret;
  464. file_accessed(in);
  465. }
  466. return ret;
  467. }
  468. EXPORT_SYMBOL(generic_file_splice_read);
  469. static const struct pipe_buf_operations default_pipe_buf_ops = {
  470. .can_merge = 0,
  471. .map = generic_pipe_buf_map,
  472. .unmap = generic_pipe_buf_unmap,
  473. .confirm = generic_pipe_buf_confirm,
  474. .release = generic_pipe_buf_release,
  475. .steal = generic_pipe_buf_steal,
  476. .get = generic_pipe_buf_get,
  477. };
  478. static ssize_t kernel_readv(struct file *file, const struct iovec *vec,
  479. unsigned long vlen, loff_t offset)
  480. {
  481. mm_segment_t old_fs;
  482. loff_t pos = offset;
  483. ssize_t res;
  484. old_fs = get_fs();
  485. set_fs(get_ds());
  486. /* The cast to a user pointer is valid due to the set_fs() */
  487. res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos);
  488. set_fs(old_fs);
  489. return res;
  490. }
  491. static ssize_t kernel_write(struct file *file, const char *buf, size_t count,
  492. loff_t pos)
  493. {
  494. mm_segment_t old_fs;
  495. ssize_t res;
  496. old_fs = get_fs();
  497. set_fs(get_ds());
  498. /* The cast to a user pointer is valid due to the set_fs() */
  499. res = vfs_write(file, (const char __user *)buf, count, &pos);
  500. set_fs(old_fs);
  501. return res;
  502. }
  503. ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
  504. struct pipe_inode_info *pipe, size_t len,
  505. unsigned int flags)
  506. {
  507. unsigned int nr_pages;
  508. unsigned int nr_freed;
  509. size_t offset;
  510. struct page *pages[PIPE_DEF_BUFFERS];
  511. struct partial_page partial[PIPE_DEF_BUFFERS];
  512. struct iovec *vec, __vec[PIPE_DEF_BUFFERS];
  513. ssize_t res;
  514. size_t this_len;
  515. int error;
  516. int i;
  517. struct splice_pipe_desc spd = {
  518. .pages = pages,
  519. .partial = partial,
  520. .flags = flags,
  521. .ops = &default_pipe_buf_ops,
  522. .spd_release = spd_release_page,
  523. };
  524. if (splice_grow_spd(pipe, &spd))
  525. return -ENOMEM;
  526. res = -ENOMEM;
  527. vec = __vec;
  528. if (pipe->buffers > PIPE_DEF_BUFFERS) {
  529. vec = kmalloc(pipe->buffers * sizeof(struct iovec), GFP_KERNEL);
  530. if (!vec)
  531. goto shrink_ret;
  532. }
  533. offset = *ppos & ~PAGE_CACHE_MASK;
  534. nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  535. for (i = 0; i < nr_pages && i < pipe->buffers && len; i++) {
  536. struct page *page;
  537. page = alloc_page(GFP_USER);
  538. error = -ENOMEM;
  539. if (!page)
  540. goto err;
  541. this_len = min_t(size_t, len, PAGE_CACHE_SIZE - offset);
  542. vec[i].iov_base = (void __user *) page_address(page);
  543. vec[i].iov_len = this_len;
  544. spd.pages[i] = page;
  545. spd.nr_pages++;
  546. len -= this_len;
  547. offset = 0;
  548. }
  549. res = kernel_readv(in, vec, spd.nr_pages, *ppos);
  550. if (res < 0) {
  551. error = res;
  552. goto err;
  553. }
  554. error = 0;
  555. if (!res)
  556. goto err;
  557. nr_freed = 0;
  558. for (i = 0; i < spd.nr_pages; i++) {
  559. this_len = min_t(size_t, vec[i].iov_len, res);
  560. spd.partial[i].offset = 0;
  561. spd.partial[i].len = this_len;
  562. if (!this_len) {
  563. __free_page(spd.pages[i]);
  564. spd.pages[i] = NULL;
  565. nr_freed++;
  566. }
  567. res -= this_len;
  568. }
  569. spd.nr_pages -= nr_freed;
  570. res = splice_to_pipe(pipe, &spd);
  571. if (res > 0)
  572. *ppos += res;
  573. shrink_ret:
  574. if (vec != __vec)
  575. kfree(vec);
  576. splice_shrink_spd(pipe, &spd);
  577. return res;
  578. err:
  579. for (i = 0; i < spd.nr_pages; i++)
  580. __free_page(spd.pages[i]);
  581. res = error;
  582. goto shrink_ret;
  583. }
  584. EXPORT_SYMBOL(default_file_splice_read);
  585. /*
  586. * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
  587. * using sendpage(). Return the number of bytes sent.
  588. */
  589. static int pipe_to_sendpage(struct pipe_inode_info *pipe,
  590. struct pipe_buffer *buf, struct splice_desc *sd)
  591. {
  592. struct file *file = sd->u.file;
  593. loff_t pos = sd->pos;
  594. int more;
  595. if (!likely(file->f_op && file->f_op->sendpage))
  596. return -EINVAL;
  597. more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
  598. if (sd->len < sd->total_len)
  599. more |= MSG_SENDPAGE_NOTLAST;
  600. return file->f_op->sendpage(file, buf->page, buf->offset,
  601. sd->len, &pos, more);
  602. }
  603. /*
  604. * This is a little more tricky than the file -> pipe splicing. There are
  605. * basically three cases:
  606. *
  607. * - Destination page already exists in the address space and there
  608. * are users of it. For that case we have no other option that
  609. * copying the data. Tough luck.
  610. * - Destination page already exists in the address space, but there
  611. * are no users of it. Make sure it's uptodate, then drop it. Fall
  612. * through to last case.
  613. * - Destination page does not exist, we can add the pipe page to
  614. * the page cache and avoid the copy.
  615. *
  616. * If asked to move pages to the output file (SPLICE_F_MOVE is set in
  617. * sd->flags), we attempt to migrate pages from the pipe to the output
  618. * file address space page cache. This is possible if no one else has
  619. * the pipe page referenced outside of the pipe and page cache. If
  620. * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
  621. * a new page in the output file page cache and fill/dirty that.
  622. */
  623. int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  624. struct splice_desc *sd)
  625. {
  626. struct file *file = sd->u.file;
  627. struct address_space *mapping = file->f_mapping;
  628. unsigned int offset, this_len;
  629. struct page *page;
  630. void *fsdata;
  631. int ret;
  632. offset = sd->pos & ~PAGE_CACHE_MASK;
  633. this_len = sd->len;
  634. if (this_len + offset > PAGE_CACHE_SIZE)
  635. this_len = PAGE_CACHE_SIZE - offset;
  636. ret = pagecache_write_begin(file, mapping, sd->pos, this_len,
  637. AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
  638. if (unlikely(ret))
  639. goto out;
  640. if (buf->page != page) {
  641. char *src = buf->ops->map(pipe, buf, 1);
  642. char *dst = kmap_atomic(page);
  643. memcpy(dst + offset, src + buf->offset, this_len);
  644. flush_dcache_page(page);
  645. kunmap_atomic(dst);
  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. }