^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/fs/file_table.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 1991, 1992 Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/fdtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/cred.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/eventpoll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/fsnotify.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/sysctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/percpu_counter.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/task_work.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/ima.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /* sysctl tunables... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct files_stat_struct files_stat = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) .max_files = NR_FILE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* SLAB cache for file structures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static struct kmem_cache *filp_cachep __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static struct percpu_counter nr_files __cacheline_aligned_in_smp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static void file_free_rcu(struct rcu_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct file *f = container_of(head, struct file, f_u.fu_rcuhead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) put_cred(f->f_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) kmem_cache_free(filp_cachep, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static inline void file_free(struct file *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) security_file_free(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (!(f->f_mode & FMODE_NOACCOUNT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) percpu_counter_dec(&nr_files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) call_rcu(&f->f_u.fu_rcuhead, file_free_rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * Return the total number of open files in the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static long get_nr_files(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return percpu_counter_read_positive(&nr_files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * Return the maximum number of open files in the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) unsigned long get_max_files(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return files_stat.max_files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) EXPORT_SYMBOL_GPL(get_max_files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * Handle nr_files sysctl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int proc_nr_files(struct ctl_table *table, int write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) void *buffer, size_t *lenp, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) files_stat.nr_files = get_nr_files();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int proc_nr_files(struct ctl_table *table, int write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) void *buffer, size_t *lenp, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static struct file *__alloc_file(int flags, const struct cred *cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct file *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (unlikely(!f))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) f->f_cred = get_cred(cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) error = security_file_alloc(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (unlikely(error)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) file_free_rcu(&f->f_u.fu_rcuhead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) atomic_long_set(&f->f_count, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) rwlock_init(&f->f_owner.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) spin_lock_init(&f->f_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) mutex_init(&f->f_pos_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) eventpoll_init_file(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) f->f_flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) f->f_mode = OPEN_FMODE(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* f->f_version: 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* Find an unused file structure and return a pointer to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * Returns an error pointer if some error happend e.g. we over file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * structures limit, run out of memory or operation is not permitted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * Be very careful using this. You are responsible for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * getting write access to any mount that you might assign
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * to this filp, if it is opened for write. If this is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * done, you will imbalance int the mount's writer count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * and a warning at __fput() time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct file *alloc_empty_file(int flags, const struct cred *cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static long old_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct file *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * Privileged users can go above max_files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * percpu_counters are inaccurate. Do an expensive check before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * we go and fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) goto over;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) f = __alloc_file(flags, cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (!IS_ERR(f))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) percpu_counter_inc(&nr_files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) over:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) /* Ran out of filps - report that */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (get_nr_files() > old_max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) pr_info("VFS: file-max limit %lu reached\n", get_max_files());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) old_max = get_nr_files();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return ERR_PTR(-ENFILE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * Variant of alloc_empty_file() that doesn't check and modify nr_files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * Should not be used unless there's a very good reason to do so.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct file *alloc_empty_file_noaccount(int flags, const struct cred *cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct file *f = __alloc_file(flags, cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (!IS_ERR(f))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) f->f_mode |= FMODE_NOACCOUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * alloc_file - allocate and initialize a 'struct file'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * @path: the (dentry, vfsmount) pair for the new file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * @flags: O_... flags with which the new file will be opened
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * @fop: the 'struct file_operations' for the new file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static struct file *alloc_file(const struct path *path, int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) const struct file_operations *fop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) file = alloc_empty_file(flags, current_cred());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (IS_ERR(file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) file->f_path = *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) file->f_inode = path->dentry->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) file->f_mapping = path->dentry->d_inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) file->f_wb_err = filemap_sample_wb_err(file->f_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) file->f_sb_err = file_sample_sb_err(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if ((file->f_mode & FMODE_READ) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) likely(fop->read || fop->read_iter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) file->f_mode |= FMODE_CAN_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if ((file->f_mode & FMODE_WRITE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) likely(fop->write || fop->write_iter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) file->f_mode |= FMODE_CAN_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) file->f_mode |= FMODE_OPENED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) file->f_op = fop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) i_readcount_inc(path->dentry->d_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct file *alloc_file_pseudo(struct inode *inode, struct vfsmount *mnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) const char *name, int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) const struct file_operations *fops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static const struct dentry_operations anon_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) .d_dname = simple_dname
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct qstr this = QSTR_INIT(name, strlen(name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct path path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) path.dentry = d_alloc_pseudo(mnt->mnt_sb, &this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (!path.dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (!mnt->mnt_sb->s_d_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) d_set_d_op(path.dentry, &anon_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) path.mnt = mntget(mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) d_instantiate(path.dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) file = alloc_file(&path, flags, fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (IS_ERR(file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) ihold(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) path_put(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) EXPORT_SYMBOL(alloc_file_pseudo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) struct file *alloc_file_clone(struct file *base, int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) const struct file_operations *fops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct file *f = alloc_file(&base->f_path, flags, fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (!IS_ERR(f)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) path_get(&f->f_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) f->f_mapping = base->f_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) return f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /* the real guts of fput() - releasing the last reference to file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static void __fput(struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct dentry *dentry = file->f_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) struct vfsmount *mnt = file->f_path.mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) struct inode *inode = file->f_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) fmode_t mode = file->f_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (unlikely(!(file->f_mode & FMODE_OPENED)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) might_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) fsnotify_close(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * The function eventpoll_release() should be the first called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * in the file cleanup chain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) eventpoll_release(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) locks_remove_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) ima_file_free(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (unlikely(file->f_flags & FASYNC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (file->f_op->fasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) file->f_op->fasync(-1, file, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (file->f_op->release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) file->f_op->release(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) !(mode & FMODE_PATH))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) cdev_put(inode->i_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) fops_put(file->f_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) put_pid(file->f_owner.pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) i_readcount_dec(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (mode & FMODE_WRITER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) put_write_access(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) __mnt_drop_write(mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (unlikely(mode & FMODE_NEED_UNMOUNT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) dissolve_on_fput(mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) mntput(mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) file_free(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static LLIST_HEAD(delayed_fput_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static void delayed_fput(struct work_struct *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct llist_node *node = llist_del_all(&delayed_fput_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) struct file *f, *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) llist_for_each_entry_safe(f, t, node, f_u.fu_llist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) __fput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static void ____fput(struct callback_head *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) __fput(container_of(work, struct file, f_u.fu_rcuhead));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * If kernel thread really needs to have the final fput() it has done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * to complete, call this. The only user right now is the boot - we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * *do* need to make sure our writes to binaries on initramfs has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * not left us with opened struct file waiting for __fput() - execve()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * won't work without that. Please, don't add more callers without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * very good reasons; in particular, never call that with locks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * held and never call that from a thread that might need to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * some work on any kind of umount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) void flush_delayed_fput(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) delayed_fput(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) EXPORT_SYMBOL_GPL(flush_delayed_fput);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) void fput_many(struct file *file, unsigned int refs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (atomic_long_sub_and_test(refs, &file->f_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) struct task_struct *task = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) init_task_work(&file->f_u.fu_rcuhead, ____fput);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (!task_work_add(task, &file->f_u.fu_rcuhead, TWA_RESUME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * After this task has run exit_task_work(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * task_work_add() will fail. Fall through to delayed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * fput to avoid leaking *file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (llist_add(&file->f_u.fu_llist, &delayed_fput_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) schedule_delayed_work(&delayed_fput_work, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) void fput(struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) fput_many(file, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * synchronous analog of fput(); for kernel threads that might be needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * in some umount() (and thus can't use flush_delayed_fput() without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * risking deadlocks), need to wait for completion of __fput() and know
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * for this specific struct file it won't involve anything that would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * need them. Use only if you really need it - at the very least,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * don't blindly convert fput() by kernel thread to that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) void __fput_sync(struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (atomic_long_dec_and_test(&file->f_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) struct task_struct *task = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) BUG_ON(!(task->flags & PF_KTHREAD));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) __fput(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) EXPORT_SYMBOL(fput);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) void __init files_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) percpu_counter_init(&nr_files, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * One file with associated inode and dcache is very roughly 1K. Per default
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * do not use more than 10% of our memory for files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) void __init files_maxfiles_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) unsigned long n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) unsigned long nr_pages = totalram_pages();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) unsigned long memreserve = (nr_pages - nr_free_pages()) * 3/2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) memreserve = min(memreserve, nr_pages - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) n = ((nr_pages - memreserve) * (PAGE_SIZE / 1024)) / 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) files_stat.max_files = max_t(unsigned long, n, NR_FILE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }