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