splice.c 41 KB

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