Linux 2.2.0
[davej-history.git] / fs / file_table.c
blobcbc33634aa4eb3be93377aff77fc45294efe9995
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 put_inuse(f);
85 return f;
88 * Use a reserved one if we're the superuser
90 if (nr_free_files && !current->euid)
91 goto used_one;
93 * Allocate a new one if we're below the limit.
95 if (nr_files < max_files) {
96 f = kmem_cache_alloc(filp_cache, SLAB_KERNEL);
97 if (f) {
98 nr_files++;
99 goto new_one;
101 /* Big problems... */
102 printk("VFS: filp allocation failed\n");
104 } else if (max_files > old_max) {
105 printk("VFS: file-max limit %d reached\n", max_files);
106 old_max = max_files;
108 return NULL;
112 * Clear and initialize a (private) struct file for the given dentry,
113 * and call the open function (if any). The caller must verify that
114 * inode->i_op and inode->i_op->default_file_ops are not NULL.
116 int init_private_file(struct file *filp, struct dentry *dentry, int mode)
118 memset(filp, 0, sizeof(*filp));
119 filp->f_mode = mode;
120 filp->f_count = 1;
121 filp->f_dentry = dentry;
122 filp->f_op = dentry->d_inode->i_op->default_file_ops;
123 if (filp->f_op->open)
124 return filp->f_op->open(dentry->d_inode, filp);
125 else
126 return 0;
129 void fput(struct file *file)
131 int count = file->f_count-1;
133 if (!count) {
134 locks_remove_flock(file);
135 __fput(file);
136 file->f_count = 0;
137 remove_filp(file);
138 insert_file_free(file);
139 } else
140 file->f_count = count;
143 void put_filp(struct file *file)
145 if(--file->f_count == 0) {
146 remove_filp(file);
147 insert_file_free(file);