Lines Matching full:file

3  *  linux/fs/file.c
17 #include <linux/file.h>
52 * space if any. This does not copy the file pointers. Called with the files
69 * Copy all file descriptors from the old table to the new, expanded table and
78 cpy = ofdt->max_fds * sizeof(struct file *); in copy_fdtable()
79 set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *); in copy_fdtable()
113 nr /= (1024 / sizeof(struct file *)); in alloc_fdtable()
115 nr *= (1024 / sizeof(struct file *)); in alloc_fdtable()
132 data = kvmalloc_array(nr, sizeof(struct file *), GFP_KERNEL_ACCOUNT); in alloc_fdtable()
159 * Expand the file descriptor table.
204 * This function will expand the file structures, if the requested size exceeds
306 struct file **old_fds, **new_fds; in dup_fd()
370 struct file *f = *old_fds++; in dup_fd()
387 memset(new_fds, 0, (new_fdt->max_fds - open_files) * sizeof(struct file *)); in dup_fd()
416 struct file * file = xchg(&fdt->fd[i], NULL); in close_files() local
417 if (file) { in close_files()
418 filp_close(file, files); in close_files()
483 * allocate a file descriptor, mark it busy.
573 * Install a file pointer in the fd array.
576 * setting the open_fds bitmap and installing the file in the file
578 * installing a file in the array before us. We need to detect this and
579 * fput() the struct file we are about to overwrite in this case.
584 * This consumes the "file" refcount, so callers should treat it
585 * as if they had called fput(file).
588 void fd_install(unsigned int fd, struct file *file) in fd_install() argument
593 if (WARN_ON_ONCE(unlikely(file->f_mode & FMODE_BACKING))) in fd_install()
603 rcu_assign_pointer(fdt->fd[fd], file); in fd_install()
611 rcu_assign_pointer(fdt->fd[fd], file); in fd_install()
618 * file_close_fd_locked - return file associated with fd
619 * @files: file struct to retrieve file from
620 * @fd: file descriptor to retrieve file for
626 * Returns: The file associated with @fd (NULL if @fd is not open)
628 struct file *file_close_fd_locked(struct files_struct *files, unsigned fd) in file_close_fd_locked()
631 struct file *file; in file_close_fd_locked() local
639 file = fdt->fd[fd]; in file_close_fd_locked()
640 if (file) { in file_close_fd_locked()
644 return file; in file_close_fd_locked()
650 struct file *file; in close_fd() local
653 file = file_close_fd_locked(files, fd); in close_fd()
655 if (!file) in close_fd()
658 return filp_close(file, files); in close_fd()
664 * @fdt: File descriptor table.
692 struct file *file; in __range_close() local
700 file = file_close_fd_locked(files, fd); in __range_close()
701 if (file) { in __range_close()
703 filp_close(file, files); in __range_close()
716 * __close_range() - Close all file descriptors in a given range.
718 * @fd: starting file descriptor to close
719 * @max_fd: last file descriptor to close
722 * This closes a range of file descriptors. All file descriptors
741 * copy all of the file descriptors since they still want to in __close_range()
751 * We used to share our file descriptor table, and have now in __close_range()
765 * the new file descriptor table and drop the old one. in __close_range()
777 * file_close_fd - return file associated with fd
778 * @fd: file descriptor to retrieve file for
782 * Returns: The file associated with @fd (NULL if @fd is not open)
784 struct file *file_close_fd(unsigned int fd) in file_close_fd()
787 struct file *file; in file_close_fd() local
790 file = file_close_fd_locked(files, fd); in file_close_fd()
793 return file; in file_close_fd()
814 struct file *file; in do_close_on_exec() local
817 file = fdt->fd[fd]; in do_close_on_exec()
818 if (!file) in do_close_on_exec()
823 filp_close(file, files); in do_close_on_exec()
832 static struct file *__get_file_rcu(struct file __rcu **f) in __get_file_rcu()
834 struct file __rcu *file; in __get_file_rcu() local
835 struct file __rcu *file_reloaded; in __get_file_rcu()
836 struct file __rcu *file_reloaded_cmp; in __get_file_rcu()
838 file = rcu_dereference_raw(*f); in __get_file_rcu()
839 if (!file) in __get_file_rcu()
842 if (unlikely(!atomic_long_inc_not_zero(&file->f_count))) in __get_file_rcu()
860 * __rcu protected file pointer so that if that pointer still in __get_file_rcu()
861 * matches the current file, we know we have successfully in __get_file_rcu()
862 * acquired a reference to the right file. in __get_file_rcu()
864 * If the pointers don't match the file has been reallocated by in __get_file_rcu()
867 if (file == file_reloaded_cmp) in __get_file_rcu()
870 fput(file); in __get_file_rcu()
875 * get_file_rcu - try go get a reference to a file under rcu
876 * @f: the file to get a reference on
886 struct file *get_file_rcu(struct file __rcu **f) in get_file_rcu()
889 struct file __rcu *file; in get_file_rcu() local
891 file = __get_file_rcu(f); in get_file_rcu()
892 if (!IS_ERR(file)) in get_file_rcu()
893 return file; in get_file_rcu()
899 * get_file_active - try go get a reference to a file
900 * @f: the file to get a reference on
910 struct file *get_file_active(struct file **f) in get_file_active()
912 struct file __rcu *file; in get_file_active() local
915 file = __get_file_rcu(f); in get_file_active()
917 if (IS_ERR(file)) in get_file_active()
918 file = NULL; in get_file_active()
919 return file; in get_file_active()
923 static inline struct file *__fget_files_rcu(struct files_struct *files, in __fget_files_rcu()
927 struct file *file; in __fget_files_rcu() local
929 struct file __rcu **fdentry; in __fget_files_rcu()
943 file = rcu_dereference_raw(*fdentry); in __fget_files_rcu()
944 file = (void *)(nospec_mask & (unsigned long)file); in __fget_files_rcu()
945 if (unlikely(!file)) in __fget_files_rcu()
949 * Ok, we have a file pointer that was valid at in __fget_files_rcu()
959 if (unlikely(!atomic_long_inc_not_zero(&file->f_count))) in __fget_files_rcu()
965 * (a) the file ref already went down to zero and the in __fget_files_rcu()
966 * file hasn't been reused yet or the file count in __fget_files_rcu()
967 * isn't zero but the file has already been reused. in __fget_files_rcu()
969 * (b) the file table entry has changed under us. in __fget_files_rcu()
976 if (unlikely(file != rcu_dereference_raw(*fdentry)) || in __fget_files_rcu()
978 fput(file); in __fget_files_rcu()
983 * This isn't the file we're looking for or we're not in __fget_files_rcu()
986 if (unlikely(file->f_mode & mask)) { in __fget_files_rcu()
987 fput(file); in __fget_files_rcu()
992 * Ok, we have a ref to the file, and checked that it in __fget_files_rcu()
995 return file; in __fget_files_rcu()
999 static struct file *__fget_files(struct files_struct *files, unsigned int fd, in __fget_files()
1002 struct file *file; in __fget_files() local
1005 file = __fget_files_rcu(files, fd, mask); in __fget_files()
1008 return file; in __fget_files()
1011 static inline struct file *__fget(unsigned int fd, fmode_t mask) in __fget()
1016 struct file *fget(unsigned int fd) in fget()
1022 struct file *fget_raw(unsigned int fd) in fget_raw()
1028 struct file *fget_task(struct task_struct *task, unsigned int fd) in fget_task()
1030 struct file *file = NULL; in fget_task() local
1034 file = __fget_files(task->files, fd, 0); in fget_task()
1037 return file; in fget_task()
1040 struct file *lookup_fdget_rcu(unsigned int fd) in lookup_fdget_rcu()
1047 struct file *task_lookup_fdget_rcu(struct task_struct *task, unsigned int fd) in task_lookup_fdget_rcu()
1051 struct file *file = NULL; in task_lookup_fdget_rcu() local
1056 file = __fget_files_rcu(files, fd, 0); in task_lookup_fdget_rcu()
1059 return file; in task_lookup_fdget_rcu()
1062 struct file *task_lookup_next_fdget_rcu(struct task_struct *task, unsigned int *ret_fd) in task_lookup_next_fdget_rcu()
1067 struct file *file = NULL; in task_lookup_next_fdget_rcu() local
1073 file = __fget_files_rcu(files, fd, 0); in task_lookup_next_fdget_rcu()
1074 if (file) in task_lookup_next_fdget_rcu()
1080 return file; in task_lookup_next_fdget_rcu()
1085 * Lightweight file lookup - no refcnt increment if fd table isn't shared.
1090 * to userspace (i.e. you cannot remember the returned struct file * after
1092 * 2) You must not call filp_close on the returned struct file * in between
1103 struct file *file; in __fget_light() local
1109 * return a file that is concurrently being freed. in __fget_light()
1115 file = files_lookup_fd_raw(files, fd); in __fget_light()
1116 if (!file || unlikely(file->f_mode & mask)) in __fget_light()
1118 return BORROWED_FD(file); in __fget_light()
1120 file = __fget_files(files, fd, mask); in __fget_light()
1121 if (!file) in __fget_light()
1123 return CLONED_FD(file); in __fget_light()
1139 * file is marked for FMODE_ATOMIC_POS, and it can be
1143 * can make a file accessible even if it otherwise would
1147 static inline bool file_needs_f_pos_lock(struct file *file) in file_needs_f_pos_lock() argument
1149 return (file->f_mode & FMODE_ATOMIC_POS) && in file_needs_f_pos_lock()
1150 (file_count(file) > 1 || file->f_op->iterate_shared); in file_needs_f_pos_lock()
1156 struct file *file = fd_file(f); in fdget_pos() local
1158 if (file && file_needs_f_pos_lock(file)) { in fdget_pos()
1160 mutex_lock(&file->f_pos_lock); in fdget_pos()
1165 void __f_unlock_pos(struct file *f) in __f_unlock_pos()
1171 * We only lock f_pos if we have threads or if the file might be
1173 * file count (done either by fdget() or by fork()).
1199 struct file *file, unsigned fd, unsigned flags) in do_dup2() argument
1202 struct file *tofree; in do_dup2()
1209 * file immediately after grabbing descriptor, mark it larval if in do_dup2()
1224 get_file(file); in do_dup2()
1225 rcu_assign_pointer(fdt->fd[fd], file); in do_dup2()
1243 int replace_fd(unsigned fd, struct file *file, unsigned flags) in replace_fd() argument
1248 if (!file) in replace_fd()
1258 return do_dup2(files, file, fd, flags); in replace_fd()
1266 * receive_fd() - Install received file into file descriptor table
1267 * @file: struct file that was received from another process
1271 * Installs a received file into the file descriptor table, with appropriate
1276 * struct file.
1280 int receive_fd(struct file *file, int __user *ufd, unsigned int o_flags) in receive_fd() argument
1285 error = security_file_receive(file); in receive_fd()
1301 fd_install(new_fd, get_file(file)); in receive_fd()
1302 __receive_sock(file); in receive_fd()
1307 int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags) in receive_fd_replace() argument
1311 error = security_file_receive(file); in receive_fd_replace()
1314 error = replace_fd(new_fd, file, o_flags); in receive_fd_replace()
1317 __receive_sock(file); in receive_fd_replace()
1324 struct file *file; in ksys_dup3() local
1338 file = files_lookup_fd_locked(files, oldfd); in ksys_dup3()
1339 if (unlikely(!file)) in ksys_dup3()
1346 return do_dup2(files, file, newfd, flags); in ksys_dup3()
1364 struct file *f; in SYSCALL_DEFINE2()
1382 struct file *file = fget_raw(fildes); in SYSCALL_DEFINE1() local
1384 if (file) { in SYSCALL_DEFINE1()
1387 fd_install(ret, file); in SYSCALL_DEFINE1()
1389 fput(file); in SYSCALL_DEFINE1()
1394 int f_dupfd(unsigned int from, struct file *file, unsigned flags) in f_dupfd() argument
1402 get_file(file); in f_dupfd()
1403 fd_install(err, file); in f_dupfd()
1409 int (*f)(const void *, struct file *, unsigned), in iterate_fd() argument
1418 struct file *file; in iterate_fd() local
1419 file = rcu_dereference_check_fdtable(files, fdt->fd[n]); in iterate_fd()
1420 if (!file) in iterate_fd()
1422 res = f(p, file, n); in iterate_fd()