splice.c 47 KB

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