splice.c 18 KB

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