blob: 0d89e93f654e1310572dfd714912e48f918c724e [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
847int get_sb_bdev(struct file_system_type *fs_type,
848 int flags, const char *dev_name, void *data,
849 int (*fill_super)(struct super_block *, void *, int),
850 struct vfsmount *mnt)
851{
852 struct dentry *root;
853
854 root = mount_bdev(fs_type, flags, dev_name, data, fill_super);
855 if (IS_ERR(root))
856 return PTR_ERR(root);
857 mnt->mnt_root = root;
858 mnt->mnt_sb = root->d_sb;
859 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
862EXPORT_SYMBOL(get_sb_bdev);
863
864void kill_block_super(struct super_block *sb)
865{
866 struct block_device *bdev = sb->s_bdev;
Al Viro30c40d22008-02-22 19:50:45 -0500867 fmode_t mode = sb->s_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
H Hartley Sweetenddbaaf32009-04-29 20:14:57 -0400869 bdev->bd_super = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 generic_shutdown_super(sb);
871 sync_blockdev(bdev);
Tejun Heod4d77622010-11-13 11:55:18 +0100872 WARN_ON_ONCE(!(mode & FMODE_EXCL));
Tejun Heoe525fd82010-11-13 11:55:17 +0100873 blkdev_put(bdev, mode | FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
876EXPORT_SYMBOL(kill_block_super);
David Howells93614012006-09-30 20:45:40 +0200877#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Al Viro3c26ff62010-07-25 11:46:36 +0400879struct dentry *mount_nodev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 int flags, void *data,
Al Viro3c26ff62010-07-25 11:46:36 +0400881 int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 int error;
884 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
885
886 if (IS_ERR(s))
Al Viro3c26ff62010-07-25 11:46:36 +0400887 return ERR_CAST(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 s->s_flags = flags;
890
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800891 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (error) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400893 deactivate_locked_super(s);
Al Viro3c26ff62010-07-25 11:46:36 +0400894 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 s->s_flags |= MS_ACTIVE;
Al Viro3c26ff62010-07-25 11:46:36 +0400897 return dget(s->s_root);
898}
899EXPORT_SYMBOL(mount_nodev);
900
901int get_sb_nodev(struct file_system_type *fs_type,
902 int flags, void *data,
903 int (*fill_super)(struct super_block *, void *, int),
904 struct vfsmount *mnt)
905{
906 struct dentry *root;
907
908 root = mount_nodev(fs_type, flags, data, fill_super);
909 if (IS_ERR(root))
910 return PTR_ERR(root);
911 mnt->mnt_root = root;
912 mnt->mnt_sb = root->d_sb;
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -0800913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915EXPORT_SYMBOL(get_sb_nodev);
916
917static int compare_single(struct super_block *s, void *p)
918{
919 return 1;
920}
921
Al Virofc14f2f2010-07-25 01:48:30 +0400922struct dentry *mount_single(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 int flags, void *data,
Al Virofc14f2f2010-07-25 01:48:30 +0400924 int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 struct super_block *s;
927 int error;
928
929 s = sget(fs_type, compare_single, set_anon_super, NULL);
930 if (IS_ERR(s))
Al Virofc14f2f2010-07-25 01:48:30 +0400931 return ERR_CAST(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (!s->s_root) {
933 s->s_flags = flags;
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800934 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (error) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400936 deactivate_locked_super(s);
Al Virofc14f2f2010-07-25 01:48:30 +0400937 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939 s->s_flags |= MS_ACTIVE;
Kay Sievers9329d1b2009-12-18 21:18:15 +0100940 } else {
941 do_remount_sb(s, flags, data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Al Virofc14f2f2010-07-25 01:48:30 +0400943 return dget(s->s_root);
944}
945EXPORT_SYMBOL(mount_single);
946
947int get_sb_single(struct file_system_type *fs_type,
948 int flags, void *data,
949 int (*fill_super)(struct super_block *, void *, int),
950 struct vfsmount *mnt)
951{
952 struct dentry *root;
953 root = mount_single(fs_type, flags, data, fill_super);
954 if (IS_ERR(root))
955 return PTR_ERR(root);
956 mnt->mnt_root = root;
957 mnt->mnt_sb = root->d_sb;
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -0800958 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
961EXPORT_SYMBOL(get_sb_single);
962
963struct vfsmount *
Trond Myklebustbb4a58b2006-06-09 09:34:15 -0400964vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 struct vfsmount *mnt;
Al Viroc96e41e2010-07-25 00:17:56 +0400967 struct dentry *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 char *secdata = NULL;
David Howells454e2392006-06-23 02:02:57 -0700969 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 if (!type)
972 return ERR_PTR(-ENODEV);
973
David Howells454e2392006-06-23 02:02:57 -0700974 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 mnt = alloc_vfsmnt(name);
976 if (!mnt)
977 goto out;
978
Al Viro80893522010-02-05 09:30:46 -0500979 if (flags & MS_KERNMOUNT)
980 mnt->mnt_flags = MNT_INTERNAL;
981
Eric Parise0007522008-03-05 10:31:54 -0500982 if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 secdata = alloc_secdata();
David Howells454e2392006-06-23 02:02:57 -0700984 if (!secdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 goto out_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Eric Parise0007522008-03-05 10:31:54 -0500987 error = security_sb_copy_data(data, secdata);
David Howells454e2392006-06-23 02:02:57 -0700988 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 goto out_free_secdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991
Al Viroc96e41e2010-07-25 00:17:56 +0400992 if (type->mount) {
993 root = type->mount(type, flags, name, data);
994 if (IS_ERR(root)) {
995 error = PTR_ERR(root);
996 goto out_free_secdata;
997 }
998 mnt->mnt_root = root;
999 mnt->mnt_sb = root->d_sb;
1000 } else {
1001 error = type->get_sb(type, flags, name, data, mnt);
1002 if (error < 0)
1003 goto out_free_secdata;
1004 }
Lee Schermerhornb4c07bc2007-07-15 23:40:54 -07001005 BUG_ON(!mnt->mnt_sb);
Jörn Engel5129a462010-04-25 08:54:42 +02001006 WARN_ON(!mnt->mnt_sb->s_bdi);
Jens Axboe95f28602011-03-17 11:13:12 +01001007 WARN_ON(mnt->mnt_sb->s_bdi == &default_backing_dev_info);
Al Viro7a4dec52010-08-09 12:05:43 -04001008 mnt->mnt_sb->s_flags |= MS_BORN;
David Howells454e2392006-06-23 02:02:57 -07001009
Jörn Engel5129a462010-04-25 08:54:42 +02001010 error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
1011 if (error)
1012 goto out_sb;
David Howells454e2392006-06-23 02:02:57 -07001013
Jeff Layton42cb56a2009-09-18 13:05:53 -07001014 /*
1015 * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
1016 * but s_maxbytes was an unsigned long long for many releases. Throw
1017 * this warning for a little while to try and catch filesystems that
1018 * violate this rule. This warning should be either removed or
1019 * converted to a BUG() in 2.6.34.
1020 */
1021 WARN((mnt->mnt_sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
1022 "negative value (%lld)\n", type->name, mnt->mnt_sb->s_maxbytes);
1023
David Howells454e2392006-06-23 02:02:57 -07001024 mnt->mnt_mountpoint = mnt->mnt_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 mnt->mnt_parent = mnt;
David Howells454e2392006-06-23 02:02:57 -07001026 up_write(&mnt->mnt_sb->s_umount);
Gerald Schaefer8680e222005-06-21 17:15:16 -07001027 free_secdata(secdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return mnt;
1029out_sb:
David Howells454e2392006-06-23 02:02:57 -07001030 dput(mnt->mnt_root);
Al Viro74dbbdd2009-05-06 01:07:50 -04001031 deactivate_locked_super(mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032out_free_secdata:
1033 free_secdata(secdata);
1034out_mnt:
1035 free_vfsmnt(mnt);
1036out:
David Howells454e2392006-06-23 02:02:57 -07001037 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
Trond Myklebustbb4a58b2006-06-09 09:34:15 -04001040EXPORT_SYMBOL_GPL(vfs_kern_mount);
1041
Josef Bacik18e9e512010-03-23 10:34:56 -04001042/**
Randy Dunlap7000d3c2010-05-24 22:22:34 -07001043 * freeze_super - lock the filesystem and force it into a consistent state
1044 * @sb: the super to lock
Josef Bacik18e9e512010-03-23 10:34:56 -04001045 *
1046 * Syncs the super to make sure the filesystem is consistent and calls the fs's
1047 * freeze_fs. Subsequent calls to this without first thawing the fs will return
1048 * -EBUSY.
1049 */
1050int freeze_super(struct super_block *sb)
1051{
1052 int ret;
1053
1054 atomic_inc(&sb->s_active);
1055 down_write(&sb->s_umount);
1056 if (sb->s_frozen) {
1057 deactivate_locked_super(sb);
1058 return -EBUSY;
1059 }
1060
1061 if (sb->s_flags & MS_RDONLY) {
1062 sb->s_frozen = SB_FREEZE_TRANS;
1063 smp_wmb();
1064 up_write(&sb->s_umount);
1065 return 0;
1066 }
1067
1068 sb->s_frozen = SB_FREEZE_WRITE;
1069 smp_wmb();
1070
1071 sync_filesystem(sb);
1072
1073 sb->s_frozen = SB_FREEZE_TRANS;
1074 smp_wmb();
1075
1076 sync_blockdev(sb->s_bdev);
1077 if (sb->s_op->freeze_fs) {
1078 ret = sb->s_op->freeze_fs(sb);
1079 if (ret) {
1080 printk(KERN_ERR
1081 "VFS:Filesystem freeze failed\n");
1082 sb->s_frozen = SB_UNFROZEN;
1083 deactivate_locked_super(sb);
1084 return ret;
1085 }
1086 }
1087 up_write(&sb->s_umount);
1088 return 0;
1089}
1090EXPORT_SYMBOL(freeze_super);
1091
1092/**
1093 * thaw_super -- unlock filesystem
1094 * @sb: the super to thaw
1095 *
1096 * Unlocks the filesystem and marks it writeable again after freeze_super().
1097 */
1098int thaw_super(struct super_block *sb)
1099{
1100 int error;
1101
1102 down_write(&sb->s_umount);
1103 if (sb->s_frozen == SB_UNFROZEN) {
1104 up_write(&sb->s_umount);
1105 return -EINVAL;
1106 }
1107
1108 if (sb->s_flags & MS_RDONLY)
1109 goto out;
1110
1111 if (sb->s_op->unfreeze_fs) {
1112 error = sb->s_op->unfreeze_fs(sb);
1113 if (error) {
1114 printk(KERN_ERR
1115 "VFS:Filesystem thaw failed\n");
1116 sb->s_frozen = SB_FREEZE_TRANS;
1117 up_write(&sb->s_umount);
1118 return error;
1119 }
1120 }
1121
1122out:
1123 sb->s_frozen = SB_UNFROZEN;
1124 smp_wmb();
1125 wake_up(&sb->s_wait_unfrozen);
1126 deactivate_locked_super(sb);
1127
1128 return 0;
1129}
1130EXPORT_SYMBOL(thaw_super);
1131
Miklos Szeredi79c0b2d2007-05-08 00:25:43 -07001132static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
1133{
1134 int err;
1135 const char *subtype = strchr(fstype, '.');
1136 if (subtype) {
1137 subtype++;
1138 err = -EINVAL;
1139 if (!subtype[0])
1140 goto err;
1141 } else
1142 subtype = "";
1143
1144 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
1145 err = -ENOMEM;
1146 if (!mnt->mnt_sb->s_subtype)
1147 goto err;
1148 return mnt;
1149
1150 err:
Al Virof03c6592011-01-14 22:30:21 -05001151 mntput(mnt);
Miklos Szeredi79c0b2d2007-05-08 00:25:43 -07001152 return ERR_PTR(err);
1153}
1154
Trond Myklebustbb4a58b2006-06-09 09:34:15 -04001155struct vfsmount *
1156do_kern_mount(const char *fstype, int flags, const char *name, void *data)
1157{
1158 struct file_system_type *type = get_fs_type(fstype);
1159 struct vfsmount *mnt;
1160 if (!type)
1161 return ERR_PTR(-ENODEV);
1162 mnt = vfs_kern_mount(type, flags, name, data);
Miklos Szeredi79c0b2d2007-05-08 00:25:43 -07001163 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
1164 !mnt->mnt_sb->s_subtype)
1165 mnt = fs_set_subtype(mnt, fstype);
Trond Myklebustbb4a58b2006-06-09 09:34:15 -04001166 put_filesystem(type);
1167 return mnt;
1168}
Al Viro8a4e98d2008-02-24 01:43:03 -05001169EXPORT_SYMBOL_GPL(do_kern_mount);
Trond Myklebustbb4a58b2006-06-09 09:34:15 -04001170
Pavel Emelyanov8bf97252007-10-18 23:40:02 -07001171struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Pavel Emelyanov8bf97252007-10-18 23:40:02 -07001173 return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Pavel Emelyanov8bf97252007-10-18 23:40:02 -07001176EXPORT_SYMBOL_GPL(kern_mount_data);