read_write.c 21 KB

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