splice.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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, error;
  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. error = 0;
  225. for (i = 0; i < nr_pages; i++, index++) {
  226. find_page:
  227. /*
  228. * lookup the page for this index
  229. */
  230. page = find_get_page(mapping, index);
  231. if (!page) {
  232. /*
  233. * If in nonblock mode then dont block on
  234. * readpage (we've kicked readahead so there
  235. * will be asynchronous progress):
  236. */
  237. if (flags & SPLICE_F_NONBLOCK)
  238. break;
  239. /*
  240. * page didn't exist, allocate one
  241. */
  242. page = page_cache_alloc_cold(mapping);
  243. if (!page)
  244. break;
  245. error = add_to_page_cache_lru(page, mapping, index,
  246. mapping_gfp_mask(mapping));
  247. if (unlikely(error)) {
  248. page_cache_release(page);
  249. break;
  250. }
  251. goto readpage;
  252. }
  253. /*
  254. * If the page isn't uptodate, we may need to start io on it
  255. */
  256. if (!PageUptodate(page)) {
  257. lock_page(page);
  258. /*
  259. * page was truncated, stop here. if this isn't the
  260. * first page, we'll just complete what we already
  261. * added
  262. */
  263. if (!page->mapping) {
  264. unlock_page(page);
  265. page_cache_release(page);
  266. break;
  267. }
  268. /*
  269. * page was already under io and is now done, great
  270. */
  271. if (PageUptodate(page)) {
  272. unlock_page(page);
  273. goto fill_it;
  274. }
  275. readpage:
  276. /*
  277. * need to read in the page
  278. */
  279. error = mapping->a_ops->readpage(in, page);
  280. if (unlikely(error)) {
  281. page_cache_release(page);
  282. if (error == AOP_TRUNCATED_PAGE)
  283. goto find_page;
  284. break;
  285. }
  286. }
  287. fill_it:
  288. pages[i] = page;
  289. }
  290. if (i)
  291. return move_to_pipe(pipe, pages, i, offset, len, flags);
  292. return error;
  293. }
  294. /**
  295. * generic_file_splice_read - splice data from file to a pipe
  296. * @in: file to splice from
  297. * @pipe: pipe to splice to
  298. * @len: number of bytes to splice
  299. * @flags: splice modifier flags
  300. *
  301. * Will read pages from given file and fill them into a pipe.
  302. *
  303. */
  304. ssize_t generic_file_splice_read(struct file *in, struct pipe_inode_info *pipe,
  305. size_t len, unsigned int flags)
  306. {
  307. ssize_t spliced;
  308. int ret;
  309. ret = 0;
  310. spliced = 0;
  311. while (len) {
  312. ret = __generic_file_splice_read(in, pipe, len, flags);
  313. if (ret <= 0)
  314. break;
  315. in->f_pos += ret;
  316. len -= ret;
  317. spliced += ret;
  318. if (!(flags & SPLICE_F_NONBLOCK))
  319. continue;
  320. ret = -EAGAIN;
  321. break;
  322. }
  323. if (spliced)
  324. return spliced;
  325. return ret;
  326. }
  327. EXPORT_SYMBOL(generic_file_splice_read);
  328. /*
  329. * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
  330. * using sendpage().
  331. */
  332. static int pipe_to_sendpage(struct pipe_inode_info *info,
  333. struct pipe_buffer *buf, struct splice_desc *sd)
  334. {
  335. struct file *file = sd->file;
  336. loff_t pos = sd->pos;
  337. unsigned int offset;
  338. ssize_t ret;
  339. void *ptr;
  340. int more;
  341. /*
  342. * sub-optimal, but we are limited by the pipe ->map. we don't
  343. * need a kmap'ed buffer here, we just want to make sure we
  344. * have the page pinned if the pipe page originates from the
  345. * page cache
  346. */
  347. ptr = buf->ops->map(file, info, buf);
  348. if (IS_ERR(ptr))
  349. return PTR_ERR(ptr);
  350. offset = pos & ~PAGE_CACHE_MASK;
  351. more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
  352. ret = file->f_op->sendpage(file, buf->page, offset, sd->len, &pos,more);
  353. buf->ops->unmap(info, buf);
  354. if (ret == sd->len)
  355. return 0;
  356. return -EIO;
  357. }
  358. /*
  359. * This is a little more tricky than the file -> pipe splicing. There are
  360. * basically three cases:
  361. *
  362. * - Destination page already exists in the address space and there
  363. * are users of it. For that case we have no other option that
  364. * copying the data. Tough luck.
  365. * - Destination page already exists in the address space, but there
  366. * are no users of it. Make sure it's uptodate, then drop it. Fall
  367. * through to last case.
  368. * - Destination page does not exist, we can add the pipe page to
  369. * the page cache and avoid the copy.
  370. *
  371. * If asked to move pages to the output file (SPLICE_F_MOVE is set in
  372. * sd->flags), we attempt to migrate pages from the pipe to the output
  373. * file address space page cache. This is possible if no one else has
  374. * the pipe page referenced outside of the pipe and page cache. If
  375. * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
  376. * a new page in the output file page cache and fill/dirty that.
  377. */
  378. static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
  379. struct splice_desc *sd)
  380. {
  381. struct file *file = sd->file;
  382. struct address_space *mapping = file->f_mapping;
  383. gfp_t gfp_mask = mapping_gfp_mask(mapping);
  384. unsigned int offset;
  385. struct page *page;
  386. pgoff_t index;
  387. char *src;
  388. int ret;
  389. /*
  390. * make sure the data in this buffer is uptodate
  391. */
  392. src = buf->ops->map(file, info, buf);
  393. if (IS_ERR(src))
  394. return PTR_ERR(src);
  395. index = sd->pos >> PAGE_CACHE_SHIFT;
  396. offset = sd->pos & ~PAGE_CACHE_MASK;
  397. /*
  398. * reuse buf page, if SPLICE_F_MOVE is set
  399. */
  400. if (sd->flags & SPLICE_F_MOVE) {
  401. /*
  402. * If steal succeeds, buf->page is now pruned from the vm
  403. * side (LRU and page cache) and we can reuse it.
  404. */
  405. if (buf->ops->steal(info, buf))
  406. goto find_page;
  407. /*
  408. * this will also set the page locked
  409. */
  410. page = buf->page;
  411. if (add_to_page_cache(page, mapping, index, gfp_mask))
  412. goto find_page;
  413. if (!(buf->flags & PIPE_BUF_FLAG_LRU))
  414. lru_cache_add(page);
  415. } else {
  416. find_page:
  417. ret = -ENOMEM;
  418. page = find_or_create_page(mapping, index, gfp_mask);
  419. if (!page)
  420. goto out_nomem;
  421. /*
  422. * If the page is uptodate, it is also locked. If it isn't
  423. * uptodate, we can mark it uptodate if we are filling the
  424. * full page. Otherwise we need to read it in first...
  425. */
  426. if (!PageUptodate(page)) {
  427. if (sd->len < PAGE_CACHE_SIZE) {
  428. ret = mapping->a_ops->readpage(file, page);
  429. if (unlikely(ret))
  430. goto out;
  431. lock_page(page);
  432. if (!PageUptodate(page)) {
  433. /*
  434. * page got invalidated, repeat
  435. */
  436. if (!page->mapping) {
  437. unlock_page(page);
  438. page_cache_release(page);
  439. goto find_page;
  440. }
  441. ret = -EIO;
  442. goto out;
  443. }
  444. } else {
  445. WARN_ON(!PageLocked(page));
  446. SetPageUptodate(page);
  447. }
  448. }
  449. }
  450. ret = mapping->a_ops->prepare_write(file, page, 0, sd->len);
  451. if (ret == AOP_TRUNCATED_PAGE) {
  452. page_cache_release(page);
  453. goto find_page;
  454. } else if (ret)
  455. goto out;
  456. if (!(buf->flags & PIPE_BUF_FLAG_STOLEN)) {
  457. char *dst = kmap_atomic(page, KM_USER0);
  458. memcpy(dst + offset, src + buf->offset, sd->len);
  459. flush_dcache_page(page);
  460. kunmap_atomic(dst, KM_USER0);
  461. }
  462. ret = mapping->a_ops->commit_write(file, page, 0, sd->len);
  463. if (ret == AOP_TRUNCATED_PAGE) {
  464. page_cache_release(page);
  465. goto find_page;
  466. } else if (ret)
  467. goto out;
  468. mark_page_accessed(page);
  469. balance_dirty_pages_ratelimited(mapping);
  470. out:
  471. if (!(buf->flags & PIPE_BUF_FLAG_STOLEN)) {
  472. page_cache_release(page);
  473. unlock_page(page);
  474. }
  475. out_nomem:
  476. buf->ops->unmap(info, buf);
  477. return ret;
  478. }
  479. typedef int (splice_actor)(struct pipe_inode_info *, struct pipe_buffer *,
  480. struct splice_desc *);
  481. /*
  482. * Pipe input worker. Most of this logic works like a regular pipe, the
  483. * key here is the 'actor' worker passed in that actually moves the data
  484. * to the wanted destination. See pipe_to_file/pipe_to_sendpage above.
  485. */
  486. static ssize_t move_from_pipe(struct pipe_inode_info *pipe, struct file *out,
  487. size_t len, unsigned int flags,
  488. splice_actor *actor)
  489. {
  490. int ret, do_wakeup, err;
  491. struct splice_desc sd;
  492. ret = 0;
  493. do_wakeup = 0;
  494. sd.total_len = len;
  495. sd.flags = flags;
  496. sd.file = out;
  497. sd.pos = out->f_pos;
  498. if (pipe->inode)
  499. mutex_lock(&pipe->inode->i_mutex);
  500. for (;;) {
  501. int bufs = pipe->nrbufs;
  502. if (bufs) {
  503. int curbuf = pipe->curbuf;
  504. struct pipe_buffer *buf = pipe->bufs + curbuf;
  505. struct pipe_buf_operations *ops = buf->ops;
  506. sd.len = buf->len;
  507. if (sd.len > sd.total_len)
  508. sd.len = sd.total_len;
  509. err = actor(pipe, buf, &sd);
  510. if (err) {
  511. if (!ret && err != -ENODATA)
  512. ret = err;
  513. break;
  514. }
  515. ret += sd.len;
  516. buf->offset += sd.len;
  517. buf->len -= sd.len;
  518. if (!buf->len) {
  519. buf->ops = NULL;
  520. ops->release(pipe, buf);
  521. curbuf = (curbuf + 1) & (PIPE_BUFFERS - 1);
  522. pipe->curbuf = curbuf;
  523. pipe->nrbufs = --bufs;
  524. do_wakeup = 1;
  525. }
  526. sd.pos += sd.len;
  527. sd.total_len -= sd.len;
  528. if (!sd.total_len)
  529. break;
  530. }
  531. if (bufs)
  532. continue;
  533. if (!pipe->writers)
  534. break;
  535. if (!pipe->waiting_writers) {
  536. if (ret)
  537. break;
  538. }
  539. if (flags & SPLICE_F_NONBLOCK) {
  540. if (!ret)
  541. ret = -EAGAIN;
  542. break;
  543. }
  544. if (signal_pending(current)) {
  545. if (!ret)
  546. ret = -ERESTARTSYS;
  547. break;
  548. }
  549. if (do_wakeup) {
  550. smp_mb();
  551. if (waitqueue_active(&pipe->wait))
  552. wake_up_interruptible_sync(&pipe->wait);
  553. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  554. do_wakeup = 0;
  555. }
  556. pipe_wait(pipe);
  557. }
  558. if (pipe->inode)
  559. mutex_unlock(&pipe->inode->i_mutex);
  560. if (do_wakeup) {
  561. smp_mb();
  562. if (waitqueue_active(&pipe->wait))
  563. wake_up_interruptible(&pipe->wait);
  564. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  565. }
  566. mutex_lock(&out->f_mapping->host->i_mutex);
  567. out->f_pos = sd.pos;
  568. mutex_unlock(&out->f_mapping->host->i_mutex);
  569. return ret;
  570. }
  571. /**
  572. * generic_file_splice_write - splice data from a pipe to a file
  573. * @pipe: pipe info
  574. * @out: file to write to
  575. * @len: number of bytes to splice
  576. * @flags: splice modifier flags
  577. *
  578. * Will either move or copy pages (determined by @flags options) from
  579. * the given pipe inode to the given file.
  580. *
  581. */
  582. ssize_t
  583. generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  584. size_t len, unsigned int flags)
  585. {
  586. struct address_space *mapping = out->f_mapping;
  587. ssize_t ret;
  588. ret = move_from_pipe(pipe, out, len, flags, pipe_to_file);
  589. /*
  590. * if file or inode is SYNC and we actually wrote some data, sync it
  591. */
  592. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(mapping->host))
  593. && ret > 0) {
  594. struct inode *inode = mapping->host;
  595. int err;
  596. mutex_lock(&inode->i_mutex);
  597. err = generic_osync_inode(mapping->host, mapping,
  598. OSYNC_METADATA|OSYNC_DATA);
  599. mutex_unlock(&inode->i_mutex);
  600. if (err)
  601. ret = err;
  602. }
  603. return ret;
  604. }
  605. EXPORT_SYMBOL(generic_file_splice_write);
  606. /**
  607. * generic_splice_sendpage - splice data from a pipe to a socket
  608. * @inode: pipe inode
  609. * @out: socket to write to
  610. * @len: number of bytes to splice
  611. * @flags: splice modifier flags
  612. *
  613. * Will send @len bytes from the pipe to a network socket. No data copying
  614. * is involved.
  615. *
  616. */
  617. ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
  618. size_t len, unsigned int flags)
  619. {
  620. return move_from_pipe(pipe, out, len, flags, pipe_to_sendpage);
  621. }
  622. EXPORT_SYMBOL(generic_splice_sendpage);
  623. /*
  624. * Attempt to initiate a splice from pipe to file.
  625. */
  626. static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  627. size_t len, unsigned int flags)
  628. {
  629. loff_t pos;
  630. int ret;
  631. if (!out->f_op || !out->f_op->splice_write)
  632. return -EINVAL;
  633. if (!(out->f_mode & FMODE_WRITE))
  634. return -EBADF;
  635. pos = out->f_pos;
  636. ret = rw_verify_area(WRITE, out, &pos, len);
  637. if (unlikely(ret < 0))
  638. return ret;
  639. return out->f_op->splice_write(pipe, out, len, flags);
  640. }
  641. /*
  642. * Attempt to initiate a splice from a file to a pipe.
  643. */
  644. static long do_splice_to(struct file *in, struct pipe_inode_info *pipe,
  645. size_t len, unsigned int flags)
  646. {
  647. loff_t pos, isize, left;
  648. int ret;
  649. if (!in->f_op || !in->f_op->splice_read)
  650. return -EINVAL;
  651. if (!(in->f_mode & FMODE_READ))
  652. return -EBADF;
  653. pos = in->f_pos;
  654. ret = rw_verify_area(READ, in, &pos, len);
  655. if (unlikely(ret < 0))
  656. return ret;
  657. isize = i_size_read(in->f_mapping->host);
  658. if (unlikely(in->f_pos >= isize))
  659. return 0;
  660. left = isize - in->f_pos;
  661. if (left < len)
  662. len = left;
  663. return in->f_op->splice_read(in, pipe, len, flags);
  664. }
  665. long do_splice_direct(struct file *in, struct file *out, size_t len,
  666. unsigned int flags)
  667. {
  668. struct pipe_inode_info *pipe;
  669. long ret, bytes;
  670. umode_t i_mode;
  671. int i;
  672. /*
  673. * We require the input being a regular file, as we don't want to
  674. * randomly drop data for eg socket -> socket splicing. Use the
  675. * piped splicing for that!
  676. */
  677. i_mode = in->f_dentry->d_inode->i_mode;
  678. if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  679. return -EINVAL;
  680. /*
  681. * neither in nor out is a pipe, setup an internal pipe attached to
  682. * 'out' and transfer the wanted data from 'in' to 'out' through that
  683. */
  684. pipe = current->splice_pipe;
  685. if (!pipe) {
  686. pipe = alloc_pipe_info(NULL);
  687. if (!pipe)
  688. return -ENOMEM;
  689. /*
  690. * We don't have an immediate reader, but we'll read the stuff
  691. * out of the pipe right after the move_to_pipe(). So set
  692. * PIPE_READERS appropriately.
  693. */
  694. pipe->readers = 1;
  695. current->splice_pipe = pipe;
  696. }
  697. /*
  698. * do the splice
  699. */
  700. ret = 0;
  701. bytes = 0;
  702. while (len) {
  703. size_t read_len, max_read_len;
  704. /*
  705. * Do at most PIPE_BUFFERS pages worth of transfer:
  706. */
  707. max_read_len = min(len, (size_t)(PIPE_BUFFERS*PAGE_SIZE));
  708. ret = do_splice_to(in, pipe, max_read_len, flags);
  709. if (unlikely(ret < 0))
  710. goto out_release;
  711. read_len = ret;
  712. /*
  713. * NOTE: nonblocking mode only applies to the input. We
  714. * must not do the output in nonblocking mode as then we
  715. * could get stuck data in the internal pipe:
  716. */
  717. ret = do_splice_from(pipe, out, read_len,
  718. flags & ~SPLICE_F_NONBLOCK);
  719. if (unlikely(ret < 0))
  720. goto out_release;
  721. bytes += ret;
  722. len -= ret;
  723. /*
  724. * In nonblocking mode, if we got back a short read then
  725. * that was due to either an IO error or due to the
  726. * pagecache entry not being there. In the IO error case
  727. * the _next_ splice attempt will produce a clean IO error
  728. * return value (not a short read), so in both cases it's
  729. * correct to break out of the loop here:
  730. */
  731. if ((flags & SPLICE_F_NONBLOCK) && (read_len < max_read_len))
  732. break;
  733. }
  734. pipe->nrbufs = pipe->curbuf = 0;
  735. return bytes;
  736. out_release:
  737. /*
  738. * If we did an incomplete transfer we must release
  739. * the pipe buffers in question:
  740. */
  741. for (i = 0; i < PIPE_BUFFERS; i++) {
  742. struct pipe_buffer *buf = pipe->bufs + i;
  743. if (buf->ops) {
  744. buf->ops->release(pipe, buf);
  745. buf->ops = NULL;
  746. }
  747. }
  748. pipe->nrbufs = pipe->curbuf = 0;
  749. /*
  750. * If we transferred some data, return the number of bytes:
  751. */
  752. if (bytes > 0)
  753. return bytes;
  754. return ret;
  755. }
  756. EXPORT_SYMBOL(do_splice_direct);
  757. /*
  758. * Determine where to splice to/from.
  759. */
  760. static long do_splice(struct file *in, loff_t __user *off_in,
  761. struct file *out, loff_t __user *off_out,
  762. size_t len, unsigned int flags)
  763. {
  764. struct pipe_inode_info *pipe;
  765. pipe = in->f_dentry->d_inode->i_pipe;
  766. if (pipe) {
  767. if (off_in)
  768. return -ESPIPE;
  769. if (off_out) {
  770. if (out->f_op->llseek == no_llseek)
  771. return -EINVAL;
  772. if (copy_from_user(&out->f_pos, off_out,
  773. sizeof(loff_t)))
  774. return -EFAULT;
  775. }
  776. return do_splice_from(pipe, out, len, flags);
  777. }
  778. pipe = out->f_dentry->d_inode->i_pipe;
  779. if (pipe) {
  780. if (off_out)
  781. return -ESPIPE;
  782. if (off_in) {
  783. if (in->f_op->llseek == no_llseek)
  784. return -EINVAL;
  785. if (copy_from_user(&in->f_pos, off_in, sizeof(loff_t)))
  786. return -EFAULT;
  787. }
  788. return do_splice_to(in, pipe, len, flags);
  789. }
  790. return -EINVAL;
  791. }
  792. asmlinkage long sys_splice(int fd_in, loff_t __user *off_in,
  793. int fd_out, loff_t __user *off_out,
  794. size_t len, unsigned int flags)
  795. {
  796. long error;
  797. struct file *in, *out;
  798. int fput_in, fput_out;
  799. if (unlikely(!len))
  800. return 0;
  801. error = -EBADF;
  802. in = fget_light(fd_in, &fput_in);
  803. if (in) {
  804. if (in->f_mode & FMODE_READ) {
  805. out = fget_light(fd_out, &fput_out);
  806. if (out) {
  807. if (out->f_mode & FMODE_WRITE)
  808. error = do_splice(in, off_in,
  809. out, off_out,
  810. len, flags);
  811. fput_light(out, fput_out);
  812. }
  813. }
  814. fput_light(in, fput_in);
  815. }
  816. return error;
  817. }