splice.c 41 KB

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