read_write.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. 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 (unlikely(!access_ok(vrfy_dir(type), buf, len))) {
  608. ret = -EFAULT;
  609. goto out;
  610. }
  611. if (len > MAX_RW_COUNT - ret) {
  612. len = MAX_RW_COUNT - ret;
  613. iov[seg].iov_len = len;
  614. }
  615. ret += len;
  616. }
  617. out:
  618. *ret_pointer = iov;
  619. return ret;
  620. }
  621. static ssize_t do_readv_writev(int type, struct file *file,
  622. const struct iovec __user * uvector,
  623. unsigned long nr_segs, loff_t *pos)
  624. {
  625. size_t tot_len;
  626. struct iovec iovstack[UIO_FASTIOV];
  627. struct iovec *iov = iovstack;
  628. ssize_t ret;
  629. io_fn_t fn;
  630. iov_fn_t fnv;
  631. if (!file->f_op) {
  632. ret = -EINVAL;
  633. goto out;
  634. }
  635. ret = rw_copy_check_uvector(type, uvector, nr_segs,
  636. ARRAY_SIZE(iovstack), iovstack, &iov);
  637. if (ret <= 0)
  638. goto out;
  639. tot_len = ret;
  640. ret = rw_verify_area(type, file, pos, tot_len);
  641. if (ret < 0)
  642. goto out;
  643. fnv = NULL;
  644. if (type == READ) {
  645. fn = file->f_op->read;
  646. fnv = file->f_op->aio_read;
  647. } else {
  648. fn = (io_fn_t)file->f_op->write;
  649. fnv = file->f_op->aio_write;
  650. }
  651. if (fnv)
  652. ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
  653. pos, fnv);
  654. else
  655. ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
  656. out:
  657. if (iov != iovstack)
  658. kfree(iov);
  659. if ((ret + (type == READ)) > 0) {
  660. if (type == READ)
  661. fsnotify_access(file);
  662. else
  663. fsnotify_modify(file);
  664. }
  665. return ret;
  666. }
  667. ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
  668. unsigned long vlen, loff_t *pos)
  669. {
  670. if (!(file->f_mode & FMODE_READ))
  671. return -EBADF;
  672. if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
  673. return -EINVAL;
  674. return do_readv_writev(READ, file, vec, vlen, pos);
  675. }
  676. EXPORT_SYMBOL(vfs_readv);
  677. ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
  678. unsigned long vlen, loff_t *pos)
  679. {
  680. if (!(file->f_mode & FMODE_WRITE))
  681. return -EBADF;
  682. if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
  683. return -EINVAL;
  684. return do_readv_writev(WRITE, file, vec, vlen, pos);
  685. }
  686. EXPORT_SYMBOL(vfs_writev);
  687. SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
  688. unsigned long, vlen)
  689. {
  690. struct file *file;
  691. ssize_t ret = -EBADF;
  692. int fput_needed;
  693. file = fget_light(fd, &fput_needed);
  694. if (file) {
  695. loff_t pos = file_pos_read(file);
  696. ret = vfs_readv(file, vec, vlen, &pos);
  697. file_pos_write(file, pos);
  698. fput_light(file, fput_needed);
  699. }
  700. if (ret > 0)
  701. add_rchar(current, ret);
  702. inc_syscr(current);
  703. return ret;
  704. }
  705. SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
  706. unsigned long, vlen)
  707. {
  708. struct file *file;
  709. ssize_t ret = -EBADF;
  710. int fput_needed;
  711. file = fget_light(fd, &fput_needed);
  712. if (file) {
  713. loff_t pos = file_pos_read(file);
  714. ret = vfs_writev(file, vec, vlen, &pos);
  715. file_pos_write(file, pos);
  716. fput_light(file, fput_needed);
  717. }
  718. if (ret > 0)
  719. add_wchar(current, ret);
  720. inc_syscw(current);
  721. return ret;
  722. }
  723. static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
  724. {
  725. #define HALF_LONG_BITS (BITS_PER_LONG / 2)
  726. return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
  727. }
  728. SYSCALL_DEFINE5(preadv, 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_PREAD)
  741. ret = vfs_readv(file, vec, vlen, &pos);
  742. fput_light(file, fput_needed);
  743. }
  744. if (ret > 0)
  745. add_rchar(current, ret);
  746. inc_syscr(current);
  747. return ret;
  748. }
  749. SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
  750. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  751. {
  752. loff_t pos = pos_from_hilo(pos_h, pos_l);
  753. struct file *file;
  754. ssize_t ret = -EBADF;
  755. int fput_needed;
  756. if (pos < 0)
  757. return -EINVAL;
  758. file = fget_light(fd, &fput_needed);
  759. if (file) {
  760. ret = -ESPIPE;
  761. if (file->f_mode & FMODE_PWRITE)
  762. ret = vfs_writev(file, vec, vlen, &pos);
  763. fput_light(file, fput_needed);
  764. }
  765. if (ret > 0)
  766. add_wchar(current, ret);
  767. inc_syscw(current);
  768. return ret;
  769. }
  770. static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
  771. size_t count, loff_t max)
  772. {
  773. struct file * in_file, * out_file;
  774. struct inode * in_inode, * out_inode;
  775. loff_t pos;
  776. ssize_t retval;
  777. int fput_needed_in, fput_needed_out, fl;
  778. /*
  779. * Get input file, and verify that it is ok..
  780. */
  781. retval = -EBADF;
  782. in_file = fget_light(in_fd, &fput_needed_in);
  783. if (!in_file)
  784. goto out;
  785. if (!(in_file->f_mode & FMODE_READ))
  786. goto fput_in;
  787. retval = -ESPIPE;
  788. if (!ppos)
  789. ppos = &in_file->f_pos;
  790. else
  791. if (!(in_file->f_mode & FMODE_PREAD))
  792. goto fput_in;
  793. retval = rw_verify_area(READ, in_file, ppos, count);
  794. if (retval < 0)
  795. goto fput_in;
  796. count = retval;
  797. /*
  798. * Get output file, and verify that it is ok..
  799. */
  800. retval = -EBADF;
  801. out_file = fget_light(out_fd, &fput_needed_out);
  802. if (!out_file)
  803. goto fput_in;
  804. if (!(out_file->f_mode & FMODE_WRITE))
  805. goto fput_out;
  806. retval = -EINVAL;
  807. in_inode = in_file->f_path.dentry->d_inode;
  808. out_inode = out_file->f_path.dentry->d_inode;
  809. retval = rw_verify_area(WRITE, out_file, &out_file->f_pos, count);
  810. if (retval < 0)
  811. goto fput_out;
  812. count = retval;
  813. if (!max)
  814. max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
  815. pos = *ppos;
  816. if (unlikely(pos + count > max)) {
  817. retval = -EOVERFLOW;
  818. if (pos >= max)
  819. goto fput_out;
  820. count = max - pos;
  821. }
  822. fl = 0;
  823. #if 0
  824. /*
  825. * We need to debate whether we can enable this or not. The
  826. * man page documents EAGAIN return for the output at least,
  827. * and the application is arguably buggy if it doesn't expect
  828. * EAGAIN on a non-blocking file descriptor.
  829. */
  830. if (in_file->f_flags & O_NONBLOCK)
  831. fl = SPLICE_F_NONBLOCK;
  832. #endif
  833. retval = do_splice_direct(in_file, ppos, out_file, count, fl);
  834. if (retval > 0) {
  835. add_rchar(current, retval);
  836. add_wchar(current, retval);
  837. }
  838. inc_syscr(current);
  839. inc_syscw(current);
  840. if (*ppos > max)
  841. retval = -EOVERFLOW;
  842. fput_out:
  843. fput_light(out_file, fput_needed_out);
  844. fput_in:
  845. fput_light(in_file, fput_needed_in);
  846. out:
  847. return retval;
  848. }
  849. SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
  850. {
  851. loff_t pos;
  852. off_t off;
  853. ssize_t ret;
  854. if (offset) {
  855. if (unlikely(get_user(off, offset)))
  856. return -EFAULT;
  857. pos = off;
  858. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  859. if (unlikely(put_user(pos, offset)))
  860. return -EFAULT;
  861. return ret;
  862. }
  863. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  864. }
  865. SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
  866. {
  867. loff_t pos;
  868. ssize_t ret;
  869. if (offset) {
  870. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  871. return -EFAULT;
  872. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  873. if (unlikely(put_user(pos, offset)))
  874. return -EFAULT;
  875. return ret;
  876. }
  877. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  878. }