blob: 37a75410079ea92116f9053b1c8dcaba88b441e8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Engelhardt96de0e22007-10-19 23:21:04 +020018 * Torbjörn Lindh (torbjorn.lindh@gopta.se), April 14, 1996.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * 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 Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/acct.h>
26#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/mount.h>
28#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/writeback.h> /* for the emergency remount stuff */
30#include <linux/idr.h>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080031#include <linux/mutex.h>
Jens Axboe5477d0f2010-04-29 20:33:35 +020032#include <linux/backing-dev.h>
Nick Pigginceb5bdc2011-01-07 17:50:05 +110033#include <linux/rculist_bl.h>
Dan Magenheimerc515e1f2011-05-26 10:01:43 -060034#include <linux/cleancache.h>
Al Viro6d59e7f2008-03-22 15:48:17 -040035#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038LIST_HEAD(super_blocks);
39DEFINE_SPINLOCK(sb_lock);
40
Dave Chinnerb0d40c92011-07-08 14:14:42 +100041/*
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 */
48static 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 Torvalds1da177e2005-04-16 15:20:36 -070083/**
84 * alloc_super - create new superblock
Henrik Kretzschmarfe2bbc42006-09-06 00:03:41 -070085 * @type: filesystem type superblock should belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 *
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 Molnarcf516242006-07-03 00:25:27 -070090static struct super_block *alloc_super(struct file_system_type *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Oliver Neukum11b0b5a2006-03-25 03:08:13 -080092 struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
Alexey Dobriyanb87221d2009-09-21 17:01:09 -070093 static const struct super_operations default_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 if (s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (security_sb_alloc(s)) {
97 kfree(s);
98 s = NULL;
99 goto out;
100 }
Nick Piggin6416ccb2010-08-18 04:37:38 +1000101#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 Torvalds1da177e2005-04-16 15:20:36 -0700115 INIT_LIST_HEAD(&s->s_files);
Nick Piggin6416ccb2010-08-18 04:37:38 +1000116#endif
Jens Axboe95f28602011-03-17 11:13:12 +0100117 s->s_bdi = &default_backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 INIT_LIST_HEAD(&s->s_instances);
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100119 INIT_HLIST_BL_HEAD(&s->s_anon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 INIT_LIST_HEAD(&s->s_inodes);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700121 INIT_LIST_HEAD(&s->s_dentry_lru);
Dave Chinner98b745c2011-07-08 14:14:39 +1000122 INIT_LIST_HEAD(&s->s_inode_lru);
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000123 spin_lock_init(&s->s_inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 init_rwsem(&s->s_umount);
Ingo Molnar7892f2f2006-01-09 15:59:25 -0800125 mutex_init(&s->s_lock);
Arjan van de Ven897c6ff2006-07-03 00:25:28 -0700126 lockdep_set_class(&s->s_umount, &type->s_umount_key);
Ingo Molnarcf516242006-07-03 00:25:27 -0700127 /*
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 Zijlstraada723d2009-02-18 14:48:30 -0800133 /*
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 Virob20bd1a2010-03-22 08:53:19 -0400149 s->s_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 atomic_set(&s->s_active, 1);
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800151 mutex_init(&s->s_vfs_rename_mutex);
Roland Dreier51ee0492010-04-27 14:23:57 -0700152 lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
Ingo Molnard3be9152006-03-23 03:00:29 -0800153 mutex_init(&s->s_dquot.dqio_mutex);
154 mutex_init(&s->s_dquot.dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 init_rwsem(&s->s_dquot.dqptr_sem);
156 init_waitqueue_head(&s->s_wait_unfrozen);
157 s->s_maxbytes = MAX_NON_LFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 s->s_op = &default_op;
159 s->s_time_gran = 1000000000;
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600160 s->cleancache_poolid = -1;
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000161
162 s->s_shrink.seeks = DEFAULT_SEEKS;
163 s->s_shrink.shrink = prune_super;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165out:
166 return s;
167}
168
169/**
170 * destroy_super - frees a superblock
171 * @s: superblock to free
172 *
173 * Frees a superblock.
174 */
175static inline void destroy_super(struct super_block *s)
176{
Nick Piggin6416ccb2010-08-18 04:37:38 +1000177#ifdef CONFIG_SMP
178 free_percpu(s->s_files);
179#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 security_sb_free(s);
Miklos Szeredi79c0b2d2007-05-08 00:25:43 -0700181 kfree(s->s_subtype);
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800182 kfree(s->s_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 kfree(s);
184}
185
186/* Superblock refcounting */
187
188/*
Al Viro35cf7ba2010-03-22 21:13:53 -0400189 * Drop a superblock's refcount. The caller must hold sb_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
Al Viro35cf7ba2010-03-22 21:13:53 -0400191void __put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (!--sb->s_count) {
Al Viro551de6f2010-03-22 19:36:35 -0400194 list_del_init(&sb->s_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 destroy_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
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 Axboe03ba3782009-09-09 09:08:54 +0200206void put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 spin_lock(&sb_lock);
209 __put_super(sb);
210 spin_unlock(&sb_lock);
211}
212
213
214/**
Al Viro74dbbdd2009-05-06 01:07:50 -0400215 * deactivate_locked_super - drop an active reference to superblock
216 * @s: superblock to deactivate
217 *
Al Viro1712ac82010-03-22 15:22:31 -0400218 * 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 Viro74dbbdd2009-05-06 01:07:50 -0400224 */
225void deactivate_locked_super(struct super_block *s)
226{
227 struct file_system_type *fs = s->s_type;
Al Virob20bd1a2010-03-22 08:53:19 -0400228 if (atomic_dec_and_test(&s->s_active)) {
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600229 cleancache_flush_fs(s);
Al Viro74dbbdd2009-05-06 01:07:50 -0400230 fs->kill_sb(s);
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000231
232 /* caches are now gone, we can safely kill the shrinker now */
233 unregister_shrinker(&s->s_shrink);
234
Boaz Harroshd863b502011-02-10 15:01:20 -0800235 /*
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 Viro74dbbdd2009-05-06 01:07:50 -0400240 put_filesystem(fs);
241 put_super(s);
242 } else {
243 up_write(&s->s_umount);
244 }
245}
246
247EXPORT_SYMBOL(deactivate_locked_super);
248
249/**
Al Viro1712ac82010-03-22 15:22:31 -0400250 * 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 */
257void 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
265EXPORT_SYMBOL(deactivate_super);
266
267/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * 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 Triplett9c4dbee2006-09-29 01:59:29 -0700278static int grab_super(struct super_block *s) __releases(sb_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Al Virob20bd1a2010-03-22 08:53:19 -0400280 if (atomic_inc_not_zero(&s->s_active)) {
281 spin_unlock(&sb_lock);
Al Virob20bd1a2010-03-22 08:53:19 -0400282 return 1;
283 }
284 /* it's going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 s->s_count++;
286 spin_unlock(&sb_lock);
Al Viro1712ac82010-03-22 15:22:31 -0400287 /* wait for it to die */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 down_write(&s->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 up_write(&s->s_umount);
290 put_super(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return 0;
292}
293
David Howellscf9a2ae2006-08-29 19:05:54 +0100294/*
Dave Chinner12ad3ab2011-07-08 14:14:41 +1000295 * 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 */
306bool 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 Viro914e2632006-10-18 13:55:46 -0400328 * Superblock locking. We really ought to get rid of these two.
329 */
330void lock_super(struct super_block * sb)
331{
332 get_fs_excl();
333 mutex_lock(&sb->s_lock);
334}
335
336void unlock_super(struct super_block * sb)
337{
338 put_fs_excl();
339 mutex_unlock(&sb->s_lock);
340}
341
342EXPORT_SYMBOL(lock_super);
343EXPORT_SYMBOL(unlock_super);
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345/**
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 Howellsc636ebd2006-10-11 01:22:19 -0700354 *
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 Torvalds1da177e2005-04-16 15:20:36 -0700358 */
359void generic_shutdown_super(struct super_block *sb)
360{
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800361 const struct super_operations *sop = sb->s_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
David Howellsc636ebd2006-10-11 01:22:19 -0700363 if (sb->s_root) {
364 shrink_dcache_for_umount(sb);
Jan Kara60b06802009-04-27 16:43:53 +0200365 sync_filesystem(sb);
Al Viroa9e220f2009-05-05 22:10:44 -0400366 get_fs_excl();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 sb->s_flags &= ~MS_ACTIVE;
Arjan van de Venefaee192009-01-06 07:20:54 -0800368
Al Viro63997e92010-10-25 20:49:35 -0400369 fsnotify_unmount_inodes(&sb->s_inodes);
370
371 evict_inodes(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (sop->put_super)
374 sop->put_super(sb);
375
Al Viro63997e92010-10-25 20:49:35 -0400376 if (!list_empty(&sb->s_inodes)) {
Dave Jones7b4fe292006-02-07 12:58:48 -0800377 printk("VFS: Busy inodes after unmount of %s. "
378 "Self-destruct in 5 seconds. Have a nice day...\n",
379 sb->s_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
Al Viroa9e220f2009-05-05 22:10:44 -0400381 put_fs_excl();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383 spin_lock(&sb_lock);
384 /* should be initialized for __put_super_and_need_restart() */
Al Viro551de6f2010-03-22 19:36:35 -0400385 list_del_init(&sb->s_instances);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 spin_unlock(&sb_lock);
387 up_write(&sb->s_umount);
388}
389
390EXPORT_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 */
399struct 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 Kaehlcked4730122007-10-18 23:39:57 -0700405 struct super_block *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 int err;
407
408retry:
409 spin_lock(&sb_lock);
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700410 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 Zefana3cfbb52009-03-12 14:31:29 -0700416 if (s) {
417 up_write(&s->s_umount);
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700418 destroy_super(s);
Al Viro7a4dec52010-08-09 12:05:43 -0400419 s = NULL;
Li Zefana3cfbb52009-03-12 14:31:29 -0700420 }
Al Virod3f21472010-03-23 11:11:05 -0400421 down_write(&old->s_umount);
Al Viro7a4dec52010-08-09 12:05:43 -0400422 if (unlikely(!(old->s_flags & MS_BORN))) {
423 deactivate_locked_super(old);
424 goto retry;
425 }
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700426 return old;
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 if (!s) {
430 spin_unlock(&sb_lock);
Ingo Molnarcf516242006-07-03 00:25:27 -0700431 s = alloc_super(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 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 Zefana3cfbb52009-03-12 14:31:29 -0700440 up_write(&s->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 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 Chinnerb0d40c92011-07-08 14:14:42 +1000450 register_shrinker(&s->s_shrink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return s;
452}
453
454EXPORT_SYMBOL(sget);
455
456void drop_super(struct super_block *sb)
457{
458 up_read(&sb->s_umount);
459 put_super(sb);
460}
461
462EXPORT_SYMBOL(drop_super);
463
Christoph Hellwige5004752009-05-05 16:08:56 +0200464/**
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 Torvalds1da177e2005-04-16 15:20:36 -0700472 * 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 */
476void sync_supers(void)
477{
Al Virodca33252010-07-25 02:31:46 +0400478 struct super_block *sb, *p = NULL;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400481 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400482 if (list_empty(&sb->s_instances))
483 continue;
Christoph Hellwige5004752009-05-05 16:08:56 +0200484 if (sb->s_op->write_super && sb->s_dirt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 sb->s_count++;
486 spin_unlock(&sb_lock);
Christoph Hellwige5004752009-05-05 16:08:56 +0200487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 down_read(&sb->s_umount);
Christoph Hellwige5004752009-05-05 16:08:56 +0200489 if (sb->s_root && sb->s_dirt)
490 sb->s_op->write_super(sb);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700491 up_read(&sb->s_umount);
Christoph Hellwige5004752009-05-05 16:08:56 +0200492
Kirill Korotaev618f0632005-06-23 00:09:54 -0700493 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400494 if (p)
495 __put_super(p);
496 p = sb;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700497 }
498 }
Al Virodca33252010-07-25 02:31:46 +0400499 if (p)
500 __put_super(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 spin_unlock(&sb_lock);
502}
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504/**
Al Viro01a05b32010-03-23 06:06:58 -0400505 * 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 */
512void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
513{
Al Virodca33252010-07-25 02:31:46 +0400514 struct super_block *sb, *p = NULL;
Al Viro01a05b32010-03-23 06:06:58 -0400515
516 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400517 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro01a05b32010-03-23 06:06:58 -0400518 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 Virodca33252010-07-25 02:31:46 +0400529 if (p)
530 __put_super(p);
531 p = sb;
Al Viro01a05b32010-03-23 06:06:58 -0400532 }
Al Virodca33252010-07-25 02:31:46 +0400533 if (p)
534 __put_super(p);
Al Viro01a05b32010-03-23 06:06:58 -0400535 spin_unlock(&sb_lock);
536}
537
538/**
Al Viro43e15cd2011-06-03 20:16:57 -0400539 * 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 */
547void 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
572EXPORT_SYMBOL(iterate_supers_type);
573
574/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 * 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 Virodf40c012010-03-22 20:23:25 -0400582struct super_block *get_super(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700584 struct super_block *sb;
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (!bdev)
587 return NULL;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700590rescan:
591 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400592 if (list_empty(&sb->s_instances))
593 continue;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700594 if (sb->s_bdev == bdev) {
595 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700597 down_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400598 /* still alive? */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700599 if (sb->s_root)
600 return sb;
601 up_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400602 /* nope, got unmounted */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700603 spin_lock(&sb_lock);
Al Virodf40c012010-03-22 20:23:25 -0400604 __put_super(sb);
605 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607 }
608 spin_unlock(&sb_lock);
609 return NULL;
610}
611
612EXPORT_SYMBOL(get_super);
Christoph Hellwig45042302009-08-03 23:28:35 +0200613
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 Virod3f21472010-03-23 11:11:05 -0400620 * reference or %NULL if none was found.
Christoph Hellwig45042302009-08-03 23:28:35 +0200621 */
622struct super_block *get_active_super(struct block_device *bdev)
623{
624 struct super_block *sb;
625
626 if (!bdev)
627 return NULL;
628
Al Viro14945832010-03-22 20:15:33 -0400629restart:
Christoph Hellwig45042302009-08-03 23:28:35 +0200630 spin_lock(&sb_lock);
631 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400632 if (list_empty(&sb->s_instances))
633 continue;
Al Viro14945832010-03-22 20:15:33 -0400634 if (sb->s_bdev == bdev) {
635 if (grab_super(sb)) /* drops sb_lock */
636 return sb;
637 else
638 goto restart;
639 }
Christoph Hellwig45042302009-08-03 23:28:35 +0200640 }
641 spin_unlock(&sb_lock);
642 return NULL;
643}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Al Virodf40c012010-03-22 20:23:25 -0400645struct super_block *user_get_super(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700647 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700650rescan:
651 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400652 if (list_empty(&sb->s_instances))
653 continue;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700654 if (sb->s_dev == dev) {
655 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700657 down_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400658 /* still alive? */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700659 if (sb->s_root)
660 return sb;
661 up_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400662 /* nope, got unmounted */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700663 spin_lock(&sb_lock);
Al Virodf40c012010-03-22 20:23:25 -0400664 __put_super(sb);
665 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667 }
668 spin_unlock(&sb_lock);
669 return NULL;
670}
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 * 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 */
681int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
682{
683 int retval;
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400684 int remount_ro;
Christoph Hellwig45042302009-08-03 23:28:35 +0200685
686 if (sb->s_frozen != SB_UNFROZEN)
687 return -EBUSY;
688
David Howells93614012006-09-30 20:45:40 +0200689#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
691 return -EACCES;
David Howells93614012006-09-30 20:45:40 +0200692#endif
Christoph Hellwig45042302009-08-03 23:28:35 +0200693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (flags & MS_RDONLY)
695 acct_auto_close(sb);
696 shrink_dcache_sb(sb);
Jan Kara60b06802009-04-27 16:43:53 +0200697 sync_filesystem(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Nick Piggind208bbd2009-12-21 16:28:53 -0800699 remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
Nick Piggind208bbd2009-12-21 16:28:53 -0800700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /* If we are remounting RDONLY and current sb is read/write,
702 make sure there are no rw files opened */
Nick Piggind208bbd2009-12-21 16:28:53 -0800703 if (remount_ro) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (force)
705 mark_files_ro(sb);
J. R. Okajimab0895512009-06-17 01:16:50 +0900706 else if (!fs_may_remount_ro(sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return -EBUSY;
708 }
709
710 if (sb->s_op->remount_fs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 retval = sb->s_op->remount_fs(sb, &flags, data);
J. R. Okajimab0895512009-06-17 01:16:50 +0900712 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return retval;
714 }
715 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400716
Nick Piggind208bbd2009-12-21 16:28:53 -0800717 /*
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 Torvalds1da177e2005-04-16 15:20:36 -0700727 return 0;
728}
729
Jens Axboea2a95372009-03-17 09:38:40 +0100730static void do_emergency_remount(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Al Virodca33252010-07-25 02:31:46 +0400732 struct super_block *sb, *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400735 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400736 if (list_empty(&sb->s_instances))
737 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 sb->s_count++;
739 spin_unlock(&sb_lock);
Al Viro443b94b2009-05-05 23:48:50 -0400740 down_write(&sb->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
742 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 * What lock protects sb->s_flags??
744 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 do_remount_sb(sb, MS_RDONLY, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Al Viro443b94b2009-05-05 23:48:50 -0400747 up_write(&sb->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400749 if (p)
750 __put_super(p);
751 p = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Al Virodca33252010-07-25 02:31:46 +0400753 if (p)
754 __put_super(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 spin_unlock(&sb_lock);
Jens Axboea2a95372009-03-17 09:38:40 +0100756 kfree(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 printk("Emergency Remount complete\n");
758}
759
760void emergency_remount(void)
761{
Jens Axboea2a95372009-03-17 09:38:40 +0100762 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 Torvalds1da177e2005-04-16 15:20:36 -0700769}
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 Dobriyanad76cbc2008-08-28 06:26:23 +0400776static DEFINE_IDA(unnamed_dev_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
Al Viroc63e09e2009-06-24 02:05:18 -0400778static int unnamed_dev_start = 0; /* don't bother trying below it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Al Viro0ee5dc62011-07-07 15:44:25 -0400780int get_anon_bdev(dev_t *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 int dev;
783 int error;
784
785 retry:
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400786 if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return -ENOMEM;
788 spin_lock(&unnamed_dev_lock);
Al Viroc63e09e2009-06-24 02:05:18 -0400789 error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
Al Virof21f6222009-06-24 03:12:00 -0400790 if (!error)
791 unnamed_dev_start = dev + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 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 Dobriyanad76cbc2008-08-28 06:26:23 +0400801 ida_remove(&unnamed_dev_ida, dev);
Al Virof21f6222009-06-24 03:12:00 -0400802 if (unnamed_dev_start > dev)
803 unnamed_dev_start = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 spin_unlock(&unnamed_dev_lock);
805 return -EMFILE;
806 }
Al Viro0ee5dc62011-07-07 15:44:25 -0400807 *p = MKDEV(0, dev & MINORMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return 0;
809}
Al Viro0ee5dc62011-07-07 15:44:25 -0400810EXPORT_SYMBOL(get_anon_bdev);
811
812void 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}
821EXPORT_SYMBOL(free_anon_bdev);
822
823int 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 Torvalds1da177e2005-04-16 15:20:36 -0700830
831EXPORT_SYMBOL(set_anon_super);
832
833void kill_anon_super(struct super_block *sb)
834{
Al Viro0ee5dc62011-07-07 15:44:25 -0400835 dev_t dev = sb->s_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 generic_shutdown_super(sb);
Al Viro0ee5dc62011-07-07 15:44:25 -0400837 free_anon_bdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
840EXPORT_SYMBOL(kill_anon_super);
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842void 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
849EXPORT_SYMBOL(kill_litter_super);
850
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700851static int ns_test_super(struct super_block *sb, void *data)
852{
853 return sb->s_fs_info == data;
854}
855
856static 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 Viroceefda62010-07-26 13:16:50 +0400862struct dentry *mount_ns(struct file_system_type *fs_type, int flags,
863 void *data, int (*fill_super)(struct super_block *, void *, int))
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700864{
865 struct super_block *sb;
866
867 sb = sget(fs_type, ns_test_super, ns_set_super, data);
868 if (IS_ERR(sb))
Al Viroceefda62010-07-26 13:16:50 +0400869 return ERR_CAST(sb);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700870
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 Viro74dbbdd2009-05-06 01:07:50 -0400876 deactivate_locked_super(sb);
Al Viroceefda62010-07-26 13:16:50 +0400877 return ERR_PTR(err);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700878 }
879
880 sb->s_flags |= MS_ACTIVE;
881 }
882
Al Viroceefda62010-07-26 13:16:50 +0400883 return dget(sb->s_root);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700884}
885
Al Viroceefda62010-07-26 13:16:50 +0400886EXPORT_SYMBOL(mount_ns);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700887
David Howells93614012006-09-30 20:45:40 +0200888#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889static 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 Axboe32a88aa2009-09-16 15:02:33 +0200893
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 Torvalds1da177e2005-04-16 15:20:36 -0700899 return 0;
900}
901
902static int test_bdev_super(struct super_block *s, void *data)
903{
904 return (void *)s->s_bdev == data;
905}
906
Al Viro152a0832010-07-25 00:46:55 +0400907struct dentry *mount_bdev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 int flags, const char *dev_name, void *data,
Al Viro152a0832010-07-25 00:46:55 +0400909 int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 struct block_device *bdev;
912 struct super_block *s;
Tejun Heod4d77622010-11-13 11:55:18 +0100913 fmode_t mode = FMODE_READ | FMODE_EXCL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 int error = 0;
915
Al Viro30c40d22008-02-22 19:50:45 -0500916 if (!(flags & MS_RDONLY))
917 mode |= FMODE_WRITE;
918
Tejun Heod4d77622010-11-13 11:55:18 +0100919 bdev = blkdev_get_by_path(dev_name, mode, fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (IS_ERR(bdev))
Al Viro152a0832010-07-25 00:46:55 +0400921 return ERR_CAST(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
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 Hellwig4fadd7b2009-08-03 23:28:06 +0200928 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 Torvalds1da177e2005-04-16 15:20:36 -0700934 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
Christoph Hellwig4fadd7b2009-08-03 23:28:06 +0200935 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700937 goto error_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 if (s->s_root) {
940 if ((flags ^ s->s_flags) & MS_RDONLY) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400941 deactivate_locked_super(s);
David Howells454e2392006-06-23 02:02:57 -0700942 error = -EBUSY;
943 goto error_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
David Howells454e2392006-06-23 02:02:57 -0700945
Tejun Heo4f331f02010-07-20 15:18:07 -0700946 /*
947 * s_umount nests inside bd_mutex during
Tejun Heoe525fd82010-11-13 11:55:17 +0100948 * __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 Heo4f331f02010-07-20 15:18:07 -0700952 */
953 up_write(&s->s_umount);
Tejun Heod4d77622010-11-13 11:55:18 +0100954 blkdev_put(bdev, mode);
Tejun Heo4f331f02010-07-20 15:18:07 -0700955 down_write(&s->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 } else {
957 char b[BDEVNAME_SIZE];
958
Al Viro9e1f1de2011-06-03 18:24:58 -0400959 s->s_flags = flags | MS_NOSEC;
Al Viro30c40d22008-02-22 19:50:45 -0500960 s->s_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
Pekka Enberge78c9a02006-01-08 01:03:39 -0800962 sb_set_blocksize(s, block_size(bdev));
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800963 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (error) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400965 deactivate_locked_super(s);
David Howells454e2392006-06-23 02:02:57 -0700966 goto error;
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800967 }
David Howells454e2392006-06-23 02:02:57 -0700968
969 s->s_flags |= MS_ACTIVE;
Theodore Ts'o87d8fe12009-01-03 09:47:09 -0500970 bdev->bd_super = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972
Al Viro152a0832010-07-25 00:46:55 +0400973 return dget(s->s_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
David Howells454e2392006-06-23 02:02:57 -0700975error_s:
976 error = PTR_ERR(s);
977error_bdev:
Tejun Heod4d77622010-11-13 11:55:18 +0100978 blkdev_put(bdev, mode);
David Howells454e2392006-06-23 02:02:57 -0700979error:
Al Viro152a0832010-07-25 00:46:55 +0400980 return ERR_PTR(error);
981}
982EXPORT_SYMBOL(mount_bdev);
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984void kill_block_super(struct super_block *sb)
985{
986 struct block_device *bdev = sb->s_bdev;
Al Viro30c40d22008-02-22 19:50:45 -0500987 fmode_t mode = sb->s_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
H Hartley Sweetenddbaaf32009-04-29 20:14:57 -0400989 bdev->bd_super = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 generic_shutdown_super(sb);
991 sync_blockdev(bdev);
Tejun Heod4d77622010-11-13 11:55:18 +0100992 WARN_ON_ONCE(!(mode & FMODE_EXCL));
Tejun Heoe525fd82010-11-13 11:55:17 +0100993 blkdev_put(bdev, mode | FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
996EXPORT_SYMBOL(kill_block_super);
David Howells93614012006-09-30 20:45:40 +0200997#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Al Viro3c26ff62010-07-25 11:46:36 +0400999struct dentry *mount_nodev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 int flags, void *data,
Al Viro3c26ff62010-07-25 11:46:36 +04001001 int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 int error;
1004 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
1005
1006 if (IS_ERR(s))
Al Viro3c26ff62010-07-25 11:46:36 +04001007 return ERR_CAST(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 s->s_flags = flags;
1010
Theodore Ts'o9b04c992006-03-24 03:15:10 -08001011 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (error) {
Al Viro74dbbdd2009-05-06 01:07:50 -04001013 deactivate_locked_super(s);
Al Viro3c26ff62010-07-25 11:46:36 +04001014 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016 s->s_flags |= MS_ACTIVE;
Al Viro3c26ff62010-07-25 11:46:36 +04001017 return dget(s->s_root);
1018}
1019EXPORT_SYMBOL(mount_nodev);
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021static int compare_single(struct super_block *s, void *p)
1022{
1023 return 1;
1024}
1025
Al Virofc14f2f2010-07-25 01:48:30 +04001026struct dentry *mount_single(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 int flags, void *data,
Al Virofc14f2f2010-07-25 01:48:30 +04001028 int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
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 Virofc14f2f2010-07-25 01:48:30 +04001035 return ERR_CAST(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (!s->s_root) {
1037 s->s_flags = flags;
Theodore Ts'o9b04c992006-03-24 03:15:10 -08001038 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (error) {
Al Viro74dbbdd2009-05-06 01:07:50 -04001040 deactivate_locked_super(s);
Al Virofc14f2f2010-07-25 01:48:30 +04001041 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043 s->s_flags |= MS_ACTIVE;
Kay Sievers9329d1b2009-12-18 21:18:15 +01001044 } else {
1045 do_remount_sb(s, flags, data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
Al Virofc14f2f2010-07-25 01:48:30 +04001047 return dget(s->s_root);
1048}
1049EXPORT_SYMBOL(mount_single);
1050
Al Viro9d412a42011-03-17 22:08:28 -04001051struct dentry *
1052mount_fs(struct file_system_type *type, int flags, const char *name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Al Viroc96e41e2010-07-25 00:17:56 +04001054 struct dentry *root;
Al Viro9d412a42011-03-17 22:08:28 -04001055 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 char *secdata = NULL;
Al Viro9d412a42011-03-17 22:08:28 -04001057 int error = -ENOMEM;
Al Viro80893522010-02-05 09:30:46 -05001058
Eric Parise0007522008-03-05 10:31:54 -05001059 if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 secdata = alloc_secdata();
David Howells454e2392006-06-23 02:02:57 -07001061 if (!secdata)
Al Viro9d412a42011-03-17 22:08:28 -04001062 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Eric Parise0007522008-03-05 10:31:54 -05001064 error = security_sb_copy_data(data, secdata);
David Howells454e2392006-06-23 02:02:57 -07001065 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 goto out_free_secdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068
Al Viro1a102ff2011-03-16 09:07:58 -04001069 root = type->mount(type, flags, name, data);
1070 if (IS_ERR(root)) {
1071 error = PTR_ERR(root);
1072 goto out_free_secdata;
Al Viroc96e41e2010-07-25 00:17:56 +04001073 }
Al Viro9d412a42011-03-17 22:08:28 -04001074 sb = root->d_sb;
1075 BUG_ON(!sb);
1076 WARN_ON(!sb->s_bdi);
Linus Torvalds6c510382011-03-24 10:16:26 -07001077 WARN_ON(sb->s_bdi == &default_backing_dev_info);
Al Viro9d412a42011-03-17 22:08:28 -04001078 sb->s_flags |= MS_BORN;
David Howells454e2392006-06-23 02:02:57 -07001079
Al Viro9d412a42011-03-17 22:08:28 -04001080 error = security_sb_kern_mount(sb, flags, secdata);
Jörn Engel5129a462010-04-25 08:54:42 +02001081 if (error)
1082 goto out_sb;
David Howells454e2392006-06-23 02:02:57 -07001083
Jeff Layton42cb56a2009-09-18 13:05:53 -07001084 /*
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 Layton4358b562011-03-29 09:33:31 -04001088 * violate this rule.
Jeff Layton42cb56a2009-09-18 13:05:53 -07001089 */
Al Viro9d412a42011-03-17 22:08:28 -04001090 WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
1091 "negative value (%lld)\n", type->name, sb->s_maxbytes);
Jeff Layton42cb56a2009-09-18 13:05:53 -07001092
Al Viro9d412a42011-03-17 22:08:28 -04001093 up_write(&sb->s_umount);
Gerald Schaefer8680e222005-06-21 17:15:16 -07001094 free_secdata(secdata);
Al Viro9d412a42011-03-17 22:08:28 -04001095 return root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096out_sb:
Al Viro9d412a42011-03-17 22:08:28 -04001097 dput(root);
1098 deactivate_locked_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099out_free_secdata:
1100 free_secdata(secdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101out:
David Howells454e2392006-06-23 02:02:57 -07001102 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
Josef Bacik18e9e512010-03-23 10:34:56 -04001105/**
Randy Dunlap7000d3c2010-05-24 22:22:34 -07001106 * freeze_super - lock the filesystem and force it into a consistent state
1107 * @sb: the super to lock
Josef Bacik18e9e512010-03-23 10:34:56 -04001108 *
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 */
1113int 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}
1153EXPORT_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 */
1161int 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
1185out:
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}
1193EXPORT_SYMBOL(thaw_super);