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