splice.c 47 KB

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