splice.c 46 KB

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