splice.c 17 KB

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