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