read_write.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. int check_access)
  557. {
  558. unsigned long seg;
  559. ssize_t ret;
  560. struct iovec *iov = fast_pointer;
  561. /*
  562. * SuS says "The readv() function *may* fail if the iovcnt argument
  563. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  564. * traditionally returned zero for zero segments, so...
  565. */
  566. if (nr_segs == 0) {
  567. ret = 0;
  568. goto out;
  569. }
  570. /*
  571. * First get the "struct iovec" from user memory and
  572. * verify all the pointers
  573. */
  574. if (nr_segs > UIO_MAXIOV) {
  575. ret = -EINVAL;
  576. goto out;
  577. }
  578. if (nr_segs > fast_segs) {
  579. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  580. if (iov == NULL) {
  581. ret = -ENOMEM;
  582. goto out;
  583. }
  584. }
  585. if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
  586. ret = -EFAULT;
  587. goto out;
  588. }
  589. /*
  590. * According to the Single Unix Specification we should return EINVAL
  591. * if an element length is < 0 when cast to ssize_t or if the
  592. * total length would overflow the ssize_t return value of the
  593. * system call.
  594. *
  595. * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
  596. * overflow case.
  597. */
  598. ret = 0;
  599. for (seg = 0; seg < nr_segs; seg++) {
  600. void __user *buf = iov[seg].iov_base;
  601. ssize_t len = (ssize_t)iov[seg].iov_len;
  602. /* see if we we're about to use an invalid len or if
  603. * it's about to overflow ssize_t */
  604. if (len < 0) {
  605. ret = -EINVAL;
  606. goto out;
  607. }
  608. if (check_access
  609. && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
  610. ret = -EFAULT;
  611. goto out;
  612. }
  613. if (len > MAX_RW_COUNT - ret) {
  614. len = MAX_RW_COUNT - ret;
  615. iov[seg].iov_len = len;
  616. }
  617. ret += len;
  618. }
  619. out:
  620. *ret_pointer = iov;
  621. return ret;
  622. }
  623. static ssize_t do_readv_writev(int type, struct file *file,
  624. const struct iovec __user * uvector,
  625. unsigned long nr_segs, loff_t *pos)
  626. {
  627. size_t tot_len;
  628. struct iovec iovstack[UIO_FASTIOV];
  629. struct iovec *iov = iovstack;
  630. ssize_t ret;
  631. io_fn_t fn;
  632. iov_fn_t fnv;
  633. if (!file->f_op) {
  634. ret = -EINVAL;
  635. goto out;
  636. }
  637. ret = rw_copy_check_uvector(type, uvector, nr_segs,
  638. ARRAY_SIZE(iovstack), iovstack, &iov, 1);
  639. if (ret <= 0)
  640. goto out;
  641. tot_len = ret;
  642. ret = rw_verify_area(type, file, pos, tot_len);
  643. if (ret < 0)
  644. goto out;
  645. fnv = NULL;
  646. if (type == READ) {
  647. fn = file->f_op->read;
  648. fnv = file->f_op->aio_read;
  649. } else {
  650. fn = (io_fn_t)file->f_op->write;
  651. fnv = file->f_op->aio_write;
  652. }
  653. if (fnv)
  654. ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
  655. pos, fnv);
  656. else
  657. ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
  658. out:
  659. if (iov != iovstack)
  660. kfree(iov);
  661. if ((ret + (type == READ)) > 0) {
  662. if (type == READ)
  663. fsnotify_access(file);
  664. else
  665. fsnotify_modify(file);
  666. }
  667. return ret;
  668. }
  669. ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
  670. unsigned long vlen, loff_t *pos)
  671. {
  672. if (!(file->f_mode & FMODE_READ))
  673. return -EBADF;
  674. if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
  675. return -EINVAL;
  676. return do_readv_writev(READ, file, vec, vlen, pos);
  677. }
  678. EXPORT_SYMBOL(vfs_readv);
  679. ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
  680. unsigned long vlen, loff_t *pos)
  681. {
  682. if (!(file->f_mode & FMODE_WRITE))
  683. return -EBADF;
  684. if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
  685. return -EINVAL;
  686. return do_readv_writev(WRITE, file, vec, vlen, pos);
  687. }
  688. EXPORT_SYMBOL(vfs_writev);
  689. SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
  690. unsigned long, vlen)
  691. {
  692. struct file *file;
  693. ssize_t ret = -EBADF;
  694. int fput_needed;
  695. file = fget_light(fd, &fput_needed);
  696. if (file) {
  697. loff_t pos = file_pos_read(file);
  698. ret = vfs_readv(file, vec, vlen, &pos);
  699. file_pos_write(file, pos);
  700. fput_light(file, fput_needed);
  701. }
  702. if (ret > 0)
  703. add_rchar(current, ret);
  704. inc_syscr(current);
  705. return ret;
  706. }
  707. SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
  708. unsigned long, vlen)
  709. {
  710. struct file *file;
  711. ssize_t ret = -EBADF;
  712. int fput_needed;
  713. file = fget_light(fd, &fput_needed);
  714. if (file) {
  715. loff_t pos = file_pos_read(file);
  716. ret = vfs_writev(file, vec, vlen, &pos);
  717. file_pos_write(file, pos);
  718. fput_light(file, fput_needed);
  719. }
  720. if (ret > 0)
  721. add_wchar(current, ret);
  722. inc_syscw(current);
  723. return ret;
  724. }
  725. static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
  726. {
  727. #define HALF_LONG_BITS (BITS_PER_LONG / 2)
  728. return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
  729. }
  730. SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
  731. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  732. {
  733. loff_t pos = pos_from_hilo(pos_h, pos_l);
  734. struct file *file;
  735. ssize_t ret = -EBADF;
  736. int fput_needed;
  737. if (pos < 0)
  738. return -EINVAL;
  739. file = fget_light(fd, &fput_needed);
  740. if (file) {
  741. ret = -ESPIPE;
  742. if (file->f_mode & FMODE_PREAD)
  743. ret = vfs_readv(file, vec, vlen, &pos);
  744. fput_light(file, fput_needed);
  745. }
  746. if (ret > 0)
  747. add_rchar(current, ret);
  748. inc_syscr(current);
  749. return ret;
  750. }
  751. SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
  752. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  753. {
  754. loff_t pos = pos_from_hilo(pos_h, pos_l);
  755. struct file *file;
  756. ssize_t ret = -EBADF;
  757. int fput_needed;
  758. if (pos < 0)
  759. return -EINVAL;
  760. file = fget_light(fd, &fput_needed);
  761. if (file) {
  762. ret = -ESPIPE;
  763. if (file->f_mode & FMODE_PWRITE)
  764. ret = vfs_writev(file, vec, vlen, &pos);
  765. fput_light(file, fput_needed);
  766. }
  767. if (ret > 0)
  768. add_wchar(current, ret);
  769. inc_syscw(current);
  770. return ret;
  771. }
  772. static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
  773. size_t count, loff_t max)
  774. {
  775. struct file * in_file, * out_file;
  776. struct inode * in_inode, * out_inode;
  777. loff_t pos;
  778. ssize_t retval;
  779. int fput_needed_in, fput_needed_out, fl;
  780. /*
  781. * Get input file, and verify that it is ok..
  782. */
  783. retval = -EBADF;
  784. in_file = fget_light(in_fd, &fput_needed_in);
  785. if (!in_file)
  786. goto out;
  787. if (!(in_file->f_mode & FMODE_READ))
  788. goto fput_in;
  789. retval = -ESPIPE;
  790. if (!ppos)
  791. ppos = &in_file->f_pos;
  792. else
  793. if (!(in_file->f_mode & FMODE_PREAD))
  794. goto fput_in;
  795. retval = rw_verify_area(READ, in_file, ppos, count);
  796. if (retval < 0)
  797. goto fput_in;
  798. count = retval;
  799. /*
  800. * Get output file, and verify that it is ok..
  801. */
  802. retval = -EBADF;
  803. out_file = fget_light(out_fd, &fput_needed_out);
  804. if (!out_file)
  805. goto fput_in;
  806. if (!(out_file->f_mode & FMODE_WRITE))
  807. goto fput_out;
  808. retval = -EINVAL;
  809. in_inode = in_file->f_path.dentry->d_inode;
  810. out_inode = out_file->f_path.dentry->d_inode;
  811. retval = rw_verify_area(WRITE, out_file, &out_file->f_pos, count);
  812. if (retval < 0)
  813. goto fput_out;
  814. count = retval;
  815. if (!max)
  816. max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
  817. pos = *ppos;
  818. if (unlikely(pos + count > max)) {
  819. retval = -EOVERFLOW;
  820. if (pos >= max)
  821. goto fput_out;
  822. count = max - pos;
  823. }
  824. fl = 0;
  825. #if 0
  826. /*
  827. * We need to debate whether we can enable this or not. The
  828. * man page documents EAGAIN return for the output at least,
  829. * and the application is arguably buggy if it doesn't expect
  830. * EAGAIN on a non-blocking file descriptor.
  831. */
  832. if (in_file->f_flags & O_NONBLOCK)
  833. fl = SPLICE_F_NONBLOCK;
  834. #endif
  835. retval = do_splice_direct(in_file, ppos, out_file, count, fl);
  836. if (retval > 0) {
  837. add_rchar(current, retval);
  838. add_wchar(current, retval);
  839. }
  840. inc_syscr(current);
  841. inc_syscw(current);
  842. if (*ppos > max)
  843. retval = -EOVERFLOW;
  844. fput_out:
  845. fput_light(out_file, fput_needed_out);
  846. fput_in:
  847. fput_light(in_file, fput_needed_in);
  848. out:
  849. return retval;
  850. }
  851. SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
  852. {
  853. loff_t pos;
  854. off_t off;
  855. ssize_t ret;
  856. if (offset) {
  857. if (unlikely(get_user(off, offset)))
  858. return -EFAULT;
  859. pos = off;
  860. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  861. if (unlikely(put_user(pos, offset)))
  862. return -EFAULT;
  863. return ret;
  864. }
  865. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  866. }
  867. SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
  868. {
  869. loff_t pos;
  870. ssize_t ret;
  871. if (offset) {
  872. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  873. return -EFAULT;
  874. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  875. if (unlikely(put_user(pos, offset)))
  876. return -EFAULT;
  877. return ret;
  878. }
  879. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  880. }