shithub: lwext4

Download patch

ref: 2e93de2e29c061c7cf7f0fc9562a00d3dc16b561
parent: 670b5f15834213fec6fe0ffd41d3419591cb64a5
author: ngkaho1234 <ngkaho1234@gmail.com>
date: Sat May 7 04:55:20 EDT 2016

ext4: count on free blocks and inodes number after journal recovery.

--- a/src/ext4.c
+++ b/src/ext4.c
@@ -48,6 +48,7 @@
 #include "ext4_dir.h"
 #include "ext4_inode.h"
 #include "ext4_super.h"
+#include "ext4_block_group.h"
 #include "ext4_dir_idx.h"
 #include "ext4_xattr.h"
 #include "ext4_journal.h"
@@ -565,7 +566,31 @@
 		jbd_put_fs(jbd_fs);
 		free(jbd_fs);
 	}
+	if (r == EOK && !mp->fs.read_only) {
+		uint32_t bgid;
+		uint64_t free_blocks_count = 0;
+		uint32_t free_inodes_count = 0;
+		struct ext4_block_group_ref bg_ref;
 
+		/* Update superblock's stats */
+		for (bgid = 0;bgid < ext4_block_group_cnt(&mp->fs.sb);bgid++) {
+			r = ext4_fs_get_block_group_ref(&mp->fs, bgid, &bg_ref);
+			if (r != EOK)
+				goto Finish;
+
+			free_blocks_count +=
+				ext4_bg_get_free_blocks_count(bg_ref.block_group,
+						&mp->fs.sb);
+			free_inodes_count +=
+				ext4_bg_get_free_inodes_count(bg_ref.block_group,
+						&mp->fs.sb);
+
+			ext4_fs_put_block_group_ref(&bg_ref);
+		}
+		ext4_sb_set_free_blocks_cnt(&mp->fs.sb, free_blocks_count);
+		ext4_set32(&mp->fs.sb, free_inodes_count, free_inodes_count);
+		/* We don't need to save the superblock stats immediately. */
+	}
 
 Finish:
 	EXT4_MP_UNLOCK(mp);