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