Lines Matching full:fd

7  *  Manage the dynamic fd arrays in the process files_struct.
105 kvfree(fdt->fd); in __free_fdtable()
120 * Copy 'count' fd bits from the old table to the new table and clear the extra
149 memcpy(nfdt->fd, ofdt->fd, cpy); in copy_fdtable()
150 memset((char *)nfdt->fd + cpy, 0, set); in copy_fdtable()
179 * 256 slots (i.e. 1Kb fd array). in alloc_fdtable()
180 * 3. on 64bit don't skip anything, 1Kb fd array means 128 slots there in alloc_fdtable()
207 fdt->fd = data; in alloc_fdtable()
223 kvfree(fdt->fd); in alloc_fdtable()
232 * This function will allocate a new fdtable and both fd array and fdset, of
307 static inline void __set_close_on_exec(unsigned int fd, struct fdtable *fdt, in __set_close_on_exec() argument
311 __set_bit(fd, fdt->close_on_exec); in __set_close_on_exec()
313 if (test_bit(fd, fdt->close_on_exec)) in __set_close_on_exec()
314 __clear_bit(fd, fdt->close_on_exec); in __set_close_on_exec()
318 static inline void __set_open_fd(unsigned int fd, struct fdtable *fdt, bool set) in __set_open_fd() argument
320 __set_bit(fd, fdt->open_fds); in __set_open_fd()
321 __set_close_on_exec(fd, fdt, set); in __set_open_fd()
322 fd /= BITS_PER_LONG; in __set_open_fd()
323 if (!~fdt->open_fds[fd]) in __set_open_fd()
324 __set_bit(fd, fdt->full_fds_bits); in __set_open_fd()
327 static inline void __clear_open_fd(unsigned int fd, struct fdtable *fdt) in __clear_open_fd() argument
329 __clear_bit(fd, fdt->open_fds); in __clear_open_fd()
330 fd /= BITS_PER_LONG; in __clear_open_fd()
331 if (test_bit(fd, fdt->full_fds_bits)) in __clear_open_fd()
332 __clear_bit(fd, fdt->full_fds_bits); in __clear_open_fd()
335 static inline bool fd_is_open(unsigned int fd, const struct fdtable *fdt) in fd_is_open() argument
337 return test_bit(fd, fdt->open_fds); in fd_is_open()
390 new_fdt->fd = &newf->fd_array[0]; in dup_fd()
397 * Check whether we need to allocate a larger fd array and fd set. in dup_fd()
412 * Reacquire the oldf lock and a pointer to its fd table in dup_fd()
413 * who knows it may have a new bigger fd table. We need in dup_fd()
423 old_fds = old_fdt->fd; in dup_fd()
424 new_fds = new_fdt->fd; in dup_fd()
427 * We may be racing against fd allocation from other threads using this in dup_fd()
438 * ref the file if we see it and mark the fd slot as unused otherwise. in dup_fd()
462 * It is safe to dereference the fd table without RCU or in close_files()
477 struct file *file = fdt->fd[i]; in close_files()
520 .fd = &init_files.fd_array[0],
558 unsigned int fd; in alloc_fd() local
565 fd = start; in alloc_fd()
566 if (fd < files->next_fd) in alloc_fd()
567 fd = files->next_fd; in alloc_fd()
569 if (likely(fd < fdt->max_fds)) in alloc_fd()
570 fd = find_next_fd(fdt, fd); in alloc_fd()
577 if (unlikely(fd >= end)) in alloc_fd()
580 if (unlikely(fd >= fdt->max_fds)) { in alloc_fd()
581 error = expand_files(files, fd); in alloc_fd()
589 files->next_fd = fd + 1; in alloc_fd()
591 __set_open_fd(fd, fdt, flags & O_CLOEXEC); in alloc_fd()
592 error = fd; in alloc_fd()
593 VFS_BUG_ON(rcu_access_pointer(fdt->fd[fd]) != NULL); in alloc_fd()
611 static void __put_unused_fd(struct files_struct *files, unsigned int fd) in __put_unused_fd() argument
614 __clear_open_fd(fd, fdt); in __put_unused_fd()
615 if (fd < files->next_fd) in __put_unused_fd()
616 files->next_fd = fd; in __put_unused_fd()
619 void put_unused_fd(unsigned int fd) in put_unused_fd() argument
623 __put_unused_fd(files, fd); in put_unused_fd()
630 * fd_install - install a file pointer in the fd array
631 * @fd: file descriptor to install the file in
637 void fd_install(unsigned int fd, struct file *file) in fd_install() argument
651 VFS_BUG_ON(rcu_access_pointer(fdt->fd[fd]) != NULL); in fd_install()
652 rcu_assign_pointer(fdt->fd[fd], file); in fd_install()
659 VFS_BUG_ON(rcu_access_pointer(fdt->fd[fd]) != NULL); in fd_install()
660 rcu_assign_pointer(fdt->fd[fd], file); in fd_install()
667 * file_close_fd_locked - return file associated with fd
669 * @fd: file descriptor to retrieve file for
675 * Returns: The file associated with @fd (NULL if @fd is not open)
677 struct file *file_close_fd_locked(struct files_struct *files, unsigned fd) in file_close_fd_locked() argument
684 if (fd >= fdt->max_fds) in file_close_fd_locked()
687 fd = array_index_nospec(fd, fdt->max_fds); in file_close_fd_locked()
688 file = rcu_dereference_raw(fdt->fd[fd]); in file_close_fd_locked()
690 rcu_assign_pointer(fdt->fd[fd], NULL); in file_close_fd_locked()
691 __put_unused_fd(files, fd); in file_close_fd_locked()
696 int close_fd(unsigned fd) in close_fd() argument
702 file = file_close_fd_locked(files, fd); in close_fd()
712 * last_fd - return last valid index into fd table
725 unsigned int fd, unsigned int max_fd) in __range_cloexec() argument
733 if (fd <= max_fd) in __range_cloexec()
734 bitmap_set(fdt->close_on_exec, fd, max_fd - fd + 1); in __range_cloexec()
738 static inline void __range_close(struct files_struct *files, unsigned int fd, in __range_close() argument
748 for (; fd <= max_fd; fd++) { in __range_close()
749 file = file_close_fd_locked(files, fd); in __range_close()
767 * @fd: starting file descriptor to close
772 * from @fd up to and including @max_fd are closed.
775 SYSCALL_DEFINE3(close_range, unsigned int, fd, unsigned int, max_fd, in SYSCALL_DEFINE3() argument
784 if (fd > max_fd) in SYSCALL_DEFINE3()
788 struct fd_range range = {fd, max_fd}, *punch_hole = &range; in SYSCALL_DEFINE3()
809 __range_cloexec(cur_fds, fd, max_fd); in SYSCALL_DEFINE3()
811 __range_close(cur_fds, fd, max_fd); in SYSCALL_DEFINE3()
828 * file_close_fd - return file associated with fd
829 * @fd: file descriptor to retrieve file for
833 * Returns: The file associated with @fd (NULL if @fd is not open)
835 struct file *file_close_fd(unsigned int fd) in file_close_fd() argument
841 file = file_close_fd_locked(files, fd); in file_close_fd()
856 unsigned fd = i * BITS_PER_LONG; in do_close_on_exec() local
858 if (fd >= fdt->max_fds) in do_close_on_exec()
864 for ( ; set ; fd++, set >>= 1) { in do_close_on_exec()
868 file = fdt->fd[fd]; in do_close_on_exec()
871 rcu_assign_pointer(fdt->fd[fd], NULL); in do_close_on_exec()
872 __put_unused_fd(files, fd); in do_close_on_exec()
975 unsigned int fd, fmode_t mask) in __fget_files_rcu() argument
983 /* Mask is a 0 for invalid fd's, ~0 for valid ones */ in __fget_files_rcu()
984 nospec_mask = array_index_mask_nospec(fd, fdt->max_fds); in __fget_files_rcu()
987 * fdentry points to the 'fd' offset, or fdt->fd[0]. in __fget_files_rcu()
988 * Loading from fdt->fd[0] is always safe, because the in __fget_files_rcu()
991 fdentry = fdt->fd + (fd & nospec_mask); in __fget_files_rcu()
1021 * Note that we don't need to re-check the 'fdt->fd' in __fget_files_rcu()
1050 static struct file *__fget_files(struct files_struct *files, unsigned int fd, in __fget_files() argument
1056 file = __fget_files_rcu(files, fd, mask); in __fget_files()
1062 static inline struct file *__fget(unsigned int fd, fmode_t mask) in __fget() argument
1064 return __fget_files(current->files, fd, mask); in __fget()
1067 struct file *fget(unsigned int fd) in fget() argument
1069 return __fget(fd, FMODE_PATH); in fget()
1073 struct file *fget_raw(unsigned int fd) in fget_raw() argument
1075 return __fget(fd, 0); in fget_raw()
1079 struct file *fget_task(struct task_struct *task, unsigned int fd) in fget_task() argument
1085 file = __fget_files(task->files, fd, 0); in fget_task()
1095 unsigned int fd = *ret_fd; in fget_task_next() local
1102 for (; fd < files_fdtable(files)->max_fds; fd++) { in fget_task_next()
1103 file = __fget_files_rcu(files, fd, 0); in fget_task_next()
1110 *ret_fd = fd; in fget_task_next()
1116 * Lightweight file lookup - no refcnt increment if fd table isn't shared.
1138 static inline struct fd __fget_light(unsigned int fd, fmode_t mask) in __fget_light() argument
1153 file = files_lookup_fd_raw(files, fd); in __fget_light()
1158 file = __fget_files(files, fd, mask); in __fget_light()
1164 struct fd fdget(unsigned int fd) in fdget() argument
1166 return __fget_light(fd, FMODE_PATH); in fdget()
1170 struct fd fdget_raw(unsigned int fd) in fdget_raw() argument
1172 return __fget_light(fd, 0); in fdget_raw()
1206 struct fd fdget_pos(unsigned int fd) in fdget_pos() argument
1208 struct fd f = fdget(fd); in fdget_pos()
1229 void set_close_on_exec(unsigned int fd, int flag) in set_close_on_exec() argument
1233 __set_close_on_exec(fd, files_fdtable(files), flag); in set_close_on_exec()
1237 bool get_close_on_exec(unsigned int fd) in get_close_on_exec() argument
1241 res = close_on_exec(fd, current->files); in get_close_on_exec()
1247 struct file *file, unsigned fd, unsigned flags) in do_dup2() argument
1254 * dup2() is expected to close the file installed in the target fd slot in do_dup2()
1255 * (if any). However, userspace hand-picking a fd may be racing against in do_dup2()
1260 * fd = get_unused_fd_flags(); // fd slot reserved, ->fd[fd] == NULL in do_dup2()
1262 * fd_install(fd, file); // only now ->fd[fd] == file in do_dup2()
1264 * It is an invariant that a successfully allocated fd has a NULL entry in do_dup2()
1267 * If we fit the window, we have the fd to populate, yet no target file in do_dup2()
1280 fd = array_index_nospec(fd, fdt->max_fds); in do_dup2()
1281 tofree = rcu_dereference_raw(fdt->fd[fd]); in do_dup2()
1282 if (!tofree && fd_is_open(fd, fdt)) in do_dup2()
1285 rcu_assign_pointer(fdt->fd[fd], file); in do_dup2()
1286 __set_open_fd(fd, fdt, flags & O_CLOEXEC); in do_dup2()
1292 return fd; in do_dup2()
1299 int replace_fd(unsigned fd, struct file *file, unsigned flags) in replace_fd() argument
1305 return close_fd(fd); in replace_fd()
1307 if (fd >= rlimit(RLIMIT_NOFILE)) in replace_fd()
1311 err = expand_files(files, fd); in replace_fd()
1314 return do_dup2(files, file, fd, flags); in replace_fd()
1324 * @ufd: __user pointer to write new fd number to
1325 * @o_flags: the O_* flags to apply to the new fd entry
1328 * checks and count updates. Optionally writes the fd number to userspace, if
1334 * Returns newly install fd or -ve on error.
1475 file = rcu_dereference_check_fdtable(files, fdt->fd[n]); in iterate_fd()