Lines Matching full:fd
7 * Manage the dynamic fd arrays in the process files_struct.
37 kvfree(fdt->fd); in __free_fdtable()
52 * Copy 'count' fd bits from the old table to the new table and clear the extra
81 memcpy(nfdt->fd, ofdt->fd, cpy); in copy_fdtable()
82 memset((char *)nfdt->fd + cpy, 0, set); in copy_fdtable()
136 fdt->fd = data; in alloc_fdtable()
152 kvfree(fdt->fd); in alloc_fdtable()
161 * This function will allocate a new fdtable and both fd array and fdset, of
246 static inline void __set_close_on_exec(unsigned int fd, struct fdtable *fdt) in __set_close_on_exec() argument
248 __set_bit(fd, fdt->close_on_exec); in __set_close_on_exec()
251 static inline void __clear_close_on_exec(unsigned int fd, struct fdtable *fdt) in __clear_close_on_exec() argument
253 if (test_bit(fd, fdt->close_on_exec)) in __clear_close_on_exec()
254 __clear_bit(fd, fdt->close_on_exec); in __clear_close_on_exec()
257 static inline void __set_open_fd(unsigned int fd, struct fdtable *fdt) in __set_open_fd() argument
259 __set_bit(fd, fdt->open_fds); in __set_open_fd()
260 fd /= BITS_PER_LONG; in __set_open_fd()
261 if (!~fdt->open_fds[fd]) in __set_open_fd()
262 __set_bit(fd, fdt->full_fds_bits); in __set_open_fd()
265 static inline void __clear_open_fd(unsigned int fd, struct fdtable *fdt) in __clear_open_fd() argument
267 __clear_bit(fd, fdt->open_fds); in __clear_open_fd()
268 __clear_bit(fd / BITS_PER_LONG, fdt->full_fds_bits); in __clear_open_fd()
322 new_fdt->fd = &newf->fd_array[0]; in dup_fd()
329 * Check whether we need to allocate a larger fd array and fd set. in dup_fd()
351 * Reacquire the oldf lock and a pointer to its fd table in dup_fd()
352 * who knows it may have a new bigger fd table. We need in dup_fd()
362 old_fds = old_fdt->fd; in dup_fd()
363 new_fds = new_fdt->fd; in dup_fd()
366 * We may be racing against fd allocation from other threads using this in dup_fd()
377 * ref the file if we see it and mark the fd slot as unused otherwise. in dup_fd()
405 * It is safe to dereference the fd table without RCU or in close_files()
420 struct file * file = xchg(&fdt->fd[i], NULL); in close_files()
463 .fd = &init_files.fd_array[0],
492 unsigned int fd; in alloc_fd() local
499 fd = start; in alloc_fd()
500 if (fd < files->next_fd) in alloc_fd()
501 fd = files->next_fd; in alloc_fd()
503 if (fd < fdt->max_fds) in alloc_fd()
504 fd = find_next_fd(fdt, fd); in alloc_fd()
511 if (fd >= end) in alloc_fd()
514 error = expand_files(files, fd); in alloc_fd()
526 files->next_fd = fd + 1; in alloc_fd()
528 __set_open_fd(fd, fdt); in alloc_fd()
530 __set_close_on_exec(fd, fdt); in alloc_fd()
532 __clear_close_on_exec(fd, fdt); in alloc_fd()
533 error = fd; in alloc_fd()
536 if (rcu_access_pointer(fdt->fd[fd]) != NULL) { in alloc_fd()
537 printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd); in alloc_fd()
538 rcu_assign_pointer(fdt->fd[fd], NULL); in alloc_fd()
558 static void __put_unused_fd(struct files_struct *files, unsigned int fd) in __put_unused_fd() argument
561 __clear_open_fd(fd, fdt); in __put_unused_fd()
562 if (fd < files->next_fd) in __put_unused_fd()
563 files->next_fd = fd; in __put_unused_fd()
566 void put_unused_fd(unsigned int fd) in put_unused_fd() argument
570 __put_unused_fd(files, fd); in put_unused_fd()
577 * Install a file pointer in the fd array.
592 void fd_install(unsigned int fd, struct file *file) in fd_install() argument
603 BUG_ON(fdt->fd[fd] != NULL); in fd_install()
604 rcu_assign_pointer(fdt->fd[fd], file); in fd_install()
611 BUG_ON(fdt->fd[fd] != NULL); in fd_install()
612 rcu_assign_pointer(fdt->fd[fd], file); in fd_install()
619 * pick_file - return file associatd with fd
621 * @fd: file descriptor to retrieve file for
625 * Returns: The file associated with @fd (NULL if @fd is not open)
627 static struct file *pick_file(struct files_struct *files, unsigned fd) in pick_file() argument
632 if (fd >= fdt->max_fds) in pick_file()
635 fd = array_index_nospec(fd, fdt->max_fds); in pick_file()
636 file = rcu_dereference_raw(fdt->fd[fd]); in pick_file()
638 rcu_assign_pointer(fdt->fd[fd], NULL); in pick_file()
639 __put_unused_fd(files, fd); in pick_file()
644 int close_fd(unsigned fd) in close_fd() argument
650 file = pick_file(files, fd); in close_fd()
660 * last_fd - return last valid index into fd table
673 unsigned int fd, unsigned int max_fd) in __range_cloexec() argument
681 if (fd <= max_fd) in __range_cloexec()
682 bitmap_set(fdt->close_on_exec, fd, max_fd - fd + 1); in __range_cloexec()
686 static inline void __range_close(struct files_struct *files, unsigned int fd, in __range_close() argument
696 for (; fd <= max_fd; fd++) { in __range_close()
697 file = pick_file(files, fd); in __range_close()
715 * @fd: starting file descriptor to close
720 * from @fd up to and including @max_fd are closed.
722 int __close_range(unsigned fd, unsigned max_fd, unsigned int flags) in __close_range() argument
730 if (fd > max_fd) in __close_range()
734 struct fd_range range = {fd, max_fd}, *punch_hole = ⦥ in __close_range()
755 __range_cloexec(cur_fds, fd, max_fd); in __close_range()
757 __range_close(cur_fds, fd, max_fd); in __close_range()
777 struct file *__close_fd_get_file(unsigned int fd) in __close_fd_get_file() argument
779 return pick_file(current->files, fd); in __close_fd_get_file()
786 struct file *close_fd_get_file(unsigned int fd) in close_fd_get_file() argument
792 file = pick_file(files, fd); in close_fd_get_file()
807 unsigned fd = i * BITS_PER_LONG; in do_close_on_exec() local
809 if (fd >= fdt->max_fds) in do_close_on_exec()
815 for ( ; set ; fd++, set >>= 1) { in do_close_on_exec()
819 file = fdt->fd[fd]; in do_close_on_exec()
822 rcu_assign_pointer(fdt->fd[fd], NULL); in do_close_on_exec()
823 __put_unused_fd(files, fd); in do_close_on_exec()
835 unsigned int fd, fmode_t mask) in __fget_files_rcu() argument
842 if (unlikely(fd >= fdt->max_fds)) in __fget_files_rcu()
845 fdentry = fdt->fd + array_index_nospec(fd, fdt->max_fds); in __fget_files_rcu()
868 * Note that we don't need to re-check the 'fdt->fd' in __fget_files_rcu()
888 static struct file *__fget_files(struct files_struct *files, unsigned int fd, in __fget_files() argument
894 file = __fget_files_rcu(files, fd, mask); in __fget_files()
900 static inline struct file *__fget(unsigned int fd, fmode_t mask) in __fget() argument
902 return __fget_files(current->files, fd, mask); in __fget()
905 struct file *fget(unsigned int fd) in fget() argument
907 return __fget(fd, FMODE_PATH); in fget()
911 struct file *fget_raw(unsigned int fd) in fget_raw() argument
913 return __fget(fd, 0); in fget_raw()
917 struct file *fget_task(struct task_struct *task, unsigned int fd) in fget_task() argument
923 file = __fget_files(task->files, fd, 0); in fget_task()
929 struct file *task_lookup_fd_rcu(struct task_struct *task, unsigned int fd) in task_lookup_fd_rcu() argument
938 file = files_lookup_fd_rcu(files, fd); in task_lookup_fd_rcu()
948 unsigned int fd = *ret_fd; in task_lookup_next_fd_rcu() local
954 for (; fd < files_fdtable(files)->max_fds; fd++) { in task_lookup_next_fd_rcu()
955 file = files_lookup_fd_rcu(files, fd); in task_lookup_next_fd_rcu()
961 *ret_fd = fd; in task_lookup_next_fd_rcu()
967 * Lightweight file lookup - no refcnt increment if fd table isn't shared.
982 static unsigned long __fget_light(unsigned int fd, fmode_t mask) in __fget_light() argument
997 file = files_lookup_fd_raw(files, fd); in __fget_light()
1002 file = __fget(fd, mask); in __fget_light()
1008 unsigned long __fdget(unsigned int fd) in __fdget() argument
1010 return __fget_light(fd, FMODE_PATH); in __fdget()
1014 unsigned long __fdget_raw(unsigned int fd) in __fdget_raw() argument
1016 return __fget_light(fd, 0); in __fdget_raw()
1035 unsigned long __fdget_pos(unsigned int fd) in __fdget_pos() argument
1037 unsigned long v = __fdget(fd); in __fdget_pos()
1058 void set_close_on_exec(unsigned int fd, int flag) in set_close_on_exec() argument
1065 __set_close_on_exec(fd, fdt); in set_close_on_exec()
1067 __clear_close_on_exec(fd, fdt); in set_close_on_exec()
1071 bool get_close_on_exec(unsigned int fd) in get_close_on_exec() argument
1078 res = close_on_exec(fd, fdt); in get_close_on_exec()
1084 struct file *file, unsigned fd, unsigned flags) in do_dup2() argument
1105 fd = array_index_nospec(fd, fdt->max_fds); in do_dup2()
1106 tofree = rcu_dereference_raw(fdt->fd[fd]); in do_dup2()
1107 if (!tofree && fd_is_open(fd, fdt)) in do_dup2()
1110 rcu_assign_pointer(fdt->fd[fd], file); in do_dup2()
1111 __set_open_fd(fd, fdt); in do_dup2()
1113 __set_close_on_exec(fd, fdt); in do_dup2()
1115 __clear_close_on_exec(fd, fdt); in do_dup2()
1121 return fd; in do_dup2()
1128 int replace_fd(unsigned fd, struct file *file, unsigned flags) in replace_fd() argument
1134 return close_fd(fd); in replace_fd()
1136 if (fd >= rlimit(RLIMIT_NOFILE)) in replace_fd()
1140 err = expand_files(files, fd); in replace_fd()
1143 return do_dup2(files, file, fd, flags); in replace_fd()
1153 * @ufd: __user pointer to write new fd number to
1154 * @o_flags: the O_* flags to apply to the new fd entry
1157 * checks and count updates. Optionally writes the fd number to userspace, if
1163 * Returns newly install fd or -ve on error.
1305 file = rcu_dereference_check_fdtable(files, fdt->fd[n]); in iterate_fd()