splice.c 40 KB

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