read_write.c 17 KB

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