splice.c 38 KB

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