splice.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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@suse.de>
  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/pipe_fs_i.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. struct partial_page {
  32. unsigned int offset;
  33. unsigned int len;
  34. };
  35. /*
  36. * Passed to splice_to_pipe
  37. */
  38. struct splice_pipe_desc {
  39. struct page **pages; /* page map */
  40. struct partial_page *partial; /* pages[] may not be contig */
  41. int nr_pages; /* number of pages in map */
  42. unsigned int flags; /* splice flags */
  43. struct pipe_buf_operations *ops;/* ops associated with output pipe */
  44. };
  45. /*
  46. * Attempt to steal a page from a pipe buffer. This should perhaps go into
  47. * a vm helper function, it's already simplified quite a bit by the
  48. * addition of remove_mapping(). If success is returned, the caller may
  49. * attempt to reuse this page for another destination.
  50. */
  51. static int page_cache_pipe_buf_steal(struct pipe_inode_info *info,
  52. struct pipe_buffer *buf)
  53. {
  54. struct page *page = buf->page;
  55. struct address_space *mapping = page_mapping(page);
  56. lock_page(page);
  57. WARN_ON(!PageUptodate(page));
  58. /*
  59. * At least for ext2 with nobh option, we need to wait on writeback
  60. * completing on this page, since we'll remove it from the pagecache.
  61. * Otherwise truncate wont wait on the page, allowing the disk
  62. * blocks to be reused by someone else before we actually wrote our
  63. * data to them. fs corruption ensues.
  64. */
  65. wait_on_page_writeback(page);
  66. if (PagePrivate(page))
  67. try_to_release_page(page, mapping_gfp_mask(mapping));
  68. if (!remove_mapping(mapping, page)) {
  69. unlock_page(page);
  70. return 1;
  71. }
  72. buf->flags |= PIPE_BUF_FLAG_LRU;
  73. return 0;
  74. }
  75. static void page_cache_pipe_buf_release(struct pipe_inode_info *info,
  76. struct pipe_buffer *buf)
  77. {
  78. page_cache_release(buf->page);
  79. buf->page = NULL;
  80. buf->flags &= ~PIPE_BUF_FLAG_LRU;
  81. }
  82. static int page_cache_pipe_buf_pin(struct pipe_inode_info *info,
  83. struct pipe_buffer *buf)
  84. {
  85. struct page *page = buf->page;
  86. int err;
  87. if (!PageUptodate(page)) {
  88. lock_page(page);
  89. /*
  90. * Page got truncated/unhashed. This will cause a 0-byte
  91. * splice, if this is the first page.
  92. */
  93. if (!page->mapping) {
  94. err = -ENODATA;
  95. goto error;
  96. }
  97. /*
  98. * Uh oh, read-error from disk.
  99. */
  100. if (!PageUptodate(page)) {
  101. err = -EIO;
  102. goto error;
  103. }
  104. /*
  105. * Page is ok afterall, we are done.
  106. */
  107. unlock_page(page);
  108. }
  109. return 0;
  110. error:
  111. unlock_page(page);
  112. return err;
  113. }
  114. static struct pipe_buf_operations page_cache_pipe_buf_ops = {
  115. .can_merge = 0,
  116. .map = generic_pipe_buf_map,
  117. .unmap = generic_pipe_buf_unmap,
  118. .pin = page_cache_pipe_buf_pin,
  119. .release = page_cache_pipe_buf_release,
  120. .steal = page_cache_pipe_buf_steal,
  121. .get = generic_pipe_buf_get,
  122. };
  123. static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
  124. struct pipe_buffer *buf)
  125. {
  126. return 1;
  127. }
  128. static struct pipe_buf_operations user_page_pipe_buf_ops = {
  129. .can_merge = 0,
  130. .map = generic_pipe_buf_map,
  131. .unmap = generic_pipe_buf_unmap,
  132. .pin = generic_pipe_buf_pin,
  133. .release = page_cache_pipe_buf_release,
  134. .steal = user_page_pipe_buf_steal,
  135. .get = generic_pipe_buf_get,
  136. };
  137. /*
  138. * Pipe output worker. This sets up our pipe format with the page cache
  139. * pipe buffer operations. Otherwise very similar to the regular pipe_writev().
  140. */
  141. static ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
  142. struct splice_pipe_desc *spd)
  143. {
  144. int ret, do_wakeup, page_nr;
  145. ret = 0;
  146. do_wakeup = 0;
  147. page_nr = 0;
  148. if (pipe->inode)
  149. mutex_lock(&pipe->inode->i_mutex);
  150. for (;;) {
  151. if (!pipe->readers) {
  152. send_sig(SIGPIPE, current, 0);
  153. if (!ret)
  154. ret = -EPIPE;
  155. break;
  156. }
  157. if (pipe->nrbufs < PIPE_BUFFERS) {
  158. int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1);
  159. struct pipe_buffer *buf = pipe->bufs + newbuf;
  160. buf->page = spd->pages[page_nr];
  161. buf->offset = spd->partial[page_nr].offset;
  162. buf->len = spd->partial[page_nr].len;
  163. buf->ops = spd->ops;
  164. pipe->nrbufs++;
  165. page_nr++;
  166. ret += buf->len;
  167. if (pipe->inode)
  168. do_wakeup = 1;
  169. if (!--spd->nr_pages)
  170. break;
  171. if (pipe->nrbufs < PIPE_BUFFERS)
  172. continue;
  173. break;
  174. }
  175. if (spd->flags & SPLICE_F_NONBLOCK) {
  176. if (!ret)
  177. ret = -EAGAIN;
  178. break;
  179. }
  180. if (signal_pending(current)) {
  181. if (!ret)
  182. ret = -ERESTARTSYS;
  183. break;
  184. }
  185. if (do_wakeup) {
  186. smp_mb();
  187. if (waitqueue_active(&pipe->wait))
  188. wake_up_interruptible_sync(&pipe->wait);
  189. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  190. do_wakeup = 0;
  191. }
  192. pipe->waiting_writers++;
  193. pipe_wait(pipe);
  194. pipe->waiting_writers--;
  195. }
  196. if (pipe->inode)
  197. mutex_unlock(&pipe->inode->i_mutex);
  198. if (do_wakeup) {
  199. smp_mb();
  200. if (waitqueue_active(&pipe->wait))
  201. wake_up_interruptible(&pipe->wait);
  202. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  203. }
  204. while (page_nr < spd->nr_pages)
  205. page_cache_release(spd->pages[page_nr++]);
  206. return ret;
  207. }
  208. static int
  209. __generic_file_splice_read(struct file *in, loff_t *ppos,
  210. struct pipe_inode_info *pipe, size_t len,
  211. unsigned int flags)
  212. {
  213. struct address_space *mapping = in->f_mapping;
  214. unsigned int loff, nr_pages;
  215. struct page *pages[PIPE_BUFFERS];
  216. struct partial_page partial[PIPE_BUFFERS];
  217. struct page *page;
  218. pgoff_t index, end_index;
  219. loff_t isize;
  220. size_t total_len;
  221. int error, page_nr;
  222. struct splice_pipe_desc spd = {
  223. .pages = pages,
  224. .partial = partial,
  225. .flags = flags,
  226. .ops = &page_cache_pipe_buf_ops,
  227. };
  228. index = *ppos >> PAGE_CACHE_SHIFT;
  229. loff = *ppos & ~PAGE_CACHE_MASK;
  230. nr_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  231. if (nr_pages > PIPE_BUFFERS)
  232. nr_pages = PIPE_BUFFERS;
  233. /*
  234. * Initiate read-ahead on this page range. however, don't call into
  235. * read-ahead if this is a non-zero offset (we are likely doing small
  236. * chunk splice and the page is already there) for a single page.
  237. */
  238. if (!loff || nr_pages > 1)
  239. page_cache_readahead(mapping, &in->f_ra, in, index, nr_pages);
  240. /*
  241. * Now fill in the holes:
  242. */
  243. error = 0;
  244. total_len = 0;
  245. /*
  246. * Lookup the (hopefully) full range of pages we need.
  247. */
  248. spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages);
  249. /*
  250. * If find_get_pages_contig() returned fewer pages than we needed,
  251. * allocate the rest.
  252. */
  253. index += spd.nr_pages;
  254. while (spd.nr_pages < nr_pages) {
  255. /*
  256. * Page could be there, find_get_pages_contig() breaks on
  257. * the first hole.
  258. */
  259. page = find_get_page(mapping, index);
  260. if (!page) {
  261. /*
  262. * Make sure the read-ahead engine is notified
  263. * about this failure.
  264. */
  265. handle_ra_miss(mapping, &in->f_ra, index);
  266. /*
  267. * page didn't exist, allocate one.
  268. */
  269. page = page_cache_alloc_cold(mapping);
  270. if (!page)
  271. break;
  272. error = add_to_page_cache_lru(page, mapping, index,
  273. mapping_gfp_mask(mapping));
  274. if (unlikely(error)) {
  275. page_cache_release(page);
  276. break;
  277. }
  278. /*
  279. * add_to_page_cache() locks the page, unlock it
  280. * to avoid convoluting the logic below even more.
  281. */
  282. unlock_page(page);
  283. }
  284. pages[spd.nr_pages++] = page;
  285. index++;
  286. }
  287. /*
  288. * Now loop over the map and see if we need to start IO on any
  289. * pages, fill in the partial map, etc.
  290. */
  291. index = *ppos >> PAGE_CACHE_SHIFT;
  292. nr_pages = spd.nr_pages;
  293. spd.nr_pages = 0;
  294. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  295. unsigned int this_len;
  296. if (!len)
  297. break;
  298. /*
  299. * this_len is the max we'll use from this page
  300. */
  301. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  302. page = pages[page_nr];
  303. /*
  304. * If the page isn't uptodate, we may need to start io on it
  305. */
  306. if (!PageUptodate(page)) {
  307. /*
  308. * If in nonblock mode then dont block on waiting
  309. * for an in-flight io page
  310. */
  311. if (flags & SPLICE_F_NONBLOCK)
  312. break;
  313. lock_page(page);
  314. /*
  315. * page was truncated, stop here. if this isn't the
  316. * first page, we'll just complete what we already
  317. * added
  318. */
  319. if (!page->mapping) {
  320. unlock_page(page);
  321. break;
  322. }
  323. /*
  324. * page was already under io and is now done, great
  325. */
  326. if (PageUptodate(page)) {
  327. unlock_page(page);
  328. goto fill_it;
  329. }
  330. /*
  331. * need to read in the page
  332. */
  333. error = mapping->a_ops->readpage(in, page);
  334. if (unlikely(error)) {
  335. /*
  336. * We really should re-lookup the page here,
  337. * but it complicates things a lot. Instead
  338. * lets just do what we already stored, and
  339. * we'll get it the next time we are called.
  340. */
  341. if (error == AOP_TRUNCATED_PAGE)
  342. error = 0;
  343. break;
  344. }
  345. /*
  346. * i_size must be checked after ->readpage().
  347. */
  348. isize = i_size_read(mapping->host);
  349. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  350. if (unlikely(!isize || index > end_index))
  351. break;
  352. /*
  353. * if this is the last page, see if we need to shrink
  354. * the length and stop
  355. */
  356. if (end_index == index) {
  357. loff = PAGE_CACHE_SIZE - (isize & ~PAGE_CACHE_MASK);
  358. if (total_len + loff > isize)
  359. break;
  360. /*
  361. * force quit after adding this page
  362. */
  363. len = this_len;
  364. this_len = min(this_len, loff);
  365. loff = 0;
  366. }
  367. }
  368. fill_it:
  369. partial[page_nr].offset = loff;
  370. partial[page_nr].len = this_len;
  371. len -= this_len;
  372. total_len += this_len;
  373. loff = 0;
  374. spd.nr_pages++;
  375. index++;
  376. }
  377. /*
  378. * Release any pages at the end, if we quit early. 'i' is how far
  379. * we got, 'nr_pages' is how many pages are in the map.
  380. */
  381. while (page_nr < nr_pages)
  382. page_cache_release(pages[page_nr++]);
  383. if (spd.nr_pages)
  384. return splice_to_pipe(pipe, &spd);
  385. return error;
  386. }
  387. /**
  388. * generic_file_splice_read - splice data from file to a pipe
  389. * @in: file to splice from
  390. * @pipe: pipe to splice to
  391. * @len: number of bytes to splice
  392. * @flags: splice modifier flags
  393. *
  394. * Will read pages from given file and fill them into a pipe.
  395. */
  396. ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
  397. struct pipe_inode_info *pipe, size_t len,
  398. unsigned int flags)
  399. {
  400. ssize_t spliced;
  401. int ret;
  402. ret = 0;
  403. spliced = 0;
  404. while (len) {
  405. ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
  406. if (ret < 0)
  407. break;
  408. else if (!ret) {
  409. if (spliced)
  410. break;
  411. if (flags & SPLICE_F_NONBLOCK) {
  412. ret = -EAGAIN;
  413. break;
  414. }
  415. }
  416. *ppos += ret;
  417. len -= ret;
  418. spliced += ret;
  419. }
  420. if (spliced)
  421. return spliced;
  422. return ret;
  423. }
  424. EXPORT_SYMBOL(generic_file_splice_read);
  425. /*
  426. * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
  427. * using sendpage(). Return the number of bytes sent.
  428. */
  429. static int pipe_to_sendpage(struct pipe_inode_info *info,
  430. struct pipe_buffer *buf, struct splice_desc *sd)
  431. {
  432. struct file *file = sd->file;
  433. loff_t pos = sd->pos;
  434. int ret, more;
  435. ret = buf->ops->pin(info, buf);
  436. if (!ret) {
  437. more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
  438. ret = file->f_op->sendpage(file, buf->page, buf->offset,
  439. sd->len, &pos, more);
  440. }
  441. return ret;
  442. }
  443. /*
  444. * This is a little more tricky than the file -> pipe splicing. There are
  445. * basically three cases:
  446. *
  447. * - Destination page already exists in the address space and there
  448. * are users of it. For that case we have no other option that
  449. * copying the data. Tough luck.
  450. * - Destination page already exists in the address space, but there
  451. * are no users of it. Make sure it's uptodate, then drop it. Fall
  452. * through to last case.
  453. * - Destination page does not exist, we can add the pipe page to
  454. * the page cache and avoid the copy.
  455. *
  456. * If asked to move pages to the output file (SPLICE_F_MOVE is set in
  457. * sd->flags), we attempt to migrate pages from the pipe to the output
  458. * file address space page cache. This is possible if no one else has
  459. * the pipe page referenced outside of the pipe and page cache. If
  460. * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
  461. * a new page in the output file page cache and fill/dirty that.
  462. */
  463. static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
  464. struct splice_desc *sd)
  465. {
  466. struct file *file = sd->file;
  467. struct address_space *mapping = file->f_mapping;
  468. gfp_t gfp_mask = mapping_gfp_mask(mapping);
  469. unsigned int offset, this_len;
  470. struct page *page;
  471. pgoff_t index;
  472. int ret;
  473. /*
  474. * make sure the data in this buffer is uptodate
  475. */
  476. ret = buf->ops->pin(info, buf);
  477. if (unlikely(ret))
  478. return ret;
  479. index = sd->pos >> PAGE_CACHE_SHIFT;
  480. offset = sd->pos & ~PAGE_CACHE_MASK;
  481. this_len = sd->len;
  482. if (this_len + offset > PAGE_CACHE_SIZE)
  483. this_len = PAGE_CACHE_SIZE - offset;
  484. /*
  485. * Reuse buf page, if SPLICE_F_MOVE is set and we are doing a full
  486. * page.
  487. */
  488. if ((sd->flags & SPLICE_F_MOVE) && this_len == PAGE_CACHE_SIZE) {
  489. /*
  490. * If steal succeeds, buf->page is now pruned from the vm
  491. * side (LRU and page cache) and we can reuse it. The page
  492. * will also be looked on successful return.
  493. */
  494. if (buf->ops->steal(info, buf))
  495. goto find_page;
  496. page = buf->page;
  497. if (add_to_page_cache(page, mapping, index, gfp_mask)) {
  498. unlock_page(page);
  499. goto find_page;
  500. }
  501. page_cache_get(page);
  502. if (!(buf->flags & PIPE_BUF_FLAG_LRU))
  503. lru_cache_add(page);
  504. } else {
  505. find_page:
  506. page = find_lock_page(mapping, index);
  507. if (!page) {
  508. ret = -ENOMEM;
  509. page = page_cache_alloc_cold(mapping);
  510. if (unlikely(!page))
  511. goto out_nomem;
  512. /*
  513. * This will also lock the page
  514. */
  515. ret = add_to_page_cache_lru(page, mapping, index,
  516. gfp_mask);
  517. if (unlikely(ret))
  518. goto out;
  519. }
  520. /*
  521. * We get here with the page locked. If the page is also
  522. * uptodate, we don't need to do more. If it isn't, we
  523. * may need to bring it in if we are not going to overwrite
  524. * the full page.
  525. */
  526. if (!PageUptodate(page)) {
  527. if (this_len < PAGE_CACHE_SIZE) {
  528. ret = mapping->a_ops->readpage(file, page);
  529. if (unlikely(ret))
  530. goto out;
  531. lock_page(page);
  532. if (!PageUptodate(page)) {
  533. /*
  534. * Page got invalidated, repeat.
  535. */
  536. if (!page->mapping) {
  537. unlock_page(page);
  538. page_cache_release(page);
  539. goto find_page;
  540. }
  541. ret = -EIO;
  542. goto out;
  543. }
  544. } else
  545. SetPageUptodate(page);
  546. }
  547. }
  548. ret = mapping->a_ops->prepare_write(file, page, offset, offset+this_len);
  549. if (ret == AOP_TRUNCATED_PAGE) {
  550. page_cache_release(page);
  551. goto find_page;
  552. } else if (ret)
  553. goto out;
  554. if (buf->page != page) {
  555. /*
  556. * Careful, ->map() uses KM_USER0!
  557. */
  558. char *src = buf->ops->map(info, buf, 1);
  559. char *dst = kmap_atomic(page, KM_USER1);
  560. memcpy(dst + offset, src + buf->offset, this_len);
  561. flush_dcache_page(page);
  562. kunmap_atomic(dst, KM_USER1);
  563. buf->ops->unmap(info, buf, src);
  564. }
  565. ret = mapping->a_ops->commit_write(file, page, offset, offset+this_len);
  566. if (!ret) {
  567. /*
  568. * Return the number of bytes written and mark page as
  569. * accessed, we are now done!
  570. */
  571. ret = this_len;
  572. mark_page_accessed(page);
  573. balance_dirty_pages_ratelimited(mapping);
  574. } else if (ret == AOP_TRUNCATED_PAGE) {
  575. page_cache_release(page);
  576. goto find_page;
  577. }
  578. out:
  579. page_cache_release(page);
  580. unlock_page(page);
  581. out_nomem:
  582. return ret;
  583. }
  584. /*
  585. * Pipe input worker. Most of this logic works like a regular pipe, the
  586. * key here is the 'actor' worker passed in that actually moves the data
  587. * to the wanted destination. See pipe_to_file/pipe_to_sendpage above.
  588. */
  589. ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
  590. loff_t *ppos, size_t len, unsigned int flags,
  591. splice_actor *actor)
  592. {
  593. int ret, do_wakeup, err;
  594. struct splice_desc sd;
  595. ret = 0;
  596. do_wakeup = 0;
  597. sd.total_len = len;
  598. sd.flags = flags;
  599. sd.file = out;
  600. sd.pos = *ppos;
  601. if (pipe->inode)
  602. mutex_lock(&pipe->inode->i_mutex);
  603. for (;;) {
  604. if (pipe->nrbufs) {
  605. struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  606. struct pipe_buf_operations *ops = buf->ops;
  607. sd.len = buf->len;
  608. if (sd.len > sd.total_len)
  609. sd.len = sd.total_len;
  610. err = actor(pipe, buf, &sd);
  611. if (err <= 0) {
  612. if (!ret && err != -ENODATA)
  613. ret = err;
  614. break;
  615. }
  616. ret += err;
  617. buf->offset += err;
  618. buf->len -= err;
  619. sd.len -= err;
  620. sd.pos += err;
  621. sd.total_len -= err;
  622. if (sd.len)
  623. continue;
  624. if (!buf->len) {
  625. buf->ops = NULL;
  626. ops->release(pipe, buf);
  627. pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
  628. pipe->nrbufs--;
  629. if (pipe->inode)
  630. do_wakeup = 1;
  631. }
  632. if (!sd.total_len)
  633. break;
  634. }
  635. if (pipe->nrbufs)
  636. continue;
  637. if (!pipe->writers)
  638. break;
  639. if (!pipe->waiting_writers) {
  640. if (ret)
  641. break;
  642. }
  643. if (flags & SPLICE_F_NONBLOCK) {
  644. if (!ret)
  645. ret = -EAGAIN;
  646. break;
  647. }
  648. if (signal_pending(current)) {
  649. if (!ret)
  650. ret = -ERESTARTSYS;
  651. break;
  652. }
  653. if (do_wakeup) {
  654. smp_mb();
  655. if (waitqueue_active(&pipe->wait))
  656. wake_up_interruptible_sync(&pipe->wait);
  657. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  658. do_wakeup = 0;
  659. }
  660. pipe_wait(pipe);
  661. }
  662. if (pipe->inode)
  663. mutex_unlock(&pipe->inode->i_mutex);
  664. if (do_wakeup) {
  665. smp_mb();
  666. if (waitqueue_active(&pipe->wait))
  667. wake_up_interruptible(&pipe->wait);
  668. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  669. }
  670. return ret;
  671. }
  672. /**
  673. * generic_file_splice_write - splice data from a pipe to a file
  674. * @pipe: pipe info
  675. * @out: file to write to
  676. * @len: number of bytes to splice
  677. * @flags: splice modifier flags
  678. *
  679. * Will either move or copy pages (determined by @flags options) from
  680. * the given pipe inode to the given file.
  681. *
  682. */
  683. ssize_t
  684. generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  685. loff_t *ppos, size_t len, unsigned int flags)
  686. {
  687. struct address_space *mapping = out->f_mapping;
  688. ssize_t ret;
  689. ret = splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_file);
  690. if (ret > 0) {
  691. struct inode *inode = mapping->host;
  692. *ppos += ret;
  693. /*
  694. * If file or inode is SYNC and we actually wrote some data,
  695. * sync it.
  696. */
  697. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  698. int err;
  699. mutex_lock(&inode->i_mutex);
  700. err = generic_osync_inode(inode, mapping,
  701. OSYNC_METADATA|OSYNC_DATA);
  702. mutex_unlock(&inode->i_mutex);
  703. if (err)
  704. ret = err;
  705. }
  706. }
  707. return ret;
  708. }
  709. EXPORT_SYMBOL(generic_file_splice_write);
  710. /**
  711. * generic_splice_sendpage - splice data from a pipe to a socket
  712. * @inode: pipe inode
  713. * @out: socket to write to
  714. * @len: number of bytes to splice
  715. * @flags: splice modifier flags
  716. *
  717. * Will send @len bytes from the pipe to a network socket. No data copying
  718. * is involved.
  719. *
  720. */
  721. ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
  722. loff_t *ppos, size_t len, unsigned int flags)
  723. {
  724. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
  725. }
  726. EXPORT_SYMBOL(generic_splice_sendpage);
  727. /*
  728. * Attempt to initiate a splice from pipe to file.
  729. */
  730. static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  731. loff_t *ppos, size_t len, unsigned int flags)
  732. {
  733. int ret;
  734. if (unlikely(!out->f_op || !out->f_op->splice_write))
  735. return -EINVAL;
  736. if (unlikely(!(out->f_mode & FMODE_WRITE)))
  737. return -EBADF;
  738. ret = rw_verify_area(WRITE, out, ppos, len);
  739. if (unlikely(ret < 0))
  740. return ret;
  741. return out->f_op->splice_write(pipe, out, ppos, len, flags);
  742. }
  743. /*
  744. * Attempt to initiate a splice from a file to a pipe.
  745. */
  746. static long do_splice_to(struct file *in, loff_t *ppos,
  747. struct pipe_inode_info *pipe, size_t len,
  748. unsigned int flags)
  749. {
  750. loff_t isize, left;
  751. int ret;
  752. if (unlikely(!in->f_op || !in->f_op->splice_read))
  753. return -EINVAL;
  754. if (unlikely(!(in->f_mode & FMODE_READ)))
  755. return -EBADF;
  756. ret = rw_verify_area(READ, in, ppos, len);
  757. if (unlikely(ret < 0))
  758. return ret;
  759. isize = i_size_read(in->f_mapping->host);
  760. if (unlikely(*ppos >= isize))
  761. return 0;
  762. left = isize - *ppos;
  763. if (unlikely(left < len))
  764. len = left;
  765. return in->f_op->splice_read(in, ppos, pipe, len, flags);
  766. }
  767. long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
  768. size_t len, unsigned int flags)
  769. {
  770. struct pipe_inode_info *pipe;
  771. long ret, bytes;
  772. loff_t out_off;
  773. umode_t i_mode;
  774. int i;
  775. /*
  776. * We require the input being a regular file, as we don't want to
  777. * randomly drop data for eg socket -> socket splicing. Use the
  778. * piped splicing for that!
  779. */
  780. i_mode = in->f_dentry->d_inode->i_mode;
  781. if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  782. return -EINVAL;
  783. /*
  784. * neither in nor out is a pipe, setup an internal pipe attached to
  785. * 'out' and transfer the wanted data from 'in' to 'out' through that
  786. */
  787. pipe = current->splice_pipe;
  788. if (unlikely(!pipe)) {
  789. pipe = alloc_pipe_info(NULL);
  790. if (!pipe)
  791. return -ENOMEM;
  792. /*
  793. * We don't have an immediate reader, but we'll read the stuff
  794. * out of the pipe right after the splice_to_pipe(). So set
  795. * PIPE_READERS appropriately.
  796. */
  797. pipe->readers = 1;
  798. current->splice_pipe = pipe;
  799. }
  800. /*
  801. * Do the splice.
  802. */
  803. ret = 0;
  804. bytes = 0;
  805. out_off = 0;
  806. while (len) {
  807. size_t read_len, max_read_len;
  808. /*
  809. * Do at most PIPE_BUFFERS pages worth of transfer:
  810. */
  811. max_read_len = min(len, (size_t)(PIPE_BUFFERS*PAGE_SIZE));
  812. ret = do_splice_to(in, ppos, pipe, max_read_len, flags);
  813. if (unlikely(ret < 0))
  814. goto out_release;
  815. read_len = ret;
  816. /*
  817. * NOTE: nonblocking mode only applies to the input. We
  818. * must not do the output in nonblocking mode as then we
  819. * could get stuck data in the internal pipe:
  820. */
  821. ret = do_splice_from(pipe, out, &out_off, read_len,
  822. flags & ~SPLICE_F_NONBLOCK);
  823. if (unlikely(ret < 0))
  824. goto out_release;
  825. bytes += ret;
  826. len -= ret;
  827. /*
  828. * In nonblocking mode, if we got back a short read then
  829. * that was due to either an IO error or due to the
  830. * pagecache entry not being there. In the IO error case
  831. * the _next_ splice attempt will produce a clean IO error
  832. * return value (not a short read), so in both cases it's
  833. * correct to break out of the loop here:
  834. */
  835. if ((flags & SPLICE_F_NONBLOCK) && (read_len < max_read_len))
  836. break;
  837. }
  838. pipe->nrbufs = pipe->curbuf = 0;
  839. return bytes;
  840. out_release:
  841. /*
  842. * If we did an incomplete transfer we must release
  843. * the pipe buffers in question:
  844. */
  845. for (i = 0; i < PIPE_BUFFERS; i++) {
  846. struct pipe_buffer *buf = pipe->bufs + i;
  847. if (buf->ops) {
  848. buf->ops->release(pipe, buf);
  849. buf->ops = NULL;
  850. }
  851. }
  852. pipe->nrbufs = pipe->curbuf = 0;
  853. /*
  854. * If we transferred some data, return the number of bytes:
  855. */
  856. if (bytes > 0)
  857. return bytes;
  858. return ret;
  859. }
  860. EXPORT_SYMBOL(do_splice_direct);
  861. /*
  862. * Determine where to splice to/from.
  863. */
  864. static long do_splice(struct file *in, loff_t __user *off_in,
  865. struct file *out, loff_t __user *off_out,
  866. size_t len, unsigned int flags)
  867. {
  868. struct pipe_inode_info *pipe;
  869. loff_t offset, *off;
  870. long ret;
  871. pipe = in->f_dentry->d_inode->i_pipe;
  872. if (pipe) {
  873. if (off_in)
  874. return -ESPIPE;
  875. if (off_out) {
  876. if (out->f_op->llseek == no_llseek)
  877. return -EINVAL;
  878. if (copy_from_user(&offset, off_out, sizeof(loff_t)))
  879. return -EFAULT;
  880. off = &offset;
  881. } else
  882. off = &out->f_pos;
  883. ret = do_splice_from(pipe, out, off, len, flags);
  884. if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
  885. ret = -EFAULT;
  886. return ret;
  887. }
  888. pipe = out->f_dentry->d_inode->i_pipe;
  889. if (pipe) {
  890. if (off_out)
  891. return -ESPIPE;
  892. if (off_in) {
  893. if (in->f_op->llseek == no_llseek)
  894. return -EINVAL;
  895. if (copy_from_user(&offset, off_in, sizeof(loff_t)))
  896. return -EFAULT;
  897. off = &offset;
  898. } else
  899. off = &in->f_pos;
  900. ret = do_splice_to(in, off, pipe, len, flags);
  901. if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
  902. ret = -EFAULT;
  903. return ret;
  904. }
  905. return -EINVAL;
  906. }
  907. /*
  908. * Map an iov into an array of pages and offset/length tupples. With the
  909. * partial_page structure, we can map several non-contiguous ranges into
  910. * our ones pages[] map instead of splitting that operation into pieces.
  911. * Could easily be exported as a generic helper for other users, in which
  912. * case one would probably want to add a 'max_nr_pages' parameter as well.
  913. */
  914. static int get_iovec_page_array(const struct iovec __user *iov,
  915. unsigned int nr_vecs, struct page **pages,
  916. struct partial_page *partial)
  917. {
  918. int buffers = 0, error = 0;
  919. /*
  920. * It's ok to take the mmap_sem for reading, even
  921. * across a "get_user()".
  922. */
  923. down_read(&current->mm->mmap_sem);
  924. while (nr_vecs) {
  925. unsigned long off, npages;
  926. void __user *base;
  927. size_t len;
  928. int i;
  929. /*
  930. * Get user address base and length for this iovec.
  931. */
  932. error = get_user(base, &iov->iov_base);
  933. if (unlikely(error))
  934. break;
  935. error = get_user(len, &iov->iov_len);
  936. if (unlikely(error))
  937. break;
  938. /*
  939. * Sanity check this iovec. 0 read succeeds.
  940. */
  941. if (unlikely(!len))
  942. break;
  943. error = -EFAULT;
  944. if (unlikely(!base))
  945. break;
  946. /*
  947. * Get this base offset and number of pages, then map
  948. * in the user pages.
  949. */
  950. off = (unsigned long) base & ~PAGE_MASK;
  951. npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  952. if (npages > PIPE_BUFFERS - buffers)
  953. npages = PIPE_BUFFERS - buffers;
  954. error = get_user_pages(current, current->mm,
  955. (unsigned long) base, npages, 0, 0,
  956. &pages[buffers], NULL);
  957. if (unlikely(error <= 0))
  958. break;
  959. /*
  960. * Fill this contiguous range into the partial page map.
  961. */
  962. for (i = 0; i < error; i++) {
  963. const int plen = min_t(size_t, len, PAGE_SIZE) - off;
  964. partial[buffers].offset = off;
  965. partial[buffers].len = plen;
  966. off = 0;
  967. len -= plen;
  968. buffers++;
  969. }
  970. /*
  971. * We didn't complete this iov, stop here since it probably
  972. * means we have to move some of this into a pipe to
  973. * be able to continue.
  974. */
  975. if (len)
  976. break;
  977. /*
  978. * Don't continue if we mapped fewer pages than we asked for,
  979. * or if we mapped the max number of pages that we have
  980. * room for.
  981. */
  982. if (error < npages || buffers == PIPE_BUFFERS)
  983. break;
  984. nr_vecs--;
  985. iov++;
  986. }
  987. up_read(&current->mm->mmap_sem);
  988. if (buffers)
  989. return buffers;
  990. return error;
  991. }
  992. /*
  993. * vmsplice splices a user address range into a pipe. It can be thought of
  994. * as splice-from-memory, where the regular splice is splice-from-file (or
  995. * to file). In both cases the output is a pipe, naturally.
  996. *
  997. * Note that vmsplice only supports splicing _from_ user memory to a pipe,
  998. * not the other way around. Splicing from user memory is a simple operation
  999. * that can be supported without any funky alignment restrictions or nasty
  1000. * vm tricks. We simply map in the user memory and fill them into a pipe.
  1001. * The reverse isn't quite as easy, though. There are two possible solutions
  1002. * for that:
  1003. *
  1004. * - memcpy() the data internally, at which point we might as well just
  1005. * do a regular read() on the buffer anyway.
  1006. * - Lots of nasty vm tricks, that are neither fast nor flexible (it
  1007. * has restriction limitations on both ends of the pipe).
  1008. *
  1009. * Alas, it isn't here.
  1010. *
  1011. */
  1012. static long do_vmsplice(struct file *file, const struct iovec __user *iov,
  1013. unsigned long nr_segs, unsigned int flags)
  1014. {
  1015. struct pipe_inode_info *pipe = file->f_dentry->d_inode->i_pipe;
  1016. struct page *pages[PIPE_BUFFERS];
  1017. struct partial_page partial[PIPE_BUFFERS];
  1018. struct splice_pipe_desc spd = {
  1019. .pages = pages,
  1020. .partial = partial,
  1021. .flags = flags,
  1022. .ops = &user_page_pipe_buf_ops,
  1023. };
  1024. if (unlikely(!pipe))
  1025. return -EBADF;
  1026. if (unlikely(nr_segs > UIO_MAXIOV))
  1027. return -EINVAL;
  1028. else if (unlikely(!nr_segs))
  1029. return 0;
  1030. spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial);
  1031. if (spd.nr_pages <= 0)
  1032. return spd.nr_pages;
  1033. return splice_to_pipe(pipe, &spd);
  1034. }
  1035. asmlinkage long sys_vmsplice(int fd, const struct iovec __user *iov,
  1036. unsigned long nr_segs, unsigned int flags)
  1037. {
  1038. struct file *file;
  1039. long error;
  1040. int fput;
  1041. error = -EBADF;
  1042. file = fget_light(fd, &fput);
  1043. if (file) {
  1044. if (file->f_mode & FMODE_WRITE)
  1045. error = do_vmsplice(file, iov, nr_segs, flags);
  1046. fput_light(file, fput);
  1047. }
  1048. return error;
  1049. }
  1050. asmlinkage long sys_splice(int fd_in, loff_t __user *off_in,
  1051. int fd_out, loff_t __user *off_out,
  1052. size_t len, unsigned int flags)
  1053. {
  1054. long error;
  1055. struct file *in, *out;
  1056. int fput_in, fput_out;
  1057. if (unlikely(!len))
  1058. return 0;
  1059. error = -EBADF;
  1060. in = fget_light(fd_in, &fput_in);
  1061. if (in) {
  1062. if (in->f_mode & FMODE_READ) {
  1063. out = fget_light(fd_out, &fput_out);
  1064. if (out) {
  1065. if (out->f_mode & FMODE_WRITE)
  1066. error = do_splice(in, off_in,
  1067. out, off_out,
  1068. len, flags);
  1069. fput_light(out, fput_out);
  1070. }
  1071. }
  1072. fput_light(in, fput_in);
  1073. }
  1074. return error;
  1075. }
  1076. /*
  1077. * Link contents of ipipe to opipe.
  1078. */
  1079. static int link_pipe(struct pipe_inode_info *ipipe,
  1080. struct pipe_inode_info *opipe,
  1081. size_t len, unsigned int flags)
  1082. {
  1083. struct pipe_buffer *ibuf, *obuf;
  1084. int ret, do_wakeup, i, ipipe_first;
  1085. ret = do_wakeup = ipipe_first = 0;
  1086. /*
  1087. * Potential ABBA deadlock, work around it by ordering lock
  1088. * grabbing by inode address. Otherwise two different processes
  1089. * could deadlock (one doing tee from A -> B, the other from B -> A).
  1090. */
  1091. if (ipipe->inode < opipe->inode) {
  1092. ipipe_first = 1;
  1093. mutex_lock(&ipipe->inode->i_mutex);
  1094. mutex_lock(&opipe->inode->i_mutex);
  1095. } else {
  1096. mutex_lock(&opipe->inode->i_mutex);
  1097. mutex_lock(&ipipe->inode->i_mutex);
  1098. }
  1099. for (i = 0;; i++) {
  1100. if (!opipe->readers) {
  1101. send_sig(SIGPIPE, current, 0);
  1102. if (!ret)
  1103. ret = -EPIPE;
  1104. break;
  1105. }
  1106. if (ipipe->nrbufs - i) {
  1107. ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
  1108. /*
  1109. * If we have room, fill this buffer
  1110. */
  1111. if (opipe->nrbufs < PIPE_BUFFERS) {
  1112. int nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
  1113. /*
  1114. * Get a reference to this pipe buffer,
  1115. * so we can copy the contents over.
  1116. */
  1117. ibuf->ops->get(ipipe, ibuf);
  1118. obuf = opipe->bufs + nbuf;
  1119. *obuf = *ibuf;
  1120. if (obuf->len > len)
  1121. obuf->len = len;
  1122. opipe->nrbufs++;
  1123. do_wakeup = 1;
  1124. ret += obuf->len;
  1125. len -= obuf->len;
  1126. if (!len)
  1127. break;
  1128. if (opipe->nrbufs < PIPE_BUFFERS)
  1129. continue;
  1130. }
  1131. /*
  1132. * We have input available, but no output room.
  1133. * If we already copied data, return that. If we
  1134. * need to drop the opipe lock, it must be ordered
  1135. * last to avoid deadlocks.
  1136. */
  1137. if ((flags & SPLICE_F_NONBLOCK) || !ipipe_first) {
  1138. if (!ret)
  1139. ret = -EAGAIN;
  1140. break;
  1141. }
  1142. if (signal_pending(current)) {
  1143. if (!ret)
  1144. ret = -ERESTARTSYS;
  1145. break;
  1146. }
  1147. if (do_wakeup) {
  1148. smp_mb();
  1149. if (waitqueue_active(&opipe->wait))
  1150. wake_up_interruptible(&opipe->wait);
  1151. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  1152. do_wakeup = 0;
  1153. }
  1154. opipe->waiting_writers++;
  1155. pipe_wait(opipe);
  1156. opipe->waiting_writers--;
  1157. continue;
  1158. }
  1159. /*
  1160. * No input buffers, do the usual checks for available
  1161. * writers and blocking and wait if necessary
  1162. */
  1163. if (!ipipe->writers)
  1164. break;
  1165. if (!ipipe->waiting_writers) {
  1166. if (ret)
  1167. break;
  1168. }
  1169. /*
  1170. * pipe_wait() drops the ipipe mutex. To avoid deadlocks
  1171. * with another process, we can only safely do that if
  1172. * the ipipe lock is ordered last.
  1173. */
  1174. if ((flags & SPLICE_F_NONBLOCK) || ipipe_first) {
  1175. if (!ret)
  1176. ret = -EAGAIN;
  1177. break;
  1178. }
  1179. if (signal_pending(current)) {
  1180. if (!ret)
  1181. ret = -ERESTARTSYS;
  1182. break;
  1183. }
  1184. if (waitqueue_active(&ipipe->wait))
  1185. wake_up_interruptible_sync(&ipipe->wait);
  1186. kill_fasync(&ipipe->fasync_writers, SIGIO, POLL_OUT);
  1187. pipe_wait(ipipe);
  1188. }
  1189. mutex_unlock(&ipipe->inode->i_mutex);
  1190. mutex_unlock(&opipe->inode->i_mutex);
  1191. if (do_wakeup) {
  1192. smp_mb();
  1193. if (waitqueue_active(&opipe->wait))
  1194. wake_up_interruptible(&opipe->wait);
  1195. kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
  1196. }
  1197. return ret;
  1198. }
  1199. /*
  1200. * This is a tee(1) implementation that works on pipes. It doesn't copy
  1201. * any data, it simply references the 'in' pages on the 'out' pipe.
  1202. * The 'flags' used are the SPLICE_F_* variants, currently the only
  1203. * applicable one is SPLICE_F_NONBLOCK.
  1204. */
  1205. static long do_tee(struct file *in, struct file *out, size_t len,
  1206. unsigned int flags)
  1207. {
  1208. struct pipe_inode_info *ipipe = in->f_dentry->d_inode->i_pipe;
  1209. struct pipe_inode_info *opipe = out->f_dentry->d_inode->i_pipe;
  1210. /*
  1211. * Link ipipe to the two output pipes, consuming as we go along.
  1212. */
  1213. if (ipipe && opipe)
  1214. return link_pipe(ipipe, opipe, len, flags);
  1215. return -EINVAL;
  1216. }
  1217. asmlinkage long sys_tee(int fdin, int fdout, size_t len, unsigned int flags)
  1218. {
  1219. struct file *in;
  1220. int error, fput_in;
  1221. if (unlikely(!len))
  1222. return 0;
  1223. error = -EBADF;
  1224. in = fget_light(fdin, &fput_in);
  1225. if (in) {
  1226. if (in->f_mode & FMODE_READ) {
  1227. int fput_out;
  1228. struct file *out = fget_light(fdout, &fput_out);
  1229. if (out) {
  1230. if (out->f_mode & FMODE_WRITE)
  1231. error = do_tee(in, out, len, flags);
  1232. fput_light(out, fput_out);
  1233. }
  1234. }
  1235. fput_light(in, fput_in);
  1236. }
  1237. return error;
  1238. }