block_validity.c 9.5 KB

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