block_validity.c 6.2 KB

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