read_write.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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. SYSCALL_DEFINE3(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. SYSCALL_DEFINE5(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. kiocb.ki_nbytes = len;
  230. for (;;) {
  231. ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
  232. if (ret != -EIOCBRETRY)
  233. break;
  234. wait_on_retry_sync_kiocb(&kiocb);
  235. }
  236. if (-EIOCBQUEUED == ret)
  237. ret = wait_on_sync_kiocb(&kiocb);
  238. *ppos = kiocb.ki_pos;
  239. return ret;
  240. }
  241. EXPORT_SYMBOL(do_sync_read);
  242. ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
  243. {
  244. ssize_t ret;
  245. if (!(file->f_mode & FMODE_READ))
  246. return -EBADF;
  247. if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
  248. return -EINVAL;
  249. if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
  250. return -EFAULT;
  251. ret = rw_verify_area(READ, file, pos, count);
  252. if (ret >= 0) {
  253. count = ret;
  254. if (file->f_op->read)
  255. ret = file->f_op->read(file, buf, count, pos);
  256. else
  257. ret = do_sync_read(file, buf, count, pos);
  258. if (ret > 0) {
  259. fsnotify_access(file->f_path.dentry);
  260. add_rchar(current, ret);
  261. }
  262. inc_syscr(current);
  263. }
  264. return ret;
  265. }
  266. EXPORT_SYMBOL(vfs_read);
  267. ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
  268. {
  269. struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
  270. struct kiocb kiocb;
  271. ssize_t ret;
  272. init_sync_kiocb(&kiocb, filp);
  273. kiocb.ki_pos = *ppos;
  274. kiocb.ki_left = len;
  275. kiocb.ki_nbytes = len;
  276. for (;;) {
  277. ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
  278. if (ret != -EIOCBRETRY)
  279. break;
  280. wait_on_retry_sync_kiocb(&kiocb);
  281. }
  282. if (-EIOCBQUEUED == ret)
  283. ret = wait_on_sync_kiocb(&kiocb);
  284. *ppos = kiocb.ki_pos;
  285. return ret;
  286. }
  287. EXPORT_SYMBOL(do_sync_write);
  288. ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
  289. {
  290. ssize_t ret;
  291. if (!(file->f_mode & FMODE_WRITE))
  292. return -EBADF;
  293. if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
  294. return -EINVAL;
  295. if (unlikely(!access_ok(VERIFY_READ, buf, count)))
  296. return -EFAULT;
  297. ret = rw_verify_area(WRITE, file, pos, count);
  298. if (ret >= 0) {
  299. count = ret;
  300. if (file->f_op->write)
  301. ret = file->f_op->write(file, buf, count, pos);
  302. else
  303. ret = do_sync_write(file, buf, count, pos);
  304. if (ret > 0) {
  305. fsnotify_modify(file->f_path.dentry);
  306. add_wchar(current, ret);
  307. }
  308. inc_syscw(current);
  309. }
  310. return ret;
  311. }
  312. EXPORT_SYMBOL(vfs_write);
  313. static inline loff_t file_pos_read(struct file *file)
  314. {
  315. return file->f_pos;
  316. }
  317. static inline void file_pos_write(struct file *file, loff_t pos)
  318. {
  319. file->f_pos = pos;
  320. }
  321. SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
  322. {
  323. struct file *file;
  324. ssize_t ret = -EBADF;
  325. int fput_needed;
  326. file = fget_light(fd, &fput_needed);
  327. if (file) {
  328. loff_t pos = file_pos_read(file);
  329. ret = vfs_read(file, buf, count, &pos);
  330. file_pos_write(file, pos);
  331. fput_light(file, fput_needed);
  332. }
  333. return ret;
  334. }
  335. SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
  336. size_t, count)
  337. {
  338. struct file *file;
  339. ssize_t ret = -EBADF;
  340. int fput_needed;
  341. file = fget_light(fd, &fput_needed);
  342. if (file) {
  343. loff_t pos = file_pos_read(file);
  344. ret = vfs_write(file, buf, count, &pos);
  345. file_pos_write(file, pos);
  346. fput_light(file, fput_needed);
  347. }
  348. return ret;
  349. }
  350. SYSCALL_DEFINE(pread64)(unsigned int fd, char __user *buf,
  351. size_t count, loff_t pos)
  352. {
  353. struct file *file;
  354. ssize_t ret = -EBADF;
  355. int fput_needed;
  356. if (pos < 0)
  357. return -EINVAL;
  358. file = fget_light(fd, &fput_needed);
  359. if (file) {
  360. ret = -ESPIPE;
  361. if (file->f_mode & FMODE_PREAD)
  362. ret = vfs_read(file, buf, count, &pos);
  363. fput_light(file, fput_needed);
  364. }
  365. return ret;
  366. }
  367. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  368. asmlinkage long SyS_pread64(long fd, long buf, long count, loff_t pos)
  369. {
  370. return SYSC_pread64((unsigned int) fd, (char __user *) buf,
  371. (size_t) count, pos);
  372. }
  373. SYSCALL_ALIAS(sys_pread64, SyS_pread64);
  374. #endif
  375. SYSCALL_DEFINE(pwrite64)(unsigned int fd, const char __user *buf,
  376. size_t count, loff_t pos)
  377. {
  378. struct file *file;
  379. ssize_t ret = -EBADF;
  380. int fput_needed;
  381. if (pos < 0)
  382. return -EINVAL;
  383. file = fget_light(fd, &fput_needed);
  384. if (file) {
  385. ret = -ESPIPE;
  386. if (file->f_mode & FMODE_PWRITE)
  387. ret = vfs_write(file, buf, count, &pos);
  388. fput_light(file, fput_needed);
  389. }
  390. return ret;
  391. }
  392. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  393. asmlinkage long SyS_pwrite64(long fd, long buf, long count, loff_t pos)
  394. {
  395. return SYSC_pwrite64((unsigned int) fd, (const char __user *) buf,
  396. (size_t) count, pos);
  397. }
  398. SYSCALL_ALIAS(sys_pwrite64, SyS_pwrite64);
  399. #endif
  400. /*
  401. * Reduce an iovec's length in-place. Return the resulting number of segments
  402. */
  403. unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
  404. {
  405. unsigned long seg = 0;
  406. size_t len = 0;
  407. while (seg < nr_segs) {
  408. seg++;
  409. if (len + iov->iov_len >= to) {
  410. iov->iov_len = to - len;
  411. break;
  412. }
  413. len += iov->iov_len;
  414. iov++;
  415. }
  416. return seg;
  417. }
  418. EXPORT_SYMBOL(iov_shorten);
  419. ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
  420. unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
  421. {
  422. struct kiocb kiocb;
  423. ssize_t ret;
  424. init_sync_kiocb(&kiocb, filp);
  425. kiocb.ki_pos = *ppos;
  426. kiocb.ki_left = len;
  427. kiocb.ki_nbytes = len;
  428. for (;;) {
  429. ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
  430. if (ret != -EIOCBRETRY)
  431. break;
  432. wait_on_retry_sync_kiocb(&kiocb);
  433. }
  434. if (ret == -EIOCBQUEUED)
  435. ret = wait_on_sync_kiocb(&kiocb);
  436. *ppos = kiocb.ki_pos;
  437. return ret;
  438. }
  439. /* Do it by hand, with file-ops */
  440. ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
  441. unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
  442. {
  443. struct iovec *vector = iov;
  444. ssize_t ret = 0;
  445. while (nr_segs > 0) {
  446. void __user *base;
  447. size_t len;
  448. ssize_t nr;
  449. base = vector->iov_base;
  450. len = vector->iov_len;
  451. vector++;
  452. nr_segs--;
  453. nr = fn(filp, base, len, ppos);
  454. if (nr < 0) {
  455. if (!ret)
  456. ret = nr;
  457. break;
  458. }
  459. ret += nr;
  460. if (nr != len)
  461. break;
  462. }
  463. return ret;
  464. }
  465. /* A write operation does a read from user space and vice versa */
  466. #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
  467. ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
  468. unsigned long nr_segs, unsigned long fast_segs,
  469. struct iovec *fast_pointer,
  470. struct iovec **ret_pointer)
  471. {
  472. unsigned long seg;
  473. ssize_t ret;
  474. struct iovec *iov = fast_pointer;
  475. /*
  476. * SuS says "The readv() function *may* fail if the iovcnt argument
  477. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  478. * traditionally returned zero for zero segments, so...
  479. */
  480. if (nr_segs == 0) {
  481. ret = 0;
  482. goto out;
  483. }
  484. /*
  485. * First get the "struct iovec" from user memory and
  486. * verify all the pointers
  487. */
  488. if (nr_segs > UIO_MAXIOV) {
  489. ret = -EINVAL;
  490. goto out;
  491. }
  492. if (nr_segs > fast_segs) {
  493. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  494. if (iov == NULL) {
  495. ret = -ENOMEM;
  496. goto out;
  497. }
  498. }
  499. if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
  500. ret = -EFAULT;
  501. goto out;
  502. }
  503. /*
  504. * According to the Single Unix Specification we should return EINVAL
  505. * if an element length is < 0 when cast to ssize_t or if the
  506. * total length would overflow the ssize_t return value of the
  507. * system call.
  508. */
  509. ret = 0;
  510. for (seg = 0; seg < nr_segs; seg++) {
  511. void __user *buf = iov[seg].iov_base;
  512. ssize_t len = (ssize_t)iov[seg].iov_len;
  513. /* see if we we're about to use an invalid len or if
  514. * it's about to overflow ssize_t */
  515. if (len < 0 || (ret + len < ret)) {
  516. ret = -EINVAL;
  517. goto out;
  518. }
  519. if (unlikely(!access_ok(vrfy_dir(type), buf, len))) {
  520. ret = -EFAULT;
  521. goto out;
  522. }
  523. ret += len;
  524. }
  525. out:
  526. *ret_pointer = iov;
  527. return ret;
  528. }
  529. static ssize_t do_readv_writev(int type, struct file *file,
  530. const struct iovec __user * uvector,
  531. unsigned long nr_segs, loff_t *pos)
  532. {
  533. size_t tot_len;
  534. struct iovec iovstack[UIO_FASTIOV];
  535. struct iovec *iov = iovstack;
  536. ssize_t ret;
  537. io_fn_t fn;
  538. iov_fn_t fnv;
  539. if (!file->f_op) {
  540. ret = -EINVAL;
  541. goto out;
  542. }
  543. ret = rw_copy_check_uvector(type, uvector, nr_segs,
  544. ARRAY_SIZE(iovstack), iovstack, &iov);
  545. if (ret <= 0)
  546. goto out;
  547. tot_len = ret;
  548. ret = rw_verify_area(type, file, pos, tot_len);
  549. if (ret < 0)
  550. goto out;
  551. fnv = NULL;
  552. if (type == READ) {
  553. fn = file->f_op->read;
  554. fnv = file->f_op->aio_read;
  555. } else {
  556. fn = (io_fn_t)file->f_op->write;
  557. fnv = file->f_op->aio_write;
  558. }
  559. if (fnv)
  560. ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
  561. pos, fnv);
  562. else
  563. ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
  564. out:
  565. if (iov != iovstack)
  566. kfree(iov);
  567. if ((ret + (type == READ)) > 0) {
  568. if (type == READ)
  569. fsnotify_access(file->f_path.dentry);
  570. else
  571. fsnotify_modify(file->f_path.dentry);
  572. }
  573. return ret;
  574. }
  575. ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
  576. unsigned long vlen, loff_t *pos)
  577. {
  578. if (!(file->f_mode & FMODE_READ))
  579. return -EBADF;
  580. if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
  581. return -EINVAL;
  582. return do_readv_writev(READ, file, vec, vlen, pos);
  583. }
  584. EXPORT_SYMBOL(vfs_readv);
  585. ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
  586. unsigned long vlen, loff_t *pos)
  587. {
  588. if (!(file->f_mode & FMODE_WRITE))
  589. return -EBADF;
  590. if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
  591. return -EINVAL;
  592. return do_readv_writev(WRITE, file, vec, vlen, pos);
  593. }
  594. EXPORT_SYMBOL(vfs_writev);
  595. SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
  596. unsigned long, vlen)
  597. {
  598. struct file *file;
  599. ssize_t ret = -EBADF;
  600. int fput_needed;
  601. file = fget_light(fd, &fput_needed);
  602. if (file) {
  603. loff_t pos = file_pos_read(file);
  604. ret = vfs_readv(file, vec, vlen, &pos);
  605. file_pos_write(file, pos);
  606. fput_light(file, fput_needed);
  607. }
  608. if (ret > 0)
  609. add_rchar(current, ret);
  610. inc_syscr(current);
  611. return ret;
  612. }
  613. SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
  614. unsigned long, vlen)
  615. {
  616. struct file *file;
  617. ssize_t ret = -EBADF;
  618. int fput_needed;
  619. file = fget_light(fd, &fput_needed);
  620. if (file) {
  621. loff_t pos = file_pos_read(file);
  622. ret = vfs_writev(file, vec, vlen, &pos);
  623. file_pos_write(file, pos);
  624. fput_light(file, fput_needed);
  625. }
  626. if (ret > 0)
  627. add_wchar(current, ret);
  628. inc_syscw(current);
  629. return ret;
  630. }
  631. static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
  632. {
  633. #define HALF_LONG_BITS (BITS_PER_LONG / 2)
  634. return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
  635. }
  636. SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
  637. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  638. {
  639. loff_t pos = pos_from_hilo(pos_h, pos_l);
  640. struct file *file;
  641. ssize_t ret = -EBADF;
  642. int fput_needed;
  643. if (pos < 0)
  644. return -EINVAL;
  645. file = fget_light(fd, &fput_needed);
  646. if (file) {
  647. ret = -ESPIPE;
  648. if (file->f_mode & FMODE_PREAD)
  649. ret = vfs_readv(file, vec, vlen, &pos);
  650. fput_light(file, fput_needed);
  651. }
  652. if (ret > 0)
  653. add_rchar(current, ret);
  654. inc_syscr(current);
  655. return ret;
  656. }
  657. SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
  658. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  659. {
  660. loff_t pos = pos_from_hilo(pos_h, pos_l);
  661. struct file *file;
  662. ssize_t ret = -EBADF;
  663. int fput_needed;
  664. if (pos < 0)
  665. return -EINVAL;
  666. file = fget_light(fd, &fput_needed);
  667. if (file) {
  668. ret = -ESPIPE;
  669. if (file->f_mode & FMODE_PWRITE)
  670. ret = vfs_writev(file, vec, vlen, &pos);
  671. fput_light(file, fput_needed);
  672. }
  673. if (ret > 0)
  674. add_wchar(current, ret);
  675. inc_syscw(current);
  676. return ret;
  677. }
  678. static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
  679. size_t count, loff_t max)
  680. {
  681. struct file * in_file, * out_file;
  682. struct inode * in_inode, * out_inode;
  683. loff_t pos;
  684. ssize_t retval;
  685. int fput_needed_in, fput_needed_out, fl;
  686. /*
  687. * Get input file, and verify that it is ok..
  688. */
  689. retval = -EBADF;
  690. in_file = fget_light(in_fd, &fput_needed_in);
  691. if (!in_file)
  692. goto out;
  693. if (!(in_file->f_mode & FMODE_READ))
  694. goto fput_in;
  695. retval = -ESPIPE;
  696. if (!ppos)
  697. ppos = &in_file->f_pos;
  698. else
  699. if (!(in_file->f_mode & FMODE_PREAD))
  700. goto fput_in;
  701. retval = rw_verify_area(READ, in_file, ppos, count);
  702. if (retval < 0)
  703. goto fput_in;
  704. count = retval;
  705. /*
  706. * Get output file, and verify that it is ok..
  707. */
  708. retval = -EBADF;
  709. out_file = fget_light(out_fd, &fput_needed_out);
  710. if (!out_file)
  711. goto fput_in;
  712. if (!(out_file->f_mode & FMODE_WRITE))
  713. goto fput_out;
  714. retval = -EINVAL;
  715. in_inode = in_file->f_path.dentry->d_inode;
  716. out_inode = out_file->f_path.dentry->d_inode;
  717. retval = rw_verify_area(WRITE, out_file, &out_file->f_pos, count);
  718. if (retval < 0)
  719. goto fput_out;
  720. count = retval;
  721. if (!max)
  722. max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
  723. pos = *ppos;
  724. if (unlikely(pos + count > max)) {
  725. retval = -EOVERFLOW;
  726. if (pos >= max)
  727. goto fput_out;
  728. count = max - pos;
  729. }
  730. fl = 0;
  731. #if 0
  732. /*
  733. * We need to debate whether we can enable this or not. The
  734. * man page documents EAGAIN return for the output at least,
  735. * and the application is arguably buggy if it doesn't expect
  736. * EAGAIN on a non-blocking file descriptor.
  737. */
  738. if (in_file->f_flags & O_NONBLOCK)
  739. fl = SPLICE_F_NONBLOCK;
  740. #endif
  741. retval = do_splice_direct(in_file, ppos, out_file, count, fl);
  742. if (retval > 0) {
  743. add_rchar(current, retval);
  744. add_wchar(current, retval);
  745. }
  746. inc_syscr(current);
  747. inc_syscw(current);
  748. if (*ppos > max)
  749. retval = -EOVERFLOW;
  750. fput_out:
  751. fput_light(out_file, fput_needed_out);
  752. fput_in:
  753. fput_light(in_file, fput_needed_in);
  754. out:
  755. return retval;
  756. }
  757. SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
  758. {
  759. loff_t pos;
  760. off_t off;
  761. ssize_t ret;
  762. if (offset) {
  763. if (unlikely(get_user(off, offset)))
  764. return -EFAULT;
  765. pos = off;
  766. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  767. if (unlikely(put_user(pos, offset)))
  768. return -EFAULT;
  769. return ret;
  770. }
  771. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  772. }
  773. SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
  774. {
  775. loff_t pos;
  776. ssize_t ret;
  777. if (offset) {
  778. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  779. return -EFAULT;
  780. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  781. if (unlikely(put_user(pos, offset)))
  782. return -EFAULT;
  783. return ret;
  784. }
  785. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  786. }