162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * linux/fs/file_table.c 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 1991, 1992 Linus Torvalds 662306a36Sopenharmony_ci * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu) 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#include <linux/string.h> 1062306a36Sopenharmony_ci#include <linux/slab.h> 1162306a36Sopenharmony_ci#include <linux/file.h> 1262306a36Sopenharmony_ci#include <linux/fdtable.h> 1362306a36Sopenharmony_ci#include <linux/init.h> 1462306a36Sopenharmony_ci#include <linux/module.h> 1562306a36Sopenharmony_ci#include <linux/fs.h> 1662306a36Sopenharmony_ci#include <linux/filelock.h> 1762306a36Sopenharmony_ci#include <linux/security.h> 1862306a36Sopenharmony_ci#include <linux/cred.h> 1962306a36Sopenharmony_ci#include <linux/eventpoll.h> 2062306a36Sopenharmony_ci#include <linux/rcupdate.h> 2162306a36Sopenharmony_ci#include <linux/mount.h> 2262306a36Sopenharmony_ci#include <linux/capability.h> 2362306a36Sopenharmony_ci#include <linux/cdev.h> 2462306a36Sopenharmony_ci#include <linux/fsnotify.h> 2562306a36Sopenharmony_ci#include <linux/sysctl.h> 2662306a36Sopenharmony_ci#include <linux/percpu_counter.h> 2762306a36Sopenharmony_ci#include <linux/percpu.h> 2862306a36Sopenharmony_ci#include <linux/task_work.h> 2962306a36Sopenharmony_ci#include <linux/ima.h> 3062306a36Sopenharmony_ci#include <linux/swap.h> 3162306a36Sopenharmony_ci#include <linux/kmemleak.h> 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci#include <linux/atomic.h> 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci#include "internal.h" 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci/* sysctl tunables... */ 3862306a36Sopenharmony_cistatic struct files_stat_struct files_stat = { 3962306a36Sopenharmony_ci .max_files = NR_FILE 4062306a36Sopenharmony_ci}; 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci/* SLAB cache for file structures */ 4362306a36Sopenharmony_cistatic struct kmem_cache *filp_cachep __read_mostly; 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_cistatic struct percpu_counter nr_files __cacheline_aligned_in_smp; 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci/* Container for backing file with optional real path */ 4862306a36Sopenharmony_cistruct backing_file { 4962306a36Sopenharmony_ci struct file file; 5062306a36Sopenharmony_ci struct path real_path; 5162306a36Sopenharmony_ci}; 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_cistatic inline struct backing_file *backing_file(struct file *f) 5462306a36Sopenharmony_ci{ 5562306a36Sopenharmony_ci return container_of(f, struct backing_file, file); 5662306a36Sopenharmony_ci} 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_cistruct path *backing_file_real_path(struct file *f) 5962306a36Sopenharmony_ci{ 6062306a36Sopenharmony_ci return &backing_file(f)->real_path; 6162306a36Sopenharmony_ci} 6262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(backing_file_real_path); 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_cistatic void file_free_rcu(struct rcu_head *head) 6562306a36Sopenharmony_ci{ 6662306a36Sopenharmony_ci struct file *f = container_of(head, struct file, f_rcuhead); 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci put_cred(f->f_cred); 6962306a36Sopenharmony_ci if (unlikely(f->f_mode & FMODE_BACKING)) 7062306a36Sopenharmony_ci kfree(backing_file(f)); 7162306a36Sopenharmony_ci else 7262306a36Sopenharmony_ci kmem_cache_free(filp_cachep, f); 7362306a36Sopenharmony_ci} 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_cistatic inline void file_free(struct file *f) 7662306a36Sopenharmony_ci{ 7762306a36Sopenharmony_ci security_file_free(f); 7862306a36Sopenharmony_ci if (unlikely(f->f_mode & FMODE_BACKING)) 7962306a36Sopenharmony_ci path_put(backing_file_real_path(f)); 8062306a36Sopenharmony_ci if (likely(!(f->f_mode & FMODE_NOACCOUNT))) 8162306a36Sopenharmony_ci percpu_counter_dec(&nr_files); 8262306a36Sopenharmony_ci call_rcu(&f->f_rcuhead, file_free_rcu); 8362306a36Sopenharmony_ci} 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci/* 8662306a36Sopenharmony_ci * Return the total number of open files in the system 8762306a36Sopenharmony_ci */ 8862306a36Sopenharmony_cistatic long get_nr_files(void) 8962306a36Sopenharmony_ci{ 9062306a36Sopenharmony_ci return percpu_counter_read_positive(&nr_files); 9162306a36Sopenharmony_ci} 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci/* 9462306a36Sopenharmony_ci * Return the maximum number of open files in the system 9562306a36Sopenharmony_ci */ 9662306a36Sopenharmony_ciunsigned long get_max_files(void) 9762306a36Sopenharmony_ci{ 9862306a36Sopenharmony_ci return files_stat.max_files; 9962306a36Sopenharmony_ci} 10062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(get_max_files); 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS) 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci/* 10562306a36Sopenharmony_ci * Handle nr_files sysctl 10662306a36Sopenharmony_ci */ 10762306a36Sopenharmony_cistatic int proc_nr_files(struct ctl_table *table, int write, void *buffer, 10862306a36Sopenharmony_ci size_t *lenp, loff_t *ppos) 10962306a36Sopenharmony_ci{ 11062306a36Sopenharmony_ci files_stat.nr_files = get_nr_files(); 11162306a36Sopenharmony_ci return proc_doulongvec_minmax(table, write, buffer, lenp, ppos); 11262306a36Sopenharmony_ci} 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_cistatic struct ctl_table fs_stat_sysctls[] = { 11562306a36Sopenharmony_ci { 11662306a36Sopenharmony_ci .procname = "file-nr", 11762306a36Sopenharmony_ci .data = &files_stat, 11862306a36Sopenharmony_ci .maxlen = sizeof(files_stat), 11962306a36Sopenharmony_ci .mode = 0444, 12062306a36Sopenharmony_ci .proc_handler = proc_nr_files, 12162306a36Sopenharmony_ci }, 12262306a36Sopenharmony_ci { 12362306a36Sopenharmony_ci .procname = "file-max", 12462306a36Sopenharmony_ci .data = &files_stat.max_files, 12562306a36Sopenharmony_ci .maxlen = sizeof(files_stat.max_files), 12662306a36Sopenharmony_ci .mode = 0644, 12762306a36Sopenharmony_ci .proc_handler = proc_doulongvec_minmax, 12862306a36Sopenharmony_ci .extra1 = SYSCTL_LONG_ZERO, 12962306a36Sopenharmony_ci .extra2 = SYSCTL_LONG_MAX, 13062306a36Sopenharmony_ci }, 13162306a36Sopenharmony_ci { 13262306a36Sopenharmony_ci .procname = "nr_open", 13362306a36Sopenharmony_ci .data = &sysctl_nr_open, 13462306a36Sopenharmony_ci .maxlen = sizeof(unsigned int), 13562306a36Sopenharmony_ci .mode = 0644, 13662306a36Sopenharmony_ci .proc_handler = proc_dointvec_minmax, 13762306a36Sopenharmony_ci .extra1 = &sysctl_nr_open_min, 13862306a36Sopenharmony_ci .extra2 = &sysctl_nr_open_max, 13962306a36Sopenharmony_ci }, 14062306a36Sopenharmony_ci { } 14162306a36Sopenharmony_ci}; 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_cistatic int __init init_fs_stat_sysctls(void) 14462306a36Sopenharmony_ci{ 14562306a36Sopenharmony_ci register_sysctl_init("fs", fs_stat_sysctls); 14662306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_BINFMT_MISC)) { 14762306a36Sopenharmony_ci struct ctl_table_header *hdr; 14862306a36Sopenharmony_ci hdr = register_sysctl_mount_point("fs/binfmt_misc"); 14962306a36Sopenharmony_ci kmemleak_not_leak(hdr); 15062306a36Sopenharmony_ci } 15162306a36Sopenharmony_ci return 0; 15262306a36Sopenharmony_ci} 15362306a36Sopenharmony_cifs_initcall(init_fs_stat_sysctls); 15462306a36Sopenharmony_ci#endif 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_cistatic int init_file(struct file *f, int flags, const struct cred *cred) 15762306a36Sopenharmony_ci{ 15862306a36Sopenharmony_ci int error; 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci f->f_cred = get_cred(cred); 16162306a36Sopenharmony_ci error = security_file_alloc(f); 16262306a36Sopenharmony_ci if (unlikely(error)) { 16362306a36Sopenharmony_ci put_cred(f->f_cred); 16462306a36Sopenharmony_ci return error; 16562306a36Sopenharmony_ci } 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci atomic_long_set(&f->f_count, 1); 16862306a36Sopenharmony_ci rwlock_init(&f->f_owner.lock); 16962306a36Sopenharmony_ci spin_lock_init(&f->f_lock); 17062306a36Sopenharmony_ci mutex_init(&f->f_pos_lock); 17162306a36Sopenharmony_ci f->f_flags = flags; 17262306a36Sopenharmony_ci f->f_mode = OPEN_FMODE(flags); 17362306a36Sopenharmony_ci /* f->f_version: 0 */ 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci return 0; 17662306a36Sopenharmony_ci} 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci/* Find an unused file structure and return a pointer to it. 17962306a36Sopenharmony_ci * Returns an error pointer if some error happend e.g. we over file 18062306a36Sopenharmony_ci * structures limit, run out of memory or operation is not permitted. 18162306a36Sopenharmony_ci * 18262306a36Sopenharmony_ci * Be very careful using this. You are responsible for 18362306a36Sopenharmony_ci * getting write access to any mount that you might assign 18462306a36Sopenharmony_ci * to this filp, if it is opened for write. If this is not 18562306a36Sopenharmony_ci * done, you will imbalance int the mount's writer count 18662306a36Sopenharmony_ci * and a warning at __fput() time. 18762306a36Sopenharmony_ci */ 18862306a36Sopenharmony_cistruct file *alloc_empty_file(int flags, const struct cred *cred) 18962306a36Sopenharmony_ci{ 19062306a36Sopenharmony_ci static long old_max; 19162306a36Sopenharmony_ci struct file *f; 19262306a36Sopenharmony_ci int error; 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci /* 19562306a36Sopenharmony_ci * Privileged users can go above max_files 19662306a36Sopenharmony_ci */ 19762306a36Sopenharmony_ci if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) { 19862306a36Sopenharmony_ci /* 19962306a36Sopenharmony_ci * percpu_counters are inaccurate. Do an expensive check before 20062306a36Sopenharmony_ci * we go and fail. 20162306a36Sopenharmony_ci */ 20262306a36Sopenharmony_ci if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files) 20362306a36Sopenharmony_ci goto over; 20462306a36Sopenharmony_ci } 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_ci f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL); 20762306a36Sopenharmony_ci if (unlikely(!f)) 20862306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_ci error = init_file(f, flags, cred); 21162306a36Sopenharmony_ci if (unlikely(error)) { 21262306a36Sopenharmony_ci kmem_cache_free(filp_cachep, f); 21362306a36Sopenharmony_ci return ERR_PTR(error); 21462306a36Sopenharmony_ci } 21562306a36Sopenharmony_ci 21662306a36Sopenharmony_ci percpu_counter_inc(&nr_files); 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci return f; 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ciover: 22162306a36Sopenharmony_ci /* Ran out of filps - report that */ 22262306a36Sopenharmony_ci if (get_nr_files() > old_max) { 22362306a36Sopenharmony_ci pr_info("VFS: file-max limit %lu reached\n", get_max_files()); 22462306a36Sopenharmony_ci old_max = get_nr_files(); 22562306a36Sopenharmony_ci } 22662306a36Sopenharmony_ci return ERR_PTR(-ENFILE); 22762306a36Sopenharmony_ci} 22862306a36Sopenharmony_ci 22962306a36Sopenharmony_ci/* 23062306a36Sopenharmony_ci * Variant of alloc_empty_file() that doesn't check and modify nr_files. 23162306a36Sopenharmony_ci * 23262306a36Sopenharmony_ci * This is only for kernel internal use, and the allocate file must not be 23362306a36Sopenharmony_ci * installed into file tables or such. 23462306a36Sopenharmony_ci */ 23562306a36Sopenharmony_cistruct file *alloc_empty_file_noaccount(int flags, const struct cred *cred) 23662306a36Sopenharmony_ci{ 23762306a36Sopenharmony_ci struct file *f; 23862306a36Sopenharmony_ci int error; 23962306a36Sopenharmony_ci 24062306a36Sopenharmony_ci f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL); 24162306a36Sopenharmony_ci if (unlikely(!f)) 24262306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ci error = init_file(f, flags, cred); 24562306a36Sopenharmony_ci if (unlikely(error)) { 24662306a36Sopenharmony_ci kmem_cache_free(filp_cachep, f); 24762306a36Sopenharmony_ci return ERR_PTR(error); 24862306a36Sopenharmony_ci } 24962306a36Sopenharmony_ci 25062306a36Sopenharmony_ci f->f_mode |= FMODE_NOACCOUNT; 25162306a36Sopenharmony_ci 25262306a36Sopenharmony_ci return f; 25362306a36Sopenharmony_ci} 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_ci/* 25662306a36Sopenharmony_ci * Variant of alloc_empty_file() that allocates a backing_file container 25762306a36Sopenharmony_ci * and doesn't check and modify nr_files. 25862306a36Sopenharmony_ci * 25962306a36Sopenharmony_ci * This is only for kernel internal use, and the allocate file must not be 26062306a36Sopenharmony_ci * installed into file tables or such. 26162306a36Sopenharmony_ci */ 26262306a36Sopenharmony_cistruct file *alloc_empty_backing_file(int flags, const struct cred *cred) 26362306a36Sopenharmony_ci{ 26462306a36Sopenharmony_ci struct backing_file *ff; 26562306a36Sopenharmony_ci int error; 26662306a36Sopenharmony_ci 26762306a36Sopenharmony_ci ff = kzalloc(sizeof(struct backing_file), GFP_KERNEL); 26862306a36Sopenharmony_ci if (unlikely(!ff)) 26962306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci error = init_file(&ff->file, flags, cred); 27262306a36Sopenharmony_ci if (unlikely(error)) { 27362306a36Sopenharmony_ci kfree(ff); 27462306a36Sopenharmony_ci return ERR_PTR(error); 27562306a36Sopenharmony_ci } 27662306a36Sopenharmony_ci 27762306a36Sopenharmony_ci ff->file.f_mode |= FMODE_BACKING | FMODE_NOACCOUNT; 27862306a36Sopenharmony_ci return &ff->file; 27962306a36Sopenharmony_ci} 28062306a36Sopenharmony_ci 28162306a36Sopenharmony_ci/** 28262306a36Sopenharmony_ci * alloc_file - allocate and initialize a 'struct file' 28362306a36Sopenharmony_ci * 28462306a36Sopenharmony_ci * @path: the (dentry, vfsmount) pair for the new file 28562306a36Sopenharmony_ci * @flags: O_... flags with which the new file will be opened 28662306a36Sopenharmony_ci * @fop: the 'struct file_operations' for the new file 28762306a36Sopenharmony_ci */ 28862306a36Sopenharmony_cistatic struct file *alloc_file(const struct path *path, int flags, 28962306a36Sopenharmony_ci const struct file_operations *fop) 29062306a36Sopenharmony_ci{ 29162306a36Sopenharmony_ci struct file *file; 29262306a36Sopenharmony_ci 29362306a36Sopenharmony_ci file = alloc_empty_file(flags, current_cred()); 29462306a36Sopenharmony_ci if (IS_ERR(file)) 29562306a36Sopenharmony_ci return file; 29662306a36Sopenharmony_ci 29762306a36Sopenharmony_ci file->f_path = *path; 29862306a36Sopenharmony_ci file->f_inode = path->dentry->d_inode; 29962306a36Sopenharmony_ci file->f_mapping = path->dentry->d_inode->i_mapping; 30062306a36Sopenharmony_ci file->f_wb_err = filemap_sample_wb_err(file->f_mapping); 30162306a36Sopenharmony_ci file->f_sb_err = file_sample_sb_err(file); 30262306a36Sopenharmony_ci if (fop->llseek) 30362306a36Sopenharmony_ci file->f_mode |= FMODE_LSEEK; 30462306a36Sopenharmony_ci if ((file->f_mode & FMODE_READ) && 30562306a36Sopenharmony_ci likely(fop->read || fop->read_iter)) 30662306a36Sopenharmony_ci file->f_mode |= FMODE_CAN_READ; 30762306a36Sopenharmony_ci if ((file->f_mode & FMODE_WRITE) && 30862306a36Sopenharmony_ci likely(fop->write || fop->write_iter)) 30962306a36Sopenharmony_ci file->f_mode |= FMODE_CAN_WRITE; 31062306a36Sopenharmony_ci file->f_iocb_flags = iocb_flags(file); 31162306a36Sopenharmony_ci file->f_mode |= FMODE_OPENED; 31262306a36Sopenharmony_ci file->f_op = fop; 31362306a36Sopenharmony_ci if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) 31462306a36Sopenharmony_ci i_readcount_inc(path->dentry->d_inode); 31562306a36Sopenharmony_ci return file; 31662306a36Sopenharmony_ci} 31762306a36Sopenharmony_ci 31862306a36Sopenharmony_cistruct file *alloc_file_pseudo(struct inode *inode, struct vfsmount *mnt, 31962306a36Sopenharmony_ci const char *name, int flags, 32062306a36Sopenharmony_ci const struct file_operations *fops) 32162306a36Sopenharmony_ci{ 32262306a36Sopenharmony_ci static const struct dentry_operations anon_ops = { 32362306a36Sopenharmony_ci .d_dname = simple_dname 32462306a36Sopenharmony_ci }; 32562306a36Sopenharmony_ci struct qstr this = QSTR_INIT(name, strlen(name)); 32662306a36Sopenharmony_ci struct path path; 32762306a36Sopenharmony_ci struct file *file; 32862306a36Sopenharmony_ci 32962306a36Sopenharmony_ci path.dentry = d_alloc_pseudo(mnt->mnt_sb, &this); 33062306a36Sopenharmony_ci if (!path.dentry) 33162306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 33262306a36Sopenharmony_ci if (!mnt->mnt_sb->s_d_op) 33362306a36Sopenharmony_ci d_set_d_op(path.dentry, &anon_ops); 33462306a36Sopenharmony_ci path.mnt = mntget(mnt); 33562306a36Sopenharmony_ci d_instantiate(path.dentry, inode); 33662306a36Sopenharmony_ci file = alloc_file(&path, flags, fops); 33762306a36Sopenharmony_ci if (IS_ERR(file)) { 33862306a36Sopenharmony_ci ihold(inode); 33962306a36Sopenharmony_ci path_put(&path); 34062306a36Sopenharmony_ci } 34162306a36Sopenharmony_ci return file; 34262306a36Sopenharmony_ci} 34362306a36Sopenharmony_ciEXPORT_SYMBOL(alloc_file_pseudo); 34462306a36Sopenharmony_ci 34562306a36Sopenharmony_cistruct file *alloc_file_clone(struct file *base, int flags, 34662306a36Sopenharmony_ci const struct file_operations *fops) 34762306a36Sopenharmony_ci{ 34862306a36Sopenharmony_ci struct file *f = alloc_file(&base->f_path, flags, fops); 34962306a36Sopenharmony_ci if (!IS_ERR(f)) { 35062306a36Sopenharmony_ci path_get(&f->f_path); 35162306a36Sopenharmony_ci f->f_mapping = base->f_mapping; 35262306a36Sopenharmony_ci } 35362306a36Sopenharmony_ci return f; 35462306a36Sopenharmony_ci} 35562306a36Sopenharmony_ci 35662306a36Sopenharmony_ci/* the real guts of fput() - releasing the last reference to file 35762306a36Sopenharmony_ci */ 35862306a36Sopenharmony_cistatic void __fput(struct file *file) 35962306a36Sopenharmony_ci{ 36062306a36Sopenharmony_ci struct dentry *dentry = file->f_path.dentry; 36162306a36Sopenharmony_ci struct vfsmount *mnt = file->f_path.mnt; 36262306a36Sopenharmony_ci struct inode *inode = file->f_inode; 36362306a36Sopenharmony_ci fmode_t mode = file->f_mode; 36462306a36Sopenharmony_ci 36562306a36Sopenharmony_ci if (unlikely(!(file->f_mode & FMODE_OPENED))) 36662306a36Sopenharmony_ci goto out; 36762306a36Sopenharmony_ci 36862306a36Sopenharmony_ci might_sleep(); 36962306a36Sopenharmony_ci 37062306a36Sopenharmony_ci fsnotify_close(file); 37162306a36Sopenharmony_ci /* 37262306a36Sopenharmony_ci * The function eventpoll_release() should be the first called 37362306a36Sopenharmony_ci * in the file cleanup chain. 37462306a36Sopenharmony_ci */ 37562306a36Sopenharmony_ci eventpoll_release(file); 37662306a36Sopenharmony_ci locks_remove_file(file); 37762306a36Sopenharmony_ci 37862306a36Sopenharmony_ci ima_file_free(file); 37962306a36Sopenharmony_ci if (unlikely(file->f_flags & FASYNC)) { 38062306a36Sopenharmony_ci if (file->f_op->fasync) 38162306a36Sopenharmony_ci file->f_op->fasync(-1, file, 0); 38262306a36Sopenharmony_ci } 38362306a36Sopenharmony_ci if (file->f_op->release) 38462306a36Sopenharmony_ci file->f_op->release(inode, file); 38562306a36Sopenharmony_ci if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL && 38662306a36Sopenharmony_ci !(mode & FMODE_PATH))) { 38762306a36Sopenharmony_ci cdev_put(inode->i_cdev); 38862306a36Sopenharmony_ci } 38962306a36Sopenharmony_ci fops_put(file->f_op); 39062306a36Sopenharmony_ci put_pid(file->f_owner.pid); 39162306a36Sopenharmony_ci put_file_access(file); 39262306a36Sopenharmony_ci dput(dentry); 39362306a36Sopenharmony_ci if (unlikely(mode & FMODE_NEED_UNMOUNT)) 39462306a36Sopenharmony_ci dissolve_on_fput(mnt); 39562306a36Sopenharmony_ci mntput(mnt); 39662306a36Sopenharmony_ciout: 39762306a36Sopenharmony_ci file_free(file); 39862306a36Sopenharmony_ci} 39962306a36Sopenharmony_ci 40062306a36Sopenharmony_cistatic LLIST_HEAD(delayed_fput_list); 40162306a36Sopenharmony_cistatic void delayed_fput(struct work_struct *unused) 40262306a36Sopenharmony_ci{ 40362306a36Sopenharmony_ci struct llist_node *node = llist_del_all(&delayed_fput_list); 40462306a36Sopenharmony_ci struct file *f, *t; 40562306a36Sopenharmony_ci 40662306a36Sopenharmony_ci llist_for_each_entry_safe(f, t, node, f_llist) 40762306a36Sopenharmony_ci __fput(f); 40862306a36Sopenharmony_ci} 40962306a36Sopenharmony_ci 41062306a36Sopenharmony_cistatic void ____fput(struct callback_head *work) 41162306a36Sopenharmony_ci{ 41262306a36Sopenharmony_ci __fput(container_of(work, struct file, f_rcuhead)); 41362306a36Sopenharmony_ci} 41462306a36Sopenharmony_ci 41562306a36Sopenharmony_ci/* 41662306a36Sopenharmony_ci * If kernel thread really needs to have the final fput() it has done 41762306a36Sopenharmony_ci * to complete, call this. The only user right now is the boot - we 41862306a36Sopenharmony_ci * *do* need to make sure our writes to binaries on initramfs has 41962306a36Sopenharmony_ci * not left us with opened struct file waiting for __fput() - execve() 42062306a36Sopenharmony_ci * won't work without that. Please, don't add more callers without 42162306a36Sopenharmony_ci * very good reasons; in particular, never call that with locks 42262306a36Sopenharmony_ci * held and never call that from a thread that might need to do 42362306a36Sopenharmony_ci * some work on any kind of umount. 42462306a36Sopenharmony_ci */ 42562306a36Sopenharmony_civoid flush_delayed_fput(void) 42662306a36Sopenharmony_ci{ 42762306a36Sopenharmony_ci delayed_fput(NULL); 42862306a36Sopenharmony_ci} 42962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(flush_delayed_fput); 43062306a36Sopenharmony_ci 43162306a36Sopenharmony_cistatic DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput); 43262306a36Sopenharmony_ci 43362306a36Sopenharmony_civoid fput(struct file *file) 43462306a36Sopenharmony_ci{ 43562306a36Sopenharmony_ci if (atomic_long_dec_and_test(&file->f_count)) { 43662306a36Sopenharmony_ci struct task_struct *task = current; 43762306a36Sopenharmony_ci 43862306a36Sopenharmony_ci if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) { 43962306a36Sopenharmony_ci init_task_work(&file->f_rcuhead, ____fput); 44062306a36Sopenharmony_ci if (!task_work_add(task, &file->f_rcuhead, TWA_RESUME)) 44162306a36Sopenharmony_ci return; 44262306a36Sopenharmony_ci /* 44362306a36Sopenharmony_ci * After this task has run exit_task_work(), 44462306a36Sopenharmony_ci * task_work_add() will fail. Fall through to delayed 44562306a36Sopenharmony_ci * fput to avoid leaking *file. 44662306a36Sopenharmony_ci */ 44762306a36Sopenharmony_ci } 44862306a36Sopenharmony_ci 44962306a36Sopenharmony_ci if (llist_add(&file->f_llist, &delayed_fput_list)) 45062306a36Sopenharmony_ci schedule_delayed_work(&delayed_fput_work, 1); 45162306a36Sopenharmony_ci } 45262306a36Sopenharmony_ci} 45362306a36Sopenharmony_ci 45462306a36Sopenharmony_ci/* 45562306a36Sopenharmony_ci * synchronous analog of fput(); for kernel threads that might be needed 45662306a36Sopenharmony_ci * in some umount() (and thus can't use flush_delayed_fput() without 45762306a36Sopenharmony_ci * risking deadlocks), need to wait for completion of __fput() and know 45862306a36Sopenharmony_ci * for this specific struct file it won't involve anything that would 45962306a36Sopenharmony_ci * need them. Use only if you really need it - at the very least, 46062306a36Sopenharmony_ci * don't blindly convert fput() by kernel thread to that. 46162306a36Sopenharmony_ci */ 46262306a36Sopenharmony_civoid __fput_sync(struct file *file) 46362306a36Sopenharmony_ci{ 46462306a36Sopenharmony_ci if (atomic_long_dec_and_test(&file->f_count)) 46562306a36Sopenharmony_ci __fput(file); 46662306a36Sopenharmony_ci} 46762306a36Sopenharmony_ci 46862306a36Sopenharmony_ciEXPORT_SYMBOL(fput); 46962306a36Sopenharmony_ciEXPORT_SYMBOL(__fput_sync); 47062306a36Sopenharmony_ci 47162306a36Sopenharmony_civoid __init files_init(void) 47262306a36Sopenharmony_ci{ 47362306a36Sopenharmony_ci filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0, 47462306a36Sopenharmony_ci SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT, NULL); 47562306a36Sopenharmony_ci percpu_counter_init(&nr_files, 0, GFP_KERNEL); 47662306a36Sopenharmony_ci} 47762306a36Sopenharmony_ci 47862306a36Sopenharmony_ci/* 47962306a36Sopenharmony_ci * One file with associated inode and dcache is very roughly 1K. Per default 48062306a36Sopenharmony_ci * do not use more than 10% of our memory for files. 48162306a36Sopenharmony_ci */ 48262306a36Sopenharmony_civoid __init files_maxfiles_init(void) 48362306a36Sopenharmony_ci{ 48462306a36Sopenharmony_ci unsigned long n; 48562306a36Sopenharmony_ci unsigned long nr_pages = totalram_pages(); 48662306a36Sopenharmony_ci unsigned long memreserve = (nr_pages - nr_free_pages()) * 3/2; 48762306a36Sopenharmony_ci 48862306a36Sopenharmony_ci memreserve = min(memreserve, nr_pages - 1); 48962306a36Sopenharmony_ci n = ((nr_pages - memreserve) * (PAGE_SIZE / 1024)) / 10; 49062306a36Sopenharmony_ci 49162306a36Sopenharmony_ci files_stat.max_files = max_t(unsigned long, n, NR_FILE); 49262306a36Sopenharmony_ci} 493