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