splice.c 46 KB

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