fedora core 6 1.2949 + vserver 2.2.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 <linux/vs_context.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  *      @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) __releases(sb_lock)
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  * Superblock locking.  We really ought to get rid of these two.
228  */
229 void lock_super(struct super_block * sb)
230 {
231         get_fs_excl();
232         mutex_lock(&sb->s_lock);
233 }
234
235 void unlock_super(struct super_block * sb)
236 {
237         put_fs_excl();
238         mutex_unlock(&sb->s_lock);
239 }
240
241 EXPORT_SYMBOL(lock_super);
242 EXPORT_SYMBOL(unlock_super);
243
244 /*
245  * Write out and wait upon all dirty data associated with this
246  * superblock.  Filesystem data as well as the underlying block
247  * device.  Takes the superblock lock.  Requires a second blkdev
248  * flush by the caller to complete the operation.
249  */
250 void __fsync_super(struct super_block *sb)
251 {
252         sync_inodes_sb(sb, 0);
253         DQUOT_SYNC(sb);
254         lock_super(sb);
255         if (sb->s_dirt && sb->s_op->write_super)
256                 sb->s_op->write_super(sb);
257         unlock_super(sb);
258         if (sb->s_op->sync_fs)
259                 sb->s_op->sync_fs(sb, 1);
260         sync_blockdev(sb->s_bdev);
261         sync_inodes_sb(sb, 1);
262 }
263
264 /*
265  * Write out and wait upon all dirty data associated with this
266  * superblock.  Filesystem data as well as the underlying block
267  * device.  Takes the superblock lock.
268  */
269 int fsync_super(struct super_block *sb)
270 {
271         __fsync_super(sb);
272         return sync_blockdev(sb->s_bdev);
273 }
274
275 /**
276  *      generic_shutdown_super  -       common helper for ->kill_sb()
277  *      @sb: superblock to kill
278  *
279  *      generic_shutdown_super() does all fs-independent work on superblock
280  *      shutdown.  Typical ->kill_sb() should pick all fs-specific objects
281  *      that need destruction out of superblock, call generic_shutdown_super()
282  *      and release aforementioned objects.  Note: dentries and inodes _are_
283  *      taken care of and do not need specific handling.
284  *
285  *      Upon calling this function, the filesystem may no longer alter or
286  *      rearrange the set of dentries belonging to this super_block, nor may it
287  *      change the attachments of dentries to inodes.
288  */
289 void generic_shutdown_super(struct super_block *sb)
290 {
291         struct super_operations *sop = sb->s_op;
292
293         if (sb->s_root) {
294                 shrink_dcache_for_umount(sb);
295                 fsync_super(sb);
296                 lock_super(sb);
297                 sb->s_flags &= ~MS_ACTIVE;
298                 /* bad name - it should be evict_inodes() */
299                 invalidate_inodes(sb);
300                 lock_kernel();
301
302                 if (sop->write_super && sb->s_dirt)
303                         sop->write_super(sb);
304                 if (sop->put_super)
305                         sop->put_super(sb);
306
307                 /* Forget any remaining inodes */
308                 if (invalidate_inodes(sb)) {
309                         printk("VFS: Busy inodes after unmount of %s. "
310                            "Self-destruct in 5 seconds.  Have a nice day...\n",
311                            sb->s_id);
312                 }
313
314                 unlock_kernel();
315                 unlock_super(sb);
316         }
317         spin_lock(&sb_lock);
318         /* should be initialized for __put_super_and_need_restart() */
319         list_del_init(&sb->s_list);
320         list_del(&sb->s_instances);
321         spin_unlock(&sb_lock);
322         up_write(&sb->s_umount);
323 }
324
325 EXPORT_SYMBOL(generic_shutdown_super);
326
327 /**
328  *      sget    -       find or create a superblock
329  *      @type:  filesystem type superblock should belong to
330  *      @test:  comparison callback
331  *      @set:   setup callback
332  *      @data:  argument to each of them
333  */
334 struct super_block *sget(struct file_system_type *type,
335                         int (*test)(struct super_block *,void *),
336                         int (*set)(struct super_block *,void *),
337                         void *data)
338 {
339         struct super_block *s = NULL;
340         struct list_head *p;
341         int err;
342
343 retry:
344         spin_lock(&sb_lock);
345         if (test) list_for_each(p, &type->fs_supers) {
346                 struct super_block *old;
347                 old = list_entry(p, struct super_block, s_instances);
348                 if (!test(old, data))
349                         continue;
350                 if (!grab_super(old))
351                         goto retry;
352                 if (s)
353                         destroy_super(s);
354                 return old;
355         }
356         if (!s) {
357                 spin_unlock(&sb_lock);
358                 s = alloc_super(type);
359                 if (!s)
360                         return ERR_PTR(-ENOMEM);
361                 goto retry;
362         }
363                 
364         err = set(s, data);
365         if (err) {
366                 spin_unlock(&sb_lock);
367                 destroy_super(s);
368                 return ERR_PTR(err);
369         }
370         s->s_type = type;
371         strlcpy(s->s_id, type->name, sizeof(s->s_id));
372         list_add_tail(&s->s_list, &super_blocks);
373         list_add(&s->s_instances, &type->fs_supers);
374         spin_unlock(&sb_lock);
375         get_filesystem(type);
376         return s;
377 }
378
379 EXPORT_SYMBOL(sget);
380
381 void drop_super(struct super_block *sb)
382 {
383         up_read(&sb->s_umount);
384         put_super(sb);
385 }
386
387 EXPORT_SYMBOL(drop_super);
388
389 static inline void write_super(struct super_block *sb)
390 {
391         lock_super(sb);
392         if (sb->s_root && sb->s_dirt)
393                 if (sb->s_op->write_super)
394                         sb->s_op->write_super(sb);
395         unlock_super(sb);
396 }
397
398 /*
399  * Note: check the dirty flag before waiting, so we don't
400  * hold up the sync while mounting a device. (The newly
401  * mounted device won't need syncing.)
402  */
403 void sync_supers(void)
404 {
405         struct super_block *sb;
406
407         spin_lock(&sb_lock);
408 restart:
409         list_for_each_entry(sb, &super_blocks, s_list) {
410                 if (sb->s_dirt) {
411                         sb->s_count++;
412                         spin_unlock(&sb_lock);
413                         down_read(&sb->s_umount);
414                         write_super(sb);
415                         up_read(&sb->s_umount);
416                         spin_lock(&sb_lock);
417                         if (__put_super_and_need_restart(sb))
418                                 goto restart;
419                 }
420         }
421         spin_unlock(&sb_lock);
422 }
423
424 /*
425  * Call the ->sync_fs super_op against all filesytems which are r/w and
426  * which implement it.
427  *
428  * This operation is careful to avoid the livelock which could easily happen
429  * if two or more filesystems are being continuously dirtied.  s_need_sync_fs
430  * is used only here.  We set it against all filesystems and then clear it as
431  * we sync them.  So redirtied filesystems are skipped.
432  *
433  * But if process A is currently running sync_filesytems and then process B
434  * calls sync_filesystems as well, process B will set all the s_need_sync_fs
435  * flags again, which will cause process A to resync everything.  Fix that with
436  * a local mutex.
437  *
438  * (Fabian) Avoid sync_fs with clean fs & wait mode 0
439  */
440 void sync_filesystems(int wait)
441 {
442         struct super_block *sb;
443         static DEFINE_MUTEX(mutex);
444
445         mutex_lock(&mutex);             /* Could be down_interruptible */
446         spin_lock(&sb_lock);
447         list_for_each_entry(sb, &super_blocks, s_list) {
448                 if (!sb->s_op->sync_fs)
449                         continue;
450                 if (sb->s_flags & MS_RDONLY)
451                         continue;
452                 sb->s_need_sync_fs = 1;
453         }
454
455 restart:
456         list_for_each_entry(sb, &super_blocks, s_list) {
457                 if (!sb->s_need_sync_fs)
458                         continue;
459                 sb->s_need_sync_fs = 0;
460                 if (sb->s_flags & MS_RDONLY)
461                         continue;       /* hm.  Was remounted r/o meanwhile */
462                 sb->s_count++;
463                 spin_unlock(&sb_lock);
464                 down_read(&sb->s_umount);
465                 if (sb->s_root && (wait || sb->s_dirt))
466                         sb->s_op->sync_fs(sb, wait);
467                 up_read(&sb->s_umount);
468                 /* restart only when sb is no longer on the list */
469                 spin_lock(&sb_lock);
470                 if (__put_super_and_need_restart(sb))
471                         goto restart;
472         }
473         spin_unlock(&sb_lock);
474         mutex_unlock(&mutex);
475 }
476
477 /**
478  *      get_super - get the superblock of a device
479  *      @bdev: device to get the superblock for
480  *      
481  *      Scans the superblock list and finds the superblock of the file system
482  *      mounted on the device given. %NULL is returned if no match is found.
483  */
484
485 struct super_block * get_super(struct block_device *bdev)
486 {
487         struct super_block *sb;
488
489         if (!bdev)
490                 return NULL;
491
492         spin_lock(&sb_lock);
493 rescan:
494         list_for_each_entry(sb, &super_blocks, s_list) {
495                 if (sb->s_bdev == bdev) {
496                         sb->s_count++;
497                         spin_unlock(&sb_lock);
498                         down_read(&sb->s_umount);
499                         if (sb->s_root)
500                                 return sb;
501                         up_read(&sb->s_umount);
502                         /* restart only when sb is no longer on the list */
503                         spin_lock(&sb_lock);
504                         if (__put_super_and_need_restart(sb))
505                                 goto rescan;
506                 }
507         }
508         spin_unlock(&sb_lock);
509         return NULL;
510 }
511
512 EXPORT_SYMBOL(get_super);
513  
514 struct super_block * user_get_super(dev_t dev)
515 {
516         struct super_block *sb;
517
518         spin_lock(&sb_lock);
519 rescan:
520         list_for_each_entry(sb, &super_blocks, s_list) {
521                 if (sb->s_dev ==  dev) {
522                         sb->s_count++;
523                         spin_unlock(&sb_lock);
524                         down_read(&sb->s_umount);
525                         if (sb->s_root)
526                                 return sb;
527                         up_read(&sb->s_umount);
528                         /* restart only when sb is no longer on the list */
529                         spin_lock(&sb_lock);
530                         if (__put_super_and_need_restart(sb))
531                                 goto rescan;
532                 }
533         }
534         spin_unlock(&sb_lock);
535         return NULL;
536 }
537
538 asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf)
539 {
540         struct super_block *s;
541         struct ustat tmp;
542         struct kstatfs sbuf;
543         int err = -EINVAL;
544
545         s = user_get_super(new_decode_dev(dev));
546         if (s == NULL)
547                 goto out;
548         err = vfs_statfs(s->s_root, &sbuf);
549         drop_super(s);
550         if (err)
551                 goto out;
552
553         memset(&tmp,0,sizeof(struct ustat));
554         tmp.f_tfree = sbuf.f_bfree;
555         tmp.f_tinode = sbuf.f_ffree;
556
557         err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
558 out:
559         return err;
560 }
561
562 /**
563  *      mark_files_ro
564  *      @sb: superblock in question
565  *
566  *      All files are marked read/only.  We don't care about pending
567  *      delete files so this should be used in 'force' mode only
568  */
569
570 static void mark_files_ro(struct super_block *sb)
571 {
572         struct file *f;
573
574         file_list_lock();
575         list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
576                 if (S_ISREG(f->f_path.dentry->d_inode->i_mode) && file_count(f))
577                         f->f_mode &= ~FMODE_WRITE;
578         }
579         file_list_unlock();
580 }
581
582 /**
583  *      do_remount_sb - asks filesystem to change mount options.
584  *      @sb:    superblock in question
585  *      @flags: numeric part of options
586  *      @data:  the rest of options
587  *      @force: whether or not to force the change
588  *
589  *      Alters the mount options of a mounted file system.
590  */
591 int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
592 {
593         int retval;
594         
595 #ifdef CONFIG_BLOCK
596         if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
597                 return -EACCES;
598 #endif
599         if (flags & MS_RDONLY)
600                 acct_auto_close(sb);
601         shrink_dcache_sb(sb);
602         fsync_super(sb);
603
604         /* If we are remounting RDONLY and current sb is read/write,
605            make sure there are no rw files opened */
606         if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
607                 if (force)
608                         mark_files_ro(sb);
609                 else if (!fs_may_remount_ro(sb))
610                         return -EBUSY;
611         }
612
613         if (sb->s_op->remount_fs) {
614                 lock_super(sb);
615                 retval = sb->s_op->remount_fs(sb, &flags, data);
616                 unlock_super(sb);
617                 if (retval)
618                         return retval;
619         }
620         sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
621         return 0;
622 }
623
624 static void do_emergency_remount(unsigned long foo)
625 {
626         struct super_block *sb;
627
628         spin_lock(&sb_lock);
629         list_for_each_entry(sb, &super_blocks, s_list) {
630                 sb->s_count++;
631                 spin_unlock(&sb_lock);
632                 down_read(&sb->s_umount);
633                 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
634                         /*
635                          * ->remount_fs needs lock_kernel().
636                          *
637                          * What lock protects sb->s_flags??
638                          */
639                         lock_kernel();
640                         do_remount_sb(sb, MS_RDONLY, NULL, 1);
641                         unlock_kernel();
642                 }
643                 drop_super(sb);
644                 spin_lock(&sb_lock);
645         }
646         spin_unlock(&sb_lock);
647         printk("Emergency Remount complete\n");
648 }
649
650 void emergency_remount(void)
651 {
652         pdflush_operation(do_emergency_remount, 0);
653 }
654
655 /*
656  * Unnamed block devices are dummy devices used by virtual
657  * filesystems which don't use real block-devices.  -- jrs
658  */
659
660 static struct idr unnamed_dev_idr;
661 static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
662
663 int set_anon_super(struct super_block *s, void *data)
664 {
665         int dev;
666         int error;
667
668  retry:
669         if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0)
670                 return -ENOMEM;
671         spin_lock(&unnamed_dev_lock);
672         error = idr_get_new(&unnamed_dev_idr, NULL, &dev);
673         spin_unlock(&unnamed_dev_lock);
674         if (error == -EAGAIN)
675                 /* We raced and lost with another CPU. */
676                 goto retry;
677         else if (error)
678                 return -EAGAIN;
679
680         if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
681                 spin_lock(&unnamed_dev_lock);
682                 idr_remove(&unnamed_dev_idr, dev);
683                 spin_unlock(&unnamed_dev_lock);
684                 return -EMFILE;
685         }
686         s->s_dev = MKDEV(0, dev & MINORMASK);
687         return 0;
688 }
689
690 EXPORT_SYMBOL(set_anon_super);
691
692 void kill_anon_super(struct super_block *sb)
693 {
694         int slot = MINOR(sb->s_dev);
695
696         generic_shutdown_super(sb);
697         spin_lock(&unnamed_dev_lock);
698         idr_remove(&unnamed_dev_idr, slot);
699         spin_unlock(&unnamed_dev_lock);
700 }
701
702 EXPORT_SYMBOL(kill_anon_super);
703
704 void __init unnamed_dev_init(void)
705 {
706         idr_init(&unnamed_dev_idr);
707 }
708
709 void kill_litter_super(struct super_block *sb)
710 {
711         if (sb->s_root)
712                 d_genocide(sb->s_root);
713         kill_anon_super(sb);
714 }
715
716 EXPORT_SYMBOL(kill_litter_super);
717
718 #ifdef CONFIG_BLOCK
719 static int set_bdev_super(struct super_block *s, void *data)
720 {
721         s->s_bdev = data;
722         s->s_dev = s->s_bdev->bd_dev;
723         return 0;
724 }
725
726 static int test_bdev_super(struct super_block *s, void *data)
727 {
728         return (void *)s->s_bdev == data;
729 }
730
731 static void bdev_uevent(struct block_device *bdev, enum kobject_action action)
732 {
733         if (bdev->bd_disk) {
734                 if (bdev->bd_part)
735                         kobject_uevent(&bdev->bd_part->kobj, action);
736                 else
737                         kobject_uevent(&bdev->bd_disk->kobj, action);
738         }
739 }
740
741 int get_sb_bdev(struct file_system_type *fs_type,
742         int flags, const char *dev_name, void *data,
743         int (*fill_super)(struct super_block *, void *, int),
744         struct vfsmount *mnt)
745 {
746         struct block_device *bdev;
747         struct super_block *s;
748         int error = 0;
749
750         bdev = open_bdev_excl(dev_name, flags, fs_type);
751         if (IS_ERR(bdev))
752                 return PTR_ERR(bdev);
753
754         /*
755          * once the super is inserted into the list by sget, s_umount
756          * will protect the lockfs code from trying to start a snapshot
757          * while we are mounting
758          */
759         down(&bdev->bd_mount_sem);
760         s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
761         up(&bdev->bd_mount_sem);
762         if (IS_ERR(s))
763                 goto error_s;
764
765         if (s->s_root) {
766                 if ((flags ^ s->s_flags) & MS_RDONLY) {
767                         up_write(&s->s_umount);
768                         deactivate_super(s);
769                         error = -EBUSY;
770                         goto error_bdev;
771                 }
772
773                 close_bdev_excl(bdev);
774         } else {
775                 char b[BDEVNAME_SIZE];
776
777                 s->s_flags = flags;
778                 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
779                 sb_set_blocksize(s, block_size(bdev));
780                 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
781                 if (error) {
782                         up_write(&s->s_umount);
783                         deactivate_super(s);
784                         goto error;
785                 }
786
787                 s->s_flags |= MS_ACTIVE;
788                 bdev_uevent(bdev, KOBJ_MOUNT);
789         }
790
791         return simple_set_mnt(mnt, s);
792
793 error_s:
794         error = PTR_ERR(s);
795 error_bdev:
796         close_bdev_excl(bdev);
797 error:
798         return error;
799 }
800
801 EXPORT_SYMBOL(get_sb_bdev);
802
803 void kill_block_super(struct super_block *sb)
804 {
805         struct block_device *bdev = sb->s_bdev;
806
807         bdev_uevent(bdev, KOBJ_UMOUNT);
808         generic_shutdown_super(sb);
809         sync_blockdev(bdev);
810         close_bdev_excl(bdev);
811 }
812
813 EXPORT_SYMBOL(kill_block_super);
814 #endif
815
816 int get_sb_nodev(struct file_system_type *fs_type,
817         int flags, void *data,
818         int (*fill_super)(struct super_block *, void *, int),
819         struct vfsmount *mnt)
820 {
821         int error;
822         struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
823
824         if (IS_ERR(s))
825                 return PTR_ERR(s);
826
827         s->s_flags = flags;
828
829         error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
830         if (error) {
831                 up_write(&s->s_umount);
832                 deactivate_super(s);
833                 return error;
834         }
835         s->s_flags |= MS_ACTIVE;
836         return simple_set_mnt(mnt, s);
837 }
838
839 EXPORT_SYMBOL(get_sb_nodev);
840
841 static int compare_single(struct super_block *s, void *p)
842 {
843         return 1;
844 }
845
846 int get_sb_single(struct file_system_type *fs_type,
847         int flags, void *data,
848         int (*fill_super)(struct super_block *, void *, int),
849         struct vfsmount *mnt)
850 {
851         struct super_block *s;
852         int error;
853
854         s = sget(fs_type, compare_single, set_anon_super, NULL);
855         if (IS_ERR(s))
856                 return PTR_ERR(s);
857         if (!s->s_root) {
858                 s->s_flags = flags;
859                 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
860                 if (error) {
861                         up_write(&s->s_umount);
862                         deactivate_super(s);
863                         return error;
864                 }
865                 s->s_flags |= MS_ACTIVE;
866         }
867         do_remount_sb(s, flags, data, 0);
868         return simple_set_mnt(mnt, s);
869 }
870
871 EXPORT_SYMBOL(get_sb_single);
872
873 struct vfsmount *
874 vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
875 {
876         struct vfsmount *mnt;
877         struct super_block *sb;
878         char *secdata = NULL;
879         int error;
880
881         if (!type)
882                 return ERR_PTR(-ENODEV);
883
884         error = -ENOMEM;
885         mnt = alloc_vfsmnt(name);
886         if (!mnt)
887                 goto out;
888
889         if (data) {
890                 secdata = alloc_secdata();
891                 if (!secdata)
892                         goto out_mnt;
893
894                 error = security_sb_copy_data(type, data, secdata);
895                 if (error)
896                         goto out_free_secdata;
897         }
898
899         error = type->get_sb(type, flags, name, data, mnt);
900         if (error < 0)
901                 goto out_free_secdata;
902
903         sb = mnt->mnt_sb;
904         error = -EPERM;
905         if (!vx_capable(CAP_SYS_ADMIN, VXC_BINARY_MOUNT) && !sb->s_bdev &&
906                 (sb->s_magic != PROC_SUPER_MAGIC) &&
907                 (sb->s_magic != DEVPTS_SUPER_MAGIC))
908                 goto out_sb;
909
910         error = security_sb_kern_mount(sb, secdata);
911         if (error)
912                 goto out_sb;
913
914         mnt->mnt_mountpoint = mnt->mnt_root;
915         mnt->mnt_parent = mnt;
916         up_write(&mnt->mnt_sb->s_umount);
917         free_secdata(secdata);
918         return mnt;
919 out_sb:
920         dput(mnt->mnt_root);
921         up_write(&mnt->mnt_sb->s_umount);
922         deactivate_super(mnt->mnt_sb);
923 out_free_secdata:
924         free_secdata(secdata);
925 out_mnt:
926         free_vfsmnt(mnt);
927 out:
928         return ERR_PTR(error);
929 }
930
931 EXPORT_SYMBOL_GPL(vfs_kern_mount);
932
933 struct vfsmount *
934 do_kern_mount(const char *fstype, int flags, const char *name, void *data)
935 {
936         struct file_system_type *type = get_fs_type(fstype);
937         struct vfsmount *mnt;
938
939         if (!type)
940                 return ERR_PTR(-ENODEV);
941
942         mnt = ERR_PTR(-EPERM);
943         if ((type->fs_flags & FS_BINARY_MOUNTDATA) &&
944                 !vx_capable(CAP_SYS_ADMIN, VXC_BINARY_MOUNT))
945                 goto out_put;
946
947         mnt = vfs_kern_mount(type, flags, name, data);
948 out_put:
949         put_filesystem(type);
950         return mnt;
951 }
952
953 struct vfsmount *kern_mount(struct file_system_type *type)
954 {
955         return vfs_kern_mount(type, 0, type->name, NULL);
956 }
957
958 EXPORT_SYMBOL(kern_mount);