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. file_accessed(in);
  444. }
  445. return ret;
  446. }
  447. EXPORT_SYMBOL(generic_file_splice_read);
  448. static const struct pipe_buf_operations default_pipe_buf_ops = {
  449. .can_merge = 0,
  450. .map = generic_pipe_buf_map,
  451. .unmap = generic_pipe_buf_unmap,
  452. .confirm = generic_pipe_buf_confirm,
  453. .release = generic_pipe_buf_release,
  454. .steal = generic_pipe_buf_steal,
  455. .get = generic_pipe_buf_get,
  456. };
  457. static ssize_t kernel_readv(struct file *file, const struct iovec *vec,
  458. unsigned long vlen, loff_t offset)
  459. {
  460. mm_segment_t old_fs;
  461. loff_t pos = offset;
  462. ssize_t res;
  463. old_fs = get_fs();
  464. set_fs(get_ds());
  465. /* The cast to a user pointer is valid due to the set_fs() */
  466. res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos);
  467. set_fs(old_fs);
  468. return res;
  469. }
  470. static ssize_t kernel_write(struct file *file, const char *buf, size_t count,
  471. loff_t pos)
  472. {
  473. mm_segment_t old_fs;
  474. ssize_t res;
  475. old_fs = get_fs();
  476. set_fs(get_ds());
  477. /* The cast to a user pointer is valid due to the set_fs() */
  478. res = vfs_write(file, (const char __user *)buf, count, &pos);
  479. set_fs(old_fs);
  480. return res;
  481. }
  482. ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
  483. struct pipe_inode_info *pipe, size_t len,
  484. unsigned int flags)
  485. {
  486. unsigned int nr_pages;
  487. unsigned int nr_freed;
  488. size_t offset;
  489. struct page *pages[PIPE_BUFFERS];
  490. struct partial_page partial[PIPE_BUFFERS];
  491. struct iovec vec[PIPE_BUFFERS];
  492. pgoff_t index;
  493. ssize_t res;
  494. size_t this_len;
  495. int error;
  496. int i;
  497. struct splice_pipe_desc spd = {
  498. .pages = pages,
  499. .partial = partial,
  500. .flags = flags,
  501. .ops = &default_pipe_buf_ops,
  502. .spd_release = spd_release_page,
  503. };
  504. index = *ppos >> PAGE_CACHE_SHIFT;
  505. offset = *ppos & ~PAGE_CACHE_MASK;
  506. nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  507. for (i = 0; i < nr_pages && i < PIPE_BUFFERS && len; i++) {
  508. struct page *page;
  509. page = alloc_page(GFP_USER);
  510. error = -ENOMEM;
  511. if (!page)
  512. goto err;
  513. this_len = min_t(size_t, len, PAGE_CACHE_SIZE - offset);
  514. vec[i].iov_base = (void __user *) page_address(page);
  515. vec[i].iov_len = this_len;
  516. pages[i] = page;
  517. spd.nr_pages++;
  518. len -= this_len;
  519. offset = 0;
  520. }
  521. res = kernel_readv(in, vec, spd.nr_pages, *ppos);
  522. if (res < 0) {
  523. error = res;
  524. goto err;
  525. }
  526. error = 0;
  527. if (!res)
  528. goto err;
  529. nr_freed = 0;
  530. for (i = 0; i < spd.nr_pages; i++) {
  531. this_len = min_t(size_t, vec[i].iov_len, res);
  532. partial[i].offset = 0;
  533. partial[i].len = this_len;
  534. if (!this_len) {
  535. __free_page(pages[i]);
  536. pages[i] = NULL;
  537. nr_freed++;
  538. }
  539. res -= this_len;
  540. }
  541. spd.nr_pages -= nr_freed;
  542. res = splice_to_pipe(pipe, &spd);
  543. if (res > 0)
  544. *ppos += res;
  545. return res;
  546. err:
  547. for (i = 0; i < spd.nr_pages; i++)
  548. __free_page(pages[i]);
  549. return error;
  550. }
  551. EXPORT_SYMBOL(default_file_splice_read);
  552. /*
  553. * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
  554. * using sendpage(). Return the number of bytes sent.
  555. */
  556. static int pipe_to_sendpage(struct pipe_inode_info *pipe,
  557. struct pipe_buffer *buf, struct splice_desc *sd)
  558. {
  559. struct file *file = sd->u.file;
  560. loff_t pos = sd->pos;
  561. int ret, more;
  562. ret = buf->ops->confirm(pipe, buf);
  563. if (!ret) {
  564. more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
  565. if (file->f_op && file->f_op->sendpage)
  566. ret = file->f_op->sendpage(file, buf->page, buf->offset,
  567. sd->len, &pos, more);
  568. else
  569. ret = -EINVAL;
  570. }
  571. return ret;
  572. }
  573. /*
  574. * This is a little more tricky than the file -> pipe splicing. There are
  575. * basically three cases:
  576. *
  577. * - Destination page already exists in the address space and there
  578. * are users of it. For that case we have no other option that
  579. * copying the data. Tough luck.
  580. * - Destination page already exists in the address space, but there
  581. * are no users of it. Make sure it's uptodate, then drop it. Fall
  582. * through to last case.
  583. * - Destination page does not exist, we can add the pipe page to
  584. * the page cache and avoid the copy.
  585. *
  586. * If asked to move pages to the output file (SPLICE_F_MOVE is set in
  587. * sd->flags), we attempt to migrate pages from the pipe to the output
  588. * file address space page cache. This is possible if no one else has
  589. * the pipe page referenced outside of the pipe and page cache. If
  590. * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
  591. * a new page in the output file page cache and fill/dirty that.
  592. */
  593. int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  594. struct splice_desc *sd)
  595. {
  596. struct file *file = sd->u.file;
  597. struct address_space *mapping = file->f_mapping;
  598. unsigned int offset, this_len;
  599. struct page *page;
  600. void *fsdata;
  601. int ret;
  602. /*
  603. * make sure the data in this buffer is uptodate
  604. */
  605. ret = buf->ops->confirm(pipe, buf);
  606. if (unlikely(ret))
  607. return ret;
  608. offset = sd->pos & ~PAGE_CACHE_MASK;
  609. this_len = sd->len;
  610. if (this_len + offset > PAGE_CACHE_SIZE)
  611. this_len = PAGE_CACHE_SIZE - offset;
  612. ret = pagecache_write_begin(file, mapping, sd->pos, this_len,
  613. AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
  614. if (unlikely(ret))
  615. goto out;
  616. if (buf->page != page) {
  617. /*
  618. * Careful, ->map() uses KM_USER0!
  619. */
  620. char *src = buf->ops->map(pipe, buf, 1);
  621. char *dst = kmap_atomic(page, KM_USER1);
  622. memcpy(dst + offset, src + buf->offset, this_len);
  623. flush_dcache_page(page);
  624. kunmap_atomic(dst, KM_USER1);
  625. buf->ops->unmap(pipe, buf, src);
  626. }
  627. ret = pagecache_write_end(file, mapping, sd->pos, this_len, this_len,
  628. page, fsdata);
  629. out:
  630. return ret;
  631. }
  632. EXPORT_SYMBOL(pipe_to_file);
  633. static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
  634. {
  635. smp_mb();
  636. if (waitqueue_active(&pipe->wait))
  637. wake_up_interruptible(&pipe->wait);
  638. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  639. }
  640. /**
  641. * splice_from_pipe_feed - feed available data from a pipe to a file
  642. * @pipe: pipe to splice from
  643. * @sd: information to @actor
  644. * @actor: handler that splices the data
  645. *
  646. * Description:
  647. * This function loops over the pipe and calls @actor to do the
  648. * actual moving of a single struct pipe_buffer to the desired
  649. * destination. It returns when there's no more buffers left in
  650. * the pipe or if the requested number of bytes (@sd->total_len)
  651. * have been copied. It returns a positive number (one) if the
  652. * pipe needs to be filled with more data, zero if the required
  653. * number of bytes have been copied and -errno on error.
  654. *
  655. * This, together with splice_from_pipe_{begin,end,next}, may be
  656. * used to implement the functionality of __splice_from_pipe() when
  657. * locking is required around copying the pipe buffers to the
  658. * destination.
  659. */
  660. int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
  661. splice_actor *actor)
  662. {
  663. int ret;
  664. while (pipe->nrbufs) {
  665. struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  666. const struct pipe_buf_operations *ops = buf->ops;
  667. sd->len = buf->len;
  668. if (sd->len > sd->total_len)
  669. sd->len = sd->total_len;
  670. ret = actor(pipe, buf, sd);
  671. if (ret <= 0) {
  672. if (ret == -ENODATA)
  673. ret = 0;
  674. return ret;
  675. }
  676. buf->offset += ret;
  677. buf->len -= ret;
  678. sd->num_spliced += ret;
  679. sd->len -= ret;
  680. sd->pos += ret;
  681. sd->total_len -= ret;
  682. if (!buf->len) {
  683. buf->ops = NULL;
  684. ops->release(pipe, buf);
  685. pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
  686. pipe->nrbufs--;
  687. if (pipe->inode)
  688. sd->need_wakeup = true;
  689. }
  690. if (!sd->total_len)
  691. return 0;
  692. }
  693. return 1;
  694. }
  695. EXPORT_SYMBOL(splice_from_pipe_feed);
  696. /**
  697. * splice_from_pipe_next - wait for some data to splice from
  698. * @pipe: pipe to splice from
  699. * @sd: information about the splice operation
  700. *
  701. * Description:
  702. * This function will wait for some data and return a positive
  703. * value (one) if pipe buffers are available. It will return zero
  704. * or -errno if no more data needs to be spliced.
  705. */
  706. int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
  707. {
  708. while (!pipe->nrbufs) {
  709. if (!pipe->writers)
  710. return 0;
  711. if (!pipe->waiting_writers && sd->num_spliced)
  712. return 0;
  713. if (sd->flags & SPLICE_F_NONBLOCK)
  714. return -EAGAIN;
  715. if (signal_pending(current))
  716. return -ERESTARTSYS;
  717. if (sd->need_wakeup) {
  718. wakeup_pipe_writers(pipe);
  719. sd->need_wakeup = false;
  720. }
  721. pipe_wait(pipe);
  722. }
  723. return 1;
  724. }
  725. EXPORT_SYMBOL(splice_from_pipe_next);
  726. /**
  727. * splice_from_pipe_begin - start splicing from pipe
  728. * @sd: information about the splice operation
  729. *
  730. * Description:
  731. * This function should be called before a loop containing
  732. * splice_from_pipe_next() and splice_from_pipe_feed() to
  733. * initialize the necessary fields of @sd.
  734. */
  735. void splice_from_pipe_begin(struct splice_desc *sd)
  736. {
  737. sd->num_spliced = 0;
  738. sd->need_wakeup = false;
  739. }
  740. EXPORT_SYMBOL(splice_from_pipe_begin);
  741. /**
  742. * splice_from_pipe_end - finish splicing from pipe
  743. * @pipe: pipe to splice from
  744. * @sd: information about the splice operation
  745. *
  746. * Description:
  747. * This function will wake up pipe writers if necessary. It should
  748. * be called after a loop containing splice_from_pipe_next() and
  749. * splice_from_pipe_feed().
  750. */
  751. void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
  752. {
  753. if (sd->need_wakeup)
  754. wakeup_pipe_writers(pipe);
  755. }
  756. EXPORT_SYMBOL(splice_from_pipe_end);
  757. /**
  758. * __splice_from_pipe - splice data from a pipe to given actor
  759. * @pipe: pipe to splice from
  760. * @sd: information to @actor
  761. * @actor: handler that splices the data
  762. *
  763. * Description:
  764. * This function does little more than loop over the pipe and call
  765. * @actor to do the actual moving of a single struct pipe_buffer to
  766. * the desired destination. See pipe_to_file, pipe_to_sendpage, or
  767. * pipe_to_user.
  768. *
  769. */
  770. ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
  771. splice_actor *actor)
  772. {
  773. int ret;
  774. splice_from_pipe_begin(sd);
  775. do {
  776. ret = splice_from_pipe_next(pipe, sd);
  777. if (ret > 0)
  778. ret = splice_from_pipe_feed(pipe, sd, actor);
  779. } while (ret > 0);
  780. splice_from_pipe_end(pipe, sd);
  781. return sd->num_spliced ? sd->num_spliced : ret;
  782. }
  783. EXPORT_SYMBOL(__splice_from_pipe);
  784. /**
  785. * splice_from_pipe - splice data from a pipe to a file
  786. * @pipe: pipe to splice from
  787. * @out: file to splice to
  788. * @ppos: position in @out
  789. * @len: how many bytes to splice
  790. * @flags: splice modifier flags
  791. * @actor: handler that splices the data
  792. *
  793. * Description:
  794. * See __splice_from_pipe. This function locks the pipe inode,
  795. * otherwise it's identical to __splice_from_pipe().
  796. *
  797. */
  798. ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
  799. loff_t *ppos, size_t len, unsigned int flags,
  800. splice_actor *actor)
  801. {
  802. ssize_t ret;
  803. struct splice_desc sd = {
  804. .total_len = len,
  805. .flags = flags,
  806. .pos = *ppos,
  807. .u.file = out,
  808. };
  809. pipe_lock(pipe);
  810. ret = __splice_from_pipe(pipe, &sd, actor);
  811. pipe_unlock(pipe);
  812. return ret;
  813. }
  814. /**
  815. * generic_file_splice_write - splice data from a pipe to a file
  816. * @pipe: pipe info
  817. * @out: file to write to
  818. * @ppos: position in @out
  819. * @len: number of bytes to splice
  820. * @flags: splice modifier flags
  821. *
  822. * Description:
  823. * Will either move or copy pages (determined by @flags options) from
  824. * the given pipe inode to the given file.
  825. *
  826. */
  827. ssize_t
  828. generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  829. loff_t *ppos, size_t len, unsigned int flags)
  830. {
  831. struct address_space *mapping = out->f_mapping;
  832. struct inode *inode = mapping->host;
  833. struct splice_desc sd = {
  834. .total_len = len,
  835. .flags = flags,
  836. .pos = *ppos,
  837. .u.file = out,
  838. };
  839. ssize_t ret;
  840. pipe_lock(pipe);
  841. splice_from_pipe_begin(&sd);
  842. do {
  843. ret = splice_from_pipe_next(pipe, &sd);
  844. if (ret <= 0)
  845. break;
  846. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  847. ret = file_remove_suid(out);
  848. if (!ret) {
  849. file_update_time(out);
  850. ret = splice_from_pipe_feed(pipe, &sd, pipe_to_file);
  851. }
  852. mutex_unlock(&inode->i_mutex);
  853. } while (ret > 0);
  854. splice_from_pipe_end(pipe, &sd);
  855. pipe_unlock(pipe);
  856. if (sd.num_spliced)
  857. ret = sd.num_spliced;
  858. if (ret > 0) {
  859. unsigned long nr_pages;
  860. int err;
  861. nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  862. err = generic_write_sync(out, *ppos, ret);
  863. if (err)
  864. ret = err;
  865. else
  866. *ppos += ret;
  867. balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
  868. }
  869. return ret;
  870. }
  871. EXPORT_SYMBOL(generic_file_splice_write);
  872. static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  873. struct splice_desc *sd)
  874. {
  875. int ret;
  876. void *data;
  877. ret = buf->ops->confirm(pipe, buf);
  878. if (ret)
  879. return ret;
  880. data = buf->ops->map(pipe, buf, 0);
  881. ret = kernel_write(sd->u.file, data + buf->offset, sd->len, sd->pos);
  882. buf->ops->unmap(pipe, buf, data);
  883. return ret;
  884. }
  885. static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
  886. struct file *out, loff_t *ppos,
  887. size_t len, unsigned int flags)
  888. {
  889. ssize_t ret;
  890. ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
  891. if (ret > 0)
  892. *ppos += ret;
  893. return ret;
  894. }
  895. /**
  896. * generic_splice_sendpage - splice data from a pipe to a socket
  897. * @pipe: pipe to splice from
  898. * @out: socket to write to
  899. * @ppos: position in @out
  900. * @len: number of bytes to splice
  901. * @flags: splice modifier flags
  902. *
  903. * Description:
  904. * Will send @len bytes from the pipe to a network socket. No data copying
  905. * is involved.
  906. *
  907. */
  908. ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
  909. loff_t *ppos, size_t len, unsigned int flags)
  910. {
  911. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
  912. }
  913. EXPORT_SYMBOL(generic_splice_sendpage);
  914. /*
  915. * Attempt to initiate a splice from pipe to file.
  916. */
  917. static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  918. loff_t *ppos, size_t len, unsigned int flags)
  919. {
  920. ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
  921. loff_t *, size_t, unsigned int);
  922. int ret;
  923. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  924. return -EBADF;
  925. if (unlikely(out->f_flags & O_APPEND))
  926. return -EINVAL;
  927. ret = rw_verify_area(WRITE, out, ppos, len);
  928. if (unlikely(ret < 0))
  929. return ret;
  930. if (out->f_op && out->f_op->splice_write)
  931. splice_write = out->f_op->splice_write;
  932. else
  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. if (in->f_op && in->f_op->splice_read)
  952. splice_read = in->f_op->splice_read;
  953. else
  954. splice_read = default_file_splice_read;
  955. return splice_read(in, ppos, pipe, len, flags);
  956. }
  957. /**
  958. * splice_direct_to_actor - splices data directly between two non-pipes
  959. * @in: file to splice from
  960. * @sd: actor information on where to splice to
  961. * @actor: handles the data splicing
  962. *
  963. * Description:
  964. * This is a special case helper to splice directly between two
  965. * points, without requiring an explicit pipe. Internally an allocated
  966. * pipe is cached in the process, and reused during the lifetime of
  967. * that process.
  968. *
  969. */
  970. ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
  971. splice_direct_actor *actor)
  972. {
  973. struct pipe_inode_info *pipe;
  974. long ret, bytes;
  975. umode_t i_mode;
  976. size_t len;
  977. int i, flags;
  978. /*
  979. * We require the input being a regular file, as we don't want to
  980. * randomly drop data for eg socket -> socket splicing. Use the
  981. * piped splicing for that!
  982. */
  983. i_mode = in->f_path.dentry->d_inode->i_mode;
  984. if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  985. return -EINVAL;
  986. /*
  987. * neither in nor out is a pipe, setup an internal pipe attached to
  988. * 'out' and transfer the wanted data from 'in' to 'out' through that
  989. */
  990. pipe = current->splice_pipe;
  991. if (unlikely(!pipe)) {
  992. pipe = alloc_pipe_info(NULL);
  993. if (!pipe)
  994. return -ENOMEM;
  995. /*
  996. * We don't have an immediate reader, but we'll read the stuff
  997. * out of the pipe right after the splice_to_pipe(). So set
  998. * PIPE_READERS appropriately.
  999. */
  1000. pipe->readers = 1;
  1001. current->splice_pipe = pipe;
  1002. }
  1003. /*
  1004. * Do the splice.
  1005. */
  1006. ret = 0;
  1007. bytes = 0;
  1008. len = sd->total_len;
  1009. flags = sd->flags;
  1010. /*
  1011. * Don't block on output, we have to drain the direct pipe.
  1012. */
  1013. sd->flags &= ~SPLICE_F_NONBLOCK;
  1014. while (len) {
  1015. size_t read_len;
  1016. loff_t pos = sd->pos, prev_pos = pos;
  1017. ret = do_splice_to(in, &pos, pipe, len, flags);
  1018. if (unlikely(ret <= 0))
  1019. goto out_release;
  1020. read_len = ret;
  1021. sd->total_len = read_len;
  1022. /*
  1023. * NOTE: nonblocking mode only applies to the input. We
  1024. * must not do the output in nonblocking mode as then we
  1025. * could get stuck data in the internal pipe:
  1026. */
  1027. ret = actor(pipe, sd);
  1028. if (unlikely(ret <= 0)) {
  1029. sd->pos = prev_pos;
  1030. goto out_release;
  1031. }
  1032. bytes += ret;
  1033. len -= ret;
  1034. sd->pos = pos;
  1035. if (ret < read_len) {
  1036. sd->pos = prev_pos + ret;
  1037. goto out_release;
  1038. }
  1039. }
  1040. done:
  1041. pipe->nrbufs = pipe->curbuf = 0;
  1042. file_accessed(in);
  1043. return bytes;
  1044. out_release:
  1045. /*
  1046. * If we did an incomplete transfer we must release
  1047. * the pipe buffers in question:
  1048. */
  1049. for (i = 0; i < PIPE_BUFFERS; i++) {
  1050. struct pipe_buffer *buf = pipe->bufs + i;
  1051. if (buf->ops) {
  1052. buf->ops->release(pipe, buf);
  1053. buf->ops = NULL;
  1054. }
  1055. }
  1056. if (!bytes)
  1057. bytes = ret;
  1058. goto done;
  1059. }
  1060. EXPORT_SYMBOL(splice_direct_to_actor);
  1061. static int direct_splice_actor(struct pipe_inode_info *pipe,
  1062. struct splice_desc *sd)
  1063. {
  1064. struct file *file = sd->u.file;
  1065. return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
  1066. }
  1067. /**
  1068. * do_splice_direct - splices data directly between two files
  1069. * @in: file to splice from
  1070. * @ppos: input file offset
  1071. * @out: file to splice to
  1072. * @len: number of bytes to splice
  1073. * @flags: splice modifier flags
  1074. *
  1075. * Description:
  1076. * For use by do_sendfile(). splice can easily emulate sendfile, but
  1077. * doing it in the application would incur an extra system call
  1078. * (splice in + splice out, as compared to just sendfile()). So this helper
  1079. * can splice directly through a process-private pipe.
  1080. *
  1081. */
  1082. long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
  1083. size_t len, unsigned int flags)
  1084. {
  1085. struct splice_desc sd = {
  1086. .len = len,
  1087. .total_len = len,
  1088. .flags = flags,
  1089. .pos = *ppos,
  1090. .u.file = out,
  1091. };
  1092. long ret;
  1093. ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
  1094. if (ret > 0)
  1095. *ppos = sd.pos;
  1096. return ret;
  1097. }
  1098. static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  1099. struct pipe_inode_info *opipe,
  1100. size_t len, unsigned int flags);
  1101. /*
  1102. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  1103. * location, so checking ->i_pipe is not enough to verify that this is a
  1104. * pipe.
  1105. */
  1106. static inline struct pipe_inode_info *pipe_info(struct inode *inode)
  1107. {
  1108. if (S_ISFIFO(inode->i_mode))
  1109. return inode->i_pipe;
  1110. return NULL;
  1111. }
  1112. /*
  1113. * Determine where to splice to/from.
  1114. */
  1115. static long do_splice(struct file *in, loff_t __user *off_in,
  1116. struct file *out, loff_t __user *off_out,
  1117. size_t len, unsigned int flags)
  1118. {
  1119. struct pipe_inode_info *ipipe;
  1120. struct pipe_inode_info *opipe;
  1121. loff_t offset, *off;
  1122. long ret;
  1123. ipipe = pipe_info(in->f_path.dentry->d_inode);
  1124. opipe = pipe_info(out->f_path.dentry->d_inode);
  1125. if (ipipe && opipe) {
  1126. if (off_in || off_out)
  1127. return -ESPIPE;
  1128. if (!(in->f_mode & FMODE_READ))
  1129. return -EBADF;
  1130. if (!(out->f_mode & FMODE_WRITE))
  1131. return -EBADF;
  1132. /* Splicing to self would be fun, but... */
  1133. if (ipipe == opipe)
  1134. return -EINVAL;
  1135. return splice_pipe_to_pipe(ipipe, opipe, len, flags);
  1136. }
  1137. if (ipipe) {
  1138. if (off_in)
  1139. return -ESPIPE;
  1140. if (off_out) {
  1141. if (!out->f_op || !out->f_op->llseek ||
  1142. out->f_op->llseek == no_llseek)
  1143. return -EINVAL;
  1144. if (copy_from_user(&offset, off_out, sizeof(loff_t)))
  1145. return -EFAULT;
  1146. off = &offset;
  1147. } else
  1148. off = &out->f_pos;
  1149. ret = do_splice_from(ipipe, out, off, len, flags);
  1150. if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
  1151. ret = -EFAULT;
  1152. return ret;
  1153. }
  1154. if (opipe) {
  1155. if (off_out)
  1156. return -ESPIPE;
  1157. if (off_in) {
  1158. if (!in->f_op || !in->f_op->llseek ||
  1159. in->f_op->llseek == no_llseek)
  1160. return -EINVAL;
  1161. if (copy_from_user(&offset, off_in, sizeof(loff_t)))
  1162. return -EFAULT;
  1163. off = &offset;
  1164. } else
  1165. off = &in->f_pos;
  1166. ret = do_splice_to(in, off, opipe, len, flags);
  1167. if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
  1168. ret = -EFAULT;
  1169. return ret;
  1170. }
  1171. return -EINVAL;
  1172. }
  1173. /*
  1174. * Map an iov into an array of pages and offset/length tupples. With the
  1175. * partial_page structure, we can map several non-contiguous ranges into
  1176. * our ones pages[] map instead of splitting that operation into pieces.
  1177. * Could easily be exported as a generic helper for other users, in which
  1178. * case one would probably want to add a 'max_nr_pages' parameter as well.
  1179. */
  1180. static int get_iovec_page_array(const struct iovec __user *iov,
  1181. unsigned int nr_vecs, struct page **pages,
  1182. struct partial_page *partial, int aligned)
  1183. {
  1184. int buffers = 0, error = 0;
  1185. while (nr_vecs) {
  1186. unsigned long off, npages;
  1187. struct iovec entry;
  1188. void __user *base;
  1189. size_t len;
  1190. int i;
  1191. error = -EFAULT;
  1192. if (copy_from_user(&entry, iov, sizeof(entry)))
  1193. break;
  1194. base = entry.iov_base;
  1195. len = entry.iov_len;
  1196. /*
  1197. * Sanity check this iovec. 0 read succeeds.
  1198. */
  1199. error = 0;
  1200. if (unlikely(!len))
  1201. break;
  1202. error = -EFAULT;
  1203. if (!access_ok(VERIFY_READ, base, len))
  1204. break;
  1205. /*
  1206. * Get this base offset and number of pages, then map
  1207. * in the user pages.
  1208. */
  1209. off = (unsigned long) base & ~PAGE_MASK;
  1210. /*
  1211. * If asked for alignment, the offset must be zero and the
  1212. * length a multiple of the PAGE_SIZE.
  1213. */
  1214. error = -EINVAL;
  1215. if (aligned && (off || len & ~PAGE_MASK))
  1216. break;
  1217. npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1218. if (npages > PIPE_BUFFERS - buffers)
  1219. npages = PIPE_BUFFERS - buffers;
  1220. error = get_user_pages_fast((unsigned long)base, npages,
  1221. 0, &pages[buffers]);
  1222. if (unlikely(error <= 0))
  1223. break;
  1224. /*
  1225. * Fill this contiguous range into the partial page map.
  1226. */
  1227. for (i = 0; i < error; i++) {
  1228. const int plen = min_t(size_t, len, PAGE_SIZE - off);
  1229. partial[buffers].offset = off;
  1230. partial[buffers].len = plen;
  1231. off = 0;
  1232. len -= plen;
  1233. buffers++;
  1234. }
  1235. /*
  1236. * We didn't complete this iov, stop here since it probably
  1237. * means we have to move some of this into a pipe to
  1238. * be able to continue.
  1239. */
  1240. if (len)
  1241. break;
  1242. /*
  1243. * Don't continue if we mapped fewer pages than we asked for,
  1244. * or if we mapped the max number of pages that we have
  1245. * room for.
  1246. */
  1247. if (error < npages || buffers == PIPE_BUFFERS)
  1248. break;
  1249. nr_vecs--;
  1250. iov++;
  1251. }
  1252. if (buffers)
  1253. return buffers;
  1254. return error;
  1255. }
  1256. static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  1257. struct splice_desc *sd)
  1258. {
  1259. char *src;
  1260. int ret;
  1261. ret = buf->ops->confirm(pipe, buf);
  1262. if (unlikely(ret))
  1263. return ret;
  1264. /*
  1265. * See if we can use the atomic maps, by prefaulting in the
  1266. * pages and doing an atomic copy
  1267. */
  1268. if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) {
  1269. src = buf->ops->map(pipe, buf, 1);
  1270. ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset,
  1271. sd->len);
  1272. buf->ops->unmap(pipe, buf, src);
  1273. if (!ret) {
  1274. ret = sd->len;
  1275. goto out;
  1276. }
  1277. }
  1278. /*
  1279. * No dice, use slow non-atomic map and copy
  1280. */
  1281. src = buf->ops->map(pipe, buf, 0);
  1282. ret = sd->len;
  1283. if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len))
  1284. ret = -EFAULT;
  1285. buf->ops->unmap(pipe, buf, src);
  1286. out:
  1287. if (ret > 0)
  1288. sd->u.userptr += ret;
  1289. return ret;
  1290. }
  1291. /*
  1292. * For lack of a better implementation, implement vmsplice() to userspace
  1293. * as a simple copy of the pipes pages to the user iov.
  1294. */
  1295. static long vmsplice_to_user(struct file *file, const struct iovec __user *iov,
  1296. unsigned long nr_segs, unsigned int flags)
  1297. {
  1298. struct pipe_inode_info *pipe;
  1299. struct splice_desc sd;
  1300. ssize_t size;
  1301. int error;
  1302. long ret;
  1303. pipe = pipe_info(file->f_path.dentry->d_inode);
  1304. if (!pipe)
  1305. return -EBADF;
  1306. pipe_lock(pipe);
  1307. error = ret = 0;
  1308. while (nr_segs) {
  1309. void __user *base;
  1310. size_t len;
  1311. /*
  1312. * Get user address base and length for this iovec.
  1313. */
  1314. error = get_user(base, &iov->iov_base);
  1315. if (unlikely(error))
  1316. break;
  1317. error = get_user(len, &iov->iov_len);
  1318. if (unlikely(error))
  1319. break;
  1320. /*
  1321. * Sanity check this iovec. 0 read succeeds.
  1322. */
  1323. if (unlikely(!len))
  1324. break;
  1325. if (unlikely(!base)) {
  1326. error = -EFAULT;
  1327. break;
  1328. }
  1329. if (unlikely(!access_ok(VERIFY_WRITE, base, len))) {
  1330. error = -EFAULT;
  1331. break;
  1332. }
  1333. sd.len = 0;
  1334. sd.total_len = len;
  1335. sd.flags = flags;
  1336. sd.u.userptr = base;
  1337. sd.pos = 0;
  1338. size = __splice_from_pipe(pipe, &sd, pipe_to_user);
  1339. if (size < 0) {
  1340. if (!ret)
  1341. ret = size;
  1342. break;
  1343. }
  1344. ret += size;
  1345. if (size < len)
  1346. break;
  1347. nr_segs--;
  1348. iov++;
  1349. }
  1350. pipe_unlock(pipe);
  1351. if (!ret)
  1352. ret = error;
  1353. return ret;
  1354. }
  1355. /*
  1356. * vmsplice splices a user address range into a pipe. It can be thought of
  1357. * as splice-from-memory, where the regular splice is splice-from-file (or
  1358. * to file). In both cases the output is a pipe, naturally.
  1359. */
  1360. static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
  1361. unsigned long nr_segs, unsigned int flags)
  1362. {
  1363. struct pipe_inode_info *pipe;
  1364. struct page *pages[PIPE_BUFFERS];
  1365. struct partial_page partial[PIPE_BUFFERS];
  1366. struct splice_pipe_desc spd = {
  1367. .pages = pages,
  1368. .partial = partial,
  1369. .flags = flags,
  1370. .ops = &user_page_pipe_buf_ops,
  1371. .spd_release = spd_release_page,
  1372. };
  1373. pipe = pipe_info(file->f_path.dentry->d_inode);
  1374. if (!pipe)
  1375. return -EBADF;
  1376. spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
  1377. flags & SPLICE_F_GIFT);
  1378. if (spd.nr_pages <= 0)
  1379. return spd.nr_pages;
  1380. return splice_to_pipe(pipe, &spd);
  1381. }
  1382. /*
  1383. * Note that vmsplice only really supports true splicing _from_ user memory
  1384. * to a pipe, not the other way around. Splicing from user memory is a simple
  1385. * operation that can be supported without any funky alignment restrictions
  1386. * or nasty vm tricks. We simply map in the user memory and fill them into
  1387. * a pipe. The reverse isn't quite as easy, though. There are two possible
  1388. * solutions for that:
  1389. *
  1390. * - memcpy() the data internally, at which point we might as well just
  1391. * do a regular read() on the buffer anyway.
  1392. * - Lots of nasty vm tricks, that are neither fast nor flexible (it
  1393. * has restriction limitations on both ends of the pipe).
  1394. *
  1395. * Currently we punt and implement it as a normal copy, see pipe_to_user().
  1396. *
  1397. */
  1398. SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
  1399. unsigned long, nr_segs, unsigned int, flags)
  1400. {
  1401. struct file *file;
  1402. long error;
  1403. int fput;
  1404. if (unlikely(nr_segs > UIO_MAXIOV))
  1405. return -EINVAL;
  1406. else if (unlikely(!nr_segs))
  1407. return 0;
  1408. error = -EBADF;
  1409. file = fget_light(fd, &fput);
  1410. if (file) {
  1411. if (file->f_mode & FMODE_WRITE)
  1412. error = vmsplice_to_pipe(file, iov, nr_segs, flags);
  1413. else if (file->f_mode & FMODE_READ)
  1414. error = vmsplice_to_user(file, iov, nr_segs, flags);
  1415. fput_light(file, fput);
  1416. }
  1417. return error;
  1418. }
  1419. SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
  1420. int, fd_out, loff_t __user *, off_out,
  1421. size_t, len, unsigned int, flags)
  1422. {
  1423. long error;
  1424. struct file *in, *out;
  1425. int fput_in, fput_out;
  1426. if (unlikely(!len))
  1427. return 0;
  1428. error = -EBADF;
  1429. in = fget_light(fd_in, &fput_in);
  1430. if (in) {
  1431. if (in->f_mode & FMODE_READ) {
  1432. out = fget_light(fd_out, &fput_out);
  1433. if (out) {
  1434. if (out->f_mode & FMODE_WRITE)
  1435. error = do_splice(in, off_in,
  1436. out, off_out,
  1437. len, flags);
  1438. fput_light(out, fput_out);
  1439. }
  1440. }
  1441. fput_light(in, fput_in);
  1442. }
  1443. return error;
  1444. }
  1445. /*
  1446. * Make sure there's data to read. Wait for input if we can, otherwise
  1447. * return an appropriate error.
  1448. */
  1449. static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1450. {
  1451. int ret;
  1452. /*
  1453. * Check ->nrbufs without the inode lock first. This function
  1454. * is speculative anyways, so missing one is ok.
  1455. */
  1456. if (pipe->nrbufs)
  1457. return 0;
  1458. ret = 0;
  1459. pipe_lock(pipe);
  1460. while (!pipe->nrbufs) {
  1461. if (signal_pending(current)) {
  1462. ret = -ERESTARTSYS;
  1463. break;
  1464. }
  1465. if (!pipe->writers)
  1466. break;
  1467. if (!pipe->waiting_writers) {
  1468. if (flags & SPLICE_F_NONBLOCK) {
  1469. ret = -EAGAIN;
  1470. break;
  1471. }
  1472. }
  1473. pipe_wait(pipe);
  1474. }
  1475. pipe_unlock(pipe);
  1476. return ret;
  1477. }
  1478. /*
  1479. * Make sure there's writeable room. Wait for room if we can, otherwise
  1480. * return an appropriate error.
  1481. */
  1482. static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1483. {
  1484. int ret;
  1485. /*
  1486. * Check ->nrbufs without the inode lock first. This function
  1487. * is speculative anyways, so missing one is ok.
  1488. */
  1489. if (pipe->nrbufs < PIPE_BUFFERS)
  1490. return 0;
  1491. ret = 0;
  1492. pipe_lock(pipe);
  1493. while (pipe->nrbufs >= PIPE_BUFFERS) {
  1494. if (!pipe->readers) {
  1495. send_sig(SIGPIPE, current, 0);
  1496. ret = -EPIPE;
  1497. break;
  1498. }
  1499. if (flags & SPLICE_F_NONBLOCK) {
  1500. ret = -EAGAIN;
  1501. break;
  1502. }
  1503. if (signal_pending(current)) {
  1504. ret = -ERESTARTSYS;
  1505. break;
  1506. }
  1507. pipe->waiting_writers++;
  1508. pipe_wait(pipe);
  1509. pipe->waiting_writers--;
  1510. }
  1511. pipe_unlock(pipe);
  1512. return ret;
  1513. }
  1514. /*
  1515. * Splice contents of ipipe to opipe.
  1516. */
  1517. static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  1518. struct pipe_inode_info *opipe,
  1519. size_t len, unsigned int flags)
  1520. {
  1521. struct pipe_buffer *ibuf, *obuf;
  1522. int ret = 0, nbuf;
  1523. bool input_wakeup = false;
  1524. retry:
  1525. ret = ipipe_prep(ipipe, flags);
  1526. if (ret)
  1527. return ret;
  1528. ret = opipe_prep(opipe, flags);
  1529. if (ret)
  1530. return ret;
  1531. /*
  1532. * Potential ABBA deadlock, work around it by ordering lock
  1533. * grabbing by pipe info address. Otherwise two different processes
  1534. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1535. */
  1536. pipe_double_lock(ipipe, opipe);
  1537. do {
  1538. if (!opipe->readers) {
  1539. send_sig(SIGPIPE, current, 0);
  1540. if (!ret)
  1541. ret = -EPIPE;
  1542. break;
  1543. }
  1544. if (!ipipe->nrbufs && !ipipe->writers)
  1545. break;
  1546. /*
  1547. * Cannot make any progress, because either the input
  1548. * pipe is empty or the output pipe is full.
  1549. */
  1550. if (!ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS) {
  1551. /* Already processed some buffers, break */
  1552. if (ret)
  1553. break;
  1554. if (flags & SPLICE_F_NONBLOCK) {
  1555. ret = -EAGAIN;
  1556. break;
  1557. }
  1558. /*
  1559. * We raced with another reader/writer and haven't
  1560. * managed to process any buffers. A zero return
  1561. * value means EOF, so retry instead.
  1562. */
  1563. pipe_unlock(ipipe);
  1564. pipe_unlock(opipe);
  1565. goto retry;
  1566. }
  1567. ibuf = ipipe->bufs + ipipe->curbuf;
  1568. nbuf = (opipe->curbuf + opipe->nrbufs) % PIPE_BUFFERS;
  1569. obuf = opipe->bufs + nbuf;
  1570. if (len >= ibuf->len) {
  1571. /*
  1572. * Simply move the whole buffer from ipipe to opipe
  1573. */
  1574. *obuf = *ibuf;
  1575. ibuf->ops = NULL;
  1576. opipe->nrbufs++;
  1577. ipipe->curbuf = (ipipe->curbuf + 1) % PIPE_BUFFERS;
  1578. ipipe->nrbufs--;
  1579. input_wakeup = true;
  1580. } else {
  1581. /*
  1582. * Get a reference to this pipe buffer,
  1583. * so we can copy the contents over.
  1584. */
  1585. ibuf->ops->get(ipipe, ibuf);
  1586. *obuf = *ibuf;
  1587. /*
  1588. * Don't inherit the gift flag, we need to
  1589. * prevent multiple steals of this page.
  1590. */
  1591. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1592. obuf->len = len;
  1593. opipe->nrbufs++;
  1594. ibuf->offset += obuf->len;
  1595. ibuf->len -= obuf->len;
  1596. }
  1597. ret += obuf->len;
  1598. len -= obuf->len;
  1599. } while (len);
  1600. pipe_unlock(ipipe);
  1601. pipe_unlock(opipe);
  1602. /*
  1603. * If we put data in the output pipe, wakeup any potential readers.
  1604. */
  1605. if (ret > 0) {
  1606. smp_mb();
  1607. if (waitqueue_active(&opipe->wait))
  1608. wake_up_interruptible(&opipe->wait);
  1609. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  1610. }
  1611. if (input_wakeup)
  1612. wakeup_pipe_writers(ipipe);
  1613. return ret;
  1614. }
  1615. /*
  1616. * Link contents of ipipe to opipe.
  1617. */
  1618. static int link_pipe(struct pipe_inode_info *ipipe,
  1619. struct pipe_inode_info *opipe,
  1620. size_t len, unsigned int flags)
  1621. {
  1622. struct pipe_buffer *ibuf, *obuf;
  1623. int ret = 0, i = 0, nbuf;
  1624. /*
  1625. * Potential ABBA deadlock, work around it by ordering lock
  1626. * grabbing by pipe info address. Otherwise two different processes
  1627. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1628. */
  1629. pipe_double_lock(ipipe, opipe);
  1630. do {
  1631. if (!opipe->readers) {
  1632. send_sig(SIGPIPE, current, 0);
  1633. if (!ret)
  1634. ret = -EPIPE;
  1635. break;
  1636. }
  1637. /*
  1638. * If we have iterated all input buffers or ran out of
  1639. * output room, break.
  1640. */
  1641. if (i >= ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS)
  1642. break;
  1643. ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
  1644. nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
  1645. /*
  1646. * Get a reference to this pipe buffer,
  1647. * so we can copy the contents over.
  1648. */
  1649. ibuf->ops->get(ipipe, ibuf);
  1650. obuf = opipe->bufs + nbuf;
  1651. *obuf = *ibuf;
  1652. /*
  1653. * Don't inherit the gift flag, we need to
  1654. * prevent multiple steals of this page.
  1655. */
  1656. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1657. if (obuf->len > len)
  1658. obuf->len = len;
  1659. opipe->nrbufs++;
  1660. ret += obuf->len;
  1661. len -= obuf->len;
  1662. i++;
  1663. } while (len);
  1664. /*
  1665. * return EAGAIN if we have the potential of some data in the
  1666. * future, otherwise just return 0
  1667. */
  1668. if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
  1669. ret = -EAGAIN;
  1670. pipe_unlock(ipipe);
  1671. pipe_unlock(opipe);
  1672. /*
  1673. * If we put data in the output pipe, wakeup any potential readers.
  1674. */
  1675. if (ret > 0) {
  1676. smp_mb();
  1677. if (waitqueue_active(&opipe->wait))
  1678. wake_up_interruptible(&opipe->wait);
  1679. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  1680. }
  1681. return ret;
  1682. }
  1683. /*
  1684. * This is a tee(1) implementation that works on pipes. It doesn't copy
  1685. * any data, it simply references the 'in' pages on the 'out' pipe.
  1686. * The 'flags' used are the SPLICE_F_* variants, currently the only
  1687. * applicable one is SPLICE_F_NONBLOCK.
  1688. */
  1689. static long do_tee(struct file *in, struct file *out, size_t len,
  1690. unsigned int flags)
  1691. {
  1692. struct pipe_inode_info *ipipe = pipe_info(in->f_path.dentry->d_inode);
  1693. struct pipe_inode_info *opipe = pipe_info(out->f_path.dentry->d_inode);
  1694. int ret = -EINVAL;
  1695. /*
  1696. * Duplicate the contents of ipipe to opipe without actually
  1697. * copying the data.
  1698. */
  1699. if (ipipe && opipe && ipipe != opipe) {
  1700. /*
  1701. * Keep going, unless we encounter an error. The ipipe/opipe
  1702. * ordering doesn't really matter.
  1703. */
  1704. ret = ipipe_prep(ipipe, flags);
  1705. if (!ret) {
  1706. ret = opipe_prep(opipe, flags);
  1707. if (!ret)
  1708. ret = link_pipe(ipipe, opipe, len, flags);
  1709. }
  1710. }
  1711. return ret;
  1712. }
  1713. SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
  1714. {
  1715. struct file *in;
  1716. int error, fput_in;
  1717. if (unlikely(!len))
  1718. return 0;
  1719. error = -EBADF;
  1720. in = fget_light(fdin, &fput_in);
  1721. if (in) {
  1722. if (in->f_mode & FMODE_READ) {
  1723. int fput_out;
  1724. struct file *out = fget_light(fdout, &fput_out);
  1725. if (out) {
  1726. if (out->f_mode & FMODE_WRITE)
  1727. error = do_tee(in, out, len, flags);
  1728. fput_light(out, fput_out);
  1729. }
  1730. }
  1731. fput_light(in, fput_in);
  1732. }
  1733. return error;
  1734. }