read_write.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  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. file_start_write(file);
  412. if (file->f_op->write)
  413. ret = file->f_op->write(file, buf, count, pos);
  414. else
  415. ret = do_sync_write(file, buf, count, pos);
  416. if (ret > 0) {
  417. fsnotify_modify(file);
  418. add_wchar(current, ret);
  419. }
  420. inc_syscw(current);
  421. file_end_write(file);
  422. }
  423. return ret;
  424. }
  425. EXPORT_SYMBOL(vfs_write);
  426. static inline loff_t file_pos_read(struct file *file)
  427. {
  428. return file->f_pos;
  429. }
  430. static inline void file_pos_write(struct file *file, loff_t pos)
  431. {
  432. file->f_pos = pos;
  433. }
  434. SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
  435. {
  436. struct fd f = fdget(fd);
  437. ssize_t ret = -EBADF;
  438. if (f.file) {
  439. loff_t pos = file_pos_read(f.file);
  440. ret = vfs_read(f.file, buf, count, &pos);
  441. file_pos_write(f.file, pos);
  442. fdput(f);
  443. }
  444. return ret;
  445. }
  446. SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
  447. size_t, count)
  448. {
  449. struct fd f = fdget(fd);
  450. ssize_t ret = -EBADF;
  451. if (f.file) {
  452. loff_t pos = file_pos_read(f.file);
  453. ret = vfs_write(f.file, buf, count, &pos);
  454. file_pos_write(f.file, pos);
  455. fdput(f);
  456. }
  457. return ret;
  458. }
  459. SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
  460. size_t, count, loff_t, pos)
  461. {
  462. struct fd f;
  463. ssize_t ret = -EBADF;
  464. if (pos < 0)
  465. return -EINVAL;
  466. f = fdget(fd);
  467. if (f.file) {
  468. ret = -ESPIPE;
  469. if (f.file->f_mode & FMODE_PREAD)
  470. ret = vfs_read(f.file, buf, count, &pos);
  471. fdput(f);
  472. }
  473. return ret;
  474. }
  475. SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
  476. size_t, count, loff_t, pos)
  477. {
  478. struct fd f;
  479. ssize_t ret = -EBADF;
  480. if (pos < 0)
  481. return -EINVAL;
  482. f = fdget(fd);
  483. if (f.file) {
  484. ret = -ESPIPE;
  485. if (f.file->f_mode & FMODE_PWRITE)
  486. ret = vfs_write(f.file, buf, count, &pos);
  487. fdput(f);
  488. }
  489. return ret;
  490. }
  491. /*
  492. * Reduce an iovec's length in-place. Return the resulting number of segments
  493. */
  494. unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
  495. {
  496. unsigned long seg = 0;
  497. size_t len = 0;
  498. while (seg < nr_segs) {
  499. seg++;
  500. if (len + iov->iov_len >= to) {
  501. iov->iov_len = to - len;
  502. break;
  503. }
  504. len += iov->iov_len;
  505. iov++;
  506. }
  507. return seg;
  508. }
  509. EXPORT_SYMBOL(iov_shorten);
  510. static ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
  511. unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
  512. {
  513. struct kiocb kiocb;
  514. ssize_t ret;
  515. init_sync_kiocb(&kiocb, filp);
  516. kiocb.ki_pos = *ppos;
  517. kiocb.ki_left = len;
  518. kiocb.ki_nbytes = len;
  519. for (;;) {
  520. ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
  521. if (ret != -EIOCBRETRY)
  522. break;
  523. wait_on_retry_sync_kiocb(&kiocb);
  524. }
  525. if (ret == -EIOCBQUEUED)
  526. ret = wait_on_sync_kiocb(&kiocb);
  527. *ppos = kiocb.ki_pos;
  528. return ret;
  529. }
  530. /* Do it by hand, with file-ops */
  531. static ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
  532. unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
  533. {
  534. struct iovec *vector = iov;
  535. ssize_t ret = 0;
  536. while (nr_segs > 0) {
  537. void __user *base;
  538. size_t len;
  539. ssize_t nr;
  540. base = vector->iov_base;
  541. len = vector->iov_len;
  542. vector++;
  543. nr_segs--;
  544. nr = fn(filp, base, len, ppos);
  545. if (nr < 0) {
  546. if (!ret)
  547. ret = nr;
  548. break;
  549. }
  550. ret += nr;
  551. if (nr != len)
  552. break;
  553. }
  554. return ret;
  555. }
  556. /* A write operation does a read from user space and vice versa */
  557. #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
  558. ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
  559. unsigned long nr_segs, unsigned long fast_segs,
  560. struct iovec *fast_pointer,
  561. struct iovec **ret_pointer)
  562. {
  563. unsigned long seg;
  564. ssize_t ret;
  565. struct iovec *iov = fast_pointer;
  566. /*
  567. * SuS says "The readv() function *may* fail if the iovcnt argument
  568. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  569. * traditionally returned zero for zero segments, so...
  570. */
  571. if (nr_segs == 0) {
  572. ret = 0;
  573. goto out;
  574. }
  575. /*
  576. * First get the "struct iovec" from user memory and
  577. * verify all the pointers
  578. */
  579. if (nr_segs > UIO_MAXIOV) {
  580. ret = -EINVAL;
  581. goto out;
  582. }
  583. if (nr_segs > fast_segs) {
  584. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  585. if (iov == NULL) {
  586. ret = -ENOMEM;
  587. goto out;
  588. }
  589. }
  590. if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
  591. ret = -EFAULT;
  592. goto out;
  593. }
  594. /*
  595. * According to the Single Unix Specification we should return EINVAL
  596. * if an element length is < 0 when cast to ssize_t or if the
  597. * total length would overflow the ssize_t return value of the
  598. * system call.
  599. *
  600. * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
  601. * overflow case.
  602. */
  603. ret = 0;
  604. for (seg = 0; seg < nr_segs; seg++) {
  605. void __user *buf = iov[seg].iov_base;
  606. ssize_t len = (ssize_t)iov[seg].iov_len;
  607. /* see if we we're about to use an invalid len or if
  608. * it's about to overflow ssize_t */
  609. if (len < 0) {
  610. ret = -EINVAL;
  611. goto out;
  612. }
  613. if (type >= 0
  614. && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
  615. ret = -EFAULT;
  616. goto out;
  617. }
  618. if (len > MAX_RW_COUNT - ret) {
  619. len = MAX_RW_COUNT - ret;
  620. iov[seg].iov_len = len;
  621. }
  622. ret += len;
  623. }
  624. out:
  625. *ret_pointer = iov;
  626. return ret;
  627. }
  628. static ssize_t do_readv_writev(int type, struct file *file,
  629. const struct iovec __user * uvector,
  630. unsigned long nr_segs, loff_t *pos)
  631. {
  632. size_t tot_len;
  633. struct iovec iovstack[UIO_FASTIOV];
  634. struct iovec *iov = iovstack;
  635. ssize_t ret;
  636. io_fn_t fn;
  637. iov_fn_t fnv;
  638. if (!file->f_op) {
  639. ret = -EINVAL;
  640. goto out;
  641. }
  642. ret = rw_copy_check_uvector(type, uvector, nr_segs,
  643. ARRAY_SIZE(iovstack), iovstack, &iov);
  644. if (ret <= 0)
  645. goto out;
  646. tot_len = ret;
  647. ret = rw_verify_area(type, file, pos, tot_len);
  648. if (ret < 0)
  649. goto out;
  650. fnv = NULL;
  651. if (type == READ) {
  652. fn = file->f_op->read;
  653. fnv = file->f_op->aio_read;
  654. } else {
  655. fn = (io_fn_t)file->f_op->write;
  656. fnv = file->f_op->aio_write;
  657. file_start_write(file);
  658. }
  659. if (fnv)
  660. ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
  661. pos, fnv);
  662. else
  663. ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
  664. if (type != READ)
  665. file_end_write(file);
  666. out:
  667. if (iov != iovstack)
  668. kfree(iov);
  669. if ((ret + (type == READ)) > 0) {
  670. if (type == READ)
  671. fsnotify_access(file);
  672. else
  673. fsnotify_modify(file);
  674. }
  675. return ret;
  676. }
  677. ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
  678. unsigned long vlen, loff_t *pos)
  679. {
  680. if (!(file->f_mode & FMODE_READ))
  681. return -EBADF;
  682. if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
  683. return -EINVAL;
  684. return do_readv_writev(READ, file, vec, vlen, pos);
  685. }
  686. EXPORT_SYMBOL(vfs_readv);
  687. ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
  688. unsigned long vlen, loff_t *pos)
  689. {
  690. if (!(file->f_mode & FMODE_WRITE))
  691. return -EBADF;
  692. if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
  693. return -EINVAL;
  694. return do_readv_writev(WRITE, file, vec, vlen, pos);
  695. }
  696. EXPORT_SYMBOL(vfs_writev);
  697. SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
  698. unsigned long, vlen)
  699. {
  700. struct fd f = fdget(fd);
  701. ssize_t ret = -EBADF;
  702. if (f.file) {
  703. loff_t pos = file_pos_read(f.file);
  704. ret = vfs_readv(f.file, vec, vlen, &pos);
  705. file_pos_write(f.file, pos);
  706. fdput(f);
  707. }
  708. if (ret > 0)
  709. add_rchar(current, ret);
  710. inc_syscr(current);
  711. return ret;
  712. }
  713. SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
  714. unsigned long, vlen)
  715. {
  716. struct fd f = fdget(fd);
  717. ssize_t ret = -EBADF;
  718. if (f.file) {
  719. loff_t pos = file_pos_read(f.file);
  720. ret = vfs_writev(f.file, vec, vlen, &pos);
  721. file_pos_write(f.file, pos);
  722. fdput(f);
  723. }
  724. if (ret > 0)
  725. add_wchar(current, ret);
  726. inc_syscw(current);
  727. return ret;
  728. }
  729. static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
  730. {
  731. #define HALF_LONG_BITS (BITS_PER_LONG / 2)
  732. return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
  733. }
  734. SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
  735. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  736. {
  737. loff_t pos = pos_from_hilo(pos_h, pos_l);
  738. struct fd f;
  739. ssize_t ret = -EBADF;
  740. if (pos < 0)
  741. return -EINVAL;
  742. f = fdget(fd);
  743. if (f.file) {
  744. ret = -ESPIPE;
  745. if (f.file->f_mode & FMODE_PREAD)
  746. ret = vfs_readv(f.file, vec, vlen, &pos);
  747. fdput(f);
  748. }
  749. if (ret > 0)
  750. add_rchar(current, ret);
  751. inc_syscr(current);
  752. return ret;
  753. }
  754. SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
  755. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  756. {
  757. loff_t pos = pos_from_hilo(pos_h, pos_l);
  758. struct fd f;
  759. ssize_t ret = -EBADF;
  760. if (pos < 0)
  761. return -EINVAL;
  762. f = fdget(fd);
  763. if (f.file) {
  764. ret = -ESPIPE;
  765. if (f.file->f_mode & FMODE_PWRITE)
  766. ret = vfs_writev(f.file, vec, vlen, &pos);
  767. fdput(f);
  768. }
  769. if (ret > 0)
  770. add_wchar(current, ret);
  771. inc_syscw(current);
  772. return ret;
  773. }
  774. #ifdef CONFIG_COMPAT
  775. static ssize_t compat_do_readv_writev(int type, struct file *file,
  776. const struct compat_iovec __user *uvector,
  777. unsigned long nr_segs, loff_t *pos)
  778. {
  779. compat_ssize_t tot_len;
  780. struct iovec iovstack[UIO_FASTIOV];
  781. struct iovec *iov = iovstack;
  782. ssize_t ret;
  783. io_fn_t fn;
  784. iov_fn_t fnv;
  785. ret = -EINVAL;
  786. if (!file->f_op)
  787. goto out;
  788. ret = -EFAULT;
  789. if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
  790. goto out;
  791. ret = compat_rw_copy_check_uvector(type, uvector, nr_segs,
  792. UIO_FASTIOV, iovstack, &iov);
  793. if (ret <= 0)
  794. goto out;
  795. tot_len = ret;
  796. ret = rw_verify_area(type, file, pos, tot_len);
  797. if (ret < 0)
  798. goto out;
  799. fnv = NULL;
  800. if (type == READ) {
  801. fn = file->f_op->read;
  802. fnv = file->f_op->aio_read;
  803. } else {
  804. fn = (io_fn_t)file->f_op->write;
  805. fnv = file->f_op->aio_write;
  806. file_start_write(file);
  807. }
  808. if (fnv)
  809. ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
  810. pos, fnv);
  811. else
  812. ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
  813. if (type != READ)
  814. file_end_write(file);
  815. out:
  816. if (iov != iovstack)
  817. kfree(iov);
  818. if ((ret + (type == READ)) > 0) {
  819. if (type == READ)
  820. fsnotify_access(file);
  821. else
  822. fsnotify_modify(file);
  823. }
  824. return ret;
  825. }
  826. static size_t compat_readv(struct file *file,
  827. const struct compat_iovec __user *vec,
  828. unsigned long vlen, loff_t *pos)
  829. {
  830. ssize_t ret = -EBADF;
  831. if (!(file->f_mode & FMODE_READ))
  832. goto out;
  833. ret = -EINVAL;
  834. if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
  835. goto out;
  836. ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
  837. out:
  838. if (ret > 0)
  839. add_rchar(current, ret);
  840. inc_syscr(current);
  841. return ret;
  842. }
  843. COMPAT_SYSCALL_DEFINE3(readv, unsigned long, fd,
  844. const struct compat_iovec __user *,vec,
  845. unsigned long, vlen)
  846. {
  847. struct fd f = fdget(fd);
  848. ssize_t ret;
  849. loff_t pos;
  850. if (!f.file)
  851. return -EBADF;
  852. pos = f.file->f_pos;
  853. ret = compat_readv(f.file, vec, vlen, &pos);
  854. f.file->f_pos = pos;
  855. fdput(f);
  856. return ret;
  857. }
  858. COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
  859. const struct compat_iovec __user *,vec,
  860. unsigned long, vlen, loff_t, pos)
  861. {
  862. struct fd f;
  863. ssize_t ret;
  864. if (pos < 0)
  865. return -EINVAL;
  866. f = fdget(fd);
  867. if (!f.file)
  868. return -EBADF;
  869. ret = -ESPIPE;
  870. if (f.file->f_mode & FMODE_PREAD)
  871. ret = compat_readv(f.file, vec, vlen, &pos);
  872. fdput(f);
  873. return ret;
  874. }
  875. COMPAT_SYSCALL_DEFINE5(preadv, unsigned long, fd,
  876. const struct compat_iovec __user *,vec,
  877. unsigned long, vlen, u32, pos_low, u32, pos_high)
  878. {
  879. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  880. return compat_sys_preadv64(fd, vec, vlen, pos);
  881. }
  882. static size_t compat_writev(struct file *file,
  883. const struct compat_iovec __user *vec,
  884. unsigned long vlen, loff_t *pos)
  885. {
  886. ssize_t ret = -EBADF;
  887. if (!(file->f_mode & FMODE_WRITE))
  888. goto out;
  889. ret = -EINVAL;
  890. if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
  891. goto out;
  892. ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
  893. out:
  894. if (ret > 0)
  895. add_wchar(current, ret);
  896. inc_syscw(current);
  897. return ret;
  898. }
  899. COMPAT_SYSCALL_DEFINE3(writev, unsigned long, fd,
  900. const struct compat_iovec __user *, vec,
  901. unsigned long, vlen)
  902. {
  903. struct fd f = fdget(fd);
  904. ssize_t ret;
  905. loff_t pos;
  906. if (!f.file)
  907. return -EBADF;
  908. pos = f.file->f_pos;
  909. ret = compat_writev(f.file, vec, vlen, &pos);
  910. f.file->f_pos = pos;
  911. fdput(f);
  912. return ret;
  913. }
  914. COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
  915. const struct compat_iovec __user *,vec,
  916. unsigned long, vlen, loff_t, pos)
  917. {
  918. struct fd f;
  919. ssize_t ret;
  920. if (pos < 0)
  921. return -EINVAL;
  922. f = fdget(fd);
  923. if (!f.file)
  924. return -EBADF;
  925. ret = -ESPIPE;
  926. if (f.file->f_mode & FMODE_PWRITE)
  927. ret = compat_writev(f.file, vec, vlen, &pos);
  928. fdput(f);
  929. return ret;
  930. }
  931. COMPAT_SYSCALL_DEFINE5(pwritev, unsigned long, fd,
  932. const struct compat_iovec __user *,vec,
  933. unsigned long, vlen, u32, pos_low, u32, pos_high)
  934. {
  935. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  936. return compat_sys_pwritev64(fd, vec, vlen, pos);
  937. }
  938. #endif
  939. static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
  940. size_t count, loff_t max)
  941. {
  942. struct fd in, out;
  943. struct inode *in_inode, *out_inode;
  944. loff_t pos;
  945. ssize_t retval;
  946. int fl;
  947. /*
  948. * Get input file, and verify that it is ok..
  949. */
  950. retval = -EBADF;
  951. in = fdget(in_fd);
  952. if (!in.file)
  953. goto out;
  954. if (!(in.file->f_mode & FMODE_READ))
  955. goto fput_in;
  956. retval = -ESPIPE;
  957. if (!ppos)
  958. ppos = &in.file->f_pos;
  959. else
  960. if (!(in.file->f_mode & FMODE_PREAD))
  961. goto fput_in;
  962. retval = rw_verify_area(READ, in.file, ppos, count);
  963. if (retval < 0)
  964. goto fput_in;
  965. count = retval;
  966. /*
  967. * Get output file, and verify that it is ok..
  968. */
  969. retval = -EBADF;
  970. out = fdget(out_fd);
  971. if (!out.file)
  972. goto fput_in;
  973. if (!(out.file->f_mode & FMODE_WRITE))
  974. goto fput_out;
  975. retval = -EINVAL;
  976. in_inode = file_inode(in.file);
  977. out_inode = file_inode(out.file);
  978. retval = rw_verify_area(WRITE, out.file, &out.file->f_pos, count);
  979. if (retval < 0)
  980. goto fput_out;
  981. count = retval;
  982. if (!max)
  983. max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
  984. pos = *ppos;
  985. if (unlikely(pos + count > max)) {
  986. retval = -EOVERFLOW;
  987. if (pos >= max)
  988. goto fput_out;
  989. count = max - pos;
  990. }
  991. fl = 0;
  992. #if 0
  993. /*
  994. * We need to debate whether we can enable this or not. The
  995. * man page documents EAGAIN return for the output at least,
  996. * and the application is arguably buggy if it doesn't expect
  997. * EAGAIN on a non-blocking file descriptor.
  998. */
  999. if (in.file->f_flags & O_NONBLOCK)
  1000. fl = SPLICE_F_NONBLOCK;
  1001. #endif
  1002. retval = do_splice_direct(in.file, ppos, out.file, count, fl);
  1003. if (retval > 0) {
  1004. add_rchar(current, retval);
  1005. add_wchar(current, retval);
  1006. fsnotify_access(in.file);
  1007. fsnotify_modify(out.file);
  1008. }
  1009. inc_syscr(current);
  1010. inc_syscw(current);
  1011. if (*ppos > max)
  1012. retval = -EOVERFLOW;
  1013. fput_out:
  1014. fdput(out);
  1015. fput_in:
  1016. fdput(in);
  1017. out:
  1018. return retval;
  1019. }
  1020. SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
  1021. {
  1022. loff_t pos;
  1023. off_t off;
  1024. ssize_t ret;
  1025. if (offset) {
  1026. if (unlikely(get_user(off, offset)))
  1027. return -EFAULT;
  1028. pos = off;
  1029. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  1030. if (unlikely(put_user(pos, offset)))
  1031. return -EFAULT;
  1032. return ret;
  1033. }
  1034. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1035. }
  1036. SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
  1037. {
  1038. loff_t pos;
  1039. ssize_t ret;
  1040. if (offset) {
  1041. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  1042. return -EFAULT;
  1043. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  1044. if (unlikely(put_user(pos, offset)))
  1045. return -EFAULT;
  1046. return ret;
  1047. }
  1048. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1049. }
  1050. #ifdef CONFIG_COMPAT
  1051. COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
  1052. compat_off_t __user *, offset, compat_size_t, count)
  1053. {
  1054. loff_t pos;
  1055. off_t off;
  1056. ssize_t ret;
  1057. if (offset) {
  1058. if (unlikely(get_user(off, offset)))
  1059. return -EFAULT;
  1060. pos = off;
  1061. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  1062. if (unlikely(put_user(pos, offset)))
  1063. return -EFAULT;
  1064. return ret;
  1065. }
  1066. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1067. }
  1068. COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
  1069. compat_loff_t __user *, offset, compat_size_t, count)
  1070. {
  1071. loff_t pos;
  1072. ssize_t ret;
  1073. if (offset) {
  1074. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  1075. return -EFAULT;
  1076. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  1077. if (unlikely(put_user(pos, offset)))
  1078. return -EFAULT;
  1079. return ret;
  1080. }
  1081. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1082. }
  1083. #endif