[PATCH] spinlock consolidation
[pandora-kernel.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/rcupdate.h>
18 #include <linux/mount.h>
19 #include <linux/cdev.h>
20 #include <linux/fsnotify.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. Not pretty! */
30  __cacheline_aligned_in_smp DEFINE_SPINLOCK(files_lock);
31
32 static DEFINE_SPINLOCK(filp_count_lock);
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_rcu(struct rcu_head *head)
58 {
59         struct file *f =  container_of(head, struct file, f_rcuhead);
60         kmem_cache_free(filp_cachep, f);
61 }
62
63 static inline void file_free(struct file *f)
64 {
65         call_rcu(&f->f_rcuhead, file_free_rcu);
66 }
67
68 /* Find an unused file structure and return a pointer to it.
69  * Returns NULL, if there are no more free file structures or
70  * we run out of memory.
71  */
72 struct file *get_empty_filp(void)
73 {
74         static int old_max;
75         struct file * f;
76
77         /*
78          * Privileged users can go above max_files
79          */
80         if (files_stat.nr_files >= files_stat.max_files &&
81                                 !capable(CAP_SYS_ADMIN))
82                 goto over;
83
84         f = kmem_cache_alloc(filp_cachep, GFP_KERNEL);
85         if (f == NULL)
86                 goto fail;
87
88         memset(f, 0, sizeof(*f));
89         if (security_file_alloc(f))
90                 goto fail_sec;
91
92         eventpoll_init_file(f);
93         atomic_set(&f->f_count, 1);
94         f->f_uid = current->fsuid;
95         f->f_gid = current->fsgid;
96         rwlock_init(&f->f_owner.lock);
97         /* f->f_version: 0 */
98         INIT_LIST_HEAD(&f->f_list);
99         return f;
100
101 over:
102         /* Ran out of filps - report that */
103         if (files_stat.nr_files > old_max) {
104                 printk(KERN_INFO "VFS: file-max limit %d reached\n",
105                                         files_stat.max_files);
106                 old_max = files_stat.nr_files;
107         }
108         goto fail;
109
110 fail_sec:
111         file_free(f);
112 fail:
113         return NULL;
114 }
115
116 EXPORT_SYMBOL(get_empty_filp);
117
118 void fastcall fput(struct file *file)
119 {
120         if (rcuref_dec_and_test(&file->f_count))
121                 __fput(file);
122 }
123
124 EXPORT_SYMBOL(fput);
125
126 /* __fput is called from task context when aio completion releases the last
127  * last use of a struct file *.  Do not use otherwise.
128  */
129 void fastcall __fput(struct file *file)
130 {
131         struct dentry *dentry = file->f_dentry;
132         struct vfsmount *mnt = file->f_vfsmnt;
133         struct inode *inode = dentry->d_inode;
134
135         might_sleep();
136
137         fsnotify_close(file);
138         /*
139          * The function eventpoll_release() should be the first called
140          * in the file cleanup chain.
141          */
142         eventpoll_release(file);
143         locks_remove_flock(file);
144
145         if (file->f_op && file->f_op->release)
146                 file->f_op->release(inode, file);
147         security_file_free(file);
148         if (unlikely(inode->i_cdev != NULL))
149                 cdev_put(inode->i_cdev);
150         fops_put(file->f_op);
151         if (file->f_mode & FMODE_WRITE)
152                 put_write_access(inode);
153         file_kill(file);
154         file->f_dentry = NULL;
155         file->f_vfsmnt = NULL;
156         file_free(file);
157         dput(dentry);
158         mntput(mnt);
159 }
160
161 struct file fastcall *fget(unsigned int fd)
162 {
163         struct file *file;
164         struct files_struct *files = current->files;
165
166         rcu_read_lock();
167         file = fcheck_files(files, fd);
168         if (file) {
169                 if (!rcuref_inc_lf(&file->f_count)) {
170                         /* File object ref couldn't be taken */
171                         rcu_read_unlock();
172                         return NULL;
173                 }
174         }
175         rcu_read_unlock();
176
177         return file;
178 }
179
180 EXPORT_SYMBOL(fget);
181
182 /*
183  * Lightweight file lookup - no refcnt increment if fd table isn't shared. 
184  * You can use this only if it is guranteed that the current task already 
185  * holds a refcnt to that file. That check has to be done at fget() only
186  * and a flag is returned to be passed to the corresponding fput_light().
187  * There must not be a cloning between an fget_light/fput_light pair.
188  */
189 struct file fastcall *fget_light(unsigned int fd, int *fput_needed)
190 {
191         struct file *file;
192         struct files_struct *files = current->files;
193
194         *fput_needed = 0;
195         if (likely((atomic_read(&files->count) == 1))) {
196                 file = fcheck_files(files, fd);
197         } else {
198                 rcu_read_lock();
199                 file = fcheck_files(files, fd);
200                 if (file) {
201                         if (rcuref_inc_lf(&file->f_count))
202                                 *fput_needed = 1;
203                         else
204                                 /* Didn't get the reference, someone's freed */
205                                 file = NULL;
206                 }
207                 rcu_read_unlock();
208         }
209
210         return file;
211 }
212
213
214 void put_filp(struct file *file)
215 {
216         if (rcuref_dec_and_test(&file->f_count)) {
217                 security_file_free(file);
218                 file_kill(file);
219                 file_free(file);
220         }
221 }
222
223 void file_move(struct file *file, struct list_head *list)
224 {
225         if (!list)
226                 return;
227         file_list_lock();
228         list_move(&file->f_list, list);
229         file_list_unlock();
230 }
231
232 void file_kill(struct file *file)
233 {
234         if (!list_empty(&file->f_list)) {
235                 file_list_lock();
236                 list_del_init(&file->f_list);
237                 file_list_unlock();
238         }
239 }
240
241 int fs_may_remount_ro(struct super_block *sb)
242 {
243         struct list_head *p;
244
245         /* Check that no files are currently opened for writing. */
246         file_list_lock();
247         list_for_each(p, &sb->s_files) {
248                 struct file *file = list_entry(p, struct file, f_list);
249                 struct inode *inode = file->f_dentry->d_inode;
250
251                 /* File with pending delete? */
252                 if (inode->i_nlink == 0)
253                         goto too_bad;
254
255                 /* Writeable file? */
256                 if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
257                         goto too_bad;
258         }
259         file_list_unlock();
260         return 1; /* Tis' cool bro. */
261 too_bad:
262         file_list_unlock();
263         return 0;
264 }
265
266 void __init files_init(unsigned long mempages)
267
268         int n; 
269         /* One file with associated inode and dcache is very roughly 1K. 
270          * Per default don't use more than 10% of our memory for files. 
271          */ 
272
273         n = (mempages * (PAGE_SIZE / 1024)) / 10;
274         files_stat.max_files = n; 
275         if (files_stat.max_files < NR_FILE)
276                 files_stat.max_files = NR_FILE;
277         files_defer_init();
278