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