splice.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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@suse.de>
  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. /*
  31. * Passed to the actors
  32. */
  33. struct splice_desc {
  34. unsigned int len, total_len; /* current and remaining length */
  35. unsigned int flags; /* splice flags */
  36. struct file *file; /* file to read/write */
  37. loff_t pos; /* file position */
  38. };
  39. /*
  40. * Attempt to steal a page from a pipe buffer. This should perhaps go into
  41. * a vm helper function, it's already simplified quite a bit by the
  42. * addition of remove_mapping(). If success is returned, the caller may
  43. * attempt to reuse this page for another destination.
  44. */
  45. static int page_cache_pipe_buf_steal(struct pipe_inode_info *info,
  46. struct pipe_buffer *buf)
  47. {
  48. struct page *page = buf->page;
  49. struct address_space *mapping = page_mapping(page);
  50. lock_page(page);
  51. WARN_ON(!PageUptodate(page));
  52. /*
  53. * At least for ext2 with nobh option, we need to wait on writeback
  54. * completing on this page, since we'll remove it from the pagecache.
  55. * Otherwise truncate wont wait on the page, allowing the disk
  56. * blocks to be reused by someone else before we actually wrote our
  57. * data to them. fs corruption ensues.
  58. */
  59. wait_on_page_writeback(page);
  60. if (PagePrivate(page))
  61. try_to_release_page(page, mapping_gfp_mask(mapping));
  62. if (!remove_mapping(mapping, page)) {
  63. unlock_page(page);
  64. return 1;
  65. }
  66. buf->flags |= PIPE_BUF_FLAG_STOLEN | PIPE_BUF_FLAG_LRU;
  67. return 0;
  68. }
  69. static void page_cache_pipe_buf_release(struct pipe_inode_info *info,
  70. struct pipe_buffer *buf)
  71. {
  72. page_cache_release(buf->page);
  73. buf->page = NULL;
  74. buf->flags &= ~(PIPE_BUF_FLAG_STOLEN | PIPE_BUF_FLAG_LRU);
  75. }
  76. static void *page_cache_pipe_buf_map(struct file *file,
  77. struct pipe_inode_info *info,
  78. struct pipe_buffer *buf)
  79. {
  80. struct page *page = buf->page;
  81. int err;
  82. if (!PageUptodate(page)) {
  83. lock_page(page);
  84. /*
  85. * Page got truncated/unhashed. This will cause a 0-byte
  86. * splice, if this is the first page.
  87. */
  88. if (!page->mapping) {
  89. err = -ENODATA;
  90. goto error;
  91. }
  92. /*
  93. * Uh oh, read-error from disk.
  94. */
  95. if (!PageUptodate(page)) {
  96. err = -EIO;
  97. goto error;
  98. }
  99. /*
  100. * Page is ok afterall, fall through to mapping.
  101. */
  102. unlock_page(page);
  103. }
  104. return kmap(page);
  105. error:
  106. unlock_page(page);
  107. return ERR_PTR(err);
  108. }
  109. static void page_cache_pipe_buf_unmap(struct pipe_inode_info *info,
  110. struct pipe_buffer *buf)
  111. {
  112. kunmap(buf->page);
  113. }
  114. static void page_cache_pipe_buf_get(struct pipe_inode_info *info,
  115. struct pipe_buffer *buf)
  116. {
  117. page_cache_get(buf->page);
  118. }
  119. static struct pipe_buf_operations page_cache_pipe_buf_ops = {
  120. .can_merge = 0,
  121. .map = page_cache_pipe_buf_map,
  122. .unmap = page_cache_pipe_buf_unmap,
  123. .release = page_cache_pipe_buf_release,
  124. .steal = page_cache_pipe_buf_steal,
  125. .get = page_cache_pipe_buf_get,
  126. };
  127. /*
  128. * Pipe output worker. This sets up our pipe format with the page cache
  129. * pipe buffer operations. Otherwise very similar to the regular pipe_writev().
  130. */
  131. static ssize_t move_to_pipe(struct pipe_inode_info *pipe, struct page **pages,
  132. int nr_pages, unsigned long len,
  133. unsigned int offset, unsigned int flags)
  134. {
  135. int ret, do_wakeup, i;
  136. ret = 0;
  137. do_wakeup = 0;
  138. i = 0;
  139. if (pipe->inode)
  140. mutex_lock(&pipe->inode->i_mutex);
  141. for (;;) {
  142. if (!pipe->readers) {
  143. send_sig(SIGPIPE, current, 0);
  144. if (!ret)
  145. ret = -EPIPE;
  146. break;
  147. }
  148. if (pipe->nrbufs < PIPE_BUFFERS) {
  149. int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1);
  150. struct pipe_buffer *buf = pipe->bufs + newbuf;
  151. struct page *page = pages[i++];
  152. unsigned long this_len;
  153. this_len = PAGE_CACHE_SIZE - offset;
  154. if (this_len > len)
  155. this_len = len;
  156. buf->page = page;
  157. buf->offset = offset;
  158. buf->len = this_len;
  159. buf->ops = &page_cache_pipe_buf_ops;
  160. pipe->nrbufs++;
  161. if (pipe->inode)
  162. do_wakeup = 1;
  163. ret += this_len;
  164. len -= this_len;
  165. offset = 0;
  166. if (!--nr_pages)
  167. break;
  168. if (!len)
  169. break;
  170. if (pipe->nrbufs < PIPE_BUFFERS)
  171. continue;
  172. break;
  173. }
  174. if (flags & SPLICE_F_NONBLOCK) {
  175. if (!ret)
  176. ret = -EAGAIN;
  177. break;
  178. }
  179. if (signal_pending(current)) {
  180. if (!ret)
  181. ret = -ERESTARTSYS;
  182. break;
  183. }
  184. if (do_wakeup) {
  185. smp_mb();
  186. if (waitqueue_active(&pipe->wait))
  187. wake_up_interruptible_sync(&pipe->wait);
  188. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  189. do_wakeup = 0;
  190. }
  191. pipe->waiting_writers++;
  192. pipe_wait(pipe);
  193. pipe->waiting_writers--;
  194. }
  195. if (pipe->inode)
  196. mutex_unlock(&pipe->inode->i_mutex);
  197. if (do_wakeup) {
  198. smp_mb();
  199. if (waitqueue_active(&pipe->wait))
  200. wake_up_interruptible(&pipe->wait);
  201. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  202. }
  203. while (i < nr_pages)
  204. page_cache_release(pages[i++]);
  205. return ret;
  206. }
  207. static int
  208. __generic_file_splice_read(struct file *in, loff_t *ppos,
  209. struct pipe_inode_info *pipe, size_t len,
  210. unsigned int flags)
  211. {
  212. struct address_space *mapping = in->f_mapping;
  213. unsigned int loff, offset, nr_pages;
  214. struct page *pages[PIPE_BUFFERS];
  215. struct page *page;
  216. pgoff_t index, end_index;
  217. loff_t isize;
  218. size_t bytes;
  219. int i, error;
  220. index = *ppos >> PAGE_CACHE_SHIFT;
  221. loff = offset = *ppos & ~PAGE_CACHE_MASK;
  222. nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  223. if (nr_pages > PIPE_BUFFERS)
  224. nr_pages = PIPE_BUFFERS;
  225. /*
  226. * Initiate read-ahead on this page range. however, don't call into
  227. * read-ahead if this is a non-zero offset (we are likely doing small
  228. * chunk splice and the page is already there) for a single page.
  229. */
  230. if (!offset || nr_pages > 1)
  231. do_page_cache_readahead(mapping, in, index, nr_pages);
  232. /*
  233. * Now fill in the holes:
  234. */
  235. error = 0;
  236. bytes = 0;
  237. for (i = 0; i < nr_pages; i++, index++) {
  238. find_page:
  239. /*
  240. * lookup the page for this index
  241. */
  242. page = find_get_page(mapping, index);
  243. if (!page) {
  244. /*
  245. * page didn't exist, allocate one
  246. */
  247. page = page_cache_alloc_cold(mapping);
  248. if (!page)
  249. break;
  250. error = add_to_page_cache_lru(page, mapping, index,
  251. mapping_gfp_mask(mapping));
  252. if (unlikely(error)) {
  253. page_cache_release(page);
  254. break;
  255. }
  256. goto readpage;
  257. }
  258. /*
  259. * If the page isn't uptodate, we may need to start io on it
  260. */
  261. if (!PageUptodate(page)) {
  262. /*
  263. * If in nonblock mode then dont block on waiting
  264. * for an in-flight io page
  265. */
  266. if (flags & SPLICE_F_NONBLOCK)
  267. break;
  268. lock_page(page);
  269. /*
  270. * page was truncated, stop here. if this isn't the
  271. * first page, we'll just complete what we already
  272. * added
  273. */
  274. if (!page->mapping) {
  275. unlock_page(page);
  276. page_cache_release(page);
  277. break;
  278. }
  279. /*
  280. * page was already under io and is now done, great
  281. */
  282. if (PageUptodate(page)) {
  283. unlock_page(page);
  284. goto fill_it;
  285. }
  286. readpage:
  287. /*
  288. * need to read in the page
  289. */
  290. error = mapping->a_ops->readpage(in, page);
  291. if (unlikely(error)) {
  292. page_cache_release(page);
  293. if (error == AOP_TRUNCATED_PAGE)
  294. goto find_page;
  295. break;
  296. }
  297. /*
  298. * i_size must be checked after ->readpage().
  299. */
  300. isize = i_size_read(mapping->host);
  301. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  302. if (unlikely(!isize || index > end_index)) {
  303. page_cache_release(page);
  304. break;
  305. }
  306. /*
  307. * if this is the last page, see if we need to shrink
  308. * the length and stop
  309. */
  310. if (end_index == index) {
  311. loff = PAGE_CACHE_SIZE - (isize & ~PAGE_CACHE_MASK);
  312. if (bytes + loff > isize) {
  313. page_cache_release(page);
  314. break;
  315. }
  316. /*
  317. * force quit after adding this page
  318. */
  319. nr_pages = i;
  320. }
  321. }
  322. fill_it:
  323. pages[i] = page;
  324. bytes += PAGE_CACHE_SIZE - loff;
  325. loff = 0;
  326. }
  327. if (i)
  328. return move_to_pipe(pipe, pages, i, bytes, offset, flags);
  329. return error;
  330. }
  331. /**
  332. * generic_file_splice_read - splice data from file to a pipe
  333. * @in: file to splice from
  334. * @pipe: pipe to splice to
  335. * @len: number of bytes to splice
  336. * @flags: splice modifier flags
  337. *
  338. * Will read pages from given file and fill them into a pipe.
  339. */
  340. ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
  341. struct pipe_inode_info *pipe, size_t len,
  342. unsigned int flags)
  343. {
  344. ssize_t spliced;
  345. int ret;
  346. ret = 0;
  347. spliced = 0;
  348. while (len) {
  349. ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
  350. if (ret < 0)
  351. break;
  352. else if (!ret) {
  353. if (spliced)
  354. break;
  355. if (flags & SPLICE_F_NONBLOCK) {
  356. ret = -EAGAIN;
  357. break;
  358. }
  359. }
  360. *ppos += ret;
  361. len -= ret;
  362. spliced += ret;
  363. }
  364. if (spliced)
  365. return spliced;
  366. return ret;
  367. }
  368. EXPORT_SYMBOL(generic_file_splice_read);
  369. /*
  370. * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
  371. * using sendpage().
  372. */
  373. static int pipe_to_sendpage(struct pipe_inode_info *info,
  374. struct pipe_buffer *buf, struct splice_desc *sd)
  375. {
  376. struct file *file = sd->file;
  377. loff_t pos = sd->pos;
  378. unsigned int offset;
  379. ssize_t ret;
  380. void *ptr;
  381. int more;
  382. /*
  383. * Sub-optimal, but we are limited by the pipe ->map. We don't
  384. * need a kmap'ed buffer here, we just want to make sure we
  385. * have the page pinned if the pipe page originates from the
  386. * page cache.
  387. */
  388. ptr = buf->ops->map(file, info, buf);
  389. if (IS_ERR(ptr))
  390. return PTR_ERR(ptr);
  391. offset = pos & ~PAGE_CACHE_MASK;
  392. more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
  393. ret = file->f_op->sendpage(file, buf->page, offset, sd->len, &pos,more);
  394. buf->ops->unmap(info, buf);
  395. if (ret == sd->len)
  396. return 0;
  397. return -EIO;
  398. }
  399. /*
  400. * This is a little more tricky than the file -> pipe splicing. There are
  401. * basically three cases:
  402. *
  403. * - Destination page already exists in the address space and there
  404. * are users of it. For that case we have no other option that
  405. * copying the data. Tough luck.
  406. * - Destination page already exists in the address space, but there
  407. * are no users of it. Make sure it's uptodate, then drop it. Fall
  408. * through to last case.
  409. * - Destination page does not exist, we can add the pipe page to
  410. * the page cache and avoid the copy.
  411. *
  412. * If asked to move pages to the output file (SPLICE_F_MOVE is set in
  413. * sd->flags), we attempt to migrate pages from the pipe to the output
  414. * file address space page cache. This is possible if no one else has
  415. * the pipe page referenced outside of the pipe and page cache. If
  416. * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
  417. * a new page in the output file page cache and fill/dirty that.
  418. */
  419. static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
  420. struct splice_desc *sd)
  421. {
  422. struct file *file = sd->file;
  423. struct address_space *mapping = file->f_mapping;
  424. gfp_t gfp_mask = mapping_gfp_mask(mapping);
  425. unsigned int offset;
  426. struct page *page;
  427. pgoff_t index;
  428. char *src;
  429. int ret;
  430. /*
  431. * make sure the data in this buffer is uptodate
  432. */
  433. src = buf->ops->map(file, info, buf);
  434. if (IS_ERR(src))
  435. return PTR_ERR(src);
  436. index = sd->pos >> PAGE_CACHE_SHIFT;
  437. offset = sd->pos & ~PAGE_CACHE_MASK;
  438. /*
  439. * Reuse buf page, if SPLICE_F_MOVE is set.
  440. */
  441. if (sd->flags & SPLICE_F_MOVE) {
  442. /*
  443. * If steal succeeds, buf->page is now pruned from the vm
  444. * side (LRU and page cache) and we can reuse it. The page
  445. * will also be looked on successful return.
  446. */
  447. if (buf->ops->steal(info, buf))
  448. goto find_page;
  449. page = buf->page;
  450. if (add_to_page_cache(page, mapping, index, gfp_mask))
  451. goto find_page;
  452. if (!(buf->flags & PIPE_BUF_FLAG_LRU))
  453. lru_cache_add(page);
  454. } else {
  455. find_page:
  456. page = find_lock_page(mapping, index);
  457. if (!page) {
  458. ret = -ENOMEM;
  459. page = page_cache_alloc_cold(mapping);
  460. if (unlikely(!page))
  461. goto out_nomem;
  462. /*
  463. * This will also lock the page
  464. */
  465. ret = add_to_page_cache_lru(page, mapping, index,
  466. gfp_mask);
  467. if (unlikely(ret))
  468. goto out;
  469. }
  470. /*
  471. * We get here with the page locked. If the page is also
  472. * uptodate, we don't need to do more. If it isn't, we
  473. * may need to bring it in if we are not going to overwrite
  474. * the full page.
  475. */
  476. if (!PageUptodate(page)) {
  477. if (sd->len < PAGE_CACHE_SIZE) {
  478. ret = mapping->a_ops->readpage(file, page);
  479. if (unlikely(ret))
  480. goto out;
  481. lock_page(page);
  482. if (!PageUptodate(page)) {
  483. /*
  484. * Page got invalidated, repeat.
  485. */
  486. if (!page->mapping) {
  487. unlock_page(page);
  488. page_cache_release(page);
  489. goto find_page;
  490. }
  491. ret = -EIO;
  492. goto out;
  493. }
  494. } else
  495. SetPageUptodate(page);
  496. }
  497. }
  498. ret = mapping->a_ops->prepare_write(file, page, 0, sd->len);
  499. if (ret == AOP_TRUNCATED_PAGE) {
  500. page_cache_release(page);
  501. goto find_page;
  502. } else if (ret)
  503. goto out;
  504. if (!(buf->flags & PIPE_BUF_FLAG_STOLEN)) {
  505. char *dst = kmap_atomic(page, KM_USER0);
  506. memcpy(dst + offset, src + buf->offset, sd->len);
  507. flush_dcache_page(page);
  508. kunmap_atomic(dst, KM_USER0);
  509. }
  510. ret = mapping->a_ops->commit_write(file, page, 0, sd->len);
  511. if (ret == AOP_TRUNCATED_PAGE) {
  512. page_cache_release(page);
  513. goto find_page;
  514. } else if (ret)
  515. goto out;
  516. mark_page_accessed(page);
  517. balance_dirty_pages_ratelimited(mapping);
  518. out:
  519. if (!(buf->flags & PIPE_BUF_FLAG_STOLEN))
  520. page_cache_release(page);
  521. unlock_page(page);
  522. out_nomem:
  523. buf->ops->unmap(info, buf);
  524. return ret;
  525. }
  526. typedef int (splice_actor)(struct pipe_inode_info *, struct pipe_buffer *,
  527. struct splice_desc *);
  528. /*
  529. * Pipe input worker. Most of this logic works like a regular pipe, the
  530. * key here is the 'actor' worker passed in that actually moves the data
  531. * to the wanted destination. See pipe_to_file/pipe_to_sendpage above.
  532. */
  533. static ssize_t move_from_pipe(struct pipe_inode_info *pipe, struct file *out,
  534. loff_t *ppos, size_t len, unsigned int flags,
  535. splice_actor *actor)
  536. {
  537. int ret, do_wakeup, err;
  538. struct splice_desc sd;
  539. ret = 0;
  540. do_wakeup = 0;
  541. sd.total_len = len;
  542. sd.flags = flags;
  543. sd.file = out;
  544. sd.pos = *ppos;
  545. if (pipe->inode)
  546. mutex_lock(&pipe->inode->i_mutex);
  547. for (;;) {
  548. if (pipe->nrbufs) {
  549. struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  550. struct pipe_buf_operations *ops = buf->ops;
  551. sd.len = buf->len;
  552. if (sd.len > sd.total_len)
  553. sd.len = sd.total_len;
  554. err = actor(pipe, buf, &sd);
  555. if (err) {
  556. if (!ret && err != -ENODATA)
  557. ret = err;
  558. break;
  559. }
  560. ret += sd.len;
  561. buf->offset += sd.len;
  562. buf->len -= sd.len;
  563. if (!buf->len) {
  564. buf->ops = NULL;
  565. ops->release(pipe, buf);
  566. pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
  567. pipe->nrbufs--;
  568. if (pipe->inode)
  569. do_wakeup = 1;
  570. }
  571. sd.pos += sd.len;
  572. sd.total_len -= sd.len;
  573. if (!sd.total_len)
  574. break;
  575. }
  576. if (pipe->nrbufs)
  577. continue;
  578. if (!pipe->writers)
  579. break;
  580. if (!pipe->waiting_writers) {
  581. if (ret)
  582. break;
  583. }
  584. if (flags & SPLICE_F_NONBLOCK) {
  585. if (!ret)
  586. ret = -EAGAIN;
  587. break;
  588. }
  589. if (signal_pending(current)) {
  590. if (!ret)
  591. ret = -ERESTARTSYS;
  592. break;
  593. }
  594. if (do_wakeup) {
  595. smp_mb();
  596. if (waitqueue_active(&pipe->wait))
  597. wake_up_interruptible_sync(&pipe->wait);
  598. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  599. do_wakeup = 0;
  600. }
  601. pipe_wait(pipe);
  602. }
  603. if (pipe->inode)
  604. mutex_unlock(&pipe->inode->i_mutex);
  605. if (do_wakeup) {
  606. smp_mb();
  607. if (waitqueue_active(&pipe->wait))
  608. wake_up_interruptible(&pipe->wait);
  609. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  610. }
  611. return ret;
  612. }
  613. /**
  614. * generic_file_splice_write - splice data from a pipe to a file
  615. * @pipe: pipe info
  616. * @out: file to write to
  617. * @len: number of bytes to splice
  618. * @flags: splice modifier flags
  619. *
  620. * Will either move or copy pages (determined by @flags options) from
  621. * the given pipe inode to the given file.
  622. *
  623. */
  624. ssize_t
  625. generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  626. loff_t *ppos, size_t len, unsigned int flags)
  627. {
  628. struct address_space *mapping = out->f_mapping;
  629. ssize_t ret;
  630. ret = move_from_pipe(pipe, out, ppos, len, flags, pipe_to_file);
  631. if (ret > 0) {
  632. struct inode *inode = mapping->host;
  633. *ppos += ret;
  634. /*
  635. * If file or inode is SYNC and we actually wrote some data,
  636. * sync it.
  637. */
  638. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  639. int err;
  640. mutex_lock(&inode->i_mutex);
  641. err = generic_osync_inode(inode, mapping,
  642. OSYNC_METADATA|OSYNC_DATA);
  643. mutex_unlock(&inode->i_mutex);
  644. if (err)
  645. ret = err;
  646. }
  647. }
  648. return ret;
  649. }
  650. EXPORT_SYMBOL(generic_file_splice_write);
  651. /**
  652. * generic_splice_sendpage - splice data from a pipe to a socket
  653. * @inode: pipe inode
  654. * @out: socket to write to
  655. * @len: number of bytes to splice
  656. * @flags: splice modifier flags
  657. *
  658. * Will send @len bytes from the pipe to a network socket. No data copying
  659. * is involved.
  660. *
  661. */
  662. ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
  663. loff_t *ppos, size_t len, unsigned int flags)
  664. {
  665. return move_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
  666. }
  667. EXPORT_SYMBOL(generic_splice_sendpage);
  668. /*
  669. * Attempt to initiate a splice from pipe to file.
  670. */
  671. static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  672. loff_t *ppos, size_t len, unsigned int flags)
  673. {
  674. int ret;
  675. if (unlikely(!out->f_op || !out->f_op->splice_write))
  676. return -EINVAL;
  677. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  678. return -EBADF;
  679. ret = rw_verify_area(WRITE, out, ppos, len);
  680. if (unlikely(ret < 0))
  681. return ret;
  682. return out->f_op->splice_write(pipe, out, ppos, len, flags);
  683. }
  684. /*
  685. * Attempt to initiate a splice from a file to a pipe.
  686. */
  687. static long do_splice_to(struct file *in, loff_t *ppos,
  688. struct pipe_inode_info *pipe, size_t len,
  689. unsigned int flags)
  690. {
  691. loff_t isize, left;
  692. int ret;
  693. if (unlikely(!in->f_op || !in->f_op->splice_read))
  694. return -EINVAL;
  695. if (unlikely(!(in->f_mode & FMODE_READ)))
  696. return -EBADF;
  697. ret = rw_verify_area(READ, in, ppos, len);
  698. if (unlikely(ret < 0))
  699. return ret;
  700. isize = i_size_read(in->f_mapping->host);
  701. if (unlikely(*ppos >= isize))
  702. return 0;
  703. left = isize - *ppos;
  704. if (unlikely(left < len))
  705. len = left;
  706. return in->f_op->splice_read(in, ppos, pipe, len, flags);
  707. }
  708. long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
  709. size_t len, unsigned int flags)
  710. {
  711. struct pipe_inode_info *pipe;
  712. long ret, bytes;
  713. loff_t out_off;
  714. umode_t i_mode;
  715. int i;
  716. /*
  717. * We require the input being a regular file, as we don't want to
  718. * randomly drop data for eg socket -> socket splicing. Use the
  719. * piped splicing for that!
  720. */
  721. i_mode = in->f_dentry->d_inode->i_mode;
  722. if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  723. return -EINVAL;
  724. /*
  725. * neither in nor out is a pipe, setup an internal pipe attached to
  726. * 'out' and transfer the wanted data from 'in' to 'out' through that
  727. */
  728. pipe = current->splice_pipe;
  729. if (unlikely(!pipe)) {
  730. pipe = alloc_pipe_info(NULL);
  731. if (!pipe)
  732. return -ENOMEM;
  733. /*
  734. * We don't have an immediate reader, but we'll read the stuff
  735. * out of the pipe right after the move_to_pipe(). So set
  736. * PIPE_READERS appropriately.
  737. */
  738. pipe->readers = 1;
  739. current->splice_pipe = pipe;
  740. }
  741. /*
  742. * Do the splice.
  743. */
  744. ret = 0;
  745. bytes = 0;
  746. out_off = 0;
  747. while (len) {
  748. size_t read_len, max_read_len;
  749. /*
  750. * Do at most PIPE_BUFFERS pages worth of transfer:
  751. */
  752. max_read_len = min(len, (size_t)(PIPE_BUFFERS*PAGE_SIZE));
  753. ret = do_splice_to(in, ppos, pipe, max_read_len, flags);
  754. if (unlikely(ret < 0))
  755. goto out_release;
  756. read_len = ret;
  757. /*
  758. * NOTE: nonblocking mode only applies to the input. We
  759. * must not do the output in nonblocking mode as then we
  760. * could get stuck data in the internal pipe:
  761. */
  762. ret = do_splice_from(pipe, out, &out_off, read_len,
  763. flags & ~SPLICE_F_NONBLOCK);
  764. if (unlikely(ret < 0))
  765. goto out_release;
  766. bytes += ret;
  767. len -= ret;
  768. /*
  769. * In nonblocking mode, if we got back a short read then
  770. * that was due to either an IO error or due to the
  771. * pagecache entry not being there. In the IO error case
  772. * the _next_ splice attempt will produce a clean IO error
  773. * return value (not a short read), so in both cases it's
  774. * correct to break out of the loop here:
  775. */
  776. if ((flags & SPLICE_F_NONBLOCK) && (read_len < max_read_len))
  777. break;
  778. }
  779. pipe->nrbufs = pipe->curbuf = 0;
  780. return bytes;
  781. out_release:
  782. /*
  783. * If we did an incomplete transfer we must release
  784. * the pipe buffers in question:
  785. */
  786. for (i = 0; i < PIPE_BUFFERS; i++) {
  787. struct pipe_buffer *buf = pipe->bufs + i;
  788. if (buf->ops) {
  789. buf->ops->release(pipe, buf);
  790. buf->ops = NULL;
  791. }
  792. }
  793. pipe->nrbufs = pipe->curbuf = 0;
  794. /*
  795. * If we transferred some data, return the number of bytes:
  796. */
  797. if (bytes > 0)
  798. return bytes;
  799. return ret;
  800. }
  801. EXPORT_SYMBOL(do_splice_direct);
  802. /*
  803. * Determine where to splice to/from.
  804. */
  805. static long do_splice(struct file *in, loff_t __user *off_in,
  806. struct file *out, loff_t __user *off_out,
  807. size_t len, unsigned int flags)
  808. {
  809. struct pipe_inode_info *pipe;
  810. loff_t offset, *off;
  811. long ret;
  812. pipe = in->f_dentry->d_inode->i_pipe;
  813. if (pipe) {
  814. if (off_in)
  815. return -ESPIPE;
  816. if (off_out) {
  817. if (out->f_op->llseek == no_llseek)
  818. return -EINVAL;
  819. if (copy_from_user(&offset, off_out, sizeof(loff_t)))
  820. return -EFAULT;
  821. off = &offset;
  822. } else
  823. off = &out->f_pos;
  824. ret = do_splice_from(pipe, out, off, len, flags);
  825. if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
  826. ret = -EFAULT;
  827. return ret;
  828. }
  829. pipe = out->f_dentry->d_inode->i_pipe;
  830. if (pipe) {
  831. if (off_out)
  832. return -ESPIPE;
  833. if (off_in) {
  834. if (in->f_op->llseek == no_llseek)
  835. return -EINVAL;
  836. if (copy_from_user(&offset, off_in, sizeof(loff_t)))
  837. return -EFAULT;
  838. off = &offset;
  839. } else
  840. off = &in->f_pos;
  841. ret = do_splice_to(in, off, pipe, len, flags);
  842. if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
  843. ret = -EFAULT;
  844. return ret;
  845. }
  846. return -EINVAL;
  847. }
  848. asmlinkage long sys_splice(int fd_in, loff_t __user *off_in,
  849. int fd_out, loff_t __user *off_out,
  850. size_t len, unsigned int flags)
  851. {
  852. long error;
  853. struct file *in, *out;
  854. int fput_in, fput_out;
  855. if (unlikely(!len))
  856. return 0;
  857. error = -EBADF;
  858. in = fget_light(fd_in, &fput_in);
  859. if (in) {
  860. if (in->f_mode & FMODE_READ) {
  861. out = fget_light(fd_out, &fput_out);
  862. if (out) {
  863. if (out->f_mode & FMODE_WRITE)
  864. error = do_splice(in, off_in,
  865. out, off_out,
  866. len, flags);
  867. fput_light(out, fput_out);
  868. }
  869. }
  870. fput_light(in, fput_in);
  871. }
  872. return error;
  873. }
  874. /*
  875. * Link contents of ipipe to opipe.
  876. */
  877. static int link_pipe(struct pipe_inode_info *ipipe,
  878. struct pipe_inode_info *opipe,
  879. size_t len, unsigned int flags)
  880. {
  881. struct pipe_buffer *ibuf, *obuf;
  882. int ret, do_wakeup, i, ipipe_first;
  883. ret = do_wakeup = ipipe_first = 0;
  884. /*
  885. * Potential ABBA deadlock, work around it by ordering lock
  886. * grabbing by inode address. Otherwise two different processes
  887. * could deadlock (one doing tee from A -> B, the other from B -> A).
  888. */
  889. if (ipipe->inode < opipe->inode) {
  890. ipipe_first = 1;
  891. mutex_lock(&ipipe->inode->i_mutex);
  892. mutex_lock(&opipe->inode->i_mutex);
  893. } else {
  894. mutex_lock(&opipe->inode->i_mutex);
  895. mutex_lock(&ipipe->inode->i_mutex);
  896. }
  897. for (i = 0;; i++) {
  898. if (!opipe->readers) {
  899. send_sig(SIGPIPE, current, 0);
  900. if (!ret)
  901. ret = -EPIPE;
  902. break;
  903. }
  904. if (ipipe->nrbufs - i) {
  905. ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
  906. /*
  907. * If we have room, fill this buffer
  908. */
  909. if (opipe->nrbufs < PIPE_BUFFERS) {
  910. int nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
  911. /*
  912. * Get a reference to this pipe buffer,
  913. * so we can copy the contents over.
  914. */
  915. ibuf->ops->get(ipipe, ibuf);
  916. obuf = opipe->bufs + nbuf;
  917. *obuf = *ibuf;
  918. if (obuf->len > len)
  919. obuf->len = len;
  920. opipe->nrbufs++;
  921. do_wakeup = 1;
  922. ret += obuf->len;
  923. len -= obuf->len;
  924. if (!len)
  925. break;
  926. if (opipe->nrbufs < PIPE_BUFFERS)
  927. continue;
  928. }
  929. /*
  930. * We have input available, but no output room.
  931. * If we already copied data, return that. If we
  932. * need to drop the opipe lock, it must be ordered
  933. * last to avoid deadlocks.
  934. */
  935. if ((flags & SPLICE_F_NONBLOCK) || !ipipe_first) {
  936. if (!ret)
  937. ret = -EAGAIN;
  938. break;
  939. }
  940. if (signal_pending(current)) {
  941. if (!ret)
  942. ret = -ERESTARTSYS;
  943. break;
  944. }
  945. if (do_wakeup) {
  946. smp_mb();
  947. if (waitqueue_active(&opipe->wait))
  948. wake_up_interruptible(&opipe->wait);
  949. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  950. do_wakeup = 0;
  951. }
  952. opipe->waiting_writers++;
  953. pipe_wait(opipe);
  954. opipe->waiting_writers--;
  955. continue;
  956. }
  957. /*
  958. * No input buffers, do the usual checks for available
  959. * writers and blocking and wait if necessary
  960. */
  961. if (!ipipe->writers)
  962. break;
  963. if (!ipipe->waiting_writers) {
  964. if (ret)
  965. break;
  966. }
  967. /*
  968. * pipe_wait() drops the ipipe mutex. To avoid deadlocks
  969. * with another process, we can only safely do that if
  970. * the ipipe lock is ordered last.
  971. */
  972. if ((flags & SPLICE_F_NONBLOCK) || ipipe_first) {
  973. if (!ret)
  974. ret = -EAGAIN;
  975. break;
  976. }
  977. if (signal_pending(current)) {
  978. if (!ret)
  979. ret = -ERESTARTSYS;
  980. break;
  981. }
  982. if (waitqueue_active(&ipipe->wait))
  983. wake_up_interruptible_sync(&ipipe->wait);
  984. kill_fasync(&ipipe->fasync_writers, SIGIO, POLL_OUT);
  985. pipe_wait(ipipe);
  986. }
  987. mutex_unlock(&ipipe->inode->i_mutex);
  988. mutex_unlock(&opipe->inode->i_mutex);
  989. if (do_wakeup) {
  990. smp_mb();
  991. if (waitqueue_active(&opipe->wait))
  992. wake_up_interruptible(&opipe->wait);
  993. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  994. }
  995. return ret;
  996. }
  997. /*
  998. * This is a tee(1) implementation that works on pipes. It doesn't copy
  999. * any data, it simply references the 'in' pages on the 'out' pipe.
  1000. * The 'flags' used are the SPLICE_F_* variants, currently the only
  1001. * applicable one is SPLICE_F_NONBLOCK.
  1002. */
  1003. static long do_tee(struct file *in, struct file *out, size_t len,
  1004. unsigned int flags)
  1005. {
  1006. struct pipe_inode_info *ipipe = in->f_dentry->d_inode->i_pipe;
  1007. struct pipe_inode_info *opipe = out->f_dentry->d_inode->i_pipe;
  1008. /*
  1009. * Link ipipe to the two output pipes, consuming as we go along.
  1010. */
  1011. if (ipipe && opipe)
  1012. return link_pipe(ipipe, opipe, len, flags);
  1013. return -EINVAL;
  1014. }
  1015. asmlinkage long sys_tee(int fdin, int fdout, size_t len, unsigned int flags)
  1016. {
  1017. struct file *in;
  1018. int error, fput_in;
  1019. if (unlikely(!len))
  1020. return 0;
  1021. error = -EBADF;
  1022. in = fget_light(fdin, &fput_in);
  1023. if (in) {
  1024. if (in->f_mode & FMODE_READ) {
  1025. int fput_out;
  1026. struct file *out = fget_light(fdout, &fput_out);
  1027. if (out) {
  1028. if (out->f_mode & FMODE_WRITE)
  1029. error = do_tee(in, out, len, flags);
  1030. fput_light(out, fput_out);
  1031. }
  1032. }
  1033. fput_light(in, fput_in);
  1034. }
  1035. return error;
  1036. }