Lines Matching full:file

12 #include <linux/file.h>
37 static inline bool unsigned_offsets(struct file *file) in unsigned_offsets() argument
39 return file->f_mode & FMODE_UNSIGNED_OFFSET; in unsigned_offsets()
43 * vfs_setpos - update the file offset for lseek
44 * @file: file structure in question
45 * @offset: file offset to seek to
46 * @maxsize: maximum file size
48 * This is a low-level filesystem helper for updating the file offset to
50 * not equal to the current file offset.
54 loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize) in vfs_setpos() argument
56 if (offset < 0 && !unsigned_offsets(file)) in vfs_setpos()
61 if (offset != file->f_pos) { in vfs_setpos()
62 file->f_pos = offset; in vfs_setpos()
63 file->f_version = 0; in vfs_setpos()
71 * @file: file structure to seek on
72 * @offset: file offset to seek to
74 * @maxsize: max size of this file in file system
78 * maximum file size and a custom EOF position, for e.g. hashed directories
86 generic_file_llseek_size(struct file *file, loff_t offset, int whence, in generic_file_llseek_size() argument
97 * f_pos value back to the file because a concurrent read(), in generic_file_llseek_size()
101 return file->f_pos; in generic_file_llseek_size()
107 spin_lock(&file->f_lock); in generic_file_llseek_size()
108 offset = vfs_setpos(file, file->f_pos + offset, maxsize); in generic_file_llseek_size()
109 spin_unlock(&file->f_lock); in generic_file_llseek_size()
113 * In the generic case the entire file is data, so as long as in generic_file_llseek_size()
114 * offset isn't at the end of the file then the offset is data. in generic_file_llseek_size()
121 * There is a virtual hole at the end of the file, so as long as in generic_file_llseek_size()
130 return vfs_setpos(file, offset, maxsize); in generic_file_llseek_size()
136 * @file: file structure to seek on
137 * @offset: file offset to seek to
141 * filesystems. It just updates the file offset to the value specified by
144 loff_t generic_file_llseek(struct file *file, loff_t offset, int whence) in generic_file_llseek() argument
146 struct inode *inode = file->f_mapping->host; in generic_file_llseek()
148 return generic_file_llseek_size(file, offset, whence, in generic_file_llseek()
156 * @file: file structure to seek on
157 * @offset: file offset to seek to
159 * @size: size of the file
162 loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size) in fixed_size_llseek() argument
166 return generic_file_llseek_size(file, offset, whence, in fixed_size_llseek()
176 * @file: file structure to seek on
177 * @offset: file offset to seek to
181 loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence) in no_seek_end_llseek() argument
185 return generic_file_llseek_size(file, offset, whence, in no_seek_end_llseek()
195 * @file: file structure to seek on
196 * @offset: file offset to seek to
201 loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size) in no_seek_end_llseek_size() argument
205 return generic_file_llseek_size(file, offset, whence, in no_seek_end_llseek_size()
215 * @file: file structure to seek on
216 * @offset: file offset to seek to
220 * userspace expects the seek to succeed but the (device) file is actually not
224 loff_t noop_llseek(struct file *file, loff_t offset, int whence) in noop_llseek() argument
226 return file->f_pos; in noop_llseek()
230 loff_t default_llseek(struct file *file, loff_t offset, int whence) in default_llseek() argument
232 struct inode *inode = file_inode(file); in default_llseek()
242 retval = file->f_pos; in default_llseek()
245 offset += file->f_pos; in default_llseek()
249 * In the generic case the entire file is data, so as in default_llseek()
250 * long as offset isn't at the end of the file then the in default_llseek()
260 * There is a virtual hole at the end of the file, so in default_llseek()
272 if (offset >= 0 || unsigned_offsets(file)) { in default_llseek()
273 if (offset != file->f_pos) { in default_llseek()
274 file->f_pos = offset; in default_llseek()
275 file->f_version = 0; in default_llseek()
285 loff_t vfs_llseek(struct file *file, loff_t offset, int whence) in vfs_llseek() argument
287 if (!(file->f_mode & FMODE_LSEEK)) in vfs_llseek()
289 return file->f_op->llseek(file, offset, whence); in vfs_llseek()
297 if (!f.file) in ksys_lseek()
302 loff_t res = vfs_llseek(f.file, offset, whence); in ksys_lseek()
333 if (!f.file) in SYSCALL_DEFINE5()
340 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low, in SYSCALL_DEFINE5()
355 int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count) in rw_verify_area() argument
364 if (!unsigned_offsets(file)) in rw_verify_area()
369 if (!unsigned_offsets(file)) in rw_verify_area()
374 return security_file_permission(file, in rw_verify_area()
379 static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos) in new_sync_read()
396 static int warn_unsupported(struct file *file, const char *op) in warn_unsupported() argument
399 "kernel %s not supported for file %pD4 (pid: %d comm: %.20s)\n", in warn_unsupported()
400 op, file, current->pid, current->comm); in warn_unsupported()
404 ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos) in __kernel_read() argument
414 if (WARN_ON_ONCE(!(file->f_mode & FMODE_READ))) in __kernel_read()
416 if (!(file->f_mode & FMODE_CAN_READ)) in __kernel_read()
422 if (unlikely(!file->f_op->read_iter || file->f_op->read)) in __kernel_read()
423 return warn_unsupported(file, "read"); in __kernel_read()
425 init_sync_kiocb(&kiocb, file); in __kernel_read()
428 ret = file->f_op->read_iter(&kiocb, &iter); in __kernel_read()
432 fsnotify_access(file); in __kernel_read()
439 ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos) in kernel_read() argument
443 ret = rw_verify_area(READ, file, pos, count); in kernel_read()
446 return __kernel_read(file, buf, count, pos); in kernel_read()
450 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos) in vfs_read() argument
454 if (!(file->f_mode & FMODE_READ)) in vfs_read()
456 if (!(file->f_mode & FMODE_CAN_READ)) in vfs_read()
461 ret = rw_verify_area(READ, file, pos, count); in vfs_read()
467 if (file->f_op->read) in vfs_read()
468 ret = file->f_op->read(file, buf, count, pos); in vfs_read()
469 else if (file->f_op->read_iter) in vfs_read()
470 ret = new_sync_read(file, buf, count, pos); in vfs_read()
474 fsnotify_access(file); in vfs_read()
481 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos) in new_sync_write()
499 ssize_t __kernel_write_iter(struct file *file, struct iov_iter *from, loff_t *pos) in __kernel_write_iter() argument
504 if (WARN_ON_ONCE(!(file->f_mode & FMODE_WRITE))) in __kernel_write_iter()
506 if (!(file->f_mode & FMODE_CAN_WRITE)) in __kernel_write_iter()
512 if (unlikely(!file->f_op->write_iter || file->f_op->write)) in __kernel_write_iter()
513 return warn_unsupported(file, "write"); in __kernel_write_iter()
515 init_sync_kiocb(&kiocb, file); in __kernel_write_iter()
517 ret = file->f_op->write_iter(&kiocb, from); in __kernel_write_iter()
521 fsnotify_modify(file); in __kernel_write_iter()
529 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos) in __kernel_write() argument
537 return __kernel_write_iter(file, &iter, pos); in __kernel_write()
548 ssize_t kernel_write(struct file *file, const void *buf, size_t count, in kernel_write() argument
553 ret = rw_verify_area(WRITE, file, pos, count); in kernel_write()
557 file_start_write(file); in kernel_write()
558 ret = __kernel_write(file, buf, count, pos); in kernel_write()
559 file_end_write(file); in kernel_write()
564 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) in vfs_write() argument
568 if (!(file->f_mode & FMODE_WRITE)) in vfs_write()
570 if (!(file->f_mode & FMODE_CAN_WRITE)) in vfs_write()
575 ret = rw_verify_area(WRITE, file, pos, count); in vfs_write()
580 file_start_write(file); in vfs_write()
581 if (file->f_op->write) in vfs_write()
582 ret = file->f_op->write(file, buf, count, pos); in vfs_write()
583 else if (file->f_op->write_iter) in vfs_write()
584 ret = new_sync_write(file, buf, count, pos); in vfs_write()
588 fsnotify_modify(file); in vfs_write()
592 file_end_write(file); in vfs_write()
596 /* file_ppos returns &file->f_pos or NULL if file is stream */
597 static inline loff_t *file_ppos(struct file *file) in file_ppos() argument
599 return file->f_mode & FMODE_STREAM ? NULL : &file->f_pos; in file_ppos()
607 if (f.file) { in ksys_read()
608 loff_t pos, *ppos = file_ppos(f.file); in ksys_read()
613 ret = vfs_read(f.file, buf, count, ppos); in ksys_read()
615 f.file->f_pos = pos; in ksys_read()
631 if (f.file) { in ksys_write()
632 loff_t pos, *ppos = file_ppos(f.file); in ksys_write()
637 ret = vfs_write(f.file, buf, count, ppos); in ksys_write()
639 f.file->f_pos = pos; in ksys_write()
662 if (f.file) { in ksys_pread64()
664 if (f.file->f_mode & FMODE_PREAD) in ksys_pread64()
665 ret = vfs_read(f.file, buf, count, &pos); in ksys_pread64()
696 if (f.file) { in ksys_pwrite64()
698 if (f.file->f_mode & FMODE_PWRITE) in ksys_pwrite64()
699 ret = vfs_write(f.file, buf, count, &pos); in ksys_pwrite64()
720 static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter, in do_iter_readv_writev()
742 /* Do it by hand, with file-ops */
743 static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter, in do_loop_readv_writev()
776 static ssize_t do_iter_read(struct file *file, struct iov_iter *iter, in do_iter_read() argument
782 if (!(file->f_mode & FMODE_READ)) in do_iter_read()
784 if (!(file->f_mode & FMODE_CAN_READ)) in do_iter_read()
790 ret = rw_verify_area(READ, file, pos, tot_len); in do_iter_read()
794 if (file->f_op->read_iter) in do_iter_read()
795 ret = do_iter_readv_writev(file, iter, pos, READ, flags); in do_iter_read()
797 ret = do_loop_readv_writev(file, iter, pos, READ, flags); in do_iter_read()
800 fsnotify_access(file); in do_iter_read()
804 ssize_t vfs_iocb_iter_read(struct file *file, struct kiocb *iocb, in vfs_iocb_iter_read() argument
810 if (!file->f_op->read_iter) in vfs_iocb_iter_read()
812 if (!(file->f_mode & FMODE_READ)) in vfs_iocb_iter_read()
814 if (!(file->f_mode & FMODE_CAN_READ)) in vfs_iocb_iter_read()
820 ret = rw_verify_area(READ, file, &iocb->ki_pos, tot_len); in vfs_iocb_iter_read()
824 ret = call_read_iter(file, iocb, iter); in vfs_iocb_iter_read()
827 fsnotify_access(file); in vfs_iocb_iter_read()
832 ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos, in vfs_iter_read() argument
835 if (!file->f_op->read_iter) in vfs_iter_read()
837 return do_iter_read(file, iter, ppos, flags); in vfs_iter_read()
841 static ssize_t do_iter_write(struct file *file, struct iov_iter *iter, in do_iter_write() argument
847 if (!(file->f_mode & FMODE_WRITE)) in do_iter_write()
849 if (!(file->f_mode & FMODE_CAN_WRITE)) in do_iter_write()
855 ret = rw_verify_area(WRITE, file, pos, tot_len); in do_iter_write()
859 if (file->f_op->write_iter) in do_iter_write()
860 ret = do_iter_readv_writev(file, iter, pos, WRITE, flags); in do_iter_write()
862 ret = do_loop_readv_writev(file, iter, pos, WRITE, flags); in do_iter_write()
864 fsnotify_modify(file); in do_iter_write()
868 ssize_t vfs_iocb_iter_write(struct file *file, struct kiocb *iocb, in vfs_iocb_iter_write() argument
874 if (!file->f_op->write_iter) in vfs_iocb_iter_write()
876 if (!(file->f_mode & FMODE_WRITE)) in vfs_iocb_iter_write()
878 if (!(file->f_mode & FMODE_CAN_WRITE)) in vfs_iocb_iter_write()
884 ret = rw_verify_area(WRITE, file, &iocb->ki_pos, tot_len); in vfs_iocb_iter_write()
888 ret = call_write_iter(file, iocb, iter); in vfs_iocb_iter_write()
890 fsnotify_modify(file); in vfs_iocb_iter_write()
896 ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos, in vfs_iter_write() argument
899 if (!file->f_op->write_iter) in vfs_iter_write()
901 return do_iter_write(file, iter, ppos, flags); in vfs_iter_write()
905 static ssize_t vfs_readv(struct file *file, const struct iovec __user *vec, in vfs_readv() argument
915 ret = do_iter_read(file, &iter, pos, flags); in vfs_readv()
922 static ssize_t vfs_writev(struct file *file, const struct iovec __user *vec, in vfs_writev() argument
932 file_start_write(file); in vfs_writev()
933 ret = do_iter_write(file, &iter, pos, flags); in vfs_writev()
934 file_end_write(file); in vfs_writev()
946 if (f.file) { in do_readv()
947 loff_t pos, *ppos = file_ppos(f.file); in do_readv()
952 ret = vfs_readv(f.file, vec, vlen, ppos, flags); in do_readv()
954 f.file->f_pos = pos; in do_readv()
970 if (f.file) { in do_writev()
971 loff_t pos, *ppos = file_ppos(f.file); in do_writev()
976 ret = vfs_writev(f.file, vec, vlen, ppos, flags); in do_writev()
978 f.file->f_pos = pos; in do_writev()
1004 if (f.file) { in do_preadv()
1006 if (f.file->f_mode & FMODE_PREAD) in do_preadv()
1007 ret = vfs_readv(f.file, vec, vlen, &pos, flags); in do_preadv()
1027 if (f.file) { in do_pwritev()
1029 if (f.file->f_mode & FMODE_PWRITE) in do_pwritev()
1030 ret = vfs_writev(f.file, vec, vlen, &pos, flags); in do_pwritev()
1192 * Get input file, and verify that it is ok.. in do_sendfile()
1196 if (!in.file) in do_sendfile()
1198 if (!(in.file->f_mode & FMODE_READ)) in do_sendfile()
1202 pos = in.file->f_pos; in do_sendfile()
1205 if (!(in.file->f_mode & FMODE_PREAD)) in do_sendfile()
1208 retval = rw_verify_area(READ, in.file, &pos, count); in do_sendfile()
1215 * Get output file, and verify that it is ok.. in do_sendfile()
1219 if (!out.file) in do_sendfile()
1221 if (!(out.file->f_mode & FMODE_WRITE)) in do_sendfile()
1223 in_inode = file_inode(in.file); in do_sendfile()
1224 out_inode = file_inode(out.file); in do_sendfile()
1225 out_pos = out.file->f_pos; in do_sendfile()
1243 * EAGAIN on a non-blocking file descriptor. in do_sendfile()
1245 if (in.file->f_flags & O_NONBLOCK) in do_sendfile()
1248 opipe = get_pipe_info(out.file, true); in do_sendfile()
1250 retval = rw_verify_area(WRITE, out.file, &out_pos, count); in do_sendfile()
1253 file_start_write(out.file); in do_sendfile()
1254 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, in do_sendfile()
1256 file_end_write(out.file); in do_sendfile()
1258 if (out.file->f_flags & O_NONBLOCK) in do_sendfile()
1261 retval = splice_file_to_pipe(in.file, opipe, &pos, count, fl); in do_sendfile()
1267 fsnotify_access(in.file); in do_sendfile()
1268 fsnotify_modify(out.file); in do_sendfile()
1269 out.file->f_pos = out_pos; in do_sendfile()
1273 in.file->f_pos = pos; in do_sendfile()
1367 * @file_in: file structure to read from
1368 * @pos_in: file offset to read from
1369 * @file_out: file structure to write data to
1370 * @pos_out: file offset to write data to
1374 * This is a generic filesystem helper to copy data from one file to another.
1375 * It has no constraints on the source or destination file owners - the files
1386 ssize_t generic_copy_file_range(struct file *file_in, loff_t pos_in, in generic_copy_file_range()
1387 struct file *file_out, loff_t pos_out, in generic_copy_file_range()
1398 * Performs necessary checks before doing a file copy
1404 static int generic_copy_file_checks(struct file *file_in, loff_t pos_in, in generic_copy_file_checks()
1405 struct file *file_out, loff_t pos_out, in generic_copy_file_checks()
1420 * a file of the wrong filesystem type to filesystem driver can result in generic_copy_file_checks()
1460 /* Don't allow overlapped copying within the same file. */ in generic_copy_file_checks()
1471 * copy_file_range() differs from regular file read and write in that it
1475 ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in, in vfs_copy_file_range()
1476 struct file *file_out, loff_t pos_out, in vfs_copy_file_range()
1504 * Cloning is supported by more file systems, so we implement copy on in vfs_copy_file_range()
1569 if (!f_in.file) in SYSCALL_DEFINE6()
1573 if (!f_out.file) in SYSCALL_DEFINE6()
1581 pos_in = f_in.file->f_pos; in SYSCALL_DEFINE6()
1588 pos_out = f_out.file->f_pos; in SYSCALL_DEFINE6()
1595 ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len, in SYSCALL_DEFINE6()
1605 f_in.file->f_pos = pos_in; in SYSCALL_DEFINE6()
1612 f_out.file->f_pos = pos_out; in SYSCALL_DEFINE6()
1629 int generic_write_check_limits(struct file *file, loff_t pos, loff_t *count) in generic_write_check_limits() argument
1631 struct inode *inode = file->f_mapping->host; in generic_write_check_limits()
1643 if (!(file->f_flags & O_LARGEFILE)) in generic_write_check_limits()
1657 struct file *file = iocb->ki_filp; in generic_write_checks_count() local
1658 struct inode *inode = file->f_mapping->host; in generic_write_checks_count()
1671 (file->f_mode & FMODE_BUF_WASYNC))) in generic_write_checks_count()
1700 * Performs common checks before doing a file copy/clone
1703 int generic_file_rw_checks(struct file *file_in, struct file *file_out) in generic_file_rw_checks()