splice.c 40 KB

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