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