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