1
2
3
4
5
6
7
8#include <linux/string.h>
9#include <linux/slab.h>
10#include <linux/file.h>
11#include <linux/fdtable.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/fs.h>
15#include <linux/security.h>
16#include <linux/eventpoll.h>
17#include <linux/rcupdate.h>
18#include <linux/mount.h>
19#include <linux/capability.h>
20#include <linux/cdev.h>
21#include <linux/fsnotify.h>
22#include <linux/sysctl.h>
23#include <linux/lglock.h>
24#include <linux/percpu_counter.h>
25#include <linux/percpu.h>
26#include <linux/ima.h>
27
28#include <linux/atomic.h>
29
30#include "internal.h"
31
32
33struct files_stat_struct files_stat = {
34 .max_files = NR_FILE
35};
36
37DECLARE_LGLOCK(files_lglock);
38DEFINE_LGLOCK(files_lglock);
39
40
41static struct kmem_cache *filp_cachep __read_mostly;
42
43static struct percpu_counter nr_files __cacheline_aligned_in_smp;
44
45static inline void file_free_rcu(struct rcu_head *head)
46{
47 struct file *f = container_of(head, struct file, f_u.fu_rcuhead);
48
49 put_cred(f->f_cred);
50 kmem_cache_free(filp_cachep, f);
51}
52
53static inline void file_free(struct file *f)
54{
55 percpu_counter_dec(&nr_files);
56 file_check_state(f);
57 call_rcu(&f->f_u.fu_rcuhead, file_free_rcu);
58}
59
60
61
62
63static long get_nr_files(void)
64{
65 return percpu_counter_read_positive(&nr_files);
66}
67
68
69
70
71unsigned long get_max_files(void)
72{
73 return files_stat.max_files;
74}
75EXPORT_SYMBOL_GPL(get_max_files);
76
77
78
79
80#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
81int proc_nr_files(ctl_table *table, int write,
82 void __user *buffer, size_t *lenp, loff_t *ppos)
83{
84 files_stat.nr_files = get_nr_files();
85 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
86}
87#else
88int proc_nr_files(ctl_table *table, int write,
89 void __user *buffer, size_t *lenp, loff_t *ppos)
90{
91 return -ENOSYS;
92}
93#endif
94
95
96
97
98
99
100
101
102
103
104
105struct file *get_empty_filp(void)
106{
107 const struct cred *cred = current_cred();
108 static long old_max;
109 struct file * f;
110
111
112
113
114 if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
115
116
117
118
119 if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
120 goto over;
121 }
122
123 f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
124 if (f == NULL)
125 goto fail;
126
127 percpu_counter_inc(&nr_files);
128 f->f_cred = get_cred(cred);
129 if (security_file_alloc(f))
130 goto fail_sec;
131
132 INIT_LIST_HEAD(&f->f_u.fu_list);
133 atomic_long_set(&f->f_count, 1);
134 rwlock_init(&f->f_owner.lock);
135 spin_lock_init(&f->f_lock);
136 eventpoll_init_file(f);
137
138 return f;
139
140over:
141
142 if (get_nr_files() > old_max) {
143 pr_info("VFS: file-max limit %lu reached\n", get_max_files());
144 old_max = get_nr_files();
145 }
146 goto fail;
147
148fail_sec:
149 file_free(f);
150fail:
151 return NULL;
152}
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169struct file *alloc_file(struct path *path, fmode_t mode,
170 const struct file_operations *fop)
171{
172 struct file *file;
173
174 file = get_empty_filp();
175 if (!file)
176 return NULL;
177
178 file->f_path = *path;
179 file->f_mapping = path->dentry->d_inode->i_mapping;
180 file->f_mode = mode;
181 file->f_op = fop;
182
183
184
185
186
187
188
189 if ((mode & FMODE_WRITE) && !special_file(path->dentry->d_inode->i_mode)) {
190 file_take_write(file);
191 WARN_ON(mnt_clone_write(path->mnt));
192 }
193 if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
194 i_readcount_inc(path->dentry->d_inode);
195 return file;
196}
197EXPORT_SYMBOL(alloc_file);
198
199
200
201
202
203
204
205
206
207void drop_file_write_access(struct file *file)
208{
209 struct vfsmount *mnt = file->f_path.mnt;
210 struct dentry *dentry = file->f_path.dentry;
211 struct inode *inode = dentry->d_inode;
212
213 put_write_access(inode);
214
215 if (special_file(inode->i_mode))
216 return;
217 if (file_check_writeable(file) != 0)
218 return;
219 mnt_drop_write(mnt);
220 file_release_write(file);
221}
222EXPORT_SYMBOL_GPL(drop_file_write_access);
223
224
225
226static void __fput(struct file *file)
227{
228 struct dentry *dentry = file->f_path.dentry;
229 struct vfsmount *mnt = file->f_path.mnt;
230 struct inode *inode = dentry->d_inode;
231
232 might_sleep();
233
234 fsnotify_close(file);
235
236
237
238
239 eventpoll_release(file);
240 locks_remove_flock(file);
241
242 if (unlikely(file->f_flags & FASYNC)) {
243 if (file->f_op && file->f_op->fasync)
244 file->f_op->fasync(-1, file, 0);
245 }
246 if (file->f_op && file->f_op->release)
247 file->f_op->release(inode, file);
248 security_file_free(file);
249 ima_file_free(file);
250 if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL &&
251 !(file->f_mode & FMODE_PATH))) {
252 cdev_put(inode->i_cdev);
253 }
254 fops_put(file->f_op);
255 put_pid(file->f_owner.pid);
256 file_sb_list_del(file);
257 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
258 i_readcount_dec(inode);
259 if (file->f_mode & FMODE_WRITE)
260 drop_file_write_access(file);
261 file->f_path.dentry = NULL;
262 file->f_path.mnt = NULL;
263 file_free(file);
264 dput(dentry);
265 mntput(mnt);
266}
267
268void fput(struct file *file)
269{
270 if (atomic_long_dec_and_test(&file->f_count))
271 __fput(file);
272}
273
274EXPORT_SYMBOL(fput);
275
276struct file *fget(unsigned int fd)
277{
278 struct file *file;
279 struct files_struct *files = current->files;
280
281 rcu_read_lock();
282 file = fcheck_files(files, fd);
283 if (file) {
284
285 if (file->f_mode & FMODE_PATH ||
286 !atomic_long_inc_not_zero(&file->f_count))
287 file = NULL;
288 }
289 rcu_read_unlock();
290
291 return file;
292}
293
294EXPORT_SYMBOL(fget);
295
296struct file *fget_raw(unsigned int fd)
297{
298 struct file *file;
299 struct files_struct *files = current->files;
300
301 rcu_read_lock();
302 file = fcheck_files(files, fd);
303 if (file) {
304
305 if (!atomic_long_inc_not_zero(&file->f_count))
306 file = NULL;
307 }
308 rcu_read_unlock();
309
310 return file;
311}
312
313EXPORT_SYMBOL(fget_raw);
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331struct file *fget_light(unsigned int fd, int *fput_needed)
332{
333 struct file *file;
334 struct files_struct *files = current->files;
335
336 *fput_needed = 0;
337 if (atomic_read(&files->count) == 1) {
338 file = fcheck_files(files, fd);
339 if (file && (file->f_mode & FMODE_PATH))
340 file = NULL;
341 } else {
342 rcu_read_lock();
343 file = fcheck_files(files, fd);
344 if (file) {
345 if (!(file->f_mode & FMODE_PATH) &&
346 atomic_long_inc_not_zero(&file->f_count))
347 *fput_needed = 1;
348 else
349
350 file = NULL;
351 }
352 rcu_read_unlock();
353 }
354
355 return file;
356}
357
358struct file *fget_raw_light(unsigned int fd, int *fput_needed)
359{
360 struct file *file;
361 struct files_struct *files = current->files;
362
363 *fput_needed = 0;
364 if (atomic_read(&files->count) == 1) {
365 file = fcheck_files(files, fd);
366 } else {
367 rcu_read_lock();
368 file = fcheck_files(files, fd);
369 if (file) {
370 if (atomic_long_inc_not_zero(&file->f_count))
371 *fput_needed = 1;
372 else
373
374 file = NULL;
375 }
376 rcu_read_unlock();
377 }
378
379 return file;
380}
381
382void put_filp(struct file *file)
383{
384 if (atomic_long_dec_and_test(&file->f_count)) {
385 security_file_free(file);
386 file_sb_list_del(file);
387 file_free(file);
388 }
389}
390
391static inline int file_list_cpu(struct file *file)
392{
393#ifdef CONFIG_SMP
394 return file->f_sb_list_cpu;
395#else
396 return smp_processor_id();
397#endif
398}
399
400
401static inline void __file_sb_list_add(struct file *file, struct super_block *sb)
402{
403 struct list_head *list;
404#ifdef CONFIG_SMP
405 int cpu;
406 cpu = smp_processor_id();
407 file->f_sb_list_cpu = cpu;
408 list = per_cpu_ptr(sb->s_files, cpu);
409#else
410 list = &sb->s_files;
411#endif
412 list_add(&file->f_u.fu_list, list);
413}
414
415
416
417
418
419
420
421
422
423void file_sb_list_add(struct file *file, struct super_block *sb)
424{
425 lg_local_lock(files_lglock);
426 __file_sb_list_add(file, sb);
427 lg_local_unlock(files_lglock);
428}
429
430
431
432
433
434
435
436
437void file_sb_list_del(struct file *file)
438{
439 if (!list_empty(&file->f_u.fu_list)) {
440 lg_local_lock_cpu(files_lglock, file_list_cpu(file));
441 list_del_init(&file->f_u.fu_list);
442 lg_local_unlock_cpu(files_lglock, file_list_cpu(file));
443 }
444}
445
446#ifdef CONFIG_SMP
447
448
449
450
451
452#define do_file_list_for_each_entry(__sb, __file) \
453{ \
454 int i; \
455 for_each_possible_cpu(i) { \
456 struct list_head *list; \
457 list = per_cpu_ptr((__sb)->s_files, i); \
458 list_for_each_entry((__file), list, f_u.fu_list)
459
460#define while_file_list_for_each_entry \
461 } \
462}
463
464#else
465
466#define do_file_list_for_each_entry(__sb, __file) \
467{ \
468 struct list_head *list; \
469 list = &(sb)->s_files; \
470 list_for_each_entry((__file), list, f_u.fu_list)
471
472#define while_file_list_for_each_entry \
473}
474
475#endif
476
477
478
479
480
481
482
483
484void mark_files_ro(struct super_block *sb)
485{
486 struct file *f;
487
488retry:
489 lg_global_lock(files_lglock);
490 do_file_list_for_each_entry(sb, f) {
491 struct vfsmount *mnt;
492 if (!S_ISREG(f->f_path.dentry->d_inode->i_mode))
493 continue;
494 if (!file_count(f))
495 continue;
496 if (!(f->f_mode & FMODE_WRITE))
497 continue;
498 spin_lock(&f->f_lock);
499 f->f_mode &= ~FMODE_WRITE;
500 spin_unlock(&f->f_lock);
501 if (file_check_writeable(f) != 0)
502 continue;
503 file_release_write(f);
504 mnt = mntget(f->f_path.mnt);
505
506 lg_global_unlock(files_lglock);
507 mnt_drop_write(mnt);
508 mntput(mnt);
509 goto retry;
510 } while_file_list_for_each_entry;
511 lg_global_unlock(files_lglock);
512}
513
514void __init files_init(unsigned long mempages)
515{
516 unsigned long n;
517
518 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
519 SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
520
521
522
523
524
525
526 n = (mempages * (PAGE_SIZE / 1024)) / 10;
527 files_stat.max_files = max_t(unsigned long, n, NR_FILE);
528 files_defer_init();
529 lg_lock_init(files_lglock);
530 percpu_counter_init(&nr_files, 0);
531}
532