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