splice.c 46 KB

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