block_validity.c 6.0 KB

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