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