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