splice.c 36 KB

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