splice.c 47 KB

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