Linux 2.6.14.1
[linux/fpc-iii.git] / fs / file_table.c
blob86ec8ae985b413bd3d8b135ff8dfd9fe305ef5bf
1 /*
2 * linux/fs/file_table.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
6 */
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>
22 /* sysctl tunables... */
23 struct files_stat_struct files_stat = {
24 .max_files = NR_FILE
27 EXPORT_SYMBOL(files_stat); /* Needed by unix.o */
29 /* public. Not pretty! */
30 __cacheline_aligned_in_smp DEFINE_SPINLOCK(files_lock);
32 static DEFINE_SPINLOCK(filp_count_lock);
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
38 void filp_ctor(void * objp, struct kmem_cache_s *cachep, unsigned long cflags)
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);
49 void filp_dtor(void * objp, struct kmem_cache_s *cachep, unsigned long dflags)
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);
57 static inline void file_free_rcu(struct rcu_head *head)
59 struct file *f = container_of(head, struct file, f_rcuhead);
60 kmem_cache_free(filp_cachep, f);
63 static inline void file_free(struct file *f)
65 call_rcu(&f->f_rcuhead, file_free_rcu);
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.
72 struct file *get_empty_filp(void)
74 static int old_max;
75 struct file * f;
78 * Privileged users can go above max_files
80 if (files_stat.nr_files >= files_stat.max_files &&
81 !capable(CAP_SYS_ADMIN))
82 goto over;
84 f = kmem_cache_alloc(filp_cachep, GFP_KERNEL);
85 if (f == NULL)
86 goto fail;
88 memset(f, 0, sizeof(*f));
89 if (security_file_alloc(f))
90 goto fail_sec;
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;
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;
108 goto fail;
110 fail_sec:
111 file_free(f);
112 fail:
113 return NULL;
116 EXPORT_SYMBOL(get_empty_filp);
118 void fastcall fput(struct file *file)
120 if (rcuref_dec_and_test(&file->f_count))
121 __fput(file);
124 EXPORT_SYMBOL(fput);
126 /* __fput is called from task context when aio completion releases the last
127 * last use of a struct file *. Do not use otherwise.
129 void fastcall __fput(struct file *file)
131 struct dentry *dentry = file->f_dentry;
132 struct vfsmount *mnt = file->f_vfsmnt;
133 struct inode *inode = dentry->d_inode;
135 might_sleep();
137 fsnotify_close(file);
139 * The function eventpoll_release() should be the first called
140 * in the file cleanup chain.
142 eventpoll_release(file);
143 locks_remove_flock(file);
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);
161 struct file fastcall *fget(unsigned int fd)
163 struct file *file;
164 struct files_struct *files = current->files;
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;
175 rcu_read_unlock();
177 return file;
180 EXPORT_SYMBOL(fget);
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.
189 struct file fastcall *fget_light(unsigned int fd, int *fput_needed)
191 struct file *file;
192 struct files_struct *files = current->files;
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;
207 rcu_read_unlock();
210 return file;
214 void put_filp(struct file *file)
216 if (rcuref_dec_and_test(&file->f_count)) {
217 security_file_free(file);
218 file_kill(file);
219 file_free(file);
223 void file_move(struct file *file, struct list_head *list)
225 if (!list)
226 return;
227 file_list_lock();
228 list_move(&file->f_list, list);
229 file_list_unlock();
232 void file_kill(struct file *file)
234 if (!list_empty(&file->f_list)) {
235 file_list_lock();
236 list_del_init(&file->f_list);
237 file_list_unlock();
241 int fs_may_remount_ro(struct super_block *sb)
243 struct list_head *p;
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;
251 /* File with pending delete? */
252 if (inode->i_nlink == 0)
253 goto too_bad;
255 /* Writeable file? */
256 if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
257 goto too_bad;
259 file_list_unlock();
260 return 1; /* Tis' cool bro. */
261 too_bad:
262 file_list_unlock();
263 return 0;
266 void __init files_init(unsigned long mempages)
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.
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();