code style scripts/checkpatch.pl (linux-3.9-rc1) formatting
[linux-2.6.34.14-moxart.git] / fs / super.c
blob7123fa728af7ab840c655cfb8f50d20f9e7486a8
1 /*
2 * linux/fs/super.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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
13 * GK 2/5/95 - Changed to support mounting the root fs via NFS
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:
18 * Torbjörn Lindh (torbjorn.lindh@gopta.se), April 14, 1996.
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
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <linux/smp_lock.h>
27 #include <linux/acct.h>
28 #include <linux/blkdev.h>
29 #include <linux/quotaops.h>
30 #include <linux/namei.h>
31 #include <linux/mount.h>
32 #include <linux/security.h>
33 #include <linux/syscalls.h>
34 #include <linux/vfs.h>
35 #include <linux/writeback.h> /* for the emergency remount stuff */
36 #include <linux/idr.h>
37 #include <linux/kobject.h>
38 #include <linux/mutex.h>
39 #include <linux/file.h>
40 #include <linux/backing-dev.h>
41 #include <asm/uaccess.h>
42 #include "internal.h"
45 LIST_HEAD(super_blocks);
46 DEFINE_SPINLOCK(sb_lock);
48 /**
49 * alloc_super - create new superblock
50 * @type: filesystem type superblock should belong to
52 * Allocates and initializes a new &struct super_block. alloc_super()
53 * returns a pointer new superblock or %NULL if allocation had failed.
55 static struct super_block *alloc_super(struct file_system_type *type)
57 struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
58 static const struct super_operations default_op;
60 if (s) {
61 if (security_sb_alloc(s)) {
62 kfree(s);
63 s = NULL;
64 goto out;
66 INIT_LIST_HEAD(&s->s_files);
67 s->s_bdi = &default_backing_dev_info;
68 INIT_LIST_HEAD(&s->s_instances);
69 INIT_HLIST_HEAD(&s->s_anon);
70 INIT_LIST_HEAD(&s->s_inodes);
71 INIT_LIST_HEAD(&s->s_dentry_lru);
72 init_rwsem(&s->s_umount);
73 mutex_init(&s->s_lock);
74 lockdep_set_class(&s->s_umount, &type->s_umount_key);
76 * The locking rules for s_lock are up to the
77 * filesystem. For example ext3fs has different
78 * lock ordering than usbfs:
80 lockdep_set_class(&s->s_lock, &type->s_lock_key);
82 * sget() can have s_umount recursion.
84 * When it cannot find a suitable sb, it allocates a new
85 * one (this one), and tries again to find a suitable old
86 * one.
88 * In case that succeeds, it will acquire the s_umount
89 * lock of the old one. Since these are clearly distrinct
90 * locks, and this object isn't exposed yet, there's no
91 * risk of deadlocks.
93 * Annotate this by putting this lock in a different
94 * subclass.
96 down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
97 s->s_count = S_BIAS;
98 atomic_set(&s->s_active, 1);
99 mutex_init(&s->s_vfs_rename_mutex);
100 mutex_init(&s->s_dquot.dqio_mutex);
101 mutex_init(&s->s_dquot.dqonoff_mutex);
102 init_rwsem(&s->s_dquot.dqptr_sem);
103 init_waitqueue_head(&s->s_wait_unfrozen);
104 s->s_maxbytes = MAX_NON_LFS;
105 s->dq_op = sb_dquot_ops;
106 s->s_qcop = sb_quotactl_ops;
107 s->s_op = &default_op;
108 s->s_time_gran = 1000000000;
110 out:
111 return s;
115 * destroy_super - frees a superblock
116 * @s: superblock to free
118 * Frees a superblock.
120 static inline void destroy_super(struct super_block *s)
122 security_sb_free(s);
123 kfree(s->s_subtype);
124 kfree(s->s_options);
125 kfree(s);
128 /* Superblock refcounting */
131 * Drop a superblock's refcount. Returns non-zero if the superblock was
132 * destroyed. The caller must hold sb_lock.
134 static int __put_super(struct super_block *sb)
136 int ret = 0;
138 if (!--sb->s_count) {
139 destroy_super(sb);
140 ret = 1;
142 return ret;
146 * Drop a superblock's refcount.
147 * Returns non-zero if the superblock is about to be destroyed and
148 * at least is already removed from super_blocks list, so if we are
149 * making a loop through super blocks then we need to restart.
150 * The caller must hold sb_lock.
152 int __put_super_and_need_restart(struct super_block *sb)
154 /* check for race with generic_shutdown_super() */
155 if (list_empty(&sb->s_list)) {
156 /* super block is removed, need to restart... */
157 __put_super(sb);
158 return 1;
160 /* can't be the last, since s_list is still in use */
161 sb->s_count--;
162 BUG_ON(sb->s_count == 0);
163 return 0;
167 * put_super - drop a temporary reference to superblock
168 * @sb: superblock in question
170 * Drops a temporary reference, frees superblock if there's no
171 * references left.
173 void put_super(struct super_block *sb)
175 spin_lock(&sb_lock);
176 __put_super(sb);
177 spin_unlock(&sb_lock);
182 * deactivate_super - drop an active reference to superblock
183 * @s: superblock to deactivate
185 * Drops an active reference to superblock, acquiring a temprory one if
186 * there is no active references left. In that case we lock superblock,
187 * tell fs driver to shut it down and drop the temporary reference we
188 * had just acquired.
190 void deactivate_super(struct super_block *s)
192 struct file_system_type *fs = s->s_type;
193 if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
194 s->s_count -= S_BIAS-1;
195 spin_unlock(&sb_lock);
196 vfs_dq_off(s, 0);
197 down_write(&s->s_umount);
198 fs->kill_sb(s);
199 put_filesystem(fs);
200 put_super(s);
204 EXPORT_SYMBOL(deactivate_super);
207 * deactivate_locked_super - drop an active reference to superblock
208 * @s: superblock to deactivate
210 * Equivalent of up_write(&s->s_umount); deactivate_super(s);, except that
211 * it does not unlock it until it's all over. As the result, it's safe to
212 * use to dispose of new superblock on ->get_sb() failure exits - nobody
213 * will see the sucker until it's all over. Equivalent using up_write +
214 * deactivate_super is safe for that purpose only if superblock is either
215 * safe to use or has NULL ->s_root when we unlock.
217 void deactivate_locked_super(struct super_block *s)
219 struct file_system_type *fs = s->s_type;
220 if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
221 s->s_count -= S_BIAS-1;
222 spin_unlock(&sb_lock);
223 vfs_dq_off(s, 0);
224 fs->kill_sb(s);
225 put_filesystem(fs);
226 put_super(s);
227 } else {
228 up_write(&s->s_umount);
232 EXPORT_SYMBOL(deactivate_locked_super);
235 * grab_super - acquire an active reference
236 * @s: reference we are trying to make active
238 * Tries to acquire an active reference. grab_super() is used when we
239 * had just found a superblock in super_blocks or fs_type->fs_supers
240 * and want to turn it into a full-blown active reference. grab_super()
241 * is called with sb_lock held and drops it. Returns 1 in case of
242 * success, 0 if we had failed (superblock contents was already dead or
243 * dying when grab_super() had been called).
245 static int grab_super(struct super_block *s) __releases(sb_lock)
247 s->s_count++;
248 spin_unlock(&sb_lock);
249 down_write(&s->s_umount);
250 if (s->s_root) {
251 spin_lock(&sb_lock);
252 if (s->s_count > S_BIAS) {
253 atomic_inc(&s->s_active);
254 s->s_count--;
255 spin_unlock(&sb_lock);
256 return 1;
258 spin_unlock(&sb_lock);
260 up_write(&s->s_umount);
261 put_super(s);
262 yield();
263 return 0;
267 * Superblock locking. We really ought to get rid of these two.
269 void lock_super(struct super_block * sb)
271 get_fs_excl();
272 mutex_lock(&sb->s_lock);
275 void unlock_super(struct super_block * sb)
277 put_fs_excl();
278 mutex_unlock(&sb->s_lock);
281 EXPORT_SYMBOL(lock_super);
282 EXPORT_SYMBOL(unlock_super);
285 * generic_shutdown_super - common helper for ->kill_sb()
286 * @sb: superblock to kill
288 * generic_shutdown_super() does all fs-independent work on superblock
289 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
290 * that need destruction out of superblock, call generic_shutdown_super()
291 * and release aforementioned objects. Note: dentries and inodes _are_
292 * taken care of and do not need specific handling.
294 * Upon calling this function, the filesystem may no longer alter or
295 * rearrange the set of dentries belonging to this super_block, nor may it
296 * change the attachments of dentries to inodes.
298 void generic_shutdown_super(struct super_block *sb)
300 const struct super_operations *sop = sb->s_op;
303 if (sb->s_root) {
304 shrink_dcache_for_umount(sb);
305 sync_filesystem(sb);
306 get_fs_excl();
307 sb->s_flags &= ~MS_ACTIVE;
309 /* bad name - it should be evict_inodes() */
310 invalidate_inodes(sb);
312 if (sop->put_super)
313 sop->put_super(sb);
315 /* Forget any remaining inodes */
316 if (invalidate_inodes(sb)) {
317 printk("VFS: Busy inodes after unmount of %s. "
318 "Self-destruct in 5 seconds. Have a nice day...\n",
319 sb->s_id);
321 put_fs_excl();
323 spin_lock(&sb_lock);
324 /* should be initialized for __put_super_and_need_restart() */
325 list_del_init(&sb->s_list);
326 list_del(&sb->s_instances);
327 spin_unlock(&sb_lock);
328 up_write(&sb->s_umount);
331 EXPORT_SYMBOL(generic_shutdown_super);
334 * sget - find or create a superblock
335 * @type: filesystem type superblock should belong to
336 * @test: comparison callback
337 * @set: setup callback
338 * @data: argument to each of them
340 struct super_block *sget(struct file_system_type *type,
341 int (*test)(struct super_block *,void *),
342 int (*set)(struct super_block *,void *),
343 void *data)
345 struct super_block *s = NULL;
346 struct super_block *old;
347 int err;
349 retry:
350 spin_lock(&sb_lock);
351 if (test) {
352 list_for_each_entry(old, &type->fs_supers, s_instances) {
353 if (!test(old, data))
354 continue;
355 if (!grab_super(old))
356 goto retry;
357 if (s) {
358 up_write(&s->s_umount);
359 destroy_super(s);
360 s = NULL;
362 if (unlikely(!(old->s_flags & MS_BORN))) {
363 deactivate_locked_super(old);
364 goto retry;
366 return old;
369 if (!s) {
370 spin_unlock(&sb_lock);
371 s = alloc_super(type);
372 if (!s)
373 return ERR_PTR(-ENOMEM);
374 goto retry;
377 err = set(s, data);
378 if (err) {
379 spin_unlock(&sb_lock);
380 up_write(&s->s_umount);
381 destroy_super(s);
382 return ERR_PTR(err);
384 s->s_type = type;
385 strlcpy(s->s_id, type->name, sizeof(s->s_id));
386 list_add_tail(&s->s_list, &super_blocks);
387 list_add(&s->s_instances, &type->fs_supers);
388 spin_unlock(&sb_lock);
389 get_filesystem(type);
390 return s;
393 EXPORT_SYMBOL(sget);
395 void drop_super(struct super_block *sb)
397 up_read(&sb->s_umount);
398 put_super(sb);
401 EXPORT_SYMBOL(drop_super);
404 * sync_supers - helper for periodic superblock writeback
406 * Call the write_super method if present on all dirty superblocks in
407 * the system. This is for the periodic writeback used by most older
408 * filesystems. For data integrity superblock writeback use
409 * sync_filesystems() instead.
411 * Note: check the dirty flag before waiting, so we don't
412 * hold up the sync while mounting a device. (The newly
413 * mounted device won't need syncing.)
415 void sync_supers(void)
417 struct super_block *sb;
419 spin_lock(&sb_lock);
420 restart:
421 list_for_each_entry(sb, &super_blocks, s_list) {
422 if (sb->s_op->write_super && sb->s_dirt) {
423 sb->s_count++;
424 spin_unlock(&sb_lock);
426 down_read(&sb->s_umount);
427 if (sb->s_root && sb->s_dirt)
428 sb->s_op->write_super(sb);
429 up_read(&sb->s_umount);
431 spin_lock(&sb_lock);
432 if (__put_super_and_need_restart(sb))
433 goto restart;
436 spin_unlock(&sb_lock);
440 * get_super - get the superblock of a device
441 * @bdev: device to get the superblock for
443 * Scans the superblock list and finds the superblock of the file system
444 * mounted on the device given. %NULL is returned if no match is found.
447 struct super_block * get_super(struct block_device *bdev)
449 struct super_block *sb;
451 if (!bdev)
452 return NULL;
454 spin_lock(&sb_lock);
455 rescan:
456 list_for_each_entry(sb, &super_blocks, s_list) {
457 if (sb->s_bdev == bdev) {
458 sb->s_count++;
459 spin_unlock(&sb_lock);
460 down_read(&sb->s_umount);
461 if (sb->s_root)
462 return sb;
463 up_read(&sb->s_umount);
464 /* restart only when sb is no longer on the list */
465 spin_lock(&sb_lock);
466 if (__put_super_and_need_restart(sb))
467 goto rescan;
470 spin_unlock(&sb_lock);
471 return NULL;
474 EXPORT_SYMBOL(get_super);
477 * get_active_super - get an active reference to the superblock of a device
478 * @bdev: device to get the superblock for
480 * Scans the superblock list and finds the superblock of the file system
481 * mounted on the device given. Returns the superblock with an active
482 * reference and s_umount held exclusively or %NULL if none was found.
484 struct super_block *get_active_super(struct block_device *bdev)
486 struct super_block *sb;
488 if (!bdev)
489 return NULL;
491 spin_lock(&sb_lock);
492 list_for_each_entry(sb, &super_blocks, s_list) {
493 if (sb->s_bdev != bdev)
494 continue;
496 sb->s_count++;
497 spin_unlock(&sb_lock);
498 down_write(&sb->s_umount);
499 if (sb->s_root) {
500 spin_lock(&sb_lock);
501 if (sb->s_count > S_BIAS) {
502 atomic_inc(&sb->s_active);
503 sb->s_count--;
504 spin_unlock(&sb_lock);
505 return sb;
507 spin_unlock(&sb_lock);
509 up_write(&sb->s_umount);
510 put_super(sb);
511 yield();
512 spin_lock(&sb_lock);
514 spin_unlock(&sb_lock);
515 return NULL;
518 struct super_block * user_get_super(dev_t dev)
520 struct super_block *sb;
522 spin_lock(&sb_lock);
523 rescan:
524 list_for_each_entry(sb, &super_blocks, s_list) {
525 if (sb->s_dev == dev) {
526 sb->s_count++;
527 spin_unlock(&sb_lock);
528 down_read(&sb->s_umount);
529 if (sb->s_root)
530 return sb;
531 up_read(&sb->s_umount);
532 /* restart only when sb is no longer on the list */
533 spin_lock(&sb_lock);
534 if (__put_super_and_need_restart(sb))
535 goto rescan;
538 spin_unlock(&sb_lock);
539 return NULL;
542 SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
544 struct super_block *s;
545 struct ustat tmp;
546 struct kstatfs sbuf;
547 int err = -EINVAL;
549 s = user_get_super(new_decode_dev(dev));
550 if (s == NULL)
551 goto out;
552 err = vfs_statfs(s->s_root, &sbuf);
553 drop_super(s);
554 if (err)
555 goto out;
557 memset(&tmp,0,sizeof(struct ustat));
558 tmp.f_tfree = sbuf.f_bfree;
559 tmp.f_tinode = sbuf.f_ffree;
561 err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
562 out:
563 return err;
567 * do_remount_sb - asks filesystem to change mount options.
568 * @sb: superblock in question
569 * @flags: numeric part of options
570 * @data: the rest of options
571 * @force: whether or not to force the change
573 * Alters the mount options of a mounted file system.
575 int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
577 int retval;
578 int remount_rw, remount_ro;
580 if (sb->s_frozen != SB_UNFROZEN)
581 return -EBUSY;
583 #ifdef CONFIG_BLOCK
584 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
585 return -EACCES;
586 #endif
588 if (flags & MS_RDONLY)
589 acct_auto_close(sb);
590 shrink_dcache_sb(sb);
591 sync_filesystem(sb);
593 remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
594 remount_rw = !(flags & MS_RDONLY) && (sb->s_flags & MS_RDONLY);
596 /* If we are remounting RDONLY and current sb is read/write,
597 make sure there are no rw files opened */
598 if (remount_ro) {
599 if (force)
600 mark_files_ro(sb);
601 else if (!fs_may_remount_ro(sb))
602 return -EBUSY;
603 retval = vfs_dq_off(sb, 1);
604 if (retval < 0 && retval != -ENOSYS)
605 return -EBUSY;
608 if (sb->s_op->remount_fs) {
609 retval = sb->s_op->remount_fs(sb, &flags, data);
610 if (retval)
611 return retval;
613 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
614 if (remount_rw)
615 vfs_dq_quota_on_remount(sb);
617 * Some filesystems modify their metadata via some other path than the
618 * bdev buffer cache (eg. use a private mapping, or directories in
619 * pagecache, etc). Also file data modifications go via their own
620 * mappings. So If we try to mount readonly then copy the filesystem
621 * from bdev, we could get stale data, so invalidate it to give a best
622 * effort at coherency.
624 if (remount_ro && sb->s_bdev)
625 invalidate_bdev(sb->s_bdev);
626 return 0;
629 static void do_emergency_remount(struct work_struct *work)
631 struct super_block *sb;
633 spin_lock(&sb_lock);
634 list_for_each_entry(sb, &super_blocks, s_list) {
635 sb->s_count++;
636 spin_unlock(&sb_lock);
637 down_write(&sb->s_umount);
638 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
640 * ->remount_fs needs lock_kernel().
642 * What lock protects sb->s_flags??
644 do_remount_sb(sb, MS_RDONLY, NULL, 1);
646 up_write(&sb->s_umount);
647 put_super(sb);
648 spin_lock(&sb_lock);
650 spin_unlock(&sb_lock);
651 kfree(work);
652 printk("Emergency Remount complete\n");
655 void emergency_remount(void)
657 struct work_struct *work;
659 work = kmalloc(sizeof(*work), GFP_ATOMIC);
660 if (work) {
661 INIT_WORK(work, do_emergency_remount);
662 schedule_work(work);
667 * Unnamed block devices are dummy devices used by virtual
668 * filesystems which don't use real block-devices. -- jrs
671 static DEFINE_IDA(unnamed_dev_ida);
672 static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
673 static int unnamed_dev_start = 0; /* don't bother trying below it */
675 int set_anon_super(struct super_block *s, void *data)
677 int dev;
678 int error;
680 retry:
681 if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
682 return -ENOMEM;
683 spin_lock(&unnamed_dev_lock);
684 error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
685 if (!error)
686 unnamed_dev_start = dev + 1;
687 spin_unlock(&unnamed_dev_lock);
688 if (error == -EAGAIN)
689 /* We raced and lost with another CPU. */
690 goto retry;
691 else if (error)
692 return -EAGAIN;
694 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
695 spin_lock(&unnamed_dev_lock);
696 ida_remove(&unnamed_dev_ida, dev);
697 if (unnamed_dev_start > dev)
698 unnamed_dev_start = dev;
699 spin_unlock(&unnamed_dev_lock);
700 return -EMFILE;
702 s->s_dev = MKDEV(0, dev & MINORMASK);
703 s->s_bdi = &noop_backing_dev_info;
704 return 0;
707 EXPORT_SYMBOL(set_anon_super);
709 void kill_anon_super(struct super_block *sb)
711 int slot = MINOR(sb->s_dev);
713 generic_shutdown_super(sb);
714 spin_lock(&unnamed_dev_lock);
715 ida_remove(&unnamed_dev_ida, slot);
716 if (slot < unnamed_dev_start)
717 unnamed_dev_start = slot;
718 spin_unlock(&unnamed_dev_lock);
721 EXPORT_SYMBOL(kill_anon_super);
723 void kill_litter_super(struct super_block *sb)
725 if (sb->s_root)
726 d_genocide(sb->s_root);
727 kill_anon_super(sb);
730 EXPORT_SYMBOL(kill_litter_super);
732 static int ns_test_super(struct super_block *sb, void *data)
734 return sb->s_fs_info == data;
737 static int ns_set_super(struct super_block *sb, void *data)
739 sb->s_fs_info = data;
740 return set_anon_super(sb, NULL);
743 int get_sb_ns(struct file_system_type *fs_type, int flags, void *data,
744 int (*fill_super)(struct super_block *, void *, int),
745 struct vfsmount *mnt)
747 struct super_block *sb;
749 sb = sget(fs_type, ns_test_super, ns_set_super, data);
750 if (IS_ERR(sb))
751 return PTR_ERR(sb);
753 if (!sb->s_root) {
754 int err;
755 sb->s_flags = flags;
756 err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
757 if (err) {
758 deactivate_locked_super(sb);
759 return err;
762 sb->s_flags |= MS_ACTIVE;
765 simple_set_mnt(mnt, sb);
766 return 0;
769 EXPORT_SYMBOL(get_sb_ns);
771 #ifdef CONFIG_BLOCK
772 static int set_bdev_super(struct super_block *s, void *data)
774 s->s_bdev = data;
775 s->s_dev = s->s_bdev->bd_dev;
778 * We set the bdi here to the queue backing, file systems can
779 * overwrite this in ->fill_super()
781 s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
782 return 0;
785 static int test_bdev_super(struct super_block *s, void *data)
787 return (void *)s->s_bdev == data;
790 int get_sb_bdev(struct file_system_type *fs_type,
791 int flags, const char *dev_name, void *data,
792 int (*fill_super)(struct super_block *, void *, int),
793 struct vfsmount *mnt)
795 struct block_device *bdev;
796 struct super_block *s;
797 fmode_t mode = FMODE_READ;
798 int error = 0;
800 if (!(flags & MS_RDONLY))
801 mode |= FMODE_WRITE;
803 bdev = open_bdev_exclusive(dev_name, mode, fs_type);
804 if (IS_ERR(bdev))
805 return PTR_ERR(bdev);
808 * once the super is inserted into the list by sget, s_umount
809 * will protect the lockfs code from trying to start a snapshot
810 * while we are mounting
812 mutex_lock(&bdev->bd_fsfreeze_mutex);
813 if (bdev->bd_fsfreeze_count > 0) {
814 mutex_unlock(&bdev->bd_fsfreeze_mutex);
815 error = -EBUSY;
816 goto error_bdev;
818 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
819 mutex_unlock(&bdev->bd_fsfreeze_mutex);
820 if (IS_ERR(s))
821 goto error_s;
823 if (s->s_root) {
824 if ((flags ^ s->s_flags) & MS_RDONLY) {
825 deactivate_locked_super(s);
826 error = -EBUSY;
827 goto error_bdev;
830 close_bdev_exclusive(bdev, mode);
831 } else {
832 char b[BDEVNAME_SIZE];
834 s->s_flags = flags;
835 s->s_mode = mode;
836 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
837 sb_set_blocksize(s, block_size(bdev));
838 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
839 if (error) {
840 deactivate_locked_super(s);
841 goto error;
844 s->s_flags |= MS_ACTIVE;
845 bdev->bd_super = s;
848 simple_set_mnt(mnt, s);
849 return 0;
851 error_s:
852 error = PTR_ERR(s);
853 error_bdev:
854 close_bdev_exclusive(bdev, mode);
855 error:
856 return error;
859 EXPORT_SYMBOL(get_sb_bdev);
861 void kill_block_super(struct super_block *sb)
863 struct block_device *bdev = sb->s_bdev;
864 fmode_t mode = sb->s_mode;
866 bdev->bd_super = NULL;
867 generic_shutdown_super(sb);
868 sync_blockdev(bdev);
869 close_bdev_exclusive(bdev, mode);
872 EXPORT_SYMBOL(kill_block_super);
873 #endif
875 int get_sb_nodev(struct file_system_type *fs_type,
876 int flags, void *data,
877 int (*fill_super)(struct super_block *, void *, int),
878 struct vfsmount *mnt)
880 int error;
881 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
883 if (IS_ERR(s))
884 return PTR_ERR(s);
886 s->s_flags = flags;
888 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
889 if (error) {
890 deactivate_locked_super(s);
891 return error;
893 s->s_flags |= MS_ACTIVE;
894 simple_set_mnt(mnt, s);
895 return 0;
898 EXPORT_SYMBOL(get_sb_nodev);
900 static int compare_single(struct super_block *s, void *p)
902 return 1;
905 int get_sb_single(struct file_system_type *fs_type,
906 int flags, void *data,
907 int (*fill_super)(struct super_block *, void *, int),
908 struct vfsmount *mnt)
910 struct super_block *s;
911 int error;
913 s = sget(fs_type, compare_single, set_anon_super, NULL);
914 if (IS_ERR(s))
915 return PTR_ERR(s);
916 if (!s->s_root) {
917 s->s_flags = flags;
918 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
919 if (error) {
920 deactivate_locked_super(s);
921 return error;
923 s->s_flags |= MS_ACTIVE;
924 } else {
925 do_remount_sb(s, flags, data, 0);
927 simple_set_mnt(mnt, s);
928 return 0;
931 EXPORT_SYMBOL(get_sb_single);
933 struct vfsmount *
934 vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
936 struct vfsmount *mnt;
937 char *secdata = NULL;
938 int error;
940 if (!type)
941 return ERR_PTR(-ENODEV);
943 error = -ENOMEM;
944 mnt = alloc_vfsmnt(name);
945 if (!mnt)
946 goto out;
948 if (flags & MS_KERNMOUNT)
949 mnt->mnt_flags = MNT_INTERNAL;
951 if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
952 secdata = alloc_secdata();
953 if (!secdata)
954 goto out_mnt;
956 error = security_sb_copy_data(data, secdata);
957 if (error)
958 goto out_free_secdata;
961 error = type->get_sb(type, flags, name, data, mnt);
962 if (error < 0)
963 goto out_free_secdata;
964 BUG_ON(!mnt->mnt_sb);
965 WARN_ON(!mnt->mnt_sb->s_bdi);
966 WARN_ON(mnt->mnt_sb->s_bdi == &default_backing_dev_info);
967 mnt->mnt_sb->s_flags |= MS_BORN;
969 error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
970 if (error)
971 goto out_sb;
974 * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
975 * but s_maxbytes was an unsigned long long for many releases. Throw
976 * this warning for a little while to try and catch filesystems that
977 * violate this rule. This warning should be either removed or
978 * converted to a BUG() in 2.6.34.
980 WARN((mnt->mnt_sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
981 "negative value (%lld)\n", type->name, mnt->mnt_sb->s_maxbytes);
983 mnt->mnt_mountpoint = mnt->mnt_root;
984 mnt->mnt_parent = mnt;
985 up_write(&mnt->mnt_sb->s_umount);
986 free_secdata(secdata);
987 return mnt;
988 out_sb:
989 dput(mnt->mnt_root);
990 deactivate_locked_super(mnt->mnt_sb);
991 out_free_secdata:
992 free_secdata(secdata);
993 out_mnt:
994 free_vfsmnt(mnt);
995 out:
996 return ERR_PTR(error);
999 EXPORT_SYMBOL_GPL(vfs_kern_mount);
1001 static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
1003 int err;
1004 const char *subtype = strchr(fstype, '.');
1005 if (subtype) {
1006 subtype++;
1007 err = -EINVAL;
1008 if (!subtype[0])
1009 goto err;
1010 } else
1011 subtype = "";
1013 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
1014 err = -ENOMEM;
1015 if (!mnt->mnt_sb->s_subtype)
1016 goto err;
1017 return mnt;
1019 err:
1020 mntput(mnt);
1021 return ERR_PTR(err);
1024 struct vfsmount *
1025 do_kern_mount(const char *fstype, int flags, const char *name, void *data)
1027 struct file_system_type *type = get_fs_type(fstype);
1028 struct vfsmount *mnt;
1029 if (!type)
1030 return ERR_PTR(-ENODEV);
1031 mnt = vfs_kern_mount(type, flags, name, data);
1032 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
1033 !mnt->mnt_sb->s_subtype)
1034 mnt = fs_set_subtype(mnt, fstype);
1035 put_filesystem(type);
1036 return mnt;
1038 EXPORT_SYMBOL_GPL(do_kern_mount);
1040 struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
1042 return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
1045 EXPORT_SYMBOL_GPL(kern_mount_data);