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/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
;
29 file
->f_pprev
= &free_filps
;
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
;
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
)
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
);
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;
76 if (nr_free_files
> NR_RESERVED_FILES
) {
82 memset(f
, 0, sizeof(*f
));
84 f
->f_version
= ++event
;
85 f
->f_uid
= current
->fsuid
;
86 f
->f_gid
= current
->fsgid
;
91 * Use a reserved one if we're the superuser
93 if (nr_free_files
&& !current
->euid
)
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
);
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
);
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
));
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
);
134 void fput(struct file
*file
)
136 int count
= file
->f_count
-1;
139 locks_remove_flock(file
);
143 insert_file_free(file
);
145 file
->f_count
= count
;
148 void put_filp(struct file
*file
)
150 if(--file
->f_count
== 0) {
152 insert_file_free(file
);