Import 2.1.116pre1
[davej-history.git] / fs / file_table.c
blob3b5f05f8194b09426bfc77d54de9e382e3dd0f5f
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/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/fs.h>
11 #include <linux/file.h>
12 #include <linux/string.h>
13 #include <linux/mm.h>
14 #include <linux/slab.h>
16 /* SLAB cache for filp's. */
17 static kmem_cache_t *filp_cache;
19 /* sysctl tunables... */
20 int nr_files = 0; /* read only */
21 int nr_free_files = 0; /* read only */
22 int max_files = NR_FILE;/* tunable */
24 /* Free list management, if you are here you must have f_count == 0 */
25 static struct file * free_filps = NULL;
27 void insert_file_free(struct file *file)
29 if((file->f_next = free_filps) != NULL)
30 free_filps->f_pprev = &file->f_next;
31 free_filps = file;
32 file->f_pprev = &free_filps;
33 nr_free_files++;
36 /* The list of in-use filp's must be exported (ugh...) */
37 struct file *inuse_filps = NULL;
39 static inline void put_inuse(struct file *file)
41 if((file->f_next = inuse_filps) != NULL)
42 inuse_filps->f_pprev = &file->f_next;
43 inuse_filps = file;
44 file->f_pprev = &inuse_filps;
47 /* N.B. This should be an __initfunc ... */
48 void file_table_init(void)
50 filp_cache = kmem_cache_create("filp", sizeof(struct file),
52 SLAB_HWCACHE_ALIGN, NULL, NULL);
53 if(!filp_cache)
54 panic("VFS: Cannot alloc filp SLAB cache.");
56 * We could allocate the reserved files here, but really
57 * shouldn't need to: the normal boot process will create
58 * plenty of free files.
62 /* Find an unused file structure and return a pointer to it.
63 * Returns NULL, if there are no more free file structures or
64 * we run out of memory.
66 struct file * get_empty_filp(void)
68 static int old_max = 0;
69 struct file * f;
71 if (nr_free_files > NR_RESERVED_FILES) {
72 used_one:
73 f = free_filps;
74 remove_filp(f);
75 nr_free_files--;
76 new_one:
77 memset(f, 0, sizeof(*f));
78 f->f_count = 1;
79 f->f_version = ++event;
80 put_inuse(f);
81 return f;
84 * Use a reserved one if we're the superuser
86 if (nr_free_files && !current->euid)
87 goto used_one;
89 * Allocate a new one if we're below the limit.
91 if (nr_files < max_files) {
92 f = kmem_cache_alloc(filp_cache, SLAB_KERNEL);
93 if (f) {
94 nr_files++;
95 goto new_one;
97 /* Big problems... */
98 printk("VFS: filp allocation failed\n");
100 } else if (max_files > old_max) {
101 printk("VFS: file-max limit %d reached\n", max_files);
102 old_max = max_files;
104 return NULL;
108 * Clear and initialize a (private) struct file for the given dentry,
109 * and call the open function (if any). The caller must verify that
110 * inode->i_op and inode->i_op->default_file_ops are not NULL.
112 int init_private_file(struct file *filp, struct dentry *dentry, int mode)
114 memset(filp, 0, sizeof(*filp));
115 filp->f_mode = mode;
116 filp->f_count = 1;
117 filp->f_dentry = dentry;
118 filp->f_op = dentry->d_inode->i_op->default_file_ops;
119 if (filp->f_op->open)
120 return filp->f_op->open(dentry->d_inode, filp);
121 else
122 return 0;