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