MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / file_table.c
blob3750a140ef431bcc3c7f2603477df989aa12d781
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/mount.h>
18 #include <linux/cdev.h>
20 /* sysctl tunables... */
21 struct files_stat_struct files_stat = {
22 .max_files = NR_FILE
25 EXPORT_SYMBOL(files_stat); /* Needed by unix.o */
27 /* public *and* exported. Not pretty! */
28 spinlock_t __cacheline_aligned_in_smp files_lock = SPIN_LOCK_UNLOCKED;
30 EXPORT_SYMBOL(files_lock);
32 static spinlock_t filp_count_lock = SPIN_LOCK_UNLOCKED;
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(struct file *f)
59 kmem_cache_free(filp_cachep, f);
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.
66 struct file *get_empty_filp(void)
68 static int old_max;
69 struct file * f;
72 * Privileged users can go above max_files
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;
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;
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");
103 fail:
104 return NULL;
107 EXPORT_SYMBOL(get_empty_filp);
109 void fastcall fput(struct file *file)
111 if (atomic_dec_and_test(&file->f_count))
112 __fput(file);
115 EXPORT_SYMBOL(fput);
117 /* __fput is called from task context when aio completion releases the last
118 * last use of a struct file *. Do not use otherwise.
120 void fastcall __fput(struct file *file)
122 struct dentry *dentry = file->f_dentry;
123 struct vfsmount *mnt = file->f_vfsmnt;
124 struct inode *inode = dentry->d_inode;
126 might_sleep();
128 * The function eventpoll_release() should be the first called
129 * in the file cleanup chain.
131 eventpoll_release(file);
132 locks_remove_flock(file);
134 if (file->f_op && file->f_op->release)
135 file->f_op->release(inode, file);
136 security_file_free(file);
137 if (unlikely(inode->i_cdev != NULL))
138 cdev_put(inode->i_cdev);
139 fops_put(file->f_op);
140 if (file->f_mode & FMODE_WRITE)
141 put_write_access(inode);
142 file_kill(file);
143 file->f_dentry = NULL;
144 file->f_vfsmnt = NULL;
145 file_free(file);
146 dput(dentry);
147 mntput(mnt);
150 struct file fastcall *fget(unsigned int fd)
152 struct file *file;
153 struct files_struct *files = current->files;
155 spin_lock(&files->file_lock);
156 file = fcheck_files(files, fd);
157 if (file)
158 get_file(file);
159 spin_unlock(&files->file_lock);
160 return file;
163 EXPORT_SYMBOL(fget);
166 * Lightweight file lookup - no refcnt increment if fd table isn't shared.
167 * You can use this only if it is guranteed that the current task already
168 * holds a refcnt to that file. That check has to be done at fget() only
169 * and a flag is returned to be passed to the corresponding fput_light().
170 * There must not be a cloning between an fget_light/fput_light pair.
172 struct file fastcall *fget_light(unsigned int fd, int *fput_needed)
174 struct file *file;
175 struct files_struct *files = current->files;
177 *fput_needed = 0;
178 if (likely((atomic_read(&files->count) == 1))) {
179 file = fcheck_files(files, fd);
180 } else {
181 spin_lock(&files->file_lock);
182 file = fcheck_files(files, fd);
183 if (file) {
184 get_file(file);
185 *fput_needed = 1;
187 spin_unlock(&files->file_lock);
189 return file;
193 void put_filp(struct file *file)
195 if (atomic_dec_and_test(&file->f_count)) {
196 security_file_free(file);
197 file_kill(file);
198 file_free(file);
202 EXPORT_SYMBOL(put_filp);
204 void file_move(struct file *file, struct list_head *list)
206 if (!list)
207 return;
208 file_list_lock();
209 list_move(&file->f_list, list);
210 file_list_unlock();
213 void file_kill(struct file *file)
215 if (!list_empty(&file->f_list)) {
216 file_list_lock();
217 list_del_init(&file->f_list);
218 file_list_unlock();
222 int fs_may_remount_ro(struct super_block *sb)
224 struct list_head *p;
226 /* Check that no files are currently opened for writing. */
227 file_list_lock();
228 list_for_each(p, &sb->s_files) {
229 struct file *file = list_entry(p, struct file, f_list);
230 struct inode *inode = file->f_dentry->d_inode;
232 /* File with pending delete? */
233 if (inode->i_nlink == 0)
234 goto too_bad;
236 /* Writeable file? */
237 if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
238 goto too_bad;
240 file_list_unlock();
241 return 1; /* Tis' cool bro. */
242 too_bad:
243 file_list_unlock();
244 return 0;
247 void __init files_init(unsigned long mempages)
249 int n;
250 /* One file with associated inode and dcache is very roughly 1K.
251 * Per default don't use more than 10% of our memory for files.
254 n = (mempages * (PAGE_SIZE / 1024)) / 10;
255 files_stat.max_files = n;
256 if (files_stat.max_files < NR_FILE)
257 files_stat.max_files = NR_FILE;