USB: isp1760: Support board-specific hardware configurations
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / super.c
blob453877c5697be1b927bc8b9654f98f9cad71644b
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/buffer_head.h> /* for fsync_super() */
32 #include <linux/mount.h>
33 #include <linux/security.h>
34 #include <linux/syscalls.h>
35 #include <linux/vfs.h>
36 #include <linux/writeback.h> /* for the emergency remount stuff */
37 #include <linux/idr.h>
38 #include <linux/kobject.h>
39 #include <linux/mutex.h>
40 #include <linux/file.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 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_dirty);
67 INIT_LIST_HEAD(&s->s_io);
68 INIT_LIST_HEAD(&s->s_more_io);
69 INIT_LIST_HEAD(&s->s_files);
70 INIT_LIST_HEAD(&s->s_instances);
71 INIT_HLIST_HEAD(&s->s_anon);
72 INIT_LIST_HEAD(&s->s_inodes);
73 init_rwsem(&s->s_umount);
74 mutex_init(&s->s_lock);
75 lockdep_set_class(&s->s_umount, &type->s_umount_key);
77 * The locking rules for s_lock are up to the
78 * filesystem. For example ext3fs has different
79 * lock ordering than usbfs:
81 lockdep_set_class(&s->s_lock, &type->s_lock_key);
82 down_write(&s->s_umount);
83 s->s_count = S_BIAS;
84 atomic_set(&s->s_active, 1);
85 mutex_init(&s->s_vfs_rename_mutex);
86 mutex_init(&s->s_dquot.dqio_mutex);
87 mutex_init(&s->s_dquot.dqonoff_mutex);
88 init_rwsem(&s->s_dquot.dqptr_sem);
89 init_waitqueue_head(&s->s_wait_unfrozen);
90 s->s_maxbytes = MAX_NON_LFS;
91 s->dq_op = sb_dquot_ops;
92 s->s_qcop = sb_quotactl_ops;
93 s->s_op = &default_op;
94 s->s_time_gran = 1000000000;
96 out:
97 return s;
101 * destroy_super - frees a superblock
102 * @s: superblock to free
104 * Frees a superblock.
106 static inline void destroy_super(struct super_block *s)
108 security_sb_free(s);
109 kfree(s->s_subtype);
110 kfree(s->s_options);
111 kfree(s);
114 /* Superblock refcounting */
117 * Drop a superblock's refcount. Returns non-zero if the superblock was
118 * destroyed. The caller must hold sb_lock.
120 static int __put_super(struct super_block *sb)
122 int ret = 0;
124 if (!--sb->s_count) {
125 destroy_super(sb);
126 ret = 1;
128 return ret;
132 * Drop a superblock's refcount.
133 * Returns non-zero if the superblock is about to be destroyed and
134 * at least is already removed from super_blocks list, so if we are
135 * making a loop through super blocks then we need to restart.
136 * The caller must hold sb_lock.
138 int __put_super_and_need_restart(struct super_block *sb)
140 /* check for race with generic_shutdown_super() */
141 if (list_empty(&sb->s_list)) {
142 /* super block is removed, need to restart... */
143 __put_super(sb);
144 return 1;
146 /* can't be the last, since s_list is still in use */
147 sb->s_count--;
148 BUG_ON(sb->s_count == 0);
149 return 0;
153 * put_super - drop a temporary reference to superblock
154 * @sb: superblock in question
156 * Drops a temporary reference, frees superblock if there's no
157 * references left.
159 static void put_super(struct super_block *sb)
161 spin_lock(&sb_lock);
162 __put_super(sb);
163 spin_unlock(&sb_lock);
168 * deactivate_super - drop an active reference to superblock
169 * @s: superblock to deactivate
171 * Drops an active reference to superblock, acquiring a temprory one if
172 * there is no active references left. In that case we lock superblock,
173 * tell fs driver to shut it down and drop the temporary reference we
174 * had just acquired.
176 void deactivate_super(struct super_block *s)
178 struct file_system_type *fs = s->s_type;
179 if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
180 s->s_count -= S_BIAS-1;
181 spin_unlock(&sb_lock);
182 DQUOT_OFF(s, 0);
183 down_write(&s->s_umount);
184 fs->kill_sb(s);
185 put_filesystem(fs);
186 put_super(s);
190 EXPORT_SYMBOL(deactivate_super);
193 * grab_super - acquire an active reference
194 * @s: reference we are trying to make active
196 * Tries to acquire an active reference. grab_super() is used when we
197 * had just found a superblock in super_blocks or fs_type->fs_supers
198 * and want to turn it into a full-blown active reference. grab_super()
199 * is called with sb_lock held and drops it. Returns 1 in case of
200 * success, 0 if we had failed (superblock contents was already dead or
201 * dying when grab_super() had been called).
203 static int grab_super(struct super_block *s) __releases(sb_lock)
205 s->s_count++;
206 spin_unlock(&sb_lock);
207 down_write(&s->s_umount);
208 if (s->s_root) {
209 spin_lock(&sb_lock);
210 if (s->s_count > S_BIAS) {
211 atomic_inc(&s->s_active);
212 s->s_count--;
213 spin_unlock(&sb_lock);
214 return 1;
216 spin_unlock(&sb_lock);
218 up_write(&s->s_umount);
219 put_super(s);
220 yield();
221 return 0;
225 * Superblock locking. We really ought to get rid of these two.
227 void lock_super(struct super_block * sb)
229 get_fs_excl();
230 mutex_lock(&sb->s_lock);
233 void unlock_super(struct super_block * sb)
235 put_fs_excl();
236 mutex_unlock(&sb->s_lock);
239 EXPORT_SYMBOL(lock_super);
240 EXPORT_SYMBOL(unlock_super);
243 * Write out and wait upon all dirty data associated with this
244 * superblock. Filesystem data as well as the underlying block
245 * device. Takes the superblock lock. Requires a second blkdev
246 * flush by the caller to complete the operation.
248 void __fsync_super(struct super_block *sb)
250 sync_inodes_sb(sb, 0);
251 DQUOT_SYNC(sb);
252 lock_super(sb);
253 if (sb->s_dirt && sb->s_op->write_super)
254 sb->s_op->write_super(sb);
255 unlock_super(sb);
256 if (sb->s_op->sync_fs)
257 sb->s_op->sync_fs(sb, 1);
258 sync_blockdev(sb->s_bdev);
259 sync_inodes_sb(sb, 1);
263 * Write out and wait upon all dirty data associated with this
264 * superblock. Filesystem data as well as the underlying block
265 * device. Takes the superblock lock.
267 int fsync_super(struct super_block *sb)
269 __fsync_super(sb);
270 return sync_blockdev(sb->s_bdev);
274 * generic_shutdown_super - common helper for ->kill_sb()
275 * @sb: superblock to kill
277 * generic_shutdown_super() does all fs-independent work on superblock
278 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
279 * that need destruction out of superblock, call generic_shutdown_super()
280 * and release aforementioned objects. Note: dentries and inodes _are_
281 * taken care of and do not need specific handling.
283 * Upon calling this function, the filesystem may no longer alter or
284 * rearrange the set of dentries belonging to this super_block, nor may it
285 * change the attachments of dentries to inodes.
287 void generic_shutdown_super(struct super_block *sb)
289 const struct super_operations *sop = sb->s_op;
291 if (sb->s_root) {
292 shrink_dcache_for_umount(sb);
293 fsync_super(sb);
294 lock_super(sb);
295 sb->s_flags &= ~MS_ACTIVE;
296 /* bad name - it should be evict_inodes() */
297 invalidate_inodes(sb);
298 lock_kernel();
300 if (sop->write_super && sb->s_dirt)
301 sop->write_super(sb);
302 if (sop->put_super)
303 sop->put_super(sb);
305 /* Forget any remaining inodes */
306 if (invalidate_inodes(sb)) {
307 printk("VFS: Busy inodes after unmount of %s. "
308 "Self-destruct in 5 seconds. Have a nice day...\n",
309 sb->s_id);
312 unlock_kernel();
313 unlock_super(sb);
315 spin_lock(&sb_lock);
316 /* should be initialized for __put_super_and_need_restart() */
317 list_del_init(&sb->s_list);
318 list_del(&sb->s_instances);
319 spin_unlock(&sb_lock);
320 up_write(&sb->s_umount);
323 EXPORT_SYMBOL(generic_shutdown_super);
326 * sget - find or create a superblock
327 * @type: filesystem type superblock should belong to
328 * @test: comparison callback
329 * @set: setup callback
330 * @data: argument to each of them
332 struct super_block *sget(struct file_system_type *type,
333 int (*test)(struct super_block *,void *),
334 int (*set)(struct super_block *,void *),
335 void *data)
337 struct super_block *s = NULL;
338 struct super_block *old;
339 int err;
341 retry:
342 spin_lock(&sb_lock);
343 if (test) {
344 list_for_each_entry(old, &type->fs_supers, s_instances) {
345 if (!test(old, data))
346 continue;
347 if (!grab_super(old))
348 goto retry;
349 if (s)
350 destroy_super(s);
351 return old;
354 if (!s) {
355 spin_unlock(&sb_lock);
356 s = alloc_super(type);
357 if (!s)
358 return ERR_PTR(-ENOMEM);
359 goto retry;
362 err = set(s, data);
363 if (err) {
364 spin_unlock(&sb_lock);
365 destroy_super(s);
366 return ERR_PTR(err);
368 s->s_type = type;
369 strlcpy(s->s_id, type->name, sizeof(s->s_id));
370 list_add_tail(&s->s_list, &super_blocks);
371 list_add(&s->s_instances, &type->fs_supers);
372 spin_unlock(&sb_lock);
373 get_filesystem(type);
374 return s;
377 EXPORT_SYMBOL(sget);
379 void drop_super(struct super_block *sb)
381 up_read(&sb->s_umount);
382 put_super(sb);
385 EXPORT_SYMBOL(drop_super);
387 static inline void write_super(struct super_block *sb)
389 lock_super(sb);
390 if (sb->s_root && sb->s_dirt)
391 if (sb->s_op->write_super)
392 sb->s_op->write_super(sb);
393 unlock_super(sb);
397 * Note: check the dirty flag before waiting, so we don't
398 * hold up the sync while mounting a device. (The newly
399 * mounted device won't need syncing.)
401 void sync_supers(void)
403 struct super_block *sb;
405 spin_lock(&sb_lock);
406 restart:
407 list_for_each_entry(sb, &super_blocks, s_list) {
408 if (sb->s_dirt) {
409 sb->s_count++;
410 spin_unlock(&sb_lock);
411 down_read(&sb->s_umount);
412 write_super(sb);
413 up_read(&sb->s_umount);
414 spin_lock(&sb_lock);
415 if (__put_super_and_need_restart(sb))
416 goto restart;
419 spin_unlock(&sb_lock);
423 * Call the ->sync_fs super_op against all filesystems which are r/w and
424 * which implement it.
426 * This operation is careful to avoid the livelock which could easily happen
427 * if two or more filesystems are being continuously dirtied. s_need_sync_fs
428 * is used only here. We set it against all filesystems and then clear it as
429 * we sync them. So redirtied filesystems are skipped.
431 * But if process A is currently running sync_filesystems and then process B
432 * calls sync_filesystems as well, process B will set all the s_need_sync_fs
433 * flags again, which will cause process A to resync everything. Fix that with
434 * a local mutex.
436 * (Fabian) Avoid sync_fs with clean fs & wait mode 0
438 void sync_filesystems(int wait)
440 struct super_block *sb;
441 static DEFINE_MUTEX(mutex);
443 mutex_lock(&mutex); /* Could be down_interruptible */
444 spin_lock(&sb_lock);
445 list_for_each_entry(sb, &super_blocks, s_list) {
446 if (!sb->s_op->sync_fs)
447 continue;
448 if (sb->s_flags & MS_RDONLY)
449 continue;
450 sb->s_need_sync_fs = 1;
453 restart:
454 list_for_each_entry(sb, &super_blocks, s_list) {
455 if (!sb->s_need_sync_fs)
456 continue;
457 sb->s_need_sync_fs = 0;
458 if (sb->s_flags & MS_RDONLY)
459 continue; /* hm. Was remounted r/o meanwhile */
460 sb->s_count++;
461 spin_unlock(&sb_lock);
462 down_read(&sb->s_umount);
463 if (sb->s_root && (wait || sb->s_dirt))
464 sb->s_op->sync_fs(sb, wait);
465 up_read(&sb->s_umount);
466 /* restart only when sb is no longer on the list */
467 spin_lock(&sb_lock);
468 if (__put_super_and_need_restart(sb))
469 goto restart;
471 spin_unlock(&sb_lock);
472 mutex_unlock(&mutex);
476 * get_super - get the superblock of a device
477 * @bdev: device to get the superblock for
479 * Scans the superblock list and finds the superblock of the file system
480 * mounted on the device given. %NULL is returned if no match is found.
483 struct super_block * get_super(struct block_device *bdev)
485 struct super_block *sb;
487 if (!bdev)
488 return NULL;
490 spin_lock(&sb_lock);
491 rescan:
492 list_for_each_entry(sb, &super_blocks, s_list) {
493 if (sb->s_bdev == bdev) {
494 sb->s_count++;
495 spin_unlock(&sb_lock);
496 down_read(&sb->s_umount);
497 if (sb->s_root)
498 return sb;
499 up_read(&sb->s_umount);
500 /* restart only when sb is no longer on the list */
501 spin_lock(&sb_lock);
502 if (__put_super_and_need_restart(sb))
503 goto rescan;
506 spin_unlock(&sb_lock);
507 return NULL;
510 EXPORT_SYMBOL(get_super);
512 struct super_block * user_get_super(dev_t dev)
514 struct super_block *sb;
516 spin_lock(&sb_lock);
517 rescan:
518 list_for_each_entry(sb, &super_blocks, s_list) {
519 if (sb->s_dev == dev) {
520 sb->s_count++;
521 spin_unlock(&sb_lock);
522 down_read(&sb->s_umount);
523 if (sb->s_root)
524 return sb;
525 up_read(&sb->s_umount);
526 /* restart only when sb is no longer on the list */
527 spin_lock(&sb_lock);
528 if (__put_super_and_need_restart(sb))
529 goto rescan;
532 spin_unlock(&sb_lock);
533 return NULL;
536 asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf)
538 struct super_block *s;
539 struct ustat tmp;
540 struct kstatfs sbuf;
541 int err = -EINVAL;
543 s = user_get_super(new_decode_dev(dev));
544 if (s == NULL)
545 goto out;
546 err = vfs_statfs(s->s_root, &sbuf);
547 drop_super(s);
548 if (err)
549 goto out;
551 memset(&tmp,0,sizeof(struct ustat));
552 tmp.f_tfree = sbuf.f_bfree;
553 tmp.f_tinode = sbuf.f_ffree;
555 err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
556 out:
557 return err;
561 * mark_files_ro - mark all files read-only
562 * @sb: superblock in question
564 * All files are marked read-only. We don't care about pending
565 * delete files so this should be used in 'force' mode only.
568 static void mark_files_ro(struct super_block *sb)
570 struct file *f;
572 retry:
573 file_list_lock();
574 list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
575 struct vfsmount *mnt;
576 if (!S_ISREG(f->f_path.dentry->d_inode->i_mode))
577 continue;
578 if (!file_count(f))
579 continue;
580 if (!(f->f_mode & FMODE_WRITE))
581 continue;
582 f->f_mode &= ~FMODE_WRITE;
583 if (file_check_writeable(f) != 0)
584 continue;
585 file_release_write(f);
586 mnt = mntget(f->f_path.mnt);
587 file_list_unlock();
589 * This can sleep, so we can't hold
590 * the file_list_lock() spinlock.
592 mnt_drop_write(mnt);
593 mntput(mnt);
594 goto retry;
596 file_list_unlock();
600 * do_remount_sb - asks filesystem to change mount options.
601 * @sb: superblock in question
602 * @flags: numeric part of options
603 * @data: the rest of options
604 * @force: whether or not to force the change
606 * Alters the mount options of a mounted file system.
608 int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
610 int retval;
611 int remount_rw;
613 #ifdef CONFIG_BLOCK
614 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
615 return -EACCES;
616 #endif
617 if (flags & MS_RDONLY)
618 acct_auto_close(sb);
619 shrink_dcache_sb(sb);
620 fsync_super(sb);
622 /* If we are remounting RDONLY and current sb is read/write,
623 make sure there are no rw files opened */
624 if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
625 if (force)
626 mark_files_ro(sb);
627 else if (!fs_may_remount_ro(sb))
628 return -EBUSY;
629 retval = DQUOT_OFF(sb, 1);
630 if (retval < 0 && retval != -ENOSYS)
631 return -EBUSY;
633 remount_rw = !(flags & MS_RDONLY) && (sb->s_flags & MS_RDONLY);
635 if (sb->s_op->remount_fs) {
636 lock_super(sb);
637 retval = sb->s_op->remount_fs(sb, &flags, data);
638 unlock_super(sb);
639 if (retval)
640 return retval;
642 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
643 if (remount_rw)
644 DQUOT_ON_REMOUNT(sb);
645 return 0;
648 static void do_emergency_remount(unsigned long foo)
650 struct super_block *sb;
652 spin_lock(&sb_lock);
653 list_for_each_entry(sb, &super_blocks, s_list) {
654 sb->s_count++;
655 spin_unlock(&sb_lock);
656 down_read(&sb->s_umount);
657 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
659 * ->remount_fs needs lock_kernel().
661 * What lock protects sb->s_flags??
663 lock_kernel();
664 do_remount_sb(sb, MS_RDONLY, NULL, 1);
665 unlock_kernel();
667 drop_super(sb);
668 spin_lock(&sb_lock);
670 spin_unlock(&sb_lock);
671 printk("Emergency Remount complete\n");
674 void emergency_remount(void)
676 pdflush_operation(do_emergency_remount, 0);
680 * Unnamed block devices are dummy devices used by virtual
681 * filesystems which don't use real block-devices. -- jrs
684 static struct idr unnamed_dev_idr;
685 static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
687 int set_anon_super(struct super_block *s, void *data)
689 int dev;
690 int error;
692 retry:
693 if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0)
694 return -ENOMEM;
695 spin_lock(&unnamed_dev_lock);
696 error = idr_get_new(&unnamed_dev_idr, NULL, &dev);
697 spin_unlock(&unnamed_dev_lock);
698 if (error == -EAGAIN)
699 /* We raced and lost with another CPU. */
700 goto retry;
701 else if (error)
702 return -EAGAIN;
704 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
705 spin_lock(&unnamed_dev_lock);
706 idr_remove(&unnamed_dev_idr, dev);
707 spin_unlock(&unnamed_dev_lock);
708 return -EMFILE;
710 s->s_dev = MKDEV(0, dev & MINORMASK);
711 return 0;
714 EXPORT_SYMBOL(set_anon_super);
716 void kill_anon_super(struct super_block *sb)
718 int slot = MINOR(sb->s_dev);
720 generic_shutdown_super(sb);
721 spin_lock(&unnamed_dev_lock);
722 idr_remove(&unnamed_dev_idr, slot);
723 spin_unlock(&unnamed_dev_lock);
726 EXPORT_SYMBOL(kill_anon_super);
728 void __init unnamed_dev_init(void)
730 idr_init(&unnamed_dev_idr);
733 void kill_litter_super(struct super_block *sb)
735 if (sb->s_root)
736 d_genocide(sb->s_root);
737 kill_anon_super(sb);
740 EXPORT_SYMBOL(kill_litter_super);
742 #ifdef CONFIG_BLOCK
743 static int set_bdev_super(struct super_block *s, void *data)
745 s->s_bdev = data;
746 s->s_dev = s->s_bdev->bd_dev;
747 return 0;
750 static int test_bdev_super(struct super_block *s, void *data)
752 return (void *)s->s_bdev == data;
755 int get_sb_bdev(struct file_system_type *fs_type,
756 int flags, const char *dev_name, void *data,
757 int (*fill_super)(struct super_block *, void *, int),
758 struct vfsmount *mnt)
760 struct block_device *bdev;
761 struct super_block *s;
762 int error = 0;
764 bdev = open_bdev_excl(dev_name, flags, fs_type);
765 if (IS_ERR(bdev))
766 return PTR_ERR(bdev);
769 * once the super is inserted into the list by sget, s_umount
770 * will protect the lockfs code from trying to start a snapshot
771 * while we are mounting
773 down(&bdev->bd_mount_sem);
774 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
775 up(&bdev->bd_mount_sem);
776 if (IS_ERR(s))
777 goto error_s;
779 if (s->s_root) {
780 if ((flags ^ s->s_flags) & MS_RDONLY) {
781 up_write(&s->s_umount);
782 deactivate_super(s);
783 error = -EBUSY;
784 goto error_bdev;
787 close_bdev_excl(bdev);
788 } else {
789 char b[BDEVNAME_SIZE];
791 s->s_flags = flags;
792 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
793 sb_set_blocksize(s, block_size(bdev));
794 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
795 if (error) {
796 up_write(&s->s_umount);
797 deactivate_super(s);
798 goto error;
801 s->s_flags |= MS_ACTIVE;
804 return simple_set_mnt(mnt, s);
806 error_s:
807 error = PTR_ERR(s);
808 error_bdev:
809 close_bdev_excl(bdev);
810 error:
811 return error;
814 EXPORT_SYMBOL(get_sb_bdev);
816 void kill_block_super(struct super_block *sb)
818 struct block_device *bdev = sb->s_bdev;
820 generic_shutdown_super(sb);
821 sync_blockdev(bdev);
822 close_bdev_excl(bdev);
825 EXPORT_SYMBOL(kill_block_super);
826 #endif
828 int get_sb_nodev(struct file_system_type *fs_type,
829 int flags, void *data,
830 int (*fill_super)(struct super_block *, void *, int),
831 struct vfsmount *mnt)
833 int error;
834 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
836 if (IS_ERR(s))
837 return PTR_ERR(s);
839 s->s_flags = flags;
841 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
842 if (error) {
843 up_write(&s->s_umount);
844 deactivate_super(s);
845 return error;
847 s->s_flags |= MS_ACTIVE;
848 return simple_set_mnt(mnt, s);
851 EXPORT_SYMBOL(get_sb_nodev);
853 static int compare_single(struct super_block *s, void *p)
855 return 1;
858 int get_sb_single(struct file_system_type *fs_type,
859 int flags, void *data,
860 int (*fill_super)(struct super_block *, void *, int),
861 struct vfsmount *mnt)
863 struct super_block *s;
864 int error;
866 s = sget(fs_type, compare_single, set_anon_super, NULL);
867 if (IS_ERR(s))
868 return PTR_ERR(s);
869 if (!s->s_root) {
870 s->s_flags = flags;
871 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
872 if (error) {
873 up_write(&s->s_umount);
874 deactivate_super(s);
875 return error;
877 s->s_flags |= MS_ACTIVE;
879 do_remount_sb(s, flags, data, 0);
880 return simple_set_mnt(mnt, s);
883 EXPORT_SYMBOL(get_sb_single);
885 struct vfsmount *
886 vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
888 struct vfsmount *mnt;
889 char *secdata = NULL;
890 int error;
892 if (!type)
893 return ERR_PTR(-ENODEV);
895 error = -ENOMEM;
896 mnt = alloc_vfsmnt(name);
897 if (!mnt)
898 goto out;
900 if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
901 secdata = alloc_secdata();
902 if (!secdata)
903 goto out_mnt;
905 error = security_sb_copy_data(data, secdata);
906 if (error)
907 goto out_free_secdata;
910 error = type->get_sb(type, flags, name, data, mnt);
911 if (error < 0)
912 goto out_free_secdata;
913 BUG_ON(!mnt->mnt_sb);
915 error = security_sb_kern_mount(mnt->mnt_sb, secdata);
916 if (error)
917 goto out_sb;
919 mnt->mnt_mountpoint = mnt->mnt_root;
920 mnt->mnt_parent = mnt;
921 up_write(&mnt->mnt_sb->s_umount);
922 free_secdata(secdata);
923 return mnt;
924 out_sb:
925 dput(mnt->mnt_root);
926 up_write(&mnt->mnt_sb->s_umount);
927 deactivate_super(mnt->mnt_sb);
928 out_free_secdata:
929 free_secdata(secdata);
930 out_mnt:
931 free_vfsmnt(mnt);
932 out:
933 return ERR_PTR(error);
936 EXPORT_SYMBOL_GPL(vfs_kern_mount);
938 static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
940 int err;
941 const char *subtype = strchr(fstype, '.');
942 if (subtype) {
943 subtype++;
944 err = -EINVAL;
945 if (!subtype[0])
946 goto err;
947 } else
948 subtype = "";
950 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
951 err = -ENOMEM;
952 if (!mnt->mnt_sb->s_subtype)
953 goto err;
954 return mnt;
956 err:
957 mntput(mnt);
958 return ERR_PTR(err);
961 struct vfsmount *
962 do_kern_mount(const char *fstype, int flags, const char *name, void *data)
964 struct file_system_type *type = get_fs_type(fstype);
965 struct vfsmount *mnt;
966 if (!type)
967 return ERR_PTR(-ENODEV);
968 mnt = vfs_kern_mount(type, flags, name, data);
969 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
970 !mnt->mnt_sb->s_subtype)
971 mnt = fs_set_subtype(mnt, fstype);
972 put_filesystem(type);
973 return mnt;
975 EXPORT_SYMBOL_GPL(do_kern_mount);
977 struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
979 return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
982 EXPORT_SYMBOL_GPL(kern_mount_data);