]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/super.c
fs/partitions: use ADDPART_FLAG_RAID instead of magic number
[net-next-2.6.git] / fs / super.c
CommitLineData
1da177e4
LT
1/*
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:
96de0e25 18 * Torbjörn Lindh (torbjorn.lindh@gopta.se), April 14, 1996.
1da177e4
LT
19 * 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
1da177e4
LT
23#include <linux/module.h>
24#include <linux/slab.h>
1da177e4
LT
25#include <linux/acct.h>
26#include <linux/blkdev.h>
27#include <linux/quotaops.h>
1da177e4
LT
28#include <linux/mount.h>
29#include <linux/security.h>
1da177e4
LT
30#include <linux/writeback.h> /* for the emergency remount stuff */
31#include <linux/idr.h>
353ab6e9 32#include <linux/mutex.h>
5477d0fa 33#include <linux/backing-dev.h>
6d59e7f5 34#include "internal.h"
1da177e4
LT
35
36
1da177e4
LT
37LIST_HEAD(super_blocks);
38DEFINE_SPINLOCK(sb_lock);
39
40/**
41 * alloc_super - create new superblock
fe2bbc48 42 * @type: filesystem type superblock should belong to
1da177e4
LT
43 *
44 * Allocates and initializes a new &struct super_block. alloc_super()
45 * returns a pointer new superblock or %NULL if allocation had failed.
46 */
cf516249 47static struct super_block *alloc_super(struct file_system_type *type)
1da177e4 48{
11b0b5ab 49 struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
b87221de 50 static const struct super_operations default_op;
1da177e4
LT
51
52 if (s) {
1da177e4
LT
53 if (security_sb_alloc(s)) {
54 kfree(s);
55 s = NULL;
56 goto out;
57 }
1da177e4
LT
58 INIT_LIST_HEAD(&s->s_files);
59 INIT_LIST_HEAD(&s->s_instances);
60 INIT_HLIST_HEAD(&s->s_anon);
61 INIT_LIST_HEAD(&s->s_inodes);
da3bbdd4 62 INIT_LIST_HEAD(&s->s_dentry_lru);
1da177e4 63 init_rwsem(&s->s_umount);
7892f2f4 64 mutex_init(&s->s_lock);
897c6ff9 65 lockdep_set_class(&s->s_umount, &type->s_umount_key);
cf516249
IM
66 /*
67 * The locking rules for s_lock are up to the
68 * filesystem. For example ext3fs has different
69 * lock ordering than usbfs:
70 */
71 lockdep_set_class(&s->s_lock, &type->s_lock_key);
ada723dc
PZ
72 /*
73 * sget() can have s_umount recursion.
74 *
75 * When it cannot find a suitable sb, it allocates a new
76 * one (this one), and tries again to find a suitable old
77 * one.
78 *
79 * In case that succeeds, it will acquire the s_umount
80 * lock of the old one. Since these are clearly distrinct
81 * locks, and this object isn't exposed yet, there's no
82 * risk of deadlocks.
83 *
84 * Annotate this by putting this lock in a different
85 * subclass.
86 */
87 down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
b20bd1a5 88 s->s_count = 1;
1da177e4 89 atomic_set(&s->s_active, 1);
a11f3a05 90 mutex_init(&s->s_vfs_rename_mutex);
d3be915f
IM
91 mutex_init(&s->s_dquot.dqio_mutex);
92 mutex_init(&s->s_dquot.dqonoff_mutex);
1da177e4
LT
93 init_rwsem(&s->s_dquot.dqptr_sem);
94 init_waitqueue_head(&s->s_wait_unfrozen);
95 s->s_maxbytes = MAX_NON_LFS;
96 s->dq_op = sb_dquot_ops;
97 s->s_qcop = sb_quotactl_ops;
98 s->s_op = &default_op;
99 s->s_time_gran = 1000000000;
100 }
101out:
102 return s;
103}
104
105/**
106 * destroy_super - frees a superblock
107 * @s: superblock to free
108 *
109 * Frees a superblock.
110 */
111static inline void destroy_super(struct super_block *s)
112{
113 security_sb_free(s);
79c0b2df 114 kfree(s->s_subtype);
b3b304a2 115 kfree(s->s_options);
1da177e4
LT
116 kfree(s);
117}
118
119/* Superblock refcounting */
120
121/*
35cf7ba0 122 * Drop a superblock's refcount. The caller must hold sb_lock.
1da177e4 123 */
35cf7ba0 124void __put_super(struct super_block *sb)
1da177e4 125{
1da177e4 126 if (!--sb->s_count) {
551de6f3 127 list_del_init(&sb->s_list);
1da177e4 128 destroy_super(sb);
1da177e4 129 }
1da177e4
LT
130}
131
132/**
133 * put_super - drop a temporary reference to superblock
134 * @sb: superblock in question
135 *
136 * Drops a temporary reference, frees superblock if there's no
137 * references left.
138 */
03ba3782 139void put_super(struct super_block *sb)
1da177e4
LT
140{
141 spin_lock(&sb_lock);
142 __put_super(sb);
143 spin_unlock(&sb_lock);
144}
145
146
147/**
1712ac8f 148 * deactivate_locked_super - drop an active reference to superblock
1da177e4
LT
149 * @s: superblock to deactivate
150 *
1712ac8f
AV
151 * Drops an active reference to superblock, converting it into a temprory
152 * one if there is no other active references left. In that case we
1da177e4
LT
153 * tell fs driver to shut it down and drop the temporary reference we
154 * had just acquired.
1712ac8f
AV
155 *
156 * Caller holds exclusive lock on superblock; that lock is released.
1da177e4 157 */
1712ac8f 158void deactivate_locked_super(struct super_block *s)
1da177e4
LT
159{
160 struct file_system_type *fs = s->s_type;
b20bd1a5 161 if (atomic_dec_and_test(&s->s_active)) {
9e3509e2 162 vfs_dq_off(s, 0);
1da177e4
LT
163 fs->kill_sb(s);
164 put_filesystem(fs);
165 put_super(s);
1712ac8f
AV
166 } else {
167 up_write(&s->s_umount);
1da177e4
LT
168 }
169}
170
1712ac8f 171EXPORT_SYMBOL(deactivate_locked_super);
1da177e4 172
74dbbdd7 173/**
1712ac8f 174 * deactivate_super - drop an active reference to superblock
74dbbdd7
AV
175 * @s: superblock to deactivate
176 *
1712ac8f
AV
177 * Variant of deactivate_locked_super(), except that superblock is *not*
178 * locked by caller. If we are going to drop the final active reference,
179 * lock will be acquired prior to that.
74dbbdd7 180 */
1712ac8f 181void deactivate_super(struct super_block *s)
74dbbdd7 182{
1712ac8f
AV
183 if (!atomic_add_unless(&s->s_active, -1, 1)) {
184 down_write(&s->s_umount);
185 deactivate_locked_super(s);
74dbbdd7
AV
186 }
187}
188
1712ac8f 189EXPORT_SYMBOL(deactivate_super);
74dbbdd7 190
1da177e4
LT
191/**
192 * grab_super - acquire an active reference
193 * @s: reference we are trying to make active
194 *
195 * Tries to acquire an active reference. grab_super() is used when we
196 * had just found a superblock in super_blocks or fs_type->fs_supers
197 * and want to turn it into a full-blown active reference. grab_super()
198 * is called with sb_lock held and drops it. Returns 1 in case of
199 * success, 0 if we had failed (superblock contents was already dead or
200 * dying when grab_super() had been called).
201 */
9c4dbee7 202static int grab_super(struct super_block *s) __releases(sb_lock)
1da177e4 203{
b20bd1a5
AV
204 if (atomic_inc_not_zero(&s->s_active)) {
205 spin_unlock(&sb_lock);
b20bd1a5
AV
206 return 1;
207 }
208 /* it's going away */
1da177e4
LT
209 s->s_count++;
210 spin_unlock(&sb_lock);
1712ac8f 211 /* wait for it to die */
1da177e4 212 down_write(&s->s_umount);
1da177e4
LT
213 up_write(&s->s_umount);
214 put_super(s);
1da177e4
LT
215 return 0;
216}
217
914e2637
AV
218/*
219 * Superblock locking. We really ought to get rid of these two.
220 */
221void lock_super(struct super_block * sb)
222{
223 get_fs_excl();
224 mutex_lock(&sb->s_lock);
225}
226
227void unlock_super(struct super_block * sb)
228{
229 put_fs_excl();
230 mutex_unlock(&sb->s_lock);
231}
232
233EXPORT_SYMBOL(lock_super);
234EXPORT_SYMBOL(unlock_super);
235
1da177e4
LT
236/**
237 * generic_shutdown_super - common helper for ->kill_sb()
238 * @sb: superblock to kill
239 *
240 * generic_shutdown_super() does all fs-independent work on superblock
241 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
242 * that need destruction out of superblock, call generic_shutdown_super()
243 * and release aforementioned objects. Note: dentries and inodes _are_
244 * taken care of and do not need specific handling.
c636ebdb
DH
245 *
246 * Upon calling this function, the filesystem may no longer alter or
247 * rearrange the set of dentries belonging to this super_block, nor may it
248 * change the attachments of dentries to inodes.
1da177e4
LT
249 */
250void generic_shutdown_super(struct super_block *sb)
251{
ee9b6d61 252 const struct super_operations *sop = sb->s_op;
1da177e4 253
efaee192 254
c636ebdb
DH
255 if (sb->s_root) {
256 shrink_dcache_for_umount(sb);
60b0680f 257 sync_filesystem(sb);
a9e220f8 258 get_fs_excl();
1da177e4 259 sb->s_flags &= ~MS_ACTIVE;
efaee192 260
1da177e4
LT
261 /* bad name - it should be evict_inodes() */
262 invalidate_inodes(sb);
1da177e4 263
1da177e4
LT
264 if (sop->put_super)
265 sop->put_super(sb);
266
267 /* Forget any remaining inodes */
268 if (invalidate_inodes(sb)) {
7b4fe29e
DJ
269 printk("VFS: Busy inodes after unmount of %s. "
270 "Self-destruct in 5 seconds. Have a nice day...\n",
271 sb->s_id);
1da177e4 272 }
a9e220f8 273 put_fs_excl();
1da177e4
LT
274 }
275 spin_lock(&sb_lock);
276 /* should be initialized for __put_super_and_need_restart() */
551de6f3 277 list_del_init(&sb->s_instances);
1da177e4
LT
278 spin_unlock(&sb_lock);
279 up_write(&sb->s_umount);
280}
281
282EXPORT_SYMBOL(generic_shutdown_super);
283
284/**
285 * sget - find or create a superblock
286 * @type: filesystem type superblock should belong to
287 * @test: comparison callback
288 * @set: setup callback
289 * @data: argument to each of them
290 */
291struct super_block *sget(struct file_system_type *type,
292 int (*test)(struct super_block *,void *),
293 int (*set)(struct super_block *,void *),
294 void *data)
295{
296 struct super_block *s = NULL;
d4730127 297 struct super_block *old;
1da177e4
LT
298 int err;
299
300retry:
301 spin_lock(&sb_lock);
d4730127
MK
302 if (test) {
303 list_for_each_entry(old, &type->fs_supers, s_instances) {
304 if (!test(old, data))
305 continue;
306 if (!grab_super(old))
307 goto retry;
a3cfbb53
LZ
308 if (s) {
309 up_write(&s->s_umount);
d4730127 310 destroy_super(s);
a3cfbb53 311 }
d3f21473 312 down_write(&old->s_umount);
d4730127
MK
313 return old;
314 }
1da177e4
LT
315 }
316 if (!s) {
317 spin_unlock(&sb_lock);
cf516249 318 s = alloc_super(type);
1da177e4
LT
319 if (!s)
320 return ERR_PTR(-ENOMEM);
321 goto retry;
322 }
323
324 err = set(s, data);
325 if (err) {
326 spin_unlock(&sb_lock);
a3cfbb53 327 up_write(&s->s_umount);
1da177e4
LT
328 destroy_super(s);
329 return ERR_PTR(err);
330 }
331 s->s_type = type;
332 strlcpy(s->s_id, type->name, sizeof(s->s_id));
333 list_add_tail(&s->s_list, &super_blocks);
334 list_add(&s->s_instances, &type->fs_supers);
335 spin_unlock(&sb_lock);
336 get_filesystem(type);
337 return s;
338}
339
340EXPORT_SYMBOL(sget);
341
342void drop_super(struct super_block *sb)
343{
344 up_read(&sb->s_umount);
345 put_super(sb);
346}
347
348EXPORT_SYMBOL(drop_super);
349
e5004753
CH
350/**
351 * sync_supers - helper for periodic superblock writeback
352 *
353 * Call the write_super method if present on all dirty superblocks in
354 * the system. This is for the periodic writeback used by most older
355 * filesystems. For data integrity superblock writeback use
356 * sync_filesystems() instead.
357 *
1da177e4
LT
358 * Note: check the dirty flag before waiting, so we don't
359 * hold up the sync while mounting a device. (The newly
360 * mounted device won't need syncing.)
361 */
362void sync_supers(void)
363{
6754af64 364 struct super_block *sb, *n;
618f0636 365
1da177e4 366 spin_lock(&sb_lock);
6754af64 367 list_for_each_entry_safe(sb, n, &super_blocks, s_list) {
551de6f3
AV
368 if (list_empty(&sb->s_instances))
369 continue;
e5004753 370 if (sb->s_op->write_super && sb->s_dirt) {
1da177e4
LT
371 sb->s_count++;
372 spin_unlock(&sb_lock);
e5004753 373
1da177e4 374 down_read(&sb->s_umount);
e5004753
CH
375 if (sb->s_root && sb->s_dirt)
376 sb->s_op->write_super(sb);
618f0636 377 up_read(&sb->s_umount);
e5004753 378
618f0636 379 spin_lock(&sb_lock);
6754af64 380 __put_super(sb);
618f0636
KK
381 }
382 }
1da177e4
LT
383 spin_unlock(&sb_lock);
384}
385
01a05b33
AV
386/**
387 * iterate_supers - call function for all active superblocks
388 * @f: function to call
389 * @arg: argument to pass to it
390 *
391 * Scans the superblock list and calls given function, passing it
392 * locked superblock and given argument.
393 */
394void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
395{
396 struct super_block *sb, *n;
397
398 spin_lock(&sb_lock);
399 list_for_each_entry_safe(sb, n, &super_blocks, s_list) {
400 if (list_empty(&sb->s_instances))
401 continue;
402 sb->s_count++;
403 spin_unlock(&sb_lock);
404
405 down_read(&sb->s_umount);
406 if (sb->s_root)
407 f(sb, arg);
408 up_read(&sb->s_umount);
409
410 spin_lock(&sb_lock);
411 __put_super(sb);
412 }
413 spin_unlock(&sb_lock);
414}
415
1da177e4
LT
416/**
417 * get_super - get the superblock of a device
418 * @bdev: device to get the superblock for
419 *
420 * Scans the superblock list and finds the superblock of the file system
421 * mounted on the device given. %NULL is returned if no match is found.
422 */
423
df40c01a 424struct super_block *get_super(struct block_device *bdev)
1da177e4 425{
618f0636
KK
426 struct super_block *sb;
427
1da177e4
LT
428 if (!bdev)
429 return NULL;
618f0636 430
1da177e4 431 spin_lock(&sb_lock);
618f0636
KK
432rescan:
433 list_for_each_entry(sb, &super_blocks, s_list) {
551de6f3
AV
434 if (list_empty(&sb->s_instances))
435 continue;
618f0636
KK
436 if (sb->s_bdev == bdev) {
437 sb->s_count++;
1da177e4 438 spin_unlock(&sb_lock);
618f0636 439 down_read(&sb->s_umount);
df40c01a 440 /* still alive? */
618f0636
KK
441 if (sb->s_root)
442 return sb;
443 up_read(&sb->s_umount);
df40c01a 444 /* nope, got unmounted */
618f0636 445 spin_lock(&sb_lock);
df40c01a
AV
446 __put_super(sb);
447 goto rescan;
1da177e4
LT
448 }
449 }
450 spin_unlock(&sb_lock);
451 return NULL;
452}
453
454EXPORT_SYMBOL(get_super);
4504230a
CH
455
456/**
457 * get_active_super - get an active reference to the superblock of a device
458 * @bdev: device to get the superblock for
459 *
460 * Scans the superblock list and finds the superblock of the file system
461 * mounted on the device given. Returns the superblock with an active
d3f21473 462 * reference or %NULL if none was found.
4504230a
CH
463 */
464struct super_block *get_active_super(struct block_device *bdev)
465{
466 struct super_block *sb;
467
468 if (!bdev)
469 return NULL;
470
1494583d 471restart:
4504230a
CH
472 spin_lock(&sb_lock);
473 list_for_each_entry(sb, &super_blocks, s_list) {
551de6f3
AV
474 if (list_empty(&sb->s_instances))
475 continue;
1494583d
AV
476 if (sb->s_bdev == bdev) {
477 if (grab_super(sb)) /* drops sb_lock */
478 return sb;
479 else
480 goto restart;
481 }
4504230a
CH
482 }
483 spin_unlock(&sb_lock);
484 return NULL;
485}
1da177e4 486
df40c01a 487struct super_block *user_get_super(dev_t dev)
1da177e4 488{
618f0636 489 struct super_block *sb;
1da177e4 490
1da177e4 491 spin_lock(&sb_lock);
618f0636
KK
492rescan:
493 list_for_each_entry(sb, &super_blocks, s_list) {
551de6f3
AV
494 if (list_empty(&sb->s_instances))
495 continue;
618f0636
KK
496 if (sb->s_dev == dev) {
497 sb->s_count++;
1da177e4 498 spin_unlock(&sb_lock);
618f0636 499 down_read(&sb->s_umount);
df40c01a 500 /* still alive? */
618f0636
KK
501 if (sb->s_root)
502 return sb;
503 up_read(&sb->s_umount);
df40c01a 504 /* nope, got unmounted */
618f0636 505 spin_lock(&sb_lock);
df40c01a
AV
506 __put_super(sb);
507 goto rescan;
1da177e4
LT
508 }
509 }
510 spin_unlock(&sb_lock);
511 return NULL;
512}
513
1da177e4
LT
514/**
515 * do_remount_sb - asks filesystem to change mount options.
516 * @sb: superblock in question
517 * @flags: numeric part of options
518 * @data: the rest of options
519 * @force: whether or not to force the change
520 *
521 * Alters the mount options of a mounted file system.
522 */
523int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
524{
525 int retval;
d208bbdd 526 int remount_rw, remount_ro;
4504230a
CH
527
528 if (sb->s_frozen != SB_UNFROZEN)
529 return -EBUSY;
530
9361401e 531#ifdef CONFIG_BLOCK
1da177e4
LT
532 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
533 return -EACCES;
9361401e 534#endif
4504230a 535
1da177e4
LT
536 if (flags & MS_RDONLY)
537 acct_auto_close(sb);
538 shrink_dcache_sb(sb);
60b0680f 539 sync_filesystem(sb);
1da177e4 540
d208bbdd
NP
541 remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
542 remount_rw = !(flags & MS_RDONLY) && (sb->s_flags & MS_RDONLY);
543
1da177e4
LT
544 /* If we are remounting RDONLY and current sb is read/write,
545 make sure there are no rw files opened */
d208bbdd 546 if (remount_ro) {
1da177e4
LT
547 if (force)
548 mark_files_ro(sb);
b0895513 549 else if (!fs_may_remount_ro(sb))
1da177e4 550 return -EBUSY;
9e3509e2 551 retval = vfs_dq_off(sb, 1);
b0895513 552 if (retval < 0 && retval != -ENOSYS)
0ff5af83 553 return -EBUSY;
1da177e4
LT
554 }
555
556 if (sb->s_op->remount_fs) {
1da177e4 557 retval = sb->s_op->remount_fs(sb, &flags, data);
b0895513 558 if (retval)
1da177e4
LT
559 return retval;
560 }
561 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
0ff5af83 562 if (remount_rw)
9e3509e2 563 vfs_dq_quota_on_remount(sb);
d208bbdd
NP
564 /*
565 * Some filesystems modify their metadata via some other path than the
566 * bdev buffer cache (eg. use a private mapping, or directories in
567 * pagecache, etc). Also file data modifications go via their own
568 * mappings. So If we try to mount readonly then copy the filesystem
569 * from bdev, we could get stale data, so invalidate it to give a best
570 * effort at coherency.
571 */
572 if (remount_ro && sb->s_bdev)
573 invalidate_bdev(sb->s_bdev);
1da177e4
LT
574 return 0;
575}
576
a2a9537a 577static void do_emergency_remount(struct work_struct *work)
1da177e4 578{
e7fe0585 579 struct super_block *sb, *n;
1da177e4
LT
580
581 spin_lock(&sb_lock);
e7fe0585 582 list_for_each_entry_safe(sb, n, &super_blocks, s_list) {
551de6f3
AV
583 if (list_empty(&sb->s_instances))
584 continue;
1da177e4
LT
585 sb->s_count++;
586 spin_unlock(&sb_lock);
443b94ba 587 down_write(&sb->s_umount);
1da177e4
LT
588 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
589 /*
1da177e4
LT
590 * What lock protects sb->s_flags??
591 */
1da177e4 592 do_remount_sb(sb, MS_RDONLY, NULL, 1);
1da177e4 593 }
443b94ba 594 up_write(&sb->s_umount);
1da177e4 595 spin_lock(&sb_lock);
e7fe0585 596 __put_super(sb);
1da177e4
LT
597 }
598 spin_unlock(&sb_lock);
a2a9537a 599 kfree(work);
1da177e4
LT
600 printk("Emergency Remount complete\n");
601}
602
603void emergency_remount(void)
604{
a2a9537a
JA
605 struct work_struct *work;
606
607 work = kmalloc(sizeof(*work), GFP_ATOMIC);
608 if (work) {
609 INIT_WORK(work, do_emergency_remount);
610 schedule_work(work);
611 }
1da177e4
LT
612}
613
614/*
615 * Unnamed block devices are dummy devices used by virtual
616 * filesystems which don't use real block-devices. -- jrs
617 */
618
ad76cbc6 619static DEFINE_IDA(unnamed_dev_ida);
1da177e4 620static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
c63e09ec 621static int unnamed_dev_start = 0; /* don't bother trying below it */
1da177e4
LT
622
623int set_anon_super(struct super_block *s, void *data)
624{
625 int dev;
626 int error;
627
628 retry:
ad76cbc6 629 if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
1da177e4
LT
630 return -ENOMEM;
631 spin_lock(&unnamed_dev_lock);
c63e09ec 632 error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
f21f6220
AV
633 if (!error)
634 unnamed_dev_start = dev + 1;
1da177e4
LT
635 spin_unlock(&unnamed_dev_lock);
636 if (error == -EAGAIN)
637 /* We raced and lost with another CPU. */
638 goto retry;
639 else if (error)
640 return -EAGAIN;
641
642 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
643 spin_lock(&unnamed_dev_lock);
ad76cbc6 644 ida_remove(&unnamed_dev_ida, dev);
f21f6220
AV
645 if (unnamed_dev_start > dev)
646 unnamed_dev_start = dev;
1da177e4
LT
647 spin_unlock(&unnamed_dev_lock);
648 return -EMFILE;
649 }
650 s->s_dev = MKDEV(0, dev & MINORMASK);
5129a469 651 s->s_bdi = &noop_backing_dev_info;
1da177e4
LT
652 return 0;
653}
654
655EXPORT_SYMBOL(set_anon_super);
656
657void kill_anon_super(struct super_block *sb)
658{
659 int slot = MINOR(sb->s_dev);
660
661 generic_shutdown_super(sb);
662 spin_lock(&unnamed_dev_lock);
ad76cbc6 663 ida_remove(&unnamed_dev_ida, slot);
c63e09ec
AV
664 if (slot < unnamed_dev_start)
665 unnamed_dev_start = slot;
1da177e4
LT
666 spin_unlock(&unnamed_dev_lock);
667}
668
669EXPORT_SYMBOL(kill_anon_super);
670
1da177e4
LT
671void kill_litter_super(struct super_block *sb)
672{
673 if (sb->s_root)
674 d_genocide(sb->s_root);
675 kill_anon_super(sb);
676}
677
678EXPORT_SYMBOL(kill_litter_super);
679
909e6d94
SH
680static int ns_test_super(struct super_block *sb, void *data)
681{
682 return sb->s_fs_info == data;
683}
684
685static int ns_set_super(struct super_block *sb, void *data)
686{
687 sb->s_fs_info = data;
688 return set_anon_super(sb, NULL);
689}
690
691int get_sb_ns(struct file_system_type *fs_type, int flags, void *data,
692 int (*fill_super)(struct super_block *, void *, int),
693 struct vfsmount *mnt)
694{
695 struct super_block *sb;
696
697 sb = sget(fs_type, ns_test_super, ns_set_super, data);
698 if (IS_ERR(sb))
699 return PTR_ERR(sb);
700
701 if (!sb->s_root) {
702 int err;
703 sb->s_flags = flags;
704 err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
705 if (err) {
74dbbdd7 706 deactivate_locked_super(sb);
909e6d94
SH
707 return err;
708 }
709
710 sb->s_flags |= MS_ACTIVE;
711 }
712
713 simple_set_mnt(mnt, sb);
714 return 0;
715}
716
717EXPORT_SYMBOL(get_sb_ns);
718
9361401e 719#ifdef CONFIG_BLOCK
1da177e4
LT
720static int set_bdev_super(struct super_block *s, void *data)
721{
722 s->s_bdev = data;
723 s->s_dev = s->s_bdev->bd_dev;
32a88aa1
JA
724
725 /*
726 * We set the bdi here to the queue backing, file systems can
727 * overwrite this in ->fill_super()
728 */
729 s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
1da177e4
LT
730 return 0;
731}
732
733static int test_bdev_super(struct super_block *s, void *data)
734{
735 return (void *)s->s_bdev == data;
736}
737
454e2398 738int get_sb_bdev(struct file_system_type *fs_type,
1da177e4 739 int flags, const char *dev_name, void *data,
454e2398
DH
740 int (*fill_super)(struct super_block *, void *, int),
741 struct vfsmount *mnt)
1da177e4
LT
742{
743 struct block_device *bdev;
744 struct super_block *s;
30c40d2c 745 fmode_t mode = FMODE_READ;
1da177e4
LT
746 int error = 0;
747
30c40d2c
AV
748 if (!(flags & MS_RDONLY))
749 mode |= FMODE_WRITE;
750
751 bdev = open_bdev_exclusive(dev_name, mode, fs_type);
1da177e4 752 if (IS_ERR(bdev))
454e2398 753 return PTR_ERR(bdev);
1da177e4
LT
754
755 /*
756 * once the super is inserted into the list by sget, s_umount
757 * will protect the lockfs code from trying to start a snapshot
758 * while we are mounting
759 */
4fadd7bb
CH
760 mutex_lock(&bdev->bd_fsfreeze_mutex);
761 if (bdev->bd_fsfreeze_count > 0) {
762 mutex_unlock(&bdev->bd_fsfreeze_mutex);
763 error = -EBUSY;
764 goto error_bdev;
765 }
1da177e4 766 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
4fadd7bb 767 mutex_unlock(&bdev->bd_fsfreeze_mutex);
1da177e4 768 if (IS_ERR(s))
454e2398 769 goto error_s;
1da177e4
LT
770
771 if (s->s_root) {
772 if ((flags ^ s->s_flags) & MS_RDONLY) {
74dbbdd7 773 deactivate_locked_super(s);
454e2398
DH
774 error = -EBUSY;
775 goto error_bdev;
1da177e4 776 }
454e2398 777
30c40d2c 778 close_bdev_exclusive(bdev, mode);
1da177e4
LT
779 } else {
780 char b[BDEVNAME_SIZE];
781
782 s->s_flags = flags;
30c40d2c 783 s->s_mode = mode;
1da177e4 784 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
e78c9a00 785 sb_set_blocksize(s, block_size(bdev));
9b04c997 786 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
1da177e4 787 if (error) {
74dbbdd7 788 deactivate_locked_super(s);
454e2398 789 goto error;
fa675765 790 }
454e2398
DH
791
792 s->s_flags |= MS_ACTIVE;
87d8fe1e 793 bdev->bd_super = s;
1da177e4
LT
794 }
795
a3ec947c
SB
796 simple_set_mnt(mnt, s);
797 return 0;
1da177e4 798
454e2398
DH
799error_s:
800 error = PTR_ERR(s);
801error_bdev:
30c40d2c 802 close_bdev_exclusive(bdev, mode);
454e2398
DH
803error:
804 return error;
1da177e4
LT
805}
806
807EXPORT_SYMBOL(get_sb_bdev);
808
809void kill_block_super(struct super_block *sb)
810{
811 struct block_device *bdev = sb->s_bdev;
30c40d2c 812 fmode_t mode = sb->s_mode;
1da177e4 813
ddbaaf30 814 bdev->bd_super = NULL;
1da177e4
LT
815 generic_shutdown_super(sb);
816 sync_blockdev(bdev);
30c40d2c 817 close_bdev_exclusive(bdev, mode);
1da177e4
LT
818}
819
820EXPORT_SYMBOL(kill_block_super);
9361401e 821#endif
1da177e4 822
454e2398 823int get_sb_nodev(struct file_system_type *fs_type,
1da177e4 824 int flags, void *data,
454e2398
DH
825 int (*fill_super)(struct super_block *, void *, int),
826 struct vfsmount *mnt)
1da177e4
LT
827{
828 int error;
829 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
830
831 if (IS_ERR(s))
454e2398 832 return PTR_ERR(s);
1da177e4
LT
833
834 s->s_flags = flags;
835
9b04c997 836 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
1da177e4 837 if (error) {
74dbbdd7 838 deactivate_locked_super(s);
454e2398 839 return error;
1da177e4
LT
840 }
841 s->s_flags |= MS_ACTIVE;
a3ec947c
SB
842 simple_set_mnt(mnt, s);
843 return 0;
1da177e4
LT
844}
845
846EXPORT_SYMBOL(get_sb_nodev);
847
848static int compare_single(struct super_block *s, void *p)
849{
850 return 1;
851}
852
454e2398 853int get_sb_single(struct file_system_type *fs_type,
1da177e4 854 int flags, void *data,
454e2398
DH
855 int (*fill_super)(struct super_block *, void *, int),
856 struct vfsmount *mnt)
1da177e4
LT
857{
858 struct super_block *s;
859 int error;
860
861 s = sget(fs_type, compare_single, set_anon_super, NULL);
862 if (IS_ERR(s))
454e2398 863 return PTR_ERR(s);
1da177e4
LT
864 if (!s->s_root) {
865 s->s_flags = flags;
9b04c997 866 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
1da177e4 867 if (error) {
74dbbdd7 868 deactivate_locked_super(s);
454e2398 869 return error;
1da177e4
LT
870 }
871 s->s_flags |= MS_ACTIVE;
9329d1be
KS
872 } else {
873 do_remount_sb(s, flags, data, 0);
1da177e4 874 }
a3ec947c
SB
875 simple_set_mnt(mnt, s);
876 return 0;
1da177e4
LT
877}
878
879EXPORT_SYMBOL(get_sb_single);
880
881struct vfsmount *
bb4a58bf 882vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
1da177e4 883{
1da177e4 884 struct vfsmount *mnt;
1da177e4 885 char *secdata = NULL;
454e2398 886 int error;
1da177e4
LT
887
888 if (!type)
889 return ERR_PTR(-ENODEV);
1da177e4 890
454e2398 891 error = -ENOMEM;
1da177e4
LT
892 mnt = alloc_vfsmnt(name);
893 if (!mnt)
894 goto out;
895
8089352a
AV
896 if (flags & MS_KERNMOUNT)
897 mnt->mnt_flags = MNT_INTERNAL;
898
e0007529 899 if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
1da177e4 900 secdata = alloc_secdata();
454e2398 901 if (!secdata)
1da177e4 902 goto out_mnt;
1da177e4 903
e0007529 904 error = security_sb_copy_data(data, secdata);
454e2398 905 if (error)
1da177e4 906 goto out_free_secdata;
1da177e4
LT
907 }
908
454e2398
DH
909 error = type->get_sb(type, flags, name, data, mnt);
910 if (error < 0)
1da177e4 911 goto out_free_secdata;
b4c07bce 912 BUG_ON(!mnt->mnt_sb);
5129a469 913 WARN_ON(!mnt->mnt_sb->s_bdi);
454e2398 914
5129a469
JE
915 error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
916 if (error)
917 goto out_sb;
454e2398 918
42cb56ae
JL
919 /*
920 * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
921 * but s_maxbytes was an unsigned long long for many releases. Throw
922 * this warning for a little while to try and catch filesystems that
923 * violate this rule. This warning should be either removed or
924 * converted to a BUG() in 2.6.34.
925 */
926 WARN((mnt->mnt_sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
927 "negative value (%lld)\n", type->name, mnt->mnt_sb->s_maxbytes);
928
454e2398 929 mnt->mnt_mountpoint = mnt->mnt_root;
1da177e4 930 mnt->mnt_parent = mnt;
454e2398 931 up_write(&mnt->mnt_sb->s_umount);
8680e22f 932 free_secdata(secdata);
1da177e4
LT
933 return mnt;
934out_sb:
454e2398 935 dput(mnt->mnt_root);
74dbbdd7 936 deactivate_locked_super(mnt->mnt_sb);
1da177e4
LT
937out_free_secdata:
938 free_secdata(secdata);
939out_mnt:
940 free_vfsmnt(mnt);
941out:
454e2398 942 return ERR_PTR(error);
1da177e4
LT
943}
944
bb4a58bf
TM
945EXPORT_SYMBOL_GPL(vfs_kern_mount);
946
18e9e510
JB
947/**
948 * freeze_super -- lock the filesystem and force it into a consistent state
949 * @super: the super to lock
950 *
951 * Syncs the super to make sure the filesystem is consistent and calls the fs's
952 * freeze_fs. Subsequent calls to this without first thawing the fs will return
953 * -EBUSY.
954 */
955int freeze_super(struct super_block *sb)
956{
957 int ret;
958
959 atomic_inc(&sb->s_active);
960 down_write(&sb->s_umount);
961 if (sb->s_frozen) {
962 deactivate_locked_super(sb);
963 return -EBUSY;
964 }
965
966 if (sb->s_flags & MS_RDONLY) {
967 sb->s_frozen = SB_FREEZE_TRANS;
968 smp_wmb();
969 up_write(&sb->s_umount);
970 return 0;
971 }
972
973 sb->s_frozen = SB_FREEZE_WRITE;
974 smp_wmb();
975
976 sync_filesystem(sb);
977
978 sb->s_frozen = SB_FREEZE_TRANS;
979 smp_wmb();
980
981 sync_blockdev(sb->s_bdev);
982 if (sb->s_op->freeze_fs) {
983 ret = sb->s_op->freeze_fs(sb);
984 if (ret) {
985 printk(KERN_ERR
986 "VFS:Filesystem freeze failed\n");
987 sb->s_frozen = SB_UNFROZEN;
988 deactivate_locked_super(sb);
989 return ret;
990 }
991 }
992 up_write(&sb->s_umount);
993 return 0;
994}
995EXPORT_SYMBOL(freeze_super);
996
997/**
998 * thaw_super -- unlock filesystem
999 * @sb: the super to thaw
1000 *
1001 * Unlocks the filesystem and marks it writeable again after freeze_super().
1002 */
1003int thaw_super(struct super_block *sb)
1004{
1005 int error;
1006
1007 down_write(&sb->s_umount);
1008 if (sb->s_frozen == SB_UNFROZEN) {
1009 up_write(&sb->s_umount);
1010 return -EINVAL;
1011 }
1012
1013 if (sb->s_flags & MS_RDONLY)
1014 goto out;
1015
1016 if (sb->s_op->unfreeze_fs) {
1017 error = sb->s_op->unfreeze_fs(sb);
1018 if (error) {
1019 printk(KERN_ERR
1020 "VFS:Filesystem thaw failed\n");
1021 sb->s_frozen = SB_FREEZE_TRANS;
1022 up_write(&sb->s_umount);
1023 return error;
1024 }
1025 }
1026
1027out:
1028 sb->s_frozen = SB_UNFROZEN;
1029 smp_wmb();
1030 wake_up(&sb->s_wait_unfrozen);
1031 deactivate_locked_super(sb);
1032
1033 return 0;
1034}
1035EXPORT_SYMBOL(thaw_super);
1036
79c0b2df
MS
1037static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
1038{
1039 int err;
1040 const char *subtype = strchr(fstype, '.');
1041 if (subtype) {
1042 subtype++;
1043 err = -EINVAL;
1044 if (!subtype[0])
1045 goto err;
1046 } else
1047 subtype = "";
1048
1049 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
1050 err = -ENOMEM;
1051 if (!mnt->mnt_sb->s_subtype)
1052 goto err;
1053 return mnt;
1054
1055 err:
1056 mntput(mnt);
1057 return ERR_PTR(err);
1058}
1059
bb4a58bf
TM
1060struct vfsmount *
1061do_kern_mount(const char *fstype, int flags, const char *name, void *data)
1062{
1063 struct file_system_type *type = get_fs_type(fstype);
1064 struct vfsmount *mnt;
1065 if (!type)
1066 return ERR_PTR(-ENODEV);
1067 mnt = vfs_kern_mount(type, flags, name, data);
79c0b2df
MS
1068 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
1069 !mnt->mnt_sb->s_subtype)
1070 mnt = fs_set_subtype(mnt, fstype);
bb4a58bf
TM
1071 put_filesystem(type);
1072 return mnt;
1073}
8a4e98d9 1074EXPORT_SYMBOL_GPL(do_kern_mount);
bb4a58bf 1075
8bf9725c 1076struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
1da177e4 1077{
8bf9725c 1078 return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
1da177e4
LT
1079}
1080
8bf9725c 1081EXPORT_SYMBOL_GPL(kern_mount_data);