blob: 86ec8ae985b413bd3d8b135ff8dfd9fe305ef5bf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070017#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/mount.h>
19#include <linux/cdev.h>
Robert Love0eeca282005-07-12 17:06:03 -040020#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
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 */
38void 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
49void 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
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070057static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static inline void file_free(struct file *f)
64{
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070065 call_rcu(&f->f_rcuhead, file_free_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
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 */
72struct file *get_empty_filp(void)
73{
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -070074 static int old_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 struct file * f;
76
77 /*
78 * Privileged users can go above max_files
79 */
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -070080 if (files_stat.nr_files >= files_stat.max_files &&
81 !capable(CAP_SYS_ADMIN))
82 goto over;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -070084 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);
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -070099 return f;
100
101over:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 /* Ran out of filps - report that */
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -0700103 if (files_stat.nr_files > old_max) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 printk(KERN_INFO "VFS: file-max limit %d reached\n",
105 files_stat.max_files);
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -0700106 old_max = files_stat.nr_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -0700108 goto fail;
109
110fail_sec:
111 file_free(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112fail:
113 return NULL;
114}
115
116EXPORT_SYMBOL(get_empty_filp);
117
118void fastcall fput(struct file *file)
119{
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700120 if (rcuref_dec_and_test(&file->f_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 __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();
Robert Love0eeca282005-07-12 17:06:03 -0400136
137 fsnotify_close(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /*
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
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700166 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 file = fcheck_files(files, fd);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700168 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 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 {
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700198 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 file = fcheck_files(files, fd);
200 if (file) {
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700201 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700207 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return file;
211}
212
213
214void put_filp(struct file *file)
215{
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700216 if (rcuref_dec_and_test(&file->f_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 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();
228 list_move(&file->f_list, list);
229 file_list_unlock();
230}
231
232void 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
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) {
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. */
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;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700277 files_defer_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}