splice.c 39 KB

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