splice.c 40 KB

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