splice.c 46 KB

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