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/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
;
28 file
->f_pprev
= &free_filps
;
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
;
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
)
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
);
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;
75 if (nr_free_files
> NR_RESERVED_FILES
) {
81 memset(f
, 0, sizeof(*f
));
83 f
->f_version
= ++event
;
84 f
->f_uid
= current
->fsuid
;
85 f
->f_gid
= current
->fsgid
;
90 * Use a reserved one if we're the superuser
92 if (nr_free_files
&& !current
->euid
)
95 * Allocate a new one if we're below the limit.
97 if (nr_files
< max_files
) {
98 f
= kmem_cache_alloc(filp_cache
, SLAB_KERNEL
);
103 /* Big problems... */
104 printk("VFS: filp allocation failed\n");
106 } else if (max_files
> old_max
) {
107 printk("VFS: file-max limit %d reached\n", max_files
);
114 * Clear and initialize a (private) struct file for the given dentry,
115 * and call the open function (if any). The caller must verify that
116 * inode->i_op and inode->i_op->default_file_ops are not NULL.
118 int init_private_file(struct file
*filp
, struct dentry
*dentry
, int mode
)
120 memset(filp
, 0, sizeof(*filp
));
123 filp
->f_dentry
= dentry
;
124 filp
->f_uid
= current
->fsuid
;
125 filp
->f_gid
= current
->fsgid
;
126 filp
->f_op
= dentry
->d_inode
->i_op
->default_file_ops
;
127 if (filp
->f_op
->open
)
128 return filp
->f_op
->open(dentry
->d_inode
, filp
);
133 void fput(struct file
*file
)
135 int count
= file
->f_count
-1;
138 locks_remove_flock(file
);
142 insert_file_free(file
);
144 file
->f_count
= count
;
147 void put_filp(struct file
*file
)
149 if(--file
->f_count
== 0) {
151 insert_file_free(file
);