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); |
Ingo Molnar | 7892f2f | 2006-01-09 15:59:25 -0800 | [diff] [blame] | 75 | mutex_init(&s->s_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) { |
Dave Jones | 7b4fe29 | 2006-02-07 12:58:48 -0800 | [diff] [blame] | 250 | printk("VFS: Busy inodes after unmount of %s. " |
| 251 | "Self-destruct in 5 seconds. Have a nice day...\n", |
| 252 | sb->s_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | unlock_kernel(); |
| 256 | unlock_super(sb); |
| 257 | } |
| 258 | spin_lock(&sb_lock); |
| 259 | /* should be initialized for __put_super_and_need_restart() */ |
| 260 | list_del_init(&sb->s_list); |
| 261 | list_del(&sb->s_instances); |
| 262 | spin_unlock(&sb_lock); |
| 263 | up_write(&sb->s_umount); |
| 264 | } |
| 265 | |
| 266 | EXPORT_SYMBOL(generic_shutdown_super); |
| 267 | |
| 268 | /** |
| 269 | * sget - find or create a superblock |
| 270 | * @type: filesystem type superblock should belong to |
| 271 | * @test: comparison callback |
| 272 | * @set: setup callback |
| 273 | * @data: argument to each of them |
| 274 | */ |
| 275 | struct super_block *sget(struct file_system_type *type, |
| 276 | int (*test)(struct super_block *,void *), |
| 277 | int (*set)(struct super_block *,void *), |
| 278 | void *data) |
| 279 | { |
| 280 | struct super_block *s = NULL; |
| 281 | struct list_head *p; |
| 282 | int err; |
| 283 | |
| 284 | retry: |
| 285 | spin_lock(&sb_lock); |
| 286 | if (test) list_for_each(p, &type->fs_supers) { |
| 287 | struct super_block *old; |
| 288 | old = list_entry(p, struct super_block, s_instances); |
| 289 | if (!test(old, data)) |
| 290 | continue; |
| 291 | if (!grab_super(old)) |
| 292 | goto retry; |
| 293 | if (s) |
| 294 | destroy_super(s); |
| 295 | return old; |
| 296 | } |
| 297 | if (!s) { |
| 298 | spin_unlock(&sb_lock); |
| 299 | s = alloc_super(); |
| 300 | if (!s) |
| 301 | return ERR_PTR(-ENOMEM); |
| 302 | goto retry; |
| 303 | } |
| 304 | |
| 305 | err = set(s, data); |
| 306 | if (err) { |
| 307 | spin_unlock(&sb_lock); |
| 308 | destroy_super(s); |
| 309 | return ERR_PTR(err); |
| 310 | } |
| 311 | s->s_type = type; |
| 312 | strlcpy(s->s_id, type->name, sizeof(s->s_id)); |
| 313 | list_add_tail(&s->s_list, &super_blocks); |
| 314 | list_add(&s->s_instances, &type->fs_supers); |
| 315 | spin_unlock(&sb_lock); |
| 316 | get_filesystem(type); |
| 317 | return s; |
| 318 | } |
| 319 | |
| 320 | EXPORT_SYMBOL(sget); |
| 321 | |
| 322 | void drop_super(struct super_block *sb) |
| 323 | { |
| 324 | up_read(&sb->s_umount); |
| 325 | put_super(sb); |
| 326 | } |
| 327 | |
| 328 | EXPORT_SYMBOL(drop_super); |
| 329 | |
| 330 | static inline void write_super(struct super_block *sb) |
| 331 | { |
| 332 | lock_super(sb); |
| 333 | if (sb->s_root && sb->s_dirt) |
| 334 | if (sb->s_op->write_super) |
| 335 | sb->s_op->write_super(sb); |
| 336 | unlock_super(sb); |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Note: check the dirty flag before waiting, so we don't |
| 341 | * hold up the sync while mounting a device. (The newly |
| 342 | * mounted device won't need syncing.) |
| 343 | */ |
| 344 | void sync_supers(void) |
| 345 | { |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 346 | struct super_block *sb; |
| 347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | spin_lock(&sb_lock); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 349 | restart: |
| 350 | list_for_each_entry(sb, &super_blocks, s_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | if (sb->s_dirt) { |
| 352 | sb->s_count++; |
| 353 | spin_unlock(&sb_lock); |
| 354 | down_read(&sb->s_umount); |
| 355 | write_super(sb); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 356 | up_read(&sb->s_umount); |
| 357 | spin_lock(&sb_lock); |
| 358 | if (__put_super_and_need_restart(sb)) |
| 359 | goto restart; |
| 360 | } |
| 361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | spin_unlock(&sb_lock); |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Call the ->sync_fs super_op against all filesytems which are r/w and |
| 367 | * which implement it. |
| 368 | * |
| 369 | * This operation is careful to avoid the livelock which could easily happen |
| 370 | * if two or more filesystems are being continuously dirtied. s_need_sync_fs |
| 371 | * is used only here. We set it against all filesystems and then clear it as |
| 372 | * we sync them. So redirtied filesystems are skipped. |
| 373 | * |
| 374 | * But if process A is currently running sync_filesytems and then process B |
| 375 | * calls sync_filesystems as well, process B will set all the s_need_sync_fs |
| 376 | * flags again, which will cause process A to resync everything. Fix that with |
| 377 | * a local mutex. |
| 378 | * |
| 379 | * (Fabian) Avoid sync_fs with clean fs & wait mode 0 |
| 380 | */ |
| 381 | void sync_filesystems(int wait) |
| 382 | { |
| 383 | struct super_block *sb; |
| 384 | static DECLARE_MUTEX(mutex); |
| 385 | |
| 386 | down(&mutex); /* Could be down_interruptible */ |
| 387 | spin_lock(&sb_lock); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 388 | list_for_each_entry(sb, &super_blocks, s_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | if (!sb->s_op->sync_fs) |
| 390 | continue; |
| 391 | if (sb->s_flags & MS_RDONLY) |
| 392 | continue; |
| 393 | sb->s_need_sync_fs = 1; |
| 394 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
| 396 | restart: |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 397 | list_for_each_entry(sb, &super_blocks, s_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | if (!sb->s_need_sync_fs) |
| 399 | continue; |
| 400 | sb->s_need_sync_fs = 0; |
| 401 | if (sb->s_flags & MS_RDONLY) |
| 402 | continue; /* hm. Was remounted r/o meanwhile */ |
| 403 | sb->s_count++; |
| 404 | spin_unlock(&sb_lock); |
| 405 | down_read(&sb->s_umount); |
| 406 | if (sb->s_root && (wait || sb->s_dirt)) |
| 407 | sb->s_op->sync_fs(sb, wait); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 408 | up_read(&sb->s_umount); |
| 409 | /* restart only when sb is no longer on the list */ |
| 410 | spin_lock(&sb_lock); |
| 411 | if (__put_super_and_need_restart(sb)) |
| 412 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
| 414 | spin_unlock(&sb_lock); |
| 415 | up(&mutex); |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * get_super - get the superblock of a device |
| 420 | * @bdev: device to get the superblock for |
| 421 | * |
| 422 | * Scans the superblock list and finds the superblock of the file system |
| 423 | * mounted on the device given. %NULL is returned if no match is found. |
| 424 | */ |
| 425 | |
| 426 | struct super_block * get_super(struct block_device *bdev) |
| 427 | { |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 428 | struct super_block *sb; |
| 429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | if (!bdev) |
| 431 | return NULL; |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | spin_lock(&sb_lock); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 434 | rescan: |
| 435 | list_for_each_entry(sb, &super_blocks, s_list) { |
| 436 | if (sb->s_bdev == bdev) { |
| 437 | sb->s_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | spin_unlock(&sb_lock); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 439 | down_read(&sb->s_umount); |
| 440 | if (sb->s_root) |
| 441 | return sb; |
| 442 | up_read(&sb->s_umount); |
| 443 | /* restart only when sb is no longer on the list */ |
| 444 | spin_lock(&sb_lock); |
| 445 | if (__put_super_and_need_restart(sb)) |
| 446 | goto rescan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | spin_unlock(&sb_lock); |
| 450 | return NULL; |
| 451 | } |
| 452 | |
| 453 | EXPORT_SYMBOL(get_super); |
| 454 | |
| 455 | struct super_block * user_get_super(dev_t dev) |
| 456 | { |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 457 | struct super_block *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | spin_lock(&sb_lock); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 460 | rescan: |
| 461 | list_for_each_entry(sb, &super_blocks, s_list) { |
| 462 | if (sb->s_dev == dev) { |
| 463 | sb->s_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | spin_unlock(&sb_lock); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 465 | down_read(&sb->s_umount); |
| 466 | if (sb->s_root) |
| 467 | return sb; |
| 468 | up_read(&sb->s_umount); |
| 469 | /* restart only when sb is no longer on the list */ |
| 470 | spin_lock(&sb_lock); |
| 471 | if (__put_super_and_need_restart(sb)) |
| 472 | goto rescan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | spin_unlock(&sb_lock); |
| 476 | return NULL; |
| 477 | } |
| 478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf) |
| 480 | { |
| 481 | struct super_block *s; |
| 482 | struct ustat tmp; |
| 483 | struct kstatfs sbuf; |
| 484 | int err = -EINVAL; |
| 485 | |
| 486 | s = user_get_super(new_decode_dev(dev)); |
| 487 | if (s == NULL) |
| 488 | goto out; |
| 489 | err = vfs_statfs(s, &sbuf); |
| 490 | drop_super(s); |
| 491 | if (err) |
| 492 | goto out; |
| 493 | |
| 494 | memset(&tmp,0,sizeof(struct ustat)); |
| 495 | tmp.f_tfree = sbuf.f_bfree; |
| 496 | tmp.f_tinode = sbuf.f_ffree; |
| 497 | |
| 498 | err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0; |
| 499 | out: |
| 500 | return err; |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * mark_files_ro |
| 505 | * @sb: superblock in question |
| 506 | * |
| 507 | * All files are marked read/only. We don't care about pending |
| 508 | * delete files so this should be used in 'force' mode only |
| 509 | */ |
| 510 | |
| 511 | static void mark_files_ro(struct super_block *sb) |
| 512 | { |
| 513 | struct file *f; |
| 514 | |
| 515 | file_list_lock(); |
Eric Dumazet | 2f51201 | 2005-10-30 15:02:16 -0800 | [diff] [blame] | 516 | list_for_each_entry(f, &sb->s_files, f_u.fu_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | if (S_ISREG(f->f_dentry->d_inode->i_mode) && file_count(f)) |
| 518 | f->f_mode &= ~FMODE_WRITE; |
| 519 | } |
| 520 | file_list_unlock(); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * do_remount_sb - asks filesystem to change mount options. |
| 525 | * @sb: superblock in question |
| 526 | * @flags: numeric part of options |
| 527 | * @data: the rest of options |
| 528 | * @force: whether or not to force the change |
| 529 | * |
| 530 | * Alters the mount options of a mounted file system. |
| 531 | */ |
| 532 | int do_remount_sb(struct super_block *sb, int flags, void *data, int force) |
| 533 | { |
| 534 | int retval; |
| 535 | |
| 536 | if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev)) |
| 537 | return -EACCES; |
| 538 | if (flags & MS_RDONLY) |
| 539 | acct_auto_close(sb); |
| 540 | shrink_dcache_sb(sb); |
| 541 | fsync_super(sb); |
| 542 | |
| 543 | /* If we are remounting RDONLY and current sb is read/write, |
| 544 | make sure there are no rw files opened */ |
| 545 | if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) { |
| 546 | if (force) |
| 547 | mark_files_ro(sb); |
| 548 | else if (!fs_may_remount_ro(sb)) |
| 549 | return -EBUSY; |
| 550 | } |
| 551 | |
| 552 | if (sb->s_op->remount_fs) { |
| 553 | lock_super(sb); |
| 554 | retval = sb->s_op->remount_fs(sb, &flags, data); |
| 555 | unlock_super(sb); |
| 556 | if (retval) |
| 557 | return retval; |
| 558 | } |
| 559 | sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK); |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static void do_emergency_remount(unsigned long foo) |
| 564 | { |
| 565 | struct super_block *sb; |
| 566 | |
| 567 | spin_lock(&sb_lock); |
| 568 | list_for_each_entry(sb, &super_blocks, s_list) { |
| 569 | sb->s_count++; |
| 570 | spin_unlock(&sb_lock); |
| 571 | down_read(&sb->s_umount); |
| 572 | if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) { |
| 573 | /* |
| 574 | * ->remount_fs needs lock_kernel(). |
| 575 | * |
| 576 | * What lock protects sb->s_flags?? |
| 577 | */ |
| 578 | lock_kernel(); |
| 579 | do_remount_sb(sb, MS_RDONLY, NULL, 1); |
| 580 | unlock_kernel(); |
| 581 | } |
| 582 | drop_super(sb); |
| 583 | spin_lock(&sb_lock); |
| 584 | } |
| 585 | spin_unlock(&sb_lock); |
| 586 | printk("Emergency Remount complete\n"); |
| 587 | } |
| 588 | |
| 589 | void emergency_remount(void) |
| 590 | { |
| 591 | pdflush_operation(do_emergency_remount, 0); |
| 592 | } |
| 593 | |
| 594 | /* |
| 595 | * Unnamed block devices are dummy devices used by virtual |
| 596 | * filesystems which don't use real block-devices. -- jrs |
| 597 | */ |
| 598 | |
| 599 | static struct idr unnamed_dev_idr; |
| 600 | static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */ |
| 601 | |
| 602 | int set_anon_super(struct super_block *s, void *data) |
| 603 | { |
| 604 | int dev; |
| 605 | int error; |
| 606 | |
| 607 | retry: |
| 608 | if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0) |
| 609 | return -ENOMEM; |
| 610 | spin_lock(&unnamed_dev_lock); |
| 611 | error = idr_get_new(&unnamed_dev_idr, NULL, &dev); |
| 612 | spin_unlock(&unnamed_dev_lock); |
| 613 | if (error == -EAGAIN) |
| 614 | /* We raced and lost with another CPU. */ |
| 615 | goto retry; |
| 616 | else if (error) |
| 617 | return -EAGAIN; |
| 618 | |
| 619 | if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) { |
| 620 | spin_lock(&unnamed_dev_lock); |
| 621 | idr_remove(&unnamed_dev_idr, dev); |
| 622 | spin_unlock(&unnamed_dev_lock); |
| 623 | return -EMFILE; |
| 624 | } |
| 625 | s->s_dev = MKDEV(0, dev & MINORMASK); |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | EXPORT_SYMBOL(set_anon_super); |
| 630 | |
| 631 | void kill_anon_super(struct super_block *sb) |
| 632 | { |
| 633 | int slot = MINOR(sb->s_dev); |
| 634 | |
| 635 | generic_shutdown_super(sb); |
| 636 | spin_lock(&unnamed_dev_lock); |
| 637 | idr_remove(&unnamed_dev_idr, slot); |
| 638 | spin_unlock(&unnamed_dev_lock); |
| 639 | } |
| 640 | |
| 641 | EXPORT_SYMBOL(kill_anon_super); |
| 642 | |
| 643 | void __init unnamed_dev_init(void) |
| 644 | { |
| 645 | idr_init(&unnamed_dev_idr); |
| 646 | } |
| 647 | |
| 648 | void kill_litter_super(struct super_block *sb) |
| 649 | { |
| 650 | if (sb->s_root) |
| 651 | d_genocide(sb->s_root); |
| 652 | kill_anon_super(sb); |
| 653 | } |
| 654 | |
| 655 | EXPORT_SYMBOL(kill_litter_super); |
| 656 | |
| 657 | static int set_bdev_super(struct super_block *s, void *data) |
| 658 | { |
| 659 | s->s_bdev = data; |
| 660 | s->s_dev = s->s_bdev->bd_dev; |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | static int test_bdev_super(struct super_block *s, void *data) |
| 665 | { |
| 666 | return (void *)s->s_bdev == data; |
| 667 | } |
| 668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | struct super_block *get_sb_bdev(struct file_system_type *fs_type, |
| 670 | int flags, const char *dev_name, void *data, |
| 671 | int (*fill_super)(struct super_block *, void *, int)) |
| 672 | { |
| 673 | struct block_device *bdev; |
| 674 | struct super_block *s; |
| 675 | int error = 0; |
| 676 | |
| 677 | bdev = open_bdev_excl(dev_name, flags, fs_type); |
| 678 | if (IS_ERR(bdev)) |
| 679 | return (struct super_block *)bdev; |
| 680 | |
| 681 | /* |
| 682 | * once the super is inserted into the list by sget, s_umount |
| 683 | * will protect the lockfs code from trying to start a snapshot |
| 684 | * while we are mounting |
| 685 | */ |
| 686 | down(&bdev->bd_mount_sem); |
| 687 | s = sget(fs_type, test_bdev_super, set_bdev_super, bdev); |
| 688 | up(&bdev->bd_mount_sem); |
| 689 | if (IS_ERR(s)) |
| 690 | goto out; |
| 691 | |
| 692 | if (s->s_root) { |
| 693 | if ((flags ^ s->s_flags) & MS_RDONLY) { |
| 694 | up_write(&s->s_umount); |
| 695 | deactivate_super(s); |
| 696 | s = ERR_PTR(-EBUSY); |
| 697 | } |
| 698 | goto out; |
| 699 | } else { |
| 700 | char b[BDEVNAME_SIZE]; |
| 701 | |
| 702 | s->s_flags = flags; |
| 703 | strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); |
Pekka Enberg | e78c9a0 | 2006-01-08 01:03:39 -0800 | [diff] [blame] | 704 | sb_set_blocksize(s, block_size(bdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0); |
| 706 | if (error) { |
| 707 | up_write(&s->s_umount); |
| 708 | deactivate_super(s); |
| 709 | s = ERR_PTR(error); |
Kay Sievers | 033b96f | 2005-11-11 06:09:55 +0100 | [diff] [blame] | 710 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | s->s_flags |= MS_ACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | return s; |
| 715 | |
| 716 | out: |
| 717 | close_bdev_excl(bdev); |
| 718 | return s; |
| 719 | } |
| 720 | |
| 721 | EXPORT_SYMBOL(get_sb_bdev); |
| 722 | |
| 723 | void kill_block_super(struct super_block *sb) |
| 724 | { |
| 725 | struct block_device *bdev = sb->s_bdev; |
| 726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | generic_shutdown_super(sb); |
| 728 | sync_blockdev(bdev); |
| 729 | close_bdev_excl(bdev); |
| 730 | } |
| 731 | |
| 732 | EXPORT_SYMBOL(kill_block_super); |
| 733 | |
| 734 | struct super_block *get_sb_nodev(struct file_system_type *fs_type, |
| 735 | int flags, void *data, |
| 736 | int (*fill_super)(struct super_block *, void *, int)) |
| 737 | { |
| 738 | int error; |
| 739 | struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL); |
| 740 | |
| 741 | if (IS_ERR(s)) |
| 742 | return s; |
| 743 | |
| 744 | s->s_flags = flags; |
| 745 | |
| 746 | error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0); |
| 747 | if (error) { |
| 748 | up_write(&s->s_umount); |
| 749 | deactivate_super(s); |
| 750 | return ERR_PTR(error); |
| 751 | } |
| 752 | s->s_flags |= MS_ACTIVE; |
| 753 | return s; |
| 754 | } |
| 755 | |
| 756 | EXPORT_SYMBOL(get_sb_nodev); |
| 757 | |
| 758 | static int compare_single(struct super_block *s, void *p) |
| 759 | { |
| 760 | return 1; |
| 761 | } |
| 762 | |
| 763 | struct super_block *get_sb_single(struct file_system_type *fs_type, |
| 764 | int flags, void *data, |
| 765 | int (*fill_super)(struct super_block *, void *, int)) |
| 766 | { |
| 767 | struct super_block *s; |
| 768 | int error; |
| 769 | |
| 770 | s = sget(fs_type, compare_single, set_anon_super, NULL); |
| 771 | if (IS_ERR(s)) |
| 772 | return s; |
| 773 | if (!s->s_root) { |
| 774 | s->s_flags = flags; |
| 775 | error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0); |
| 776 | if (error) { |
| 777 | up_write(&s->s_umount); |
| 778 | deactivate_super(s); |
| 779 | return ERR_PTR(error); |
| 780 | } |
| 781 | s->s_flags |= MS_ACTIVE; |
| 782 | } |
| 783 | do_remount_sb(s, flags, data, 0); |
| 784 | return s; |
| 785 | } |
| 786 | |
| 787 | EXPORT_SYMBOL(get_sb_single); |
| 788 | |
| 789 | struct vfsmount * |
| 790 | do_kern_mount(const char *fstype, int flags, const char *name, void *data) |
| 791 | { |
| 792 | struct file_system_type *type = get_fs_type(fstype); |
| 793 | struct super_block *sb = ERR_PTR(-ENOMEM); |
| 794 | struct vfsmount *mnt; |
| 795 | int error; |
| 796 | char *secdata = NULL; |
| 797 | |
| 798 | if (!type) |
| 799 | return ERR_PTR(-ENODEV); |
| 800 | |
| 801 | mnt = alloc_vfsmnt(name); |
| 802 | if (!mnt) |
| 803 | goto out; |
| 804 | |
| 805 | if (data) { |
| 806 | secdata = alloc_secdata(); |
| 807 | if (!secdata) { |
| 808 | sb = ERR_PTR(-ENOMEM); |
| 809 | goto out_mnt; |
| 810 | } |
| 811 | |
| 812 | error = security_sb_copy_data(type, data, secdata); |
| 813 | if (error) { |
| 814 | sb = ERR_PTR(error); |
| 815 | goto out_free_secdata; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | sb = type->get_sb(type, flags, name, data); |
| 820 | if (IS_ERR(sb)) |
| 821 | goto out_free_secdata; |
| 822 | error = security_sb_kern_mount(sb, secdata); |
| 823 | if (error) |
| 824 | goto out_sb; |
| 825 | mnt->mnt_sb = sb; |
| 826 | mnt->mnt_root = dget(sb->s_root); |
| 827 | mnt->mnt_mountpoint = sb->s_root; |
| 828 | mnt->mnt_parent = mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | up_write(&sb->s_umount); |
Gerald Schaefer | 8680e22 | 2005-06-21 17:15:16 -0700 | [diff] [blame] | 830 | free_secdata(secdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | put_filesystem(type); |
| 832 | return mnt; |
| 833 | out_sb: |
| 834 | up_write(&sb->s_umount); |
| 835 | deactivate_super(sb); |
| 836 | sb = ERR_PTR(error); |
| 837 | out_free_secdata: |
| 838 | free_secdata(secdata); |
| 839 | out_mnt: |
| 840 | free_vfsmnt(mnt); |
| 841 | out: |
| 842 | put_filesystem(type); |
| 843 | return (struct vfsmount *)sb; |
| 844 | } |
| 845 | |
| 846 | EXPORT_SYMBOL_GPL(do_kern_mount); |
| 847 | |
| 848 | struct vfsmount *kern_mount(struct file_system_type *type) |
| 849 | { |
| 850 | return do_kern_mount(type->name, 0, type->name, NULL); |
| 851 | } |
| 852 | |
| 853 | EXPORT_SYMBOL(kern_mount); |