Lines Matching refs:file
3 * linux/fs/file.c
17 #include <linux/file.h>
36 if (WARN_ONCE(cnt >= FILE_REF_RELEASED, "imbalanced put on file reference count")) {
121 * space if any. This does not copy the file pointers. Called with the files
138 * Copy all file descriptors from the old table to the new, expanded table and
147 cpy = ofdt->max_fds * sizeof(struct file *);
148 set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
206 * a process tries to use a file descriptor near that limit. For example,
208 * systemd typically sets it to - then trying to use a file descriptor
209 * close to that value will require allocating a file descriptor table
212 if (unlikely(nr > INT_MAX / sizeof(struct file *)))
219 data = kvmalloc_array(nr, sizeof(struct file *), GFP_KERNEL_ACCOUNT);
246 * Expand the file descriptor table.
283 * This function will expand the file structures, if the requested size exceeds
386 struct file **old_fds, **new_fds;
447 * the file can show up as we are walking the array below.
453 * ref the file if we see it and mark the fd slot as unused otherwise.
456 struct file *f = rcu_dereference_raw(*old_fds++);
467 memset(new_fds, 0, (new_fdt->max_fds - open_files) * sizeof(struct file *));
492 struct file *file = fdt->fd[i];
493 if (file) {
494 filp_close(file, files);
568 * allocate a file descriptor, mark it busy.
645 * fd_install - install a file pointer in the fd array
646 * @fd: file descriptor to install the file in
647 * @file: the file to install
649 * This consumes the "file" refcount, so callers should treat it
650 * as if they had called fput(file).
652 void fd_install(unsigned int fd, struct file *file)
657 if (WARN_ON_ONCE(unlikely(file->f_mode & FMODE_BACKING)))
667 rcu_assign_pointer(fdt->fd[fd], file);
675 rcu_assign_pointer(fdt->fd[fd], file);
682 * file_close_fd_locked - return file associated with fd
683 * @files: file struct to retrieve file from
684 * @fd: file descriptor to retrieve file for
690 * Returns: The file associated with @fd (NULL if @fd is not open)
692 struct file *file_close_fd_locked(struct files_struct *files, unsigned fd)
695 struct file *file;
703 file = rcu_dereference_raw(fdt->fd[fd]);
704 if (file) {
708 return file;
714 struct file *file;
717 file = file_close_fd_locked(files, fd);
719 if (!file)
722 return filp_close(file, files);
756 struct file *file;
764 file = file_close_fd_locked(files, fd);
765 if (file) {
767 filp_close(file, files);
780 * sys_close_range() - Close all file descriptors in a given range.
782 * @fd: starting file descriptor to close
783 * @max_fd: last file descriptor to close
786 * This closes a range of file descriptors. All file descriptors
788 * Currently, errors to close a given file descriptor are ignored.
807 * copy all of the file descriptors since they still want to
817 * We used to share our file descriptor table, and have now
831 * the new file descriptor table and drop the old one.
843 * file_close_fd - return file associated with fd
844 * @fd: file descriptor to retrieve file for
848 * Returns: The file associated with @fd (NULL if @fd is not open)
850 struct file *file_close_fd(unsigned int fd)
853 struct file *file;
856 file = file_close_fd_locked(files, fd);
859 return file;
880 struct file *file;
883 file = fdt->fd[fd];
884 if (!file)
889 filp_close(file, files);
898 static struct file *__get_file_rcu(struct file __rcu **f)
900 struct file __rcu *file;
901 struct file __rcu *file_reloaded;
902 struct file __rcu *file_reloaded_cmp;
904 file = rcu_dereference_raw(*f);
905 if (!file)
908 if (unlikely(!file_ref_get(&file->f_ref)))
926 * __rcu protected file pointer so that if that pointer still
927 * matches the current file, we know we have successfully
928 * acquired a reference to the right file.
930 * If the pointers don't match the file has been reallocated by
933 if (file == file_reloaded_cmp)
936 fput(file);
941 * get_file_rcu - try go get a reference to a file under rcu
942 * @f: the file to get a reference on
952 struct file *get_file_rcu(struct file __rcu **f)
955 struct file __rcu *file;
957 file = __get_file_rcu(f);
958 if (!IS_ERR(file))
959 return file;
965 * get_file_active - try go get a reference to a file
966 * @f: the file to get a reference on
976 struct file *get_file_active(struct file **f)
978 struct file __rcu *file;
981 file = __get_file_rcu(f);
983 if (IS_ERR(file))
984 file = NULL;
985 return file;
989 static inline struct file *__fget_files_rcu(struct files_struct *files,
993 struct file *file;
995 struct file __rcu **fdentry;
1009 file = rcu_dereference_raw(*fdentry);
1010 file = (void *)(nospec_mask & (unsigned long)file);
1011 if (unlikely(!file))
1015 * Ok, we have a file pointer that was valid at
1025 if (unlikely(!file_ref_get(&file->f_ref)))
1031 * (a) the file ref already went down to zero and the
1032 * file hasn't been reused yet or the file count
1033 * isn't zero but the file has already been reused.
1035 * (b) the file table entry has changed under us.
1042 if (unlikely(file != rcu_dereference_raw(*fdentry)) ||
1044 fput(file);
1049 * This isn't the file we're looking for or we're not
1052 if (unlikely(file->f_mode & mask)) {
1053 fput(file);
1058 * Ok, we have a ref to the file, and checked that it
1061 return file;
1065 static struct file *__fget_files(struct files_struct *files, unsigned int fd,
1068 struct file *file;
1071 file = __fget_files_rcu(files, fd, mask);
1074 return file;
1077 static inline struct file *__fget(unsigned int fd, fmode_t mask)
1082 struct file *fget(unsigned int fd)
1088 struct file *fget_raw(unsigned int fd)
1094 struct file *fget_task(struct task_struct *task, unsigned int fd)
1096 struct file *file = NULL;
1100 file = __fget_files(task->files, fd, 0);
1103 return file;
1106 struct file *fget_task_next(struct task_struct *task, unsigned int *ret_fd)
1111 struct file *file = NULL;
1118 file = __fget_files_rcu(files, fd, 0);
1119 if (file)
1126 return file;
1131 * Lightweight file lookup - no refcnt increment if fd table isn't shared.
1136 * to userspace (i.e. you cannot remember the returned struct file * after
1138 * 2) You must not call filp_close on the returned struct file * in between
1151 * See also the documentation in rust/kernel/file.rs.
1156 struct file *file;
1162 * return a file that is concurrently being freed.
1168 file = files_lookup_fd_raw(files, fd);
1169 if (!file || unlikely(file->f_mode & mask))
1171 return BORROWED_FD(file);
1173 file = __fget_files(files, fd, mask);
1174 if (!file)
1176 return CLONED_FD(file);
1192 * file is marked for FMODE_ATOMIC_POS, and it can be
1196 * can make a file accessible even if it otherwise would
1200 static inline bool file_needs_f_pos_lock(struct file *file)
1202 if (!(file->f_mode & FMODE_ATOMIC_POS))
1204 if (__file_ref_read_raw(&file->f_ref) != FILE_REF_ONEREF)
1206 if (file->f_op->iterate_shared)
1211 bool file_seek_cur_needs_f_lock(struct file *file)
1213 if (!(file->f_mode & FMODE_ATOMIC_POS) && !file->f_op->iterate_shared)
1218 * this file obj, in which case the caller is expected to provide the
1228 struct file *file = fd_file(f);
1230 if (likely(file) && file_needs_f_pos_lock(file)) {
1232 mutex_lock(&file->f_pos_lock);
1237 void __f_unlock_pos(struct file *f)
1243 * We only lock f_pos if we have threads or if the file might be
1245 * file count (done either by fdget() or by fork()).
1266 struct file *file, unsigned fd, unsigned flags)
1269 struct file *tofree;
1273 * dup2() is expected to close the file installed in the target fd slot
1280 * file = hard_work_goes_here();
1281 * fd_install(fd, file); // only now ->fd[fd] == file
1286 * If we fit the window, we have the fd to populate, yet no target file
1287 * to close. Trying to ignore it and install our new file would violate
1288 * the invariant and make fd_install() overwrite our file.
1303 get_file(file);
1304 rcu_assign_pointer(fdt->fd[fd], file);
1318 int replace_fd(unsigned fd, struct file *file, unsigned flags)
1323 if (!file)
1333 return do_dup2(files, file, fd, flags);
1341 * receive_fd() - Install received file into file descriptor table
1342 * @file: struct file that was received from another process
1346 * Installs a received file into the file descriptor table, with appropriate
1351 * struct file.
1355 int receive_fd(struct file *file, int __user *ufd, unsigned int o_flags)
1360 error = security_file_receive(file);
1376 fd_install(new_fd, get_file(file));
1377 __receive_sock(file);
1382 int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags)
1386 error = security_file_receive(file);
1389 error = replace_fd(new_fd, file, o_flags);
1392 __receive_sock(file);
1399 struct file *file;
1413 file = files_lookup_fd_locked(files, oldfd);
1414 if (unlikely(!file))
1421 return do_dup2(files, file, newfd, flags);
1439 struct file *f;
1457 struct file *file = fget_raw(fildes);
1459 if (file) {
1462 fd_install(ret, file);
1464 fput(file);
1469 int f_dupfd(unsigned int from, struct file *file, unsigned flags)
1477 get_file(file);
1478 fd_install(err, file);
1484 int (*f)(const void *, struct file *, unsigned),
1493 struct file *file;
1494 file = rcu_dereference_check_fdtable(files, fdt->fd[n]);
1495 if (!file)
1497 res = f(p, file, n);