splice.c 46 KB

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