read_write.c 20 KB

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