shrinker.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include "node.h"
  16. static LIST_HEAD(f2fs_list);
  17. static DEFINE_SPINLOCK(f2fs_list_lock);
  18. static unsigned int shrinker_run_no;
  19. static unsigned long __count_nat_entries(struct f2fs_sb_info *sbi)
  20. {
  21. return NM_I(sbi)->nat_cnt - NM_I(sbi)->dirty_nat_cnt;
  22. }
  23. static unsigned long __count_free_nids(struct f2fs_sb_info *sbi)
  24. {
  25. if (NM_I(sbi)->fcnt > MAX_FREE_NIDS)
  26. return NM_I(sbi)->fcnt - MAX_FREE_NIDS;
  27. return 0;
  28. }
  29. static unsigned long __count_extent_cache(struct f2fs_sb_info *sbi)
  30. {
  31. return atomic_read(&sbi->total_zombie_tree) +
  32. atomic_read(&sbi->total_ext_node);
  33. }
  34. unsigned long f2fs_shrink_count(struct shrinker *shrink,
  35. struct shrink_control *sc)
  36. {
  37. struct f2fs_sb_info *sbi;
  38. struct list_head *p;
  39. unsigned long count = 0;
  40. spin_lock(&f2fs_list_lock);
  41. p = f2fs_list.next;
  42. while (p != &f2fs_list) {
  43. sbi = list_entry(p, struct f2fs_sb_info, s_list);
  44. /* stop f2fs_put_super */
  45. if (!mutex_trylock(&sbi->umount_mutex)) {
  46. p = p->next;
  47. continue;
  48. }
  49. spin_unlock(&f2fs_list_lock);
  50. /* count extent cache entries */
  51. count += __count_extent_cache(sbi);
  52. /* shrink clean nat cache entries */
  53. count += __count_nat_entries(sbi);
  54. /* count free nids cache entries */
  55. count += __count_free_nids(sbi);
  56. spin_lock(&f2fs_list_lock);
  57. p = p->next;
  58. mutex_unlock(&sbi->umount_mutex);
  59. }
  60. spin_unlock(&f2fs_list_lock);
  61. return count;
  62. }
  63. unsigned long f2fs_shrink_scan(struct shrinker *shrink,
  64. struct shrink_control *sc)
  65. {
  66. unsigned long nr = sc->nr_to_scan;
  67. struct f2fs_sb_info *sbi;
  68. struct list_head *p;
  69. unsigned int run_no;
  70. unsigned long freed = 0;
  71. spin_lock(&f2fs_list_lock);
  72. do {
  73. run_no = ++shrinker_run_no;
  74. } while (run_no == 0);
  75. p = f2fs_list.next;
  76. while (p != &f2fs_list) {
  77. sbi = list_entry(p, struct f2fs_sb_info, s_list);
  78. if (sbi->shrinker_run_no == run_no)
  79. break;
  80. /* stop f2fs_put_super */
  81. if (!mutex_trylock(&sbi->umount_mutex)) {
  82. p = p->next;
  83. continue;
  84. }
  85. spin_unlock(&f2fs_list_lock);
  86. sbi->shrinker_run_no = run_no;
  87. /* shrink extent cache entries */
  88. freed += f2fs_shrink_extent_tree(sbi, nr >> 1);
  89. /* shrink clean nat cache entries */
  90. if (freed < nr)
  91. freed += try_to_free_nats(sbi, nr - freed);
  92. /* shrink free nids cache entries */
  93. if (freed < nr)
  94. freed += try_to_free_nids(sbi, nr - freed);
  95. spin_lock(&f2fs_list_lock);
  96. p = p->next;
  97. list_move_tail(&sbi->s_list, &f2fs_list);
  98. mutex_unlock(&sbi->umount_mutex);
  99. if (freed >= nr)
  100. break;
  101. }
  102. spin_unlock(&f2fs_list_lock);
  103. return freed;
  104. }
  105. void f2fs_join_shrinker(struct f2fs_sb_info *sbi)
  106. {
  107. spin_lock(&f2fs_list_lock);
  108. list_add_tail(&sbi->s_list, &f2fs_list);
  109. spin_unlock(&f2fs_list_lock);
  110. }
  111. void f2fs_leave_shrinker(struct f2fs_sb_info *sbi)
  112. {
  113. f2fs_shrink_extent_tree(sbi, __count_extent_cache(sbi));
  114. spin_lock(&f2fs_list_lock);
  115. list_del(&sbi->s_list);
  116. spin_unlock(&f2fs_list_lock);
  117. }