031f3cb859554de117031d4490c8bf67c5a9b220
[linux-2.6.git] / fs / file_table.c
1 /*
2  *  linux/fs/file_table.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *  Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
6  */
7
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>
14 #include <linux/fs.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>
21
22 /* sysctl tunables... */
23 struct files_stat_struct files_stat = {
24         .max_files = NR_FILE
25 };
26
27 EXPORT_SYMBOL(files_stat); /* Needed by unix.o */
28
29 /* public. Not pretty! */
30  __cacheline_aligned_in_smp DEFINE_SPINLOCK(files_lock);
31
32 static DEFINE_SPINLOCK(filp_count_lock);
33
34 /* slab constructors and destructors are called from arbitrary
35  * context and must be fully threaded - use a local spinlock
36  * to protect files_stat.nr_files
37  */
38 void filp_ctor(void * objp, struct kmem_cache_s *cachep, unsigned long cflags)
39 {
40         if ((cflags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
41             SLAB_CTOR_CONSTRUCTOR) {
42                 unsigned long flags;
43                 spin_lock_irqsave(&filp_count_lock, flags);
44                 files_stat.nr_files++;
45                 spin_unlock_irqrestore(&filp_count_lock, flags);
46         }
47 }
48
49 void filp_dtor(void * objp, struct kmem_cache_s *cachep, unsigned long dflags)
50 {
51         unsigned long flags;
52         spin_lock_irqsave(&filp_count_lock, flags);
53         files_stat.nr_files--;
54         spin_unlock_irqrestore(&filp_count_lock, flags);
55 }
56
57 static inline void file_free(struct file *f)
58 {
59         kmem_cache_free(filp_cachep, f);
60 }
61
62 /* Find an unused file structure and return a pointer to it.
63  * Returns NULL, if there are no more free file structures or
64  * we run out of memory.
65  */
66 struct file *get_empty_filp(void)
67 {
68 static int old_max;
69         struct file * f;
70
71         /*
72          * Privileged users can go above max_files
73          */
74         if (files_stat.nr_files < files_stat.max_files ||
75                                 capable(CAP_SYS_ADMIN)) {
76                 f = kmem_cache_alloc(filp_cachep, GFP_KERNEL);
77                 if (f) {
78                         memset(f, 0, sizeof(*f));
79                         if (security_file_alloc(f)) {
80                                 file_free(f);
81                                 goto fail;
82                         }
83                         eventpoll_init_file(f);
84                         atomic_set(&f->f_count, 1);
85                         f->f_uid = current->fsuid;
86                         f->f_gid = current->fsgid;
87                         rwlock_init(&f->f_owner.lock);
88                         /* f->f_version: 0 */
89                         INIT_LIST_HEAD(&f->f_list);
90                         // set_vx_info(&f->f_vx_info, current->vx_info);
91                         f->f_xid = vx_current_xid();
92                         vx_files_inc(f);
93                         f->f_maxcount = INT_MAX;
94                         return f;
95                 }
96         }
97
98         /* Ran out of filps - report that */
99         if (files_stat.max_files >= old_max) {
100                 printk(KERN_INFO "VFS: file-max limit %d reached\n",
101                                         files_stat.max_files);
102                 old_max = files_stat.max_files;
103         } else {
104                 /* Big problems... */
105                 printk(KERN_WARNING "VFS: filp allocation failed\n");
106         }
107 fail:
108         return NULL;
109 }
110
111 EXPORT_SYMBOL(get_empty_filp);
112
113 void fastcall fput(struct file *file)
114 {
115         if (atomic_dec_and_test(&file->f_count))
116                 __fput(file);
117 }
118
119 EXPORT_SYMBOL(fput);
120
121 /* __fput is called from task context when aio completion releases the last
122  * last use of a struct file *.  Do not use otherwise.
123  */
124 void fastcall __fput(struct file *file)
125 {
126         struct dentry *dentry = file->f_dentry;
127         struct vfsmount *mnt = file->f_vfsmnt;
128         struct inode *inode = dentry->d_inode;
129
130         might_sleep();
131         /*
132          * The function eventpoll_release() should be the first called
133          * in the file cleanup chain.
134          */
135         eventpoll_release(file);
136         locks_remove_flock(file);
137
138         if (file->f_op && file->f_op->release)
139                 file->f_op->release(inode, file);
140         security_file_free(file);
141         if (unlikely(inode->i_cdev != NULL))
142                 cdev_put(inode->i_cdev);
143         fops_put(file->f_op);
144         if (file->f_mode & FMODE_WRITE)
145                 put_write_access(inode);
146         vx_files_dec(file);
147         file->f_xid = 0;
148         file_kill(file);
149         file->f_dentry = NULL;
150         file->f_vfsmnt = NULL;
151         file_free(file);
152         dput(dentry);
153         mntput(mnt);
154 }
155
156 struct file fastcall *fget(unsigned int fd)
157 {
158         struct file *file;
159         struct files_struct *files = current->files;
160
161         spin_lock(&files->file_lock);
162         file = fcheck_files(files, fd);
163         if (file)
164                 get_file(file);
165         spin_unlock(&files->file_lock);
166         return file;
167 }
168
169 EXPORT_SYMBOL(fget);
170
171 /*
172  * Lightweight file lookup - no refcnt increment if fd table isn't shared. 
173  * You can use this only if it is guranteed that the current task already 
174  * holds a refcnt to that file. That check has to be done at fget() only
175  * and a flag is returned to be passed to the corresponding fput_light().
176  * There must not be a cloning between an fget_light/fput_light pair.
177  */
178 struct file fastcall *fget_light(unsigned int fd, int *fput_needed)
179 {
180         struct file *file;
181         struct files_struct *files = current->files;
182
183         *fput_needed = 0;
184         if (likely((atomic_read(&files->count) == 1))) {
185                 file = fcheck_files(files, fd);
186         } else {
187                 spin_lock(&files->file_lock);
188                 file = fcheck_files(files, fd);
189                 if (file) {
190                         get_file(file);
191                         *fput_needed = 1;
192                 }
193                 spin_unlock(&files->file_lock);
194         }
195         return file;
196 }
197
198
199 void put_filp(struct file *file)
200 {
201         if (atomic_dec_and_test(&file->f_count)) {
202                 security_file_free(file);
203                 vx_files_dec(file);
204                 file->f_xid = 0;
205                 file_kill(file);
206                 file_free(file);
207         }
208 }
209
210 void file_move(struct file *file, struct list_head *list)
211 {
212         if (!list)
213                 return;
214         file_list_lock();
215         list_move(&file->f_list, list);
216         file_list_unlock();
217 }
218
219 void file_kill(struct file *file)
220 {
221         if (!list_empty(&file->f_list)) {
222                 file_list_lock();
223                 list_del_init(&file->f_list);
224                 file_list_unlock();
225         }
226 }
227
228 int fs_may_remount_ro(struct super_block *sb)
229 {
230         struct list_head *p;
231
232         /* Check that no files are currently opened for writing. */
233         file_list_lock();
234         list_for_each(p, &sb->s_files) {
235                 struct file *file = list_entry(p, struct file, f_list);
236                 struct inode *inode = file->f_dentry->d_inode;
237
238                 /* File with pending delete? */
239                 if (inode->i_nlink == 0)
240                         goto too_bad;
241
242                 /* Writeable file? */
243                 if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
244                         goto too_bad;
245         }
246         file_list_unlock();
247         return 1; /* Tis' cool bro. */
248 too_bad:
249         file_list_unlock();
250         return 0;
251 }
252
253 void __init files_init(unsigned long mempages)
254
255         int n; 
256         /* One file with associated inode and dcache is very roughly 1K. 
257          * Per default don't use more than 10% of our memory for files. 
258          */ 
259
260         n = (mempages * (PAGE_SIZE / 1024)) / 10;
261         files_stat.max_files = n; 
262         if (files_stat.max_files < NR_FILE)
263                 files_stat.max_files = NR_FILE;
264