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