blob: 73ab9f9b35716c7d20bb896de267b866054bbfec [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);
Dave Chinner09cc9fc2011-07-08 14:14:40 +100081 spin_lock_init(&s->s_inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 init_rwsem(&s->s_umount);
Ingo Molnar7892f2f2006-01-09 15:59:25 -080083 mutex_init(&s->s_lock);
Arjan van de Ven897c6ff2006-07-03 00:25:28 -070084 lockdep_set_class(&s->s_umount, &type->s_umount_key);
Ingo Molnarcf516242006-07-03 00:25:27 -070085 /*
86 * The locking rules for s_lock are up to the
87 * filesystem. For example ext3fs has different
88 * lock ordering than usbfs:
89 */
90 lockdep_set_class(&s->s_lock, &type->s_lock_key);
Peter Zijlstraada723d2009-02-18 14:48:30 -080091 /*
92 * sget() can have s_umount recursion.
93 *
94 * When it cannot find a suitable sb, it allocates a new
95 * one (this one), and tries again to find a suitable old
96 * one.
97 *
98 * In case that succeeds, it will acquire the s_umount
99 * lock of the old one. Since these are clearly distrinct
100 * locks, and this object isn't exposed yet, there's no
101 * risk of deadlocks.
102 *
103 * Annotate this by putting this lock in a different
104 * subclass.
105 */
106 down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
Al Virob20bd1a2010-03-22 08:53:19 -0400107 s->s_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 atomic_set(&s->s_active, 1);
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800109 mutex_init(&s->s_vfs_rename_mutex);
Roland Dreier51ee0492010-04-27 14:23:57 -0700110 lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
Ingo Molnard3be9152006-03-23 03:00:29 -0800111 mutex_init(&s->s_dquot.dqio_mutex);
112 mutex_init(&s->s_dquot.dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 init_rwsem(&s->s_dquot.dqptr_sem);
114 init_waitqueue_head(&s->s_wait_unfrozen);
115 s->s_maxbytes = MAX_NON_LFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 s->s_op = &default_op;
117 s->s_time_gran = 1000000000;
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600118 s->cleancache_poolid = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120out:
121 return s;
122}
123
124/**
125 * destroy_super - frees a superblock
126 * @s: superblock to free
127 *
128 * Frees a superblock.
129 */
130static inline void destroy_super(struct super_block *s)
131{
Nick Piggin6416ccb2010-08-18 04:37:38 +1000132#ifdef CONFIG_SMP
133 free_percpu(s->s_files);
134#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 security_sb_free(s);
Miklos Szeredi79c0b2d2007-05-08 00:25:43 -0700136 kfree(s->s_subtype);
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800137 kfree(s->s_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 kfree(s);
139}
140
141/* Superblock refcounting */
142
143/*
Al Viro35cf7ba2010-03-22 21:13:53 -0400144 * Drop a superblock's refcount. The caller must hold sb_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 */
Al Viro35cf7ba2010-03-22 21:13:53 -0400146void __put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (!--sb->s_count) {
Al Viro551de6f2010-03-22 19:36:35 -0400149 list_del_init(&sb->s_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 destroy_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
154/**
155 * put_super - drop a temporary reference to superblock
156 * @sb: superblock in question
157 *
158 * Drops a temporary reference, frees superblock if there's no
159 * references left.
160 */
Jens Axboe03ba3782009-09-09 09:08:54 +0200161void put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 spin_lock(&sb_lock);
164 __put_super(sb);
165 spin_unlock(&sb_lock);
166}
167
168
169/**
Al Viro74dbbdd2009-05-06 01:07:50 -0400170 * deactivate_locked_super - drop an active reference to superblock
171 * @s: superblock to deactivate
172 *
Al Viro1712ac82010-03-22 15:22:31 -0400173 * Drops an active reference to superblock, converting it into a temprory
174 * one if there is no other active references left. In that case we
175 * tell fs driver to shut it down and drop the temporary reference we
176 * had just acquired.
177 *
178 * Caller holds exclusive lock on superblock; that lock is released.
Al Viro74dbbdd2009-05-06 01:07:50 -0400179 */
180void deactivate_locked_super(struct super_block *s)
181{
182 struct file_system_type *fs = s->s_type;
Al Virob20bd1a2010-03-22 08:53:19 -0400183 if (atomic_dec_and_test(&s->s_active)) {
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600184 cleancache_flush_fs(s);
Al Viro74dbbdd2009-05-06 01:07:50 -0400185 fs->kill_sb(s);
Boaz Harroshd863b502011-02-10 15:01:20 -0800186 /*
187 * We need to call rcu_barrier so all the delayed rcu free
188 * inodes are flushed before we release the fs module.
189 */
190 rcu_barrier();
Al Viro74dbbdd2009-05-06 01:07:50 -0400191 put_filesystem(fs);
192 put_super(s);
193 } else {
194 up_write(&s->s_umount);
195 }
196}
197
198EXPORT_SYMBOL(deactivate_locked_super);
199
200/**
Al Viro1712ac82010-03-22 15:22:31 -0400201 * deactivate_super - drop an active reference to superblock
202 * @s: superblock to deactivate
203 *
204 * Variant of deactivate_locked_super(), except that superblock is *not*
205 * locked by caller. If we are going to drop the final active reference,
206 * lock will be acquired prior to that.
207 */
208void deactivate_super(struct super_block *s)
209{
210 if (!atomic_add_unless(&s->s_active, -1, 1)) {
211 down_write(&s->s_umount);
212 deactivate_locked_super(s);
213 }
214}
215
216EXPORT_SYMBOL(deactivate_super);
217
218/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * grab_super - acquire an active reference
220 * @s: reference we are trying to make active
221 *
222 * Tries to acquire an active reference. grab_super() is used when we
223 * had just found a superblock in super_blocks or fs_type->fs_supers
224 * and want to turn it into a full-blown active reference. grab_super()
225 * is called with sb_lock held and drops it. Returns 1 in case of
226 * success, 0 if we had failed (superblock contents was already dead or
227 * dying when grab_super() had been called).
228 */
Josh Triplett9c4dbee2006-09-29 01:59:29 -0700229static int grab_super(struct super_block *s) __releases(sb_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Al Virob20bd1a2010-03-22 08:53:19 -0400231 if (atomic_inc_not_zero(&s->s_active)) {
232 spin_unlock(&sb_lock);
Al Virob20bd1a2010-03-22 08:53:19 -0400233 return 1;
234 }
235 /* it's going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 s->s_count++;
237 spin_unlock(&sb_lock);
Al Viro1712ac82010-03-22 15:22:31 -0400238 /* wait for it to die */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 down_write(&s->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 up_write(&s->s_umount);
241 put_super(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return 0;
243}
244
David Howellscf9a2ae2006-08-29 19:05:54 +0100245/*
Al Viro914e2632006-10-18 13:55:46 -0400246 * Superblock locking. We really ought to get rid of these two.
247 */
248void lock_super(struct super_block * sb)
249{
250 get_fs_excl();
251 mutex_lock(&sb->s_lock);
252}
253
254void unlock_super(struct super_block * sb)
255{
256 put_fs_excl();
257 mutex_unlock(&sb->s_lock);
258}
259
260EXPORT_SYMBOL(lock_super);
261EXPORT_SYMBOL(unlock_super);
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263/**
264 * generic_shutdown_super - common helper for ->kill_sb()
265 * @sb: superblock to kill
266 *
267 * generic_shutdown_super() does all fs-independent work on superblock
268 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
269 * that need destruction out of superblock, call generic_shutdown_super()
270 * and release aforementioned objects. Note: dentries and inodes _are_
271 * taken care of and do not need specific handling.
David Howellsc636ebd2006-10-11 01:22:19 -0700272 *
273 * Upon calling this function, the filesystem may no longer alter or
274 * rearrange the set of dentries belonging to this super_block, nor may it
275 * change the attachments of dentries to inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 */
277void generic_shutdown_super(struct super_block *sb)
278{
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800279 const struct super_operations *sop = sb->s_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Arjan van de Venefaee192009-01-06 07:20:54 -0800281
David Howellsc636ebd2006-10-11 01:22:19 -0700282 if (sb->s_root) {
283 shrink_dcache_for_umount(sb);
Jan Kara60b06802009-04-27 16:43:53 +0200284 sync_filesystem(sb);
Al Viroa9e220f2009-05-05 22:10:44 -0400285 get_fs_excl();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 sb->s_flags &= ~MS_ACTIVE;
Arjan van de Venefaee192009-01-06 07:20:54 -0800287
Al Viro63997e92010-10-25 20:49:35 -0400288 fsnotify_unmount_inodes(&sb->s_inodes);
289
290 evict_inodes(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (sop->put_super)
293 sop->put_super(sb);
294
Al Viro63997e92010-10-25 20:49:35 -0400295 if (!list_empty(&sb->s_inodes)) {
Dave Jones7b4fe292006-02-07 12:58:48 -0800296 printk("VFS: Busy inodes after unmount of %s. "
297 "Self-destruct in 5 seconds. Have a nice day...\n",
298 sb->s_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
Al Viroa9e220f2009-05-05 22:10:44 -0400300 put_fs_excl();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302 spin_lock(&sb_lock);
303 /* should be initialized for __put_super_and_need_restart() */
Al Viro551de6f2010-03-22 19:36:35 -0400304 list_del_init(&sb->s_instances);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 spin_unlock(&sb_lock);
306 up_write(&sb->s_umount);
307}
308
309EXPORT_SYMBOL(generic_shutdown_super);
310
311/**
312 * sget - find or create a superblock
313 * @type: filesystem type superblock should belong to
314 * @test: comparison callback
315 * @set: setup callback
316 * @data: argument to each of them
317 */
318struct super_block *sget(struct file_system_type *type,
319 int (*test)(struct super_block *,void *),
320 int (*set)(struct super_block *,void *),
321 void *data)
322{
323 struct super_block *s = NULL;
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700324 struct super_block *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int err;
326
327retry:
328 spin_lock(&sb_lock);
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700329 if (test) {
330 list_for_each_entry(old, &type->fs_supers, s_instances) {
331 if (!test(old, data))
332 continue;
333 if (!grab_super(old))
334 goto retry;
Li Zefana3cfbb52009-03-12 14:31:29 -0700335 if (s) {
336 up_write(&s->s_umount);
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700337 destroy_super(s);
Al Viro7a4dec52010-08-09 12:05:43 -0400338 s = NULL;
Li Zefana3cfbb52009-03-12 14:31:29 -0700339 }
Al Virod3f21472010-03-23 11:11:05 -0400340 down_write(&old->s_umount);
Al Viro7a4dec52010-08-09 12:05:43 -0400341 if (unlikely(!(old->s_flags & MS_BORN))) {
342 deactivate_locked_super(old);
343 goto retry;
344 }
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700345 return old;
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 if (!s) {
349 spin_unlock(&sb_lock);
Ingo Molnarcf516242006-07-03 00:25:27 -0700350 s = alloc_super(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (!s)
352 return ERR_PTR(-ENOMEM);
353 goto retry;
354 }
355
356 err = set(s, data);
357 if (err) {
358 spin_unlock(&sb_lock);
Li Zefana3cfbb52009-03-12 14:31:29 -0700359 up_write(&s->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 destroy_super(s);
361 return ERR_PTR(err);
362 }
363 s->s_type = type;
364 strlcpy(s->s_id, type->name, sizeof(s->s_id));
365 list_add_tail(&s->s_list, &super_blocks);
366 list_add(&s->s_instances, &type->fs_supers);
367 spin_unlock(&sb_lock);
368 get_filesystem(type);
369 return s;
370}
371
372EXPORT_SYMBOL(sget);
373
374void drop_super(struct super_block *sb)
375{
376 up_read(&sb->s_umount);
377 put_super(sb);
378}
379
380EXPORT_SYMBOL(drop_super);
381
Christoph Hellwige5004752009-05-05 16:08:56 +0200382/**
383 * sync_supers - helper for periodic superblock writeback
384 *
385 * Call the write_super method if present on all dirty superblocks in
386 * the system. This is for the periodic writeback used by most older
387 * filesystems. For data integrity superblock writeback use
388 * sync_filesystems() instead.
389 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 * Note: check the dirty flag before waiting, so we don't
391 * hold up the sync while mounting a device. (The newly
392 * mounted device won't need syncing.)
393 */
394void sync_supers(void)
395{
Al Virodca33252010-07-25 02:31:46 +0400396 struct super_block *sb, *p = NULL;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400399 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400400 if (list_empty(&sb->s_instances))
401 continue;
Christoph Hellwige5004752009-05-05 16:08:56 +0200402 if (sb->s_op->write_super && sb->s_dirt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 sb->s_count++;
404 spin_unlock(&sb_lock);
Christoph Hellwige5004752009-05-05 16:08:56 +0200405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 down_read(&sb->s_umount);
Christoph Hellwige5004752009-05-05 16:08:56 +0200407 if (sb->s_root && sb->s_dirt)
408 sb->s_op->write_super(sb);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700409 up_read(&sb->s_umount);
Christoph Hellwige5004752009-05-05 16:08:56 +0200410
Kirill Korotaev618f0632005-06-23 00:09:54 -0700411 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400412 if (p)
413 __put_super(p);
414 p = sb;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700415 }
416 }
Al Virodca33252010-07-25 02:31:46 +0400417 if (p)
418 __put_super(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 spin_unlock(&sb_lock);
420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422/**
Al Viro01a05b32010-03-23 06:06:58 -0400423 * iterate_supers - call function for all active superblocks
424 * @f: function to call
425 * @arg: argument to pass to it
426 *
427 * Scans the superblock list and calls given function, passing it
428 * locked superblock and given argument.
429 */
430void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
431{
Al Virodca33252010-07-25 02:31:46 +0400432 struct super_block *sb, *p = NULL;
Al Viro01a05b32010-03-23 06:06:58 -0400433
434 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400435 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro01a05b32010-03-23 06:06:58 -0400436 if (list_empty(&sb->s_instances))
437 continue;
438 sb->s_count++;
439 spin_unlock(&sb_lock);
440
441 down_read(&sb->s_umount);
442 if (sb->s_root)
443 f(sb, arg);
444 up_read(&sb->s_umount);
445
446 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400447 if (p)
448 __put_super(p);
449 p = sb;
Al Viro01a05b32010-03-23 06:06:58 -0400450 }
Al Virodca33252010-07-25 02:31:46 +0400451 if (p)
452 __put_super(p);
Al Viro01a05b32010-03-23 06:06:58 -0400453 spin_unlock(&sb_lock);
454}
455
456/**
Al Viro43e15cd2011-06-03 20:16:57 -0400457 * iterate_supers_type - call function for superblocks of given type
458 * @type: fs type
459 * @f: function to call
460 * @arg: argument to pass to it
461 *
462 * Scans the superblock list and calls given function, passing it
463 * locked superblock and given argument.
464 */
465void iterate_supers_type(struct file_system_type *type,
466 void (*f)(struct super_block *, void *), void *arg)
467{
468 struct super_block *sb, *p = NULL;
469
470 spin_lock(&sb_lock);
471 list_for_each_entry(sb, &type->fs_supers, s_instances) {
472 sb->s_count++;
473 spin_unlock(&sb_lock);
474
475 down_read(&sb->s_umount);
476 if (sb->s_root)
477 f(sb, arg);
478 up_read(&sb->s_umount);
479
480 spin_lock(&sb_lock);
481 if (p)
482 __put_super(p);
483 p = sb;
484 }
485 if (p)
486 __put_super(p);
487 spin_unlock(&sb_lock);
488}
489
490EXPORT_SYMBOL(iterate_supers_type);
491
492/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * get_super - get the superblock of a device
494 * @bdev: device to get the superblock for
495 *
496 * Scans the superblock list and finds the superblock of the file system
497 * mounted on the device given. %NULL is returned if no match is found.
498 */
499
Al Virodf40c012010-03-22 20:23:25 -0400500struct super_block *get_super(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700502 struct super_block *sb;
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (!bdev)
505 return NULL;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700508rescan:
509 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400510 if (list_empty(&sb->s_instances))
511 continue;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700512 if (sb->s_bdev == bdev) {
513 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700515 down_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400516 /* still alive? */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700517 if (sb->s_root)
518 return sb;
519 up_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400520 /* nope, got unmounted */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700521 spin_lock(&sb_lock);
Al Virodf40c012010-03-22 20:23:25 -0400522 __put_super(sb);
523 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 }
526 spin_unlock(&sb_lock);
527 return NULL;
528}
529
530EXPORT_SYMBOL(get_super);
Christoph Hellwig45042302009-08-03 23:28:35 +0200531
532/**
533 * get_active_super - get an active reference to the superblock of a device
534 * @bdev: device to get the superblock for
535 *
536 * Scans the superblock list and finds the superblock of the file system
537 * mounted on the device given. Returns the superblock with an active
Al Virod3f21472010-03-23 11:11:05 -0400538 * reference or %NULL if none was found.
Christoph Hellwig45042302009-08-03 23:28:35 +0200539 */
540struct super_block *get_active_super(struct block_device *bdev)
541{
542 struct super_block *sb;
543
544 if (!bdev)
545 return NULL;
546
Al Viro14945832010-03-22 20:15:33 -0400547restart:
Christoph Hellwig45042302009-08-03 23:28:35 +0200548 spin_lock(&sb_lock);
549 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400550 if (list_empty(&sb->s_instances))
551 continue;
Al Viro14945832010-03-22 20:15:33 -0400552 if (sb->s_bdev == bdev) {
553 if (grab_super(sb)) /* drops sb_lock */
554 return sb;
555 else
556 goto restart;
557 }
Christoph Hellwig45042302009-08-03 23:28:35 +0200558 }
559 spin_unlock(&sb_lock);
560 return NULL;
561}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Al Virodf40c012010-03-22 20:23:25 -0400563struct super_block *user_get_super(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700565 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700568rescan:
569 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400570 if (list_empty(&sb->s_instances))
571 continue;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700572 if (sb->s_dev == dev) {
573 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700575 down_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400576 /* still alive? */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700577 if (sb->s_root)
578 return sb;
579 up_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400580 /* nope, got unmounted */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700581 spin_lock(&sb_lock);
Al Virodf40c012010-03-22 20:23:25 -0400582 __put_super(sb);
583 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585 }
586 spin_unlock(&sb_lock);
587 return NULL;
588}
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * do_remount_sb - asks filesystem to change mount options.
592 * @sb: superblock in question
593 * @flags: numeric part of options
594 * @data: the rest of options
595 * @force: whether or not to force the change
596 *
597 * Alters the mount options of a mounted file system.
598 */
599int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
600{
601 int retval;
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400602 int remount_ro;
Christoph Hellwig45042302009-08-03 23:28:35 +0200603
604 if (sb->s_frozen != SB_UNFROZEN)
605 return -EBUSY;
606
David Howells93614012006-09-30 20:45:40 +0200607#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
609 return -EACCES;
David Howells93614012006-09-30 20:45:40 +0200610#endif
Christoph Hellwig45042302009-08-03 23:28:35 +0200611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (flags & MS_RDONLY)
613 acct_auto_close(sb);
614 shrink_dcache_sb(sb);
Jan Kara60b06802009-04-27 16:43:53 +0200615 sync_filesystem(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Nick Piggind208bbd2009-12-21 16:28:53 -0800617 remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
Nick Piggind208bbd2009-12-21 16:28:53 -0800618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* If we are remounting RDONLY and current sb is read/write,
620 make sure there are no rw files opened */
Nick Piggind208bbd2009-12-21 16:28:53 -0800621 if (remount_ro) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (force)
623 mark_files_ro(sb);
J. R. Okajimab0895512009-06-17 01:16:50 +0900624 else if (!fs_may_remount_ro(sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return -EBUSY;
626 }
627
628 if (sb->s_op->remount_fs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 retval = sb->s_op->remount_fs(sb, &flags, data);
J. R. Okajimab0895512009-06-17 01:16:50 +0900630 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return retval;
632 }
633 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400634
Nick Piggind208bbd2009-12-21 16:28:53 -0800635 /*
636 * Some filesystems modify their metadata via some other path than the
637 * bdev buffer cache (eg. use a private mapping, or directories in
638 * pagecache, etc). Also file data modifications go via their own
639 * mappings. So If we try to mount readonly then copy the filesystem
640 * from bdev, we could get stale data, so invalidate it to give a best
641 * effort at coherency.
642 */
643 if (remount_ro && sb->s_bdev)
644 invalidate_bdev(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return 0;
646}
647
Jens Axboea2a95372009-03-17 09:38:40 +0100648static void do_emergency_remount(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Al Virodca33252010-07-25 02:31:46 +0400650 struct super_block *sb, *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400653 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400654 if (list_empty(&sb->s_instances))
655 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 sb->s_count++;
657 spin_unlock(&sb_lock);
Al Viro443b94b2009-05-05 23:48:50 -0400658 down_write(&sb->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
660 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 * What lock protects sb->s_flags??
662 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 do_remount_sb(sb, MS_RDONLY, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Al Viro443b94b2009-05-05 23:48:50 -0400665 up_write(&sb->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400667 if (p)
668 __put_super(p);
669 p = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
Al Virodca33252010-07-25 02:31:46 +0400671 if (p)
672 __put_super(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 spin_unlock(&sb_lock);
Jens Axboea2a95372009-03-17 09:38:40 +0100674 kfree(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 printk("Emergency Remount complete\n");
676}
677
678void emergency_remount(void)
679{
Jens Axboea2a95372009-03-17 09:38:40 +0100680 struct work_struct *work;
681
682 work = kmalloc(sizeof(*work), GFP_ATOMIC);
683 if (work) {
684 INIT_WORK(work, do_emergency_remount);
685 schedule_work(work);
686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689/*
690 * Unnamed block devices are dummy devices used by virtual
691 * filesystems which don't use real block-devices. -- jrs
692 */
693
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400694static DEFINE_IDA(unnamed_dev_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
Al Viroc63e09e2009-06-24 02:05:18 -0400696static int unnamed_dev_start = 0; /* don't bother trying below it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Al Viro0ee5dc62011-07-07 15:44:25 -0400698int get_anon_bdev(dev_t *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 int dev;
701 int error;
702
703 retry:
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400704 if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return -ENOMEM;
706 spin_lock(&unnamed_dev_lock);
Al Viroc63e09e2009-06-24 02:05:18 -0400707 error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
Al Virof21f6222009-06-24 03:12:00 -0400708 if (!error)
709 unnamed_dev_start = dev + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 spin_unlock(&unnamed_dev_lock);
711 if (error == -EAGAIN)
712 /* We raced and lost with another CPU. */
713 goto retry;
714 else if (error)
715 return -EAGAIN;
716
717 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
718 spin_lock(&unnamed_dev_lock);
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400719 ida_remove(&unnamed_dev_ida, dev);
Al Virof21f6222009-06-24 03:12:00 -0400720 if (unnamed_dev_start > dev)
721 unnamed_dev_start = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 spin_unlock(&unnamed_dev_lock);
723 return -EMFILE;
724 }
Al Viro0ee5dc62011-07-07 15:44:25 -0400725 *p = MKDEV(0, dev & MINORMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return 0;
727}
Al Viro0ee5dc62011-07-07 15:44:25 -0400728EXPORT_SYMBOL(get_anon_bdev);
729
730void free_anon_bdev(dev_t dev)
731{
732 int slot = MINOR(dev);
733 spin_lock(&unnamed_dev_lock);
734 ida_remove(&unnamed_dev_ida, slot);
735 if (slot < unnamed_dev_start)
736 unnamed_dev_start = slot;
737 spin_unlock(&unnamed_dev_lock);
738}
739EXPORT_SYMBOL(free_anon_bdev);
740
741int set_anon_super(struct super_block *s, void *data)
742{
743 int error = get_anon_bdev(&s->s_dev);
744 if (!error)
745 s->s_bdi = &noop_backing_dev_info;
746 return error;
747}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749EXPORT_SYMBOL(set_anon_super);
750
751void kill_anon_super(struct super_block *sb)
752{
Al Viro0ee5dc62011-07-07 15:44:25 -0400753 dev_t dev = sb->s_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 generic_shutdown_super(sb);
Al Viro0ee5dc62011-07-07 15:44:25 -0400755 free_anon_bdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
758EXPORT_SYMBOL(kill_anon_super);
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760void kill_litter_super(struct super_block *sb)
761{
762 if (sb->s_root)
763 d_genocide(sb->s_root);
764 kill_anon_super(sb);
765}
766
767EXPORT_SYMBOL(kill_litter_super);
768
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700769static int ns_test_super(struct super_block *sb, void *data)
770{
771 return sb->s_fs_info == data;
772}
773
774static int ns_set_super(struct super_block *sb, void *data)
775{
776 sb->s_fs_info = data;
777 return set_anon_super(sb, NULL);
778}
779
Al Viroceefda62010-07-26 13:16:50 +0400780struct dentry *mount_ns(struct file_system_type *fs_type, int flags,
781 void *data, int (*fill_super)(struct super_block *, void *, int))
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700782{
783 struct super_block *sb;
784
785 sb = sget(fs_type, ns_test_super, ns_set_super, data);
786 if (IS_ERR(sb))
Al Viroceefda62010-07-26 13:16:50 +0400787 return ERR_CAST(sb);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700788
789 if (!sb->s_root) {
790 int err;
791 sb->s_flags = flags;
792 err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
793 if (err) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400794 deactivate_locked_super(sb);
Al Viroceefda62010-07-26 13:16:50 +0400795 return ERR_PTR(err);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700796 }
797
798 sb->s_flags |= MS_ACTIVE;
799 }
800
Al Viroceefda62010-07-26 13:16:50 +0400801 return dget(sb->s_root);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700802}
803
Al Viroceefda62010-07-26 13:16:50 +0400804EXPORT_SYMBOL(mount_ns);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700805
David Howells93614012006-09-30 20:45:40 +0200806#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807static int set_bdev_super(struct super_block *s, void *data)
808{
809 s->s_bdev = data;
810 s->s_dev = s->s_bdev->bd_dev;
Jens Axboe32a88aa2009-09-16 15:02:33 +0200811
812 /*
813 * We set the bdi here to the queue backing, file systems can
814 * overwrite this in ->fill_super()
815 */
816 s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return 0;
818}
819
820static int test_bdev_super(struct super_block *s, void *data)
821{
822 return (void *)s->s_bdev == data;
823}
824
Al Viro152a0832010-07-25 00:46:55 +0400825struct dentry *mount_bdev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 int flags, const char *dev_name, void *data,
Al Viro152a0832010-07-25 00:46:55 +0400827 int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 struct block_device *bdev;
830 struct super_block *s;
Tejun Heod4d77622010-11-13 11:55:18 +0100831 fmode_t mode = FMODE_READ | FMODE_EXCL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 int error = 0;
833
Al Viro30c40d22008-02-22 19:50:45 -0500834 if (!(flags & MS_RDONLY))
835 mode |= FMODE_WRITE;
836
Tejun Heod4d77622010-11-13 11:55:18 +0100837 bdev = blkdev_get_by_path(dev_name, mode, fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (IS_ERR(bdev))
Al Viro152a0832010-07-25 00:46:55 +0400839 return ERR_CAST(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /*
842 * once the super is inserted into the list by sget, s_umount
843 * will protect the lockfs code from trying to start a snapshot
844 * while we are mounting
845 */
Christoph Hellwig4fadd7b2009-08-03 23:28:06 +0200846 mutex_lock(&bdev->bd_fsfreeze_mutex);
847 if (bdev->bd_fsfreeze_count > 0) {
848 mutex_unlock(&bdev->bd_fsfreeze_mutex);
849 error = -EBUSY;
850 goto error_bdev;
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
Christoph Hellwig4fadd7b2009-08-03 23:28:06 +0200853 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700855 goto error_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 if (s->s_root) {
858 if ((flags ^ s->s_flags) & MS_RDONLY) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400859 deactivate_locked_super(s);
David Howells454e2392006-06-23 02:02:57 -0700860 error = -EBUSY;
861 goto error_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
David Howells454e2392006-06-23 02:02:57 -0700863
Tejun Heo4f331f02010-07-20 15:18:07 -0700864 /*
865 * s_umount nests inside bd_mutex during
Tejun Heoe525fd82010-11-13 11:55:17 +0100866 * __invalidate_device(). blkdev_put() acquires
867 * bd_mutex and can't be called under s_umount. Drop
868 * s_umount temporarily. This is safe as we're
869 * holding an active reference.
Tejun Heo4f331f02010-07-20 15:18:07 -0700870 */
871 up_write(&s->s_umount);
Tejun Heod4d77622010-11-13 11:55:18 +0100872 blkdev_put(bdev, mode);
Tejun Heo4f331f02010-07-20 15:18:07 -0700873 down_write(&s->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 } else {
875 char b[BDEVNAME_SIZE];
876
Al Viro9e1f1de2011-06-03 18:24:58 -0400877 s->s_flags = flags | MS_NOSEC;
Al Viro30c40d22008-02-22 19:50:45 -0500878 s->s_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
Pekka Enberge78c9a02006-01-08 01:03:39 -0800880 sb_set_blocksize(s, block_size(bdev));
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800881 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (error) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400883 deactivate_locked_super(s);
David Howells454e2392006-06-23 02:02:57 -0700884 goto error;
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800885 }
David Howells454e2392006-06-23 02:02:57 -0700886
887 s->s_flags |= MS_ACTIVE;
Theodore Ts'o87d8fe12009-01-03 09:47:09 -0500888 bdev->bd_super = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890
Al Viro152a0832010-07-25 00:46:55 +0400891 return dget(s->s_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
David Howells454e2392006-06-23 02:02:57 -0700893error_s:
894 error = PTR_ERR(s);
895error_bdev:
Tejun Heod4d77622010-11-13 11:55:18 +0100896 blkdev_put(bdev, mode);
David Howells454e2392006-06-23 02:02:57 -0700897error:
Al Viro152a0832010-07-25 00:46:55 +0400898 return ERR_PTR(error);
899}
900EXPORT_SYMBOL(mount_bdev);
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902void kill_block_super(struct super_block *sb)
903{
904 struct block_device *bdev = sb->s_bdev;
Al Viro30c40d22008-02-22 19:50:45 -0500905 fmode_t mode = sb->s_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
H Hartley Sweetenddbaaf32009-04-29 20:14:57 -0400907 bdev->bd_super = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 generic_shutdown_super(sb);
909 sync_blockdev(bdev);
Tejun Heod4d77622010-11-13 11:55:18 +0100910 WARN_ON_ONCE(!(mode & FMODE_EXCL));
Tejun Heoe525fd82010-11-13 11:55:17 +0100911 blkdev_put(bdev, mode | FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
914EXPORT_SYMBOL(kill_block_super);
David Howells93614012006-09-30 20:45:40 +0200915#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Al Viro3c26ff62010-07-25 11:46:36 +0400917struct dentry *mount_nodev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 int flags, void *data,
Al Viro3c26ff62010-07-25 11:46:36 +0400919 int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 int error;
922 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
923
924 if (IS_ERR(s))
Al Viro3c26ff62010-07-25 11:46:36 +0400925 return ERR_CAST(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 s->s_flags = flags;
928
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800929 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (error) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400931 deactivate_locked_super(s);
Al Viro3c26ff62010-07-25 11:46:36 +0400932 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934 s->s_flags |= MS_ACTIVE;
Al Viro3c26ff62010-07-25 11:46:36 +0400935 return dget(s->s_root);
936}
937EXPORT_SYMBOL(mount_nodev);
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939static int compare_single(struct super_block *s, void *p)
940{
941 return 1;
942}
943
Al Virofc14f2f2010-07-25 01:48:30 +0400944struct dentry *mount_single(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 int flags, void *data,
Al Virofc14f2f2010-07-25 01:48:30 +0400946 int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
948 struct super_block *s;
949 int error;
950
951 s = sget(fs_type, compare_single, set_anon_super, NULL);
952 if (IS_ERR(s))
Al Virofc14f2f2010-07-25 01:48:30 +0400953 return ERR_CAST(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (!s->s_root) {
955 s->s_flags = flags;
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800956 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (error) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400958 deactivate_locked_super(s);
Al Virofc14f2f2010-07-25 01:48:30 +0400959 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961 s->s_flags |= MS_ACTIVE;
Kay Sievers9329d1b2009-12-18 21:18:15 +0100962 } else {
963 do_remount_sb(s, flags, data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
Al Virofc14f2f2010-07-25 01:48:30 +0400965 return dget(s->s_root);
966}
967EXPORT_SYMBOL(mount_single);
968
Al Viro9d412a42011-03-17 22:08:28 -0400969struct dentry *
970mount_fs(struct file_system_type *type, int flags, const char *name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Al Viroc96e41e2010-07-25 00:17:56 +0400972 struct dentry *root;
Al Viro9d412a42011-03-17 22:08:28 -0400973 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 char *secdata = NULL;
Al Viro9d412a42011-03-17 22:08:28 -0400975 int error = -ENOMEM;
Al Viro80893522010-02-05 09:30:46 -0500976
Eric Parise0007522008-03-05 10:31:54 -0500977 if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 secdata = alloc_secdata();
David Howells454e2392006-06-23 02:02:57 -0700979 if (!secdata)
Al Viro9d412a42011-03-17 22:08:28 -0400980 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Eric Parise0007522008-03-05 10:31:54 -0500982 error = security_sb_copy_data(data, secdata);
David Howells454e2392006-06-23 02:02:57 -0700983 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 goto out_free_secdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986
Al Viro1a102ff2011-03-16 09:07:58 -0400987 root = type->mount(type, flags, name, data);
988 if (IS_ERR(root)) {
989 error = PTR_ERR(root);
990 goto out_free_secdata;
Al Viroc96e41e2010-07-25 00:17:56 +0400991 }
Al Viro9d412a42011-03-17 22:08:28 -0400992 sb = root->d_sb;
993 BUG_ON(!sb);
994 WARN_ON(!sb->s_bdi);
Linus Torvalds6c510382011-03-24 10:16:26 -0700995 WARN_ON(sb->s_bdi == &default_backing_dev_info);
Al Viro9d412a42011-03-17 22:08:28 -0400996 sb->s_flags |= MS_BORN;
David Howells454e2392006-06-23 02:02:57 -0700997
Al Viro9d412a42011-03-17 22:08:28 -0400998 error = security_sb_kern_mount(sb, flags, secdata);
Jörn Engel5129a462010-04-25 08:54:42 +0200999 if (error)
1000 goto out_sb;
David Howells454e2392006-06-23 02:02:57 -07001001
Jeff Layton42cb56a2009-09-18 13:05:53 -07001002 /*
1003 * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
1004 * but s_maxbytes was an unsigned long long for many releases. Throw
1005 * this warning for a little while to try and catch filesystems that
Jeff Layton4358b562011-03-29 09:33:31 -04001006 * violate this rule.
Jeff Layton42cb56a2009-09-18 13:05:53 -07001007 */
Al Viro9d412a42011-03-17 22:08:28 -04001008 WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
1009 "negative value (%lld)\n", type->name, sb->s_maxbytes);
Jeff Layton42cb56a2009-09-18 13:05:53 -07001010
Al Viro9d412a42011-03-17 22:08:28 -04001011 up_write(&sb->s_umount);
Gerald Schaefer8680e222005-06-21 17:15:16 -07001012 free_secdata(secdata);
Al Viro9d412a42011-03-17 22:08:28 -04001013 return root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014out_sb:
Al Viro9d412a42011-03-17 22:08:28 -04001015 dput(root);
1016 deactivate_locked_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017out_free_secdata:
1018 free_secdata(secdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019out:
David Howells454e2392006-06-23 02:02:57 -07001020 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
Josef Bacik18e9e512010-03-23 10:34:56 -04001023/**
Randy Dunlap7000d3c2010-05-24 22:22:34 -07001024 * freeze_super - lock the filesystem and force it into a consistent state
1025 * @sb: the super to lock
Josef Bacik18e9e512010-03-23 10:34:56 -04001026 *
1027 * Syncs the super to make sure the filesystem is consistent and calls the fs's
1028 * freeze_fs. Subsequent calls to this without first thawing the fs will return
1029 * -EBUSY.
1030 */
1031int freeze_super(struct super_block *sb)
1032{
1033 int ret;
1034
1035 atomic_inc(&sb->s_active);
1036 down_write(&sb->s_umount);
1037 if (sb->s_frozen) {
1038 deactivate_locked_super(sb);
1039 return -EBUSY;
1040 }
1041
1042 if (sb->s_flags & MS_RDONLY) {
1043 sb->s_frozen = SB_FREEZE_TRANS;
1044 smp_wmb();
1045 up_write(&sb->s_umount);
1046 return 0;
1047 }
1048
1049 sb->s_frozen = SB_FREEZE_WRITE;
1050 smp_wmb();
1051
1052 sync_filesystem(sb);
1053
1054 sb->s_frozen = SB_FREEZE_TRANS;
1055 smp_wmb();
1056
1057 sync_blockdev(sb->s_bdev);
1058 if (sb->s_op->freeze_fs) {
1059 ret = sb->s_op->freeze_fs(sb);
1060 if (ret) {
1061 printk(KERN_ERR
1062 "VFS:Filesystem freeze failed\n");
1063 sb->s_frozen = SB_UNFROZEN;
1064 deactivate_locked_super(sb);
1065 return ret;
1066 }
1067 }
1068 up_write(&sb->s_umount);
1069 return 0;
1070}
1071EXPORT_SYMBOL(freeze_super);
1072
1073/**
1074 * thaw_super -- unlock filesystem
1075 * @sb: the super to thaw
1076 *
1077 * Unlocks the filesystem and marks it writeable again after freeze_super().
1078 */
1079int thaw_super(struct super_block *sb)
1080{
1081 int error;
1082
1083 down_write(&sb->s_umount);
1084 if (sb->s_frozen == SB_UNFROZEN) {
1085 up_write(&sb->s_umount);
1086 return -EINVAL;
1087 }
1088
1089 if (sb->s_flags & MS_RDONLY)
1090 goto out;
1091
1092 if (sb->s_op->unfreeze_fs) {
1093 error = sb->s_op->unfreeze_fs(sb);
1094 if (error) {
1095 printk(KERN_ERR
1096 "VFS:Filesystem thaw failed\n");
1097 sb->s_frozen = SB_FREEZE_TRANS;
1098 up_write(&sb->s_umount);
1099 return error;
1100 }
1101 }
1102
1103out:
1104 sb->s_frozen = SB_UNFROZEN;
1105 smp_wmb();
1106 wake_up(&sb->s_wait_unfrozen);
1107 deactivate_locked_super(sb);
1108
1109 return 0;
1110}
1111EXPORT_SYMBOL(thaw_super);