splice.c 40 KB

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