splice.c 27 KB

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