patch-2.6.6-vs1.9.0
[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
20 /* sysctl tunables... */
21 struct files_stat_struct files_stat = {
22         .max_files = NR_FILE
23 };
24
25 EXPORT_SYMBOL(files_stat); /* Needed by unix.o */
26
27 /* public *and* exported. Not pretty! */
28 spinlock_t __cacheline_aligned_in_smp files_lock = SPIN_LOCK_UNLOCKED;
29
30 EXPORT_SYMBOL(files_lock);
31
32 static spinlock_t filp_count_lock = SPIN_LOCK_UNLOCKED;
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                         f->f_owner.lock = RW_LOCK_UNLOCKED;
88                         /* f->f_version: 0 */
89                         INIT_LIST_HEAD(&f->f_list);
90                         vx_files_inc(f);
91                         return f;
92                 }
93         }
94
95         /* Ran out of filps - report that */
96         if (files_stat.max_files >= old_max) {
97                 printk(KERN_INFO "VFS: file-max limit %d reached\n",
98                                         files_stat.max_files);
99                 old_max = files_stat.max_files;
100         } else {
101                 /* Big problems... */
102                 printk(KERN_WARNING "VFS: filp allocation failed\n");
103         }
104 fail:
105         return NULL;
106 }
107
108 EXPORT_SYMBOL(get_empty_filp);
109
110 /*
111  * Clear and initialize a (private) struct file for the given dentry,
112  * allocate the security structure, and call the open function (if any).  
113  * The file should be released using close_private_file.
114  */
115 int open_private_file(struct file *filp, struct dentry *dentry, int flags)
116 {
117         int error;
118         memset(filp, 0, sizeof(*filp));
119         eventpoll_init_file(filp);
120         filp->f_flags  = flags;
121         filp->f_mode   = (flags+1) & O_ACCMODE;
122         atomic_set(&filp->f_count, 1);
123         filp->f_dentry = dentry;
124         filp->f_mapping = dentry->d_inode->i_mapping;
125         filp->f_uid    = current->fsuid;
126         filp->f_gid    = current->fsgid;
127         filp->f_op     = dentry->d_inode->i_fop;
128         INIT_LIST_HEAD(&filp->f_list);
129         error = security_file_alloc(filp);
130         if (!error)
131                 if (filp->f_op && filp->f_op->open) {
132                         error = filp->f_op->open(dentry->d_inode, filp);
133                         if (error)
134                                 security_file_free(filp);
135                 }
136         return error;
137 }
138
139 EXPORT_SYMBOL(open_private_file);
140
141 /*
142  * Release a private file by calling the release function (if any) and
143  * freeing the security structure.
144  */
145 void close_private_file(struct file *file)
146 {
147         struct inode * inode = file->f_dentry->d_inode;
148
149         if (file->f_op && file->f_op->release)
150                 file->f_op->release(inode, file);
151         security_file_free(file);
152 }
153
154 EXPORT_SYMBOL(close_private_file);
155
156 void fastcall fput(struct file *file)
157 {
158         if (atomic_dec_and_test(&file->f_count))
159                 __fput(file);
160 }
161
162 EXPORT_SYMBOL(fput);
163
164 /* __fput is called from task context when aio completion releases the last
165  * last use of a struct file *.  Do not use otherwise.
166  */
167 void fastcall __fput(struct file *file)
168 {
169         struct dentry *dentry = file->f_dentry;
170         struct vfsmount *mnt = file->f_vfsmnt;
171         struct inode *inode = dentry->d_inode;
172
173         /*
174          * The function eventpoll_release() should be the first called
175          * in the file cleanup chain.
176          */
177         eventpoll_release(file);
178         locks_remove_flock(file);
179
180         if (file->f_op && file->f_op->release)
181                 file->f_op->release(inode, file);
182         security_file_free(file);
183         if (unlikely(inode->i_cdev != NULL))
184                 cdev_put(inode->i_cdev);
185         fops_put(file->f_op);
186         if (file->f_mode & FMODE_WRITE)
187                 put_write_access(inode);
188         vx_files_dec(file);
189         file_kill(file);
190         file->f_dentry = NULL;
191         file->f_vfsmnt = NULL;
192         file_free(file);
193         dput(dentry);
194         mntput(mnt);
195 }
196
197 struct file fastcall *fget(unsigned int fd)
198 {
199         struct file *file;
200         struct files_struct *files = current->files;
201
202         spin_lock(&files->file_lock);
203         file = fcheck_files(files, fd);
204         if (file)
205                 get_file(file);
206         spin_unlock(&files->file_lock);
207         return file;
208 }
209
210 EXPORT_SYMBOL(fget);
211
212 /*
213  * Lightweight file lookup - no refcnt increment if fd table isn't shared. 
214  * You can use this only if it is guranteed that the current task already 
215  * holds a refcnt to that file. That check has to be done at fget() only
216  * and a flag is returned to be passed to the corresponding fput_light().
217  * There must not be a cloning between an fget_light/fput_light pair.
218  */
219 struct file fastcall *fget_light(unsigned int fd, int *fput_needed)
220 {
221         struct file *file;
222         struct files_struct *files = current->files;
223
224         *fput_needed = 0;
225         if (likely((atomic_read(&files->count) == 1))) {
226                 file = fcheck_files(files, fd);
227         } else {
228                 spin_lock(&files->file_lock);
229                 file = fcheck_files(files, fd);
230                 if (file) {
231                         get_file(file);
232                         *fput_needed = 1;
233                 }
234                 spin_unlock(&files->file_lock);
235         }
236         return file;
237 }
238
239
240 void put_filp(struct file *file)
241 {
242         if (atomic_dec_and_test(&file->f_count)) {
243                 security_file_free(file);
244                 file_kill(file);
245                 file_free(file);
246         }
247 }
248
249 EXPORT_SYMBOL(put_filp);
250
251 void file_move(struct file *file, struct list_head *list)
252 {
253         if (!list)
254                 return;
255         file_list_lock();
256         list_move(&file->f_list, list);
257         file_list_unlock();
258 }
259
260 void file_kill(struct file *file)
261 {
262         if (!list_empty(&file->f_list)) {
263                 file_list_lock();
264                 list_del_init(&file->f_list);
265                 file_list_unlock();
266         }
267 }
268
269 int fs_may_remount_ro(struct super_block *sb)
270 {
271         struct list_head *p;
272
273         /* Check that no files are currently opened for writing. */
274         file_list_lock();
275         list_for_each(p, &sb->s_files) {
276                 struct file *file = list_entry(p, struct file, f_list);
277                 struct inode *inode = file->f_dentry->d_inode;
278
279                 /* File with pending delete? */
280                 if (inode->i_nlink == 0)
281                         goto too_bad;
282
283                 /* Writeable file? */
284                 if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
285                         goto too_bad;
286         }
287         file_list_unlock();
288         return 1; /* Tis' cool bro. */
289 too_bad:
290         file_list_unlock();
291         return 0;
292 }
293
294 void __init files_init(unsigned long mempages)
295
296         int n; 
297         /* One file with associated inode and dcache is very roughly 1K. 
298          * Per default don't use more than 10% of our memory for files. 
299          */ 
300
301         n = (mempages * (PAGE_SIZE / 1024)) / 10;
302         files_stat.max_files = n; 
303         if (files_stat.max_files < NR_FILE)
304                 files_stat.max_files = NR_FILE;
305