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