read_write.c 22 KB

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