make oldconfig will rebuild these...
[linux-2.4.21-pre4.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/slab.h>
25 #include <linux/locks.h>
26 #include <linux/smp_lock.h>
27 #include <linux/devfs_fs_kernel.h>
28 #include <linux/major.h>
29 #include <linux/acct.h>
30
31 #include <asm/uaccess.h>
32
33 #include <linux/kmod.h>
34 #define __NO_VERSION__
35 #include <linux/module.h>
36
37 LIST_HEAD(super_blocks);
38 spinlock_t sb_lock = SPIN_LOCK_UNLOCKED;
39
40 /*
41  * Handling of filesystem drivers list.
42  * Rules:
43  *      Inclusion to/removals from/scanning of list are protected by spinlock.
44  *      During the unload module must call unregister_filesystem().
45  *      We can access the fields of list element if:
46  *              1) spinlock is held or
47  *              2) we hold the reference to the module.
48  *      The latter can be guaranteed by call of try_inc_mod_count(); if it
49  *      returned 0 we must skip the element, otherwise we got the reference.
50  *      Once the reference is obtained we can drop the spinlock.
51  */
52
53 static struct file_system_type *file_systems;
54 static rwlock_t file_systems_lock = RW_LOCK_UNLOCKED;
55
56 /* WARNING: This can be used only if we _already_ own a reference */
57 static void get_filesystem(struct file_system_type *fs)
58 {
59         if (fs->owner)
60                 __MOD_INC_USE_COUNT(fs->owner);
61 }
62
63 static void put_filesystem(struct file_system_type *fs)
64 {
65         if (fs->owner)
66                 __MOD_DEC_USE_COUNT(fs->owner);
67 }
68
69 static struct file_system_type **find_filesystem(const char *name)
70 {
71         struct file_system_type **p;
72         for (p=&file_systems; *p; p=&(*p)->next)
73                 if (strcmp((*p)->name,name) == 0)
74                         break;
75         return p;
76 }
77
78 /**
79  *      register_filesystem - register a new filesystem
80  *      @fs: the file system structure
81  *
82  *      Adds the file system passed to the list of file systems the kernel
83  *      is aware of for mount and other syscalls. Returns 0 on success,
84  *      or a negative errno code on an error.
85  *
86  *      The &struct file_system_type that is passed is linked into the kernel 
87  *      structures and must not be freed until the file system has been
88  *      unregistered.
89  */
90  
91 int register_filesystem(struct file_system_type * fs)
92 {
93         int res = 0;
94         struct file_system_type ** p;
95
96         if (!fs)
97                 return -EINVAL;
98         if (fs->next)
99                 return -EBUSY;
100         INIT_LIST_HEAD(&fs->fs_supers);
101         write_lock(&file_systems_lock);
102         p = find_filesystem(fs->name);
103         if (*p)
104                 res = -EBUSY;
105         else
106                 *p = fs;
107         write_unlock(&file_systems_lock);
108         return res;
109 }
110
111 /**
112  *      unregister_filesystem - unregister a file system
113  *      @fs: filesystem to unregister
114  *
115  *      Remove a file system that was previously successfully registered
116  *      with the kernel. An error is returned if the file system is not found.
117  *      Zero is returned on a success.
118  *      
119  *      Once this function has returned the &struct file_system_type structure
120  *      may be freed or reused.
121  */
122  
123 int unregister_filesystem(struct file_system_type * fs)
124 {
125         struct file_system_type ** tmp;
126
127         write_lock(&file_systems_lock);
128         tmp = &file_systems;
129         while (*tmp) {
130                 if (fs == *tmp) {
131                         *tmp = fs->next;
132                         fs->next = NULL;
133                         write_unlock(&file_systems_lock);
134                         return 0;
135                 }
136                 tmp = &(*tmp)->next;
137         }
138         write_unlock(&file_systems_lock);
139         return -EINVAL;
140 }
141
142 static int fs_index(const char * __name)
143 {
144         struct file_system_type * tmp;
145         char * name;
146         int err, index;
147
148         name = getname(__name);
149         err = PTR_ERR(name);
150         if (IS_ERR(name))
151                 return err;
152
153         err = -EINVAL;
154         read_lock(&file_systems_lock);
155         for (tmp=file_systems, index=0 ; tmp ; tmp=tmp->next, index++) {
156                 if (strcmp(tmp->name,name) == 0) {
157                         err = index;
158                         break;
159                 }
160         }
161         read_unlock(&file_systems_lock);
162         putname(name);
163         return err;
164 }
165
166 static int fs_name(unsigned int index, char * buf)
167 {
168         struct file_system_type * tmp;
169         int len, res;
170
171         read_lock(&file_systems_lock);
172         for (tmp = file_systems; tmp; tmp = tmp->next, index--)
173                 if (index <= 0 && try_inc_mod_count(tmp->owner))
174                                 break;
175         read_unlock(&file_systems_lock);
176         if (!tmp)
177                 return -EINVAL;
178
179         /* OK, we got the reference, so we can safely block */
180         len = strlen(tmp->name) + 1;
181         res = copy_to_user(buf, tmp->name, len) ? -EFAULT : 0;
182         put_filesystem(tmp);
183         return res;
184 }
185
186 static int fs_maxindex(void)
187 {
188         struct file_system_type * tmp;
189         int index;
190
191         read_lock(&file_systems_lock);
192         for (tmp = file_systems, index = 0 ; tmp ; tmp = tmp->next, index++)
193                 ;
194         read_unlock(&file_systems_lock);
195         return index;
196 }
197
198 /*
199  * Whee.. Weird sysv syscall. 
200  */
201 asmlinkage long sys_sysfs(int option, unsigned long arg1, unsigned long arg2)
202 {
203         int retval = -EINVAL;
204
205         switch (option) {
206                 case 1:
207                         retval = fs_index((const char *) arg1);
208                         break;
209
210                 case 2:
211                         retval = fs_name(arg1, (char *) arg2);
212                         break;
213
214                 case 3:
215                         retval = fs_maxindex();
216                         break;
217         }
218         return retval;
219 }
220
221 int get_filesystem_list(char * buf)
222 {
223         int len = 0;
224         struct file_system_type * tmp;
225
226         read_lock(&file_systems_lock);
227         tmp = file_systems;
228         while (tmp && len < PAGE_SIZE - 80) {
229                 len += sprintf(buf+len, "%s\t%s\n",
230                         (tmp->fs_flags & FS_REQUIRES_DEV) ? "" : "nodev",
231                         tmp->name);
232                 tmp = tmp->next;
233         }
234         read_unlock(&file_systems_lock);
235         return len;
236 }
237
238 struct file_system_type *get_fs_type(const char *name)
239 {
240         struct file_system_type *fs;
241         
242         read_lock(&file_systems_lock);
243         fs = *(find_filesystem(name));
244         if (fs && !try_inc_mod_count(fs->owner))
245                 fs = NULL;
246         read_unlock(&file_systems_lock);
247         if (!fs && (request_module(name) == 0)) {
248                 read_lock(&file_systems_lock);
249                 fs = *(find_filesystem(name));
250                 if (fs && !try_inc_mod_count(fs->owner))
251                         fs = NULL;
252                 read_unlock(&file_systems_lock);
253         }
254         return fs;
255 }
256
257 /**
258  *      alloc_super     -       create new superblock
259  *
260  *      Allocates and initializes a new &struct super_block.  alloc_super()
261  *      returns a pointer new superblock or %NULL if allocation had failed.
262  */
263 static struct super_block *alloc_super(void)
264 {
265         static struct super_operations empty_sops = {};
266         struct super_block *s = kmalloc(sizeof(struct super_block),  GFP_USER);
267         if (s) {
268                 memset(s, 0, sizeof(struct super_block));
269                 INIT_LIST_HEAD(&s->s_dirty);
270                 INIT_LIST_HEAD(&s->s_locked_inodes);
271                 INIT_LIST_HEAD(&s->s_files);
272                 INIT_LIST_HEAD(&s->s_instances);
273                 init_rwsem(&s->s_umount);
274                 sema_init(&s->s_lock, 1);
275                 down_write(&s->s_umount);
276                 s->s_count = S_BIAS;
277                 atomic_set(&s->s_active, 1);
278                 sema_init(&s->s_vfs_rename_sem,1);
279                 sema_init(&s->s_nfsd_free_path_sem,1);
280                 sema_init(&s->s_dquot.dqio_sem, 1);
281                 sema_init(&s->s_dquot.dqoff_sem, 1);
282                 s->s_maxbytes = MAX_NON_LFS;
283                 s->s_op = &empty_sops;
284         }
285         return s;
286 }
287
288 /**
289  *      destroy_super   -       frees a superblock
290  *      @s: superblock to free
291  *
292  *      Frees a superblock.
293  */
294 static inline void destroy_super(struct super_block *s)
295 {
296         kfree(s);
297 }
298
299 /* Superblock refcounting  */
300
301 /**
302  *      deactivate_super        -       turn an active reference into temporary
303  *      @s: superblock to deactivate
304  *
305  *      Turns an active reference into temporary one.  Returns 0 if there are
306  *      other active references, 1 if we had deactivated the last one.
307  */
308 static inline int deactivate_super(struct super_block *s)
309 {
310         if (!atomic_dec_and_lock(&s->s_active, &sb_lock))
311                 return 0;
312         s->s_count -= S_BIAS-1;
313         spin_unlock(&sb_lock);
314         return 1;
315 }
316
317 /**
318  *      put_super       -       drop a temporary reference to superblock
319  *      @s: superblock in question
320  *
321  *      Drops a temporary reference, frees superblock if there's no
322  *      references left.
323  */
324 static inline void put_super(struct super_block *s)
325 {
326         spin_lock(&sb_lock);
327         if (!--s->s_count)
328                 destroy_super(s);
329         spin_unlock(&sb_lock);
330 }
331
332 /**
333  *      grab_super      - acquire an active reference
334  *      @s      - reference we are trying to make active
335  *
336  *      Tries to acquire an active reference.  grab_super() is used when we
337  *      had just found a superblock in super_blocks or fs_type->fs_supers
338  *      and want to turn it into a full-blown active reference.  grab_super()
339  *      is called with sb_lock held and drops it.  Returns 1 in case of
340  *      success, 0 if we had failed (superblock contents was already dead or
341  *      dying when grab_super() had been called).
342  */
343 static int grab_super(struct super_block *s)
344 {
345         s->s_count++;
346         spin_unlock(&sb_lock);
347         down_write(&s->s_umount);
348         if (s->s_root) {
349                 spin_lock(&sb_lock);
350                 if (s->s_count > S_BIAS) {
351                         atomic_inc(&s->s_active);
352                         s->s_count--;
353                         spin_unlock(&sb_lock);
354                         return 1;
355                 }
356                 spin_unlock(&sb_lock);
357         }
358         up_write(&s->s_umount);
359         put_super(s);
360         return 0;
361 }
362  
363 /**
364  *      insert_super    -       put superblock on the lists
365  *      @s:     superblock in question
366  *      @type:  filesystem type it will belong to
367  *
368  *      Associates superblock with fs type and puts it on per-type and global
369  *      superblocks' lists.  Should be called with sb_lock held; drops it.
370  */
371 static void insert_super(struct super_block *s, struct file_system_type *type)
372 {
373         s->s_type = type;
374         list_add(&s->s_list, super_blocks.prev);
375         list_add(&s->s_instances, &type->fs_supers);
376         spin_unlock(&sb_lock);
377         get_filesystem(type);
378 }
379
380 static void put_anon_dev(kdev_t dev);
381
382 /**
383  *      remove_super    -       makes superblock unreachable
384  *      @s:     superblock in question
385  *
386  *      Removes superblock from the lists, unlocks it, drop the reference
387  *      and releases the hosting device.  @s should have no active
388  *      references by that time and after remove_super() it's essentially
389  *      in rundown mode - all remaining references are temporary, no new
390  *      reference of any sort are going to appear and all holders of
391  *      temporary ones will eventually drop them.  At that point superblock
392  *      itself will be destroyed; all its contents is already gone.
393  */
394 static void remove_super(struct super_block *s)
395 {
396         kdev_t dev = s->s_dev;
397         struct block_device *bdev = s->s_bdev;
398         struct file_system_type *fs = s->s_type;
399
400         spin_lock(&sb_lock);
401         list_del(&s->s_list);
402         list_del(&s->s_instances);
403         spin_unlock(&sb_lock);
404         up_write(&s->s_umount);
405         put_super(s);
406         put_filesystem(fs);
407         if (bdev)
408                 blkdev_put(bdev, BDEV_FS);
409         else
410                 put_anon_dev(dev);
411 }
412
413 struct vfsmount *alloc_vfsmnt(char *name);
414 void free_vfsmnt(struct vfsmount *mnt);
415
416 static inline struct super_block * find_super(kdev_t dev)
417 {
418         struct list_head *p;
419
420         list_for_each(p, &super_blocks) {
421                 struct super_block * s = sb_entry(p);
422                 if (s->s_dev == dev) {
423                         s->s_count++;
424                         return s;
425                 }
426         }
427         return NULL;
428 }
429
430 void drop_super(struct super_block *sb)
431 {
432         up_read(&sb->s_umount);
433         put_super(sb);
434 }
435
436 static inline void write_super(struct super_block *sb)
437 {
438         lock_super(sb);
439         if (sb->s_root && sb->s_dirt)
440                 if (sb->s_op && sb->s_op->write_super)
441                         sb->s_op->write_super(sb);
442         unlock_super(sb);
443 }
444
445 /*
446  * Note: check the dirty flag before waiting, so we don't
447  * hold up the sync while mounting a device. (The newly
448  * mounted device won't need syncing.)
449  */
450 void sync_supers(kdev_t dev, int wait)
451 {
452         struct super_block * sb;
453
454         if (dev) {
455                 sb = get_super(dev);
456                 if (sb) {
457                         if (sb->s_dirt)
458                                 write_super(sb);
459                         if (wait && sb->s_op && sb->s_op->sync_fs)
460                                 sb->s_op->sync_fs(sb);
461                         drop_super(sb);
462                 }
463                 return;
464         }
465 restart:
466         spin_lock(&sb_lock);
467         sb = sb_entry(super_blocks.next);
468         while (sb != sb_entry(&super_blocks))
469                 if (sb->s_dirt) {
470                         sb->s_count++;
471                         spin_unlock(&sb_lock);
472                         down_read(&sb->s_umount);
473                         write_super(sb);
474                         if (wait && sb->s_root && sb->s_op && sb->s_op->sync_fs)
475                                 sb->s_op->sync_fs(sb);
476                         drop_super(sb);
477                         goto restart;
478                 } else
479                         sb = sb_entry(sb->s_list.next);
480         spin_unlock(&sb_lock);
481 }
482
483 /**
484  *      get_super       -       get the superblock of a device
485  *      @dev: device to get the superblock for
486  *      
487  *      Scans the superblock list and finds the superblock of the file system
488  *      mounted on the device given. %NULL is returned if no match is found.
489  */
490  
491 struct super_block * get_super(kdev_t dev)
492 {
493         struct super_block * s;
494
495         if (!dev)
496                 return NULL;
497 restart:
498         spin_lock(&sb_lock);
499         s = find_super(dev);
500         if (s) {
501                 spin_unlock(&sb_lock);
502                 down_read(&s->s_umount);
503                 if (s->s_root)
504                         return s;
505                 drop_super(s);
506                 goto restart;
507         }
508         spin_unlock(&sb_lock);
509         return NULL;
510 }
511
512 asmlinkage long sys_ustat(dev_t dev, struct ustat * ubuf)
513 {
514         struct super_block *s;
515         struct ustat tmp;
516         struct statfs sbuf;
517         int err = -EINVAL;
518
519         s = get_super(to_kdev_t(dev));
520         if (s == NULL)
521                 goto out;
522         err = vfs_statfs(s, &sbuf);
523         drop_super(s);
524         if (err)
525                 goto out;
526
527         memset(&tmp,0,sizeof(struct ustat));
528         tmp.f_tfree = sbuf.f_bfree;
529         tmp.f_tinode = sbuf.f_ffree;
530
531         err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
532 out:
533         return err;
534 }
535
536 /**
537  *      do_remount_sb   -       asks filesystem to change mount options.
538  *      @sb:    superblock in question
539  *      @flags: numeric part of options
540  *      @data:  the rest of options
541  *
542  *      Alters the mount options of a mounted file system.
543  */
544 int do_remount_sb(struct super_block *sb, int flags, void *data)
545 {
546         int retval;
547         
548         if (!(flags & MS_RDONLY) && sb->s_dev && is_read_only(sb->s_dev))
549                 return -EACCES;
550                 /*flags |= MS_RDONLY;*/
551         if (flags & MS_RDONLY)
552                 acct_auto_close(sb->s_dev);
553         shrink_dcache_sb(sb);
554         fsync_super(sb);
555         /* If we are remounting RDONLY, make sure there are no rw files open */
556         if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY))
557                 if (!fs_may_remount_ro(sb))
558                         return -EBUSY;
559         if (sb->s_op && sb->s_op->remount_fs) {
560                 lock_super(sb);
561                 retval = sb->s_op->remount_fs(sb, &flags, data);
562                 unlock_super(sb);
563                 if (retval)
564                         return retval;
565         }
566         sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
567         return 0;
568 }
569
570 /*
571  * Unnamed block devices are dummy devices used by virtual
572  * filesystems which don't use real block-devices.  -- jrs
573  */
574
575 enum {Max_anon = 256};
576 static unsigned long unnamed_dev_in_use[Max_anon/(8*sizeof(unsigned long))];
577 static spinlock_t unnamed_dev_lock = SPIN_LOCK_UNLOCKED;/* protects the above */
578
579 /**
580  *      put_anon_dev    -       release anonymous device number.
581  *      @dev:   device in question
582  */
583 static void put_anon_dev(kdev_t dev)
584 {
585         spin_lock(&unnamed_dev_lock);
586         clear_bit(MINOR(dev), unnamed_dev_in_use);
587         spin_unlock(&unnamed_dev_lock);
588 }
589
590 /**
591  *      get_anon_super  -       allocate a superblock for non-device fs
592  *      @type:          filesystem type
593  *      @compare:       check if existing superblock is what we want
594  *      @data:          argument for @compare.
595  *
596  *      get_anon_super is a helper for non-blockdevice filesystems.
597  *      It either finds and returns one of the superblocks of given type
598  *      (if it can find one that would satisfy caller) or creates a new
599  *      one.  In the either case we return an active reference to superblock
600  *      with ->s_umount locked.  If superblock is new it gets a new
601  *      anonymous device allocated for it and is inserted into lists -
602  *      other initialization is left to caller.
603  *
604  *      Rather than duplicating all that logics every time when
605  *      we want something that doesn't fit "nodev" and "single" we pull
606  *      the relevant code into common helper and let get_sb_...() call
607  *      it.
608  *
609  *      NB: get_sb_...() is going to become an fs type method, with
610  *      current ->read_super() becoming a callback used by common instances.
611  */
612 struct super_block *get_anon_super(struct file_system_type *type,
613         int (*compare)(struct super_block *,void *), void *data)
614 {
615         struct super_block *s = alloc_super();
616         kdev_t dev;
617         struct list_head *p;
618
619         if (!s)
620                 return ERR_PTR(-ENOMEM);
621
622 retry:
623         spin_lock(&sb_lock);
624         if (compare) list_for_each(p, &type->fs_supers) {
625                 struct super_block *old;
626                 old = list_entry(p, struct super_block, s_instances);
627                 if (!compare(old, data))
628                         continue;
629                 if (!grab_super(old))
630                         goto retry;
631                 destroy_super(s);
632                 return old;
633         }
634
635         spin_lock(&unnamed_dev_lock);
636         dev = find_first_zero_bit(unnamed_dev_in_use, Max_anon);
637         if (dev == Max_anon) {
638                 spin_unlock(&unnamed_dev_lock);
639                 spin_unlock(&sb_lock);
640                 destroy_super(s);
641                 return ERR_PTR(-EMFILE);
642         }
643         set_bit(dev, unnamed_dev_in_use);
644         spin_unlock(&unnamed_dev_lock);
645
646         s->s_dev = dev;
647         insert_super(s, type);
648         return s;
649 }
650
651 static struct super_block *get_sb_bdev(struct file_system_type *fs_type,
652         int flags, char *dev_name, void * data)
653 {
654         struct inode *inode;
655         struct block_device *bdev;
656         struct block_device_operations *bdops;
657         devfs_handle_t de;
658         struct super_block * s;
659         struct nameidata nd;
660         struct list_head *p;
661         kdev_t dev;
662         int error = 0;
663         mode_t mode = FMODE_READ; /* we always need it ;-) */
664
665         /* What device it is? */
666         if (!dev_name || !*dev_name)
667                 return ERR_PTR(-EINVAL);
668         error = path_lookup(dev_name, LOOKUP_FOLLOW|LOOKUP_POSITIVE, &nd);
669         if (error)
670                 return ERR_PTR(error);
671         inode = nd.dentry->d_inode;
672         error = -ENOTBLK;
673         if (!S_ISBLK(inode->i_mode))
674                 goto out;
675         error = -EACCES;
676         if (nd.mnt->mnt_flags & MNT_NODEV)
677                 goto out;
678         bd_acquire(inode);
679         bdev = inode->i_bdev;
680         de = devfs_get_handle_from_inode (inode);
681         bdops = devfs_get_ops (de);         /*  Increments module use count  */
682         if (bdops) bdev->bd_op = bdops;
683         /* Done with lookups, semaphore down */
684         dev = to_kdev_t(bdev->bd_dev);
685         if (!(flags & MS_RDONLY))
686                 mode |= FMODE_WRITE;
687         error = blkdev_get(bdev, mode, 0, BDEV_FS);
688         devfs_put_ops (de);   /*  Decrement module use count now we're safe  */
689         if (error)
690                 goto out;
691         check_disk_change(dev);
692         error = -EACCES;
693         if (!(flags & MS_RDONLY) && is_read_only(dev))
694                 goto out1;
695
696         error = -ENOMEM;
697         s = alloc_super();
698         if (!s)
699                 goto out1;
700
701         error = -EBUSY;
702 restart:
703         spin_lock(&sb_lock);
704
705         list_for_each(p, &super_blocks) {
706                 struct super_block *old = sb_entry(p);
707                 if (old->s_dev != dev)
708                         continue;
709                 if (old->s_type != fs_type ||
710                     ((flags ^ old->s_flags) & MS_RDONLY)) {
711                         spin_unlock(&sb_lock);
712                         destroy_super(s);
713                         goto out1;
714                 }
715                 if (!grab_super(old))
716                         goto restart;
717                 destroy_super(s);
718                 blkdev_put(bdev, BDEV_FS);
719                 path_release(&nd);
720                 return old;
721         }
722         s->s_dev = dev;
723         s->s_bdev = bdev;
724         s->s_flags = flags;
725         insert_super(s, fs_type);
726         if (!fs_type->read_super(s, data, flags & MS_VERBOSE ? 1 : 0))
727                 goto Einval;
728         s->s_flags |= MS_ACTIVE;
729         path_release(&nd);
730         return s;
731
732 Einval:
733         deactivate_super(s);
734         remove_super(s);
735         error = -EINVAL;
736         goto out;
737 out1:
738         blkdev_put(bdev, BDEV_FS);
739 out:
740         path_release(&nd);
741         return ERR_PTR(error);
742 }
743
744 static struct super_block *get_sb_nodev(struct file_system_type *fs_type,
745         int flags, char *dev_name, void *data)
746 {
747         struct super_block *s = get_anon_super(fs_type, NULL, NULL);
748
749         if (IS_ERR(s))
750                 return s;
751
752         s->s_flags = flags;
753         if (!fs_type->read_super(s, data, flags & MS_VERBOSE ? 1 : 0)) {
754                 deactivate_super(s);
755                 remove_super(s);
756                 return ERR_PTR(-EINVAL);
757         }
758         s->s_flags |= MS_ACTIVE;
759         return s;
760 }
761
762 static int compare_single(struct super_block *s, void *p)
763 {
764         return 1;
765 }
766
767 static struct super_block *get_sb_single(struct file_system_type *fs_type,
768         int flags, char *dev_name, void *data)
769 {
770         struct super_block *s = get_anon_super(fs_type, compare_single, NULL);
771
772         if (IS_ERR(s))
773                 return s;
774         if (!s->s_root) {
775                 s->s_flags = flags;
776                 if (!fs_type->read_super(s, data, flags & MS_VERBOSE ? 1 : 0)) {
777                         deactivate_super(s);
778                         remove_super(s);
779                         return ERR_PTR(-EINVAL);
780                 }
781                 s->s_flags |= MS_ACTIVE;
782         }
783         do_remount_sb(s, flags, data);
784         return s;
785 }
786
787 struct vfsmount *
788 do_kern_mount(const char *fstype, int flags, char *name, void *data)
789 {
790         struct file_system_type *type = get_fs_type(fstype);
791         struct super_block *sb = ERR_PTR(-ENOMEM);
792         struct vfsmount *mnt;
793
794         if (!type)
795                 return ERR_PTR(-ENODEV);
796
797         mnt = alloc_vfsmnt(name);
798         if (!mnt)
799                 goto out;
800         if (type->fs_flags & FS_REQUIRES_DEV)
801                 sb = get_sb_bdev(type, flags, name, data);
802         else if (type->fs_flags & FS_SINGLE)
803                 sb = get_sb_single(type, flags, name, data);
804         else
805                 sb = get_sb_nodev(type, flags, name, data);
806         if (IS_ERR(sb))
807                 goto out_mnt;
808         if (type->fs_flags & FS_NOMOUNT)
809                 sb->s_flags |= MS_NOUSER;
810         mnt->mnt_sb = sb;
811         mnt->mnt_root = dget(sb->s_root);
812         mnt->mnt_mountpoint = sb->s_root;
813         mnt->mnt_parent = mnt;
814         up_write(&sb->s_umount);
815         put_filesystem(type);
816         return mnt;
817 out_mnt:
818         free_vfsmnt(mnt);
819 out:
820         put_filesystem(type);
821         return (struct vfsmount *)sb;
822 }
823
824 void kill_super(struct super_block *sb)
825 {
826         struct dentry *root = sb->s_root;
827         struct file_system_type *fs = sb->s_type;
828         struct super_operations *sop = sb->s_op;
829
830         if (!deactivate_super(sb))
831                 return;
832
833         down_write(&sb->s_umount);
834         sb->s_root = NULL;
835         /* Need to clean after the sucker */
836         if (fs->fs_flags & FS_LITTER)
837                 d_genocide(root);
838         shrink_dcache_parent(root);
839         dput(root);
840         fsync_super(sb);
841         lock_super(sb);
842         lock_kernel();
843         sb->s_flags &= ~MS_ACTIVE;
844         invalidate_inodes(sb);  /* bad name - it should be evict_inodes() */
845         if (sop) {
846                 if (sop->write_super && sb->s_dirt)
847                         sop->write_super(sb);
848                 if (sop->put_super)
849                         sop->put_super(sb);
850         }
851
852         /* Forget any remaining inodes */
853         if (invalidate_inodes(sb)) {
854                 printk(KERN_ERR "VFS: Busy inodes after unmount. "
855                         "Self-destruct in 5 seconds.  Have a nice day...\n");
856         }
857
858         unlock_kernel();
859         unlock_super(sb);
860         remove_super(sb);
861 }
862
863 struct vfsmount *kern_mount(struct file_system_type *type)
864 {
865         return do_kern_mount(type->name, 0, (char *)type->name, NULL);
866 }