block_validity.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext4/block_validity.c
  4. *
  5. * Copyright (C) 2009
  6. * Theodore Ts'o (tytso@mit.edu)
  7. *
  8. * Track which blocks in the filesystem are metadata blocks that
  9. * should never be used as data blocks by files or directories.
  10. */
  11. #include <linux/time.h>
  12. #include <linux/fs.h>
  13. #include <linux/namei.h>
  14. #include <linux/quotaops.h>
  15. #include <linux/buffer_head.h>
  16. #include <linux/swap.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/blkdev.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. u32 ino;
  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. rcu_barrier();
  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. entry1->ino == entry2->ino)
  45. return 1;
  46. return 0;
  47. }
  48. static void release_system_zone(struct ext4_system_blocks *system_blks)
  49. {
  50. struct ext4_system_zone *entry, *n;
  51. rbtree_postorder_for_each_entry_safe(entry, n,
  52. &system_blks->root, node)
  53. kmem_cache_free(ext4_system_zone_cachep, entry);
  54. }
  55. /*
  56. * Mark a range of blocks as belonging to the "system zone" --- that
  57. * is, filesystem metadata blocks which should never be used by
  58. * inodes.
  59. */
  60. static int add_system_zone(struct ext4_system_blocks *system_blks,
  61. ext4_fsblk_t start_blk,
  62. unsigned int count, u32 ino)
  63. {
  64. struct ext4_system_zone *new_entry, *entry;
  65. struct rb_node **n = &system_blks->root.rb_node, *node;
  66. struct rb_node *parent = NULL, *new_node = NULL;
  67. while (*n) {
  68. parent = *n;
  69. entry = rb_entry(parent, struct ext4_system_zone, node);
  70. if (start_blk < entry->start_blk)
  71. n = &(*n)->rb_left;
  72. else if (start_blk >= (entry->start_blk + entry->count))
  73. n = &(*n)->rb_right;
  74. else /* Unexpected overlap of system zones. */
  75. return -EFSCORRUPTED;
  76. }
  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_entry->ino = ino;
  84. new_node = &new_entry->node;
  85. rb_link_node(new_node, parent, n);
  86. rb_insert_color(new_node, &system_blks->root);
  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, &system_blks->root);
  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, &system_blks->root);
  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->root);
  117. while (node) {
  118. entry = rb_entry(node, struct ext4_system_zone, node);
  119. printk(KERN_CONT "%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(KERN_CONT "\n");
  125. }
  126. /*
  127. * Returns 1 if the passed-in block region (start_blk,
  128. * start_blk+count) is valid; 0 if some part of the block region
  129. * overlaps with filesystem metadata blocks.
  130. */
  131. static int ext4_data_block_valid_rcu(struct ext4_sb_info *sbi,
  132. struct ext4_system_blocks *system_blks,
  133. ext4_fsblk_t start_blk,
  134. unsigned int count, ino_t ino)
  135. {
  136. struct ext4_system_zone *entry;
  137. struct rb_node *n;
  138. if ((start_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
  139. (start_blk + count < start_blk) ||
  140. (start_blk + count > ext4_blocks_count(sbi->s_es))) {
  141. sbi->s_es->s_last_error_block = cpu_to_le64(start_blk);
  142. return 0;
  143. }
  144. if (system_blks == NULL)
  145. return 1;
  146. n = system_blks->root.rb_node;
  147. while (n) {
  148. entry = rb_entry(n, struct ext4_system_zone, node);
  149. if (start_blk + count - 1 < entry->start_blk)
  150. n = n->rb_left;
  151. else if (start_blk >= (entry->start_blk + entry->count))
  152. n = n->rb_right;
  153. else {
  154. sbi->s_es->s_last_error_block = cpu_to_le64(start_blk);
  155. return entry->ino == ino;
  156. }
  157. }
  158. return 1;
  159. }
  160. static int ext4_protect_reserved_inode(struct super_block *sb,
  161. struct ext4_system_blocks *system_blks,
  162. u32 ino)
  163. {
  164. struct inode *inode;
  165. struct ext4_sb_info *sbi = EXT4_SB(sb);
  166. struct ext4_map_blocks map;
  167. u32 i = 0, num;
  168. int err = 0, n;
  169. if ((ino < EXT4_ROOT_INO) ||
  170. (ino > le32_to_cpu(sbi->s_es->s_inodes_count)))
  171. return -EINVAL;
  172. inode = ext4_iget(sb, ino, EXT4_IGET_SPECIAL);
  173. if (IS_ERR(inode))
  174. return PTR_ERR(inode);
  175. num = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  176. while (i < num) {
  177. cond_resched();
  178. map.m_lblk = i;
  179. map.m_len = num - i;
  180. n = ext4_map_blocks(NULL, inode, &map, 0);
  181. if (n < 0) {
  182. err = n;
  183. break;
  184. }
  185. if (n == 0) {
  186. i++;
  187. } else {
  188. err = add_system_zone(system_blks, map.m_pblk, n, ino);
  189. if (err < 0) {
  190. if (err == -EFSCORRUPTED) {
  191. ext4_error(sb,
  192. "blocks %llu-%llu from inode %u "
  193. "overlap system zone", map.m_pblk,
  194. map.m_pblk + map.m_len - 1, ino);
  195. }
  196. break;
  197. }
  198. i += n;
  199. }
  200. }
  201. iput(inode);
  202. return err;
  203. }
  204. static void ext4_destroy_system_zone(struct rcu_head *rcu)
  205. {
  206. struct ext4_system_blocks *system_blks;
  207. system_blks = container_of(rcu, struct ext4_system_blocks, rcu);
  208. release_system_zone(system_blks);
  209. kfree(system_blks);
  210. }
  211. /*
  212. * Build system zone rbtree which is used for block validity checking.
  213. *
  214. * The update of system_blks pointer in this function is protected by
  215. * sb->s_umount semaphore. However we have to be careful as we can be
  216. * racing with ext4_data_block_valid() calls reading system_blks rbtree
  217. * protected only by RCU. That's why we first build the rbtree and then
  218. * swap it in place.
  219. */
  220. int ext4_setup_system_zone(struct super_block *sb)
  221. {
  222. ext4_group_t ngroups = ext4_get_groups_count(sb);
  223. struct ext4_sb_info *sbi = EXT4_SB(sb);
  224. struct ext4_system_blocks *system_blks;
  225. struct ext4_group_desc *gdp;
  226. ext4_group_t i;
  227. int flex_size = ext4_flex_bg_size(sbi);
  228. int ret;
  229. system_blks = kzalloc(sizeof(*system_blks), GFP_KERNEL);
  230. if (!system_blks)
  231. return -ENOMEM;
  232. for (i=0; i < ngroups; i++) {
  233. if (ext4_bg_has_super(sb, i) &&
  234. ((i < 5) || ((i % flex_size) == 0)))
  235. add_system_zone(system_blks,
  236. ext4_group_first_block_no(sb, i),
  237. ext4_bg_num_gdb(sb, i) + 1, 0);
  238. gdp = ext4_get_group_desc(sb, i, NULL);
  239. ret = add_system_zone(system_blks,
  240. ext4_block_bitmap(sb, gdp), 1, 0);
  241. if (ret)
  242. goto err;
  243. ret = add_system_zone(system_blks,
  244. ext4_inode_bitmap(sb, gdp), 1, 0);
  245. if (ret)
  246. goto err;
  247. ret = add_system_zone(system_blks,
  248. ext4_inode_table(sb, gdp),
  249. sbi->s_itb_per_group, 0);
  250. if (ret)
  251. goto err;
  252. }
  253. if (ext4_has_feature_journal(sb) && sbi->s_es->s_journal_inum) {
  254. ret = ext4_protect_reserved_inode(sb, system_blks,
  255. le32_to_cpu(sbi->s_es->s_journal_inum));
  256. if (ret)
  257. goto err;
  258. }
  259. /*
  260. * System blks rbtree complete, announce it once to prevent racing
  261. * with ext4_data_block_valid() accessing the rbtree at the same
  262. * time.
  263. */
  264. rcu_assign_pointer(sbi->system_blks, system_blks);
  265. if (test_opt(sb, DEBUG))
  266. debug_print_tree(sbi);
  267. return 0;
  268. err:
  269. release_system_zone(system_blks);
  270. kfree(system_blks);
  271. return ret;
  272. }
  273. /*
  274. * Called when the filesystem is unmounted or when remounting it with
  275. * noblock_validity specified.
  276. *
  277. * The update of system_blks pointer in this function is protected by
  278. * sb->s_umount semaphore. However we have to be careful as we can be
  279. * racing with ext4_data_block_valid() calls reading system_blks rbtree
  280. * protected only by RCU. So we first clear the system_blks pointer and
  281. * then free the rbtree only after RCU grace period expires.
  282. */
  283. void ext4_release_system_zone(struct super_block *sb)
  284. {
  285. struct ext4_system_blocks *system_blks;
  286. system_blks = rcu_dereference_protected(EXT4_SB(sb)->system_blks,
  287. lockdep_is_held(&sb->s_umount));
  288. rcu_assign_pointer(EXT4_SB(sb)->system_blks, NULL);
  289. if (system_blks)
  290. call_rcu(&system_blks->rcu, ext4_destroy_system_zone);
  291. }
  292. int ext4_inode_block_valid(struct inode *inode, ext4_fsblk_t start_blk,
  293. unsigned int count)
  294. {
  295. struct ext4_system_blocks *system_blks;
  296. int ret;
  297. /*
  298. * Lock the system zone to prevent it being released concurrently
  299. * when doing a remount which inverse current "[no]block_validity"
  300. * mount option.
  301. */
  302. rcu_read_lock();
  303. system_blks = rcu_dereference(EXT4_SB(inode->i_sb)->system_blks);
  304. ret = ext4_data_block_valid_rcu(EXT4_SB(inode->i_sb), system_blks,
  305. start_blk, count, inode->i_ino);
  306. rcu_read_unlock();
  307. return ret;
  308. }
  309. int ext4_check_blockref(const char *function, unsigned int line,
  310. struct inode *inode, __le32 *p, unsigned int max)
  311. {
  312. struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
  313. __le32 *bref = p;
  314. unsigned int blk;
  315. if (ext4_has_feature_journal(inode->i_sb) &&
  316. (inode->i_ino ==
  317. le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum)))
  318. return 0;
  319. while (bref < p+max) {
  320. blk = le32_to_cpu(*bref++);
  321. if (blk &&
  322. unlikely(!ext4_inode_block_valid(inode, blk, 1))) {
  323. es->s_last_error_block = cpu_to_le64(blk);
  324. ext4_error_inode(inode, function, line, blk,
  325. "invalid block");
  326. return -EFSCORRUPTED;
  327. }
  328. }
  329. return 0;
  330. }