read_write.c 18 KB

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