read_write.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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_path.dentry->d_inode);
  59. break;
  60. case 1:
  61. offset += file->f_pos;
  62. }
  63. retval = -EINVAL;
  64. if (offset>=0 && offset<=file->f_path.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_path.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. inode = file->f_path.dentry->d_inode;
  176. if (unlikely((ssize_t) count < 0))
  177. goto Einval;
  178. pos = *ppos;
  179. if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
  180. goto Einval;
  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_path.dentry);
  241. add_rchar(current, ret);
  242. }
  243. inc_syscr(current);
  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_path.dentry);
  289. add_wchar(current, ret);
  290. }
  291. inc_syscw(current);
  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. ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
  387. unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
  388. {
  389. struct kiocb kiocb;
  390. ssize_t ret;
  391. init_sync_kiocb(&kiocb, filp);
  392. kiocb.ki_pos = *ppos;
  393. kiocb.ki_left = len;
  394. kiocb.ki_nbytes = len;
  395. for (;;) {
  396. ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
  397. if (ret != -EIOCBRETRY)
  398. break;
  399. wait_on_retry_sync_kiocb(&kiocb);
  400. }
  401. if (ret == -EIOCBQUEUED)
  402. ret = wait_on_sync_kiocb(&kiocb);
  403. *ppos = kiocb.ki_pos;
  404. return ret;
  405. }
  406. /* Do it by hand, with file-ops */
  407. ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
  408. unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
  409. {
  410. struct iovec *vector = iov;
  411. ssize_t ret = 0;
  412. while (nr_segs > 0) {
  413. void __user *base;
  414. size_t len;
  415. ssize_t nr;
  416. base = vector->iov_base;
  417. len = vector->iov_len;
  418. vector++;
  419. nr_segs--;
  420. nr = fn(filp, base, len, ppos);
  421. if (nr < 0) {
  422. if (!ret)
  423. ret = nr;
  424. break;
  425. }
  426. ret += nr;
  427. if (nr != len)
  428. break;
  429. }
  430. return ret;
  431. }
  432. /* A write operation does a read from user space and vice versa */
  433. #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
  434. ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
  435. unsigned long nr_segs, unsigned long fast_segs,
  436. struct iovec *fast_pointer,
  437. struct iovec **ret_pointer)
  438. {
  439. unsigned long seg;
  440. ssize_t ret;
  441. struct iovec *iov = fast_pointer;
  442. /*
  443. * SuS says "The readv() function *may* fail if the iovcnt argument
  444. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  445. * traditionally returned zero for zero segments, so...
  446. */
  447. if (nr_segs == 0) {
  448. ret = 0;
  449. goto out;
  450. }
  451. /*
  452. * First get the "struct iovec" from user memory and
  453. * verify all the pointers
  454. */
  455. if (nr_segs > UIO_MAXIOV) {
  456. ret = -EINVAL;
  457. goto out;
  458. }
  459. if (nr_segs > fast_segs) {
  460. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  461. if (iov == NULL) {
  462. ret = -ENOMEM;
  463. goto out;
  464. }
  465. }
  466. if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
  467. ret = -EFAULT;
  468. goto out;
  469. }
  470. /*
  471. * According to the Single Unix Specification we should return EINVAL
  472. * if an element length is < 0 when cast to ssize_t or if the
  473. * total length would overflow the ssize_t return value of the
  474. * system call.
  475. */
  476. ret = 0;
  477. for (seg = 0; seg < nr_segs; seg++) {
  478. void __user *buf = iov[seg].iov_base;
  479. ssize_t len = (ssize_t)iov[seg].iov_len;
  480. /* see if we we're about to use an invalid len or if
  481. * it's about to overflow ssize_t */
  482. if (len < 0 || (ret + len < ret)) {
  483. ret = -EINVAL;
  484. goto out;
  485. }
  486. if (unlikely(!access_ok(vrfy_dir(type), buf, len))) {
  487. ret = -EFAULT;
  488. goto out;
  489. }
  490. ret += len;
  491. }
  492. out:
  493. *ret_pointer = iov;
  494. return ret;
  495. }
  496. static ssize_t do_readv_writev(int type, struct file *file,
  497. const struct iovec __user * uvector,
  498. unsigned long nr_segs, loff_t *pos)
  499. {
  500. size_t tot_len;
  501. struct iovec iovstack[UIO_FASTIOV];
  502. struct iovec *iov = iovstack;
  503. ssize_t ret;
  504. io_fn_t fn;
  505. iov_fn_t fnv;
  506. if (!file->f_op) {
  507. ret = -EINVAL;
  508. goto out;
  509. }
  510. ret = rw_copy_check_uvector(type, uvector, nr_segs,
  511. ARRAY_SIZE(iovstack), iovstack, &iov);
  512. if (ret <= 0)
  513. goto out;
  514. tot_len = ret;
  515. ret = rw_verify_area(type, file, pos, tot_len);
  516. if (ret < 0)
  517. goto out;
  518. ret = security_file_permission(file, type == READ ? MAY_READ : MAY_WRITE);
  519. if (ret)
  520. goto out;
  521. fnv = NULL;
  522. if (type == READ) {
  523. fn = file->f_op->read;
  524. fnv = file->f_op->aio_read;
  525. } else {
  526. fn = (io_fn_t)file->f_op->write;
  527. fnv = file->f_op->aio_write;
  528. }
  529. if (fnv)
  530. ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
  531. pos, fnv);
  532. else
  533. ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
  534. out:
  535. if (iov != iovstack)
  536. kfree(iov);
  537. if ((ret + (type == READ)) > 0) {
  538. if (type == READ)
  539. fsnotify_access(file->f_path.dentry);
  540. else
  541. fsnotify_modify(file->f_path.dentry);
  542. }
  543. return ret;
  544. }
  545. ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
  546. unsigned long vlen, loff_t *pos)
  547. {
  548. if (!(file->f_mode & FMODE_READ))
  549. return -EBADF;
  550. if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
  551. return -EINVAL;
  552. return do_readv_writev(READ, file, vec, vlen, pos);
  553. }
  554. EXPORT_SYMBOL(vfs_readv);
  555. ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
  556. unsigned long vlen, loff_t *pos)
  557. {
  558. if (!(file->f_mode & FMODE_WRITE))
  559. return -EBADF;
  560. if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
  561. return -EINVAL;
  562. return do_readv_writev(WRITE, file, vec, vlen, pos);
  563. }
  564. EXPORT_SYMBOL(vfs_writev);
  565. asmlinkage ssize_t
  566. sys_readv(unsigned long fd, const struct iovec __user *vec, unsigned long vlen)
  567. {
  568. struct file *file;
  569. ssize_t ret = -EBADF;
  570. int fput_needed;
  571. file = fget_light(fd, &fput_needed);
  572. if (file) {
  573. loff_t pos = file_pos_read(file);
  574. ret = vfs_readv(file, vec, vlen, &pos);
  575. file_pos_write(file, pos);
  576. fput_light(file, fput_needed);
  577. }
  578. if (ret > 0)
  579. add_rchar(current, ret);
  580. inc_syscr(current);
  581. return ret;
  582. }
  583. asmlinkage ssize_t
  584. sys_writev(unsigned long fd, const struct iovec __user *vec, unsigned long vlen)
  585. {
  586. struct file *file;
  587. ssize_t ret = -EBADF;
  588. int fput_needed;
  589. file = fget_light(fd, &fput_needed);
  590. if (file) {
  591. loff_t pos = file_pos_read(file);
  592. ret = vfs_writev(file, vec, vlen, &pos);
  593. file_pos_write(file, pos);
  594. fput_light(file, fput_needed);
  595. }
  596. if (ret > 0)
  597. add_wchar(current, ret);
  598. inc_syscw(current);
  599. return ret;
  600. }
  601. static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
  602. size_t count, loff_t max)
  603. {
  604. struct file * in_file, * out_file;
  605. struct inode * in_inode, * out_inode;
  606. loff_t pos;
  607. ssize_t retval;
  608. int fput_needed_in, fput_needed_out;
  609. /*
  610. * Get input file, and verify that it is ok..
  611. */
  612. retval = -EBADF;
  613. in_file = fget_light(in_fd, &fput_needed_in);
  614. if (!in_file)
  615. goto out;
  616. if (!(in_file->f_mode & FMODE_READ))
  617. goto fput_in;
  618. retval = -EINVAL;
  619. in_inode = in_file->f_path.dentry->d_inode;
  620. if (!in_inode)
  621. goto fput_in;
  622. if (!in_file->f_op || !in_file->f_op->sendfile)
  623. goto fput_in;
  624. retval = -ESPIPE;
  625. if (!ppos)
  626. ppos = &in_file->f_pos;
  627. else
  628. if (!(in_file->f_mode & FMODE_PREAD))
  629. goto fput_in;
  630. retval = rw_verify_area(READ, in_file, ppos, count);
  631. if (retval < 0)
  632. goto fput_in;
  633. count = retval;
  634. retval = security_file_permission (in_file, MAY_READ);
  635. if (retval)
  636. goto fput_in;
  637. /*
  638. * Get output file, and verify that it is ok..
  639. */
  640. retval = -EBADF;
  641. out_file = fget_light(out_fd, &fput_needed_out);
  642. if (!out_file)
  643. goto fput_in;
  644. if (!(out_file->f_mode & FMODE_WRITE))
  645. goto fput_out;
  646. retval = -EINVAL;
  647. if (!out_file->f_op || !out_file->f_op->sendpage)
  648. goto fput_out;
  649. out_inode = out_file->f_path.dentry->d_inode;
  650. retval = rw_verify_area(WRITE, out_file, &out_file->f_pos, count);
  651. if (retval < 0)
  652. goto fput_out;
  653. count = retval;
  654. retval = security_file_permission (out_file, MAY_WRITE);
  655. if (retval)
  656. goto fput_out;
  657. if (!max)
  658. max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
  659. pos = *ppos;
  660. retval = -EINVAL;
  661. if (unlikely(pos < 0))
  662. goto fput_out;
  663. if (unlikely(pos + count > max)) {
  664. retval = -EOVERFLOW;
  665. if (pos >= max)
  666. goto fput_out;
  667. count = max - pos;
  668. }
  669. retval = in_file->f_op->sendfile(in_file, ppos, count, file_send_actor, out_file);
  670. if (retval > 0) {
  671. add_rchar(current, retval);
  672. add_wchar(current, retval);
  673. }
  674. inc_syscr(current);
  675. inc_syscw(current);
  676. if (*ppos > max)
  677. retval = -EOVERFLOW;
  678. fput_out:
  679. fput_light(out_file, fput_needed_out);
  680. fput_in:
  681. fput_light(in_file, fput_needed_in);
  682. out:
  683. return retval;
  684. }
  685. asmlinkage ssize_t sys_sendfile(int out_fd, int in_fd, off_t __user *offset, size_t count)
  686. {
  687. loff_t pos;
  688. off_t off;
  689. ssize_t ret;
  690. if (offset) {
  691. if (unlikely(get_user(off, offset)))
  692. return -EFAULT;
  693. pos = off;
  694. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  695. if (unlikely(put_user(pos, offset)))
  696. return -EFAULT;
  697. return ret;
  698. }
  699. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  700. }
  701. asmlinkage ssize_t sys_sendfile64(int out_fd, int in_fd, loff_t __user *offset, size_t count)
  702. {
  703. loff_t pos;
  704. ssize_t ret;
  705. if (offset) {
  706. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  707. return -EFAULT;
  708. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  709. if (unlikely(put_user(pos, offset)))
  710. return -EFAULT;
  711. return ret;
  712. }
  713. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  714. }