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/percpu_counter.h>
24#include <linux/ima.h>
25
26#include <asm/atomic.h>
27
28#include "internal.h"
29
30
31struct files_stat_struct files_stat = {
32 .max_files = NR_FILE
33};
34
35
36__cacheline_aligned_in_smp DEFINE_SPINLOCK(files_lock);
37
38
39static struct kmem_cache *filp_cachep __read_mostly;
40
41static struct percpu_counter nr_files __cacheline_aligned_in_smp;
42
43static inline void file_free_rcu(struct rcu_head *head)
44{
45 struct file *f = container_of(head, struct file, f_u.fu_rcuhead);
46
47 put_cred(f->f_cred);
48 kmem_cache_free(filp_cachep, f);
49}
50
51static inline void file_free(struct file *f)
52{
53 percpu_counter_dec(&nr_files);
54 file_check_state(f);
55 call_rcu(&f->f_u.fu_rcuhead, file_free_rcu);
56}
57
58
59
60
61static int get_nr_files(void)
62{
63 return percpu_counter_read_positive(&nr_files);
64}
65
66
67
68
69int get_max_files(void)
70{
71 return files_stat.max_files;
72}
73EXPORT_SYMBOL_GPL(get_max_files);
74
75
76
77
78#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
79int proc_nr_files(ctl_table *table, int write,
80 void __user *buffer, size_t *lenp, loff_t *ppos)
81{
82 files_stat.nr_files = get_nr_files();
83 return proc_dointvec(table, write, buffer, lenp, ppos);
84}
85#else
86int proc_nr_files(ctl_table *table, int write,
87 void __user *buffer, size_t *lenp, loff_t *ppos)
88{
89 return -ENOSYS;
90}
91#endif
92
93
94
95
96
97
98
99
100
101
102
103struct file *get_empty_filp(void)
104{
105 const struct cred *cred = current_cred();
106 static int old_max;
107 struct file * f;
108
109
110
111
112 if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
113
114
115
116
117 if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
118 goto over;
119 }
120
121 f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
122 if (f == NULL)
123 goto fail;
124
125 percpu_counter_inc(&nr_files);
126 if (security_file_alloc(f))
127 goto fail_sec;
128
129 INIT_LIST_HEAD(&f->f_u.fu_list);
130 atomic_long_set(&f->f_count, 1);
131 rwlock_init(&f->f_owner.lock);
132 f->f_cred = get_cred(cred);
133 spin_lock_init(&f->f_lock);
134 eventpoll_init_file(f);
135
136 return f;
137
138over:
139
140 if (get_nr_files() > old_max) {
141 printk(KERN_INFO "VFS: file-max limit %d reached\n",
142 get_max_files());
143 old_max = get_nr_files();
144 }
145 goto fail;
146
147fail_sec:
148 file_free(f);
149fail:
150 return NULL;
151}
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168struct file *alloc_file(struct path *path, fmode_t mode,
169 const struct file_operations *fop)
170{
171 struct file *file;
172
173 file = get_empty_filp();
174 if (!file)
175 return NULL;
176
177 file->f_path = *path;
178 file->f_mapping = path->dentry->d_inode->i_mapping;
179 file->f_mode = mode;
180 file->f_op = fop;
181
182
183
184
185
186
187
188 if ((mode & FMODE_WRITE) && !special_file(path->dentry->d_inode->i_mode)) {
189 file_take_write(file);
190 WARN_ON(mnt_clone_write(path->mnt));
191 }
192 ima_counts_get(file);
193 return file;
194}
195EXPORT_SYMBOL(alloc_file);
196
197
198
199
200
201
202
203
204
205void drop_file_write_access(struct file *file)
206{
207 struct vfsmount *mnt = file->f_path.mnt;
208 struct dentry *dentry = file->f_path.dentry;
209 struct inode *inode = dentry->d_inode;
210
211 put_write_access(inode);
212
213 if (special_file(inode->i_mode))
214 return;
215 if (file_check_writeable(file) != 0)
216 return;
217 mnt_drop_write(mnt);
218 file_release_write(file);
219}
220EXPORT_SYMBOL_GPL(drop_file_write_access);
221
222
223
224static void __fput(struct file *file)
225{
226 struct dentry *dentry = file->f_path.dentry;
227 struct vfsmount *mnt = file->f_path.mnt;
228 struct inode *inode = dentry->d_inode;
229
230 might_sleep();
231
232 fsnotify_close(file);
233
234
235
236
237 eventpoll_release(file);
238 locks_remove_flock(file);
239
240 if (unlikely(file->f_flags & FASYNC)) {
241 if (file->f_op && file->f_op->fasync)
242 file->f_op->fasync(-1, file, 0);
243 }
244 if (file->f_op && file->f_op->release)
245 file->f_op->release(inode, file);
246 security_file_free(file);
247 ima_file_free(file);
248 if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL))
249 cdev_put(inode->i_cdev);
250 fops_put(file->f_op);
251 put_pid(file->f_owner.pid);
252 file_kill(file);
253 if (file->f_mode & FMODE_WRITE)
254 drop_file_write_access(file);
255 file->f_path.dentry = NULL;
256 file->f_path.mnt = NULL;
257 file_free(file);
258 dput(dentry);
259 mntput(mnt);
260}
261
262void fput(struct file *file)
263{
264 if (atomic_long_dec_and_test(&file->f_count))
265 __fput(file);
266}
267
268EXPORT_SYMBOL(fput);
269
270struct file *fget(unsigned int fd)
271{
272 struct file *file;
273 struct files_struct *files = current->files;
274
275 rcu_read_lock();
276 file = fcheck_files(files, fd);
277 if (file) {
278 if (!atomic_long_inc_not_zero(&file->f_count)) {
279
280 rcu_read_unlock();
281 return NULL;
282 }
283 }
284 rcu_read_unlock();
285
286 return file;
287}
288
289EXPORT_SYMBOL(fget);
290
291
292
293
294
295
296
297
298struct file *fget_light(unsigned int fd, int *fput_needed)
299{
300 struct file *file;
301 struct files_struct *files = current->files;
302
303 *fput_needed = 0;
304 if (likely((atomic_read(&files->count) == 1))) {
305 file = fcheck_files(files, fd);
306 } else {
307 rcu_read_lock();
308 file = fcheck_files(files, fd);
309 if (file) {
310 if (atomic_long_inc_not_zero(&file->f_count))
311 *fput_needed = 1;
312 else
313
314 file = NULL;
315 }
316 rcu_read_unlock();
317 }
318
319 return file;
320}
321
322
323void put_filp(struct file *file)
324{
325 if (atomic_long_dec_and_test(&file->f_count)) {
326 security_file_free(file);
327 file_kill(file);
328 file_free(file);
329 }
330}
331
332void file_move(struct file *file, struct list_head *list)
333{
334 if (!list)
335 return;
336 file_list_lock();
337 list_move(&file->f_u.fu_list, list);
338 file_list_unlock();
339}
340
341void file_kill(struct file *file)
342{
343 if (!list_empty(&file->f_u.fu_list)) {
344 file_list_lock();
345 list_del_init(&file->f_u.fu_list);
346 file_list_unlock();
347 }
348}
349
350int fs_may_remount_ro(struct super_block *sb)
351{
352 struct file *file;
353
354
355 file_list_lock();
356 list_for_each_entry(file, &sb->s_files, f_u.fu_list) {
357 struct inode *inode = file->f_path.dentry->d_inode;
358
359
360 if (inode->i_nlink == 0)
361 goto too_bad;
362
363
364 if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
365 goto too_bad;
366 }
367 file_list_unlock();
368 return 1;
369too_bad:
370 file_list_unlock();
371 return 0;
372}
373
374
375
376
377
378
379
380
381void mark_files_ro(struct super_block *sb)
382{
383 struct file *f;
384
385retry:
386 file_list_lock();
387 list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
388 struct vfsmount *mnt;
389 if (!S_ISREG(f->f_path.dentry->d_inode->i_mode))
390 continue;
391 if (!file_count(f))
392 continue;
393 if (!(f->f_mode & FMODE_WRITE))
394 continue;
395 spin_lock(&f->f_lock);
396 f->f_mode &= ~FMODE_WRITE;
397 spin_unlock(&f->f_lock);
398 if (file_check_writeable(f) != 0)
399 continue;
400 file_release_write(f);
401 mnt = mntget(f->f_path.mnt);
402 file_list_unlock();
403
404
405
406
407 mnt_drop_write(mnt);
408 mntput(mnt);
409 goto retry;
410 }
411 file_list_unlock();
412}
413
414void __init files_init(unsigned long mempages)
415{
416 int n;
417
418 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
419 SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
420
421
422
423
424
425
426 n = (mempages * (PAGE_SIZE / 1024)) / 10;
427 files_stat.max_files = n;
428 if (files_stat.max_files < NR_FILE)
429 files_stat.max_files = NR_FILE;
430 files_defer_init();
431 percpu_counter_init(&nr_files, 0);
432}
433