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