shrinker.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * f2fs shrinker support
  3. * the basic infra was copied from fs/ubifs/shrinker.c
  4. *
  5. * Copyright (c) 2015 Motorola Mobility
  6. * Copyright (c) 2015 Jaegeuk Kim <jaegeuk@kernel.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/fs.h>
  13. #include <linux/f2fs_fs.h>
  14. #include "f2fs.h"
  15. static LIST_HEAD(f2fs_list);
  16. static DEFINE_SPINLOCK(f2fs_list_lock);
  17. static unsigned int shrinker_run_no;
  18. static unsigned long __count_nat_entries(struct f2fs_sb_info *sbi)
  19. {
  20. return NM_I(sbi)->nat_cnt - NM_I(sbi)->dirty_nat_cnt;
  21. }
  22. static unsigned long __count_free_nids(struct f2fs_sb_info *sbi)
  23. {
  24. if (NM_I(sbi)->fcnt > NAT_ENTRY_PER_BLOCK)
  25. return NM_I(sbi)->fcnt - NAT_ENTRY_PER_BLOCK;
  26. return 0;
  27. }
  28. static unsigned long __count_extent_cache(struct f2fs_sb_info *sbi)
  29. {
  30. return sbi->total_ext_tree + atomic_read(&sbi->total_ext_node);
  31. }
  32. unsigned long f2fs_shrink_count(struct shrinker *shrink,
  33. struct shrink_control *sc)
  34. {
  35. struct f2fs_sb_info *sbi;
  36. struct list_head *p;
  37. unsigned long count = 0;
  38. spin_lock(&f2fs_list_lock);
  39. p = f2fs_list.next;
  40. while (p != &f2fs_list) {
  41. sbi = list_entry(p, struct f2fs_sb_info, s_list);
  42. /* stop f2fs_put_super */
  43. if (!mutex_trylock(&sbi->umount_mutex)) {
  44. p = p->next;
  45. continue;
  46. }
  47. spin_unlock(&f2fs_list_lock);
  48. /* count extent cache entries */
  49. count += __count_extent_cache(sbi);
  50. /* shrink clean nat cache entries */
  51. count += __count_nat_entries(sbi);
  52. /* count free nids cache entries */
  53. count += __count_free_nids(sbi);
  54. spin_lock(&f2fs_list_lock);
  55. p = p->next;
  56. mutex_unlock(&sbi->umount_mutex);
  57. }
  58. spin_unlock(&f2fs_list_lock);
  59. return count;
  60. }
  61. unsigned long f2fs_shrink_scan(struct shrinker *shrink,
  62. struct shrink_control *sc)
  63. {
  64. unsigned long nr = sc->nr_to_scan;
  65. struct f2fs_sb_info *sbi;
  66. struct list_head *p;
  67. unsigned int run_no;
  68. unsigned long freed = 0;
  69. spin_lock(&f2fs_list_lock);
  70. do {
  71. run_no = ++shrinker_run_no;
  72. } while (run_no == 0);
  73. p = f2fs_list.next;
  74. while (p != &f2fs_list) {
  75. sbi = list_entry(p, struct f2fs_sb_info, s_list);
  76. if (sbi->shrinker_run_no == run_no)
  77. break;
  78. /* stop f2fs_put_super */
  79. if (!mutex_trylock(&sbi->umount_mutex)) {
  80. p = p->next;
  81. continue;
  82. }
  83. spin_unlock(&f2fs_list_lock);
  84. sbi->shrinker_run_no = run_no;
  85. /* shrink extent cache entries */
  86. freed += f2fs_shrink_extent_tree(sbi, nr >> 1);
  87. /* shrink clean nat cache entries */
  88. if (freed < nr)
  89. freed += try_to_free_nats(sbi, nr - freed);
  90. /* shrink free nids cache entries */
  91. if (freed < nr)
  92. freed += try_to_free_nids(sbi, nr - freed);
  93. spin_lock(&f2fs_list_lock);
  94. p = p->next;
  95. list_move_tail(&sbi->s_list, &f2fs_list);
  96. mutex_unlock(&sbi->umount_mutex);
  97. if (freed >= nr)
  98. break;
  99. }
  100. spin_unlock(&f2fs_list_lock);
  101. return freed;
  102. }
  103. void f2fs_join_shrinker(struct f2fs_sb_info *sbi)
  104. {
  105. spin_lock(&f2fs_list_lock);
  106. list_add_tail(&sbi->s_list, &f2fs_list);
  107. spin_unlock(&f2fs_list_lock);
  108. }
  109. void f2fs_leave_shrinker(struct f2fs_sb_info *sbi)
  110. {
  111. f2fs_shrink_extent_tree(sbi, __count_extent_cache(sbi));
  112. spin_lock(&f2fs_list_lock);
  113. list_del(&sbi->s_list);
  114. spin_unlock(&f2fs_list_lock);
  115. }