splice.c 40 KB

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