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/rcupdate.h> 18 #include <linux/mount.h> 19 #include <linux/capability.h> 20 #include <linux/cdev.h> 21 #include <linux/fsnotify.h> 22 23 /* sysctl tunables... */ 24 struct files_stat_struct files_stat = { 25 .max_files = NR_FILE 26 }; 27 28 EXPORT_SYMBOL(files_stat); /* Needed by unix.o */ 29 30 /* public. Not pretty! */ 31 __cacheline_aligned_in_smp DEFINE_SPINLOCK(files_lock); 32 33 static 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 */ 39 void filp_ctor(void *objp, struct kmem_cache *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 *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_rcu(struct rcu_head *head) 59 { 60 struct file *f = container_of(head, struct file, f_u.fu_rcuhead); 61 kmem_cache_free(filp_cachep, f); 62 } 63 64 static inline void file_free(struct file *f) 65 { 66 call_rcu(&f->f_u.fu_rcuhead, file_free_rcu); 67 } 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 */ 73 struct file *get_empty_filp(void) 74 { 75 static int old_max; 76 struct file * f; 77 78 /* 79 * Privileged users can go above max_files 80 */ 81 if (files_stat.nr_files >= files_stat.max_files && 82 !capable(CAP_SYS_ADMIN)) 83 goto over; 84 85 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 */ 99 INIT_LIST_HEAD(&f->f_u.fu_list); 100 return f; 101 102 over: 103 /* Ran out of filps - report that */ 104 if (files_stat.nr_files > old_max) { 105 printk(KERN_INFO "VFS: file-max limit %d reached\n", 106 files_stat.max_files); 107 old_max = files_stat.nr_files; 108 } 109 goto fail; 110 111 fail_sec: 112 file_free(f); 113 fail: 114 return NULL; 115 } 116 117 EXPORT_SYMBOL(get_empty_filp); 118 119 void fastcall fput(struct file *file) 120 { 121 if (atomic_dec_and_test(&file->f_count)) 122 __fput(file); 123 } 124 125 EXPORT_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 */ 130 void 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(); 137 138 fsnotify_close(file); 139 /* 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 162 struct file fastcall *fget(unsigned int fd) 163 { 164 struct file *file; 165 struct files_struct *files = current->files; 166 167 rcu_read_lock(); 168 file = fcheck_files(files, fd); 169 if (file) { 170 if (!atomic_inc_not_zero(&file->f_count)) { 171 /* File object ref couldn't be taken */ 172 rcu_read_unlock(); 173 return NULL; 174 } 175 } 176 rcu_read_unlock(); 177 178 return file; 179 } 180 181 EXPORT_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 */ 190 struct 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 { 199 rcu_read_lock(); 200 file = fcheck_files(files, fd); 201 if (file) { 202 if (atomic_inc_not_zero(&file->f_count)) 203 *fput_needed = 1; 204 else 205 /* Didn't get the reference, someone's freed */ 206 file = NULL; 207 } 208 rcu_read_unlock(); 209 } 210 211 return file; 212 } 213 214 215 void put_filp(struct file *file) 216 { 217 if (atomic_dec_and_test(&file->f_count)) { 218 security_file_free(file); 219 file_kill(file); 220 file_free(file); 221 } 222 } 223 224 void file_move(struct file *file, struct list_head *list) 225 { 226 if (!list) 227 return; 228 file_list_lock(); 229 list_move(&file->f_u.fu_list, list); 230 file_list_unlock(); 231 } 232 233 void file_kill(struct file *file) 234 { 235 if (!list_empty(&file->f_u.fu_list)) { 236 file_list_lock(); 237 list_del_init(&file->f_u.fu_list); 238 file_list_unlock(); 239 } 240 } 241 242 int 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) { 249 struct file *file = list_entry(p, struct file, f_u.fu_list); 250 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. */ 262 too_bad: 263 file_list_unlock(); 264 return 0; 265 } 266 267 void __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; 278 files_defer_init(); 279 } 280