read_write.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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 <linux/compat.h>
  18. #include "read_write.h"
  19. #include "internal.h"
  20. #include <asm/uaccess.h>
  21. #include <asm/unistd.h>
  22. const struct file_operations generic_ro_fops = {
  23. .llseek = generic_file_llseek,
  24. .read = do_sync_read,
  25. .aio_read = generic_file_aio_read,
  26. .mmap = generic_file_readonly_mmap,
  27. .splice_read = generic_file_splice_read,
  28. };
  29. EXPORT_SYMBOL(generic_ro_fops);
  30. static inline int unsigned_offsets(struct file *file)
  31. {
  32. return file->f_mode & FMODE_UNSIGNED_OFFSET;
  33. }
  34. static loff_t lseek_execute(struct file *file, struct inode *inode,
  35. loff_t offset, loff_t maxsize)
  36. {
  37. if (offset < 0 && !unsigned_offsets(file))
  38. return -EINVAL;
  39. if (offset > maxsize)
  40. return -EINVAL;
  41. if (offset != file->f_pos) {
  42. file->f_pos = offset;
  43. file->f_version = 0;
  44. }
  45. return offset;
  46. }
  47. /**
  48. * generic_file_llseek_size - generic llseek implementation for regular files
  49. * @file: file structure to seek on
  50. * @offset: file offset to seek to
  51. * @whence: type of seek
  52. * @size: max size of this file in file system
  53. * @eof: offset used for SEEK_END position
  54. *
  55. * This is a variant of generic_file_llseek that allows passing in a custom
  56. * maximum file size and a custom EOF position, for e.g. hashed directories
  57. *
  58. * Synchronization:
  59. * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
  60. * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
  61. * read/writes behave like SEEK_SET against seeks.
  62. */
  63. loff_t
  64. generic_file_llseek_size(struct file *file, loff_t offset, int whence,
  65. loff_t maxsize, loff_t eof)
  66. {
  67. struct inode *inode = file->f_mapping->host;
  68. switch (whence) {
  69. case SEEK_END:
  70. offset += eof;
  71. break;
  72. case SEEK_CUR:
  73. /*
  74. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  75. * position-querying operation. Avoid rewriting the "same"
  76. * f_pos value back to the file because a concurrent read(),
  77. * write() or lseek() might have altered it
  78. */
  79. if (offset == 0)
  80. return file->f_pos;
  81. /*
  82. * f_lock protects against read/modify/write race with other
  83. * SEEK_CURs. Note that parallel writes and reads behave
  84. * like SEEK_SET.
  85. */
  86. spin_lock(&file->f_lock);
  87. offset = lseek_execute(file, inode, file->f_pos + offset,
  88. maxsize);
  89. spin_unlock(&file->f_lock);
  90. return offset;
  91. case SEEK_DATA:
  92. /*
  93. * In the generic case the entire file is data, so as long as
  94. * offset isn't at the end of the file then the offset is data.
  95. */
  96. if (offset >= eof)
  97. return -ENXIO;
  98. break;
  99. case SEEK_HOLE:
  100. /*
  101. * There is a virtual hole at the end of the file, so as long as
  102. * offset isn't i_size or larger, return i_size.
  103. */
  104. if (offset >= eof)
  105. return -ENXIO;
  106. offset = eof;
  107. break;
  108. }
  109. return lseek_execute(file, inode, offset, maxsize);
  110. }
  111. EXPORT_SYMBOL(generic_file_llseek_size);
  112. /**
  113. * generic_file_llseek - generic llseek implementation for regular files
  114. * @file: file structure to seek on
  115. * @offset: file offset to seek to
  116. * @whence: type of seek
  117. *
  118. * This is a generic implemenation of ->llseek useable for all normal local
  119. * filesystems. It just updates the file offset to the value specified by
  120. * @offset and @whence.
  121. */
  122. loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
  123. {
  124. struct inode *inode = file->f_mapping->host;
  125. return generic_file_llseek_size(file, offset, whence,
  126. inode->i_sb->s_maxbytes,
  127. i_size_read(inode));
  128. }
  129. EXPORT_SYMBOL(generic_file_llseek);
  130. /**
  131. * noop_llseek - No Operation Performed llseek implementation
  132. * @file: file structure to seek on
  133. * @offset: file offset to seek to
  134. * @whence: type of seek
  135. *
  136. * This is an implementation of ->llseek useable for the rare special case when
  137. * userspace expects the seek to succeed but the (device) file is actually not
  138. * able to perform the seek. In this case you use noop_llseek() instead of
  139. * falling back to the default implementation of ->llseek.
  140. */
  141. loff_t noop_llseek(struct file *file, loff_t offset, int whence)
  142. {
  143. return file->f_pos;
  144. }
  145. EXPORT_SYMBOL(noop_llseek);
  146. loff_t no_llseek(struct file *file, loff_t offset, int whence)
  147. {
  148. return -ESPIPE;
  149. }
  150. EXPORT_SYMBOL(no_llseek);
  151. loff_t default_llseek(struct file *file, loff_t offset, int whence)
  152. {
  153. struct inode *inode = file_inode(file);
  154. loff_t retval;
  155. mutex_lock(&inode->i_mutex);
  156. switch (whence) {
  157. case SEEK_END:
  158. offset += i_size_read(inode);
  159. break;
  160. case SEEK_CUR:
  161. if (offset == 0) {
  162. retval = file->f_pos;
  163. goto out;
  164. }
  165. offset += file->f_pos;
  166. break;
  167. case SEEK_DATA:
  168. /*
  169. * In the generic case the entire file is data, so as
  170. * long as offset isn't at the end of the file then the
  171. * offset is data.
  172. */
  173. if (offset >= inode->i_size) {
  174. retval = -ENXIO;
  175. goto out;
  176. }
  177. break;
  178. case SEEK_HOLE:
  179. /*
  180. * There is a virtual hole at the end of the file, so
  181. * as long as offset isn't i_size or larger, return
  182. * i_size.
  183. */
  184. if (offset >= inode->i_size) {
  185. retval = -ENXIO;
  186. goto out;
  187. }
  188. offset = inode->i_size;
  189. break;
  190. }
  191. retval = -EINVAL;
  192. if (offset >= 0 || unsigned_offsets(file)) {
  193. if (offset != file->f_pos) {
  194. file->f_pos = offset;
  195. file->f_version = 0;
  196. }
  197. retval = offset;
  198. }
  199. out:
  200. mutex_unlock(&inode->i_mutex);
  201. return retval;
  202. }
  203. EXPORT_SYMBOL(default_llseek);
  204. loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
  205. {
  206. loff_t (*fn)(struct file *, loff_t, int);
  207. fn = no_llseek;
  208. if (file->f_mode & FMODE_LSEEK) {
  209. if (file->f_op && file->f_op->llseek)
  210. fn = file->f_op->llseek;
  211. }
  212. return fn(file, offset, whence);
  213. }
  214. EXPORT_SYMBOL(vfs_llseek);
  215. SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
  216. {
  217. off_t retval;
  218. struct fd f = fdget(fd);
  219. if (!f.file)
  220. return -EBADF;
  221. retval = -EINVAL;
  222. if (whence <= SEEK_MAX) {
  223. loff_t res = vfs_llseek(f.file, offset, whence);
  224. retval = res;
  225. if (res != (loff_t)retval)
  226. retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
  227. }
  228. fdput(f);
  229. return retval;
  230. }
  231. #ifdef CONFIG_COMPAT
  232. COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
  233. {
  234. return sys_lseek(fd, offset, whence);
  235. }
  236. #endif
  237. #ifdef __ARCH_WANT_SYS_LLSEEK
  238. SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
  239. unsigned long, offset_low, loff_t __user *, result,
  240. unsigned int, whence)
  241. {
  242. int retval;
  243. struct fd f = fdget(fd);
  244. loff_t offset;
  245. if (!f.file)
  246. return -EBADF;
  247. retval = -EINVAL;
  248. if (whence > SEEK_MAX)
  249. goto out_putf;
  250. offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
  251. whence);
  252. retval = (int)offset;
  253. if (offset >= 0) {
  254. retval = -EFAULT;
  255. if (!copy_to_user(result, &offset, sizeof(offset)))
  256. retval = 0;
  257. }
  258. out_putf:
  259. fdput(f);
  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_inode(file);
  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 __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
  376. {
  377. mm_segment_t old_fs;
  378. const char __user *p;
  379. ssize_t ret;
  380. if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
  381. return -EINVAL;
  382. old_fs = get_fs();
  383. set_fs(get_ds());
  384. p = (__force const char __user *)buf;
  385. if (count > MAX_RW_COUNT)
  386. count = MAX_RW_COUNT;
  387. if (file->f_op->write)
  388. ret = file->f_op->write(file, p, count, pos);
  389. else
  390. ret = do_sync_write(file, p, count, pos);
  391. set_fs(old_fs);
  392. if (ret > 0) {
  393. fsnotify_modify(file);
  394. add_wchar(current, ret);
  395. }
  396. inc_syscw(current);
  397. return ret;
  398. }
  399. ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
  400. {
  401. ssize_t ret;
  402. if (!(file->f_mode & FMODE_WRITE))
  403. return -EBADF;
  404. if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
  405. return -EINVAL;
  406. if (unlikely(!access_ok(VERIFY_READ, buf, count)))
  407. return -EFAULT;
  408. ret = rw_verify_area(WRITE, file, pos, count);
  409. if (ret >= 0) {
  410. count = ret;
  411. if (file->f_op->write)
  412. ret = file->f_op->write(file, buf, count, pos);
  413. else
  414. ret = do_sync_write(file, buf, count, pos);
  415. if (ret > 0) {
  416. fsnotify_modify(file);
  417. add_wchar(current, ret);
  418. }
  419. inc_syscw(current);
  420. }
  421. return ret;
  422. }
  423. EXPORT_SYMBOL(vfs_write);
  424. static inline loff_t file_pos_read(struct file *file)
  425. {
  426. return file->f_pos;
  427. }
  428. static inline void file_pos_write(struct file *file, loff_t pos)
  429. {
  430. file->f_pos = pos;
  431. }
  432. SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
  433. {
  434. struct fd f = fdget(fd);
  435. ssize_t ret = -EBADF;
  436. if (f.file) {
  437. loff_t pos = file_pos_read(f.file);
  438. ret = vfs_read(f.file, buf, count, &pos);
  439. file_pos_write(f.file, pos);
  440. fdput(f);
  441. }
  442. return ret;
  443. }
  444. SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
  445. size_t, count)
  446. {
  447. struct fd f = fdget(fd);
  448. ssize_t ret = -EBADF;
  449. if (f.file) {
  450. loff_t pos = file_pos_read(f.file);
  451. ret = vfs_write(f.file, buf, count, &pos);
  452. file_pos_write(f.file, pos);
  453. fdput(f);
  454. }
  455. return ret;
  456. }
  457. SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
  458. size_t, count, loff_t, pos)
  459. {
  460. struct fd f;
  461. ssize_t ret = -EBADF;
  462. if (pos < 0)
  463. return -EINVAL;
  464. f = fdget(fd);
  465. if (f.file) {
  466. ret = -ESPIPE;
  467. if (f.file->f_mode & FMODE_PREAD)
  468. ret = vfs_read(f.file, buf, count, &pos);
  469. fdput(f);
  470. }
  471. return ret;
  472. }
  473. SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
  474. size_t, count, loff_t, pos)
  475. {
  476. struct fd f;
  477. ssize_t ret = -EBADF;
  478. if (pos < 0)
  479. return -EINVAL;
  480. f = fdget(fd);
  481. if (f.file) {
  482. ret = -ESPIPE;
  483. if (f.file->f_mode & FMODE_PWRITE)
  484. ret = vfs_write(f.file, buf, count, &pos);
  485. fdput(f);
  486. }
  487. return ret;
  488. }
  489. /*
  490. * Reduce an iovec's length in-place. Return the resulting number of segments
  491. */
  492. unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
  493. {
  494. unsigned long seg = 0;
  495. size_t len = 0;
  496. while (seg < nr_segs) {
  497. seg++;
  498. if (len + iov->iov_len >= to) {
  499. iov->iov_len = to - len;
  500. break;
  501. }
  502. len += iov->iov_len;
  503. iov++;
  504. }
  505. return seg;
  506. }
  507. EXPORT_SYMBOL(iov_shorten);
  508. ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
  509. unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
  510. {
  511. struct kiocb kiocb;
  512. ssize_t ret;
  513. init_sync_kiocb(&kiocb, filp);
  514. kiocb.ki_pos = *ppos;
  515. kiocb.ki_left = len;
  516. kiocb.ki_nbytes = len;
  517. for (;;) {
  518. ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
  519. if (ret != -EIOCBRETRY)
  520. break;
  521. wait_on_retry_sync_kiocb(&kiocb);
  522. }
  523. if (ret == -EIOCBQUEUED)
  524. ret = wait_on_sync_kiocb(&kiocb);
  525. *ppos = kiocb.ki_pos;
  526. return ret;
  527. }
  528. /* Do it by hand, with file-ops */
  529. ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
  530. unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
  531. {
  532. struct iovec *vector = iov;
  533. ssize_t ret = 0;
  534. while (nr_segs > 0) {
  535. void __user *base;
  536. size_t len;
  537. ssize_t nr;
  538. base = vector->iov_base;
  539. len = vector->iov_len;
  540. vector++;
  541. nr_segs--;
  542. nr = fn(filp, base, len, ppos);
  543. if (nr < 0) {
  544. if (!ret)
  545. ret = nr;
  546. break;
  547. }
  548. ret += nr;
  549. if (nr != len)
  550. break;
  551. }
  552. return ret;
  553. }
  554. /* A write operation does a read from user space and vice versa */
  555. #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
  556. ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
  557. unsigned long nr_segs, unsigned long fast_segs,
  558. struct iovec *fast_pointer,
  559. struct iovec **ret_pointer)
  560. {
  561. unsigned long seg;
  562. ssize_t ret;
  563. struct iovec *iov = fast_pointer;
  564. /*
  565. * SuS says "The readv() function *may* fail if the iovcnt argument
  566. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  567. * traditionally returned zero for zero segments, so...
  568. */
  569. if (nr_segs == 0) {
  570. ret = 0;
  571. goto out;
  572. }
  573. /*
  574. * First get the "struct iovec" from user memory and
  575. * verify all the pointers
  576. */
  577. if (nr_segs > UIO_MAXIOV) {
  578. ret = -EINVAL;
  579. goto out;
  580. }
  581. if (nr_segs > fast_segs) {
  582. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  583. if (iov == NULL) {
  584. ret = -ENOMEM;
  585. goto out;
  586. }
  587. }
  588. if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
  589. ret = -EFAULT;
  590. goto out;
  591. }
  592. /*
  593. * According to the Single Unix Specification we should return EINVAL
  594. * if an element length is < 0 when cast to ssize_t or if the
  595. * total length would overflow the ssize_t return value of the
  596. * system call.
  597. *
  598. * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
  599. * overflow case.
  600. */
  601. ret = 0;
  602. for (seg = 0; seg < nr_segs; seg++) {
  603. void __user *buf = iov[seg].iov_base;
  604. ssize_t len = (ssize_t)iov[seg].iov_len;
  605. /* see if we we're about to use an invalid len or if
  606. * it's about to overflow ssize_t */
  607. if (len < 0) {
  608. ret = -EINVAL;
  609. goto out;
  610. }
  611. if (type >= 0
  612. && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
  613. ret = -EFAULT;
  614. goto out;
  615. }
  616. if (len > MAX_RW_COUNT - ret) {
  617. len = MAX_RW_COUNT - ret;
  618. iov[seg].iov_len = len;
  619. }
  620. ret += len;
  621. }
  622. out:
  623. *ret_pointer = iov;
  624. return ret;
  625. }
  626. static ssize_t do_readv_writev(int type, struct file *file,
  627. const struct iovec __user * uvector,
  628. unsigned long nr_segs, loff_t *pos)
  629. {
  630. size_t tot_len;
  631. struct iovec iovstack[UIO_FASTIOV];
  632. struct iovec *iov = iovstack;
  633. ssize_t ret;
  634. io_fn_t fn;
  635. iov_fn_t fnv;
  636. if (!file->f_op) {
  637. ret = -EINVAL;
  638. goto out;
  639. }
  640. ret = rw_copy_check_uvector(type, uvector, nr_segs,
  641. ARRAY_SIZE(iovstack), iovstack, &iov);
  642. if (ret <= 0)
  643. goto out;
  644. tot_len = ret;
  645. ret = rw_verify_area(type, file, pos, tot_len);
  646. if (ret < 0)
  647. goto out;
  648. fnv = NULL;
  649. if (type == READ) {
  650. fn = file->f_op->read;
  651. fnv = file->f_op->aio_read;
  652. } else {
  653. fn = (io_fn_t)file->f_op->write;
  654. fnv = file->f_op->aio_write;
  655. }
  656. if (fnv)
  657. ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
  658. pos, fnv);
  659. else
  660. ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
  661. out:
  662. if (iov != iovstack)
  663. kfree(iov);
  664. if ((ret + (type == READ)) > 0) {
  665. if (type == READ)
  666. fsnotify_access(file);
  667. else
  668. fsnotify_modify(file);
  669. }
  670. return ret;
  671. }
  672. ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
  673. unsigned long vlen, loff_t *pos)
  674. {
  675. if (!(file->f_mode & FMODE_READ))
  676. return -EBADF;
  677. if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
  678. return -EINVAL;
  679. return do_readv_writev(READ, file, vec, vlen, pos);
  680. }
  681. EXPORT_SYMBOL(vfs_readv);
  682. ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
  683. unsigned long vlen, loff_t *pos)
  684. {
  685. if (!(file->f_mode & FMODE_WRITE))
  686. return -EBADF;
  687. if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
  688. return -EINVAL;
  689. return do_readv_writev(WRITE, file, vec, vlen, pos);
  690. }
  691. EXPORT_SYMBOL(vfs_writev);
  692. SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
  693. unsigned long, vlen)
  694. {
  695. struct fd f = fdget(fd);
  696. ssize_t ret = -EBADF;
  697. if (f.file) {
  698. loff_t pos = file_pos_read(f.file);
  699. ret = vfs_readv(f.file, vec, vlen, &pos);
  700. file_pos_write(f.file, pos);
  701. fdput(f);
  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 fd f = fdget(fd);
  712. ssize_t ret = -EBADF;
  713. if (f.file) {
  714. loff_t pos = file_pos_read(f.file);
  715. ret = vfs_writev(f.file, vec, vlen, &pos);
  716. file_pos_write(f.file, pos);
  717. fdput(f);
  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 fd f;
  734. ssize_t ret = -EBADF;
  735. if (pos < 0)
  736. return -EINVAL;
  737. f = fdget(fd);
  738. if (f.file) {
  739. ret = -ESPIPE;
  740. if (f.file->f_mode & FMODE_PREAD)
  741. ret = vfs_readv(f.file, vec, vlen, &pos);
  742. fdput(f);
  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 fd f;
  754. ssize_t ret = -EBADF;
  755. if (pos < 0)
  756. return -EINVAL;
  757. f = fdget(fd);
  758. if (f.file) {
  759. ret = -ESPIPE;
  760. if (f.file->f_mode & FMODE_PWRITE)
  761. ret = vfs_writev(f.file, vec, vlen, &pos);
  762. fdput(f);
  763. }
  764. if (ret > 0)
  765. add_wchar(current, ret);
  766. inc_syscw(current);
  767. return ret;
  768. }
  769. static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
  770. size_t count, loff_t max)
  771. {
  772. struct fd in, out;
  773. struct inode *in_inode, *out_inode;
  774. loff_t pos;
  775. ssize_t retval;
  776. int fl;
  777. /*
  778. * Get input file, and verify that it is ok..
  779. */
  780. retval = -EBADF;
  781. in = fdget(in_fd);
  782. if (!in.file)
  783. goto out;
  784. if (!(in.file->f_mode & FMODE_READ))
  785. goto fput_in;
  786. retval = -ESPIPE;
  787. if (!ppos)
  788. ppos = &in.file->f_pos;
  789. else
  790. if (!(in.file->f_mode & FMODE_PREAD))
  791. goto fput_in;
  792. retval = rw_verify_area(READ, in.file, ppos, count);
  793. if (retval < 0)
  794. goto fput_in;
  795. count = retval;
  796. /*
  797. * Get output file, and verify that it is ok..
  798. */
  799. retval = -EBADF;
  800. out = fdget(out_fd);
  801. if (!out.file)
  802. goto fput_in;
  803. if (!(out.file->f_mode & FMODE_WRITE))
  804. goto fput_out;
  805. retval = -EINVAL;
  806. in_inode = file_inode(in.file);
  807. out_inode = file_inode(out.file);
  808. retval = rw_verify_area(WRITE, out.file, &out.file->f_pos, count);
  809. if (retval < 0)
  810. goto fput_out;
  811. count = retval;
  812. if (!max)
  813. max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
  814. pos = *ppos;
  815. if (unlikely(pos + count > max)) {
  816. retval = -EOVERFLOW;
  817. if (pos >= max)
  818. goto fput_out;
  819. count = max - pos;
  820. }
  821. fl = 0;
  822. #if 0
  823. /*
  824. * We need to debate whether we can enable this or not. The
  825. * man page documents EAGAIN return for the output at least,
  826. * and the application is arguably buggy if it doesn't expect
  827. * EAGAIN on a non-blocking file descriptor.
  828. */
  829. if (in.file->f_flags & O_NONBLOCK)
  830. fl = SPLICE_F_NONBLOCK;
  831. #endif
  832. retval = do_splice_direct(in.file, ppos, out.file, count, fl);
  833. if (retval > 0) {
  834. add_rchar(current, retval);
  835. add_wchar(current, retval);
  836. fsnotify_access(in.file);
  837. fsnotify_modify(out.file);
  838. }
  839. inc_syscr(current);
  840. inc_syscw(current);
  841. if (*ppos > max)
  842. retval = -EOVERFLOW;
  843. fput_out:
  844. fdput(out);
  845. fput_in:
  846. fdput(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. }
  880. #ifdef CONFIG_COMPAT
  881. COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
  882. compat_off_t __user *, offset, compat_size_t, count)
  883. {
  884. loff_t pos;
  885. off_t off;
  886. ssize_t ret;
  887. if (offset) {
  888. if (unlikely(get_user(off, offset)))
  889. return -EFAULT;
  890. pos = off;
  891. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  892. if (unlikely(put_user(pos, offset)))
  893. return -EFAULT;
  894. return ret;
  895. }
  896. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  897. }
  898. COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
  899. compat_loff_t __user *, offset, compat_size_t, count)
  900. {
  901. loff_t pos;
  902. ssize_t ret;
  903. if (offset) {
  904. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  905. return -EFAULT;
  906. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  907. if (unlikely(put_user(pos, offset)))
  908. return -EFAULT;
  909. return ret;
  910. }
  911. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  912. }
  913. #endif