]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/file_table.c
[PATCH] move capable() to capability.h
[net-next-2.6.git] / fs / file_table.c
CommitLineData
1da177e4
LT
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>
ab2af1f5 17#include <linux/rcupdate.h>
1da177e4
LT
18#include <linux/mount.h>
19#include <linux/cdev.h>
0eeca283 20#include <linux/fsnotify.h>
1da177e4
LT
21
22/* sysctl tunables... */
23struct files_stat_struct files_stat = {
24 .max_files = NR_FILE
25};
26
27EXPORT_SYMBOL(files_stat); /* Needed by unix.o */
28
29/* public. Not pretty! */
30 __cacheline_aligned_in_smp DEFINE_SPINLOCK(files_lock);
31
32static 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 */
2109a2d1 38void filp_ctor(void *objp, struct kmem_cache *cachep, unsigned long cflags)
1da177e4
LT
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
2109a2d1 49void filp_dtor(void *objp, struct kmem_cache *cachep, unsigned long dflags)
1da177e4
LT
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
ab2af1f5 57static inline void file_free_rcu(struct rcu_head *head)
1da177e4 58{
2f512016 59 struct file *f = container_of(head, struct file, f_u.fu_rcuhead);
1da177e4
LT
60 kmem_cache_free(filp_cachep, f);
61}
62
ab2af1f5
DS
63static inline void file_free(struct file *f)
64{
2f512016 65 call_rcu(&f->f_u.fu_rcuhead, file_free_rcu);
ab2af1f5
DS
66}
67
1da177e4
LT
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 */
72struct file *get_empty_filp(void)
73{
af4d2ecb 74 static int old_max;
1da177e4
LT
75 struct file * f;
76
77 /*
78 * Privileged users can go above max_files
79 */
af4d2ecb
KK
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;
1da177e4 91
af4d2ecb
KK
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 */
2f512016 98 INIT_LIST_HEAD(&f->f_u.fu_list);
af4d2ecb
KK
99 return f;
100
101over:
1da177e4 102 /* Ran out of filps - report that */
af4d2ecb 103 if (files_stat.nr_files > old_max) {
1da177e4
LT
104 printk(KERN_INFO "VFS: file-max limit %d reached\n",
105 files_stat.max_files);
af4d2ecb 106 old_max = files_stat.nr_files;
1da177e4 107 }
af4d2ecb
KK
108 goto fail;
109
110fail_sec:
111 file_free(f);
1da177e4
LT
112fail:
113 return NULL;
114}
115
116EXPORT_SYMBOL(get_empty_filp);
117
118void fastcall fput(struct file *file)
119{
095975da 120 if (atomic_dec_and_test(&file->f_count))
1da177e4
LT
121 __fput(file);
122}
123
124EXPORT_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 */
129void 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();
0eeca283
RL
136
137 fsnotify_close(file);
1da177e4
LT
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
161struct file fastcall *fget(unsigned int fd)
162{
163 struct file *file;
164 struct files_struct *files = current->files;
165
ab2af1f5 166 rcu_read_lock();
1da177e4 167 file = fcheck_files(files, fd);
ab2af1f5 168 if (file) {
095975da 169 if (!atomic_inc_not_zero(&file->f_count)) {
ab2af1f5
DS
170 /* File object ref couldn't be taken */
171 rcu_read_unlock();
172 return NULL;
173 }
174 }
175 rcu_read_unlock();
176
1da177e4
LT
177 return file;
178}
179
180EXPORT_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 */
189struct 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 {
ab2af1f5 198 rcu_read_lock();
1da177e4
LT
199 file = fcheck_files(files, fd);
200 if (file) {
095975da 201 if (atomic_inc_not_zero(&file->f_count))
ab2af1f5
DS
202 *fput_needed = 1;
203 else
204 /* Didn't get the reference, someone's freed */
205 file = NULL;
1da177e4 206 }
ab2af1f5 207 rcu_read_unlock();
1da177e4 208 }
ab2af1f5 209
1da177e4
LT
210 return file;
211}
212
213
214void put_filp(struct file *file)
215{
095975da 216 if (atomic_dec_and_test(&file->f_count)) {
1da177e4
LT
217 security_file_free(file);
218 file_kill(file);
219 file_free(file);
220 }
221}
222
223void file_move(struct file *file, struct list_head *list)
224{
225 if (!list)
226 return;
227 file_list_lock();
2f512016 228 list_move(&file->f_u.fu_list, list);
1da177e4
LT
229 file_list_unlock();
230}
231
232void file_kill(struct file *file)
233{
2f512016 234 if (!list_empty(&file->f_u.fu_list)) {
1da177e4 235 file_list_lock();
2f512016 236 list_del_init(&file->f_u.fu_list);
1da177e4
LT
237 file_list_unlock();
238 }
239}
240
241int 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) {
2f512016 248 struct file *file = list_entry(p, struct file, f_u.fu_list);
1da177e4
LT
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. */
261too_bad:
262 file_list_unlock();
263 return 0;
264}
265
266void __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;
ab2af1f5 277 files_defer_init();
1da177e4 278}