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