splice.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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(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().
  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. unsigned int offset;
  388. ssize_t ret;
  389. void *ptr;
  390. int more;
  391. /*
  392. * Sub-optimal, but we are limited by the pipe ->map. We don't
  393. * need a kmap'ed buffer here, we just want to make sure we
  394. * have the page pinned if the pipe page originates from the
  395. * page cache.
  396. */
  397. ptr = buf->ops->map(file, info, buf);
  398. if (IS_ERR(ptr))
  399. return PTR_ERR(ptr);
  400. offset = pos & ~PAGE_CACHE_MASK;
  401. more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
  402. ret = file->f_op->sendpage(file, buf->page, offset, sd->len, &pos,more);
  403. buf->ops->unmap(info, buf);
  404. if (ret == sd->len)
  405. return 0;
  406. return -EIO;
  407. }
  408. /*
  409. * This is a little more tricky than the file -> pipe splicing. There are
  410. * basically three cases:
  411. *
  412. * - Destination page already exists in the address space and there
  413. * are users of it. For that case we have no other option that
  414. * copying the data. Tough luck.
  415. * - Destination page already exists in the address space, but there
  416. * are no users of it. Make sure it's uptodate, then drop it. Fall
  417. * through to last case.
  418. * - Destination page does not exist, we can add the pipe page to
  419. * the page cache and avoid the copy.
  420. *
  421. * If asked to move pages to the output file (SPLICE_F_MOVE is set in
  422. * sd->flags), we attempt to migrate pages from the pipe to the output
  423. * file address space page cache. This is possible if no one else has
  424. * the pipe page referenced outside of the pipe and page cache. If
  425. * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
  426. * a new page in the output file page cache and fill/dirty that.
  427. */
  428. static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
  429. struct splice_desc *sd)
  430. {
  431. struct file *file = sd->file;
  432. struct address_space *mapping = file->f_mapping;
  433. gfp_t gfp_mask = mapping_gfp_mask(mapping);
  434. unsigned int offset;
  435. struct page *page;
  436. pgoff_t index;
  437. char *src;
  438. int ret;
  439. /*
  440. * make sure the data in this buffer is uptodate
  441. */
  442. src = buf->ops->map(file, info, buf);
  443. if (IS_ERR(src))
  444. return PTR_ERR(src);
  445. index = sd->pos >> PAGE_CACHE_SHIFT;
  446. offset = sd->pos & ~PAGE_CACHE_MASK;
  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 (sd->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, 0, sd->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, sd->len);
  516. flush_dcache_page(page);
  517. kunmap_atomic(dst, KM_USER0);
  518. }
  519. ret = mapping->a_ops->commit_write(file, page, 0, sd->len);
  520. if (ret == AOP_TRUNCATED_PAGE) {
  521. page_cache_release(page);
  522. goto find_page;
  523. } else if (ret)
  524. goto out;
  525. mark_page_accessed(page);
  526. balance_dirty_pages_ratelimited(mapping);
  527. out:
  528. if (!(buf->flags & PIPE_BUF_FLAG_STOLEN))
  529. page_cache_release(page);
  530. unlock_page(page);
  531. out_nomem:
  532. buf->ops->unmap(info, buf);
  533. return ret;
  534. }
  535. typedef int (splice_actor)(struct pipe_inode_info *, struct pipe_buffer *,
  536. struct splice_desc *);
  537. /*
  538. * Pipe input worker. Most of this logic works like a regular pipe, the
  539. * key here is the 'actor' worker passed in that actually moves the data
  540. * to the wanted destination. See pipe_to_file/pipe_to_sendpage above.
  541. */
  542. static ssize_t move_from_pipe(struct pipe_inode_info *pipe, struct file *out,
  543. loff_t *ppos, size_t len, unsigned int flags,
  544. splice_actor *actor)
  545. {
  546. int ret, do_wakeup, err;
  547. struct splice_desc sd;
  548. ret = 0;
  549. do_wakeup = 0;
  550. sd.total_len = len;
  551. sd.flags = flags;
  552. sd.file = out;
  553. sd.pos = *ppos;
  554. if (pipe->inode)
  555. mutex_lock(&pipe->inode->i_mutex);
  556. for (;;) {
  557. if (pipe->nrbufs) {
  558. struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  559. struct pipe_buf_operations *ops = buf->ops;
  560. sd.len = buf->len;
  561. if (sd.len > sd.total_len)
  562. sd.len = sd.total_len;
  563. err = actor(pipe, buf, &sd);
  564. if (err) {
  565. if (!ret && err != -ENODATA)
  566. ret = err;
  567. break;
  568. }
  569. ret += sd.len;
  570. buf->offset += sd.len;
  571. buf->len -= sd.len;
  572. if (!buf->len) {
  573. buf->ops = NULL;
  574. ops->release(pipe, buf);
  575. pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
  576. pipe->nrbufs--;
  577. if (pipe->inode)
  578. do_wakeup = 1;
  579. }
  580. sd.pos += sd.len;
  581. sd.total_len -= sd.len;
  582. if (!sd.total_len)
  583. break;
  584. }
  585. if (pipe->nrbufs)
  586. continue;
  587. if (!pipe->writers)
  588. break;
  589. if (!pipe->waiting_writers) {
  590. if (ret)
  591. break;
  592. }
  593. if (flags & SPLICE_F_NONBLOCK) {
  594. if (!ret)
  595. ret = -EAGAIN;
  596. break;
  597. }
  598. if (signal_pending(current)) {
  599. if (!ret)
  600. ret = -ERESTARTSYS;
  601. break;
  602. }
  603. if (do_wakeup) {
  604. smp_mb();
  605. if (waitqueue_active(&pipe->wait))
  606. wake_up_interruptible_sync(&pipe->wait);
  607. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  608. do_wakeup = 0;
  609. }
  610. pipe_wait(pipe);
  611. }
  612. if (pipe->inode)
  613. mutex_unlock(&pipe->inode->i_mutex);
  614. if (do_wakeup) {
  615. smp_mb();
  616. if (waitqueue_active(&pipe->wait))
  617. wake_up_interruptible(&pipe->wait);
  618. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  619. }
  620. return ret;
  621. }
  622. /**
  623. * generic_file_splice_write - splice data from a pipe to a file
  624. * @pipe: pipe info
  625. * @out: file to write to
  626. * @len: number of bytes to splice
  627. * @flags: splice modifier flags
  628. *
  629. * Will either move or copy pages (determined by @flags options) from
  630. * the given pipe inode to the given file.
  631. *
  632. */
  633. ssize_t
  634. generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  635. loff_t *ppos, size_t len, unsigned int flags)
  636. {
  637. struct address_space *mapping = out->f_mapping;
  638. ssize_t ret;
  639. ret = move_from_pipe(pipe, out, ppos, len, flags, pipe_to_file);
  640. if (ret > 0) {
  641. struct inode *inode = mapping->host;
  642. *ppos += ret;
  643. /*
  644. * If file or inode is SYNC and we actually wrote some data,
  645. * sync it.
  646. */
  647. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  648. int err;
  649. mutex_lock(&inode->i_mutex);
  650. err = generic_osync_inode(inode, mapping,
  651. OSYNC_METADATA|OSYNC_DATA);
  652. mutex_unlock(&inode->i_mutex);
  653. if (err)
  654. ret = err;
  655. }
  656. }
  657. return ret;
  658. }
  659. EXPORT_SYMBOL(generic_file_splice_write);
  660. /**
  661. * generic_splice_sendpage - splice data from a pipe to a socket
  662. * @inode: pipe inode
  663. * @out: socket to write to
  664. * @len: number of bytes to splice
  665. * @flags: splice modifier flags
  666. *
  667. * Will send @len bytes from the pipe to a network socket. No data copying
  668. * is involved.
  669. *
  670. */
  671. ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
  672. loff_t *ppos, size_t len, unsigned int flags)
  673. {
  674. return move_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
  675. }
  676. EXPORT_SYMBOL(generic_splice_sendpage);
  677. /*
  678. * Attempt to initiate a splice from pipe to file.
  679. */
  680. static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  681. loff_t *ppos, size_t len, unsigned int flags)
  682. {
  683. int ret;
  684. if (unlikely(!out->f_op || !out->f_op->splice_write))
  685. return -EINVAL;
  686. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  687. return -EBADF;
  688. ret = rw_verify_area(WRITE, out, ppos, len);
  689. if (unlikely(ret < 0))
  690. return ret;
  691. return out->f_op->splice_write(pipe, out, ppos, len, flags);
  692. }
  693. /*
  694. * Attempt to initiate a splice from a file to a pipe.
  695. */
  696. static long do_splice_to(struct file *in, loff_t *ppos,
  697. struct pipe_inode_info *pipe, size_t len,
  698. unsigned int flags)
  699. {
  700. loff_t isize, left;
  701. int ret;
  702. if (unlikely(!in->f_op || !in->f_op->splice_read))
  703. return -EINVAL;
  704. if (unlikely(!(in->f_mode & FMODE_READ)))
  705. return -EBADF;
  706. ret = rw_verify_area(READ, in, ppos, len);
  707. if (unlikely(ret < 0))
  708. return ret;
  709. isize = i_size_read(in->f_mapping->host);
  710. if (unlikely(*ppos >= isize))
  711. return 0;
  712. left = isize - *ppos;
  713. if (unlikely(left < len))
  714. len = left;
  715. return in->f_op->splice_read(in, ppos, pipe, len, flags);
  716. }
  717. long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
  718. size_t len, unsigned int flags)
  719. {
  720. struct pipe_inode_info *pipe;
  721. long ret, bytes;
  722. loff_t out_off;
  723. umode_t i_mode;
  724. int i;
  725. /*
  726. * We require the input being a regular file, as we don't want to
  727. * randomly drop data for eg socket -> socket splicing. Use the
  728. * piped splicing for that!
  729. */
  730. i_mode = in->f_dentry->d_inode->i_mode;
  731. if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  732. return -EINVAL;
  733. /*
  734. * neither in nor out is a pipe, setup an internal pipe attached to
  735. * 'out' and transfer the wanted data from 'in' to 'out' through that
  736. */
  737. pipe = current->splice_pipe;
  738. if (unlikely(!pipe)) {
  739. pipe = alloc_pipe_info(NULL);
  740. if (!pipe)
  741. return -ENOMEM;
  742. /*
  743. * We don't have an immediate reader, but we'll read the stuff
  744. * out of the pipe right after the move_to_pipe(). So set
  745. * PIPE_READERS appropriately.
  746. */
  747. pipe->readers = 1;
  748. current->splice_pipe = pipe;
  749. }
  750. /*
  751. * Do the splice.
  752. */
  753. ret = 0;
  754. bytes = 0;
  755. out_off = 0;
  756. while (len) {
  757. size_t read_len, max_read_len;
  758. /*
  759. * Do at most PIPE_BUFFERS pages worth of transfer:
  760. */
  761. max_read_len = min(len, (size_t)(PIPE_BUFFERS*PAGE_SIZE));
  762. ret = do_splice_to(in, ppos, pipe, max_read_len, flags);
  763. if (unlikely(ret < 0))
  764. goto out_release;
  765. read_len = ret;
  766. /*
  767. * NOTE: nonblocking mode only applies to the input. We
  768. * must not do the output in nonblocking mode as then we
  769. * could get stuck data in the internal pipe:
  770. */
  771. ret = do_splice_from(pipe, out, &out_off, read_len,
  772. flags & ~SPLICE_F_NONBLOCK);
  773. if (unlikely(ret < 0))
  774. goto out_release;
  775. bytes += ret;
  776. len -= ret;
  777. /*
  778. * In nonblocking mode, if we got back a short read then
  779. * that was due to either an IO error or due to the
  780. * pagecache entry not being there. In the IO error case
  781. * the _next_ splice attempt will produce a clean IO error
  782. * return value (not a short read), so in both cases it's
  783. * correct to break out of the loop here:
  784. */
  785. if ((flags & SPLICE_F_NONBLOCK) && (read_len < max_read_len))
  786. break;
  787. }
  788. pipe->nrbufs = pipe->curbuf = 0;
  789. return bytes;
  790. out_release:
  791. /*
  792. * If we did an incomplete transfer we must release
  793. * the pipe buffers in question:
  794. */
  795. for (i = 0; i < PIPE_BUFFERS; i++) {
  796. struct pipe_buffer *buf = pipe->bufs + i;
  797. if (buf->ops) {
  798. buf->ops->release(pipe, buf);
  799. buf->ops = NULL;
  800. }
  801. }
  802. pipe->nrbufs = pipe->curbuf = 0;
  803. /*
  804. * If we transferred some data, return the number of bytes:
  805. */
  806. if (bytes > 0)
  807. return bytes;
  808. return ret;
  809. }
  810. EXPORT_SYMBOL(do_splice_direct);
  811. /*
  812. * Determine where to splice to/from.
  813. */
  814. static long do_splice(struct file *in, loff_t __user *off_in,
  815. struct file *out, loff_t __user *off_out,
  816. size_t len, unsigned int flags)
  817. {
  818. struct pipe_inode_info *pipe;
  819. loff_t offset, *off;
  820. long ret;
  821. pipe = in->f_dentry->d_inode->i_pipe;
  822. if (pipe) {
  823. if (off_in)
  824. return -ESPIPE;
  825. if (off_out) {
  826. if (out->f_op->llseek == no_llseek)
  827. return -EINVAL;
  828. if (copy_from_user(&offset, off_out, sizeof(loff_t)))
  829. return -EFAULT;
  830. off = &offset;
  831. } else
  832. off = &out->f_pos;
  833. ret = do_splice_from(pipe, out, off, len, flags);
  834. if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
  835. ret = -EFAULT;
  836. return ret;
  837. }
  838. pipe = out->f_dentry->d_inode->i_pipe;
  839. if (pipe) {
  840. if (off_out)
  841. return -ESPIPE;
  842. if (off_in) {
  843. if (in->f_op->llseek == no_llseek)
  844. return -EINVAL;
  845. if (copy_from_user(&offset, off_in, sizeof(loff_t)))
  846. return -EFAULT;
  847. off = &offset;
  848. } else
  849. off = &in->f_pos;
  850. ret = do_splice_to(in, off, pipe, len, flags);
  851. if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
  852. ret = -EFAULT;
  853. return ret;
  854. }
  855. return -EINVAL;
  856. }
  857. asmlinkage long sys_splice(int fd_in, loff_t __user *off_in,
  858. int fd_out, loff_t __user *off_out,
  859. size_t len, unsigned int flags)
  860. {
  861. long error;
  862. struct file *in, *out;
  863. int fput_in, fput_out;
  864. if (unlikely(!len))
  865. return 0;
  866. error = -EBADF;
  867. in = fget_light(fd_in, &fput_in);
  868. if (in) {
  869. if (in->f_mode & FMODE_READ) {
  870. out = fget_light(fd_out, &fput_out);
  871. if (out) {
  872. if (out->f_mode & FMODE_WRITE)
  873. error = do_splice(in, off_in,
  874. out, off_out,
  875. len, flags);
  876. fput_light(out, fput_out);
  877. }
  878. }
  879. fput_light(in, fput_in);
  880. }
  881. return error;
  882. }
  883. /*
  884. * Link contents of ipipe to opipe.
  885. */
  886. static int link_pipe(struct pipe_inode_info *ipipe,
  887. struct pipe_inode_info *opipe,
  888. size_t len, unsigned int flags)
  889. {
  890. struct pipe_buffer *ibuf, *obuf;
  891. int ret, do_wakeup, i, ipipe_first;
  892. ret = do_wakeup = ipipe_first = 0;
  893. /*
  894. * Potential ABBA deadlock, work around it by ordering lock
  895. * grabbing by inode address. Otherwise two different processes
  896. * could deadlock (one doing tee from A -> B, the other from B -> A).
  897. */
  898. if (ipipe->inode < opipe->inode) {
  899. ipipe_first = 1;
  900. mutex_lock(&ipipe->inode->i_mutex);
  901. mutex_lock(&opipe->inode->i_mutex);
  902. } else {
  903. mutex_lock(&opipe->inode->i_mutex);
  904. mutex_lock(&ipipe->inode->i_mutex);
  905. }
  906. for (i = 0;; i++) {
  907. if (!opipe->readers) {
  908. send_sig(SIGPIPE, current, 0);
  909. if (!ret)
  910. ret = -EPIPE;
  911. break;
  912. }
  913. if (ipipe->nrbufs - i) {
  914. ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
  915. /*
  916. * If we have room, fill this buffer
  917. */
  918. if (opipe->nrbufs < PIPE_BUFFERS) {
  919. int nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
  920. /*
  921. * Get a reference to this pipe buffer,
  922. * so we can copy the contents over.
  923. */
  924. ibuf->ops->get(ipipe, ibuf);
  925. obuf = opipe->bufs + nbuf;
  926. *obuf = *ibuf;
  927. if (obuf->len > len)
  928. obuf->len = len;
  929. opipe->nrbufs++;
  930. do_wakeup = 1;
  931. ret += obuf->len;
  932. len -= obuf->len;
  933. if (!len)
  934. break;
  935. if (opipe->nrbufs < PIPE_BUFFERS)
  936. continue;
  937. }
  938. /*
  939. * We have input available, but no output room.
  940. * If we already copied data, return that. If we
  941. * need to drop the opipe lock, it must be ordered
  942. * last to avoid deadlocks.
  943. */
  944. if ((flags & SPLICE_F_NONBLOCK) || !ipipe_first) {
  945. if (!ret)
  946. ret = -EAGAIN;
  947. break;
  948. }
  949. if (signal_pending(current)) {
  950. if (!ret)
  951. ret = -ERESTARTSYS;
  952. break;
  953. }
  954. if (do_wakeup) {
  955. smp_mb();
  956. if (waitqueue_active(&opipe->wait))
  957. wake_up_interruptible(&opipe->wait);
  958. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  959. do_wakeup = 0;
  960. }
  961. opipe->waiting_writers++;
  962. pipe_wait(opipe);
  963. opipe->waiting_writers--;
  964. continue;
  965. }
  966. /*
  967. * No input buffers, do the usual checks for available
  968. * writers and blocking and wait if necessary
  969. */
  970. if (!ipipe->writers)
  971. break;
  972. if (!ipipe->waiting_writers) {
  973. if (ret)
  974. break;
  975. }
  976. /*
  977. * pipe_wait() drops the ipipe mutex. To avoid deadlocks
  978. * with another process, we can only safely do that if
  979. * the ipipe lock is ordered last.
  980. */
  981. if ((flags & SPLICE_F_NONBLOCK) || ipipe_first) {
  982. if (!ret)
  983. ret = -EAGAIN;
  984. break;
  985. }
  986. if (signal_pending(current)) {
  987. if (!ret)
  988. ret = -ERESTARTSYS;
  989. break;
  990. }
  991. if (waitqueue_active(&ipipe->wait))
  992. wake_up_interruptible_sync(&ipipe->wait);
  993. kill_fasync(&ipipe->fasync_writers, SIGIO, POLL_OUT);
  994. pipe_wait(ipipe);
  995. }
  996. mutex_unlock(&ipipe->inode->i_mutex);
  997. mutex_unlock(&opipe->inode->i_mutex);
  998. if (do_wakeup) {
  999. smp_mb();
  1000. if (waitqueue_active(&opipe->wait))
  1001. wake_up_interruptible(&opipe->wait);
  1002. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  1003. }
  1004. return ret;
  1005. }
  1006. /*
  1007. * This is a tee(1) implementation that works on pipes. It doesn't copy
  1008. * any data, it simply references the 'in' pages on the 'out' pipe.
  1009. * The 'flags' used are the SPLICE_F_* variants, currently the only
  1010. * applicable one is SPLICE_F_NONBLOCK.
  1011. */
  1012. static long do_tee(struct file *in, struct file *out, size_t len,
  1013. unsigned int flags)
  1014. {
  1015. struct pipe_inode_info *ipipe = in->f_dentry->d_inode->i_pipe;
  1016. struct pipe_inode_info *opipe = out->f_dentry->d_inode->i_pipe;
  1017. /*
  1018. * Link ipipe to the two output pipes, consuming as we go along.
  1019. */
  1020. if (ipipe && opipe)
  1021. return link_pipe(ipipe, opipe, len, flags);
  1022. return -EINVAL;
  1023. }
  1024. asmlinkage long sys_tee(int fdin, int fdout, size_t len, unsigned int flags)
  1025. {
  1026. struct file *in;
  1027. int error, fput_in;
  1028. if (unlikely(!len))
  1029. return 0;
  1030. error = -EBADF;
  1031. in = fget_light(fdin, &fput_in);
  1032. if (in) {
  1033. if (in->f_mode & FMODE_READ) {
  1034. int fput_out;
  1035. struct file *out = fget_light(fdout, &fput_out);
  1036. if (out) {
  1037. if (out->f_mode & FMODE_WRITE)
  1038. error = do_tee(in, out, len, flags);
  1039. fput_light(out, fput_out);
  1040. }
  1041. }
  1042. fput_light(in, fput_in);
  1043. }
  1044. return error;
  1045. }