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/module.h>
13 #include <linux/smp_lock.h>
15 #include <linux/security.h>
16 #include <linux/eventpoll.h>
17 #include <linux/rcupdate.h>
18 #include <linux/mount.h>
19 #include <linux/cdev.h>
20 #include <linux/fsnotify.h>
22 /* sysctl tunables... */
23 struct files_stat_struct files_stat
= {
27 EXPORT_SYMBOL(files_stat
); /* Needed by unix.o */
29 /* public. Not pretty! */
30 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(files_lock
);
32 static DEFINE_SPINLOCK(filp_count_lock
);
34 /* slab constructors and destructors are called from arbitrary
35 * context and must be fully threaded - use a local spinlock
36 * to protect files_stat.nr_files
38 void filp_ctor(void *objp
, struct kmem_cache
*cachep
, unsigned long cflags
)
40 if ((cflags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
41 SLAB_CTOR_CONSTRUCTOR
) {
43 spin_lock_irqsave(&filp_count_lock
, flags
);
44 files_stat
.nr_files
++;
45 spin_unlock_irqrestore(&filp_count_lock
, flags
);
49 void filp_dtor(void *objp
, struct kmem_cache
*cachep
, unsigned long dflags
)
52 spin_lock_irqsave(&filp_count_lock
, flags
);
53 files_stat
.nr_files
--;
54 spin_unlock_irqrestore(&filp_count_lock
, flags
);
57 static inline void file_free_rcu(struct rcu_head
*head
)
59 struct file
*f
= container_of(head
, struct file
, f_u
.fu_rcuhead
);
60 kmem_cache_free(filp_cachep
, f
);
63 static inline void file_free(struct file
*f
)
65 call_rcu(&f
->f_u
.fu_rcuhead
, file_free_rcu
);
68 /* Find an unused file structure and return a pointer to it.
69 * Returns NULL, if there are no more free file structures or
70 * we run out of memory.
72 struct file
*get_empty_filp(void)
78 * Privileged users can go above max_files
80 if (files_stat
.nr_files
>= files_stat
.max_files
&&
81 !capable(CAP_SYS_ADMIN
))
84 f
= kmem_cache_alloc(filp_cachep
, GFP_KERNEL
);
88 memset(f
, 0, sizeof(*f
));
89 if (security_file_alloc(f
))
92 eventpoll_init_file(f
);
93 atomic_set(&f
->f_count
, 1);
94 f
->f_uid
= current
->fsuid
;
95 f
->f_gid
= current
->fsgid
;
96 rwlock_init(&f
->f_owner
.lock
);
98 INIT_LIST_HEAD(&f
->f_u
.fu_list
);
102 /* Ran out of filps - report that */
103 if (files_stat
.nr_files
> old_max
) {
104 printk(KERN_INFO
"VFS: file-max limit %d reached\n",
105 files_stat
.max_files
);
106 old_max
= files_stat
.nr_files
;
116 EXPORT_SYMBOL(get_empty_filp
);
118 void fastcall
fput(struct file
*file
)
120 if (atomic_dec_and_test(&file
->f_count
))
126 /* __fput is called from task context when aio completion releases the last
127 * last use of a struct file *. Do not use otherwise.
129 void fastcall
__fput(struct file
*file
)
131 struct dentry
*dentry
= file
->f_dentry
;
132 struct vfsmount
*mnt
= file
->f_vfsmnt
;
133 struct inode
*inode
= dentry
->d_inode
;
137 fsnotify_close(file
);
139 * The function eventpoll_release() should be the first called
140 * in the file cleanup chain.
142 eventpoll_release(file
);
143 locks_remove_flock(file
);
145 if (file
->f_op
&& file
->f_op
->release
)
146 file
->f_op
->release(inode
, file
);
147 security_file_free(file
);
148 if (unlikely(inode
->i_cdev
!= NULL
))
149 cdev_put(inode
->i_cdev
);
150 fops_put(file
->f_op
);
151 if (file
->f_mode
& FMODE_WRITE
)
152 put_write_access(inode
);
154 file
->f_dentry
= NULL
;
155 file
->f_vfsmnt
= NULL
;
161 struct file fastcall
*fget(unsigned int fd
)
164 struct files_struct
*files
= current
->files
;
167 file
= fcheck_files(files
, fd
);
169 if (!atomic_inc_not_zero(&file
->f_count
)) {
170 /* File object ref couldn't be taken */
183 * Lightweight file lookup - no refcnt increment if fd table isn't shared.
184 * You can use this only if it is guranteed that the current task already
185 * holds a refcnt to that file. That check has to be done at fget() only
186 * and a flag is returned to be passed to the corresponding fput_light().
187 * There must not be a cloning between an fget_light/fput_light pair.
189 struct file fastcall
*fget_light(unsigned int fd
, int *fput_needed
)
192 struct files_struct
*files
= current
->files
;
195 if (likely((atomic_read(&files
->count
) == 1))) {
196 file
= fcheck_files(files
, fd
);
199 file
= fcheck_files(files
, fd
);
201 if (atomic_inc_not_zero(&file
->f_count
))
204 /* Didn't get the reference, someone's freed */
214 void put_filp(struct file
*file
)
216 if (atomic_dec_and_test(&file
->f_count
)) {
217 security_file_free(file
);
223 void file_move(struct file
*file
, struct list_head
*list
)
228 list_move(&file
->f_u
.fu_list
, list
);
232 void file_kill(struct file
*file
)
234 if (!list_empty(&file
->f_u
.fu_list
)) {
236 list_del_init(&file
->f_u
.fu_list
);
241 int fs_may_remount_ro(struct super_block
*sb
)
245 /* Check that no files are currently opened for writing. */
247 list_for_each(p
, &sb
->s_files
) {
248 struct file
*file
= list_entry(p
, struct file
, f_u
.fu_list
);
249 struct inode
*inode
= file
->f_dentry
->d_inode
;
251 /* File with pending delete? */
252 if (inode
->i_nlink
== 0)
255 /* Writeable file? */
256 if (S_ISREG(inode
->i_mode
) && (file
->f_mode
& FMODE_WRITE
))
260 return 1; /* Tis' cool bro. */
266 void __init
files_init(unsigned long mempages
)
269 /* One file with associated inode and dcache is very roughly 1K.
270 * Per default don't use more than 10% of our memory for files.
273 n
= (mempages
* (PAGE_SIZE
/ 1024)) / 10;
274 files_stat
.max_files
= n
;
275 if (files_stat
.max_files
< NR_FILE
)
276 files_stat
.max_files
= NR_FILE
;