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/mount.h>
18 #include <linux/cdev.h>
20 /* sysctl tunables... */
21 struct files_stat_struct files_stat
= {
25 EXPORT_SYMBOL(files_stat
); /* Needed by unix.o */
27 /* public. Not pretty! */
28 spinlock_t __cacheline_aligned_in_smp files_lock
= SPIN_LOCK_UNLOCKED
;
30 static spinlock_t filp_count_lock
= SPIN_LOCK_UNLOCKED
;
32 /* slab constructors and destructors are called from arbitrary
33 * context and must be fully threaded - use a local spinlock
34 * to protect files_stat.nr_files
36 void filp_ctor(void * objp
, struct kmem_cache_s
*cachep
, unsigned long cflags
)
38 if ((cflags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
39 SLAB_CTOR_CONSTRUCTOR
) {
41 spin_lock_irqsave(&filp_count_lock
, flags
);
42 files_stat
.nr_files
++;
43 spin_unlock_irqrestore(&filp_count_lock
, flags
);
47 void filp_dtor(void * objp
, struct kmem_cache_s
*cachep
, unsigned long dflags
)
50 spin_lock_irqsave(&filp_count_lock
, flags
);
51 files_stat
.nr_files
--;
52 spin_unlock_irqrestore(&filp_count_lock
, flags
);
55 static inline void file_free(struct file
*f
)
57 kmem_cache_free(filp_cachep
, f
);
60 /* Find an unused file structure and return a pointer to it.
61 * Returns NULL, if there are no more free file structures or
62 * we run out of memory.
64 struct file
*get_empty_filp(void)
70 * Privileged users can go above max_files
72 if (files_stat
.nr_files
< files_stat
.max_files
||
73 capable(CAP_SYS_ADMIN
)) {
74 f
= kmem_cache_alloc(filp_cachep
, GFP_KERNEL
);
76 memset(f
, 0, sizeof(*f
));
77 if (security_file_alloc(f
)) {
81 eventpoll_init_file(f
);
82 atomic_set(&f
->f_count
, 1);
83 f
->f_uid
= current
->fsuid
;
84 f
->f_gid
= current
->fsgid
;
85 f
->f_owner
.lock
= RW_LOCK_UNLOCKED
;
87 INIT_LIST_HEAD(&f
->f_list
);
92 /* Ran out of filps - report that */
93 if (files_stat
.max_files
>= old_max
) {
94 printk(KERN_INFO
"VFS: file-max limit %d reached\n",
95 files_stat
.max_files
);
96 old_max
= files_stat
.max_files
;
99 printk(KERN_WARNING
"VFS: filp allocation failed\n");
105 EXPORT_SYMBOL(get_empty_filp
);
107 void fastcall
fput(struct file
*file
)
109 if (atomic_dec_and_test(&file
->f_count
))
115 /* __fput is called from task context when aio completion releases the last
116 * last use of a struct file *. Do not use otherwise.
118 void fastcall
__fput(struct file
*file
)
120 struct dentry
*dentry
= file
->f_dentry
;
121 struct vfsmount
*mnt
= file
->f_vfsmnt
;
122 struct inode
*inode
= dentry
->d_inode
;
126 * The function eventpoll_release() should be the first called
127 * in the file cleanup chain.
129 eventpoll_release(file
);
130 locks_remove_flock(file
);
132 if (file
->f_op
&& file
->f_op
->release
)
133 file
->f_op
->release(inode
, file
);
134 security_file_free(file
);
135 if (unlikely(inode
->i_cdev
!= NULL
))
136 cdev_put(inode
->i_cdev
);
137 fops_put(file
->f_op
);
138 if (file
->f_mode
& FMODE_WRITE
)
139 put_write_access(inode
);
141 file
->f_dentry
= NULL
;
142 file
->f_vfsmnt
= NULL
;
148 struct file fastcall
*fget(unsigned int fd
)
151 struct files_struct
*files
= current
->files
;
153 spin_lock(&files
->file_lock
);
154 file
= fcheck_files(files
, fd
);
157 spin_unlock(&files
->file_lock
);
164 * Lightweight file lookup - no refcnt increment if fd table isn't shared.
165 * You can use this only if it is guranteed that the current task already
166 * holds a refcnt to that file. That check has to be done at fget() only
167 * and a flag is returned to be passed to the corresponding fput_light().
168 * There must not be a cloning between an fget_light/fput_light pair.
170 struct file fastcall
*fget_light(unsigned int fd
, int *fput_needed
)
173 struct files_struct
*files
= current
->files
;
176 if (likely((atomic_read(&files
->count
) == 1))) {
177 file
= fcheck_files(files
, fd
);
179 spin_lock(&files
->file_lock
);
180 file
= fcheck_files(files
, fd
);
185 spin_unlock(&files
->file_lock
);
191 void put_filp(struct file
*file
)
193 if (atomic_dec_and_test(&file
->f_count
)) {
194 security_file_free(file
);
200 void file_move(struct file
*file
, struct list_head
*list
)
205 list_move(&file
->f_list
, list
);
209 void file_kill(struct file
*file
)
211 if (!list_empty(&file
->f_list
)) {
213 list_del_init(&file
->f_list
);
218 int fs_may_remount_ro(struct super_block
*sb
)
222 /* Check that no files are currently opened for writing. */
224 list_for_each(p
, &sb
->s_files
) {
225 struct file
*file
= list_entry(p
, struct file
, f_list
);
226 struct inode
*inode
= file
->f_dentry
->d_inode
;
228 /* File with pending delete? */
229 if (inode
->i_nlink
== 0)
232 /* Writeable file? */
233 if (S_ISREG(inode
->i_mode
) && (file
->f_mode
& FMODE_WRITE
))
237 return 1; /* Tis' cool bro. */
243 void __init
files_init(unsigned long mempages
)
246 /* One file with associated inode and dcache is very roughly 1K.
247 * Per default don't use more than 10% of our memory for files.
250 n
= (mempages
* (PAGE_SIZE
/ 1024)) / 10;
251 files_stat
.max_files
= n
;
252 if (files_stat
.max_files
< NR_FILE
)
253 files_stat
.max_files
= NR_FILE
;