splice.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  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 (PagePrivate(page) && !try_to_release_page(page, GFP_KERNEL))
  58. goto out_unlock;
  59. /*
  60. * If we succeeded in removing the mapping, set LRU flag
  61. * and return good.
  62. */
  63. if (remove_mapping(mapping, page)) {
  64. buf->flags |= PIPE_BUF_FLAG_LRU;
  65. return 0;
  66. }
  67. }
  68. /*
  69. * Raced with truncate or failed to remove page from current
  70. * address space, unlock and return failure.
  71. */
  72. out_unlock:
  73. unlock_page(page);
  74. return 1;
  75. }
  76. static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
  77. struct pipe_buffer *buf)
  78. {
  79. page_cache_release(buf->page);
  80. buf->flags &= ~PIPE_BUF_FLAG_LRU;
  81. }
  82. /*
  83. * Check whether the contents of buf is OK to access. Since the content
  84. * is a page cache page, IO may be in flight.
  85. */
  86. static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
  87. struct pipe_buffer *buf)
  88. {
  89. struct page *page = buf->page;
  90. int err;
  91. if (!PageUptodate(page)) {
  92. lock_page(page);
  93. /*
  94. * Page got truncated/unhashed. This will cause a 0-byte
  95. * splice, if this is the first page.
  96. */
  97. if (!page->mapping) {
  98. err = -ENODATA;
  99. goto error;
  100. }
  101. /*
  102. * Uh oh, read-error from disk.
  103. */
  104. if (!PageUptodate(page)) {
  105. err = -EIO;
  106. goto error;
  107. }
  108. /*
  109. * Page is ok afterall, we are done.
  110. */
  111. unlock_page(page);
  112. }
  113. return 0;
  114. error:
  115. unlock_page(page);
  116. return err;
  117. }
  118. static const struct pipe_buf_operations page_cache_pipe_buf_ops = {
  119. .can_merge = 0,
  120. .map = generic_pipe_buf_map,
  121. .unmap = generic_pipe_buf_unmap,
  122. .confirm = page_cache_pipe_buf_confirm,
  123. .release = page_cache_pipe_buf_release,
  124. .steal = page_cache_pipe_buf_steal,
  125. .get = generic_pipe_buf_get,
  126. };
  127. static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
  128. struct pipe_buffer *buf)
  129. {
  130. if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
  131. return 1;
  132. buf->flags |= PIPE_BUF_FLAG_LRU;
  133. return generic_pipe_buf_steal(pipe, buf);
  134. }
  135. static const struct pipe_buf_operations user_page_pipe_buf_ops = {
  136. .can_merge = 0,
  137. .map = generic_pipe_buf_map,
  138. .unmap = generic_pipe_buf_unmap,
  139. .confirm = generic_pipe_buf_confirm,
  140. .release = page_cache_pipe_buf_release,
  141. .steal = user_page_pipe_buf_steal,
  142. .get = generic_pipe_buf_get,
  143. };
  144. /**
  145. * splice_to_pipe - fill passed data into a pipe
  146. * @pipe: pipe to fill
  147. * @spd: data to fill
  148. *
  149. * Description:
  150. * @spd contains a map of pages and len/offset tuples, along with
  151. * the struct pipe_buf_operations associated with these pages. This
  152. * function will link that data to the pipe.
  153. *
  154. */
  155. ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
  156. struct splice_pipe_desc *spd)
  157. {
  158. unsigned int spd_pages = spd->nr_pages;
  159. int ret, do_wakeup, page_nr;
  160. ret = 0;
  161. do_wakeup = 0;
  162. page_nr = 0;
  163. if (pipe->inode)
  164. mutex_lock(&pipe->inode->i_mutex);
  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. if (pipe->inode) {
  215. mutex_unlock(&pipe->inode->i_mutex);
  216. if (do_wakeup) {
  217. smp_mb();
  218. if (waitqueue_active(&pipe->wait))
  219. wake_up_interruptible(&pipe->wait);
  220. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  221. }
  222. }
  223. while (page_nr < spd_pages)
  224. spd->spd_release(spd, page_nr++);
  225. return ret;
  226. }
  227. static void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
  228. {
  229. page_cache_release(spd->pages[i]);
  230. }
  231. static int
  232. __generic_file_splice_read(struct file *in, loff_t *ppos,
  233. struct pipe_inode_info *pipe, size_t len,
  234. unsigned int flags)
  235. {
  236. struct address_space *mapping = in->f_mapping;
  237. unsigned int loff, nr_pages, req_pages;
  238. struct page *pages[PIPE_BUFFERS];
  239. struct partial_page partial[PIPE_BUFFERS];
  240. struct page *page;
  241. pgoff_t index, end_index;
  242. loff_t isize;
  243. int error, page_nr;
  244. struct splice_pipe_desc spd = {
  245. .pages = pages,
  246. .partial = partial,
  247. .flags = flags,
  248. .ops = &page_cache_pipe_buf_ops,
  249. .spd_release = spd_release_page,
  250. };
  251. index = *ppos >> PAGE_CACHE_SHIFT;
  252. loff = *ppos & ~PAGE_CACHE_MASK;
  253. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  254. nr_pages = min(req_pages, (unsigned)PIPE_BUFFERS);
  255. /*
  256. * Lookup the (hopefully) full range of pages we need.
  257. */
  258. spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages);
  259. index += spd.nr_pages;
  260. /*
  261. * If find_get_pages_contig() returned fewer pages than we needed,
  262. * readahead/allocate the rest and fill in the holes.
  263. */
  264. if (spd.nr_pages < nr_pages)
  265. page_cache_sync_readahead(mapping, &in->f_ra, in,
  266. index, req_pages - spd.nr_pages);
  267. error = 0;
  268. while (spd.nr_pages < nr_pages) {
  269. /*
  270. * Page could be there, find_get_pages_contig() breaks on
  271. * the first hole.
  272. */
  273. page = find_get_page(mapping, index);
  274. if (!page) {
  275. /*
  276. * page didn't exist, allocate one.
  277. */
  278. page = page_cache_alloc_cold(mapping);
  279. if (!page)
  280. break;
  281. error = add_to_page_cache_lru(page, mapping, index,
  282. mapping_gfp_mask(mapping));
  283. if (unlikely(error)) {
  284. page_cache_release(page);
  285. if (error == -EEXIST)
  286. continue;
  287. break;
  288. }
  289. /*
  290. * add_to_page_cache() locks the page, unlock it
  291. * to avoid convoluting the logic below even more.
  292. */
  293. unlock_page(page);
  294. }
  295. pages[spd.nr_pages++] = page;
  296. index++;
  297. }
  298. /*
  299. * Now loop over the map and see if we need to start IO on any
  300. * pages, fill in the partial map, etc.
  301. */
  302. index = *ppos >> PAGE_CACHE_SHIFT;
  303. nr_pages = spd.nr_pages;
  304. spd.nr_pages = 0;
  305. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  306. unsigned int this_len;
  307. if (!len)
  308. break;
  309. /*
  310. * this_len is the max we'll use from this page
  311. */
  312. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  313. page = pages[page_nr];
  314. if (PageReadahead(page))
  315. page_cache_async_readahead(mapping, &in->f_ra, in,
  316. page, index, req_pages - page_nr);
  317. /*
  318. * If the page isn't uptodate, we may need to start io on it
  319. */
  320. if (!PageUptodate(page)) {
  321. /*
  322. * If in nonblock mode then dont block on waiting
  323. * for an in-flight io page
  324. */
  325. if (flags & SPLICE_F_NONBLOCK) {
  326. if (!trylock_page(page)) {
  327. error = -EAGAIN;
  328. break;
  329. }
  330. } else
  331. lock_page(page);
  332. /*
  333. * Page was truncated, or invalidated by the
  334. * filesystem. Redo the find/create, but this time the
  335. * page is kept locked, so there's no chance of another
  336. * race with truncate/invalidate.
  337. */
  338. if (!page->mapping) {
  339. unlock_page(page);
  340. page = find_or_create_page(mapping, index,
  341. mapping_gfp_mask(mapping));
  342. if (!page) {
  343. error = -ENOMEM;
  344. break;
  345. }
  346. page_cache_release(pages[page_nr]);
  347. pages[page_nr] = page;
  348. }
  349. /*
  350. * page was already under io and is now done, great
  351. */
  352. if (PageUptodate(page)) {
  353. unlock_page(page);
  354. goto fill_it;
  355. }
  356. /*
  357. * need to read in the page
  358. */
  359. error = mapping->a_ops->readpage(in, page);
  360. if (unlikely(error)) {
  361. /*
  362. * We really should re-lookup the page here,
  363. * but it complicates things a lot. Instead
  364. * lets just do what we already stored, and
  365. * we'll get it the next time we are called.
  366. */
  367. if (error == AOP_TRUNCATED_PAGE)
  368. error = 0;
  369. break;
  370. }
  371. }
  372. fill_it:
  373. /*
  374. * i_size must be checked after PageUptodate.
  375. */
  376. isize = i_size_read(mapping->host);
  377. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  378. if (unlikely(!isize || index > end_index))
  379. break;
  380. /*
  381. * if this is the last page, see if we need to shrink
  382. * the length and stop
  383. */
  384. if (end_index == index) {
  385. unsigned int plen;
  386. /*
  387. * max good bytes in this page
  388. */
  389. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  390. if (plen <= loff)
  391. break;
  392. /*
  393. * force quit after adding this page
  394. */
  395. this_len = min(this_len, plen - loff);
  396. len = this_len;
  397. }
  398. partial[page_nr].offset = loff;
  399. partial[page_nr].len = this_len;
  400. len -= this_len;
  401. loff = 0;
  402. spd.nr_pages++;
  403. index++;
  404. }
  405. /*
  406. * Release any pages at the end, if we quit early. 'page_nr' is how far
  407. * we got, 'nr_pages' is how many pages are in the map.
  408. */
  409. while (page_nr < nr_pages)
  410. page_cache_release(pages[page_nr++]);
  411. in->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT;
  412. if (spd.nr_pages)
  413. return splice_to_pipe(pipe, &spd);
  414. return error;
  415. }
  416. /**
  417. * generic_file_splice_read - splice data from file to a pipe
  418. * @in: file to splice from
  419. * @ppos: position in @in
  420. * @pipe: pipe to splice to
  421. * @len: number of bytes to splice
  422. * @flags: splice modifier flags
  423. *
  424. * Description:
  425. * Will read pages from given file and fill them into a pipe. Can be
  426. * used as long as the address_space operations for the source implements
  427. * a readpage() hook.
  428. *
  429. */
  430. ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
  431. struct pipe_inode_info *pipe, size_t len,
  432. unsigned int flags)
  433. {
  434. loff_t isize, left;
  435. int ret;
  436. isize = i_size_read(in->f_mapping->host);
  437. if (unlikely(*ppos >= isize))
  438. return 0;
  439. left = isize - *ppos;
  440. if (unlikely(left < len))
  441. len = left;
  442. ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
  443. if (ret > 0)
  444. *ppos += ret;
  445. return ret;
  446. }
  447. EXPORT_SYMBOL(generic_file_splice_read);
  448. /*
  449. * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
  450. * using sendpage(). Return the number of bytes sent.
  451. */
  452. static int pipe_to_sendpage(struct pipe_inode_info *pipe,
  453. struct pipe_buffer *buf, struct splice_desc *sd)
  454. {
  455. struct file *file = sd->u.file;
  456. loff_t pos = sd->pos;
  457. int ret, more;
  458. ret = buf->ops->confirm(pipe, buf);
  459. if (!ret) {
  460. more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
  461. ret = file->f_op->sendpage(file, buf->page, buf->offset,
  462. sd->len, &pos, more);
  463. }
  464. return ret;
  465. }
  466. /*
  467. * This is a little more tricky than the file -> pipe splicing. There are
  468. * basically three cases:
  469. *
  470. * - Destination page already exists in the address space and there
  471. * are users of it. For that case we have no other option that
  472. * copying the data. Tough luck.
  473. * - Destination page already exists in the address space, but there
  474. * are no users of it. Make sure it's uptodate, then drop it. Fall
  475. * through to last case.
  476. * - Destination page does not exist, we can add the pipe page to
  477. * the page cache and avoid the copy.
  478. *
  479. * If asked to move pages to the output file (SPLICE_F_MOVE is set in
  480. * sd->flags), we attempt to migrate pages from the pipe to the output
  481. * file address space page cache. This is possible if no one else has
  482. * the pipe page referenced outside of the pipe and page cache. If
  483. * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
  484. * a new page in the output file page cache and fill/dirty that.
  485. */
  486. static int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  487. struct splice_desc *sd)
  488. {
  489. struct file *file = sd->u.file;
  490. struct address_space *mapping = file->f_mapping;
  491. unsigned int offset, this_len;
  492. struct page *page;
  493. void *fsdata;
  494. int ret;
  495. /*
  496. * make sure the data in this buffer is uptodate
  497. */
  498. ret = buf->ops->confirm(pipe, buf);
  499. if (unlikely(ret))
  500. return ret;
  501. offset = sd->pos & ~PAGE_CACHE_MASK;
  502. this_len = sd->len;
  503. if (this_len + offset > PAGE_CACHE_SIZE)
  504. this_len = PAGE_CACHE_SIZE - offset;
  505. ret = pagecache_write_begin(file, mapping, sd->pos, this_len,
  506. AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
  507. if (unlikely(ret))
  508. goto out;
  509. if (buf->page != page) {
  510. /*
  511. * Careful, ->map() uses KM_USER0!
  512. */
  513. char *src = buf->ops->map(pipe, buf, 1);
  514. char *dst = kmap_atomic(page, KM_USER1);
  515. memcpy(dst + offset, src + buf->offset, this_len);
  516. flush_dcache_page(page);
  517. kunmap_atomic(dst, KM_USER1);
  518. buf->ops->unmap(pipe, buf, src);
  519. }
  520. ret = pagecache_write_end(file, mapping, sd->pos, this_len, this_len,
  521. page, fsdata);
  522. out:
  523. return ret;
  524. }
  525. /**
  526. * __splice_from_pipe - splice data from a pipe to given actor
  527. * @pipe: pipe to splice from
  528. * @sd: information to @actor
  529. * @actor: handler that splices the data
  530. *
  531. * Description:
  532. * This function does little more than loop over the pipe and call
  533. * @actor to do the actual moving of a single struct pipe_buffer to
  534. * the desired destination. See pipe_to_file, pipe_to_sendpage, or
  535. * pipe_to_user.
  536. *
  537. */
  538. ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
  539. splice_actor *actor)
  540. {
  541. int ret, do_wakeup, err;
  542. ret = 0;
  543. do_wakeup = 0;
  544. for (;;) {
  545. if (pipe->nrbufs) {
  546. struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  547. const struct pipe_buf_operations *ops = buf->ops;
  548. sd->len = buf->len;
  549. if (sd->len > sd->total_len)
  550. sd->len = sd->total_len;
  551. err = actor(pipe, buf, sd);
  552. if (err <= 0) {
  553. if (!ret && err != -ENODATA)
  554. ret = err;
  555. break;
  556. }
  557. ret += err;
  558. buf->offset += err;
  559. buf->len -= err;
  560. sd->len -= err;
  561. sd->pos += err;
  562. sd->total_len -= err;
  563. if (sd->len)
  564. continue;
  565. if (!buf->len) {
  566. buf->ops = NULL;
  567. ops->release(pipe, buf);
  568. pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
  569. pipe->nrbufs--;
  570. if (pipe->inode)
  571. do_wakeup = 1;
  572. }
  573. if (!sd->total_len)
  574. break;
  575. }
  576. if (pipe->nrbufs)
  577. continue;
  578. if (!pipe->writers)
  579. break;
  580. if (!pipe->waiting_writers) {
  581. if (ret)
  582. break;
  583. }
  584. if (sd->flags & SPLICE_F_NONBLOCK) {
  585. if (!ret)
  586. ret = -EAGAIN;
  587. break;
  588. }
  589. if (signal_pending(current)) {
  590. if (!ret)
  591. ret = -ERESTARTSYS;
  592. break;
  593. }
  594. if (do_wakeup) {
  595. smp_mb();
  596. if (waitqueue_active(&pipe->wait))
  597. wake_up_interruptible_sync(&pipe->wait);
  598. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  599. do_wakeup = 0;
  600. }
  601. pipe_wait(pipe);
  602. }
  603. if (do_wakeup) {
  604. smp_mb();
  605. if (waitqueue_active(&pipe->wait))
  606. wake_up_interruptible(&pipe->wait);
  607. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  608. }
  609. return ret;
  610. }
  611. EXPORT_SYMBOL(__splice_from_pipe);
  612. /**
  613. * splice_from_pipe - splice data from a pipe to a file
  614. * @pipe: pipe to splice from
  615. * @out: file to splice to
  616. * @ppos: position in @out
  617. * @len: how many bytes to splice
  618. * @flags: splice modifier flags
  619. * @actor: handler that splices the data
  620. *
  621. * Description:
  622. * See __splice_from_pipe. This function locks the input and output inodes,
  623. * otherwise it's identical to __splice_from_pipe().
  624. *
  625. */
  626. ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
  627. loff_t *ppos, size_t len, unsigned int flags,
  628. splice_actor *actor)
  629. {
  630. ssize_t ret;
  631. struct inode *inode = out->f_mapping->host;
  632. struct splice_desc sd = {
  633. .total_len = len,
  634. .flags = flags,
  635. .pos = *ppos,
  636. .u.file = out,
  637. };
  638. /*
  639. * The actor worker might be calling ->write_begin and
  640. * ->write_end. Most of the time, these expect i_mutex to
  641. * be held. Since this may result in an ABBA deadlock with
  642. * pipe->inode, we have to order lock acquiry here.
  643. */
  644. inode_double_lock(inode, pipe->inode);
  645. ret = __splice_from_pipe(pipe, &sd, actor);
  646. inode_double_unlock(inode, pipe->inode);
  647. return ret;
  648. }
  649. /**
  650. * generic_file_splice_write_nolock - generic_file_splice_write without mutexes
  651. * @pipe: pipe info
  652. * @out: file to write to
  653. * @ppos: position in @out
  654. * @len: number of bytes to splice
  655. * @flags: splice modifier flags
  656. *
  657. * Description:
  658. * Will either move or copy pages (determined by @flags options) from
  659. * the given pipe inode to the given file. The caller is responsible
  660. * for acquiring i_mutex on both inodes.
  661. *
  662. */
  663. ssize_t
  664. generic_file_splice_write_nolock(struct pipe_inode_info *pipe, struct file *out,
  665. loff_t *ppos, size_t len, unsigned int flags)
  666. {
  667. struct address_space *mapping = out->f_mapping;
  668. struct inode *inode = mapping->host;
  669. struct splice_desc sd = {
  670. .total_len = len,
  671. .flags = flags,
  672. .pos = *ppos,
  673. .u.file = out,
  674. };
  675. ssize_t ret;
  676. int err;
  677. err = file_remove_suid(out);
  678. if (unlikely(err))
  679. return err;
  680. ret = __splice_from_pipe(pipe, &sd, pipe_to_file);
  681. if (ret > 0) {
  682. unsigned long nr_pages;
  683. *ppos += ret;
  684. nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  685. /*
  686. * If file or inode is SYNC and we actually wrote some data,
  687. * sync it.
  688. */
  689. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  690. err = generic_osync_inode(inode, mapping,
  691. OSYNC_METADATA|OSYNC_DATA);
  692. if (err)
  693. ret = err;
  694. }
  695. balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
  696. }
  697. return ret;
  698. }
  699. EXPORT_SYMBOL(generic_file_splice_write_nolock);
  700. /**
  701. * generic_file_splice_write - splice data from a pipe to a file
  702. * @pipe: pipe info
  703. * @out: file to write to
  704. * @ppos: position in @out
  705. * @len: number of bytes to splice
  706. * @flags: splice modifier flags
  707. *
  708. * Description:
  709. * Will either move or copy pages (determined by @flags options) from
  710. * the given pipe inode to the given file.
  711. *
  712. */
  713. ssize_t
  714. generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  715. loff_t *ppos, size_t len, unsigned int flags)
  716. {
  717. struct address_space *mapping = out->f_mapping;
  718. struct inode *inode = mapping->host;
  719. struct splice_desc sd = {
  720. .total_len = len,
  721. .flags = flags,
  722. .pos = *ppos,
  723. .u.file = out,
  724. };
  725. ssize_t ret;
  726. inode_double_lock(inode, pipe->inode);
  727. ret = file_remove_suid(out);
  728. if (likely(!ret))
  729. ret = __splice_from_pipe(pipe, &sd, pipe_to_file);
  730. inode_double_unlock(inode, pipe->inode);
  731. if (ret > 0) {
  732. unsigned long nr_pages;
  733. *ppos += ret;
  734. nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  735. /*
  736. * If file or inode is SYNC and we actually wrote some data,
  737. * sync it.
  738. */
  739. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  740. int err;
  741. mutex_lock(&inode->i_mutex);
  742. err = generic_osync_inode(inode, mapping,
  743. OSYNC_METADATA|OSYNC_DATA);
  744. mutex_unlock(&inode->i_mutex);
  745. if (err)
  746. ret = err;
  747. }
  748. balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
  749. }
  750. return ret;
  751. }
  752. EXPORT_SYMBOL(generic_file_splice_write);
  753. /**
  754. * generic_splice_sendpage - splice data from a pipe to a socket
  755. * @pipe: pipe to splice from
  756. * @out: socket to write to
  757. * @ppos: position in @out
  758. * @len: number of bytes to splice
  759. * @flags: splice modifier flags
  760. *
  761. * Description:
  762. * Will send @len bytes from the pipe to a network socket. No data copying
  763. * is involved.
  764. *
  765. */
  766. ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
  767. loff_t *ppos, size_t len, unsigned int flags)
  768. {
  769. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
  770. }
  771. EXPORT_SYMBOL(generic_splice_sendpage);
  772. /*
  773. * Attempt to initiate a splice from pipe to file.
  774. */
  775. static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  776. loff_t *ppos, size_t len, unsigned int flags)
  777. {
  778. int ret;
  779. if (unlikely(!out->f_op || !out->f_op->splice_write))
  780. return -EINVAL;
  781. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  782. return -EBADF;
  783. if (unlikely(out->f_flags & O_APPEND))
  784. return -EINVAL;
  785. ret = rw_verify_area(WRITE, out, ppos, len);
  786. if (unlikely(ret < 0))
  787. return ret;
  788. return out->f_op->splice_write(pipe, out, ppos, len, flags);
  789. }
  790. /*
  791. * Attempt to initiate a splice from a file to a pipe.
  792. */
  793. static long do_splice_to(struct file *in, loff_t *ppos,
  794. struct pipe_inode_info *pipe, size_t len,
  795. unsigned int flags)
  796. {
  797. int ret;
  798. if (unlikely(!in->f_op || !in->f_op->splice_read))
  799. return -EINVAL;
  800. if (unlikely(!(in->f_mode & FMODE_READ)))
  801. return -EBADF;
  802. ret = rw_verify_area(READ, in, ppos, len);
  803. if (unlikely(ret < 0))
  804. return ret;
  805. return in->f_op->splice_read(in, ppos, pipe, len, flags);
  806. }
  807. /**
  808. * splice_direct_to_actor - splices data directly between two non-pipes
  809. * @in: file to splice from
  810. * @sd: actor information on where to splice to
  811. * @actor: handles the data splicing
  812. *
  813. * Description:
  814. * This is a special case helper to splice directly between two
  815. * points, without requiring an explicit pipe. Internally an allocated
  816. * pipe is cached in the process, and reused during the lifetime of
  817. * that process.
  818. *
  819. */
  820. ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
  821. splice_direct_actor *actor)
  822. {
  823. struct pipe_inode_info *pipe;
  824. long ret, bytes;
  825. umode_t i_mode;
  826. size_t len;
  827. int i, flags;
  828. /*
  829. * We require the input being a regular file, as we don't want to
  830. * randomly drop data for eg socket -> socket splicing. Use the
  831. * piped splicing for that!
  832. */
  833. i_mode = in->f_path.dentry->d_inode->i_mode;
  834. if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  835. return -EINVAL;
  836. /*
  837. * neither in nor out is a pipe, setup an internal pipe attached to
  838. * 'out' and transfer the wanted data from 'in' to 'out' through that
  839. */
  840. pipe = current->splice_pipe;
  841. if (unlikely(!pipe)) {
  842. pipe = alloc_pipe_info(NULL);
  843. if (!pipe)
  844. return -ENOMEM;
  845. /*
  846. * We don't have an immediate reader, but we'll read the stuff
  847. * out of the pipe right after the splice_to_pipe(). So set
  848. * PIPE_READERS appropriately.
  849. */
  850. pipe->readers = 1;
  851. current->splice_pipe = pipe;
  852. }
  853. /*
  854. * Do the splice.
  855. */
  856. ret = 0;
  857. bytes = 0;
  858. len = sd->total_len;
  859. flags = sd->flags;
  860. /*
  861. * Don't block on output, we have to drain the direct pipe.
  862. */
  863. sd->flags &= ~SPLICE_F_NONBLOCK;
  864. while (len) {
  865. size_t read_len;
  866. loff_t pos = sd->pos, prev_pos = pos;
  867. ret = do_splice_to(in, &pos, pipe, len, flags);
  868. if (unlikely(ret <= 0))
  869. goto out_release;
  870. read_len = ret;
  871. sd->total_len = read_len;
  872. /*
  873. * NOTE: nonblocking mode only applies to the input. We
  874. * must not do the output in nonblocking mode as then we
  875. * could get stuck data in the internal pipe:
  876. */
  877. ret = actor(pipe, sd);
  878. if (unlikely(ret <= 0)) {
  879. sd->pos = prev_pos;
  880. goto out_release;
  881. }
  882. bytes += ret;
  883. len -= ret;
  884. sd->pos = pos;
  885. if (ret < read_len) {
  886. sd->pos = prev_pos + ret;
  887. goto out_release;
  888. }
  889. }
  890. done:
  891. pipe->nrbufs = pipe->curbuf = 0;
  892. file_accessed(in);
  893. return bytes;
  894. out_release:
  895. /*
  896. * If we did an incomplete transfer we must release
  897. * the pipe buffers in question:
  898. */
  899. for (i = 0; i < PIPE_BUFFERS; i++) {
  900. struct pipe_buffer *buf = pipe->bufs + i;
  901. if (buf->ops) {
  902. buf->ops->release(pipe, buf);
  903. buf->ops = NULL;
  904. }
  905. }
  906. if (!bytes)
  907. bytes = ret;
  908. goto done;
  909. }
  910. EXPORT_SYMBOL(splice_direct_to_actor);
  911. static int direct_splice_actor(struct pipe_inode_info *pipe,
  912. struct splice_desc *sd)
  913. {
  914. struct file *file = sd->u.file;
  915. return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
  916. }
  917. /**
  918. * do_splice_direct - splices data directly between two files
  919. * @in: file to splice from
  920. * @ppos: input file offset
  921. * @out: file to splice to
  922. * @len: number of bytes to splice
  923. * @flags: splice modifier flags
  924. *
  925. * Description:
  926. * For use by do_sendfile(). splice can easily emulate sendfile, but
  927. * doing it in the application would incur an extra system call
  928. * (splice in + splice out, as compared to just sendfile()). So this helper
  929. * can splice directly through a process-private pipe.
  930. *
  931. */
  932. long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
  933. size_t len, unsigned int flags)
  934. {
  935. struct splice_desc sd = {
  936. .len = len,
  937. .total_len = len,
  938. .flags = flags,
  939. .pos = *ppos,
  940. .u.file = out,
  941. };
  942. long ret;
  943. ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
  944. if (ret > 0)
  945. *ppos = sd.pos;
  946. return ret;
  947. }
  948. /*
  949. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  950. * location, so checking ->i_pipe is not enough to verify that this is a
  951. * pipe.
  952. */
  953. static inline struct pipe_inode_info *pipe_info(struct inode *inode)
  954. {
  955. if (S_ISFIFO(inode->i_mode))
  956. return inode->i_pipe;
  957. return NULL;
  958. }
  959. /*
  960. * Determine where to splice to/from.
  961. */
  962. static long do_splice(struct file *in, loff_t __user *off_in,
  963. struct file *out, loff_t __user *off_out,
  964. size_t len, unsigned int flags)
  965. {
  966. struct pipe_inode_info *pipe;
  967. loff_t offset, *off;
  968. long ret;
  969. pipe = pipe_info(in->f_path.dentry->d_inode);
  970. if (pipe) {
  971. if (off_in)
  972. return -ESPIPE;
  973. if (off_out) {
  974. if (out->f_op->llseek == no_llseek)
  975. return -EINVAL;
  976. if (copy_from_user(&offset, off_out, sizeof(loff_t)))
  977. return -EFAULT;
  978. off = &offset;
  979. } else
  980. off = &out->f_pos;
  981. ret = do_splice_from(pipe, out, off, len, flags);
  982. if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
  983. ret = -EFAULT;
  984. return ret;
  985. }
  986. pipe = pipe_info(out->f_path.dentry->d_inode);
  987. if (pipe) {
  988. if (off_out)
  989. return -ESPIPE;
  990. if (off_in) {
  991. if (in->f_op->llseek == no_llseek)
  992. return -EINVAL;
  993. if (copy_from_user(&offset, off_in, sizeof(loff_t)))
  994. return -EFAULT;
  995. off = &offset;
  996. } else
  997. off = &in->f_pos;
  998. ret = do_splice_to(in, off, pipe, len, flags);
  999. if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
  1000. ret = -EFAULT;
  1001. return ret;
  1002. }
  1003. return -EINVAL;
  1004. }
  1005. /*
  1006. * Map an iov into an array of pages and offset/length tupples. With the
  1007. * partial_page structure, we can map several non-contiguous ranges into
  1008. * our ones pages[] map instead of splitting that operation into pieces.
  1009. * Could easily be exported as a generic helper for other users, in which
  1010. * case one would probably want to add a 'max_nr_pages' parameter as well.
  1011. */
  1012. static int get_iovec_page_array(const struct iovec __user *iov,
  1013. unsigned int nr_vecs, struct page **pages,
  1014. struct partial_page *partial, int aligned)
  1015. {
  1016. int buffers = 0, error = 0;
  1017. while (nr_vecs) {
  1018. unsigned long off, npages;
  1019. struct iovec entry;
  1020. void __user *base;
  1021. size_t len;
  1022. int i;
  1023. error = -EFAULT;
  1024. if (copy_from_user(&entry, iov, sizeof(entry)))
  1025. break;
  1026. base = entry.iov_base;
  1027. len = entry.iov_len;
  1028. /*
  1029. * Sanity check this iovec. 0 read succeeds.
  1030. */
  1031. error = 0;
  1032. if (unlikely(!len))
  1033. break;
  1034. error = -EFAULT;
  1035. if (!access_ok(VERIFY_READ, base, len))
  1036. break;
  1037. /*
  1038. * Get this base offset and number of pages, then map
  1039. * in the user pages.
  1040. */
  1041. off = (unsigned long) base & ~PAGE_MASK;
  1042. /*
  1043. * If asked for alignment, the offset must be zero and the
  1044. * length a multiple of the PAGE_SIZE.
  1045. */
  1046. error = -EINVAL;
  1047. if (aligned && (off || len & ~PAGE_MASK))
  1048. break;
  1049. npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1050. if (npages > PIPE_BUFFERS - buffers)
  1051. npages = PIPE_BUFFERS - buffers;
  1052. error = get_user_pages_fast((unsigned long)base, npages,
  1053. 0, &pages[buffers]);
  1054. if (unlikely(error <= 0))
  1055. break;
  1056. /*
  1057. * Fill this contiguous range into the partial page map.
  1058. */
  1059. for (i = 0; i < error; i++) {
  1060. const int plen = min_t(size_t, len, PAGE_SIZE - off);
  1061. partial[buffers].offset = off;
  1062. partial[buffers].len = plen;
  1063. off = 0;
  1064. len -= plen;
  1065. buffers++;
  1066. }
  1067. /*
  1068. * We didn't complete this iov, stop here since it probably
  1069. * means we have to move some of this into a pipe to
  1070. * be able to continue.
  1071. */
  1072. if (len)
  1073. break;
  1074. /*
  1075. * Don't continue if we mapped fewer pages than we asked for,
  1076. * or if we mapped the max number of pages that we have
  1077. * room for.
  1078. */
  1079. if (error < npages || buffers == PIPE_BUFFERS)
  1080. break;
  1081. nr_vecs--;
  1082. iov++;
  1083. }
  1084. if (buffers)
  1085. return buffers;
  1086. return error;
  1087. }
  1088. static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  1089. struct splice_desc *sd)
  1090. {
  1091. char *src;
  1092. int ret;
  1093. ret = buf->ops->confirm(pipe, buf);
  1094. if (unlikely(ret))
  1095. return ret;
  1096. /*
  1097. * See if we can use the atomic maps, by prefaulting in the
  1098. * pages and doing an atomic copy
  1099. */
  1100. if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) {
  1101. src = buf->ops->map(pipe, buf, 1);
  1102. ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset,
  1103. sd->len);
  1104. buf->ops->unmap(pipe, buf, src);
  1105. if (!ret) {
  1106. ret = sd->len;
  1107. goto out;
  1108. }
  1109. }
  1110. /*
  1111. * No dice, use slow non-atomic map and copy
  1112. */
  1113. src = buf->ops->map(pipe, buf, 0);
  1114. ret = sd->len;
  1115. if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len))
  1116. ret = -EFAULT;
  1117. buf->ops->unmap(pipe, buf, src);
  1118. out:
  1119. if (ret > 0)
  1120. sd->u.userptr += ret;
  1121. return ret;
  1122. }
  1123. /*
  1124. * For lack of a better implementation, implement vmsplice() to userspace
  1125. * as a simple copy of the pipes pages to the user iov.
  1126. */
  1127. static long vmsplice_to_user(struct file *file, const struct iovec __user *iov,
  1128. unsigned long nr_segs, unsigned int flags)
  1129. {
  1130. struct pipe_inode_info *pipe;
  1131. struct splice_desc sd;
  1132. ssize_t size;
  1133. int error;
  1134. long ret;
  1135. pipe = pipe_info(file->f_path.dentry->d_inode);
  1136. if (!pipe)
  1137. return -EBADF;
  1138. if (pipe->inode)
  1139. mutex_lock(&pipe->inode->i_mutex);
  1140. error = ret = 0;
  1141. while (nr_segs) {
  1142. void __user *base;
  1143. size_t len;
  1144. /*
  1145. * Get user address base and length for this iovec.
  1146. */
  1147. error = get_user(base, &iov->iov_base);
  1148. if (unlikely(error))
  1149. break;
  1150. error = get_user(len, &iov->iov_len);
  1151. if (unlikely(error))
  1152. break;
  1153. /*
  1154. * Sanity check this iovec. 0 read succeeds.
  1155. */
  1156. if (unlikely(!len))
  1157. break;
  1158. if (unlikely(!base)) {
  1159. error = -EFAULT;
  1160. break;
  1161. }
  1162. if (unlikely(!access_ok(VERIFY_WRITE, base, len))) {
  1163. error = -EFAULT;
  1164. break;
  1165. }
  1166. sd.len = 0;
  1167. sd.total_len = len;
  1168. sd.flags = flags;
  1169. sd.u.userptr = base;
  1170. sd.pos = 0;
  1171. size = __splice_from_pipe(pipe, &sd, pipe_to_user);
  1172. if (size < 0) {
  1173. if (!ret)
  1174. ret = size;
  1175. break;
  1176. }
  1177. ret += size;
  1178. if (size < len)
  1179. break;
  1180. nr_segs--;
  1181. iov++;
  1182. }
  1183. if (pipe->inode)
  1184. mutex_unlock(&pipe->inode->i_mutex);
  1185. if (!ret)
  1186. ret = error;
  1187. return ret;
  1188. }
  1189. /*
  1190. * vmsplice splices a user address range into a pipe. It can be thought of
  1191. * as splice-from-memory, where the regular splice is splice-from-file (or
  1192. * to file). In both cases the output is a pipe, naturally.
  1193. */
  1194. static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
  1195. unsigned long nr_segs, unsigned int flags)
  1196. {
  1197. struct pipe_inode_info *pipe;
  1198. struct page *pages[PIPE_BUFFERS];
  1199. struct partial_page partial[PIPE_BUFFERS];
  1200. struct splice_pipe_desc spd = {
  1201. .pages = pages,
  1202. .partial = partial,
  1203. .flags = flags,
  1204. .ops = &user_page_pipe_buf_ops,
  1205. .spd_release = spd_release_page,
  1206. };
  1207. pipe = pipe_info(file->f_path.dentry->d_inode);
  1208. if (!pipe)
  1209. return -EBADF;
  1210. spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
  1211. flags & SPLICE_F_GIFT);
  1212. if (spd.nr_pages <= 0)
  1213. return spd.nr_pages;
  1214. return splice_to_pipe(pipe, &spd);
  1215. }
  1216. /*
  1217. * Note that vmsplice only really supports true splicing _from_ user memory
  1218. * to a pipe, not the other way around. Splicing from user memory is a simple
  1219. * operation that can be supported without any funky alignment restrictions
  1220. * or nasty vm tricks. We simply map in the user memory and fill them into
  1221. * a pipe. The reverse isn't quite as easy, though. There are two possible
  1222. * solutions for that:
  1223. *
  1224. * - memcpy() the data internally, at which point we might as well just
  1225. * do a regular read() on the buffer anyway.
  1226. * - Lots of nasty vm tricks, that are neither fast nor flexible (it
  1227. * has restriction limitations on both ends of the pipe).
  1228. *
  1229. * Currently we punt and implement it as a normal copy, see pipe_to_user().
  1230. *
  1231. */
  1232. SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
  1233. unsigned long, nr_segs, unsigned int, flags)
  1234. {
  1235. struct file *file;
  1236. long error;
  1237. int fput;
  1238. if (unlikely(nr_segs > UIO_MAXIOV))
  1239. return -EINVAL;
  1240. else if (unlikely(!nr_segs))
  1241. return 0;
  1242. error = -EBADF;
  1243. file = fget_light(fd, &fput);
  1244. if (file) {
  1245. if (file->f_mode & FMODE_WRITE)
  1246. error = vmsplice_to_pipe(file, iov, nr_segs, flags);
  1247. else if (file->f_mode & FMODE_READ)
  1248. error = vmsplice_to_user(file, iov, nr_segs, flags);
  1249. fput_light(file, fput);
  1250. }
  1251. return error;
  1252. }
  1253. SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
  1254. int, fd_out, loff_t __user *, off_out,
  1255. size_t, len, unsigned int, flags)
  1256. {
  1257. long error;
  1258. struct file *in, *out;
  1259. int fput_in, fput_out;
  1260. if (unlikely(!len))
  1261. return 0;
  1262. error = -EBADF;
  1263. in = fget_light(fd_in, &fput_in);
  1264. if (in) {
  1265. if (in->f_mode & FMODE_READ) {
  1266. out = fget_light(fd_out, &fput_out);
  1267. if (out) {
  1268. if (out->f_mode & FMODE_WRITE)
  1269. error = do_splice(in, off_in,
  1270. out, off_out,
  1271. len, flags);
  1272. fput_light(out, fput_out);
  1273. }
  1274. }
  1275. fput_light(in, fput_in);
  1276. }
  1277. return error;
  1278. }
  1279. /*
  1280. * Make sure there's data to read. Wait for input if we can, otherwise
  1281. * return an appropriate error.
  1282. */
  1283. static int link_ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1284. {
  1285. int ret;
  1286. /*
  1287. * Check ->nrbufs without the inode lock first. This function
  1288. * is speculative anyways, so missing one is ok.
  1289. */
  1290. if (pipe->nrbufs)
  1291. return 0;
  1292. ret = 0;
  1293. mutex_lock(&pipe->inode->i_mutex);
  1294. while (!pipe->nrbufs) {
  1295. if (signal_pending(current)) {
  1296. ret = -ERESTARTSYS;
  1297. break;
  1298. }
  1299. if (!pipe->writers)
  1300. break;
  1301. if (!pipe->waiting_writers) {
  1302. if (flags & SPLICE_F_NONBLOCK) {
  1303. ret = -EAGAIN;
  1304. break;
  1305. }
  1306. }
  1307. pipe_wait(pipe);
  1308. }
  1309. mutex_unlock(&pipe->inode->i_mutex);
  1310. return ret;
  1311. }
  1312. /*
  1313. * Make sure there's writeable room. Wait for room if we can, otherwise
  1314. * return an appropriate error.
  1315. */
  1316. static int link_opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1317. {
  1318. int ret;
  1319. /*
  1320. * Check ->nrbufs without the inode lock first. This function
  1321. * is speculative anyways, so missing one is ok.
  1322. */
  1323. if (pipe->nrbufs < PIPE_BUFFERS)
  1324. return 0;
  1325. ret = 0;
  1326. mutex_lock(&pipe->inode->i_mutex);
  1327. while (pipe->nrbufs >= PIPE_BUFFERS) {
  1328. if (!pipe->readers) {
  1329. send_sig(SIGPIPE, current, 0);
  1330. ret = -EPIPE;
  1331. break;
  1332. }
  1333. if (flags & SPLICE_F_NONBLOCK) {
  1334. ret = -EAGAIN;
  1335. break;
  1336. }
  1337. if (signal_pending(current)) {
  1338. ret = -ERESTARTSYS;
  1339. break;
  1340. }
  1341. pipe->waiting_writers++;
  1342. pipe_wait(pipe);
  1343. pipe->waiting_writers--;
  1344. }
  1345. mutex_unlock(&pipe->inode->i_mutex);
  1346. return ret;
  1347. }
  1348. /*
  1349. * Link contents of ipipe to opipe.
  1350. */
  1351. static int link_pipe(struct pipe_inode_info *ipipe,
  1352. struct pipe_inode_info *opipe,
  1353. size_t len, unsigned int flags)
  1354. {
  1355. struct pipe_buffer *ibuf, *obuf;
  1356. int ret = 0, i = 0, nbuf;
  1357. /*
  1358. * Potential ABBA deadlock, work around it by ordering lock
  1359. * grabbing by inode address. Otherwise two different processes
  1360. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1361. */
  1362. inode_double_lock(ipipe->inode, opipe->inode);
  1363. do {
  1364. if (!opipe->readers) {
  1365. send_sig(SIGPIPE, current, 0);
  1366. if (!ret)
  1367. ret = -EPIPE;
  1368. break;
  1369. }
  1370. /*
  1371. * If we have iterated all input buffers or ran out of
  1372. * output room, break.
  1373. */
  1374. if (i >= ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS)
  1375. break;
  1376. ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
  1377. nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
  1378. /*
  1379. * Get a reference to this pipe buffer,
  1380. * so we can copy the contents over.
  1381. */
  1382. ibuf->ops->get(ipipe, ibuf);
  1383. obuf = opipe->bufs + nbuf;
  1384. *obuf = *ibuf;
  1385. /*
  1386. * Don't inherit the gift flag, we need to
  1387. * prevent multiple steals of this page.
  1388. */
  1389. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1390. if (obuf->len > len)
  1391. obuf->len = len;
  1392. opipe->nrbufs++;
  1393. ret += obuf->len;
  1394. len -= obuf->len;
  1395. i++;
  1396. } while (len);
  1397. /*
  1398. * return EAGAIN if we have the potential of some data in the
  1399. * future, otherwise just return 0
  1400. */
  1401. if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
  1402. ret = -EAGAIN;
  1403. inode_double_unlock(ipipe->inode, opipe->inode);
  1404. /*
  1405. * If we put data in the output pipe, wakeup any potential readers.
  1406. */
  1407. if (ret > 0) {
  1408. smp_mb();
  1409. if (waitqueue_active(&opipe->wait))
  1410. wake_up_interruptible(&opipe->wait);
  1411. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  1412. }
  1413. return ret;
  1414. }
  1415. /*
  1416. * This is a tee(1) implementation that works on pipes. It doesn't copy
  1417. * any data, it simply references the 'in' pages on the 'out' pipe.
  1418. * The 'flags' used are the SPLICE_F_* variants, currently the only
  1419. * applicable one is SPLICE_F_NONBLOCK.
  1420. */
  1421. static long do_tee(struct file *in, struct file *out, size_t len,
  1422. unsigned int flags)
  1423. {
  1424. struct pipe_inode_info *ipipe = pipe_info(in->f_path.dentry->d_inode);
  1425. struct pipe_inode_info *opipe = pipe_info(out->f_path.dentry->d_inode);
  1426. int ret = -EINVAL;
  1427. /*
  1428. * Duplicate the contents of ipipe to opipe without actually
  1429. * copying the data.
  1430. */
  1431. if (ipipe && opipe && ipipe != opipe) {
  1432. /*
  1433. * Keep going, unless we encounter an error. The ipipe/opipe
  1434. * ordering doesn't really matter.
  1435. */
  1436. ret = link_ipipe_prep(ipipe, flags);
  1437. if (!ret) {
  1438. ret = link_opipe_prep(opipe, flags);
  1439. if (!ret)
  1440. ret = link_pipe(ipipe, opipe, len, flags);
  1441. }
  1442. }
  1443. return ret;
  1444. }
  1445. SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
  1446. {
  1447. struct file *in;
  1448. int error, fput_in;
  1449. if (unlikely(!len))
  1450. return 0;
  1451. error = -EBADF;
  1452. in = fget_light(fdin, &fput_in);
  1453. if (in) {
  1454. if (in->f_mode & FMODE_READ) {
  1455. int fput_out;
  1456. struct file *out = fget_light(fdout, &fput_out);
  1457. if (out) {
  1458. if (out->f_mode & FMODE_WRITE)
  1459. error = do_tee(in, out, len, flags);
  1460. fput_light(out, fput_out);
  1461. }
  1462. }
  1463. fput_light(in, fput_in);
  1464. }
  1465. return error;
  1466. }