xref: /openbmc/linux/fs/file_table.c (revision 021a160a)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/file_table.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
61da177e4SLinus Torvalds  *  Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/string.h>
101da177e4SLinus Torvalds #include <linux/slab.h>
111da177e4SLinus Torvalds #include <linux/file.h>
129f3acc31SAl Viro #include <linux/fdtable.h>
131da177e4SLinus Torvalds #include <linux/init.h>
141da177e4SLinus Torvalds #include <linux/module.h>
151da177e4SLinus Torvalds #include <linux/fs.h>
165970e15dSJeff Layton #include <linux/filelock.h>
171da177e4SLinus Torvalds #include <linux/security.h>
185b825c3aSIngo Molnar #include <linux/cred.h>
191da177e4SLinus Torvalds #include <linux/eventpoll.h>
20ab2af1f5SDipankar Sarma #include <linux/rcupdate.h>
211da177e4SLinus Torvalds #include <linux/mount.h>
2216f7e0feSRandy Dunlap #include <linux/capability.h>
231da177e4SLinus Torvalds #include <linux/cdev.h>
240eeca283SRobert Love #include <linux/fsnotify.h>
25529bf6beSDipankar Sarma #include <linux/sysctl.h>
26529bf6beSDipankar Sarma #include <linux/percpu_counter.h>
276416ccb7SNick Piggin #include <linux/percpu.h>
284a9d4b02SAl Viro #include <linux/task_work.h>
290552f879SAl Viro #include <linux/ima.h>
304248b0daSMel Gorman #include <linux/swap.h>
31a3580ac9SLuis Chamberlain #include <linux/kmemleak.h>
32529bf6beSDipankar Sarma 
3360063497SArun Sharma #include <linux/atomic.h>
341da177e4SLinus Torvalds 
35e81e3f4dSEric Paris #include "internal.h"
36e81e3f4dSEric Paris 
371da177e4SLinus Torvalds /* sysctl tunables... */
38204d5a24SLuis Chamberlain static struct files_stat_struct files_stat = {
391da177e4SLinus Torvalds 	.max_files = NR_FILE
401da177e4SLinus Torvalds };
411da177e4SLinus Torvalds 
42b6b3fdeaSEric Dumazet /* SLAB cache for file structures */
43b6b3fdeaSEric Dumazet static struct kmem_cache *filp_cachep __read_mostly;
44b6b3fdeaSEric Dumazet 
45529bf6beSDipankar Sarma static struct percpu_counter nr_files __cacheline_aligned_in_smp;
461da177e4SLinus Torvalds 
4762d53c4aSAmir Goldstein /* Container for backing file with optional real path */
4862d53c4aSAmir Goldstein struct backing_file {
4962d53c4aSAmir Goldstein 	struct file file;
5062d53c4aSAmir Goldstein 	struct path real_path;
5162d53c4aSAmir Goldstein };
5262d53c4aSAmir Goldstein 
backing_file(struct file * f)5362d53c4aSAmir Goldstein static inline struct backing_file *backing_file(struct file *f)
5462d53c4aSAmir Goldstein {
5562d53c4aSAmir Goldstein 	return container_of(f, struct backing_file, file);
5662d53c4aSAmir Goldstein }
5762d53c4aSAmir Goldstein 
backing_file_real_path(struct file * f)5862d53c4aSAmir Goldstein struct path *backing_file_real_path(struct file *f)
5962d53c4aSAmir Goldstein {
6062d53c4aSAmir Goldstein 	return &backing_file(f)->real_path;
6162d53c4aSAmir Goldstein }
6262d53c4aSAmir Goldstein EXPORT_SYMBOL_GPL(backing_file_real_path);
6362d53c4aSAmir Goldstein 
file_free_rcu(struct rcu_head * head)645c33b183SAl Viro static void file_free_rcu(struct rcu_head *head)
65ab2af1f5SDipankar Sarma {
66e87f2c26SAl Viro 	struct file *f = container_of(head, struct file, f_rcuhead);
67d76b0d9bSDavid Howells 
68d76b0d9bSDavid Howells 	put_cred(f->f_cred);
6962d53c4aSAmir Goldstein 	if (unlikely(f->f_mode & FMODE_BACKING))
7062d53c4aSAmir Goldstein 		kfree(backing_file(f));
7162d53c4aSAmir Goldstein 	else
72ab2af1f5SDipankar Sarma 		kmem_cache_free(filp_cachep, f);
73ab2af1f5SDipankar Sarma }
74ab2af1f5SDipankar Sarma 
file_free(struct file * f)751da177e4SLinus Torvalds static inline void file_free(struct file *f)
761da177e4SLinus Torvalds {
77e8cff84fSAl Viro 	security_file_free(f);
7862d53c4aSAmir Goldstein 	if (unlikely(f->f_mode & FMODE_BACKING))
7962d53c4aSAmir Goldstein 		path_put(backing_file_real_path(f));
8062d53c4aSAmir Goldstein 	if (likely(!(f->f_mode & FMODE_NOACCOUNT)))
81529bf6beSDipankar Sarma 		percpu_counter_dec(&nr_files);
82e87f2c26SAl Viro 	call_rcu(&f->f_rcuhead, file_free_rcu);
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds 
85529bf6beSDipankar Sarma /*
86529bf6beSDipankar Sarma  * Return the total number of open files in the system
87529bf6beSDipankar Sarma  */
get_nr_files(void)88518de9b3SEric Dumazet static long get_nr_files(void)
89529bf6beSDipankar Sarma {
90529bf6beSDipankar Sarma 	return percpu_counter_read_positive(&nr_files);
91529bf6beSDipankar Sarma }
92529bf6beSDipankar Sarma 
93529bf6beSDipankar Sarma /*
94529bf6beSDipankar Sarma  * Return the maximum number of open files in the system
95529bf6beSDipankar Sarma  */
get_max_files(void)96518de9b3SEric Dumazet unsigned long get_max_files(void)
97529bf6beSDipankar Sarma {
98529bf6beSDipankar Sarma 	return files_stat.max_files;
99529bf6beSDipankar Sarma }
100529bf6beSDipankar Sarma EXPORT_SYMBOL_GPL(get_max_files);
101529bf6beSDipankar Sarma 
102204d5a24SLuis Chamberlain #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
103204d5a24SLuis Chamberlain 
104529bf6beSDipankar Sarma /*
105529bf6beSDipankar Sarma  * Handle nr_files sysctl
106529bf6beSDipankar Sarma  */
proc_nr_files(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)107204d5a24SLuis Chamberlain static int proc_nr_files(struct ctl_table *table, int write, void *buffer,
108204d5a24SLuis Chamberlain 			 size_t *lenp, loff_t *ppos)
109529bf6beSDipankar Sarma {
110529bf6beSDipankar Sarma 	files_stat.nr_files = get_nr_files();
111518de9b3SEric Dumazet 	return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
112529bf6beSDipankar Sarma }
113204d5a24SLuis Chamberlain 
114204d5a24SLuis Chamberlain static struct ctl_table fs_stat_sysctls[] = {
115529bf6beSDipankar Sarma 	{
116204d5a24SLuis Chamberlain 		.procname	= "file-nr",
117204d5a24SLuis Chamberlain 		.data		= &files_stat,
118204d5a24SLuis Chamberlain 		.maxlen		= sizeof(files_stat),
119204d5a24SLuis Chamberlain 		.mode		= 0444,
120204d5a24SLuis Chamberlain 		.proc_handler	= proc_nr_files,
121204d5a24SLuis Chamberlain 	},
122204d5a24SLuis Chamberlain 	{
123204d5a24SLuis Chamberlain 		.procname	= "file-max",
124204d5a24SLuis Chamberlain 		.data		= &files_stat.max_files,
125204d5a24SLuis Chamberlain 		.maxlen		= sizeof(files_stat.max_files),
126204d5a24SLuis Chamberlain 		.mode		= 0644,
127204d5a24SLuis Chamberlain 		.proc_handler	= proc_doulongvec_minmax,
128204d5a24SLuis Chamberlain 		.extra1		= SYSCTL_LONG_ZERO,
129204d5a24SLuis Chamberlain 		.extra2		= SYSCTL_LONG_MAX,
130204d5a24SLuis Chamberlain 	},
131204d5a24SLuis Chamberlain 	{
132204d5a24SLuis Chamberlain 		.procname	= "nr_open",
133204d5a24SLuis Chamberlain 		.data		= &sysctl_nr_open,
134204d5a24SLuis Chamberlain 		.maxlen		= sizeof(unsigned int),
135204d5a24SLuis Chamberlain 		.mode		= 0644,
136204d5a24SLuis Chamberlain 		.proc_handler	= proc_dointvec_minmax,
137204d5a24SLuis Chamberlain 		.extra1		= &sysctl_nr_open_min,
138204d5a24SLuis Chamberlain 		.extra2		= &sysctl_nr_open_max,
139204d5a24SLuis Chamberlain 	},
140204d5a24SLuis Chamberlain 	{ }
141204d5a24SLuis Chamberlain };
142204d5a24SLuis Chamberlain 
init_fs_stat_sysctls(void)143204d5a24SLuis Chamberlain static int __init init_fs_stat_sysctls(void)
144204d5a24SLuis Chamberlain {
145204d5a24SLuis Chamberlain 	register_sysctl_init("fs", fs_stat_sysctls);
146a3580ac9SLuis Chamberlain 	if (IS_ENABLED(CONFIG_BINFMT_MISC)) {
147a3580ac9SLuis Chamberlain 		struct ctl_table_header *hdr;
148a3580ac9SLuis Chamberlain 		hdr = register_sysctl_mount_point("fs/binfmt_misc");
149a3580ac9SLuis Chamberlain 		kmemleak_not_leak(hdr);
150a3580ac9SLuis Chamberlain 	}
151204d5a24SLuis Chamberlain 	return 0;
152529bf6beSDipankar Sarma }
153204d5a24SLuis Chamberlain fs_initcall(init_fs_stat_sysctls);
154529bf6beSDipankar Sarma #endif
155529bf6beSDipankar Sarma 
init_file(struct file * f,int flags,const struct cred * cred)1568a05a8c3SAmir Goldstein static int init_file(struct file *f, int flags, const struct cred *cred)
1571da177e4SLinus Torvalds {
1581afc99beSAl Viro 	int error;
1591da177e4SLinus Torvalds 
16078d29788STetsuo Handa 	f->f_cred = get_cred(cred);
1611afc99beSAl Viro 	error = security_file_alloc(f);
1621afc99beSAl Viro 	if (unlikely(error)) {
163dff745c1SAmir Goldstein 		put_cred(f->f_cred);
1648a05a8c3SAmir Goldstein 		return error;
1651afc99beSAl Viro 	}
166af4d2ecbSKirill Korotaev 
167516e0cc5SAl Viro 	atomic_long_set(&f->f_count, 1);
1685a6b7951SBenjamin LaHaise 	rwlock_init(&f->f_owner.lock);
16968499914SJonathan Corbet 	spin_lock_init(&f->f_lock);
1709c225f26SLinus Torvalds 	mutex_init(&f->f_pos_lock);
171ea73ea72SAl Viro 	f->f_flags = flags;
172ea73ea72SAl Viro 	f->f_mode = OPEN_FMODE(flags);
1735a6b7951SBenjamin LaHaise 	/* f->f_version: 0 */
174d3b1084dSMiklos Szeredi 
1758a05a8c3SAmir Goldstein 	return 0;
176d3b1084dSMiklos Szeredi }
177d3b1084dSMiklos Szeredi 
178d3b1084dSMiklos Szeredi /* Find an unused file structure and return a pointer to it.
179d3b1084dSMiklos Szeredi  * Returns an error pointer if some error happend e.g. we over file
180d3b1084dSMiklos Szeredi  * structures limit, run out of memory or operation is not permitted.
181d3b1084dSMiklos Szeredi  *
182d3b1084dSMiklos Szeredi  * Be very careful using this.  You are responsible for
183d3b1084dSMiklos Szeredi  * getting write access to any mount that you might assign
184d3b1084dSMiklos Szeredi  * to this filp, if it is opened for write.  If this is not
185d3b1084dSMiklos Szeredi  * done, you will imbalance int the mount's writer count
186d3b1084dSMiklos Szeredi  * and a warning at __fput() time.
187d3b1084dSMiklos Szeredi  */
alloc_empty_file(int flags,const struct cred * cred)188d3b1084dSMiklos Szeredi struct file *alloc_empty_file(int flags, const struct cred *cred)
189d3b1084dSMiklos Szeredi {
190d3b1084dSMiklos Szeredi 	static long old_max;
191d3b1084dSMiklos Szeredi 	struct file *f;
1928a05a8c3SAmir Goldstein 	int error;
193d3b1084dSMiklos Szeredi 
194d3b1084dSMiklos Szeredi 	/*
195d3b1084dSMiklos Szeredi 	 * Privileged users can go above max_files
196d3b1084dSMiklos Szeredi 	 */
197d3b1084dSMiklos Szeredi 	if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
198d3b1084dSMiklos Szeredi 		/*
199d3b1084dSMiklos Szeredi 		 * percpu_counters are inaccurate.  Do an expensive check before
200d3b1084dSMiklos Szeredi 		 * we go and fail.
201d3b1084dSMiklos Szeredi 		 */
202d3b1084dSMiklos Szeredi 		if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
203d3b1084dSMiklos Szeredi 			goto over;
204d3b1084dSMiklos Szeredi 	}
205d3b1084dSMiklos Szeredi 
2068a05a8c3SAmir Goldstein 	f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
2078a05a8c3SAmir Goldstein 	if (unlikely(!f))
2088a05a8c3SAmir Goldstein 		return ERR_PTR(-ENOMEM);
2098a05a8c3SAmir Goldstein 
2108a05a8c3SAmir Goldstein 	error = init_file(f, flags, cred);
211dff745c1SAmir Goldstein 	if (unlikely(error)) {
212dff745c1SAmir Goldstein 		kmem_cache_free(filp_cachep, f);
2138a05a8c3SAmir Goldstein 		return ERR_PTR(error);
214dff745c1SAmir Goldstein 	}
2158a05a8c3SAmir Goldstein 
216e8cff84fSAl Viro 	percpu_counter_inc(&nr_files);
217d3b1084dSMiklos Szeredi 
2181da177e4SLinus Torvalds 	return f;
2191da177e4SLinus Torvalds 
220af4d2ecbSKirill Korotaev over:
2211da177e4SLinus Torvalds 	/* Ran out of filps - report that */
222529bf6beSDipankar Sarma 	if (get_nr_files() > old_max) {
223518de9b3SEric Dumazet 		pr_info("VFS: file-max limit %lu reached\n", get_max_files());
224529bf6beSDipankar Sarma 		old_max = get_nr_files();
2251da177e4SLinus Torvalds 	}
2261afc99beSAl Viro 	return ERR_PTR(-ENFILE);
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
229d3b1084dSMiklos Szeredi /*
230d3b1084dSMiklos Szeredi  * Variant of alloc_empty_file() that doesn't check and modify nr_files.
231d3b1084dSMiklos Szeredi  *
2328a05a8c3SAmir Goldstein  * This is only for kernel internal use, and the allocate file must not be
2338a05a8c3SAmir Goldstein  * installed into file tables or such.
234d3b1084dSMiklos Szeredi  */
alloc_empty_file_noaccount(int flags,const struct cred * cred)235d3b1084dSMiklos Szeredi struct file *alloc_empty_file_noaccount(int flags, const struct cred *cred)
236d3b1084dSMiklos Szeredi {
2378a05a8c3SAmir Goldstein 	struct file *f;
2388a05a8c3SAmir Goldstein 	int error;
239d3b1084dSMiklos Szeredi 
2408a05a8c3SAmir Goldstein 	f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
2418a05a8c3SAmir Goldstein 	if (unlikely(!f))
2428a05a8c3SAmir Goldstein 		return ERR_PTR(-ENOMEM);
2438a05a8c3SAmir Goldstein 
2448a05a8c3SAmir Goldstein 	error = init_file(f, flags, cred);
245dff745c1SAmir Goldstein 	if (unlikely(error)) {
246dff745c1SAmir Goldstein 		kmem_cache_free(filp_cachep, f);
2478a05a8c3SAmir Goldstein 		return ERR_PTR(error);
248dff745c1SAmir Goldstein 	}
2498a05a8c3SAmir Goldstein 
250d3b1084dSMiklos Szeredi 	f->f_mode |= FMODE_NOACCOUNT;
251d3b1084dSMiklos Szeredi 
252d3b1084dSMiklos Szeredi 	return f;
253d3b1084dSMiklos Szeredi }
254d3b1084dSMiklos Szeredi 
25562d53c4aSAmir Goldstein /*
25662d53c4aSAmir Goldstein  * Variant of alloc_empty_file() that allocates a backing_file container
25762d53c4aSAmir Goldstein  * and doesn't check and modify nr_files.
25862d53c4aSAmir Goldstein  *
25962d53c4aSAmir Goldstein  * This is only for kernel internal use, and the allocate file must not be
26062d53c4aSAmir Goldstein  * installed into file tables or such.
26162d53c4aSAmir Goldstein  */
alloc_empty_backing_file(int flags,const struct cred * cred)26262d53c4aSAmir Goldstein struct file *alloc_empty_backing_file(int flags, const struct cred *cred)
26362d53c4aSAmir Goldstein {
26462d53c4aSAmir Goldstein 	struct backing_file *ff;
26562d53c4aSAmir Goldstein 	int error;
26662d53c4aSAmir Goldstein 
26762d53c4aSAmir Goldstein 	ff = kzalloc(sizeof(struct backing_file), GFP_KERNEL);
26862d53c4aSAmir Goldstein 	if (unlikely(!ff))
26962d53c4aSAmir Goldstein 		return ERR_PTR(-ENOMEM);
27062d53c4aSAmir Goldstein 
27162d53c4aSAmir Goldstein 	error = init_file(&ff->file, flags, cred);
272dff745c1SAmir Goldstein 	if (unlikely(error)) {
273dff745c1SAmir Goldstein 		kfree(ff);
27462d53c4aSAmir Goldstein 		return ERR_PTR(error);
275dff745c1SAmir Goldstein 	}
27662d53c4aSAmir Goldstein 
27762d53c4aSAmir Goldstein 	ff->file.f_mode |= FMODE_BACKING | FMODE_NOACCOUNT;
27862d53c4aSAmir Goldstein 	return &ff->file;
27962d53c4aSAmir Goldstein }
28062d53c4aSAmir Goldstein 
281ce8d2cdfSDave Hansen /**
282ce8d2cdfSDave Hansen  * alloc_file - allocate and initialize a 'struct file'
283a457606aSEric Biggers  *
284a457606aSEric Biggers  * @path: the (dentry, vfsmount) pair for the new file
285c9c554f2SAl Viro  * @flags: O_... flags with which the new file will be opened
286ce8d2cdfSDave Hansen  * @fop: the 'struct file_operations' for the new file
287ce8d2cdfSDave Hansen  */
alloc_file(const struct path * path,int flags,const struct file_operations * fop)288ee1904baSAl Viro static struct file *alloc_file(const struct path *path, int flags,
2892c48b9c4SAl Viro 		const struct file_operations *fop)
290ce8d2cdfSDave Hansen {
291ce8d2cdfSDave Hansen 	struct file *file;
292ce8d2cdfSDave Hansen 
293ea73ea72SAl Viro 	file = alloc_empty_file(flags, current_cred());
2941afc99beSAl Viro 	if (IS_ERR(file))
29539b65252SAnatol Pomozov 		return file;
296ce8d2cdfSDave Hansen 
2972c48b9c4SAl Viro 	file->f_path = *path;
298dd37978cSAl Viro 	file->f_inode = path->dentry->d_inode;
2992c48b9c4SAl Viro 	file->f_mapping = path->dentry->d_inode->i_mapping;
3005660e13dSJeff Layton 	file->f_wb_err = filemap_sample_wb_err(file->f_mapping);
301735e4ae5SJeff Layton 	file->f_sb_err = file_sample_sb_err(file);
302868941b1SJason A. Donenfeld 	if (fop->llseek)
303e7478158SJason A. Donenfeld 		file->f_mode |= FMODE_LSEEK;
304c9c554f2SAl Viro 	if ((file->f_mode & FMODE_READ) &&
30584363182SAl Viro 	     likely(fop->read || fop->read_iter))
306c9c554f2SAl Viro 		file->f_mode |= FMODE_CAN_READ;
307c9c554f2SAl Viro 	if ((file->f_mode & FMODE_WRITE) &&
30884363182SAl Viro 	     likely(fop->write || fop->write_iter))
309c9c554f2SAl Viro 		file->f_mode |= FMODE_CAN_WRITE;
310164f4064SAl Viro 	file->f_iocb_flags = iocb_flags(file);
311f5d11409SAl Viro 	file->f_mode |= FMODE_OPENED;
312ce8d2cdfSDave Hansen 	file->f_op = fop;
313c9c554f2SAl Viro 	if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
314890275b5SMimi Zohar 		i_readcount_inc(path->dentry->d_inode);
3153d1e4631SAl Viro 	return file;
316ce8d2cdfSDave Hansen }
317ce8d2cdfSDave Hansen 
alloc_file_pseudo(struct inode * inode,struct vfsmount * mnt,const char * name,int flags,const struct file_operations * fops)318d93aa9d8SAl Viro struct file *alloc_file_pseudo(struct inode *inode, struct vfsmount *mnt,
319d93aa9d8SAl Viro 				const char *name, int flags,
320d93aa9d8SAl Viro 				const struct file_operations *fops)
321d93aa9d8SAl Viro {
322d93aa9d8SAl Viro 	static const struct dentry_operations anon_ops = {
323d93aa9d8SAl Viro 		.d_dname = simple_dname
324d93aa9d8SAl Viro 	};
325d93aa9d8SAl Viro 	struct qstr this = QSTR_INIT(name, strlen(name));
326d93aa9d8SAl Viro 	struct path path;
327d93aa9d8SAl Viro 	struct file *file;
328d93aa9d8SAl Viro 
329d93aa9d8SAl Viro 	path.dentry = d_alloc_pseudo(mnt->mnt_sb, &this);
330d93aa9d8SAl Viro 	if (!path.dentry)
331d93aa9d8SAl Viro 		return ERR_PTR(-ENOMEM);
332d93aa9d8SAl Viro 	if (!mnt->mnt_sb->s_d_op)
333d93aa9d8SAl Viro 		d_set_d_op(path.dentry, &anon_ops);
334d93aa9d8SAl Viro 	path.mnt = mntget(mnt);
335d93aa9d8SAl Viro 	d_instantiate(path.dentry, inode);
336b6509f6aSMel Gorman 	file = alloc_file(&path, flags, fops);
337d93aa9d8SAl Viro 	if (IS_ERR(file)) {
338d93aa9d8SAl Viro 		ihold(inode);
339d93aa9d8SAl Viro 		path_put(&path);
340d93aa9d8SAl Viro 	}
341d93aa9d8SAl Viro 	return file;
342d93aa9d8SAl Viro }
343d93aa9d8SAl Viro EXPORT_SYMBOL(alloc_file_pseudo);
344d93aa9d8SAl Viro 
alloc_file_clone(struct file * base,int flags,const struct file_operations * fops)345183266f2SAl Viro struct file *alloc_file_clone(struct file *base, int flags,
346183266f2SAl Viro 				const struct file_operations *fops)
347183266f2SAl Viro {
348183266f2SAl Viro 	struct file *f = alloc_file(&base->f_path, flags, fops);
349183266f2SAl Viro 	if (!IS_ERR(f)) {
350183266f2SAl Viro 		path_get(&f->f_path);
351183266f2SAl Viro 		f->f_mapping = base->f_mapping;
352183266f2SAl Viro 	}
353183266f2SAl Viro 	return f;
354183266f2SAl Viro }
355183266f2SAl Viro 
356d7065da0SAl Viro /* the real guts of fput() - releasing the last reference to file
3571da177e4SLinus Torvalds  */
__fput(struct file * file)358d7065da0SAl Viro static void __fput(struct file *file)
3591da177e4SLinus Torvalds {
3600f7fc9e4SJosef "Jeff" Sipek 	struct dentry *dentry = file->f_path.dentry;
3610f7fc9e4SJosef "Jeff" Sipek 	struct vfsmount *mnt = file->f_path.mnt;
362c77ceceeSDavid Howells 	struct inode *inode = file->f_inode;
363a07b2000SAl Viro 	fmode_t mode = file->f_mode;
3641da177e4SLinus Torvalds 
3654d27f326SAl Viro 	if (unlikely(!(file->f_mode & FMODE_OPENED)))
3664d27f326SAl Viro 		goto out;
3674d27f326SAl Viro 
3681da177e4SLinus Torvalds 	might_sleep();
3690eeca283SRobert Love 
3700eeca283SRobert Love 	fsnotify_close(file);
3711da177e4SLinus Torvalds 	/*
3721da177e4SLinus Torvalds 	 * The function eventpoll_release() should be the first called
3731da177e4SLinus Torvalds 	 * in the file cleanup chain.
3741da177e4SLinus Torvalds 	 */
3751da177e4SLinus Torvalds 	eventpoll_release(file);
37678ed8a13SJeff Layton 	locks_remove_file(file);
3771da177e4SLinus Torvalds 
378bb02b186SMimi Zohar 	ima_file_free(file);
379233e70f4SAl Viro 	if (unlikely(file->f_flags & FASYNC)) {
38072c2d531SAl Viro 		if (file->f_op->fasync)
381233e70f4SAl Viro 			file->f_op->fasync(-1, file, 0);
382233e70f4SAl Viro 	}
38372c2d531SAl Viro 	if (file->f_op->release)
3841da177e4SLinus Torvalds 		file->f_op->release(inode, file);
38560ed8cf7SMiklos Szeredi 	if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL &&
386a07b2000SAl Viro 		     !(mode & FMODE_PATH))) {
3871da177e4SLinus Torvalds 		cdev_put(inode->i_cdev);
38860ed8cf7SMiklos Szeredi 	}
3891da177e4SLinus Torvalds 	fops_put(file->f_op);
390609d7fa9SEric W. Biederman 	put_pid(file->f_owner.pid);
391d6da19c9SAmir Goldstein 	put_file_access(file);
3921da177e4SLinus Torvalds 	dput(dentry);
393a07b2000SAl Viro 	if (unlikely(mode & FMODE_NEED_UNMOUNT))
394a07b2000SAl Viro 		dissolve_on_fput(mnt);
3951da177e4SLinus Torvalds 	mntput(mnt);
3964d27f326SAl Viro out:
3974d27f326SAl Viro 	file_free(file);
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
4004f5e65a1SOleg Nesterov static LLIST_HEAD(delayed_fput_list);
delayed_fput(struct work_struct * unused)4014a9d4b02SAl Viro static void delayed_fput(struct work_struct *unused)
4024a9d4b02SAl Viro {
4034f5e65a1SOleg Nesterov 	struct llist_node *node = llist_del_all(&delayed_fput_list);
404b9ea557eSByungchul Park 	struct file *f, *t;
4054f5e65a1SOleg Nesterov 
406e87f2c26SAl Viro 	llist_for_each_entry_safe(f, t, node, f_llist)
407b9ea557eSByungchul Park 		__fput(f);
4084a9d4b02SAl Viro }
4094a9d4b02SAl Viro 
____fput(struct callback_head * work)4104a9d4b02SAl Viro static void ____fput(struct callback_head *work)
4114a9d4b02SAl Viro {
412e87f2c26SAl Viro 	__fput(container_of(work, struct file, f_rcuhead));
4134a9d4b02SAl Viro }
4144a9d4b02SAl Viro 
4154a9d4b02SAl Viro /*
4164a9d4b02SAl Viro  * If kernel thread really needs to have the final fput() it has done
4174a9d4b02SAl Viro  * to complete, call this.  The only user right now is the boot - we
4184a9d4b02SAl Viro  * *do* need to make sure our writes to binaries on initramfs has
4194a9d4b02SAl Viro  * not left us with opened struct file waiting for __fput() - execve()
4204a9d4b02SAl Viro  * won't work without that.  Please, don't add more callers without
4214a9d4b02SAl Viro  * very good reasons; in particular, never call that with locks
4224a9d4b02SAl Viro  * held and never call that from a thread that might need to do
4234a9d4b02SAl Viro  * some work on any kind of umount.
4244a9d4b02SAl Viro  */
flush_delayed_fput(void)4254a9d4b02SAl Viro void flush_delayed_fput(void)
4264a9d4b02SAl Viro {
4274a9d4b02SAl Viro 	delayed_fput(NULL);
4284a9d4b02SAl Viro }
4297239a40cSTrond Myklebust EXPORT_SYMBOL_GPL(flush_delayed_fput);
4304a9d4b02SAl Viro 
431c7314d74SAl Viro static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
4324a9d4b02SAl Viro 
fput(struct file * file)43381132a39SGou Hao void fput(struct file *file)
434d7065da0SAl Viro {
43581132a39SGou Hao 	if (atomic_long_dec_and_test(&file->f_count)) {
4364a9d4b02SAl Viro 		struct task_struct *task = current;
437e7b2c406SOleg Nesterov 
438e7b2c406SOleg Nesterov 		if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
439e87f2c26SAl Viro 			init_task_work(&file->f_rcuhead, ____fput);
440e87f2c26SAl Viro 			if (!task_work_add(task, &file->f_rcuhead, TWA_RESUME))
441e7b2c406SOleg Nesterov 				return;
44264372501SAndrew Morton 			/*
44364372501SAndrew Morton 			 * After this task has run exit_task_work(),
444be49b30aSAndrew Morton 			 * task_work_add() will fail.  Fall through to delayed
44564372501SAndrew Morton 			 * fput to avoid leaking *file.
44664372501SAndrew Morton 			 */
447e7b2c406SOleg Nesterov 		}
4484f5e65a1SOleg Nesterov 
449e87f2c26SAl Viro 		if (llist_add(&file->f_llist, &delayed_fput_list))
450c7314d74SAl Viro 			schedule_delayed_work(&delayed_fput_work, 1);
4514a9d4b02SAl Viro 	}
4524a9d4b02SAl Viro }
4534a9d4b02SAl Viro 
4544a9d4b02SAl Viro /*
4554a9d4b02SAl Viro  * synchronous analog of fput(); for kernel threads that might be needed
4564a9d4b02SAl Viro  * in some umount() (and thus can't use flush_delayed_fput() without
4574a9d4b02SAl Viro  * risking deadlocks), need to wait for completion of __fput() and know
4584a9d4b02SAl Viro  * for this specific struct file it won't involve anything that would
4594a9d4b02SAl Viro  * need them.  Use only if you really need it - at the very least,
4604a9d4b02SAl Viro  * don't blindly convert fput() by kernel thread to that.
4614a9d4b02SAl Viro  */
__fput_sync(struct file * file)4624a9d4b02SAl Viro void __fput_sync(struct file *file)
4634a9d4b02SAl Viro {
464*021a160aSLinus Torvalds 	if (atomic_long_dec_and_test(&file->f_count))
465d7065da0SAl Viro 		__fput(file);
466d7065da0SAl Viro }
467d7065da0SAl Viro 
468d7065da0SAl Viro EXPORT_SYMBOL(fput);
469f0043206STrond Myklebust EXPORT_SYMBOL(__fput_sync);
470d7065da0SAl Viro 
files_init(void)4714248b0daSMel Gorman void __init files_init(void)
4721da177e4SLinus Torvalds {
473b6b3fdeaSEric Dumazet 	filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
474f3f7c093SShakeel Butt 			SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT, NULL);
4754248b0daSMel Gorman 	percpu_counter_init(&nr_files, 0, GFP_KERNEL);
4764248b0daSMel Gorman }
477b6b3fdeaSEric Dumazet 
478b6b3fdeaSEric Dumazet /*
4794248b0daSMel Gorman  * One file with associated inode and dcache is very roughly 1K. Per default
4804248b0daSMel Gorman  * do not use more than 10% of our memory for files.
4811da177e4SLinus Torvalds  */
files_maxfiles_init(void)4824248b0daSMel Gorman void __init files_maxfiles_init(void)
4834248b0daSMel Gorman {
4844248b0daSMel Gorman 	unsigned long n;
485ca79b0c2SArun KS 	unsigned long nr_pages = totalram_pages();
4863d6357deSArun KS 	unsigned long memreserve = (nr_pages - nr_free_pages()) * 3/2;
4871da177e4SLinus Torvalds 
4883d6357deSArun KS 	memreserve = min(memreserve, nr_pages - 1);
4893d6357deSArun KS 	n = ((nr_pages - memreserve) * (PAGE_SIZE / 1024)) / 10;
4904248b0daSMel Gorman 
491518de9b3SEric Dumazet 	files_stat.max_files = max_t(unsigned long, n, NR_FILE);
4921da177e4SLinus Torvalds }
493