splice.c 45 KB

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