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