Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 17 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/mount.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 19 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/cdev.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 21 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Pekka J Enberg | 2109a2d | 2005-11-07 00:58:01 -0800 | [diff] [blame] | 39 | void filp_ctor(void *objp, struct kmem_cache *cachep, unsigned long cflags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Pekka J Enberg | 2109a2d | 2005-11-07 00:58:01 -0800 | [diff] [blame] | 50 | void filp_dtor(void *objp, struct kmem_cache *cachep, unsigned long dflags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 58 | static inline void file_free_rcu(struct rcu_head *head) |
| 59 | { |
Eric Dumazet | 2f51201 | 2005-10-30 15:02:16 -0800 | [diff] [blame] | 60 | struct file *f = container_of(head, struct file, f_u.fu_rcuhead); |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 61 | kmem_cache_free(filp_cachep, f); |
| 62 | } |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | static inline void file_free(struct file *f) |
| 65 | { |
Eric Dumazet | 2f51201 | 2005-10-30 15:02:16 -0800 | [diff] [blame] | 66 | call_rcu(&f->f_u.fu_rcuhead, file_free_rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Kirill Korotaev | af4d2ec | 2005-06-23 00:09:50 -0700 | [diff] [blame] | 75 | static int old_max; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | struct file * f; |
| 77 | |
| 78 | /* |
| 79 | * Privileged users can go above max_files |
| 80 | */ |
Kirill Korotaev | af4d2ec | 2005-06-23 00:09:50 -0700 | [diff] [blame] | 81 | if (files_stat.nr_files >= files_stat.max_files && |
| 82 | !capable(CAP_SYS_ADMIN)) |
| 83 | goto over; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Kirill Korotaev | af4d2ec | 2005-06-23 00:09:50 -0700 | [diff] [blame] | 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 */ |
Eric Dumazet | 2f51201 | 2005-10-30 15:02:16 -0800 | [diff] [blame] | 99 | INIT_LIST_HEAD(&f->f_u.fu_list); |
Kirill Korotaev | af4d2ec | 2005-06-23 00:09:50 -0700 | [diff] [blame] | 100 | return f; |
| 101 | |
| 102 | over: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | /* Ran out of filps - report that */ |
Kirill Korotaev | af4d2ec | 2005-06-23 00:09:50 -0700 | [diff] [blame] | 104 | if (files_stat.nr_files > old_max) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | printk(KERN_INFO "VFS: file-max limit %d reached\n", |
| 106 | files_stat.max_files); |
Kirill Korotaev | af4d2ec | 2005-06-23 00:09:50 -0700 | [diff] [blame] | 107 | old_max = files_stat.nr_files; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
Kirill Korotaev | af4d2ec | 2005-06-23 00:09:50 -0700 | [diff] [blame] | 109 | goto fail; |
| 110 | |
| 111 | fail_sec: |
| 112 | file_free(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | fail: |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | EXPORT_SYMBOL(get_empty_filp); |
| 118 | |
| 119 | void fastcall fput(struct file *file) |
| 120 | { |
Nick Piggin | 095975d | 2006-01-08 01:02:19 -0800 | [diff] [blame] | 121 | if (atomic_dec_and_test(&file->f_count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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(); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 137 | |
| 138 | fsnotify_close(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 167 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | file = fcheck_files(files, fd); |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 169 | if (file) { |
Nick Piggin | 095975d | 2006-01-08 01:02:19 -0800 | [diff] [blame] | 170 | if (!atomic_inc_not_zero(&file->f_count)) { |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 171 | /* File object ref couldn't be taken */ |
| 172 | rcu_read_unlock(); |
| 173 | return NULL; |
| 174 | } |
| 175 | } |
| 176 | rcu_read_unlock(); |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 { |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 199 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | file = fcheck_files(files, fd); |
| 201 | if (file) { |
Nick Piggin | 095975d | 2006-01-08 01:02:19 -0800 | [diff] [blame] | 202 | if (atomic_inc_not_zero(&file->f_count)) |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 203 | *fput_needed = 1; |
| 204 | else |
| 205 | /* Didn't get the reference, someone's freed */ |
| 206 | file = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 208 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | return file; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | void put_filp(struct file *file) |
| 216 | { |
Nick Piggin | 095975d | 2006-01-08 01:02:19 -0800 | [diff] [blame] | 217 | if (atomic_dec_and_test(&file->f_count)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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(); |
Eric Dumazet | 2f51201 | 2005-10-30 15:02:16 -0800 | [diff] [blame] | 229 | list_move(&file->f_u.fu_list, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | file_list_unlock(); |
| 231 | } |
| 232 | |
| 233 | void file_kill(struct file *file) |
| 234 | { |
Eric Dumazet | 2f51201 | 2005-10-30 15:02:16 -0800 | [diff] [blame] | 235 | if (!list_empty(&file->f_u.fu_list)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | file_list_lock(); |
Eric Dumazet | 2f51201 | 2005-10-30 15:02:16 -0800 | [diff] [blame] | 237 | list_del_init(&file->f_u.fu_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Eric Dumazet | 2f51201 | 2005-10-30 15:02:16 -0800 | [diff] [blame] | 249 | struct file *file = list_entry(p, struct file, f_u.fu_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 278 | files_defer_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |