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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/acct.h> |
| 26 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/mount.h> |
| 28 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/writeback.h> /* for the emergency remount stuff */ |
| 30 | #include <linux/idr.h> |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 31 | #include <linux/mutex.h> |
Jens Axboe | 5477d0f | 2010-04-29 20:33:35 +0200 | [diff] [blame] | 32 | #include <linux/backing-dev.h> |
Nick Piggin | ceb5bdc | 2011-01-07 17:50:05 +1100 | [diff] [blame] | 33 | #include <linux/rculist_bl.h> |
Al Viro | 6d59e7f | 2008-03-22 15:48:17 -0400 | [diff] [blame] | 34 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | LIST_HEAD(super_blocks); |
| 38 | DEFINE_SPINLOCK(sb_lock); |
| 39 | |
| 40 | /** |
| 41 | * alloc_super - create new superblock |
Henrik Kretzschmar | fe2bbc4 | 2006-09-06 00:03:41 -0700 | [diff] [blame] | 42 | * @type: filesystem type superblock should belong to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | * |
| 44 | * Allocates and initializes a new &struct super_block. alloc_super() |
| 45 | * returns a pointer new superblock or %NULL if allocation had failed. |
| 46 | */ |
Ingo Molnar | cf51624 | 2006-07-03 00:25:27 -0700 | [diff] [blame] | 47 | static struct super_block *alloc_super(struct file_system_type *type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
Oliver Neukum | 11b0b5a | 2006-03-25 03:08:13 -0800 | [diff] [blame] | 49 | struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER); |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 50 | static const struct super_operations default_op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | if (s) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | if (security_sb_alloc(s)) { |
| 54 | kfree(s); |
| 55 | s = NULL; |
| 56 | goto out; |
| 57 | } |
Nick Piggin | 6416ccb | 2010-08-18 04:37:38 +1000 | [diff] [blame] | 58 | #ifdef CONFIG_SMP |
| 59 | s->s_files = alloc_percpu(struct list_head); |
| 60 | if (!s->s_files) { |
| 61 | security_sb_free(s); |
| 62 | kfree(s); |
| 63 | s = NULL; |
| 64 | goto out; |
| 65 | } else { |
| 66 | int i; |
| 67 | |
| 68 | for_each_possible_cpu(i) |
| 69 | INIT_LIST_HEAD(per_cpu_ptr(s->s_files, i)); |
| 70 | } |
| 71 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | INIT_LIST_HEAD(&s->s_files); |
Nick Piggin | 6416ccb | 2010-08-18 04:37:38 +1000 | [diff] [blame] | 73 | #endif |
Jens Axboe | 95f2860 | 2011-03-17 11:13:12 +0100 | [diff] [blame] | 74 | s->s_bdi = &default_backing_dev_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | INIT_LIST_HEAD(&s->s_instances); |
Nick Piggin | ceb5bdc | 2011-01-07 17:50:05 +1100 | [diff] [blame] | 76 | INIT_HLIST_BL_HEAD(&s->s_anon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | INIT_LIST_HEAD(&s->s_inodes); |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 78 | INIT_LIST_HEAD(&s->s_dentry_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | init_rwsem(&s->s_umount); |
Ingo Molnar | 7892f2f | 2006-01-09 15:59:25 -0800 | [diff] [blame] | 80 | mutex_init(&s->s_lock); |
Arjan van de Ven | 897c6ff | 2006-07-03 00:25:28 -0700 | [diff] [blame] | 81 | lockdep_set_class(&s->s_umount, &type->s_umount_key); |
Ingo Molnar | cf51624 | 2006-07-03 00:25:27 -0700 | [diff] [blame] | 82 | /* |
| 83 | * The locking rules for s_lock are up to the |
| 84 | * filesystem. For example ext3fs has different |
| 85 | * lock ordering than usbfs: |
| 86 | */ |
| 87 | lockdep_set_class(&s->s_lock, &type->s_lock_key); |
Peter Zijlstra | ada723d | 2009-02-18 14:48:30 -0800 | [diff] [blame] | 88 | /* |
| 89 | * sget() can have s_umount recursion. |
| 90 | * |
| 91 | * When it cannot find a suitable sb, it allocates a new |
| 92 | * one (this one), and tries again to find a suitable old |
| 93 | * one. |
| 94 | * |
| 95 | * In case that succeeds, it will acquire the s_umount |
| 96 | * lock of the old one. Since these are clearly distrinct |
| 97 | * locks, and this object isn't exposed yet, there's no |
| 98 | * risk of deadlocks. |
| 99 | * |
| 100 | * Annotate this by putting this lock in a different |
| 101 | * subclass. |
| 102 | */ |
| 103 | down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING); |
Al Viro | b20bd1a | 2010-03-22 08:53:19 -0400 | [diff] [blame] | 104 | s->s_count = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | atomic_set(&s->s_active, 1); |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 106 | mutex_init(&s->s_vfs_rename_mutex); |
Roland Dreier | 51ee049 | 2010-04-27 14:23:57 -0700 | [diff] [blame] | 107 | lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 108 | mutex_init(&s->s_dquot.dqio_mutex); |
| 109 | mutex_init(&s->s_dquot.dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | init_rwsem(&s->s_dquot.dqptr_sem); |
| 111 | init_waitqueue_head(&s->s_wait_unfrozen); |
| 112 | s->s_maxbytes = MAX_NON_LFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | s->s_op = &default_op; |
| 114 | s->s_time_gran = 1000000000; |
| 115 | } |
| 116 | out: |
| 117 | return s; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * destroy_super - frees a superblock |
| 122 | * @s: superblock to free |
| 123 | * |
| 124 | * Frees a superblock. |
| 125 | */ |
| 126 | static inline void destroy_super(struct super_block *s) |
| 127 | { |
Nick Piggin | 6416ccb | 2010-08-18 04:37:38 +1000 | [diff] [blame] | 128 | #ifdef CONFIG_SMP |
| 129 | free_percpu(s->s_files); |
| 130 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | security_sb_free(s); |
Miklos Szeredi | 79c0b2d | 2007-05-08 00:25:43 -0700 | [diff] [blame] | 132 | kfree(s->s_subtype); |
Miklos Szeredi | b3b304a | 2008-02-08 04:21:35 -0800 | [diff] [blame] | 133 | kfree(s->s_options); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | kfree(s); |
| 135 | } |
| 136 | |
| 137 | /* Superblock refcounting */ |
| 138 | |
| 139 | /* |
Al Viro | 35cf7ba | 2010-03-22 21:13:53 -0400 | [diff] [blame] | 140 | * Drop a superblock's refcount. The caller must hold sb_lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | */ |
Al Viro | 35cf7ba | 2010-03-22 21:13:53 -0400 | [diff] [blame] | 142 | void __put_super(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | if (!--sb->s_count) { |
Al Viro | 551de6f | 2010-03-22 19:36:35 -0400 | [diff] [blame] | 145 | list_del_init(&sb->s_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | destroy_super(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 157 | void put_super(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
| 159 | spin_lock(&sb_lock); |
| 160 | __put_super(sb); |
| 161 | spin_unlock(&sb_lock); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | /** |
Al Viro | 74dbbdd | 2009-05-06 01:07:50 -0400 | [diff] [blame] | 166 | * deactivate_locked_super - drop an active reference to superblock |
| 167 | * @s: superblock to deactivate |
| 168 | * |
Al Viro | 1712ac8 | 2010-03-22 15:22:31 -0400 | [diff] [blame] | 169 | * Drops an active reference to superblock, converting it into a temprory |
| 170 | * one if there is no other active references left. In that case we |
| 171 | * tell fs driver to shut it down and drop the temporary reference we |
| 172 | * had just acquired. |
| 173 | * |
| 174 | * Caller holds exclusive lock on superblock; that lock is released. |
Al Viro | 74dbbdd | 2009-05-06 01:07:50 -0400 | [diff] [blame] | 175 | */ |
| 176 | void deactivate_locked_super(struct super_block *s) |
| 177 | { |
| 178 | struct file_system_type *fs = s->s_type; |
Al Viro | b20bd1a | 2010-03-22 08:53:19 -0400 | [diff] [blame] | 179 | if (atomic_dec_and_test(&s->s_active)) { |
Al Viro | 74dbbdd | 2009-05-06 01:07:50 -0400 | [diff] [blame] | 180 | fs->kill_sb(s); |
Boaz Harrosh | d863b50 | 2011-02-10 15:01:20 -0800 | [diff] [blame] | 181 | /* |
| 182 | * We need to call rcu_barrier so all the delayed rcu free |
| 183 | * inodes are flushed before we release the fs module. |
| 184 | */ |
| 185 | rcu_barrier(); |
Al Viro | 74dbbdd | 2009-05-06 01:07:50 -0400 | [diff] [blame] | 186 | put_filesystem(fs); |
| 187 | put_super(s); |
| 188 | } else { |
| 189 | up_write(&s->s_umount); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | EXPORT_SYMBOL(deactivate_locked_super); |
| 194 | |
| 195 | /** |
Al Viro | 1712ac8 | 2010-03-22 15:22:31 -0400 | [diff] [blame] | 196 | * deactivate_super - drop an active reference to superblock |
| 197 | * @s: superblock to deactivate |
| 198 | * |
| 199 | * Variant of deactivate_locked_super(), except that superblock is *not* |
| 200 | * locked by caller. If we are going to drop the final active reference, |
| 201 | * lock will be acquired prior to that. |
| 202 | */ |
| 203 | void deactivate_super(struct super_block *s) |
| 204 | { |
| 205 | if (!atomic_add_unless(&s->s_active, -1, 1)) { |
| 206 | down_write(&s->s_umount); |
| 207 | deactivate_locked_super(s); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | EXPORT_SYMBOL(deactivate_super); |
| 212 | |
| 213 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | * grab_super - acquire an active reference |
| 215 | * @s: reference we are trying to make active |
| 216 | * |
| 217 | * Tries to acquire an active reference. grab_super() is used when we |
| 218 | * had just found a superblock in super_blocks or fs_type->fs_supers |
| 219 | * and want to turn it into a full-blown active reference. grab_super() |
| 220 | * is called with sb_lock held and drops it. Returns 1 in case of |
| 221 | * success, 0 if we had failed (superblock contents was already dead or |
| 222 | * dying when grab_super() had been called). |
| 223 | */ |
Josh Triplett | 9c4dbee | 2006-09-29 01:59:29 -0700 | [diff] [blame] | 224 | static int grab_super(struct super_block *s) __releases(sb_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
Al Viro | b20bd1a | 2010-03-22 08:53:19 -0400 | [diff] [blame] | 226 | if (atomic_inc_not_zero(&s->s_active)) { |
| 227 | spin_unlock(&sb_lock); |
Al Viro | b20bd1a | 2010-03-22 08:53:19 -0400 | [diff] [blame] | 228 | return 1; |
| 229 | } |
| 230 | /* it's going away */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | s->s_count++; |
| 232 | spin_unlock(&sb_lock); |
Al Viro | 1712ac8 | 2010-03-22 15:22:31 -0400 | [diff] [blame] | 233 | /* wait for it to die */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | down_write(&s->s_umount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | up_write(&s->s_umount); |
| 236 | put_super(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | return 0; |
| 238 | } |
| 239 | |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 240 | /* |
Al Viro | 914e263 | 2006-10-18 13:55:46 -0400 | [diff] [blame] | 241 | * Superblock locking. We really ought to get rid of these two. |
| 242 | */ |
| 243 | void lock_super(struct super_block * sb) |
| 244 | { |
| 245 | get_fs_excl(); |
| 246 | mutex_lock(&sb->s_lock); |
| 247 | } |
| 248 | |
| 249 | void unlock_super(struct super_block * sb) |
| 250 | { |
| 251 | put_fs_excl(); |
| 252 | mutex_unlock(&sb->s_lock); |
| 253 | } |
| 254 | |
| 255 | EXPORT_SYMBOL(lock_super); |
| 256 | EXPORT_SYMBOL(unlock_super); |
| 257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | /** |
| 259 | * generic_shutdown_super - common helper for ->kill_sb() |
| 260 | * @sb: superblock to kill |
| 261 | * |
| 262 | * generic_shutdown_super() does all fs-independent work on superblock |
| 263 | * shutdown. Typical ->kill_sb() should pick all fs-specific objects |
| 264 | * that need destruction out of superblock, call generic_shutdown_super() |
| 265 | * and release aforementioned objects. Note: dentries and inodes _are_ |
| 266 | * taken care of and do not need specific handling. |
David Howells | c636ebd | 2006-10-11 01:22:19 -0700 | [diff] [blame] | 267 | * |
| 268 | * Upon calling this function, the filesystem may no longer alter or |
| 269 | * rearrange the set of dentries belonging to this super_block, nor may it |
| 270 | * change the attachments of dentries to inodes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | */ |
| 272 | void generic_shutdown_super(struct super_block *sb) |
| 273 | { |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 274 | const struct super_operations *sop = sb->s_op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
Arjan van de Ven | efaee19 | 2009-01-06 07:20:54 -0800 | [diff] [blame] | 276 | |
David Howells | c636ebd | 2006-10-11 01:22:19 -0700 | [diff] [blame] | 277 | if (sb->s_root) { |
| 278 | shrink_dcache_for_umount(sb); |
Jan Kara | 60b0680 | 2009-04-27 16:43:53 +0200 | [diff] [blame] | 279 | sync_filesystem(sb); |
Al Viro | a9e220f | 2009-05-05 22:10:44 -0400 | [diff] [blame] | 280 | get_fs_excl(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | sb->s_flags &= ~MS_ACTIVE; |
Arjan van de Ven | efaee19 | 2009-01-06 07:20:54 -0800 | [diff] [blame] | 282 | |
Al Viro | 63997e9 | 2010-10-25 20:49:35 -0400 | [diff] [blame] | 283 | fsnotify_unmount_inodes(&sb->s_inodes); |
| 284 | |
| 285 | evict_inodes(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | if (sop->put_super) |
| 288 | sop->put_super(sb); |
| 289 | |
Al Viro | 63997e9 | 2010-10-25 20:49:35 -0400 | [diff] [blame] | 290 | if (!list_empty(&sb->s_inodes)) { |
Dave Jones | 7b4fe29 | 2006-02-07 12:58:48 -0800 | [diff] [blame] | 291 | printk("VFS: Busy inodes after unmount of %s. " |
| 292 | "Self-destruct in 5 seconds. Have a nice day...\n", |
| 293 | sb->s_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
Al Viro | a9e220f | 2009-05-05 22:10:44 -0400 | [diff] [blame] | 295 | put_fs_excl(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | } |
| 297 | spin_lock(&sb_lock); |
| 298 | /* should be initialized for __put_super_and_need_restart() */ |
Al Viro | 551de6f | 2010-03-22 19:36:35 -0400 | [diff] [blame] | 299 | list_del_init(&sb->s_instances); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | spin_unlock(&sb_lock); |
| 301 | up_write(&sb->s_umount); |
| 302 | } |
| 303 | |
| 304 | EXPORT_SYMBOL(generic_shutdown_super); |
| 305 | |
| 306 | /** |
| 307 | * sget - find or create a superblock |
| 308 | * @type: filesystem type superblock should belong to |
| 309 | * @test: comparison callback |
| 310 | * @set: setup callback |
| 311 | * @data: argument to each of them |
| 312 | */ |
| 313 | struct super_block *sget(struct file_system_type *type, |
| 314 | int (*test)(struct super_block *,void *), |
| 315 | int (*set)(struct super_block *,void *), |
| 316 | void *data) |
| 317 | { |
| 318 | struct super_block *s = NULL; |
Matthias Kaehlcke | d473012 | 2007-10-18 23:39:57 -0700 | [diff] [blame] | 319 | struct super_block *old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | int err; |
| 321 | |
| 322 | retry: |
| 323 | spin_lock(&sb_lock); |
Matthias Kaehlcke | d473012 | 2007-10-18 23:39:57 -0700 | [diff] [blame] | 324 | if (test) { |
| 325 | list_for_each_entry(old, &type->fs_supers, s_instances) { |
| 326 | if (!test(old, data)) |
| 327 | continue; |
| 328 | if (!grab_super(old)) |
| 329 | goto retry; |
Li Zefan | a3cfbb5 | 2009-03-12 14:31:29 -0700 | [diff] [blame] | 330 | if (s) { |
| 331 | up_write(&s->s_umount); |
Matthias Kaehlcke | d473012 | 2007-10-18 23:39:57 -0700 | [diff] [blame] | 332 | destroy_super(s); |
Al Viro | 7a4dec5 | 2010-08-09 12:05:43 -0400 | [diff] [blame] | 333 | s = NULL; |
Li Zefan | a3cfbb5 | 2009-03-12 14:31:29 -0700 | [diff] [blame] | 334 | } |
Al Viro | d3f2147 | 2010-03-23 11:11:05 -0400 | [diff] [blame] | 335 | down_write(&old->s_umount); |
Al Viro | 7a4dec5 | 2010-08-09 12:05:43 -0400 | [diff] [blame] | 336 | if (unlikely(!(old->s_flags & MS_BORN))) { |
| 337 | deactivate_locked_super(old); |
| 338 | goto retry; |
| 339 | } |
Matthias Kaehlcke | d473012 | 2007-10-18 23:39:57 -0700 | [diff] [blame] | 340 | return old; |
| 341 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } |
| 343 | if (!s) { |
| 344 | spin_unlock(&sb_lock); |
Ingo Molnar | cf51624 | 2006-07-03 00:25:27 -0700 | [diff] [blame] | 345 | s = alloc_super(type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | if (!s) |
| 347 | return ERR_PTR(-ENOMEM); |
| 348 | goto retry; |
| 349 | } |
| 350 | |
| 351 | err = set(s, data); |
| 352 | if (err) { |
| 353 | spin_unlock(&sb_lock); |
Li Zefan | a3cfbb5 | 2009-03-12 14:31:29 -0700 | [diff] [blame] | 354 | up_write(&s->s_umount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | destroy_super(s); |
| 356 | return ERR_PTR(err); |
| 357 | } |
| 358 | s->s_type = type; |
| 359 | strlcpy(s->s_id, type->name, sizeof(s->s_id)); |
| 360 | list_add_tail(&s->s_list, &super_blocks); |
| 361 | list_add(&s->s_instances, &type->fs_supers); |
| 362 | spin_unlock(&sb_lock); |
| 363 | get_filesystem(type); |
| 364 | return s; |
| 365 | } |
| 366 | |
| 367 | EXPORT_SYMBOL(sget); |
| 368 | |
| 369 | void drop_super(struct super_block *sb) |
| 370 | { |
| 371 | up_read(&sb->s_umount); |
| 372 | put_super(sb); |
| 373 | } |
| 374 | |
| 375 | EXPORT_SYMBOL(drop_super); |
| 376 | |
Christoph Hellwig | e500475 | 2009-05-05 16:08:56 +0200 | [diff] [blame] | 377 | /** |
| 378 | * sync_supers - helper for periodic superblock writeback |
| 379 | * |
| 380 | * Call the write_super method if present on all dirty superblocks in |
| 381 | * the system. This is for the periodic writeback used by most older |
| 382 | * filesystems. For data integrity superblock writeback use |
| 383 | * sync_filesystems() instead. |
| 384 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | * Note: check the dirty flag before waiting, so we don't |
| 386 | * hold up the sync while mounting a device. (The newly |
| 387 | * mounted device won't need syncing.) |
| 388 | */ |
| 389 | void sync_supers(void) |
| 390 | { |
Al Viro | dca3325 | 2010-07-25 02:31:46 +0400 | [diff] [blame] | 391 | struct super_block *sb, *p = NULL; |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | spin_lock(&sb_lock); |
Al Viro | dca3325 | 2010-07-25 02:31:46 +0400 | [diff] [blame] | 394 | list_for_each_entry(sb, &super_blocks, s_list) { |
Al Viro | 551de6f | 2010-03-22 19:36:35 -0400 | [diff] [blame] | 395 | if (list_empty(&sb->s_instances)) |
| 396 | continue; |
Christoph Hellwig | e500475 | 2009-05-05 16:08:56 +0200 | [diff] [blame] | 397 | if (sb->s_op->write_super && sb->s_dirt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | sb->s_count++; |
| 399 | spin_unlock(&sb_lock); |
Christoph Hellwig | e500475 | 2009-05-05 16:08:56 +0200 | [diff] [blame] | 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | down_read(&sb->s_umount); |
Christoph Hellwig | e500475 | 2009-05-05 16:08:56 +0200 | [diff] [blame] | 402 | if (sb->s_root && sb->s_dirt) |
| 403 | sb->s_op->write_super(sb); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 404 | up_read(&sb->s_umount); |
Christoph Hellwig | e500475 | 2009-05-05 16:08:56 +0200 | [diff] [blame] | 405 | |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 406 | spin_lock(&sb_lock); |
Al Viro | dca3325 | 2010-07-25 02:31:46 +0400 | [diff] [blame] | 407 | if (p) |
| 408 | __put_super(p); |
| 409 | p = sb; |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 410 | } |
| 411 | } |
Al Viro | dca3325 | 2010-07-25 02:31:46 +0400 | [diff] [blame] | 412 | if (p) |
| 413 | __put_super(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | spin_unlock(&sb_lock); |
| 415 | } |
| 416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | /** |
Al Viro | 01a05b3 | 2010-03-23 06:06:58 -0400 | [diff] [blame] | 418 | * iterate_supers - call function for all active superblocks |
| 419 | * @f: function to call |
| 420 | * @arg: argument to pass to it |
| 421 | * |
| 422 | * Scans the superblock list and calls given function, passing it |
| 423 | * locked superblock and given argument. |
| 424 | */ |
| 425 | void iterate_supers(void (*f)(struct super_block *, void *), void *arg) |
| 426 | { |
Al Viro | dca3325 | 2010-07-25 02:31:46 +0400 | [diff] [blame] | 427 | struct super_block *sb, *p = NULL; |
Al Viro | 01a05b3 | 2010-03-23 06:06:58 -0400 | [diff] [blame] | 428 | |
| 429 | spin_lock(&sb_lock); |
Al Viro | dca3325 | 2010-07-25 02:31:46 +0400 | [diff] [blame] | 430 | list_for_each_entry(sb, &super_blocks, s_list) { |
Al Viro | 01a05b3 | 2010-03-23 06:06:58 -0400 | [diff] [blame] | 431 | if (list_empty(&sb->s_instances)) |
| 432 | continue; |
| 433 | sb->s_count++; |
| 434 | spin_unlock(&sb_lock); |
| 435 | |
| 436 | down_read(&sb->s_umount); |
| 437 | if (sb->s_root) |
| 438 | f(sb, arg); |
| 439 | up_read(&sb->s_umount); |
| 440 | |
| 441 | spin_lock(&sb_lock); |
Al Viro | dca3325 | 2010-07-25 02:31:46 +0400 | [diff] [blame] | 442 | if (p) |
| 443 | __put_super(p); |
| 444 | p = sb; |
Al Viro | 01a05b3 | 2010-03-23 06:06:58 -0400 | [diff] [blame] | 445 | } |
Al Viro | dca3325 | 2010-07-25 02:31:46 +0400 | [diff] [blame] | 446 | if (p) |
| 447 | __put_super(p); |
Al Viro | 01a05b3 | 2010-03-23 06:06:58 -0400 | [diff] [blame] | 448 | spin_unlock(&sb_lock); |
| 449 | } |
| 450 | |
| 451 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | * get_super - get the superblock of a device |
| 453 | * @bdev: device to get the superblock for |
| 454 | * |
| 455 | * Scans the superblock list and finds the superblock of the file system |
| 456 | * mounted on the device given. %NULL is returned if no match is found. |
| 457 | */ |
| 458 | |
Al Viro | df40c01 | 2010-03-22 20:23:25 -0400 | [diff] [blame] | 459 | struct super_block *get_super(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | { |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 461 | struct super_block *sb; |
| 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | if (!bdev) |
| 464 | return NULL; |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | spin_lock(&sb_lock); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 467 | rescan: |
| 468 | list_for_each_entry(sb, &super_blocks, s_list) { |
Al Viro | 551de6f | 2010-03-22 19:36:35 -0400 | [diff] [blame] | 469 | if (list_empty(&sb->s_instances)) |
| 470 | continue; |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 471 | if (sb->s_bdev == bdev) { |
| 472 | sb->s_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | spin_unlock(&sb_lock); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 474 | down_read(&sb->s_umount); |
Al Viro | df40c01 | 2010-03-22 20:23:25 -0400 | [diff] [blame] | 475 | /* still alive? */ |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 476 | if (sb->s_root) |
| 477 | return sb; |
| 478 | up_read(&sb->s_umount); |
Al Viro | df40c01 | 2010-03-22 20:23:25 -0400 | [diff] [blame] | 479 | /* nope, got unmounted */ |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 480 | spin_lock(&sb_lock); |
Al Viro | df40c01 | 2010-03-22 20:23:25 -0400 | [diff] [blame] | 481 | __put_super(sb); |
| 482 | goto rescan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | spin_unlock(&sb_lock); |
| 486 | return NULL; |
| 487 | } |
| 488 | |
| 489 | EXPORT_SYMBOL(get_super); |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 490 | |
| 491 | /** |
| 492 | * get_active_super - get an active reference to the superblock of a device |
| 493 | * @bdev: device to get the superblock for |
| 494 | * |
| 495 | * Scans the superblock list and finds the superblock of the file system |
| 496 | * mounted on the device given. Returns the superblock with an active |
Al Viro | d3f2147 | 2010-03-23 11:11:05 -0400 | [diff] [blame] | 497 | * reference or %NULL if none was found. |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 498 | */ |
| 499 | struct super_block *get_active_super(struct block_device *bdev) |
| 500 | { |
| 501 | struct super_block *sb; |
| 502 | |
| 503 | if (!bdev) |
| 504 | return NULL; |
| 505 | |
Al Viro | 1494583 | 2010-03-22 20:15:33 -0400 | [diff] [blame] | 506 | restart: |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 507 | spin_lock(&sb_lock); |
| 508 | list_for_each_entry(sb, &super_blocks, s_list) { |
Al Viro | 551de6f | 2010-03-22 19:36:35 -0400 | [diff] [blame] | 509 | if (list_empty(&sb->s_instances)) |
| 510 | continue; |
Al Viro | 1494583 | 2010-03-22 20:15:33 -0400 | [diff] [blame] | 511 | if (sb->s_bdev == bdev) { |
| 512 | if (grab_super(sb)) /* drops sb_lock */ |
| 513 | return sb; |
| 514 | else |
| 515 | goto restart; |
| 516 | } |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 517 | } |
| 518 | spin_unlock(&sb_lock); |
| 519 | return NULL; |
| 520 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
Al Viro | df40c01 | 2010-03-22 20:23:25 -0400 | [diff] [blame] | 522 | struct super_block *user_get_super(dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 524 | struct super_block *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | spin_lock(&sb_lock); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 527 | rescan: |
| 528 | list_for_each_entry(sb, &super_blocks, s_list) { |
Al Viro | 551de6f | 2010-03-22 19:36:35 -0400 | [diff] [blame] | 529 | if (list_empty(&sb->s_instances)) |
| 530 | continue; |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 531 | if (sb->s_dev == dev) { |
| 532 | sb->s_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | spin_unlock(&sb_lock); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 534 | down_read(&sb->s_umount); |
Al Viro | df40c01 | 2010-03-22 20:23:25 -0400 | [diff] [blame] | 535 | /* still alive? */ |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 536 | if (sb->s_root) |
| 537 | return sb; |
| 538 | up_read(&sb->s_umount); |
Al Viro | df40c01 | 2010-03-22 20:23:25 -0400 | [diff] [blame] | 539 | /* nope, got unmounted */ |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 540 | spin_lock(&sb_lock); |
Al Viro | df40c01 | 2010-03-22 20:23:25 -0400 | [diff] [blame] | 541 | __put_super(sb); |
| 542 | goto rescan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | spin_unlock(&sb_lock); |
| 546 | return NULL; |
| 547 | } |
| 548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | * do_remount_sb - asks filesystem to change mount options. |
| 551 | * @sb: superblock in question |
| 552 | * @flags: numeric part of options |
| 553 | * @data: the rest of options |
| 554 | * @force: whether or not to force the change |
| 555 | * |
| 556 | * Alters the mount options of a mounted file system. |
| 557 | */ |
| 558 | int do_remount_sb(struct super_block *sb, int flags, void *data, int force) |
| 559 | { |
| 560 | int retval; |
Christoph Hellwig | c79d967 | 2010-05-19 07:16:40 -0400 | [diff] [blame] | 561 | int remount_ro; |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 562 | |
| 563 | if (sb->s_frozen != SB_UNFROZEN) |
| 564 | return -EBUSY; |
| 565 | |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 566 | #ifdef CONFIG_BLOCK |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev)) |
| 568 | return -EACCES; |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 569 | #endif |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | if (flags & MS_RDONLY) |
| 572 | acct_auto_close(sb); |
| 573 | shrink_dcache_sb(sb); |
Jan Kara | 60b0680 | 2009-04-27 16:43:53 +0200 | [diff] [blame] | 574 | sync_filesystem(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
Nick Piggin | d208bbd | 2009-12-21 16:28:53 -0800 | [diff] [blame] | 576 | remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY); |
Nick Piggin | d208bbd | 2009-12-21 16:28:53 -0800 | [diff] [blame] | 577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | /* If we are remounting RDONLY and current sb is read/write, |
| 579 | make sure there are no rw files opened */ |
Nick Piggin | d208bbd | 2009-12-21 16:28:53 -0800 | [diff] [blame] | 580 | if (remount_ro) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | if (force) |
| 582 | mark_files_ro(sb); |
J. R. Okajima | b089551 | 2009-06-17 01:16:50 +0900 | [diff] [blame] | 583 | else if (!fs_may_remount_ro(sb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | return -EBUSY; |
| 585 | } |
| 586 | |
| 587 | if (sb->s_op->remount_fs) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | retval = sb->s_op->remount_fs(sb, &flags, data); |
J. R. Okajima | b089551 | 2009-06-17 01:16:50 +0900 | [diff] [blame] | 589 | if (retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | return retval; |
| 591 | } |
| 592 | sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK); |
Christoph Hellwig | c79d967 | 2010-05-19 07:16:40 -0400 | [diff] [blame] | 593 | |
Nick Piggin | d208bbd | 2009-12-21 16:28:53 -0800 | [diff] [blame] | 594 | /* |
| 595 | * Some filesystems modify their metadata via some other path than the |
| 596 | * bdev buffer cache (eg. use a private mapping, or directories in |
| 597 | * pagecache, etc). Also file data modifications go via their own |
| 598 | * mappings. So If we try to mount readonly then copy the filesystem |
| 599 | * from bdev, we could get stale data, so invalidate it to give a best |
| 600 | * effort at coherency. |
| 601 | */ |
| 602 | if (remount_ro && sb->s_bdev) |
| 603 | invalidate_bdev(sb->s_bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | return 0; |
| 605 | } |
| 606 | |
Jens Axboe | a2a9537 | 2009-03-17 09:38:40 +0100 | [diff] [blame] | 607 | static void do_emergency_remount(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | { |
Al Viro | dca3325 | 2010-07-25 02:31:46 +0400 | [diff] [blame] | 609 | struct super_block *sb, *p = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
| 611 | spin_lock(&sb_lock); |
Al Viro | dca3325 | 2010-07-25 02:31:46 +0400 | [diff] [blame] | 612 | list_for_each_entry(sb, &super_blocks, s_list) { |
Al Viro | 551de6f | 2010-03-22 19:36:35 -0400 | [diff] [blame] | 613 | if (list_empty(&sb->s_instances)) |
| 614 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | sb->s_count++; |
| 616 | spin_unlock(&sb_lock); |
Al Viro | 443b94b | 2009-05-05 23:48:50 -0400 | [diff] [blame] | 617 | down_write(&sb->s_umount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) { |
| 619 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | * What lock protects sb->s_flags?? |
| 621 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | do_remount_sb(sb, MS_RDONLY, NULL, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | } |
Al Viro | 443b94b | 2009-05-05 23:48:50 -0400 | [diff] [blame] | 624 | up_write(&sb->s_umount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | spin_lock(&sb_lock); |
Al Viro | dca3325 | 2010-07-25 02:31:46 +0400 | [diff] [blame] | 626 | if (p) |
| 627 | __put_super(p); |
| 628 | p = sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | } |
Al Viro | dca3325 | 2010-07-25 02:31:46 +0400 | [diff] [blame] | 630 | if (p) |
| 631 | __put_super(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | spin_unlock(&sb_lock); |
Jens Axboe | a2a9537 | 2009-03-17 09:38:40 +0100 | [diff] [blame] | 633 | kfree(work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | printk("Emergency Remount complete\n"); |
| 635 | } |
| 636 | |
| 637 | void emergency_remount(void) |
| 638 | { |
Jens Axboe | a2a9537 | 2009-03-17 09:38:40 +0100 | [diff] [blame] | 639 | struct work_struct *work; |
| 640 | |
| 641 | work = kmalloc(sizeof(*work), GFP_ATOMIC); |
| 642 | if (work) { |
| 643 | INIT_WORK(work, do_emergency_remount); |
| 644 | schedule_work(work); |
| 645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /* |
| 649 | * Unnamed block devices are dummy devices used by virtual |
| 650 | * filesystems which don't use real block-devices. -- jrs |
| 651 | */ |
| 652 | |
Alexey Dobriyan | ad76cbc | 2008-08-28 06:26:23 +0400 | [diff] [blame] | 653 | static DEFINE_IDA(unnamed_dev_ida); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */ |
Al Viro | c63e09e | 2009-06-24 02:05:18 -0400 | [diff] [blame] | 655 | static int unnamed_dev_start = 0; /* don't bother trying below it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
| 657 | int set_anon_super(struct super_block *s, void *data) |
| 658 | { |
| 659 | int dev; |
| 660 | int error; |
| 661 | |
| 662 | retry: |
Alexey Dobriyan | ad76cbc | 2008-08-28 06:26:23 +0400 | [diff] [blame] | 663 | if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | return -ENOMEM; |
| 665 | spin_lock(&unnamed_dev_lock); |
Al Viro | c63e09e | 2009-06-24 02:05:18 -0400 | [diff] [blame] | 666 | error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev); |
Al Viro | f21f622 | 2009-06-24 03:12:00 -0400 | [diff] [blame] | 667 | if (!error) |
| 668 | unnamed_dev_start = dev + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | spin_unlock(&unnamed_dev_lock); |
| 670 | if (error == -EAGAIN) |
| 671 | /* We raced and lost with another CPU. */ |
| 672 | goto retry; |
| 673 | else if (error) |
| 674 | return -EAGAIN; |
| 675 | |
| 676 | if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) { |
| 677 | spin_lock(&unnamed_dev_lock); |
Alexey Dobriyan | ad76cbc | 2008-08-28 06:26:23 +0400 | [diff] [blame] | 678 | ida_remove(&unnamed_dev_ida, dev); |
Al Viro | f21f622 | 2009-06-24 03:12:00 -0400 | [diff] [blame] | 679 | if (unnamed_dev_start > dev) |
| 680 | unnamed_dev_start = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | spin_unlock(&unnamed_dev_lock); |
| 682 | return -EMFILE; |
| 683 | } |
| 684 | s->s_dev = MKDEV(0, dev & MINORMASK); |
Jörn Engel | 5129a46 | 2010-04-25 08:54:42 +0200 | [diff] [blame] | 685 | s->s_bdi = &noop_backing_dev_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | EXPORT_SYMBOL(set_anon_super); |
| 690 | |
| 691 | void kill_anon_super(struct super_block *sb) |
| 692 | { |
| 693 | int slot = MINOR(sb->s_dev); |
| 694 | |
| 695 | generic_shutdown_super(sb); |
| 696 | spin_lock(&unnamed_dev_lock); |
Alexey Dobriyan | ad76cbc | 2008-08-28 06:26:23 +0400 | [diff] [blame] | 697 | ida_remove(&unnamed_dev_ida, slot); |
Al Viro | c63e09e | 2009-06-24 02:05:18 -0400 | [diff] [blame] | 698 | if (slot < unnamed_dev_start) |
| 699 | unnamed_dev_start = slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | spin_unlock(&unnamed_dev_lock); |
| 701 | } |
| 702 | |
| 703 | EXPORT_SYMBOL(kill_anon_super); |
| 704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | void kill_litter_super(struct super_block *sb) |
| 706 | { |
| 707 | if (sb->s_root) |
| 708 | d_genocide(sb->s_root); |
| 709 | kill_anon_super(sb); |
| 710 | } |
| 711 | |
| 712 | EXPORT_SYMBOL(kill_litter_super); |
| 713 | |
Serge E. Hallyn | 909e6d9 | 2009-04-06 19:01:07 -0700 | [diff] [blame] | 714 | static int ns_test_super(struct super_block *sb, void *data) |
| 715 | { |
| 716 | return sb->s_fs_info == data; |
| 717 | } |
| 718 | |
| 719 | static int ns_set_super(struct super_block *sb, void *data) |
| 720 | { |
| 721 | sb->s_fs_info = data; |
| 722 | return set_anon_super(sb, NULL); |
| 723 | } |
| 724 | |
Al Viro | ceefda6 | 2010-07-26 13:16:50 +0400 | [diff] [blame] | 725 | struct dentry *mount_ns(struct file_system_type *fs_type, int flags, |
| 726 | void *data, int (*fill_super)(struct super_block *, void *, int)) |
Serge E. Hallyn | 909e6d9 | 2009-04-06 19:01:07 -0700 | [diff] [blame] | 727 | { |
| 728 | struct super_block *sb; |
| 729 | |
| 730 | sb = sget(fs_type, ns_test_super, ns_set_super, data); |
| 731 | if (IS_ERR(sb)) |
Al Viro | ceefda6 | 2010-07-26 13:16:50 +0400 | [diff] [blame] | 732 | return ERR_CAST(sb); |
Serge E. Hallyn | 909e6d9 | 2009-04-06 19:01:07 -0700 | [diff] [blame] | 733 | |
| 734 | if (!sb->s_root) { |
| 735 | int err; |
| 736 | sb->s_flags = flags; |
| 737 | err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0); |
| 738 | if (err) { |
Al Viro | 74dbbdd | 2009-05-06 01:07:50 -0400 | [diff] [blame] | 739 | deactivate_locked_super(sb); |
Al Viro | ceefda6 | 2010-07-26 13:16:50 +0400 | [diff] [blame] | 740 | return ERR_PTR(err); |
Serge E. Hallyn | 909e6d9 | 2009-04-06 19:01:07 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | sb->s_flags |= MS_ACTIVE; |
| 744 | } |
| 745 | |
Al Viro | ceefda6 | 2010-07-26 13:16:50 +0400 | [diff] [blame] | 746 | return dget(sb->s_root); |
Serge E. Hallyn | 909e6d9 | 2009-04-06 19:01:07 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Al Viro | ceefda6 | 2010-07-26 13:16:50 +0400 | [diff] [blame] | 749 | EXPORT_SYMBOL(mount_ns); |
Serge E. Hallyn | 909e6d9 | 2009-04-06 19:01:07 -0700 | [diff] [blame] | 750 | |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 751 | #ifdef CONFIG_BLOCK |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | static int set_bdev_super(struct super_block *s, void *data) |
| 753 | { |
| 754 | s->s_bdev = data; |
| 755 | s->s_dev = s->s_bdev->bd_dev; |
Jens Axboe | 32a88aa | 2009-09-16 15:02:33 +0200 | [diff] [blame] | 756 | |
| 757 | /* |
| 758 | * We set the bdi here to the queue backing, file systems can |
| 759 | * overwrite this in ->fill_super() |
| 760 | */ |
| 761 | s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | static int test_bdev_super(struct super_block *s, void *data) |
| 766 | { |
| 767 | return (void *)s->s_bdev == data; |
| 768 | } |
| 769 | |
Al Viro | 152a083 | 2010-07-25 00:46:55 +0400 | [diff] [blame] | 770 | struct dentry *mount_bdev(struct file_system_type *fs_type, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | int flags, const char *dev_name, void *data, |
Al Viro | 152a083 | 2010-07-25 00:46:55 +0400 | [diff] [blame] | 772 | int (*fill_super)(struct super_block *, void *, int)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | { |
| 774 | struct block_device *bdev; |
| 775 | struct super_block *s; |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 776 | fmode_t mode = FMODE_READ | FMODE_EXCL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | int error = 0; |
| 778 | |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 779 | if (!(flags & MS_RDONLY)) |
| 780 | mode |= FMODE_WRITE; |
| 781 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 782 | bdev = blkdev_get_by_path(dev_name, mode, fs_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | if (IS_ERR(bdev)) |
Al Viro | 152a083 | 2010-07-25 00:46:55 +0400 | [diff] [blame] | 784 | return ERR_CAST(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | |
| 786 | /* |
| 787 | * once the super is inserted into the list by sget, s_umount |
| 788 | * will protect the lockfs code from trying to start a snapshot |
| 789 | * while we are mounting |
| 790 | */ |
Christoph Hellwig | 4fadd7b | 2009-08-03 23:28:06 +0200 | [diff] [blame] | 791 | mutex_lock(&bdev->bd_fsfreeze_mutex); |
| 792 | if (bdev->bd_fsfreeze_count > 0) { |
| 793 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
| 794 | error = -EBUSY; |
| 795 | goto error_bdev; |
| 796 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | s = sget(fs_type, test_bdev_super, set_bdev_super, bdev); |
Christoph Hellwig | 4fadd7b | 2009-08-03 23:28:06 +0200 | [diff] [blame] | 798 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | if (IS_ERR(s)) |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 800 | goto error_s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | |
| 802 | if (s->s_root) { |
| 803 | if ((flags ^ s->s_flags) & MS_RDONLY) { |
Al Viro | 74dbbdd | 2009-05-06 01:07:50 -0400 | [diff] [blame] | 804 | deactivate_locked_super(s); |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 805 | error = -EBUSY; |
| 806 | goto error_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | } |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 808 | |
Tejun Heo | 4f331f0 | 2010-07-20 15:18:07 -0700 | [diff] [blame] | 809 | /* |
| 810 | * s_umount nests inside bd_mutex during |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 811 | * __invalidate_device(). blkdev_put() acquires |
| 812 | * bd_mutex and can't be called under s_umount. Drop |
| 813 | * s_umount temporarily. This is safe as we're |
| 814 | * holding an active reference. |
Tejun Heo | 4f331f0 | 2010-07-20 15:18:07 -0700 | [diff] [blame] | 815 | */ |
| 816 | up_write(&s->s_umount); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 817 | blkdev_put(bdev, mode); |
Tejun Heo | 4f331f0 | 2010-07-20 15:18:07 -0700 | [diff] [blame] | 818 | down_write(&s->s_umount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | } else { |
| 820 | char b[BDEVNAME_SIZE]; |
| 821 | |
| 822 | s->s_flags = flags; |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 823 | s->s_mode = mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); |
Pekka Enberg | e78c9a0 | 2006-01-08 01:03:39 -0800 | [diff] [blame] | 825 | sb_set_blocksize(s, block_size(bdev)); |
Theodore Ts'o | 9b04c99 | 2006-03-24 03:15:10 -0800 | [diff] [blame] | 826 | error = fill_super(s, data, flags & MS_SILENT ? 1 : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | if (error) { |
Al Viro | 74dbbdd | 2009-05-06 01:07:50 -0400 | [diff] [blame] | 828 | deactivate_locked_super(s); |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 829 | goto error; |
Greg Kroah-Hartman | fa67576 | 2006-02-22 09:39:02 -0800 | [diff] [blame] | 830 | } |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 831 | |
| 832 | s->s_flags |= MS_ACTIVE; |
Theodore Ts'o | 87d8fe1 | 2009-01-03 09:47:09 -0500 | [diff] [blame] | 833 | bdev->bd_super = s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Al Viro | 152a083 | 2010-07-25 00:46:55 +0400 | [diff] [blame] | 836 | return dget(s->s_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 838 | error_s: |
| 839 | error = PTR_ERR(s); |
| 840 | error_bdev: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 841 | blkdev_put(bdev, mode); |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 842 | error: |
Al Viro | 152a083 | 2010-07-25 00:46:55 +0400 | [diff] [blame] | 843 | return ERR_PTR(error); |
| 844 | } |
| 845 | EXPORT_SYMBOL(mount_bdev); |
| 846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | void kill_block_super(struct super_block *sb) |
| 848 | { |
| 849 | struct block_device *bdev = sb->s_bdev; |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 850 | fmode_t mode = sb->s_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | |
H Hartley Sweeten | ddbaaf3 | 2009-04-29 20:14:57 -0400 | [diff] [blame] | 852 | bdev->bd_super = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | generic_shutdown_super(sb); |
| 854 | sync_blockdev(bdev); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 855 | WARN_ON_ONCE(!(mode & FMODE_EXCL)); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 856 | blkdev_put(bdev, mode | FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | EXPORT_SYMBOL(kill_block_super); |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 860 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | |
Al Viro | 3c26ff6 | 2010-07-25 11:46:36 +0400 | [diff] [blame] | 862 | struct dentry *mount_nodev(struct file_system_type *fs_type, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | int flags, void *data, |
Al Viro | 3c26ff6 | 2010-07-25 11:46:36 +0400 | [diff] [blame] | 864 | int (*fill_super)(struct super_block *, void *, int)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | { |
| 866 | int error; |
| 867 | struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL); |
| 868 | |
| 869 | if (IS_ERR(s)) |
Al Viro | 3c26ff6 | 2010-07-25 11:46:36 +0400 | [diff] [blame] | 870 | return ERR_CAST(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
| 872 | s->s_flags = flags; |
| 873 | |
Theodore Ts'o | 9b04c99 | 2006-03-24 03:15:10 -0800 | [diff] [blame] | 874 | error = fill_super(s, data, flags & MS_SILENT ? 1 : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | if (error) { |
Al Viro | 74dbbdd | 2009-05-06 01:07:50 -0400 | [diff] [blame] | 876 | deactivate_locked_super(s); |
Al Viro | 3c26ff6 | 2010-07-25 11:46:36 +0400 | [diff] [blame] | 877 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | } |
| 879 | s->s_flags |= MS_ACTIVE; |
Al Viro | 3c26ff6 | 2010-07-25 11:46:36 +0400 | [diff] [blame] | 880 | return dget(s->s_root); |
| 881 | } |
| 882 | EXPORT_SYMBOL(mount_nodev); |
| 883 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | static int compare_single(struct super_block *s, void *p) |
| 885 | { |
| 886 | return 1; |
| 887 | } |
| 888 | |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 889 | struct dentry *mount_single(struct file_system_type *fs_type, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | int flags, void *data, |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 891 | int (*fill_super)(struct super_block *, void *, int)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | { |
| 893 | struct super_block *s; |
| 894 | int error; |
| 895 | |
| 896 | s = sget(fs_type, compare_single, set_anon_super, NULL); |
| 897 | if (IS_ERR(s)) |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 898 | return ERR_CAST(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | if (!s->s_root) { |
| 900 | s->s_flags = flags; |
Theodore Ts'o | 9b04c99 | 2006-03-24 03:15:10 -0800 | [diff] [blame] | 901 | error = fill_super(s, data, flags & MS_SILENT ? 1 : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | if (error) { |
Al Viro | 74dbbdd | 2009-05-06 01:07:50 -0400 | [diff] [blame] | 903 | deactivate_locked_super(s); |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 904 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | } |
| 906 | s->s_flags |= MS_ACTIVE; |
Kay Sievers | 9329d1b | 2009-12-18 21:18:15 +0100 | [diff] [blame] | 907 | } else { |
| 908 | do_remount_sb(s, flags, data, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | } |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 910 | return dget(s->s_root); |
| 911 | } |
| 912 | EXPORT_SYMBOL(mount_single); |
| 913 | |
Al Viro | 9d412a4 | 2011-03-17 22:08:28 -0400 | [diff] [blame] | 914 | struct dentry * |
| 915 | mount_fs(struct file_system_type *type, int flags, const char *name, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | { |
Al Viro | c96e41e | 2010-07-25 00:17:56 +0400 | [diff] [blame] | 917 | struct dentry *root; |
Al Viro | 9d412a4 | 2011-03-17 22:08:28 -0400 | [diff] [blame] | 918 | struct super_block *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | char *secdata = NULL; |
Al Viro | 9d412a4 | 2011-03-17 22:08:28 -0400 | [diff] [blame] | 920 | int error = -ENOMEM; |
Al Viro | 8089352 | 2010-02-05 09:30:46 -0500 | [diff] [blame] | 921 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 922 | if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | secdata = alloc_secdata(); |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 924 | if (!secdata) |
Al Viro | 9d412a4 | 2011-03-17 22:08:28 -0400 | [diff] [blame] | 925 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 927 | error = security_sb_copy_data(data, secdata); |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 928 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | goto out_free_secdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Al Viro | 1a102ff | 2011-03-16 09:07:58 -0400 | [diff] [blame] | 932 | root = type->mount(type, flags, name, data); |
| 933 | if (IS_ERR(root)) { |
| 934 | error = PTR_ERR(root); |
| 935 | goto out_free_secdata; |
Al Viro | c96e41e | 2010-07-25 00:17:56 +0400 | [diff] [blame] | 936 | } |
Al Viro | 9d412a4 | 2011-03-17 22:08:28 -0400 | [diff] [blame] | 937 | sb = root->d_sb; |
| 938 | BUG_ON(!sb); |
| 939 | WARN_ON(!sb->s_bdi); |
Linus Torvalds | 6c51038 | 2011-03-24 10:16:26 -0700 | [diff] [blame] | 940 | WARN_ON(sb->s_bdi == &default_backing_dev_info); |
Al Viro | 9d412a4 | 2011-03-17 22:08:28 -0400 | [diff] [blame] | 941 | sb->s_flags |= MS_BORN; |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 942 | |
Al Viro | 9d412a4 | 2011-03-17 22:08:28 -0400 | [diff] [blame] | 943 | error = security_sb_kern_mount(sb, flags, secdata); |
Jörn Engel | 5129a46 | 2010-04-25 08:54:42 +0200 | [diff] [blame] | 944 | if (error) |
| 945 | goto out_sb; |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 946 | |
Jeff Layton | 42cb56a | 2009-09-18 13:05:53 -0700 | [diff] [blame] | 947 | /* |
| 948 | * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE |
| 949 | * but s_maxbytes was an unsigned long long for many releases. Throw |
| 950 | * this warning for a little while to try and catch filesystems that |
Jeff Layton | 4358b56 | 2011-03-29 09:33:31 -0400 | [diff] [blame] | 951 | * violate this rule. |
Jeff Layton | 42cb56a | 2009-09-18 13:05:53 -0700 | [diff] [blame] | 952 | */ |
Al Viro | 9d412a4 | 2011-03-17 22:08:28 -0400 | [diff] [blame] | 953 | WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to " |
| 954 | "negative value (%lld)\n", type->name, sb->s_maxbytes); |
Jeff Layton | 42cb56a | 2009-09-18 13:05:53 -0700 | [diff] [blame] | 955 | |
Al Viro | 9d412a4 | 2011-03-17 22:08:28 -0400 | [diff] [blame] | 956 | up_write(&sb->s_umount); |
Gerald Schaefer | 8680e22 | 2005-06-21 17:15:16 -0700 | [diff] [blame] | 957 | free_secdata(secdata); |
Al Viro | 9d412a4 | 2011-03-17 22:08:28 -0400 | [diff] [blame] | 958 | return root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | out_sb: |
Al Viro | 9d412a4 | 2011-03-17 22:08:28 -0400 | [diff] [blame] | 960 | dput(root); |
| 961 | deactivate_locked_super(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | out_free_secdata: |
| 963 | free_secdata(secdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | out: |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 965 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | } |
| 967 | |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 968 | /** |
Randy Dunlap | 7000d3c | 2010-05-24 22:22:34 -0700 | [diff] [blame] | 969 | * freeze_super - lock the filesystem and force it into a consistent state |
| 970 | * @sb: the super to lock |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 971 | * |
| 972 | * Syncs the super to make sure the filesystem is consistent and calls the fs's |
| 973 | * freeze_fs. Subsequent calls to this without first thawing the fs will return |
| 974 | * -EBUSY. |
| 975 | */ |
| 976 | int freeze_super(struct super_block *sb) |
| 977 | { |
| 978 | int ret; |
| 979 | |
| 980 | atomic_inc(&sb->s_active); |
| 981 | down_write(&sb->s_umount); |
| 982 | if (sb->s_frozen) { |
| 983 | deactivate_locked_super(sb); |
| 984 | return -EBUSY; |
| 985 | } |
| 986 | |
| 987 | if (sb->s_flags & MS_RDONLY) { |
| 988 | sb->s_frozen = SB_FREEZE_TRANS; |
| 989 | smp_wmb(); |
| 990 | up_write(&sb->s_umount); |
| 991 | return 0; |
| 992 | } |
| 993 | |
| 994 | sb->s_frozen = SB_FREEZE_WRITE; |
| 995 | smp_wmb(); |
| 996 | |
| 997 | sync_filesystem(sb); |
| 998 | |
| 999 | sb->s_frozen = SB_FREEZE_TRANS; |
| 1000 | smp_wmb(); |
| 1001 | |
| 1002 | sync_blockdev(sb->s_bdev); |
| 1003 | if (sb->s_op->freeze_fs) { |
| 1004 | ret = sb->s_op->freeze_fs(sb); |
| 1005 | if (ret) { |
| 1006 | printk(KERN_ERR |
| 1007 | "VFS:Filesystem freeze failed\n"); |
| 1008 | sb->s_frozen = SB_UNFROZEN; |
| 1009 | deactivate_locked_super(sb); |
| 1010 | return ret; |
| 1011 | } |
| 1012 | } |
| 1013 | up_write(&sb->s_umount); |
| 1014 | return 0; |
| 1015 | } |
| 1016 | EXPORT_SYMBOL(freeze_super); |
| 1017 | |
| 1018 | /** |
| 1019 | * thaw_super -- unlock filesystem |
| 1020 | * @sb: the super to thaw |
| 1021 | * |
| 1022 | * Unlocks the filesystem and marks it writeable again after freeze_super(). |
| 1023 | */ |
| 1024 | int thaw_super(struct super_block *sb) |
| 1025 | { |
| 1026 | int error; |
| 1027 | |
| 1028 | down_write(&sb->s_umount); |
| 1029 | if (sb->s_frozen == SB_UNFROZEN) { |
| 1030 | up_write(&sb->s_umount); |
| 1031 | return -EINVAL; |
| 1032 | } |
| 1033 | |
| 1034 | if (sb->s_flags & MS_RDONLY) |
| 1035 | goto out; |
| 1036 | |
| 1037 | if (sb->s_op->unfreeze_fs) { |
| 1038 | error = sb->s_op->unfreeze_fs(sb); |
| 1039 | if (error) { |
| 1040 | printk(KERN_ERR |
| 1041 | "VFS:Filesystem thaw failed\n"); |
| 1042 | sb->s_frozen = SB_FREEZE_TRANS; |
| 1043 | up_write(&sb->s_umount); |
| 1044 | return error; |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | out: |
| 1049 | sb->s_frozen = SB_UNFROZEN; |
| 1050 | smp_wmb(); |
| 1051 | wake_up(&sb->s_wait_unfrozen); |
| 1052 | deactivate_locked_super(sb); |
| 1053 | |
| 1054 | return 0; |
| 1055 | } |
| 1056 | EXPORT_SYMBOL(thaw_super); |