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