splice.c 46 KB

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