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