read_write.c 19 KB

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