splice.c 39 KB

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