Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/super.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | * |
| 6 | * super.c contains code to handle: - mount structures |
| 7 | * - super-block tables |
| 8 | * - filesystem drivers list |
| 9 | * - mount system call |
| 10 | * - umount system call |
| 11 | * - ustat system call |
| 12 | * |
| 13 | * GK 2/5/95 - Changed to support mounting the root fs via NFS |
| 14 | * |
| 15 | * Added kerneld support: Jacques Gelinas and Bjorn Ekwall |
| 16 | * Added change_root: Werner Almesberger & Hans Lermen, Feb '96 |
| 17 | * Added options to /proc/mounts: |
| 18 | * Torbjörn Lindh (torbjorn.lindh@gopta.se), April 14, 1996. |
| 19 | * Added devfs support: Richard Gooch <rgooch@atnf.csiro.au>, 13-JAN-1998 |
| 20 | * Heavily rewritten for 'one fs - one tree' dcache architecture. AV, Mar 2000 |
| 21 | */ |
| 22 | |
| 23 | #include <linux/config.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/smp_lock.h> |
| 28 | #include <linux/acct.h> |
| 29 | #include <linux/blkdev.h> |
| 30 | #include <linux/quotaops.h> |
| 31 | #include <linux/namei.h> |
| 32 | #include <linux/buffer_head.h> /* for fsync_super() */ |
| 33 | #include <linux/mount.h> |
| 34 | #include <linux/security.h> |
| 35 | #include <linux/syscalls.h> |
| 36 | #include <linux/vfs.h> |
| 37 | #include <linux/writeback.h> /* for the emergency remount stuff */ |
| 38 | #include <linux/idr.h> |
| 39 | #include <linux/kobject.h> |
| 40 | #include <asm/uaccess.h> |
| 41 | |
| 42 | |
| 43 | void get_filesystem(struct file_system_type *fs); |
| 44 | void put_filesystem(struct file_system_type *fs); |
| 45 | struct file_system_type *get_fs_type(const char *name); |
| 46 | |
| 47 | LIST_HEAD(super_blocks); |
| 48 | DEFINE_SPINLOCK(sb_lock); |
| 49 | |
| 50 | /** |
| 51 | * alloc_super - create new superblock |
| 52 | * |
| 53 | * Allocates and initializes a new &struct super_block. alloc_super() |
| 54 | * returns a pointer new superblock or %NULL if allocation had failed. |
| 55 | */ |
| 56 | static struct super_block *alloc_super(void) |
| 57 | { |
| 58 | struct super_block *s = kmalloc(sizeof(struct super_block), GFP_USER); |
| 59 | static struct super_operations default_op; |
| 60 | |
| 61 | if (s) { |
| 62 | memset(s, 0, sizeof(struct super_block)); |
| 63 | if (security_sb_alloc(s)) { |
| 64 | kfree(s); |
| 65 | s = NULL; |
| 66 | goto out; |
| 67 | } |
| 68 | INIT_LIST_HEAD(&s->s_dirty); |
| 69 | INIT_LIST_HEAD(&s->s_io); |
| 70 | INIT_LIST_HEAD(&s->s_files); |
| 71 | INIT_LIST_HEAD(&s->s_instances); |
| 72 | INIT_HLIST_HEAD(&s->s_anon); |
| 73 | INIT_LIST_HEAD(&s->s_inodes); |
| 74 | init_rwsem(&s->s_umount); |
| 75 | sema_init(&s->s_lock, 1); |
| 76 | down_write(&s->s_umount); |
| 77 | s->s_count = S_BIAS; |
| 78 | atomic_set(&s->s_active, 1); |
| 79 | sema_init(&s->s_vfs_rename_sem,1); |
| 80 | sema_init(&s->s_dquot.dqio_sem, 1); |
| 81 | sema_init(&s->s_dquot.dqonoff_sem, 1); |
| 82 | init_rwsem(&s->s_dquot.dqptr_sem); |
| 83 | init_waitqueue_head(&s->s_wait_unfrozen); |
| 84 | s->s_maxbytes = MAX_NON_LFS; |
| 85 | s->dq_op = sb_dquot_ops; |
| 86 | s->s_qcop = sb_quotactl_ops; |
| 87 | s->s_op = &default_op; |
| 88 | s->s_time_gran = 1000000000; |
| 89 | } |
| 90 | out: |
| 91 | return s; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * destroy_super - frees a superblock |
| 96 | * @s: superblock to free |
| 97 | * |
| 98 | * Frees a superblock. |
| 99 | */ |
| 100 | static inline void destroy_super(struct super_block *s) |
| 101 | { |
| 102 | security_sb_free(s); |
| 103 | kfree(s); |
| 104 | } |
| 105 | |
| 106 | /* Superblock refcounting */ |
| 107 | |
| 108 | /* |
| 109 | * Drop a superblock's refcount. Returns non-zero if the superblock was |
| 110 | * destroyed. The caller must hold sb_lock. |
| 111 | */ |
| 112 | int __put_super(struct super_block *sb) |
| 113 | { |
| 114 | int ret = 0; |
| 115 | |
| 116 | if (!--sb->s_count) { |
| 117 | destroy_super(sb); |
| 118 | ret = 1; |
| 119 | } |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Drop a superblock's refcount. |
| 125 | * Returns non-zero if the superblock is about to be destroyed and |
| 126 | * at least is already removed from super_blocks list, so if we are |
| 127 | * making a loop through super blocks then we need to restart. |
| 128 | * The caller must hold sb_lock. |
| 129 | */ |
| 130 | int __put_super_and_need_restart(struct super_block *sb) |
| 131 | { |
| 132 | /* check for race with generic_shutdown_super() */ |
| 133 | if (list_empty(&sb->s_list)) { |
| 134 | /* super block is removed, need to restart... */ |
| 135 | __put_super(sb); |
| 136 | return 1; |
| 137 | } |
| 138 | /* can't be the last, since s_list is still in use */ |
| 139 | sb->s_count--; |
| 140 | BUG_ON(sb->s_count == 0); |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * put_super - drop a temporary reference to superblock |
| 146 | * @sb: superblock in question |
| 147 | * |
| 148 | * Drops a temporary reference, frees superblock if there's no |
| 149 | * references left. |
| 150 | */ |
| 151 | static void put_super(struct super_block *sb) |
| 152 | { |
| 153 | spin_lock(&sb_lock); |
| 154 | __put_super(sb); |
| 155 | spin_unlock(&sb_lock); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /** |
| 160 | * deactivate_super - drop an active reference to superblock |
| 161 | * @s: superblock to deactivate |
| 162 | * |
| 163 | * Drops an active reference to superblock, acquiring a temprory one if |
| 164 | * there is no active references left. In that case we lock superblock, |
| 165 | * tell fs driver to shut it down and drop the temporary reference we |
| 166 | * had just acquired. |
| 167 | */ |
| 168 | void deactivate_super(struct super_block *s) |
| 169 | { |
| 170 | struct file_system_type *fs = s->s_type; |
| 171 | if (atomic_dec_and_lock(&s->s_active, &sb_lock)) { |
| 172 | s->s_count -= S_BIAS-1; |
| 173 | spin_unlock(&sb_lock); |
Al Viro | 7b7b1ac | 2005-11-07 17:13:39 -0500 | [diff] [blame] | 174 | DQUOT_OFF(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | down_write(&s->s_umount); |
| 176 | fs->kill_sb(s); |
| 177 | put_filesystem(fs); |
| 178 | put_super(s); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | EXPORT_SYMBOL(deactivate_super); |
| 183 | |
| 184 | /** |
| 185 | * grab_super - acquire an active reference |
| 186 | * @s: reference we are trying to make active |
| 187 | * |
| 188 | * Tries to acquire an active reference. grab_super() is used when we |
| 189 | * had just found a superblock in super_blocks or fs_type->fs_supers |
| 190 | * and want to turn it into a full-blown active reference. grab_super() |
| 191 | * is called with sb_lock held and drops it. Returns 1 in case of |
| 192 | * success, 0 if we had failed (superblock contents was already dead or |
| 193 | * dying when grab_super() had been called). |
| 194 | */ |
| 195 | static int grab_super(struct super_block *s) |
| 196 | { |
| 197 | s->s_count++; |
| 198 | spin_unlock(&sb_lock); |
| 199 | down_write(&s->s_umount); |
| 200 | if (s->s_root) { |
| 201 | spin_lock(&sb_lock); |
| 202 | if (s->s_count > S_BIAS) { |
| 203 | atomic_inc(&s->s_active); |
| 204 | s->s_count--; |
| 205 | spin_unlock(&sb_lock); |
| 206 | return 1; |
| 207 | } |
| 208 | spin_unlock(&sb_lock); |
| 209 | } |
| 210 | up_write(&s->s_umount); |
| 211 | put_super(s); |
| 212 | yield(); |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * generic_shutdown_super - common helper for ->kill_sb() |
| 218 | * @sb: superblock to kill |
| 219 | * |
| 220 | * generic_shutdown_super() does all fs-independent work on superblock |
| 221 | * shutdown. Typical ->kill_sb() should pick all fs-specific objects |
| 222 | * that need destruction out of superblock, call generic_shutdown_super() |
| 223 | * and release aforementioned objects. Note: dentries and inodes _are_ |
| 224 | * taken care of and do not need specific handling. |
| 225 | */ |
| 226 | void generic_shutdown_super(struct super_block *sb) |
| 227 | { |
| 228 | struct dentry *root = sb->s_root; |
| 229 | struct super_operations *sop = sb->s_op; |
| 230 | |
| 231 | if (root) { |
| 232 | sb->s_root = NULL; |
| 233 | shrink_dcache_parent(root); |
| 234 | shrink_dcache_anon(&sb->s_anon); |
| 235 | dput(root); |
| 236 | fsync_super(sb); |
| 237 | lock_super(sb); |
| 238 | sb->s_flags &= ~MS_ACTIVE; |
| 239 | /* bad name - it should be evict_inodes() */ |
| 240 | invalidate_inodes(sb); |
| 241 | lock_kernel(); |
| 242 | |
| 243 | if (sop->write_super && sb->s_dirt) |
| 244 | sop->write_super(sb); |
| 245 | if (sop->put_super) |
| 246 | sop->put_super(sb); |
| 247 | |
| 248 | /* Forget any remaining inodes */ |
| 249 | if (invalidate_inodes(sb)) { |
| 250 | printk("VFS: Busy inodes after unmount. " |
| 251 | "Self-destruct in 5 seconds. Have a nice day...\n"); |
| 252 | } |
| 253 | |
| 254 | unlock_kernel(); |
| 255 | unlock_super(sb); |
| 256 | } |
| 257 | spin_lock(&sb_lock); |
| 258 | /* should be initialized for __put_super_and_need_restart() */ |
| 259 | list_del_init(&sb->s_list); |
| 260 | list_del(&sb->s_instances); |
| 261 | spin_unlock(&sb_lock); |
| 262 | up_write(&sb->s_umount); |
| 263 | } |
| 264 | |
| 265 | EXPORT_SYMBOL(generic_shutdown_super); |
| 266 | |
| 267 | /** |
| 268 | * sget - find or create a superblock |
| 269 | * @type: filesystem type superblock should belong to |
| 270 | * @test: comparison callback |
| 271 | * @set: setup callback |
| 272 | * @data: argument to each of them |
| 273 | */ |
| 274 | struct super_block *sget(struct file_system_type *type, |
| 275 | int (*test)(struct super_block *,void *), |
| 276 | int (*set)(struct super_block *,void *), |
| 277 | void *data) |
| 278 | { |
| 279 | struct super_block *s = NULL; |
| 280 | struct list_head *p; |
| 281 | int err; |
| 282 | |
| 283 | retry: |
| 284 | spin_lock(&sb_lock); |
| 285 | if (test) list_for_each(p, &type->fs_supers) { |
| 286 | struct super_block *old; |
| 287 | old = list_entry(p, struct super_block, s_instances); |
| 288 | if (!test(old, data)) |
| 289 | continue; |
| 290 | if (!grab_super(old)) |
| 291 | goto retry; |
| 292 | if (s) |
| 293 | destroy_super(s); |
| 294 | return old; |
| 295 | } |
| 296 | if (!s) { |
| 297 | spin_unlock(&sb_lock); |
| 298 | s = alloc_super(); |
| 299 | if (!s) |
| 300 | return ERR_PTR(-ENOMEM); |
| 301 | goto retry; |
| 302 | } |
| 303 | |
| 304 | err = set(s, data); |
| 305 | if (err) { |
| 306 | spin_unlock(&sb_lock); |
| 307 | destroy_super(s); |
| 308 | return ERR_PTR(err); |
| 309 | } |
| 310 | s->s_type = type; |
| 311 | strlcpy(s->s_id, type->name, sizeof(s->s_id)); |
| 312 | list_add_tail(&s->s_list, &super_blocks); |
| 313 | list_add(&s->s_instances, &type->fs_supers); |
| 314 | spin_unlock(&sb_lock); |
| 315 | get_filesystem(type); |
| 316 | return s; |
| 317 | } |
| 318 | |
| 319 | EXPORT_SYMBOL(sget); |
| 320 | |
| 321 | void drop_super(struct super_block *sb) |
| 322 | { |
| 323 | up_read(&sb->s_umount); |
| 324 | put_super(sb); |
| 325 | } |
| 326 | |
| 327 | EXPORT_SYMBOL(drop_super); |
| 328 | |
| 329 | static inline void write_super(struct super_block *sb) |
| 330 | { |
| 331 | lock_super(sb); |
| 332 | if (sb->s_root && sb->s_dirt) |
| 333 | if (sb->s_op->write_super) |
| 334 | sb->s_op->write_super(sb); |
| 335 | unlock_super(sb); |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Note: check the dirty flag before waiting, so we don't |
| 340 | * hold up the sync while mounting a device. (The newly |
| 341 | * mounted device won't need syncing.) |
| 342 | */ |
| 343 | void sync_supers(void) |
| 344 | { |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 345 | struct super_block *sb; |
| 346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | spin_lock(&sb_lock); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 348 | restart: |
| 349 | list_for_each_entry(sb, &super_blocks, s_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | if (sb->s_dirt) { |
| 351 | sb->s_count++; |
| 352 | spin_unlock(&sb_lock); |
| 353 | down_read(&sb->s_umount); |
| 354 | write_super(sb); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 355 | up_read(&sb->s_umount); |
| 356 | spin_lock(&sb_lock); |
| 357 | if (__put_super_and_need_restart(sb)) |
| 358 | goto restart; |
| 359 | } |
| 360 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | spin_unlock(&sb_lock); |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * Call the ->sync_fs super_op against all filesytems which are r/w and |
| 366 | * which implement it. |
| 367 | * |
| 368 | * This operation is careful to avoid the livelock which could easily happen |
| 369 | * if two or more filesystems are being continuously dirtied. s_need_sync_fs |
| 370 | * is used only here. We set it against all filesystems and then clear it as |
| 371 | * we sync them. So redirtied filesystems are skipped. |
| 372 | * |
| 373 | * But if process A is currently running sync_filesytems and then process B |
| 374 | * calls sync_filesystems as well, process B will set all the s_need_sync_fs |
| 375 | * flags again, which will cause process A to resync everything. Fix that with |
| 376 | * a local mutex. |
| 377 | * |
| 378 | * (Fabian) Avoid sync_fs with clean fs & wait mode 0 |
| 379 | */ |
| 380 | void sync_filesystems(int wait) |
| 381 | { |
| 382 | struct super_block *sb; |
| 383 | static DECLARE_MUTEX(mutex); |
| 384 | |
| 385 | down(&mutex); /* Could be down_interruptible */ |
| 386 | spin_lock(&sb_lock); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 387 | list_for_each_entry(sb, &super_blocks, s_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | if (!sb->s_op->sync_fs) |
| 389 | continue; |
| 390 | if (sb->s_flags & MS_RDONLY) |
| 391 | continue; |
| 392 | sb->s_need_sync_fs = 1; |
| 393 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
| 395 | restart: |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 396 | list_for_each_entry(sb, &super_blocks, s_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | if (!sb->s_need_sync_fs) |
| 398 | continue; |
| 399 | sb->s_need_sync_fs = 0; |
| 400 | if (sb->s_flags & MS_RDONLY) |
| 401 | continue; /* hm. Was remounted r/o meanwhile */ |
| 402 | sb->s_count++; |
| 403 | spin_unlock(&sb_lock); |
| 404 | down_read(&sb->s_umount); |
| 405 | if (sb->s_root && (wait || sb->s_dirt)) |
| 406 | sb->s_op->sync_fs(sb, wait); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 407 | up_read(&sb->s_umount); |
| 408 | /* restart only when sb is no longer on the list */ |
| 409 | spin_lock(&sb_lock); |
| 410 | if (__put_super_and_need_restart(sb)) |
| 411 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | } |
| 413 | spin_unlock(&sb_lock); |
| 414 | up(&mutex); |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * get_super - get the superblock of a device |
| 419 | * @bdev: device to get the superblock for |
| 420 | * |
| 421 | * Scans the superblock list and finds the superblock of the file system |
| 422 | * mounted on the device given. %NULL is returned if no match is found. |
| 423 | */ |
| 424 | |
| 425 | struct super_block * get_super(struct block_device *bdev) |
| 426 | { |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 427 | struct super_block *sb; |
| 428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | if (!bdev) |
| 430 | return NULL; |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | spin_lock(&sb_lock); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 433 | rescan: |
| 434 | list_for_each_entry(sb, &super_blocks, s_list) { |
| 435 | if (sb->s_bdev == bdev) { |
| 436 | sb->s_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | spin_unlock(&sb_lock); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 438 | down_read(&sb->s_umount); |
| 439 | if (sb->s_root) |
| 440 | return sb; |
| 441 | up_read(&sb->s_umount); |
| 442 | /* restart only when sb is no longer on the list */ |
| 443 | spin_lock(&sb_lock); |
| 444 | if (__put_super_and_need_restart(sb)) |
| 445 | goto rescan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | spin_unlock(&sb_lock); |
| 449 | return NULL; |
| 450 | } |
| 451 | |
| 452 | EXPORT_SYMBOL(get_super); |
| 453 | |
| 454 | struct super_block * user_get_super(dev_t dev) |
| 455 | { |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 456 | struct super_block *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | spin_lock(&sb_lock); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 459 | rescan: |
| 460 | list_for_each_entry(sb, &super_blocks, s_list) { |
| 461 | if (sb->s_dev == dev) { |
| 462 | sb->s_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | spin_unlock(&sb_lock); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 464 | down_read(&sb->s_umount); |
| 465 | if (sb->s_root) |
| 466 | return sb; |
| 467 | up_read(&sb->s_umount); |
| 468 | /* restart only when sb is no longer on the list */ |
| 469 | spin_lock(&sb_lock); |
| 470 | if (__put_super_and_need_restart(sb)) |
| 471 | goto rescan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | spin_unlock(&sb_lock); |
| 475 | return NULL; |
| 476 | } |
| 477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf) |
| 479 | { |
| 480 | struct super_block *s; |
| 481 | struct ustat tmp; |
| 482 | struct kstatfs sbuf; |
| 483 | int err = -EINVAL; |
| 484 | |
| 485 | s = user_get_super(new_decode_dev(dev)); |
| 486 | if (s == NULL) |
| 487 | goto out; |
| 488 | err = vfs_statfs(s, &sbuf); |
| 489 | drop_super(s); |
| 490 | if (err) |
| 491 | goto out; |
| 492 | |
| 493 | memset(&tmp,0,sizeof(struct ustat)); |
| 494 | tmp.f_tfree = sbuf.f_bfree; |
| 495 | tmp.f_tinode = sbuf.f_ffree; |
| 496 | |
| 497 | err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0; |
| 498 | out: |
| 499 | return err; |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * mark_files_ro |
| 504 | * @sb: superblock in question |
| 505 | * |
| 506 | * All files are marked read/only. We don't care about pending |
| 507 | * delete files so this should be used in 'force' mode only |
| 508 | */ |
| 509 | |
| 510 | static void mark_files_ro(struct super_block *sb) |
| 511 | { |
| 512 | struct file *f; |
| 513 | |
| 514 | file_list_lock(); |
Eric Dumazet | 2f51201 | 2005-10-30 15:02:16 -0800 | [diff] [blame] | 515 | list_for_each_entry(f, &sb->s_files, f_u.fu_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | if (S_ISREG(f->f_dentry->d_inode->i_mode) && file_count(f)) |
| 517 | f->f_mode &= ~FMODE_WRITE; |
| 518 | } |
| 519 | file_list_unlock(); |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * do_remount_sb - asks filesystem to change mount options. |
| 524 | * @sb: superblock in question |
| 525 | * @flags: numeric part of options |
| 526 | * @data: the rest of options |
| 527 | * @force: whether or not to force the change |
| 528 | * |
| 529 | * Alters the mount options of a mounted file system. |
| 530 | */ |
| 531 | int do_remount_sb(struct super_block *sb, int flags, void *data, int force) |
| 532 | { |
| 533 | int retval; |
| 534 | |
| 535 | if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev)) |
| 536 | return -EACCES; |
| 537 | if (flags & MS_RDONLY) |
| 538 | acct_auto_close(sb); |
| 539 | shrink_dcache_sb(sb); |
| 540 | fsync_super(sb); |
| 541 | |
| 542 | /* If we are remounting RDONLY and current sb is read/write, |
| 543 | make sure there are no rw files opened */ |
| 544 | if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) { |
| 545 | if (force) |
| 546 | mark_files_ro(sb); |
| 547 | else if (!fs_may_remount_ro(sb)) |
| 548 | return -EBUSY; |
| 549 | } |
| 550 | |
| 551 | if (sb->s_op->remount_fs) { |
| 552 | lock_super(sb); |
| 553 | retval = sb->s_op->remount_fs(sb, &flags, data); |
| 554 | unlock_super(sb); |
| 555 | if (retval) |
| 556 | return retval; |
| 557 | } |
| 558 | sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK); |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | static void do_emergency_remount(unsigned long foo) |
| 563 | { |
| 564 | struct super_block *sb; |
| 565 | |
| 566 | spin_lock(&sb_lock); |
| 567 | list_for_each_entry(sb, &super_blocks, s_list) { |
| 568 | sb->s_count++; |
| 569 | spin_unlock(&sb_lock); |
| 570 | down_read(&sb->s_umount); |
| 571 | if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) { |
| 572 | /* |
| 573 | * ->remount_fs needs lock_kernel(). |
| 574 | * |
| 575 | * What lock protects sb->s_flags?? |
| 576 | */ |
| 577 | lock_kernel(); |
| 578 | do_remount_sb(sb, MS_RDONLY, NULL, 1); |
| 579 | unlock_kernel(); |
| 580 | } |
| 581 | drop_super(sb); |
| 582 | spin_lock(&sb_lock); |
| 583 | } |
| 584 | spin_unlock(&sb_lock); |
| 585 | printk("Emergency Remount complete\n"); |
| 586 | } |
| 587 | |
| 588 | void emergency_remount(void) |
| 589 | { |
| 590 | pdflush_operation(do_emergency_remount, 0); |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * Unnamed block devices are dummy devices used by virtual |
| 595 | * filesystems which don't use real block-devices. -- jrs |
| 596 | */ |
| 597 | |
| 598 | static struct idr unnamed_dev_idr; |
| 599 | static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */ |
| 600 | |
| 601 | int set_anon_super(struct super_block *s, void *data) |
| 602 | { |
| 603 | int dev; |
| 604 | int error; |
| 605 | |
| 606 | retry: |
| 607 | if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0) |
| 608 | return -ENOMEM; |
| 609 | spin_lock(&unnamed_dev_lock); |
| 610 | error = idr_get_new(&unnamed_dev_idr, NULL, &dev); |
| 611 | spin_unlock(&unnamed_dev_lock); |
| 612 | if (error == -EAGAIN) |
| 613 | /* We raced and lost with another CPU. */ |
| 614 | goto retry; |
| 615 | else if (error) |
| 616 | return -EAGAIN; |
| 617 | |
| 618 | if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) { |
| 619 | spin_lock(&unnamed_dev_lock); |
| 620 | idr_remove(&unnamed_dev_idr, dev); |
| 621 | spin_unlock(&unnamed_dev_lock); |
| 622 | return -EMFILE; |
| 623 | } |
| 624 | s->s_dev = MKDEV(0, dev & MINORMASK); |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | EXPORT_SYMBOL(set_anon_super); |
| 629 | |
| 630 | void kill_anon_super(struct super_block *sb) |
| 631 | { |
| 632 | int slot = MINOR(sb->s_dev); |
| 633 | |
| 634 | generic_shutdown_super(sb); |
| 635 | spin_lock(&unnamed_dev_lock); |
| 636 | idr_remove(&unnamed_dev_idr, slot); |
| 637 | spin_unlock(&unnamed_dev_lock); |
| 638 | } |
| 639 | |
| 640 | EXPORT_SYMBOL(kill_anon_super); |
| 641 | |
| 642 | void __init unnamed_dev_init(void) |
| 643 | { |
| 644 | idr_init(&unnamed_dev_idr); |
| 645 | } |
| 646 | |
| 647 | void kill_litter_super(struct super_block *sb) |
| 648 | { |
| 649 | if (sb->s_root) |
| 650 | d_genocide(sb->s_root); |
| 651 | kill_anon_super(sb); |
| 652 | } |
| 653 | |
| 654 | EXPORT_SYMBOL(kill_litter_super); |
| 655 | |
| 656 | static int set_bdev_super(struct super_block *s, void *data) |
| 657 | { |
| 658 | s->s_bdev = data; |
| 659 | s->s_dev = s->s_bdev->bd_dev; |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | static int test_bdev_super(struct super_block *s, void *data) |
| 664 | { |
| 665 | return (void *)s->s_bdev == data; |
| 666 | } |
| 667 | |
| 668 | static void bdev_uevent(struct block_device *bdev, enum kobject_action action) |
| 669 | { |
| 670 | if (bdev->bd_disk) { |
| 671 | if (bdev->bd_part) |
| 672 | kobject_uevent(&bdev->bd_part->kobj, action, NULL); |
| 673 | else |
| 674 | kobject_uevent(&bdev->bd_disk->kobj, action, NULL); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | struct super_block *get_sb_bdev(struct file_system_type *fs_type, |
| 679 | int flags, const char *dev_name, void *data, |
| 680 | int (*fill_super)(struct super_block *, void *, int)) |
| 681 | { |
| 682 | struct block_device *bdev; |
| 683 | struct super_block *s; |
| 684 | int error = 0; |
| 685 | |
| 686 | bdev = open_bdev_excl(dev_name, flags, fs_type); |
| 687 | if (IS_ERR(bdev)) |
| 688 | return (struct super_block *)bdev; |
| 689 | |
| 690 | /* |
| 691 | * once the super is inserted into the list by sget, s_umount |
| 692 | * will protect the lockfs code from trying to start a snapshot |
| 693 | * while we are mounting |
| 694 | */ |
| 695 | down(&bdev->bd_mount_sem); |
| 696 | s = sget(fs_type, test_bdev_super, set_bdev_super, bdev); |
| 697 | up(&bdev->bd_mount_sem); |
| 698 | if (IS_ERR(s)) |
| 699 | goto out; |
| 700 | |
| 701 | if (s->s_root) { |
| 702 | if ((flags ^ s->s_flags) & MS_RDONLY) { |
| 703 | up_write(&s->s_umount); |
| 704 | deactivate_super(s); |
| 705 | s = ERR_PTR(-EBUSY); |
| 706 | } |
| 707 | goto out; |
| 708 | } else { |
| 709 | char b[BDEVNAME_SIZE]; |
| 710 | |
| 711 | s->s_flags = flags; |
| 712 | strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); |
| 713 | s->s_old_blocksize = block_size(bdev); |
| 714 | sb_set_blocksize(s, s->s_old_blocksize); |
| 715 | error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0); |
| 716 | if (error) { |
| 717 | up_write(&s->s_umount); |
| 718 | deactivate_super(s); |
| 719 | s = ERR_PTR(error); |
| 720 | } else { |
| 721 | s->s_flags |= MS_ACTIVE; |
| 722 | bdev_uevent(bdev, KOBJ_MOUNT); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | return s; |
| 727 | |
| 728 | out: |
| 729 | close_bdev_excl(bdev); |
| 730 | return s; |
| 731 | } |
| 732 | |
| 733 | EXPORT_SYMBOL(get_sb_bdev); |
| 734 | |
| 735 | void kill_block_super(struct super_block *sb) |
| 736 | { |
| 737 | struct block_device *bdev = sb->s_bdev; |
| 738 | |
| 739 | bdev_uevent(bdev, KOBJ_UMOUNT); |
| 740 | generic_shutdown_super(sb); |
| 741 | sync_blockdev(bdev); |
| 742 | close_bdev_excl(bdev); |
| 743 | } |
| 744 | |
| 745 | EXPORT_SYMBOL(kill_block_super); |
| 746 | |
| 747 | struct super_block *get_sb_nodev(struct file_system_type *fs_type, |
| 748 | int flags, void *data, |
| 749 | int (*fill_super)(struct super_block *, void *, int)) |
| 750 | { |
| 751 | int error; |
| 752 | struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL); |
| 753 | |
| 754 | if (IS_ERR(s)) |
| 755 | return s; |
| 756 | |
| 757 | s->s_flags = flags; |
| 758 | |
| 759 | error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0); |
| 760 | if (error) { |
| 761 | up_write(&s->s_umount); |
| 762 | deactivate_super(s); |
| 763 | return ERR_PTR(error); |
| 764 | } |
| 765 | s->s_flags |= MS_ACTIVE; |
| 766 | return s; |
| 767 | } |
| 768 | |
| 769 | EXPORT_SYMBOL(get_sb_nodev); |
| 770 | |
| 771 | static int compare_single(struct super_block *s, void *p) |
| 772 | { |
| 773 | return 1; |
| 774 | } |
| 775 | |
| 776 | struct super_block *get_sb_single(struct file_system_type *fs_type, |
| 777 | int flags, void *data, |
| 778 | int (*fill_super)(struct super_block *, void *, int)) |
| 779 | { |
| 780 | struct super_block *s; |
| 781 | int error; |
| 782 | |
| 783 | s = sget(fs_type, compare_single, set_anon_super, NULL); |
| 784 | if (IS_ERR(s)) |
| 785 | return s; |
| 786 | if (!s->s_root) { |
| 787 | s->s_flags = flags; |
| 788 | error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0); |
| 789 | if (error) { |
| 790 | up_write(&s->s_umount); |
| 791 | deactivate_super(s); |
| 792 | return ERR_PTR(error); |
| 793 | } |
| 794 | s->s_flags |= MS_ACTIVE; |
| 795 | } |
| 796 | do_remount_sb(s, flags, data, 0); |
| 797 | return s; |
| 798 | } |
| 799 | |
| 800 | EXPORT_SYMBOL(get_sb_single); |
| 801 | |
| 802 | struct vfsmount * |
| 803 | do_kern_mount(const char *fstype, int flags, const char *name, void *data) |
| 804 | { |
| 805 | struct file_system_type *type = get_fs_type(fstype); |
| 806 | struct super_block *sb = ERR_PTR(-ENOMEM); |
| 807 | struct vfsmount *mnt; |
| 808 | int error; |
| 809 | char *secdata = NULL; |
| 810 | |
| 811 | if (!type) |
| 812 | return ERR_PTR(-ENODEV); |
| 813 | |
| 814 | mnt = alloc_vfsmnt(name); |
| 815 | if (!mnt) |
| 816 | goto out; |
| 817 | |
| 818 | if (data) { |
| 819 | secdata = alloc_secdata(); |
| 820 | if (!secdata) { |
| 821 | sb = ERR_PTR(-ENOMEM); |
| 822 | goto out_mnt; |
| 823 | } |
| 824 | |
| 825 | error = security_sb_copy_data(type, data, secdata); |
| 826 | if (error) { |
| 827 | sb = ERR_PTR(error); |
| 828 | goto out_free_secdata; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | sb = type->get_sb(type, flags, name, data); |
| 833 | if (IS_ERR(sb)) |
| 834 | goto out_free_secdata; |
| 835 | error = security_sb_kern_mount(sb, secdata); |
| 836 | if (error) |
| 837 | goto out_sb; |
| 838 | mnt->mnt_sb = sb; |
| 839 | mnt->mnt_root = dget(sb->s_root); |
| 840 | mnt->mnt_mountpoint = sb->s_root; |
| 841 | mnt->mnt_parent = mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | up_write(&sb->s_umount); |
Gerald Schaefer | 8680e22 | 2005-06-21 17:15:16 -0700 | [diff] [blame] | 843 | free_secdata(secdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | put_filesystem(type); |
| 845 | return mnt; |
| 846 | out_sb: |
| 847 | up_write(&sb->s_umount); |
| 848 | deactivate_super(sb); |
| 849 | sb = ERR_PTR(error); |
| 850 | out_free_secdata: |
| 851 | free_secdata(secdata); |
| 852 | out_mnt: |
| 853 | free_vfsmnt(mnt); |
| 854 | out: |
| 855 | put_filesystem(type); |
| 856 | return (struct vfsmount *)sb; |
| 857 | } |
| 858 | |
| 859 | EXPORT_SYMBOL_GPL(do_kern_mount); |
| 860 | |
| 861 | struct vfsmount *kern_mount(struct file_system_type *type) |
| 862 | { |
| 863 | return do_kern_mount(type->name, 0, type->name, NULL); |
| 864 | } |
| 865 | |
| 866 | EXPORT_SYMBOL(kern_mount); |