read_write.c 22 KB

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