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