splice.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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 and fixing the initial implementation
  13. * bugs.
  14. *
  15. * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
  16. * Copyright (C) 2005 Linus Torvalds <torvalds@osdl.org>
  17. *
  18. */
  19. #include <linux/fs.h>
  20. #include <linux/file.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/pipe_fs_i.h>
  23. #include <linux/mm_inline.h>
  24. #include <linux/swap.h>
  25. #include <linux/writeback.h>
  26. #include <linux/buffer_head.h>
  27. #include <linux/module.h>
  28. #include <linux/syscalls.h>
  29. /*
  30. * Passed to the actors
  31. */
  32. struct splice_desc {
  33. unsigned int len, total_len; /* current and remaining length */
  34. unsigned int flags; /* splice flags */
  35. struct file *file; /* file to read/write */
  36. loff_t pos; /* file position */
  37. };
  38. /*
  39. * Attempt to steal a page from a pipe buffer. This should perhaps go into
  40. * a vm helper function, it's already simplified quite a bit by the
  41. * addition of remove_mapping(). If success is returned, the caller may
  42. * attempt to reuse this page for another destination.
  43. */
  44. static int page_cache_pipe_buf_steal(struct pipe_inode_info *info,
  45. struct pipe_buffer *buf)
  46. {
  47. struct page *page = buf->page;
  48. struct address_space *mapping = page_mapping(page);
  49. WARN_ON(!PageLocked(page));
  50. WARN_ON(!PageUptodate(page));
  51. /*
  52. * At least for ext2 with nobh option, we need to wait on writeback
  53. * completing on this page, since we'll remove it from the pagecache.
  54. * Otherwise truncate wont wait on the page, allowing the disk
  55. * blocks to be reused by someone else before we actually wrote our
  56. * data to them. fs corruption ensues.
  57. */
  58. wait_on_page_writeback(page);
  59. if (PagePrivate(page))
  60. try_to_release_page(page, mapping_gfp_mask(mapping));
  61. if (!remove_mapping(mapping, page))
  62. return 1;
  63. buf->flags |= PIPE_BUF_FLAG_STOLEN | PIPE_BUF_FLAG_LRU;
  64. return 0;
  65. }
  66. static void page_cache_pipe_buf_release(struct pipe_inode_info *info,
  67. struct pipe_buffer *buf)
  68. {
  69. page_cache_release(buf->page);
  70. buf->page = NULL;
  71. buf->flags &= ~(PIPE_BUF_FLAG_STOLEN | PIPE_BUF_FLAG_LRU);
  72. }
  73. static void *page_cache_pipe_buf_map(struct file *file,
  74. struct pipe_inode_info *info,
  75. struct pipe_buffer *buf)
  76. {
  77. struct page *page = buf->page;
  78. int err;
  79. if (!PageUptodate(page)) {
  80. lock_page(page);
  81. /*
  82. * Page got truncated/unhashed. This will cause a 0-byte
  83. * splice, if this is the first page
  84. */
  85. if (!page->mapping) {
  86. err = -ENODATA;
  87. goto error;
  88. }
  89. /*
  90. * uh oh, read-error from disk
  91. */
  92. if (!PageUptodate(page)) {
  93. err = -EIO;
  94. goto error;
  95. }
  96. /*
  97. * page is ok afterall, fall through to mapping
  98. */
  99. unlock_page(page);
  100. }
  101. return kmap(page);
  102. error:
  103. unlock_page(page);
  104. return ERR_PTR(err);
  105. }
  106. static void page_cache_pipe_buf_unmap(struct pipe_inode_info *info,
  107. struct pipe_buffer *buf)
  108. {
  109. kunmap(buf->page);
  110. }
  111. static struct pipe_buf_operations page_cache_pipe_buf_ops = {
  112. .can_merge = 0,
  113. .map = page_cache_pipe_buf_map,
  114. .unmap = page_cache_pipe_buf_unmap,
  115. .release = page_cache_pipe_buf_release,
  116. .steal = page_cache_pipe_buf_steal,
  117. };
  118. /*
  119. * Pipe output worker. This sets up our pipe format with the page cache
  120. * pipe buffer operations. Otherwise very similar to the regular pipe_writev().
  121. */
  122. static ssize_t move_to_pipe(struct pipe_inode_info *pipe, struct page **pages,
  123. int nr_pages, unsigned long offset,
  124. unsigned long len, unsigned int flags)
  125. {
  126. int ret, do_wakeup, i;
  127. ret = 0;
  128. do_wakeup = 0;
  129. i = 0;
  130. if (pipe->inode)
  131. mutex_lock(&pipe->inode->i_mutex);
  132. for (;;) {
  133. int bufs;
  134. if (!pipe->readers) {
  135. send_sig(SIGPIPE, current, 0);
  136. if (!ret)
  137. ret = -EPIPE;
  138. break;
  139. }
  140. bufs = pipe->nrbufs;
  141. if (bufs < PIPE_BUFFERS) {
  142. int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS - 1);
  143. struct pipe_buffer *buf = pipe->bufs + newbuf;
  144. struct page *page = pages[i++];
  145. unsigned long this_len;
  146. this_len = PAGE_CACHE_SIZE - offset;
  147. if (this_len > len)
  148. this_len = len;
  149. buf->page = page;
  150. buf->offset = offset;
  151. buf->len = this_len;
  152. buf->ops = &page_cache_pipe_buf_ops;
  153. pipe->nrbufs = ++bufs;
  154. do_wakeup = 1;
  155. ret += this_len;
  156. len -= this_len;
  157. offset = 0;
  158. if (!--nr_pages)
  159. break;
  160. if (!len)
  161. break;
  162. if (bufs < PIPE_BUFFERS)
  163. continue;
  164. break;
  165. }
  166. if (flags & SPLICE_F_NONBLOCK) {
  167. if (!ret)
  168. ret = -EAGAIN;
  169. break;
  170. }
  171. if (signal_pending(current)) {
  172. if (!ret)
  173. ret = -ERESTARTSYS;
  174. break;
  175. }
  176. if (do_wakeup) {
  177. smp_mb();
  178. if (waitqueue_active(&pipe->wait))
  179. wake_up_interruptible_sync(&pipe->wait);
  180. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  181. do_wakeup = 0;
  182. }
  183. pipe->waiting_writers++;
  184. pipe_wait(pipe);
  185. pipe->waiting_writers--;
  186. }
  187. if (pipe->inode)
  188. mutex_unlock(&pipe->inode->i_mutex);
  189. if (do_wakeup) {
  190. smp_mb();
  191. if (waitqueue_active(&pipe->wait))
  192. wake_up_interruptible(&pipe->wait);
  193. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  194. }
  195. while (i < nr_pages)
  196. page_cache_release(pages[i++]);
  197. return ret;
  198. }
  199. static int
  200. __generic_file_splice_read(struct file *in, struct pipe_inode_info *pipe,
  201. size_t len, unsigned int flags)
  202. {
  203. struct address_space *mapping = in->f_mapping;
  204. unsigned int offset, nr_pages;
  205. struct page *pages[PIPE_BUFFERS];
  206. struct page *page;
  207. pgoff_t index;
  208. int i;
  209. index = in->f_pos >> PAGE_CACHE_SHIFT;
  210. offset = in->f_pos & ~PAGE_CACHE_MASK;
  211. nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  212. if (nr_pages > PIPE_BUFFERS)
  213. nr_pages = PIPE_BUFFERS;
  214. /*
  215. * initiate read-ahead on this page range. however, don't call into
  216. * read-ahead if this is a non-zero offset (we are likely doing small
  217. * chunk splice and the page is already there) for a single page.
  218. */
  219. if (!offset || nr_pages > 1)
  220. do_page_cache_readahead(mapping, in, index, nr_pages);
  221. /*
  222. * now fill in the holes
  223. */
  224. for (i = 0; i < nr_pages; i++, index++) {
  225. /*
  226. * no page there, look one up / create it
  227. */
  228. page = find_or_create_page(mapping, index,
  229. mapping_gfp_mask(mapping));
  230. if (!page)
  231. break;
  232. if (PageUptodate(page))
  233. unlock_page(page);
  234. else {
  235. int error = mapping->a_ops->readpage(in, page);
  236. if (unlikely(error)) {
  237. page_cache_release(page);
  238. break;
  239. }
  240. }
  241. pages[i] = page;
  242. }
  243. if (i)
  244. return move_to_pipe(pipe, pages, i, offset, len, flags);
  245. return 0;
  246. }
  247. /**
  248. * generic_file_splice_read - splice data from file to a pipe
  249. * @in: file to splice from
  250. * @pipe: pipe to splice to
  251. * @len: number of bytes to splice
  252. * @flags: splice modifier flags
  253. *
  254. * Will read pages from given file and fill them into a pipe.
  255. *
  256. */
  257. ssize_t generic_file_splice_read(struct file *in, struct pipe_inode_info *pipe,
  258. size_t len, unsigned int flags)
  259. {
  260. ssize_t spliced;
  261. int ret;
  262. ret = 0;
  263. spliced = 0;
  264. while (len) {
  265. ret = __generic_file_splice_read(in, pipe, len, flags);
  266. if (ret <= 0)
  267. break;
  268. in->f_pos += ret;
  269. len -= ret;
  270. spliced += ret;
  271. if (!(flags & SPLICE_F_NONBLOCK))
  272. continue;
  273. ret = -EAGAIN;
  274. break;
  275. }
  276. if (spliced)
  277. return spliced;
  278. return ret;
  279. }
  280. EXPORT_SYMBOL(generic_file_splice_read);
  281. /*
  282. * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
  283. * using sendpage().
  284. */
  285. static int pipe_to_sendpage(struct pipe_inode_info *info,
  286. struct pipe_buffer *buf, struct splice_desc *sd)
  287. {
  288. struct file *file = sd->file;
  289. loff_t pos = sd->pos;
  290. unsigned int offset;
  291. ssize_t ret;
  292. void *ptr;
  293. int more;
  294. /*
  295. * sub-optimal, but we are limited by the pipe ->map. we don't
  296. * need a kmap'ed buffer here, we just want to make sure we
  297. * have the page pinned if the pipe page originates from the
  298. * page cache
  299. */
  300. ptr = buf->ops->map(file, info, buf);
  301. if (IS_ERR(ptr))
  302. return PTR_ERR(ptr);
  303. offset = pos & ~PAGE_CACHE_MASK;
  304. more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
  305. ret = file->f_op->sendpage(file, buf->page, offset, sd->len, &pos,more);
  306. buf->ops->unmap(info, buf);
  307. if (ret == sd->len)
  308. return 0;
  309. return -EIO;
  310. }
  311. /*
  312. * This is a little more tricky than the file -> pipe splicing. There are
  313. * basically three cases:
  314. *
  315. * - Destination page already exists in the address space and there
  316. * are users of it. For that case we have no other option that
  317. * copying the data. Tough luck.
  318. * - Destination page already exists in the address space, but there
  319. * are no users of it. Make sure it's uptodate, then drop it. Fall
  320. * through to last case.
  321. * - Destination page does not exist, we can add the pipe page to
  322. * the page cache and avoid the copy.
  323. *
  324. * If asked to move pages to the output file (SPLICE_F_MOVE is set in
  325. * sd->flags), we attempt to migrate pages from the pipe to the output
  326. * file address space page cache. This is possible if no one else has
  327. * the pipe page referenced outside of the pipe and page cache. If
  328. * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
  329. * a new page in the output file page cache and fill/dirty that.
  330. */
  331. static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
  332. struct splice_desc *sd)
  333. {
  334. struct file *file = sd->file;
  335. struct address_space *mapping = file->f_mapping;
  336. gfp_t gfp_mask = mapping_gfp_mask(mapping);
  337. unsigned int offset;
  338. struct page *page;
  339. pgoff_t index;
  340. char *src;
  341. int ret;
  342. /*
  343. * make sure the data in this buffer is uptodate
  344. */
  345. src = buf->ops->map(file, info, buf);
  346. if (IS_ERR(src))
  347. return PTR_ERR(src);
  348. index = sd->pos >> PAGE_CACHE_SHIFT;
  349. offset = sd->pos & ~PAGE_CACHE_MASK;
  350. /*
  351. * reuse buf page, if SPLICE_F_MOVE is set
  352. */
  353. if (sd->flags & SPLICE_F_MOVE) {
  354. /*
  355. * If steal succeeds, buf->page is now pruned from the vm
  356. * side (LRU and page cache) and we can reuse it.
  357. */
  358. if (buf->ops->steal(info, buf))
  359. goto find_page;
  360. /*
  361. * this will also set the page locked
  362. */
  363. page = buf->page;
  364. if (add_to_page_cache(page, mapping, index, gfp_mask))
  365. goto find_page;
  366. if (!(buf->flags & PIPE_BUF_FLAG_LRU))
  367. lru_cache_add(page);
  368. } else {
  369. find_page:
  370. ret = -ENOMEM;
  371. page = find_or_create_page(mapping, index, gfp_mask);
  372. if (!page)
  373. goto out_nomem;
  374. /*
  375. * If the page is uptodate, it is also locked. If it isn't
  376. * uptodate, we can mark it uptodate if we are filling the
  377. * full page. Otherwise we need to read it in first...
  378. */
  379. if (!PageUptodate(page)) {
  380. if (sd->len < PAGE_CACHE_SIZE) {
  381. ret = mapping->a_ops->readpage(file, page);
  382. if (unlikely(ret))
  383. goto out;
  384. lock_page(page);
  385. if (!PageUptodate(page)) {
  386. /*
  387. * page got invalidated, repeat
  388. */
  389. if (!page->mapping) {
  390. unlock_page(page);
  391. page_cache_release(page);
  392. goto find_page;
  393. }
  394. ret = -EIO;
  395. goto out;
  396. }
  397. } else {
  398. WARN_ON(!PageLocked(page));
  399. SetPageUptodate(page);
  400. }
  401. }
  402. }
  403. ret = mapping->a_ops->prepare_write(file, page, 0, sd->len);
  404. if (ret == AOP_TRUNCATED_PAGE) {
  405. page_cache_release(page);
  406. goto find_page;
  407. } else if (ret)
  408. goto out;
  409. if (!(buf->flags & PIPE_BUF_FLAG_STOLEN)) {
  410. char *dst = kmap_atomic(page, KM_USER0);
  411. memcpy(dst + offset, src + buf->offset, sd->len);
  412. flush_dcache_page(page);
  413. kunmap_atomic(dst, KM_USER0);
  414. }
  415. ret = mapping->a_ops->commit_write(file, page, 0, sd->len);
  416. if (ret == AOP_TRUNCATED_PAGE) {
  417. page_cache_release(page);
  418. goto find_page;
  419. } else if (ret)
  420. goto out;
  421. mark_page_accessed(page);
  422. balance_dirty_pages_ratelimited(mapping);
  423. out:
  424. if (!(buf->flags & PIPE_BUF_FLAG_STOLEN)) {
  425. page_cache_release(page);
  426. unlock_page(page);
  427. }
  428. out_nomem:
  429. buf->ops->unmap(info, buf);
  430. return ret;
  431. }
  432. typedef int (splice_actor)(struct pipe_inode_info *, struct pipe_buffer *,
  433. struct splice_desc *);
  434. /*
  435. * Pipe input worker. Most of this logic works like a regular pipe, the
  436. * key here is the 'actor' worker passed in that actually moves the data
  437. * to the wanted destination. See pipe_to_file/pipe_to_sendpage above.
  438. */
  439. static ssize_t move_from_pipe(struct pipe_inode_info *pipe, struct file *out,
  440. size_t len, unsigned int flags,
  441. splice_actor *actor)
  442. {
  443. int ret, do_wakeup, err;
  444. struct splice_desc sd;
  445. ret = 0;
  446. do_wakeup = 0;
  447. sd.total_len = len;
  448. sd.flags = flags;
  449. sd.file = out;
  450. sd.pos = out->f_pos;
  451. if (pipe->inode)
  452. mutex_lock(&pipe->inode->i_mutex);
  453. for (;;) {
  454. int bufs = pipe->nrbufs;
  455. if (bufs) {
  456. int curbuf = pipe->curbuf;
  457. struct pipe_buffer *buf = pipe->bufs + curbuf;
  458. struct pipe_buf_operations *ops = buf->ops;
  459. sd.len = buf->len;
  460. if (sd.len > sd.total_len)
  461. sd.len = sd.total_len;
  462. err = actor(pipe, buf, &sd);
  463. if (err) {
  464. if (!ret && err != -ENODATA)
  465. ret = err;
  466. break;
  467. }
  468. ret += sd.len;
  469. buf->offset += sd.len;
  470. buf->len -= sd.len;
  471. if (!buf->len) {
  472. buf->ops = NULL;
  473. ops->release(pipe, buf);
  474. curbuf = (curbuf + 1) & (PIPE_BUFFERS - 1);
  475. pipe->curbuf = curbuf;
  476. pipe->nrbufs = --bufs;
  477. do_wakeup = 1;
  478. }
  479. sd.pos += sd.len;
  480. sd.total_len -= sd.len;
  481. if (!sd.total_len)
  482. break;
  483. }
  484. if (bufs)
  485. continue;
  486. if (!pipe->writers)
  487. break;
  488. if (!pipe->waiting_writers) {
  489. if (ret)
  490. break;
  491. }
  492. if (flags & SPLICE_F_NONBLOCK) {
  493. if (!ret)
  494. ret = -EAGAIN;
  495. break;
  496. }
  497. if (signal_pending(current)) {
  498. if (!ret)
  499. ret = -ERESTARTSYS;
  500. break;
  501. }
  502. if (do_wakeup) {
  503. smp_mb();
  504. if (waitqueue_active(&pipe->wait))
  505. wake_up_interruptible_sync(&pipe->wait);
  506. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  507. do_wakeup = 0;
  508. }
  509. pipe_wait(pipe);
  510. }
  511. if (pipe->inode)
  512. mutex_unlock(&pipe->inode->i_mutex);
  513. if (do_wakeup) {
  514. smp_mb();
  515. if (waitqueue_active(&pipe->wait))
  516. wake_up_interruptible(&pipe->wait);
  517. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  518. }
  519. mutex_lock(&out->f_mapping->host->i_mutex);
  520. out->f_pos = sd.pos;
  521. mutex_unlock(&out->f_mapping->host->i_mutex);
  522. return ret;
  523. }
  524. /**
  525. * generic_file_splice_write - splice data from a pipe to a file
  526. * @pipe: pipe info
  527. * @out: file to write to
  528. * @len: number of bytes to splice
  529. * @flags: splice modifier flags
  530. *
  531. * Will either move or copy pages (determined by @flags options) from
  532. * the given pipe inode to the given file.
  533. *
  534. */
  535. ssize_t
  536. generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  537. size_t len, unsigned int flags)
  538. {
  539. struct address_space *mapping = out->f_mapping;
  540. ssize_t ret;
  541. ret = move_from_pipe(pipe, out, len, flags, pipe_to_file);
  542. /*
  543. * if file or inode is SYNC and we actually wrote some data, sync it
  544. */
  545. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(mapping->host))
  546. && ret > 0) {
  547. struct inode *inode = mapping->host;
  548. int err;
  549. mutex_lock(&inode->i_mutex);
  550. err = generic_osync_inode(mapping->host, mapping,
  551. OSYNC_METADATA|OSYNC_DATA);
  552. mutex_unlock(&inode->i_mutex);
  553. if (err)
  554. ret = err;
  555. }
  556. return ret;
  557. }
  558. EXPORT_SYMBOL(generic_file_splice_write);
  559. /**
  560. * generic_splice_sendpage - splice data from a pipe to a socket
  561. * @inode: pipe inode
  562. * @out: socket to write to
  563. * @len: number of bytes to splice
  564. * @flags: splice modifier flags
  565. *
  566. * Will send @len bytes from the pipe to a network socket. No data copying
  567. * is involved.
  568. *
  569. */
  570. ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
  571. size_t len, unsigned int flags)
  572. {
  573. return move_from_pipe(pipe, out, len, flags, pipe_to_sendpage);
  574. }
  575. EXPORT_SYMBOL(generic_splice_sendpage);
  576. /*
  577. * Attempt to initiate a splice from pipe to file.
  578. */
  579. static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  580. size_t len, unsigned int flags)
  581. {
  582. loff_t pos;
  583. int ret;
  584. if (!out->f_op || !out->f_op->splice_write)
  585. return -EINVAL;
  586. if (!(out->f_mode & FMODE_WRITE))
  587. return -EBADF;
  588. pos = out->f_pos;
  589. ret = rw_verify_area(WRITE, out, &pos, len);
  590. if (unlikely(ret < 0))
  591. return ret;
  592. return out->f_op->splice_write(pipe, out, len, flags);
  593. }
  594. /*
  595. * Attempt to initiate a splice from a file to a pipe.
  596. */
  597. static long do_splice_to(struct file *in, struct pipe_inode_info *pipe,
  598. size_t len, unsigned int flags)
  599. {
  600. loff_t pos, isize, left;
  601. int ret;
  602. if (!in->f_op || !in->f_op->splice_read)
  603. return -EINVAL;
  604. if (!(in->f_mode & FMODE_READ))
  605. return -EBADF;
  606. pos = in->f_pos;
  607. ret = rw_verify_area(READ, in, &pos, len);
  608. if (unlikely(ret < 0))
  609. return ret;
  610. isize = i_size_read(in->f_mapping->host);
  611. if (unlikely(in->f_pos >= isize))
  612. return 0;
  613. left = isize - in->f_pos;
  614. if (left < len)
  615. len = left;
  616. return in->f_op->splice_read(in, pipe, len, flags);
  617. }
  618. /*
  619. * Determine where to splice to/from.
  620. */
  621. static long do_splice(struct file *in, struct file *out, size_t len,
  622. unsigned int flags)
  623. {
  624. struct pipe_inode_info *pipe;
  625. pipe = in->f_dentry->d_inode->i_pipe;
  626. if (pipe)
  627. return do_splice_from(pipe, out, len, flags);
  628. pipe = out->f_dentry->d_inode->i_pipe;
  629. if (pipe)
  630. return do_splice_to(in, pipe, len, flags);
  631. return -EINVAL;
  632. }
  633. asmlinkage long sys_splice(int fdin, int fdout, size_t len, unsigned int flags)
  634. {
  635. long error;
  636. struct file *in, *out;
  637. int fput_in, fput_out;
  638. if (unlikely(!len))
  639. return 0;
  640. error = -EBADF;
  641. in = fget_light(fdin, &fput_in);
  642. if (in) {
  643. if (in->f_mode & FMODE_READ) {
  644. out = fget_light(fdout, &fput_out);
  645. if (out) {
  646. if (out->f_mode & FMODE_WRITE)
  647. error = do_splice(in, out, len, flags);
  648. fput_light(out, fput_out);
  649. }
  650. }
  651. fput_light(in, fput_in);
  652. }
  653. return error;
  654. }