splice.c 33 KB

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