splice.c 46 KB

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