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