block_validity.c 6.7 KB

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