splice.c 40 KB

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