blob: 3f73eb1f195a12638cf00fa276b39cd6eaa9f18e [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/fs.h>
14#include <linux/security.h>
15#include <linux/eventpoll.h>
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070016#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mount.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080018#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/cdev.h>
Robert Love0eeca282005-07-12 17:06:03 -040020#include <linux/fsnotify.h>
Dipankar Sarma529bf6b2006-03-07 21:55:35 -080021#include <linux/sysctl.h>
22#include <linux/percpu_counter.h>
23
24#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/* sysctl tunables... */
27struct files_stat_struct files_stat = {
28 .max_files = NR_FILE
29};
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/* public. Not pretty! */
Dipankar Sarma529bf6b2006-03-07 21:55:35 -080032__cacheline_aligned_in_smp DEFINE_SPINLOCK(files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Dipankar Sarma529bf6b2006-03-07 21:55:35 -080034static struct percpu_counter nr_files __cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070036static inline void file_free_rcu(struct rcu_head *head)
37{
Eric Dumazet2f512012005-10-30 15:02:16 -080038 struct file *f = container_of(head, struct file, f_u.fu_rcuhead);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070039 kmem_cache_free(filp_cachep, f);
40}
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static inline void file_free(struct file *f)
43{
Dipankar Sarma529bf6b2006-03-07 21:55:35 -080044 percpu_counter_dec(&nr_files);
Eric Dumazet2f512012005-10-30 15:02:16 -080045 call_rcu(&f->f_u.fu_rcuhead, file_free_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Dipankar Sarma529bf6b2006-03-07 21:55:35 -080048/*
49 * Return the total number of open files in the system
50 */
51static int get_nr_files(void)
52{
53 return percpu_counter_read_positive(&nr_files);
54}
55
56/*
57 * Return the maximum number of open files in the system
58 */
59int get_max_files(void)
60{
61 return files_stat.max_files;
62}
63EXPORT_SYMBOL_GPL(get_max_files);
64
65/*
66 * Handle nr_files sysctl
67 */
68#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
69int proc_nr_files(ctl_table *table, int write, struct file *filp,
70 void __user *buffer, size_t *lenp, loff_t *ppos)
71{
72 files_stat.nr_files = get_nr_files();
73 return proc_dointvec(table, write, filp, buffer, lenp, ppos);
74}
75#else
76int proc_nr_files(ctl_table *table, int write, struct file *filp,
77 void __user *buffer, size_t *lenp, loff_t *ppos)
78{
79 return -ENOSYS;
80}
81#endif
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* Find an unused file structure and return a pointer to it.
84 * Returns NULL, if there are no more free file structures or
85 * we run out of memory.
Dave Hansen430e2852008-02-15 14:37:26 -080086 *
87 * Be very careful using this. You are responsible for
88 * getting write access to any mount that you might assign
89 * to this filp, if it is opened for write. If this is not
90 * done, you will imbalance int the mount's writer count
91 * and a warning at __fput() time.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
93struct file *get_empty_filp(void)
94{
Benjamin LaHaise5a6b7952006-03-23 03:01:03 -080095 struct task_struct *tsk;
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -070096 static int old_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 struct file * f;
98
99 /*
100 * Privileged users can go above max_files
101 */
Dipankar Sarma529bf6b2006-03-07 21:55:35 -0800102 if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
103 /*
104 * percpu_counters are inaccurate. Do an expensive check before
105 * we go and fail.
106 */
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -0700107 if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
Dipankar Sarma529bf6b2006-03-07 21:55:35 -0800108 goto over;
109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Denis Cheng4975e452007-10-16 23:26:19 -0700111 f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -0700112 if (f == NULL)
113 goto fail;
114
Dipankar Sarma529bf6b2006-03-07 21:55:35 -0800115 percpu_counter_inc(&nr_files);
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -0700116 if (security_file_alloc(f))
117 goto fail_sec;
118
Benjamin LaHaise5a6b7952006-03-23 03:01:03 -0800119 tsk = current;
Eric Dumazet2f512012005-10-30 15:02:16 -0800120 INIT_LIST_HEAD(&f->f_u.fu_list);
Benjamin LaHaise5a6b7952006-03-23 03:01:03 -0800121 atomic_set(&f->f_count, 1);
122 rwlock_init(&f->f_owner.lock);
123 f->f_uid = tsk->fsuid;
124 f->f_gid = tsk->fsgid;
125 eventpoll_init_file(f);
126 /* f->f_version: 0 */
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -0700127 return f;
128
129over:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 /* Ran out of filps - report that */
Dipankar Sarma529bf6b2006-03-07 21:55:35 -0800131 if (get_nr_files() > old_max) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 printk(KERN_INFO "VFS: file-max limit %d reached\n",
Dipankar Sarma529bf6b2006-03-07 21:55:35 -0800133 get_max_files());
134 old_max = get_nr_files();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
Kirill Korotaevaf4d2ec2005-06-23 00:09:50 -0700136 goto fail;
137
138fail_sec:
139 file_free(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140fail:
141 return NULL;
142}
143
144EXPORT_SYMBOL(get_empty_filp);
145
Dave Hansence8d2cd2007-10-16 23:31:13 -0700146/**
147 * alloc_file - allocate and initialize a 'struct file'
148 * @mnt: the vfsmount on which the file will reside
149 * @dentry: the dentry representing the new file
150 * @mode: the mode with which the new file will be opened
151 * @fop: the 'struct file_operations' for the new file
152 *
153 * Use this instead of get_empty_filp() to get a new
154 * 'struct file'. Do so because of the same initialization
155 * pitfalls reasons listed for init_file(). This is a
156 * preferred interface to using init_file().
157 *
158 * If all the callers of init_file() are eliminated, its
159 * code should be moved into this function.
160 */
161struct file *alloc_file(struct vfsmount *mnt, struct dentry *dentry,
162 mode_t mode, const struct file_operations *fop)
163{
164 struct file *file;
165 struct path;
166
167 file = get_empty_filp();
168 if (!file)
169 return NULL;
170
171 init_file(file, mnt, dentry, mode, fop);
172 return file;
173}
174EXPORT_SYMBOL(alloc_file);
175
176/**
177 * init_file - initialize a 'struct file'
178 * @file: the already allocated 'struct file' to initialized
179 * @mnt: the vfsmount on which the file resides
180 * @dentry: the dentry representing this file
181 * @mode: the mode the file is opened with
182 * @fop: the 'struct file_operations' for this file
183 *
184 * Use this instead of setting the members directly. Doing so
185 * avoids making mistakes like forgetting the mntget() or
186 * forgetting to take a write on the mnt.
187 *
188 * Note: This is a crappy interface. It is here to make
189 * merging with the existing users of get_empty_filp()
190 * who have complex failure logic easier. All users
191 * of this should be moving to alloc_file().
192 */
193int init_file(struct file *file, struct vfsmount *mnt, struct dentry *dentry,
194 mode_t mode, const struct file_operations *fop)
195{
196 int error = 0;
197 file->f_path.dentry = dentry;
198 file->f_path.mnt = mntget(mnt);
199 file->f_mapping = dentry->d_inode->i_mapping;
200 file->f_mode = mode;
201 file->f_op = fop;
202 return error;
203}
204EXPORT_SYMBOL(init_file);
205
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800206void fput(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Nick Piggin095975d2006-01-08 01:02:19 -0800208 if (atomic_dec_and_test(&file->f_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 __fput(file);
210}
211
212EXPORT_SYMBOL(fput);
213
Dave Hansenaceaf782008-02-15 14:37:31 -0800214/**
215 * drop_file_write_access - give up ability to write to a file
216 * @file: the file to which we will stop writing
217 *
218 * This is a central place which will give up the ability
219 * to write to @file, along with access to write through
220 * its vfsmount.
221 */
222void drop_file_write_access(struct file *file)
223{
224 struct dentry *dentry = file->f_path.dentry;
225 struct inode *inode = dentry->d_inode;
226
227 put_write_access(inode);
228}
229EXPORT_SYMBOL_GPL(drop_file_write_access);
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/* __fput is called from task context when aio completion releases the last
232 * last use of a struct file *. Do not use otherwise.
233 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800234void __fput(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800236 struct dentry *dentry = file->f_path.dentry;
237 struct vfsmount *mnt = file->f_path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 struct inode *inode = dentry->d_inode;
239
240 might_sleep();
Robert Love0eeca282005-07-12 17:06:03 -0400241
242 fsnotify_close(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /*
244 * The function eventpoll_release() should be the first called
245 * in the file cleanup chain.
246 */
247 eventpoll_release(file);
248 locks_remove_flock(file);
249
250 if (file->f_op && file->f_op->release)
251 file->f_op->release(inode, file);
252 security_file_free(file);
Theodore Ts'o577c4eb2006-09-27 01:50:49 -0700253 if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 cdev_put(inode->i_cdev);
255 fops_put(file->f_op);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700256 put_pid(file->f_owner.pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 file_kill(file);
Dave Hansenaceaf782008-02-15 14:37:31 -0800258 if (file->f_mode & FMODE_WRITE)
259 drop_file_write_access(file);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800260 file->f_path.dentry = NULL;
261 file->f_path.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 file_free(file);
263 dput(dentry);
264 mntput(mnt);
265}
266
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800267struct file *fget(unsigned int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 struct file *file;
270 struct files_struct *files = current->files;
271
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700272 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 file = fcheck_files(files, fd);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700274 if (file) {
Nick Piggin095975d2006-01-08 01:02:19 -0800275 if (!atomic_inc_not_zero(&file->f_count)) {
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700276 /* File object ref couldn't be taken */
277 rcu_read_unlock();
278 return NULL;
279 }
280 }
281 rcu_read_unlock();
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return file;
284}
285
286EXPORT_SYMBOL(fget);
287
288/*
289 * Lightweight file lookup - no refcnt increment if fd table isn't shared.
290 * You can use this only if it is guranteed that the current task already
291 * holds a refcnt to that file. That check has to be done at fget() only
292 * and a flag is returned to be passed to the corresponding fput_light().
293 * There must not be a cloning between an fget_light/fput_light pair.
294 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800295struct file *fget_light(unsigned int fd, int *fput_needed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 struct file *file;
298 struct files_struct *files = current->files;
299
300 *fput_needed = 0;
301 if (likely((atomic_read(&files->count) == 1))) {
302 file = fcheck_files(files, fd);
303 } else {
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700304 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 file = fcheck_files(files, fd);
306 if (file) {
Nick Piggin095975d2006-01-08 01:02:19 -0800307 if (atomic_inc_not_zero(&file->f_count))
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700308 *fput_needed = 1;
309 else
310 /* Didn't get the reference, someone's freed */
311 file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700313 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return file;
317}
318
319
320void put_filp(struct file *file)
321{
Nick Piggin095975d2006-01-08 01:02:19 -0800322 if (atomic_dec_and_test(&file->f_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 security_file_free(file);
324 file_kill(file);
325 file_free(file);
326 }
327}
328
329void file_move(struct file *file, struct list_head *list)
330{
331 if (!list)
332 return;
333 file_list_lock();
Eric Dumazet2f512012005-10-30 15:02:16 -0800334 list_move(&file->f_u.fu_list, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 file_list_unlock();
336}
337
338void file_kill(struct file *file)
339{
Eric Dumazet2f512012005-10-30 15:02:16 -0800340 if (!list_empty(&file->f_u.fu_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 file_list_lock();
Eric Dumazet2f512012005-10-30 15:02:16 -0800342 list_del_init(&file->f_u.fu_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 file_list_unlock();
344 }
345}
346
347int fs_may_remount_ro(struct super_block *sb)
348{
Matthias Kaehlckecfdaf9e2007-10-18 23:39:56 -0700349 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /* Check that no files are currently opened for writing. */
352 file_list_lock();
Matthias Kaehlckecfdaf9e2007-10-18 23:39:56 -0700353 list_for_each_entry(file, &sb->s_files, f_u.fu_list) {
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800354 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /* File with pending delete? */
357 if (inode->i_nlink == 0)
358 goto too_bad;
359
360 /* Writeable file? */
361 if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
362 goto too_bad;
363 }
364 file_list_unlock();
365 return 1; /* Tis' cool bro. */
366too_bad:
367 file_list_unlock();
368 return 0;
369}
370
371void __init files_init(unsigned long mempages)
372{
373 int n;
374 /* One file with associated inode and dcache is very roughly 1K.
375 * Per default don't use more than 10% of our memory for files.
376 */
377
378 n = (mempages * (PAGE_SIZE / 1024)) / 10;
379 files_stat.max_files = n;
380 if (files_stat.max_files < NR_FILE)
381 files_stat.max_files = NR_FILE;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700382 files_defer_init();
Mingming Cao0216bfc2006-06-23 02:05:41 -0700383 percpu_counter_init(&nr_files, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}