splice.c 47 KB

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