read_write.c 21 KB

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