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