read_write.c 22 KB

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