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