blob: 768b581675433ed0e02eb0a9bcf35288de7adca5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/file_table.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
6 */
7
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>
14#include <linux/fs.h>
15#include <linux/security.h>
16#include <linux/eventpoll.h>
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070017#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/mount.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080019#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/cdev.h>
Robert Love0eeca282005-07-12 17:06:03 -040021#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23/* sysctl tunables... */
24struct files_stat_struct files_stat = {
25 .max_files = NR_FILE
26};
27
28EXPORT_SYMBOL(files_stat); /* Needed by unix.o */
29
30/* public. Not pretty! */
31 __cacheline_aligned_in_smp DEFINE_SPINLOCK(files_lock);
32
33static DEFINE_SPINLOCK(filp_count_lock);
34
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
38 */
Pekka J Enberg2109a2d2005-11-07 00:58:01 -080039void filp_ctor(void *objp, struct kmem_cache *cachep, unsigned long cflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 if ((cflags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
42 SLAB_CTOR_CONSTRUCTOR) {
43 unsigned long flags;
44 spin_lock_irqsave(&filp_count_lock, flags);
45 files_stat.nr_files++;
46 spin_unlock_irqrestore(&filp_count_lock, flags);
47 }
48}
49
Pekka J Enberg2109a2d2005-11-07 00:58:01 -080050void filp_dtor(void *objp, struct kmem_cache *cachep, unsigned long dflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 unsigned long flags;
53 spin_lock_irqsave(&filp_count_lock, flags);
54 files_stat.nr_files--;
55 spin_unlock_irqrestore(&filp_count_lock, flags);
56}
57
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070058static inline void file_free_rcu(struct rcu_head *head)
59{
Eric Dumazet2f512012005-10-30 15:02:16 -080060 struct file *f = container_of(head, struct file, f_u.fu_rcuhead);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070061 kmem_cache_free(filp_cachep, f);
62}
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static inline void file_free(struct file *f)
65{
Eric Dumazet2f512012005-10-30 15:02:16 -080066 call_rcu(&f->f_u.fu_rcuhead, file_free_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
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.
72 */
73struct file *get_empty_filp(void)
74{
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -070075 static int old_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct file * f;
77
78 /*
79 * Privileged users can go above max_files
80 */
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -070081 if (files_stat.nr_files >= files_stat.max_files &&
82 !capable(CAP_SYS_ADMIN))
83 goto over;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -070085 f = kmem_cache_alloc(filp_cachep, GFP_KERNEL);
86 if (f == NULL)
87 goto fail;
88
89 memset(f, 0, sizeof(*f));
90 if (security_file_alloc(f))
91 goto fail_sec;
92
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);
98 /* f->f_version: 0 */
Eric Dumazet2f512012005-10-30 15:02:16 -080099 INIT_LIST_HEAD(&f->f_u.fu_list);
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -0700100 return f;
101
102over:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 /* Ran out of filps - report that */
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -0700104 if (files_stat.nr_files > old_max) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 printk(KERN_INFO "VFS: file-max limit %d reached\n",
106 files_stat.max_files);
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -0700107 old_max = files_stat.nr_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -0700109 goto fail;
110
111fail_sec:
112 file_free(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113fail:
114 return NULL;
115}
116
117EXPORT_SYMBOL(get_empty_filp);
118
119void fastcall fput(struct file *file)
120{
Nick Piggin095975d2006-01-08 01:02:19 -0800121 if (atomic_dec_and_test(&file->f_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 __fput(file);
123}
124
125EXPORT_SYMBOL(fput);
126
127/* __fput is called from task context when aio completion releases the last
128 * last use of a struct file *. Do not use otherwise.
129 */
130void fastcall __fput(struct file *file)
131{
132 struct dentry *dentry = file->f_dentry;
133 struct vfsmount *mnt = file->f_vfsmnt;
134 struct inode *inode = dentry->d_inode;
135
136 might_sleep();
Robert Love0eeca282005-07-12 17:06:03 -0400137
138 fsnotify_close(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 /*
140 * The function eventpoll_release() should be the first called
141 * in the file cleanup chain.
142 */
143 eventpoll_release(file);
144 locks_remove_flock(file);
145
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);
154 file_kill(file);
155 file->f_dentry = NULL;
156 file->f_vfsmnt = NULL;
157 file_free(file);
158 dput(dentry);
159 mntput(mnt);
160}
161
162struct file fastcall *fget(unsigned int fd)
163{
164 struct file *file;
165 struct files_struct *files = current->files;
166
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700167 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 file = fcheck_files(files, fd);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700169 if (file) {
Nick Piggin095975d2006-01-08 01:02:19 -0800170 if (!atomic_inc_not_zero(&file->f_count)) {
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700171 /* File object ref couldn't be taken */
172 rcu_read_unlock();
173 return NULL;
174 }
175 }
176 rcu_read_unlock();
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return file;
179}
180
181EXPORT_SYMBOL(fget);
182
183/*
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.
189 */
190struct file fastcall *fget_light(unsigned int fd, int *fput_needed)
191{
192 struct file *file;
193 struct files_struct *files = current->files;
194
195 *fput_needed = 0;
196 if (likely((atomic_read(&files->count) == 1))) {
197 file = fcheck_files(files, fd);
198 } else {
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700199 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 file = fcheck_files(files, fd);
201 if (file) {
Nick Piggin095975d2006-01-08 01:02:19 -0800202 if (atomic_inc_not_zero(&file->f_count))
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700203 *fput_needed = 1;
204 else
205 /* Didn't get the reference, someone's freed */
206 file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700208 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return file;
212}
213
214
215void put_filp(struct file *file)
216{
Nick Piggin095975d2006-01-08 01:02:19 -0800217 if (atomic_dec_and_test(&file->f_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 security_file_free(file);
219 file_kill(file);
220 file_free(file);
221 }
222}
223
224void file_move(struct file *file, struct list_head *list)
225{
226 if (!list)
227 return;
228 file_list_lock();
Eric Dumazet2f512012005-10-30 15:02:16 -0800229 list_move(&file->f_u.fu_list, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 file_list_unlock();
231}
232
233void file_kill(struct file *file)
234{
Eric Dumazet2f512012005-10-30 15:02:16 -0800235 if (!list_empty(&file->f_u.fu_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 file_list_lock();
Eric Dumazet2f512012005-10-30 15:02:16 -0800237 list_del_init(&file->f_u.fu_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 file_list_unlock();
239 }
240}
241
242int fs_may_remount_ro(struct super_block *sb)
243{
244 struct list_head *p;
245
246 /* Check that no files are currently opened for writing. */
247 file_list_lock();
248 list_for_each(p, &sb->s_files) {
Eric Dumazet2f512012005-10-30 15:02:16 -0800249 struct file *file = list_entry(p, struct file, f_u.fu_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 struct inode *inode = file->f_dentry->d_inode;
251
252 /* File with pending delete? */
253 if (inode->i_nlink == 0)
254 goto too_bad;
255
256 /* Writeable file? */
257 if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
258 goto too_bad;
259 }
260 file_list_unlock();
261 return 1; /* Tis' cool bro. */
262too_bad:
263 file_list_unlock();
264 return 0;
265}
266
267void __init files_init(unsigned long mempages)
268{
269 int n;
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.
272 */
273
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;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700278 files_defer_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}