read_write.c 26 KB

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