read_write.c 18 KB

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