splice.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783
  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. static 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. /*
  735. * Attempt to initiate a splice from a file to a pipe.
  736. */
  737. static long do_splice_to(struct file *in, loff_t *ppos,
  738. struct pipe_inode_info *pipe, size_t len,
  739. unsigned int flags)
  740. {
  741. ssize_t (*splice_read)(struct file *, loff_t *,
  742. struct pipe_inode_info *, size_t, unsigned int);
  743. int ret;
  744. if (unlikely(!(in->f_mode & FMODE_READ)))
  745. return -EBADF;
  746. ret = rw_verify_area(READ, in, ppos, len);
  747. if (unlikely(ret < 0))
  748. return ret;
  749. if (unlikely(len > MAX_RW_COUNT))
  750. len = MAX_RW_COUNT;
  751. if (in->f_op->splice_read)
  752. splice_read = in->f_op->splice_read;
  753. else
  754. splice_read = default_file_splice_read;
  755. return splice_read(in, ppos, pipe, len, flags);
  756. }
  757. /**
  758. * splice_direct_to_actor - splices data directly between two non-pipes
  759. * @in: file to splice from
  760. * @sd: actor information on where to splice to
  761. * @actor: handles the data splicing
  762. *
  763. * Description:
  764. * This is a special case helper to splice directly between two
  765. * points, without requiring an explicit pipe. Internally an allocated
  766. * pipe is cached in the process, and reused during the lifetime of
  767. * that process.
  768. *
  769. */
  770. ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
  771. splice_direct_actor *actor)
  772. {
  773. struct pipe_inode_info *pipe;
  774. long ret, bytes;
  775. umode_t i_mode;
  776. size_t len;
  777. int i, flags, more;
  778. /*
  779. * We require the input being a regular file, as we don't want to
  780. * randomly drop data for eg socket -> socket splicing. Use the
  781. * piped splicing for that!
  782. */
  783. i_mode = file_inode(in)->i_mode;
  784. if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  785. return -EINVAL;
  786. /*
  787. * neither in nor out is a pipe, setup an internal pipe attached to
  788. * 'out' and transfer the wanted data from 'in' to 'out' through that
  789. */
  790. pipe = current->splice_pipe;
  791. if (unlikely(!pipe)) {
  792. pipe = alloc_pipe_info();
  793. if (!pipe)
  794. return -ENOMEM;
  795. /*
  796. * We don't have an immediate reader, but we'll read the stuff
  797. * out of the pipe right after the splice_to_pipe(). So set
  798. * PIPE_READERS appropriately.
  799. */
  800. pipe->readers = 1;
  801. current->splice_pipe = pipe;
  802. }
  803. /*
  804. * Do the splice.
  805. */
  806. ret = 0;
  807. bytes = 0;
  808. len = sd->total_len;
  809. flags = sd->flags;
  810. /*
  811. * Don't block on output, we have to drain the direct pipe.
  812. */
  813. sd->flags &= ~SPLICE_F_NONBLOCK;
  814. more = sd->flags & SPLICE_F_MORE;
  815. WARN_ON_ONCE(pipe->nrbufs != 0);
  816. while (len) {
  817. unsigned int pipe_pages;
  818. size_t read_len;
  819. loff_t pos = sd->pos, prev_pos = pos;
  820. /* Don't try to read more the pipe has space for. */
  821. pipe_pages = pipe->buffers - pipe->nrbufs;
  822. read_len = min(len, (size_t)pipe_pages << PAGE_SHIFT);
  823. ret = do_splice_to(in, &pos, pipe, read_len, flags);
  824. if (unlikely(ret <= 0))
  825. goto out_release;
  826. read_len = ret;
  827. sd->total_len = read_len;
  828. /*
  829. * If more data is pending, set SPLICE_F_MORE
  830. * If this is the last data and SPLICE_F_MORE was not set
  831. * initially, clears it.
  832. */
  833. if (read_len < len)
  834. sd->flags |= SPLICE_F_MORE;
  835. else if (!more)
  836. sd->flags &= ~SPLICE_F_MORE;
  837. /*
  838. * NOTE: nonblocking mode only applies to the input. We
  839. * must not do the output in nonblocking mode as then we
  840. * could get stuck data in the internal pipe:
  841. */
  842. ret = actor(pipe, sd);
  843. if (unlikely(ret <= 0)) {
  844. sd->pos = prev_pos;
  845. goto out_release;
  846. }
  847. bytes += ret;
  848. len -= ret;
  849. sd->pos = pos;
  850. if (ret < read_len) {
  851. sd->pos = prev_pos + ret;
  852. goto out_release;
  853. }
  854. }
  855. done:
  856. pipe->nrbufs = pipe->curbuf = 0;
  857. file_accessed(in);
  858. return bytes;
  859. out_release:
  860. /*
  861. * If we did an incomplete transfer we must release
  862. * the pipe buffers in question:
  863. */
  864. for (i = 0; i < pipe->buffers; i++) {
  865. struct pipe_buffer *buf = pipe->bufs + i;
  866. if (buf->ops)
  867. pipe_buf_release(pipe, buf);
  868. }
  869. if (!bytes)
  870. bytes = ret;
  871. goto done;
  872. }
  873. EXPORT_SYMBOL(splice_direct_to_actor);
  874. static int direct_splice_actor(struct pipe_inode_info *pipe,
  875. struct splice_desc *sd)
  876. {
  877. struct file *file = sd->u.file;
  878. return do_splice_from(pipe, file, sd->opos, sd->total_len,
  879. sd->flags);
  880. }
  881. /**
  882. * do_splice_direct - splices data directly between two files
  883. * @in: file to splice from
  884. * @ppos: input file offset
  885. * @out: file to splice to
  886. * @opos: output file offset
  887. * @len: number of bytes to splice
  888. * @flags: splice modifier flags
  889. *
  890. * Description:
  891. * For use by do_sendfile(). splice can easily emulate sendfile, but
  892. * doing it in the application would incur an extra system call
  893. * (splice in + splice out, as compared to just sendfile()). So this helper
  894. * can splice directly through a process-private pipe.
  895. *
  896. */
  897. long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
  898. loff_t *opos, size_t len, unsigned int flags)
  899. {
  900. struct splice_desc sd = {
  901. .len = len,
  902. .total_len = len,
  903. .flags = flags,
  904. .pos = *ppos,
  905. .u.file = out,
  906. .opos = opos,
  907. };
  908. long ret;
  909. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  910. return -EBADF;
  911. if (unlikely(out->f_flags & O_APPEND))
  912. return -EINVAL;
  913. ret = rw_verify_area(WRITE, out, opos, len);
  914. if (unlikely(ret < 0))
  915. return ret;
  916. ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
  917. if (ret > 0)
  918. *ppos = sd.pos;
  919. return ret;
  920. }
  921. EXPORT_SYMBOL(do_splice_direct);
  922. static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
  923. {
  924. for (;;) {
  925. if (unlikely(!pipe->readers)) {
  926. send_sig(SIGPIPE, current, 0);
  927. return -EPIPE;
  928. }
  929. if (pipe->nrbufs != pipe->buffers)
  930. return 0;
  931. if (flags & SPLICE_F_NONBLOCK)
  932. return -EAGAIN;
  933. if (signal_pending(current))
  934. return -ERESTARTSYS;
  935. pipe->waiting_writers++;
  936. pipe_wait(pipe);
  937. pipe->waiting_writers--;
  938. }
  939. }
  940. static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  941. struct pipe_inode_info *opipe,
  942. size_t len, unsigned int flags);
  943. /*
  944. * Determine where to splice to/from.
  945. */
  946. static long do_splice(struct file *in, loff_t __user *off_in,
  947. struct file *out, loff_t __user *off_out,
  948. size_t len, unsigned int flags)
  949. {
  950. struct pipe_inode_info *ipipe;
  951. struct pipe_inode_info *opipe;
  952. loff_t offset;
  953. long ret;
  954. ipipe = get_pipe_info(in);
  955. opipe = get_pipe_info(out);
  956. if (ipipe && opipe) {
  957. if (off_in || off_out)
  958. return -ESPIPE;
  959. if (!(in->f_mode & FMODE_READ))
  960. return -EBADF;
  961. if (!(out->f_mode & FMODE_WRITE))
  962. return -EBADF;
  963. /* Splicing to self would be fun, but... */
  964. if (ipipe == opipe)
  965. return -EINVAL;
  966. return splice_pipe_to_pipe(ipipe, opipe, len, flags);
  967. }
  968. if (ipipe) {
  969. if (off_in)
  970. return -ESPIPE;
  971. if (off_out) {
  972. if (!(out->f_mode & FMODE_PWRITE))
  973. return -EINVAL;
  974. if (copy_from_user(&offset, off_out, sizeof(loff_t)))
  975. return -EFAULT;
  976. } else {
  977. offset = out->f_pos;
  978. }
  979. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  980. return -EBADF;
  981. if (unlikely(out->f_flags & O_APPEND))
  982. return -EINVAL;
  983. ret = rw_verify_area(WRITE, out, &offset, len);
  984. if (unlikely(ret < 0))
  985. return ret;
  986. file_start_write(out);
  987. ret = do_splice_from(ipipe, out, &offset, len, flags);
  988. file_end_write(out);
  989. if (!off_out)
  990. out->f_pos = offset;
  991. else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
  992. ret = -EFAULT;
  993. return ret;
  994. }
  995. if (opipe) {
  996. if (off_out)
  997. return -ESPIPE;
  998. if (off_in) {
  999. if (!(in->f_mode & FMODE_PREAD))
  1000. return -EINVAL;
  1001. if (copy_from_user(&offset, off_in, sizeof(loff_t)))
  1002. return -EFAULT;
  1003. } else {
  1004. offset = in->f_pos;
  1005. }
  1006. pipe_lock(opipe);
  1007. ret = wait_for_space(opipe, flags);
  1008. if (!ret) {
  1009. unsigned int pipe_pages;
  1010. /* Don't try to read more the pipe has space for. */
  1011. pipe_pages = opipe->buffers - opipe->nrbufs;
  1012. len = min(len, (size_t)pipe_pages << PAGE_SHIFT);
  1013. ret = do_splice_to(in, &offset, opipe, len, flags);
  1014. }
  1015. pipe_unlock(opipe);
  1016. if (ret > 0)
  1017. wakeup_pipe_readers(opipe);
  1018. if (!off_in)
  1019. in->f_pos = offset;
  1020. else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
  1021. ret = -EFAULT;
  1022. return ret;
  1023. }
  1024. return -EINVAL;
  1025. }
  1026. static int iter_to_pipe(struct iov_iter *from,
  1027. struct pipe_inode_info *pipe,
  1028. unsigned flags)
  1029. {
  1030. struct pipe_buffer buf = {
  1031. .ops = &user_page_pipe_buf_ops,
  1032. .flags = flags
  1033. };
  1034. size_t total = 0;
  1035. int ret = 0;
  1036. bool failed = false;
  1037. while (iov_iter_count(from) && !failed) {
  1038. struct page *pages[16];
  1039. ssize_t copied;
  1040. size_t start;
  1041. int n;
  1042. copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
  1043. if (copied <= 0) {
  1044. ret = copied;
  1045. break;
  1046. }
  1047. for (n = 0; copied; n++, start = 0) {
  1048. int size = min_t(int, copied, PAGE_SIZE - start);
  1049. if (!failed) {
  1050. buf.page = pages[n];
  1051. buf.offset = start;
  1052. buf.len = size;
  1053. ret = add_to_pipe(pipe, &buf);
  1054. if (unlikely(ret < 0)) {
  1055. failed = true;
  1056. } else {
  1057. iov_iter_advance(from, ret);
  1058. total += ret;
  1059. }
  1060. } else {
  1061. put_page(pages[n]);
  1062. }
  1063. copied -= size;
  1064. }
  1065. }
  1066. return total ? total : ret;
  1067. }
  1068. static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  1069. struct splice_desc *sd)
  1070. {
  1071. int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
  1072. return n == sd->len ? n : -EFAULT;
  1073. }
  1074. /*
  1075. * For lack of a better implementation, implement vmsplice() to userspace
  1076. * as a simple copy of the pipes pages to the user iov.
  1077. */
  1078. static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
  1079. unsigned int flags)
  1080. {
  1081. struct pipe_inode_info *pipe = get_pipe_info(file);
  1082. struct splice_desc sd = {
  1083. .total_len = iov_iter_count(iter),
  1084. .flags = flags,
  1085. .u.data = iter
  1086. };
  1087. long ret = 0;
  1088. if (!pipe)
  1089. return -EBADF;
  1090. if (sd.total_len) {
  1091. pipe_lock(pipe);
  1092. ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
  1093. pipe_unlock(pipe);
  1094. }
  1095. return ret;
  1096. }
  1097. /*
  1098. * vmsplice splices a user address range into a pipe. It can be thought of
  1099. * as splice-from-memory, where the regular splice is splice-from-file (or
  1100. * to file). In both cases the output is a pipe, naturally.
  1101. */
  1102. static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
  1103. unsigned int flags)
  1104. {
  1105. struct pipe_inode_info *pipe;
  1106. long ret = 0;
  1107. unsigned buf_flag = 0;
  1108. if (flags & SPLICE_F_GIFT)
  1109. buf_flag = PIPE_BUF_FLAG_GIFT;
  1110. pipe = get_pipe_info(file);
  1111. if (!pipe)
  1112. return -EBADF;
  1113. pipe_lock(pipe);
  1114. ret = wait_for_space(pipe, flags);
  1115. if (!ret)
  1116. ret = iter_to_pipe(iter, pipe, buf_flag);
  1117. pipe_unlock(pipe);
  1118. if (ret > 0)
  1119. wakeup_pipe_readers(pipe);
  1120. return ret;
  1121. }
  1122. static int vmsplice_type(struct fd f, int *type)
  1123. {
  1124. if (!f.file)
  1125. return -EBADF;
  1126. if (f.file->f_mode & FMODE_WRITE) {
  1127. *type = WRITE;
  1128. } else if (f.file->f_mode & FMODE_READ) {
  1129. *type = READ;
  1130. } else {
  1131. fdput(f);
  1132. return -EBADF;
  1133. }
  1134. return 0;
  1135. }
  1136. /*
  1137. * Note that vmsplice only really supports true splicing _from_ user memory
  1138. * to a pipe, not the other way around. Splicing from user memory is a simple
  1139. * operation that can be supported without any funky alignment restrictions
  1140. * or nasty vm tricks. We simply map in the user memory and fill them into
  1141. * a pipe. The reverse isn't quite as easy, though. There are two possible
  1142. * solutions for that:
  1143. *
  1144. * - memcpy() the data internally, at which point we might as well just
  1145. * do a regular read() on the buffer anyway.
  1146. * - Lots of nasty vm tricks, that are neither fast nor flexible (it
  1147. * has restriction limitations on both ends of the pipe).
  1148. *
  1149. * Currently we punt and implement it as a normal copy, see pipe_to_user().
  1150. *
  1151. */
  1152. static long do_vmsplice(struct file *f, struct iov_iter *iter, unsigned int flags)
  1153. {
  1154. if (unlikely(flags & ~SPLICE_F_ALL))
  1155. return -EINVAL;
  1156. if (!iov_iter_count(iter))
  1157. return 0;
  1158. if (iov_iter_rw(iter) == WRITE)
  1159. return vmsplice_to_pipe(f, iter, flags);
  1160. else
  1161. return vmsplice_to_user(f, iter, flags);
  1162. }
  1163. SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
  1164. unsigned long, nr_segs, unsigned int, flags)
  1165. {
  1166. struct iovec iovstack[UIO_FASTIOV];
  1167. struct iovec *iov = iovstack;
  1168. struct iov_iter iter;
  1169. long error;
  1170. struct fd f;
  1171. int type;
  1172. f = fdget(fd);
  1173. error = vmsplice_type(f, &type);
  1174. if (error)
  1175. return error;
  1176. error = import_iovec(type, uiov, nr_segs,
  1177. ARRAY_SIZE(iovstack), &iov, &iter);
  1178. if (!error) {
  1179. error = do_vmsplice(f.file, &iter, flags);
  1180. kfree(iov);
  1181. }
  1182. fdput(f);
  1183. return error;
  1184. }
  1185. #ifdef CONFIG_COMPAT
  1186. COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
  1187. unsigned int, nr_segs, unsigned int, flags)
  1188. {
  1189. struct iovec iovstack[UIO_FASTIOV];
  1190. struct iovec *iov = iovstack;
  1191. struct iov_iter iter;
  1192. long error;
  1193. struct fd f;
  1194. int type;
  1195. f = fdget(fd);
  1196. error = vmsplice_type(f, &type);
  1197. if (error)
  1198. return error;
  1199. error = compat_import_iovec(type, iov32, nr_segs,
  1200. ARRAY_SIZE(iovstack), &iov, &iter);
  1201. if (!error) {
  1202. error = do_vmsplice(f.file, &iter, flags);
  1203. kfree(iov);
  1204. }
  1205. fdput(f);
  1206. return error;
  1207. }
  1208. #endif
  1209. SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
  1210. int, fd_out, loff_t __user *, off_out,
  1211. size_t, len, unsigned int, flags)
  1212. {
  1213. struct fd in, out;
  1214. long error;
  1215. if (unlikely(!len))
  1216. return 0;
  1217. if (unlikely(flags & ~SPLICE_F_ALL))
  1218. return -EINVAL;
  1219. error = -EBADF;
  1220. in = fdget(fd_in);
  1221. if (in.file) {
  1222. if (in.file->f_mode & FMODE_READ) {
  1223. out = fdget(fd_out);
  1224. if (out.file) {
  1225. if (out.file->f_mode & FMODE_WRITE)
  1226. error = do_splice(in.file, off_in,
  1227. out.file, off_out,
  1228. len, flags);
  1229. fdput(out);
  1230. }
  1231. }
  1232. fdput(in);
  1233. }
  1234. return error;
  1235. }
  1236. /*
  1237. * Make sure there's data to read. Wait for input if we can, otherwise
  1238. * return an appropriate error.
  1239. */
  1240. static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1241. {
  1242. int ret;
  1243. /*
  1244. * Check ->nrbufs without the inode lock first. This function
  1245. * is speculative anyways, so missing one is ok.
  1246. */
  1247. if (pipe->nrbufs)
  1248. return 0;
  1249. ret = 0;
  1250. pipe_lock(pipe);
  1251. while (!pipe->nrbufs) {
  1252. if (signal_pending(current)) {
  1253. ret = -ERESTARTSYS;
  1254. break;
  1255. }
  1256. if (!pipe->writers)
  1257. break;
  1258. if (!pipe->waiting_writers) {
  1259. if (flags & SPLICE_F_NONBLOCK) {
  1260. ret = -EAGAIN;
  1261. break;
  1262. }
  1263. }
  1264. pipe_wait(pipe);
  1265. }
  1266. pipe_unlock(pipe);
  1267. return ret;
  1268. }
  1269. /*
  1270. * Make sure there's writeable room. Wait for room if we can, otherwise
  1271. * return an appropriate error.
  1272. */
  1273. static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1274. {
  1275. int ret;
  1276. /*
  1277. * Check ->nrbufs without the inode lock first. This function
  1278. * is speculative anyways, so missing one is ok.
  1279. */
  1280. if (pipe->nrbufs < pipe->buffers)
  1281. return 0;
  1282. ret = 0;
  1283. pipe_lock(pipe);
  1284. while (pipe->nrbufs >= pipe->buffers) {
  1285. if (!pipe->readers) {
  1286. send_sig(SIGPIPE, current, 0);
  1287. ret = -EPIPE;
  1288. break;
  1289. }
  1290. if (flags & SPLICE_F_NONBLOCK) {
  1291. ret = -EAGAIN;
  1292. break;
  1293. }
  1294. if (signal_pending(current)) {
  1295. ret = -ERESTARTSYS;
  1296. break;
  1297. }
  1298. pipe->waiting_writers++;
  1299. pipe_wait(pipe);
  1300. pipe->waiting_writers--;
  1301. }
  1302. pipe_unlock(pipe);
  1303. return ret;
  1304. }
  1305. /*
  1306. * Splice contents of ipipe to opipe.
  1307. */
  1308. static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  1309. struct pipe_inode_info *opipe,
  1310. size_t len, unsigned int flags)
  1311. {
  1312. struct pipe_buffer *ibuf, *obuf;
  1313. int ret = 0, nbuf;
  1314. bool input_wakeup = false;
  1315. retry:
  1316. ret = ipipe_prep(ipipe, flags);
  1317. if (ret)
  1318. return ret;
  1319. ret = opipe_prep(opipe, flags);
  1320. if (ret)
  1321. return ret;
  1322. /*
  1323. * Potential ABBA deadlock, work around it by ordering lock
  1324. * grabbing by pipe info address. Otherwise two different processes
  1325. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1326. */
  1327. pipe_double_lock(ipipe, opipe);
  1328. do {
  1329. if (!opipe->readers) {
  1330. send_sig(SIGPIPE, current, 0);
  1331. if (!ret)
  1332. ret = -EPIPE;
  1333. break;
  1334. }
  1335. if (!ipipe->nrbufs && !ipipe->writers)
  1336. break;
  1337. /*
  1338. * Cannot make any progress, because either the input
  1339. * pipe is empty or the output pipe is full.
  1340. */
  1341. if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
  1342. /* Already processed some buffers, break */
  1343. if (ret)
  1344. break;
  1345. if (flags & SPLICE_F_NONBLOCK) {
  1346. ret = -EAGAIN;
  1347. break;
  1348. }
  1349. /*
  1350. * We raced with another reader/writer and haven't
  1351. * managed to process any buffers. A zero return
  1352. * value means EOF, so retry instead.
  1353. */
  1354. pipe_unlock(ipipe);
  1355. pipe_unlock(opipe);
  1356. goto retry;
  1357. }
  1358. ibuf = ipipe->bufs + ipipe->curbuf;
  1359. nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
  1360. obuf = opipe->bufs + nbuf;
  1361. if (len >= ibuf->len) {
  1362. /*
  1363. * Simply move the whole buffer from ipipe to opipe
  1364. */
  1365. *obuf = *ibuf;
  1366. ibuf->ops = NULL;
  1367. opipe->nrbufs++;
  1368. ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
  1369. ipipe->nrbufs--;
  1370. input_wakeup = true;
  1371. } else {
  1372. /*
  1373. * Get a reference to this pipe buffer,
  1374. * so we can copy the contents over.
  1375. */
  1376. if (!pipe_buf_get(ipipe, ibuf)) {
  1377. if (ret == 0)
  1378. ret = -EFAULT;
  1379. break;
  1380. }
  1381. *obuf = *ibuf;
  1382. /*
  1383. * Don't inherit the gift flag, we need to
  1384. * prevent multiple steals of this page.
  1385. */
  1386. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1387. pipe_buf_mark_unmergeable(obuf);
  1388. obuf->len = len;
  1389. opipe->nrbufs++;
  1390. ibuf->offset += obuf->len;
  1391. ibuf->len -= obuf->len;
  1392. }
  1393. ret += obuf->len;
  1394. len -= obuf->len;
  1395. } while (len);
  1396. pipe_unlock(ipipe);
  1397. pipe_unlock(opipe);
  1398. /*
  1399. * If we put data in the output pipe, wakeup any potential readers.
  1400. */
  1401. if (ret > 0)
  1402. wakeup_pipe_readers(opipe);
  1403. if (input_wakeup)
  1404. wakeup_pipe_writers(ipipe);
  1405. return ret;
  1406. }
  1407. /*
  1408. * Link contents of ipipe to opipe.
  1409. */
  1410. static int link_pipe(struct pipe_inode_info *ipipe,
  1411. struct pipe_inode_info *opipe,
  1412. size_t len, unsigned int flags)
  1413. {
  1414. struct pipe_buffer *ibuf, *obuf;
  1415. int ret = 0, i = 0, nbuf;
  1416. /*
  1417. * Potential ABBA deadlock, work around it by ordering lock
  1418. * grabbing by pipe info address. Otherwise two different processes
  1419. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1420. */
  1421. pipe_double_lock(ipipe, opipe);
  1422. do {
  1423. if (!opipe->readers) {
  1424. send_sig(SIGPIPE, current, 0);
  1425. if (!ret)
  1426. ret = -EPIPE;
  1427. break;
  1428. }
  1429. /*
  1430. * If we have iterated all input buffers or ran out of
  1431. * output room, break.
  1432. */
  1433. if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
  1434. break;
  1435. ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
  1436. nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
  1437. /*
  1438. * Get a reference to this pipe buffer,
  1439. * so we can copy the contents over.
  1440. */
  1441. if (!pipe_buf_get(ipipe, ibuf)) {
  1442. if (ret == 0)
  1443. ret = -EFAULT;
  1444. break;
  1445. }
  1446. obuf = opipe->bufs + nbuf;
  1447. *obuf = *ibuf;
  1448. /*
  1449. * Don't inherit the gift flag, we need to
  1450. * prevent multiple steals of this page.
  1451. */
  1452. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1453. pipe_buf_mark_unmergeable(obuf);
  1454. if (obuf->len > len)
  1455. obuf->len = len;
  1456. opipe->nrbufs++;
  1457. ret += obuf->len;
  1458. len -= obuf->len;
  1459. i++;
  1460. } while (len);
  1461. /*
  1462. * return EAGAIN if we have the potential of some data in the
  1463. * future, otherwise just return 0
  1464. */
  1465. if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
  1466. ret = -EAGAIN;
  1467. pipe_unlock(ipipe);
  1468. pipe_unlock(opipe);
  1469. /*
  1470. * If we put data in the output pipe, wakeup any potential readers.
  1471. */
  1472. if (ret > 0)
  1473. wakeup_pipe_readers(opipe);
  1474. return ret;
  1475. }
  1476. /*
  1477. * This is a tee(1) implementation that works on pipes. It doesn't copy
  1478. * any data, it simply references the 'in' pages on the 'out' pipe.
  1479. * The 'flags' used are the SPLICE_F_* variants, currently the only
  1480. * applicable one is SPLICE_F_NONBLOCK.
  1481. */
  1482. static long do_tee(struct file *in, struct file *out, size_t len,
  1483. unsigned int flags)
  1484. {
  1485. struct pipe_inode_info *ipipe = get_pipe_info(in);
  1486. struct pipe_inode_info *opipe = get_pipe_info(out);
  1487. int ret = -EINVAL;
  1488. /*
  1489. * Duplicate the contents of ipipe to opipe without actually
  1490. * copying the data.
  1491. */
  1492. if (ipipe && opipe && ipipe != opipe) {
  1493. /*
  1494. * Keep going, unless we encounter an error. The ipipe/opipe
  1495. * ordering doesn't really matter.
  1496. */
  1497. ret = ipipe_prep(ipipe, flags);
  1498. if (!ret) {
  1499. ret = opipe_prep(opipe, flags);
  1500. if (!ret)
  1501. ret = link_pipe(ipipe, opipe, len, flags);
  1502. }
  1503. }
  1504. return ret;
  1505. }
  1506. SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
  1507. {
  1508. struct fd in;
  1509. int error;
  1510. if (unlikely(flags & ~SPLICE_F_ALL))
  1511. return -EINVAL;
  1512. if (unlikely(!len))
  1513. return 0;
  1514. error = -EBADF;
  1515. in = fdget(fdin);
  1516. if (in.file) {
  1517. if (in.file->f_mode & FMODE_READ) {
  1518. struct fd out = fdget(fdout);
  1519. if (out.file) {
  1520. if (out.file->f_mode & FMODE_WRITE)
  1521. error = do_tee(in.file, out.file,
  1522. len, flags);
  1523. fdput(out);
  1524. }
  1525. }
  1526. fdput(in);
  1527. }
  1528. return error;
  1529. }