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