2 * linux/fs/read_write.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/slab.h>
8 #include <linux/stat.h>
9 #include <linux/fcntl.h>
10 #include <linux/file.h>
11 #include <linux/uio.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 <linux/mount.h>
22 #include <asm/uaccess.h>
23 #include <asm/unistd.h>
25 const struct file_operations generic_ro_fops = {
26 .llseek = generic_file_llseek,
27 .read_iter = generic_file_read_iter,
28 .mmap = generic_file_readonly_mmap,
29 .splice_read = generic_file_splice_read,
32 EXPORT_SYMBOL(generic_ro_fops);
34 static inline int unsigned_offsets(struct file *file)
36 return file->f_mode & FMODE_UNSIGNED_OFFSET;
40 * vfs_setpos - update the file offset for lseek
41 * @file: file structure in question
42 * @offset: file offset to seek to
43 * @maxsize: maximum file size
45 * This is a low-level filesystem helper for updating the file offset to
46 * the value specified by @offset if the given offset is valid and it is
47 * not equal to the current file offset.
49 * Return the specified offset on success and -EINVAL on invalid offset.
51 loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
53 if (offset < 0 && !unsigned_offsets(file))
58 if (offset != file->f_pos) {
64 EXPORT_SYMBOL(vfs_setpos);
67 * generic_file_llseek_size - generic llseek implementation for regular files
68 * @file: file structure to seek on
69 * @offset: file offset to seek to
70 * @whence: type of seek
71 * @size: max size of this file in file system
72 * @eof: offset used for SEEK_END position
74 * This is a variant of generic_file_llseek that allows passing in a custom
75 * maximum file size and a custom EOF position, for e.g. hashed directories
78 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
79 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
80 * read/writes behave like SEEK_SET against seeks.
83 generic_file_llseek_size(struct file *file, loff_t offset, int whence,
84 loff_t maxsize, loff_t eof)
92 * Here we special-case the lseek(fd, 0, SEEK_CUR)
93 * position-querying operation. Avoid rewriting the "same"
94 * f_pos value back to the file because a concurrent read(),
95 * write() or lseek() might have altered it
100 * f_lock protects against read/modify/write race with other
101 * SEEK_CURs. Note that parallel writes and reads behave
104 spin_lock(&file->f_lock);
105 offset = vfs_setpos(file, file->f_pos + offset, maxsize);
106 spin_unlock(&file->f_lock);
110 * In the generic case the entire file is data, so as long as
111 * offset isn't at the end of the file then the offset is data.
118 * There is a virtual hole at the end of the file, so as long as
119 * offset isn't i_size or larger, return i_size.
127 return vfs_setpos(file, offset, maxsize);
129 EXPORT_SYMBOL(generic_file_llseek_size);
132 * generic_file_llseek - generic llseek implementation for regular files
133 * @file: file structure to seek on
134 * @offset: file offset to seek to
135 * @whence: type of seek
137 * This is a generic implemenation of ->llseek useable for all normal local
138 * filesystems. It just updates the file offset to the value specified by
139 * @offset and @whence.
141 loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
143 struct inode *inode = file->f_mapping->host;
145 return generic_file_llseek_size(file, offset, whence,
146 inode->i_sb->s_maxbytes,
149 EXPORT_SYMBOL(generic_file_llseek);
152 * fixed_size_llseek - llseek implementation for fixed-sized devices
153 * @file: file structure to seek on
154 * @offset: file offset to seek to
155 * @whence: type of seek
156 * @size: size of the file
159 loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
162 case SEEK_SET: case SEEK_CUR: case SEEK_END:
163 return generic_file_llseek_size(file, offset, whence,
169 EXPORT_SYMBOL(fixed_size_llseek);
172 * no_seek_end_llseek - llseek implementation for fixed-sized devices
173 * @file: file structure to seek on
174 * @offset: file offset to seek to
175 * @whence: type of seek
178 loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence)
181 case SEEK_SET: case SEEK_CUR:
182 return generic_file_llseek_size(file, offset, whence,
188 EXPORT_SYMBOL(no_seek_end_llseek);
191 * no_seek_end_llseek_size - llseek implementation for fixed-sized devices
192 * @file: file structure to seek on
193 * @offset: file offset to seek to
194 * @whence: type of seek
195 * @size: maximal offset allowed
198 loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size)
201 case SEEK_SET: case SEEK_CUR:
202 return generic_file_llseek_size(file, offset, whence,
208 EXPORT_SYMBOL(no_seek_end_llseek_size);
211 * noop_llseek - No Operation Performed llseek implementation
212 * @file: file structure to seek on
213 * @offset: file offset to seek to
214 * @whence: type of seek
216 * This is an implementation of ->llseek useable for the rare special case when
217 * userspace expects the seek to succeed but the (device) file is actually not
218 * able to perform the seek. In this case you use noop_llseek() instead of
219 * falling back to the default implementation of ->llseek.
221 loff_t noop_llseek(struct file *file, loff_t offset, int whence)
225 EXPORT_SYMBOL(noop_llseek);
227 loff_t no_llseek(struct file *file, loff_t offset, int whence)
231 EXPORT_SYMBOL(no_llseek);
233 loff_t default_llseek(struct file *file, loff_t offset, int whence)
235 struct inode *inode = file_inode(file);
241 offset += i_size_read(inode);
245 retval = file->f_pos;
248 offset += file->f_pos;
252 * In the generic case the entire file is data, so as
253 * long as offset isn't at the end of the file then the
256 if (offset >= inode->i_size) {
263 * There is a virtual hole at the end of the file, so
264 * as long as offset isn't i_size or larger, return
267 if (offset >= inode->i_size) {
271 offset = inode->i_size;
275 if (offset >= 0 || unsigned_offsets(file)) {
276 if (offset != file->f_pos) {
277 file->f_pos = offset;
286 EXPORT_SYMBOL(default_llseek);
288 loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
290 loff_t (*fn)(struct file *, loff_t, int);
293 if (file->f_mode & FMODE_LSEEK) {
294 if (file->f_op->llseek)
295 fn = file->f_op->llseek;
297 return fn(file, offset, whence);
299 EXPORT_SYMBOL(vfs_llseek);
301 static inline struct fd fdget_pos(int fd)
303 return __to_fd(__fdget_pos(fd));
306 static inline void fdput_pos(struct fd f)
308 if (f.flags & FDPUT_POS_UNLOCK)
309 mutex_unlock(&f.file->f_pos_lock);
313 SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
316 struct fd f = fdget_pos(fd);
321 if (whence <= SEEK_MAX) {
322 loff_t res = vfs_llseek(f.file, offset, whence);
324 if (res != (loff_t)retval)
325 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
332 COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
334 return sys_lseek(fd, offset, whence);
338 #ifdef __ARCH_WANT_SYS_LLSEEK
339 SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
340 unsigned long, offset_low, loff_t __user *, result,
341 unsigned int, whence)
344 struct fd f = fdget_pos(fd);
351 if (whence > SEEK_MAX)
354 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
357 retval = (int)offset;
360 if (!copy_to_user(result, &offset, sizeof(offset)))
369 ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos)
374 if (!file->f_op->read_iter)
377 init_sync_kiocb(&kiocb, file);
378 kiocb.ki_pos = *ppos;
381 ret = file->f_op->read_iter(&kiocb, iter);
382 BUG_ON(ret == -EIOCBQUEUED);
384 *ppos = kiocb.ki_pos;
387 EXPORT_SYMBOL(vfs_iter_read);
389 ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos)
394 if (!file->f_op->write_iter)
397 init_sync_kiocb(&kiocb, file);
398 kiocb.ki_pos = *ppos;
401 ret = file->f_op->write_iter(&kiocb, iter);
402 BUG_ON(ret == -EIOCBQUEUED);
404 *ppos = kiocb.ki_pos;
407 EXPORT_SYMBOL(vfs_iter_write);
410 * rw_verify_area doesn't like huge counts. We limit
411 * them to something that fits in "int" so that others
412 * won't have to do range checks all the time.
414 int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
418 int retval = -EINVAL;
420 inode = file_inode(file);
421 if (unlikely((ssize_t) count < 0))
424 if (unlikely(pos < 0)) {
425 if (!unsigned_offsets(file))
427 if (count >= -pos) /* both values are in 0..LLONG_MAX */
429 } else if (unlikely((loff_t) (pos + count) < 0)) {
430 if (!unsigned_offsets(file))
434 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
435 retval = locks_mandatory_area(inode, file, pos, pos + count - 1,
436 read_write == READ ? F_RDLCK : F_WRLCK);
440 retval = security_file_permission(file,
441 read_write == READ ? MAY_READ : MAY_WRITE);
444 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
447 static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
449 struct iovec iov = { .iov_base = buf, .iov_len = len };
451 struct iov_iter iter;
454 init_sync_kiocb(&kiocb, filp);
455 kiocb.ki_pos = *ppos;
456 iov_iter_init(&iter, READ, &iov, 1, len);
458 ret = filp->f_op->read_iter(&kiocb, &iter);
459 BUG_ON(ret == -EIOCBQUEUED);
460 *ppos = kiocb.ki_pos;
464 ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
467 if (file->f_op->read)
468 return file->f_op->read(file, buf, count, pos);
469 else if (file->f_op->read_iter)
470 return new_sync_read(file, buf, count, pos);
474 EXPORT_SYMBOL(__vfs_read);
476 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
480 if (!(file->f_mode & FMODE_READ))
482 if (!(file->f_mode & FMODE_CAN_READ))
484 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
487 ret = rw_verify_area(READ, file, pos, count);
490 ret = __vfs_read(file, buf, count, pos);
492 fsnotify_access(file);
493 add_rchar(current, ret);
501 EXPORT_SYMBOL(vfs_read);
503 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
505 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
507 struct iov_iter iter;
510 init_sync_kiocb(&kiocb, filp);
511 kiocb.ki_pos = *ppos;
512 iov_iter_init(&iter, WRITE, &iov, 1, len);
514 ret = filp->f_op->write_iter(&kiocb, &iter);
515 BUG_ON(ret == -EIOCBQUEUED);
517 *ppos = kiocb.ki_pos;
521 ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
524 if (file->f_op->write)
525 return file->f_op->write(file, p, count, pos);
526 else if (file->f_op->write_iter)
527 return new_sync_write(file, p, count, pos);
531 EXPORT_SYMBOL(__vfs_write);
533 ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
536 const char __user *p;
539 if (!(file->f_mode & FMODE_CAN_WRITE))
544 p = (__force const char __user *)buf;
545 if (count > MAX_RW_COUNT)
546 count = MAX_RW_COUNT;
547 ret = __vfs_write(file, p, count, pos);
550 fsnotify_modify(file);
551 add_wchar(current, ret);
557 EXPORT_SYMBOL(__kernel_write);
559 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
563 if (!(file->f_mode & FMODE_WRITE))
565 if (!(file->f_mode & FMODE_CAN_WRITE))
567 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
570 ret = rw_verify_area(WRITE, file, pos, count);
573 file_start_write(file);
574 ret = __vfs_write(file, buf, count, pos);
576 fsnotify_modify(file);
577 add_wchar(current, ret);
580 file_end_write(file);
586 EXPORT_SYMBOL(vfs_write);
588 static inline loff_t file_pos_read(struct file *file)
593 static inline void file_pos_write(struct file *file, loff_t pos)
598 SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
600 struct fd f = fdget_pos(fd);
601 ssize_t ret = -EBADF;
604 loff_t pos = file_pos_read(f.file);
605 ret = vfs_read(f.file, buf, count, &pos);
607 file_pos_write(f.file, pos);
613 SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
616 struct fd f = fdget_pos(fd);
617 ssize_t ret = -EBADF;
620 loff_t pos = file_pos_read(f.file);
621 ret = vfs_write(f.file, buf, count, &pos);
623 file_pos_write(f.file, pos);
630 SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
631 size_t, count, loff_t, pos)
634 ssize_t ret = -EBADF;
642 if (f.file->f_mode & FMODE_PREAD)
643 ret = vfs_read(f.file, buf, count, &pos);
650 SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
651 size_t, count, loff_t, pos)
654 ssize_t ret = -EBADF;
662 if (f.file->f_mode & FMODE_PWRITE)
663 ret = vfs_write(f.file, buf, count, &pos);
671 * Reduce an iovec's length in-place. Return the resulting number of segments
673 unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
675 unsigned long seg = 0;
678 while (seg < nr_segs) {
680 if (len + iov->iov_len >= to) {
681 iov->iov_len = to - len;
689 EXPORT_SYMBOL(iov_shorten);
691 static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
692 loff_t *ppos, iter_fn_t fn)
697 init_sync_kiocb(&kiocb, filp);
698 kiocb.ki_pos = *ppos;
700 ret = fn(&kiocb, iter);
701 BUG_ON(ret == -EIOCBQUEUED);
702 *ppos = kiocb.ki_pos;
706 /* Do it by hand, with file-ops */
707 ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
708 loff_t *ppos, io_fn_t fn)
712 while (iov_iter_count(iter)) {
713 struct iovec iovec = iov_iter_iovec(iter);
716 nr = fn(filp, iovec.iov_base, iovec.iov_len, ppos);
724 if (nr != iovec.iov_len)
726 iov_iter_advance(iter, nr);
732 /* A write operation does a read from user space and vice versa */
733 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
735 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
736 unsigned long nr_segs, unsigned long fast_segs,
737 struct iovec *fast_pointer,
738 struct iovec **ret_pointer)
742 struct iovec *iov = fast_pointer;
745 * SuS says "The readv() function *may* fail if the iovcnt argument
746 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
747 * traditionally returned zero for zero segments, so...
755 * First get the "struct iovec" from user memory and
756 * verify all the pointers
758 if (nr_segs > UIO_MAXIOV) {
762 if (nr_segs > fast_segs) {
763 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
769 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
775 * According to the Single Unix Specification we should return EINVAL
776 * if an element length is < 0 when cast to ssize_t or if the
777 * total length would overflow the ssize_t return value of the
780 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
784 for (seg = 0; seg < nr_segs; seg++) {
785 void __user *buf = iov[seg].iov_base;
786 ssize_t len = (ssize_t)iov[seg].iov_len;
788 /* see if we we're about to use an invalid len or if
789 * it's about to overflow ssize_t */
795 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
799 if (len > MAX_RW_COUNT - ret) {
800 len = MAX_RW_COUNT - ret;
801 iov[seg].iov_len = len;
810 static ssize_t do_readv_writev(int type, struct file *file,
811 const struct iovec __user * uvector,
812 unsigned long nr_segs, loff_t *pos)
815 struct iovec iovstack[UIO_FASTIOV];
816 struct iovec *iov = iovstack;
817 struct iov_iter iter;
822 ret = import_iovec(type, uvector, nr_segs,
823 ARRAY_SIZE(iovstack), &iov, &iter);
827 tot_len = iov_iter_count(&iter);
830 ret = rw_verify_area(type, file, pos, tot_len);
835 fn = file->f_op->read;
836 iter_fn = file->f_op->read_iter;
838 fn = (io_fn_t)file->f_op->write;
839 iter_fn = file->f_op->write_iter;
840 file_start_write(file);
844 ret = do_iter_readv_writev(file, &iter, pos, iter_fn);
846 ret = do_loop_readv_writev(file, &iter, pos, fn);
849 file_end_write(file);
853 if ((ret + (type == READ)) > 0) {
855 fsnotify_access(file);
857 fsnotify_modify(file);
862 ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
863 unsigned long vlen, loff_t *pos)
865 if (!(file->f_mode & FMODE_READ))
867 if (!(file->f_mode & FMODE_CAN_READ))
870 return do_readv_writev(READ, file, vec, vlen, pos);
873 EXPORT_SYMBOL(vfs_readv);
875 ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
876 unsigned long vlen, loff_t *pos)
878 if (!(file->f_mode & FMODE_WRITE))
880 if (!(file->f_mode & FMODE_CAN_WRITE))
883 return do_readv_writev(WRITE, file, vec, vlen, pos);
886 EXPORT_SYMBOL(vfs_writev);
888 SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
891 struct fd f = fdget_pos(fd);
892 ssize_t ret = -EBADF;
895 loff_t pos = file_pos_read(f.file);
896 ret = vfs_readv(f.file, vec, vlen, &pos);
898 file_pos_write(f.file, pos);
903 add_rchar(current, ret);
908 SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
911 struct fd f = fdget_pos(fd);
912 ssize_t ret = -EBADF;
915 loff_t pos = file_pos_read(f.file);
916 ret = vfs_writev(f.file, vec, vlen, &pos);
918 file_pos_write(f.file, pos);
923 add_wchar(current, ret);
928 static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
930 #define HALF_LONG_BITS (BITS_PER_LONG / 2)
931 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
934 SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
935 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
937 loff_t pos = pos_from_hilo(pos_h, pos_l);
939 ssize_t ret = -EBADF;
947 if (f.file->f_mode & FMODE_PREAD)
948 ret = vfs_readv(f.file, vec, vlen, &pos);
953 add_rchar(current, ret);
958 SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
959 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
961 loff_t pos = pos_from_hilo(pos_h, pos_l);
963 ssize_t ret = -EBADF;
971 if (f.file->f_mode & FMODE_PWRITE)
972 ret = vfs_writev(f.file, vec, vlen, &pos);
977 add_wchar(current, ret);
984 static ssize_t compat_do_readv_writev(int type, struct file *file,
985 const struct compat_iovec __user *uvector,
986 unsigned long nr_segs, loff_t *pos)
988 compat_ssize_t tot_len;
989 struct iovec iovstack[UIO_FASTIOV];
990 struct iovec *iov = iovstack;
991 struct iov_iter iter;
996 ret = compat_import_iovec(type, uvector, nr_segs,
997 UIO_FASTIOV, &iov, &iter);
1001 tot_len = iov_iter_count(&iter);
1004 ret = rw_verify_area(type, file, pos, tot_len);
1009 fn = file->f_op->read;
1010 iter_fn = file->f_op->read_iter;
1012 fn = (io_fn_t)file->f_op->write;
1013 iter_fn = file->f_op->write_iter;
1014 file_start_write(file);
1018 ret = do_iter_readv_writev(file, &iter, pos, iter_fn);
1020 ret = do_loop_readv_writev(file, &iter, pos, fn);
1023 file_end_write(file);
1027 if ((ret + (type == READ)) > 0) {
1029 fsnotify_access(file);
1031 fsnotify_modify(file);
1036 static size_t compat_readv(struct file *file,
1037 const struct compat_iovec __user *vec,
1038 unsigned long vlen, loff_t *pos)
1040 ssize_t ret = -EBADF;
1042 if (!(file->f_mode & FMODE_READ))
1046 if (!(file->f_mode & FMODE_CAN_READ))
1049 ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
1053 add_rchar(current, ret);
1058 COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
1059 const struct compat_iovec __user *,vec,
1060 compat_ulong_t, vlen)
1062 struct fd f = fdget_pos(fd);
1068 pos = f.file->f_pos;
1069 ret = compat_readv(f.file, vec, vlen, &pos);
1071 f.file->f_pos = pos;
1076 static long __compat_sys_preadv64(unsigned long fd,
1077 const struct compat_iovec __user *vec,
1078 unsigned long vlen, loff_t pos)
1089 if (f.file->f_mode & FMODE_PREAD)
1090 ret = compat_readv(f.file, vec, vlen, &pos);
1095 #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1096 COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1097 const struct compat_iovec __user *,vec,
1098 unsigned long, vlen, loff_t, pos)
1100 return __compat_sys_preadv64(fd, vec, vlen, pos);
1104 COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
1105 const struct compat_iovec __user *,vec,
1106 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
1108 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1110 return __compat_sys_preadv64(fd, vec, vlen, pos);
1113 static size_t compat_writev(struct file *file,
1114 const struct compat_iovec __user *vec,
1115 unsigned long vlen, loff_t *pos)
1117 ssize_t ret = -EBADF;
1119 if (!(file->f_mode & FMODE_WRITE))
1123 if (!(file->f_mode & FMODE_CAN_WRITE))
1126 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
1130 add_wchar(current, ret);
1135 COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
1136 const struct compat_iovec __user *, vec,
1137 compat_ulong_t, vlen)
1139 struct fd f = fdget_pos(fd);
1145 pos = f.file->f_pos;
1146 ret = compat_writev(f.file, vec, vlen, &pos);
1148 f.file->f_pos = pos;
1153 static long __compat_sys_pwritev64(unsigned long fd,
1154 const struct compat_iovec __user *vec,
1155 unsigned long vlen, loff_t pos)
1166 if (f.file->f_mode & FMODE_PWRITE)
1167 ret = compat_writev(f.file, vec, vlen, &pos);
1172 #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
1173 COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1174 const struct compat_iovec __user *,vec,
1175 unsigned long, vlen, loff_t, pos)
1177 return __compat_sys_pwritev64(fd, vec, vlen, pos);
1181 COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
1182 const struct compat_iovec __user *,vec,
1183 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
1185 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1187 return __compat_sys_pwritev64(fd, vec, vlen, pos);
1191 static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1192 size_t count, loff_t max)
1195 struct inode *in_inode, *out_inode;
1202 * Get input file, and verify that it is ok..
1208 if (!(in.file->f_mode & FMODE_READ))
1212 pos = in.file->f_pos;
1215 if (!(in.file->f_mode & FMODE_PREAD))
1218 retval = rw_verify_area(READ, in.file, &pos, count);
1224 * Get output file, and verify that it is ok..
1227 out = fdget(out_fd);
1230 if (!(out.file->f_mode & FMODE_WRITE))
1233 in_inode = file_inode(in.file);
1234 out_inode = file_inode(out.file);
1235 out_pos = out.file->f_pos;
1236 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
1242 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1244 if (unlikely(pos + count > max)) {
1245 retval = -EOVERFLOW;
1254 * We need to debate whether we can enable this or not. The
1255 * man page documents EAGAIN return for the output at least,
1256 * and the application is arguably buggy if it doesn't expect
1257 * EAGAIN on a non-blocking file descriptor.
1259 if (in.file->f_flags & O_NONBLOCK)
1260 fl = SPLICE_F_NONBLOCK;
1262 file_start_write(out.file);
1263 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
1264 file_end_write(out.file);
1267 add_rchar(current, retval);
1268 add_wchar(current, retval);
1269 fsnotify_access(in.file);
1270 fsnotify_modify(out.file);
1271 out.file->f_pos = out_pos;
1275 in.file->f_pos = pos;
1281 retval = -EOVERFLOW;
1291 SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
1298 if (unlikely(get_user(off, offset)))
1301 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1302 if (unlikely(put_user(pos, offset)))
1307 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1310 SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
1316 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1318 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1319 if (unlikely(put_user(pos, offset)))
1324 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1327 #ifdef CONFIG_COMPAT
1328 COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1329 compat_off_t __user *, offset, compat_size_t, count)
1336 if (unlikely(get_user(off, offset)))
1339 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1340 if (unlikely(put_user(pos, offset)))
1345 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1348 COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1349 compat_loff_t __user *, offset, compat_size_t, count)
1355 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1357 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1358 if (unlikely(put_user(pos, offset)))
1363 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1368 * copy_file_range() differs from regular file read and write in that it
1369 * specifically allows return partial success. When it does so is up to
1370 * the copy_file_range method.
1372 ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
1373 struct file *file_out, loff_t pos_out,
1374 size_t len, unsigned int flags)
1376 struct inode *inode_in = file_inode(file_in);
1377 struct inode *inode_out = file_inode(file_out);
1383 /* copy_file_range allows full ssize_t len, ignoring MAX_RW_COUNT */
1384 ret = rw_verify_area(READ, file_in, &pos_in, len);
1386 ret = rw_verify_area(WRITE, file_out, &pos_out, len);
1390 if (!(file_in->f_mode & FMODE_READ) ||
1391 !(file_out->f_mode & FMODE_WRITE) ||
1392 (file_out->f_flags & O_APPEND))
1395 /* this could be relaxed once a method supports cross-fs copies */
1396 if (inode_in->i_sb != inode_out->i_sb)
1402 ret = mnt_want_write_file(file_out);
1407 if (file_out->f_op->copy_file_range)
1408 ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out,
1409 pos_out, len, flags);
1410 if (ret == -EOPNOTSUPP)
1411 ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out,
1412 len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0);
1415 fsnotify_access(file_in);
1416 add_rchar(current, ret);
1417 fsnotify_modify(file_out);
1418 add_wchar(current, ret);
1423 mnt_drop_write_file(file_out);
1427 EXPORT_SYMBOL(vfs_copy_file_range);
1429 SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
1430 int, fd_out, loff_t __user *, off_out,
1431 size_t, len, unsigned int, flags)
1437 ssize_t ret = -EBADF;
1439 f_in = fdget(fd_in);
1443 f_out = fdget(fd_out);
1449 if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
1452 pos_in = f_in.file->f_pos;
1456 if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
1459 pos_out = f_out.file->f_pos;
1462 ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
1469 if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
1472 f_in.file->f_pos = pos_in;
1476 if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
1479 f_out.file->f_pos = pos_out;
1491 static int clone_verify_area(struct file *file, loff_t pos, u64 len, bool write)
1493 struct inode *inode = file_inode(file);
1495 if (unlikely(pos < 0))
1498 if (unlikely((loff_t) (pos + len) < 0))
1501 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
1502 loff_t end = len ? pos + len - 1 : OFFSET_MAX;
1505 retval = locks_mandatory_area(inode, file, pos, end,
1506 write ? F_WRLCK : F_RDLCK);
1511 return security_file_permission(file, write ? MAY_WRITE : MAY_READ);
1514 int vfs_clone_file_range(struct file *file_in, loff_t pos_in,
1515 struct file *file_out, loff_t pos_out, u64 len)
1517 struct inode *inode_in = file_inode(file_in);
1518 struct inode *inode_out = file_inode(file_out);
1521 if (inode_in->i_sb != inode_out->i_sb ||
1522 file_in->f_path.mnt != file_out->f_path.mnt)
1525 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1527 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1530 if (!(file_in->f_mode & FMODE_READ) ||
1531 !(file_out->f_mode & FMODE_WRITE) ||
1532 (file_out->f_flags & O_APPEND) ||
1533 !file_in->f_op->clone_file_range)
1536 ret = clone_verify_area(file_in, pos_in, len, false);
1540 ret = clone_verify_area(file_out, pos_out, len, true);
1544 if (pos_in + len > i_size_read(inode_in))
1547 ret = mnt_want_write_file(file_out);
1551 ret = file_in->f_op->clone_file_range(file_in, pos_in,
1552 file_out, pos_out, len);
1554 fsnotify_access(file_in);
1555 fsnotify_modify(file_out);
1558 mnt_drop_write_file(file_out);
1561 EXPORT_SYMBOL(vfs_clone_file_range);
1563 int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same)
1565 struct file_dedupe_range_info *info;
1566 struct inode *src = file_inode(file);
1571 bool is_admin = capable(CAP_SYS_ADMIN);
1572 u16 count = same->dest_count;
1573 struct file *dst_file;
1577 if (!(file->f_mode & FMODE_READ))
1580 if (same->reserved1 || same->reserved2)
1583 off = same->src_offset;
1584 len = same->src_length;
1587 if (S_ISDIR(src->i_mode))
1591 if (!S_ISREG(src->i_mode))
1594 ret = clone_verify_area(file, off, len, false);
1599 /* pre-format output fields to sane values */
1600 for (i = 0; i < count; i++) {
1601 same->info[i].bytes_deduped = 0ULL;
1602 same->info[i].status = FILE_DEDUPE_RANGE_SAME;
1605 for (i = 0, info = same->info; i < count; i++, info++) {
1607 struct fd dst_fd = fdget(info->dest_fd);
1609 dst_file = dst_fd.file;
1611 info->status = -EBADF;
1614 dst = file_inode(dst_file);
1616 ret = mnt_want_write_file(dst_file);
1622 dst_off = info->dest_offset;
1623 ret = clone_verify_area(dst_file, dst_off, len, true);
1630 if (info->reserved) {
1631 info->status = -EINVAL;
1632 } else if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
1633 info->status = -EINVAL;
1634 } else if (file->f_path.mnt != dst_file->f_path.mnt) {
1635 info->status = -EXDEV;
1636 } else if (S_ISDIR(dst->i_mode)) {
1637 info->status = -EISDIR;
1638 } else if (dst_file->f_op->dedupe_file_range == NULL) {
1639 info->status = -EINVAL;
1641 deduped = dst_file->f_op->dedupe_file_range(file, off,
1644 if (deduped == -EBADE)
1645 info->status = FILE_DEDUPE_RANGE_DIFFERS;
1646 else if (deduped < 0)
1647 info->status = deduped;
1649 info->bytes_deduped += deduped;
1653 mnt_drop_write_file(dst_file);
1657 if (fatal_signal_pending(current))
1664 EXPORT_SYMBOL(vfs_dedupe_file_range);