splice.c 47 KB

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