Import 2.3.7pre9
[davej-history.git] / fs / file_table.c
blob45b68daecbd3992f3fe84b674a7a01c6b0c6193c
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>
13 /* SLAB cache for filp's. */
14 static kmem_cache_t *filp_cache;
16 /* sysctl tunables... */
17 int nr_files = 0; /* read only */
18 int nr_free_files = 0; /* read only */
19 int max_files = NR_FILE;/* tunable */
21 /* Free list management, if you are here you must have f_count == 0 */
22 static struct file * free_filps = NULL;
24 static void insert_file_free(struct file *file)
26 if((file->f_next = free_filps) != NULL)
27 free_filps->f_pprev = &file->f_next;
28 free_filps = file;
29 file->f_pprev = &free_filps;
30 nr_free_files++;
33 /* The list of in-use filp's must be exported (ugh...) */
34 struct file *inuse_filps = NULL;
36 static inline void put_inuse(struct file *file)
38 if((file->f_next = inuse_filps) != NULL)
39 inuse_filps->f_pprev = &file->f_next;
40 inuse_filps = file;
41 file->f_pprev = &inuse_filps;
44 /* It does not matter which list it is on. */
45 static inline void remove_filp(struct file *file)
47 if(file->f_next)
48 file->f_next->f_pprev = file->f_pprev;
49 *file->f_pprev = file->f_next;
53 void __init file_table_init(void)
55 filp_cache = kmem_cache_create("filp", sizeof(struct file),
57 SLAB_HWCACHE_ALIGN, NULL, NULL);
58 if(!filp_cache)
59 panic("VFS: Cannot alloc filp SLAB cache.");
61 * We could allocate the reserved files here, but really
62 * shouldn't need to: the normal boot process will create
63 * plenty of free files.
67 /* Find an unused file structure and return a pointer to it.
68 * Returns NULL, if there are no more free file structures or
69 * we run out of memory.
71 struct file * get_empty_filp(void)
73 static int old_max = 0;
74 struct file * f;
76 if (nr_free_files > NR_RESERVED_FILES) {
77 used_one:
78 f = free_filps;
79 remove_filp(f);
80 nr_free_files--;
81 new_one:
82 memset(f, 0, sizeof(*f));
83 f->f_count = 1;
84 f->f_version = ++event;
85 f->f_uid = current->fsuid;
86 f->f_gid = current->fsgid;
87 put_inuse(f);
88 return f;
91 * Use a reserved one if we're the superuser
93 if (nr_free_files && !current->euid)
94 goto used_one;
96 * Allocate a new one if we're below the limit.
98 if (nr_files < max_files) {
99 f = kmem_cache_alloc(filp_cache, SLAB_KERNEL);
100 if (f) {
101 nr_files++;
102 goto new_one;
104 /* Big problems... */
105 printk("VFS: filp allocation failed\n");
107 } else if (max_files > old_max) {
108 printk("VFS: file-max limit %d reached\n", max_files);
109 old_max = max_files;
111 return NULL;
115 * Clear and initialize a (private) struct file for the given dentry,
116 * and call the open function (if any). The caller must verify that
117 * inode->i_op and inode->i_op->default_file_ops are not NULL.
119 int init_private_file(struct file *filp, struct dentry *dentry, int mode)
121 memset(filp, 0, sizeof(*filp));
122 filp->f_mode = mode;
123 filp->f_count = 1;
124 filp->f_dentry = dentry;
125 filp->f_uid = current->fsuid;
126 filp->f_gid = current->fsgid;
127 filp->f_op = dentry->d_inode->i_op->default_file_ops;
128 if (filp->f_op->open)
129 return filp->f_op->open(dentry->d_inode, filp);
130 else
131 return 0;
134 void fput(struct file *file)
136 int count = file->f_count-1;
138 if (!count) {
139 locks_remove_flock(file);
140 __fput(file);
141 file->f_count = 0;
142 remove_filp(file);
143 insert_file_free(file);
144 } else
145 file->f_count = count;
148 void put_filp(struct file *file)
150 if(--file->f_count == 0) {
151 remove_filp(file);
152 insert_file_free(file);