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