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/capability.h>
20 #include <linux/cdev.h>
21 #include <linux/fsnotify.h>
23 /* sysctl tunables... */
24 struct files_stat_struct files_stat
= {
28 EXPORT_SYMBOL(files_stat
); /* Needed by unix.o */
30 /* public. Not pretty! */
31 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(files_lock
);
33 static DEFINE_SPINLOCK(filp_count_lock
);
35 /* slab constructors and destructors are called from arbitrary
36 * context and must be fully threaded - use a local spinlock
37 * to protect files_stat.nr_files
39 void filp_ctor(void *objp
, struct kmem_cache
*cachep
, unsigned long cflags
)
41 if ((cflags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
42 SLAB_CTOR_CONSTRUCTOR
) {
44 spin_lock_irqsave(&filp_count_lock
, flags
);
45 files_stat
.nr_files
++;
46 spin_unlock_irqrestore(&filp_count_lock
, flags
);
50 void filp_dtor(void *objp
, struct kmem_cache
*cachep
, unsigned long dflags
)
53 spin_lock_irqsave(&filp_count_lock
, flags
);
54 files_stat
.nr_files
--;
55 spin_unlock_irqrestore(&filp_count_lock
, flags
);
58 static inline void file_free_rcu(struct rcu_head
*head
)
60 struct file
*f
= container_of(head
, struct file
, f_u
.fu_rcuhead
);
61 kmem_cache_free(filp_cachep
, f
);
64 static inline void file_free(struct file
*f
)
66 call_rcu(&f
->f_u
.fu_rcuhead
, file_free_rcu
);
69 /* Find an unused file structure and return a pointer to it.
70 * Returns NULL, if there are no more free file structures or
71 * we run out of memory.
73 struct file
*get_empty_filp(void)
79 * Privileged users can go above max_files
81 if (files_stat
.nr_files
>= files_stat
.max_files
&&
82 !capable(CAP_SYS_ADMIN
))
85 f
= kmem_cache_alloc(filp_cachep
, GFP_KERNEL
);
89 memset(f
, 0, sizeof(*f
));
90 if (security_file_alloc(f
))
93 eventpoll_init_file(f
);
94 atomic_set(&f
->f_count
, 1);
95 f
->f_uid
= current
->fsuid
;
96 f
->f_gid
= current
->fsgid
;
97 rwlock_init(&f
->f_owner
.lock
);
99 INIT_LIST_HEAD(&f
->f_u
.fu_list
);
103 /* Ran out of filps - report that */
104 if (files_stat
.nr_files
> old_max
) {
105 printk(KERN_INFO
"VFS: file-max limit %d reached\n",
106 files_stat
.max_files
);
107 old_max
= files_stat
.nr_files
;
117 EXPORT_SYMBOL(get_empty_filp
);
119 void fastcall
fput(struct file
*file
)
121 if (atomic_dec_and_test(&file
->f_count
))
127 /* __fput is called from task context when aio completion releases the last
128 * last use of a struct file *. Do not use otherwise.
130 void fastcall
__fput(struct file
*file
)
132 struct dentry
*dentry
= file
->f_dentry
;
133 struct vfsmount
*mnt
= file
->f_vfsmnt
;
134 struct inode
*inode
= dentry
->d_inode
;
138 fsnotify_close(file
);
140 * The function eventpoll_release() should be the first called
141 * in the file cleanup chain.
143 eventpoll_release(file
);
144 locks_remove_flock(file
);
146 if (file
->f_op
&& file
->f_op
->release
)
147 file
->f_op
->release(inode
, file
);
148 security_file_free(file
);
149 if (unlikely(inode
->i_cdev
!= NULL
))
150 cdev_put(inode
->i_cdev
);
151 fops_put(file
->f_op
);
152 if (file
->f_mode
& FMODE_WRITE
)
153 put_write_access(inode
);
155 file
->f_dentry
= NULL
;
156 file
->f_vfsmnt
= NULL
;
162 struct file fastcall
*fget(unsigned int fd
)
165 struct files_struct
*files
= current
->files
;
168 file
= fcheck_files(files
, fd
);
170 if (!atomic_inc_not_zero(&file
->f_count
)) {
171 /* File object ref couldn't be taken */
184 * Lightweight file lookup - no refcnt increment if fd table isn't shared.
185 * You can use this only if it is guranteed that the current task already
186 * holds a refcnt to that file. That check has to be done at fget() only
187 * and a flag is returned to be passed to the corresponding fput_light().
188 * There must not be a cloning between an fget_light/fput_light pair.
190 struct file fastcall
*fget_light(unsigned int fd
, int *fput_needed
)
193 struct files_struct
*files
= current
->files
;
196 if (likely((atomic_read(&files
->count
) == 1))) {
197 file
= fcheck_files(files
, fd
);
200 file
= fcheck_files(files
, fd
);
202 if (atomic_inc_not_zero(&file
->f_count
))
205 /* Didn't get the reference, someone's freed */
215 void put_filp(struct file
*file
)
217 if (atomic_dec_and_test(&file
->f_count
)) {
218 security_file_free(file
);
224 void file_move(struct file
*file
, struct list_head
*list
)
229 list_move(&file
->f_u
.fu_list
, list
);
233 void file_kill(struct file
*file
)
235 if (!list_empty(&file
->f_u
.fu_list
)) {
237 list_del_init(&file
->f_u
.fu_list
);
242 int fs_may_remount_ro(struct super_block
*sb
)
246 /* Check that no files are currently opened for writing. */
248 list_for_each(p
, &sb
->s_files
) {
249 struct file
*file
= list_entry(p
, struct file
, f_u
.fu_list
);
250 struct inode
*inode
= file
->f_dentry
->d_inode
;
252 /* File with pending delete? */
253 if (inode
->i_nlink
== 0)
256 /* Writeable file? */
257 if (S_ISREG(inode
->i_mode
) && (file
->f_mode
& FMODE_WRITE
))
261 return 1; /* Tis' cool bro. */
267 void __init
files_init(unsigned long mempages
)
270 /* One file with associated inode and dcache is very roughly 1K.
271 * Per default don't use more than 10% of our memory for files.
274 n
= (mempages
* (PAGE_SIZE
/ 1024)) / 10;
275 files_stat
.max_files
= n
;
276 if (files_stat
.max_files
< NR_FILE
)
277 files_stat
.max_files
= NR_FILE
;