2 * linux/fs/file_table.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
11 #include <linux/file.h>
12 #include <linux/string.h>
14 #include <linux/slab.h>
15 #include <linux/init.h>
17 /* SLAB cache for filp's. */
18 static kmem_cache_t
*filp_cache
;
20 /* sysctl tunables... */
21 int nr_files
= 0; /* read only */
22 int nr_free_files
= 0; /* read only */
23 int max_files
= NR_FILE
;/* tunable */
25 /* Free list management, if you are here you must have f_count == 0 */
26 static struct file
* free_filps
= NULL
;
28 void insert_file_free(struct file
*file
)
30 if((file
->f_next
= free_filps
) != NULL
)
31 free_filps
->f_pprev
= &file
->f_next
;
33 file
->f_pprev
= &free_filps
;
37 /* The list of in-use filp's must be exported (ugh...) */
38 struct file
*inuse_filps
= NULL
;
40 static inline void put_inuse(struct file
*file
)
42 if((file
->f_next
= inuse_filps
) != NULL
)
43 inuse_filps
->f_pprev
= &file
->f_next
;
45 file
->f_pprev
= &inuse_filps
;
48 void __init
file_table_init(void)
50 filp_cache
= kmem_cache_create("filp", sizeof(struct file
),
52 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
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;
71 if (nr_free_files
> NR_RESERVED_FILES
) {
77 memset(f
, 0, sizeof(*f
));
79 f
->f_version
= ++event
;
84 * Use a reserved one if we're the superuser
86 if (nr_free_files
&& !current
->euid
)
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
);
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
);
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
));
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
);