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