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