read_write.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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 inline int unsigned_offsets(struct file *file)
  29. {
  30. return file->f_mode & FMODE_UNSIGNED_OFFSET;
  31. }
  32. /**
  33. * generic_file_llseek_unlocked - lockless generic llseek implementation
  34. * @file: file structure to seek on
  35. * @offset: file offset to seek to
  36. * @origin: type of seek
  37. *
  38. * Updates the file offset to the value specified by @offset and @origin.
  39. * Locking must be provided by the caller.
  40. */
  41. loff_t
  42. generic_file_llseek_unlocked(struct file *file, loff_t offset, int origin)
  43. {
  44. struct inode *inode = file->f_mapping->host;
  45. switch (origin) {
  46. case SEEK_END:
  47. offset += inode->i_size;
  48. break;
  49. case SEEK_CUR:
  50. /*
  51. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  52. * position-querying operation. Avoid rewriting the "same"
  53. * f_pos value back to the file because a concurrent read(),
  54. * write() or lseek() might have altered it
  55. */
  56. if (offset == 0)
  57. return file->f_pos;
  58. offset += file->f_pos;
  59. break;
  60. }
  61. if (offset < 0 && !unsigned_offsets(file))
  62. return -EINVAL;
  63. if (offset > inode->i_sb->s_maxbytes)
  64. return -EINVAL;
  65. /* Special lock needed here? */
  66. if (offset != file->f_pos) {
  67. file->f_pos = offset;
  68. file->f_version = 0;
  69. }
  70. return offset;
  71. }
  72. EXPORT_SYMBOL(generic_file_llseek_unlocked);
  73. /**
  74. * generic_file_llseek - generic llseek implementation for regular files
  75. * @file: file structure to seek on
  76. * @offset: file offset to seek to
  77. * @origin: type of seek
  78. *
  79. * This is a generic implemenation of ->llseek useable for all normal local
  80. * filesystems. It just updates the file offset to the value specified by
  81. * @offset and @origin under i_mutex.
  82. */
  83. loff_t generic_file_llseek(struct file *file, loff_t offset, int origin)
  84. {
  85. loff_t rval;
  86. mutex_lock(&file->f_dentry->d_inode->i_mutex);
  87. rval = generic_file_llseek_unlocked(file, offset, origin);
  88. mutex_unlock(&file->f_dentry->d_inode->i_mutex);
  89. return rval;
  90. }
  91. EXPORT_SYMBOL(generic_file_llseek);
  92. /**
  93. * noop_llseek - No Operation Performed llseek implementation
  94. * @file: file structure to seek on
  95. * @offset: file offset to seek to
  96. * @origin: type of seek
  97. *
  98. * This is an implementation of ->llseek useable for the rare special case when
  99. * userspace expects the seek to succeed but the (device) file is actually not
  100. * able to perform the seek. In this case you use noop_llseek() instead of
  101. * falling back to the default implementation of ->llseek.
  102. */
  103. loff_t noop_llseek(struct file *file, loff_t offset, int origin)
  104. {
  105. return file->f_pos;
  106. }
  107. EXPORT_SYMBOL(noop_llseek);
  108. loff_t no_llseek(struct file *file, loff_t offset, int origin)
  109. {
  110. return -ESPIPE;
  111. }
  112. EXPORT_SYMBOL(no_llseek);
  113. loff_t default_llseek(struct file *file, loff_t offset, int origin)
  114. {
  115. loff_t retval;
  116. mutex_lock(&file->f_dentry->d_inode->i_mutex);
  117. switch (origin) {
  118. case SEEK_END:
  119. offset += i_size_read(file->f_path.dentry->d_inode);
  120. break;
  121. case SEEK_CUR:
  122. if (offset == 0) {
  123. retval = file->f_pos;
  124. goto out;
  125. }
  126. offset += file->f_pos;
  127. }
  128. retval = -EINVAL;
  129. if (offset >= 0 || unsigned_offsets(file)) {
  130. if (offset != file->f_pos) {
  131. file->f_pos = offset;
  132. file->f_version = 0;
  133. }
  134. retval = offset;
  135. }
  136. out:
  137. mutex_unlock(&file->f_dentry->d_inode->i_mutex);
  138. return retval;
  139. }
  140. EXPORT_SYMBOL(default_llseek);
  141. loff_t vfs_llseek(struct file *file, loff_t offset, int origin)
  142. {
  143. loff_t (*fn)(struct file *, loff_t, int);
  144. fn = no_llseek;
  145. if (file->f_mode & FMODE_LSEEK) {
  146. if (file->f_op && file->f_op->llseek)
  147. fn = file->f_op->llseek;
  148. }
  149. return fn(file, offset, origin);
  150. }
  151. EXPORT_SYMBOL(vfs_llseek);
  152. SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, origin)
  153. {
  154. off_t retval;
  155. struct file * file;
  156. int fput_needed;
  157. retval = -EBADF;
  158. file = fget_light(fd, &fput_needed);
  159. if (!file)
  160. goto bad;
  161. retval = -EINVAL;
  162. if (origin <= SEEK_MAX) {
  163. loff_t res = vfs_llseek(file, offset, origin);
  164. retval = res;
  165. if (res != (loff_t)retval)
  166. retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
  167. }
  168. fput_light(file, fput_needed);
  169. bad:
  170. return retval;
  171. }
  172. #ifdef __ARCH_WANT_SYS_LLSEEK
  173. SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
  174. unsigned long, offset_low, loff_t __user *, result,
  175. unsigned int, origin)
  176. {
  177. int retval;
  178. struct file * file;
  179. loff_t offset;
  180. int fput_needed;
  181. retval = -EBADF;
  182. file = fget_light(fd, &fput_needed);
  183. if (!file)
  184. goto bad;
  185. retval = -EINVAL;
  186. if (origin > SEEK_MAX)
  187. goto out_putf;
  188. offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low,
  189. origin);
  190. retval = (int)offset;
  191. if (offset >= 0) {
  192. retval = -EFAULT;
  193. if (!copy_to_user(result, &offset, sizeof(offset)))
  194. retval = 0;
  195. }
  196. out_putf:
  197. fput_light(file, fput_needed);
  198. bad:
  199. return retval;
  200. }
  201. #endif
  202. /*
  203. * rw_verify_area doesn't like huge counts. We limit
  204. * them to something that fits in "int" so that others
  205. * won't have to do range checks all the time.
  206. */
  207. int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
  208. {
  209. struct inode *inode;
  210. loff_t pos;
  211. int retval = -EINVAL;
  212. inode = file->f_path.dentry->d_inode;
  213. if (unlikely((ssize_t) count < 0))
  214. return retval;
  215. pos = *ppos;
  216. if (unlikely(pos < 0)) {
  217. if (!unsigned_offsets(file))
  218. return retval;
  219. if (count >= -pos) /* both values are in 0..LLONG_MAX */
  220. return -EOVERFLOW;
  221. } else if (unlikely((loff_t) (pos + count) < 0)) {
  222. if (!unsigned_offsets(file))
  223. return retval;
  224. }
  225. if (unlikely(inode->i_flock && mandatory_lock(inode))) {
  226. retval = locks_mandatory_area(
  227. read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
  228. inode, file, pos, count);
  229. if (retval < 0)
  230. return retval;
  231. }
  232. retval = security_file_permission(file,
  233. read_write == READ ? MAY_READ : MAY_WRITE);
  234. if (retval)
  235. return retval;
  236. return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
  237. }
  238. static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
  239. {
  240. set_current_state(TASK_UNINTERRUPTIBLE);
  241. if (!kiocbIsKicked(iocb))
  242. schedule();
  243. else
  244. kiocbClearKicked(iocb);
  245. __set_current_state(TASK_RUNNING);
  246. }
  247. ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
  248. {
  249. struct iovec iov = { .iov_base = buf, .iov_len = len };
  250. struct kiocb kiocb;
  251. ssize_t ret;
  252. init_sync_kiocb(&kiocb, filp);
  253. kiocb.ki_pos = *ppos;
  254. kiocb.ki_left = len;
  255. kiocb.ki_nbytes = len;
  256. for (;;) {
  257. ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
  258. if (ret != -EIOCBRETRY)
  259. break;
  260. wait_on_retry_sync_kiocb(&kiocb);
  261. }
  262. if (-EIOCBQUEUED == ret)
  263. ret = wait_on_sync_kiocb(&kiocb);
  264. *ppos = kiocb.ki_pos;
  265. return ret;
  266. }
  267. EXPORT_SYMBOL(do_sync_read);
  268. ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
  269. {
  270. ssize_t ret;
  271. if (!(file->f_mode & FMODE_READ))
  272. return -EBADF;
  273. if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
  274. return -EINVAL;
  275. if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
  276. return -EFAULT;
  277. ret = rw_verify_area(READ, file, pos, count);
  278. if (ret >= 0) {
  279. count = ret;
  280. if (file->f_op->read)
  281. ret = file->f_op->read(file, buf, count, pos);
  282. else
  283. ret = do_sync_read(file, buf, count, pos);
  284. if (ret > 0) {
  285. fsnotify_access(file);
  286. add_rchar(current, ret);
  287. }
  288. inc_syscr(current);
  289. }
  290. return ret;
  291. }
  292. EXPORT_SYMBOL(vfs_read);
  293. ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
  294. {
  295. struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
  296. struct kiocb kiocb;
  297. ssize_t ret;
  298. init_sync_kiocb(&kiocb, filp);
  299. kiocb.ki_pos = *ppos;
  300. kiocb.ki_left = len;
  301. kiocb.ki_nbytes = len;
  302. for (;;) {
  303. ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
  304. if (ret != -EIOCBRETRY)
  305. break;
  306. wait_on_retry_sync_kiocb(&kiocb);
  307. }
  308. if (-EIOCBQUEUED == ret)
  309. ret = wait_on_sync_kiocb(&kiocb);
  310. *ppos = kiocb.ki_pos;
  311. return ret;
  312. }
  313. EXPORT_SYMBOL(do_sync_write);
  314. ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
  315. {
  316. ssize_t ret;
  317. if (!(file->f_mode & FMODE_WRITE))
  318. return -EBADF;
  319. if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
  320. return -EINVAL;
  321. if (unlikely(!access_ok(VERIFY_READ, buf, count)))
  322. return -EFAULT;
  323. ret = rw_verify_area(WRITE, file, pos, count);
  324. if (ret >= 0) {
  325. count = ret;
  326. if (file->f_op->write)
  327. ret = file->f_op->write(file, buf, count, pos);
  328. else
  329. ret = do_sync_write(file, buf, count, pos);
  330. if (ret > 0) {
  331. fsnotify_modify(file);
  332. add_wchar(current, ret);
  333. }
  334. inc_syscw(current);
  335. }
  336. return ret;
  337. }
  338. EXPORT_SYMBOL(vfs_write);
  339. static inline loff_t file_pos_read(struct file *file)
  340. {
  341. return file->f_pos;
  342. }
  343. static inline void file_pos_write(struct file *file, loff_t pos)
  344. {
  345. file->f_pos = pos;
  346. }
  347. SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
  348. {
  349. struct file *file;
  350. ssize_t ret = -EBADF;
  351. int fput_needed;
  352. file = fget_light(fd, &fput_needed);
  353. if (file) {
  354. loff_t pos = file_pos_read(file);
  355. ret = vfs_read(file, buf, count, &pos);
  356. file_pos_write(file, pos);
  357. fput_light(file, fput_needed);
  358. }
  359. return ret;
  360. }
  361. SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
  362. size_t, count)
  363. {
  364. struct file *file;
  365. ssize_t ret = -EBADF;
  366. int fput_needed;
  367. file = fget_light(fd, &fput_needed);
  368. if (file) {
  369. loff_t pos = file_pos_read(file);
  370. ret = vfs_write(file, buf, count, &pos);
  371. file_pos_write(file, pos);
  372. fput_light(file, fput_needed);
  373. }
  374. return ret;
  375. }
  376. SYSCALL_DEFINE(pread64)(unsigned int fd, char __user *buf,
  377. size_t count, loff_t pos)
  378. {
  379. struct file *file;
  380. ssize_t ret = -EBADF;
  381. int fput_needed;
  382. if (pos < 0)
  383. return -EINVAL;
  384. file = fget_light(fd, &fput_needed);
  385. if (file) {
  386. ret = -ESPIPE;
  387. if (file->f_mode & FMODE_PREAD)
  388. ret = vfs_read(file, buf, count, &pos);
  389. fput_light(file, fput_needed);
  390. }
  391. return ret;
  392. }
  393. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  394. asmlinkage long SyS_pread64(long fd, long buf, long count, loff_t pos)
  395. {
  396. return SYSC_pread64((unsigned int) fd, (char __user *) buf,
  397. (size_t) count, pos);
  398. }
  399. SYSCALL_ALIAS(sys_pread64, SyS_pread64);
  400. #endif
  401. SYSCALL_DEFINE(pwrite64)(unsigned int fd, const char __user *buf,
  402. size_t count, loff_t pos)
  403. {
  404. struct file *file;
  405. ssize_t ret = -EBADF;
  406. int fput_needed;
  407. if (pos < 0)
  408. return -EINVAL;
  409. file = fget_light(fd, &fput_needed);
  410. if (file) {
  411. ret = -ESPIPE;
  412. if (file->f_mode & FMODE_PWRITE)
  413. ret = vfs_write(file, buf, count, &pos);
  414. fput_light(file, fput_needed);
  415. }
  416. return ret;
  417. }
  418. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  419. asmlinkage long SyS_pwrite64(long fd, long buf, long count, loff_t pos)
  420. {
  421. return SYSC_pwrite64((unsigned int) fd, (const char __user *) buf,
  422. (size_t) count, pos);
  423. }
  424. SYSCALL_ALIAS(sys_pwrite64, SyS_pwrite64);
  425. #endif
  426. /*
  427. * Reduce an iovec's length in-place. Return the resulting number of segments
  428. */
  429. unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
  430. {
  431. unsigned long seg = 0;
  432. size_t len = 0;
  433. while (seg < nr_segs) {
  434. seg++;
  435. if (len + iov->iov_len >= to) {
  436. iov->iov_len = to - len;
  437. break;
  438. }
  439. len += iov->iov_len;
  440. iov++;
  441. }
  442. return seg;
  443. }
  444. EXPORT_SYMBOL(iov_shorten);
  445. ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
  446. unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
  447. {
  448. struct kiocb kiocb;
  449. ssize_t ret;
  450. init_sync_kiocb(&kiocb, filp);
  451. kiocb.ki_pos = *ppos;
  452. kiocb.ki_left = len;
  453. kiocb.ki_nbytes = len;
  454. for (;;) {
  455. ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
  456. if (ret != -EIOCBRETRY)
  457. break;
  458. wait_on_retry_sync_kiocb(&kiocb);
  459. }
  460. if (ret == -EIOCBQUEUED)
  461. ret = wait_on_sync_kiocb(&kiocb);
  462. *ppos = kiocb.ki_pos;
  463. return ret;
  464. }
  465. /* Do it by hand, with file-ops */
  466. ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
  467. unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
  468. {
  469. struct iovec *vector = iov;
  470. ssize_t ret = 0;
  471. while (nr_segs > 0) {
  472. void __user *base;
  473. size_t len;
  474. ssize_t nr;
  475. base = vector->iov_base;
  476. len = vector->iov_len;
  477. vector++;
  478. nr_segs--;
  479. nr = fn(filp, base, len, ppos);
  480. if (nr < 0) {
  481. if (!ret)
  482. ret = nr;
  483. break;
  484. }
  485. ret += nr;
  486. if (nr != len)
  487. break;
  488. }
  489. return ret;
  490. }
  491. /* A write operation does a read from user space and vice versa */
  492. #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
  493. ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
  494. unsigned long nr_segs, unsigned long fast_segs,
  495. struct iovec *fast_pointer,
  496. struct iovec **ret_pointer)
  497. {
  498. unsigned long seg;
  499. ssize_t ret;
  500. struct iovec *iov = fast_pointer;
  501. /*
  502. * SuS says "The readv() function *may* fail if the iovcnt argument
  503. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  504. * traditionally returned zero for zero segments, so...
  505. */
  506. if (nr_segs == 0) {
  507. ret = 0;
  508. goto out;
  509. }
  510. /*
  511. * First get the "struct iovec" from user memory and
  512. * verify all the pointers
  513. */
  514. if (nr_segs > UIO_MAXIOV) {
  515. ret = -EINVAL;
  516. goto out;
  517. }
  518. if (nr_segs > fast_segs) {
  519. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  520. if (iov == NULL) {
  521. ret = -ENOMEM;
  522. goto out;
  523. }
  524. }
  525. if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
  526. ret = -EFAULT;
  527. goto out;
  528. }
  529. /*
  530. * According to the Single Unix Specification we should return EINVAL
  531. * if an element length is < 0 when cast to ssize_t or if the
  532. * total length would overflow the ssize_t return value of the
  533. * system call.
  534. *
  535. * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
  536. * overflow case.
  537. */
  538. ret = 0;
  539. for (seg = 0; seg < nr_segs; seg++) {
  540. void __user *buf = iov[seg].iov_base;
  541. ssize_t len = (ssize_t)iov[seg].iov_len;
  542. /* see if we we're about to use an invalid len or if
  543. * it's about to overflow ssize_t */
  544. if (len < 0) {
  545. ret = -EINVAL;
  546. goto out;
  547. }
  548. if (unlikely(!access_ok(vrfy_dir(type), buf, len))) {
  549. ret = -EFAULT;
  550. goto out;
  551. }
  552. if (len > MAX_RW_COUNT - ret) {
  553. len = MAX_RW_COUNT - ret;
  554. iov[seg].iov_len = len;
  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. }