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>
14 #include <linux/iobuf.h>
16 /* sysctl tunables... */
17 struct files_stat_struct files_stat = {0, 0, NR_FILE};
19 /* Here the new files go */
20 static LIST_HEAD(anon_list);
21 /* And here the free ones sit */
22 static LIST_HEAD(free_list);
23 /* public *and* exported. Not pretty! */
24 spinlock_t files_lock = SPIN_LOCK_UNLOCKED;
26 /* Find an unused file structure and return a pointer to it.
27 * Returns NULL, if there are no more free file structures or
28 * we run out of memory.
32 struct file * get_empty_filp(void)
34 static int old_max = 0;
38 if (files_stat.nr_free_files > NR_RESERVED_FILES) {
40 f = list_entry(free_list.next, struct file, f_list);
42 files_stat.nr_free_files--;
44 memset(f, 0, sizeof(*f));
45 atomic_set(&f->f_count,1);
46 f->f_version = ++event;
47 f->f_uid = current->fsuid;
48 f->f_gid = current->fsgid;
49 f->f_maxcount = INT_MAX;
50 list_add(&f->f_list, &anon_list);
55 * Use a reserved one if we're the superuser
57 if (files_stat.nr_free_files && !current->euid)
60 * Allocate a new one if we're below the limit.
62 if (files_stat.nr_files < files_stat.max_files) {
64 f = kmem_cache_alloc(filp_cachep, SLAB_KERNEL);
67 files_stat.nr_files++;
71 printk(KERN_WARNING "VFS: filp allocation failed\n");
73 } else if (files_stat.max_files > old_max) {
74 printk(KERN_INFO "VFS: file-max limit %d reached\n", files_stat.max_files);
75 old_max = files_stat.max_files;
82 * Clear and initialize a (private) struct file for the given dentry,
83 * and call the open function (if any). The caller must verify that
84 * inode->i_fop is not NULL.
86 int init_private_file(struct file *filp, struct dentry *dentry, int mode)
88 memset(filp, 0, sizeof(*filp));
90 atomic_set(&filp->f_count, 1);
91 filp->f_dentry = dentry;
92 filp->f_uid = current->fsuid;
93 filp->f_gid = current->fsgid;
94 filp->f_op = dentry->d_inode->i_fop;
95 filp->f_maxcount = INT_MAX;
98 return filp->f_op->open(dentry->d_inode, filp);
103 void fastcall fput(struct file * file)
105 struct dentry * dentry = file->f_dentry;
106 struct vfsmount * mnt = file->f_vfsmnt;
107 struct inode * inode = dentry->d_inode;
109 if (atomic_dec_and_test(&file->f_count)) {
110 locks_remove_flock(file);
113 free_kiovec(1, &file->f_iobuf);
115 if (file->f_op && file->f_op->release)
116 file->f_op->release(inode, file);
117 fops_put(file->f_op);
118 if (file->f_mode & FMODE_WRITE)
119 put_write_access(inode);
121 file->f_dentry = NULL;
122 file->f_vfsmnt = NULL;
123 list_del(&file->f_list);
124 list_add(&file->f_list, &free_list);
125 files_stat.nr_free_files++;
132 struct file fastcall *fget(unsigned int fd)
135 struct files_struct *files = current->files;
137 read_lock(&files->file_lock);
141 read_unlock(&files->file_lock);
145 /* Here. put_filp() is SMP-safe now. */
147 void put_filp(struct file *file)
149 if(atomic_dec_and_test(&file->f_count)) {
151 list_del(&file->f_list);
152 list_add(&file->f_list, &free_list);
153 files_stat.nr_free_files++;
158 void file_move(struct file *file, struct list_head *list)
163 list_del(&file->f_list);
164 list_add(&file->f_list, list);
168 int fs_may_remount_ro(struct super_block *sb)
172 /* Check that no files are currently opened for writing. */
174 for (p = sb->s_files.next; p != &sb->s_files; p = p->next) {
175 struct file *file = list_entry(p, struct file, f_list);
176 struct inode *inode = file->f_dentry->d_inode;
178 /* File with pending delete? */
179 if (inode->i_nlink == 0)
183 if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
187 return 1; /* Tis' cool bro. */
193 void __init files_init(unsigned long mempages)
196 /* One file with associated inode and dcache is very roughly 1K.
197 * Per default don't use more than 10% of our memory for files.
200 n = (mempages * (PAGE_SIZE / 1024)) / 10;
201 files_stat.max_files = n;
202 if (files_stat.max_files < NR_FILE)
203 files_stat.max_files = NR_FILE;