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