splice.c 39 KB

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