2019-05-19 13:08:55 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
|
* linux/fs/file_table.c
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 1991, 1992 Linus Torvalds
|
|
|
|
|
* Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <linux/string.h>
|
|
|
|
|
#include <linux/slab.h>
|
|
|
|
|
#include <linux/file.h>
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
#include <linux/fs.h>
|
2022-11-20 09:15:34 -05:00
|
|
|
#include <linux/filelock.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
#include <linux/security.h>
|
2017-02-02 17:54:15 +01:00
|
|
|
#include <linux/cred.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
#include <linux/eventpoll.h>
|
2005-09-09 13:04:13 -07:00
|
|
|
#include <linux/rcupdate.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
#include <linux/mount.h>
|
2006-01-11 12:17:46 -08:00
|
|
|
#include <linux/capability.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
#include <linux/cdev.h>
|
2005-07-12 17:06:03 -04:00
|
|
|
#include <linux/fsnotify.h>
|
2006-03-07 21:55:35 -08:00
|
|
|
#include <linux/sysctl.h>
|
|
|
|
|
#include <linux/percpu_counter.h>
|
2010-08-18 04:37:38 +10:00
|
|
|
#include <linux/percpu.h>
|
2012-06-24 09:56:45 +04:00
|
|
|
#include <linux/task_work.h>
|
2015-08-06 15:46:20 -07:00
|
|
|
#include <linux/swap.h>
|
2022-02-14 18:08:28 -08:00
|
|
|
#include <linux/kmemleak.h>
|
2006-03-07 21:55:35 -08:00
|
|
|
|
2011-07-26 16:09:06 -07:00
|
|
|
#include <linux/atomic.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2026-03-28 18:37:28 +01:00
|
|
|
#include <asm/runtime-const.h>
|
|
|
|
|
|
2009-12-04 15:47:36 -05:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
/* sysctl tunables... */
|
2022-01-21 22:12:56 -08:00
|
|
|
static struct files_stat_struct files_stat = {
|
2005-04-16 15:20:36 -07:00
|
|
|
.max_files = NR_FILE
|
|
|
|
|
};
|
|
|
|
|
|
2008-12-10 09:35:45 -08:00
|
|
|
/* SLAB cache for file structures */
|
2026-03-28 18:37:28 +01:00
|
|
|
static struct kmem_cache *__filp_cache __ro_after_init;
|
|
|
|
|
#define filp_cache runtime_const_ptr(__filp_cache)
|
|
|
|
|
static struct kmem_cache *__bfilp_cache __ro_after_init;
|
|
|
|
|
#define bfilp_cache runtime_const_ptr(__bfilp_cache)
|
2008-12-10 09:35:45 -08:00
|
|
|
|
2006-03-07 21:55:35 -08:00
|
|
|
static struct percpu_counter nr_files __cacheline_aligned_in_smp;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2023-10-09 18:37:12 +03:00
|
|
|
/* Container for backing file with optional user path */
|
2023-06-15 14:22:28 +03:00
|
|
|
struct backing_file {
|
|
|
|
|
struct file file;
|
2024-10-07 16:23:57 +02:00
|
|
|
union {
|
|
|
|
|
struct path user_path;
|
|
|
|
|
freeptr_t bf_freeptr;
|
|
|
|
|
};
|
2025-12-19 13:18:22 -05:00
|
|
|
#ifdef CONFIG_SECURITY
|
|
|
|
|
void *security;
|
|
|
|
|
#endif
|
2023-06-15 14:22:28 +03:00
|
|
|
};
|
|
|
|
|
|
2025-06-07 13:53:03 +02:00
|
|
|
#define backing_file(f) container_of(f, struct backing_file, file)
|
2023-06-15 14:22:28 +03:00
|
|
|
|
2025-09-04 23:16:35 -04:00
|
|
|
const struct path *backing_file_user_path(const struct file *f)
|
2023-06-15 14:22:28 +03:00
|
|
|
{
|
2023-10-09 18:37:12 +03:00
|
|
|
return &backing_file(f)->user_path;
|
2023-06-15 14:22:28 +03:00
|
|
|
}
|
2023-10-09 18:37:12 +03:00
|
|
|
EXPORT_SYMBOL_GPL(backing_file_user_path);
|
2025-06-07 13:53:03 +02:00
|
|
|
|
|
|
|
|
void backing_file_set_user_path(struct file *f, const struct path *path)
|
|
|
|
|
{
|
|
|
|
|
backing_file(f)->user_path = *path;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL_GPL(backing_file_set_user_path);
|
2023-06-15 14:22:28 +03:00
|
|
|
|
2025-12-19 13:18:22 -05:00
|
|
|
#ifdef CONFIG_SECURITY
|
|
|
|
|
void *backing_file_security(const struct file *f)
|
|
|
|
|
{
|
|
|
|
|
return backing_file(f)->security;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void backing_file_set_security(struct file *f, void *security)
|
|
|
|
|
{
|
|
|
|
|
backing_file(f)->security = security;
|
|
|
|
|
}
|
|
|
|
|
#endif /* CONFIG_SECURITY */
|
|
|
|
|
|
2026-03-30 10:27:51 +02:00
|
|
|
static inline void backing_file_free(struct backing_file *ff)
|
|
|
|
|
{
|
2025-12-19 13:18:22 -05:00
|
|
|
security_backing_file_free(&ff->file);
|
2026-03-30 10:27:51 +02:00
|
|
|
path_put(&ff->user_path);
|
2026-04-13 15:17:28 -07:00
|
|
|
kmem_cache_free(bfilp_cache, ff);
|
2026-03-30 10:27:51 +02:00
|
|
|
}
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
static inline void file_free(struct file *f)
|
|
|
|
|
{
|
2018-07-09 11:24:21 -04:00
|
|
|
security_file_free(f);
|
2023-06-15 14:22:28 +03:00
|
|
|
if (likely(!(f->f_mode & FMODE_NOACCOUNT)))
|
2018-07-18 15:44:40 +02:00
|
|
|
percpu_counter_dec(&nr_files);
|
2023-09-29 08:45:59 +02:00
|
|
|
put_cred(f->f_cred);
|
|
|
|
|
if (unlikely(f->f_mode & FMODE_BACKING)) {
|
2026-03-30 10:27:51 +02:00
|
|
|
backing_file_free(backing_file(f));
|
2023-09-29 08:45:59 +02:00
|
|
|
} else {
|
2026-03-28 18:37:28 +01:00
|
|
|
kmem_cache_free(filp_cache, f);
|
2023-09-29 08:45:59 +02:00
|
|
|
}
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
2006-03-07 21:55:35 -08:00
|
|
|
/*
|
|
|
|
|
* Return the total number of open files in the system
|
|
|
|
|
*/
|
2010-10-26 14:22:44 -07:00
|
|
|
static long get_nr_files(void)
|
2006-03-07 21:55:35 -08:00
|
|
|
{
|
|
|
|
|
return percpu_counter_read_positive(&nr_files);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Return the maximum number of open files in the system
|
|
|
|
|
*/
|
2010-10-26 14:22:44 -07:00
|
|
|
unsigned long get_max_files(void)
|
2006-03-07 21:55:35 -08:00
|
|
|
{
|
|
|
|
|
return files_stat.max_files;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL_GPL(get_max_files);
|
|
|
|
|
|
2022-01-21 22:12:56 -08:00
|
|
|
#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
|
|
|
|
|
|
2006-03-07 21:55:35 -08:00
|
|
|
/*
|
|
|
|
|
* Handle nr_files sysctl
|
|
|
|
|
*/
|
2024-07-24 20:59:29 +02:00
|
|
|
static int proc_nr_files(const struct ctl_table *table, int write, void *buffer,
|
2022-01-21 22:12:56 -08:00
|
|
|
size_t *lenp, loff_t *ppos)
|
2006-03-07 21:55:35 -08:00
|
|
|
{
|
2025-04-10 19:21:17 +08:00
|
|
|
files_stat.nr_files = percpu_counter_sum_positive(&nr_files);
|
2010-10-26 14:22:44 -07:00
|
|
|
return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
|
2006-03-07 21:55:35 -08:00
|
|
|
}
|
2022-01-21 22:12:56 -08:00
|
|
|
|
2025-01-28 13:48:37 +01:00
|
|
|
static const struct ctl_table fs_stat_sysctls[] = {
|
2022-01-21 22:12:56 -08:00
|
|
|
{
|
|
|
|
|
.procname = "file-nr",
|
|
|
|
|
.data = &files_stat,
|
|
|
|
|
.maxlen = sizeof(files_stat),
|
|
|
|
|
.mode = 0444,
|
|
|
|
|
.proc_handler = proc_nr_files,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
.procname = "file-max",
|
|
|
|
|
.data = &files_stat.max_files,
|
|
|
|
|
.maxlen = sizeof(files_stat.max_files),
|
|
|
|
|
.mode = 0644,
|
|
|
|
|
.proc_handler = proc_doulongvec_minmax,
|
|
|
|
|
.extra1 = SYSCTL_LONG_ZERO,
|
|
|
|
|
.extra2 = SYSCTL_LONG_MAX,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
.procname = "nr_open",
|
|
|
|
|
.data = &sysctl_nr_open,
|
|
|
|
|
.maxlen = sizeof(unsigned int),
|
|
|
|
|
.mode = 0644,
|
2024-11-24 11:46:36 +08:00
|
|
|
.proc_handler = proc_douintvec_minmax,
|
2022-01-21 22:12:56 -08:00
|
|
|
.extra1 = &sysctl_nr_open_min,
|
|
|
|
|
.extra2 = &sysctl_nr_open_max,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int __init init_fs_stat_sysctls(void)
|
2006-03-07 21:55:35 -08:00
|
|
|
{
|
2022-01-21 22:12:56 -08:00
|
|
|
register_sysctl_init("fs", fs_stat_sysctls);
|
2022-02-14 18:08:28 -08:00
|
|
|
if (IS_ENABLED(CONFIG_BINFMT_MISC)) {
|
|
|
|
|
struct ctl_table_header *hdr;
|
2024-07-27 12:51:34 +05:30
|
|
|
|
2022-02-14 18:08:28 -08:00
|
|
|
hdr = register_sysctl_mount_point("fs/binfmt_misc");
|
|
|
|
|
kmemleak_not_leak(hdr);
|
|
|
|
|
}
|
2022-01-21 22:12:56 -08:00
|
|
|
return 0;
|
2006-03-07 21:55:35 -08:00
|
|
|
}
|
2022-01-21 22:12:56 -08:00
|
|
|
fs_initcall(init_fs_stat_sysctls);
|
2006-03-07 21:55:35 -08:00
|
|
|
#endif
|
|
|
|
|
|
2023-06-15 14:22:27 +03:00
|
|
|
static int init_file(struct file *f, int flags, const struct cred *cred)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2013-02-14 20:41:04 -05:00
|
|
|
int error;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2011-02-04 18:13:24 +00:00
|
|
|
f->f_cred = get_cred(cred);
|
2013-02-14 20:41:04 -05:00
|
|
|
error = security_file_alloc(f);
|
|
|
|
|
if (unlikely(error)) {
|
2023-07-01 20:11:34 +03:00
|
|
|
put_cred(f->f_cred);
|
2023-06-15 14:22:27 +03:00
|
|
|
return error;
|
2013-02-14 20:41:04 -05:00
|
|
|
}
|
2005-06-23 00:09:50 -07:00
|
|
|
|
2009-02-06 13:52:43 -07:00
|
|
|
spin_lock_init(&f->f_lock);
|
2024-08-30 15:04:59 +02:00
|
|
|
/*
|
|
|
|
|
* Note that f_pos_lock is only used for files raising
|
|
|
|
|
* FMODE_ATOMIC_POS and directories. Other files such as pipes
|
|
|
|
|
* don't need it and since f_pos_lock is in a union may reuse
|
|
|
|
|
* the space for other purposes. They are expected to initialize
|
|
|
|
|
* the respective member when opening the file.
|
|
|
|
|
*/
|
2014-03-03 09:36:58 -08:00
|
|
|
mutex_init(&f->f_pos_lock);
|
2025-07-20 15:32:31 -04:00
|
|
|
memset(&f->__f_path, 0, sizeof(f->f_path));
|
2024-10-07 16:23:59 +02:00
|
|
|
memset(&f->f_ra, 0, sizeof(f->f_ra));
|
|
|
|
|
|
|
|
|
|
f->f_flags = flags;
|
|
|
|
|
f->f_mode = OPEN_FMODE(flags);
|
2026-01-09 22:15:36 +01:00
|
|
|
/*
|
|
|
|
|
* Disable permission and pre-content events for all files by default.
|
|
|
|
|
* They may be enabled later by fsnotify_open_perm_and_set_mode().
|
|
|
|
|
*/
|
|
|
|
|
file_set_fsnotify_mode(f, FMODE_NONOTIFY_PERM);
|
2024-10-07 16:23:59 +02:00
|
|
|
|
|
|
|
|
f->f_op = NULL;
|
|
|
|
|
f->f_mapping = NULL;
|
|
|
|
|
f->private_data = NULL;
|
|
|
|
|
f->f_inode = NULL;
|
|
|
|
|
f->f_owner = NULL;
|
|
|
|
|
#ifdef CONFIG_EPOLL
|
|
|
|
|
f->f_ep = NULL;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
f->f_iocb_flags = 0;
|
|
|
|
|
f->f_pos = 0;
|
|
|
|
|
f->f_wb_err = 0;
|
|
|
|
|
f->f_sb_err = 0;
|
2018-07-18 15:44:40 +02:00
|
|
|
|
2023-09-29 08:45:59 +02:00
|
|
|
/*
|
2025-09-24 20:21:39 +08:00
|
|
|
* We're SLAB_TYPESAFE_BY_RCU so initialize f_ref last. While
|
2023-09-29 08:45:59 +02:00
|
|
|
* fget-rcu pattern users need to be able to handle spurious
|
|
|
|
|
* refcount bumps we should reinitialize the reused file first.
|
|
|
|
|
*/
|
2024-10-07 16:23:59 +02:00
|
|
|
file_ref_init(&f->f_ref, 1);
|
2023-06-15 14:22:27 +03:00
|
|
|
return 0;
|
2018-07-18 15:44:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Find an unused file structure and return a pointer to it.
|
|
|
|
|
* Returns an error pointer if some error happend e.g. we over file
|
|
|
|
|
* structures limit, run out of memory or operation is not permitted.
|
|
|
|
|
*
|
|
|
|
|
* Be very careful using this. You are responsible for
|
|
|
|
|
* getting write access to any mount that you might assign
|
|
|
|
|
* to this filp, if it is opened for write. If this is not
|
|
|
|
|
* done, you will imbalance int the mount's writer count
|
|
|
|
|
* and a warning at __fput() time.
|
|
|
|
|
*/
|
|
|
|
|
struct file *alloc_empty_file(int flags, const struct cred *cred)
|
|
|
|
|
{
|
|
|
|
|
static long old_max;
|
|
|
|
|
struct file *f;
|
2023-06-15 14:22:27 +03:00
|
|
|
int error;
|
2018-07-18 15:44:40 +02:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Privileged users can go above max_files
|
|
|
|
|
*/
|
2025-03-19 13:49:23 +01:00
|
|
|
if (unlikely(get_nr_files() >= files_stat.max_files) &&
|
|
|
|
|
!capable(CAP_SYS_ADMIN)) {
|
2018-07-18 15:44:40 +02:00
|
|
|
/*
|
|
|
|
|
* percpu_counters are inaccurate. Do an expensive check before
|
|
|
|
|
* we go and fail.
|
|
|
|
|
*/
|
|
|
|
|
if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
|
|
|
|
|
goto over;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 18:37:28 +01:00
|
|
|
f = kmem_cache_alloc(filp_cache, GFP_KERNEL);
|
2023-06-15 14:22:27 +03:00
|
|
|
if (unlikely(!f))
|
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
|
|
error = init_file(f, flags, cred);
|
2023-07-01 20:11:34 +03:00
|
|
|
if (unlikely(error)) {
|
2026-03-28 18:37:28 +01:00
|
|
|
kmem_cache_free(filp_cache, f);
|
2023-06-15 14:22:27 +03:00
|
|
|
return ERR_PTR(error);
|
2023-07-01 20:11:34 +03:00
|
|
|
}
|
2023-06-15 14:22:27 +03:00
|
|
|
|
|
|
|
|
percpu_counter_inc(&nr_files);
|
2018-07-18 15:44:40 +02:00
|
|
|
|
2005-06-23 00:09:50 -07:00
|
|
|
return f;
|
|
|
|
|
|
|
|
|
|
over:
|
2005-04-16 15:20:36 -07:00
|
|
|
/* Ran out of filps - report that */
|
2006-03-07 21:55:35 -08:00
|
|
|
if (get_nr_files() > old_max) {
|
2010-10-26 14:22:44 -07:00
|
|
|
pr_info("VFS: file-max limit %lu reached\n", get_max_files());
|
2006-03-07 21:55:35 -08:00
|
|
|
old_max = get_nr_files();
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2013-02-14 20:41:04 -05:00
|
|
|
return ERR_PTR(-ENFILE);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
2018-07-18 15:44:40 +02:00
|
|
|
/*
|
|
|
|
|
* Variant of alloc_empty_file() that doesn't check and modify nr_files.
|
|
|
|
|
*
|
2023-06-15 14:22:27 +03:00
|
|
|
* This is only for kernel internal use, and the allocate file must not be
|
|
|
|
|
* installed into file tables or such.
|
2018-07-18 15:44:40 +02:00
|
|
|
*/
|
|
|
|
|
struct file *alloc_empty_file_noaccount(int flags, const struct cred *cred)
|
|
|
|
|
{
|
2023-06-15 14:22:27 +03:00
|
|
|
struct file *f;
|
|
|
|
|
int error;
|
2018-07-18 15:44:40 +02:00
|
|
|
|
2026-03-28 18:37:28 +01:00
|
|
|
f = kmem_cache_alloc(filp_cache, GFP_KERNEL);
|
2023-06-15 14:22:27 +03:00
|
|
|
if (unlikely(!f))
|
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
|
|
error = init_file(f, flags, cred);
|
2023-07-01 20:11:34 +03:00
|
|
|
if (unlikely(error)) {
|
2026-03-28 18:37:28 +01:00
|
|
|
kmem_cache_free(filp_cache, f);
|
2023-06-15 14:22:27 +03:00
|
|
|
return ERR_PTR(error);
|
2023-07-01 20:11:34 +03:00
|
|
|
}
|
2023-06-15 14:22:27 +03:00
|
|
|
|
|
|
|
|
f->f_mode |= FMODE_NOACCOUNT;
|
2018-07-18 15:44:40 +02:00
|
|
|
|
|
|
|
|
return f;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-19 13:18:22 -05:00
|
|
|
static int init_backing_file(struct backing_file *ff,
|
|
|
|
|
const struct file *user_file)
|
2026-03-30 10:27:51 +02:00
|
|
|
{
|
|
|
|
|
memset(&ff->user_path, 0, sizeof(ff->user_path));
|
2025-12-19 13:18:22 -05:00
|
|
|
backing_file_set_security(&ff->file, NULL);
|
|
|
|
|
return security_backing_file_alloc(&ff->file, user_file);
|
2026-03-30 10:27:51 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-15 14:22:28 +03:00
|
|
|
/*
|
|
|
|
|
* Variant of alloc_empty_file() that allocates a backing_file container
|
|
|
|
|
* and doesn't check and modify nr_files.
|
|
|
|
|
*
|
|
|
|
|
* This is only for kernel internal use, and the allocate file must not be
|
|
|
|
|
* installed into file tables or such.
|
|
|
|
|
*/
|
2025-12-19 13:18:22 -05:00
|
|
|
struct file *alloc_empty_backing_file(int flags, const struct cred *cred,
|
|
|
|
|
const struct file *user_file)
|
2023-06-15 14:22:28 +03:00
|
|
|
{
|
|
|
|
|
struct backing_file *ff;
|
|
|
|
|
int error;
|
|
|
|
|
|
2026-03-28 18:37:28 +01:00
|
|
|
ff = kmem_cache_alloc(bfilp_cache, GFP_KERNEL);
|
2023-06-15 14:22:28 +03:00
|
|
|
if (unlikely(!ff))
|
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
|
|
error = init_file(&ff->file, flags, cred);
|
2023-07-01 20:11:34 +03:00
|
|
|
if (unlikely(error)) {
|
2026-03-28 18:37:28 +01:00
|
|
|
kmem_cache_free(bfilp_cache, ff);
|
2023-06-15 14:22:28 +03:00
|
|
|
return ERR_PTR(error);
|
2023-07-01 20:11:34 +03:00
|
|
|
}
|
2023-06-15 14:22:28 +03:00
|
|
|
|
2026-03-30 10:27:51 +02:00
|
|
|
/* The f_mode flags must be set before fput(). */
|
2023-06-15 14:22:28 +03:00
|
|
|
ff->file.f_mode |= FMODE_BACKING | FMODE_NOACCOUNT;
|
2025-12-19 13:18:22 -05:00
|
|
|
error = init_backing_file(ff, user_file);
|
2026-03-30 10:27:51 +02:00
|
|
|
if (unlikely(error)) {
|
|
|
|
|
fput(&ff->file);
|
|
|
|
|
return ERR_PTR(error);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-15 14:22:28 +03:00
|
|
|
return &ff->file;
|
|
|
|
|
}
|
2026-01-23 01:31:23 +00:00
|
|
|
EXPORT_SYMBOL_GPL(alloc_empty_backing_file);
|
2023-06-15 14:22:28 +03:00
|
|
|
|
2007-10-16 23:31:13 -07:00
|
|
|
/**
|
2024-02-08 19:08:37 +01:00
|
|
|
* file_init_path - initialize a 'struct file' based on path
|
2014-10-12 14:29:29 -05:00
|
|
|
*
|
2024-02-08 19:08:37 +01:00
|
|
|
* @file: the file to set up
|
2014-10-12 14:29:29 -05:00
|
|
|
* @path: the (dentry, vfsmount) pair for the new file
|
2007-10-16 23:31:13 -07:00
|
|
|
* @fop: the 'struct file_operations' for the new file
|
|
|
|
|
*/
|
2024-02-08 19:08:37 +01:00
|
|
|
static void file_init_path(struct file *file, const struct path *path,
|
|
|
|
|
const struct file_operations *fop)
|
2007-10-16 23:31:13 -07:00
|
|
|
{
|
2025-07-20 15:32:31 -04:00
|
|
|
file->__f_path = *path;
|
2013-03-01 19:48:30 -05:00
|
|
|
file->f_inode = path->dentry->d_inode;
|
2009-08-09 00:52:35 +04:00
|
|
|
file->f_mapping = path->dentry->d_inode->i_mapping;
|
2017-07-06 07:02:25 -04:00
|
|
|
file->f_wb_err = filemap_sample_wb_err(file->f_mapping);
|
2020-06-01 21:45:36 -07:00
|
|
|
file->f_sb_err = file_sample_sb_err(file);
|
2022-06-29 15:07:00 +02:00
|
|
|
if (fop->llseek)
|
2022-06-29 15:06:57 +02:00
|
|
|
file->f_mode |= FMODE_LSEEK;
|
2018-07-11 14:19:04 -04:00
|
|
|
if ((file->f_mode & FMODE_READ) &&
|
2015-04-04 01:14:53 -04:00
|
|
|
likely(fop->read || fop->read_iter))
|
2018-07-11 14:19:04 -04:00
|
|
|
file->f_mode |= FMODE_CAN_READ;
|
|
|
|
|
if ((file->f_mode & FMODE_WRITE) &&
|
2015-04-04 01:14:53 -04:00
|
|
|
likely(fop->write || fop->write_iter))
|
2018-07-11 14:19:04 -04:00
|
|
|
file->f_mode |= FMODE_CAN_WRITE;
|
2022-05-22 11:38:11 -04:00
|
|
|
file->f_iocb_flags = iocb_flags(file);
|
2018-07-09 02:35:08 -04:00
|
|
|
file->f_mode |= FMODE_OPENED;
|
2007-10-16 23:31:13 -07:00
|
|
|
file->f_op = fop;
|
2018-07-11 14:19:04 -04:00
|
|
|
if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
|
2010-11-02 10:13:07 -04:00
|
|
|
i_readcount_inc(path->dentry->d_inode);
|
2024-02-08 19:08:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* alloc_file - allocate and initialize a 'struct file'
|
|
|
|
|
*
|
|
|
|
|
* @path: the (dentry, vfsmount) pair for the new file
|
|
|
|
|
* @flags: O_... flags with which the new file will be opened
|
|
|
|
|
* @fop: the 'struct file_operations' for the new file
|
|
|
|
|
*/
|
|
|
|
|
static struct file *alloc_file(const struct path *path, int flags,
|
|
|
|
|
const struct file_operations *fop)
|
|
|
|
|
{
|
|
|
|
|
struct file *file;
|
|
|
|
|
|
|
|
|
|
file = alloc_empty_file(flags, current_cred());
|
|
|
|
|
if (!IS_ERR(file))
|
|
|
|
|
file_init_path(file, path, fop);
|
2009-08-08 23:56:29 +04:00
|
|
|
return file;
|
2007-10-16 23:31:13 -07:00
|
|
|
}
|
|
|
|
|
|
2024-02-08 19:08:37 +01:00
|
|
|
static inline int alloc_path_pseudo(const char *name, struct inode *inode,
|
|
|
|
|
struct vfsmount *mnt, struct path *path)
|
2018-06-09 09:40:05 -04:00
|
|
|
{
|
2025-01-23 22:51:04 -05:00
|
|
|
path->dentry = d_alloc_pseudo(mnt->mnt_sb, &QSTR(name));
|
2024-02-08 19:08:37 +01:00
|
|
|
if (!path->dentry)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
path->mnt = mntget(mnt);
|
|
|
|
|
d_instantiate(path->dentry, inode);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct file *alloc_file_pseudo(struct inode *inode, struct vfsmount *mnt,
|
|
|
|
|
const char *name, int flags,
|
|
|
|
|
const struct file_operations *fops)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
2018-06-09 09:40:05 -04:00
|
|
|
struct path path;
|
|
|
|
|
struct file *file;
|
|
|
|
|
|
2024-02-08 19:08:37 +01:00
|
|
|
ret = alloc_path_pseudo(name, inode, mnt, &path);
|
|
|
|
|
if (ret)
|
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
|
|
2020-06-29 15:41:45 +01:00
|
|
|
file = alloc_file(&path, flags, fops);
|
2018-06-09 09:40:05 -04:00
|
|
|
if (IS_ERR(file)) {
|
|
|
|
|
ihold(inode);
|
|
|
|
|
path_put(&path);
|
2025-02-03 23:32:04 +01:00
|
|
|
return file;
|
2018-06-09 09:40:05 -04:00
|
|
|
}
|
2025-02-03 23:32:04 +01:00
|
|
|
/*
|
|
|
|
|
* Disable all fsnotify events for pseudo files by default.
|
|
|
|
|
* They may be enabled by caller with file_set_fsnotify_mode().
|
|
|
|
|
*/
|
|
|
|
|
file_set_fsnotify_mode(file, FMODE_NONOTIFY);
|
2018-06-09 09:40:05 -04:00
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(alloc_file_pseudo);
|
|
|
|
|
|
2024-02-08 19:10:45 +01:00
|
|
|
struct file *alloc_file_pseudo_noaccount(struct inode *inode,
|
|
|
|
|
struct vfsmount *mnt, const char *name,
|
|
|
|
|
int flags,
|
|
|
|
|
const struct file_operations *fops)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
struct path path;
|
|
|
|
|
struct file *file;
|
|
|
|
|
|
|
|
|
|
ret = alloc_path_pseudo(name, inode, mnt, &path);
|
|
|
|
|
if (ret)
|
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
|
|
|
|
|
|
file = alloc_empty_file_noaccount(flags, current_cred());
|
|
|
|
|
if (IS_ERR(file)) {
|
|
|
|
|
ihold(inode);
|
|
|
|
|
path_put(&path);
|
|
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
file_init_path(file, &path, fops);
|
2025-02-03 23:32:04 +01:00
|
|
|
/*
|
|
|
|
|
* Disable all fsnotify events for pseudo files by default.
|
|
|
|
|
* They may be enabled by caller with file_set_fsnotify_mode().
|
|
|
|
|
*/
|
|
|
|
|
file_set_fsnotify_mode(file, FMODE_NONOTIFY);
|
2024-02-08 19:10:45 +01:00
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL_GPL(alloc_file_pseudo_noaccount);
|
|
|
|
|
|
2018-06-17 14:15:10 -04:00
|
|
|
struct file *alloc_file_clone(struct file *base, int flags,
|
|
|
|
|
const struct file_operations *fops)
|
|
|
|
|
{
|
2024-07-27 12:51:34 +05:30
|
|
|
struct file *f;
|
|
|
|
|
|
|
|
|
|
f = alloc_file(&base->f_path, flags, fops);
|
2018-06-17 14:15:10 -04:00
|
|
|
if (!IS_ERR(f)) {
|
|
|
|
|
path_get(&f->f_path);
|
|
|
|
|
f->f_mapping = base->f_mapping;
|
|
|
|
|
}
|
|
|
|
|
return f;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-26 15:13:55 -04:00
|
|
|
/* the real guts of fput() - releasing the last reference to file
|
2005-04-16 15:20:36 -07:00
|
|
|
*/
|
2010-05-26 15:13:55 -04:00
|
|
|
static void __fput(struct file *file)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2006-12-08 02:36:35 -08:00
|
|
|
struct dentry *dentry = file->f_path.dentry;
|
|
|
|
|
struct vfsmount *mnt = file->f_path.mnt;
|
2013-06-13 23:37:49 +01:00
|
|
|
struct inode *inode = file->f_inode;
|
2018-11-05 17:40:30 +00:00
|
|
|
fmode_t mode = file->f_mode;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2018-07-09 11:14:39 -04:00
|
|
|
if (unlikely(!(file->f_mode & FMODE_OPENED)))
|
|
|
|
|
goto out;
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
might_sleep();
|
2005-07-12 17:06:03 -04:00
|
|
|
|
|
|
|
|
fsnotify_close(file);
|
2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
|
* The function eventpoll_release() should be the first called
|
|
|
|
|
* in the file cleanup chain.
|
|
|
|
|
*/
|
|
|
|
|
eventpoll_release(file);
|
2014-02-03 12:13:08 -05:00
|
|
|
locks_remove_file(file);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2024-02-15 11:31:01 +01:00
|
|
|
security_file_release(file);
|
2008-10-31 23:28:30 +00:00
|
|
|
if (unlikely(file->f_flags & FASYNC)) {
|
2013-09-22 16:27:52 -04:00
|
|
|
if (file->f_op->fasync)
|
2008-10-31 23:28:30 +00:00
|
|
|
file->f_op->fasync(-1, file, 0);
|
|
|
|
|
}
|
2013-09-22 16:27:52 -04:00
|
|
|
if (file->f_op->release)
|
2005-04-16 15:20:36 -07:00
|
|
|
file->f_op->release(inode, file);
|
2011-03-16 18:17:54 +01:00
|
|
|
if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL &&
|
2018-11-05 17:40:30 +00:00
|
|
|
!(mode & FMODE_PATH))) {
|
2005-04-16 15:20:36 -07:00
|
|
|
cdev_put(inode->i_cdev);
|
2011-03-16 18:17:54 +01:00
|
|
|
}
|
2005-04-16 15:20:36 -07:00
|
|
|
fops_put(file->f_op);
|
2024-08-09 18:00:01 +02:00
|
|
|
file_f_owner_release(file);
|
2022-08-16 17:53:17 +03:00
|
|
|
put_file_access(file);
|
2005-04-16 15:20:36 -07:00
|
|
|
dput(dentry);
|
2018-11-05 17:40:30 +00:00
|
|
|
if (unlikely(mode & FMODE_NEED_UNMOUNT))
|
|
|
|
|
dissolve_on_fput(mnt);
|
2005-04-16 15:20:36 -07:00
|
|
|
mntput(mnt);
|
2018-07-09 11:14:39 -04:00
|
|
|
out:
|
|
|
|
|
file_free(file);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
2013-07-08 14:24:16 -07:00
|
|
|
static LLIST_HEAD(delayed_fput_list);
|
2012-06-24 09:56:45 +04:00
|
|
|
static void delayed_fput(struct work_struct *unused)
|
|
|
|
|
{
|
2013-07-08 14:24:16 -07:00
|
|
|
struct llist_node *node = llist_del_all(&delayed_fput_list);
|
2017-08-07 17:45:39 +09:00
|
|
|
struct file *f, *t;
|
2013-07-08 14:24:16 -07:00
|
|
|
|
2022-05-22 09:28:12 -04:00
|
|
|
llist_for_each_entry_safe(f, t, node, f_llist)
|
2017-08-07 17:45:39 +09:00
|
|
|
__fput(f);
|
2012-06-24 09:56:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ____fput(struct callback_head *work)
|
|
|
|
|
{
|
2023-11-30 13:49:09 +01:00
|
|
|
__fput(container_of(work, struct file, f_task_work));
|
2012-06-24 09:56:45 +04:00
|
|
|
}
|
|
|
|
|
|
2024-10-23 13:58:50 +08:00
|
|
|
static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
|
|
|
|
|
|
2012-06-24 09:56:45 +04:00
|
|
|
/*
|
|
|
|
|
* If kernel thread really needs to have the final fput() it has done
|
|
|
|
|
* to complete, call this. The only user right now is the boot - we
|
|
|
|
|
* *do* need to make sure our writes to binaries on initramfs has
|
|
|
|
|
* not left us with opened struct file waiting for __fput() - execve()
|
|
|
|
|
* won't work without that. Please, don't add more callers without
|
|
|
|
|
* very good reasons; in particular, never call that with locks
|
|
|
|
|
* held and never call that from a thread that might need to do
|
|
|
|
|
* some work on any kind of umount.
|
|
|
|
|
*/
|
|
|
|
|
void flush_delayed_fput(void)
|
|
|
|
|
{
|
|
|
|
|
delayed_fput(NULL);
|
2024-10-23 13:58:50 +08:00
|
|
|
flush_delayed_work(&delayed_fput_work);
|
2012-06-24 09:56:45 +04:00
|
|
|
}
|
2019-08-18 14:18:47 -04:00
|
|
|
EXPORT_SYMBOL_GPL(flush_delayed_fput);
|
2012-06-24 09:56:45 +04:00
|
|
|
|
2025-03-05 13:36:41 +01:00
|
|
|
static void __fput_deferred(struct file *file)
|
|
|
|
|
{
|
|
|
|
|
struct task_struct *task = current;
|
|
|
|
|
|
|
|
|
|
if (unlikely(!(file->f_mode & (FMODE_BACKING | FMODE_OPENED)))) {
|
|
|
|
|
file_free(file);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
|
|
|
|
|
init_task_work(&file->f_task_work, ____fput);
|
|
|
|
|
if (!task_work_add(task, &file->f_task_work, TWA_RESUME))
|
|
|
|
|
return;
|
|
|
|
|
/*
|
|
|
|
|
* After this task has run exit_task_work(),
|
|
|
|
|
* task_work_add() will fail. Fall through to delayed
|
|
|
|
|
* fput to avoid leaking *file.
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (llist_add(&file->f_llist, &delayed_fput_list))
|
|
|
|
|
schedule_delayed_work(&delayed_fput_work, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-02 10:46:48 +08:00
|
|
|
void fput(struct file *file)
|
2010-05-26 15:13:55 -04:00
|
|
|
{
|
2025-03-05 13:36:41 +01:00
|
|
|
if (unlikely(file_ref_put(&file->f_ref)))
|
|
|
|
|
__fput_deferred(file);
|
2012-06-24 09:56:45 +04:00
|
|
|
}
|
2025-03-05 13:36:41 +01:00
|
|
|
EXPORT_SYMBOL(fput);
|
2012-06-24 09:56:45 +04:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* synchronous analog of fput(); for kernel threads that might be needed
|
|
|
|
|
* in some umount() (and thus can't use flush_delayed_fput() without
|
|
|
|
|
* risking deadlocks), need to wait for completion of __fput() and know
|
|
|
|
|
* for this specific struct file it won't involve anything that would
|
|
|
|
|
* need them. Use only if you really need it - at the very least,
|
|
|
|
|
* don't blindly convert fput() by kernel thread to that.
|
|
|
|
|
*/
|
|
|
|
|
void __fput_sync(struct file *file)
|
|
|
|
|
{
|
2024-10-07 16:23:59 +02:00
|
|
|
if (file_ref_put(&file->f_ref))
|
2010-05-26 15:13:55 -04:00
|
|
|
__fput(file);
|
|
|
|
|
}
|
2022-04-03 15:58:11 -04:00
|
|
|
EXPORT_SYMBOL(__fput_sync);
|
2010-05-26 15:13:55 -04:00
|
|
|
|
2025-03-05 13:36:41 +01:00
|
|
|
/*
|
|
|
|
|
* Equivalent to __fput_sync(), but optimized for being called with the last
|
|
|
|
|
* reference.
|
|
|
|
|
*
|
|
|
|
|
* See file_ref_put_close() for details.
|
|
|
|
|
*/
|
|
|
|
|
void fput_close_sync(struct file *file)
|
|
|
|
|
{
|
|
|
|
|
if (likely(file_ref_put_close(&file->f_ref)))
|
|
|
|
|
__fput(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Equivalent to fput(), but optimized for being called with the last
|
|
|
|
|
* reference.
|
|
|
|
|
*
|
|
|
|
|
* See file_ref_put_close() for details.
|
|
|
|
|
*/
|
|
|
|
|
void fput_close(struct file *file)
|
|
|
|
|
{
|
|
|
|
|
if (file_ref_put_close(&file->f_ref))
|
|
|
|
|
__fput_deferred(file);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-06 15:46:20 -07:00
|
|
|
void __init files_init(void)
|
2017-08-07 17:45:39 +09:00
|
|
|
{
|
2024-09-05 09:56:56 +02:00
|
|
|
struct kmem_cache_args args = {
|
|
|
|
|
.use_freeptr_offset = true,
|
|
|
|
|
.freeptr_offset = offsetof(struct file, f_freeptr),
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-28 18:37:28 +01:00
|
|
|
__filp_cache = kmem_cache_create("filp", sizeof(struct file), &args,
|
2024-09-05 09:56:56 +02:00
|
|
|
SLAB_HWCACHE_ALIGN | SLAB_PANIC |
|
|
|
|
|
SLAB_ACCOUNT | SLAB_TYPESAFE_BY_RCU);
|
2026-03-28 18:37:28 +01:00
|
|
|
runtime_const_init(ptr, __filp_cache);
|
2024-10-07 16:23:57 +02:00
|
|
|
|
|
|
|
|
args.freeptr_offset = offsetof(struct backing_file, bf_freeptr);
|
2026-03-28 18:37:28 +01:00
|
|
|
__bfilp_cache = kmem_cache_create("bfilp", sizeof(struct backing_file),
|
2024-10-07 16:23:57 +02:00
|
|
|
&args, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
|
|
|
|
|
SLAB_ACCOUNT | SLAB_TYPESAFE_BY_RCU);
|
2026-03-28 18:37:28 +01:00
|
|
|
runtime_const_init(ptr, __bfilp_cache);
|
|
|
|
|
|
2014-09-08 09:51:29 +09:00
|
|
|
percpu_counter_init(&nr_files, 0, GFP_KERNEL);
|
2015-08-06 15:46:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* One file with associated inode and dcache is very roughly 1K. Per default
|
|
|
|
|
* do not use more than 10% of our memory for files.
|
|
|
|
|
*/
|
|
|
|
|
void __init files_maxfiles_init(void)
|
|
|
|
|
{
|
|
|
|
|
unsigned long n;
|
2018-12-28 00:34:29 -08:00
|
|
|
unsigned long nr_pages = totalram_pages();
|
2018-12-28 00:34:20 -08:00
|
|
|
unsigned long memreserve = (nr_pages - nr_free_pages()) * 3/2;
|
2015-08-06 15:46:20 -07:00
|
|
|
|
2018-12-28 00:34:20 -08:00
|
|
|
memreserve = min(memreserve, nr_pages - 1);
|
|
|
|
|
n = ((nr_pages - memreserve) * (PAGE_SIZE / 1024)) / 10;
|
2015-08-06 15:46:20 -07:00
|
|
|
|
|
|
|
|
files_stat.max_files = max_t(unsigned long, n, NR_FILE);
|
2017-08-07 17:45:39 +09:00
|
|
|
}
|