read_write.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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 under i_mutex.
  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_DEFINE(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. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  474. asmlinkage long SyS_pread64(long fd, long buf, long count, loff_t pos)
  475. {
  476. return SYSC_pread64((unsigned int) fd, (char __user *) buf,
  477. (size_t) count, pos);
  478. }
  479. SYSCALL_ALIAS(sys_pread64, SyS_pread64);
  480. #endif
  481. SYSCALL_DEFINE(pwrite64)(unsigned int fd, const char __user *buf,
  482. size_t count, loff_t pos)
  483. {
  484. struct fd f;
  485. ssize_t ret = -EBADF;
  486. if (pos < 0)
  487. return -EINVAL;
  488. f = fdget(fd);
  489. if (f.file) {
  490. ret = -ESPIPE;
  491. if (f.file->f_mode & FMODE_PWRITE)
  492. ret = vfs_write(f.file, buf, count, &pos);
  493. fdput(f);
  494. }
  495. return ret;
  496. }
  497. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  498. asmlinkage long SyS_pwrite64(long fd, long buf, long count, loff_t pos)
  499. {
  500. return SYSC_pwrite64((unsigned int) fd, (const char __user *) buf,
  501. (size_t) count, pos);
  502. }
  503. SYSCALL_ALIAS(sys_pwrite64, SyS_pwrite64);
  504. #endif
  505. /*
  506. * Reduce an iovec's length in-place. Return the resulting number of segments
  507. */
  508. unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
  509. {
  510. unsigned long seg = 0;
  511. size_t len = 0;
  512. while (seg < nr_segs) {
  513. seg++;
  514. if (len + iov->iov_len >= to) {
  515. iov->iov_len = to - len;
  516. break;
  517. }
  518. len += iov->iov_len;
  519. iov++;
  520. }
  521. return seg;
  522. }
  523. EXPORT_SYMBOL(iov_shorten);
  524. ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
  525. unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
  526. {
  527. struct kiocb kiocb;
  528. ssize_t ret;
  529. init_sync_kiocb(&kiocb, filp);
  530. kiocb.ki_pos = *ppos;
  531. kiocb.ki_left = len;
  532. kiocb.ki_nbytes = len;
  533. for (;;) {
  534. ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
  535. if (ret != -EIOCBRETRY)
  536. break;
  537. wait_on_retry_sync_kiocb(&kiocb);
  538. }
  539. if (ret == -EIOCBQUEUED)
  540. ret = wait_on_sync_kiocb(&kiocb);
  541. *ppos = kiocb.ki_pos;
  542. return ret;
  543. }
  544. /* Do it by hand, with file-ops */
  545. ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
  546. unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
  547. {
  548. struct iovec *vector = iov;
  549. ssize_t ret = 0;
  550. while (nr_segs > 0) {
  551. void __user *base;
  552. size_t len;
  553. ssize_t nr;
  554. base = vector->iov_base;
  555. len = vector->iov_len;
  556. vector++;
  557. nr_segs--;
  558. nr = fn(filp, base, len, ppos);
  559. if (nr < 0) {
  560. if (!ret)
  561. ret = nr;
  562. break;
  563. }
  564. ret += nr;
  565. if (nr != len)
  566. break;
  567. }
  568. return ret;
  569. }
  570. /* A write operation does a read from user space and vice versa */
  571. #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
  572. ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
  573. unsigned long nr_segs, unsigned long fast_segs,
  574. struct iovec *fast_pointer,
  575. struct iovec **ret_pointer)
  576. {
  577. unsigned long seg;
  578. ssize_t ret;
  579. struct iovec *iov = fast_pointer;
  580. /*
  581. * SuS says "The readv() function *may* fail if the iovcnt argument
  582. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  583. * traditionally returned zero for zero segments, so...
  584. */
  585. if (nr_segs == 0) {
  586. ret = 0;
  587. goto out;
  588. }
  589. /*
  590. * First get the "struct iovec" from user memory and
  591. * verify all the pointers
  592. */
  593. if (nr_segs > UIO_MAXIOV) {
  594. ret = -EINVAL;
  595. goto out;
  596. }
  597. if (nr_segs > fast_segs) {
  598. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  599. if (iov == NULL) {
  600. ret = -ENOMEM;
  601. goto out;
  602. }
  603. }
  604. if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
  605. ret = -EFAULT;
  606. goto out;
  607. }
  608. /*
  609. * According to the Single Unix Specification we should return EINVAL
  610. * if an element length is < 0 when cast to ssize_t or if the
  611. * total length would overflow the ssize_t return value of the
  612. * system call.
  613. *
  614. * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
  615. * overflow case.
  616. */
  617. ret = 0;
  618. for (seg = 0; seg < nr_segs; seg++) {
  619. void __user *buf = iov[seg].iov_base;
  620. ssize_t len = (ssize_t)iov[seg].iov_len;
  621. /* see if we we're about to use an invalid len or if
  622. * it's about to overflow ssize_t */
  623. if (len < 0) {
  624. ret = -EINVAL;
  625. goto out;
  626. }
  627. if (type >= 0
  628. && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
  629. ret = -EFAULT;
  630. goto out;
  631. }
  632. if (len > MAX_RW_COUNT - ret) {
  633. len = MAX_RW_COUNT - ret;
  634. iov[seg].iov_len = len;
  635. }
  636. ret += len;
  637. }
  638. out:
  639. *ret_pointer = iov;
  640. return ret;
  641. }
  642. static ssize_t do_readv_writev(int type, struct file *file,
  643. const struct iovec __user * uvector,
  644. unsigned long nr_segs, loff_t *pos)
  645. {
  646. size_t tot_len;
  647. struct iovec iovstack[UIO_FASTIOV];
  648. struct iovec *iov = iovstack;
  649. ssize_t ret;
  650. io_fn_t fn;
  651. iov_fn_t fnv;
  652. if (!file->f_op) {
  653. ret = -EINVAL;
  654. goto out;
  655. }
  656. ret = rw_copy_check_uvector(type, uvector, nr_segs,
  657. ARRAY_SIZE(iovstack), iovstack, &iov);
  658. if (ret <= 0)
  659. goto out;
  660. tot_len = ret;
  661. ret = rw_verify_area(type, file, pos, tot_len);
  662. if (ret < 0)
  663. goto out;
  664. fnv = NULL;
  665. if (type == READ) {
  666. fn = file->f_op->read;
  667. fnv = file->f_op->aio_read;
  668. } else {
  669. fn = (io_fn_t)file->f_op->write;
  670. fnv = file->f_op->aio_write;
  671. }
  672. if (fnv)
  673. ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
  674. pos, fnv);
  675. else
  676. ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
  677. out:
  678. if (iov != iovstack)
  679. kfree(iov);
  680. if ((ret + (type == READ)) > 0) {
  681. if (type == READ)
  682. fsnotify_access(file);
  683. else
  684. fsnotify_modify(file);
  685. }
  686. return ret;
  687. }
  688. ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
  689. unsigned long vlen, loff_t *pos)
  690. {
  691. if (!(file->f_mode & FMODE_READ))
  692. return -EBADF;
  693. if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
  694. return -EINVAL;
  695. return do_readv_writev(READ, file, vec, vlen, pos);
  696. }
  697. EXPORT_SYMBOL(vfs_readv);
  698. ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
  699. unsigned long vlen, loff_t *pos)
  700. {
  701. if (!(file->f_mode & FMODE_WRITE))
  702. return -EBADF;
  703. if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
  704. return -EINVAL;
  705. return do_readv_writev(WRITE, file, vec, vlen, pos);
  706. }
  707. EXPORT_SYMBOL(vfs_writev);
  708. SYSCALL_DEFINE3(readv, 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_readv(f.file, vec, vlen, &pos);
  716. file_pos_write(f.file, pos);
  717. fdput(f);
  718. }
  719. if (ret > 0)
  720. add_rchar(current, ret);
  721. inc_syscr(current);
  722. return ret;
  723. }
  724. SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
  725. unsigned long, vlen)
  726. {
  727. struct fd f = fdget(fd);
  728. ssize_t ret = -EBADF;
  729. if (f.file) {
  730. loff_t pos = file_pos_read(f.file);
  731. ret = vfs_writev(f.file, vec, vlen, &pos);
  732. file_pos_write(f.file, pos);
  733. fdput(f);
  734. }
  735. if (ret > 0)
  736. add_wchar(current, ret);
  737. inc_syscw(current);
  738. return ret;
  739. }
  740. static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
  741. {
  742. #define HALF_LONG_BITS (BITS_PER_LONG / 2)
  743. return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
  744. }
  745. SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
  746. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  747. {
  748. loff_t pos = pos_from_hilo(pos_h, pos_l);
  749. struct fd f;
  750. ssize_t ret = -EBADF;
  751. if (pos < 0)
  752. return -EINVAL;
  753. f = fdget(fd);
  754. if (f.file) {
  755. ret = -ESPIPE;
  756. if (f.file->f_mode & FMODE_PREAD)
  757. ret = vfs_readv(f.file, vec, vlen, &pos);
  758. fdput(f);
  759. }
  760. if (ret > 0)
  761. add_rchar(current, ret);
  762. inc_syscr(current);
  763. return ret;
  764. }
  765. SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
  766. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  767. {
  768. loff_t pos = pos_from_hilo(pos_h, pos_l);
  769. struct fd f;
  770. ssize_t ret = -EBADF;
  771. if (pos < 0)
  772. return -EINVAL;
  773. f = fdget(fd);
  774. if (f.file) {
  775. ret = -ESPIPE;
  776. if (f.file->f_mode & FMODE_PWRITE)
  777. ret = vfs_writev(f.file, vec, vlen, &pos);
  778. fdput(f);
  779. }
  780. if (ret > 0)
  781. add_wchar(current, ret);
  782. inc_syscw(current);
  783. return ret;
  784. }
  785. ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, size_t count,
  786. loff_t max)
  787. {
  788. struct fd in, out;
  789. struct inode *in_inode, *out_inode;
  790. loff_t pos;
  791. ssize_t retval;
  792. int fl;
  793. /*
  794. * Get input file, and verify that it is ok..
  795. */
  796. retval = -EBADF;
  797. in = fdget(in_fd);
  798. if (!in.file)
  799. goto out;
  800. if (!(in.file->f_mode & FMODE_READ))
  801. goto fput_in;
  802. retval = -ESPIPE;
  803. if (!ppos)
  804. ppos = &in.file->f_pos;
  805. else
  806. if (!(in.file->f_mode & FMODE_PREAD))
  807. goto fput_in;
  808. retval = rw_verify_area(READ, in.file, ppos, count);
  809. if (retval < 0)
  810. goto fput_in;
  811. count = retval;
  812. /*
  813. * Get output file, and verify that it is ok..
  814. */
  815. retval = -EBADF;
  816. out = fdget(out_fd);
  817. if (!out.file)
  818. goto fput_in;
  819. if (!(out.file->f_mode & FMODE_WRITE))
  820. goto fput_out;
  821. retval = -EINVAL;
  822. in_inode = file_inode(in.file);
  823. out_inode = file_inode(out.file);
  824. retval = rw_verify_area(WRITE, out.file, &out.file->f_pos, count);
  825. if (retval < 0)
  826. goto fput_out;
  827. count = retval;
  828. if (!max)
  829. max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
  830. pos = *ppos;
  831. if (unlikely(pos + count > max)) {
  832. retval = -EOVERFLOW;
  833. if (pos >= max)
  834. goto fput_out;
  835. count = max - pos;
  836. }
  837. fl = 0;
  838. #if 0
  839. /*
  840. * We need to debate whether we can enable this or not. The
  841. * man page documents EAGAIN return for the output at least,
  842. * and the application is arguably buggy if it doesn't expect
  843. * EAGAIN on a non-blocking file descriptor.
  844. */
  845. if (in.file->f_flags & O_NONBLOCK)
  846. fl = SPLICE_F_NONBLOCK;
  847. #endif
  848. retval = do_splice_direct(in.file, ppos, out.file, count, fl);
  849. if (retval > 0) {
  850. add_rchar(current, retval);
  851. add_wchar(current, retval);
  852. fsnotify_access(in.file);
  853. fsnotify_modify(out.file);
  854. }
  855. inc_syscr(current);
  856. inc_syscw(current);
  857. if (*ppos > max)
  858. retval = -EOVERFLOW;
  859. fput_out:
  860. fdput(out);
  861. fput_in:
  862. fdput(in);
  863. out:
  864. return retval;
  865. }
  866. SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
  867. {
  868. loff_t pos;
  869. off_t off;
  870. ssize_t ret;
  871. if (offset) {
  872. if (unlikely(get_user(off, offset)))
  873. return -EFAULT;
  874. pos = off;
  875. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  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. }
  882. SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
  883. {
  884. loff_t pos;
  885. ssize_t ret;
  886. if (offset) {
  887. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  888. return -EFAULT;
  889. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  890. if (unlikely(put_user(pos, offset)))
  891. return -EFAULT;
  892. return ret;
  893. }
  894. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  895. }