read_write.c 27 KB

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