Lines Matching full:file

3  *  linux/fs/file.c
17 #include <linux/file.h>
51 * space if any. This does not copy the file pointers. Called with the files
73 * Copy all file descriptors from the old table to the new, expanded table and
82 cpy = ofdt->max_fds * sizeof(struct file *); in copy_fdtable()
83 set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *); in copy_fdtable()
117 nr /= (1024 / sizeof(struct file *)); in alloc_fdtable()
119 nr *= (1024 / sizeof(struct file *)); in alloc_fdtable()
136 data = kvmalloc_array(nr, sizeof(struct file *), GFP_KERNEL_ACCOUNT); in alloc_fdtable()
163 * Expand the file descriptor table.
208 * This function will expand the file structures, if the requested size exceeds
319 struct file **old_fds, **new_fds; in dup_fd()
383 struct file *f = *old_fds++; in dup_fd()
400 memset(new_fds, 0, (new_fdt->max_fds - open_files) * sizeof(struct file *)); in dup_fd()
430 struct file * file = xchg(&fdt->fd[i], NULL); in close_files() local
431 if (file) { in close_files()
432 filp_close(file, files); in close_files()
497 * allocate a file descriptor, mark it busy.
587 * Install a file pointer in the fd array.
590 * setting the open_fds bitmap and installing the file in the file
592 * installing a file in the array before us. We need to detect this and
593 * fput() the struct file we are about to overwrite in this case.
598 * This consumes the "file" refcount, so callers should treat it
599 * as if they had called fput(file).
602 void fd_install(unsigned int fd, struct file *file) in fd_install() argument
607 if (WARN_ON_ONCE(unlikely(file->f_mode & FMODE_BACKING))) in fd_install()
617 rcu_assign_pointer(fdt->fd[fd], file); in fd_install()
625 rcu_assign_pointer(fdt->fd[fd], file); in fd_install()
632 * file_close_fd_locked - return file associated with fd
633 * @files: file struct to retrieve file from
634 * @fd: file descriptor to retrieve file for
640 * Returns: The file associated with @fd (NULL if @fd is not open)
642 struct file *file_close_fd_locked(struct files_struct *files, unsigned fd) in file_close_fd_locked()
645 struct file *file; in file_close_fd_locked() local
653 file = fdt->fd[fd]; in file_close_fd_locked()
654 if (file) { in file_close_fd_locked()
658 return file; in file_close_fd_locked()
664 struct file *file; in close_fd() local
667 file = file_close_fd_locked(files, fd); in close_fd()
669 if (!file) in close_fd()
672 return filp_close(file, files); in close_fd()
678 * @fdt: File descriptor table.
706 struct file *file; in __range_close() local
714 file = file_close_fd_locked(files, fd); in __range_close()
715 if (file) { in __range_close()
717 filp_close(file, files); in __range_close()
730 * __close_range() - Close all file descriptors in a given range.
732 * @fd: starting file descriptor to close
733 * @max_fd: last file descriptor to close
736 * This closes a range of file descriptors. All file descriptors
756 * copy all of the file descriptors since they still want to in __close_range()
763 * file descriptors beneath the lowest file descriptor. in __close_range()
776 * We used to share our file descriptor table, and have now in __close_range()
791 * the new file descriptor table and drop the old one. in __close_range()
803 * file_close_fd - return file associated with fd
804 * @fd: file descriptor to retrieve file for
808 * Returns: The file associated with @fd (NULL if @fd is not open)
810 struct file *file_close_fd(unsigned int fd) in file_close_fd()
813 struct file *file; in file_close_fd() local
816 file = file_close_fd_locked(files, fd); in file_close_fd()
819 return file; in file_close_fd()
840 struct file *file; in do_close_on_exec() local
843 file = fdt->fd[fd]; in do_close_on_exec()
844 if (!file) in do_close_on_exec()
849 filp_close(file, files); in do_close_on_exec()
858 static struct file *__get_file_rcu(struct file __rcu **f) in __get_file_rcu()
860 struct file __rcu *file; in __get_file_rcu() local
861 struct file __rcu *file_reloaded; in __get_file_rcu()
862 struct file __rcu *file_reloaded_cmp; in __get_file_rcu()
864 file = rcu_dereference_raw(*f); in __get_file_rcu()
865 if (!file) in __get_file_rcu()
868 if (unlikely(!atomic_long_inc_not_zero(&file->f_count))) in __get_file_rcu()
886 * __rcu protected file pointer so that if that pointer still in __get_file_rcu()
887 * matches the current file, we know we have successfully in __get_file_rcu()
888 * acquired a reference to the right file. in __get_file_rcu()
890 * If the pointers don't match the file has been reallocated by in __get_file_rcu()
893 if (file == file_reloaded_cmp) in __get_file_rcu()
896 fput(file); in __get_file_rcu()
901 * get_file_rcu - try go get a reference to a file under rcu
902 * @f: the file to get a reference on
912 struct file *get_file_rcu(struct file __rcu **f) in get_file_rcu()
915 struct file __rcu *file; in get_file_rcu() local
917 file = __get_file_rcu(f); in get_file_rcu()
918 if (unlikely(!file)) in get_file_rcu()
921 if (unlikely(IS_ERR(file))) in get_file_rcu()
924 return file; in get_file_rcu()
930 * get_file_active - try go get a reference to a file
931 * @f: the file to get a reference on
941 struct file *get_file_active(struct file **f) in get_file_active()
943 struct file __rcu *file; in get_file_active() local
946 file = __get_file_rcu(f); in get_file_active()
948 if (IS_ERR(file)) in get_file_active()
949 file = NULL; in get_file_active()
950 return file; in get_file_active()
954 static inline struct file *__fget_files_rcu(struct files_struct *files, in __fget_files_rcu()
958 struct file *file; in __fget_files_rcu() local
960 struct file __rcu **fdentry; in __fget_files_rcu()
974 file = rcu_dereference_raw(*fdentry); in __fget_files_rcu()
975 file = (void *)(nospec_mask & (unsigned long)file); in __fget_files_rcu()
976 if (unlikely(!file)) in __fget_files_rcu()
980 * Ok, we have a file pointer that was valid at in __fget_files_rcu()
990 if (unlikely(!atomic_long_inc_not_zero(&file->f_count))) in __fget_files_rcu()
996 * (a) the file ref already went down to zero and the in __fget_files_rcu()
997 * file hasn't been reused yet or the file count in __fget_files_rcu()
998 * isn't zero but the file has already been reused. in __fget_files_rcu()
1000 * (b) the file table entry has changed under us. in __fget_files_rcu()
1007 if (unlikely(file != rcu_dereference_raw(*fdentry)) || in __fget_files_rcu()
1009 fput(file); in __fget_files_rcu()
1014 * This isn't the file we're looking for or we're not in __fget_files_rcu()
1017 if (unlikely(file->f_mode & mask)) { in __fget_files_rcu()
1018 fput(file); in __fget_files_rcu()
1023 * Ok, we have a ref to the file, and checked that it in __fget_files_rcu()
1026 return file; in __fget_files_rcu()
1030 static struct file *__fget_files(struct files_struct *files, unsigned int fd, in __fget_files()
1033 struct file *file; in __fget_files() local
1036 file = __fget_files_rcu(files, fd, mask); in __fget_files()
1039 return file; in __fget_files()
1042 static inline struct file *__fget(unsigned int fd, fmode_t mask) in __fget()
1047 struct file *fget(unsigned int fd) in fget()
1053 struct file *fget_raw(unsigned int fd) in fget_raw()
1059 struct file *fget_task(struct task_struct *task, unsigned int fd) in fget_task()
1061 struct file *file = NULL; in fget_task() local
1065 file = __fget_files(task->files, fd, 0); in fget_task()
1068 return file; in fget_task()
1071 struct file *lookup_fdget_rcu(unsigned int fd) in lookup_fdget_rcu()
1078 struct file *task_lookup_fdget_rcu(struct task_struct *task, unsigned int fd) in task_lookup_fdget_rcu()
1082 struct file *file = NULL; in task_lookup_fdget_rcu() local
1087 file = __fget_files_rcu(files, fd, 0); in task_lookup_fdget_rcu()
1090 return file; in task_lookup_fdget_rcu()
1093 struct file *task_lookup_next_fdget_rcu(struct task_struct *task, unsigned int *ret_fd) in task_lookup_next_fdget_rcu()
1098 struct file *file = NULL; in task_lookup_next_fdget_rcu() local
1104 file = __fget_files_rcu(files, fd, 0); in task_lookup_next_fdget_rcu()
1105 if (file) in task_lookup_next_fdget_rcu()
1111 return file; in task_lookup_next_fdget_rcu()
1116 * Lightweight file lookup - no refcnt increment if fd table isn't shared.
1121 * to userspace (i.e. you cannot remember the returned struct file * after
1123 * 2) You must not call filp_close on the returned struct file * in between
1134 struct file *file; in __fget_light() local
1140 * return a file that is concurrently being freed. in __fget_light()
1146 file = files_lookup_fd_raw(files, fd); in __fget_light()
1147 if (!file || unlikely(file->f_mode & mask)) in __fget_light()
1149 return (unsigned long)file; in __fget_light()
1151 file = __fget_files(files, fd, mask); in __fget_light()
1152 if (!file) in __fget_light()
1154 return FDPUT_FPUT | (unsigned long)file; in __fget_light()
1170 * file is marked for FMODE_ATOMIC_POS, and it can be
1174 * can make a file accessible even if it otherwise would
1178 static inline bool file_needs_f_pos_lock(struct file *file) in file_needs_f_pos_lock() argument
1180 return (file->f_mode & FMODE_ATOMIC_POS) && in file_needs_f_pos_lock()
1181 (file_count(file) > 1 || file->f_op->iterate_shared); in file_needs_f_pos_lock()
1187 struct file *file = (struct file *)(v & ~3); in __fdget_pos() local
1189 if (file && file_needs_f_pos_lock(file)) { in __fdget_pos()
1191 mutex_lock(&file->f_pos_lock); in __fdget_pos()
1196 void __f_unlock_pos(struct file *f) in __f_unlock_pos()
1202 * We only lock f_pos if we have threads or if the file might be
1204 * file count (done either by fdget() or by fork()).
1233 struct file *file, unsigned fd, unsigned flags) in do_dup2() argument
1236 struct file *tofree; in do_dup2()
1243 * file immediately after grabbing descriptor, mark it larval if in do_dup2()
1257 get_file(file); in do_dup2()
1258 rcu_assign_pointer(fdt->fd[fd], file); in do_dup2()
1276 int replace_fd(unsigned fd, struct file *file, unsigned flags) in replace_fd() argument
1281 if (!file) in replace_fd()
1291 return do_dup2(files, file, fd, flags); in replace_fd()
1299 * receive_fd() - Install received file into file descriptor table
1300 * @file: struct file that was received from another process
1304 * Installs a received file into the file descriptor table, with appropriate
1309 * struct file.
1313 int receive_fd(struct file *file, int __user *ufd, unsigned int o_flags) in receive_fd() argument
1318 error = security_file_receive(file); in receive_fd()
1334 fd_install(new_fd, get_file(file)); in receive_fd()
1335 __receive_sock(file); in receive_fd()
1340 int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags) in receive_fd_replace() argument
1344 error = security_file_receive(file); in receive_fd_replace()
1347 error = replace_fd(new_fd, file, o_flags); in receive_fd_replace()
1350 __receive_sock(file); in receive_fd_replace()
1357 struct file *file; in ksys_dup3() local
1371 file = files_lookup_fd_locked(files, oldfd); in ksys_dup3()
1372 if (unlikely(!file)) in ksys_dup3()
1379 return do_dup2(files, file, newfd, flags); in ksys_dup3()
1397 struct file *f; in SYSCALL_DEFINE2()
1415 struct file *file = fget_raw(fildes); in SYSCALL_DEFINE1() local
1417 if (file) { in SYSCALL_DEFINE1()
1420 fd_install(ret, file); in SYSCALL_DEFINE1()
1422 fput(file); in SYSCALL_DEFINE1()
1427 int f_dupfd(unsigned int from, struct file *file, unsigned flags) in f_dupfd() argument
1435 get_file(file); in f_dupfd()
1436 fd_install(err, file); in f_dupfd()
1442 int (*f)(const void *, struct file *, unsigned), in iterate_fd() argument
1451 struct file *file; in iterate_fd() local
1452 file = rcu_dereference_check_fdtable(files, fdt->fd[n]); in iterate_fd()
1453 if (!file) in iterate_fd()
1455 res = f(p, file, n); in iterate_fd()