Lines Matching full:file

3  *  linux/fs/file.c
17 #include <linux/file.h>
75 if (WARN_ONCE(cnt >= FILE_REF_RELEASED, "imbalanced put on file reference count")) { in __file_ref_put()
116 * space if any. This does not copy the file pointers. Called with the files
133 * Copy all file descriptors from the old table to the new, expanded table and
142 cpy = ofdt->max_fds * sizeof(struct file *); in copy_fdtable()
143 set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *); in copy_fdtable()
199 data = kvmalloc_array(nr, sizeof(struct file *), GFP_KERNEL_ACCOUNT); in alloc_fdtable()
226 * Expand the file descriptor table.
263 * This function will expand the file structures, if the requested size exceeds
366 struct file **old_fds, **new_fds; in dup_fd()
427 * the file can show up as we are walking the array below. in dup_fd()
433 * ref the file if we see it and mark the fd slot as unused otherwise. in dup_fd()
436 struct file *f = rcu_dereference_raw(*old_fds++); in dup_fd()
447 memset(new_fds, 0, (new_fdt->max_fds - open_files) * sizeof(struct file *)); in dup_fd()
472 struct file *file = fdt->fd[i]; in close_files() local
473 if (file) { in close_files()
474 filp_close(file, files); in close_files()
548 * allocate a file descriptor, mark it busy.
624 * Install a file pointer in the fd array.
627 * setting the open_fds bitmap and installing the file in the file
629 * installing a file in the array before us. We need to detect this and
630 * fput() the struct file we are about to overwrite in this case.
635 * This consumes the "file" refcount, so callers should treat it
636 * as if they had called fput(file).
639 void fd_install(unsigned int fd, struct file *file) in fd_install() argument
644 if (WARN_ON_ONCE(unlikely(file->f_mode & FMODE_BACKING))) in fd_install()
654 rcu_assign_pointer(fdt->fd[fd], file); in fd_install()
662 rcu_assign_pointer(fdt->fd[fd], file); in fd_install()
669 * file_close_fd_locked - return file associated with fd
670 * @files: file struct to retrieve file from
671 * @fd: file descriptor to retrieve file for
677 * Returns: The file associated with @fd (NULL if @fd is not open)
679 struct file *file_close_fd_locked(struct files_struct *files, unsigned fd) in file_close_fd_locked()
682 struct file *file; in file_close_fd_locked() local
690 file = rcu_dereference_raw(fdt->fd[fd]); in file_close_fd_locked()
691 if (file) { in file_close_fd_locked()
695 return file; in file_close_fd_locked()
701 struct file *file; in close_fd() local
704 file = file_close_fd_locked(files, fd); in close_fd()
706 if (!file) in close_fd()
709 return filp_close(file, files); in close_fd()
715 * @fdt: File descriptor table.
743 struct file *file; in __range_close() local
751 file = file_close_fd_locked(files, fd); in __range_close()
752 if (file) { in __range_close()
754 filp_close(file, files); in __range_close()
767 * sys_close_range() - Close all file descriptors in a given range.
769 * @fd: starting file descriptor to close
770 * @max_fd: last file descriptor to close
773 * This closes a range of file descriptors. All file descriptors
775 * Currently, errors to close a given file descriptor are ignored.
794 * copy all of the file descriptors since they still want to in SYSCALL_DEFINE3()
804 * We used to share our file descriptor table, and have now in SYSCALL_DEFINE3()
818 * the new file descriptor table and drop the old one. in SYSCALL_DEFINE3()
830 * file_close_fd - return file associated with fd
831 * @fd: file descriptor to retrieve file for
835 * Returns: The file associated with @fd (NULL if @fd is not open)
837 struct file *file_close_fd(unsigned int fd) in file_close_fd()
840 struct file *file; in file_close_fd() local
843 file = file_close_fd_locked(files, fd); in file_close_fd()
846 return file; in file_close_fd()
867 struct file *file; in do_close_on_exec() local
870 file = fdt->fd[fd]; in do_close_on_exec()
871 if (!file) in do_close_on_exec()
876 filp_close(file, files); in do_close_on_exec()
885 static struct file *__get_file_rcu(struct file __rcu **f) in __get_file_rcu()
887 struct file __rcu *file; in __get_file_rcu() local
888 struct file __rcu *file_reloaded; in __get_file_rcu()
889 struct file __rcu *file_reloaded_cmp; in __get_file_rcu()
891 file = rcu_dereference_raw(*f); in __get_file_rcu()
892 if (!file) in __get_file_rcu()
895 if (unlikely(!file_ref_get(&file->f_ref))) in __get_file_rcu()
913 * __rcu protected file pointer so that if that pointer still in __get_file_rcu()
914 * matches the current file, we know we have successfully in __get_file_rcu()
915 * acquired a reference to the right file. in __get_file_rcu()
917 * If the pointers don't match the file has been reallocated by in __get_file_rcu()
920 if (file == file_reloaded_cmp) in __get_file_rcu()
923 fput(file); in __get_file_rcu()
928 * get_file_rcu - try go get a reference to a file under rcu
929 * @f: the file to get a reference on
939 struct file *get_file_rcu(struct file __rcu **f) in get_file_rcu()
942 struct file __rcu *file; in get_file_rcu() local
944 file = __get_file_rcu(f); in get_file_rcu()
945 if (!IS_ERR(file)) in get_file_rcu()
946 return file; in get_file_rcu()
952 * get_file_active - try go get a reference to a file
953 * @f: the file to get a reference on
963 struct file *get_file_active(struct file **f) in get_file_active()
965 struct file __rcu *file; in get_file_active() local
968 file = __get_file_rcu(f); in get_file_active()
970 if (IS_ERR(file)) in get_file_active()
971 file = NULL; in get_file_active()
972 return file; in get_file_active()
976 static inline struct file *__fget_files_rcu(struct files_struct *files, in __fget_files_rcu()
980 struct file *file; in __fget_files_rcu() local
982 struct file __rcu **fdentry; in __fget_files_rcu()
996 file = rcu_dereference_raw(*fdentry); in __fget_files_rcu()
997 file = (void *)(nospec_mask & (unsigned long)file); in __fget_files_rcu()
998 if (unlikely(!file)) in __fget_files_rcu()
1002 * Ok, we have a file pointer that was valid at in __fget_files_rcu()
1012 if (unlikely(!file_ref_get(&file->f_ref))) in __fget_files_rcu()
1018 * (a) the file ref already went down to zero and the in __fget_files_rcu()
1019 * file hasn't been reused yet or the file count in __fget_files_rcu()
1020 * isn't zero but the file has already been reused. in __fget_files_rcu()
1022 * (b) the file table entry has changed under us. in __fget_files_rcu()
1029 if (unlikely(file != rcu_dereference_raw(*fdentry)) || in __fget_files_rcu()
1031 fput(file); in __fget_files_rcu()
1036 * This isn't the file we're looking for or we're not in __fget_files_rcu()
1039 if (unlikely(file->f_mode & mask)) { in __fget_files_rcu()
1040 fput(file); in __fget_files_rcu()
1045 * Ok, we have a ref to the file, and checked that it in __fget_files_rcu()
1048 return file; in __fget_files_rcu()
1052 static struct file *__fget_files(struct files_struct *files, unsigned int fd, in __fget_files()
1055 struct file *file; in __fget_files() local
1058 file = __fget_files_rcu(files, fd, mask); in __fget_files()
1061 return file; in __fget_files()
1064 static inline struct file *__fget(unsigned int fd, fmode_t mask) in __fget()
1069 struct file *fget(unsigned int fd) in fget()
1075 struct file *fget_raw(unsigned int fd) in fget_raw()
1081 struct file *fget_task(struct task_struct *task, unsigned int fd) in fget_task()
1083 struct file *file = NULL; in fget_task() local
1087 file = __fget_files(task->files, fd, 0); in fget_task()
1090 return file; in fget_task()
1093 struct file *fget_task_next(struct task_struct *task, unsigned int *ret_fd) in fget_task_next()
1098 struct file *file = NULL; in fget_task_next() local
1105 file = __fget_files_rcu(files, fd, 0); in fget_task_next()
1106 if (file) in fget_task_next()
1113 return file; in fget_task_next()
1118 * Lightweight file lookup - no refcnt increment if fd table isn't shared.
1123 * to userspace (i.e. you cannot remember the returned struct file * after
1125 * 2) You must not call filp_close on the returned struct file * in between
1138 * See also the documentation in rust/kernel/file.rs.
1143 struct file *file; in __fget_light() local
1149 * return a file that is concurrently being freed. in __fget_light()
1155 file = files_lookup_fd_raw(files, fd); in __fget_light()
1156 if (!file || unlikely(file->f_mode & mask)) in __fget_light()
1158 return BORROWED_FD(file); in __fget_light()
1160 file = __fget_files(files, fd, mask); in __fget_light()
1161 if (!file) in __fget_light()
1163 return CLONED_FD(file); in __fget_light()
1179 * file is marked for FMODE_ATOMIC_POS, and it can be
1183 * can make a file accessible even if it otherwise would
1187 static inline bool file_needs_f_pos_lock(struct file *file) in file_needs_f_pos_lock() argument
1189 return (file->f_mode & FMODE_ATOMIC_POS) && in file_needs_f_pos_lock()
1190 (file_count(file) > 1 || file->f_op->iterate_shared); in file_needs_f_pos_lock()
1196 struct file *file = fd_file(f); in fdget_pos() local
1198 if (file && file_needs_f_pos_lock(file)) { in fdget_pos()
1200 mutex_lock(&file->f_pos_lock); in fdget_pos()
1205 void __f_unlock_pos(struct file *f) in __f_unlock_pos()
1211 * We only lock f_pos if we have threads or if the file might be
1213 * file count (done either by fdget() or by fork()).
1234 struct file *file, unsigned fd, unsigned flags) in do_dup2() argument
1237 struct file *tofree; in do_dup2()
1251 get_file(file); in do_dup2()
1252 rcu_assign_pointer(fdt->fd[fd], file); in do_dup2()
1266 int replace_fd(unsigned fd, struct file *file, unsigned flags) in replace_fd() argument
1271 if (!file) in replace_fd()
1281 return do_dup2(files, file, fd, flags); in replace_fd()
1289 * receive_fd() - Install received file into file descriptor table
1290 * @file: struct file that was received from another process
1294 * Installs a received file into the file descriptor table, with appropriate
1299 * struct file.
1303 int receive_fd(struct file *file, int __user *ufd, unsigned int o_flags) in receive_fd() argument
1308 error = security_file_receive(file); in receive_fd()
1324 fd_install(new_fd, get_file(file)); in receive_fd()
1325 __receive_sock(file); in receive_fd()
1330 int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags) in receive_fd_replace() argument
1334 error = security_file_receive(file); in receive_fd_replace()
1337 error = replace_fd(new_fd, file, o_flags); in receive_fd_replace()
1340 __receive_sock(file); in receive_fd_replace()
1347 struct file *file; in ksys_dup3() local
1361 file = files_lookup_fd_locked(files, oldfd); in ksys_dup3()
1362 if (unlikely(!file)) in ksys_dup3()
1369 return do_dup2(files, file, newfd, flags); in ksys_dup3()
1387 struct file *f; in SYSCALL_DEFINE2()
1405 struct file *file = fget_raw(fildes); in SYSCALL_DEFINE1() local
1407 if (file) { in SYSCALL_DEFINE1()
1410 fd_install(ret, file); in SYSCALL_DEFINE1()
1412 fput(file); in SYSCALL_DEFINE1()
1417 int f_dupfd(unsigned int from, struct file *file, unsigned flags) in f_dupfd() argument
1425 get_file(file); in f_dupfd()
1426 fd_install(err, file); in f_dupfd()
1432 int (*f)(const void *, struct file *, unsigned), in iterate_fd() argument
1441 struct file *file; in iterate_fd() local
1442 file = rcu_dereference_check_fdtable(files, fdt->fd[n]); in iterate_fd()
1443 if (!file) in iterate_fd()
1445 res = f(p, file, n); in iterate_fd()