splice.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  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@kernel.dk>
  16. * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
  17. * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
  18. *
  19. */
  20. #include <linux/bvec.h>
  21. #include <linux/fs.h>
  22. #include <linux/file.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/splice.h>
  25. #include <linux/memcontrol.h>
  26. #include <linux/mm_inline.h>
  27. #include <linux/swap.h>
  28. #include <linux/writeback.h>
  29. #include <linux/export.h>
  30. #include <linux/syscalls.h>
  31. #include <linux/uio.h>
  32. #include <linux/security.h>
  33. #include <linux/gfp.h>
  34. #include <linux/socket.h>
  35. #include <linux/compat.h>
  36. #include <linux/sched/signal.h>
  37. #include "internal.h"
  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 *pipe,
  45. struct pipe_buffer *buf)
  46. {
  47. struct page *page = buf->page;
  48. struct address_space *mapping;
  49. lock_page(page);
  50. mapping = page_mapping(page);
  51. if (mapping) {
  52. WARN_ON(!PageUptodate(page));
  53. /*
  54. * At least for ext2 with nobh option, we need to wait on
  55. * writeback completing on this page, since we'll remove it
  56. * from the pagecache. Otherwise truncate wont wait on the
  57. * page, allowing the disk blocks to be reused by someone else
  58. * before we actually wrote our data to them. fs corruption
  59. * ensues.
  60. */
  61. wait_on_page_writeback(page);
  62. if (page_has_private(page) &&
  63. !try_to_release_page(page, GFP_KERNEL))
  64. goto out_unlock;
  65. /*
  66. * If we succeeded in removing the mapping, set LRU flag
  67. * and return good.
  68. */
  69. if (remove_mapping(mapping, page)) {
  70. buf->flags |= PIPE_BUF_FLAG_LRU;
  71. return 0;
  72. }
  73. }
  74. /*
  75. * Raced with truncate or failed to remove page from current
  76. * address space, unlock and return failure.
  77. */
  78. out_unlock:
  79. unlock_page(page);
  80. return 1;
  81. }
  82. static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
  83. struct pipe_buffer *buf)
  84. {
  85. put_page(buf->page);
  86. buf->flags &= ~PIPE_BUF_FLAG_LRU;
  87. }
  88. /*
  89. * Check whether the contents of buf is OK to access. Since the content
  90. * is a page cache page, IO may be in flight.
  91. */
  92. static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
  93. struct pipe_buffer *buf)
  94. {
  95. struct page *page = buf->page;
  96. int err;
  97. if (!PageUptodate(page)) {
  98. lock_page(page);
  99. /*
  100. * Page got truncated/unhashed. This will cause a 0-byte
  101. * splice, if this is the first page.
  102. */
  103. if (!page->mapping) {
  104. err = -ENODATA;
  105. goto error;
  106. }
  107. /*
  108. * Uh oh, read-error from disk.
  109. */
  110. if (!PageUptodate(page)) {
  111. err = -EIO;
  112. goto error;
  113. }
  114. /*
  115. * Page is ok afterall, we are done.
  116. */
  117. unlock_page(page);
  118. }
  119. return 0;
  120. error:
  121. unlock_page(page);
  122. return err;
  123. }
  124. const struct pipe_buf_operations page_cache_pipe_buf_ops = {
  125. .can_merge = 0,
  126. .confirm = page_cache_pipe_buf_confirm,
  127. .release = page_cache_pipe_buf_release,
  128. .steal = page_cache_pipe_buf_steal,
  129. .get = generic_pipe_buf_get,
  130. };
  131. static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
  132. struct pipe_buffer *buf)
  133. {
  134. if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
  135. return 1;
  136. buf->flags |= PIPE_BUF_FLAG_LRU;
  137. return generic_pipe_buf_steal(pipe, buf);
  138. }
  139. static const struct pipe_buf_operations user_page_pipe_buf_ops = {
  140. .can_merge = 0,
  141. .confirm = generic_pipe_buf_confirm,
  142. .release = page_cache_pipe_buf_release,
  143. .steal = user_page_pipe_buf_steal,
  144. .get = generic_pipe_buf_get,
  145. };
  146. static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
  147. {
  148. smp_mb();
  149. if (waitqueue_active(&pipe->wait))
  150. wake_up_interruptible(&pipe->wait);
  151. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  152. }
  153. /**
  154. * splice_to_pipe - fill passed data into a pipe
  155. * @pipe: pipe to fill
  156. * @spd: data to fill
  157. *
  158. * Description:
  159. * @spd contains a map of pages and len/offset tuples, along with
  160. * the struct pipe_buf_operations associated with these pages. This
  161. * function will link that data to the pipe.
  162. *
  163. */
  164. ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
  165. struct splice_pipe_desc *spd)
  166. {
  167. unsigned int spd_pages = spd->nr_pages;
  168. int ret = 0, page_nr = 0;
  169. if (!spd_pages)
  170. return 0;
  171. if (unlikely(!pipe->readers)) {
  172. send_sig(SIGPIPE, current, 0);
  173. ret = -EPIPE;
  174. goto out;
  175. }
  176. while (pipe->nrbufs < pipe->buffers) {
  177. int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
  178. struct pipe_buffer *buf = pipe->bufs + newbuf;
  179. buf->page = spd->pages[page_nr];
  180. buf->offset = spd->partial[page_nr].offset;
  181. buf->len = spd->partial[page_nr].len;
  182. buf->private = spd->partial[page_nr].private;
  183. buf->ops = spd->ops;
  184. buf->flags = 0;
  185. pipe->nrbufs++;
  186. page_nr++;
  187. ret += buf->len;
  188. if (!--spd->nr_pages)
  189. break;
  190. }
  191. if (!ret)
  192. ret = -EAGAIN;
  193. out:
  194. while (page_nr < spd_pages)
  195. spd->spd_release(spd, page_nr++);
  196. return ret;
  197. }
  198. EXPORT_SYMBOL_GPL(splice_to_pipe);
  199. ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
  200. {
  201. int ret;
  202. if (unlikely(!pipe->readers)) {
  203. send_sig(SIGPIPE, current, 0);
  204. ret = -EPIPE;
  205. } else if (pipe->nrbufs == pipe->buffers) {
  206. ret = -EAGAIN;
  207. } else {
  208. int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
  209. pipe->bufs[newbuf] = *buf;
  210. pipe->nrbufs++;
  211. return buf->len;
  212. }
  213. pipe_buf_release(pipe, buf);
  214. return ret;
  215. }
  216. EXPORT_SYMBOL(add_to_pipe);
  217. /*
  218. * Check if we need to grow the arrays holding pages and partial page
  219. * descriptions.
  220. */
  221. int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
  222. {
  223. unsigned int buffers = READ_ONCE(pipe->buffers);
  224. spd->nr_pages_max = buffers;
  225. if (buffers <= PIPE_DEF_BUFFERS)
  226. return 0;
  227. spd->pages = kmalloc_array(buffers, sizeof(struct page *), GFP_KERNEL);
  228. spd->partial = kmalloc_array(buffers, sizeof(struct partial_page),
  229. GFP_KERNEL);
  230. if (spd->pages && spd->partial)
  231. return 0;
  232. kfree(spd->pages);
  233. kfree(spd->partial);
  234. return -ENOMEM;
  235. }
  236. void splice_shrink_spd(struct splice_pipe_desc *spd)
  237. {
  238. if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
  239. return;
  240. kfree(spd->pages);
  241. kfree(spd->partial);
  242. }
  243. /**
  244. * generic_file_splice_read - splice data from file to a pipe
  245. * @in: file to splice from
  246. * @ppos: position in @in
  247. * @pipe: pipe to splice to
  248. * @len: number of bytes to splice
  249. * @flags: splice modifier flags
  250. *
  251. * Description:
  252. * Will read pages from given file and fill them into a pipe. Can be
  253. * used as long as it has more or less sane ->read_iter().
  254. *
  255. */
  256. ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
  257. struct pipe_inode_info *pipe, size_t len,
  258. unsigned int flags)
  259. {
  260. struct iov_iter to;
  261. struct kiocb kiocb;
  262. int idx, ret;
  263. iov_iter_pipe(&to, ITER_PIPE | READ, pipe, len);
  264. idx = to.idx;
  265. init_sync_kiocb(&kiocb, in);
  266. kiocb.ki_pos = *ppos;
  267. ret = call_read_iter(in, &kiocb, &to);
  268. if (ret > 0) {
  269. *ppos = kiocb.ki_pos;
  270. file_accessed(in);
  271. } else if (ret < 0) {
  272. to.idx = idx;
  273. to.iov_offset = 0;
  274. iov_iter_advance(&to, 0); /* to free what was emitted */
  275. /*
  276. * callers of ->splice_read() expect -EAGAIN on
  277. * "can't put anything in there", rather than -EFAULT.
  278. */
  279. if (ret == -EFAULT)
  280. ret = -EAGAIN;
  281. }
  282. return ret;
  283. }
  284. EXPORT_SYMBOL(generic_file_splice_read);
  285. const struct pipe_buf_operations default_pipe_buf_ops = {
  286. .can_merge = 0,
  287. .confirm = generic_pipe_buf_confirm,
  288. .release = generic_pipe_buf_release,
  289. .steal = generic_pipe_buf_steal,
  290. .get = generic_pipe_buf_get,
  291. };
  292. int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
  293. struct pipe_buffer *buf)
  294. {
  295. return 1;
  296. }
  297. /* Pipe buffer operations for a socket and similar. */
  298. const struct pipe_buf_operations nosteal_pipe_buf_ops = {
  299. .can_merge = 0,
  300. .confirm = generic_pipe_buf_confirm,
  301. .release = generic_pipe_buf_release,
  302. .steal = generic_pipe_buf_nosteal,
  303. .get = generic_pipe_buf_get,
  304. };
  305. EXPORT_SYMBOL(nosteal_pipe_buf_ops);
  306. static ssize_t kernel_readv(struct file *file, const struct kvec *vec,
  307. unsigned long vlen, loff_t offset)
  308. {
  309. mm_segment_t old_fs;
  310. loff_t pos = offset;
  311. ssize_t res;
  312. old_fs = get_fs();
  313. set_fs(get_ds());
  314. /* The cast to a user pointer is valid due to the set_fs() */
  315. res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
  316. set_fs(old_fs);
  317. return res;
  318. }
  319. static ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
  320. struct pipe_inode_info *pipe, size_t len,
  321. unsigned int flags)
  322. {
  323. struct kvec *vec, __vec[PIPE_DEF_BUFFERS];
  324. struct iov_iter to;
  325. struct page **pages;
  326. unsigned int nr_pages;
  327. size_t offset, base, copied = 0;
  328. ssize_t res;
  329. int i;
  330. if (pipe->nrbufs == pipe->buffers)
  331. return -EAGAIN;
  332. /*
  333. * Try to keep page boundaries matching to source pagecache ones -
  334. * it probably won't be much help, but...
  335. */
  336. offset = *ppos & ~PAGE_MASK;
  337. iov_iter_pipe(&to, ITER_PIPE | READ, pipe, len + offset);
  338. res = iov_iter_get_pages_alloc(&to, &pages, len + offset, &base);
  339. if (res <= 0)
  340. return -ENOMEM;
  341. nr_pages = DIV_ROUND_UP(res + base, PAGE_SIZE);
  342. vec = __vec;
  343. if (nr_pages > PIPE_DEF_BUFFERS) {
  344. vec = kmalloc_array(nr_pages, sizeof(struct kvec), GFP_KERNEL);
  345. if (unlikely(!vec)) {
  346. res = -ENOMEM;
  347. goto out;
  348. }
  349. }
  350. pipe->bufs[to.idx].offset = offset;
  351. pipe->bufs[to.idx].len -= offset;
  352. for (i = 0; i < nr_pages; i++) {
  353. size_t this_len = min_t(size_t, len, PAGE_SIZE - offset);
  354. vec[i].iov_base = page_address(pages[i]) + offset;
  355. vec[i].iov_len = this_len;
  356. len -= this_len;
  357. offset = 0;
  358. }
  359. res = kernel_readv(in, vec, nr_pages, *ppos);
  360. if (res > 0) {
  361. copied = res;
  362. *ppos += res;
  363. }
  364. if (vec != __vec)
  365. kfree(vec);
  366. out:
  367. for (i = 0; i < nr_pages; i++)
  368. put_page(pages[i]);
  369. kvfree(pages);
  370. iov_iter_advance(&to, copied); /* truncates and discards */
  371. return res;
  372. }
  373. /*
  374. * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
  375. * using sendpage(). Return the number of bytes sent.
  376. */
  377. static int pipe_to_sendpage(struct pipe_inode_info *pipe,
  378. struct pipe_buffer *buf, struct splice_desc *sd)
  379. {
  380. struct file *file = sd->u.file;
  381. loff_t pos = sd->pos;
  382. int more;
  383. if (!likely(file->f_op->sendpage))
  384. return -EINVAL;
  385. more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
  386. if (sd->len < sd->total_len && pipe->nrbufs > 1)
  387. more |= MSG_SENDPAGE_NOTLAST;
  388. return file->f_op->sendpage(file, buf->page, buf->offset,
  389. sd->len, &pos, more);
  390. }
  391. static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
  392. {
  393. smp_mb();
  394. if (waitqueue_active(&pipe->wait))
  395. wake_up_interruptible(&pipe->wait);
  396. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  397. }
  398. /**
  399. * splice_from_pipe_feed - feed available data from a pipe to a file
  400. * @pipe: pipe to splice from
  401. * @sd: information to @actor
  402. * @actor: handler that splices the data
  403. *
  404. * Description:
  405. * This function loops over the pipe and calls @actor to do the
  406. * actual moving of a single struct pipe_buffer to the desired
  407. * destination. It returns when there's no more buffers left in
  408. * the pipe or if the requested number of bytes (@sd->total_len)
  409. * have been copied. It returns a positive number (one) if the
  410. * pipe needs to be filled with more data, zero if the required
  411. * number of bytes have been copied and -errno on error.
  412. *
  413. * This, together with splice_from_pipe_{begin,end,next}, may be
  414. * used to implement the functionality of __splice_from_pipe() when
  415. * locking is required around copying the pipe buffers to the
  416. * destination.
  417. */
  418. static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
  419. splice_actor *actor)
  420. {
  421. int ret;
  422. while (pipe->nrbufs) {
  423. struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  424. sd->len = buf->len;
  425. if (sd->len > sd->total_len)
  426. sd->len = sd->total_len;
  427. ret = pipe_buf_confirm(pipe, buf);
  428. if (unlikely(ret)) {
  429. if (ret == -ENODATA)
  430. ret = 0;
  431. return ret;
  432. }
  433. ret = actor(pipe, buf, sd);
  434. if (ret <= 0)
  435. return ret;
  436. buf->offset += ret;
  437. buf->len -= ret;
  438. sd->num_spliced += ret;
  439. sd->len -= ret;
  440. sd->pos += ret;
  441. sd->total_len -= ret;
  442. if (!buf->len) {
  443. pipe_buf_release(pipe, buf);
  444. pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
  445. pipe->nrbufs--;
  446. if (pipe->files)
  447. sd->need_wakeup = true;
  448. }
  449. if (!sd->total_len)
  450. return 0;
  451. }
  452. return 1;
  453. }
  454. /**
  455. * splice_from_pipe_next - wait for some data to splice from
  456. * @pipe: pipe to splice from
  457. * @sd: information about the splice operation
  458. *
  459. * Description:
  460. * This function will wait for some data and return a positive
  461. * value (one) if pipe buffers are available. It will return zero
  462. * or -errno if no more data needs to be spliced.
  463. */
  464. static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
  465. {
  466. /*
  467. * Check for signal early to make process killable when there are
  468. * always buffers available
  469. */
  470. if (signal_pending(current))
  471. return -ERESTARTSYS;
  472. while (!pipe->nrbufs) {
  473. if (!pipe->writers)
  474. return 0;
  475. if (!pipe->waiting_writers && sd->num_spliced)
  476. return 0;
  477. if (sd->flags & SPLICE_F_NONBLOCK)
  478. return -EAGAIN;
  479. if (signal_pending(current))
  480. return -ERESTARTSYS;
  481. if (sd->need_wakeup) {
  482. wakeup_pipe_writers(pipe);
  483. sd->need_wakeup = false;
  484. }
  485. pipe_wait(pipe);
  486. }
  487. return 1;
  488. }
  489. /**
  490. * splice_from_pipe_begin - start splicing from pipe
  491. * @sd: information about the splice operation
  492. *
  493. * Description:
  494. * This function should be called before a loop containing
  495. * splice_from_pipe_next() and splice_from_pipe_feed() to
  496. * initialize the necessary fields of @sd.
  497. */
  498. static void splice_from_pipe_begin(struct splice_desc *sd)
  499. {
  500. sd->num_spliced = 0;
  501. sd->need_wakeup = false;
  502. }
  503. /**
  504. * splice_from_pipe_end - finish splicing from pipe
  505. * @pipe: pipe to splice from
  506. * @sd: information about the splice operation
  507. *
  508. * Description:
  509. * This function will wake up pipe writers if necessary. It should
  510. * be called after a loop containing splice_from_pipe_next() and
  511. * splice_from_pipe_feed().
  512. */
  513. static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
  514. {
  515. if (sd->need_wakeup)
  516. wakeup_pipe_writers(pipe);
  517. }
  518. /**
  519. * __splice_from_pipe - splice data from a pipe to given actor
  520. * @pipe: pipe to splice from
  521. * @sd: information to @actor
  522. * @actor: handler that splices the data
  523. *
  524. * Description:
  525. * This function does little more than loop over the pipe and call
  526. * @actor to do the actual moving of a single struct pipe_buffer to
  527. * the desired destination. See pipe_to_file, pipe_to_sendpage, or
  528. * pipe_to_user.
  529. *
  530. */
  531. ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
  532. splice_actor *actor)
  533. {
  534. int ret;
  535. splice_from_pipe_begin(sd);
  536. do {
  537. cond_resched();
  538. ret = splice_from_pipe_next(pipe, sd);
  539. if (ret > 0)
  540. ret = splice_from_pipe_feed(pipe, sd, actor);
  541. } while (ret > 0);
  542. splice_from_pipe_end(pipe, sd);
  543. return sd->num_spliced ? sd->num_spliced : ret;
  544. }
  545. EXPORT_SYMBOL(__splice_from_pipe);
  546. /**
  547. * splice_from_pipe - splice data from a pipe to a file
  548. * @pipe: pipe to splice from
  549. * @out: file to splice to
  550. * @ppos: position in @out
  551. * @len: how many bytes to splice
  552. * @flags: splice modifier flags
  553. * @actor: handler that splices the data
  554. *
  555. * Description:
  556. * See __splice_from_pipe. This function locks the pipe inode,
  557. * otherwise it's identical to __splice_from_pipe().
  558. *
  559. */
  560. ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
  561. loff_t *ppos, size_t len, unsigned int flags,
  562. splice_actor *actor)
  563. {
  564. ssize_t ret;
  565. struct splice_desc sd = {
  566. .total_len = len,
  567. .flags = flags,
  568. .pos = *ppos,
  569. .u.file = out,
  570. };
  571. pipe_lock(pipe);
  572. ret = __splice_from_pipe(pipe, &sd, actor);
  573. pipe_unlock(pipe);
  574. return ret;
  575. }
  576. /**
  577. * iter_file_splice_write - splice data from a pipe to a file
  578. * @pipe: pipe info
  579. * @out: file to write to
  580. * @ppos: position in @out
  581. * @len: number of bytes to splice
  582. * @flags: splice modifier flags
  583. *
  584. * Description:
  585. * Will either move or copy pages (determined by @flags options) from
  586. * the given pipe inode to the given file.
  587. * This one is ->write_iter-based.
  588. *
  589. */
  590. ssize_t
  591. iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  592. loff_t *ppos, size_t len, unsigned int flags)
  593. {
  594. struct splice_desc sd = {
  595. .total_len = len,
  596. .flags = flags,
  597. .pos = *ppos,
  598. .u.file = out,
  599. };
  600. int nbufs = pipe->buffers;
  601. struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
  602. GFP_KERNEL);
  603. ssize_t ret;
  604. if (unlikely(!array))
  605. return -ENOMEM;
  606. pipe_lock(pipe);
  607. splice_from_pipe_begin(&sd);
  608. while (sd.total_len) {
  609. struct iov_iter from;
  610. size_t left;
  611. int n, idx;
  612. ret = splice_from_pipe_next(pipe, &sd);
  613. if (ret <= 0)
  614. break;
  615. if (unlikely(nbufs < pipe->buffers)) {
  616. kfree(array);
  617. nbufs = pipe->buffers;
  618. array = kcalloc(nbufs, sizeof(struct bio_vec),
  619. GFP_KERNEL);
  620. if (!array) {
  621. ret = -ENOMEM;
  622. break;
  623. }
  624. }
  625. /* build the vector */
  626. left = sd.total_len;
  627. for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) {
  628. struct pipe_buffer *buf = pipe->bufs + idx;
  629. size_t this_len = buf->len;
  630. if (this_len > left)
  631. this_len = left;
  632. if (idx == pipe->buffers - 1)
  633. idx = -1;
  634. ret = pipe_buf_confirm(pipe, buf);
  635. if (unlikely(ret)) {
  636. if (ret == -ENODATA)
  637. ret = 0;
  638. goto done;
  639. }
  640. array[n].bv_page = buf->page;
  641. array[n].bv_len = this_len;
  642. array[n].bv_offset = buf->offset;
  643. left -= this_len;
  644. }
  645. iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n,
  646. sd.total_len - left);
  647. ret = vfs_iter_write(out, &from, &sd.pos, 0);
  648. if (ret <= 0)
  649. break;
  650. sd.num_spliced += ret;
  651. sd.total_len -= ret;
  652. *ppos = sd.pos;
  653. /* dismiss the fully eaten buffers, adjust the partial one */
  654. while (ret) {
  655. struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  656. if (ret >= buf->len) {
  657. ret -= buf->len;
  658. buf->len = 0;
  659. pipe_buf_release(pipe, buf);
  660. pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
  661. pipe->nrbufs--;
  662. if (pipe->files)
  663. sd.need_wakeup = true;
  664. } else {
  665. buf->offset += ret;
  666. buf->len -= ret;
  667. ret = 0;
  668. }
  669. }
  670. }
  671. done:
  672. kfree(array);
  673. splice_from_pipe_end(pipe, &sd);
  674. pipe_unlock(pipe);
  675. if (sd.num_spliced)
  676. ret = sd.num_spliced;
  677. return ret;
  678. }
  679. EXPORT_SYMBOL(iter_file_splice_write);
  680. static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  681. struct splice_desc *sd)
  682. {
  683. int ret;
  684. void *data;
  685. loff_t tmp = sd->pos;
  686. data = kmap(buf->page);
  687. ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
  688. kunmap(buf->page);
  689. return ret;
  690. }
  691. static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
  692. struct file *out, loff_t *ppos,
  693. size_t len, unsigned int flags)
  694. {
  695. ssize_t ret;
  696. ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
  697. if (ret > 0)
  698. *ppos += ret;
  699. return ret;
  700. }
  701. /**
  702. * generic_splice_sendpage - splice data from a pipe to a socket
  703. * @pipe: pipe to splice from
  704. * @out: socket to write to
  705. * @ppos: position in @out
  706. * @len: number of bytes to splice
  707. * @flags: splice modifier flags
  708. *
  709. * Description:
  710. * Will send @len bytes from the pipe to a network socket. No data copying
  711. * is involved.
  712. *
  713. */
  714. ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
  715. loff_t *ppos, size_t len, unsigned int flags)
  716. {
  717. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
  718. }
  719. EXPORT_SYMBOL(generic_splice_sendpage);
  720. /*
  721. * Attempt to initiate a splice from pipe to file.
  722. */
  723. long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  724. loff_t *ppos, size_t len, unsigned int flags)
  725. {
  726. ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
  727. loff_t *, size_t, unsigned int);
  728. if (out->f_op->splice_write)
  729. splice_write = out->f_op->splice_write;
  730. else
  731. splice_write = default_file_splice_write;
  732. return splice_write(pipe, out, ppos, len, flags);
  733. }
  734. EXPORT_SYMBOL_GPL(do_splice_from);
  735. /*
  736. * Attempt to initiate a splice from a file to a pipe.
  737. */
  738. long do_splice_to(struct file *in, loff_t *ppos,
  739. struct pipe_inode_info *pipe, size_t len,
  740. unsigned int flags)
  741. {
  742. ssize_t (*splice_read)(struct file *, loff_t *,
  743. struct pipe_inode_info *, size_t, unsigned int);
  744. int ret;
  745. if (unlikely(!(in->f_mode & FMODE_READ)))
  746. return -EBADF;
  747. ret = rw_verify_area(READ, in, ppos, len);
  748. if (unlikely(ret < 0))
  749. return ret;
  750. if (unlikely(len > MAX_RW_COUNT))
  751. len = MAX_RW_COUNT;
  752. if (in->f_op->splice_read)
  753. splice_read = in->f_op->splice_read;
  754. else
  755. splice_read = default_file_splice_read;
  756. return splice_read(in, ppos, pipe, len, flags);
  757. }
  758. EXPORT_SYMBOL_GPL(do_splice_to);
  759. /**
  760. * splice_direct_to_actor - splices data directly between two non-pipes
  761. * @in: file to splice from
  762. * @sd: actor information on where to splice to
  763. * @actor: handles the data splicing
  764. *
  765. * Description:
  766. * This is a special case helper to splice directly between two
  767. * points, without requiring an explicit pipe. Internally an allocated
  768. * pipe is cached in the process, and reused during the lifetime of
  769. * that process.
  770. *
  771. */
  772. ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
  773. splice_direct_actor *actor)
  774. {
  775. struct pipe_inode_info *pipe;
  776. long ret, bytes;
  777. umode_t i_mode;
  778. size_t len;
  779. int i, flags, more;
  780. /*
  781. * We require the input being a regular file, as we don't want to
  782. * randomly drop data for eg socket -> socket splicing. Use the
  783. * piped splicing for that!
  784. */
  785. i_mode = file_inode(in)->i_mode;
  786. if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  787. return -EINVAL;
  788. /*
  789. * neither in nor out is a pipe, setup an internal pipe attached to
  790. * 'out' and transfer the wanted data from 'in' to 'out' through that
  791. */
  792. pipe = current->splice_pipe;
  793. if (unlikely(!pipe)) {
  794. pipe = alloc_pipe_info();
  795. if (!pipe)
  796. return -ENOMEM;
  797. /*
  798. * We don't have an immediate reader, but we'll read the stuff
  799. * out of the pipe right after the splice_to_pipe(). So set
  800. * PIPE_READERS appropriately.
  801. */
  802. pipe->readers = 1;
  803. current->splice_pipe = pipe;
  804. }
  805. /*
  806. * Do the splice.
  807. */
  808. ret = 0;
  809. bytes = 0;
  810. len = sd->total_len;
  811. flags = sd->flags;
  812. /*
  813. * Don't block on output, we have to drain the direct pipe.
  814. */
  815. sd->flags &= ~SPLICE_F_NONBLOCK;
  816. more = sd->flags & SPLICE_F_MORE;
  817. WARN_ON_ONCE(pipe->nrbufs != 0);
  818. while (len) {
  819. unsigned int pipe_pages;
  820. size_t read_len;
  821. loff_t pos = sd->pos, prev_pos = pos;
  822. /* Don't try to read more the pipe has space for. */
  823. pipe_pages = pipe->buffers - pipe->nrbufs;
  824. read_len = min(len, (size_t)pipe_pages << PAGE_SHIFT);
  825. ret = do_splice_to(in, &pos, pipe, read_len, flags);
  826. if (unlikely(ret <= 0))
  827. goto out_release;
  828. read_len = ret;
  829. sd->total_len = read_len;
  830. /*
  831. * If more data is pending, set SPLICE_F_MORE
  832. * If this is the last data and SPLICE_F_MORE was not set
  833. * initially, clears it.
  834. */
  835. if (read_len < len)
  836. sd->flags |= SPLICE_F_MORE;
  837. else if (!more)
  838. sd->flags &= ~SPLICE_F_MORE;
  839. /*
  840. * NOTE: nonblocking mode only applies to the input. We
  841. * must not do the output in nonblocking mode as then we
  842. * could get stuck data in the internal pipe:
  843. */
  844. ret = actor(pipe, sd);
  845. if (unlikely(ret <= 0)) {
  846. sd->pos = prev_pos;
  847. goto out_release;
  848. }
  849. bytes += ret;
  850. len -= ret;
  851. sd->pos = pos;
  852. if (ret < read_len) {
  853. sd->pos = prev_pos + ret;
  854. goto out_release;
  855. }
  856. }
  857. done:
  858. pipe->nrbufs = pipe->curbuf = 0;
  859. file_accessed(in);
  860. return bytes;
  861. out_release:
  862. /*
  863. * If we did an incomplete transfer we must release
  864. * the pipe buffers in question:
  865. */
  866. for (i = 0; i < pipe->buffers; i++) {
  867. struct pipe_buffer *buf = pipe->bufs + i;
  868. if (buf->ops)
  869. pipe_buf_release(pipe, buf);
  870. }
  871. if (!bytes)
  872. bytes = ret;
  873. goto done;
  874. }
  875. EXPORT_SYMBOL(splice_direct_to_actor);
  876. static int direct_splice_actor(struct pipe_inode_info *pipe,
  877. struct splice_desc *sd)
  878. {
  879. struct file *file = sd->u.file;
  880. return do_splice_from(pipe, file, sd->opos, sd->total_len,
  881. sd->flags);
  882. }
  883. /**
  884. * do_splice_direct - splices data directly between two files
  885. * @in: file to splice from
  886. * @ppos: input file offset
  887. * @out: file to splice to
  888. * @opos: output file offset
  889. * @len: number of bytes to splice
  890. * @flags: splice modifier flags
  891. *
  892. * Description:
  893. * For use by do_sendfile(). splice can easily emulate sendfile, but
  894. * doing it in the application would incur an extra system call
  895. * (splice in + splice out, as compared to just sendfile()). So this helper
  896. * can splice directly through a process-private pipe.
  897. *
  898. */
  899. long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
  900. loff_t *opos, size_t len, unsigned int flags)
  901. {
  902. struct splice_desc sd = {
  903. .len = len,
  904. .total_len = len,
  905. .flags = flags,
  906. .pos = *ppos,
  907. .u.file = out,
  908. .opos = opos,
  909. };
  910. long ret;
  911. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  912. return -EBADF;
  913. if (unlikely(out->f_flags & O_APPEND))
  914. return -EINVAL;
  915. ret = rw_verify_area(WRITE, out, opos, len);
  916. if (unlikely(ret < 0))
  917. return ret;
  918. ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
  919. if (ret > 0)
  920. *ppos = sd.pos;
  921. return ret;
  922. }
  923. EXPORT_SYMBOL(do_splice_direct);
  924. static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
  925. {
  926. for (;;) {
  927. if (unlikely(!pipe->readers)) {
  928. send_sig(SIGPIPE, current, 0);
  929. return -EPIPE;
  930. }
  931. if (pipe->nrbufs != pipe->buffers)
  932. return 0;
  933. if (flags & SPLICE_F_NONBLOCK)
  934. return -EAGAIN;
  935. if (signal_pending(current))
  936. return -ERESTARTSYS;
  937. pipe->waiting_writers++;
  938. pipe_wait(pipe);
  939. pipe->waiting_writers--;
  940. }
  941. }
  942. static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  943. struct pipe_inode_info *opipe,
  944. size_t len, unsigned int flags);
  945. /*
  946. * Determine where to splice to/from.
  947. */
  948. static long do_splice(struct file *in, loff_t __user *off_in,
  949. struct file *out, loff_t __user *off_out,
  950. size_t len, unsigned int flags)
  951. {
  952. struct pipe_inode_info *ipipe;
  953. struct pipe_inode_info *opipe;
  954. loff_t offset;
  955. long ret;
  956. ipipe = get_pipe_info(in);
  957. opipe = get_pipe_info(out);
  958. if (ipipe && opipe) {
  959. if (off_in || off_out)
  960. return -ESPIPE;
  961. if (!(in->f_mode & FMODE_READ))
  962. return -EBADF;
  963. if (!(out->f_mode & FMODE_WRITE))
  964. return -EBADF;
  965. /* Splicing to self would be fun, but... */
  966. if (ipipe == opipe)
  967. return -EINVAL;
  968. return splice_pipe_to_pipe(ipipe, opipe, len, flags);
  969. }
  970. if (ipipe) {
  971. if (off_in)
  972. return -ESPIPE;
  973. if (off_out) {
  974. if (!(out->f_mode & FMODE_PWRITE))
  975. return -EINVAL;
  976. if (copy_from_user(&offset, off_out, sizeof(loff_t)))
  977. return -EFAULT;
  978. } else {
  979. offset = out->f_pos;
  980. }
  981. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  982. return -EBADF;
  983. if (unlikely(out->f_flags & O_APPEND))
  984. return -EINVAL;
  985. ret = rw_verify_area(WRITE, out, &offset, len);
  986. if (unlikely(ret < 0))
  987. return ret;
  988. file_start_write(out);
  989. ret = do_splice_from(ipipe, out, &offset, len, flags);
  990. file_end_write(out);
  991. if (!off_out)
  992. out->f_pos = offset;
  993. else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
  994. ret = -EFAULT;
  995. return ret;
  996. }
  997. if (opipe) {
  998. if (off_out)
  999. return -ESPIPE;
  1000. if (off_in) {
  1001. if (!(in->f_mode & FMODE_PREAD))
  1002. return -EINVAL;
  1003. if (copy_from_user(&offset, off_in, sizeof(loff_t)))
  1004. return -EFAULT;
  1005. } else {
  1006. offset = in->f_pos;
  1007. }
  1008. pipe_lock(opipe);
  1009. ret = wait_for_space(opipe, flags);
  1010. if (!ret) {
  1011. unsigned int pipe_pages;
  1012. /* Don't try to read more the pipe has space for. */
  1013. pipe_pages = opipe->buffers - opipe->nrbufs;
  1014. len = min(len, (size_t)pipe_pages << PAGE_SHIFT);
  1015. ret = do_splice_to(in, &offset, opipe, len, flags);
  1016. }
  1017. pipe_unlock(opipe);
  1018. if (ret > 0)
  1019. wakeup_pipe_readers(opipe);
  1020. if (!off_in)
  1021. in->f_pos = offset;
  1022. else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
  1023. ret = -EFAULT;
  1024. return ret;
  1025. }
  1026. return -EINVAL;
  1027. }
  1028. static int iter_to_pipe(struct iov_iter *from,
  1029. struct pipe_inode_info *pipe,
  1030. unsigned flags)
  1031. {
  1032. struct pipe_buffer buf = {
  1033. .ops = &user_page_pipe_buf_ops,
  1034. .flags = flags
  1035. };
  1036. size_t total = 0;
  1037. int ret = 0;
  1038. bool failed = false;
  1039. while (iov_iter_count(from) && !failed) {
  1040. struct page *pages[16];
  1041. ssize_t copied;
  1042. size_t start;
  1043. int n;
  1044. copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
  1045. if (copied <= 0) {
  1046. ret = copied;
  1047. break;
  1048. }
  1049. for (n = 0; copied; n++, start = 0) {
  1050. int size = min_t(int, copied, PAGE_SIZE - start);
  1051. if (!failed) {
  1052. buf.page = pages[n];
  1053. buf.offset = start;
  1054. buf.len = size;
  1055. ret = add_to_pipe(pipe, &buf);
  1056. if (unlikely(ret < 0)) {
  1057. failed = true;
  1058. } else {
  1059. iov_iter_advance(from, ret);
  1060. total += ret;
  1061. }
  1062. } else {
  1063. put_page(pages[n]);
  1064. }
  1065. copied -= size;
  1066. }
  1067. }
  1068. return total ? total : ret;
  1069. }
  1070. static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  1071. struct splice_desc *sd)
  1072. {
  1073. int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
  1074. return n == sd->len ? n : -EFAULT;
  1075. }
  1076. /*
  1077. * For lack of a better implementation, implement vmsplice() to userspace
  1078. * as a simple copy of the pipes pages to the user iov.
  1079. */
  1080. static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
  1081. unsigned int flags)
  1082. {
  1083. struct pipe_inode_info *pipe = get_pipe_info(file);
  1084. struct splice_desc sd = {
  1085. .total_len = iov_iter_count(iter),
  1086. .flags = flags,
  1087. .u.data = iter
  1088. };
  1089. long ret = 0;
  1090. if (!pipe)
  1091. return -EBADF;
  1092. if (sd.total_len) {
  1093. pipe_lock(pipe);
  1094. ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
  1095. pipe_unlock(pipe);
  1096. }
  1097. return ret;
  1098. }
  1099. /*
  1100. * vmsplice splices a user address range into a pipe. It can be thought of
  1101. * as splice-from-memory, where the regular splice is splice-from-file (or
  1102. * to file). In both cases the output is a pipe, naturally.
  1103. */
  1104. static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
  1105. unsigned int flags)
  1106. {
  1107. struct pipe_inode_info *pipe;
  1108. long ret = 0;
  1109. unsigned buf_flag = 0;
  1110. if (flags & SPLICE_F_GIFT)
  1111. buf_flag = PIPE_BUF_FLAG_GIFT;
  1112. pipe = get_pipe_info(file);
  1113. if (!pipe)
  1114. return -EBADF;
  1115. pipe_lock(pipe);
  1116. ret = wait_for_space(pipe, flags);
  1117. if (!ret)
  1118. ret = iter_to_pipe(iter, pipe, buf_flag);
  1119. pipe_unlock(pipe);
  1120. if (ret > 0)
  1121. wakeup_pipe_readers(pipe);
  1122. return ret;
  1123. }
  1124. static int vmsplice_type(struct fd f, int *type)
  1125. {
  1126. if (!f.file)
  1127. return -EBADF;
  1128. if (f.file->f_mode & FMODE_WRITE) {
  1129. *type = WRITE;
  1130. } else if (f.file->f_mode & FMODE_READ) {
  1131. *type = READ;
  1132. } else {
  1133. fdput(f);
  1134. return -EBADF;
  1135. }
  1136. return 0;
  1137. }
  1138. /*
  1139. * Note that vmsplice only really supports true splicing _from_ user memory
  1140. * to a pipe, not the other way around. Splicing from user memory is a simple
  1141. * operation that can be supported without any funky alignment restrictions
  1142. * or nasty vm tricks. We simply map in the user memory and fill them into
  1143. * a pipe. The reverse isn't quite as easy, though. There are two possible
  1144. * solutions for that:
  1145. *
  1146. * - memcpy() the data internally, at which point we might as well just
  1147. * do a regular read() on the buffer anyway.
  1148. * - Lots of nasty vm tricks, that are neither fast nor flexible (it
  1149. * has restriction limitations on both ends of the pipe).
  1150. *
  1151. * Currently we punt and implement it as a normal copy, see pipe_to_user().
  1152. *
  1153. */
  1154. static long do_vmsplice(struct file *f, struct iov_iter *iter, unsigned int flags)
  1155. {
  1156. if (unlikely(flags & ~SPLICE_F_ALL))
  1157. return -EINVAL;
  1158. if (!iov_iter_count(iter))
  1159. return 0;
  1160. if (iov_iter_rw(iter) == WRITE)
  1161. return vmsplice_to_pipe(f, iter, flags);
  1162. else
  1163. return vmsplice_to_user(f, iter, flags);
  1164. }
  1165. SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
  1166. unsigned long, nr_segs, unsigned int, flags)
  1167. {
  1168. struct iovec iovstack[UIO_FASTIOV];
  1169. struct iovec *iov = iovstack;
  1170. struct iov_iter iter;
  1171. long error;
  1172. struct fd f;
  1173. int type;
  1174. f = fdget(fd);
  1175. error = vmsplice_type(f, &type);
  1176. if (error)
  1177. return error;
  1178. error = import_iovec(type, uiov, nr_segs,
  1179. ARRAY_SIZE(iovstack), &iov, &iter);
  1180. if (!error) {
  1181. error = do_vmsplice(f.file, &iter, flags);
  1182. kfree(iov);
  1183. }
  1184. fdput(f);
  1185. return error;
  1186. }
  1187. #ifdef CONFIG_COMPAT
  1188. COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
  1189. unsigned int, nr_segs, unsigned int, flags)
  1190. {
  1191. struct iovec iovstack[UIO_FASTIOV];
  1192. struct iovec *iov = iovstack;
  1193. struct iov_iter iter;
  1194. long error;
  1195. struct fd f;
  1196. int type;
  1197. f = fdget(fd);
  1198. error = vmsplice_type(f, &type);
  1199. if (error)
  1200. return error;
  1201. error = compat_import_iovec(type, iov32, nr_segs,
  1202. ARRAY_SIZE(iovstack), &iov, &iter);
  1203. if (!error) {
  1204. error = do_vmsplice(f.file, &iter, flags);
  1205. kfree(iov);
  1206. }
  1207. fdput(f);
  1208. return error;
  1209. }
  1210. #endif
  1211. SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
  1212. int, fd_out, loff_t __user *, off_out,
  1213. size_t, len, unsigned int, flags)
  1214. {
  1215. struct fd in, out;
  1216. long error;
  1217. if (unlikely(!len))
  1218. return 0;
  1219. if (unlikely(flags & ~SPLICE_F_ALL))
  1220. return -EINVAL;
  1221. error = -EBADF;
  1222. in = fdget(fd_in);
  1223. if (in.file) {
  1224. if (in.file->f_mode & FMODE_READ) {
  1225. out = fdget(fd_out);
  1226. if (out.file) {
  1227. if (out.file->f_mode & FMODE_WRITE)
  1228. error = do_splice(in.file, off_in,
  1229. out.file, off_out,
  1230. len, flags);
  1231. fdput(out);
  1232. }
  1233. }
  1234. fdput(in);
  1235. }
  1236. return error;
  1237. }
  1238. /*
  1239. * Make sure there's data to read. Wait for input if we can, otherwise
  1240. * return an appropriate error.
  1241. */
  1242. static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1243. {
  1244. int ret;
  1245. /*
  1246. * Check ->nrbufs without the inode lock first. This function
  1247. * is speculative anyways, so missing one is ok.
  1248. */
  1249. if (pipe->nrbufs)
  1250. return 0;
  1251. ret = 0;
  1252. pipe_lock(pipe);
  1253. while (!pipe->nrbufs) {
  1254. if (signal_pending(current)) {
  1255. ret = -ERESTARTSYS;
  1256. break;
  1257. }
  1258. if (!pipe->writers)
  1259. break;
  1260. if (!pipe->waiting_writers) {
  1261. if (flags & SPLICE_F_NONBLOCK) {
  1262. ret = -EAGAIN;
  1263. break;
  1264. }
  1265. }
  1266. pipe_wait(pipe);
  1267. }
  1268. pipe_unlock(pipe);
  1269. return ret;
  1270. }
  1271. /*
  1272. * Make sure there's writeable room. Wait for room if we can, otherwise
  1273. * return an appropriate error.
  1274. */
  1275. static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1276. {
  1277. int ret;
  1278. /*
  1279. * Check ->nrbufs without the inode lock first. This function
  1280. * is speculative anyways, so missing one is ok.
  1281. */
  1282. if (pipe->nrbufs < pipe->buffers)
  1283. return 0;
  1284. ret = 0;
  1285. pipe_lock(pipe);
  1286. while (pipe->nrbufs >= pipe->buffers) {
  1287. if (!pipe->readers) {
  1288. send_sig(SIGPIPE, current, 0);
  1289. ret = -EPIPE;
  1290. break;
  1291. }
  1292. if (flags & SPLICE_F_NONBLOCK) {
  1293. ret = -EAGAIN;
  1294. break;
  1295. }
  1296. if (signal_pending(current)) {
  1297. ret = -ERESTARTSYS;
  1298. break;
  1299. }
  1300. pipe->waiting_writers++;
  1301. pipe_wait(pipe);
  1302. pipe->waiting_writers--;
  1303. }
  1304. pipe_unlock(pipe);
  1305. return ret;
  1306. }
  1307. /*
  1308. * Splice contents of ipipe to opipe.
  1309. */
  1310. static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  1311. struct pipe_inode_info *opipe,
  1312. size_t len, unsigned int flags)
  1313. {
  1314. struct pipe_buffer *ibuf, *obuf;
  1315. int ret = 0, nbuf;
  1316. bool input_wakeup = false;
  1317. retry:
  1318. ret = ipipe_prep(ipipe, flags);
  1319. if (ret)
  1320. return ret;
  1321. ret = opipe_prep(opipe, flags);
  1322. if (ret)
  1323. return ret;
  1324. /*
  1325. * Potential ABBA deadlock, work around it by ordering lock
  1326. * grabbing by pipe info address. Otherwise two different processes
  1327. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1328. */
  1329. pipe_double_lock(ipipe, opipe);
  1330. do {
  1331. if (!opipe->readers) {
  1332. send_sig(SIGPIPE, current, 0);
  1333. if (!ret)
  1334. ret = -EPIPE;
  1335. break;
  1336. }
  1337. if (!ipipe->nrbufs && !ipipe->writers)
  1338. break;
  1339. /*
  1340. * Cannot make any progress, because either the input
  1341. * pipe is empty or the output pipe is full.
  1342. */
  1343. if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
  1344. /* Already processed some buffers, break */
  1345. if (ret)
  1346. break;
  1347. if (flags & SPLICE_F_NONBLOCK) {
  1348. ret = -EAGAIN;
  1349. break;
  1350. }
  1351. /*
  1352. * We raced with another reader/writer and haven't
  1353. * managed to process any buffers. A zero return
  1354. * value means EOF, so retry instead.
  1355. */
  1356. pipe_unlock(ipipe);
  1357. pipe_unlock(opipe);
  1358. goto retry;
  1359. }
  1360. ibuf = ipipe->bufs + ipipe->curbuf;
  1361. nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
  1362. obuf = opipe->bufs + nbuf;
  1363. if (len >= ibuf->len) {
  1364. /*
  1365. * Simply move the whole buffer from ipipe to opipe
  1366. */
  1367. *obuf = *ibuf;
  1368. ibuf->ops = NULL;
  1369. opipe->nrbufs++;
  1370. ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
  1371. ipipe->nrbufs--;
  1372. input_wakeup = true;
  1373. } else {
  1374. /*
  1375. * Get a reference to this pipe buffer,
  1376. * so we can copy the contents over.
  1377. */
  1378. if (!pipe_buf_get(ipipe, ibuf)) {
  1379. if (ret == 0)
  1380. ret = -EFAULT;
  1381. break;
  1382. }
  1383. *obuf = *ibuf;
  1384. /*
  1385. * Don't inherit the gift flag, we need to
  1386. * prevent multiple steals of this page.
  1387. */
  1388. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1389. pipe_buf_mark_unmergeable(obuf);
  1390. obuf->len = len;
  1391. opipe->nrbufs++;
  1392. ibuf->offset += obuf->len;
  1393. ibuf->len -= obuf->len;
  1394. }
  1395. ret += obuf->len;
  1396. len -= obuf->len;
  1397. } while (len);
  1398. pipe_unlock(ipipe);
  1399. pipe_unlock(opipe);
  1400. /*
  1401. * If we put data in the output pipe, wakeup any potential readers.
  1402. */
  1403. if (ret > 0)
  1404. wakeup_pipe_readers(opipe);
  1405. if (input_wakeup)
  1406. wakeup_pipe_writers(ipipe);
  1407. return ret;
  1408. }
  1409. /*
  1410. * Link contents of ipipe to opipe.
  1411. */
  1412. static int link_pipe(struct pipe_inode_info *ipipe,
  1413. struct pipe_inode_info *opipe,
  1414. size_t len, unsigned int flags)
  1415. {
  1416. struct pipe_buffer *ibuf, *obuf;
  1417. int ret = 0, i = 0, nbuf;
  1418. /*
  1419. * Potential ABBA deadlock, work around it by ordering lock
  1420. * grabbing by pipe info address. Otherwise two different processes
  1421. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1422. */
  1423. pipe_double_lock(ipipe, opipe);
  1424. do {
  1425. if (!opipe->readers) {
  1426. send_sig(SIGPIPE, current, 0);
  1427. if (!ret)
  1428. ret = -EPIPE;
  1429. break;
  1430. }
  1431. /*
  1432. * If we have iterated all input buffers or ran out of
  1433. * output room, break.
  1434. */
  1435. if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
  1436. break;
  1437. ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
  1438. nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
  1439. /*
  1440. * Get a reference to this pipe buffer,
  1441. * so we can copy the contents over.
  1442. */
  1443. if (!pipe_buf_get(ipipe, ibuf)) {
  1444. if (ret == 0)
  1445. ret = -EFAULT;
  1446. break;
  1447. }
  1448. obuf = opipe->bufs + nbuf;
  1449. *obuf = *ibuf;
  1450. /*
  1451. * Don't inherit the gift flag, we need to
  1452. * prevent multiple steals of this page.
  1453. */
  1454. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1455. pipe_buf_mark_unmergeable(obuf);
  1456. if (obuf->len > len)
  1457. obuf->len = len;
  1458. opipe->nrbufs++;
  1459. ret += obuf->len;
  1460. len -= obuf->len;
  1461. i++;
  1462. } while (len);
  1463. /*
  1464. * return EAGAIN if we have the potential of some data in the
  1465. * future, otherwise just return 0
  1466. */
  1467. if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
  1468. ret = -EAGAIN;
  1469. pipe_unlock(ipipe);
  1470. pipe_unlock(opipe);
  1471. /*
  1472. * If we put data in the output pipe, wakeup any potential readers.
  1473. */
  1474. if (ret > 0)
  1475. wakeup_pipe_readers(opipe);
  1476. return ret;
  1477. }
  1478. /*
  1479. * This is a tee(1) implementation that works on pipes. It doesn't copy
  1480. * any data, it simply references the 'in' pages on the 'out' pipe.
  1481. * The 'flags' used are the SPLICE_F_* variants, currently the only
  1482. * applicable one is SPLICE_F_NONBLOCK.
  1483. */
  1484. static long do_tee(struct file *in, struct file *out, size_t len,
  1485. unsigned int flags)
  1486. {
  1487. struct pipe_inode_info *ipipe = get_pipe_info(in);
  1488. struct pipe_inode_info *opipe = get_pipe_info(out);
  1489. int ret = -EINVAL;
  1490. /*
  1491. * Duplicate the contents of ipipe to opipe without actually
  1492. * copying the data.
  1493. */
  1494. if (ipipe && opipe && ipipe != opipe) {
  1495. /*
  1496. * Keep going, unless we encounter an error. The ipipe/opipe
  1497. * ordering doesn't really matter.
  1498. */
  1499. ret = ipipe_prep(ipipe, flags);
  1500. if (!ret) {
  1501. ret = opipe_prep(opipe, flags);
  1502. if (!ret)
  1503. ret = link_pipe(ipipe, opipe, len, flags);
  1504. }
  1505. }
  1506. return ret;
  1507. }
  1508. SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
  1509. {
  1510. struct fd in;
  1511. int error;
  1512. if (unlikely(flags & ~SPLICE_F_ALL))
  1513. return -EINVAL;
  1514. if (unlikely(!len))
  1515. return 0;
  1516. error = -EBADF;
  1517. in = fdget(fdin);
  1518. if (in.file) {
  1519. if (in.file->f_mode & FMODE_READ) {
  1520. struct fd out = fdget(fdout);
  1521. if (out.file) {
  1522. if (out.file->f_mode & FMODE_WRITE)
  1523. error = do_tee(in.file, out.file,
  1524. len, flags);
  1525. fdput(out);
  1526. }
  1527. }
  1528. fdput(in);
  1529. }
  1530. return error;
  1531. }