blob: 8a06881b1920b9e164ab276ffe73180ebfbbe97a [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>
Al Viro6d59e7f2008-03-22 15:48:17 -040034#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037LIST_HEAD(super_blocks);
38DEFINE_SPINLOCK(sb_lock);
39
40/**
41 * alloc_super - create new superblock
Henrik Kretzschmarfe2bbc42006-09-06 00:03:41 -070042 * @type: filesystem type superblock should belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 *
44 * Allocates and initializes a new &struct super_block. alloc_super()
45 * returns a pointer new superblock or %NULL if allocation had failed.
46 */
Ingo Molnarcf516242006-07-03 00:25:27 -070047static struct super_block *alloc_super(struct file_system_type *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Oliver Neukum11b0b5a2006-03-25 03:08:13 -080049 struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
Alexey Dobriyanb87221d2009-09-21 17:01:09 -070050 static const struct super_operations default_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 if (s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (security_sb_alloc(s)) {
54 kfree(s);
55 s = NULL;
56 goto out;
57 }
Nick Piggin6416ccb2010-08-18 04:37:38 +100058#ifdef CONFIG_SMP
59 s->s_files = alloc_percpu(struct list_head);
60 if (!s->s_files) {
61 security_sb_free(s);
62 kfree(s);
63 s = NULL;
64 goto out;
65 } else {
66 int i;
67
68 for_each_possible_cpu(i)
69 INIT_LIST_HEAD(per_cpu_ptr(s->s_files, i));
70 }
71#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 INIT_LIST_HEAD(&s->s_files);
Nick Piggin6416ccb2010-08-18 04:37:38 +100073#endif
Jens Axboe95f28602011-03-17 11:13:12 +010074 s->s_bdi = &default_backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 INIT_LIST_HEAD(&s->s_instances);
Nick Pigginceb5bdc2011-01-07 17:50:05 +110076 INIT_HLIST_BL_HEAD(&s->s_anon);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 INIT_LIST_HEAD(&s->s_inodes);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -070078 INIT_LIST_HEAD(&s->s_dentry_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 init_rwsem(&s->s_umount);
Ingo Molnar7892f2f2006-01-09 15:59:25 -080080 mutex_init(&s->s_lock);
Arjan van de Ven897c6ff2006-07-03 00:25:28 -070081 lockdep_set_class(&s->s_umount, &type->s_umount_key);
Ingo Molnarcf516242006-07-03 00:25:27 -070082 /*
83 * The locking rules for s_lock are up to the
84 * filesystem. For example ext3fs has different
85 * lock ordering than usbfs:
86 */
87 lockdep_set_class(&s->s_lock, &type->s_lock_key);
Peter Zijlstraada723d2009-02-18 14:48:30 -080088 /*
89 * sget() can have s_umount recursion.
90 *
91 * When it cannot find a suitable sb, it allocates a new
92 * one (this one), and tries again to find a suitable old
93 * one.
94 *
95 * In case that succeeds, it will acquire the s_umount
96 * lock of the old one. Since these are clearly distrinct
97 * locks, and this object isn't exposed yet, there's no
98 * risk of deadlocks.
99 *
100 * Annotate this by putting this lock in a different
101 * subclass.
102 */
103 down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
Al Virob20bd1a2010-03-22 08:53:19 -0400104 s->s_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 atomic_set(&s->s_active, 1);
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800106 mutex_init(&s->s_vfs_rename_mutex);
Roland Dreier51ee0492010-04-27 14:23:57 -0700107 lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
Ingo Molnard3be9152006-03-23 03:00:29 -0800108 mutex_init(&s->s_dquot.dqio_mutex);
109 mutex_init(&s->s_dquot.dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 init_rwsem(&s->s_dquot.dqptr_sem);
111 init_waitqueue_head(&s->s_wait_unfrozen);
112 s->s_maxbytes = MAX_NON_LFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 s->s_op = &default_op;
114 s->s_time_gran = 1000000000;
115 }
116out:
117 return s;
118}
119
120/**
121 * destroy_super - frees a superblock
122 * @s: superblock to free
123 *
124 * Frees a superblock.
125 */
126static inline void destroy_super(struct super_block *s)
127{
Nick Piggin6416ccb2010-08-18 04:37:38 +1000128#ifdef CONFIG_SMP
129 free_percpu(s->s_files);
130#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 security_sb_free(s);
Miklos Szeredi79c0b2d2007-05-08 00:25:43 -0700132 kfree(s->s_subtype);
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800133 kfree(s->s_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 kfree(s);
135}
136
137/* Superblock refcounting */
138
139/*
Al Viro35cf7ba2010-03-22 21:13:53 -0400140 * Drop a superblock's refcount. The caller must hold sb_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 */
Al Viro35cf7ba2010-03-22 21:13:53 -0400142void __put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (!--sb->s_count) {
Al Viro551de6f2010-03-22 19:36:35 -0400145 list_del_init(&sb->s_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 destroy_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
150/**
151 * put_super - drop a temporary reference to superblock
152 * @sb: superblock in question
153 *
154 * Drops a temporary reference, frees superblock if there's no
155 * references left.
156 */
Jens Axboe03ba3782009-09-09 09:08:54 +0200157void put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 spin_lock(&sb_lock);
160 __put_super(sb);
161 spin_unlock(&sb_lock);
162}
163
164
165/**
Al Viro74dbbdd2009-05-06 01:07:50 -0400166 * deactivate_locked_super - drop an active reference to superblock
167 * @s: superblock to deactivate
168 *
Al Viro1712ac82010-03-22 15:22:31 -0400169 * Drops an active reference to superblock, converting it into a temprory
170 * one if there is no other active references left. In that case we
171 * tell fs driver to shut it down and drop the temporary reference we
172 * had just acquired.
173 *
174 * Caller holds exclusive lock on superblock; that lock is released.
Al Viro74dbbdd2009-05-06 01:07:50 -0400175 */
176void deactivate_locked_super(struct super_block *s)
177{
178 struct file_system_type *fs = s->s_type;
Al Virob20bd1a2010-03-22 08:53:19 -0400179 if (atomic_dec_and_test(&s->s_active)) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400180 fs->kill_sb(s);
Boaz Harroshd863b502011-02-10 15:01:20 -0800181 /*
182 * We need to call rcu_barrier so all the delayed rcu free
183 * inodes are flushed before we release the fs module.
184 */
185 rcu_barrier();
Al Viro74dbbdd2009-05-06 01:07:50 -0400186 put_filesystem(fs);
187 put_super(s);
188 } else {
189 up_write(&s->s_umount);
190 }
191}
192
193EXPORT_SYMBOL(deactivate_locked_super);
194
195/**
Al Viro1712ac82010-03-22 15:22:31 -0400196 * deactivate_super - drop an active reference to superblock
197 * @s: superblock to deactivate
198 *
199 * Variant of deactivate_locked_super(), except that superblock is *not*
200 * locked by caller. If we are going to drop the final active reference,
201 * lock will be acquired prior to that.
202 */
203void deactivate_super(struct super_block *s)
204{
205 if (!atomic_add_unless(&s->s_active, -1, 1)) {
206 down_write(&s->s_umount);
207 deactivate_locked_super(s);
208 }
209}
210
211EXPORT_SYMBOL(deactivate_super);
212
213/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * grab_super - acquire an active reference
215 * @s: reference we are trying to make active
216 *
217 * Tries to acquire an active reference. grab_super() is used when we
218 * had just found a superblock in super_blocks or fs_type->fs_supers
219 * and want to turn it into a full-blown active reference. grab_super()
220 * is called with sb_lock held and drops it. Returns 1 in case of
221 * success, 0 if we had failed (superblock contents was already dead or
222 * dying when grab_super() had been called).
223 */
Josh Triplett9c4dbee2006-09-29 01:59:29 -0700224static int grab_super(struct super_block *s) __releases(sb_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Al Virob20bd1a2010-03-22 08:53:19 -0400226 if (atomic_inc_not_zero(&s->s_active)) {
227 spin_unlock(&sb_lock);
Al Virob20bd1a2010-03-22 08:53:19 -0400228 return 1;
229 }
230 /* it's going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 s->s_count++;
232 spin_unlock(&sb_lock);
Al Viro1712ac82010-03-22 15:22:31 -0400233 /* wait for it to die */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 down_write(&s->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 up_write(&s->s_umount);
236 put_super(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return 0;
238}
239
David Howellscf9a2ae2006-08-29 19:05:54 +0100240/*
Al Viro914e2632006-10-18 13:55:46 -0400241 * Superblock locking. We really ought to get rid of these two.
242 */
243void lock_super(struct super_block * sb)
244{
245 get_fs_excl();
246 mutex_lock(&sb->s_lock);
247}
248
249void unlock_super(struct super_block * sb)
250{
251 put_fs_excl();
252 mutex_unlock(&sb->s_lock);
253}
254
255EXPORT_SYMBOL(lock_super);
256EXPORT_SYMBOL(unlock_super);
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/**
259 * generic_shutdown_super - common helper for ->kill_sb()
260 * @sb: superblock to kill
261 *
262 * generic_shutdown_super() does all fs-independent work on superblock
263 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
264 * that need destruction out of superblock, call generic_shutdown_super()
265 * and release aforementioned objects. Note: dentries and inodes _are_
266 * taken care of and do not need specific handling.
David Howellsc636ebd2006-10-11 01:22:19 -0700267 *
268 * Upon calling this function, the filesystem may no longer alter or
269 * rearrange the set of dentries belonging to this super_block, nor may it
270 * change the attachments of dentries to inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 */
272void generic_shutdown_super(struct super_block *sb)
273{
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800274 const struct super_operations *sop = sb->s_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Arjan van de Venefaee192009-01-06 07:20:54 -0800276
David Howellsc636ebd2006-10-11 01:22:19 -0700277 if (sb->s_root) {
278 shrink_dcache_for_umount(sb);
Jan Kara60b06802009-04-27 16:43:53 +0200279 sync_filesystem(sb);
Al Viroa9e220f2009-05-05 22:10:44 -0400280 get_fs_excl();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 sb->s_flags &= ~MS_ACTIVE;
Arjan van de Venefaee192009-01-06 07:20:54 -0800282
Al Viro63997e92010-10-25 20:49:35 -0400283 fsnotify_unmount_inodes(&sb->s_inodes);
284
285 evict_inodes(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (sop->put_super)
288 sop->put_super(sb);
289
Al Viro63997e92010-10-25 20:49:35 -0400290 if (!list_empty(&sb->s_inodes)) {
Dave Jones7b4fe292006-02-07 12:58:48 -0800291 printk("VFS: Busy inodes after unmount of %s. "
292 "Self-destruct in 5 seconds. Have a nice day...\n",
293 sb->s_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
Al Viroa9e220f2009-05-05 22:10:44 -0400295 put_fs_excl();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297 spin_lock(&sb_lock);
298 /* should be initialized for __put_super_and_need_restart() */
Al Viro551de6f2010-03-22 19:36:35 -0400299 list_del_init(&sb->s_instances);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 spin_unlock(&sb_lock);
301 up_write(&sb->s_umount);
302}
303
304EXPORT_SYMBOL(generic_shutdown_super);
305
306/**
307 * sget - find or create a superblock
308 * @type: filesystem type superblock should belong to
309 * @test: comparison callback
310 * @set: setup callback
311 * @data: argument to each of them
312 */
313struct super_block *sget(struct file_system_type *type,
314 int (*test)(struct super_block *,void *),
315 int (*set)(struct super_block *,void *),
316 void *data)
317{
318 struct super_block *s = NULL;
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700319 struct super_block *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int err;
321
322retry:
323 spin_lock(&sb_lock);
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700324 if (test) {
325 list_for_each_entry(old, &type->fs_supers, s_instances) {
326 if (!test(old, data))
327 continue;
328 if (!grab_super(old))
329 goto retry;
Li Zefana3cfbb52009-03-12 14:31:29 -0700330 if (s) {
331 up_write(&s->s_umount);
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700332 destroy_super(s);
Al Viro7a4dec52010-08-09 12:05:43 -0400333 s = NULL;
Li Zefana3cfbb52009-03-12 14:31:29 -0700334 }
Al Virod3f21472010-03-23 11:11:05 -0400335 down_write(&old->s_umount);
Al Viro7a4dec52010-08-09 12:05:43 -0400336 if (unlikely(!(old->s_flags & MS_BORN))) {
337 deactivate_locked_super(old);
338 goto retry;
339 }
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700340 return old;
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 if (!s) {
344 spin_unlock(&sb_lock);
Ingo Molnarcf516242006-07-03 00:25:27 -0700345 s = alloc_super(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (!s)
347 return ERR_PTR(-ENOMEM);
348 goto retry;
349 }
350
351 err = set(s, data);
352 if (err) {
353 spin_unlock(&sb_lock);
Li Zefana3cfbb52009-03-12 14:31:29 -0700354 up_write(&s->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 destroy_super(s);
356 return ERR_PTR(err);
357 }
358 s->s_type = type;
359 strlcpy(s->s_id, type->name, sizeof(s->s_id));
360 list_add_tail(&s->s_list, &super_blocks);
361 list_add(&s->s_instances, &type->fs_supers);
362 spin_unlock(&sb_lock);
363 get_filesystem(type);
364 return s;
365}
366
367EXPORT_SYMBOL(sget);
368
369void drop_super(struct super_block *sb)
370{
371 up_read(&sb->s_umount);
372 put_super(sb);
373}
374
375EXPORT_SYMBOL(drop_super);
376
Christoph Hellwige5004752009-05-05 16:08:56 +0200377/**
378 * sync_supers - helper for periodic superblock writeback
379 *
380 * Call the write_super method if present on all dirty superblocks in
381 * the system. This is for the periodic writeback used by most older
382 * filesystems. For data integrity superblock writeback use
383 * sync_filesystems() instead.
384 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 * Note: check the dirty flag before waiting, so we don't
386 * hold up the sync while mounting a device. (The newly
387 * mounted device won't need syncing.)
388 */
389void sync_supers(void)
390{
Al Virodca33252010-07-25 02:31:46 +0400391 struct super_block *sb, *p = NULL;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400394 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400395 if (list_empty(&sb->s_instances))
396 continue;
Christoph Hellwige5004752009-05-05 16:08:56 +0200397 if (sb->s_op->write_super && sb->s_dirt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 sb->s_count++;
399 spin_unlock(&sb_lock);
Christoph Hellwige5004752009-05-05 16:08:56 +0200400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 down_read(&sb->s_umount);
Christoph Hellwige5004752009-05-05 16:08:56 +0200402 if (sb->s_root && sb->s_dirt)
403 sb->s_op->write_super(sb);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700404 up_read(&sb->s_umount);
Christoph Hellwige5004752009-05-05 16:08:56 +0200405
Kirill Korotaev618f0632005-06-23 00:09:54 -0700406 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400407 if (p)
408 __put_super(p);
409 p = sb;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700410 }
411 }
Al Virodca33252010-07-25 02:31:46 +0400412 if (p)
413 __put_super(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 spin_unlock(&sb_lock);
415}
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/**
Al Viro01a05b32010-03-23 06:06:58 -0400418 * iterate_supers - call function for all active superblocks
419 * @f: function to call
420 * @arg: argument to pass to it
421 *
422 * Scans the superblock list and calls given function, passing it
423 * locked superblock and given argument.
424 */
425void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
426{
Al Virodca33252010-07-25 02:31:46 +0400427 struct super_block *sb, *p = NULL;
Al Viro01a05b32010-03-23 06:06:58 -0400428
429 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400430 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro01a05b32010-03-23 06:06:58 -0400431 if (list_empty(&sb->s_instances))
432 continue;
433 sb->s_count++;
434 spin_unlock(&sb_lock);
435
436 down_read(&sb->s_umount);
437 if (sb->s_root)
438 f(sb, arg);
439 up_read(&sb->s_umount);
440
441 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400442 if (p)
443 __put_super(p);
444 p = sb;
Al Viro01a05b32010-03-23 06:06:58 -0400445 }
Al Virodca33252010-07-25 02:31:46 +0400446 if (p)
447 __put_super(p);
Al Viro01a05b32010-03-23 06:06:58 -0400448 spin_unlock(&sb_lock);
449}
450
451/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * get_super - get the superblock of a device
453 * @bdev: device to get the superblock for
454 *
455 * Scans the superblock list and finds the superblock of the file system
456 * mounted on the device given. %NULL is returned if no match is found.
457 */
458
Al Virodf40c012010-03-22 20:23:25 -0400459struct super_block *get_super(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700461 struct super_block *sb;
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (!bdev)
464 return NULL;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700467rescan:
468 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400469 if (list_empty(&sb->s_instances))
470 continue;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700471 if (sb->s_bdev == bdev) {
472 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700474 down_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400475 /* still alive? */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700476 if (sb->s_root)
477 return sb;
478 up_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400479 /* nope, got unmounted */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700480 spin_lock(&sb_lock);
Al Virodf40c012010-03-22 20:23:25 -0400481 __put_super(sb);
482 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484 }
485 spin_unlock(&sb_lock);
486 return NULL;
487}
488
489EXPORT_SYMBOL(get_super);
Christoph Hellwig45042302009-08-03 23:28:35 +0200490
491/**
492 * get_active_super - get an active reference to the superblock of a device
493 * @bdev: device to get the superblock for
494 *
495 * Scans the superblock list and finds the superblock of the file system
496 * mounted on the device given. Returns the superblock with an active
Al Virod3f21472010-03-23 11:11:05 -0400497 * reference or %NULL if none was found.
Christoph Hellwig45042302009-08-03 23:28:35 +0200498 */
499struct super_block *get_active_super(struct block_device *bdev)
500{
501 struct super_block *sb;
502
503 if (!bdev)
504 return NULL;
505
Al Viro14945832010-03-22 20:15:33 -0400506restart:
Christoph Hellwig45042302009-08-03 23:28:35 +0200507 spin_lock(&sb_lock);
508 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400509 if (list_empty(&sb->s_instances))
510 continue;
Al Viro14945832010-03-22 20:15:33 -0400511 if (sb->s_bdev == bdev) {
512 if (grab_super(sb)) /* drops sb_lock */
513 return sb;
514 else
515 goto restart;
516 }
Christoph Hellwig45042302009-08-03 23:28:35 +0200517 }
518 spin_unlock(&sb_lock);
519 return NULL;
520}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Al Virodf40c012010-03-22 20:23:25 -0400522struct super_block *user_get_super(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700524 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700527rescan:
528 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400529 if (list_empty(&sb->s_instances))
530 continue;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700531 if (sb->s_dev == dev) {
532 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700534 down_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400535 /* still alive? */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700536 if (sb->s_root)
537 return sb;
538 up_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400539 /* nope, got unmounted */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700540 spin_lock(&sb_lock);
Al Virodf40c012010-03-22 20:23:25 -0400541 __put_super(sb);
542 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544 }
545 spin_unlock(&sb_lock);
546 return NULL;
547}
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 * do_remount_sb - asks filesystem to change mount options.
551 * @sb: superblock in question
552 * @flags: numeric part of options
553 * @data: the rest of options
554 * @force: whether or not to force the change
555 *
556 * Alters the mount options of a mounted file system.
557 */
558int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
559{
560 int retval;
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400561 int remount_ro;
Christoph Hellwig45042302009-08-03 23:28:35 +0200562
563 if (sb->s_frozen != SB_UNFROZEN)
564 return -EBUSY;
565
David Howells93614012006-09-30 20:45:40 +0200566#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
568 return -EACCES;
David Howells93614012006-09-30 20:45:40 +0200569#endif
Christoph Hellwig45042302009-08-03 23:28:35 +0200570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (flags & MS_RDONLY)
572 acct_auto_close(sb);
573 shrink_dcache_sb(sb);
Jan Kara60b06802009-04-27 16:43:53 +0200574 sync_filesystem(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Nick Piggind208bbd2009-12-21 16:28:53 -0800576 remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
Nick Piggind208bbd2009-12-21 16:28:53 -0800577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* If we are remounting RDONLY and current sb is read/write,
579 make sure there are no rw files opened */
Nick Piggind208bbd2009-12-21 16:28:53 -0800580 if (remount_ro) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (force)
582 mark_files_ro(sb);
J. R. Okajimab0895512009-06-17 01:16:50 +0900583 else if (!fs_may_remount_ro(sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return -EBUSY;
585 }
586
587 if (sb->s_op->remount_fs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 retval = sb->s_op->remount_fs(sb, &flags, data);
J. R. Okajimab0895512009-06-17 01:16:50 +0900589 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return retval;
591 }
592 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400593
Nick Piggind208bbd2009-12-21 16:28:53 -0800594 /*
595 * Some filesystems modify their metadata via some other path than the
596 * bdev buffer cache (eg. use a private mapping, or directories in
597 * pagecache, etc). Also file data modifications go via their own
598 * mappings. So If we try to mount readonly then copy the filesystem
599 * from bdev, we could get stale data, so invalidate it to give a best
600 * effort at coherency.
601 */
602 if (remount_ro && sb->s_bdev)
603 invalidate_bdev(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return 0;
605}
606
Jens Axboea2a95372009-03-17 09:38:40 +0100607static void do_emergency_remount(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Al Virodca33252010-07-25 02:31:46 +0400609 struct super_block *sb, *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400612 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400613 if (list_empty(&sb->s_instances))
614 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 sb->s_count++;
616 spin_unlock(&sb_lock);
Al Viro443b94b2009-05-05 23:48:50 -0400617 down_write(&sb->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
619 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 * What lock protects sb->s_flags??
621 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 do_remount_sb(sb, MS_RDONLY, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Al Viro443b94b2009-05-05 23:48:50 -0400624 up_write(&sb->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400626 if (p)
627 __put_super(p);
628 p = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Al Virodca33252010-07-25 02:31:46 +0400630 if (p)
631 __put_super(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 spin_unlock(&sb_lock);
Jens Axboea2a95372009-03-17 09:38:40 +0100633 kfree(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 printk("Emergency Remount complete\n");
635}
636
637void emergency_remount(void)
638{
Jens Axboea2a95372009-03-17 09:38:40 +0100639 struct work_struct *work;
640
641 work = kmalloc(sizeof(*work), GFP_ATOMIC);
642 if (work) {
643 INIT_WORK(work, do_emergency_remount);
644 schedule_work(work);
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648/*
649 * Unnamed block devices are dummy devices used by virtual
650 * filesystems which don't use real block-devices. -- jrs
651 */
652
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400653static DEFINE_IDA(unnamed_dev_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
Al Viroc63e09e2009-06-24 02:05:18 -0400655static int unnamed_dev_start = 0; /* don't bother trying below it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657int set_anon_super(struct super_block *s, void *data)
658{
659 int dev;
660 int error;
661
662 retry:
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400663 if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return -ENOMEM;
665 spin_lock(&unnamed_dev_lock);
Al Viroc63e09e2009-06-24 02:05:18 -0400666 error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
Al Virof21f6222009-06-24 03:12:00 -0400667 if (!error)
668 unnamed_dev_start = dev + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 spin_unlock(&unnamed_dev_lock);
670 if (error == -EAGAIN)
671 /* We raced and lost with another CPU. */
672 goto retry;
673 else if (error)
674 return -EAGAIN;
675
676 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
677 spin_lock(&unnamed_dev_lock);
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400678 ida_remove(&unnamed_dev_ida, dev);
Al Virof21f6222009-06-24 03:12:00 -0400679 if (unnamed_dev_start > dev)
680 unnamed_dev_start = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 spin_unlock(&unnamed_dev_lock);
682 return -EMFILE;
683 }
684 s->s_dev = MKDEV(0, dev & MINORMASK);
Jörn Engel5129a462010-04-25 08:54:42 +0200685 s->s_bdi = &noop_backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return 0;
687}
688
689EXPORT_SYMBOL(set_anon_super);
690
691void kill_anon_super(struct super_block *sb)
692{
693 int slot = MINOR(sb->s_dev);
694
695 generic_shutdown_super(sb);
696 spin_lock(&unnamed_dev_lock);
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400697 ida_remove(&unnamed_dev_ida, slot);
Al Viroc63e09e2009-06-24 02:05:18 -0400698 if (slot < unnamed_dev_start)
699 unnamed_dev_start = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 spin_unlock(&unnamed_dev_lock);
701}
702
703EXPORT_SYMBOL(kill_anon_super);
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705void kill_litter_super(struct super_block *sb)
706{
707 if (sb->s_root)
708 d_genocide(sb->s_root);
709 kill_anon_super(sb);
710}
711
712EXPORT_SYMBOL(kill_litter_super);
713
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700714static int ns_test_super(struct super_block *sb, void *data)
715{
716 return sb->s_fs_info == data;
717}
718
719static int ns_set_super(struct super_block *sb, void *data)
720{
721 sb->s_fs_info = data;
722 return set_anon_super(sb, NULL);
723}
724
Al Viroceefda62010-07-26 13:16:50 +0400725struct dentry *mount_ns(struct file_system_type *fs_type, int flags,
726 void *data, int (*fill_super)(struct super_block *, void *, int))
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700727{
728 struct super_block *sb;
729
730 sb = sget(fs_type, ns_test_super, ns_set_super, data);
731 if (IS_ERR(sb))
Al Viroceefda62010-07-26 13:16:50 +0400732 return ERR_CAST(sb);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700733
734 if (!sb->s_root) {
735 int err;
736 sb->s_flags = flags;
737 err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
738 if (err) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400739 deactivate_locked_super(sb);
Al Viroceefda62010-07-26 13:16:50 +0400740 return ERR_PTR(err);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700741 }
742
743 sb->s_flags |= MS_ACTIVE;
744 }
745
Al Viroceefda62010-07-26 13:16:50 +0400746 return dget(sb->s_root);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700747}
748
Al Viroceefda62010-07-26 13:16:50 +0400749EXPORT_SYMBOL(mount_ns);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700750
David Howells93614012006-09-30 20:45:40 +0200751#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752static int set_bdev_super(struct super_block *s, void *data)
753{
754 s->s_bdev = data;
755 s->s_dev = s->s_bdev->bd_dev;
Jens Axboe32a88aa2009-09-16 15:02:33 +0200756
757 /*
758 * We set the bdi here to the queue backing, file systems can
759 * overwrite this in ->fill_super()
760 */
761 s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return 0;
763}
764
765static int test_bdev_super(struct super_block *s, void *data)
766{
767 return (void *)s->s_bdev == data;
768}
769
Al Viro152a0832010-07-25 00:46:55 +0400770struct dentry *mount_bdev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 int flags, const char *dev_name, void *data,
Al Viro152a0832010-07-25 00:46:55 +0400772 int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 struct block_device *bdev;
775 struct super_block *s;
Tejun Heod4d77622010-11-13 11:55:18 +0100776 fmode_t mode = FMODE_READ | FMODE_EXCL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int error = 0;
778
Al Viro30c40d22008-02-22 19:50:45 -0500779 if (!(flags & MS_RDONLY))
780 mode |= FMODE_WRITE;
781
Tejun Heod4d77622010-11-13 11:55:18 +0100782 bdev = blkdev_get_by_path(dev_name, mode, fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (IS_ERR(bdev))
Al Viro152a0832010-07-25 00:46:55 +0400784 return ERR_CAST(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 /*
787 * once the super is inserted into the list by sget, s_umount
788 * will protect the lockfs code from trying to start a snapshot
789 * while we are mounting
790 */
Christoph Hellwig4fadd7b2009-08-03 23:28:06 +0200791 mutex_lock(&bdev->bd_fsfreeze_mutex);
792 if (bdev->bd_fsfreeze_count > 0) {
793 mutex_unlock(&bdev->bd_fsfreeze_mutex);
794 error = -EBUSY;
795 goto error_bdev;
796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
Christoph Hellwig4fadd7b2009-08-03 23:28:06 +0200798 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700800 goto error_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 if (s->s_root) {
803 if ((flags ^ s->s_flags) & MS_RDONLY) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400804 deactivate_locked_super(s);
David Howells454e2392006-06-23 02:02:57 -0700805 error = -EBUSY;
806 goto error_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
David Howells454e2392006-06-23 02:02:57 -0700808
Tejun Heo4f331f02010-07-20 15:18:07 -0700809 /*
810 * s_umount nests inside bd_mutex during
Tejun Heoe525fd82010-11-13 11:55:17 +0100811 * __invalidate_device(). blkdev_put() acquires
812 * bd_mutex and can't be called under s_umount. Drop
813 * s_umount temporarily. This is safe as we're
814 * holding an active reference.
Tejun Heo4f331f02010-07-20 15:18:07 -0700815 */
816 up_write(&s->s_umount);
Tejun Heod4d77622010-11-13 11:55:18 +0100817 blkdev_put(bdev, mode);
Tejun Heo4f331f02010-07-20 15:18:07 -0700818 down_write(&s->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 } else {
820 char b[BDEVNAME_SIZE];
821
822 s->s_flags = flags;
Al Viro30c40d22008-02-22 19:50:45 -0500823 s->s_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
Pekka Enberge78c9a02006-01-08 01:03:39 -0800825 sb_set_blocksize(s, block_size(bdev));
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800826 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (error) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400828 deactivate_locked_super(s);
David Howells454e2392006-06-23 02:02:57 -0700829 goto error;
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800830 }
David Howells454e2392006-06-23 02:02:57 -0700831
832 s->s_flags |= MS_ACTIVE;
Theodore Ts'o87d8fe12009-01-03 09:47:09 -0500833 bdev->bd_super = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
Al Viro152a0832010-07-25 00:46:55 +0400836 return dget(s->s_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
David Howells454e2392006-06-23 02:02:57 -0700838error_s:
839 error = PTR_ERR(s);
840error_bdev:
Tejun Heod4d77622010-11-13 11:55:18 +0100841 blkdev_put(bdev, mode);
David Howells454e2392006-06-23 02:02:57 -0700842error:
Al Viro152a0832010-07-25 00:46:55 +0400843 return ERR_PTR(error);
844}
845EXPORT_SYMBOL(mount_bdev);
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847void kill_block_super(struct super_block *sb)
848{
849 struct block_device *bdev = sb->s_bdev;
Al Viro30c40d22008-02-22 19:50:45 -0500850 fmode_t mode = sb->s_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
H Hartley Sweetenddbaaf32009-04-29 20:14:57 -0400852 bdev->bd_super = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 generic_shutdown_super(sb);
854 sync_blockdev(bdev);
Tejun Heod4d77622010-11-13 11:55:18 +0100855 WARN_ON_ONCE(!(mode & FMODE_EXCL));
Tejun Heoe525fd82010-11-13 11:55:17 +0100856 blkdev_put(bdev, mode | FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859EXPORT_SYMBOL(kill_block_super);
David Howells93614012006-09-30 20:45:40 +0200860#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Al Viro3c26ff62010-07-25 11:46:36 +0400862struct dentry *mount_nodev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 int flags, void *data,
Al Viro3c26ff62010-07-25 11:46:36 +0400864 int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 int error;
867 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
868
869 if (IS_ERR(s))
Al Viro3c26ff62010-07-25 11:46:36 +0400870 return ERR_CAST(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 s->s_flags = flags;
873
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800874 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (error) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400876 deactivate_locked_super(s);
Al Viro3c26ff62010-07-25 11:46:36 +0400877 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879 s->s_flags |= MS_ACTIVE;
Al Viro3c26ff62010-07-25 11:46:36 +0400880 return dget(s->s_root);
881}
882EXPORT_SYMBOL(mount_nodev);
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884static int compare_single(struct super_block *s, void *p)
885{
886 return 1;
887}
888
Al Virofc14f2f2010-07-25 01:48:30 +0400889struct dentry *mount_single(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 int flags, void *data,
Al Virofc14f2f2010-07-25 01:48:30 +0400891 int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 struct super_block *s;
894 int error;
895
896 s = sget(fs_type, compare_single, set_anon_super, NULL);
897 if (IS_ERR(s))
Al Virofc14f2f2010-07-25 01:48:30 +0400898 return ERR_CAST(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (!s->s_root) {
900 s->s_flags = flags;
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800901 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (error) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400903 deactivate_locked_super(s);
Al Virofc14f2f2010-07-25 01:48:30 +0400904 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906 s->s_flags |= MS_ACTIVE;
Kay Sievers9329d1b2009-12-18 21:18:15 +0100907 } else {
908 do_remount_sb(s, flags, data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
Al Virofc14f2f2010-07-25 01:48:30 +0400910 return dget(s->s_root);
911}
912EXPORT_SYMBOL(mount_single);
913
Al Viro9d412a42011-03-17 22:08:28 -0400914struct dentry *
915mount_fs(struct file_system_type *type, int flags, const char *name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Al Viroc96e41e2010-07-25 00:17:56 +0400917 struct dentry *root;
Al Viro9d412a42011-03-17 22:08:28 -0400918 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 char *secdata = NULL;
Al Viro9d412a42011-03-17 22:08:28 -0400920 int error = -ENOMEM;
Al Viro80893522010-02-05 09:30:46 -0500921
Eric Parise0007522008-03-05 10:31:54 -0500922 if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 secdata = alloc_secdata();
David Howells454e2392006-06-23 02:02:57 -0700924 if (!secdata)
Al Viro9d412a42011-03-17 22:08:28 -0400925 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Eric Parise0007522008-03-05 10:31:54 -0500927 error = security_sb_copy_data(data, secdata);
David Howells454e2392006-06-23 02:02:57 -0700928 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 goto out_free_secdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931
Al Viro1a102ff2011-03-16 09:07:58 -0400932 root = type->mount(type, flags, name, data);
933 if (IS_ERR(root)) {
934 error = PTR_ERR(root);
935 goto out_free_secdata;
Al Viroc96e41e2010-07-25 00:17:56 +0400936 }
Al Viro9d412a42011-03-17 22:08:28 -0400937 sb = root->d_sb;
938 BUG_ON(!sb);
939 WARN_ON(!sb->s_bdi);
Linus Torvalds6c510382011-03-24 10:16:26 -0700940 WARN_ON(sb->s_bdi == &default_backing_dev_info);
Al Viro9d412a42011-03-17 22:08:28 -0400941 sb->s_flags |= MS_BORN;
David Howells454e2392006-06-23 02:02:57 -0700942
Al Viro9d412a42011-03-17 22:08:28 -0400943 error = security_sb_kern_mount(sb, flags, secdata);
Jörn Engel5129a462010-04-25 08:54:42 +0200944 if (error)
945 goto out_sb;
David Howells454e2392006-06-23 02:02:57 -0700946
Jeff Layton42cb56a2009-09-18 13:05:53 -0700947 /*
948 * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
949 * but s_maxbytes was an unsigned long long for many releases. Throw
950 * this warning for a little while to try and catch filesystems that
951 * violate this rule. This warning should be either removed or
952 * converted to a BUG() in 2.6.34.
953 */
Al Viro9d412a42011-03-17 22:08:28 -0400954 WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
955 "negative value (%lld)\n", type->name, sb->s_maxbytes);
Jeff Layton42cb56a2009-09-18 13:05:53 -0700956
Al Viro9d412a42011-03-17 22:08:28 -0400957 up_write(&sb->s_umount);
Gerald Schaefer8680e222005-06-21 17:15:16 -0700958 free_secdata(secdata);
Al Viro9d412a42011-03-17 22:08:28 -0400959 return root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960out_sb:
Al Viro9d412a42011-03-17 22:08:28 -0400961 dput(root);
962 deactivate_locked_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963out_free_secdata:
964 free_secdata(secdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965out:
David Howells454e2392006-06-23 02:02:57 -0700966 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
Josef Bacik18e9e512010-03-23 10:34:56 -0400969/**
Randy Dunlap7000d3c2010-05-24 22:22:34 -0700970 * freeze_super - lock the filesystem and force it into a consistent state
971 * @sb: the super to lock
Josef Bacik18e9e512010-03-23 10:34:56 -0400972 *
973 * Syncs the super to make sure the filesystem is consistent and calls the fs's
974 * freeze_fs. Subsequent calls to this without first thawing the fs will return
975 * -EBUSY.
976 */
977int freeze_super(struct super_block *sb)
978{
979 int ret;
980
981 atomic_inc(&sb->s_active);
982 down_write(&sb->s_umount);
983 if (sb->s_frozen) {
984 deactivate_locked_super(sb);
985 return -EBUSY;
986 }
987
988 if (sb->s_flags & MS_RDONLY) {
989 sb->s_frozen = SB_FREEZE_TRANS;
990 smp_wmb();
991 up_write(&sb->s_umount);
992 return 0;
993 }
994
995 sb->s_frozen = SB_FREEZE_WRITE;
996 smp_wmb();
997
998 sync_filesystem(sb);
999
1000 sb->s_frozen = SB_FREEZE_TRANS;
1001 smp_wmb();
1002
1003 sync_blockdev(sb->s_bdev);
1004 if (sb->s_op->freeze_fs) {
1005 ret = sb->s_op->freeze_fs(sb);
1006 if (ret) {
1007 printk(KERN_ERR
1008 "VFS:Filesystem freeze failed\n");
1009 sb->s_frozen = SB_UNFROZEN;
1010 deactivate_locked_super(sb);
1011 return ret;
1012 }
1013 }
1014 up_write(&sb->s_umount);
1015 return 0;
1016}
1017EXPORT_SYMBOL(freeze_super);
1018
1019/**
1020 * thaw_super -- unlock filesystem
1021 * @sb: the super to thaw
1022 *
1023 * Unlocks the filesystem and marks it writeable again after freeze_super().
1024 */
1025int thaw_super(struct super_block *sb)
1026{
1027 int error;
1028
1029 down_write(&sb->s_umount);
1030 if (sb->s_frozen == SB_UNFROZEN) {
1031 up_write(&sb->s_umount);
1032 return -EINVAL;
1033 }
1034
1035 if (sb->s_flags & MS_RDONLY)
1036 goto out;
1037
1038 if (sb->s_op->unfreeze_fs) {
1039 error = sb->s_op->unfreeze_fs(sb);
1040 if (error) {
1041 printk(KERN_ERR
1042 "VFS:Filesystem thaw failed\n");
1043 sb->s_frozen = SB_FREEZE_TRANS;
1044 up_write(&sb->s_umount);
1045 return error;
1046 }
1047 }
1048
1049out:
1050 sb->s_frozen = SB_UNFROZEN;
1051 smp_wmb();
1052 wake_up(&sb->s_wait_unfrozen);
1053 deactivate_locked_super(sb);
1054
1055 return 0;
1056}
1057EXPORT_SYMBOL(thaw_super);