splice.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  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. return ret;
  444. }
  445. EXPORT_SYMBOL(generic_file_splice_read);
  446. /*
  447. * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
  448. * using sendpage(). Return the number of bytes sent.
  449. */
  450. static int pipe_to_sendpage(struct pipe_inode_info *pipe,
  451. struct pipe_buffer *buf, struct splice_desc *sd)
  452. {
  453. struct file *file = sd->u.file;
  454. loff_t pos = sd->pos;
  455. int ret, more;
  456. ret = buf->ops->confirm(pipe, buf);
  457. if (!ret) {
  458. more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
  459. ret = file->f_op->sendpage(file, buf->page, buf->offset,
  460. sd->len, &pos, more);
  461. }
  462. return ret;
  463. }
  464. /*
  465. * This is a little more tricky than the file -> pipe splicing. There are
  466. * basically three cases:
  467. *
  468. * - Destination page already exists in the address space and there
  469. * are users of it. For that case we have no other option that
  470. * copying the data. Tough luck.
  471. * - Destination page already exists in the address space, but there
  472. * are no users of it. Make sure it's uptodate, then drop it. Fall
  473. * through to last case.
  474. * - Destination page does not exist, we can add the pipe page to
  475. * the page cache and avoid the copy.
  476. *
  477. * If asked to move pages to the output file (SPLICE_F_MOVE is set in
  478. * sd->flags), we attempt to migrate pages from the pipe to the output
  479. * file address space page cache. This is possible if no one else has
  480. * the pipe page referenced outside of the pipe and page cache. If
  481. * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
  482. * a new page in the output file page cache and fill/dirty that.
  483. */
  484. int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  485. struct splice_desc *sd)
  486. {
  487. struct file *file = sd->u.file;
  488. struct address_space *mapping = file->f_mapping;
  489. unsigned int offset, this_len;
  490. struct page *page;
  491. void *fsdata;
  492. int ret;
  493. /*
  494. * make sure the data in this buffer is uptodate
  495. */
  496. ret = buf->ops->confirm(pipe, buf);
  497. if (unlikely(ret))
  498. return ret;
  499. offset = sd->pos & ~PAGE_CACHE_MASK;
  500. this_len = sd->len;
  501. if (this_len + offset > PAGE_CACHE_SIZE)
  502. this_len = PAGE_CACHE_SIZE - offset;
  503. ret = pagecache_write_begin(file, mapping, sd->pos, this_len,
  504. AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
  505. if (unlikely(ret))
  506. goto out;
  507. if (buf->page != page) {
  508. /*
  509. * Careful, ->map() uses KM_USER0!
  510. */
  511. char *src = buf->ops->map(pipe, buf, 1);
  512. char *dst = kmap_atomic(page, KM_USER1);
  513. memcpy(dst + offset, src + buf->offset, this_len);
  514. flush_dcache_page(page);
  515. kunmap_atomic(dst, KM_USER1);
  516. buf->ops->unmap(pipe, buf, src);
  517. }
  518. ret = pagecache_write_end(file, mapping, sd->pos, this_len, this_len,
  519. page, fsdata);
  520. out:
  521. return ret;
  522. }
  523. EXPORT_SYMBOL(pipe_to_file);
  524. static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
  525. {
  526. smp_mb();
  527. if (waitqueue_active(&pipe->wait))
  528. wake_up_interruptible(&pipe->wait);
  529. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  530. }
  531. /**
  532. * splice_from_pipe_feed - feed available data from a pipe to a file
  533. * @pipe: pipe to splice from
  534. * @sd: information to @actor
  535. * @actor: handler that splices the data
  536. *
  537. * Description:
  538. * This function loops over the pipe and calls @actor to do the
  539. * actual moving of a single struct pipe_buffer to the desired
  540. * destination. It returns when there's no more buffers left in
  541. * the pipe or if the requested number of bytes (@sd->total_len)
  542. * have been copied. It returns a positive number (one) if the
  543. * pipe needs to be filled with more data, zero if the required
  544. * number of bytes have been copied and -errno on error.
  545. *
  546. * This, together with splice_from_pipe_{begin,end,next}, may be
  547. * used to implement the functionality of __splice_from_pipe() when
  548. * locking is required around copying the pipe buffers to the
  549. * destination.
  550. */
  551. int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
  552. splice_actor *actor)
  553. {
  554. int ret;
  555. while (pipe->nrbufs) {
  556. struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  557. const struct pipe_buf_operations *ops = buf->ops;
  558. sd->len = buf->len;
  559. if (sd->len > sd->total_len)
  560. sd->len = sd->total_len;
  561. ret = actor(pipe, buf, sd);
  562. if (ret <= 0) {
  563. if (ret == -ENODATA)
  564. ret = 0;
  565. return ret;
  566. }
  567. buf->offset += ret;
  568. buf->len -= ret;
  569. sd->num_spliced += ret;
  570. sd->len -= ret;
  571. sd->pos += ret;
  572. sd->total_len -= ret;
  573. if (!buf->len) {
  574. buf->ops = NULL;
  575. ops->release(pipe, buf);
  576. pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
  577. pipe->nrbufs--;
  578. if (pipe->inode)
  579. sd->need_wakeup = true;
  580. }
  581. if (!sd->total_len)
  582. return 0;
  583. }
  584. return 1;
  585. }
  586. EXPORT_SYMBOL(splice_from_pipe_feed);
  587. /**
  588. * splice_from_pipe_next - wait for some data to splice from
  589. * @pipe: pipe to splice from
  590. * @sd: information about the splice operation
  591. *
  592. * Description:
  593. * This function will wait for some data and return a positive
  594. * value (one) if pipe buffers are available. It will return zero
  595. * or -errno if no more data needs to be spliced.
  596. */
  597. int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
  598. {
  599. while (!pipe->nrbufs) {
  600. if (!pipe->writers)
  601. return 0;
  602. if (!pipe->waiting_writers && sd->num_spliced)
  603. return 0;
  604. if (sd->flags & SPLICE_F_NONBLOCK)
  605. return -EAGAIN;
  606. if (signal_pending(current))
  607. return -ERESTARTSYS;
  608. if (sd->need_wakeup) {
  609. wakeup_pipe_writers(pipe);
  610. sd->need_wakeup = false;
  611. }
  612. pipe_wait(pipe);
  613. }
  614. return 1;
  615. }
  616. EXPORT_SYMBOL(splice_from_pipe_next);
  617. /**
  618. * splice_from_pipe_begin - start splicing from pipe
  619. * @sd: information about the splice operation
  620. *
  621. * Description:
  622. * This function should be called before a loop containing
  623. * splice_from_pipe_next() and splice_from_pipe_feed() to
  624. * initialize the necessary fields of @sd.
  625. */
  626. void splice_from_pipe_begin(struct splice_desc *sd)
  627. {
  628. sd->num_spliced = 0;
  629. sd->need_wakeup = false;
  630. }
  631. EXPORT_SYMBOL(splice_from_pipe_begin);
  632. /**
  633. * splice_from_pipe_end - finish splicing from pipe
  634. * @pipe: pipe to splice from
  635. * @sd: information about the splice operation
  636. *
  637. * Description:
  638. * This function will wake up pipe writers if necessary. It should
  639. * be called after a loop containing splice_from_pipe_next() and
  640. * splice_from_pipe_feed().
  641. */
  642. void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
  643. {
  644. if (sd->need_wakeup)
  645. wakeup_pipe_writers(pipe);
  646. }
  647. EXPORT_SYMBOL(splice_from_pipe_end);
  648. /**
  649. * __splice_from_pipe - splice data from a pipe to given actor
  650. * @pipe: pipe to splice from
  651. * @sd: information to @actor
  652. * @actor: handler that splices the data
  653. *
  654. * Description:
  655. * This function does little more than loop over the pipe and call
  656. * @actor to do the actual moving of a single struct pipe_buffer to
  657. * the desired destination. See pipe_to_file, pipe_to_sendpage, or
  658. * pipe_to_user.
  659. *
  660. */
  661. ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
  662. splice_actor *actor)
  663. {
  664. int ret;
  665. splice_from_pipe_begin(sd);
  666. do {
  667. ret = splice_from_pipe_next(pipe, sd);
  668. if (ret > 0)
  669. ret = splice_from_pipe_feed(pipe, sd, actor);
  670. } while (ret > 0);
  671. splice_from_pipe_end(pipe, sd);
  672. return sd->num_spliced ? sd->num_spliced : ret;
  673. }
  674. EXPORT_SYMBOL(__splice_from_pipe);
  675. /**
  676. * splice_from_pipe - splice data from a pipe to a file
  677. * @pipe: pipe to splice from
  678. * @out: file to splice to
  679. * @ppos: position in @out
  680. * @len: how many bytes to splice
  681. * @flags: splice modifier flags
  682. * @actor: handler that splices the data
  683. *
  684. * Description:
  685. * See __splice_from_pipe. This function locks the pipe inode,
  686. * otherwise it's identical to __splice_from_pipe().
  687. *
  688. */
  689. ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
  690. loff_t *ppos, size_t len, unsigned int flags,
  691. splice_actor *actor)
  692. {
  693. ssize_t ret;
  694. struct splice_desc sd = {
  695. .total_len = len,
  696. .flags = flags,
  697. .pos = *ppos,
  698. .u.file = out,
  699. };
  700. pipe_lock(pipe);
  701. ret = __splice_from_pipe(pipe, &sd, actor);
  702. pipe_unlock(pipe);
  703. return ret;
  704. }
  705. /**
  706. * generic_file_splice_write - splice data from a pipe to a file
  707. * @pipe: pipe info
  708. * @out: file to write to
  709. * @ppos: position in @out
  710. * @len: number of bytes to splice
  711. * @flags: splice modifier flags
  712. *
  713. * Description:
  714. * Will either move or copy pages (determined by @flags options) from
  715. * the given pipe inode to the given file.
  716. *
  717. */
  718. ssize_t
  719. generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  720. loff_t *ppos, size_t len, unsigned int flags)
  721. {
  722. struct address_space *mapping = out->f_mapping;
  723. struct inode *inode = mapping->host;
  724. struct splice_desc sd = {
  725. .total_len = len,
  726. .flags = flags,
  727. .pos = *ppos,
  728. .u.file = out,
  729. };
  730. ssize_t ret;
  731. pipe_lock(pipe);
  732. splice_from_pipe_begin(&sd);
  733. do {
  734. ret = splice_from_pipe_next(pipe, &sd);
  735. if (ret <= 0)
  736. break;
  737. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  738. ret = file_remove_suid(out);
  739. if (!ret)
  740. ret = splice_from_pipe_feed(pipe, &sd, pipe_to_file);
  741. mutex_unlock(&inode->i_mutex);
  742. } while (ret > 0);
  743. splice_from_pipe_end(pipe, &sd);
  744. pipe_unlock(pipe);
  745. if (sd.num_spliced)
  746. ret = sd.num_spliced;
  747. if (ret > 0) {
  748. unsigned long nr_pages;
  749. *ppos += ret;
  750. nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  751. /*
  752. * If file or inode is SYNC and we actually wrote some data,
  753. * sync it.
  754. */
  755. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  756. int err;
  757. mutex_lock(&inode->i_mutex);
  758. err = generic_osync_inode(inode, mapping,
  759. OSYNC_METADATA|OSYNC_DATA);
  760. mutex_unlock(&inode->i_mutex);
  761. if (err)
  762. ret = err;
  763. }
  764. balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
  765. }
  766. return ret;
  767. }
  768. EXPORT_SYMBOL(generic_file_splice_write);
  769. /**
  770. * generic_splice_sendpage - splice data from a pipe to a socket
  771. * @pipe: pipe to splice from
  772. * @out: socket to write to
  773. * @ppos: position in @out
  774. * @len: number of bytes to splice
  775. * @flags: splice modifier flags
  776. *
  777. * Description:
  778. * Will send @len bytes from the pipe to a network socket. No data copying
  779. * is involved.
  780. *
  781. */
  782. ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
  783. loff_t *ppos, size_t len, unsigned int flags)
  784. {
  785. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
  786. }
  787. EXPORT_SYMBOL(generic_splice_sendpage);
  788. /*
  789. * Attempt to initiate a splice from pipe to file.
  790. */
  791. static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  792. loff_t *ppos, size_t len, unsigned int flags)
  793. {
  794. int ret;
  795. if (unlikely(!out->f_op || !out->f_op->splice_write))
  796. return -EINVAL;
  797. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  798. return -EBADF;
  799. if (unlikely(out->f_flags & O_APPEND))
  800. return -EINVAL;
  801. ret = rw_verify_area(WRITE, out, ppos, len);
  802. if (unlikely(ret < 0))
  803. return ret;
  804. return out->f_op->splice_write(pipe, out, ppos, len, flags);
  805. }
  806. /*
  807. * Attempt to initiate a splice from a file to a pipe.
  808. */
  809. static long do_splice_to(struct file *in, loff_t *ppos,
  810. struct pipe_inode_info *pipe, size_t len,
  811. unsigned int flags)
  812. {
  813. int ret;
  814. if (unlikely(!in->f_op || !in->f_op->splice_read))
  815. return -EINVAL;
  816. if (unlikely(!(in->f_mode & FMODE_READ)))
  817. return -EBADF;
  818. ret = rw_verify_area(READ, in, ppos, len);
  819. if (unlikely(ret < 0))
  820. return ret;
  821. return in->f_op->splice_read(in, ppos, pipe, len, flags);
  822. }
  823. /**
  824. * splice_direct_to_actor - splices data directly between two non-pipes
  825. * @in: file to splice from
  826. * @sd: actor information on where to splice to
  827. * @actor: handles the data splicing
  828. *
  829. * Description:
  830. * This is a special case helper to splice directly between two
  831. * points, without requiring an explicit pipe. Internally an allocated
  832. * pipe is cached in the process, and reused during the lifetime of
  833. * that process.
  834. *
  835. */
  836. ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
  837. splice_direct_actor *actor)
  838. {
  839. struct pipe_inode_info *pipe;
  840. long ret, bytes;
  841. umode_t i_mode;
  842. size_t len;
  843. int i, flags;
  844. /*
  845. * We require the input being a regular file, as we don't want to
  846. * randomly drop data for eg socket -> socket splicing. Use the
  847. * piped splicing for that!
  848. */
  849. i_mode = in->f_path.dentry->d_inode->i_mode;
  850. if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  851. return -EINVAL;
  852. /*
  853. * neither in nor out is a pipe, setup an internal pipe attached to
  854. * 'out' and transfer the wanted data from 'in' to 'out' through that
  855. */
  856. pipe = current->splice_pipe;
  857. if (unlikely(!pipe)) {
  858. pipe = alloc_pipe_info(NULL);
  859. if (!pipe)
  860. return -ENOMEM;
  861. /*
  862. * We don't have an immediate reader, but we'll read the stuff
  863. * out of the pipe right after the splice_to_pipe(). So set
  864. * PIPE_READERS appropriately.
  865. */
  866. pipe->readers = 1;
  867. current->splice_pipe = pipe;
  868. }
  869. /*
  870. * Do the splice.
  871. */
  872. ret = 0;
  873. bytes = 0;
  874. len = sd->total_len;
  875. flags = sd->flags;
  876. /*
  877. * Don't block on output, we have to drain the direct pipe.
  878. */
  879. sd->flags &= ~SPLICE_F_NONBLOCK;
  880. while (len) {
  881. size_t read_len;
  882. loff_t pos = sd->pos, prev_pos = pos;
  883. ret = do_splice_to(in, &pos, pipe, len, flags);
  884. if (unlikely(ret <= 0))
  885. goto out_release;
  886. read_len = ret;
  887. sd->total_len = read_len;
  888. /*
  889. * NOTE: nonblocking mode only applies to the input. We
  890. * must not do the output in nonblocking mode as then we
  891. * could get stuck data in the internal pipe:
  892. */
  893. ret = actor(pipe, sd);
  894. if (unlikely(ret <= 0)) {
  895. sd->pos = prev_pos;
  896. goto out_release;
  897. }
  898. bytes += ret;
  899. len -= ret;
  900. sd->pos = pos;
  901. if (ret < read_len) {
  902. sd->pos = prev_pos + ret;
  903. goto out_release;
  904. }
  905. }
  906. done:
  907. pipe->nrbufs = pipe->curbuf = 0;
  908. file_accessed(in);
  909. return bytes;
  910. out_release:
  911. /*
  912. * If we did an incomplete transfer we must release
  913. * the pipe buffers in question:
  914. */
  915. for (i = 0; i < PIPE_BUFFERS; i++) {
  916. struct pipe_buffer *buf = pipe->bufs + i;
  917. if (buf->ops) {
  918. buf->ops->release(pipe, buf);
  919. buf->ops = NULL;
  920. }
  921. }
  922. if (!bytes)
  923. bytes = ret;
  924. goto done;
  925. }
  926. EXPORT_SYMBOL(splice_direct_to_actor);
  927. static int direct_splice_actor(struct pipe_inode_info *pipe,
  928. struct splice_desc *sd)
  929. {
  930. struct file *file = sd->u.file;
  931. return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
  932. }
  933. /**
  934. * do_splice_direct - splices data directly between two files
  935. * @in: file to splice from
  936. * @ppos: input file offset
  937. * @out: file to splice to
  938. * @len: number of bytes to splice
  939. * @flags: splice modifier flags
  940. *
  941. * Description:
  942. * For use by do_sendfile(). splice can easily emulate sendfile, but
  943. * doing it in the application would incur an extra system call
  944. * (splice in + splice out, as compared to just sendfile()). So this helper
  945. * can splice directly through a process-private pipe.
  946. *
  947. */
  948. long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
  949. size_t len, unsigned int flags)
  950. {
  951. struct splice_desc sd = {
  952. .len = len,
  953. .total_len = len,
  954. .flags = flags,
  955. .pos = *ppos,
  956. .u.file = out,
  957. };
  958. long ret;
  959. ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
  960. if (ret > 0)
  961. *ppos = sd.pos;
  962. return ret;
  963. }
  964. static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  965. struct pipe_inode_info *opipe,
  966. size_t len, unsigned int flags);
  967. /*
  968. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  969. * location, so checking ->i_pipe is not enough to verify that this is a
  970. * pipe.
  971. */
  972. static inline struct pipe_inode_info *pipe_info(struct inode *inode)
  973. {
  974. if (S_ISFIFO(inode->i_mode))
  975. return inode->i_pipe;
  976. return NULL;
  977. }
  978. /*
  979. * Determine where to splice to/from.
  980. */
  981. static long do_splice(struct file *in, loff_t __user *off_in,
  982. struct file *out, loff_t __user *off_out,
  983. size_t len, unsigned int flags)
  984. {
  985. struct pipe_inode_info *ipipe;
  986. struct pipe_inode_info *opipe;
  987. loff_t offset, *off;
  988. long ret;
  989. ipipe = pipe_info(in->f_path.dentry->d_inode);
  990. opipe = pipe_info(out->f_path.dentry->d_inode);
  991. if (ipipe && opipe) {
  992. if (off_in || off_out)
  993. return -ESPIPE;
  994. if (!(in->f_mode & FMODE_READ))
  995. return -EBADF;
  996. if (!(out->f_mode & FMODE_WRITE))
  997. return -EBADF;
  998. /* Splicing to self would be fun, but... */
  999. if (ipipe == opipe)
  1000. return -EINVAL;
  1001. return splice_pipe_to_pipe(ipipe, opipe, len, flags);
  1002. }
  1003. if (ipipe) {
  1004. if (off_in)
  1005. return -ESPIPE;
  1006. if (off_out) {
  1007. if (out->f_op->llseek == no_llseek)
  1008. return -EINVAL;
  1009. if (copy_from_user(&offset, off_out, sizeof(loff_t)))
  1010. return -EFAULT;
  1011. off = &offset;
  1012. } else
  1013. off = &out->f_pos;
  1014. ret = do_splice_from(ipipe, out, off, len, flags);
  1015. if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
  1016. ret = -EFAULT;
  1017. return ret;
  1018. }
  1019. if (opipe) {
  1020. if (off_out)
  1021. return -ESPIPE;
  1022. if (off_in) {
  1023. if (in->f_op->llseek == no_llseek)
  1024. return -EINVAL;
  1025. if (copy_from_user(&offset, off_in, sizeof(loff_t)))
  1026. return -EFAULT;
  1027. off = &offset;
  1028. } else
  1029. off = &in->f_pos;
  1030. ret = do_splice_to(in, off, opipe, len, flags);
  1031. if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
  1032. ret = -EFAULT;
  1033. return ret;
  1034. }
  1035. return -EINVAL;
  1036. }
  1037. /*
  1038. * Map an iov into an array of pages and offset/length tupples. With the
  1039. * partial_page structure, we can map several non-contiguous ranges into
  1040. * our ones pages[] map instead of splitting that operation into pieces.
  1041. * Could easily be exported as a generic helper for other users, in which
  1042. * case one would probably want to add a 'max_nr_pages' parameter as well.
  1043. */
  1044. static int get_iovec_page_array(const struct iovec __user *iov,
  1045. unsigned int nr_vecs, struct page **pages,
  1046. struct partial_page *partial, int aligned)
  1047. {
  1048. int buffers = 0, error = 0;
  1049. while (nr_vecs) {
  1050. unsigned long off, npages;
  1051. struct iovec entry;
  1052. void __user *base;
  1053. size_t len;
  1054. int i;
  1055. error = -EFAULT;
  1056. if (copy_from_user(&entry, iov, sizeof(entry)))
  1057. break;
  1058. base = entry.iov_base;
  1059. len = entry.iov_len;
  1060. /*
  1061. * Sanity check this iovec. 0 read succeeds.
  1062. */
  1063. error = 0;
  1064. if (unlikely(!len))
  1065. break;
  1066. error = -EFAULT;
  1067. if (!access_ok(VERIFY_READ, base, len))
  1068. break;
  1069. /*
  1070. * Get this base offset and number of pages, then map
  1071. * in the user pages.
  1072. */
  1073. off = (unsigned long) base & ~PAGE_MASK;
  1074. /*
  1075. * If asked for alignment, the offset must be zero and the
  1076. * length a multiple of the PAGE_SIZE.
  1077. */
  1078. error = -EINVAL;
  1079. if (aligned && (off || len & ~PAGE_MASK))
  1080. break;
  1081. npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1082. if (npages > PIPE_BUFFERS - buffers)
  1083. npages = PIPE_BUFFERS - buffers;
  1084. error = get_user_pages_fast((unsigned long)base, npages,
  1085. 0, &pages[buffers]);
  1086. if (unlikely(error <= 0))
  1087. break;
  1088. /*
  1089. * Fill this contiguous range into the partial page map.
  1090. */
  1091. for (i = 0; i < error; i++) {
  1092. const int plen = min_t(size_t, len, PAGE_SIZE - off);
  1093. partial[buffers].offset = off;
  1094. partial[buffers].len = plen;
  1095. off = 0;
  1096. len -= plen;
  1097. buffers++;
  1098. }
  1099. /*
  1100. * We didn't complete this iov, stop here since it probably
  1101. * means we have to move some of this into a pipe to
  1102. * be able to continue.
  1103. */
  1104. if (len)
  1105. break;
  1106. /*
  1107. * Don't continue if we mapped fewer pages than we asked for,
  1108. * or if we mapped the max number of pages that we have
  1109. * room for.
  1110. */
  1111. if (error < npages || buffers == PIPE_BUFFERS)
  1112. break;
  1113. nr_vecs--;
  1114. iov++;
  1115. }
  1116. if (buffers)
  1117. return buffers;
  1118. return error;
  1119. }
  1120. static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  1121. struct splice_desc *sd)
  1122. {
  1123. char *src;
  1124. int ret;
  1125. ret = buf->ops->confirm(pipe, buf);
  1126. if (unlikely(ret))
  1127. return ret;
  1128. /*
  1129. * See if we can use the atomic maps, by prefaulting in the
  1130. * pages and doing an atomic copy
  1131. */
  1132. if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) {
  1133. src = buf->ops->map(pipe, buf, 1);
  1134. ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset,
  1135. sd->len);
  1136. buf->ops->unmap(pipe, buf, src);
  1137. if (!ret) {
  1138. ret = sd->len;
  1139. goto out;
  1140. }
  1141. }
  1142. /*
  1143. * No dice, use slow non-atomic map and copy
  1144. */
  1145. src = buf->ops->map(pipe, buf, 0);
  1146. ret = sd->len;
  1147. if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len))
  1148. ret = -EFAULT;
  1149. buf->ops->unmap(pipe, buf, src);
  1150. out:
  1151. if (ret > 0)
  1152. sd->u.userptr += ret;
  1153. return ret;
  1154. }
  1155. /*
  1156. * For lack of a better implementation, implement vmsplice() to userspace
  1157. * as a simple copy of the pipes pages to the user iov.
  1158. */
  1159. static long vmsplice_to_user(struct file *file, const struct iovec __user *iov,
  1160. unsigned long nr_segs, unsigned int flags)
  1161. {
  1162. struct pipe_inode_info *pipe;
  1163. struct splice_desc sd;
  1164. ssize_t size;
  1165. int error;
  1166. long ret;
  1167. pipe = pipe_info(file->f_path.dentry->d_inode);
  1168. if (!pipe)
  1169. return -EBADF;
  1170. pipe_lock(pipe);
  1171. error = ret = 0;
  1172. while (nr_segs) {
  1173. void __user *base;
  1174. size_t len;
  1175. /*
  1176. * Get user address base and length for this iovec.
  1177. */
  1178. error = get_user(base, &iov->iov_base);
  1179. if (unlikely(error))
  1180. break;
  1181. error = get_user(len, &iov->iov_len);
  1182. if (unlikely(error))
  1183. break;
  1184. /*
  1185. * Sanity check this iovec. 0 read succeeds.
  1186. */
  1187. if (unlikely(!len))
  1188. break;
  1189. if (unlikely(!base)) {
  1190. error = -EFAULT;
  1191. break;
  1192. }
  1193. if (unlikely(!access_ok(VERIFY_WRITE, base, len))) {
  1194. error = -EFAULT;
  1195. break;
  1196. }
  1197. sd.len = 0;
  1198. sd.total_len = len;
  1199. sd.flags = flags;
  1200. sd.u.userptr = base;
  1201. sd.pos = 0;
  1202. size = __splice_from_pipe(pipe, &sd, pipe_to_user);
  1203. if (size < 0) {
  1204. if (!ret)
  1205. ret = size;
  1206. break;
  1207. }
  1208. ret += size;
  1209. if (size < len)
  1210. break;
  1211. nr_segs--;
  1212. iov++;
  1213. }
  1214. pipe_unlock(pipe);
  1215. if (!ret)
  1216. ret = error;
  1217. return ret;
  1218. }
  1219. /*
  1220. * vmsplice splices a user address range into a pipe. It can be thought of
  1221. * as splice-from-memory, where the regular splice is splice-from-file (or
  1222. * to file). In both cases the output is a pipe, naturally.
  1223. */
  1224. static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
  1225. unsigned long nr_segs, unsigned int flags)
  1226. {
  1227. struct pipe_inode_info *pipe;
  1228. struct page *pages[PIPE_BUFFERS];
  1229. struct partial_page partial[PIPE_BUFFERS];
  1230. struct splice_pipe_desc spd = {
  1231. .pages = pages,
  1232. .partial = partial,
  1233. .flags = flags,
  1234. .ops = &user_page_pipe_buf_ops,
  1235. .spd_release = spd_release_page,
  1236. };
  1237. pipe = pipe_info(file->f_path.dentry->d_inode);
  1238. if (!pipe)
  1239. return -EBADF;
  1240. spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
  1241. flags & SPLICE_F_GIFT);
  1242. if (spd.nr_pages <= 0)
  1243. return spd.nr_pages;
  1244. return splice_to_pipe(pipe, &spd);
  1245. }
  1246. /*
  1247. * Note that vmsplice only really supports true splicing _from_ user memory
  1248. * to a pipe, not the other way around. Splicing from user memory is a simple
  1249. * operation that can be supported without any funky alignment restrictions
  1250. * or nasty vm tricks. We simply map in the user memory and fill them into
  1251. * a pipe. The reverse isn't quite as easy, though. There are two possible
  1252. * solutions for that:
  1253. *
  1254. * - memcpy() the data internally, at which point we might as well just
  1255. * do a regular read() on the buffer anyway.
  1256. * - Lots of nasty vm tricks, that are neither fast nor flexible (it
  1257. * has restriction limitations on both ends of the pipe).
  1258. *
  1259. * Currently we punt and implement it as a normal copy, see pipe_to_user().
  1260. *
  1261. */
  1262. SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
  1263. unsigned long, nr_segs, unsigned int, flags)
  1264. {
  1265. struct file *file;
  1266. long error;
  1267. int fput;
  1268. if (unlikely(nr_segs > UIO_MAXIOV))
  1269. return -EINVAL;
  1270. else if (unlikely(!nr_segs))
  1271. return 0;
  1272. error = -EBADF;
  1273. file = fget_light(fd, &fput);
  1274. if (file) {
  1275. if (file->f_mode & FMODE_WRITE)
  1276. error = vmsplice_to_pipe(file, iov, nr_segs, flags);
  1277. else if (file->f_mode & FMODE_READ)
  1278. error = vmsplice_to_user(file, iov, nr_segs, flags);
  1279. fput_light(file, fput);
  1280. }
  1281. return error;
  1282. }
  1283. SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
  1284. int, fd_out, loff_t __user *, off_out,
  1285. size_t, len, unsigned int, flags)
  1286. {
  1287. long error;
  1288. struct file *in, *out;
  1289. int fput_in, fput_out;
  1290. if (unlikely(!len))
  1291. return 0;
  1292. error = -EBADF;
  1293. in = fget_light(fd_in, &fput_in);
  1294. if (in) {
  1295. if (in->f_mode & FMODE_READ) {
  1296. out = fget_light(fd_out, &fput_out);
  1297. if (out) {
  1298. if (out->f_mode & FMODE_WRITE)
  1299. error = do_splice(in, off_in,
  1300. out, off_out,
  1301. len, flags);
  1302. fput_light(out, fput_out);
  1303. }
  1304. }
  1305. fput_light(in, fput_in);
  1306. }
  1307. return error;
  1308. }
  1309. /*
  1310. * Make sure there's data to read. Wait for input if we can, otherwise
  1311. * return an appropriate error.
  1312. */
  1313. static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1314. {
  1315. int ret;
  1316. /*
  1317. * Check ->nrbufs without the inode lock first. This function
  1318. * is speculative anyways, so missing one is ok.
  1319. */
  1320. if (pipe->nrbufs)
  1321. return 0;
  1322. ret = 0;
  1323. pipe_lock(pipe);
  1324. while (!pipe->nrbufs) {
  1325. if (signal_pending(current)) {
  1326. ret = -ERESTARTSYS;
  1327. break;
  1328. }
  1329. if (!pipe->writers)
  1330. break;
  1331. if (!pipe->waiting_writers) {
  1332. if (flags & SPLICE_F_NONBLOCK) {
  1333. ret = -EAGAIN;
  1334. break;
  1335. }
  1336. }
  1337. pipe_wait(pipe);
  1338. }
  1339. pipe_unlock(pipe);
  1340. return ret;
  1341. }
  1342. /*
  1343. * Make sure there's writeable room. Wait for room if we can, otherwise
  1344. * return an appropriate error.
  1345. */
  1346. static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1347. {
  1348. int ret;
  1349. /*
  1350. * Check ->nrbufs without the inode lock first. This function
  1351. * is speculative anyways, so missing one is ok.
  1352. */
  1353. if (pipe->nrbufs < PIPE_BUFFERS)
  1354. return 0;
  1355. ret = 0;
  1356. pipe_lock(pipe);
  1357. while (pipe->nrbufs >= PIPE_BUFFERS) {
  1358. if (!pipe->readers) {
  1359. send_sig(SIGPIPE, current, 0);
  1360. ret = -EPIPE;
  1361. break;
  1362. }
  1363. if (flags & SPLICE_F_NONBLOCK) {
  1364. ret = -EAGAIN;
  1365. break;
  1366. }
  1367. if (signal_pending(current)) {
  1368. ret = -ERESTARTSYS;
  1369. break;
  1370. }
  1371. pipe->waiting_writers++;
  1372. pipe_wait(pipe);
  1373. pipe->waiting_writers--;
  1374. }
  1375. pipe_unlock(pipe);
  1376. return ret;
  1377. }
  1378. /*
  1379. * Splice contents of ipipe to opipe.
  1380. */
  1381. static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  1382. struct pipe_inode_info *opipe,
  1383. size_t len, unsigned int flags)
  1384. {
  1385. struct pipe_buffer *ibuf, *obuf;
  1386. int ret = 0, nbuf;
  1387. bool input_wakeup = false;
  1388. retry:
  1389. ret = ipipe_prep(ipipe, flags);
  1390. if (ret)
  1391. return ret;
  1392. ret = opipe_prep(opipe, flags);
  1393. if (ret)
  1394. return ret;
  1395. /*
  1396. * Potential ABBA deadlock, work around it by ordering lock
  1397. * grabbing by pipe info address. Otherwise two different processes
  1398. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1399. */
  1400. pipe_double_lock(ipipe, opipe);
  1401. do {
  1402. if (!opipe->readers) {
  1403. send_sig(SIGPIPE, current, 0);
  1404. if (!ret)
  1405. ret = -EPIPE;
  1406. break;
  1407. }
  1408. if (!ipipe->nrbufs && !ipipe->writers)
  1409. break;
  1410. /*
  1411. * Cannot make any progress, because either the input
  1412. * pipe is empty or the output pipe is full.
  1413. */
  1414. if (!ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS) {
  1415. /* Already processed some buffers, break */
  1416. if (ret)
  1417. break;
  1418. if (flags & SPLICE_F_NONBLOCK) {
  1419. ret = -EAGAIN;
  1420. break;
  1421. }
  1422. /*
  1423. * We raced with another reader/writer and haven't
  1424. * managed to process any buffers. A zero return
  1425. * value means EOF, so retry instead.
  1426. */
  1427. pipe_unlock(ipipe);
  1428. pipe_unlock(opipe);
  1429. goto retry;
  1430. }
  1431. ibuf = ipipe->bufs + ipipe->curbuf;
  1432. nbuf = (opipe->curbuf + opipe->nrbufs) % PIPE_BUFFERS;
  1433. obuf = opipe->bufs + nbuf;
  1434. if (len >= ibuf->len) {
  1435. /*
  1436. * Simply move the whole buffer from ipipe to opipe
  1437. */
  1438. *obuf = *ibuf;
  1439. ibuf->ops = NULL;
  1440. opipe->nrbufs++;
  1441. ipipe->curbuf = (ipipe->curbuf + 1) % PIPE_BUFFERS;
  1442. ipipe->nrbufs--;
  1443. input_wakeup = true;
  1444. } else {
  1445. /*
  1446. * Get a reference to this pipe buffer,
  1447. * so we can copy the contents over.
  1448. */
  1449. ibuf->ops->get(ipipe, ibuf);
  1450. *obuf = *ibuf;
  1451. /*
  1452. * Don't inherit the gift flag, we need to
  1453. * prevent multiple steals of this page.
  1454. */
  1455. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1456. obuf->len = len;
  1457. opipe->nrbufs++;
  1458. ibuf->offset += obuf->len;
  1459. ibuf->len -= obuf->len;
  1460. }
  1461. ret += obuf->len;
  1462. len -= obuf->len;
  1463. } while (len);
  1464. pipe_unlock(ipipe);
  1465. pipe_unlock(opipe);
  1466. /*
  1467. * If we put data in the output pipe, wakeup any potential readers.
  1468. */
  1469. if (ret > 0) {
  1470. smp_mb();
  1471. if (waitqueue_active(&opipe->wait))
  1472. wake_up_interruptible(&opipe->wait);
  1473. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  1474. }
  1475. if (input_wakeup)
  1476. wakeup_pipe_writers(ipipe);
  1477. return ret;
  1478. }
  1479. /*
  1480. * Link contents of ipipe to opipe.
  1481. */
  1482. static int link_pipe(struct pipe_inode_info *ipipe,
  1483. struct pipe_inode_info *opipe,
  1484. size_t len, unsigned int flags)
  1485. {
  1486. struct pipe_buffer *ibuf, *obuf;
  1487. int ret = 0, i = 0, nbuf;
  1488. /*
  1489. * Potential ABBA deadlock, work around it by ordering lock
  1490. * grabbing by pipe info address. Otherwise two different processes
  1491. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1492. */
  1493. pipe_double_lock(ipipe, opipe);
  1494. do {
  1495. if (!opipe->readers) {
  1496. send_sig(SIGPIPE, current, 0);
  1497. if (!ret)
  1498. ret = -EPIPE;
  1499. break;
  1500. }
  1501. /*
  1502. * If we have iterated all input buffers or ran out of
  1503. * output room, break.
  1504. */
  1505. if (i >= ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS)
  1506. break;
  1507. ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
  1508. nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
  1509. /*
  1510. * Get a reference to this pipe buffer,
  1511. * so we can copy the contents over.
  1512. */
  1513. ibuf->ops->get(ipipe, ibuf);
  1514. obuf = opipe->bufs + nbuf;
  1515. *obuf = *ibuf;
  1516. /*
  1517. * Don't inherit the gift flag, we need to
  1518. * prevent multiple steals of this page.
  1519. */
  1520. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1521. if (obuf->len > len)
  1522. obuf->len = len;
  1523. opipe->nrbufs++;
  1524. ret += obuf->len;
  1525. len -= obuf->len;
  1526. i++;
  1527. } while (len);
  1528. /*
  1529. * return EAGAIN if we have the potential of some data in the
  1530. * future, otherwise just return 0
  1531. */
  1532. if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
  1533. ret = -EAGAIN;
  1534. pipe_unlock(ipipe);
  1535. pipe_unlock(opipe);
  1536. /*
  1537. * If we put data in the output pipe, wakeup any potential readers.
  1538. */
  1539. if (ret > 0) {
  1540. smp_mb();
  1541. if (waitqueue_active(&opipe->wait))
  1542. wake_up_interruptible(&opipe->wait);
  1543. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  1544. }
  1545. return ret;
  1546. }
  1547. /*
  1548. * This is a tee(1) implementation that works on pipes. It doesn't copy
  1549. * any data, it simply references the 'in' pages on the 'out' pipe.
  1550. * The 'flags' used are the SPLICE_F_* variants, currently the only
  1551. * applicable one is SPLICE_F_NONBLOCK.
  1552. */
  1553. static long do_tee(struct file *in, struct file *out, size_t len,
  1554. unsigned int flags)
  1555. {
  1556. struct pipe_inode_info *ipipe = pipe_info(in->f_path.dentry->d_inode);
  1557. struct pipe_inode_info *opipe = pipe_info(out->f_path.dentry->d_inode);
  1558. int ret = -EINVAL;
  1559. /*
  1560. * Duplicate the contents of ipipe to opipe without actually
  1561. * copying the data.
  1562. */
  1563. if (ipipe && opipe && ipipe != opipe) {
  1564. /*
  1565. * Keep going, unless we encounter an error. The ipipe/opipe
  1566. * ordering doesn't really matter.
  1567. */
  1568. ret = ipipe_prep(ipipe, flags);
  1569. if (!ret) {
  1570. ret = opipe_prep(opipe, flags);
  1571. if (!ret)
  1572. ret = link_pipe(ipipe, opipe, len, flags);
  1573. }
  1574. }
  1575. return ret;
  1576. }
  1577. SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
  1578. {
  1579. struct file *in;
  1580. int error, fput_in;
  1581. if (unlikely(!len))
  1582. return 0;
  1583. error = -EBADF;
  1584. in = fget_light(fdin, &fput_in);
  1585. if (in) {
  1586. if (in->f_mode & FMODE_READ) {
  1587. int fput_out;
  1588. struct file *out = fget_light(fdout, &fput_out);
  1589. if (out) {
  1590. if (out->f_mode & FMODE_WRITE)
  1591. error = do_tee(in, out, len, flags);
  1592. fput_light(out, fput_out);
  1593. }
  1594. }
  1595. fput_light(in, fput_in);
  1596. }
  1597. return error;
  1598. }