This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / fs / super.c
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:
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
21  */
22
23 #include <linux/config.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/smp_lock.h>
28 #include <linux/acct.h>
29 #include <linux/blkdev.h>
30 #include <linux/quotaops.h>
31 #include <linux/namei.h>
32 #include <linux/buffer_head.h>          /* for fsync_super() */
33 #include <linux/mount.h>
34 #include <linux/security.h>
35 #include <linux/vfs.h>
36 #include <linux/writeback.h>            /* for the emergency remount stuff */
37 #include <linux/idr.h>
38 #include <asm/uaccess.h>
39
40
41 void get_filesystem(struct file_system_type *fs);
42 void put_filesystem(struct file_system_type *fs);
43 struct file_system_type *get_fs_type(const char *name);
44
45 LIST_HEAD(super_blocks);
46 spinlock_t sb_lock = SPIN_LOCK_UNLOCKED;
47
48 /**
49  *      alloc_super     -       create new superblock
50  *
51  *      Allocates and initializes a new &struct super_block.  alloc_super()
52  *      returns a pointer new superblock or %NULL if allocation had failed.
53  */
54 static struct super_block *alloc_super(void)
55 {
56         struct super_block *s = kmalloc(sizeof(struct super_block),  GFP_USER);
57         static struct super_operations default_op;
58
59         if (s) {
60                 memset(s, 0, sizeof(struct super_block));
61                 if (security_sb_alloc(s)) {
62                         kfree(s);
63                         s = NULL;
64                         goto out;
65                 }
66                 INIT_LIST_HEAD(&s->s_dirty);
67                 INIT_LIST_HEAD(&s->s_io);
68                 INIT_LIST_HEAD(&s->s_files);
69                 INIT_LIST_HEAD(&s->s_instances);
70                 INIT_HLIST_HEAD(&s->s_anon);
71                 init_rwsem(&s->s_umount);
72                 sema_init(&s->s_lock, 1);
73                 down_write(&s->s_umount);
74                 s->s_count = S_BIAS;
75                 atomic_set(&s->s_active, 1);
76                 sema_init(&s->s_vfs_rename_sem,1);
77                 sema_init(&s->s_dquot.dqio_sem, 1);
78                 sema_init(&s->s_dquot.dqonoff_sem, 1);
79                 init_rwsem(&s->s_dquot.dqptr_sem);
80                 init_waitqueue_head(&s->s_wait_unfrozen);
81                 s->s_maxbytes = MAX_NON_LFS;
82                 s->dq_op = sb_dquot_ops;
83                 s->s_qcop = sb_quotactl_ops;
84                 s->s_op = &default_op;
85         }
86 out:
87         return s;
88 }
89
90 /**
91  *      destroy_super   -       frees a superblock
92  *      @s: superblock to free
93  *
94  *      Frees a superblock.
95  */
96 static inline void destroy_super(struct super_block *s)
97 {
98         security_sb_free(s);
99         kfree(s);
100 }
101
102 /* Superblock refcounting  */
103
104 /*
105  * Drop a superblock's refcount.  Returns non-zero if the superblock was
106  * destroyed.  The caller must hold sb_lock.
107  */
108 int __put_super(struct super_block *sb)
109 {
110         int ret = 0;
111
112         if (!--sb->s_count) {
113                 destroy_super(sb);
114                 ret = 1;
115         }
116         return ret;
117 }
118
119 /**
120  *      put_super       -       drop a temporary reference to superblock
121  *      @s: superblock in question
122  *
123  *      Drops a temporary reference, frees superblock if there's no
124  *      references left.
125  */
126 static void put_super(struct super_block *sb)
127 {
128         spin_lock(&sb_lock);
129         __put_super(sb);
130         spin_unlock(&sb_lock);
131 }
132
133
134 /**
135  *      deactivate_super        -       drop an active reference to superblock
136  *      @s: superblock to deactivate
137  *
138  *      Drops an active reference to superblock, acquiring a temprory one if
139  *      there is no active references left.  In that case we lock superblock,
140  *      tell fs driver to shut it down and drop the temporary reference we
141  *      had just acquired.
142  */
143 void deactivate_super(struct super_block *s)
144 {
145         struct file_system_type *fs = s->s_type;
146         if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
147                 s->s_count -= S_BIAS-1;
148                 spin_unlock(&sb_lock);
149                 down_write(&s->s_umount);
150                 fs->kill_sb(s);
151                 put_filesystem(fs);
152                 put_super(s);
153         }
154 }
155
156 EXPORT_SYMBOL(deactivate_super);
157
158 /**
159  *      grab_super - acquire an active reference
160  *      @s: reference we are trying to make active
161  *
162  *      Tries to acquire an active reference.  grab_super() is used when we
163  *      had just found a superblock in super_blocks or fs_type->fs_supers
164  *      and want to turn it into a full-blown active reference.  grab_super()
165  *      is called with sb_lock held and drops it.  Returns 1 in case of
166  *      success, 0 if we had failed (superblock contents was already dead or
167  *      dying when grab_super() had been called).
168  */
169 static int grab_super(struct super_block *s)
170 {
171         s->s_count++;
172         spin_unlock(&sb_lock);
173         down_write(&s->s_umount);
174         if (s->s_root) {
175                 spin_lock(&sb_lock);
176                 if (s->s_count > S_BIAS) {
177                         atomic_inc(&s->s_active);
178                         s->s_count--;
179                         spin_unlock(&sb_lock);
180                         return 1;
181                 }
182                 spin_unlock(&sb_lock);
183         }
184         up_write(&s->s_umount);
185         put_super(s);
186         yield();
187         return 0;
188 }
189
190 /**
191  *      generic_shutdown_super  -       common helper for ->kill_sb()
192  *      @sb: superblock to kill
193  *
194  *      generic_shutdown_super() does all fs-independent work on superblock
195  *      shutdown.  Typical ->kill_sb() should pick all fs-specific objects
196  *      that need destruction out of superblock, call generic_shutdown_super()
197  *      and release aforementioned objects.  Note: dentries and inodes _are_
198  *      taken care of and do not need specific handling.
199  */
200 void generic_shutdown_super(struct super_block *sb)
201 {
202         struct dentry *root = sb->s_root;
203         struct super_operations *sop = sb->s_op;
204
205         if (root) {
206                 sb->s_root = NULL;
207                 shrink_dcache_parent(root);
208                 shrink_dcache_anon(&sb->s_anon);
209                 dput(root);
210                 fsync_super(sb);
211                 lock_super(sb);
212                 lock_kernel();
213                 sb->s_flags &= ~MS_ACTIVE;
214                 /* bad name - it should be evict_inodes() */
215                 invalidate_inodes(sb);
216
217                 if (sop->write_super && sb->s_dirt)
218                         sop->write_super(sb);
219                 if (sop->put_super)
220                         sop->put_super(sb);
221
222                 /* Forget any remaining inodes */
223                 if (invalidate_inodes(sb)) {
224                         printk("VFS: Busy inodes after unmount. "
225                            "Self-destruct in 5 seconds.  Have a nice day...\n");
226                 }
227
228                 unlock_kernel();
229                 unlock_super(sb);
230         }
231         spin_lock(&sb_lock);
232         list_del(&sb->s_list);
233         list_del(&sb->s_instances);
234         spin_unlock(&sb_lock);
235         up_write(&sb->s_umount);
236 }
237
238 EXPORT_SYMBOL(generic_shutdown_super);
239
240 /**
241  *      sget    -       find or create a superblock
242  *      @type:  filesystem type superblock should belong to
243  *      @test:  comparison callback
244  *      @set:   setup callback
245  *      @data:  argument to each of them
246  */
247 struct super_block *sget(struct file_system_type *type,
248                         int (*test)(struct super_block *,void *),
249                         int (*set)(struct super_block *,void *),
250                         void *data)
251 {
252         struct super_block *s = NULL;
253         struct list_head *p;
254         int err;
255
256 retry:
257         spin_lock(&sb_lock);
258         if (test) list_for_each(p, &type->fs_supers) {
259                 struct super_block *old;
260                 old = list_entry(p, struct super_block, s_instances);
261                 if (!test(old, data))
262                         continue;
263                 if (!grab_super(old))
264                         goto retry;
265                 if (s)
266                         destroy_super(s);
267                 return old;
268         }
269         if (!s) {
270                 spin_unlock(&sb_lock);
271                 s = alloc_super();
272                 if (!s)
273                         return ERR_PTR(-ENOMEM);
274                 goto retry;
275         }
276                 
277         err = set(s, data);
278         if (err) {
279                 spin_unlock(&sb_lock);
280                 destroy_super(s);
281                 return ERR_PTR(err);
282         }
283         s->s_type = type;
284         strlcpy(s->s_id, type->name, sizeof(s->s_id));
285         list_add(&s->s_list, super_blocks.prev);
286         list_add(&s->s_instances, &type->fs_supers);
287         spin_unlock(&sb_lock);
288         get_filesystem(type);
289         return s;
290 }
291
292 EXPORT_SYMBOL(sget);
293
294 void drop_super(struct super_block *sb)
295 {
296         up_read(&sb->s_umount);
297         put_super(sb);
298 }
299
300 EXPORT_SYMBOL(drop_super);
301
302 static inline void write_super(struct super_block *sb)
303 {
304         lock_super(sb);
305         if (sb->s_root && sb->s_dirt)
306                 if (sb->s_op->write_super)
307                         sb->s_op->write_super(sb);
308         unlock_super(sb);
309 }
310
311 /*
312  * Note: check the dirty flag before waiting, so we don't
313  * hold up the sync while mounting a device. (The newly
314  * mounted device won't need syncing.)
315  */
316 void sync_supers(void)
317 {
318         struct super_block * sb;
319 restart:
320         spin_lock(&sb_lock);
321         sb = sb_entry(super_blocks.next);
322         while (sb != sb_entry(&super_blocks))
323                 if (sb->s_dirt) {
324                         sb->s_count++;
325                         spin_unlock(&sb_lock);
326                         down_read(&sb->s_umount);
327                         write_super(sb);
328                         drop_super(sb);
329                         goto restart;
330                 } else
331                         sb = sb_entry(sb->s_list.next);
332         spin_unlock(&sb_lock);
333 }
334
335 /*
336  * Call the ->sync_fs super_op against all filesytems which are r/w and
337  * which implement it.
338  *
339  * This operation is careful to avoid the livelock which could easily happen
340  * if two or more filesystems are being continuously dirtied.  s_need_sync_fs
341  * is used only here.  We set it against all filesystems and then clear it as
342  * we sync them.  So redirtied filesystems are skipped.
343  *
344  * But if process A is currently running sync_filesytems and then process B
345  * calls sync_filesystems as well, process B will set all the s_need_sync_fs
346  * flags again, which will cause process A to resync everything.  Fix that with
347  * a local mutex.
348  *
349  * (Fabian) Avoid sync_fs with clean fs & wait mode 0
350  */
351 void sync_filesystems(int wait)
352 {
353         struct super_block *sb;
354         static DECLARE_MUTEX(mutex);
355
356         down(&mutex);           /* Could be down_interruptible */
357         spin_lock(&sb_lock);
358         for (sb = sb_entry(super_blocks.next); sb != sb_entry(&super_blocks);
359                         sb = sb_entry(sb->s_list.next)) {
360                 if (!sb->s_op->sync_fs)
361                         continue;
362                 if (sb->s_flags & MS_RDONLY)
363                         continue;
364                 sb->s_need_sync_fs = 1;
365         }
366         spin_unlock(&sb_lock);
367
368 restart:
369         spin_lock(&sb_lock);
370         for (sb = sb_entry(super_blocks.next); sb != sb_entry(&super_blocks);
371                         sb = sb_entry(sb->s_list.next)) {
372                 if (!sb->s_need_sync_fs)
373                         continue;
374                 sb->s_need_sync_fs = 0;
375                 if (sb->s_flags & MS_RDONLY)
376                         continue;       /* hm.  Was remounted r/o meanwhile */
377                 sb->s_count++;
378                 spin_unlock(&sb_lock);
379                 down_read(&sb->s_umount);
380                 if (sb->s_root && (wait || sb->s_dirt))
381                         sb->s_op->sync_fs(sb, wait);
382                 drop_super(sb);
383                 goto restart;
384         }
385         spin_unlock(&sb_lock);
386         up(&mutex);
387 }
388
389 /**
390  *      get_super - get the superblock of a device
391  *      @bdev: device to get the superblock for
392  *      
393  *      Scans the superblock list and finds the superblock of the file system
394  *      mounted on the device given. %NULL is returned if no match is found.
395  */
396
397 struct super_block * get_super(struct block_device *bdev)
398 {
399         struct list_head *p;
400         if (!bdev)
401                 return NULL;
402 rescan:
403         spin_lock(&sb_lock);
404         list_for_each(p, &super_blocks) {
405                 struct super_block *s = sb_entry(p);
406                 if (s->s_bdev == bdev) {
407                         s->s_count++;
408                         spin_unlock(&sb_lock);
409                         down_read(&s->s_umount);
410                         if (s->s_root)
411                                 return s;
412                         drop_super(s);
413                         goto rescan;
414                 }
415         }
416         spin_unlock(&sb_lock);
417         return NULL;
418 }
419
420 EXPORT_SYMBOL(get_super);
421  
422 struct super_block * user_get_super(dev_t dev)
423 {
424         struct list_head *p;
425
426 rescan:
427         spin_lock(&sb_lock);
428         list_for_each(p, &super_blocks) {
429                 struct super_block *s = sb_entry(p);
430                 if (s->s_dev ==  dev) {
431                         s->s_count++;
432                         spin_unlock(&sb_lock);
433                         down_read(&s->s_umount);
434                         if (s->s_root)
435                                 return s;
436                         drop_super(s);
437                         goto rescan;
438                 }
439         }
440         spin_unlock(&sb_lock);
441         return NULL;
442 }
443
444 EXPORT_SYMBOL(user_get_super);
445
446 asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf)
447 {
448         struct super_block *s;
449         struct ustat tmp;
450         struct kstatfs sbuf;
451         int err = -EINVAL;
452
453         s = user_get_super(new_decode_dev(dev));
454         if (s == NULL)
455                 goto out;
456         err = vfs_statfs(s, &sbuf);
457         drop_super(s);
458         if (err)
459                 goto out;
460
461         memset(&tmp,0,sizeof(struct ustat));
462         tmp.f_tfree = sbuf.f_bfree;
463         tmp.f_tinode = sbuf.f_ffree;
464
465         err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
466 out:
467         return err;
468 }
469
470 /**
471  *      mark_files_ro
472  *      @sb: superblock in question
473  *
474  *      All files are marked read/only.  We don't care about pending
475  *      delete files so this should be used in 'force' mode only
476  */
477
478 static void mark_files_ro(struct super_block *sb)
479 {
480         struct file *f;
481
482         file_list_lock();
483         list_for_each_entry(f, &sb->s_files, f_list) {
484                 if (S_ISREG(f->f_dentry->d_inode->i_mode) && file_count(f))
485                         f->f_mode &= ~FMODE_WRITE;
486         }
487         file_list_unlock();
488 }
489
490 /**
491  *      do_remount_sb - asks filesystem to change mount options.
492  *      @sb:    superblock in question
493  *      @flags: numeric part of options
494  *      @data:  the rest of options
495  *      @force: whether or not to force the change
496  *
497  *      Alters the mount options of a mounted file system.
498  */
499 int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
500 {
501         int retval;
502         
503         if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
504                 return -EACCES;
505         if (flags & MS_RDONLY)
506                 acct_auto_close(sb);
507         shrink_dcache_sb(sb);
508         fsync_super(sb);
509
510         /* If we are remounting RDONLY and current sb is read/write,
511            make sure there are no rw files opened */
512         if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
513                 if (force)
514                         mark_files_ro(sb);
515                 else if (!fs_may_remount_ro(sb))
516                         return -EBUSY;
517         }
518
519         if (sb->s_op->remount_fs) {
520                 lock_super(sb);
521                 retval = sb->s_op->remount_fs(sb, &flags, data);
522                 unlock_super(sb);
523                 if (retval)
524                         return retval;
525         }
526         sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
527         return 0;
528 }
529
530 static void do_emergency_remount(unsigned long foo)
531 {
532         struct super_block *sb;
533
534         spin_lock(&sb_lock);
535         list_for_each_entry(sb, &super_blocks, s_list) {
536                 sb->s_count++;
537                 spin_unlock(&sb_lock);
538                 down_read(&sb->s_umount);
539                 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
540                         /*
541                          * ->remount_fs needs lock_kernel().
542                          *
543                          * What lock protects sb->s_flags??
544                          */
545                         lock_kernel();
546                         do_remount_sb(sb, MS_RDONLY, NULL, 1);
547                         unlock_kernel();
548                 }
549                 drop_super(sb);
550                 spin_lock(&sb_lock);
551         }
552         spin_unlock(&sb_lock);
553         printk("Emergency Remount complete\n");
554 }
555
556 void emergency_remount(void)
557 {
558         pdflush_operation(do_emergency_remount, 0);
559 }
560
561 /*
562  * Unnamed block devices are dummy devices used by virtual
563  * filesystems which don't use real block-devices.  -- jrs
564  */
565
566 static struct idr unnamed_dev_idr;
567 static spinlock_t unnamed_dev_lock = SPIN_LOCK_UNLOCKED;/* protects the above */
568
569 int set_anon_super(struct super_block *s, void *data)
570 {
571         int dev;
572         int error;
573
574  retry:
575         if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0)
576                 return -ENOMEM;
577         spin_lock(&unnamed_dev_lock);
578         error = idr_get_new(&unnamed_dev_idr, NULL, &dev);
579         spin_unlock(&unnamed_dev_lock);
580         if (error == -EAGAIN)
581                 /* We raced and lost with another CPU. */
582                 goto retry;
583         else if (error)
584                 return -EAGAIN;
585
586         if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
587                 spin_lock(&unnamed_dev_lock);
588                 idr_remove(&unnamed_dev_idr, dev);
589                 spin_unlock(&unnamed_dev_lock);
590                 return -EMFILE;
591         }
592         s->s_dev = MKDEV(0, dev & MINORMASK);
593         return 0;
594 }
595
596 EXPORT_SYMBOL(set_anon_super);
597
598 void kill_anon_super(struct super_block *sb)
599 {
600         int slot = MINOR(sb->s_dev);
601
602         generic_shutdown_super(sb);
603         spin_lock(&unnamed_dev_lock);
604         idr_remove(&unnamed_dev_idr, slot);
605         spin_unlock(&unnamed_dev_lock);
606 }
607
608 EXPORT_SYMBOL(kill_anon_super);
609
610 void __init unnamed_dev_init(void)
611 {
612         idr_init(&unnamed_dev_idr);
613 }
614
615 void kill_litter_super(struct super_block *sb)
616 {
617         if (sb->s_root)
618                 d_genocide(sb->s_root);
619         kill_anon_super(sb);
620 }
621
622 EXPORT_SYMBOL(kill_litter_super);
623
624 static int set_bdev_super(struct super_block *s, void *data)
625 {
626         s->s_bdev = data;
627         s->s_dev = s->s_bdev->bd_dev;
628         return 0;
629 }
630
631 static int test_bdev_super(struct super_block *s, void *data)
632 {
633         return (void *)s->s_bdev == data;
634 }
635
636 struct super_block *get_sb_bdev(struct file_system_type *fs_type,
637         int flags, const char *dev_name, void *data,
638         int (*fill_super)(struct super_block *, void *, int))
639 {
640         struct block_device *bdev;
641         struct super_block *s;
642         int error = 0;
643
644         bdev = open_bdev_excl(dev_name, flags, fs_type);
645         if (IS_ERR(bdev))
646                 return (struct super_block *)bdev;
647
648         /*
649          * once the super is inserted into the list by sget, s_umount
650          * will protect the lockfs code from trying to start a snapshot
651          * while we are mounting
652          */
653         down(&bdev->bd_mount_sem);
654         s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
655         up(&bdev->bd_mount_sem);
656         if (IS_ERR(s))
657                 goto out;
658
659         if (s->s_root) {
660                 if ((flags ^ s->s_flags) & MS_RDONLY) {
661                         up_write(&s->s_umount);
662                         deactivate_super(s);
663                         s = ERR_PTR(-EBUSY);
664                 }
665                 goto out;
666         } else {
667                 char b[BDEVNAME_SIZE];
668
669                 s->s_flags = flags;
670                 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
671                 s->s_old_blocksize = block_size(bdev);
672                 sb_set_blocksize(s, s->s_old_blocksize);
673                 error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
674                 if (error) {
675                         up_write(&s->s_umount);
676                         deactivate_super(s);
677                         s = ERR_PTR(error);
678                 } else
679                         s->s_flags |= MS_ACTIVE;
680         }
681
682         return s;
683
684 out:
685         close_bdev_excl(bdev);
686         return s;
687 }
688
689 EXPORT_SYMBOL(get_sb_bdev);
690
691 void kill_block_super(struct super_block *sb)
692 {
693         struct block_device *bdev = sb->s_bdev;
694         generic_shutdown_super(sb);
695         set_blocksize(bdev, sb->s_old_blocksize);
696         close_bdev_excl(bdev);
697 }
698
699 EXPORT_SYMBOL(kill_block_super);
700
701 struct super_block *get_sb_nodev(struct file_system_type *fs_type,
702         int flags, void *data,
703         int (*fill_super)(struct super_block *, void *, int))
704 {
705         int error;
706         struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
707
708         if (IS_ERR(s))
709                 return s;
710
711         s->s_flags = flags;
712
713         error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
714         if (error) {
715                 up_write(&s->s_umount);
716                 deactivate_super(s);
717                 return ERR_PTR(error);
718         }
719         s->s_flags |= MS_ACTIVE;
720         return s;
721 }
722
723 EXPORT_SYMBOL(get_sb_nodev);
724
725 static int compare_single(struct super_block *s, void *p)
726 {
727         return 1;
728 }
729
730 struct super_block *get_sb_single(struct file_system_type *fs_type,
731         int flags, void *data,
732         int (*fill_super)(struct super_block *, void *, int))
733 {
734         struct super_block *s;
735         int error;
736
737         s = sget(fs_type, compare_single, set_anon_super, NULL);
738         if (IS_ERR(s))
739                 return s;
740         if (!s->s_root) {
741                 s->s_flags = flags;
742                 error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
743                 if (error) {
744                         up_write(&s->s_umount);
745                         deactivate_super(s);
746                         return ERR_PTR(error);
747                 }
748                 s->s_flags |= MS_ACTIVE;
749         }
750         do_remount_sb(s, flags, data, 0);
751         return s;
752 }
753
754 EXPORT_SYMBOL(get_sb_single);
755
756 struct vfsmount *
757 do_kern_mount(const char *fstype, int flags, const char *name, void *data)
758 {
759         struct file_system_type *type = get_fs_type(fstype);
760         struct super_block *sb = ERR_PTR(-ENOMEM);
761         struct vfsmount *mnt;
762         int error;
763         char *secdata = NULL;
764
765         if (!type)
766                 return ERR_PTR(-ENODEV);
767
768         mnt = alloc_vfsmnt(name);
769         if (!mnt)
770                 goto out;
771
772         if (data) {
773                 secdata = alloc_secdata();
774                 if (!secdata) {
775                         sb = ERR_PTR(-ENOMEM);
776                         goto out_mnt;
777                 }
778
779                 error = security_sb_copy_data(type, data, secdata);
780                 if (error) {
781                         sb = ERR_PTR(error);
782                         goto out_free_secdata;
783                 }
784         }
785
786         sb = type->get_sb(type, flags, name, data);
787         if (IS_ERR(sb))
788                 goto out_free_secdata;
789         error = security_sb_kern_mount(sb, secdata);
790         if (error)
791                 goto out_sb;
792         mnt->mnt_sb = sb;
793         mnt->mnt_root = dget(sb->s_root);
794         mnt->mnt_mountpoint = sb->s_root;
795         mnt->mnt_parent = mnt;
796         up_write(&sb->s_umount);
797         put_filesystem(type);
798         return mnt;
799 out_sb:
800         up_write(&sb->s_umount);
801         deactivate_super(sb);
802         sb = ERR_PTR(error);
803 out_free_secdata:
804         free_secdata(secdata);
805 out_mnt:
806         free_vfsmnt(mnt);
807 out:
808         put_filesystem(type);
809         return (struct vfsmount *)sb;
810 }
811
812 struct vfsmount *kern_mount(struct file_system_type *type)
813 {
814         return do_kern_mount(type->name, 0, type->name, NULL);
815 }
816
817 EXPORT_SYMBOL(kern_mount);