1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/read_write.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/slab.h>
9 #include <linux/stat.h>
10 #include <linux/sched/xacct.h>
11 #include <linux/fcntl.h>
12 #include <linux/file.h>
13 #include <linux/uio.h>
14 #include <linux/fsnotify.h>
15 #include <linux/security.h>
16 #include <linux/export.h>
17 #include <linux/syscalls.h>
18 #include <linux/pagemap.h>
19 #include <linux/splice.h>
20 #include <linux/compat.h>
21 #include <linux/mount.h>
25 #include <linux/uaccess.h>
26 #include <asm/unistd.h>
28 const struct file_operations generic_ro_fops = {
29 .llseek = generic_file_llseek,
30 .read_iter = generic_file_read_iter,
31 .mmap = generic_file_readonly_mmap,
32 .splice_read = filemap_splice_read,
35 EXPORT_SYMBOL(generic_ro_fops);
37 static inline bool unsigned_offsets(struct file *file)
39 return file->f_op->fop_flags & FOP_UNSIGNED_OFFSET;
43 * vfs_setpos_cookie - update the file offset for lseek and reset cookie
44 * @file: file structure in question
45 * @offset: file offset to seek to
46 * @maxsize: maximum file size
47 * @cookie: cookie to reset
49 * Update the file offset to the value specified by @offset if the given
50 * offset is valid and it is not equal to the current file offset and
51 * reset the specified cookie to indicate that a seek happened.
53 * Return the specified offset on success and -EINVAL on invalid offset.
55 static loff_t vfs_setpos_cookie(struct file *file, loff_t offset,
56 loff_t maxsize, u64 *cookie)
58 if (offset < 0 && !unsigned_offsets(file))
63 if (offset != file->f_pos) {
72 * vfs_setpos - update the file offset for lseek
73 * @file: file structure in question
74 * @offset: file offset to seek to
75 * @maxsize: maximum file size
77 * This is a low-level filesystem helper for updating the file offset to
78 * the value specified by @offset if the given offset is valid and it is
79 * not equal to the current file offset.
81 * Return the specified offset on success and -EINVAL on invalid offset.
83 loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
85 return vfs_setpos_cookie(file, offset, maxsize, NULL);
87 EXPORT_SYMBOL(vfs_setpos);
90 * must_set_pos - check whether f_pos has to be updated
91 * @file: file to seek on
92 * @offset: offset to use
93 * @whence: type of seek operation
96 * Check whether f_pos needs to be updated and update @offset according
99 * Return: 0 if f_pos doesn't need to be updated, 1 if f_pos has to be
100 * updated, and negative error code on failure.
102 static int must_set_pos(struct file *file, loff_t *offset, int whence, loff_t eof)
110 * Here we special-case the lseek(fd, 0, SEEK_CUR)
111 * position-querying operation. Avoid rewriting the "same"
112 * f_pos value back to the file because a concurrent read(),
113 * write() or lseek() might have altered it
116 *offset = file->f_pos;
122 * In the generic case the entire file is data, so as long as
123 * offset isn't at the end of the file then the offset is data.
125 if ((unsigned long long)*offset >= eof)
130 * There is a virtual hole at the end of the file, so as long as
131 * offset isn't i_size or larger, return i_size.
133 if ((unsigned long long)*offset >= eof)
143 * generic_file_llseek_size - generic llseek implementation for regular files
144 * @file: file structure to seek on
145 * @offset: file offset to seek to
146 * @whence: type of seek
147 * @maxsize: max size of this file in file system
148 * @eof: offset used for SEEK_END position
150 * This is a variant of generic_file_llseek that allows passing in a custom
151 * maximum file size and a custom EOF position, for e.g. hashed directories
154 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
155 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
156 * read/writes behave like SEEK_SET against seeks.
159 generic_file_llseek_size(struct file *file, loff_t offset, int whence,
160 loff_t maxsize, loff_t eof)
164 ret = must_set_pos(file, &offset, whence, eof);
170 if (whence == SEEK_CUR) {
172 * If the file requires locking via f_pos_lock we know
173 * that mutual exclusion for SEEK_CUR on the same file
174 * is guaranteed. If the file isn't locked, we take
175 * f_lock to protect against f_pos races with other
178 if (file_seek_cur_needs_f_lock(file)) {
179 guard(spinlock)(&file->f_lock);
180 return vfs_setpos(file, file->f_pos + offset, maxsize);
182 return vfs_setpos(file, file->f_pos + offset, maxsize);
185 return vfs_setpos(file, offset, maxsize);
187 EXPORT_SYMBOL(generic_file_llseek_size);
190 * generic_llseek_cookie - versioned llseek implementation
191 * @file: file structure to seek on
192 * @offset: file offset to seek to
193 * @whence: type of seek
194 * @cookie: cookie to update
196 * See generic_file_llseek for a general description and locking assumptions.
198 * In contrast to generic_file_llseek, this function also resets a
199 * specified cookie to indicate a seek took place.
201 loff_t generic_llseek_cookie(struct file *file, loff_t offset, int whence,
204 struct inode *inode = file->f_mapping->host;
205 loff_t maxsize = inode->i_sb->s_maxbytes;
206 loff_t eof = i_size_read(inode);
209 if (WARN_ON_ONCE(!cookie))
213 * Require that this is only used for directories that guarantee
214 * synchronization between readdir and seek so that an update to
215 * @cookie is correctly synchronized with concurrent readdir.
217 if (WARN_ON_ONCE(!(file->f_mode & FMODE_ATOMIC_POS)))
220 ret = must_set_pos(file, &offset, whence, eof);
226 /* No need to hold f_lock because we know that f_pos_lock is held. */
227 if (whence == SEEK_CUR)
228 return vfs_setpos_cookie(file, file->f_pos + offset, maxsize, cookie);
230 return vfs_setpos_cookie(file, offset, maxsize, cookie);
232 EXPORT_SYMBOL(generic_llseek_cookie);
235 * generic_file_llseek - generic llseek implementation for regular files
236 * @file: file structure to seek on
237 * @offset: file offset to seek to
238 * @whence: type of seek
240 * This is a generic implemenation of ->llseek useable for all normal local
241 * filesystems. It just updates the file offset to the value specified by
242 * @offset and @whence.
244 loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
246 struct inode *inode = file->f_mapping->host;
248 return generic_file_llseek_size(file, offset, whence,
249 inode->i_sb->s_maxbytes,
252 EXPORT_SYMBOL(generic_file_llseek);
255 * fixed_size_llseek - llseek implementation for fixed-sized devices
256 * @file: file structure to seek on
257 * @offset: file offset to seek to
258 * @whence: type of seek
259 * @size: size of the file
262 loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
265 case SEEK_SET: case SEEK_CUR: case SEEK_END:
266 return generic_file_llseek_size(file, offset, whence,
272 EXPORT_SYMBOL(fixed_size_llseek);
275 * no_seek_end_llseek - llseek implementation for fixed-sized devices
276 * @file: file structure to seek on
277 * @offset: file offset to seek to
278 * @whence: type of seek
281 loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence)
284 case SEEK_SET: case SEEK_CUR:
285 return generic_file_llseek_size(file, offset, whence,
291 EXPORT_SYMBOL(no_seek_end_llseek);
294 * no_seek_end_llseek_size - llseek implementation for fixed-sized devices
295 * @file: file structure to seek on
296 * @offset: file offset to seek to
297 * @whence: type of seek
298 * @size: maximal offset allowed
301 loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size)
304 case SEEK_SET: case SEEK_CUR:
305 return generic_file_llseek_size(file, offset, whence,
311 EXPORT_SYMBOL(no_seek_end_llseek_size);
314 * noop_llseek - No Operation Performed llseek implementation
315 * @file: file structure to seek on
316 * @offset: file offset to seek to
317 * @whence: type of seek
319 * This is an implementation of ->llseek useable for the rare special case when
320 * userspace expects the seek to succeed but the (device) file is actually not
321 * able to perform the seek. In this case you use noop_llseek() instead of
322 * falling back to the default implementation of ->llseek.
324 loff_t noop_llseek(struct file *file, loff_t offset, int whence)
328 EXPORT_SYMBOL(noop_llseek);
330 loff_t default_llseek(struct file *file, loff_t offset, int whence)
332 struct inode *inode = file_inode(file);
335 retval = inode_lock_killable(inode);
340 offset += i_size_read(inode);
344 retval = file->f_pos;
347 offset += file->f_pos;
351 * In the generic case the entire file is data, so as
352 * long as offset isn't at the end of the file then the
355 if (offset >= inode->i_size) {
362 * There is a virtual hole at the end of the file, so
363 * as long as offset isn't i_size or larger, return
366 if (offset >= inode->i_size) {
370 offset = inode->i_size;
374 if (offset >= 0 || unsigned_offsets(file)) {
375 if (offset != file->f_pos)
376 file->f_pos = offset;
383 EXPORT_SYMBOL(default_llseek);
385 loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
387 if (!(file->f_mode & FMODE_LSEEK))
389 return file->f_op->llseek(file, offset, whence);
391 EXPORT_SYMBOL(vfs_llseek);
393 static off_t ksys_lseek(unsigned int fd, off_t offset, unsigned int whence)
396 CLASS(fd_pos, f)(fd);
401 if (whence <= SEEK_MAX) {
402 loff_t res = vfs_llseek(fd_file(f), offset, whence);
404 if (res != (loff_t)retval)
405 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
410 SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
412 return ksys_lseek(fd, offset, whence);
416 COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
418 return ksys_lseek(fd, offset, whence);
422 #if !defined(CONFIG_64BIT) || defined(CONFIG_COMPAT) || \
423 defined(__ARCH_WANT_SYS_LLSEEK)
424 SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
425 unsigned long, offset_low, loff_t __user *, result,
426 unsigned int, whence)
429 CLASS(fd_pos, f)(fd);
435 if (whence > SEEK_MAX)
438 offset = vfs_llseek(fd_file(f), ((loff_t) offset_high << 32) | offset_low,
441 retval = (int)offset;
444 if (!copy_to_user(result, &offset, sizeof(offset)))
451 int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
453 int mask = read_write == READ ? MAY_READ : MAY_WRITE;
456 if (unlikely((ssize_t) count < 0))
462 if (unlikely(pos < 0)) {
463 if (!unsigned_offsets(file))
465 if (count >= -pos) /* both values are in 0..LLONG_MAX */
467 } else if (unlikely((loff_t) (pos + count) < 0)) {
468 if (!unsigned_offsets(file))
473 ret = security_file_permission(file, mask);
477 return fsnotify_file_area_perm(file, mask, ppos, count);
479 EXPORT_SYMBOL(rw_verify_area);
481 static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
484 struct iov_iter iter;
487 init_sync_kiocb(&kiocb, filp);
488 kiocb.ki_pos = (ppos ? *ppos : 0);
489 iov_iter_ubuf(&iter, ITER_DEST, buf, len);
491 ret = filp->f_op->read_iter(&kiocb, &iter);
492 BUG_ON(ret == -EIOCBQUEUED);
494 *ppos = kiocb.ki_pos;
498 static int warn_unsupported(struct file *file, const char *op)
501 "kernel %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
502 op, file, current->pid, current->comm);
506 ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
510 .iov_len = min_t(size_t, count, MAX_RW_COUNT),
513 struct iov_iter iter;
516 if (WARN_ON_ONCE(!(file->f_mode & FMODE_READ)))
518 if (!(file->f_mode & FMODE_CAN_READ))
521 * Also fail if ->read_iter and ->read are both wired up as that
522 * implies very convoluted semantics.
524 if (unlikely(!file->f_op->read_iter || file->f_op->read))
525 return warn_unsupported(file, "read");
527 init_sync_kiocb(&kiocb, file);
528 kiocb.ki_pos = pos ? *pos : 0;
529 iov_iter_kvec(&iter, ITER_DEST, &iov, 1, iov.iov_len);
530 ret = file->f_op->read_iter(&kiocb, &iter);
534 fsnotify_access(file);
535 add_rchar(current, ret);
541 ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
545 ret = rw_verify_area(READ, file, pos, count);
548 return __kernel_read(file, buf, count, pos);
550 EXPORT_SYMBOL(kernel_read);
552 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
556 if (!(file->f_mode & FMODE_READ))
558 if (!(file->f_mode & FMODE_CAN_READ))
560 if (unlikely(!access_ok(buf, count)))
563 ret = rw_verify_area(READ, file, pos, count);
566 if (count > MAX_RW_COUNT)
567 count = MAX_RW_COUNT;
569 if (file->f_op->read)
570 ret = file->f_op->read(file, buf, count, pos);
571 else if (file->f_op->read_iter)
572 ret = new_sync_read(file, buf, count, pos);
576 fsnotify_access(file);
577 add_rchar(current, ret);
583 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
586 struct iov_iter iter;
589 init_sync_kiocb(&kiocb, filp);
590 kiocb.ki_pos = (ppos ? *ppos : 0);
591 iov_iter_ubuf(&iter, ITER_SOURCE, (void __user *)buf, len);
593 ret = filp->f_op->write_iter(&kiocb, &iter);
594 BUG_ON(ret == -EIOCBQUEUED);
596 *ppos = kiocb.ki_pos;
600 /* caller is responsible for file_start_write/file_end_write */
601 ssize_t __kernel_write_iter(struct file *file, struct iov_iter *from, loff_t *pos)
606 if (WARN_ON_ONCE(!(file->f_mode & FMODE_WRITE)))
608 if (!(file->f_mode & FMODE_CAN_WRITE))
611 * Also fail if ->write_iter and ->write are both wired up as that
612 * implies very convoluted semantics.
614 if (unlikely(!file->f_op->write_iter || file->f_op->write))
615 return warn_unsupported(file, "write");
617 init_sync_kiocb(&kiocb, file);
618 kiocb.ki_pos = pos ? *pos : 0;
619 ret = file->f_op->write_iter(&kiocb, from);
623 fsnotify_modify(file);
624 add_wchar(current, ret);
630 /* caller is responsible for file_start_write/file_end_write */
631 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
634 .iov_base = (void *)buf,
635 .iov_len = min_t(size_t, count, MAX_RW_COUNT),
637 struct iov_iter iter;
638 iov_iter_kvec(&iter, ITER_SOURCE, &iov, 1, iov.iov_len);
639 return __kernel_write_iter(file, &iter, pos);
642 * This "EXPORT_SYMBOL_GPL()" is more of a "EXPORT_SYMBOL_DONTUSE()",
643 * but autofs is one of the few internal kernel users that actually
644 * wants this _and_ can be built as a module. So we need to export
645 * this symbol for autofs, even though it really isn't appropriate
646 * for any other kernel modules.
648 EXPORT_SYMBOL_GPL(__kernel_write);
650 ssize_t kernel_write(struct file *file, const void *buf, size_t count,
655 ret = rw_verify_area(WRITE, file, pos, count);
659 file_start_write(file);
660 ret = __kernel_write(file, buf, count, pos);
661 file_end_write(file);
664 EXPORT_SYMBOL(kernel_write);
666 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
670 if (!(file->f_mode & FMODE_WRITE))
672 if (!(file->f_mode & FMODE_CAN_WRITE))
674 if (unlikely(!access_ok(buf, count)))
677 ret = rw_verify_area(WRITE, file, pos, count);
680 if (count > MAX_RW_COUNT)
681 count = MAX_RW_COUNT;
682 file_start_write(file);
683 if (file->f_op->write)
684 ret = file->f_op->write(file, buf, count, pos);
685 else if (file->f_op->write_iter)
686 ret = new_sync_write(file, buf, count, pos);
690 fsnotify_modify(file);
691 add_wchar(current, ret);
694 file_end_write(file);
698 /* file_ppos returns &file->f_pos or NULL if file is stream */
699 static inline loff_t *file_ppos(struct file *file)
701 return file->f_mode & FMODE_STREAM ? NULL : &file->f_pos;
704 ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count)
706 CLASS(fd_pos, f)(fd);
707 ssize_t ret = -EBADF;
710 loff_t pos, *ppos = file_ppos(fd_file(f));
715 ret = vfs_read(fd_file(f), buf, count, ppos);
716 if (ret >= 0 && ppos)
717 fd_file(f)->f_pos = pos;
722 SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
724 return ksys_read(fd, buf, count);
727 ssize_t ksys_write(unsigned int fd, const char __user *buf, size_t count)
729 CLASS(fd_pos, f)(fd);
730 ssize_t ret = -EBADF;
733 loff_t pos, *ppos = file_ppos(fd_file(f));
738 ret = vfs_write(fd_file(f), buf, count, ppos);
739 if (ret >= 0 && ppos)
740 fd_file(f)->f_pos = pos;
746 SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
749 return ksys_write(fd, buf, count);
752 ssize_t ksys_pread64(unsigned int fd, char __user *buf, size_t count,
762 if (fd_file(f)->f_mode & FMODE_PREAD)
763 return vfs_read(fd_file(f), buf, count, &pos);
768 SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
769 size_t, count, loff_t, pos)
771 return ksys_pread64(fd, buf, count, pos);
774 #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_PREAD64)
775 COMPAT_SYSCALL_DEFINE5(pread64, unsigned int, fd, char __user *, buf,
776 size_t, count, compat_arg_u64_dual(pos))
778 return ksys_pread64(fd, buf, count, compat_arg_u64_glue(pos));
782 ssize_t ksys_pwrite64(unsigned int fd, const char __user *buf,
783 size_t count, loff_t pos)
792 if (fd_file(f)->f_mode & FMODE_PWRITE)
793 return vfs_write(fd_file(f), buf, count, &pos);
798 SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
799 size_t, count, loff_t, pos)
801 return ksys_pwrite64(fd, buf, count, pos);
804 #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_PWRITE64)
805 COMPAT_SYSCALL_DEFINE5(pwrite64, unsigned int, fd, const char __user *, buf,
806 size_t, count, compat_arg_u64_dual(pos))
808 return ksys_pwrite64(fd, buf, count, compat_arg_u64_glue(pos));
812 static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
813 loff_t *ppos, int type, rwf_t flags)
818 init_sync_kiocb(&kiocb, filp);
819 ret = kiocb_set_rw_flags(&kiocb, flags, type);
822 kiocb.ki_pos = (ppos ? *ppos : 0);
825 ret = filp->f_op->read_iter(&kiocb, iter);
827 ret = filp->f_op->write_iter(&kiocb, iter);
828 BUG_ON(ret == -EIOCBQUEUED);
830 *ppos = kiocb.ki_pos;
834 /* Do it by hand, with file-ops */
835 static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
836 loff_t *ppos, int type, rwf_t flags)
840 if (flags & ~RWF_HIPRI)
843 while (iov_iter_count(iter)) {
847 nr = filp->f_op->read(filp, iter_iov_addr(iter),
848 iter_iov_len(iter), ppos);
850 nr = filp->f_op->write(filp, iter_iov_addr(iter),
851 iter_iov_len(iter), ppos);
860 if (nr != iter_iov_len(iter))
862 iov_iter_advance(iter, nr);
868 ssize_t vfs_iocb_iter_read(struct file *file, struct kiocb *iocb,
869 struct iov_iter *iter)
874 if (!file->f_op->read_iter)
876 if (!(file->f_mode & FMODE_READ))
878 if (!(file->f_mode & FMODE_CAN_READ))
881 tot_len = iov_iter_count(iter);
884 ret = rw_verify_area(READ, file, &iocb->ki_pos, tot_len);
888 ret = file->f_op->read_iter(iocb, iter);
891 fsnotify_access(file);
894 EXPORT_SYMBOL(vfs_iocb_iter_read);
896 ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos,
902 if (!file->f_op->read_iter)
904 if (!(file->f_mode & FMODE_READ))
906 if (!(file->f_mode & FMODE_CAN_READ))
909 tot_len = iov_iter_count(iter);
912 ret = rw_verify_area(READ, file, ppos, tot_len);
916 ret = do_iter_readv_writev(file, iter, ppos, READ, flags);
919 fsnotify_access(file);
922 EXPORT_SYMBOL(vfs_iter_read);
925 * Caller is responsible for calling kiocb_end_write() on completion
926 * if async iocb was queued.
928 ssize_t vfs_iocb_iter_write(struct file *file, struct kiocb *iocb,
929 struct iov_iter *iter)
934 if (!file->f_op->write_iter)
936 if (!(file->f_mode & FMODE_WRITE))
938 if (!(file->f_mode & FMODE_CAN_WRITE))
941 tot_len = iov_iter_count(iter);
944 ret = rw_verify_area(WRITE, file, &iocb->ki_pos, tot_len);
948 kiocb_start_write(iocb);
949 ret = file->f_op->write_iter(iocb, iter);
950 if (ret != -EIOCBQUEUED)
951 kiocb_end_write(iocb);
953 fsnotify_modify(file);
957 EXPORT_SYMBOL(vfs_iocb_iter_write);
959 ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos,
965 if (!(file->f_mode & FMODE_WRITE))
967 if (!(file->f_mode & FMODE_CAN_WRITE))
969 if (!file->f_op->write_iter)
972 tot_len = iov_iter_count(iter);
976 ret = rw_verify_area(WRITE, file, ppos, tot_len);
980 file_start_write(file);
981 ret = do_iter_readv_writev(file, iter, ppos, WRITE, flags);
983 fsnotify_modify(file);
984 file_end_write(file);
988 EXPORT_SYMBOL(vfs_iter_write);
990 static ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
991 unsigned long vlen, loff_t *pos, rwf_t flags)
993 struct iovec iovstack[UIO_FASTIOV];
994 struct iovec *iov = iovstack;
995 struct iov_iter iter;
999 if (!(file->f_mode & FMODE_READ))
1001 if (!(file->f_mode & FMODE_CAN_READ))
1004 ret = import_iovec(ITER_DEST, vec, vlen, ARRAY_SIZE(iovstack), &iov,
1009 tot_len = iov_iter_count(&iter);
1013 ret = rw_verify_area(READ, file, pos, tot_len);
1017 if (file->f_op->read_iter)
1018 ret = do_iter_readv_writev(file, &iter, pos, READ, flags);
1020 ret = do_loop_readv_writev(file, &iter, pos, READ, flags);
1023 fsnotify_access(file);
1028 static ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
1029 unsigned long vlen, loff_t *pos, rwf_t flags)
1031 struct iovec iovstack[UIO_FASTIOV];
1032 struct iovec *iov = iovstack;
1033 struct iov_iter iter;
1037 if (!(file->f_mode & FMODE_WRITE))
1039 if (!(file->f_mode & FMODE_CAN_WRITE))
1042 ret = import_iovec(ITER_SOURCE, vec, vlen, ARRAY_SIZE(iovstack), &iov,
1047 tot_len = iov_iter_count(&iter);
1051 ret = rw_verify_area(WRITE, file, pos, tot_len);
1055 file_start_write(file);
1056 if (file->f_op->write_iter)
1057 ret = do_iter_readv_writev(file, &iter, pos, WRITE, flags);
1059 ret = do_loop_readv_writev(file, &iter, pos, WRITE, flags);
1061 fsnotify_modify(file);
1062 file_end_write(file);
1068 static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
1069 unsigned long vlen, rwf_t flags)
1071 CLASS(fd_pos, f)(fd);
1072 ssize_t ret = -EBADF;
1075 loff_t pos, *ppos = file_ppos(fd_file(f));
1080 ret = vfs_readv(fd_file(f), vec, vlen, ppos, flags);
1081 if (ret >= 0 && ppos)
1082 fd_file(f)->f_pos = pos;
1086 add_rchar(current, ret);
1091 static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
1092 unsigned long vlen, rwf_t flags)
1094 CLASS(fd_pos, f)(fd);
1095 ssize_t ret = -EBADF;
1098 loff_t pos, *ppos = file_ppos(fd_file(f));
1103 ret = vfs_writev(fd_file(f), vec, vlen, ppos, flags);
1104 if (ret >= 0 && ppos)
1105 fd_file(f)->f_pos = pos;
1109 add_wchar(current, ret);
1114 static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
1116 #define HALF_LONG_BITS (BITS_PER_LONG / 2)
1117 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
1120 static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec,
1121 unsigned long vlen, loff_t pos, rwf_t flags)
1123 ssize_t ret = -EBADF;
1131 if (fd_file(f)->f_mode & FMODE_PREAD)
1132 ret = vfs_readv(fd_file(f), vec, vlen, &pos, flags);
1136 add_rchar(current, ret);
1141 static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
1142 unsigned long vlen, loff_t pos, rwf_t flags)
1144 ssize_t ret = -EBADF;
1152 if (fd_file(f)->f_mode & FMODE_PWRITE)
1153 ret = vfs_writev(fd_file(f), vec, vlen, &pos, flags);
1157 add_wchar(current, ret);
1162 SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
1163 unsigned long, vlen)
1165 return do_readv(fd, vec, vlen, 0);
1168 SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
1169 unsigned long, vlen)
1171 return do_writev(fd, vec, vlen, 0);
1174 SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
1175 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1177 loff_t pos = pos_from_hilo(pos_h, pos_l);
1179 return do_preadv(fd, vec, vlen, pos, 0);
1182 SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec,
1183 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
1186 loff_t pos = pos_from_hilo(pos_h, pos_l);
1189 return do_readv(fd, vec, vlen, flags);
1191 return do_preadv(fd, vec, vlen, pos, flags);
1194 SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
1195 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1197 loff_t pos = pos_from_hilo(pos_h, pos_l);
1199 return do_pwritev(fd, vec, vlen, pos, 0);
1202 SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,
1203 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
1206 loff_t pos = pos_from_hilo(pos_h, pos_l);
1209 return do_writev(fd, vec, vlen, flags);
1211 return do_pwritev(fd, vec, vlen, pos, flags);
1215 * Various compat syscalls. Note that they all pretend to take a native
1216 * iovec - import_iovec will properly treat those as compat_iovecs based on
1217 * in_compat_syscall().
1219 #ifdef CONFIG_COMPAT
1220 #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1221 COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1222 const struct iovec __user *, vec,
1223 unsigned long, vlen, loff_t, pos)
1225 return do_preadv(fd, vec, vlen, pos, 0);
1229 COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
1230 const struct iovec __user *, vec,
1231 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
1233 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1235 return do_preadv(fd, vec, vlen, pos, 0);
1238 #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2
1239 COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd,
1240 const struct iovec __user *, vec,
1241 unsigned long, vlen, loff_t, pos, rwf_t, flags)
1244 return do_readv(fd, vec, vlen, flags);
1245 return do_preadv(fd, vec, vlen, pos, flags);
1249 COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd,
1250 const struct iovec __user *, vec,
1251 compat_ulong_t, vlen, u32, pos_low, u32, pos_high,
1254 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1257 return do_readv(fd, vec, vlen, flags);
1258 return do_preadv(fd, vec, vlen, pos, flags);
1261 #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
1262 COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1263 const struct iovec __user *, vec,
1264 unsigned long, vlen, loff_t, pos)
1266 return do_pwritev(fd, vec, vlen, pos, 0);
1270 COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
1271 const struct iovec __user *,vec,
1272 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
1274 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1276 return do_pwritev(fd, vec, vlen, pos, 0);
1279 #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2
1280 COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd,
1281 const struct iovec __user *, vec,
1282 unsigned long, vlen, loff_t, pos, rwf_t, flags)
1285 return do_writev(fd, vec, vlen, flags);
1286 return do_pwritev(fd, vec, vlen, pos, flags);
1290 COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd,
1291 const struct iovec __user *,vec,
1292 compat_ulong_t, vlen, u32, pos_low, u32, pos_high, rwf_t, flags)
1294 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1297 return do_writev(fd, vec, vlen, flags);
1298 return do_pwritev(fd, vec, vlen, pos, flags);
1300 #endif /* CONFIG_COMPAT */
1302 static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1303 size_t count, loff_t max)
1305 struct inode *in_inode, *out_inode;
1306 struct pipe_inode_info *opipe;
1313 * Get input file, and verify that it is ok..
1315 CLASS(fd, in)(in_fd);
1318 if (!(fd_file(in)->f_mode & FMODE_READ))
1321 pos = fd_file(in)->f_pos;
1324 if (!(fd_file(in)->f_mode & FMODE_PREAD))
1327 retval = rw_verify_area(READ, fd_file(in), &pos, count);
1330 if (count > MAX_RW_COUNT)
1331 count = MAX_RW_COUNT;
1334 * Get output file, and verify that it is ok..
1336 CLASS(fd, out)(out_fd);
1339 if (!(fd_file(out)->f_mode & FMODE_WRITE))
1341 in_inode = file_inode(fd_file(in));
1342 out_inode = file_inode(fd_file(out));
1343 out_pos = fd_file(out)->f_pos;
1346 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1348 if (unlikely(pos + count > max)) {
1357 * We need to debate whether we can enable this or not. The
1358 * man page documents EAGAIN return for the output at least,
1359 * and the application is arguably buggy if it doesn't expect
1360 * EAGAIN on a non-blocking file descriptor.
1362 if (fd_file(in)->f_flags & O_NONBLOCK)
1363 fl = SPLICE_F_NONBLOCK;
1365 opipe = get_pipe_info(fd_file(out), true);
1367 retval = rw_verify_area(WRITE, fd_file(out), &out_pos, count);
1370 retval = do_splice_direct(fd_file(in), &pos, fd_file(out), &out_pos,
1373 if (fd_file(out)->f_flags & O_NONBLOCK)
1374 fl |= SPLICE_F_NONBLOCK;
1376 retval = splice_file_to_pipe(fd_file(in), opipe, &pos, count, fl);
1380 add_rchar(current, retval);
1381 add_wchar(current, retval);
1382 fsnotify_access(fd_file(in));
1383 fsnotify_modify(fd_file(out));
1384 fd_file(out)->f_pos = out_pos;
1388 fd_file(in)->f_pos = pos;
1394 retval = -EOVERFLOW;
1398 SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
1405 if (unlikely(get_user(off, offset)))
1408 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1409 if (unlikely(put_user(pos, offset)))
1414 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1417 SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
1423 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1425 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1426 if (unlikely(put_user(pos, offset)))
1431 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1434 #ifdef CONFIG_COMPAT
1435 COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1436 compat_off_t __user *, offset, compat_size_t, count)
1443 if (unlikely(get_user(off, offset)))
1446 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1447 if (unlikely(put_user(pos, offset)))
1452 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1455 COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1456 compat_loff_t __user *, offset, compat_size_t, count)
1462 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1464 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1465 if (unlikely(put_user(pos, offset)))
1470 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1475 * Performs necessary checks before doing a file copy
1477 * Can adjust amount of bytes to copy via @req_count argument.
1478 * Returns appropriate error code that caller should return or
1479 * zero in case the copy should be allowed.
1481 static int generic_copy_file_checks(struct file *file_in, loff_t pos_in,
1482 struct file *file_out, loff_t pos_out,
1483 size_t *req_count, unsigned int flags)
1485 struct inode *inode_in = file_inode(file_in);
1486 struct inode *inode_out = file_inode(file_out);
1487 uint64_t count = *req_count;
1491 ret = generic_file_rw_checks(file_in, file_out);
1496 * We allow some filesystems to handle cross sb copy, but passing
1497 * a file of the wrong filesystem type to filesystem driver can result
1498 * in an attempt to dereference the wrong type of ->private_data, so
1499 * avoid doing that until we really have a good reason.
1501 * nfs and cifs define several different file_system_type structures
1502 * and several different sets of file_operations, but they all end up
1503 * using the same ->copy_file_range() function pointer.
1505 if (flags & COPY_FILE_SPLICE) {
1506 /* cross sb splice is allowed */
1507 } else if (file_out->f_op->copy_file_range) {
1508 if (file_in->f_op->copy_file_range !=
1509 file_out->f_op->copy_file_range)
1511 } else if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb) {
1515 /* Don't touch certain kinds of inodes */
1516 if (IS_IMMUTABLE(inode_out))
1519 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
1522 /* Ensure offsets don't wrap. */
1523 if (pos_in + count < pos_in || pos_out + count < pos_out)
1526 /* Shorten the copy to EOF */
1527 size_in = i_size_read(inode_in);
1528 if (pos_in >= size_in)
1531 count = min(count, size_in - (uint64_t)pos_in);
1533 ret = generic_write_check_limits(file_out, pos_out, &count);
1537 /* Don't allow overlapped copying within the same file. */
1538 if (inode_in == inode_out &&
1539 pos_out + count > pos_in &&
1540 pos_out < pos_in + count)
1548 * copy_file_range() differs from regular file read and write in that it
1549 * specifically allows return partial success. When it does so is up to
1550 * the copy_file_range method.
1552 ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
1553 struct file *file_out, loff_t pos_out,
1554 size_t len, unsigned int flags)
1557 bool splice = flags & COPY_FILE_SPLICE;
1558 bool samesb = file_inode(file_in)->i_sb == file_inode(file_out)->i_sb;
1560 if (flags & ~COPY_FILE_SPLICE)
1563 ret = generic_copy_file_checks(file_in, pos_in, file_out, pos_out, &len,
1568 ret = rw_verify_area(READ, file_in, &pos_in, len);
1572 ret = rw_verify_area(WRITE, file_out, &pos_out, len);
1579 file_start_write(file_out);
1582 * Cloning is supported by more file systems, so we implement copy on
1583 * same sb using clone, but for filesystems where both clone and copy
1584 * are supported (e.g. nfs,cifs), we only call the copy method.
1586 if (!splice && file_out->f_op->copy_file_range) {
1587 ret = file_out->f_op->copy_file_range(file_in, pos_in,
1590 } else if (!splice && file_in->f_op->remap_file_range && samesb) {
1591 ret = file_in->f_op->remap_file_range(file_in, pos_in,
1593 min_t(loff_t, MAX_RW_COUNT, len),
1594 REMAP_FILE_CAN_SHORTEN);
1595 /* fallback to splice */
1598 } else if (samesb) {
1599 /* Fallback to splice for same sb copy for backward compat */
1603 file_end_write(file_out);
1609 * We can get here for same sb copy of filesystems that do not implement
1610 * ->copy_file_range() in case filesystem does not support clone or in
1611 * case filesystem supports clone but rejected the clone request (e.g.
1612 * because it was not block aligned).
1614 * In both cases, fall back to kernel copy so we are able to maintain a
1615 * consistent story about which filesystems support copy_file_range()
1616 * and which filesystems do not, that will allow userspace tools to
1617 * make consistent desicions w.r.t using copy_file_range().
1619 * We also get here if caller (e.g. nfsd) requested COPY_FILE_SPLICE
1620 * for server-side-copy between any two sb.
1622 * In any case, we call do_splice_direct() and not splice_file_range(),
1623 * without file_start_write() held, to avoid possible deadlocks related
1624 * to splicing from input file, while file_start_write() is held on
1625 * the output file on a different sb.
1627 ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out,
1628 min_t(size_t, len, MAX_RW_COUNT), 0);
1631 fsnotify_access(file_in);
1632 add_rchar(current, ret);
1633 fsnotify_modify(file_out);
1634 add_wchar(current, ret);
1642 EXPORT_SYMBOL(vfs_copy_file_range);
1644 SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
1645 int, fd_out, loff_t __user *, off_out,
1646 size_t, len, unsigned int, flags)
1650 ssize_t ret = -EBADF;
1652 CLASS(fd, f_in)(fd_in);
1656 CLASS(fd, f_out)(fd_out);
1657 if (fd_empty(f_out))
1661 if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
1664 pos_in = fd_file(f_in)->f_pos;
1668 if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
1671 pos_out = fd_file(f_out)->f_pos;
1677 ret = vfs_copy_file_range(fd_file(f_in), pos_in, fd_file(f_out), pos_out, len,
1684 if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
1687 fd_file(f_in)->f_pos = pos_in;
1691 if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
1694 fd_file(f_out)->f_pos = pos_out;
1701 * Don't operate on ranges the page cache doesn't support, and don't exceed the
1702 * LFS limits. If pos is under the limit it becomes a short access. If it
1703 * exceeds the limit we return -EFBIG.
1705 int generic_write_check_limits(struct file *file, loff_t pos, loff_t *count)
1707 struct inode *inode = file->f_mapping->host;
1708 loff_t max_size = inode->i_sb->s_maxbytes;
1709 loff_t limit = rlimit(RLIMIT_FSIZE);
1711 if (limit != RLIM_INFINITY) {
1713 send_sig(SIGXFSZ, current, 0);
1716 *count = min(*count, limit - pos);
1719 if (!(file->f_flags & O_LARGEFILE))
1720 max_size = MAX_NON_LFS;
1722 if (unlikely(pos >= max_size))
1725 *count = min(*count, max_size - pos);
1729 EXPORT_SYMBOL_GPL(generic_write_check_limits);
1731 /* Like generic_write_checks(), but takes size of write instead of iter. */
1732 int generic_write_checks_count(struct kiocb *iocb, loff_t *count)
1734 struct file *file = iocb->ki_filp;
1735 struct inode *inode = file->f_mapping->host;
1737 if (IS_SWAPFILE(inode))
1743 if (iocb->ki_flags & IOCB_APPEND)
1744 iocb->ki_pos = i_size_read(inode);
1746 if ((iocb->ki_flags & IOCB_NOWAIT) &&
1747 !((iocb->ki_flags & IOCB_DIRECT) ||
1748 (file->f_op->fop_flags & FOP_BUFFER_WASYNC)))
1751 return generic_write_check_limits(iocb->ki_filp, iocb->ki_pos, count);
1753 EXPORT_SYMBOL(generic_write_checks_count);
1756 * Performs necessary checks before doing a write
1758 * Can adjust writing position or amount of bytes to write.
1759 * Returns appropriate error code that caller should return or
1760 * zero in case that write should be allowed.
1762 ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
1764 loff_t count = iov_iter_count(from);
1767 ret = generic_write_checks_count(iocb, &count);
1771 iov_iter_truncate(from, count);
1772 return iov_iter_count(from);
1774 EXPORT_SYMBOL(generic_write_checks);
1777 * Performs common checks before doing a file copy/clone
1778 * from @file_in to @file_out.
1780 int generic_file_rw_checks(struct file *file_in, struct file *file_out)
1782 struct inode *inode_in = file_inode(file_in);
1783 struct inode *inode_out = file_inode(file_out);
1785 /* Don't copy dirs, pipes, sockets... */
1786 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1788 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1791 if (!(file_in->f_mode & FMODE_READ) ||
1792 !(file_out->f_mode & FMODE_WRITE) ||
1793 (file_out->f_flags & O_APPEND))
1799 int generic_atomic_write_valid(struct kiocb *iocb, struct iov_iter *iter)
1801 size_t len = iov_iter_count(iter);
1803 if (!iter_is_ubuf(iter))
1806 if (!is_power_of_2(len))
1809 if (!IS_ALIGNED(iocb->ki_pos, len))
1812 if (!(iocb->ki_flags & IOCB_DIRECT))
1817 EXPORT_SYMBOL_GPL(generic_atomic_write_valid);