splice.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  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/mm_inline.h>
  25. #include <linux/swap.h>
  26. #include <linux/writeback.h>
  27. #include <linux/buffer_head.h>
  28. #include <linux/module.h>
  29. #include <linux/syscalls.h>
  30. #include <linux/uio.h>
  31. #include <linux/security.h>
  32. /*
  33. * Attempt to steal a page from a pipe buffer. This should perhaps go into
  34. * a vm helper function, it's already simplified quite a bit by the
  35. * addition of remove_mapping(). If success is returned, the caller may
  36. * attempt to reuse this page for another destination.
  37. */
  38. static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
  39. struct pipe_buffer *buf)
  40. {
  41. struct page *page = buf->page;
  42. struct address_space *mapping;
  43. lock_page(page);
  44. mapping = page_mapping(page);
  45. if (mapping) {
  46. WARN_ON(!PageUptodate(page));
  47. /*
  48. * At least for ext2 with nobh option, we need to wait on
  49. * writeback completing on this page, since we'll remove it
  50. * from the pagecache. Otherwise truncate wont wait on the
  51. * page, allowing the disk blocks to be reused by someone else
  52. * before we actually wrote our data to them. fs corruption
  53. * ensues.
  54. */
  55. wait_on_page_writeback(page);
  56. if (PagePrivate(page) && !try_to_release_page(page, GFP_KERNEL))
  57. goto out_unlock;
  58. /*
  59. * If we succeeded in removing the mapping, set LRU flag
  60. * and return good.
  61. */
  62. if (remove_mapping(mapping, page)) {
  63. buf->flags |= PIPE_BUF_FLAG_LRU;
  64. return 0;
  65. }
  66. }
  67. /*
  68. * Raced with truncate or failed to remove page from current
  69. * address space, unlock and return failure.
  70. */
  71. out_unlock:
  72. unlock_page(page);
  73. return 1;
  74. }
  75. static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
  76. struct pipe_buffer *buf)
  77. {
  78. page_cache_release(buf->page);
  79. buf->flags &= ~PIPE_BUF_FLAG_LRU;
  80. }
  81. /*
  82. * Check whether the contents of buf is OK to access. Since the content
  83. * is a page cache page, IO may be in flight.
  84. */
  85. static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
  86. struct pipe_buffer *buf)
  87. {
  88. struct page *page = buf->page;
  89. int err;
  90. if (!PageUptodate(page)) {
  91. lock_page(page);
  92. /*
  93. * Page got truncated/unhashed. This will cause a 0-byte
  94. * splice, if this is the first page.
  95. */
  96. if (!page->mapping) {
  97. err = -ENODATA;
  98. goto error;
  99. }
  100. /*
  101. * Uh oh, read-error from disk.
  102. */
  103. if (!PageUptodate(page)) {
  104. err = -EIO;
  105. goto error;
  106. }
  107. /*
  108. * Page is ok afterall, we are done.
  109. */
  110. unlock_page(page);
  111. }
  112. return 0;
  113. error:
  114. unlock_page(page);
  115. return err;
  116. }
  117. static const struct pipe_buf_operations page_cache_pipe_buf_ops = {
  118. .can_merge = 0,
  119. .map = generic_pipe_buf_map,
  120. .unmap = generic_pipe_buf_unmap,
  121. .confirm = page_cache_pipe_buf_confirm,
  122. .release = page_cache_pipe_buf_release,
  123. .steal = page_cache_pipe_buf_steal,
  124. .get = generic_pipe_buf_get,
  125. };
  126. static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
  127. struct pipe_buffer *buf)
  128. {
  129. if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
  130. return 1;
  131. buf->flags |= PIPE_BUF_FLAG_LRU;
  132. return generic_pipe_buf_steal(pipe, buf);
  133. }
  134. static const struct pipe_buf_operations user_page_pipe_buf_ops = {
  135. .can_merge = 0,
  136. .map = generic_pipe_buf_map,
  137. .unmap = generic_pipe_buf_unmap,
  138. .confirm = generic_pipe_buf_confirm,
  139. .release = page_cache_pipe_buf_release,
  140. .steal = user_page_pipe_buf_steal,
  141. .get = generic_pipe_buf_get,
  142. };
  143. /**
  144. * splice_to_pipe - fill passed data into a pipe
  145. * @pipe: pipe to fill
  146. * @spd: data to fill
  147. *
  148. * Description:
  149. * @spd contains a map of pages and len/offset tuples, along with
  150. * the struct pipe_buf_operations associated with these pages. This
  151. * function will link that data to the pipe.
  152. *
  153. */
  154. ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
  155. struct splice_pipe_desc *spd)
  156. {
  157. unsigned int spd_pages = spd->nr_pages;
  158. int ret, do_wakeup, page_nr;
  159. ret = 0;
  160. do_wakeup = 0;
  161. page_nr = 0;
  162. if (pipe->inode)
  163. mutex_lock(&pipe->inode->i_mutex);
  164. for (;;) {
  165. if (!pipe->readers) {
  166. send_sig(SIGPIPE, current, 0);
  167. if (!ret)
  168. ret = -EPIPE;
  169. break;
  170. }
  171. if (pipe->nrbufs < PIPE_BUFFERS) {
  172. int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1);
  173. struct pipe_buffer *buf = pipe->bufs + newbuf;
  174. buf->page = spd->pages[page_nr];
  175. buf->offset = spd->partial[page_nr].offset;
  176. buf->len = spd->partial[page_nr].len;
  177. buf->private = spd->partial[page_nr].private;
  178. buf->ops = spd->ops;
  179. if (spd->flags & SPLICE_F_GIFT)
  180. buf->flags |= PIPE_BUF_FLAG_GIFT;
  181. pipe->nrbufs++;
  182. page_nr++;
  183. ret += buf->len;
  184. if (pipe->inode)
  185. do_wakeup = 1;
  186. if (!--spd->nr_pages)
  187. break;
  188. if (pipe->nrbufs < PIPE_BUFFERS)
  189. continue;
  190. break;
  191. }
  192. if (spd->flags & SPLICE_F_NONBLOCK) {
  193. if (!ret)
  194. ret = -EAGAIN;
  195. break;
  196. }
  197. if (signal_pending(current)) {
  198. if (!ret)
  199. ret = -ERESTARTSYS;
  200. break;
  201. }
  202. if (do_wakeup) {
  203. smp_mb();
  204. if (waitqueue_active(&pipe->wait))
  205. wake_up_interruptible_sync(&pipe->wait);
  206. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  207. do_wakeup = 0;
  208. }
  209. pipe->waiting_writers++;
  210. pipe_wait(pipe);
  211. pipe->waiting_writers--;
  212. }
  213. if (pipe->inode) {
  214. mutex_unlock(&pipe->inode->i_mutex);
  215. if (do_wakeup) {
  216. smp_mb();
  217. if (waitqueue_active(&pipe->wait))
  218. wake_up_interruptible(&pipe->wait);
  219. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  220. }
  221. }
  222. while (page_nr < spd_pages)
  223. spd->spd_release(spd, page_nr++);
  224. return ret;
  225. }
  226. static void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
  227. {
  228. page_cache_release(spd->pages[i]);
  229. }
  230. static int
  231. __generic_file_splice_read(struct file *in, loff_t *ppos,
  232. struct pipe_inode_info *pipe, size_t len,
  233. unsigned int flags)
  234. {
  235. struct address_space *mapping = in->f_mapping;
  236. unsigned int loff, nr_pages, req_pages;
  237. struct page *pages[PIPE_BUFFERS];
  238. struct partial_page partial[PIPE_BUFFERS];
  239. struct page *page;
  240. pgoff_t index, end_index;
  241. loff_t isize;
  242. int error, page_nr;
  243. struct splice_pipe_desc spd = {
  244. .pages = pages,
  245. .partial = partial,
  246. .flags = flags,
  247. .ops = &page_cache_pipe_buf_ops,
  248. .spd_release = spd_release_page,
  249. };
  250. index = *ppos >> PAGE_CACHE_SHIFT;
  251. loff = *ppos & ~PAGE_CACHE_MASK;
  252. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  253. nr_pages = min(req_pages, (unsigned)PIPE_BUFFERS);
  254. /*
  255. * Lookup the (hopefully) full range of pages we need.
  256. */
  257. spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages);
  258. index += spd.nr_pages;
  259. /*
  260. * If find_get_pages_contig() returned fewer pages than we needed,
  261. * readahead/allocate the rest and fill in the holes.
  262. */
  263. if (spd.nr_pages < nr_pages)
  264. page_cache_sync_readahead(mapping, &in->f_ra, in,
  265. index, req_pages - spd.nr_pages);
  266. error = 0;
  267. while (spd.nr_pages < nr_pages) {
  268. /*
  269. * Page could be there, find_get_pages_contig() breaks on
  270. * the first hole.
  271. */
  272. page = find_get_page(mapping, index);
  273. if (!page) {
  274. /*
  275. * page didn't exist, allocate one.
  276. */
  277. page = page_cache_alloc_cold(mapping);
  278. if (!page)
  279. break;
  280. error = add_to_page_cache_lru(page, mapping, index,
  281. mapping_gfp_mask(mapping));
  282. if (unlikely(error)) {
  283. page_cache_release(page);
  284. if (error == -EEXIST)
  285. continue;
  286. break;
  287. }
  288. /*
  289. * add_to_page_cache() locks the page, unlock it
  290. * to avoid convoluting the logic below even more.
  291. */
  292. unlock_page(page);
  293. }
  294. pages[spd.nr_pages++] = page;
  295. index++;
  296. }
  297. /*
  298. * Now loop over the map and see if we need to start IO on any
  299. * pages, fill in the partial map, etc.
  300. */
  301. index = *ppos >> PAGE_CACHE_SHIFT;
  302. nr_pages = spd.nr_pages;
  303. spd.nr_pages = 0;
  304. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  305. unsigned int this_len;
  306. if (!len)
  307. break;
  308. /*
  309. * this_len is the max we'll use from this page
  310. */
  311. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  312. page = pages[page_nr];
  313. if (PageReadahead(page))
  314. page_cache_async_readahead(mapping, &in->f_ra, in,
  315. page, index, req_pages - page_nr);
  316. /*
  317. * If the page isn't uptodate, we may need to start io on it
  318. */
  319. if (!PageUptodate(page)) {
  320. /*
  321. * If in nonblock mode then dont block on waiting
  322. * for an in-flight io page
  323. */
  324. if (flags & SPLICE_F_NONBLOCK) {
  325. if (TestSetPageLocked(page)) {
  326. error = -EAGAIN;
  327. break;
  328. }
  329. } else
  330. lock_page(page);
  331. /*
  332. * page was truncated, stop here. if this isn't the
  333. * first page, we'll just complete what we already
  334. * added
  335. */
  336. if (!page->mapping) {
  337. unlock_page(page);
  338. break;
  339. }
  340. /*
  341. * page was already under io and is now done, great
  342. */
  343. if (PageUptodate(page)) {
  344. unlock_page(page);
  345. goto fill_it;
  346. }
  347. /*
  348. * need to read in the page
  349. */
  350. error = mapping->a_ops->readpage(in, page);
  351. if (unlikely(error)) {
  352. /*
  353. * We really should re-lookup the page here,
  354. * but it complicates things a lot. Instead
  355. * lets just do what we already stored, and
  356. * we'll get it the next time we are called.
  357. */
  358. if (error == AOP_TRUNCATED_PAGE)
  359. error = 0;
  360. break;
  361. }
  362. }
  363. fill_it:
  364. /*
  365. * i_size must be checked after PageUptodate.
  366. */
  367. isize = i_size_read(mapping->host);
  368. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  369. if (unlikely(!isize || index > end_index))
  370. break;
  371. /*
  372. * if this is the last page, see if we need to shrink
  373. * the length and stop
  374. */
  375. if (end_index == index) {
  376. unsigned int plen;
  377. /*
  378. * max good bytes in this page
  379. */
  380. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  381. if (plen <= loff)
  382. break;
  383. /*
  384. * force quit after adding this page
  385. */
  386. this_len = min(this_len, plen - loff);
  387. len = this_len;
  388. }
  389. partial[page_nr].offset = loff;
  390. partial[page_nr].len = this_len;
  391. len -= this_len;
  392. loff = 0;
  393. spd.nr_pages++;
  394. index++;
  395. }
  396. /*
  397. * Release any pages at the end, if we quit early. 'page_nr' is how far
  398. * we got, 'nr_pages' is how many pages are in the map.
  399. */
  400. while (page_nr < nr_pages)
  401. page_cache_release(pages[page_nr++]);
  402. in->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT;
  403. if (spd.nr_pages)
  404. return splice_to_pipe(pipe, &spd);
  405. return error;
  406. }
  407. /**
  408. * generic_file_splice_read - splice data from file to a pipe
  409. * @in: file to splice from
  410. * @ppos: position in @in
  411. * @pipe: pipe to splice to
  412. * @len: number of bytes to splice
  413. * @flags: splice modifier flags
  414. *
  415. * Description:
  416. * Will read pages from given file and fill them into a pipe. Can be
  417. * used as long as the address_space operations for the source implements
  418. * a readpage() hook.
  419. *
  420. */
  421. ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
  422. struct pipe_inode_info *pipe, size_t len,
  423. unsigned int flags)
  424. {
  425. loff_t isize, left;
  426. int ret;
  427. isize = i_size_read(in->f_mapping->host);
  428. if (unlikely(*ppos >= isize))
  429. return 0;
  430. left = isize - *ppos;
  431. if (unlikely(left < len))
  432. len = left;
  433. ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
  434. if (ret > 0)
  435. *ppos += ret;
  436. return ret;
  437. }
  438. EXPORT_SYMBOL(generic_file_splice_read);
  439. /*
  440. * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
  441. * using sendpage(). Return the number of bytes sent.
  442. */
  443. static int pipe_to_sendpage(struct pipe_inode_info *pipe,
  444. struct pipe_buffer *buf, struct splice_desc *sd)
  445. {
  446. struct file *file = sd->u.file;
  447. loff_t pos = sd->pos;
  448. int ret, more;
  449. ret = buf->ops->confirm(pipe, buf);
  450. if (!ret) {
  451. more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
  452. ret = file->f_op->sendpage(file, buf->page, buf->offset,
  453. sd->len, &pos, more);
  454. }
  455. return ret;
  456. }
  457. /*
  458. * This is a little more tricky than the file -> pipe splicing. There are
  459. * basically three cases:
  460. *
  461. * - Destination page already exists in the address space and there
  462. * are users of it. For that case we have no other option that
  463. * copying the data. Tough luck.
  464. * - Destination page already exists in the address space, but there
  465. * are no users of it. Make sure it's uptodate, then drop it. Fall
  466. * through to last case.
  467. * - Destination page does not exist, we can add the pipe page to
  468. * the page cache and avoid the copy.
  469. *
  470. * If asked to move pages to the output file (SPLICE_F_MOVE is set in
  471. * sd->flags), we attempt to migrate pages from the pipe to the output
  472. * file address space page cache. This is possible if no one else has
  473. * the pipe page referenced outside of the pipe and page cache. If
  474. * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
  475. * a new page in the output file page cache and fill/dirty that.
  476. */
  477. static int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  478. struct splice_desc *sd)
  479. {
  480. struct file *file = sd->u.file;
  481. struct address_space *mapping = file->f_mapping;
  482. unsigned int offset, this_len;
  483. struct page *page;
  484. void *fsdata;
  485. int ret;
  486. /*
  487. * make sure the data in this buffer is uptodate
  488. */
  489. ret = buf->ops->confirm(pipe, buf);
  490. if (unlikely(ret))
  491. return ret;
  492. offset = sd->pos & ~PAGE_CACHE_MASK;
  493. this_len = sd->len;
  494. if (this_len + offset > PAGE_CACHE_SIZE)
  495. this_len = PAGE_CACHE_SIZE - offset;
  496. ret = pagecache_write_begin(file, mapping, sd->pos, this_len,
  497. AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
  498. if (unlikely(ret))
  499. goto out;
  500. if (buf->page != page) {
  501. /*
  502. * Careful, ->map() uses KM_USER0!
  503. */
  504. char *src = buf->ops->map(pipe, buf, 1);
  505. char *dst = kmap_atomic(page, KM_USER1);
  506. memcpy(dst + offset, src + buf->offset, this_len);
  507. flush_dcache_page(page);
  508. kunmap_atomic(dst, KM_USER1);
  509. buf->ops->unmap(pipe, buf, src);
  510. }
  511. ret = pagecache_write_end(file, mapping, sd->pos, this_len, this_len,
  512. page, fsdata);
  513. out:
  514. return ret;
  515. }
  516. /**
  517. * __splice_from_pipe - splice data from a pipe to given actor
  518. * @pipe: pipe to splice from
  519. * @sd: information to @actor
  520. * @actor: handler that splices the data
  521. *
  522. * Description:
  523. * This function does little more than loop over the pipe and call
  524. * @actor to do the actual moving of a single struct pipe_buffer to
  525. * the desired destination. See pipe_to_file, pipe_to_sendpage, or
  526. * pipe_to_user.
  527. *
  528. */
  529. ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
  530. splice_actor *actor)
  531. {
  532. int ret, do_wakeup, err;
  533. ret = 0;
  534. do_wakeup = 0;
  535. for (;;) {
  536. if (pipe->nrbufs) {
  537. struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  538. const struct pipe_buf_operations *ops = buf->ops;
  539. sd->len = buf->len;
  540. if (sd->len > sd->total_len)
  541. sd->len = sd->total_len;
  542. err = actor(pipe, buf, sd);
  543. if (err <= 0) {
  544. if (!ret && err != -ENODATA)
  545. ret = err;
  546. break;
  547. }
  548. ret += err;
  549. buf->offset += err;
  550. buf->len -= err;
  551. sd->len -= err;
  552. sd->pos += err;
  553. sd->total_len -= err;
  554. if (sd->len)
  555. continue;
  556. if (!buf->len) {
  557. buf->ops = NULL;
  558. ops->release(pipe, buf);
  559. pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
  560. pipe->nrbufs--;
  561. if (pipe->inode)
  562. do_wakeup = 1;
  563. }
  564. if (!sd->total_len)
  565. break;
  566. }
  567. if (pipe->nrbufs)
  568. continue;
  569. if (!pipe->writers)
  570. break;
  571. if (!pipe->waiting_writers) {
  572. if (ret)
  573. break;
  574. }
  575. if (sd->flags & SPLICE_F_NONBLOCK) {
  576. if (!ret)
  577. ret = -EAGAIN;
  578. break;
  579. }
  580. if (signal_pending(current)) {
  581. if (!ret)
  582. ret = -ERESTARTSYS;
  583. break;
  584. }
  585. if (do_wakeup) {
  586. smp_mb();
  587. if (waitqueue_active(&pipe->wait))
  588. wake_up_interruptible_sync(&pipe->wait);
  589. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  590. do_wakeup = 0;
  591. }
  592. pipe_wait(pipe);
  593. }
  594. if (do_wakeup) {
  595. smp_mb();
  596. if (waitqueue_active(&pipe->wait))
  597. wake_up_interruptible(&pipe->wait);
  598. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  599. }
  600. return ret;
  601. }
  602. EXPORT_SYMBOL(__splice_from_pipe);
  603. /**
  604. * splice_from_pipe - splice data from a pipe to a file
  605. * @pipe: pipe to splice from
  606. * @out: file to splice to
  607. * @ppos: position in @out
  608. * @len: how many bytes to splice
  609. * @flags: splice modifier flags
  610. * @actor: handler that splices the data
  611. *
  612. * Description:
  613. * See __splice_from_pipe. This function locks the input and output inodes,
  614. * otherwise it's identical to __splice_from_pipe().
  615. *
  616. */
  617. ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
  618. loff_t *ppos, size_t len, unsigned int flags,
  619. splice_actor *actor)
  620. {
  621. ssize_t ret;
  622. struct inode *inode = out->f_mapping->host;
  623. struct splice_desc sd = {
  624. .total_len = len,
  625. .flags = flags,
  626. .pos = *ppos,
  627. .u.file = out,
  628. };
  629. /*
  630. * The actor worker might be calling ->prepare_write and
  631. * ->commit_write. Most of the time, these expect i_mutex to
  632. * be held. Since this may result in an ABBA deadlock with
  633. * pipe->inode, we have to order lock acquiry here.
  634. */
  635. inode_double_lock(inode, pipe->inode);
  636. ret = __splice_from_pipe(pipe, &sd, actor);
  637. inode_double_unlock(inode, pipe->inode);
  638. return ret;
  639. }
  640. /**
  641. * generic_file_splice_write_nolock - generic_file_splice_write without mutexes
  642. * @pipe: pipe info
  643. * @out: file to write to
  644. * @ppos: position in @out
  645. * @len: number of bytes to splice
  646. * @flags: splice modifier flags
  647. *
  648. * Description:
  649. * Will either move or copy pages (determined by @flags options) from
  650. * the given pipe inode to the given file. The caller is responsible
  651. * for acquiring i_mutex on both inodes.
  652. *
  653. */
  654. ssize_t
  655. generic_file_splice_write_nolock(struct pipe_inode_info *pipe, struct file *out,
  656. loff_t *ppos, size_t len, unsigned int flags)
  657. {
  658. struct address_space *mapping = out->f_mapping;
  659. struct inode *inode = mapping->host;
  660. struct splice_desc sd = {
  661. .total_len = len,
  662. .flags = flags,
  663. .pos = *ppos,
  664. .u.file = out,
  665. };
  666. ssize_t ret;
  667. int err;
  668. err = remove_suid(out->f_path.dentry);
  669. if (unlikely(err))
  670. return err;
  671. ret = __splice_from_pipe(pipe, &sd, pipe_to_file);
  672. if (ret > 0) {
  673. unsigned long nr_pages;
  674. *ppos += ret;
  675. nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  676. /*
  677. * If file or inode is SYNC and we actually wrote some data,
  678. * sync it.
  679. */
  680. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  681. err = generic_osync_inode(inode, mapping,
  682. OSYNC_METADATA|OSYNC_DATA);
  683. if (err)
  684. ret = err;
  685. }
  686. balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
  687. }
  688. return ret;
  689. }
  690. EXPORT_SYMBOL(generic_file_splice_write_nolock);
  691. /**
  692. * generic_file_splice_write - splice data from a pipe to a file
  693. * @pipe: pipe info
  694. * @out: file to write to
  695. * @ppos: position in @out
  696. * @len: number of bytes to splice
  697. * @flags: splice modifier flags
  698. *
  699. * Description:
  700. * Will either move or copy pages (determined by @flags options) from
  701. * the given pipe inode to the given file.
  702. *
  703. */
  704. ssize_t
  705. generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  706. loff_t *ppos, size_t len, unsigned int flags)
  707. {
  708. struct address_space *mapping = out->f_mapping;
  709. struct inode *inode = mapping->host;
  710. struct splice_desc sd = {
  711. .total_len = len,
  712. .flags = flags,
  713. .pos = *ppos,
  714. .u.file = out,
  715. };
  716. ssize_t ret;
  717. inode_double_lock(inode, pipe->inode);
  718. ret = remove_suid(out->f_path.dentry);
  719. if (likely(!ret))
  720. ret = __splice_from_pipe(pipe, &sd, pipe_to_file);
  721. inode_double_unlock(inode, pipe->inode);
  722. if (ret > 0) {
  723. unsigned long nr_pages;
  724. *ppos += ret;
  725. nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  726. /*
  727. * If file or inode is SYNC and we actually wrote some data,
  728. * sync it.
  729. */
  730. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  731. int err;
  732. mutex_lock(&inode->i_mutex);
  733. err = generic_osync_inode(inode, mapping,
  734. OSYNC_METADATA|OSYNC_DATA);
  735. mutex_unlock(&inode->i_mutex);
  736. if (err)
  737. ret = err;
  738. }
  739. balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
  740. }
  741. return ret;
  742. }
  743. EXPORT_SYMBOL(generic_file_splice_write);
  744. /**
  745. * generic_splice_sendpage - splice data from a pipe to a socket
  746. * @pipe: pipe to splice from
  747. * @out: socket to write to
  748. * @ppos: position in @out
  749. * @len: number of bytes to splice
  750. * @flags: splice modifier flags
  751. *
  752. * Description:
  753. * Will send @len bytes from the pipe to a network socket. No data copying
  754. * is involved.
  755. *
  756. */
  757. ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
  758. loff_t *ppos, size_t len, unsigned int flags)
  759. {
  760. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
  761. }
  762. EXPORT_SYMBOL(generic_splice_sendpage);
  763. /*
  764. * Attempt to initiate a splice from pipe to file.
  765. */
  766. static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  767. loff_t *ppos, size_t len, unsigned int flags)
  768. {
  769. int ret;
  770. if (unlikely(!out->f_op || !out->f_op->splice_write))
  771. return -EINVAL;
  772. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  773. return -EBADF;
  774. ret = rw_verify_area(WRITE, out, ppos, len);
  775. if (unlikely(ret < 0))
  776. return ret;
  777. return out->f_op->splice_write(pipe, out, ppos, len, flags);
  778. }
  779. /*
  780. * Attempt to initiate a splice from a file to a pipe.
  781. */
  782. static long do_splice_to(struct file *in, loff_t *ppos,
  783. struct pipe_inode_info *pipe, size_t len,
  784. unsigned int flags)
  785. {
  786. int ret;
  787. if (unlikely(!in->f_op || !in->f_op->splice_read))
  788. return -EINVAL;
  789. if (unlikely(!(in->f_mode & FMODE_READ)))
  790. return -EBADF;
  791. ret = rw_verify_area(READ, in, ppos, len);
  792. if (unlikely(ret < 0))
  793. return ret;
  794. return in->f_op->splice_read(in, ppos, pipe, len, flags);
  795. }
  796. /**
  797. * splice_direct_to_actor - splices data directly between two non-pipes
  798. * @in: file to splice from
  799. * @sd: actor information on where to splice to
  800. * @actor: handles the data splicing
  801. *
  802. * Description:
  803. * This is a special case helper to splice directly between two
  804. * points, without requiring an explicit pipe. Internally an allocated
  805. * pipe is cached in the process, and reused during the lifetime of
  806. * that process.
  807. *
  808. */
  809. ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
  810. splice_direct_actor *actor)
  811. {
  812. struct pipe_inode_info *pipe;
  813. long ret, bytes;
  814. umode_t i_mode;
  815. size_t len;
  816. int i, flags;
  817. /*
  818. * We require the input being a regular file, as we don't want to
  819. * randomly drop data for eg socket -> socket splicing. Use the
  820. * piped splicing for that!
  821. */
  822. i_mode = in->f_path.dentry->d_inode->i_mode;
  823. if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  824. return -EINVAL;
  825. /*
  826. * neither in nor out is a pipe, setup an internal pipe attached to
  827. * 'out' and transfer the wanted data from 'in' to 'out' through that
  828. */
  829. pipe = current->splice_pipe;
  830. if (unlikely(!pipe)) {
  831. pipe = alloc_pipe_info(NULL);
  832. if (!pipe)
  833. return -ENOMEM;
  834. /*
  835. * We don't have an immediate reader, but we'll read the stuff
  836. * out of the pipe right after the splice_to_pipe(). So set
  837. * PIPE_READERS appropriately.
  838. */
  839. pipe->readers = 1;
  840. current->splice_pipe = pipe;
  841. }
  842. /*
  843. * Do the splice.
  844. */
  845. ret = 0;
  846. bytes = 0;
  847. len = sd->total_len;
  848. flags = sd->flags;
  849. /*
  850. * Don't block on output, we have to drain the direct pipe.
  851. */
  852. sd->flags &= ~SPLICE_F_NONBLOCK;
  853. while (len) {
  854. size_t read_len;
  855. loff_t pos = sd->pos, prev_pos = pos;
  856. ret = do_splice_to(in, &pos, pipe, len, flags);
  857. if (unlikely(ret <= 0))
  858. goto out_release;
  859. read_len = ret;
  860. sd->total_len = read_len;
  861. /*
  862. * NOTE: nonblocking mode only applies to the input. We
  863. * must not do the output in nonblocking mode as then we
  864. * could get stuck data in the internal pipe:
  865. */
  866. ret = actor(pipe, sd);
  867. if (unlikely(ret <= 0)) {
  868. sd->pos = prev_pos;
  869. goto out_release;
  870. }
  871. bytes += ret;
  872. len -= ret;
  873. sd->pos = pos;
  874. if (ret < read_len) {
  875. sd->pos = prev_pos + ret;
  876. goto out_release;
  877. }
  878. }
  879. done:
  880. pipe->nrbufs = pipe->curbuf = 0;
  881. file_accessed(in);
  882. return bytes;
  883. out_release:
  884. /*
  885. * If we did an incomplete transfer we must release
  886. * the pipe buffers in question:
  887. */
  888. for (i = 0; i < PIPE_BUFFERS; i++) {
  889. struct pipe_buffer *buf = pipe->bufs + i;
  890. if (buf->ops) {
  891. buf->ops->release(pipe, buf);
  892. buf->ops = NULL;
  893. }
  894. }
  895. if (!bytes)
  896. bytes = ret;
  897. goto done;
  898. }
  899. EXPORT_SYMBOL(splice_direct_to_actor);
  900. static int direct_splice_actor(struct pipe_inode_info *pipe,
  901. struct splice_desc *sd)
  902. {
  903. struct file *file = sd->u.file;
  904. return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
  905. }
  906. /**
  907. * do_splice_direct - splices data directly between two files
  908. * @in: file to splice from
  909. * @ppos: input file offset
  910. * @out: file to splice to
  911. * @len: number of bytes to splice
  912. * @flags: splice modifier flags
  913. *
  914. * Description:
  915. * For use by do_sendfile(). splice can easily emulate sendfile, but
  916. * doing it in the application would incur an extra system call
  917. * (splice in + splice out, as compared to just sendfile()). So this helper
  918. * can splice directly through a process-private pipe.
  919. *
  920. */
  921. long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
  922. size_t len, unsigned int flags)
  923. {
  924. struct splice_desc sd = {
  925. .len = len,
  926. .total_len = len,
  927. .flags = flags,
  928. .pos = *ppos,
  929. .u.file = out,
  930. };
  931. long ret;
  932. ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
  933. if (ret > 0)
  934. *ppos = sd.pos;
  935. return ret;
  936. }
  937. /*
  938. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  939. * location, so checking ->i_pipe is not enough to verify that this is a
  940. * pipe.
  941. */
  942. static inline struct pipe_inode_info *pipe_info(struct inode *inode)
  943. {
  944. if (S_ISFIFO(inode->i_mode))
  945. return inode->i_pipe;
  946. return NULL;
  947. }
  948. /*
  949. * Determine where to splice to/from.
  950. */
  951. static long do_splice(struct file *in, loff_t __user *off_in,
  952. struct file *out, loff_t __user *off_out,
  953. size_t len, unsigned int flags)
  954. {
  955. struct pipe_inode_info *pipe;
  956. loff_t offset, *off;
  957. long ret;
  958. pipe = pipe_info(in->f_path.dentry->d_inode);
  959. if (pipe) {
  960. if (off_in)
  961. return -ESPIPE;
  962. if (off_out) {
  963. if (out->f_op->llseek == no_llseek)
  964. return -EINVAL;
  965. if (copy_from_user(&offset, off_out, sizeof(loff_t)))
  966. return -EFAULT;
  967. off = &offset;
  968. } else
  969. off = &out->f_pos;
  970. ret = do_splice_from(pipe, out, off, len, flags);
  971. if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
  972. ret = -EFAULT;
  973. return ret;
  974. }
  975. pipe = pipe_info(out->f_path.dentry->d_inode);
  976. if (pipe) {
  977. if (off_out)
  978. return -ESPIPE;
  979. if (off_in) {
  980. if (in->f_op->llseek == no_llseek)
  981. return -EINVAL;
  982. if (copy_from_user(&offset, off_in, sizeof(loff_t)))
  983. return -EFAULT;
  984. off = &offset;
  985. } else
  986. off = &in->f_pos;
  987. ret = do_splice_to(in, off, pipe, len, flags);
  988. if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
  989. ret = -EFAULT;
  990. return ret;
  991. }
  992. return -EINVAL;
  993. }
  994. /*
  995. * Do a copy-from-user while holding the mmap_semaphore for reading, in a
  996. * manner safe from deadlocking with simultaneous mmap() (grabbing mmap_sem
  997. * for writing) and page faulting on the user memory pointed to by src.
  998. * This assumes that we will very rarely hit the partial != 0 path, or this
  999. * will not be a win.
  1000. */
  1001. static int copy_from_user_mmap_sem(void *dst, const void __user *src, size_t n)
  1002. {
  1003. int partial;
  1004. if (!access_ok(VERIFY_READ, src, n))
  1005. return -EFAULT;
  1006. pagefault_disable();
  1007. partial = __copy_from_user_inatomic(dst, src, n);
  1008. pagefault_enable();
  1009. /*
  1010. * Didn't copy everything, drop the mmap_sem and do a faulting copy
  1011. */
  1012. if (unlikely(partial)) {
  1013. up_read(&current->mm->mmap_sem);
  1014. partial = copy_from_user(dst, src, n);
  1015. down_read(&current->mm->mmap_sem);
  1016. }
  1017. return partial;
  1018. }
  1019. /*
  1020. * Map an iov into an array of pages and offset/length tupples. With the
  1021. * partial_page structure, we can map several non-contiguous ranges into
  1022. * our ones pages[] map instead of splitting that operation into pieces.
  1023. * Could easily be exported as a generic helper for other users, in which
  1024. * case one would probably want to add a 'max_nr_pages' parameter as well.
  1025. */
  1026. static int get_iovec_page_array(const struct iovec __user *iov,
  1027. unsigned int nr_vecs, struct page **pages,
  1028. struct partial_page *partial, int aligned)
  1029. {
  1030. int buffers = 0, error = 0;
  1031. down_read(&current->mm->mmap_sem);
  1032. while (nr_vecs) {
  1033. unsigned long off, npages;
  1034. struct iovec entry;
  1035. void __user *base;
  1036. size_t len;
  1037. int i;
  1038. error = -EFAULT;
  1039. if (copy_from_user_mmap_sem(&entry, iov, sizeof(entry)))
  1040. break;
  1041. base = entry.iov_base;
  1042. len = entry.iov_len;
  1043. /*
  1044. * Sanity check this iovec. 0 read succeeds.
  1045. */
  1046. error = 0;
  1047. if (unlikely(!len))
  1048. break;
  1049. error = -EFAULT;
  1050. if (!access_ok(VERIFY_READ, base, len))
  1051. break;
  1052. /*
  1053. * Get this base offset and number of pages, then map
  1054. * in the user pages.
  1055. */
  1056. off = (unsigned long) base & ~PAGE_MASK;
  1057. /*
  1058. * If asked for alignment, the offset must be zero and the
  1059. * length a multiple of the PAGE_SIZE.
  1060. */
  1061. error = -EINVAL;
  1062. if (aligned && (off || len & ~PAGE_MASK))
  1063. break;
  1064. npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1065. if (npages > PIPE_BUFFERS - buffers)
  1066. npages = PIPE_BUFFERS - buffers;
  1067. error = get_user_pages(current, current->mm,
  1068. (unsigned long) base, npages, 0, 0,
  1069. &pages[buffers], NULL);
  1070. if (unlikely(error <= 0))
  1071. break;
  1072. /*
  1073. * Fill this contiguous range into the partial page map.
  1074. */
  1075. for (i = 0; i < error; i++) {
  1076. const int plen = min_t(size_t, len, PAGE_SIZE - off);
  1077. partial[buffers].offset = off;
  1078. partial[buffers].len = plen;
  1079. off = 0;
  1080. len -= plen;
  1081. buffers++;
  1082. }
  1083. /*
  1084. * We didn't complete this iov, stop here since it probably
  1085. * means we have to move some of this into a pipe to
  1086. * be able to continue.
  1087. */
  1088. if (len)
  1089. break;
  1090. /*
  1091. * Don't continue if we mapped fewer pages than we asked for,
  1092. * or if we mapped the max number of pages that we have
  1093. * room for.
  1094. */
  1095. if (error < npages || buffers == PIPE_BUFFERS)
  1096. break;
  1097. nr_vecs--;
  1098. iov++;
  1099. }
  1100. up_read(&current->mm->mmap_sem);
  1101. if (buffers)
  1102. return buffers;
  1103. return error;
  1104. }
  1105. static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  1106. struct splice_desc *sd)
  1107. {
  1108. char *src;
  1109. int ret;
  1110. ret = buf->ops->confirm(pipe, buf);
  1111. if (unlikely(ret))
  1112. return ret;
  1113. /*
  1114. * See if we can use the atomic maps, by prefaulting in the
  1115. * pages and doing an atomic copy
  1116. */
  1117. if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) {
  1118. src = buf->ops->map(pipe, buf, 1);
  1119. ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset,
  1120. sd->len);
  1121. buf->ops->unmap(pipe, buf, src);
  1122. if (!ret) {
  1123. ret = sd->len;
  1124. goto out;
  1125. }
  1126. }
  1127. /*
  1128. * No dice, use slow non-atomic map and copy
  1129. */
  1130. src = buf->ops->map(pipe, buf, 0);
  1131. ret = sd->len;
  1132. if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len))
  1133. ret = -EFAULT;
  1134. buf->ops->unmap(pipe, buf, src);
  1135. out:
  1136. if (ret > 0)
  1137. sd->u.userptr += ret;
  1138. return ret;
  1139. }
  1140. /*
  1141. * For lack of a better implementation, implement vmsplice() to userspace
  1142. * as a simple copy of the pipes pages to the user iov.
  1143. */
  1144. static long vmsplice_to_user(struct file *file, const struct iovec __user *iov,
  1145. unsigned long nr_segs, unsigned int flags)
  1146. {
  1147. struct pipe_inode_info *pipe;
  1148. struct splice_desc sd;
  1149. ssize_t size;
  1150. int error;
  1151. long ret;
  1152. pipe = pipe_info(file->f_path.dentry->d_inode);
  1153. if (!pipe)
  1154. return -EBADF;
  1155. if (pipe->inode)
  1156. mutex_lock(&pipe->inode->i_mutex);
  1157. error = ret = 0;
  1158. while (nr_segs) {
  1159. void __user *base;
  1160. size_t len;
  1161. /*
  1162. * Get user address base and length for this iovec.
  1163. */
  1164. error = get_user(base, &iov->iov_base);
  1165. if (unlikely(error))
  1166. break;
  1167. error = get_user(len, &iov->iov_len);
  1168. if (unlikely(error))
  1169. break;
  1170. /*
  1171. * Sanity check this iovec. 0 read succeeds.
  1172. */
  1173. if (unlikely(!len))
  1174. break;
  1175. if (unlikely(!base)) {
  1176. error = -EFAULT;
  1177. break;
  1178. }
  1179. if (unlikely(!access_ok(VERIFY_WRITE, base, len))) {
  1180. error = -EFAULT;
  1181. break;
  1182. }
  1183. sd.len = 0;
  1184. sd.total_len = len;
  1185. sd.flags = flags;
  1186. sd.u.userptr = base;
  1187. sd.pos = 0;
  1188. size = __splice_from_pipe(pipe, &sd, pipe_to_user);
  1189. if (size < 0) {
  1190. if (!ret)
  1191. ret = size;
  1192. break;
  1193. }
  1194. ret += size;
  1195. if (size < len)
  1196. break;
  1197. nr_segs--;
  1198. iov++;
  1199. }
  1200. if (pipe->inode)
  1201. mutex_unlock(&pipe->inode->i_mutex);
  1202. if (!ret)
  1203. ret = error;
  1204. return ret;
  1205. }
  1206. /*
  1207. * vmsplice splices a user address range into a pipe. It can be thought of
  1208. * as splice-from-memory, where the regular splice is splice-from-file (or
  1209. * to file). In both cases the output is a pipe, naturally.
  1210. */
  1211. static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
  1212. unsigned long nr_segs, unsigned int flags)
  1213. {
  1214. struct pipe_inode_info *pipe;
  1215. struct page *pages[PIPE_BUFFERS];
  1216. struct partial_page partial[PIPE_BUFFERS];
  1217. struct splice_pipe_desc spd = {
  1218. .pages = pages,
  1219. .partial = partial,
  1220. .flags = flags,
  1221. .ops = &user_page_pipe_buf_ops,
  1222. .spd_release = spd_release_page,
  1223. };
  1224. pipe = pipe_info(file->f_path.dentry->d_inode);
  1225. if (!pipe)
  1226. return -EBADF;
  1227. spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
  1228. flags & SPLICE_F_GIFT);
  1229. if (spd.nr_pages <= 0)
  1230. return spd.nr_pages;
  1231. return splice_to_pipe(pipe, &spd);
  1232. }
  1233. /*
  1234. * Note that vmsplice only really supports true splicing _from_ user memory
  1235. * to a pipe, not the other way around. Splicing from user memory is a simple
  1236. * operation that can be supported without any funky alignment restrictions
  1237. * or nasty vm tricks. We simply map in the user memory and fill them into
  1238. * a pipe. The reverse isn't quite as easy, though. There are two possible
  1239. * solutions for that:
  1240. *
  1241. * - memcpy() the data internally, at which point we might as well just
  1242. * do a regular read() on the buffer anyway.
  1243. * - Lots of nasty vm tricks, that are neither fast nor flexible (it
  1244. * has restriction limitations on both ends of the pipe).
  1245. *
  1246. * Currently we punt and implement it as a normal copy, see pipe_to_user().
  1247. *
  1248. */
  1249. asmlinkage long sys_vmsplice(int fd, const struct iovec __user *iov,
  1250. unsigned long nr_segs, unsigned int flags)
  1251. {
  1252. struct file *file;
  1253. long error;
  1254. int fput;
  1255. if (unlikely(nr_segs > UIO_MAXIOV))
  1256. return -EINVAL;
  1257. else if (unlikely(!nr_segs))
  1258. return 0;
  1259. error = -EBADF;
  1260. file = fget_light(fd, &fput);
  1261. if (file) {
  1262. if (file->f_mode & FMODE_WRITE)
  1263. error = vmsplice_to_pipe(file, iov, nr_segs, flags);
  1264. else if (file->f_mode & FMODE_READ)
  1265. error = vmsplice_to_user(file, iov, nr_segs, flags);
  1266. fput_light(file, fput);
  1267. }
  1268. return error;
  1269. }
  1270. asmlinkage long sys_splice(int fd_in, loff_t __user *off_in,
  1271. int fd_out, loff_t __user *off_out,
  1272. size_t len, unsigned int flags)
  1273. {
  1274. long error;
  1275. struct file *in, *out;
  1276. int fput_in, fput_out;
  1277. if (unlikely(!len))
  1278. return 0;
  1279. error = -EBADF;
  1280. in = fget_light(fd_in, &fput_in);
  1281. if (in) {
  1282. if (in->f_mode & FMODE_READ) {
  1283. out = fget_light(fd_out, &fput_out);
  1284. if (out) {
  1285. if (out->f_mode & FMODE_WRITE)
  1286. error = do_splice(in, off_in,
  1287. out, off_out,
  1288. len, flags);
  1289. fput_light(out, fput_out);
  1290. }
  1291. }
  1292. fput_light(in, fput_in);
  1293. }
  1294. return error;
  1295. }
  1296. /*
  1297. * Make sure there's data to read. Wait for input if we can, otherwise
  1298. * return an appropriate error.
  1299. */
  1300. static int link_ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1301. {
  1302. int ret;
  1303. /*
  1304. * Check ->nrbufs without the inode lock first. This function
  1305. * is speculative anyways, so missing one is ok.
  1306. */
  1307. if (pipe->nrbufs)
  1308. return 0;
  1309. ret = 0;
  1310. mutex_lock(&pipe->inode->i_mutex);
  1311. while (!pipe->nrbufs) {
  1312. if (signal_pending(current)) {
  1313. ret = -ERESTARTSYS;
  1314. break;
  1315. }
  1316. if (!pipe->writers)
  1317. break;
  1318. if (!pipe->waiting_writers) {
  1319. if (flags & SPLICE_F_NONBLOCK) {
  1320. ret = -EAGAIN;
  1321. break;
  1322. }
  1323. }
  1324. pipe_wait(pipe);
  1325. }
  1326. mutex_unlock(&pipe->inode->i_mutex);
  1327. return ret;
  1328. }
  1329. /*
  1330. * Make sure there's writeable room. Wait for room if we can, otherwise
  1331. * return an appropriate error.
  1332. */
  1333. static int link_opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
  1334. {
  1335. int ret;
  1336. /*
  1337. * Check ->nrbufs without the inode lock first. This function
  1338. * is speculative anyways, so missing one is ok.
  1339. */
  1340. if (pipe->nrbufs < PIPE_BUFFERS)
  1341. return 0;
  1342. ret = 0;
  1343. mutex_lock(&pipe->inode->i_mutex);
  1344. while (pipe->nrbufs >= PIPE_BUFFERS) {
  1345. if (!pipe->readers) {
  1346. send_sig(SIGPIPE, current, 0);
  1347. ret = -EPIPE;
  1348. break;
  1349. }
  1350. if (flags & SPLICE_F_NONBLOCK) {
  1351. ret = -EAGAIN;
  1352. break;
  1353. }
  1354. if (signal_pending(current)) {
  1355. ret = -ERESTARTSYS;
  1356. break;
  1357. }
  1358. pipe->waiting_writers++;
  1359. pipe_wait(pipe);
  1360. pipe->waiting_writers--;
  1361. }
  1362. mutex_unlock(&pipe->inode->i_mutex);
  1363. return ret;
  1364. }
  1365. /*
  1366. * Link contents of ipipe to opipe.
  1367. */
  1368. static int link_pipe(struct pipe_inode_info *ipipe,
  1369. struct pipe_inode_info *opipe,
  1370. size_t len, unsigned int flags)
  1371. {
  1372. struct pipe_buffer *ibuf, *obuf;
  1373. int ret = 0, i = 0, nbuf;
  1374. /*
  1375. * Potential ABBA deadlock, work around it by ordering lock
  1376. * grabbing by inode address. Otherwise two different processes
  1377. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1378. */
  1379. inode_double_lock(ipipe->inode, opipe->inode);
  1380. do {
  1381. if (!opipe->readers) {
  1382. send_sig(SIGPIPE, current, 0);
  1383. if (!ret)
  1384. ret = -EPIPE;
  1385. break;
  1386. }
  1387. /*
  1388. * If we have iterated all input buffers or ran out of
  1389. * output room, break.
  1390. */
  1391. if (i >= ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS)
  1392. break;
  1393. ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
  1394. nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
  1395. /*
  1396. * Get a reference to this pipe buffer,
  1397. * so we can copy the contents over.
  1398. */
  1399. ibuf->ops->get(ipipe, ibuf);
  1400. obuf = opipe->bufs + nbuf;
  1401. *obuf = *ibuf;
  1402. /*
  1403. * Don't inherit the gift flag, we need to
  1404. * prevent multiple steals of this page.
  1405. */
  1406. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1407. if (obuf->len > len)
  1408. obuf->len = len;
  1409. opipe->nrbufs++;
  1410. ret += obuf->len;
  1411. len -= obuf->len;
  1412. i++;
  1413. } while (len);
  1414. /*
  1415. * return EAGAIN if we have the potential of some data in the
  1416. * future, otherwise just return 0
  1417. */
  1418. if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
  1419. ret = -EAGAIN;
  1420. inode_double_unlock(ipipe->inode, opipe->inode);
  1421. /*
  1422. * If we put data in the output pipe, wakeup any potential readers.
  1423. */
  1424. if (ret > 0) {
  1425. smp_mb();
  1426. if (waitqueue_active(&opipe->wait))
  1427. wake_up_interruptible(&opipe->wait);
  1428. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  1429. }
  1430. return ret;
  1431. }
  1432. /*
  1433. * This is a tee(1) implementation that works on pipes. It doesn't copy
  1434. * any data, it simply references the 'in' pages on the 'out' pipe.
  1435. * The 'flags' used are the SPLICE_F_* variants, currently the only
  1436. * applicable one is SPLICE_F_NONBLOCK.
  1437. */
  1438. static long do_tee(struct file *in, struct file *out, size_t len,
  1439. unsigned int flags)
  1440. {
  1441. struct pipe_inode_info *ipipe = pipe_info(in->f_path.dentry->d_inode);
  1442. struct pipe_inode_info *opipe = pipe_info(out->f_path.dentry->d_inode);
  1443. int ret = -EINVAL;
  1444. /*
  1445. * Duplicate the contents of ipipe to opipe without actually
  1446. * copying the data.
  1447. */
  1448. if (ipipe && opipe && ipipe != opipe) {
  1449. /*
  1450. * Keep going, unless we encounter an error. The ipipe/opipe
  1451. * ordering doesn't really matter.
  1452. */
  1453. ret = link_ipipe_prep(ipipe, flags);
  1454. if (!ret) {
  1455. ret = link_opipe_prep(opipe, flags);
  1456. if (!ret)
  1457. ret = link_pipe(ipipe, opipe, len, flags);
  1458. }
  1459. }
  1460. return ret;
  1461. }
  1462. asmlinkage long sys_tee(int fdin, int fdout, size_t len, unsigned int flags)
  1463. {
  1464. struct file *in;
  1465. int error, fput_in;
  1466. if (unlikely(!len))
  1467. return 0;
  1468. error = -EBADF;
  1469. in = fget_light(fdin, &fput_in);
  1470. if (in) {
  1471. if (in->f_mode & FMODE_READ) {
  1472. int fput_out;
  1473. struct file *out = fget_light(fdout, &fput_out);
  1474. if (out) {
  1475. if (out->f_mode & FMODE_WRITE)
  1476. error = do_tee(in, out, len, flags);
  1477. fput_light(out, fput_out);
  1478. }
  1479. }
  1480. fput_light(in, fput_in);
  1481. }
  1482. return error;
  1483. }