splice.c 40 KB

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