read_write.c 16 KB

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