read_write.c 27 KB

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