root/fs/file_table.c

Revision 27, 7.7 kB (checked in by dkaplan1, 6 years ago)

Changed calldriver arguments and references. Now includes size and flags
Added map_pages function to map full pages
Added linked lists of mappings
Added linked lists of usage counts
Modified map_struct to look in list for cached mapping
NO UNMAPPING CODE (yet)

Line 
1 /*
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>
17 #include <linux/mount.h>
18 #include <linux/cdev.h>
19
20 #include <asm/callgate.h>
21 /* sysctl tunables... */
22 struct files_stat_struct files_stat = {
23         .max_files = NR_FILE
24 };
25
26 EXPORT_SYMBOL(files_stat); /* Needed by unix.o */
27
28 /* public *and* exported. Not pretty! */
29 spinlock_t __cacheline_aligned_in_smp files_lock = SPIN_LOCK_UNLOCKED;
30
31 EXPORT_SYMBOL(files_lock);
32
33 static spinlock_t filp_count_lock = SPIN_LOCK_UNLOCKED;
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  */
39 void filp_ctor(void * objp, struct kmem_cache_s *cachep, unsigned long cflags)
40 {
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
50 void filp_dtor(void * objp, struct kmem_cache_s *cachep, unsigned long dflags)
51 {
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
58 static inline void file_free(struct file *f)
59 {
60         kmem_cache_free(filp_cachep, f);
61 }
62
63 /* Find an unused file structure and return a pointer to it.
64  * Returns NULL, if there are no more free file structures or
65  * we run out of memory.
66  */
67 struct file *get_empty_filp(void)
68 {
69 static int old_max;
70         struct file * f;
71
72         /*
73          * Privileged users can go above max_files
74          */
75         if (files_stat.nr_files < files_stat.max_files ||
76                                 capable(CAP_SYS_ADMIN)) {
77                 f = kmem_cache_alloc(filp_cachep, GFP_KERNEL);
78                 if (f) {
79                         memset(f, 0, sizeof(*f));
80                         if (security_file_alloc(f)) {
81                                 file_free(f);
82                                 goto fail;
83                         }
84                         eventpoll_init_file(f);
85                         atomic_set(&f->f_count, 1);
86                         f->f_uid = current->fsuid;
87                         f->f_gid = current->fsgid;
88                         f->f_owner.lock = RW_LOCK_UNLOCKED;
89                         /* f->f_version: 0 */
90                         INIT_LIST_HEAD(&f->f_list);
91                         return f;
92                 }
93         }
94
95         /* Ran out of filps - report that */
96         if (files_stat.max_files >= old_max) {
97                 printk(KERN_INFO "VFS: file-max limit %d reached\n",
98                                         files_stat.max_files);
99                 old_max = files_stat.max_files;
100         } else {
101                 /* Big problems... */
102                 printk(KERN_WARNING "VFS: filp allocation failed\n");
103         }
104 fail:
105         return NULL;
106 }
107
108 EXPORT_SYMBOL(get_empty_filp);
109
110 /*
111  * Clear and initialize a (private) struct file for the given dentry,
112  * allocate the security structure, and call the open function (if any). 
113  * The file should be released using close_private_file.
114  */
115 int open_private_file(struct file *filp, struct dentry *dentry, int flags)
116 {
117         int error;
118         memset(filp, 0, sizeof(*filp));
119         eventpoll_init_file(filp);
120         filp->f_flags  = flags;
121         filp->f_mode   = (flags+1) & O_ACCMODE;
122         atomic_set(&filp->f_count, 1);
123         filp->f_dentry = dentry;
124         filp->f_mapping = dentry->d_inode->i_mapping;
125         filp->f_uid    = current->fsuid;
126         filp->f_gid    = current->fsgid;
127         filp->f_op     = dentry->d_inode->i_fop;
128         INIT_LIST_HEAD(&filp->f_list);
129         error = security_file_alloc(filp);
130         if (!error)
131                 if (filp->f_op && filp->f_op->open) {
132                         error = filp->f_op->open(dentry->d_inode, filp);
133                         if (error)
134                                 security_file_free(filp);
135                 }
136         return error;
137 }
138
139 EXPORT_SYMBOL(open_private_file);
140
141 /*
142  * Release a private file by calling the release function (if any) and
143  * freeing the security structure.
144  */
145 void close_private_file(struct file *file)
146 {
147         struct inode * inode = file->f_dentry->d_inode;
148
149         if (file->f_op && file->f_op->release) {
150       if ((unsigned long) file->f_op->release > PAGE_OFFSET)
151           file->f_op->release(inode, file);
152       else {
153           CALLDRIVER_PREP2(rel, inode, file)
154           calldriver((unsigned long) file->f_op->release, rel_args, rel_sizes, 2, current->mm, 3, 0);
155       }
156   }
157         security_file_free(file);
158 }
159
160 EXPORT_SYMBOL(close_private_file);
161
162 void fastcall fput(struct file *file)
163 {
164         if (atomic_dec_and_test(&file->f_count))
165                 __fput(file);
166 }
167
168 EXPORT_SYMBOL(fput);
169
170 /* __fput is called from task context when aio completion releases the last
171  * last use of a struct file *.  Do not use otherwise.
172  */
173 void fastcall __fput(struct file *file)
174 {
175         struct dentry *dentry = file->f_dentry;
176         struct vfsmount *mnt = file->f_vfsmnt;
177         struct inode *inode = dentry->d_inode;
178
179         /*
180          * The function eventpoll_release() should be the first called
181          * in the file cleanup chain.
182          */
183         eventpoll_release(file);
184         locks_remove_flock(file);
185
186         if (file->f_op && file->f_op->release) {
187       if ((unsigned long) file->f_op->release > PAGE_OFFSET)
188           file->f_op->release(inode, file);
189       else {
190           CALLDRIVER_PREP2(rel, inode, file)
191           calldriver((unsigned long) file->f_op->release, rel_args, rel_sizes, 2, current->mm, 3, 0);
192       }
193   }
194         security_file_free(file);
195         if (unlikely(inode->i_cdev != NULL))
196                 cdev_put(inode->i_cdev);
197         fops_put(file->f_op);
198         if (file->f_mode & FMODE_WRITE)
199                 put_write_access(inode);
200         file_kill(file);
201         file->f_dentry = NULL;
202         file->f_vfsmnt = NULL;
203         file_free(file);
204         dput(dentry);
205         mntput(mnt);
206 }
207
208 struct file fastcall *fget(unsigned int fd)
209 {
210         struct file *file;
211         struct files_struct *files = current->files;
212
213         spin_lock(&files->file_lock);
214         file = fcheck_files(files, fd);
215         if (file)
216                 get_file(file);
217         spin_unlock(&files->file_lock);
218         return file;
219 }
220
221 EXPORT_SYMBOL(fget);
222
223 /*
224  * Lightweight file lookup - no refcnt increment if fd table isn't shared.
225  * You can use this only if it is guranteed that the current task already
226  * holds a refcnt to that file. That check has to be done at fget() only
227  * and a flag is returned to be passed to the corresponding fput_light().
228  * There must not be a cloning between an fget_light/fput_light pair.
229  */
230 struct file fastcall *fget_light(unsigned int fd, int *fput_needed)
231 {
232         struct file *file;
233         struct files_struct *files = current->files;
234
235         *fput_needed = 0;
236         if (likely((atomic_read(&files->count) == 1))) {
237                 file = fcheck_files(files, fd);
238         } else {
239                 spin_lock(&files->file_lock);
240                 file = fcheck_files(files, fd);
241                 if (file) {
242                         get_file(file);
243                         *fput_needed = 1;
244                 }
245                 spin_unlock(&files->file_lock);
246         }
247         return file;
248 }
249
250
251 void put_filp(struct file *file)
252 {
253         if (atomic_dec_and_test(&file->f_count)) {
254                 security_file_free(file);
255                 file_kill(file);
256                 file_free(file);
257         }
258 }
259
260 EXPORT_SYMBOL(put_filp);
261
262 void file_move(struct file *file, struct list_head *list)
263 {
264         if (!list)
265                 return;
266         file_list_lock();
267         list_move(&file->f_list, list);
268         file_list_unlock();
269 }
270
271 void file_kill(struct file *file)
272 {
273         if (!list_empty(&file->f_list)) {
274                 file_list_lock();
275                 list_del_init(&file->f_list);
276                 file_list_unlock();
277         }
278 }
279
280 int fs_may_remount_ro(struct super_block *sb)
281 {
282         struct list_head *p;
283
284         /* Check that no files are currently opened for writing. */
285         file_list_lock();
286         list_for_each(p, &sb->s_files) {
287                 struct file *file = list_entry(p, struct file, f_list);
288                 struct inode *inode = file->f_dentry->d_inode;
289
290                 /* File with pending delete? */
291                 if (inode->i_nlink == 0)
292                         goto too_bad;
293
294                 /* Writeable file? */
295                 if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
296                         goto too_bad;
297         }
298         file_list_unlock();
299         return 1; /* Tis' cool bro. */
300 too_bad:
301         file_list_unlock();
302         return 0;
303 }
304
305 void __init files_init(unsigned long mempages)
306 { 
307         int n;
308         /* One file with associated inode and dcache is very roughly 1K.
309          * Per default don't use more than 10% of our memory for files.
310          */
311
312         n = (mempages * (PAGE_SIZE / 1024)) / 10;
313         files_stat.max_files = n;
314         if (files_stat.max_files < NR_FILE)
315                 files_stat.max_files = NR_FILE;
316 } 
Note: See TracBrowser for help on using the browser.