Merge tag 'for-6.7-rc1-part2' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa...
[linux-block.git] / fs / file_table.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * linux/fs/file_table.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
7 */
8
9#include <linux/string.h>
10#include <linux/slab.h>
11#include <linux/file.h>
9f3acc31 12#include <linux/fdtable.h>
1da177e4
LT
13#include <linux/init.h>
14#include <linux/module.h>
1da177e4 15#include <linux/fs.h>
5970e15d 16#include <linux/filelock.h>
1da177e4 17#include <linux/security.h>
5b825c3a 18#include <linux/cred.h>
1da177e4 19#include <linux/eventpoll.h>
ab2af1f5 20#include <linux/rcupdate.h>
1da177e4 21#include <linux/mount.h>
16f7e0fe 22#include <linux/capability.h>
1da177e4 23#include <linux/cdev.h>
0eeca283 24#include <linux/fsnotify.h>
529bf6be
DS
25#include <linux/sysctl.h>
26#include <linux/percpu_counter.h>
6416ccb7 27#include <linux/percpu.h>
4a9d4b02 28#include <linux/task_work.h>
0552f879 29#include <linux/ima.h>
4248b0da 30#include <linux/swap.h>
a3580ac9 31#include <linux/kmemleak.h>
529bf6be 32
60063497 33#include <linux/atomic.h>
1da177e4 34
e81e3f4d
EP
35#include "internal.h"
36
1da177e4 37/* sysctl tunables... */
204d5a24 38static struct files_stat_struct files_stat = {
1da177e4
LT
39 .max_files = NR_FILE
40};
41
b6b3fdea 42/* SLAB cache for file structures */
68279f9c 43static struct kmem_cache *filp_cachep __ro_after_init;
b6b3fdea 44
529bf6be 45static struct percpu_counter nr_files __cacheline_aligned_in_smp;
1da177e4 46
def3ae83 47/* Container for backing file with optional user path */
62d53c4a
AG
48struct backing_file {
49 struct file file;
def3ae83 50 struct path user_path;
62d53c4a
AG
51};
52
53static inline struct backing_file *backing_file(struct file *f)
54{
55 return container_of(f, struct backing_file, file);
56}
57
def3ae83 58struct path *backing_file_user_path(struct file *f)
62d53c4a 59{
def3ae83 60 return &backing_file(f)->user_path;
62d53c4a 61}
def3ae83 62EXPORT_SYMBOL_GPL(backing_file_user_path);
62d53c4a 63
529bf6be 64static inline void file_free(struct file *f)
1da177e4 65{
e8cff84f 66 security_file_free(f);
62d53c4a 67 if (likely(!(f->f_mode & FMODE_NOACCOUNT)))
d3b1084d 68 percpu_counter_dec(&nr_files);
0ede61d8
CB
69 put_cred(f->f_cred);
70 if (unlikely(f->f_mode & FMODE_BACKING)) {
def3ae83 71 path_put(backing_file_user_path(f));
6cf41fcf 72 kfree(backing_file(f));
0ede61d8
CB
73 } else {
74 kmem_cache_free(filp_cachep, f);
75 }
1da177e4
LT
76}
77
93faf426
MG
78void release_empty_file(struct file *f)
79{
80 WARN_ON_ONCE(f->f_mode & (FMODE_BACKING | FMODE_OPENED));
0ede61d8
CB
81 if (atomic_long_dec_and_test(&f->f_count)) {
82 security_file_free(f);
83 put_cred(f->f_cred);
84 if (likely(!(f->f_mode & FMODE_NOACCOUNT)))
85 percpu_counter_dec(&nr_files);
86 kmem_cache_free(filp_cachep, f);
87 }
93faf426
MG
88}
89
529bf6be
DS
90/*
91 * Return the total number of open files in the system
92 */
518de9b3 93static long get_nr_files(void)
1da177e4 94{
529bf6be 95 return percpu_counter_read_positive(&nr_files);
1da177e4
LT
96}
97
529bf6be
DS
98/*
99 * Return the maximum number of open files in the system
100 */
518de9b3 101unsigned long get_max_files(void)
ab2af1f5 102{
529bf6be 103 return files_stat.max_files;
ab2af1f5 104}
529bf6be
DS
105EXPORT_SYMBOL_GPL(get_max_files);
106
204d5a24
LC
107#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
108
529bf6be
DS
109/*
110 * Handle nr_files sysctl
111 */
204d5a24
LC
112static int proc_nr_files(struct ctl_table *table, int write, void *buffer,
113 size_t *lenp, loff_t *ppos)
529bf6be
DS
114{
115 files_stat.nr_files = get_nr_files();
518de9b3 116 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
529bf6be 117}
204d5a24
LC
118
119static struct ctl_table fs_stat_sysctls[] = {
120 {
121 .procname = "file-nr",
122 .data = &files_stat,
123 .maxlen = sizeof(files_stat),
124 .mode = 0444,
125 .proc_handler = proc_nr_files,
126 },
127 {
128 .procname = "file-max",
129 .data = &files_stat.max_files,
130 .maxlen = sizeof(files_stat.max_files),
131 .mode = 0644,
132 .proc_handler = proc_doulongvec_minmax,
133 .extra1 = SYSCTL_LONG_ZERO,
134 .extra2 = SYSCTL_LONG_MAX,
135 },
136 {
137 .procname = "nr_open",
138 .data = &sysctl_nr_open,
139 .maxlen = sizeof(unsigned int),
140 .mode = 0644,
141 .proc_handler = proc_dointvec_minmax,
142 .extra1 = &sysctl_nr_open_min,
143 .extra2 = &sysctl_nr_open_max,
144 },
145 { }
146};
147
148static int __init init_fs_stat_sysctls(void)
529bf6be 149{
204d5a24 150 register_sysctl_init("fs", fs_stat_sysctls);
a3580ac9
LC
151 if (IS_ENABLED(CONFIG_BINFMT_MISC)) {
152 struct ctl_table_header *hdr;
153 hdr = register_sysctl_mount_point("fs/binfmt_misc");
154 kmemleak_not_leak(hdr);
155 }
204d5a24 156 return 0;
529bf6be 157}
204d5a24 158fs_initcall(init_fs_stat_sysctls);
529bf6be 159#endif
ab2af1f5 160
8a05a8c3 161static int init_file(struct file *f, int flags, const struct cred *cred)
d3b1084d 162{
d3b1084d
MS
163 int error;
164
d3b1084d
MS
165 f->f_cred = get_cred(cred);
166 error = security_file_alloc(f);
167 if (unlikely(error)) {
dff745c1 168 put_cred(f->f_cred);
8a05a8c3 169 return error;
d3b1084d
MS
170 }
171
d3b1084d
MS
172 rwlock_init(&f->f_owner.lock);
173 spin_lock_init(&f->f_lock);
174 mutex_init(&f->f_pos_lock);
d3b1084d
MS
175 f->f_flags = flags;
176 f->f_mode = OPEN_FMODE(flags);
177 /* f->f_version: 0 */
178
0ede61d8
CB
179 /*
180 * We're SLAB_TYPESAFE_BY_RCU so initialize f_count last. While
181 * fget-rcu pattern users need to be able to handle spurious
182 * refcount bumps we should reinitialize the reused file first.
183 */
184 atomic_long_set(&f->f_count, 1);
8a05a8c3 185 return 0;
d3b1084d
MS
186}
187
1da177e4 188/* Find an unused file structure and return a pointer to it.
1afc99be
AV
189 * Returns an error pointer if some error happend e.g. we over file
190 * structures limit, run out of memory or operation is not permitted.
430e285e
DH
191 *
192 * Be very careful using this. You are responsible for
193 * getting write access to any mount that you might assign
194 * to this filp, if it is opened for write. If this is not
195 * done, you will imbalance int the mount's writer count
196 * and a warning at __fput() time.
1da177e4 197 */
ea73ea72 198struct file *alloc_empty_file(int flags, const struct cred *cred)
1da177e4 199{
518de9b3 200 static long old_max;
1afc99be 201 struct file *f;
8a05a8c3 202 int error;
1da177e4
LT
203
204 /*
205 * Privileged users can go above max_files
206 */
529bf6be
DS
207 if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
208 /*
209 * percpu_counters are inaccurate. Do an expensive check before
210 * we go and fail.
211 */
52d9f3b4 212 if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
529bf6be
DS
213 goto over;
214 }
af4d2ecb 215
8a05a8c3
AG
216 f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
217 if (unlikely(!f))
218 return ERR_PTR(-ENOMEM);
219
220 error = init_file(f, flags, cred);
dff745c1
AG
221 if (unlikely(error)) {
222 kmem_cache_free(filp_cachep, f);
8a05a8c3 223 return ERR_PTR(error);
dff745c1 224 }
8a05a8c3
AG
225
226 percpu_counter_inc(&nr_files);
1da177e4 227
af4d2ecb
KK
228 return f;
229
230over:
1da177e4 231 /* Ran out of filps - report that */
529bf6be 232 if (get_nr_files() > old_max) {
518de9b3 233 pr_info("VFS: file-max limit %lu reached\n", get_max_files());
529bf6be 234 old_max = get_nr_files();
1da177e4 235 }
1afc99be 236 return ERR_PTR(-ENFILE);
1da177e4
LT
237}
238
d3b1084d
MS
239/*
240 * Variant of alloc_empty_file() that doesn't check and modify nr_files.
241 *
8a05a8c3
AG
242 * This is only for kernel internal use, and the allocate file must not be
243 * installed into file tables or such.
d3b1084d
MS
244 */
245struct file *alloc_empty_file_noaccount(int flags, const struct cred *cred)
246{
8a05a8c3
AG
247 struct file *f;
248 int error;
249
250 f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
251 if (unlikely(!f))
252 return ERR_PTR(-ENOMEM);
253
254 error = init_file(f, flags, cred);
dff745c1
AG
255 if (unlikely(error)) {
256 kmem_cache_free(filp_cachep, f);
8a05a8c3 257 return ERR_PTR(error);
dff745c1 258 }
d3b1084d 259
8a05a8c3 260 f->f_mode |= FMODE_NOACCOUNT;
d3b1084d
MS
261
262 return f;
263}
264
62d53c4a
AG
265/*
266 * Variant of alloc_empty_file() that allocates a backing_file container
267 * and doesn't check and modify nr_files.
268 *
269 * This is only for kernel internal use, and the allocate file must not be
270 * installed into file tables or such.
271 */
272struct file *alloc_empty_backing_file(int flags, const struct cred *cred)
273{
274 struct backing_file *ff;
275 int error;
276
277 ff = kzalloc(sizeof(struct backing_file), GFP_KERNEL);
278 if (unlikely(!ff))
279 return ERR_PTR(-ENOMEM);
280
281 error = init_file(&ff->file, flags, cred);
dff745c1
AG
282 if (unlikely(error)) {
283 kfree(ff);
62d53c4a 284 return ERR_PTR(error);
dff745c1 285 }
62d53c4a
AG
286
287 ff->file.f_mode |= FMODE_BACKING | FMODE_NOACCOUNT;
288 return &ff->file;
289}
290
ce8d2cdf
DH
291/**
292 * alloc_file - allocate and initialize a 'struct file'
a457606a
EB
293 *
294 * @path: the (dentry, vfsmount) pair for the new file
c9c554f2 295 * @flags: O_... flags with which the new file will be opened
ce8d2cdf 296 * @fop: the 'struct file_operations' for the new file
ce8d2cdf 297 */
ee1904ba 298static struct file *alloc_file(const struct path *path, int flags,
2c48b9c4 299 const struct file_operations *fop)
ce8d2cdf
DH
300{
301 struct file *file;
ce8d2cdf 302
ea73ea72 303 file = alloc_empty_file(flags, current_cred());
1afc99be 304 if (IS_ERR(file))
39b65252 305 return file;
ce8d2cdf 306
2c48b9c4 307 file->f_path = *path;
dd37978c 308 file->f_inode = path->dentry->d_inode;
2c48b9c4 309 file->f_mapping = path->dentry->d_inode->i_mapping;
5660e13d 310 file->f_wb_err = filemap_sample_wb_err(file->f_mapping);
735e4ae5 311 file->f_sb_err = file_sample_sb_err(file);
868941b1 312 if (fop->llseek)
e7478158 313 file->f_mode |= FMODE_LSEEK;
c9c554f2 314 if ((file->f_mode & FMODE_READ) &&
84363182 315 likely(fop->read || fop->read_iter))
c9c554f2
AV
316 file->f_mode |= FMODE_CAN_READ;
317 if ((file->f_mode & FMODE_WRITE) &&
84363182 318 likely(fop->write || fop->write_iter))
c9c554f2 319 file->f_mode |= FMODE_CAN_WRITE;
164f4064 320 file->f_iocb_flags = iocb_flags(file);
f5d11409 321 file->f_mode |= FMODE_OPENED;
ce8d2cdf 322 file->f_op = fop;
c9c554f2 323 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
890275b5 324 i_readcount_inc(path->dentry->d_inode);
3d1e4631 325 return file;
ce8d2cdf 326}
ce8d2cdf 327
d93aa9d8
AV
328struct file *alloc_file_pseudo(struct inode *inode, struct vfsmount *mnt,
329 const char *name, int flags,
330 const struct file_operations *fops)
331{
332 static const struct dentry_operations anon_ops = {
333 .d_dname = simple_dname
334 };
335 struct qstr this = QSTR_INIT(name, strlen(name));
336 struct path path;
337 struct file *file;
338
339 path.dentry = d_alloc_pseudo(mnt->mnt_sb, &this);
340 if (!path.dentry)
341 return ERR_PTR(-ENOMEM);
342 if (!mnt->mnt_sb->s_d_op)
343 d_set_d_op(path.dentry, &anon_ops);
344 path.mnt = mntget(mnt);
345 d_instantiate(path.dentry, inode);
b6509f6a 346 file = alloc_file(&path, flags, fops);
d93aa9d8
AV
347 if (IS_ERR(file)) {
348 ihold(inode);
349 path_put(&path);
350 }
351 return file;
352}
353EXPORT_SYMBOL(alloc_file_pseudo);
354
183266f2
AV
355struct file *alloc_file_clone(struct file *base, int flags,
356 const struct file_operations *fops)
357{
358 struct file *f = alloc_file(&base->f_path, flags, fops);
359 if (!IS_ERR(f)) {
360 path_get(&f->f_path);
361 f->f_mapping = base->f_mapping;
362 }
363 return f;
364}
365
d7065da0 366/* the real guts of fput() - releasing the last reference to file
1da177e4 367 */
d7065da0 368static void __fput(struct file *file)
1da177e4 369{
0f7fc9e4
JJS
370 struct dentry *dentry = file->f_path.dentry;
371 struct vfsmount *mnt = file->f_path.mnt;
c77cecee 372 struct inode *inode = file->f_inode;
a07b2000 373 fmode_t mode = file->f_mode;
1da177e4 374
4d27f326
AV
375 if (unlikely(!(file->f_mode & FMODE_OPENED)))
376 goto out;
377
1da177e4 378 might_sleep();
0eeca283
RL
379
380 fsnotify_close(file);
1da177e4
LT
381 /*
382 * The function eventpoll_release() should be the first called
383 * in the file cleanup chain.
384 */
385 eventpoll_release(file);
78ed8a13 386 locks_remove_file(file);
1da177e4 387
bb02b186 388 ima_file_free(file);
233e70f4 389 if (unlikely(file->f_flags & FASYNC)) {
72c2d531 390 if (file->f_op->fasync)
233e70f4
AV
391 file->f_op->fasync(-1, file, 0);
392 }
72c2d531 393 if (file->f_op->release)
1da177e4 394 file->f_op->release(inode, file);
60ed8cf7 395 if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL &&
a07b2000 396 !(mode & FMODE_PATH))) {
1da177e4 397 cdev_put(inode->i_cdev);
60ed8cf7 398 }
1da177e4 399 fops_put(file->f_op);
609d7fa9 400 put_pid(file->f_owner.pid);
d6da19c9 401 put_file_access(file);
1da177e4 402 dput(dentry);
a07b2000
AV
403 if (unlikely(mode & FMODE_NEED_UNMOUNT))
404 dissolve_on_fput(mnt);
1da177e4 405 mntput(mnt);
4d27f326
AV
406out:
407 file_free(file);
1da177e4
LT
408}
409
4f5e65a1 410static LLIST_HEAD(delayed_fput_list);
4a9d4b02
AV
411static void delayed_fput(struct work_struct *unused)
412{
4f5e65a1 413 struct llist_node *node = llist_del_all(&delayed_fput_list);
b9ea557e 414 struct file *f, *t;
4f5e65a1 415
e87f2c26 416 llist_for_each_entry_safe(f, t, node, f_llist)
b9ea557e 417 __fput(f);
4a9d4b02
AV
418}
419
420static void ____fput(struct callback_head *work)
421{
e87f2c26 422 __fput(container_of(work, struct file, f_rcuhead));
4a9d4b02
AV
423}
424
425/*
426 * If kernel thread really needs to have the final fput() it has done
427 * to complete, call this. The only user right now is the boot - we
428 * *do* need to make sure our writes to binaries on initramfs has
429 * not left us with opened struct file waiting for __fput() - execve()
430 * won't work without that. Please, don't add more callers without
431 * very good reasons; in particular, never call that with locks
432 * held and never call that from a thread that might need to do
433 * some work on any kind of umount.
434 */
435void flush_delayed_fput(void)
436{
437 delayed_fput(NULL);
438}
7239a40c 439EXPORT_SYMBOL_GPL(flush_delayed_fput);
4a9d4b02 440
c7314d74 441static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
4a9d4b02 442
81132a39 443void fput(struct file *file)
d7065da0 444{
81132a39 445 if (atomic_long_dec_and_test(&file->f_count)) {
4a9d4b02 446 struct task_struct *task = current;
e7b2c406 447
e7b2c406 448 if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
e87f2c26
AV
449 init_task_work(&file->f_rcuhead, ____fput);
450 if (!task_work_add(task, &file->f_rcuhead, TWA_RESUME))
e7b2c406 451 return;
64372501
AM
452 /*
453 * After this task has run exit_task_work(),
be49b30a 454 * task_work_add() will fail. Fall through to delayed
64372501
AM
455 * fput to avoid leaking *file.
456 */
4a9d4b02 457 }
4f5e65a1 458
e87f2c26 459 if (llist_add(&file->f_llist, &delayed_fput_list))
c7314d74 460 schedule_delayed_work(&delayed_fput_work, 1);
4a9d4b02
AV
461 }
462}
463
464/*
465 * synchronous analog of fput(); for kernel threads that might be needed
466 * in some umount() (and thus can't use flush_delayed_fput() without
467 * risking deadlocks), need to wait for completion of __fput() and know
468 * for this specific struct file it won't involve anything that would
469 * need them. Use only if you really need it - at the very least,
470 * don't blindly convert fput() by kernel thread to that.
471 */
472void __fput_sync(struct file *file)
473{
021a160a 474 if (atomic_long_dec_and_test(&file->f_count))
d7065da0
AV
475 __fput(file);
476}
477
478EXPORT_SYMBOL(fput);
f0043206 479EXPORT_SYMBOL(__fput_sync);
d7065da0 480
4248b0da 481void __init files_init(void)
b9ea557e 482{
b6b3fdea 483 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
0ede61d8
CB
484 SLAB_TYPESAFE_BY_RCU | SLAB_HWCACHE_ALIGN |
485 SLAB_PANIC | SLAB_ACCOUNT, NULL);
4248b0da
MG
486 percpu_counter_init(&nr_files, 0, GFP_KERNEL);
487}
b6b3fdea 488
4248b0da
MG
489/*
490 * One file with associated inode and dcache is very roughly 1K. Per default
491 * do not use more than 10% of our memory for files.
492 */
493void __init files_maxfiles_init(void)
494{
495 unsigned long n;
ca79b0c2 496 unsigned long nr_pages = totalram_pages();
3d6357de 497 unsigned long memreserve = (nr_pages - nr_free_pages()) * 3/2;
4248b0da 498
3d6357de
AK
499 memreserve = min(memreserve, nr_pages - 1);
500 n = ((nr_pages - memreserve) * (PAGE_SIZE / 1024)) / 10;
1da177e4 501
518de9b3 502 files_stat.max_files = max_t(unsigned long, n, NR_FILE);
b9ea557e 503}