1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/fs/file_table.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
9 #include <linux/string.h>
10 #include <linux/slab.h>
11 #include <linux/file.h>
12 #include <linux/fdtable.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
16 #include <linux/filelock.h>
17 #include <linux/security.h>
18 #include <linux/cred.h>
19 #include <linux/eventpoll.h>
20 #include <linux/rcupdate.h>
21 #include <linux/mount.h>
22 #include <linux/capability.h>
23 #include <linux/cdev.h>
24 #include <linux/fsnotify.h>
25 #include <linux/sysctl.h>
26 #include <linux/percpu_counter.h>
27 #include <linux/percpu.h>
28 #include <linux/task_work.h>
29 #include <linux/swap.h>
30 #include <linux/kmemleak.h>
32 #include <linux/atomic.h>
36 /* sysctl tunables... */
37 static struct files_stat_struct files_stat
= {
41 /* SLAB cache for file structures */
42 static struct kmem_cache
*filp_cachep __ro_after_init
;
44 static struct percpu_counter nr_files __cacheline_aligned_in_smp
;
46 /* Container for backing file with optional user path */
49 struct path user_path
;
52 static inline struct backing_file
*backing_file(struct file
*f
)
54 return container_of(f
, struct backing_file
, file
);
57 struct path
*backing_file_user_path(struct file
*f
)
59 return &backing_file(f
)->user_path
;
61 EXPORT_SYMBOL_GPL(backing_file_user_path
);
63 static inline void file_free(struct file
*f
)
65 security_file_free(f
);
66 if (likely(!(f
->f_mode
& FMODE_NOACCOUNT
)))
67 percpu_counter_dec(&nr_files
);
69 if (unlikely(f
->f_mode
& FMODE_BACKING
)) {
70 path_put(backing_file_user_path(f
));
71 kfree(backing_file(f
));
73 kmem_cache_free(filp_cachep
, f
);
78 * Return the total number of open files in the system
80 static long get_nr_files(void)
82 return percpu_counter_read_positive(&nr_files
);
86 * Return the maximum number of open files in the system
88 unsigned long get_max_files(void)
90 return files_stat
.max_files
;
92 EXPORT_SYMBOL_GPL(get_max_files
);
94 #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
97 * Handle nr_files sysctl
99 static int proc_nr_files(const struct ctl_table
*table
, int write
, void *buffer
,
100 size_t *lenp
, loff_t
*ppos
)
102 files_stat
.nr_files
= get_nr_files();
103 return proc_doulongvec_minmax(table
, write
, buffer
, lenp
, ppos
);
106 static struct ctl_table fs_stat_sysctls
[] = {
108 .procname
= "file-nr",
110 .maxlen
= sizeof(files_stat
),
112 .proc_handler
= proc_nr_files
,
115 .procname
= "file-max",
116 .data
= &files_stat
.max_files
,
117 .maxlen
= sizeof(files_stat
.max_files
),
119 .proc_handler
= proc_doulongvec_minmax
,
120 .extra1
= SYSCTL_LONG_ZERO
,
121 .extra2
= SYSCTL_LONG_MAX
,
124 .procname
= "nr_open",
125 .data
= &sysctl_nr_open
,
126 .maxlen
= sizeof(unsigned int),
128 .proc_handler
= proc_dointvec_minmax
,
129 .extra1
= &sysctl_nr_open_min
,
130 .extra2
= &sysctl_nr_open_max
,
134 static int __init
init_fs_stat_sysctls(void)
136 register_sysctl_init("fs", fs_stat_sysctls
);
137 if (IS_ENABLED(CONFIG_BINFMT_MISC
)) {
138 struct ctl_table_header
*hdr
;
139 hdr
= register_sysctl_mount_point("fs/binfmt_misc");
140 kmemleak_not_leak(hdr
);
144 fs_initcall(init_fs_stat_sysctls
);
147 static int init_file(struct file
*f
, int flags
, const struct cred
*cred
)
151 f
->f_cred
= get_cred(cred
);
152 error
= security_file_alloc(f
);
153 if (unlikely(error
)) {
158 rwlock_init(&f
->f_owner
.lock
);
159 spin_lock_init(&f
->f_lock
);
160 mutex_init(&f
->f_pos_lock
);
162 f
->f_mode
= OPEN_FMODE(flags
);
163 /* f->f_version: 0 */
166 * We're SLAB_TYPESAFE_BY_RCU so initialize f_count last. While
167 * fget-rcu pattern users need to be able to handle spurious
168 * refcount bumps we should reinitialize the reused file first.
170 atomic_long_set(&f
->f_count
, 1);
174 /* Find an unused file structure and return a pointer to it.
175 * Returns an error pointer if some error happend e.g. we over file
176 * structures limit, run out of memory or operation is not permitted.
178 * Be very careful using this. You are responsible for
179 * getting write access to any mount that you might assign
180 * to this filp, if it is opened for write. If this is not
181 * done, you will imbalance int the mount's writer count
182 * and a warning at __fput() time.
184 struct file
*alloc_empty_file(int flags
, const struct cred
*cred
)
191 * Privileged users can go above max_files
193 if (get_nr_files() >= files_stat
.max_files
&& !capable(CAP_SYS_ADMIN
)) {
195 * percpu_counters are inaccurate. Do an expensive check before
198 if (percpu_counter_sum_positive(&nr_files
) >= files_stat
.max_files
)
202 f
= kmem_cache_zalloc(filp_cachep
, GFP_KERNEL
);
204 return ERR_PTR(-ENOMEM
);
206 error
= init_file(f
, flags
, cred
);
207 if (unlikely(error
)) {
208 kmem_cache_free(filp_cachep
, f
);
209 return ERR_PTR(error
);
212 percpu_counter_inc(&nr_files
);
217 /* Ran out of filps - report that */
218 if (get_nr_files() > old_max
) {
219 pr_info("VFS: file-max limit %lu reached\n", get_max_files());
220 old_max
= get_nr_files();
222 return ERR_PTR(-ENFILE
);
226 * Variant of alloc_empty_file() that doesn't check and modify nr_files.
228 * This is only for kernel internal use, and the allocate file must not be
229 * installed into file tables or such.
231 struct file
*alloc_empty_file_noaccount(int flags
, const struct cred
*cred
)
236 f
= kmem_cache_zalloc(filp_cachep
, GFP_KERNEL
);
238 return ERR_PTR(-ENOMEM
);
240 error
= init_file(f
, flags
, cred
);
241 if (unlikely(error
)) {
242 kmem_cache_free(filp_cachep
, f
);
243 return ERR_PTR(error
);
246 f
->f_mode
|= FMODE_NOACCOUNT
;
252 * Variant of alloc_empty_file() that allocates a backing_file container
253 * and doesn't check and modify nr_files.
255 * This is only for kernel internal use, and the allocate file must not be
256 * installed into file tables or such.
258 struct file
*alloc_empty_backing_file(int flags
, const struct cred
*cred
)
260 struct backing_file
*ff
;
263 ff
= kzalloc(sizeof(struct backing_file
), GFP_KERNEL
);
265 return ERR_PTR(-ENOMEM
);
267 error
= init_file(&ff
->file
, flags
, cred
);
268 if (unlikely(error
)) {
270 return ERR_PTR(error
);
273 ff
->file
.f_mode
|= FMODE_BACKING
| FMODE_NOACCOUNT
;
278 * file_init_path - initialize a 'struct file' based on path
280 * @file: the file to set up
281 * @path: the (dentry, vfsmount) pair for the new file
282 * @fop: the 'struct file_operations' for the new file
284 static void file_init_path(struct file
*file
, const struct path
*path
,
285 const struct file_operations
*fop
)
287 file
->f_path
= *path
;
288 file
->f_inode
= path
->dentry
->d_inode
;
289 file
->f_mapping
= path
->dentry
->d_inode
->i_mapping
;
290 file
->f_wb_err
= filemap_sample_wb_err(file
->f_mapping
);
291 file
->f_sb_err
= file_sample_sb_err(file
);
293 file
->f_mode
|= FMODE_LSEEK
;
294 if ((file
->f_mode
& FMODE_READ
) &&
295 likely(fop
->read
|| fop
->read_iter
))
296 file
->f_mode
|= FMODE_CAN_READ
;
297 if ((file
->f_mode
& FMODE_WRITE
) &&
298 likely(fop
->write
|| fop
->write_iter
))
299 file
->f_mode
|= FMODE_CAN_WRITE
;
300 file
->f_iocb_flags
= iocb_flags(file
);
301 file
->f_mode
|= FMODE_OPENED
;
303 if ((file
->f_mode
& (FMODE_READ
| FMODE_WRITE
)) == FMODE_READ
)
304 i_readcount_inc(path
->dentry
->d_inode
);
308 * alloc_file - allocate and initialize a 'struct file'
310 * @path: the (dentry, vfsmount) pair for the new file
311 * @flags: O_... flags with which the new file will be opened
312 * @fop: the 'struct file_operations' for the new file
314 static struct file
*alloc_file(const struct path
*path
, int flags
,
315 const struct file_operations
*fop
)
319 file
= alloc_empty_file(flags
, current_cred());
321 file_init_path(file
, path
, fop
);
325 static inline int alloc_path_pseudo(const char *name
, struct inode
*inode
,
326 struct vfsmount
*mnt
, struct path
*path
)
328 struct qstr
this = QSTR_INIT(name
, strlen(name
));
330 path
->dentry
= d_alloc_pseudo(mnt
->mnt_sb
, &this);
333 path
->mnt
= mntget(mnt
);
334 d_instantiate(path
->dentry
, inode
);
338 struct file
*alloc_file_pseudo(struct inode
*inode
, struct vfsmount
*mnt
,
339 const char *name
, int flags
,
340 const struct file_operations
*fops
)
346 ret
= alloc_path_pseudo(name
, inode
, mnt
, &path
);
350 file
= alloc_file(&path
, flags
, fops
);
357 EXPORT_SYMBOL(alloc_file_pseudo
);
359 struct file
*alloc_file_pseudo_noaccount(struct inode
*inode
,
360 struct vfsmount
*mnt
, const char *name
,
362 const struct file_operations
*fops
)
368 ret
= alloc_path_pseudo(name
, inode
, mnt
, &path
);
372 file
= alloc_empty_file_noaccount(flags
, current_cred());
378 file_init_path(file
, &path
, fops
);
381 EXPORT_SYMBOL_GPL(alloc_file_pseudo_noaccount
);
383 struct file
*alloc_file_clone(struct file
*base
, int flags
,
384 const struct file_operations
*fops
)
386 struct file
*f
= alloc_file(&base
->f_path
, flags
, fops
);
388 path_get(&f
->f_path
);
389 f
->f_mapping
= base
->f_mapping
;
394 /* the real guts of fput() - releasing the last reference to file
396 static void __fput(struct file
*file
)
398 struct dentry
*dentry
= file
->f_path
.dentry
;
399 struct vfsmount
*mnt
= file
->f_path
.mnt
;
400 struct inode
*inode
= file
->f_inode
;
401 fmode_t mode
= file
->f_mode
;
403 if (unlikely(!(file
->f_mode
& FMODE_OPENED
)))
408 fsnotify_close(file
);
410 * The function eventpoll_release() should be the first called
411 * in the file cleanup chain.
413 eventpoll_release(file
);
414 locks_remove_file(file
);
416 security_file_release(file
);
417 if (unlikely(file
->f_flags
& FASYNC
)) {
418 if (file
->f_op
->fasync
)
419 file
->f_op
->fasync(-1, file
, 0);
421 if (file
->f_op
->release
)
422 file
->f_op
->release(inode
, file
);
423 if (unlikely(S_ISCHR(inode
->i_mode
) && inode
->i_cdev
!= NULL
&&
424 !(mode
& FMODE_PATH
))) {
425 cdev_put(inode
->i_cdev
);
427 fops_put(file
->f_op
);
428 put_pid(file
->f_owner
.pid
);
429 put_file_access(file
);
431 if (unlikely(mode
& FMODE_NEED_UNMOUNT
))
432 dissolve_on_fput(mnt
);
438 static LLIST_HEAD(delayed_fput_list
);
439 static void delayed_fput(struct work_struct
*unused
)
441 struct llist_node
*node
= llist_del_all(&delayed_fput_list
);
444 llist_for_each_entry_safe(f
, t
, node
, f_llist
)
448 static void ____fput(struct callback_head
*work
)
450 __fput(container_of(work
, struct file
, f_task_work
));
454 * If kernel thread really needs to have the final fput() it has done
455 * to complete, call this. The only user right now is the boot - we
456 * *do* need to make sure our writes to binaries on initramfs has
457 * not left us with opened struct file waiting for __fput() - execve()
458 * won't work without that. Please, don't add more callers without
459 * very good reasons; in particular, never call that with locks
460 * held and never call that from a thread that might need to do
461 * some work on any kind of umount.
463 void flush_delayed_fput(void)
467 EXPORT_SYMBOL_GPL(flush_delayed_fput
);
469 static DECLARE_DELAYED_WORK(delayed_fput_work
, delayed_fput
);
471 void fput(struct file
*file
)
473 if (atomic_long_dec_and_test(&file
->f_count
)) {
474 struct task_struct
*task
= current
;
476 if (unlikely(!(file
->f_mode
& (FMODE_BACKING
| FMODE_OPENED
)))) {
480 if (likely(!in_interrupt() && !(task
->flags
& PF_KTHREAD
))) {
481 init_task_work(&file
->f_task_work
, ____fput
);
482 if (!task_work_add(task
, &file
->f_task_work
, TWA_RESUME
))
485 * After this task has run exit_task_work(),
486 * task_work_add() will fail. Fall through to delayed
487 * fput to avoid leaking *file.
491 if (llist_add(&file
->f_llist
, &delayed_fput_list
))
492 schedule_delayed_work(&delayed_fput_work
, 1);
497 * synchronous analog of fput(); for kernel threads that might be needed
498 * in some umount() (and thus can't use flush_delayed_fput() without
499 * risking deadlocks), need to wait for completion of __fput() and know
500 * for this specific struct file it won't involve anything that would
501 * need them. Use only if you really need it - at the very least,
502 * don't blindly convert fput() by kernel thread to that.
504 void __fput_sync(struct file
*file
)
506 if (atomic_long_dec_and_test(&file
->f_count
))
511 EXPORT_SYMBOL(__fput_sync
);
513 void __init
files_init(void)
515 filp_cachep
= kmem_cache_create("filp", sizeof(struct file
), 0,
516 SLAB_TYPESAFE_BY_RCU
| SLAB_HWCACHE_ALIGN
|
517 SLAB_PANIC
| SLAB_ACCOUNT
, NULL
);
518 percpu_counter_init(&nr_files
, 0, GFP_KERNEL
);
522 * One file with associated inode and dcache is very roughly 1K. Per default
523 * do not use more than 10% of our memory for files.
525 void __init
files_maxfiles_init(void)
528 unsigned long nr_pages
= totalram_pages();
529 unsigned long memreserve
= (nr_pages
- nr_free_pages()) * 3/2;
531 memreserve
= min(memreserve
, nr_pages
- 1);
532 n
= ((nr_pages
- memreserve
) * (PAGE_SIZE
/ 1024)) / 10;
534 files_stat
.max_files
= max_t(unsigned long, n
, NR_FILE
);