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