blob: 263edeb9f0e93e8504f1744d33a6ff8e6782c1d7 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 init_rwsem(&s->s_umount);
Ingo Molnar7892f2f2006-01-09 15:59:25 -080081 mutex_init(&s->s_lock);
Arjan van de Ven897c6ff2006-07-03 00:25:28 -070082 lockdep_set_class(&s->s_umount, &type->s_umount_key);
Ingo Molnarcf516242006-07-03 00:25:27 -070083 /*
84 * The locking rules for s_lock are up to the
85 * filesystem. For example ext3fs has different
86 * lock ordering than usbfs:
87 */
88 lockdep_set_class(&s->s_lock, &type->s_lock_key);
Peter Zijlstraada723d2009-02-18 14:48:30 -080089 /*
90 * sget() can have s_umount recursion.
91 *
92 * When it cannot find a suitable sb, it allocates a new
93 * one (this one), and tries again to find a suitable old
94 * one.
95 *
96 * In case that succeeds, it will acquire the s_umount
97 * lock of the old one. Since these are clearly distrinct
98 * locks, and this object isn't exposed yet, there's no
99 * risk of deadlocks.
100 *
101 * Annotate this by putting this lock in a different
102 * subclass.
103 */
104 down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
Al Virob20bd1a2010-03-22 08:53:19 -0400105 s->s_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 atomic_set(&s->s_active, 1);
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800107 mutex_init(&s->s_vfs_rename_mutex);
Roland Dreier51ee0492010-04-27 14:23:57 -0700108 lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
Ingo Molnard3be9152006-03-23 03:00:29 -0800109 mutex_init(&s->s_dquot.dqio_mutex);
110 mutex_init(&s->s_dquot.dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 init_rwsem(&s->s_dquot.dqptr_sem);
112 init_waitqueue_head(&s->s_wait_unfrozen);
113 s->s_maxbytes = MAX_NON_LFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 s->s_op = &default_op;
115 s->s_time_gran = 1000000000;
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600116 s->cleancache_poolid = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118out:
119 return s;
120}
121
122/**
123 * destroy_super - frees a superblock
124 * @s: superblock to free
125 *
126 * Frees a superblock.
127 */
128static inline void destroy_super(struct super_block *s)
129{
Nick Piggin6416ccb2010-08-18 04:37:38 +1000130#ifdef CONFIG_SMP
131 free_percpu(s->s_files);
132#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 security_sb_free(s);
Miklos Szeredi79c0b2d2007-05-08 00:25:43 -0700134 kfree(s->s_subtype);
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800135 kfree(s->s_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 kfree(s);
137}
138
139/* Superblock refcounting */
140
141/*
Al Viro35cf7ba2010-03-22 21:13:53 -0400142 * Drop a superblock's refcount. The caller must hold sb_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 */
Al Viro35cf7ba2010-03-22 21:13:53 -0400144void __put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (!--sb->s_count) {
Al Viro551de6f2010-03-22 19:36:35 -0400147 list_del_init(&sb->s_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 destroy_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152/**
153 * put_super - drop a temporary reference to superblock
154 * @sb: superblock in question
155 *
156 * Drops a temporary reference, frees superblock if there's no
157 * references left.
158 */
Jens Axboe03ba3782009-09-09 09:08:54 +0200159void put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 spin_lock(&sb_lock);
162 __put_super(sb);
163 spin_unlock(&sb_lock);
164}
165
166
167/**
Al Viro74dbbdd2009-05-06 01:07:50 -0400168 * deactivate_locked_super - drop an active reference to superblock
169 * @s: superblock to deactivate
170 *
Al Viro1712ac82010-03-22 15:22:31 -0400171 * Drops an active reference to superblock, converting it into a temprory
172 * one if there is no other active references left. In that case we
173 * tell fs driver to shut it down and drop the temporary reference we
174 * had just acquired.
175 *
176 * Caller holds exclusive lock on superblock; that lock is released.
Al Viro74dbbdd2009-05-06 01:07:50 -0400177 */
178void deactivate_locked_super(struct super_block *s)
179{
180 struct file_system_type *fs = s->s_type;
Al Virob20bd1a2010-03-22 08:53:19 -0400181 if (atomic_dec_and_test(&s->s_active)) {
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600182 cleancache_flush_fs(s);
Al Viro74dbbdd2009-05-06 01:07:50 -0400183 fs->kill_sb(s);
Boaz Harroshd863b502011-02-10 15:01:20 -0800184 /*
185 * We need to call rcu_barrier so all the delayed rcu free
186 * inodes are flushed before we release the fs module.
187 */
188 rcu_barrier();
Al Viro74dbbdd2009-05-06 01:07:50 -0400189 put_filesystem(fs);
190 put_super(s);
191 } else {
192 up_write(&s->s_umount);
193 }
194}
195
196EXPORT_SYMBOL(deactivate_locked_super);
197
198/**
Al Viro1712ac82010-03-22 15:22:31 -0400199 * deactivate_super - drop an active reference to superblock
200 * @s: superblock to deactivate
201 *
202 * Variant of deactivate_locked_super(), except that superblock is *not*
203 * locked by caller. If we are going to drop the final active reference,
204 * lock will be acquired prior to that.
205 */
206void deactivate_super(struct super_block *s)
207{
208 if (!atomic_add_unless(&s->s_active, -1, 1)) {
209 down_write(&s->s_umount);
210 deactivate_locked_super(s);
211 }
212}
213
214EXPORT_SYMBOL(deactivate_super);
215
216/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 * grab_super - acquire an active reference
218 * @s: reference we are trying to make active
219 *
220 * Tries to acquire an active reference. grab_super() is used when we
221 * had just found a superblock in super_blocks or fs_type->fs_supers
222 * and want to turn it into a full-blown active reference. grab_super()
223 * is called with sb_lock held and drops it. Returns 1 in case of
224 * success, 0 if we had failed (superblock contents was already dead or
225 * dying when grab_super() had been called).
226 */
Josh Triplett9c4dbee2006-09-29 01:59:29 -0700227static int grab_super(struct super_block *s) __releases(sb_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Al Virob20bd1a2010-03-22 08:53:19 -0400229 if (atomic_inc_not_zero(&s->s_active)) {
230 spin_unlock(&sb_lock);
Al Virob20bd1a2010-03-22 08:53:19 -0400231 return 1;
232 }
233 /* it's going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 s->s_count++;
235 spin_unlock(&sb_lock);
Al Viro1712ac82010-03-22 15:22:31 -0400236 /* wait for it to die */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 down_write(&s->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 up_write(&s->s_umount);
239 put_super(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return 0;
241}
242
David Howellscf9a2ae2006-08-29 19:05:54 +0100243/*
Al Viro914e2632006-10-18 13:55:46 -0400244 * Superblock locking. We really ought to get rid of these two.
245 */
246void lock_super(struct super_block * sb)
247{
248 get_fs_excl();
249 mutex_lock(&sb->s_lock);
250}
251
252void unlock_super(struct super_block * sb)
253{
254 put_fs_excl();
255 mutex_unlock(&sb->s_lock);
256}
257
258EXPORT_SYMBOL(lock_super);
259EXPORT_SYMBOL(unlock_super);
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261/**
262 * generic_shutdown_super - common helper for ->kill_sb()
263 * @sb: superblock to kill
264 *
265 * generic_shutdown_super() does all fs-independent work on superblock
266 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
267 * that need destruction out of superblock, call generic_shutdown_super()
268 * and release aforementioned objects. Note: dentries and inodes _are_
269 * taken care of and do not need specific handling.
David Howellsc636ebd2006-10-11 01:22:19 -0700270 *
271 * Upon calling this function, the filesystem may no longer alter or
272 * rearrange the set of dentries belonging to this super_block, nor may it
273 * change the attachments of dentries to inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 */
275void generic_shutdown_super(struct super_block *sb)
276{
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800277 const struct super_operations *sop = sb->s_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Arjan van de Venefaee192009-01-06 07:20:54 -0800279
David Howellsc636ebd2006-10-11 01:22:19 -0700280 if (sb->s_root) {
281 shrink_dcache_for_umount(sb);
Jan Kara60b06802009-04-27 16:43:53 +0200282 sync_filesystem(sb);
Al Viroa9e220f2009-05-05 22:10:44 -0400283 get_fs_excl();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 sb->s_flags &= ~MS_ACTIVE;
Arjan van de Venefaee192009-01-06 07:20:54 -0800285
Al Viro63997e92010-10-25 20:49:35 -0400286 fsnotify_unmount_inodes(&sb->s_inodes);
287
288 evict_inodes(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (sop->put_super)
291 sop->put_super(sb);
292
Al Viro63997e92010-10-25 20:49:35 -0400293 if (!list_empty(&sb->s_inodes)) {
Dave Jones7b4fe292006-02-07 12:58:48 -0800294 printk("VFS: Busy inodes after unmount of %s. "
295 "Self-destruct in 5 seconds. Have a nice day...\n",
296 sb->s_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
Al Viroa9e220f2009-05-05 22:10:44 -0400298 put_fs_excl();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300 spin_lock(&sb_lock);
301 /* should be initialized for __put_super_and_need_restart() */
Al Viro551de6f2010-03-22 19:36:35 -0400302 list_del_init(&sb->s_instances);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 spin_unlock(&sb_lock);
304 up_write(&sb->s_umount);
305}
306
307EXPORT_SYMBOL(generic_shutdown_super);
308
309/**
310 * sget - find or create a superblock
311 * @type: filesystem type superblock should belong to
312 * @test: comparison callback
313 * @set: setup callback
314 * @data: argument to each of them
315 */
316struct super_block *sget(struct file_system_type *type,
317 int (*test)(struct super_block *,void *),
318 int (*set)(struct super_block *,void *),
319 void *data)
320{
321 struct super_block *s = NULL;
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700322 struct super_block *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 int err;
324
325retry:
326 spin_lock(&sb_lock);
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700327 if (test) {
328 list_for_each_entry(old, &type->fs_supers, s_instances) {
329 if (!test(old, data))
330 continue;
331 if (!grab_super(old))
332 goto retry;
Li Zefana3cfbb52009-03-12 14:31:29 -0700333 if (s) {
334 up_write(&s->s_umount);
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700335 destroy_super(s);
Al Viro7a4dec52010-08-09 12:05:43 -0400336 s = NULL;
Li Zefana3cfbb52009-03-12 14:31:29 -0700337 }
Al Virod3f21472010-03-23 11:11:05 -0400338 down_write(&old->s_umount);
Al Viro7a4dec52010-08-09 12:05:43 -0400339 if (unlikely(!(old->s_flags & MS_BORN))) {
340 deactivate_locked_super(old);
341 goto retry;
342 }
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700343 return old;
344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346 if (!s) {
347 spin_unlock(&sb_lock);
Ingo Molnarcf516242006-07-03 00:25:27 -0700348 s = alloc_super(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (!s)
350 return ERR_PTR(-ENOMEM);
351 goto retry;
352 }
353
354 err = set(s, data);
355 if (err) {
356 spin_unlock(&sb_lock);
Li Zefana3cfbb52009-03-12 14:31:29 -0700357 up_write(&s->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 destroy_super(s);
359 return ERR_PTR(err);
360 }
361 s->s_type = type;
362 strlcpy(s->s_id, type->name, sizeof(s->s_id));
363 list_add_tail(&s->s_list, &super_blocks);
364 list_add(&s->s_instances, &type->fs_supers);
365 spin_unlock(&sb_lock);
366 get_filesystem(type);
367 return s;
368}
369
370EXPORT_SYMBOL(sget);
371
372void drop_super(struct super_block *sb)
373{
374 up_read(&sb->s_umount);
375 put_super(sb);
376}
377
378EXPORT_SYMBOL(drop_super);
379
Christoph Hellwige5004752009-05-05 16:08:56 +0200380/**
381 * sync_supers - helper for periodic superblock writeback
382 *
383 * Call the write_super method if present on all dirty superblocks in
384 * the system. This is for the periodic writeback used by most older
385 * filesystems. For data integrity superblock writeback use
386 * sync_filesystems() instead.
387 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 * Note: check the dirty flag before waiting, so we don't
389 * hold up the sync while mounting a device. (The newly
390 * mounted device won't need syncing.)
391 */
392void sync_supers(void)
393{
Al Virodca33252010-07-25 02:31:46 +0400394 struct super_block *sb, *p = NULL;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400397 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400398 if (list_empty(&sb->s_instances))
399 continue;
Christoph Hellwige5004752009-05-05 16:08:56 +0200400 if (sb->s_op->write_super && sb->s_dirt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 sb->s_count++;
402 spin_unlock(&sb_lock);
Christoph Hellwige5004752009-05-05 16:08:56 +0200403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 down_read(&sb->s_umount);
Christoph Hellwige5004752009-05-05 16:08:56 +0200405 if (sb->s_root && sb->s_dirt)
406 sb->s_op->write_super(sb);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700407 up_read(&sb->s_umount);
Christoph Hellwige5004752009-05-05 16:08:56 +0200408
Kirill Korotaev618f0632005-06-23 00:09:54 -0700409 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400410 if (p)
411 __put_super(p);
412 p = sb;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700413 }
414 }
Al Virodca33252010-07-25 02:31:46 +0400415 if (p)
416 __put_super(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 spin_unlock(&sb_lock);
418}
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/**
Al Viro01a05b32010-03-23 06:06:58 -0400421 * iterate_supers - call function for all active superblocks
422 * @f: function to call
423 * @arg: argument to pass to it
424 *
425 * Scans the superblock list and calls given function, passing it
426 * locked superblock and given argument.
427 */
428void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
429{
Al Virodca33252010-07-25 02:31:46 +0400430 struct super_block *sb, *p = NULL;
Al Viro01a05b32010-03-23 06:06:58 -0400431
432 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400433 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro01a05b32010-03-23 06:06:58 -0400434 if (list_empty(&sb->s_instances))
435 continue;
436 sb->s_count++;
437 spin_unlock(&sb_lock);
438
439 down_read(&sb->s_umount);
440 if (sb->s_root)
441 f(sb, arg);
442 up_read(&sb->s_umount);
443
444 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400445 if (p)
446 __put_super(p);
447 p = sb;
Al Viro01a05b32010-03-23 06:06:58 -0400448 }
Al Virodca33252010-07-25 02:31:46 +0400449 if (p)
450 __put_super(p);
Al Viro01a05b32010-03-23 06:06:58 -0400451 spin_unlock(&sb_lock);
452}
453
454/**
Al Viro43e15cd2011-06-03 20:16:57 -0400455 * iterate_supers_type - call function for superblocks of given type
456 * @type: fs type
457 * @f: function to call
458 * @arg: argument to pass to it
459 *
460 * Scans the superblock list and calls given function, passing it
461 * locked superblock and given argument.
462 */
463void iterate_supers_type(struct file_system_type *type,
464 void (*f)(struct super_block *, void *), void *arg)
465{
466 struct super_block *sb, *p = NULL;
467
468 spin_lock(&sb_lock);
469 list_for_each_entry(sb, &type->fs_supers, s_instances) {
470 sb->s_count++;
471 spin_unlock(&sb_lock);
472
473 down_read(&sb->s_umount);
474 if (sb->s_root)
475 f(sb, arg);
476 up_read(&sb->s_umount);
477
478 spin_lock(&sb_lock);
479 if (p)
480 __put_super(p);
481 p = sb;
482 }
483 if (p)
484 __put_super(p);
485 spin_unlock(&sb_lock);
486}
487
488EXPORT_SYMBOL(iterate_supers_type);
489
490/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 * get_super - get the superblock of a device
492 * @bdev: device to get the superblock for
493 *
494 * Scans the superblock list and finds the superblock of the file system
495 * mounted on the device given. %NULL is returned if no match is found.
496 */
497
Al Virodf40c012010-03-22 20:23:25 -0400498struct super_block *get_super(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700500 struct super_block *sb;
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (!bdev)
503 return NULL;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700506rescan:
507 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400508 if (list_empty(&sb->s_instances))
509 continue;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700510 if (sb->s_bdev == bdev) {
511 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700513 down_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400514 /* still alive? */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700515 if (sb->s_root)
516 return sb;
517 up_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400518 /* nope, got unmounted */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700519 spin_lock(&sb_lock);
Al Virodf40c012010-03-22 20:23:25 -0400520 __put_super(sb);
521 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523 }
524 spin_unlock(&sb_lock);
525 return NULL;
526}
527
528EXPORT_SYMBOL(get_super);
Christoph Hellwig45042302009-08-03 23:28:35 +0200529
530/**
531 * get_active_super - get an active reference to the superblock of a device
532 * @bdev: device to get the superblock for
533 *
534 * Scans the superblock list and finds the superblock of the file system
535 * mounted on the device given. Returns the superblock with an active
Al Virod3f21472010-03-23 11:11:05 -0400536 * reference or %NULL if none was found.
Christoph Hellwig45042302009-08-03 23:28:35 +0200537 */
538struct super_block *get_active_super(struct block_device *bdev)
539{
540 struct super_block *sb;
541
542 if (!bdev)
543 return NULL;
544
Al Viro14945832010-03-22 20:15:33 -0400545restart:
Christoph Hellwig45042302009-08-03 23:28:35 +0200546 spin_lock(&sb_lock);
547 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400548 if (list_empty(&sb->s_instances))
549 continue;
Al Viro14945832010-03-22 20:15:33 -0400550 if (sb->s_bdev == bdev) {
551 if (grab_super(sb)) /* drops sb_lock */
552 return sb;
553 else
554 goto restart;
555 }
Christoph Hellwig45042302009-08-03 23:28:35 +0200556 }
557 spin_unlock(&sb_lock);
558 return NULL;
559}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Al Virodf40c012010-03-22 20:23:25 -0400561struct super_block *user_get_super(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700563 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700566rescan:
567 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400568 if (list_empty(&sb->s_instances))
569 continue;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700570 if (sb->s_dev == dev) {
571 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700573 down_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400574 /* still alive? */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700575 if (sb->s_root)
576 return sb;
577 up_read(&sb->s_umount);
Al Virodf40c012010-03-22 20:23:25 -0400578 /* nope, got unmounted */
Kirill Korotaev618f0632005-06-23 00:09:54 -0700579 spin_lock(&sb_lock);
Al Virodf40c012010-03-22 20:23:25 -0400580 __put_super(sb);
581 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583 }
584 spin_unlock(&sb_lock);
585 return NULL;
586}
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * do_remount_sb - asks filesystem to change mount options.
590 * @sb: superblock in question
591 * @flags: numeric part of options
592 * @data: the rest of options
593 * @force: whether or not to force the change
594 *
595 * Alters the mount options of a mounted file system.
596 */
597int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
598{
599 int retval;
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400600 int remount_ro;
Christoph Hellwig45042302009-08-03 23:28:35 +0200601
602 if (sb->s_frozen != SB_UNFROZEN)
603 return -EBUSY;
604
David Howells93614012006-09-30 20:45:40 +0200605#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
607 return -EACCES;
David Howells93614012006-09-30 20:45:40 +0200608#endif
Christoph Hellwig45042302009-08-03 23:28:35 +0200609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (flags & MS_RDONLY)
611 acct_auto_close(sb);
612 shrink_dcache_sb(sb);
Jan Kara60b06802009-04-27 16:43:53 +0200613 sync_filesystem(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Nick Piggind208bbd2009-12-21 16:28:53 -0800615 remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
Nick Piggind208bbd2009-12-21 16:28:53 -0800616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* If we are remounting RDONLY and current sb is read/write,
618 make sure there are no rw files opened */
Nick Piggind208bbd2009-12-21 16:28:53 -0800619 if (remount_ro) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (force)
621 mark_files_ro(sb);
J. R. Okajimab0895512009-06-17 01:16:50 +0900622 else if (!fs_may_remount_ro(sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return -EBUSY;
624 }
625
626 if (sb->s_op->remount_fs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 retval = sb->s_op->remount_fs(sb, &flags, data);
J. R. Okajimab0895512009-06-17 01:16:50 +0900628 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return retval;
630 }
631 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400632
Nick Piggind208bbd2009-12-21 16:28:53 -0800633 /*
634 * Some filesystems modify their metadata via some other path than the
635 * bdev buffer cache (eg. use a private mapping, or directories in
636 * pagecache, etc). Also file data modifications go via their own
637 * mappings. So If we try to mount readonly then copy the filesystem
638 * from bdev, we could get stale data, so invalidate it to give a best
639 * effort at coherency.
640 */
641 if (remount_ro && sb->s_bdev)
642 invalidate_bdev(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return 0;
644}
645
Jens Axboea2a95372009-03-17 09:38:40 +0100646static void do_emergency_remount(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Al Virodca33252010-07-25 02:31:46 +0400648 struct super_block *sb, *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400651 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400652 if (list_empty(&sb->s_instances))
653 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 sb->s_count++;
655 spin_unlock(&sb_lock);
Al Viro443b94b2009-05-05 23:48:50 -0400656 down_write(&sb->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
658 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 * What lock protects sb->s_flags??
660 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 do_remount_sb(sb, MS_RDONLY, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
Al Viro443b94b2009-05-05 23:48:50 -0400663 up_write(&sb->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400665 if (p)
666 __put_super(p);
667 p = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
Al Virodca33252010-07-25 02:31:46 +0400669 if (p)
670 __put_super(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 spin_unlock(&sb_lock);
Jens Axboea2a95372009-03-17 09:38:40 +0100672 kfree(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 printk("Emergency Remount complete\n");
674}
675
676void emergency_remount(void)
677{
Jens Axboea2a95372009-03-17 09:38:40 +0100678 struct work_struct *work;
679
680 work = kmalloc(sizeof(*work), GFP_ATOMIC);
681 if (work) {
682 INIT_WORK(work, do_emergency_remount);
683 schedule_work(work);
684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687/*
688 * Unnamed block devices are dummy devices used by virtual
689 * filesystems which don't use real block-devices. -- jrs
690 */
691
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400692static DEFINE_IDA(unnamed_dev_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
Al Viroc63e09e2009-06-24 02:05:18 -0400694static int unnamed_dev_start = 0; /* don't bother trying below it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Al Viro0ee5dc62011-07-07 15:44:25 -0400696int get_anon_bdev(dev_t *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 int dev;
699 int error;
700
701 retry:
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400702 if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return -ENOMEM;
704 spin_lock(&unnamed_dev_lock);
Al Viroc63e09e2009-06-24 02:05:18 -0400705 error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
Al Virof21f6222009-06-24 03:12:00 -0400706 if (!error)
707 unnamed_dev_start = dev + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 spin_unlock(&unnamed_dev_lock);
709 if (error == -EAGAIN)
710 /* We raced and lost with another CPU. */
711 goto retry;
712 else if (error)
713 return -EAGAIN;
714
715 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
716 spin_lock(&unnamed_dev_lock);
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400717 ida_remove(&unnamed_dev_ida, dev);
Al Virof21f6222009-06-24 03:12:00 -0400718 if (unnamed_dev_start > dev)
719 unnamed_dev_start = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 spin_unlock(&unnamed_dev_lock);
721 return -EMFILE;
722 }
Al Viro0ee5dc62011-07-07 15:44:25 -0400723 *p = MKDEV(0, dev & MINORMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return 0;
725}
Al Viro0ee5dc62011-07-07 15:44:25 -0400726EXPORT_SYMBOL(get_anon_bdev);
727
728void free_anon_bdev(dev_t dev)
729{
730 int slot = MINOR(dev);
731 spin_lock(&unnamed_dev_lock);
732 ida_remove(&unnamed_dev_ida, slot);
733 if (slot < unnamed_dev_start)
734 unnamed_dev_start = slot;
735 spin_unlock(&unnamed_dev_lock);
736}
737EXPORT_SYMBOL(free_anon_bdev);
738
739int set_anon_super(struct super_block *s, void *data)
740{
741 int error = get_anon_bdev(&s->s_dev);
742 if (!error)
743 s->s_bdi = &noop_backing_dev_info;
744 return error;
745}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747EXPORT_SYMBOL(set_anon_super);
748
749void kill_anon_super(struct super_block *sb)
750{
Al Viro0ee5dc62011-07-07 15:44:25 -0400751 dev_t dev = sb->s_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 generic_shutdown_super(sb);
Al Viro0ee5dc62011-07-07 15:44:25 -0400753 free_anon_bdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756EXPORT_SYMBOL(kill_anon_super);
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758void kill_litter_super(struct super_block *sb)
759{
760 if (sb->s_root)
761 d_genocide(sb->s_root);
762 kill_anon_super(sb);
763}
764
765EXPORT_SYMBOL(kill_litter_super);
766
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700767static int ns_test_super(struct super_block *sb, void *data)
768{
769 return sb->s_fs_info == data;
770}
771
772static int ns_set_super(struct super_block *sb, void *data)
773{
774 sb->s_fs_info = data;
775 return set_anon_super(sb, NULL);
776}
777
Al Viroceefda62010-07-26 13:16:50 +0400778struct dentry *mount_ns(struct file_system_type *fs_type, int flags,
779 void *data, int (*fill_super)(struct super_block *, void *, int))
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700780{
781 struct super_block *sb;
782
783 sb = sget(fs_type, ns_test_super, ns_set_super, data);
784 if (IS_ERR(sb))
Al Viroceefda62010-07-26 13:16:50 +0400785 return ERR_CAST(sb);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700786
787 if (!sb->s_root) {
788 int err;
789 sb->s_flags = flags;
790 err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
791 if (err) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400792 deactivate_locked_super(sb);
Al Viroceefda62010-07-26 13:16:50 +0400793 return ERR_PTR(err);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700794 }
795
796 sb->s_flags |= MS_ACTIVE;
797 }
798
Al Viroceefda62010-07-26 13:16:50 +0400799 return dget(sb->s_root);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700800}
801
Al Viroceefda62010-07-26 13:16:50 +0400802EXPORT_SYMBOL(mount_ns);
Serge E. Hallyn909e6d92009-04-06 19:01:07 -0700803
David Howells93614012006-09-30 20:45:40 +0200804#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805static int set_bdev_super(struct super_block *s, void *data)
806{
807 s->s_bdev = data;
808 s->s_dev = s->s_bdev->bd_dev;
Jens Axboe32a88aa2009-09-16 15:02:33 +0200809
810 /*
811 * We set the bdi here to the queue backing, file systems can
812 * overwrite this in ->fill_super()
813 */
814 s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return 0;
816}
817
818static int test_bdev_super(struct super_block *s, void *data)
819{
820 return (void *)s->s_bdev == data;
821}
822
Al Viro152a0832010-07-25 00:46:55 +0400823struct dentry *mount_bdev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 int flags, const char *dev_name, void *data,
Al Viro152a0832010-07-25 00:46:55 +0400825 int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 struct block_device *bdev;
828 struct super_block *s;
Tejun Heod4d77622010-11-13 11:55:18 +0100829 fmode_t mode = FMODE_READ | FMODE_EXCL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 int error = 0;
831
Al Viro30c40d22008-02-22 19:50:45 -0500832 if (!(flags & MS_RDONLY))
833 mode |= FMODE_WRITE;
834
Tejun Heod4d77622010-11-13 11:55:18 +0100835 bdev = blkdev_get_by_path(dev_name, mode, fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (IS_ERR(bdev))
Al Viro152a0832010-07-25 00:46:55 +0400837 return ERR_CAST(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 /*
840 * once the super is inserted into the list by sget, s_umount
841 * will protect the lockfs code from trying to start a snapshot
842 * while we are mounting
843 */
Christoph Hellwig4fadd7b2009-08-03 23:28:06 +0200844 mutex_lock(&bdev->bd_fsfreeze_mutex);
845 if (bdev->bd_fsfreeze_count > 0) {
846 mutex_unlock(&bdev->bd_fsfreeze_mutex);
847 error = -EBUSY;
848 goto error_bdev;
849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
Christoph Hellwig4fadd7b2009-08-03 23:28:06 +0200851 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700853 goto error_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 if (s->s_root) {
856 if ((flags ^ s->s_flags) & MS_RDONLY) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400857 deactivate_locked_super(s);
David Howells454e2392006-06-23 02:02:57 -0700858 error = -EBUSY;
859 goto error_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
David Howells454e2392006-06-23 02:02:57 -0700861
Tejun Heo4f331f02010-07-20 15:18:07 -0700862 /*
863 * s_umount nests inside bd_mutex during
Tejun Heoe525fd82010-11-13 11:55:17 +0100864 * __invalidate_device(). blkdev_put() acquires
865 * bd_mutex and can't be called under s_umount. Drop
866 * s_umount temporarily. This is safe as we're
867 * holding an active reference.
Tejun Heo4f331f02010-07-20 15:18:07 -0700868 */
869 up_write(&s->s_umount);
Tejun Heod4d77622010-11-13 11:55:18 +0100870 blkdev_put(bdev, mode);
Tejun Heo4f331f02010-07-20 15:18:07 -0700871 down_write(&s->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 } else {
873 char b[BDEVNAME_SIZE];
874
Al Viro9e1f1de2011-06-03 18:24:58 -0400875 s->s_flags = flags | MS_NOSEC;
Al Viro30c40d22008-02-22 19:50:45 -0500876 s->s_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
Pekka Enberge78c9a02006-01-08 01:03:39 -0800878 sb_set_blocksize(s, block_size(bdev));
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800879 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (error) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400881 deactivate_locked_super(s);
David Howells454e2392006-06-23 02:02:57 -0700882 goto error;
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800883 }
David Howells454e2392006-06-23 02:02:57 -0700884
885 s->s_flags |= MS_ACTIVE;
Theodore Ts'o87d8fe12009-01-03 09:47:09 -0500886 bdev->bd_super = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888
Al Viro152a0832010-07-25 00:46:55 +0400889 return dget(s->s_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
David Howells454e2392006-06-23 02:02:57 -0700891error_s:
892 error = PTR_ERR(s);
893error_bdev:
Tejun Heod4d77622010-11-13 11:55:18 +0100894 blkdev_put(bdev, mode);
David Howells454e2392006-06-23 02:02:57 -0700895error:
Al Viro152a0832010-07-25 00:46:55 +0400896 return ERR_PTR(error);
897}
898EXPORT_SYMBOL(mount_bdev);
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900void kill_block_super(struct super_block *sb)
901{
902 struct block_device *bdev = sb->s_bdev;
Al Viro30c40d22008-02-22 19:50:45 -0500903 fmode_t mode = sb->s_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
H Hartley Sweetenddbaaf32009-04-29 20:14:57 -0400905 bdev->bd_super = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 generic_shutdown_super(sb);
907 sync_blockdev(bdev);
Tejun Heod4d77622010-11-13 11:55:18 +0100908 WARN_ON_ONCE(!(mode & FMODE_EXCL));
Tejun Heoe525fd82010-11-13 11:55:17 +0100909 blkdev_put(bdev, mode | FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
912EXPORT_SYMBOL(kill_block_super);
David Howells93614012006-09-30 20:45:40 +0200913#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Al Viro3c26ff62010-07-25 11:46:36 +0400915struct dentry *mount_nodev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 int flags, void *data,
Al Viro3c26ff62010-07-25 11:46:36 +0400917 int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 int error;
920 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
921
922 if (IS_ERR(s))
Al Viro3c26ff62010-07-25 11:46:36 +0400923 return ERR_CAST(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 s->s_flags = flags;
926
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800927 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (error) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400929 deactivate_locked_super(s);
Al Viro3c26ff62010-07-25 11:46:36 +0400930 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932 s->s_flags |= MS_ACTIVE;
Al Viro3c26ff62010-07-25 11:46:36 +0400933 return dget(s->s_root);
934}
935EXPORT_SYMBOL(mount_nodev);
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937static int compare_single(struct super_block *s, void *p)
938{
939 return 1;
940}
941
Al Virofc14f2f2010-07-25 01:48:30 +0400942struct dentry *mount_single(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 int flags, void *data,
Al Virofc14f2f2010-07-25 01:48:30 +0400944 int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 struct super_block *s;
947 int error;
948
949 s = sget(fs_type, compare_single, set_anon_super, NULL);
950 if (IS_ERR(s))
Al Virofc14f2f2010-07-25 01:48:30 +0400951 return ERR_CAST(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (!s->s_root) {
953 s->s_flags = flags;
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800954 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (error) {
Al Viro74dbbdd2009-05-06 01:07:50 -0400956 deactivate_locked_super(s);
Al Virofc14f2f2010-07-25 01:48:30 +0400957 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959 s->s_flags |= MS_ACTIVE;
Kay Sievers9329d1b2009-12-18 21:18:15 +0100960 } else {
961 do_remount_sb(s, flags, data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
Al Virofc14f2f2010-07-25 01:48:30 +0400963 return dget(s->s_root);
964}
965EXPORT_SYMBOL(mount_single);
966
Al Viro9d412a42011-03-17 22:08:28 -0400967struct dentry *
968mount_fs(struct file_system_type *type, int flags, const char *name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Al Viroc96e41e2010-07-25 00:17:56 +0400970 struct dentry *root;
Al Viro9d412a42011-03-17 22:08:28 -0400971 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 char *secdata = NULL;
Al Viro9d412a42011-03-17 22:08:28 -0400973 int error = -ENOMEM;
Al Viro80893522010-02-05 09:30:46 -0500974
Eric Parise0007522008-03-05 10:31:54 -0500975 if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 secdata = alloc_secdata();
David Howells454e2392006-06-23 02:02:57 -0700977 if (!secdata)
Al Viro9d412a42011-03-17 22:08:28 -0400978 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Eric Parise0007522008-03-05 10:31:54 -0500980 error = security_sb_copy_data(data, secdata);
David Howells454e2392006-06-23 02:02:57 -0700981 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 goto out_free_secdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984
Al Viro1a102ff2011-03-16 09:07:58 -0400985 root = type->mount(type, flags, name, data);
986 if (IS_ERR(root)) {
987 error = PTR_ERR(root);
988 goto out_free_secdata;
Al Viroc96e41e2010-07-25 00:17:56 +0400989 }
Al Viro9d412a42011-03-17 22:08:28 -0400990 sb = root->d_sb;
991 BUG_ON(!sb);
992 WARN_ON(!sb->s_bdi);
Linus Torvalds6c510382011-03-24 10:16:26 -0700993 WARN_ON(sb->s_bdi == &default_backing_dev_info);
Al Viro9d412a42011-03-17 22:08:28 -0400994 sb->s_flags |= MS_BORN;
David Howells454e2392006-06-23 02:02:57 -0700995
Al Viro9d412a42011-03-17 22:08:28 -0400996 error = security_sb_kern_mount(sb, flags, secdata);
Jörn Engel5129a462010-04-25 08:54:42 +0200997 if (error)
998 goto out_sb;
David Howells454e2392006-06-23 02:02:57 -0700999
Jeff Layton42cb56a2009-09-18 13:05:53 -07001000 /*
1001 * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
1002 * but s_maxbytes was an unsigned long long for many releases. Throw
1003 * this warning for a little while to try and catch filesystems that
Jeff Layton4358b562011-03-29 09:33:31 -04001004 * violate this rule.
Jeff Layton42cb56a2009-09-18 13:05:53 -07001005 */
Al Viro9d412a42011-03-17 22:08:28 -04001006 WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
1007 "negative value (%lld)\n", type->name, sb->s_maxbytes);
Jeff Layton42cb56a2009-09-18 13:05:53 -07001008
Al Viro9d412a42011-03-17 22:08:28 -04001009 up_write(&sb->s_umount);
Gerald Schaefer8680e222005-06-21 17:15:16 -07001010 free_secdata(secdata);
Al Viro9d412a42011-03-17 22:08:28 -04001011 return root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012out_sb:
Al Viro9d412a42011-03-17 22:08:28 -04001013 dput(root);
1014 deactivate_locked_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015out_free_secdata:
1016 free_secdata(secdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017out:
David Howells454e2392006-06-23 02:02:57 -07001018 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
Josef Bacik18e9e512010-03-23 10:34:56 -04001021/**
Randy Dunlap7000d3c2010-05-24 22:22:34 -07001022 * freeze_super - lock the filesystem and force it into a consistent state
1023 * @sb: the super to lock
Josef Bacik18e9e512010-03-23 10:34:56 -04001024 *
1025 * Syncs the super to make sure the filesystem is consistent and calls the fs's
1026 * freeze_fs. Subsequent calls to this without first thawing the fs will return
1027 * -EBUSY.
1028 */
1029int freeze_super(struct super_block *sb)
1030{
1031 int ret;
1032
1033 atomic_inc(&sb->s_active);
1034 down_write(&sb->s_umount);
1035 if (sb->s_frozen) {
1036 deactivate_locked_super(sb);
1037 return -EBUSY;
1038 }
1039
1040 if (sb->s_flags & MS_RDONLY) {
1041 sb->s_frozen = SB_FREEZE_TRANS;
1042 smp_wmb();
1043 up_write(&sb->s_umount);
1044 return 0;
1045 }
1046
1047 sb->s_frozen = SB_FREEZE_WRITE;
1048 smp_wmb();
1049
1050 sync_filesystem(sb);
1051
1052 sb->s_frozen = SB_FREEZE_TRANS;
1053 smp_wmb();
1054
1055 sync_blockdev(sb->s_bdev);
1056 if (sb->s_op->freeze_fs) {
1057 ret = sb->s_op->freeze_fs(sb);
1058 if (ret) {
1059 printk(KERN_ERR
1060 "VFS:Filesystem freeze failed\n");
1061 sb->s_frozen = SB_UNFROZEN;
1062 deactivate_locked_super(sb);
1063 return ret;
1064 }
1065 }
1066 up_write(&sb->s_umount);
1067 return 0;
1068}
1069EXPORT_SYMBOL(freeze_super);
1070
1071/**
1072 * thaw_super -- unlock filesystem
1073 * @sb: the super to thaw
1074 *
1075 * Unlocks the filesystem and marks it writeable again after freeze_super().
1076 */
1077int thaw_super(struct super_block *sb)
1078{
1079 int error;
1080
1081 down_write(&sb->s_umount);
1082 if (sb->s_frozen == SB_UNFROZEN) {
1083 up_write(&sb->s_umount);
1084 return -EINVAL;
1085 }
1086
1087 if (sb->s_flags & MS_RDONLY)
1088 goto out;
1089
1090 if (sb->s_op->unfreeze_fs) {
1091 error = sb->s_op->unfreeze_fs(sb);
1092 if (error) {
1093 printk(KERN_ERR
1094 "VFS:Filesystem thaw failed\n");
1095 sb->s_frozen = SB_FREEZE_TRANS;
1096 up_write(&sb->s_umount);
1097 return error;
1098 }
1099 }
1100
1101out:
1102 sb->s_frozen = SB_UNFROZEN;
1103 smp_wmb();
1104 wake_up(&sb->s_wait_unfrozen);
1105 deactivate_locked_super(sb);
1106
1107 return 0;
1108}
1109EXPORT_SYMBOL(thaw_super);