splice.c 40 KB

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