Import 2.2.8pre2
[davej-history.git] / fs / file_table.c
blobf7679dba374dead68e8843b0bc93aaca31a07046
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/slab.h>
9 #include <linux/file.h>
10 #include <linux/init.h>
12 /* SLAB cache for filp's. */
13 static kmem_cache_t *filp_cache;
15 /* sysctl tunables... */
16 int nr_files = 0; /* read only */
17 int nr_free_files = 0; /* read only */
18 int max_files = NR_FILE;/* tunable */
20 /* Free list management, if you are here you must have f_count == 0 */
21 static struct file * free_filps = NULL;
23 static void insert_file_free(struct file *file)
25 if((file->f_next = free_filps) != NULL)
26 free_filps->f_pprev = &file->f_next;
27 free_filps = file;
28 file->f_pprev = &free_filps;
29 nr_free_files++;
32 /* The list of in-use filp's must be exported (ugh...) */
33 struct file *inuse_filps = NULL;
35 static inline void put_inuse(struct file *file)
37 if((file->f_next = inuse_filps) != NULL)
38 inuse_filps->f_pprev = &file->f_next;
39 inuse_filps = file;
40 file->f_pprev = &inuse_filps;
43 /* It does not matter which list it is on. */
44 static inline void remove_filp(struct file *file)
46 if(file->f_next)
47 file->f_next->f_pprev = file->f_pprev;
48 *file->f_pprev = file->f_next;
52 void __init file_table_init(void)
54 filp_cache = kmem_cache_create("filp", sizeof(struct file),
56 SLAB_HWCACHE_ALIGN, NULL, NULL);
57 if(!filp_cache)
58 panic("VFS: Cannot alloc filp SLAB cache.");
60 * We could allocate the reserved files here, but really
61 * shouldn't need to: the normal boot process will create
62 * plenty of free files.
66 /* Find an unused file structure and return a pointer to it.
67 * Returns NULL, if there are no more free file structures or
68 * we run out of memory.
70 struct file * get_empty_filp(void)
72 static int old_max = 0;
73 struct file * f;
75 if (nr_free_files > NR_RESERVED_FILES) {
76 used_one:
77 f = free_filps;
78 remove_filp(f);
79 nr_free_files--;
80 new_one:
81 memset(f, 0, sizeof(*f));
82 f->f_count = 1;
83 f->f_version = ++event;
84 f->f_uid = current->fsuid;
85 f->f_gid = current->fsgid;
86 put_inuse(f);
87 return f;
90 * Use a reserved one if we're the superuser
92 if (nr_free_files && !current->euid)
93 goto used_one;
95 * Allocate a new one if we're below the limit.
97 if (nr_files < max_files) {
98 f = kmem_cache_alloc(filp_cache, SLAB_KERNEL);
99 if (f) {
100 nr_files++;
101 goto new_one;
103 /* Big problems... */
104 printk("VFS: filp allocation failed\n");
106 } else if (max_files > old_max) {
107 printk("VFS: file-max limit %d reached\n", max_files);
108 old_max = max_files;
110 return NULL;
114 * Clear and initialize a (private) struct file for the given dentry,
115 * and call the open function (if any). The caller must verify that
116 * inode->i_op and inode->i_op->default_file_ops are not NULL.
118 int init_private_file(struct file *filp, struct dentry *dentry, int mode)
120 memset(filp, 0, sizeof(*filp));
121 filp->f_mode = mode;
122 filp->f_count = 1;
123 filp->f_dentry = dentry;
124 filp->f_uid = current->fsuid;
125 filp->f_gid = current->fsgid;
126 filp->f_op = dentry->d_inode->i_op->default_file_ops;
127 if (filp->f_op->open)
128 return filp->f_op->open(dentry->d_inode, filp);
129 else
130 return 0;
133 void fput(struct file *file)
135 int count = file->f_count-1;
137 if (!count) {
138 locks_remove_flock(file);
139 __fput(file);
140 file->f_count = 0;
141 remove_filp(file);
142 insert_file_free(file);
143 } else
144 file->f_count = count;
147 void put_filp(struct file *file)
149 if(--file->f_count == 0) {
150 remove_filp(file);
151 insert_file_free(file);