block_validity.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * linux/fs/ext4/block_validity.c
  3. *
  4. * Copyright (C) 2009
  5. * Theodore Ts'o (tytso@mit.edu)
  6. *
  7. * Track which blocks in the filesystem are metadata blocks that
  8. * should never be used as data blocks by files or directories.
  9. */
  10. #include <linux/time.h>
  11. #include <linux/fs.h>
  12. #include <linux/namei.h>
  13. #include <linux/quotaops.h>
  14. #include <linux/buffer_head.h>
  15. #include <linux/module.h>
  16. #include <linux/swap.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/version.h>
  19. #include <linux/blkdev.h>
  20. #include <linux/mutex.h>
  21. #include "ext4.h"
  22. struct ext4_system_zone {
  23. struct rb_node node;
  24. ext4_fsblk_t start_blk;
  25. unsigned int count;
  26. };
  27. static struct kmem_cache *ext4_system_zone_cachep;
  28. int __init init_ext4_system_zone(void)
  29. {
  30. ext4_system_zone_cachep = KMEM_CACHE(ext4_system_zone,
  31. SLAB_RECLAIM_ACCOUNT);
  32. if (ext4_system_zone_cachep == NULL)
  33. return -ENOMEM;
  34. return 0;
  35. }
  36. void exit_ext4_system_zone(void)
  37. {
  38. kmem_cache_destroy(ext4_system_zone_cachep);
  39. }
  40. static inline int can_merge(struct ext4_system_zone *entry1,
  41. struct ext4_system_zone *entry2)
  42. {
  43. if ((entry1->start_blk + entry1->count) == entry2->start_blk)
  44. return 1;
  45. return 0;
  46. }
  47. /*
  48. * Mark a range of blocks as belonging to the "system zone" --- that
  49. * is, filesystem metadata blocks which should never be used by
  50. * inodes.
  51. */
  52. static int add_system_zone(struct ext4_sb_info *sbi,
  53. ext4_fsblk_t start_blk,
  54. unsigned int count)
  55. {
  56. struct ext4_system_zone *new_entry = NULL, *entry;
  57. struct rb_node **n = &sbi->system_blks.rb_node, *node;
  58. struct rb_node *parent = NULL, *new_node = NULL;
  59. while (*n) {
  60. parent = *n;
  61. entry = rb_entry(parent, struct ext4_system_zone, node);
  62. if (start_blk < entry->start_blk)
  63. n = &(*n)->rb_left;
  64. else if (start_blk >= (entry->start_blk + entry->count))
  65. n = &(*n)->rb_right;
  66. else {
  67. if (start_blk + count > (entry->start_blk +
  68. entry->count))
  69. entry->count = (start_blk + count -
  70. entry->start_blk);
  71. new_node = *n;
  72. new_entry = rb_entry(new_node, struct ext4_system_zone,
  73. node);
  74. break;
  75. }
  76. }
  77. if (!new_entry) {
  78. new_entry = kmem_cache_alloc(ext4_system_zone_cachep,
  79. GFP_KERNEL);
  80. if (!new_entry)
  81. return -ENOMEM;
  82. new_entry->start_blk = start_blk;
  83. new_entry->count = count;
  84. new_node = &new_entry->node;
  85. rb_link_node(new_node, parent, n);
  86. rb_insert_color(new_node, &sbi->system_blks);
  87. }
  88. /* Can we merge to the left? */
  89. node = rb_prev(new_node);
  90. if (node) {
  91. entry = rb_entry(node, struct ext4_system_zone, node);
  92. if (can_merge(entry, new_entry)) {
  93. new_entry->start_blk = entry->start_blk;
  94. new_entry->count += entry->count;
  95. rb_erase(node, &sbi->system_blks);
  96. kmem_cache_free(ext4_system_zone_cachep, entry);
  97. }
  98. }
  99. /* Can we merge to the right? */
  100. node = rb_next(new_node);
  101. if (node) {
  102. entry = rb_entry(node, struct ext4_system_zone, node);
  103. if (can_merge(new_entry, entry)) {
  104. new_entry->count += entry->count;
  105. rb_erase(node, &sbi->system_blks);
  106. kmem_cache_free(ext4_system_zone_cachep, entry);
  107. }
  108. }
  109. return 0;
  110. }
  111. static void debug_print_tree(struct ext4_sb_info *sbi)
  112. {
  113. struct rb_node *node;
  114. struct ext4_system_zone *entry;
  115. int first = 1;
  116. printk(KERN_INFO "System zones: ");
  117. node = rb_first(&sbi->system_blks);
  118. while (node) {
  119. entry = rb_entry(node, struct ext4_system_zone, node);
  120. printk("%s%llu-%llu", first ? "" : ", ",
  121. entry->start_blk, entry->start_blk + entry->count - 1);
  122. first = 0;
  123. node = rb_next(node);
  124. }
  125. printk("\n");
  126. }
  127. int ext4_setup_system_zone(struct super_block *sb)
  128. {
  129. ext4_group_t ngroups = ext4_get_groups_count(sb);
  130. struct ext4_sb_info *sbi = EXT4_SB(sb);
  131. struct ext4_group_desc *gdp;
  132. ext4_group_t i;
  133. int flex_size = ext4_flex_bg_size(sbi);
  134. int ret;
  135. if (!test_opt(sb, BLOCK_VALIDITY)) {
  136. if (EXT4_SB(sb)->system_blks.rb_node)
  137. ext4_release_system_zone(sb);
  138. return 0;
  139. }
  140. if (EXT4_SB(sb)->system_blks.rb_node)
  141. return 0;
  142. for (i=0; i < ngroups; i++) {
  143. if (ext4_bg_has_super(sb, i) &&
  144. ((i < 5) || ((i % flex_size) == 0)))
  145. add_system_zone(sbi, ext4_group_first_block_no(sb, i),
  146. sbi->s_gdb_count + 1);
  147. gdp = ext4_get_group_desc(sb, i, NULL);
  148. ret = add_system_zone(sbi, ext4_block_bitmap(sb, gdp), 1);
  149. if (ret)
  150. return ret;
  151. ret = add_system_zone(sbi, ext4_inode_bitmap(sb, gdp), 1);
  152. if (ret)
  153. return ret;
  154. ret = add_system_zone(sbi, ext4_inode_table(sb, gdp),
  155. sbi->s_itb_per_group);
  156. if (ret)
  157. return ret;
  158. }
  159. if (test_opt(sb, DEBUG))
  160. debug_print_tree(EXT4_SB(sb));
  161. return 0;
  162. }
  163. /* Called when the filesystem is unmounted */
  164. void ext4_release_system_zone(struct super_block *sb)
  165. {
  166. struct rb_node *n = EXT4_SB(sb)->system_blks.rb_node;
  167. struct rb_node *parent;
  168. struct ext4_system_zone *entry;
  169. while (n) {
  170. /* Do the node's children first */
  171. if (n->rb_left) {
  172. n = n->rb_left;
  173. continue;
  174. }
  175. if (n->rb_right) {
  176. n = n->rb_right;
  177. continue;
  178. }
  179. /*
  180. * The node has no children; free it, and then zero
  181. * out parent's link to it. Finally go to the
  182. * beginning of the loop and try to free the parent
  183. * node.
  184. */
  185. parent = rb_parent(n);
  186. entry = rb_entry(n, struct ext4_system_zone, node);
  187. kmem_cache_free(ext4_system_zone_cachep, entry);
  188. if (!parent)
  189. EXT4_SB(sb)->system_blks.rb_node = NULL;
  190. else if (parent->rb_left == n)
  191. parent->rb_left = NULL;
  192. else if (parent->rb_right == n)
  193. parent->rb_right = NULL;
  194. n = parent;
  195. }
  196. EXT4_SB(sb)->system_blks.rb_node = NULL;
  197. }
  198. /*
  199. * Returns 1 if the passed-in block region (start_blk,
  200. * start_blk+count) is valid; 0 if some part of the block region
  201. * overlaps with filesystem metadata blocks.
  202. */
  203. int ext4_data_block_valid(struct ext4_sb_info *sbi, ext4_fsblk_t start_blk,
  204. unsigned int count)
  205. {
  206. struct ext4_system_zone *entry;
  207. struct rb_node *n = sbi->system_blks.rb_node;
  208. if ((start_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
  209. (start_blk + count > ext4_blocks_count(sbi->s_es)))
  210. return 0;
  211. while (n) {
  212. entry = rb_entry(n, struct ext4_system_zone, node);
  213. if (start_blk + count - 1 < entry->start_blk)
  214. n = n->rb_left;
  215. else if (start_blk >= (entry->start_blk + entry->count))
  216. n = n->rb_right;
  217. else
  218. return 0;
  219. }
  220. return 1;
  221. }