vserver 1.9.3
[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 *and* exported. Not pretty! */
30 spinlock_t __cacheline_aligned_in_smp files_lock = SPIN_LOCK_UNLOCKED;
31
32 EXPORT_SYMBOL(files_lock);
33
34 static spinlock_t filp_count_lock = SPIN_LOCK_UNLOCKED;
35
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
39  */
40 void filp_ctor(void * objp, struct kmem_cache_s *cachep, unsigned long cflags)
41 {
42         if ((cflags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
43             SLAB_CTOR_CONSTRUCTOR) {
44                 unsigned long flags;
45                 spin_lock_irqsave(&filp_count_lock, flags);
46                 files_stat.nr_files++;
47                 spin_unlock_irqrestore(&filp_count_lock, flags);
48         }
49 }
50
51 void filp_dtor(void * objp, struct kmem_cache_s *cachep, unsigned long dflags)
52 {
53         unsigned long flags;
54         spin_lock_irqsave(&filp_count_lock, flags);
55         files_stat.nr_files--;
56         spin_unlock_irqrestore(&filp_count_lock, flags);
57 }
58
59 static inline void file_free(struct file *f)
60 {
61         kmem_cache_free(filp_cachep, f);
62 }
63
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.
67  */
68 struct file *get_empty_filp(void)
69 {
70 static int old_max;
71         struct file * f;
72
73         /*
74          * Privileged users can go above max_files
75          */
76         if (files_stat.nr_files < files_stat.max_files ||
77                                 capable(CAP_SYS_ADMIN)) {
78                 f = kmem_cache_alloc(filp_cachep, GFP_KERNEL);
79                 if (f) {
80                         memset(f, 0, sizeof(*f));
81                         if (security_file_alloc(f)) {
82                                 file_free(f);
83                                 goto fail;
84                         }
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;
90                         /* f->f_version: 0 */
91                         INIT_LIST_HEAD(&f->f_list);
92                         // set_vx_info(&f->f_vx_info, current->vx_info);
93                         f->f_xid = current->xid;
94                         vx_files_inc(f);
95                         return f;
96                 }
97         }
98
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;
104         } else {
105                 /* Big problems... */
106                 printk(KERN_WARNING "VFS: filp allocation failed\n");
107         }
108 fail:
109         return NULL;
110 }
111
112 EXPORT_SYMBOL(get_empty_filp);
113
114 void fastcall fput(struct file *file)
115 {
116         if (atomic_dec_and_test(&file->f_count))
117                 __fput(file);
118 }
119
120 EXPORT_SYMBOL(fput);
121
122 /* __fput is called from task context when aio completion releases the last
123  * last use of a struct file *.  Do not use otherwise.
124  */
125 void fastcall __fput(struct file *file)
126 {
127         struct dentry *dentry = file->f_dentry;
128         struct vfsmount *mnt = file->f_vfsmnt;
129         struct inode *inode = dentry->d_inode;
130
131         might_sleep();
132         /*
133          * The function eventpoll_release() should be the first called
134          * in the file cleanup chain.
135          */
136         eventpoll_release(file);
137         locks_remove_flock(file);
138
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);
147         vx_files_dec(file);
148         file->f_xid = 0;
149         file_kill(file);
150         file->f_dentry = NULL;
151         file->f_vfsmnt = NULL;
152         file_free(file);
153         dput(dentry);
154         mntput(mnt);
155 }
156
157 struct file fastcall *fget(unsigned int fd)
158 {
159         struct file *file;
160         struct files_struct *files = current->files;
161
162         spin_lock(&files->file_lock);
163         file = fcheck_files(files, fd);
164         if (file)
165                 get_file(file);
166         spin_unlock(&files->file_lock);
167         return file;
168 }
169
170 EXPORT_SYMBOL(fget);
171
172 /*
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.
178  */
179 struct file fastcall *fget_light(unsigned int fd, int *fput_needed)
180 {
181         struct file *file;
182         struct files_struct *files = current->files;
183
184         *fput_needed = 0;
185         if (likely((atomic_read(&files->count) == 1))) {
186                 file = fcheck_files(files, fd);
187         } else {
188                 spin_lock(&files->file_lock);
189                 file = fcheck_files(files, fd);
190                 if (file) {
191                         get_file(file);
192                         *fput_needed = 1;
193                 }
194                 spin_unlock(&files->file_lock);
195         }
196         return file;
197 }
198
199
200 void put_filp(struct file *file)
201 {
202         if (atomic_dec_and_test(&file->f_count)) {
203                 security_file_free(file);
204                 vx_files_dec(file);
205                 file->f_xid = 0;
206                 file_kill(file);
207                 file_free(file);
208         }
209 }
210
211 EXPORT_SYMBOL(put_filp);
212
213 void file_move(struct file *file, struct list_head *list)
214 {
215         if (!list)
216                 return;
217         file_list_lock();
218         list_move(&file->f_list, list);
219         file_list_unlock();
220 }
221
222 void file_kill(struct file *file)
223 {
224         if (!list_empty(&file->f_list)) {
225                 file_list_lock();
226                 list_del_init(&file->f_list);
227                 file_list_unlock();
228         }
229 }
230
231 int fs_may_remount_ro(struct super_block *sb)
232 {
233         struct list_head *p;
234
235         /* Check that no files are currently opened for writing. */
236         file_list_lock();
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;
240
241                 /* File with pending delete? */
242                 if (inode->i_nlink == 0)
243                         goto too_bad;
244
245                 /* Writeable file? */
246                 if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
247                         goto too_bad;
248         }
249         file_list_unlock();
250         return 1; /* Tis' cool bro. */
251 too_bad:
252         file_list_unlock();
253         return 0;
254 }
255
256 void __init files_init(unsigned long mempages)
257
258         int n; 
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. 
261          */ 
262
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;
267