read_write.c 20 KB

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