read_write.c 21 KB

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