read_write.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*
  2. * linux/fs/read_write.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/stat.h>
  8. #include <linux/fcntl.h>
  9. #include <linux/file.h>
  10. #include <linux/uio.h>
  11. #include <linux/smp_lock.h>
  12. #include <linux/fsnotify.h>
  13. #include <linux/security.h>
  14. #include <linux/module.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/splice.h>
  18. #include "read_write.h"
  19. #include <asm/uaccess.h>
  20. #include <asm/unistd.h>
  21. const struct file_operations generic_ro_fops = {
  22. .llseek = generic_file_llseek,
  23. .read = do_sync_read,
  24. .aio_read = generic_file_aio_read,
  25. .mmap = generic_file_readonly_mmap,
  26. .splice_read = generic_file_splice_read,
  27. };
  28. EXPORT_SYMBOL(generic_ro_fops);
  29. /**
  30. * generic_file_llseek_unlocked - lockless generic llseek implementation
  31. * @file: file structure to seek on
  32. * @offset: file offset to seek to
  33. * @origin: type of seek
  34. *
  35. * Updates the file offset to the value specified by @offset and @origin.
  36. * Locking must be provided by the caller.
  37. */
  38. loff_t
  39. generic_file_llseek_unlocked(struct file *file, loff_t offset, int origin)
  40. {
  41. struct inode *inode = file->f_mapping->host;
  42. switch (origin) {
  43. case SEEK_END:
  44. offset += inode->i_size;
  45. break;
  46. case SEEK_CUR:
  47. /*
  48. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  49. * position-querying operation. Avoid rewriting the "same"
  50. * f_pos value back to the file because a concurrent read(),
  51. * write() or lseek() might have altered it
  52. */
  53. if (offset == 0)
  54. return file->f_pos;
  55. offset += file->f_pos;
  56. break;
  57. }
  58. if (offset < 0 || offset > inode->i_sb->s_maxbytes)
  59. return -EINVAL;
  60. /* Special lock needed here? */
  61. if (offset != file->f_pos) {
  62. file->f_pos = offset;
  63. file->f_version = 0;
  64. }
  65. return offset;
  66. }
  67. EXPORT_SYMBOL(generic_file_llseek_unlocked);
  68. /**
  69. * generic_file_llseek - generic llseek implementation for regular files
  70. * @file: file structure to seek on
  71. * @offset: file offset to seek to
  72. * @origin: type of seek
  73. *
  74. * This is a generic implemenation of ->llseek useable for all normal local
  75. * filesystems. It just updates the file offset to the value specified by
  76. * @offset and @origin under i_mutex.
  77. */
  78. loff_t generic_file_llseek(struct file *file, loff_t offset, int origin)
  79. {
  80. loff_t rval;
  81. mutex_lock(&file->f_dentry->d_inode->i_mutex);
  82. rval = generic_file_llseek_unlocked(file, offset, origin);
  83. mutex_unlock(&file->f_dentry->d_inode->i_mutex);
  84. return rval;
  85. }
  86. EXPORT_SYMBOL(generic_file_llseek);
  87. loff_t no_llseek(struct file *file, loff_t offset, int origin)
  88. {
  89. return -ESPIPE;
  90. }
  91. EXPORT_SYMBOL(no_llseek);
  92. loff_t default_llseek(struct file *file, loff_t offset, int origin)
  93. {
  94. loff_t retval;
  95. lock_kernel();
  96. switch (origin) {
  97. case SEEK_END:
  98. offset += i_size_read(file->f_path.dentry->d_inode);
  99. break;
  100. case SEEK_CUR:
  101. if (offset == 0) {
  102. retval = file->f_pos;
  103. goto out;
  104. }
  105. offset += file->f_pos;
  106. }
  107. retval = -EINVAL;
  108. if (offset >= 0) {
  109. if (offset != file->f_pos) {
  110. file->f_pos = offset;
  111. file->f_version = 0;
  112. }
  113. retval = offset;
  114. }
  115. out:
  116. unlock_kernel();
  117. return retval;
  118. }
  119. EXPORT_SYMBOL(default_llseek);
  120. loff_t vfs_llseek(struct file *file, loff_t offset, int origin)
  121. {
  122. loff_t (*fn)(struct file *, loff_t, int);
  123. fn = no_llseek;
  124. if (file->f_mode & FMODE_LSEEK) {
  125. fn = default_llseek;
  126. if (file->f_op && file->f_op->llseek)
  127. fn = file->f_op->llseek;
  128. }
  129. return fn(file, offset, origin);
  130. }
  131. EXPORT_SYMBOL(vfs_llseek);
  132. asmlinkage off_t sys_lseek(unsigned int fd, off_t offset, unsigned int origin)
  133. {
  134. off_t retval;
  135. struct file * file;
  136. int fput_needed;
  137. retval = -EBADF;
  138. file = fget_light(fd, &fput_needed);
  139. if (!file)
  140. goto bad;
  141. retval = -EINVAL;
  142. if (origin <= SEEK_MAX) {
  143. loff_t res = vfs_llseek(file, offset, origin);
  144. retval = res;
  145. if (res != (loff_t)retval)
  146. retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
  147. }
  148. fput_light(file, fput_needed);
  149. bad:
  150. return retval;
  151. }
  152. #ifdef __ARCH_WANT_SYS_LLSEEK
  153. asmlinkage long sys_llseek(unsigned int fd, unsigned long offset_high,
  154. unsigned long offset_low, loff_t __user * result,
  155. unsigned int origin)
  156. {
  157. int retval;
  158. struct file * file;
  159. loff_t offset;
  160. int fput_needed;
  161. retval = -EBADF;
  162. file = fget_light(fd, &fput_needed);
  163. if (!file)
  164. goto bad;
  165. retval = -EINVAL;
  166. if (origin > SEEK_MAX)
  167. goto out_putf;
  168. offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low,
  169. origin);
  170. retval = (int)offset;
  171. if (offset >= 0) {
  172. retval = -EFAULT;
  173. if (!copy_to_user(result, &offset, sizeof(offset)))
  174. retval = 0;
  175. }
  176. out_putf:
  177. fput_light(file, fput_needed);
  178. bad:
  179. return retval;
  180. }
  181. #endif
  182. /*
  183. * rw_verify_area doesn't like huge counts. We limit
  184. * them to something that fits in "int" so that others
  185. * won't have to do range checks all the time.
  186. */
  187. #define MAX_RW_COUNT (INT_MAX & PAGE_CACHE_MASK)
  188. int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
  189. {
  190. struct inode *inode;
  191. loff_t pos;
  192. int retval = -EINVAL;
  193. inode = file->f_path.dentry->d_inode;
  194. if (unlikely((ssize_t) count < 0))
  195. return retval;
  196. pos = *ppos;
  197. if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
  198. return retval;
  199. if (unlikely(inode->i_flock && mandatory_lock(inode))) {
  200. retval = locks_mandatory_area(
  201. read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
  202. inode, file, pos, count);
  203. if (retval < 0)
  204. return retval;
  205. }
  206. retval = security_file_permission(file,
  207. read_write == READ ? MAY_READ : MAY_WRITE);
  208. if (retval)
  209. return retval;
  210. return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
  211. }
  212. static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
  213. {
  214. set_current_state(TASK_UNINTERRUPTIBLE);
  215. if (!kiocbIsKicked(iocb))
  216. schedule();
  217. else
  218. kiocbClearKicked(iocb);
  219. __set_current_state(TASK_RUNNING);
  220. }
  221. ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
  222. {
  223. struct iovec iov = { .iov_base = buf, .iov_len = len };
  224. struct kiocb kiocb;
  225. ssize_t ret;
  226. init_sync_kiocb(&kiocb, filp);
  227. kiocb.ki_pos = *ppos;
  228. kiocb.ki_left = len;
  229. for (;;) {
  230. ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
  231. if (ret != -EIOCBRETRY)
  232. break;
  233. wait_on_retry_sync_kiocb(&kiocb);
  234. }
  235. if (-EIOCBQUEUED == ret)
  236. ret = wait_on_sync_kiocb(&kiocb);
  237. *ppos = kiocb.ki_pos;
  238. return ret;
  239. }
  240. EXPORT_SYMBOL(do_sync_read);
  241. ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
  242. {
  243. ssize_t ret;
  244. if (!(file->f_mode & FMODE_READ))
  245. return -EBADF;
  246. if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
  247. return -EINVAL;
  248. if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
  249. return -EFAULT;
  250. ret = rw_verify_area(READ, file, pos, count);
  251. if (ret >= 0) {
  252. count = ret;
  253. if (file->f_op->read)
  254. ret = file->f_op->read(file, buf, count, pos);
  255. else
  256. ret = do_sync_read(file, buf, count, pos);
  257. if (ret > 0) {
  258. fsnotify_access(file->f_path.dentry);
  259. add_rchar(current, ret);
  260. }
  261. inc_syscr(current);
  262. }
  263. return ret;
  264. }
  265. EXPORT_SYMBOL(vfs_read);
  266. ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
  267. {
  268. struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
  269. struct kiocb kiocb;
  270. ssize_t ret;
  271. init_sync_kiocb(&kiocb, filp);
  272. kiocb.ki_pos = *ppos;
  273. kiocb.ki_left = len;
  274. for (;;) {
  275. ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
  276. if (ret != -EIOCBRETRY)
  277. break;
  278. wait_on_retry_sync_kiocb(&kiocb);
  279. }
  280. if (-EIOCBQUEUED == ret)
  281. ret = wait_on_sync_kiocb(&kiocb);
  282. *ppos = kiocb.ki_pos;
  283. return ret;
  284. }
  285. EXPORT_SYMBOL(do_sync_write);
  286. ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
  287. {
  288. ssize_t ret;
  289. if (!(file->f_mode & FMODE_WRITE))
  290. return -EBADF;
  291. if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
  292. return -EINVAL;
  293. if (unlikely(!access_ok(VERIFY_READ, buf, count)))
  294. return -EFAULT;
  295. ret = rw_verify_area(WRITE, file, pos, count);
  296. if (ret >= 0) {
  297. count = ret;
  298. if (file->f_op->write)
  299. ret = file->f_op->write(file, buf, count, pos);
  300. else
  301. ret = do_sync_write(file, buf, count, pos);
  302. if (ret > 0) {
  303. fsnotify_modify(file->f_path.dentry);
  304. add_wchar(current, ret);
  305. }
  306. inc_syscw(current);
  307. }
  308. return ret;
  309. }
  310. EXPORT_SYMBOL(vfs_write);
  311. static inline loff_t file_pos_read(struct file *file)
  312. {
  313. return file->f_pos;
  314. }
  315. static inline void file_pos_write(struct file *file, loff_t pos)
  316. {
  317. file->f_pos = pos;
  318. }
  319. asmlinkage ssize_t sys_read(unsigned int fd, char __user * buf, size_t count)
  320. {
  321. struct file *file;
  322. ssize_t ret = -EBADF;
  323. int fput_needed;
  324. file = fget_light(fd, &fput_needed);
  325. if (file) {
  326. loff_t pos = file_pos_read(file);
  327. ret = vfs_read(file, buf, count, &pos);
  328. file_pos_write(file, pos);
  329. fput_light(file, fput_needed);
  330. }
  331. return ret;
  332. }
  333. asmlinkage ssize_t sys_write(unsigned int fd, const char __user * buf, size_t count)
  334. {
  335. struct file *file;
  336. ssize_t ret = -EBADF;
  337. int fput_needed;
  338. file = fget_light(fd, &fput_needed);
  339. if (file) {
  340. loff_t pos = file_pos_read(file);
  341. ret = vfs_write(file, buf, count, &pos);
  342. file_pos_write(file, pos);
  343. fput_light(file, fput_needed);
  344. }
  345. return ret;
  346. }
  347. asmlinkage ssize_t sys_pread64(unsigned int fd, char __user *buf,
  348. size_t count, loff_t pos)
  349. {
  350. struct file *file;
  351. ssize_t ret = -EBADF;
  352. int fput_needed;
  353. if (pos < 0)
  354. return -EINVAL;
  355. file = fget_light(fd, &fput_needed);
  356. if (file) {
  357. ret = -ESPIPE;
  358. if (file->f_mode & FMODE_PREAD)
  359. ret = vfs_read(file, buf, count, &pos);
  360. fput_light(file, fput_needed);
  361. }
  362. return ret;
  363. }
  364. asmlinkage ssize_t sys_pwrite64(unsigned int fd, const char __user *buf,
  365. size_t count, loff_t pos)
  366. {
  367. struct file *file;
  368. ssize_t ret = -EBADF;
  369. int fput_needed;
  370. if (pos < 0)
  371. return -EINVAL;
  372. file = fget_light(fd, &fput_needed);
  373. if (file) {
  374. ret = -ESPIPE;
  375. if (file->f_mode & FMODE_PWRITE)
  376. ret = vfs_write(file, buf, count, &pos);
  377. fput_light(file, fput_needed);
  378. }
  379. return ret;
  380. }
  381. /*
  382. * Reduce an iovec's length in-place. Return the resulting number of segments
  383. */
  384. unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
  385. {
  386. unsigned long seg = 0;
  387. size_t len = 0;
  388. while (seg < nr_segs) {
  389. seg++;
  390. if (len + iov->iov_len >= to) {
  391. iov->iov_len = to - len;
  392. break;
  393. }
  394. len += iov->iov_len;
  395. iov++;
  396. }
  397. return seg;
  398. }
  399. EXPORT_SYMBOL(iov_shorten);
  400. ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
  401. unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
  402. {
  403. struct kiocb kiocb;
  404. ssize_t ret;
  405. init_sync_kiocb(&kiocb, filp);
  406. kiocb.ki_pos = *ppos;
  407. kiocb.ki_left = len;
  408. kiocb.ki_nbytes = len;
  409. for (;;) {
  410. ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
  411. if (ret != -EIOCBRETRY)
  412. break;
  413. wait_on_retry_sync_kiocb(&kiocb);
  414. }
  415. if (ret == -EIOCBQUEUED)
  416. ret = wait_on_sync_kiocb(&kiocb);
  417. *ppos = kiocb.ki_pos;
  418. return ret;
  419. }
  420. /* Do it by hand, with file-ops */
  421. ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
  422. unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
  423. {
  424. struct iovec *vector = iov;
  425. ssize_t ret = 0;
  426. while (nr_segs > 0) {
  427. void __user *base;
  428. size_t len;
  429. ssize_t nr;
  430. base = vector->iov_base;
  431. len = vector->iov_len;
  432. vector++;
  433. nr_segs--;
  434. nr = fn(filp, base, len, ppos);
  435. if (nr < 0) {
  436. if (!ret)
  437. ret = nr;
  438. break;
  439. }
  440. ret += nr;
  441. if (nr != len)
  442. break;
  443. }
  444. return ret;
  445. }
  446. /* A write operation does a read from user space and vice versa */
  447. #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
  448. ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
  449. unsigned long nr_segs, unsigned long fast_segs,
  450. struct iovec *fast_pointer,
  451. struct iovec **ret_pointer)
  452. {
  453. unsigned long seg;
  454. ssize_t ret;
  455. struct iovec *iov = fast_pointer;
  456. /*
  457. * SuS says "The readv() function *may* fail if the iovcnt argument
  458. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  459. * traditionally returned zero for zero segments, so...
  460. */
  461. if (nr_segs == 0) {
  462. ret = 0;
  463. goto out;
  464. }
  465. /*
  466. * First get the "struct iovec" from user memory and
  467. * verify all the pointers
  468. */
  469. if (nr_segs > UIO_MAXIOV) {
  470. ret = -EINVAL;
  471. goto out;
  472. }
  473. if (nr_segs > fast_segs) {
  474. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  475. if (iov == NULL) {
  476. ret = -ENOMEM;
  477. goto out;
  478. }
  479. }
  480. if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
  481. ret = -EFAULT;
  482. goto out;
  483. }
  484. /*
  485. * According to the Single Unix Specification we should return EINVAL
  486. * if an element length is < 0 when cast to ssize_t or if the
  487. * total length would overflow the ssize_t return value of the
  488. * system call.
  489. */
  490. ret = 0;
  491. for (seg = 0; seg < nr_segs; seg++) {
  492. void __user *buf = iov[seg].iov_base;
  493. ssize_t len = (ssize_t)iov[seg].iov_len;
  494. /* see if we we're about to use an invalid len or if
  495. * it's about to overflow ssize_t */
  496. if (len < 0 || (ret + len < ret)) {
  497. ret = -EINVAL;
  498. goto out;
  499. }
  500. if (unlikely(!access_ok(vrfy_dir(type), buf, len))) {
  501. ret = -EFAULT;
  502. goto out;
  503. }
  504. ret += len;
  505. }
  506. out:
  507. *ret_pointer = iov;
  508. return ret;
  509. }
  510. static ssize_t do_readv_writev(int type, struct file *file,
  511. const struct iovec __user * uvector,
  512. unsigned long nr_segs, loff_t *pos)
  513. {
  514. size_t tot_len;
  515. struct iovec iovstack[UIO_FASTIOV];
  516. struct iovec *iov = iovstack;
  517. ssize_t ret;
  518. io_fn_t fn;
  519. iov_fn_t fnv;
  520. if (!file->f_op) {
  521. ret = -EINVAL;
  522. goto out;
  523. }
  524. ret = rw_copy_check_uvector(type, uvector, nr_segs,
  525. ARRAY_SIZE(iovstack), iovstack, &iov);
  526. if (ret <= 0)
  527. goto out;
  528. tot_len = ret;
  529. ret = rw_verify_area(type, file, pos, tot_len);
  530. if (ret < 0)
  531. goto out;
  532. fnv = NULL;
  533. if (type == READ) {
  534. fn = file->f_op->read;
  535. fnv = file->f_op->aio_read;
  536. } else {
  537. fn = (io_fn_t)file->f_op->write;
  538. fnv = file->f_op->aio_write;
  539. }
  540. if (fnv)
  541. ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
  542. pos, fnv);
  543. else
  544. ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
  545. out:
  546. if (iov != iovstack)
  547. kfree(iov);
  548. if ((ret + (type == READ)) > 0) {
  549. if (type == READ)
  550. fsnotify_access(file->f_path.dentry);
  551. else
  552. fsnotify_modify(file->f_path.dentry);
  553. }
  554. return ret;
  555. }
  556. ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
  557. unsigned long vlen, loff_t *pos)
  558. {
  559. if (!(file->f_mode & FMODE_READ))
  560. return -EBADF;
  561. if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
  562. return -EINVAL;
  563. return do_readv_writev(READ, file, vec, vlen, pos);
  564. }
  565. EXPORT_SYMBOL(vfs_readv);
  566. ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
  567. unsigned long vlen, loff_t *pos)
  568. {
  569. if (!(file->f_mode & FMODE_WRITE))
  570. return -EBADF;
  571. if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
  572. return -EINVAL;
  573. return do_readv_writev(WRITE, file, vec, vlen, pos);
  574. }
  575. EXPORT_SYMBOL(vfs_writev);
  576. asmlinkage ssize_t
  577. sys_readv(unsigned long fd, const struct iovec __user *vec, unsigned long vlen)
  578. {
  579. struct file *file;
  580. ssize_t ret = -EBADF;
  581. int fput_needed;
  582. file = fget_light(fd, &fput_needed);
  583. if (file) {
  584. loff_t pos = file_pos_read(file);
  585. ret = vfs_readv(file, vec, vlen, &pos);
  586. file_pos_write(file, pos);
  587. fput_light(file, fput_needed);
  588. }
  589. if (ret > 0)
  590. add_rchar(current, ret);
  591. inc_syscr(current);
  592. return ret;
  593. }
  594. asmlinkage ssize_t
  595. sys_writev(unsigned long fd, const struct iovec __user *vec, unsigned long vlen)
  596. {
  597. struct file *file;
  598. ssize_t ret = -EBADF;
  599. int fput_needed;
  600. file = fget_light(fd, &fput_needed);
  601. if (file) {
  602. loff_t pos = file_pos_read(file);
  603. ret = vfs_writev(file, vec, vlen, &pos);
  604. file_pos_write(file, pos);
  605. fput_light(file, fput_needed);
  606. }
  607. if (ret > 0)
  608. add_wchar(current, ret);
  609. inc_syscw(current);
  610. return ret;
  611. }
  612. static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
  613. size_t count, loff_t max)
  614. {
  615. struct file * in_file, * out_file;
  616. struct inode * in_inode, * out_inode;
  617. loff_t pos;
  618. ssize_t retval;
  619. int fput_needed_in, fput_needed_out, fl;
  620. /*
  621. * Get input file, and verify that it is ok..
  622. */
  623. retval = -EBADF;
  624. in_file = fget_light(in_fd, &fput_needed_in);
  625. if (!in_file)
  626. goto out;
  627. if (!(in_file->f_mode & FMODE_READ))
  628. goto fput_in;
  629. retval = -EINVAL;
  630. in_inode = in_file->f_path.dentry->d_inode;
  631. if (!in_inode)
  632. goto fput_in;
  633. if (!in_file->f_op || !in_file->f_op->splice_read)
  634. goto fput_in;
  635. retval = -ESPIPE;
  636. if (!ppos)
  637. ppos = &in_file->f_pos;
  638. else
  639. if (!(in_file->f_mode & FMODE_PREAD))
  640. goto fput_in;
  641. retval = rw_verify_area(READ, in_file, ppos, count);
  642. if (retval < 0)
  643. goto fput_in;
  644. count = retval;
  645. /*
  646. * Get output file, and verify that it is ok..
  647. */
  648. retval = -EBADF;
  649. out_file = fget_light(out_fd, &fput_needed_out);
  650. if (!out_file)
  651. goto fput_in;
  652. if (!(out_file->f_mode & FMODE_WRITE))
  653. goto fput_out;
  654. retval = -EINVAL;
  655. if (!out_file->f_op || !out_file->f_op->sendpage)
  656. goto fput_out;
  657. out_inode = out_file->f_path.dentry->d_inode;
  658. retval = rw_verify_area(WRITE, out_file, &out_file->f_pos, count);
  659. if (retval < 0)
  660. goto fput_out;
  661. count = retval;
  662. if (!max)
  663. max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
  664. pos = *ppos;
  665. retval = -EINVAL;
  666. if (unlikely(pos < 0))
  667. goto fput_out;
  668. if (unlikely(pos + count > max)) {
  669. retval = -EOVERFLOW;
  670. if (pos >= max)
  671. goto fput_out;
  672. count = max - pos;
  673. }
  674. fl = 0;
  675. #if 0
  676. /*
  677. * We need to debate whether we can enable this or not. The
  678. * man page documents EAGAIN return for the output at least,
  679. * and the application is arguably buggy if it doesn't expect
  680. * EAGAIN on a non-blocking file descriptor.
  681. */
  682. if (in_file->f_flags & O_NONBLOCK)
  683. fl = SPLICE_F_NONBLOCK;
  684. #endif
  685. retval = do_splice_direct(in_file, ppos, out_file, count, fl);
  686. if (retval > 0) {
  687. add_rchar(current, retval);
  688. add_wchar(current, retval);
  689. }
  690. inc_syscr(current);
  691. inc_syscw(current);
  692. if (*ppos > max)
  693. retval = -EOVERFLOW;
  694. fput_out:
  695. fput_light(out_file, fput_needed_out);
  696. fput_in:
  697. fput_light(in_file, fput_needed_in);
  698. out:
  699. return retval;
  700. }
  701. asmlinkage ssize_t sys_sendfile(int out_fd, int in_fd, off_t __user *offset, size_t count)
  702. {
  703. loff_t pos;
  704. off_t off;
  705. ssize_t ret;
  706. if (offset) {
  707. if (unlikely(get_user(off, offset)))
  708. return -EFAULT;
  709. pos = off;
  710. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  711. if (unlikely(put_user(pos, offset)))
  712. return -EFAULT;
  713. return ret;
  714. }
  715. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  716. }
  717. asmlinkage ssize_t sys_sendfile64(int out_fd, int in_fd, loff_t __user *offset, size_t count)
  718. {
  719. loff_t pos;
  720. ssize_t ret;
  721. if (offset) {
  722. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  723. return -EFAULT;
  724. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  725. if (unlikely(put_user(pos, offset)))
  726. return -EFAULT;
  727. return ret;
  728. }
  729. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  730. }