block_validity.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 = NULL, *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 {
  75. if (start_blk + count > (entry->start_blk +
  76. entry->count))
  77. entry->count = (start_blk + count -
  78. entry->start_blk);
  79. new_node = *n;
  80. new_entry = rb_entry(new_node, struct ext4_system_zone,
  81. node);
  82. break;
  83. }
  84. }
  85. if (!new_entry) {
  86. new_entry = kmem_cache_alloc(ext4_system_zone_cachep,
  87. GFP_KERNEL);
  88. if (!new_entry)
  89. return -ENOMEM;
  90. new_entry->start_blk = start_blk;
  91. new_entry->count = count;
  92. new_entry->ino = ino;
  93. new_node = &new_entry->node;
  94. rb_link_node(new_node, parent, n);
  95. rb_insert_color(new_node, &system_blks->root);
  96. }
  97. /* Can we merge to the left? */
  98. node = rb_prev(new_node);
  99. if (node) {
  100. entry = rb_entry(node, struct ext4_system_zone, node);
  101. if (can_merge(entry, new_entry)) {
  102. new_entry->start_blk = entry->start_blk;
  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. /* Can we merge to the right? */
  109. node = rb_next(new_node);
  110. if (node) {
  111. entry = rb_entry(node, struct ext4_system_zone, node);
  112. if (can_merge(new_entry, entry)) {
  113. new_entry->count += entry->count;
  114. rb_erase(node, &system_blks->root);
  115. kmem_cache_free(ext4_system_zone_cachep, entry);
  116. }
  117. }
  118. return 0;
  119. }
  120. static void debug_print_tree(struct ext4_sb_info *sbi)
  121. {
  122. struct rb_node *node;
  123. struct ext4_system_zone *entry;
  124. int first = 1;
  125. printk(KERN_INFO "System zones: ");
  126. node = rb_first(&sbi->system_blks->root);
  127. while (node) {
  128. entry = rb_entry(node, struct ext4_system_zone, node);
  129. printk(KERN_CONT "%s%llu-%llu", first ? "" : ", ",
  130. entry->start_blk, entry->start_blk + entry->count - 1);
  131. first = 0;
  132. node = rb_next(node);
  133. }
  134. printk(KERN_CONT "\n");
  135. }
  136. /*
  137. * Returns 1 if the passed-in block region (start_blk,
  138. * start_blk+count) is valid; 0 if some part of the block region
  139. * overlaps with filesystem metadata blocks.
  140. */
  141. static int ext4_data_block_valid_rcu(struct ext4_sb_info *sbi,
  142. struct ext4_system_blocks *system_blks,
  143. ext4_fsblk_t start_blk,
  144. unsigned int count, ino_t ino)
  145. {
  146. struct ext4_system_zone *entry;
  147. struct rb_node *n;
  148. if ((start_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
  149. (start_blk + count < start_blk) ||
  150. (start_blk + count > ext4_blocks_count(sbi->s_es))) {
  151. sbi->s_es->s_last_error_block = cpu_to_le64(start_blk);
  152. return 0;
  153. }
  154. if (system_blks == NULL)
  155. return 1;
  156. n = system_blks->root.rb_node;
  157. while (n) {
  158. entry = rb_entry(n, struct ext4_system_zone, node);
  159. if (start_blk + count - 1 < entry->start_blk)
  160. n = n->rb_left;
  161. else if (start_blk >= (entry->start_blk + entry->count))
  162. n = n->rb_right;
  163. else {
  164. sbi->s_es->s_last_error_block = cpu_to_le64(start_blk);
  165. return entry->ino == ino;
  166. }
  167. }
  168. return 1;
  169. }
  170. static int ext4_protect_reserved_inode(struct super_block *sb,
  171. struct ext4_system_blocks *system_blks,
  172. u32 ino)
  173. {
  174. struct inode *inode;
  175. struct ext4_sb_info *sbi = EXT4_SB(sb);
  176. struct ext4_map_blocks map;
  177. u32 i = 0, num;
  178. int err = 0, n;
  179. if ((ino < EXT4_ROOT_INO) ||
  180. (ino > le32_to_cpu(sbi->s_es->s_inodes_count)))
  181. return -EINVAL;
  182. inode = ext4_iget(sb, ino, EXT4_IGET_SPECIAL);
  183. if (IS_ERR(inode))
  184. return PTR_ERR(inode);
  185. num = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  186. while (i < num) {
  187. cond_resched();
  188. map.m_lblk = i;
  189. map.m_len = num - i;
  190. n = ext4_map_blocks(NULL, inode, &map, 0);
  191. if (n < 0) {
  192. err = n;
  193. break;
  194. }
  195. if (n == 0) {
  196. i++;
  197. } else {
  198. err = add_system_zone(system_blks, map.m_pblk, n, ino);
  199. if (err < 0) {
  200. if (err == -EFSCORRUPTED) {
  201. ext4_error(sb,
  202. "blocks %llu-%llu from inode %u "
  203. "overlap system zone", map.m_pblk,
  204. map.m_pblk + map.m_len - 1, ino);
  205. }
  206. break;
  207. }
  208. i += n;
  209. }
  210. }
  211. iput(inode);
  212. return err;
  213. }
  214. static void ext4_destroy_system_zone(struct rcu_head *rcu)
  215. {
  216. struct ext4_system_blocks *system_blks;
  217. system_blks = container_of(rcu, struct ext4_system_blocks, rcu);
  218. release_system_zone(system_blks);
  219. kfree(system_blks);
  220. }
  221. /*
  222. * Build system zone rbtree which is used for block validity checking.
  223. *
  224. * The update of system_blks pointer in this function is protected by
  225. * sb->s_umount semaphore. However we have to be careful as we can be
  226. * racing with ext4_data_block_valid() calls reading system_blks rbtree
  227. * protected only by RCU. That's why we first build the rbtree and then
  228. * swap it in place.
  229. */
  230. int ext4_setup_system_zone(struct super_block *sb)
  231. {
  232. ext4_group_t ngroups = ext4_get_groups_count(sb);
  233. struct ext4_sb_info *sbi = EXT4_SB(sb);
  234. struct ext4_system_blocks *system_blks;
  235. struct ext4_group_desc *gdp;
  236. ext4_group_t i;
  237. int flex_size = ext4_flex_bg_size(sbi);
  238. int ret;
  239. if (!test_opt(sb, BLOCK_VALIDITY)) {
  240. if (sbi->system_blks)
  241. ext4_release_system_zone(sb);
  242. return 0;
  243. }
  244. if (sbi->system_blks)
  245. return 0;
  246. system_blks = kzalloc(sizeof(*system_blks), GFP_KERNEL);
  247. if (!system_blks)
  248. return -ENOMEM;
  249. for (i=0; i < ngroups; i++) {
  250. if (ext4_bg_has_super(sb, i) &&
  251. ((i < 5) || ((i % flex_size) == 0)))
  252. add_system_zone(system_blks,
  253. ext4_group_first_block_no(sb, i),
  254. ext4_bg_num_gdb(sb, i) + 1, 0);
  255. gdp = ext4_get_group_desc(sb, i, NULL);
  256. ret = add_system_zone(system_blks,
  257. ext4_block_bitmap(sb, gdp), 1, 0);
  258. if (ret)
  259. goto err;
  260. ret = add_system_zone(system_blks,
  261. ext4_inode_bitmap(sb, gdp), 1, 0);
  262. if (ret)
  263. goto err;
  264. ret = add_system_zone(system_blks,
  265. ext4_inode_table(sb, gdp),
  266. sbi->s_itb_per_group, 0);
  267. if (ret)
  268. goto err;
  269. }
  270. if (ext4_has_feature_journal(sb) && sbi->s_es->s_journal_inum) {
  271. ret = ext4_protect_reserved_inode(sb, system_blks,
  272. le32_to_cpu(sbi->s_es->s_journal_inum));
  273. if (ret)
  274. goto err;
  275. }
  276. /*
  277. * System blks rbtree complete, announce it once to prevent racing
  278. * with ext4_data_block_valid() accessing the rbtree at the same
  279. * time.
  280. */
  281. rcu_assign_pointer(sbi->system_blks, system_blks);
  282. if (test_opt(sb, DEBUG))
  283. debug_print_tree(sbi);
  284. return 0;
  285. err:
  286. release_system_zone(system_blks);
  287. kfree(system_blks);
  288. return ret;
  289. }
  290. /*
  291. * Called when the filesystem is unmounted or when remounting it with
  292. * noblock_validity specified.
  293. *
  294. * The update of system_blks pointer in this function is protected by
  295. * sb->s_umount semaphore. However we have to be careful as we can be
  296. * racing with ext4_data_block_valid() calls reading system_blks rbtree
  297. * protected only by RCU. So we first clear the system_blks pointer and
  298. * then free the rbtree only after RCU grace period expires.
  299. */
  300. void ext4_release_system_zone(struct super_block *sb)
  301. {
  302. struct ext4_system_blocks *system_blks;
  303. system_blks = rcu_dereference_protected(EXT4_SB(sb)->system_blks,
  304. lockdep_is_held(&sb->s_umount));
  305. rcu_assign_pointer(EXT4_SB(sb)->system_blks, NULL);
  306. if (system_blks)
  307. call_rcu(&system_blks->rcu, ext4_destroy_system_zone);
  308. }
  309. int ext4_inode_block_valid(struct inode *inode, ext4_fsblk_t start_blk,
  310. unsigned int count)
  311. {
  312. struct ext4_system_blocks *system_blks;
  313. int ret;
  314. /*
  315. * Lock the system zone to prevent it being released concurrently
  316. * when doing a remount which inverse current "[no]block_validity"
  317. * mount option.
  318. */
  319. rcu_read_lock();
  320. system_blks = rcu_dereference(EXT4_SB(inode->i_sb)->system_blks);
  321. ret = ext4_data_block_valid_rcu(EXT4_SB(inode->i_sb), system_blks,
  322. start_blk, count, inode->i_ino);
  323. rcu_read_unlock();
  324. return ret;
  325. }
  326. int ext4_check_blockref(const char *function, unsigned int line,
  327. struct inode *inode, __le32 *p, unsigned int max)
  328. {
  329. struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
  330. __le32 *bref = p;
  331. unsigned int blk;
  332. if (ext4_has_feature_journal(inode->i_sb) &&
  333. (inode->i_ino ==
  334. le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum)))
  335. return 0;
  336. while (bref < p+max) {
  337. blk = le32_to_cpu(*bref++);
  338. if (blk &&
  339. unlikely(!ext4_inode_block_valid(inode, blk, 1))) {
  340. es->s_last_error_block = cpu_to_le64(blk);
  341. ext4_error_inode(inode, function, line, blk,
  342. "invalid block");
  343. return -EFSCORRUPTED;
  344. }
  345. }
  346. return 0;
  347. }