splice.c 47 KB

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