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>
12 #include <linux/smp_lock.h>
14 /* SLAB cache for filp's. */
15 static kmem_cache_t
*filp_cache
;
17 /* sysctl tunables... */
18 int nr_files
= 0; /* read only */
19 int nr_free_files
= 0; /* read only */
20 int max_files
= NR_FILE
;/* tunable */
22 /* Here the new files go */
23 static LIST_HEAD(anon_list
);
24 /* And here the free ones sit */
25 static LIST_HEAD(free_list
);
26 /* public *and* exported. Not pretty! */
27 spinlock_t files_lock
= SPIN_LOCK_UNLOCKED
;
29 void __init
file_table_init(void)
31 filp_cache
= kmem_cache_create("filp", sizeof(struct file
),
33 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
35 panic("VFS: Cannot alloc filp SLAB cache.");
37 * We could allocate the reserved files here, but really
38 * shouldn't need to: the normal boot process will create
39 * plenty of free files.
43 /* Find an unused file structure and return a pointer to it.
44 * Returns NULL, if there are no more free file structures or
45 * we run out of memory.
49 struct file
* get_empty_filp(void)
51 static int old_max
= 0;
55 if (nr_free_files
> NR_RESERVED_FILES
) {
57 f
= list_entry(free_list
.next
, struct file
, f_list
);
62 memset(f
, 0, sizeof(*f
));
63 atomic_set(&f
->f_count
,1);
64 f
->f_version
= ++event
;
65 f
->f_uid
= current
->fsuid
;
66 f
->f_gid
= current
->fsgid
;
68 list_add(&f
->f_list
, &anon_list
);
73 * Use a reserved one if we're the superuser
75 if (nr_free_files
&& !current
->euid
)
78 * Allocate a new one if we're below the limit.
80 if (nr_files
< max_files
) {
82 f
= kmem_cache_alloc(filp_cache
, SLAB_KERNEL
);
89 printk("VFS: filp allocation failed\n");
91 } else if (max_files
> old_max
) {
92 printk("VFS: file-max limit %d reached\n", max_files
);
100 * Clear and initialize a (private) struct file for the given dentry,
101 * and call the open function (if any). The caller must verify that
102 * inode->i_op and inode->i_op->default_file_ops are not NULL.
104 int init_private_file(struct file
*filp
, struct dentry
*dentry
, int mode
)
106 memset(filp
, 0, sizeof(*filp
));
108 atomic_set(&filp
->f_count
, 1);
109 filp
->f_dentry
= dentry
;
110 filp
->f_uid
= current
->fsuid
;
111 filp
->f_gid
= current
->fsgid
;
112 filp
->f_op
= dentry
->d_inode
->i_op
->default_file_ops
;
113 if (filp
->f_op
->open
)
114 return filp
->f_op
->open(dentry
->d_inode
, filp
);
120 * Called when retiring the last use of a file pointer.
122 static void __fput(struct file
*filp
)
124 struct dentry
* dentry
= filp
->f_dentry
;
125 struct inode
* inode
= dentry
->d_inode
;
127 if (filp
->f_op
&& filp
->f_op
->release
)
128 filp
->f_op
->release(inode
, filp
);
129 filp
->f_dentry
= NULL
;
130 if (filp
->f_mode
& FMODE_WRITE
)
131 put_write_access(inode
);
135 void _fput(struct file
*file
)
138 locks_remove_flock(file
); /* Still need the */
139 __fput(file
); /* big lock here. */
143 list_del(&file
->f_list
);
144 list_add(&file
->f_list
, &free_list
);
149 /* Here. put_filp() is SMP-safe now. */
151 void put_filp(struct file
*file
)
153 if(atomic_dec_and_test(&file
->f_count
)) {
155 list_del(&file
->f_list
);
156 list_add(&file
->f_list
, &free_list
);
162 void file_move(struct file
*file
, struct list_head
*list
)
167 list_del(&file
->f_list
);
168 list_add(&file
->f_list
, list
);
172 void file_moveto(struct file
*new, struct file
*old
)
175 list_del(&new->f_list
);
176 list_add(&new->f_list
, &old
->f_list
);
180 int fs_may_remount_ro(struct super_block
*sb
)
184 /* Check that no files are currently opened for writing. */
186 for (p
= sb
->s_files
.next
; p
!= &sb
->s_files
; p
= p
->next
) {
187 struct file
*file
= list_entry(p
, struct file
, f_list
);
188 struct inode
*inode
= file
->f_dentry
->d_inode
;
192 /* File with pending delete? */
193 if (inode
->i_nlink
== 0)
197 if (S_ISREG(inode
->i_mode
) && (file
->f_mode
& FMODE_WRITE
))
201 return 1; /* Tis' cool bro. */