read_write.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  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/aio.h>
  12. #include <linux/fsnotify.h>
  13. #include <linux/security.h>
  14. #include <linux/export.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/splice.h>
  18. #include <linux/compat.h>
  19. #include "internal.h"
  20. #include <asm/uaccess.h>
  21. #include <asm/unistd.h>
  22. typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
  23. typedef ssize_t (*iov_fn_t)(struct kiocb *, const struct iovec *,
  24. unsigned long, loff_t);
  25. const struct file_operations generic_ro_fops = {
  26. .llseek = generic_file_llseek,
  27. .read = do_sync_read,
  28. .aio_read = generic_file_aio_read,
  29. .mmap = generic_file_readonly_mmap,
  30. .splice_read = generic_file_splice_read,
  31. };
  32. EXPORT_SYMBOL(generic_ro_fops);
  33. static inline int unsigned_offsets(struct file *file)
  34. {
  35. return file->f_mode & FMODE_UNSIGNED_OFFSET;
  36. }
  37. static loff_t lseek_execute(struct file *file, struct inode *inode,
  38. loff_t offset, loff_t maxsize)
  39. {
  40. if (offset < 0 && !unsigned_offsets(file))
  41. return -EINVAL;
  42. if (offset > maxsize)
  43. return -EINVAL;
  44. if (offset != file->f_pos) {
  45. file->f_pos = offset;
  46. file->f_version = 0;
  47. }
  48. return offset;
  49. }
  50. /**
  51. * generic_file_llseek_size - generic llseek implementation for regular files
  52. * @file: file structure to seek on
  53. * @offset: file offset to seek to
  54. * @whence: type of seek
  55. * @size: max size of this file in file system
  56. * @eof: offset used for SEEK_END position
  57. *
  58. * This is a variant of generic_file_llseek that allows passing in a custom
  59. * maximum file size and a custom EOF position, for e.g. hashed directories
  60. *
  61. * Synchronization:
  62. * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
  63. * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
  64. * read/writes behave like SEEK_SET against seeks.
  65. */
  66. loff_t
  67. generic_file_llseek_size(struct file *file, loff_t offset, int whence,
  68. loff_t maxsize, loff_t eof)
  69. {
  70. struct inode *inode = file->f_mapping->host;
  71. switch (whence) {
  72. case SEEK_END:
  73. offset += eof;
  74. break;
  75. case SEEK_CUR:
  76. /*
  77. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  78. * position-querying operation. Avoid rewriting the "same"
  79. * f_pos value back to the file because a concurrent read(),
  80. * write() or lseek() might have altered it
  81. */
  82. if (offset == 0)
  83. return file->f_pos;
  84. /*
  85. * f_lock protects against read/modify/write race with other
  86. * SEEK_CURs. Note that parallel writes and reads behave
  87. * like SEEK_SET.
  88. */
  89. spin_lock(&file->f_lock);
  90. offset = lseek_execute(file, inode, file->f_pos + offset,
  91. maxsize);
  92. spin_unlock(&file->f_lock);
  93. return offset;
  94. case SEEK_DATA:
  95. /*
  96. * In the generic case the entire file is data, so as long as
  97. * offset isn't at the end of the file then the offset is data.
  98. */
  99. if (offset >= eof)
  100. return -ENXIO;
  101. break;
  102. case SEEK_HOLE:
  103. /*
  104. * There is a virtual hole at the end of the file, so as long as
  105. * offset isn't i_size or larger, return i_size.
  106. */
  107. if (offset >= eof)
  108. return -ENXIO;
  109. offset = eof;
  110. break;
  111. }
  112. return lseek_execute(file, inode, offset, maxsize);
  113. }
  114. EXPORT_SYMBOL(generic_file_llseek_size);
  115. /**
  116. * generic_file_llseek - generic llseek implementation for regular files
  117. * @file: file structure to seek on
  118. * @offset: file offset to seek to
  119. * @whence: type of seek
  120. *
  121. * This is a generic implemenation of ->llseek useable for all normal local
  122. * filesystems. It just updates the file offset to the value specified by
  123. * @offset and @whence.
  124. */
  125. loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
  126. {
  127. struct inode *inode = file->f_mapping->host;
  128. return generic_file_llseek_size(file, offset, whence,
  129. inode->i_sb->s_maxbytes,
  130. i_size_read(inode));
  131. }
  132. EXPORT_SYMBOL(generic_file_llseek);
  133. /**
  134. * fixed_size_llseek - llseek implementation for fixed-sized devices
  135. * @file: file structure to seek on
  136. * @offset: file offset to seek to
  137. * @whence: type of seek
  138. * @size: size of the file
  139. *
  140. */
  141. loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
  142. {
  143. switch (whence) {
  144. case SEEK_SET: case SEEK_CUR: case SEEK_END:
  145. return generic_file_llseek_size(file, offset, whence,
  146. size, size);
  147. default:
  148. return -EINVAL;
  149. }
  150. }
  151. EXPORT_SYMBOL(fixed_size_llseek);
  152. /**
  153. * noop_llseek - No Operation Performed llseek implementation
  154. * @file: file structure to seek on
  155. * @offset: file offset to seek to
  156. * @whence: type of seek
  157. *
  158. * This is an implementation of ->llseek useable for the rare special case when
  159. * userspace expects the seek to succeed but the (device) file is actually not
  160. * able to perform the seek. In this case you use noop_llseek() instead of
  161. * falling back to the default implementation of ->llseek.
  162. */
  163. loff_t noop_llseek(struct file *file, loff_t offset, int whence)
  164. {
  165. return file->f_pos;
  166. }
  167. EXPORT_SYMBOL(noop_llseek);
  168. loff_t no_llseek(struct file *file, loff_t offset, int whence)
  169. {
  170. return -ESPIPE;
  171. }
  172. EXPORT_SYMBOL(no_llseek);
  173. loff_t default_llseek(struct file *file, loff_t offset, int whence)
  174. {
  175. struct inode *inode = file_inode(file);
  176. loff_t retval;
  177. mutex_lock(&inode->i_mutex);
  178. switch (whence) {
  179. case SEEK_END:
  180. offset += i_size_read(inode);
  181. break;
  182. case SEEK_CUR:
  183. if (offset == 0) {
  184. retval = file->f_pos;
  185. goto out;
  186. }
  187. offset += file->f_pos;
  188. break;
  189. case SEEK_DATA:
  190. /*
  191. * In the generic case the entire file is data, so as
  192. * long as offset isn't at the end of the file then the
  193. * offset is data.
  194. */
  195. if (offset >= inode->i_size) {
  196. retval = -ENXIO;
  197. goto out;
  198. }
  199. break;
  200. case SEEK_HOLE:
  201. /*
  202. * There is a virtual hole at the end of the file, so
  203. * as long as offset isn't i_size or larger, return
  204. * i_size.
  205. */
  206. if (offset >= inode->i_size) {
  207. retval = -ENXIO;
  208. goto out;
  209. }
  210. offset = inode->i_size;
  211. break;
  212. }
  213. retval = -EINVAL;
  214. if (offset >= 0 || unsigned_offsets(file)) {
  215. if (offset != file->f_pos) {
  216. file->f_pos = offset;
  217. file->f_version = 0;
  218. }
  219. retval = offset;
  220. }
  221. out:
  222. mutex_unlock(&inode->i_mutex);
  223. return retval;
  224. }
  225. EXPORT_SYMBOL(default_llseek);
  226. loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
  227. {
  228. loff_t (*fn)(struct file *, loff_t, int);
  229. fn = no_llseek;
  230. if (file->f_mode & FMODE_LSEEK) {
  231. if (file->f_op && file->f_op->llseek)
  232. fn = file->f_op->llseek;
  233. }
  234. return fn(file, offset, whence);
  235. }
  236. EXPORT_SYMBOL(vfs_llseek);
  237. SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
  238. {
  239. off_t retval;
  240. struct fd f = fdget(fd);
  241. if (!f.file)
  242. return -EBADF;
  243. retval = -EINVAL;
  244. if (whence <= SEEK_MAX) {
  245. loff_t res = vfs_llseek(f.file, offset, whence);
  246. retval = res;
  247. if (res != (loff_t)retval)
  248. retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
  249. }
  250. fdput(f);
  251. return retval;
  252. }
  253. #ifdef CONFIG_COMPAT
  254. COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
  255. {
  256. return sys_lseek(fd, offset, whence);
  257. }
  258. #endif
  259. #ifdef __ARCH_WANT_SYS_LLSEEK
  260. SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
  261. unsigned long, offset_low, loff_t __user *, result,
  262. unsigned int, whence)
  263. {
  264. int retval;
  265. struct fd f = fdget(fd);
  266. loff_t offset;
  267. if (!f.file)
  268. return -EBADF;
  269. retval = -EINVAL;
  270. if (whence > SEEK_MAX)
  271. goto out_putf;
  272. offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
  273. whence);
  274. retval = (int)offset;
  275. if (offset >= 0) {
  276. retval = -EFAULT;
  277. if (!copy_to_user(result, &offset, sizeof(offset)))
  278. retval = 0;
  279. }
  280. out_putf:
  281. fdput(f);
  282. return retval;
  283. }
  284. #endif
  285. /*
  286. * rw_verify_area doesn't like huge counts. We limit
  287. * them to something that fits in "int" so that others
  288. * won't have to do range checks all the time.
  289. */
  290. int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
  291. {
  292. struct inode *inode;
  293. loff_t pos;
  294. int retval = -EINVAL;
  295. inode = file_inode(file);
  296. if (unlikely((ssize_t) count < 0))
  297. return retval;
  298. pos = *ppos;
  299. if (unlikely(pos < 0)) {
  300. if (!unsigned_offsets(file))
  301. return retval;
  302. if (count >= -pos) /* both values are in 0..LLONG_MAX */
  303. return -EOVERFLOW;
  304. } else if (unlikely((loff_t) (pos + count) < 0)) {
  305. if (!unsigned_offsets(file))
  306. return retval;
  307. }
  308. if (unlikely(inode->i_flock && mandatory_lock(inode))) {
  309. retval = locks_mandatory_area(
  310. read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
  311. inode, file, pos, count);
  312. if (retval < 0)
  313. return retval;
  314. }
  315. retval = security_file_permission(file,
  316. read_write == READ ? MAY_READ : MAY_WRITE);
  317. if (retval)
  318. return retval;
  319. return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
  320. }
  321. ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
  322. {
  323. struct iovec iov = { .iov_base = buf, .iov_len = len };
  324. struct kiocb kiocb;
  325. ssize_t ret;
  326. init_sync_kiocb(&kiocb, filp);
  327. kiocb.ki_pos = *ppos;
  328. kiocb.ki_left = len;
  329. kiocb.ki_nbytes = len;
  330. ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
  331. if (-EIOCBQUEUED == ret)
  332. ret = wait_on_sync_kiocb(&kiocb);
  333. *ppos = kiocb.ki_pos;
  334. return ret;
  335. }
  336. EXPORT_SYMBOL(do_sync_read);
  337. ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
  338. {
  339. ssize_t ret;
  340. if (!(file->f_mode & FMODE_READ))
  341. return -EBADF;
  342. if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
  343. return -EINVAL;
  344. if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
  345. return -EFAULT;
  346. ret = rw_verify_area(READ, file, pos, count);
  347. if (ret >= 0) {
  348. count = ret;
  349. if (file->f_op->read)
  350. ret = file->f_op->read(file, buf, count, pos);
  351. else
  352. ret = do_sync_read(file, buf, count, pos);
  353. if (ret > 0) {
  354. fsnotify_access(file);
  355. add_rchar(current, ret);
  356. }
  357. inc_syscr(current);
  358. }
  359. return ret;
  360. }
  361. EXPORT_SYMBOL(vfs_read);
  362. ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
  363. {
  364. struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
  365. struct kiocb kiocb;
  366. ssize_t ret;
  367. init_sync_kiocb(&kiocb, filp);
  368. kiocb.ki_pos = *ppos;
  369. kiocb.ki_left = len;
  370. kiocb.ki_nbytes = len;
  371. ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
  372. if (-EIOCBQUEUED == ret)
  373. ret = wait_on_sync_kiocb(&kiocb);
  374. *ppos = kiocb.ki_pos;
  375. return ret;
  376. }
  377. EXPORT_SYMBOL(do_sync_write);
  378. ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
  379. {
  380. mm_segment_t old_fs;
  381. const char __user *p;
  382. ssize_t ret;
  383. if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
  384. return -EINVAL;
  385. old_fs = get_fs();
  386. set_fs(get_ds());
  387. p = (__force const char __user *)buf;
  388. if (count > MAX_RW_COUNT)
  389. count = MAX_RW_COUNT;
  390. if (file->f_op->write)
  391. ret = file->f_op->write(file, p, count, pos);
  392. else
  393. ret = do_sync_write(file, p, count, pos);
  394. set_fs(old_fs);
  395. if (ret > 0) {
  396. fsnotify_modify(file);
  397. add_wchar(current, ret);
  398. }
  399. inc_syscw(current);
  400. return ret;
  401. }
  402. ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
  403. {
  404. ssize_t ret;
  405. if (!(file->f_mode & FMODE_WRITE))
  406. return -EBADF;
  407. if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
  408. return -EINVAL;
  409. if (unlikely(!access_ok(VERIFY_READ, buf, count)))
  410. return -EFAULT;
  411. ret = rw_verify_area(WRITE, file, pos, count);
  412. if (ret >= 0) {
  413. count = ret;
  414. file_start_write(file);
  415. if (file->f_op->write)
  416. ret = file->f_op->write(file, buf, count, pos);
  417. else
  418. ret = do_sync_write(file, buf, count, pos);
  419. if (ret > 0) {
  420. fsnotify_modify(file);
  421. add_wchar(current, ret);
  422. }
  423. inc_syscw(current);
  424. file_end_write(file);
  425. }
  426. return ret;
  427. }
  428. EXPORT_SYMBOL(vfs_write);
  429. static inline loff_t file_pos_read(struct file *file)
  430. {
  431. return file->f_pos;
  432. }
  433. static inline void file_pos_write(struct file *file, loff_t pos)
  434. {
  435. file->f_pos = pos;
  436. }
  437. SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
  438. {
  439. struct fd f = fdget(fd);
  440. ssize_t ret = -EBADF;
  441. if (f.file) {
  442. loff_t pos = file_pos_read(f.file);
  443. ret = vfs_read(f.file, buf, count, &pos);
  444. if (ret >= 0)
  445. file_pos_write(f.file, pos);
  446. fdput(f);
  447. }
  448. return ret;
  449. }
  450. SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
  451. size_t, count)
  452. {
  453. struct fd f = fdget(fd);
  454. ssize_t ret = -EBADF;
  455. if (f.file) {
  456. loff_t pos = file_pos_read(f.file);
  457. ret = vfs_write(f.file, buf, count, &pos);
  458. if (ret >= 0)
  459. file_pos_write(f.file, pos);
  460. fdput(f);
  461. }
  462. return ret;
  463. }
  464. SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
  465. size_t, count, loff_t, pos)
  466. {
  467. struct fd f;
  468. ssize_t ret = -EBADF;
  469. if (pos < 0)
  470. return -EINVAL;
  471. f = fdget(fd);
  472. if (f.file) {
  473. ret = -ESPIPE;
  474. if (f.file->f_mode & FMODE_PREAD)
  475. ret = vfs_read(f.file, buf, count, &pos);
  476. fdput(f);
  477. }
  478. return ret;
  479. }
  480. SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
  481. size_t, count, loff_t, pos)
  482. {
  483. struct fd f;
  484. ssize_t ret = -EBADF;
  485. if (pos < 0)
  486. return -EINVAL;
  487. f = fdget(fd);
  488. if (f.file) {
  489. ret = -ESPIPE;
  490. if (f.file->f_mode & FMODE_PWRITE)
  491. ret = vfs_write(f.file, buf, count, &pos);
  492. fdput(f);
  493. }
  494. return ret;
  495. }
  496. /*
  497. * Reduce an iovec's length in-place. Return the resulting number of segments
  498. */
  499. unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
  500. {
  501. unsigned long seg = 0;
  502. size_t len = 0;
  503. while (seg < nr_segs) {
  504. seg++;
  505. if (len + iov->iov_len >= to) {
  506. iov->iov_len = to - len;
  507. break;
  508. }
  509. len += iov->iov_len;
  510. iov++;
  511. }
  512. return seg;
  513. }
  514. EXPORT_SYMBOL(iov_shorten);
  515. static ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
  516. unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
  517. {
  518. struct kiocb kiocb;
  519. ssize_t ret;
  520. init_sync_kiocb(&kiocb, filp);
  521. kiocb.ki_pos = *ppos;
  522. kiocb.ki_left = len;
  523. kiocb.ki_nbytes = len;
  524. ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
  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. if (ret >= 0)
  706. file_pos_write(f.file, pos);
  707. fdput(f);
  708. }
  709. if (ret > 0)
  710. add_rchar(current, ret);
  711. inc_syscr(current);
  712. return ret;
  713. }
  714. SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
  715. unsigned long, vlen)
  716. {
  717. struct fd f = fdget(fd);
  718. ssize_t ret = -EBADF;
  719. if (f.file) {
  720. loff_t pos = file_pos_read(f.file);
  721. ret = vfs_writev(f.file, vec, vlen, &pos);
  722. if (ret >= 0)
  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. if (ret >= 0)
  857. f.file->f_pos = pos;
  858. fdput(f);
  859. return ret;
  860. }
  861. COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
  862. const struct compat_iovec __user *,vec,
  863. unsigned long, vlen, loff_t, pos)
  864. {
  865. struct fd f;
  866. ssize_t ret;
  867. if (pos < 0)
  868. return -EINVAL;
  869. f = fdget(fd);
  870. if (!f.file)
  871. return -EBADF;
  872. ret = -ESPIPE;
  873. if (f.file->f_mode & FMODE_PREAD)
  874. ret = compat_readv(f.file, vec, vlen, &pos);
  875. fdput(f);
  876. return ret;
  877. }
  878. COMPAT_SYSCALL_DEFINE5(preadv, unsigned long, fd,
  879. const struct compat_iovec __user *,vec,
  880. unsigned long, vlen, u32, pos_low, u32, pos_high)
  881. {
  882. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  883. return compat_sys_preadv64(fd, vec, vlen, pos);
  884. }
  885. static size_t compat_writev(struct file *file,
  886. const struct compat_iovec __user *vec,
  887. unsigned long vlen, loff_t *pos)
  888. {
  889. ssize_t ret = -EBADF;
  890. if (!(file->f_mode & FMODE_WRITE))
  891. goto out;
  892. ret = -EINVAL;
  893. if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
  894. goto out;
  895. ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
  896. out:
  897. if (ret > 0)
  898. add_wchar(current, ret);
  899. inc_syscw(current);
  900. return ret;
  901. }
  902. COMPAT_SYSCALL_DEFINE3(writev, unsigned long, fd,
  903. const struct compat_iovec __user *, vec,
  904. unsigned long, vlen)
  905. {
  906. struct fd f = fdget(fd);
  907. ssize_t ret;
  908. loff_t pos;
  909. if (!f.file)
  910. return -EBADF;
  911. pos = f.file->f_pos;
  912. ret = compat_writev(f.file, vec, vlen, &pos);
  913. if (ret >= 0)
  914. f.file->f_pos = pos;
  915. fdput(f);
  916. return ret;
  917. }
  918. COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
  919. const struct compat_iovec __user *,vec,
  920. unsigned long, vlen, loff_t, pos)
  921. {
  922. struct fd f;
  923. ssize_t ret;
  924. if (pos < 0)
  925. return -EINVAL;
  926. f = fdget(fd);
  927. if (!f.file)
  928. return -EBADF;
  929. ret = -ESPIPE;
  930. if (f.file->f_mode & FMODE_PWRITE)
  931. ret = compat_writev(f.file, vec, vlen, &pos);
  932. fdput(f);
  933. return ret;
  934. }
  935. COMPAT_SYSCALL_DEFINE5(pwritev, unsigned long, fd,
  936. const struct compat_iovec __user *,vec,
  937. unsigned long, vlen, u32, pos_low, u32, pos_high)
  938. {
  939. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  940. return compat_sys_pwritev64(fd, vec, vlen, pos);
  941. }
  942. #endif
  943. static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
  944. size_t count, loff_t max)
  945. {
  946. struct fd in, out;
  947. struct inode *in_inode, *out_inode;
  948. loff_t pos;
  949. loff_t out_pos;
  950. ssize_t retval;
  951. int fl;
  952. /*
  953. * Get input file, and verify that it is ok..
  954. */
  955. retval = -EBADF;
  956. in = fdget(in_fd);
  957. if (!in.file)
  958. goto out;
  959. if (!(in.file->f_mode & FMODE_READ))
  960. goto fput_in;
  961. retval = -ESPIPE;
  962. if (!ppos) {
  963. pos = in.file->f_pos;
  964. } else {
  965. pos = *ppos;
  966. if (!(in.file->f_mode & FMODE_PREAD))
  967. goto fput_in;
  968. }
  969. retval = rw_verify_area(READ, in.file, &pos, count);
  970. if (retval < 0)
  971. goto fput_in;
  972. count = retval;
  973. /*
  974. * Get output file, and verify that it is ok..
  975. */
  976. retval = -EBADF;
  977. out = fdget(out_fd);
  978. if (!out.file)
  979. goto fput_in;
  980. if (!(out.file->f_mode & FMODE_WRITE))
  981. goto fput_out;
  982. retval = -EINVAL;
  983. in_inode = file_inode(in.file);
  984. out_inode = file_inode(out.file);
  985. out_pos = out.file->f_pos;
  986. retval = rw_verify_area(WRITE, out.file, &out_pos, count);
  987. if (retval < 0)
  988. goto fput_out;
  989. count = retval;
  990. if (!max)
  991. max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
  992. if (unlikely(pos + count > max)) {
  993. retval = -EOVERFLOW;
  994. if (pos >= max)
  995. goto fput_out;
  996. count = max - pos;
  997. }
  998. fl = 0;
  999. #if 0
  1000. /*
  1001. * We need to debate whether we can enable this or not. The
  1002. * man page documents EAGAIN return for the output at least,
  1003. * and the application is arguably buggy if it doesn't expect
  1004. * EAGAIN on a non-blocking file descriptor.
  1005. */
  1006. if (in.file->f_flags & O_NONBLOCK)
  1007. fl = SPLICE_F_NONBLOCK;
  1008. #endif
  1009. file_start_write(out.file);
  1010. retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
  1011. file_end_write(out.file);
  1012. if (retval > 0) {
  1013. add_rchar(current, retval);
  1014. add_wchar(current, retval);
  1015. fsnotify_access(in.file);
  1016. fsnotify_modify(out.file);
  1017. out.file->f_pos = out_pos;
  1018. if (ppos)
  1019. *ppos = pos;
  1020. else
  1021. in.file->f_pos = pos;
  1022. }
  1023. inc_syscr(current);
  1024. inc_syscw(current);
  1025. if (pos > max)
  1026. retval = -EOVERFLOW;
  1027. fput_out:
  1028. fdput(out);
  1029. fput_in:
  1030. fdput(in);
  1031. out:
  1032. return retval;
  1033. }
  1034. SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
  1035. {
  1036. loff_t pos;
  1037. off_t off;
  1038. ssize_t ret;
  1039. if (offset) {
  1040. if (unlikely(get_user(off, offset)))
  1041. return -EFAULT;
  1042. pos = off;
  1043. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  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. SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
  1051. {
  1052. loff_t pos;
  1053. ssize_t ret;
  1054. if (offset) {
  1055. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  1056. return -EFAULT;
  1057. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  1058. if (unlikely(put_user(pos, offset)))
  1059. return -EFAULT;
  1060. return ret;
  1061. }
  1062. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1063. }
  1064. #ifdef CONFIG_COMPAT
  1065. COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
  1066. compat_off_t __user *, offset, compat_size_t, count)
  1067. {
  1068. loff_t pos;
  1069. off_t off;
  1070. ssize_t ret;
  1071. if (offset) {
  1072. if (unlikely(get_user(off, offset)))
  1073. return -EFAULT;
  1074. pos = off;
  1075. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  1076. if (unlikely(put_user(pos, offset)))
  1077. return -EFAULT;
  1078. return ret;
  1079. }
  1080. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1081. }
  1082. COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
  1083. compat_loff_t __user *, offset, compat_size_t, count)
  1084. {
  1085. loff_t pos;
  1086. ssize_t ret;
  1087. if (offset) {
  1088. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  1089. return -EFAULT;
  1090. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  1091. if (unlikely(put_user(pos, offset)))
  1092. return -EFAULT;
  1093. return ret;
  1094. }
  1095. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1096. }
  1097. #endif