splice.c 40 KB

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