splice.c 40 KB

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