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