splice.c 39 KB

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