blob: d58c11a6832fcb199ed0ed6ad8295757fc934533 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/file_table.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
6 */
7
8#include <linux/string.h>
9#include <linux/slab.h>
10#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040011#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
13#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/fs.h>
15#include <linux/security.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010016#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/eventpoll.h>
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070018#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mount.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080020#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/cdev.h>
Robert Love0eeca282005-07-12 17:06:03 -040022#include <linux/fsnotify.h>
Dipankar Sarma529bf6b2006-03-07 21:55:35 -080023#include <linux/sysctl.h>
24#include <linux/percpu_counter.h>
Nick Piggin6416ccb2010-08-18 04:37:38 +100025#include <linux/percpu.h>
Al Viro4a9d4b02012-06-24 09:56:45 +040026#include <linux/task_work.h>
Al Viro0552f872009-12-16 04:53:03 -050027#include <linux/ima.h>
Mel Gorman4248b0d2015-08-06 15:46:20 -070028#include <linux/swap.h>
Dipankar Sarma529bf6b2006-03-07 21:55:35 -080029
Arun Sharma600634972011-07-26 16:09:06 -070030#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Eric Parise81e3f42009-12-04 15:47:36 -050032#include "internal.h"
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* sysctl tunables... */
35struct files_stat_struct files_stat = {
36 .max_files = NR_FILE
37};
38
Eric Dumazetb6b3fde2008-12-10 09:35:45 -080039/* SLAB cache for file structures */
40static struct kmem_cache *filp_cachep __read_mostly;
41
Dipankar Sarma529bf6b2006-03-07 21:55:35 -080042static struct percpu_counter nr_files __cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Al Viro5c33b182012-07-20 23:05:59 +040044static void file_free_rcu(struct rcu_head *head)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070045{
David Howellsd76b0d92008-11-14 10:39:25 +110046 struct file *f = container_of(head, struct file, f_u.fu_rcuhead);
47
48 put_cred(f->f_cred);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070049 kmem_cache_free(filp_cachep, f);
50}
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static inline void file_free(struct file *f)
53{
Al Viroe8cff842018-07-09 11:24:21 -040054 security_file_free(f);
Miklos Szeredid3b10842018-07-18 15:44:40 +020055 if (!(f->f_mode & FMODE_NOACCOUNT))
56 percpu_counter_dec(&nr_files);
Eric Dumazet2f512012005-10-30 15:02:16 -080057 call_rcu(&f->f_u.fu_rcuhead, file_free_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Dipankar Sarma529bf6b2006-03-07 21:55:35 -080060/*
61 * Return the total number of open files in the system
62 */
Eric Dumazet518de9b2010-10-26 14:22:44 -070063static long get_nr_files(void)
Dipankar Sarma529bf6b2006-03-07 21:55:35 -080064{
65 return percpu_counter_read_positive(&nr_files);
66}
67
68/*
69 * Return the maximum number of open files in the system
70 */
Eric Dumazet518de9b2010-10-26 14:22:44 -070071unsigned long get_max_files(void)
Dipankar Sarma529bf6b2006-03-07 21:55:35 -080072{
73 return files_stat.max_files;
74}
75EXPORT_SYMBOL_GPL(get_max_files);
76
77/*
78 * Handle nr_files sysctl
79 */
80#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
Joe Perches1f7e0612014-06-06 14:38:05 -070081int proc_nr_files(struct ctl_table *table, int write,
Dipankar Sarma529bf6b2006-03-07 21:55:35 -080082 void __user *buffer, size_t *lenp, loff_t *ppos)
83{
84 files_stat.nr_files = get_nr_files();
Eric Dumazet518de9b2010-10-26 14:22:44 -070085 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
Dipankar Sarma529bf6b2006-03-07 21:55:35 -080086}
87#else
Joe Perches1f7e0612014-06-06 14:38:05 -070088int proc_nr_files(struct ctl_table *table, int write,
Dipankar Sarma529bf6b2006-03-07 21:55:35 -080089 void __user *buffer, size_t *lenp, loff_t *ppos)
90{
91 return -ENOSYS;
92}
93#endif
94
Miklos Szeredid3b10842018-07-18 15:44:40 +020095static struct file *__alloc_file(int flags, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Al Viro1afc99b2013-02-14 20:41:04 -050097 struct file *f;
98 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Denis Cheng4975e452007-10-16 23:26:19 -0700100 f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
Al Viro1afc99b2013-02-14 20:41:04 -0500101 if (unlikely(!f))
102 return ERR_PTR(-ENOMEM);
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -0700103
Tetsuo Handa78d29782011-02-04 18:13:24 +0000104 f->f_cred = get_cred(cred);
Al Viro1afc99b2013-02-14 20:41:04 -0500105 error = security_file_alloc(f);
106 if (unlikely(error)) {
Al Viroe8cff842018-07-09 11:24:21 -0400107 file_free_rcu(&f->f_u.fu_rcuhead);
Al Viro1afc99b2013-02-14 20:41:04 -0500108 return ERR_PTR(error);
109 }
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -0700110
Al Viro516e0cc2008-07-26 00:39:17 -0400111 atomic_long_set(&f->f_count, 1);
Benjamin LaHaise5a6b7952006-03-23 03:01:03 -0800112 rwlock_init(&f->f_owner.lock);
Jonathan Corbet68499912009-02-06 13:52:43 -0700113 spin_lock_init(&f->f_lock);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800114 mutex_init(&f->f_pos_lock);
Benjamin LaHaise5a6b7952006-03-23 03:01:03 -0800115 eventpoll_init_file(f);
Al Viroea73ea72018-07-11 15:00:04 -0400116 f->f_flags = flags;
117 f->f_mode = OPEN_FMODE(flags);
Benjamin LaHaise5a6b7952006-03-23 03:01:03 -0800118 /* f->f_version: 0 */
Miklos Szeredid3b10842018-07-18 15:44:40 +0200119
120 return f;
121}
122
123/* Find an unused file structure and return a pointer to it.
124 * Returns an error pointer if some error happend e.g. we over file
125 * structures limit, run out of memory or operation is not permitted.
126 *
127 * Be very careful using this. You are responsible for
128 * getting write access to any mount that you might assign
129 * to this filp, if it is opened for write. If this is not
130 * done, you will imbalance int the mount's writer count
131 * and a warning at __fput() time.
132 */
133struct file *alloc_empty_file(int flags, const struct cred *cred)
134{
135 static long old_max;
136 struct file *f;
137
138 /*
139 * Privileged users can go above max_files
140 */
141 if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
142 /*
143 * percpu_counters are inaccurate. Do an expensive check before
144 * we go and fail.
145 */
146 if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
147 goto over;
148 }
149
150 f = __alloc_file(flags, cred);
151 if (!IS_ERR(f))
152 percpu_counter_inc(&nr_files);
153
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -0700154 return f;
155
156over:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /* Ran out of filps - report that */
Dipankar Sarma529bf6b2006-03-07 21:55:35 -0800158 if (get_nr_files() > old_max) {
Eric Dumazet518de9b2010-10-26 14:22:44 -0700159 pr_info("VFS: file-max limit %lu reached\n", get_max_files());
Dipankar Sarma529bf6b2006-03-07 21:55:35 -0800160 old_max = get_nr_files();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
Al Viro1afc99b2013-02-14 20:41:04 -0500162 return ERR_PTR(-ENFILE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Miklos Szeredid3b10842018-07-18 15:44:40 +0200165/*
166 * Variant of alloc_empty_file() that doesn't check and modify nr_files.
167 *
168 * Should not be used unless there's a very good reason to do so.
169 */
170struct file *alloc_empty_file_noaccount(int flags, const struct cred *cred)
171{
172 struct file *f = __alloc_file(flags, cred);
173
174 if (!IS_ERR(f))
175 f->f_mode |= FMODE_NOACCOUNT;
176
177 return f;
178}
179
Dave Hansence8d2cd2007-10-16 23:31:13 -0700180/**
181 * alloc_file - allocate and initialize a 'struct file'
Eric Biggersa4576062014-10-12 14:29:29 -0500182 *
183 * @path: the (dentry, vfsmount) pair for the new file
Al Viroc9c554f2018-07-11 14:19:04 -0400184 * @flags: O_... flags with which the new file will be opened
Dave Hansence8d2cd2007-10-16 23:31:13 -0700185 * @fop: the 'struct file_operations' for the new file
Dave Hansence8d2cd2007-10-16 23:31:13 -0700186 */
Al Viroee1904b2018-06-17 14:21:27 -0400187static struct file *alloc_file(const struct path *path, int flags,
Al Viro2c48b9c2009-08-09 00:52:35 +0400188 const struct file_operations *fop)
Dave Hansence8d2cd2007-10-16 23:31:13 -0700189{
190 struct file *file;
Dave Hansence8d2cd2007-10-16 23:31:13 -0700191
Al Viroea73ea72018-07-11 15:00:04 -0400192 file = alloc_empty_file(flags, current_cred());
Al Viro1afc99b2013-02-14 20:41:04 -0500193 if (IS_ERR(file))
Anatol Pomozov39b65252012-09-12 20:11:55 -0700194 return file;
Dave Hansence8d2cd2007-10-16 23:31:13 -0700195
Al Viro2c48b9c2009-08-09 00:52:35 +0400196 file->f_path = *path;
Al Virodd379782013-03-01 19:48:30 -0500197 file->f_inode = path->dentry->d_inode;
Al Viro2c48b9c2009-08-09 00:52:35 +0400198 file->f_mapping = path->dentry->d_inode->i_mapping;
Jeff Layton5660e132017-07-06 07:02:25 -0400199 file->f_wb_err = filemap_sample_wb_err(file->f_mapping);
Al Viroc9c554f2018-07-11 14:19:04 -0400200 if ((file->f_mode & FMODE_READ) &&
Al Viro84363182015-04-04 01:14:53 -0400201 likely(fop->read || fop->read_iter))
Al Viroc9c554f2018-07-11 14:19:04 -0400202 file->f_mode |= FMODE_CAN_READ;
203 if ((file->f_mode & FMODE_WRITE) &&
Al Viro84363182015-04-04 01:14:53 -0400204 likely(fop->write || fop->write_iter))
Al Viroc9c554f2018-07-11 14:19:04 -0400205 file->f_mode |= FMODE_CAN_WRITE;
Al Virof5d11402018-07-09 02:35:08 -0400206 file->f_mode |= FMODE_OPENED;
Dave Hansence8d2cd2007-10-16 23:31:13 -0700207 file->f_op = fop;
Al Viroc9c554f2018-07-11 14:19:04 -0400208 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
Mimi Zohar890275b52010-11-02 10:13:07 -0400209 i_readcount_inc(path->dentry->d_inode);
Al Viro3d1e4632009-08-08 23:56:29 +0400210 return file;
Dave Hansence8d2cd2007-10-16 23:31:13 -0700211}
Dave Hansence8d2cd2007-10-16 23:31:13 -0700212
Al Virod93aa9d2018-06-09 09:40:05 -0400213struct file *alloc_file_pseudo(struct inode *inode, struct vfsmount *mnt,
214 const char *name, int flags,
215 const struct file_operations *fops)
216{
217 static const struct dentry_operations anon_ops = {
218 .d_dname = simple_dname
219 };
220 struct qstr this = QSTR_INIT(name, strlen(name));
221 struct path path;
222 struct file *file;
223
224 path.dentry = d_alloc_pseudo(mnt->mnt_sb, &this);
225 if (!path.dentry)
226 return ERR_PTR(-ENOMEM);
227 if (!mnt->mnt_sb->s_d_op)
228 d_set_d_op(path.dentry, &anon_ops);
229 path.mnt = mntget(mnt);
230 d_instantiate(path.dentry, inode);
231 file = alloc_file(&path, flags, fops);
232 if (IS_ERR(file)) {
233 ihold(inode);
234 path_put(&path);
235 }
236 return file;
237}
238EXPORT_SYMBOL(alloc_file_pseudo);
239
Al Viro183266f2018-06-17 14:15:10 -0400240struct file *alloc_file_clone(struct file *base, int flags,
241 const struct file_operations *fops)
242{
243 struct file *f = alloc_file(&base->f_path, flags, fops);
244 if (!IS_ERR(f)) {
245 path_get(&f->f_path);
246 f->f_mapping = base->f_mapping;
247 }
248 return f;
249}
250
Al Virod7065da2010-05-26 15:13:55 -0400251/* the real guts of fput() - releasing the last reference to file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 */
Al Virod7065da2010-05-26 15:13:55 -0400253static void __fput(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800255 struct dentry *dentry = file->f_path.dentry;
256 struct vfsmount *mnt = file->f_path.mnt;
David Howellsc77cece2013-06-13 23:37:49 +0100257 struct inode *inode = file->f_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Al Viro4d27f322018-07-09 11:14:39 -0400259 if (unlikely(!(file->f_mode & FMODE_OPENED)))
260 goto out;
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 might_sleep();
Robert Love0eeca282005-07-12 17:06:03 -0400263
264 fsnotify_close(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /*
266 * The function eventpoll_release() should be the first called
267 * in the file cleanup chain.
268 */
269 eventpoll_release(file);
Jeff Layton78ed8a12014-02-03 12:13:08 -0500270 locks_remove_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Mimi Zoharbb02b182017-06-21 21:13:18 -0400272 ima_file_free(file);
Al Viro233e70f2008-10-31 23:28:30 +0000273 if (unlikely(file->f_flags & FASYNC)) {
Al Viro72c2d532013-09-22 16:27:52 -0400274 if (file->f_op->fasync)
Al Viro233e70f2008-10-31 23:28:30 +0000275 file->f_op->fasync(-1, file, 0);
276 }
Al Viro72c2d532013-09-22 16:27:52 -0400277 if (file->f_op->release)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 file->f_op->release(inode, file);
Miklos Szeredi60ed8cf2011-03-16 18:17:54 +0100279 if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL &&
280 !(file->f_mode & FMODE_PATH))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 cdev_put(inode->i_cdev);
Miklos Szeredi60ed8cf2011-03-16 18:17:54 +0100282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 fops_put(file->f_op);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700284 put_pid(file->f_owner.pid);
Mimi Zohar890275b52010-11-02 10:13:07 -0400285 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
286 i_readcount_dec(inode);
Al Viro83f936c2014-03-14 12:02:47 -0400287 if (file->f_mode & FMODE_WRITER) {
288 put_write_access(inode);
289 __mnt_drop_write(mnt);
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 dput(dentry);
292 mntput(mnt);
Al Viro4d27f322018-07-09 11:14:39 -0400293out:
294 file_free(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Oleg Nesterov4f5e65a2013-07-08 14:24:16 -0700297static LLIST_HEAD(delayed_fput_list);
Al Viro4a9d4b02012-06-24 09:56:45 +0400298static void delayed_fput(struct work_struct *unused)
299{
Oleg Nesterov4f5e65a2013-07-08 14:24:16 -0700300 struct llist_node *node = llist_del_all(&delayed_fput_list);
Byungchul Parkb9ea5572017-08-07 17:45:39 +0900301 struct file *f, *t;
Oleg Nesterov4f5e65a2013-07-08 14:24:16 -0700302
Byungchul Parkb9ea5572017-08-07 17:45:39 +0900303 llist_for_each_entry_safe(f, t, node, f_u.fu_llist)
304 __fput(f);
Al Viro4a9d4b02012-06-24 09:56:45 +0400305}
306
307static void ____fput(struct callback_head *work)
308{
309 __fput(container_of(work, struct file, f_u.fu_rcuhead));
310}
311
312/*
313 * If kernel thread really needs to have the final fput() it has done
314 * to complete, call this. The only user right now is the boot - we
315 * *do* need to make sure our writes to binaries on initramfs has
316 * not left us with opened struct file waiting for __fput() - execve()
317 * won't work without that. Please, don't add more callers without
318 * very good reasons; in particular, never call that with locks
319 * held and never call that from a thread that might need to do
320 * some work on any kind of umount.
321 */
322void flush_delayed_fput(void)
323{
324 delayed_fput(NULL);
325}
326
Al Viroc7314d72013-10-20 08:44:39 -0400327static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
Al Viro4a9d4b02012-06-24 09:56:45 +0400328
Zhen Kongee7bdc62019-03-14 10:55:19 -0700329void flush_delayed_fput_wait(void)
330{
331 delayed_fput(NULL);
332 flush_delayed_work(&delayed_fput_work);
333}
334
Al Virod7065da2010-05-26 15:13:55 -0400335void fput(struct file *file)
336{
Al Viro4a9d4b02012-06-24 09:56:45 +0400337 if (atomic_long_dec_and_test(&file->f_count)) {
338 struct task_struct *task = current;
Oleg Nesterove7b2c402013-06-14 21:09:47 +0200339
Oleg Nesterove7b2c402013-06-14 21:09:47 +0200340 if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
341 init_task_work(&file->f_u.fu_rcuhead, ____fput);
342 if (!task_work_add(task, &file->f_u.fu_rcuhead, true))
343 return;
Andrew Morton64372502013-07-08 14:24:15 -0700344 /*
345 * After this task has run exit_task_work(),
Andrew Mortonbe49b302013-09-11 14:24:34 -0700346 * task_work_add() will fail. Fall through to delayed
Andrew Morton64372502013-07-08 14:24:15 -0700347 * fput to avoid leaking *file.
348 */
Al Viro4a9d4b02012-06-24 09:56:45 +0400349 }
Oleg Nesterov4f5e65a2013-07-08 14:24:16 -0700350
351 if (llist_add(&file->f_u.fu_llist, &delayed_fput_list))
Al Viroc7314d72013-10-20 08:44:39 -0400352 schedule_delayed_work(&delayed_fput_work, 1);
Al Viro4a9d4b02012-06-24 09:56:45 +0400353 }
354}
355
356/*
357 * synchronous analog of fput(); for kernel threads that might be needed
358 * in some umount() (and thus can't use flush_delayed_fput() without
359 * risking deadlocks), need to wait for completion of __fput() and know
360 * for this specific struct file it won't involve anything that would
361 * need them. Use only if you really need it - at the very least,
362 * don't blindly convert fput() by kernel thread to that.
363 */
364void __fput_sync(struct file *file)
365{
366 if (atomic_long_dec_and_test(&file->f_count)) {
367 struct task_struct *task = current;
Al Viro4a9d4b02012-06-24 09:56:45 +0400368 BUG_ON(!(task->flags & PF_KTHREAD));
Al Virod7065da2010-05-26 15:13:55 -0400369 __fput(file);
Al Viro4a9d4b02012-06-24 09:56:45 +0400370 }
Al Virod7065da2010-05-26 15:13:55 -0400371}
372
373EXPORT_SYMBOL(fput);
374
Mel Gorman4248b0d2015-08-06 15:46:20 -0700375void __init files_init(void)
Byungchul Parkb9ea5572017-08-07 17:45:39 +0900376{
Eric Dumazetb6b3fde2008-12-10 09:35:45 -0800377 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
Shakeel Buttf3f7c092017-11-15 17:35:44 -0800378 SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT, NULL);
Tejun Heo908c7f12014-09-08 09:51:29 +0900379 percpu_counter_init(&nr_files, 0, GFP_KERNEL);
Mel Gorman4248b0d2015-08-06 15:46:20 -0700380}
381
382/*
383 * One file with associated inode and dcache is very roughly 1K. Per default
384 * do not use more than 10% of our memory for files.
385 */
386void __init files_maxfiles_init(void)
387{
388 unsigned long n;
389 unsigned long memreserve = (totalram_pages - nr_free_pages()) * 3/2;
390
391 memreserve = min(memreserve, totalram_pages - 1);
392 n = ((totalram_pages - memreserve) * (PAGE_SIZE / 1024)) / 10;
393
394 files_stat.max_files = max_t(unsigned long, n, NR_FILE);
Byungchul Parkb9ea5572017-08-07 17:45:39 +0900395}