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