Lines Matching full:fd
7 * Manage the dynamic fd arrays in the process files_struct.
100 kvfree(fdt->fd); in __free_fdtable()
115 * Copy 'count' fd bits from the old table to the new table and clear the extra
144 memcpy(nfdt->fd, ofdt->fd, cpy); in copy_fdtable()
145 memset((char *)nfdt->fd + cpy, 0, set); in copy_fdtable()
174 * 256 slots (i.e. 1Kb fd array). in alloc_fdtable()
175 * 3. on 64bit don't skip anything, 1Kb fd array means 128 slots there in alloc_fdtable()
202 fdt->fd = data; in alloc_fdtable()
218 kvfree(fdt->fd); in alloc_fdtable()
227 * This function will allocate a new fdtable and both fd array and fdset, of
302 static inline void __set_close_on_exec(unsigned int fd, struct fdtable *fdt, in __set_close_on_exec() argument
306 __set_bit(fd, fdt->close_on_exec); in __set_close_on_exec()
308 if (test_bit(fd, fdt->close_on_exec)) in __set_close_on_exec()
309 __clear_bit(fd, fdt->close_on_exec); in __set_close_on_exec()
313 static inline void __set_open_fd(unsigned int fd, struct fdtable *fdt, bool set) in __set_open_fd() argument
315 __set_bit(fd, fdt->open_fds); in __set_open_fd()
316 __set_close_on_exec(fd, fdt, set); in __set_open_fd()
317 fd /= BITS_PER_LONG; in __set_open_fd()
318 if (!~fdt->open_fds[fd]) in __set_open_fd()
319 __set_bit(fd, fdt->full_fds_bits); in __set_open_fd()
322 static inline void __clear_open_fd(unsigned int fd, struct fdtable *fdt) in __clear_open_fd() argument
324 __clear_bit(fd, fdt->open_fds); in __clear_open_fd()
325 fd /= BITS_PER_LONG; in __clear_open_fd()
326 if (test_bit(fd, fdt->full_fds_bits)) in __clear_open_fd()
327 __clear_bit(fd, fdt->full_fds_bits); in __clear_open_fd()
330 static inline bool fd_is_open(unsigned int fd, const struct fdtable *fdt) in fd_is_open() argument
332 return test_bit(fd, fdt->open_fds); in fd_is_open()
385 new_fdt->fd = &newf->fd_array[0]; in dup_fd()
392 * Check whether we need to allocate a larger fd array and fd set. in dup_fd()
407 * Reacquire the oldf lock and a pointer to its fd table in dup_fd()
408 * who knows it may have a new bigger fd table. We need in dup_fd()
418 old_fds = old_fdt->fd; in dup_fd()
419 new_fds = new_fdt->fd; in dup_fd()
422 * We may be racing against fd allocation from other threads using this in dup_fd()
433 * ref the file if we see it and mark the fd slot as unused otherwise. in dup_fd()
457 * It is safe to dereference the fd table without RCU or in close_files()
472 struct file *file = fdt->fd[i]; in close_files()
515 .fd = &init_files.fd_array[0],
553 unsigned int fd; in alloc_fd() local
560 fd = start; in alloc_fd()
561 if (fd < files->next_fd) in alloc_fd()
562 fd = files->next_fd; in alloc_fd()
564 if (likely(fd < fdt->max_fds)) in alloc_fd()
565 fd = find_next_fd(fdt, fd); in alloc_fd()
572 if (unlikely(fd >= end)) in alloc_fd()
575 if (unlikely(fd >= fdt->max_fds)) { in alloc_fd()
576 error = expand_files(files, fd); in alloc_fd()
584 files->next_fd = fd + 1; in alloc_fd()
586 __set_open_fd(fd, fdt, flags & O_CLOEXEC); in alloc_fd()
587 error = fd; in alloc_fd()
605 static void __put_unused_fd(struct files_struct *files, unsigned int fd) in __put_unused_fd() argument
608 __clear_open_fd(fd, fdt); in __put_unused_fd()
609 if (fd < files->next_fd) in __put_unused_fd()
610 files->next_fd = fd; in __put_unused_fd()
613 void put_unused_fd(unsigned int fd) in put_unused_fd() argument
617 __put_unused_fd(files, fd); in put_unused_fd()
624 * Install a file pointer in the fd array.
639 void fd_install(unsigned int fd, struct file *file) in fd_install() argument
653 WARN_ON(fdt->fd[fd] != NULL); in fd_install()
654 rcu_assign_pointer(fdt->fd[fd], file); in fd_install()
661 BUG_ON(fdt->fd[fd] != NULL); in fd_install()
662 rcu_assign_pointer(fdt->fd[fd], file); in fd_install()
669 * file_close_fd_locked - return file associated with fd
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() argument
686 if (fd >= fdt->max_fds) in file_close_fd_locked()
689 fd = array_index_nospec(fd, fdt->max_fds); in file_close_fd_locked()
690 file = rcu_dereference_raw(fdt->fd[fd]); in file_close_fd_locked()
692 rcu_assign_pointer(fdt->fd[fd], NULL); in file_close_fd_locked()
693 __put_unused_fd(files, fd); in file_close_fd_locked()
698 int close_fd(unsigned fd) in close_fd() argument
704 file = file_close_fd_locked(files, fd); in close_fd()
714 * last_fd - return last valid index into fd table
727 unsigned int fd, unsigned int max_fd) in __range_cloexec() argument
735 if (fd <= max_fd) in __range_cloexec()
736 bitmap_set(fdt->close_on_exec, fd, max_fd - fd + 1); in __range_cloexec()
740 static inline void __range_close(struct files_struct *files, unsigned int fd, in __range_close() argument
750 for (; fd <= max_fd; fd++) { in __range_close()
751 file = file_close_fd_locked(files, fd); in __range_close()
769 * @fd: starting file descriptor to close
774 * from @fd up to and including @max_fd are closed.
777 SYSCALL_DEFINE3(close_range, unsigned int, fd, unsigned int, max_fd, in SYSCALL_DEFINE3() argument
786 if (fd > max_fd) in SYSCALL_DEFINE3()
790 struct fd_range range = {fd, max_fd}, *punch_hole = ⦥ in SYSCALL_DEFINE3()
811 __range_cloexec(cur_fds, fd, max_fd); in SYSCALL_DEFINE3()
813 __range_close(cur_fds, fd, max_fd); 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() argument
843 file = file_close_fd_locked(files, fd); in file_close_fd()
858 unsigned fd = i * BITS_PER_LONG; in do_close_on_exec() local
860 if (fd >= fdt->max_fds) in do_close_on_exec()
866 for ( ; set ; fd++, set >>= 1) { in do_close_on_exec()
870 file = fdt->fd[fd]; in do_close_on_exec()
873 rcu_assign_pointer(fdt->fd[fd], NULL); in do_close_on_exec()
874 __put_unused_fd(files, fd); in do_close_on_exec()
977 unsigned int fd, fmode_t mask) in __fget_files_rcu() argument
985 /* Mask is a 0 for invalid fd's, ~0 for valid ones */ in __fget_files_rcu()
986 nospec_mask = array_index_mask_nospec(fd, fdt->max_fds); in __fget_files_rcu()
989 * fdentry points to the 'fd' offset, or fdt->fd[0]. in __fget_files_rcu()
990 * Loading from fdt->fd[0] is always safe, because the in __fget_files_rcu()
993 fdentry = fdt->fd + (fd & nospec_mask); in __fget_files_rcu()
1023 * Note that we don't need to re-check the 'fdt->fd' in __fget_files_rcu()
1052 static struct file *__fget_files(struct files_struct *files, unsigned int fd, in __fget_files() argument
1058 file = __fget_files_rcu(files, fd, mask); in __fget_files()
1064 static inline struct file *__fget(unsigned int fd, fmode_t mask) in __fget() argument
1066 return __fget_files(current->files, fd, mask); in __fget()
1069 struct file *fget(unsigned int fd) in fget() argument
1071 return __fget(fd, FMODE_PATH); in fget()
1075 struct file *fget_raw(unsigned int fd) in fget_raw() argument
1077 return __fget(fd, 0); in fget_raw()
1081 struct file *fget_task(struct task_struct *task, unsigned int fd) in fget_task() argument
1087 file = __fget_files(task->files, fd, 0); in fget_task()
1097 unsigned int fd = *ret_fd; in fget_task_next() local
1104 for (; fd < files_fdtable(files)->max_fds; fd++) { in fget_task_next()
1105 file = __fget_files_rcu(files, fd, 0); in fget_task_next()
1112 *ret_fd = fd; in fget_task_next()
1118 * Lightweight file lookup - no refcnt increment if fd table isn't shared.
1140 static inline struct fd __fget_light(unsigned int fd, fmode_t mask) in __fget_light() argument
1155 file = files_lookup_fd_raw(files, fd); in __fget_light()
1160 file = __fget_files(files, fd, mask); in __fget_light()
1166 struct fd fdget(unsigned int fd) in fdget() argument
1168 return __fget_light(fd, FMODE_PATH); in fdget()
1172 struct fd fdget_raw(unsigned int fd) in fdget_raw() argument
1174 return __fget_light(fd, 0); in fdget_raw()
1193 struct fd fdget_pos(unsigned int fd) in fdget_pos() argument
1195 struct fd f = fdget(fd); in fdget_pos()
1216 void set_close_on_exec(unsigned int fd, int flag) in set_close_on_exec() argument
1220 __set_close_on_exec(fd, files_fdtable(files), flag); in set_close_on_exec()
1224 bool get_close_on_exec(unsigned int fd) in get_close_on_exec() argument
1228 res = close_on_exec(fd, current->files); in get_close_on_exec()
1234 struct file *file, unsigned fd, unsigned flags) in do_dup2() argument
1247 fd = array_index_nospec(fd, fdt->max_fds); in do_dup2()
1248 tofree = rcu_dereference_raw(fdt->fd[fd]); in do_dup2()
1249 if (!tofree && fd_is_open(fd, fdt)) in do_dup2()
1252 rcu_assign_pointer(fdt->fd[fd], file); in do_dup2()
1253 __set_open_fd(fd, fdt, flags & O_CLOEXEC); in do_dup2()
1259 return fd; in do_dup2()
1266 int replace_fd(unsigned fd, struct file *file, unsigned flags) in replace_fd() argument
1272 return close_fd(fd); in replace_fd()
1274 if (fd >= rlimit(RLIMIT_NOFILE)) in replace_fd()
1278 err = expand_files(files, fd); in replace_fd()
1281 return do_dup2(files, file, fd, flags); in replace_fd()
1291 * @ufd: __user pointer to write new fd number to
1292 * @o_flags: the O_* flags to apply to the new fd entry
1295 * checks and count updates. Optionally writes the fd number to userspace, if
1301 * Returns newly install fd or -ve on error.
1442 file = rcu_dereference_check_fdtable(files, fdt->fd[n]); in iterate_fd()