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