blob: e8e6dbfefe8c4bdaa8aa69b7b5b4141a896c23b4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/super.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * super.c contains code to handle: - mount structures
7 * - super-block tables
8 * - filesystem drivers list
9 * - mount system call
10 * - umount system call
11 * - ustat system call
12 *
13 * GK 2/5/95 - Changed to support mounting the root fs via NFS
14 *
15 * Added kerneld support: Jacques Gelinas and Bjorn Ekwall
16 * Added change_root: Werner Almesberger & Hans Lermen, Feb '96
17 * Added options to /proc/mounts:
Jan Engelhardt96de0e22007-10-19 23:21:04 +020018 * Torbjörn Lindh (torbjorn.lindh@gopta.se), April 14, 1996.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Added devfs support: Richard Gooch <rgooch@atnf.csiro.au>, 13-JAN-1998
20 * Heavily rewritten for 'one fs - one tree' dcache architecture. AV, Mar 2000
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/acct.h>
26#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/mount.h>
28#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/writeback.h> /* for the emergency remount stuff */
30#include <linux/idr.h>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080031#include <linux/mutex.h>
Jens Axboe5477d0f2010-04-29 20:33:35 +020032#include <linux/backing-dev.h>
Nick Pigginceb5bdc2011-01-07 17:50:05 +110033#include <linux/rculist_bl.h>
Dan Magenheimerc515e1f2011-05-26 10:01:43 -060034#include <linux/cleancache.h>
Al Viro6d59e7f2008-03-22 15:48:17 -040035#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038LIST_HEAD(super_blocks);
39DEFINE_SPINLOCK(sb_lock);
40
41/**
42 * alloc_super - create new superblock
Henrik Kretzschmarfe2bbc42006-09-06 00:03:41 -070043 * @type: filesystem type superblock should belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 *
45 * Allocates and initializes a new &struct super_block. alloc_super()
46 * returns a pointer new superblock or %NULL if allocation had failed.
47 */
Ingo Molnarcf516242006-07-03 00:25:27 -070048static struct super_block *alloc_super(struct file_system_type *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Oliver Neukum11b0b5a2006-03-25 03:08:13 -080050 struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
Alexey Dobriyanb87221d2009-09-21 17:01:09 -070051 static const struct super_operations default_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 if (s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (security_sb_alloc(s)) {
55 kfree(s);
56 s = NULL;
57 goto out;
58 }
Nick Piggin6416ccb2010-08-18 04:37:38 +100059#ifdef CONFIG_SMP
60 s->s_files = alloc_percpu(struct list_head);
61 if (!s->s_files) {
62 security_sb_free(s);
63 kfree(s);
64 s = NULL;
65 goto out;
66 } else {
67 int i;
68
69 for_each_possible_cpu(i)
70 INIT_LIST_HEAD(per_cpu_ptr(s->s_files, i));
71 }
72#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 INIT_LIST_HEAD(&s->s_files);
Nick Piggin6416ccb2010-08-18 04:37:38 +100074#endif
Jens Axboe95f28602011-03-17 11:13:12 +010075 s->s_bdi = &default_backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 INIT_LIST_HEAD(&s->s_instances);
Nick Pigginceb5bdc2011-01-07 17:50:05 +110077 INIT_HLIST_BL_HEAD(&s->s_anon);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 INIT_LIST_HEAD(&s->s_inodes);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -070079 INIT_LIST_HEAD(&s->s_dentry_lru);
Dave Chinner98b745c2011-07-08 14:14:39 +100080 INIT_LIST_HEAD(&s->s_inode_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 init_rwsem(&s->s_umount);
Ingo Molnar7892f2f2006-01-09 15:59:25 -080082 mutex_init(&s->s_lock);
Arjan van de Ven897c6ff2006-07-03 00:25:28 -070083 lockdep_set_class(&s->s_umount, &type->s_umount_key);
Ingo Molnarcf516242006-07-03 00:25:27 -070084 /*
85 * The locking rules for s_lock are up to the
86 * filesystem. For example ext3fs has different
87 * lock ordering than usbfs:
88 */
89 lockdep_set_class(&s->s_lock, &type->s_lock_key);
Peter Zijlstraada723d2009-02-18 14:48:30 -080090 /*
91 * sget() can have s_umount recursion.
92 *
93 * When it cannot find a suitable sb, it allocates a new
94 * one (this one), and tries again to find a suitable old
95 * one.
96 *
97 * In case that succeeds, it will acquire the s_umount
98 * lock of the old one. Since these are clearly distrinct
99 * locks, and this object isn't exposed yet, there's no
100 * risk of deadlocks.
101 *
102 * Annotate this by putting this lock in a different
103 * subclass.
104 */
105 down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
Al Virob20bd1a2010-03-22 08:53:19 -0400106 s->s_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 atomic_set(&s->s_active, 1);
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800108 mutex_init(&s->s_vfs_rename_mutex);
Roland Dreier51ee0492010-04-27 14:23:57 -0700109 lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
Ingo Molnard3be9152006-03-23 03:00:29 -0800110 mutex_init(&s->s_dquot.dqio_mutex);
111 mutex_init(&s->s_dquot.dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 init_rwsem(&s->s_dquot.dqptr_sem);
113 init_waitqueue_head(&s->s_wait_unfrozen);
114 s->s_maxbytes = MAX_NON_LFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 s->s_op = &default_op;
116 s->s_time_gran = 1000000000;
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600117 s->cleancache_poolid = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119out:
120 return s;
121}
122
123/**
124 * destroy_super - frees a superblock
125 * @s: superblock to free
126 *
127 * Frees a superblock.
128 */
129static inline void destroy_super(struct super_block *s)
130{
Nick Piggin6416ccb2010-08-18 04:37:38 +1000131#ifdef CONFIG_SMP
132 free_percpu(s->s_files);
133#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 security_sb_free(s);
Miklos Szeredi79c0b2d2007-05-08 00:25:43 -0700135 kfree(s->s_subtype);
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800136 kfree(s->s_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 kfree(s);
138}
139
140/* Superblock refcounting */
141
142/*
Al Viro35cf7ba2010-03-22 21:13:53 -0400143 * Drop a superblock's refcount. The caller must hold sb_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 */
Al Viro35cf7ba2010-03-22 21:13:53 -0400145void __put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (!--sb->s_count) {
Al Viro551de6f2010-03-22 19:36:35 -0400148 list_del_init(&sb->s_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 destroy_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153/**
154 * put_super - drop a temporary reference to superblock
155 * @sb: superblock in question
156 *
157 * Drops a temporary reference, frees superblock if there's no
158 * references left.
159 */
Jens Axboe03ba3782009-09-09 09:08:54 +0200160void put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 spin_lock(&sb_lock);
163 __put_super(sb);
164 spin_unlock(&sb_lock);
165}
166
167
168/**
Al Viro74dbbdd2009-05-06 01:07:50 -0400169 * deactivate_locked_super - drop an active reference to superblock
170 * @s: superblock to deactivate
171 *
Al Viro1712ac82010-03-22 15:22:31 -0400172 * Drops an active reference to superblock, converting it into a temprory
173 * one if there is no other active references left. In that case we
174 * tell fs driver to shut it down and drop the temporary reference we
175 * had just acquired.
176 *
177 * Caller holds exclusive lock on superblock; that lock is released.
Al Viro74dbbdd2009-05-06 01:07:50 -0400178 */
179void deactivate_locked_super(struct super_block *s)
180{
181 struct file_system_type *fs = s->s_type;
Al Virob20bd1a2010-03-22 08:53:19 -0400182 if (atomic_dec_and_test(&s->s_active)) {
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600183 cleancache_flush_fs(s);
Al Viro74dbbdd2009-05-06 01:07:50 -0400184 fs->kill_sb(s);
Boaz Harroshd863b502011-02-10 15:01:20 -0800185 /*
186 * We need to call rcu_barrier so all the delayed rcu free
187 * inodes are flushed before we release the fs module.
188 */
189 rcu_barrier();
Al Viro74dbbdd2009-05-06 01:07:50 -0400190 put_filesystem(fs);
191 put_super(s);
192 } else {
193 up_write(&s->s_umount);
194 }
195}
196
197EXPORT_SYMBOL(deactivate_locked_super);
198
199/**
Al Viro1712ac82010-03-22 15:22:31 -0400200 * deactivate_super - drop an active reference to superblock
201 * @s: superblock to deactivate
202 *
203 * Variant of deactivate_locked_super(), except that superblock is *not*
204 * locked by caller. If we are going to drop the final active reference,
205 * lock will be acquired prior to that.
206 */
207void deactivate_super(struct super_block *s)
208{
209 if (!atomic_add_unless(&s->s_active, -1, 1)) {
210 down_write(&s->s_umount);
211 deactivate_locked_super(s);
212 }
213}
214
215EXPORT_SYMBOL(deactivate_super);
216
217/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * grab_super - acquire an active reference
219 * @s: reference we are trying to make active
220 *
221 * Tries to acquire an active reference. grab_super() is used when we
222 * had just found a superblock in super_blocks or fs_type->fs_supers
223 * and want to turn it into a full-blown active reference. grab_super()
224 * is called with sb_lock held and drops it. Returns 1 in case of
225 * success, 0 if we had failed (superblock contents was already dead or
226 * dying when grab_super() had been called).
227 */
Josh Triplett9c4dbee2006-09-29 01:59:29 -0700228static int grab_super(struct super_block *s) __releases(sb_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Al Virob20bd1a2010-03-22 08:53:19 -0400230 if (atomic_inc_not_zero(&s->s_active)) {
231 spin_unlock(&sb_lock);
Al Virob20bd1a2010-03-22 08:53:19 -0400232 return 1;
233 }
234 /* it's going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 s->s_count++;
236 spin_unlock(&sb_lock);
Al Viro1712ac82010-03-22 15:22:31 -0400237 /* wait for it to die */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 down_write(&s->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 up_write(&s->s_umount);
240 put_super(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return 0;
242}
243
David Howellscf9a2ae2006-08-29 19:05:54 +0100244/*
Al Viro914e2632006-10-18 13:55:46 -0400245 * Superblock locking. We really ought to get rid of these two.
246 */
247void lock_super(struct super_block * sb)
248{
249 get_fs_excl();
250 mutex_lock(&sb->s_lock);
251}
252
253void unlock_super(struct super_block * sb)
254{
255 put_fs_excl();
256 mutex_unlock(&sb->s_lock);
257}
258
259EXPORT_SYMBOL(lock_super);
260EXPORT_SYMBOL(unlock_super);
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/**
263 * generic_shutdown_super - common helper for ->kill_sb()
264 * @sb: superblock to kill
265 *
266 * generic_shutdown_super() does all fs-independent work on superblock
267 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
268 * that need destruction out of superblock, call generic_shutdown_super()
269 * and release aforementioned objects. Note: dentries and inodes _are_
270 * taken care of and do not need specific handling.
David Howellsc636ebd2006-10-11 01:22:19 -0700271 *
272 * Upon calling this function, the filesystem may no longer alter or
273 * rearrange the set of dentries belonging to this super_block, nor may it
274 * change the attachments of dentries to inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 */
276void generic_shutdown_super(struct super_block *sb)
277{
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800278 const struct super_operations *sop = sb->s_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Arjan van de Venefaee192009-01-06 07:20:54 -0800280
David Howellsc636ebd2006-10-11 01:22:19 -0700281 if (sb->s_root) {
282 shrink_dcache_for_umount(sb);
Jan Kara60b06802009-04-27 16:43:53 +0200283 sync_filesystem(sb);
Al Viroa9e220f2009-05-05 22:10:44 -0400284 get_fs_excl();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 sb->s_flags &= ~MS_ACTIVE;
Arjan van de Venefaee192009-01-06 07:20:54 -0800286
Al Viro63997e92010-10-25 20:49:35 -0400287 fsnotify_unmount_inodes(&sb->s_inodes);
288
289 evict_inodes(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (sop->put_super)
292 sop->put_super(sb);
293
Al Viro63997e92010-10-25 20:49:35 -0400294 if (!list_empty(&sb->s_inodes)) {
Dave Jones7b4fe292006-02-07 12:58:48 -0800295 printk("VFS: Busy inodes after unmount of %s. "
296 "Self-destruct in 5 seconds. Have a nice day...\n",
297 sb->s_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
Al Viroa9e220f2009-05-05 22:10:44 -0400299 put_fs_excl();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301 spin_lock(&sb_lock);
302 /* should be initialized for __put_super_and_need_restart() */
Al Viro551de6f2010-03-22 19:36:35 -0400303 list_del_init(&sb->s_instances);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 spin_unlock(&sb_lock);
305 up_write(&sb->s_umount);
306}
307
308EXPORT_SYMBOL(generic_shutdown_super);
309
310/**
311 * sget - find or create a superblock
312 * @type: filesystem type superblock should belong to
313 * @test: comparison callback
314 * @set: setup callback
315 * @data: argument to each of them
316 */
317struct super_block *sget(struct file_system_type *type,
318 int (*test)(struct super_block *,void *),
319 int (*set)(struct super_block *,void *),
320 void *data)
321{
322 struct super_block *s = NULL;
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700323 struct super_block *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 int err;
325
326retry:
327 spin_lock(&sb_lock);
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700328 if (test) {
329 list_for_each_entry(old, &type->fs_supers, s_instances) {
330 if (!test(old, data))
331 continue;
332 if (!grab_super(old))
333 goto retry;
Li Zefana3cfbb52009-03-12 14:31:29 -0700334 if (s) {
335 up_write(&s->s_umount);
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700336 destroy_super(s);
Al Viro7a4dec52010-08-09 12:05:43 -0400337 s = NULL;
Li Zefana3cfbb52009-03-12 14:31:29 -0700338 }
Al Virod3f21472010-03-23 11:11:05 -0400339 down_write(&old->s_umount);
Al Viro7a4dec52010-08-09 12:05:43 -0400340 if (unlikely(!(old->s_flags & MS_BORN))) {
341 deactivate_locked_super(old);
342 goto retry;
343 }
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700344 return old;
345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347 if (!s) {
348 spin_unlock(&sb_lock);
Ingo Molnarcf516242006-07-03 00:25:27 -0700349 s = alloc_super(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (!s)
351 return ERR_PTR(-ENOMEM);
352 goto retry;
353 }
354
355 err = set(s, data);
356 if (err) {
357 spin_unlock(&sb_lock);
Li Zefana3cfbb52009-03-12 14:31:29 -0700358 up_write(&s->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 destroy_super(s);
360 return ERR_PTR(err);
361 }
362 s->s_type = type;
363 strlcpy(s->s_id, type->name, sizeof(s->s_id));
364 list_add_tail(&s->s_list, &super_blocks);
365 list_add(&s->s_instances, &type->fs_supers);
366 spin_unlock(&sb_lock);
367 get_filesystem(type);
368 return s;
369}
370
371EXPORT_SYMBOL(sget);
372
373void drop_super(struct super_block *sb)
374{
375 up_read(&sb->s_umount);
376 put_super(sb);
377}
378
379EXPORT_SYMBOL(drop_super);
380
Christoph Hellwige5004752009-05-05 16:08:56 +0200381/**
382 * sync_supers - helper for periodic superblock writeback
383 *
384 * Call the write_super method if present on all dirty superblocks in
385 * the system. This is for the periodic writeback used by most older
386 * filesystems. For data integrity superblock writeback use
387 * sync_filesystems() instead.
388 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 * Note: check the dirty flag before waiting, so we don't
390 * hold up the sync while mounting a device. (The newly
391 * mounted device won't need syncing.)
392 */
393void sync_supers(void)
394{
Al Virodca33252010-07-25 02:31:46 +0400395 struct super_block *sb, *p = NULL;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400398 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400399 if (list_empty(&sb->s_instances))
400 continue;
Christoph Hellwige5004752009-05-05 16:08:56 +0200401 if (sb->s_op->write_super && sb->s_dirt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 sb->s_count++;
403 spin_unlock(&sb_lock);
Christoph Hellwige5004752009-05-05 16:08:56 +0200404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 down_read(&sb->s_umount);
Christoph Hellwige5004752009-05-05 16:08:56 +0200406 if (sb->s_root && sb->s_dirt)
407 sb->s_op->write_super(sb);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700408 up_read(&sb->s_umount);
Christoph Hellwige5004752009-05-05 16:08:56 +0200409
Kirill Korotaev618f0632005-06-23 00:09:54 -0700410 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400411 if (p)
412 __put_super(p);
413 p = sb;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700414 }
415 }
Al Virodca33252010-07-25 02:31:46 +0400416 if (p)
417 __put_super(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 spin_unlock(&sb_lock);
419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421/**
Al Viro01a05b32010-03-23 06:06:58 -0400422 * iterate_supers - call function for all active superblocks
423 * @f: function to call
424 * @arg: argument to pass to it
425 *
426 * Scans the superblock list and calls given function, passing it
427 * locked superblock and given argument.
428 */
429void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
430{
Al Virodca33252010-07-25 02:31:46 +0400431 struct super_block *sb, *p = NULL;
Al Viro01a05b32010-03-23 06:06:58 -0400432
433 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400434 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro01a05b32010-03-23 06:06:58 -0400435 if (list_empty(&sb->s_instances))
436 continue;
437 sb->s_count++;
438 spin_unlock(&sb_lock);
439
440 down_read(&sb->s_umount);
441 if (sb->s_root)
442 f(sb, arg);
443 up_read(&sb->s_umount);
444
445 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400446 if (p)
447 __put_super(p);
448 p = sb;
Al Viro01a05b32010-03-23 06:06:58 -0400449 }
Al Virodca33252010-07-25 02:31:46 +0400450 if (p)
451 __put_super(p);
Al Viro01a05b32010-03-23 06:06:58 -0400452 spin_unlock(&sb_lock);
453}
454
455/**
Al Viro43e15cd2011-06-03 20:16:57 -0400456 * iterate_supers_type - call function for superblocks of given type
457 * @type: fs type
458 * @f: function to call
459 * @arg: argument to pass to it
460 *
461 * Scans the superblock list and calls given function, passing it
462 * locked superblock and given argument.
463 */
464void iterate_supers_type(struct file_system_type *type,
465 void (*f)(struct super_block *, void *), void *arg)
466{
467 struct super_block *sb, *p = NULL;
468
469 spin_lock(&sb_lock);
470 list_for_each_entry(sb, &type->fs_supers, s_instances) {
471 sb->s_count++;
472 spin_unlock(&sb_lock);
473
474 down_read(&sb->s_umount);
475 if (sb->s_root)
476 f(sb, arg);
477 up_read(&sb->s_umount);
478
479 spin_lock(&sb_lock);
480 if (p)
481 __put_super(p);
482 p = sb;
483 }
484 if (p)
485 __put_super(p);
486 spin_unlock(&sb_lock);
487}
488
489EXPORT_SYMBOL(iterate_supers_type);
490
491/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * get_super - get 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. %NULL is returned if no match is found.
497 */
498
Al Virodf40c012010-03-22 20:23:25 -0400499struct super_block *get_super(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700501 struct super_block *sb;
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (!bdev)
504 return NULL;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700507rescan:
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;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700511 if (sb->s_bdev == bdev) {
512 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700514 down_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400515 /* still alive? */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700516 if (sb->s_root)
517 return sb;
518 up_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400519 /* nope, got unmounted */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700520 spin_lock(&sb_lock);
Al Virodf40c012010-03-22 20:23:25 -0400521 __put_super(sb);
522 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524 }
525 spin_unlock(&sb_lock);
526 return NULL;
527}
528
529EXPORT_SYMBOL(get_super);
Christoph Hellwig45042302009-08-03 23:28:35 +0200530
531/**
532 * get_active_super - get an active reference to the superblock of a device
533 * @bdev: device to get the superblock for
534 *
535 * Scans the superblock list and finds the superblock of the file system
536 * mounted on the device given. Returns the superblock with an active
Al Virod3f21472010-03-23 11:11:05 -0400537 * reference or %NULL if none was found.
Christoph Hellwig45042302009-08-03 23:28:35 +0200538 */
539struct super_block *get_active_super(struct block_device *bdev)
540{
541 struct super_block *sb;
542
543 if (!bdev)
544 return NULL;
545
Al Viro14945832010-03-22 20:15:33 -0400546restart:
Christoph Hellwig45042302009-08-03 23:28:35 +0200547 spin_lock(&sb_lock);
548 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400549 if (list_empty(&sb->s_instances))
550 continue;
Al Viro14945832010-03-22 20:15:33 -0400551 if (sb->s_bdev == bdev) {
552 if (grab_super(sb)) /* drops sb_lock */
553 return sb;
554 else
555 goto restart;
556 }
Christoph Hellwig45042302009-08-03 23:28:35 +0200557 }
558 spin_unlock(&sb_lock);
559 return NULL;
560}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Al Virodf40c012010-03-22 20:23:25 -0400562struct super_block *user_get_super(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700564 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700567rescan:
568 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400569 if (list_empty(&sb->s_instances))
570 continue;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700571 if (sb->s_dev == dev) {
572 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700574 down_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400575 /* still alive? */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700576 if (sb->s_root)
577 return sb;
578 up_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400579 /* nope, got unmounted */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700580 spin_lock(&sb_lock);
Al Virodf40c012010-03-22 20:23:25 -0400581 __put_super(sb);
582 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584 }
585 spin_unlock(&sb_lock);
586 return NULL;
587}
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 * do_remount_sb - asks filesystem to change mount options.
591 * @sb: superblock in question
592 * @flags: numeric part of options
593 * @data: the rest of options
594 * @force: whether or not to force the change
595 *
596 * Alters the mount options of a mounted file system.
597 */
598int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
599{
600 int retval;
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400601 int remount_ro;
Christoph Hellwig45042302009-08-03 23:28:35 +0200602
603 if (sb->s_frozen != SB_UNFROZEN)
604 return -EBUSY;
605
David Howells93614012006-09-30 20:45:40 +0200606#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
608 return -EACCES;
David Howells93614012006-09-30 20:45:40 +0200609#endif
Christoph Hellwig45042302009-08-03 23:28:35 +0200610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (flags & MS_RDONLY)
612 acct_auto_close(sb);
613 shrink_dcache_sb(sb);
Jan Kara60b06802009-04-27 16:43:53 +0200614 sync_filesystem(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Nick Piggind208bbd2009-12-21 16:28:53 -0800616 remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
Nick Piggind208bbd2009-12-21 16:28:53 -0800617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /* If we are remounting RDONLY and current sb is read/write,
619 make sure there are no rw files opened */
Nick Piggind208bbd2009-12-21 16:28:53 -0800620 if (remount_ro) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (force)
622 mark_files_ro(sb);
J. R. Okajimab0895512009-06-17 01:16:50 +0900623 else if (!fs_may_remount_ro(sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return -EBUSY;
625 }
626
627 if (sb->s_op->remount_fs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 retval = sb->s_op->remount_fs(sb, &flags, data);
J. R. Okajimab0895512009-06-17 01:16:50 +0900629 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return retval;
631 }
632 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400633
Nick Piggind208bbd2009-12-21 16:28:53 -0800634 /*
635 * Some filesystems modify their metadata via some other path than the
636 * bdev buffer cache (eg. use a private mapping, or directories in
637 * pagecache, etc). Also file data modifications go via their own
638 * mappings. So If we try to mount readonly then copy the filesystem
639 * from bdev, we could get stale data, so invalidate it to give a best
640 * effort at coherency.
641 */
642 if (remount_ro && sb->s_bdev)
643 invalidate_bdev(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return 0;
645}
646
Jens Axboea2a95372009-03-17 09:38:40 +0100647static void do_emergency_remount(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Al Virodca33252010-07-25 02:31:46 +0400649 struct super_block *sb, *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400652 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400653 if (list_empty(&sb->s_instances))
654 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 sb->s_count++;
656 spin_unlock(&sb_lock);
Al Viro443b94b2009-05-05 23:48:50 -0400657 down_write(&sb->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
659 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 * What lock protects sb->s_flags??
661 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 do_remount_sb(sb, MS_RDONLY, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
Al Viro443b94b2009-05-05 23:48:50 -0400664 up_write(&sb->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400666 if (p)
667 __put_super(p);
668 p = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
Al Virodca33252010-07-25 02:31:46 +0400670 if (p)
671 __put_super(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 spin_unlock(&sb_lock);
Jens Axboea2a95372009-03-17 09:38:40 +0100673 kfree(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 printk("Emergency Remount complete\n");
675}
676
677void emergency_remount(void)
678{
Jens Axboea2a95372009-03-17 09:38:40 +0100679 struct work_struct *work;
680
681 work = kmalloc(sizeof(*work), GFP_ATOMIC);
682 if (work) {
683 INIT_WORK(work, do_emergency_remount);
684 schedule_work(work);
685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688/*
689 * Unnamed block devices are dummy devices used by virtual
690 * filesystems which don't use real block-devices. -- jrs
691 */
692
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400693static DEFINE_IDA(unnamed_dev_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
Al Viroc63e09e2009-06-24 02:05:18 -0400695static int unnamed_dev_start = 0; /* don't bother trying below it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Al Viro0ee5dc62011-07-07 15:44:25 -0400697int get_anon_bdev(dev_t *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 int dev;
700 int error;
701
702 retry:
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400703 if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return -ENOMEM;
705 spin_lock(&unnamed_dev_lock);
Al Viroc63e09e2009-06-24 02:05:18 -0400706 error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
Al Virof21f6222009-06-24 03:12:00 -0400707 if (!error)
708 unnamed_dev_start = dev + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 spin_unlock(&unnamed_dev_lock);
710 if (error == -EAGAIN)
711 /* We raced and lost with another CPU. */
712 goto retry;
713 else if (error)
714 return -EAGAIN;
715
716 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
717 spin_lock(&unnamed_dev_lock);
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400718 ida_remove(&unnamed_dev_ida, dev);
Al Virof21f6222009-06-24 03:12:00 -0400719 if (unnamed_dev_start > dev)
720 unnamed_dev_start = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 spin_unlock(&unnamed_dev_lock);
722 return -EMFILE;
723 }
Al Viro0ee5dc62011-07-07 15:44:25 -0400724 *p = MKDEV(0, dev & MINORMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 0;
726}
Al Viro0ee5dc62011-07-07 15:44:25 -0400727EXPORT_SYMBOL(get_anon_bdev);
728
729void free_anon_bdev(dev_t dev)
730{
731 int slot = MINOR(dev);
732 spin_lock(&unnamed_dev_lock);
733 ida_remove(&unnamed_dev_ida, slot);
734 if (slot < unnamed_dev_start)
735 unnamed_dev_start = slot;
736 spin_unlock(&unnamed_dev_lock);
737}
738EXPORT_SYMBOL(free_anon_bdev);
739
740int set_anon_super(struct super_block *s, void *data)
741{
742 int error = get_anon_bdev(&s->s_dev);
743 if (!error)
744 s->s_bdi = &noop_backing_dev_info;
745 return error;
746}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748EXPORT_SYMBOL(set_anon_super);
749
750void kill_anon_super(struct super_block *sb)
751{
Al Viro0ee5dc62011-07-07 15:44:25 -0400752 dev_t dev = sb->s_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 generic_shutdown_super(sb);
Al Viro0ee5dc62011-07-07 15:44:25 -0400754 free_anon_bdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
757EXPORT_SYMBOL(kill_anon_super);
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759void kill_litter_super(struct super_block *sb)
760{
761 if (sb->s_root)
762 d_genocide(sb->s_root);
763 kill_anon_super(sb);
764}
765
766EXPORT_SYMBOL(kill_litter_super);
767
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700768static int ns_test_super(struct super_block *sb, void *data)
769{
770 return sb->s_fs_info == data;
771}
772
773static int ns_set_super(struct super_block *sb, void *data)
774{
775 sb->s_fs_info = data;
776 return set_anon_super(sb, NULL);
777}
778
Al Viroceefda62010-07-26 13:16:50 +0400779struct dentry *mount_ns(struct file_system_type *fs_type, int flags,
780 void *data, int (*fill_super)(struct super_block *, void *, int))
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700781{
782 struct super_block *sb;
783
784 sb = sget(fs_type, ns_test_super, ns_set_super, data);
785 if (IS_ERR(sb))
Al Viroceefda62010-07-26 13:16:50 +0400786 return ERR_CAST(sb);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700787
788 if (!sb->s_root) {
789 int err;
790 sb->s_flags = flags;
791 err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
792 if (err) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400793 deactivate_locked_super(sb);
Al Viroceefda62010-07-26 13:16:50 +0400794 return ERR_PTR(err);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700795 }
796
797 sb->s_flags |= MS_ACTIVE;
798 }
799
Al Viroceefda62010-07-26 13:16:50 +0400800 return dget(sb->s_root);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700801}
802
Al Viroceefda62010-07-26 13:16:50 +0400803EXPORT_SYMBOL(mount_ns);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700804
David Howells93614012006-09-30 20:45:40 +0200805#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806static int set_bdev_super(struct super_block *s, void *data)
807{
808 s->s_bdev = data;
809 s->s_dev = s->s_bdev->bd_dev;
Jens Axboe32a88aa2009-09-16 15:02:33 +0200810
811 /*
812 * We set the bdi here to the queue backing, file systems can
813 * overwrite this in ->fill_super()
814 */
815 s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return 0;
817}
818
819static int test_bdev_super(struct super_block *s, void *data)
820{
821 return (void *)s->s_bdev == data;
822}
823
Al Viro152a0832010-07-25 00:46:55 +0400824struct dentry *mount_bdev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 int flags, const char *dev_name, void *data,
Al Viro152a0832010-07-25 00:46:55 +0400826 int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 struct block_device *bdev;
829 struct super_block *s;
Tejun Heod4d77622010-11-13 11:55:18 +0100830 fmode_t mode = FMODE_READ | FMODE_EXCL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 int error = 0;
832
Al Viro30c40d22008-02-22 19:50:45 -0500833 if (!(flags & MS_RDONLY))
834 mode |= FMODE_WRITE;
835
Tejun Heod4d77622010-11-13 11:55:18 +0100836 bdev = blkdev_get_by_path(dev_name, mode, fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (IS_ERR(bdev))
Al Viro152a0832010-07-25 00:46:55 +0400838 return ERR_CAST(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 /*
841 * once the super is inserted into the list by sget, s_umount
842 * will protect the lockfs code from trying to start a snapshot
843 * while we are mounting
844 */
Christoph Hellwig4fadd7b2009-08-03 23:28:06 +0200845 mutex_lock(&bdev->bd_fsfreeze_mutex);
846 if (bdev->bd_fsfreeze_count > 0) {
847 mutex_unlock(&bdev->bd_fsfreeze_mutex);
848 error = -EBUSY;
849 goto error_bdev;
850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
Christoph Hellwig4fadd7b2009-08-03 23:28:06 +0200852 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700854 goto error_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 if (s->s_root) {
857 if ((flags ^ s->s_flags) & MS_RDONLY) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400858 deactivate_locked_super(s);
David Howells454e2392006-06-23 02:02:57 -0700859 error = -EBUSY;
860 goto error_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
David Howells454e2392006-06-23 02:02:57 -0700862
Tejun Heo4f331f02010-07-20 15:18:07 -0700863 /*
864 * s_umount nests inside bd_mutex during
Tejun Heoe525fd82010-11-13 11:55:17 +0100865 * __invalidate_device(). blkdev_put() acquires
866 * bd_mutex and can't be called under s_umount. Drop
867 * s_umount temporarily. This is safe as we're
868 * holding an active reference.
Tejun Heo4f331f02010-07-20 15:18:07 -0700869 */
870 up_write(&s->s_umount);
Tejun Heod4d77622010-11-13 11:55:18 +0100871 blkdev_put(bdev, mode);
Tejun Heo4f331f02010-07-20 15:18:07 -0700872 down_write(&s->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 } else {
874 char b[BDEVNAME_SIZE];
875
Al Viro9e1f1de2011-06-03 18:24:58 -0400876 s->s_flags = flags | MS_NOSEC;
Al Viro30c40d22008-02-22 19:50:45 -0500877 s->s_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
Pekka Enberge78c9a02006-01-08 01:03:39 -0800879 sb_set_blocksize(s, block_size(bdev));
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800880 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (error) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400882 deactivate_locked_super(s);
David Howells454e2392006-06-23 02:02:57 -0700883 goto error;
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800884 }
David Howells454e2392006-06-23 02:02:57 -0700885
886 s->s_flags |= MS_ACTIVE;
Theodore Ts'o87d8fe12009-01-03 09:47:09 -0500887 bdev->bd_super = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889
Al Viro152a0832010-07-25 00:46:55 +0400890 return dget(s->s_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
David Howells454e2392006-06-23 02:02:57 -0700892error_s:
893 error = PTR_ERR(s);
894error_bdev:
Tejun Heod4d77622010-11-13 11:55:18 +0100895 blkdev_put(bdev, mode);
David Howells454e2392006-06-23 02:02:57 -0700896error:
Al Viro152a0832010-07-25 00:46:55 +0400897 return ERR_PTR(error);
898}
899EXPORT_SYMBOL(mount_bdev);
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901void kill_block_super(struct super_block *sb)
902{
903 struct block_device *bdev = sb->s_bdev;
Al Viro30c40d22008-02-22 19:50:45 -0500904 fmode_t mode = sb->s_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
H Hartley Sweetenddbaaf32009-04-29 20:14:57 -0400906 bdev->bd_super = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 generic_shutdown_super(sb);
908 sync_blockdev(bdev);
Tejun Heod4d77622010-11-13 11:55:18 +0100909 WARN_ON_ONCE(!(mode & FMODE_EXCL));
Tejun Heoe525fd82010-11-13 11:55:17 +0100910 blkdev_put(bdev, mode | FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
913EXPORT_SYMBOL(kill_block_super);
David Howells93614012006-09-30 20:45:40 +0200914#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Al Viro3c26ff62010-07-25 11:46:36 +0400916struct dentry *mount_nodev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 int flags, void *data,
Al Viro3c26ff62010-07-25 11:46:36 +0400918 int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 int error;
921 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
922
923 if (IS_ERR(s))
Al Viro3c26ff62010-07-25 11:46:36 +0400924 return ERR_CAST(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 s->s_flags = flags;
927
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800928 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (error) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400930 deactivate_locked_super(s);
Al Viro3c26ff62010-07-25 11:46:36 +0400931 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933 s->s_flags |= MS_ACTIVE;
Al Viro3c26ff62010-07-25 11:46:36 +0400934 return dget(s->s_root);
935}
936EXPORT_SYMBOL(mount_nodev);
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938static int compare_single(struct super_block *s, void *p)
939{
940 return 1;
941}
942
Al Virofc14f2f2010-07-25 01:48:30 +0400943struct dentry *mount_single(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 int flags, void *data,
Al Virofc14f2f2010-07-25 01:48:30 +0400945 int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 struct super_block *s;
948 int error;
949
950 s = sget(fs_type, compare_single, set_anon_super, NULL);
951 if (IS_ERR(s))
Al Virofc14f2f2010-07-25 01:48:30 +0400952 return ERR_CAST(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (!s->s_root) {
954 s->s_flags = flags;
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800955 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (error) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400957 deactivate_locked_super(s);
Al Virofc14f2f2010-07-25 01:48:30 +0400958 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960 s->s_flags |= MS_ACTIVE;
Kay Sievers9329d1b2009-12-18 21:18:15 +0100961 } else {
962 do_remount_sb(s, flags, data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
Al Virofc14f2f2010-07-25 01:48:30 +0400964 return dget(s->s_root);
965}
966EXPORT_SYMBOL(mount_single);
967
Al Viro9d412a42011-03-17 22:08:28 -0400968struct dentry *
969mount_fs(struct file_system_type *type, int flags, const char *name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Al Viroc96e41e2010-07-25 00:17:56 +0400971 struct dentry *root;
Al Viro9d412a42011-03-17 22:08:28 -0400972 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 char *secdata = NULL;
Al Viro9d412a42011-03-17 22:08:28 -0400974 int error = -ENOMEM;
Al Viro80893522010-02-05 09:30:46 -0500975
Eric Parise0007522008-03-05 10:31:54 -0500976 if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 secdata = alloc_secdata();
David Howells454e2392006-06-23 02:02:57 -0700978 if (!secdata)
Al Viro9d412a42011-03-17 22:08:28 -0400979 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Eric Parise0007522008-03-05 10:31:54 -0500981 error = security_sb_copy_data(data, secdata);
David Howells454e2392006-06-23 02:02:57 -0700982 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 goto out_free_secdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985
Al Viro1a102ff2011-03-16 09:07:58 -0400986 root = type->mount(type, flags, name, data);
987 if (IS_ERR(root)) {
988 error = PTR_ERR(root);
989 goto out_free_secdata;
Al Viroc96e41e2010-07-25 00:17:56 +0400990 }
Al Viro9d412a42011-03-17 22:08:28 -0400991 sb = root->d_sb;
992 BUG_ON(!sb);
993 WARN_ON(!sb->s_bdi);
Linus Torvalds6c510382011-03-24 10:16:26 -0700994 WARN_ON(sb->s_bdi == &default_backing_dev_info);
Al Viro9d412a42011-03-17 22:08:28 -0400995 sb->s_flags |= MS_BORN;
David Howells454e2392006-06-23 02:02:57 -0700996
Al Viro9d412a42011-03-17 22:08:28 -0400997 error = security_sb_kern_mount(sb, flags, secdata);
Jörn Engel5129a462010-04-25 08:54:42 +0200998 if (error)
999 goto out_sb;
David Howells454e2392006-06-23 02:02:57 -07001000
Jeff Layton42cb56a2009-09-18 13:05:53 -07001001 /*
1002 * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
1003 * but s_maxbytes was an unsigned long long for many releases. Throw
1004 * this warning for a little while to try and catch filesystems that
Jeff Layton4358b562011-03-29 09:33:31 -04001005 * violate this rule.
Jeff Layton42cb56a2009-09-18 13:05:53 -07001006 */
Al Viro9d412a42011-03-17 22:08:28 -04001007 WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
1008 "negative value (%lld)\n", type->name, sb->s_maxbytes);
Jeff Layton42cb56a2009-09-18 13:05:53 -07001009
Al Viro9d412a42011-03-17 22:08:28 -04001010 up_write(&sb->s_umount);
Gerald Schaefer8680e222005-06-21 17:15:16 -07001011 free_secdata(secdata);
Al Viro9d412a42011-03-17 22:08:28 -04001012 return root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013out_sb:
Al Viro9d412a42011-03-17 22:08:28 -04001014 dput(root);
1015 deactivate_locked_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016out_free_secdata:
1017 free_secdata(secdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018out:
David Howells454e2392006-06-23 02:02:57 -07001019 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
Josef Bacik18e9e512010-03-23 10:34:56 -04001022/**
Randy Dunlap7000d3c2010-05-24 22:22:34 -07001023 * freeze_super - lock the filesystem and force it into a consistent state
1024 * @sb: the super to lock
Josef Bacik18e9e512010-03-23 10:34:56 -04001025 *
1026 * Syncs the super to make sure the filesystem is consistent and calls the fs's
1027 * freeze_fs. Subsequent calls to this without first thawing the fs will return
1028 * -EBUSY.
1029 */
1030int freeze_super(struct super_block *sb)
1031{
1032 int ret;
1033
1034 atomic_inc(&sb->s_active);
1035 down_write(&sb->s_umount);
1036 if (sb->s_frozen) {
1037 deactivate_locked_super(sb);
1038 return -EBUSY;
1039 }
1040
1041 if (sb->s_flags & MS_RDONLY) {
1042 sb->s_frozen = SB_FREEZE_TRANS;
1043 smp_wmb();
1044 up_write(&sb->s_umount);
1045 return 0;
1046 }
1047
1048 sb->s_frozen = SB_FREEZE_WRITE;
1049 smp_wmb();
1050
1051 sync_filesystem(sb);
1052
1053 sb->s_frozen = SB_FREEZE_TRANS;
1054 smp_wmb();
1055
1056 sync_blockdev(sb->s_bdev);
1057 if (sb->s_op->freeze_fs) {
1058 ret = sb->s_op->freeze_fs(sb);
1059 if (ret) {
1060 printk(KERN_ERR
1061 "VFS:Filesystem freeze failed\n");
1062 sb->s_frozen = SB_UNFROZEN;
1063 deactivate_locked_super(sb);
1064 return ret;
1065 }
1066 }
1067 up_write(&sb->s_umount);
1068 return 0;
1069}
1070EXPORT_SYMBOL(freeze_super);
1071
1072/**
1073 * thaw_super -- unlock filesystem
1074 * @sb: the super to thaw
1075 *
1076 * Unlocks the filesystem and marks it writeable again after freeze_super().
1077 */
1078int thaw_super(struct super_block *sb)
1079{
1080 int error;
1081
1082 down_write(&sb->s_umount);
1083 if (sb->s_frozen == SB_UNFROZEN) {
1084 up_write(&sb->s_umount);
1085 return -EINVAL;
1086 }
1087
1088 if (sb->s_flags & MS_RDONLY)
1089 goto out;
1090
1091 if (sb->s_op->unfreeze_fs) {
1092 error = sb->s_op->unfreeze_fs(sb);
1093 if (error) {
1094 printk(KERN_ERR
1095 "VFS:Filesystem thaw failed\n");
1096 sb->s_frozen = SB_FREEZE_TRANS;
1097 up_write(&sb->s_umount);
1098 return error;
1099 }
1100 }
1101
1102out:
1103 sb->s_frozen = SB_UNFROZEN;
1104 smp_wmb();
1105 wake_up(&sb->s_wait_unfrozen);
1106 deactivate_locked_super(sb);
1107
1108 return 0;
1109}
1110EXPORT_SYMBOL(thaw_super);