splice.c 46 KB

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