]> git.karo-electronics.de Git - mv-sheeva.git/blob - fs/super.c
quota: move unmount handling into the filesystem
[mv-sheeva.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/acct.h>
26 #include <linux/blkdev.h>
27 #include <linux/quotaops.h>
28 #include <linux/mount.h>
29 #include <linux/security.h>
30 #include <linux/writeback.h>            /* for the emergency remount stuff */
31 #include <linux/idr.h>
32 #include <linux/mutex.h>
33 #include <linux/backing-dev.h>
34 #include "internal.h"
35
36
37 LIST_HEAD(super_blocks);
38 DEFINE_SPINLOCK(sb_lock);
39
40 /**
41  *      alloc_super     -       create new superblock
42  *      @type:  filesystem type superblock should belong to
43  *
44  *      Allocates and initializes a new &struct super_block.  alloc_super()
45  *      returns a pointer new superblock or %NULL if allocation had failed.
46  */
47 static struct super_block *alloc_super(struct file_system_type *type)
48 {
49         struct super_block *s = kzalloc(sizeof(struct super_block),  GFP_USER);
50         static const struct super_operations default_op;
51
52         if (s) {
53                 if (security_sb_alloc(s)) {
54                         kfree(s);
55                         s = NULL;
56                         goto out;
57                 }
58                 INIT_LIST_HEAD(&s->s_files);
59                 INIT_LIST_HEAD(&s->s_instances);
60                 INIT_HLIST_HEAD(&s->s_anon);
61                 INIT_LIST_HEAD(&s->s_inodes);
62                 INIT_LIST_HEAD(&s->s_dentry_lru);
63                 init_rwsem(&s->s_umount);
64                 mutex_init(&s->s_lock);
65                 lockdep_set_class(&s->s_umount, &type->s_umount_key);
66                 /*
67                  * The locking rules for s_lock are up to the
68                  * filesystem. For example ext3fs has different
69                  * lock ordering than usbfs:
70                  */
71                 lockdep_set_class(&s->s_lock, &type->s_lock_key);
72                 /*
73                  * sget() can have s_umount recursion.
74                  *
75                  * When it cannot find a suitable sb, it allocates a new
76                  * one (this one), and tries again to find a suitable old
77                  * one.
78                  *
79                  * In case that succeeds, it will acquire the s_umount
80                  * lock of the old one. Since these are clearly distrinct
81                  * locks, and this object isn't exposed yet, there's no
82                  * risk of deadlocks.
83                  *
84                  * Annotate this by putting this lock in a different
85                  * subclass.
86                  */
87                 down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
88                 s->s_count = 1;
89                 atomic_set(&s->s_active, 1);
90                 mutex_init(&s->s_vfs_rename_mutex);
91                 lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
92                 mutex_init(&s->s_dquot.dqio_mutex);
93                 mutex_init(&s->s_dquot.dqonoff_mutex);
94                 init_rwsem(&s->s_dquot.dqptr_sem);
95                 init_waitqueue_head(&s->s_wait_unfrozen);
96                 s->s_maxbytes = MAX_NON_LFS;
97                 s->dq_op = sb_dquot_ops;
98                 s->s_qcop = sb_quotactl_ops;
99                 s->s_op = &default_op;
100                 s->s_time_gran = 1000000000;
101         }
102 out:
103         return s;
104 }
105
106 /**
107  *      destroy_super   -       frees a superblock
108  *      @s: superblock to free
109  *
110  *      Frees a superblock.
111  */
112 static inline void destroy_super(struct super_block *s)
113 {
114         security_sb_free(s);
115         kfree(s->s_subtype);
116         kfree(s->s_options);
117         kfree(s);
118 }
119
120 /* Superblock refcounting  */
121
122 /*
123  * Drop a superblock's refcount.  The caller must hold sb_lock.
124  */
125 void __put_super(struct super_block *sb)
126 {
127         if (!--sb->s_count) {
128                 list_del_init(&sb->s_list);
129                 destroy_super(sb);
130         }
131 }
132
133 /**
134  *      put_super       -       drop a temporary reference to superblock
135  *      @sb: superblock in question
136  *
137  *      Drops a temporary reference, frees superblock if there's no
138  *      references left.
139  */
140 void put_super(struct super_block *sb)
141 {
142         spin_lock(&sb_lock);
143         __put_super(sb);
144         spin_unlock(&sb_lock);
145 }
146
147
148 /**
149  *      deactivate_locked_super -       drop an active reference to superblock
150  *      @s: superblock to deactivate
151  *
152  *      Drops an active reference to superblock, converting it into a temprory
153  *      one if there is no other active references left.  In that case we
154  *      tell fs driver to shut it down and drop the temporary reference we
155  *      had just acquired.
156  *
157  *      Caller holds exclusive lock on superblock; that lock is released.
158  */
159 void deactivate_locked_super(struct super_block *s)
160 {
161         struct file_system_type *fs = s->s_type;
162         if (atomic_dec_and_test(&s->s_active)) {
163                 fs->kill_sb(s);
164                 put_filesystem(fs);
165                 put_super(s);
166         } else {
167                 up_write(&s->s_umount);
168         }
169 }
170
171 EXPORT_SYMBOL(deactivate_locked_super);
172
173 /**
174  *      deactivate_super        -       drop an active reference to superblock
175  *      @s: superblock to deactivate
176  *
177  *      Variant of deactivate_locked_super(), except that superblock is *not*
178  *      locked by caller.  If we are going to drop the final active reference,
179  *      lock will be acquired prior to that.
180  */
181 void deactivate_super(struct super_block *s)
182 {
183         if (!atomic_add_unless(&s->s_active, -1, 1)) {
184                 down_write(&s->s_umount);
185                 deactivate_locked_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         if (atomic_inc_not_zero(&s->s_active)) {
205                 spin_unlock(&sb_lock);
206                 return 1;
207         }
208         /* it's going away */
209         s->s_count++;
210         spin_unlock(&sb_lock);
211         /* wait for it to die */
212         down_write(&s->s_umount);
213         up_write(&s->s_umount);
214         put_super(s);
215         return 0;
216 }
217
218 /*
219  * Superblock locking.  We really ought to get rid of these two.
220  */
221 void lock_super(struct super_block * sb)
222 {
223         get_fs_excl();
224         mutex_lock(&sb->s_lock);
225 }
226
227 void unlock_super(struct super_block * sb)
228 {
229         put_fs_excl();
230         mutex_unlock(&sb->s_lock);
231 }
232
233 EXPORT_SYMBOL(lock_super);
234 EXPORT_SYMBOL(unlock_super);
235
236 /**
237  *      generic_shutdown_super  -       common helper for ->kill_sb()
238  *      @sb: superblock to kill
239  *
240  *      generic_shutdown_super() does all fs-independent work on superblock
241  *      shutdown.  Typical ->kill_sb() should pick all fs-specific objects
242  *      that need destruction out of superblock, call generic_shutdown_super()
243  *      and release aforementioned objects.  Note: dentries and inodes _are_
244  *      taken care of and do not need specific handling.
245  *
246  *      Upon calling this function, the filesystem may no longer alter or
247  *      rearrange the set of dentries belonging to this super_block, nor may it
248  *      change the attachments of dentries to inodes.
249  */
250 void generic_shutdown_super(struct super_block *sb)
251 {
252         const struct super_operations *sop = sb->s_op;
253
254
255         if (sb->s_root) {
256                 shrink_dcache_for_umount(sb);
257                 sync_filesystem(sb);
258                 get_fs_excl();
259                 sb->s_flags &= ~MS_ACTIVE;
260
261                 /* bad name - it should be evict_inodes() */
262                 invalidate_inodes(sb);
263
264                 if (sop->put_super)
265                         sop->put_super(sb);
266
267                 /* Forget any remaining inodes */
268                 if (invalidate_inodes(sb)) {
269                         printk("VFS: Busy inodes after unmount of %s. "
270                            "Self-destruct in 5 seconds.  Have a nice day...\n",
271                            sb->s_id);
272                 }
273                 put_fs_excl();
274         }
275         spin_lock(&sb_lock);
276         /* should be initialized for __put_super_and_need_restart() */
277         list_del_init(&sb->s_instances);
278         spin_unlock(&sb_lock);
279         up_write(&sb->s_umount);
280 }
281
282 EXPORT_SYMBOL(generic_shutdown_super);
283
284 /**
285  *      sget    -       find or create a superblock
286  *      @type:  filesystem type superblock should belong to
287  *      @test:  comparison callback
288  *      @set:   setup callback
289  *      @data:  argument to each of them
290  */
291 struct super_block *sget(struct file_system_type *type,
292                         int (*test)(struct super_block *,void *),
293                         int (*set)(struct super_block *,void *),
294                         void *data)
295 {
296         struct super_block *s = NULL;
297         struct super_block *old;
298         int err;
299
300 retry:
301         spin_lock(&sb_lock);
302         if (test) {
303                 list_for_each_entry(old, &type->fs_supers, s_instances) {
304                         if (!test(old, data))
305                                 continue;
306                         if (!grab_super(old))
307                                 goto retry;
308                         if (s) {
309                                 up_write(&s->s_umount);
310                                 destroy_super(s);
311                         }
312                         down_write(&old->s_umount);
313                         return old;
314                 }
315         }
316         if (!s) {
317                 spin_unlock(&sb_lock);
318                 s = alloc_super(type);
319                 if (!s)
320                         return ERR_PTR(-ENOMEM);
321                 goto retry;
322         }
323                 
324         err = set(s, data);
325         if (err) {
326                 spin_unlock(&sb_lock);
327                 up_write(&s->s_umount);
328                 destroy_super(s);
329                 return ERR_PTR(err);
330         }
331         s->s_type = type;
332         strlcpy(s->s_id, type->name, sizeof(s->s_id));
333         list_add_tail(&s->s_list, &super_blocks);
334         list_add(&s->s_instances, &type->fs_supers);
335         spin_unlock(&sb_lock);
336         get_filesystem(type);
337         return s;
338 }
339
340 EXPORT_SYMBOL(sget);
341
342 void drop_super(struct super_block *sb)
343 {
344         up_read(&sb->s_umount);
345         put_super(sb);
346 }
347
348 EXPORT_SYMBOL(drop_super);
349
350 /**
351  * sync_supers - helper for periodic superblock writeback
352  *
353  * Call the write_super method if present on all dirty superblocks in
354  * the system.  This is for the periodic writeback used by most older
355  * filesystems.  For data integrity superblock writeback use
356  * sync_filesystems() instead.
357  *
358  * Note: check the dirty flag before waiting, so we don't
359  * hold up the sync while mounting a device. (The newly
360  * mounted device won't need syncing.)
361  */
362 void sync_supers(void)
363 {
364         struct super_block *sb, *n;
365
366         spin_lock(&sb_lock);
367         list_for_each_entry_safe(sb, n, &super_blocks, s_list) {
368                 if (list_empty(&sb->s_instances))
369                         continue;
370                 if (sb->s_op->write_super && sb->s_dirt) {
371                         sb->s_count++;
372                         spin_unlock(&sb_lock);
373
374                         down_read(&sb->s_umount);
375                         if (sb->s_root && sb->s_dirt)
376                                 sb->s_op->write_super(sb);
377                         up_read(&sb->s_umount);
378
379                         spin_lock(&sb_lock);
380                         __put_super(sb);
381                 }
382         }
383         spin_unlock(&sb_lock);
384 }
385
386 /**
387  *      iterate_supers - call function for all active superblocks
388  *      @f: function to call
389  *      @arg: argument to pass to it
390  *
391  *      Scans the superblock list and calls given function, passing it
392  *      locked superblock and given argument.
393  */
394 void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
395 {
396         struct super_block *sb, *n;
397
398         spin_lock(&sb_lock);
399         list_for_each_entry_safe(sb, n, &super_blocks, s_list) {
400                 if (list_empty(&sb->s_instances))
401                         continue;
402                 sb->s_count++;
403                 spin_unlock(&sb_lock);
404
405                 down_read(&sb->s_umount);
406                 if (sb->s_root)
407                         f(sb, arg);
408                 up_read(&sb->s_umount);
409
410                 spin_lock(&sb_lock);
411                 __put_super(sb);
412         }
413         spin_unlock(&sb_lock);
414 }
415
416 /**
417  *      get_super - get the superblock of a device
418  *      @bdev: device to get the superblock for
419  *      
420  *      Scans the superblock list and finds the superblock of the file system
421  *      mounted on the device given. %NULL is returned if no match is found.
422  */
423
424 struct super_block *get_super(struct block_device *bdev)
425 {
426         struct super_block *sb;
427
428         if (!bdev)
429                 return NULL;
430
431         spin_lock(&sb_lock);
432 rescan:
433         list_for_each_entry(sb, &super_blocks, s_list) {
434                 if (list_empty(&sb->s_instances))
435                         continue;
436                 if (sb->s_bdev == bdev) {
437                         sb->s_count++;
438                         spin_unlock(&sb_lock);
439                         down_read(&sb->s_umount);
440                         /* still alive? */
441                         if (sb->s_root)
442                                 return sb;
443                         up_read(&sb->s_umount);
444                         /* nope, got unmounted */
445                         spin_lock(&sb_lock);
446                         __put_super(sb);
447                         goto rescan;
448                 }
449         }
450         spin_unlock(&sb_lock);
451         return NULL;
452 }
453
454 EXPORT_SYMBOL(get_super);
455
456 /**
457  * get_active_super - get an active reference to the superblock of a device
458  * @bdev: device to get the superblock for
459  *
460  * Scans the superblock list and finds the superblock of the file system
461  * mounted on the device given.  Returns the superblock with an active
462  * reference or %NULL if none was found.
463  */
464 struct super_block *get_active_super(struct block_device *bdev)
465 {
466         struct super_block *sb;
467
468         if (!bdev)
469                 return NULL;
470
471 restart:
472         spin_lock(&sb_lock);
473         list_for_each_entry(sb, &super_blocks, s_list) {
474                 if (list_empty(&sb->s_instances))
475                         continue;
476                 if (sb->s_bdev == bdev) {
477                         if (grab_super(sb)) /* drops sb_lock */
478                                 return sb;
479                         else
480                                 goto restart;
481                 }
482         }
483         spin_unlock(&sb_lock);
484         return NULL;
485 }
486  
487 struct super_block *user_get_super(dev_t dev)
488 {
489         struct super_block *sb;
490
491         spin_lock(&sb_lock);
492 rescan:
493         list_for_each_entry(sb, &super_blocks, s_list) {
494                 if (list_empty(&sb->s_instances))
495                         continue;
496                 if (sb->s_dev ==  dev) {
497                         sb->s_count++;
498                         spin_unlock(&sb_lock);
499                         down_read(&sb->s_umount);
500                         /* still alive? */
501                         if (sb->s_root)
502                                 return sb;
503                         up_read(&sb->s_umount);
504                         /* nope, got unmounted */
505                         spin_lock(&sb_lock);
506                         __put_super(sb);
507                         goto rescan;
508                 }
509         }
510         spin_unlock(&sb_lock);
511         return NULL;
512 }
513
514 /**
515  *      do_remount_sb - asks filesystem to change mount options.
516  *      @sb:    superblock in question
517  *      @flags: numeric part of options
518  *      @data:  the rest of options
519  *      @force: whether or not to force the change
520  *
521  *      Alters the mount options of a mounted file system.
522  */
523 int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
524 {
525         int retval;
526         int remount_ro;
527
528         if (sb->s_frozen != SB_UNFROZEN)
529                 return -EBUSY;
530
531 #ifdef CONFIG_BLOCK
532         if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
533                 return -EACCES;
534 #endif
535
536         if (flags & MS_RDONLY)
537                 acct_auto_close(sb);
538         shrink_dcache_sb(sb);
539         sync_filesystem(sb);
540
541         remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
542
543         /* If we are remounting RDONLY and current sb is read/write,
544            make sure there are no rw files opened */
545         if (remount_ro) {
546                 if (force)
547                         mark_files_ro(sb);
548                 else if (!fs_may_remount_ro(sb))
549                         return -EBUSY;
550         }
551
552         if (sb->s_op->remount_fs) {
553                 retval = sb->s_op->remount_fs(sb, &flags, data);
554                 if (retval)
555                         return retval;
556         }
557         sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
558
559         /*
560          * Some filesystems modify their metadata via some other path than the
561          * bdev buffer cache (eg. use a private mapping, or directories in
562          * pagecache, etc). Also file data modifications go via their own
563          * mappings. So If we try to mount readonly then copy the filesystem
564          * from bdev, we could get stale data, so invalidate it to give a best
565          * effort at coherency.
566          */
567         if (remount_ro && sb->s_bdev)
568                 invalidate_bdev(sb->s_bdev);
569         return 0;
570 }
571
572 static void do_emergency_remount(struct work_struct *work)
573 {
574         struct super_block *sb, *n;
575
576         spin_lock(&sb_lock);
577         list_for_each_entry_safe(sb, n, &super_blocks, s_list) {
578                 if (list_empty(&sb->s_instances))
579                         continue;
580                 sb->s_count++;
581                 spin_unlock(&sb_lock);
582                 down_write(&sb->s_umount);
583                 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
584                         /*
585                          * What lock protects sb->s_flags??
586                          */
587                         do_remount_sb(sb, MS_RDONLY, NULL, 1);
588                 }
589                 up_write(&sb->s_umount);
590                 spin_lock(&sb_lock);
591                 __put_super(sb);
592         }
593         spin_unlock(&sb_lock);
594         kfree(work);
595         printk("Emergency Remount complete\n");
596 }
597
598 void emergency_remount(void)
599 {
600         struct work_struct *work;
601
602         work = kmalloc(sizeof(*work), GFP_ATOMIC);
603         if (work) {
604                 INIT_WORK(work, do_emergency_remount);
605                 schedule_work(work);
606         }
607 }
608
609 /*
610  * Unnamed block devices are dummy devices used by virtual
611  * filesystems which don't use real block-devices.  -- jrs
612  */
613
614 static DEFINE_IDA(unnamed_dev_ida);
615 static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
616 static int unnamed_dev_start = 0; /* don't bother trying below it */
617
618 int set_anon_super(struct super_block *s, void *data)
619 {
620         int dev;
621         int error;
622
623  retry:
624         if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
625                 return -ENOMEM;
626         spin_lock(&unnamed_dev_lock);
627         error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
628         if (!error)
629                 unnamed_dev_start = dev + 1;
630         spin_unlock(&unnamed_dev_lock);
631         if (error == -EAGAIN)
632                 /* We raced and lost with another CPU. */
633                 goto retry;
634         else if (error)
635                 return -EAGAIN;
636
637         if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
638                 spin_lock(&unnamed_dev_lock);
639                 ida_remove(&unnamed_dev_ida, dev);
640                 if (unnamed_dev_start > dev)
641                         unnamed_dev_start = dev;
642                 spin_unlock(&unnamed_dev_lock);
643                 return -EMFILE;
644         }
645         s->s_dev = MKDEV(0, dev & MINORMASK);
646         s->s_bdi = &noop_backing_dev_info;
647         return 0;
648 }
649
650 EXPORT_SYMBOL(set_anon_super);
651
652 void kill_anon_super(struct super_block *sb)
653 {
654         int slot = MINOR(sb->s_dev);
655
656         generic_shutdown_super(sb);
657         spin_lock(&unnamed_dev_lock);
658         ida_remove(&unnamed_dev_ida, slot);
659         if (slot < unnamed_dev_start)
660                 unnamed_dev_start = slot;
661         spin_unlock(&unnamed_dev_lock);
662 }
663
664 EXPORT_SYMBOL(kill_anon_super);
665
666 void kill_litter_super(struct super_block *sb)
667 {
668         if (sb->s_root)
669                 d_genocide(sb->s_root);
670         kill_anon_super(sb);
671 }
672
673 EXPORT_SYMBOL(kill_litter_super);
674
675 static int ns_test_super(struct super_block *sb, void *data)
676 {
677         return sb->s_fs_info == data;
678 }
679
680 static int ns_set_super(struct super_block *sb, void *data)
681 {
682         sb->s_fs_info = data;
683         return set_anon_super(sb, NULL);
684 }
685
686 int get_sb_ns(struct file_system_type *fs_type, int flags, void *data,
687         int (*fill_super)(struct super_block *, void *, int),
688         struct vfsmount *mnt)
689 {
690         struct super_block *sb;
691
692         sb = sget(fs_type, ns_test_super, ns_set_super, data);
693         if (IS_ERR(sb))
694                 return PTR_ERR(sb);
695
696         if (!sb->s_root) {
697                 int err;
698                 sb->s_flags = flags;
699                 err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
700                 if (err) {
701                         deactivate_locked_super(sb);
702                         return err;
703                 }
704
705                 sb->s_flags |= MS_ACTIVE;
706         }
707
708         simple_set_mnt(mnt, sb);
709         return 0;
710 }
711
712 EXPORT_SYMBOL(get_sb_ns);
713
714 #ifdef CONFIG_BLOCK
715 static int set_bdev_super(struct super_block *s, void *data)
716 {
717         s->s_bdev = data;
718         s->s_dev = s->s_bdev->bd_dev;
719
720         /*
721          * We set the bdi here to the queue backing, file systems can
722          * overwrite this in ->fill_super()
723          */
724         s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
725         return 0;
726 }
727
728 static int test_bdev_super(struct super_block *s, void *data)
729 {
730         return (void *)s->s_bdev == data;
731 }
732
733 int get_sb_bdev(struct file_system_type *fs_type,
734         int flags, const char *dev_name, void *data,
735         int (*fill_super)(struct super_block *, void *, int),
736         struct vfsmount *mnt)
737 {
738         struct block_device *bdev;
739         struct super_block *s;
740         fmode_t mode = FMODE_READ;
741         int error = 0;
742
743         if (!(flags & MS_RDONLY))
744                 mode |= FMODE_WRITE;
745
746         bdev = open_bdev_exclusive(dev_name, mode, fs_type);
747         if (IS_ERR(bdev))
748                 return PTR_ERR(bdev);
749
750         /*
751          * once the super is inserted into the list by sget, s_umount
752          * will protect the lockfs code from trying to start a snapshot
753          * while we are mounting
754          */
755         mutex_lock(&bdev->bd_fsfreeze_mutex);
756         if (bdev->bd_fsfreeze_count > 0) {
757                 mutex_unlock(&bdev->bd_fsfreeze_mutex);
758                 error = -EBUSY;
759                 goto error_bdev;
760         }
761         s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
762         mutex_unlock(&bdev->bd_fsfreeze_mutex);
763         if (IS_ERR(s))
764                 goto error_s;
765
766         if (s->s_root) {
767                 if ((flags ^ s->s_flags) & MS_RDONLY) {
768                         deactivate_locked_super(s);
769                         error = -EBUSY;
770                         goto error_bdev;
771                 }
772
773                 close_bdev_exclusive(bdev, mode);
774         } else {
775                 char b[BDEVNAME_SIZE];
776
777                 s->s_flags = flags;
778                 s->s_mode = mode;
779                 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
780                 sb_set_blocksize(s, block_size(bdev));
781                 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
782                 if (error) {
783                         deactivate_locked_super(s);
784                         goto error;
785                 }
786
787                 s->s_flags |= MS_ACTIVE;
788                 bdev->bd_super = s;
789         }
790
791         simple_set_mnt(mnt, s);
792         return 0;
793
794 error_s:
795         error = PTR_ERR(s);
796 error_bdev:
797         close_bdev_exclusive(bdev, mode);
798 error:
799         return error;
800 }
801
802 EXPORT_SYMBOL(get_sb_bdev);
803
804 void kill_block_super(struct super_block *sb)
805 {
806         struct block_device *bdev = sb->s_bdev;
807         fmode_t mode = sb->s_mode;
808
809         bdev->bd_super = NULL;
810         generic_shutdown_super(sb);
811         sync_blockdev(bdev);
812         close_bdev_exclusive(bdev, mode);
813 }
814
815 EXPORT_SYMBOL(kill_block_super);
816 #endif
817
818 int get_sb_nodev(struct file_system_type *fs_type,
819         int flags, void *data,
820         int (*fill_super)(struct super_block *, void *, int),
821         struct vfsmount *mnt)
822 {
823         int error;
824         struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
825
826         if (IS_ERR(s))
827                 return PTR_ERR(s);
828
829         s->s_flags = flags;
830
831         error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
832         if (error) {
833                 deactivate_locked_super(s);
834                 return error;
835         }
836         s->s_flags |= MS_ACTIVE;
837         simple_set_mnt(mnt, s);
838         return 0;
839 }
840
841 EXPORT_SYMBOL(get_sb_nodev);
842
843 static int compare_single(struct super_block *s, void *p)
844 {
845         return 1;
846 }
847
848 int get_sb_single(struct file_system_type *fs_type,
849         int flags, void *data,
850         int (*fill_super)(struct super_block *, void *, int),
851         struct vfsmount *mnt)
852 {
853         struct super_block *s;
854         int error;
855
856         s = sget(fs_type, compare_single, set_anon_super, NULL);
857         if (IS_ERR(s))
858                 return PTR_ERR(s);
859         if (!s->s_root) {
860                 s->s_flags = flags;
861                 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
862                 if (error) {
863                         deactivate_locked_super(s);
864                         return error;
865                 }
866                 s->s_flags |= MS_ACTIVE;
867         } else {
868                 do_remount_sb(s, flags, data, 0);
869         }
870         simple_set_mnt(mnt, s);
871         return 0;
872 }
873
874 EXPORT_SYMBOL(get_sb_single);
875
876 struct vfsmount *
877 vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
878 {
879         struct vfsmount *mnt;
880         char *secdata = NULL;
881         int error;
882
883         if (!type)
884                 return ERR_PTR(-ENODEV);
885
886         error = -ENOMEM;
887         mnt = alloc_vfsmnt(name);
888         if (!mnt)
889                 goto out;
890
891         if (flags & MS_KERNMOUNT)
892                 mnt->mnt_flags = MNT_INTERNAL;
893
894         if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
895                 secdata = alloc_secdata();
896                 if (!secdata)
897                         goto out_mnt;
898
899                 error = security_sb_copy_data(data, secdata);
900                 if (error)
901                         goto out_free_secdata;
902         }
903
904         error = type->get_sb(type, flags, name, data, mnt);
905         if (error < 0)
906                 goto out_free_secdata;
907         BUG_ON(!mnt->mnt_sb);
908         WARN_ON(!mnt->mnt_sb->s_bdi);
909
910         error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
911         if (error)
912                 goto out_sb;
913
914         /*
915          * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
916          * but s_maxbytes was an unsigned long long for many releases. Throw
917          * this warning for a little while to try and catch filesystems that
918          * violate this rule. This warning should be either removed or
919          * converted to a BUG() in 2.6.34.
920          */
921         WARN((mnt->mnt_sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
922                 "negative value (%lld)\n", type->name, mnt->mnt_sb->s_maxbytes);
923
924         mnt->mnt_mountpoint = mnt->mnt_root;
925         mnt->mnt_parent = mnt;
926         up_write(&mnt->mnt_sb->s_umount);
927         free_secdata(secdata);
928         return mnt;
929 out_sb:
930         dput(mnt->mnt_root);
931         deactivate_locked_super(mnt->mnt_sb);
932 out_free_secdata:
933         free_secdata(secdata);
934 out_mnt:
935         free_vfsmnt(mnt);
936 out:
937         return ERR_PTR(error);
938 }
939
940 EXPORT_SYMBOL_GPL(vfs_kern_mount);
941
942 /**
943  * freeze_super -- lock the filesystem and force it into a consistent state
944  * @super: the super to lock
945  *
946  * Syncs the super to make sure the filesystem is consistent and calls the fs's
947  * freeze_fs.  Subsequent calls to this without first thawing the fs will return
948  * -EBUSY.
949  */
950 int freeze_super(struct super_block *sb)
951 {
952         int ret;
953
954         atomic_inc(&sb->s_active);
955         down_write(&sb->s_umount);
956         if (sb->s_frozen) {
957                 deactivate_locked_super(sb);
958                 return -EBUSY;
959         }
960
961         if (sb->s_flags & MS_RDONLY) {
962                 sb->s_frozen = SB_FREEZE_TRANS;
963                 smp_wmb();
964                 up_write(&sb->s_umount);
965                 return 0;
966         }
967
968         sb->s_frozen = SB_FREEZE_WRITE;
969         smp_wmb();
970
971         sync_filesystem(sb);
972
973         sb->s_frozen = SB_FREEZE_TRANS;
974         smp_wmb();
975
976         sync_blockdev(sb->s_bdev);
977         if (sb->s_op->freeze_fs) {
978                 ret = sb->s_op->freeze_fs(sb);
979                 if (ret) {
980                         printk(KERN_ERR
981                                 "VFS:Filesystem freeze failed\n");
982                         sb->s_frozen = SB_UNFROZEN;
983                         deactivate_locked_super(sb);
984                         return ret;
985                 }
986         }
987         up_write(&sb->s_umount);
988         return 0;
989 }
990 EXPORT_SYMBOL(freeze_super);
991
992 /**
993  * thaw_super -- unlock filesystem
994  * @sb: the super to thaw
995  *
996  * Unlocks the filesystem and marks it writeable again after freeze_super().
997  */
998 int thaw_super(struct super_block *sb)
999 {
1000         int error;
1001
1002         down_write(&sb->s_umount);
1003         if (sb->s_frozen == SB_UNFROZEN) {
1004                 up_write(&sb->s_umount);
1005                 return -EINVAL;
1006         }
1007
1008         if (sb->s_flags & MS_RDONLY)
1009                 goto out;
1010
1011         if (sb->s_op->unfreeze_fs) {
1012                 error = sb->s_op->unfreeze_fs(sb);
1013                 if (error) {
1014                         printk(KERN_ERR
1015                                 "VFS:Filesystem thaw failed\n");
1016                         sb->s_frozen = SB_FREEZE_TRANS;
1017                         up_write(&sb->s_umount);
1018                         return error;
1019                 }
1020         }
1021
1022 out:
1023         sb->s_frozen = SB_UNFROZEN;
1024         smp_wmb();
1025         wake_up(&sb->s_wait_unfrozen);
1026         deactivate_locked_super(sb);
1027
1028         return 0;
1029 }
1030 EXPORT_SYMBOL(thaw_super);
1031
1032 static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
1033 {
1034         int err;
1035         const char *subtype = strchr(fstype, '.');
1036         if (subtype) {
1037                 subtype++;
1038                 err = -EINVAL;
1039                 if (!subtype[0])
1040                         goto err;
1041         } else
1042                 subtype = "";
1043
1044         mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
1045         err = -ENOMEM;
1046         if (!mnt->mnt_sb->s_subtype)
1047                 goto err;
1048         return mnt;
1049
1050  err:
1051         mntput(mnt);
1052         return ERR_PTR(err);
1053 }
1054
1055 struct vfsmount *
1056 do_kern_mount(const char *fstype, int flags, const char *name, void *data)
1057 {
1058         struct file_system_type *type = get_fs_type(fstype);
1059         struct vfsmount *mnt;
1060         if (!type)
1061                 return ERR_PTR(-ENODEV);
1062         mnt = vfs_kern_mount(type, flags, name, data);
1063         if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
1064             !mnt->mnt_sb->s_subtype)
1065                 mnt = fs_set_subtype(mnt, fstype);
1066         put_filesystem(type);
1067         return mnt;
1068 }
1069 EXPORT_SYMBOL_GPL(do_kern_mount);
1070
1071 struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
1072 {
1073         return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
1074 }
1075
1076 EXPORT_SYMBOL_GPL(kern_mount_data);