2 * linux/fs/file_table.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
8 #include <linux/string.h>
9 #include <linux/slab.h>
10 #include <linux/file.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/smp_lock.h>
15 #include <linux/security.h>
16 #include <linux/eventpoll.h>
17 #include <linux/mount.h>
18 #include <linux/cdev.h>
19 #include <linux/vs_limit.h>
20 #include <linux/vs_context.h>
22 /* sysctl tunables... */
23 struct files_stat_struct files_stat = {
27 EXPORT_SYMBOL(files_stat); /* Needed by unix.o */
29 /* public *and* exported. Not pretty! */
30 spinlock_t __cacheline_aligned_in_smp files_lock = SPIN_LOCK_UNLOCKED;
32 EXPORT_SYMBOL(files_lock);
34 static spinlock_t filp_count_lock = SPIN_LOCK_UNLOCKED;
36 /* slab constructors and destructors are called from arbitrary
37 * context and must be fully threaded - use a local spinlock
38 * to protect files_stat.nr_files
40 void filp_ctor(void * objp, struct kmem_cache_s *cachep, unsigned long cflags)
42 if ((cflags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
43 SLAB_CTOR_CONSTRUCTOR) {
45 spin_lock_irqsave(&filp_count_lock, flags);
46 files_stat.nr_files++;
47 spin_unlock_irqrestore(&filp_count_lock, flags);
51 void filp_dtor(void * objp, struct kmem_cache_s *cachep, unsigned long dflags)
54 spin_lock_irqsave(&filp_count_lock, flags);
55 files_stat.nr_files--;
56 spin_unlock_irqrestore(&filp_count_lock, flags);
59 static inline void file_free(struct file *f)
61 kmem_cache_free(filp_cachep, f);
64 /* Find an unused file structure and return a pointer to it.
65 * Returns NULL, if there are no more free file structures or
66 * we run out of memory.
68 struct file *get_empty_filp(void)
74 * Privileged users can go above max_files
76 if (files_stat.nr_files < files_stat.max_files ||
77 capable(CAP_SYS_ADMIN)) {
78 f = kmem_cache_alloc(filp_cachep, GFP_KERNEL);
80 memset(f, 0, sizeof(*f));
81 if (security_file_alloc(f)) {
85 eventpoll_init_file(f);
86 atomic_set(&f->f_count, 1);
87 f->f_uid = current->fsuid;
88 f->f_gid = current->fsgid;
89 f->f_owner.lock = RW_LOCK_UNLOCKED;
91 INIT_LIST_HEAD(&f->f_list);
92 // set_vx_info(&f->f_vx_info, current->vx_info);
93 f->f_xid = current->xid;
99 /* Ran out of filps - report that */
100 if (files_stat.max_files >= old_max) {
101 printk(KERN_INFO "VFS: file-max limit %d reached\n",
102 files_stat.max_files);
103 old_max = files_stat.max_files;
105 /* Big problems... */
106 printk(KERN_WARNING "VFS: filp allocation failed\n");
112 EXPORT_SYMBOL(get_empty_filp);
114 void fastcall fput(struct file *file)
116 if (atomic_dec_and_test(&file->f_count))
122 /* __fput is called from task context when aio completion releases the last
123 * last use of a struct file *. Do not use otherwise.
125 void fastcall __fput(struct file *file)
127 struct dentry *dentry = file->f_dentry;
128 struct vfsmount *mnt = file->f_vfsmnt;
129 struct inode *inode = dentry->d_inode;
133 * The function eventpoll_release() should be the first called
134 * in the file cleanup chain.
136 eventpoll_release(file);
137 locks_remove_flock(file);
139 if (file->f_op && file->f_op->release)
140 file->f_op->release(inode, file);
141 security_file_free(file);
142 if (unlikely(inode->i_cdev != NULL))
143 cdev_put(inode->i_cdev);
144 fops_put(file->f_op);
145 if (file->f_mode & FMODE_WRITE)
146 put_write_access(inode);
150 file->f_dentry = NULL;
151 file->f_vfsmnt = NULL;
157 struct file fastcall *fget(unsigned int fd)
160 struct files_struct *files = current->files;
162 spin_lock(&files->file_lock);
163 file = fcheck_files(files, fd);
166 spin_unlock(&files->file_lock);
173 * Lightweight file lookup - no refcnt increment if fd table isn't shared.
174 * You can use this only if it is guranteed that the current task already
175 * holds a refcnt to that file. That check has to be done at fget() only
176 * and a flag is returned to be passed to the corresponding fput_light().
177 * There must not be a cloning between an fget_light/fput_light pair.
179 struct file fastcall *fget_light(unsigned int fd, int *fput_needed)
182 struct files_struct *files = current->files;
185 if (likely((atomic_read(&files->count) == 1))) {
186 file = fcheck_files(files, fd);
188 spin_lock(&files->file_lock);
189 file = fcheck_files(files, fd);
194 spin_unlock(&files->file_lock);
200 void put_filp(struct file *file)
202 if (atomic_dec_and_test(&file->f_count)) {
203 security_file_free(file);
211 EXPORT_SYMBOL(put_filp);
213 void file_move(struct file *file, struct list_head *list)
218 list_move(&file->f_list, list);
222 void file_kill(struct file *file)
224 if (!list_empty(&file->f_list)) {
226 list_del_init(&file->f_list);
231 int fs_may_remount_ro(struct super_block *sb)
235 /* Check that no files are currently opened for writing. */
237 list_for_each(p, &sb->s_files) {
238 struct file *file = list_entry(p, struct file, f_list);
239 struct inode *inode = file->f_dentry->d_inode;
241 /* File with pending delete? */
242 if (inode->i_nlink == 0)
245 /* Writeable file? */
246 if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
250 return 1; /* Tis' cool bro. */
256 void __init files_init(unsigned long mempages)
259 /* One file with associated inode and dcache is very roughly 1K.
260 * Per default don't use more than 10% of our memory for files.
263 n = (mempages * (PAGE_SIZE / 1024)) / 10;
264 files_stat.max_files = n;
265 if (files_stat.max_files < NR_FILE)
266 files_stat.max_files = NR_FILE;