read_write.c 20 KB

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