Skip to contentSkip to conversation

e2fsprogs: Resizing rewrite

PATCH Superseded Jan Kara 5 participants 38 messages Aug 7, 2015 – Aug 26, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jan Kara

author
#0 lore raw

cover e2fsprogs: Resizing rewrite

Hello,

this patch series factors out large parts of resizing code into libext2fs. The motivation of this is that handling of enabling / disabling of more and more features requires moving blocks or inodes with rewriting all the references and using resize2fs for that is not logical from user interface POV.

The series is structured as follows.

* Patches 1-7 are various small cleanups and improvements.

* Patches 8-9 implement the main functionality. The functionality is implemented by two functions:

ext2fs_move_blocks() which gets filesystem and bitmap of blocks which it should make free and the function takes care of everything needed to make the blocks free.

ext2fs_move_inodes() which gets filesystem and bitmap of inodes which it should make free and the function takes care of everything needed to make these inodes free.

* Patch 10 implements enabling / disabling 64-bit feature in tune2fs where it is more logical

* Patches 11-13 add support for changing number of reserved inodes in e2fsprogs

* Patches 14-19 change resize2fs itself to use code from libext2fs to perform resizing.

I have tested the new code for various combinations of fs features to make sure I didn't mess up anything but given this is effectively a rewrite of filesystem resizing, please handle with care.

Honza

author
#1 lore raw

PATCH 1/19 ext2fs: Move function to initialize uninitialized bitmaps to libext2fs

From: Jan Kara <[email protected]>

We will need to initialize uninitialized bitmaps both from resize2fs and tune2fs. Move the function that does this to libext2fs so that code can be shared.

---
 lib/ext2fs/bitops.h       |  1 +
 lib/ext2fs/gen_bitmap64.c | 33 +++++++++++++++++++++++++++++++++
 resize/resize2fs.c        | 38 ++------------------------------------
 3 files changed, 36 insertions(+), 36 deletions(-)
lib/ext2fs/bitops.h +1 -0
index bc596087d2a7..0699a00fcec4 100644
@@ -211,6 +211,7 @@ extern errcode_t ext2fs_find_first_zero_generic_bmap(ext2fs_generic_bitmap bitma
 extern errcode_t ext2fs_find_first_set_generic_bmap(ext2fs_generic_bitmap bitmap,
 						    __u64 start, __u64 end,
 						    __u64 *out);
+extern void ext2fs_init_uninit_block_bitmaps(ext2_filsys fs);
 
 /*
  * The inline routines themselves...
lib/ext2fs/gen_bitmap64.c +33 -0
index 3fc734981c18..34122cfa8901 100644
@@ -905,3 +905,36 @@ errcode_t ext2fs_find_first_set_generic_bmap(ext2fs_generic_bitmap bitmap,
 
 	return ENOENT;
 }
+
+/*
+ * Initialize all unintialized block bitmaps
+ */
+void ext2fs_init_uninit_block_bitmaps(ext2_filsys fs)
+{
+	blk64_t		blk, lblk;
+	dgrp_t		g;
+	int		i;
+
+	if (!ext2fs_has_group_desc_csum(fs))
+		return;
+
+	for (g = 0; g < fs->group_desc_count; g++) {
+		if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT)))
+			continue;
+
+		blk = ext2fs_group_first_block2(fs, g);
+		lblk = ext2fs_group_last_block2(fs, g);
+		ext2fs_unmark_block_bitmap_range2(fs->block_map, blk,
+						  lblk - blk + 1);
+
+		ext2fs_reserve_super_and_bgd(fs, g, fs->block_map);
+		ext2fs_mark_block_bitmap2(fs->block_map,
+					  ext2fs_block_bitmap_loc(fs, g));
+		ext2fs_mark_block_bitmap2(fs->block_map,
+					  ext2fs_inode_bitmap_loc(fs, g));
+		for (i = 0, blk = ext2fs_inode_table_loc(fs, g);
+		     i < (unsigned int) fs->inode_blocks_per_group;
+		     i++, blk++)
+			ext2fs_mark_block_bitmap2(fs->block_map, blk);
+	}
+}
resize/resize2fs.c +2 -35
index 041ff75029b2..02f7d754d316 100644
@@ -41,7 +41,6 @@
 #define RESIZE2FS_DEBUG
 #endif
 
-static void fix_uninit_block_bitmaps(ext2_filsys fs);
 static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size);
 static errcode_t blocks_to_move(ext2_resize_t rfs);
 static errcode_t block_mover(ext2_resize_t rfs);
@@ -119,7 +118,7 @@ errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags,
 	ext2fs_flush(fs);
 
 	init_resource_track(&rtrack, "fix_uninit_block_bitmaps 1", fs->io);
-	fix_uninit_block_bitmaps(fs);
+	ext2fs_init_uninit_block_bitmaps(fs);
 	print_resource_track(rfs, &rtrack, fs->io);
 	retval = ext2fs_dup_handle(fs, &rfs->new_fs);
 	if (retval)
@@ -150,7 +149,7 @@ errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags,
 	print_resource_track(rfs, &rtrack, fs->io);
 
 	init_resource_track(&rtrack, "fix_uninit_block_bitmaps 2", fs->io);
-	fix_uninit_block_bitmaps(rfs->new_fs);
+	ext2fs_init_uninit_block_bitmaps(rfs->new_fs);
 	print_resource_track(rfs, &rtrack, fs->io);
 	/* Clear the block bitmap uninit flag for the last block group */
 	ext2fs_bg_flags_clear(rfs->new_fs, rfs->new_fs->group_desc_count - 1,
@@ -571,39 +570,6 @@ out:
 	return retval;
 }
 
-/*
- * Clean up the bitmaps for unitialized bitmaps
- */
-static void fix_uninit_block_bitmaps(ext2_filsys fs)
-{
-	blk64_t		blk, lblk;
-	dgrp_t		g;
-	int		i;
-
-	if (!ext2fs_has_group_desc_csum(fs))
-		return;
-
-	for (g=0; g < fs->group_desc_count; g++) {
-		if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT)))
-			continue;
-
-		blk = ext2fs_group_first_block2(fs, g);
-		lblk = ext2fs_group_last_block2(fs, g);
-		ext2fs_unmark_block_bitmap_range2(fs->block_map, blk,
-						  lblk - blk + 1);
-
-		ext2fs_reserve_super_and_bgd(fs, g, fs->block_map);
-		ext2fs_mark_block_bitmap2(fs->block_map,
-					  ext2fs_block_bitmap_loc(fs, g));
-		ext2fs_mark_block_bitmap2(fs->block_map,
-					  ext2fs_inode_bitmap_loc(fs, g));
-		for (i = 0, blk = ext2fs_inode_table_loc(fs, g);
-		     i < (unsigned int) fs->inode_blocks_per_group;
-		     i++, blk++)
-			ext2fs_mark_block_bitmap2(fs->block_map, blk);
-	}
-}
author
#2 lore raw

PATCH 2/19 ext2fs: Use range marking function to mark all inode table blocks as used

In reply to Jan Kara

From: Jan Kara <[email protected]>

---
 lib/ext2fs/gen_bitmap64.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
lib/ext2fs/gen_bitmap64.c +3 -5
index 34122cfa8901..2a54514b6b1e 100644
@@ -913,7 +913,6 @@ void ext2fs_init_uninit_block_bitmaps(ext2_filsys fs)
 {
 	blk64_t		blk, lblk;
 	dgrp_t		g;
-	int		i;
 
 	if (!ext2fs_has_group_desc_csum(fs))
 		return;
@@ -932,9 +931,8 @@ void ext2fs_init_uninit_block_bitmaps(ext2_filsys fs)
 					  ext2fs_block_bitmap_loc(fs, g));
 		ext2fs_mark_block_bitmap2(fs->block_map,
 					  ext2fs_inode_bitmap_loc(fs, g));
-		for (i = 0, blk = ext2fs_inode_table_loc(fs, g);
-		     i < (unsigned int) fs->inode_blocks_per_group;
-		     i++, blk++)
-			ext2fs_mark_block_bitmap2(fs->block_map, blk);
+		ext2fs_mark_block_bitmap_range2(fs->block_map,
+						ext2fs_inode_table_loc(fs, g),
+						fs->inode_blocks_per_group);
 	}
 }
Signature · 1 line
2.1.4
author
#3 lore raw

PATCH 3/19 ext2fs: Add pointer to allocator private data into ext2_filsys

In reply to Jan Kara

From: Jan Kara <[email protected]>

---
 lib/ext2fs/ext2fs.h | 2 ++
 1 file changed, 2 insertions(+)
lib/ext2fs/ext2fs.h +2 -0
index 613f0d1bcc91..13cbe567b19c 100644
@@ -279,6 +279,8 @@ struct struct_ext2_filsys {
 
 	io_channel			journal_io;
 	char				*journal_name;
+	/* Private data for use by get_alloc_block() function */
+	void				*alloc_data;
 };
 
 #if EXT2_FLAT_INCLUDES
Signature · 1 line
2.1.4
author
#4 lore raw

PATCH 4/19 ext2fs: Implement ext2fs_allocate_group_table2()

In reply to Jan Kara

Implement ext2fs_allocate_group_table2() which marks blocks also in fs->block_map unconditionally and which always updates allocation statistics.

---
 lib/ext2fs/alloc_tables.c | 93 ++++++++++++++++++++++++++++++++++-------------
 lib/ext2fs/ext2fs.h       |  7 ++++
 2 files changed, 74 insertions(+), 26 deletions(-)
lib/ext2fs/alloc_tables.c +67 -26
index 3e1952fa63e8..9eedd7712be9 100644
@@ -81,19 +81,59 @@ static blk64_t flexbg_offset(ext2_filsys fs, dgrp_t group, blk64_t start_blk,
 	return first_free;
 }
 
-errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
-				      ext2fs_block_bitmap bmap)
+static void update_block_bitmap_stats(ext2_filsys fs, blk64_t blk, int len,
+				      unsigned long flags)
+{
+	if (flags & EXT2FS_ALLOC_TABLE_UPDATE_STATS) {
+		ext2fs_block_alloc_stats_range(fs, blk, len, +1);
+		if (!(flags & EXT2FS_ALLOC_TABLE_SET_BLOCK_BITMAP))
+			ext2fs_unmark_block_bitmap_range2(fs->block_map, blk,
+							  len);
+	} else if (flags & EXT2FS_ALLOC_TABLE_SET_BLOCK_BITMAP) {
+		dgrp_t g, end;
+
+		ext2fs_mark_block_bitmap_range2(fs->block_map, blk, len);
+		/*
+		 * It's an overkill to assume more than two groups can be
+		 * touched but the code is easy to understand this way.
+		 */
+		end = ext2fs_group_of_blk2(fs, blk + len - 1);
+		for (g = ext2fs_group_of_blk2(fs, blk); g <= end; g++) {
+			if (ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT)) {
+				ext2fs_bg_flags_clear(fs, g,
+						      EXT2_BG_BLOCK_UNINIT);
+				ext2fs_group_desc_csum_set(fs, g);
+			}
+		}
+	}
+}
+
+/*
+ * Allocate group metadata (bitmaps, inode table) if not present. If
+ * reserve_bmap is set, we use that as a bitmap of blocks free for allocation
+ * (and update used blocks there as well as in fs->block_map).
+ */
+errcode_t ext2fs_allocate_group_table2(ext2_filsys fs, dgrp_t group,
+				       ext2fs_block_bitmap reserve_bmap,
+				       unsigned long flags)
 {
 	errcode_t	retval;
 	blk64_t		group_blk, start_blk, last_blk, new_blk;
 	dgrp_t		last_grp = 0;
 	int		rem_grps = 0, flexbg_size = 0, table_offset = 0;
+	ext2fs_block_bitmap bmap;
 
 	group_blk = ext2fs_group_first_block2(fs, group);
 	last_blk = ext2fs_group_last_block2(fs, group);
 
-	if (!bmap)
+	if (reserve_bmap == fs->block_map)
+		reserve_bmap = NULL;
+
+	if (!reserve_bmap) {
 		bmap = fs->block_map;
+		flags |= EXT2FS_ALLOC_TABLE_SET_BLOCK_BITMAP;
+	} else
+		bmap = reserve_bmap;
 
 	if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
 				      EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
@@ -153,15 +193,10 @@ errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
 					last_blk, 1, bmap, &new_blk);
 		if (retval)
 			return retval;
-		ext2fs_mark_block_bitmap2(bmap, new_blk);
 		ext2fs_block_bitmap_loc_set(fs, group, new_blk);
-		if (flexbg_size) {
-			dgrp_t gr = ext2fs_group_of_blk2(fs, new_blk);
-			ext2fs_bg_free_blocks_count_set(fs, gr, ext2fs_bg_free_blocks_count(fs, gr) - 1);
-			ext2fs_free_blocks_count_add(fs->super, -1);
-			ext2fs_bg_flags_clear(fs, gr, EXT2_BG_BLOCK_UNINIT);
-			ext2fs_group_desc_csum_set(fs, gr);
-		}
+		update_block_bitmap_stats(fs, new_blk, 1, flags);
+		if (reserve_bmap)
+			ext2fs_mark_block_bitmap2(bmap, new_blk);
 	}
 
 	if (flexbg_size) {
@@ -186,15 +221,10 @@ errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
 					 last_blk, 1, bmap, &new_blk);
 		if (retval)
 			return retval;
-		ext2fs_mark_block_bitmap2(bmap, new_blk);
 		ext2fs_inode_bitmap_loc_set(fs, group, new_blk);
-		if (flexbg_size) {
-			dgrp_t gr = ext2fs_group_of_blk2(fs, new_blk);
-			ext2fs_bg_free_blocks_count_set(fs, gr, ext2fs_bg_free_blocks_count(fs, gr) - 1);
-			ext2fs_free_blocks_count_add(fs->super, -1);
-			ext2fs_bg_flags_clear(fs, gr, EXT2_BG_BLOCK_UNINIT);
-			ext2fs_group_desc_csum_set(fs, gr);
-		}
+		update_block_bitmap_stats(fs, new_blk, 1, flags);
+		if (reserve_bmap)
+			ext2fs_mark_block_bitmap2(bmap, new_blk);
 	}
 
 	/*
@@ -223,18 +253,29 @@ errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
 						bmap, &new_blk);
 		if (retval)
 			return retval;
-		if (flexbg_size)
-			ext2fs_block_alloc_stats_range(fs, new_blk,
-				       fs->inode_blocks_per_group, +1);
-		else
-			ext2fs_mark_block_bitmap_range2(fs->block_map,
-					new_blk, fs->inode_blocks_per_group);
 		ext2fs_inode_table_loc_set(fs, group, new_blk);
+		update_block_bitmap_stats(fs, new_blk,
+					  fs->inode_blocks_per_group, flags);
+		if (reserve_bmap)
+			ext2fs_mark_block_bitmap_range2(bmap,
+					new_blk, fs->inode_blocks_per_group);
 	}
 	ext2fs_group_desc_csum_set(fs, group);
 	return 0;
 }
 
+errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
+				      ext2fs_block_bitmap bmap)
+{
+	unsigned long flags = 0;
+
+	if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
+                                      EXT4_FEATURE_INCOMPAT_FLEX_BG))
+		flags |= EXT2FS_ALLOC_TABLE_UPDATE_STATS;
+
+	return ext2fs_allocate_group_table2(fs, group, bmap, flags);
+}
+
 errcode_t ext2fs_allocate_tables(ext2_filsys fs)
 {
 	errcode_t	retval;
@@ -248,7 +289,7 @@ errcode_t ext2fs_allocate_tables(ext2_filsys fs)
 	for (i = 0; i < fs->group_desc_count; i++) {
 		if (fs->progress_ops && fs->progress_ops->update)
 			(fs->progress_ops->update)(fs, &progress, i);
-		retval = ext2fs_allocate_group_table(fs, i, fs->block_map);
+		retval = ext2fs_allocate_group_table(fs, i, 0);
 		if (retval)
 			return retval;
 	}
lib/ext2fs/ext2fs.h +7 -0
index 13cbe567b19c..3eda8d1f4ad3 100644
@@ -713,6 +713,13 @@ void ext2fs_block_alloc_stats_range(ext2_filsys fs, blk64_t blk,
 extern errcode_t ext2fs_allocate_tables(ext2_filsys fs);
 extern errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
 					     ext2fs_block_bitmap bmap);
+/* Update allocation statistics */
+#define EXT2FS_ALLOC_TABLE_UPDATE_STATS		0x0001
+/* Set fs->block_map in addition to provided bmap */
+#define EXT2FS_ALLOC_TABLE_SET_BLOCK_BITMAP	0x0002
+extern errcode_t ext2fs_allocate_group_table2(ext2_filsys fs, dgrp_t group,
+					      ext2fs_block_bitmap bmap,
+					      unsigned long flags);
 
 /* badblocks.c */
 extern errcode_t ext2fs_u32_list_create(ext2_u32_list *ret, int size);
Signature · 1 line
2.1.4
author
#5 lore raw

PATCH 5/19 resize2fs: Use ext2fs_allocate_group_table2()

In reply to Jan Kara
---
 resize/resize2fs.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
resize/resize2fs.c +4 -5
index 02f7d754d316..9d21cebe16aa 100644
@@ -1448,8 +1448,10 @@ static errcode_t blocks_to_move(ext2_resize_t rfs)
 		    ext2fs_block_bitmap_loc(fs, i))
 			continue;
 
-		retval = ext2fs_allocate_group_table(fs, i,
-						     rfs->reserve_blocks);
+		retval = ext2fs_allocate_group_table2(fs, i,
+				rfs->reserve_blocks,
+				EXT2FS_ALLOC_TABLE_UPDATE_STATS |
+				EXT2FS_ALLOC_TABLE_SET_BLOCK_BITMAP);
 		if (retval)
 			goto errout;
 
@@ -1459,7 +1461,6 @@ static errcode_t blocks_to_move(ext2_resize_t rfs)
 		 */
 		if (ext2fs_block_bitmap_loc(old_fs, i) !=
 		    (blk = ext2fs_block_bitmap_loc(fs, i))) {
-			ext2fs_block_alloc_stats2(fs, blk, +1);
 			if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
 			    !ext2fs_test_block_bitmap2(meta_bmap, blk))
 				ext2fs_mark_block_bitmap2(rfs->move_blocks,
@@ -1467,7 +1468,6 @@ static errcode_t blocks_to_move(ext2_resize_t rfs)
 		}
 		if (ext2fs_inode_bitmap_loc(old_fs, i) !=
 		    (blk = ext2fs_inode_bitmap_loc(fs, i))) {
-			ext2fs_block_alloc_stats2(fs, blk, +1);
 			if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
 			    !ext2fs_test_block_bitmap2(meta_bmap, blk))
 				ext2fs_mark_block_bitmap2(rfs->move_blocks,
@@ -1493,7 +1493,6 @@ static errcode_t blocks_to_move(ext2_resize_t rfs)
 		 */
 		for (blk = ext2fs_inode_table_loc(fs, i), j=0;
 		     j < fs->inode_blocks_per_group ; j++, blk++) {
-			ext2fs_block_alloc_stats2(fs, blk, +1);
 			if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
 			    !ext2fs_test_block_bitmap2(meta_bmap, blk))
 				ext2fs_mark_block_bitmap2(rfs->move_blocks,
Signature · 1 line
2.1.4
author
#6 lore raw

PATCH 6/19 ext2fs: Make ext2fs_reserve_super_and_bgd() clear block_uninit flag

In reply to Jan Kara

Currently ext2fs_reserve_super_and_bgd() does not change group descriptor flag showing whether a particular group block bitmap is initialized or not. Make sure to clear this flag when modifying group's block bitmap.

---
 lib/ext2fs/alloc_sb.c | 5 +++++
 1 file changed, 5 insertions(+)
lib/ext2fs/alloc_sb.c +5 -0
index 8788c00973df..9100ba29ef13 100644
@@ -52,6 +52,11 @@ int ext2fs_reserve_super_and_bgd(ext2_filsys fs,
 	ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
 				  &old_desc_blk, &new_desc_blk, &used_blks);
 
+	if (used_blks && bmap == fs->block_map) {
+		ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
+		ext2fs_group_desc_csum_set(fs, group);
+	}
+
 	if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
 		old_desc_blocks = fs->super->s_first_meta_bg;
 	else
Signature · 1 line
2.1.4
author
#7 lore raw

PATCH 7/19 ext2fs: Provide helper for wiping resize inode

In reply to Jan Kara

Provide a helper function ext2fs_wipe_resize_inode() which clears resize inode and use it from resize2fs.

---
 lib/ext2fs/ext2fs.h  |  1 +
 lib/ext2fs/res_gdt.c | 34 ++++++++++++++++++++++++++++++++++
 resize/resize2fs.c   | 26 ++------------------------
 3 files changed, 37 insertions(+), 24 deletions(-)
lib/ext2fs/ext2fs.h +1 -0
index 3eda8d1f4ad3..0b8d1f6f22b1 100644
@@ -1588,6 +1588,7 @@ extern errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f,
 
 /* res_gdt.c */
 extern errcode_t ext2fs_create_resize_inode(ext2_filsys fs);
+extern errcode_t ext2fs_wipe_resize_inode(ext2_filsys fs);
 
 /*sha256.c */
 #define EXT2FS_SHA256_LENGTH 32
lib/ext2fs/res_gdt.c +34 -0
index 46db61c5cf0d..5a44f5676df4 100644
@@ -238,3 +238,37 @@ out_free:
 	return retval;
 }
 
+/* Clear resize inode so that it can be recreated */
+errcode_t ext2fs_wipe_resize_inode(ext2_filsys fs)
+{
+	struct ext2_inode	inode;
+	errcode_t		retval;
+
+	retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
+	if (retval)
+		goto errout;
+
+	ext2fs_iblk_set(fs, &inode, 1);
+
+	retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
+	if (retval)
+		goto errout;
+
+	/* Check resize inode is sane */
+	if (!inode.i_block[EXT2_DIND_BLOCK]) {
+		retval = EINVAL;
+		goto errout;
+	}
+
+	retval = ext2fs_zero_blocks2(fs, inode.i_block[EXT2_DIND_BLOCK], 1,
+				     NULL, NULL);
+	if (retval)
+		goto errout;
+
+	retval = ext2fs_create_resize_inode(fs);
+	if (retval)
+		goto errout;
+
+errout:
+	return retval;
+}
resize/resize2fs.c +1 -21
index 9d21cebe16aa..a8ba8adcfb86 100644
@@ -2535,34 +2535,12 @@ static errcode_t reserve_sparse_super2_last_group(ext2_resize_t rfs,
  */
 static errcode_t fix_resize_inode(ext2_filsys fs)
 {
-	struct ext2_inode	inode;
 	errcode_t		retval;
 
-	if (!(fs->super->s_feature_compat &
-	      EXT2_FEATURE_COMPAT_RESIZE_INODE))
+	if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE))
 		return 0;
 
-	retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
-	if (retval) goto errout;
-
-	ext2fs_iblk_set(fs, &inode, 1);
-
-	retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
-	if (retval) goto errout;
-
-	if (!inode.i_block[EXT2_DIND_BLOCK]) {
-		/*
-		 * Avoid zeroing out block #0; that's rude.  This
-		 * should never happen anyway since the filesystem
-		 * should be fsck'ed and we assume it is consistent.
-		 */
-		fprintf(stderr, "%s",
-			_("Should never happen: resize inode corrupt!\n"));
-		exit(1);
-	}
author
#12 lore raw

PATCH 10/19 tune2fs: Implement setting and disabling of 64-bit feature

In reply to Jan Kara

Currently resize2fs is used to toggle 64-bit feature. However tune2fs should logically handle that as toggling any other feature. Since we have now factored out block moving code from resize2fs it is relatively easy to implement toggling of the feature within tune2fs.

For now we leave the same functionality also in resize2fs for testing. Later we can make resize2fs call tune2fs for backward compatibility.

---
 misc/tune2fs.c | 367 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 345 insertions(+), 22 deletions(-)
misc/tune2fs.c +345 -22
index d2e8b20f64e2..d6bfad3374d1 100644
@@ -447,10 +447,327 @@ static void request_fsck_afterwards(ext2_filsys fs)
 		printf("%s", _("(and reboot afterwards!)\n"));
 }
 
-static void convert_64bit(ext2_filsys fs, int direction)
+static errcode_t fix_resize_inode(ext2_filsys fs)
 {
-	if (!direction)
-		return;
+	errcode_t retval;
+
+	if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE))
+		return 0;
+
+	retval = ext2fs_wipe_resize_inode(fs);
+	if (retval)
+		return retval;
+
+	return ext2fs_create_resize_inode(fs);
+}
+
+/*
+ * Make blocks that are needed for new group metadata unused and write there
+ * new group descriptors
+ */
+static errcode_t move_bg_metadata(ext2_filsys fs, ext2_filsys new_fs)
+{
+	dgrp_t i;
+	blk64_t b, c, d, old_desc_blocks, new_desc_blocks, j;
+	ext2fs_block_bitmap old_map = NULL, new_map = NULL;
+	int old, new;
+	errcode_t retval;
+	int cluster_ratio;
+	int need_new_block = 0;
+
+	retval = ext2fs_allocate_block_bitmap(fs, "old map", &old_map);
+	if (retval)
+		goto out;
+
+	retval = ext2fs_allocate_block_bitmap(fs, "new map", &new_map);
+	if (retval)
+		goto out;
+
+	if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
+				      EXT2_FEATURE_INCOMPAT_META_BG)) {
+		old_desc_blocks = fs->super->s_first_meta_bg;
+		new_desc_blocks = new_fs->super->s_first_meta_bg;
+	} else {
+		old_desc_blocks = fs->desc_blocks +
+				  fs->super->s_reserved_gdt_blocks;
+		new_desc_blocks = new_fs->desc_blocks +
+				  new_fs->super->s_reserved_gdt_blocks;
+	}
+
+	/* Construct bitmaps of super/descriptor blocks in old and new fs */
+	for (i = 0; i < fs->group_desc_count; i++) {
+		retval = ext2fs_super_and_bgd_loc2(fs, i, &b, &c, &d, NULL);
+		if (retval)
+			goto out;
+		if (b)
+			ext2fs_mark_block_bitmap2(old_map, b);
+		for (j = 0; c != 0 && j < old_desc_blocks; j++)
+			ext2fs_mark_block_bitmap2(old_map, c + j);
+		if (d)
+			ext2fs_mark_block_bitmap2(old_map, d);
+
+		retval = ext2fs_super_and_bgd_loc2(new_fs, i, &b, &c, &d, NULL);
+		if (retval)
+			goto out;
+		if (b)
+			ext2fs_mark_block_bitmap2(new_map, b);
+		for (j = 0; c != 0 && j < new_desc_blocks; j++)
+			ext2fs_mark_block_bitmap2(new_map, c + j);
+		if (d)
+			ext2fs_mark_block_bitmap2(new_map, d);
+	}
+
+	cluster_ratio = EXT2FS_CLUSTER_RATIO(new_fs);
+
+	/* Find changes in block allocations for bg metadata */
+	for (b = EXT2FS_B2C(fs, fs->super->s_first_data_block);
+	     b < ext2fs_blocks_count(new_fs->super);
+	     b += cluster_ratio) {
+		old = ext2fs_test_block_bitmap2(old_map, b);
+		new = ext2fs_test_block_bitmap2(new_map, b);
+
+		if (old && !new) {
+			/* mark old_map, unmark new_map */
+			if (cluster_ratio == 1)
+				ext2fs_block_alloc_stats2(new_fs, b, -1);
+		} else if (!old && new)
+			need_new_block = 1; /* unmark old_map, mark new_map */
+		else {
+			ext2fs_unmark_block_bitmap2(old_map, b);
+			ext2fs_unmark_block_bitmap2(new_map, b);
+		}
+	}
+
+	/*
+	 * new_map now shows blocks that have been newly allocated.
+	 * old_map now shows blocks that have been newly freed.
+	 */
+
+	if (need_new_block) {
+		/*
+		 * Now relocate metadata & data conflicting with new descriptor
+		 * blocks
+		 */
+		retval = ext2fs_move_blocks(new_fs, new_map, old_map);
+		if (retval)
+			goto out;
+
+		/* Now mark blocks for new group descriptors as used */
+		for (b = 0; b < ext2fs_blocks_count(new_fs->super);
+		     b += cluster_ratio) {
+			if (ext2fs_test_block_bitmap2(new_map, b) &&
+			    !ext2fs_test_block_bitmap2(new_fs->block_map, b))
+				ext2fs_block_alloc_stats2(new_fs, b, +1);
+		}
+	}
+
+	if (cluster_ratio > 1) {
+		/* Free unused clusters */
+		for (b = 0; b < ext2fs_blocks_count(new_fs->super);
+		     b += cluster_ratio)
+			if (ext2fs_test_block_bitmap2(old_map, b))
+				ext2fs_block_alloc_stats2(new_fs, b, -1);
+	}
+
+	/* Now update resize inode */
+	retval = fix_resize_inode(new_fs);
+out:
+	if (old_map)
+		ext2fs_free_block_bitmap(old_map);
+	if (new_map)
+		ext2fs_free_block_bitmap(new_map);
+	return retval;
+}
+
+
+/* Toggle 64bit mode */
+static errcode_t resize_group_descriptors(ext2_filsys *fs_p, int direction)
+{
+	void *o, *n, *new_group_desc;
+	dgrp_t i;
+	int copy_size;
+	errcode_t retval;
+	ext2_filsys new_fs = NULL;
+	ext2_filsys fs = *fs_p;
+
+	retval = ext2fs_dup_handle(fs, &new_fs);
+	if (retval)
+		return retval;
+
+	if (direction < 0) {
+		new_fs->super->s_feature_incompat &=
+					~EXT4_FEATURE_INCOMPAT_64BIT;
+		new_fs->super->s_desc_size = EXT2_MIN_DESC_SIZE;
+	} else {
+		new_fs->super->s_feature_incompat |=
+					EXT4_FEATURE_INCOMPAT_64BIT;
+		new_fs->super->s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
+	}
+
+	o = new_fs->group_desc;
+	new_fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
+			EXT2_DESC_PER_BLOCK(new_fs->super));
+	retval = ext2fs_get_arrayzero(new_fs->desc_blocks,
+				      fs->blocksize, &new_group_desc);
+	if (retval)
+		goto out;
+
+	n = new_group_desc;
+
+	if (EXT2_DESC_SIZE(fs->super) <= EXT2_DESC_SIZE(new_fs->super))
+		copy_size = EXT2_DESC_SIZE(fs->super);
+	else
+		copy_size = EXT2_DESC_SIZE(new_fs->super);
+	for (i = 0; i < fs->group_desc_count; i++) {
+		memcpy(n, o, copy_size);
+		n += EXT2_DESC_SIZE(new_fs->super);
+		o += EXT2_DESC_SIZE(fs->super);
+	}
+
+	ext2fs_free_mem(&new_fs->group_desc);
+	new_fs->group_desc = new_group_desc;
+
+	for (i = 0; i < fs->group_desc_count; i++)
+		ext2fs_group_desc_csum_set(new_fs, i);
+
+	/*
+	 * Update number of reserved blocks so that we need to alloc / free as
+	 * few blocks as possible
+	 */
+	if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
+	    (fs->desc_blocks != new_fs->desc_blocks)) {
+		int new;
+
+		new = ((int) new_fs->super->s_reserved_gdt_blocks) +
+			(fs->desc_blocks - new_fs->desc_blocks);
+		if (new < 0)
+			new = 0;
+		if (new > (int) fs->blocksize / 4)
+			new = fs->blocksize / 4;
+		new_fs->super->s_reserved_gdt_blocks = new;
+	}
+
+	retval = move_bg_metadata(fs, new_fs);
+
+	/* Return the modified filesystem */
+	new_fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
+	ext2fs_mark_super_dirty(new_fs);
+	ext2fs_free(fs);
+	*fs_p = new_fs;
+	new_fs = NULL;
+out:
+	if (new_fs)
+		ext2fs_free(new_fs);
+	return retval;
+}
+
+/* Zero out the high bits of extent fields */
+static errcode_t zero_high_bits_in_extents(ext2_filsys fs, ext2_ino_t ino,
+					   struct ext2_inode *inode)
+{
+	ext2_extent_handle_t	handle;
+	struct ext2fs_extent	extent;
+	int			op = EXT2_EXTENT_ROOT;
+	errcode_t		errcode;
+
+	if (!(inode->i_flags & EXT4_EXTENTS_FL))
+		return 0;
+
+	errcode = ext2fs_extent_open(fs, ino, &handle);
+	if (errcode)
+		return errcode;
+
+	while (1) {
+		errcode = ext2fs_extent_get(handle, op, &extent);
+		if (errcode)
+			break;
+
+		op = EXT2_EXTENT_NEXT_SIB;
+
+		if (extent.e_pblk > (1ULL << 32)) {
+			extent.e_pblk &= (1ULL << 32) - 1;
+			errcode = ext2fs_extent_replace(handle, 0, &extent);
+			if (errcode)
+				break;
+		}
+	}
+
+	/* Ok if we run off the end */
+	if (errcode == EXT2_ET_EXTENT_NO_NEXT)
+		errcode = 0;
+	ext2fs_extent_free(handle);
+	return errcode;
+}
+
+/* Zero out the high bits of inodes. */
+static errcode_t zero_high_bits_in_inodes(ext2_filsys fs)
+{
+	int length = EXT2_INODE_SIZE(fs->super);
+	struct ext2_inode *inode = NULL;
+	ext2_inode_scan	scan = NULL;
+	errcode_t	retval;
+	ext2_ino_t	ino;
+
+	if (fs->super->s_creator_os != EXT2_OS_LINUX)
+		return 0;
+
+	retval = ext2fs_open_inode_scan(fs, 0, &scan);
+	if (retval)
+		return retval;
+
+	retval = ext2fs_get_mem(length, &inode);
+	if (retval)
+		goto out;
+
+	do {
+		retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
+		if (retval)
+			goto out;
+		if (!ino)
+			break;
+		if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino))
+			continue;
+
+		/*
+		 * Here's how we deal with high block number fields:
+		 *
+		 *  - i_size_high has been been written out with i_size_lo
+		 *    since the ext2 days, so no conversion is needed.
+		 *
+		 *  - i_blocks_hi is guarded by both the huge_file feature and
+		 *    inode flags and has always been written out with
+		 *    i_blocks_lo if the feature is set.  The field is only
+		 *    ever read if both feature and inode flag are set, so
+		 *    we don't need to zero it now.
+		 *
+		 *  - i_file_acl_high can be uninitialized, so zero it if
+		 *    it isn't already.
+		 */
+		if (inode->osd2.linux2.l_i_file_acl_high) {
+			inode->osd2.linux2.l_i_file_acl_high = 0;
+			retval = ext2fs_write_inode_full(fs, ino, inode,
+							 length);
+			if (retval)
+				goto out;
+		}
+
+		retval = zero_high_bits_in_extents(fs, ino, inode);
+		if (retval)
+			goto out;
+	} while (ino);
+
+out:
+	if (inode)
+		ext2fs_free_mem(&inode);
+	if (scan)
+		ext2fs_close_inode_scan(scan);
+	return retval;
+}
+
+static errcode_t convert_64bit(ext2_filsys *fs_p, int direction)
+{
+	ext2_filsys fs = *fs_p;
+	errcode_t retval;
 
 	/*
 	 * Is resize2fs going to demand a fsck run? Might as well tell the
@@ -459,21 +776,25 @@ static void convert_64bit(ext2_filsys fs, int direction)
 	if (!fsck_requested &&
 	    ((fs->super->s_state & EXT2_ERROR_FS) ||
 	     !(fs->super->s_state & EXT2_VALID_FS) ||
-	     fs->super->s_lastcheck < fs->super->s_mtime))
+	     fs->super->s_lastcheck < fs->super->s_mtime)) {
 		request_fsck_afterwards(fs);
-	if (fsck_requested)
-		fprintf(stderr, _("After running e2fsck, please run `resize2fs %s %s"),
-			direction > 0 ? "-b" : "-s", fs->device_name);
-	else
-		fprintf(stderr, _("Please run `resize2fs %s %s"),
-			direction > 0 ? "-b" : "-s", fs->device_name);
+		return 0;
+	}
 
-	if (undo_file)
-		fprintf(stderr, _(" -z \"%s\""), undo_file);
-	if (direction > 0)
-		fprintf(stderr, _("' to enable 64-bit mode.\n"));
-	else
-		fprintf(stderr, _("' to disable 64-bit mode.\n"));
+	/*
+	 * Read bitmaps now since we are definitely going to need them and they
+	 * get properly duplicated when creating new fs handle in
+	 * resize_group_descriptors()
+	 */
+	retval = ext2fs_read_bitmaps(fs);
+	if (retval)
+		return retval;
+
+	retval = resize_group_descriptors(fs_p, direction);
+	if (retval)
+		return retval;
+
+	return zero_high_bits_in_inodes(*fs_p);
 }
 
 /* Rewrite extents */
@@ -1253,9 +1574,9 @@ mmp_error:
 	}
 
 	/*
-	 * We don't actually toggle 64bit; resize2fs does that.  But this
-	 * must come after the metadata_csum feature_on so that it won't
-	 * complain about the lack of 64bit.
+	 * Handle 64-bit conversion. We return the feature to the original
+	 * value for now until the conversion really takes place. Otherwise
+	 * some helper macros get confused.
 	 */
 	if (FEATURE_ON(E2P_FEATURE_INCOMPAT,
 		       EXT4_FEATURE_INCOMPAT_64BIT)) {
@@ -1264,8 +1585,8 @@ mmp_error:
 					  "while mounted!\n"));
 			exit(1);
 		}
-		sb->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_64BIT;
 		feature_64bit = 1;
+		sb->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_64BIT;
 	}
 	if (FEATURE_OFF(E2P_FEATURE_INCOMPAT,
 			EXT4_FEATURE_INCOMPAT_64BIT)) {
@@ -1274,8 +1595,8 @@ mmp_error:
 					  "while mounted!\n"));
 			exit(1);
 		}
-		sb->s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
 		feature_64bit = -1;
+		sb->s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
 	}
 
 	if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
@@ -3080,6 +3401,9 @@ retry_open:
 		}
 	}
 
+	if (feature_64bit)
+		convert_64bit(&fs, feature_64bit);
+
 	if (rewrite_checksums)
 		rewrite_metadata_checksums(fs);
 
@@ -3113,6 +3437,5 @@ closefs:
 		exit(1);
 	}
 
-	convert_64bit(fs, feature_64bit);
 	return (ext2fs_close_free(&fs) ? 1 : 0);
 }
Signature · 1 line
2.1.4
author
#16 lore raw

PATCH 11/19 mke2fs: Allow specifying number of reserved inodes

In reply to Jan Kara

From: Jan Kara <[email protected]>

Add option to specify number of reserved inodes in the created filesystem.

---
 misc/mke2fs.8.in      |  6 ++++++
 misc/mke2fs.c         | 40 +++++++++++++++++++++++++++++++++++++++-
 misc/mke2fs.conf.5.in |  6 ++++++
 3 files changed, 51 insertions(+), 1 deletion(-)
misc/mke2fs.8.in +6 -0
index 40c40d3ed065..520a07185f9f 100644
@@ -384,6 +384,12 @@ Do not attempt to discard blocks at mkfs time.
 @[email protected] quota
 @QUOTA_MAN_COMMENT@feature is set. Without this extended option, the default
 @QUOTA_MAN_COMMENT@behavior is to initialize both user and group quotas.
+.TP
+.BI reserved_inodes= number
+Specify the number of inodes reserved for system files. This number must be
+at least 10. Currently 10 is enough but future features may require additional
+reserved inodes. Reserving more inodes after file system is created requires
+full file system scan so it can take a long time.
 .RE
 .TP
 .BI \-f " fragment-size"
misc/mke2fs.c +39 -1
index 78b1252d8519..d61d1a332e67 100644
@@ -1024,6 +1024,34 @@ static void parse_extended_opts(struct ext2_super_block *param,
 				r_usage++;
 				continue;
 			}
+		} else if (!strcmp(token, "reserved_inodes")) {
+			unsigned int reserved_inodes;
+
+			if (!arg) {
+				r_usage++;
+				badopt = token;
+				continue;
+			}
+			reserved_inodes = strtoul(arg, &p, 0);
+			if (*p) {
+				fprintf(stderr,
+					_("Invalid number of reserved inodes "
+					  "%s\n"),
+					arg);
+				r_usage++;
+				continue;
+			}
+			/* Ino 0 is invalid so bump by 1... */
+			reserved_inodes++;
+			if (reserved_inodes < EXT2_GOOD_OLD_FIRST_INO) {
+				fprintf(stderr,
+					_("Too few reserved inodes "
+					  "%s (must be at least %u)\n"),
+					arg, EXT2_GOOD_OLD_FIRST_INO - 1);
+				r_usage++;
+				continue;
+			}
+			param->s_first_ino = reserved_inodes;
 		} else {
 			r_usage++;
 			badopt = token;
@@ -1049,7 +1077,8 @@ static void parse_extended_opts(struct ext2_super_block *param,
 			"\ttest_fs\n"
 			"\tdiscard\n"
 			"\tnodiscard\n"
-			"\tquotatype=<usr OR grp>\n\n"),
+			"\tquotatype=<usr OR grp>\n"
+			"\treserved_inodes=<number of reserved inodes>\n\n"),
 			badopt ? badopt : "");
 		free(buf);
 		exit(1);
@@ -2422,6 +2451,15 @@ profile_error:
 		exit(1);
 	}
 
+	/* Count with one more inode for lost+found */
+	if (fs_param.s_first_ino >= fs_param.s_inodes_count + 1) {
+		com_err(program_name, 0, _("asked for more reserved inodes than filesystem has "
+			"available (%u >= %u)\n"),
+			(unsigned int)fs_param.s_first_ino,
+			(unsigned int)fs_param.s_inodes_count + 1);
+		exit(1);
+	}
+
 	/*
 	 * Calculate number of blocks to reserve
 	 */
misc/mke2fs.conf.5.in +6 -0
index ad6c11b3cb7b..06ca9e4eabc4 100644
@@ -195,6 +195,12 @@ specify one on the command line, and the filesystem-type
 specific section of the configuration file does not specify a default
 reserved ratio. This value can be a floating point number.
 .TP
+.I reserved_inodes
+This relation specifies the default number of inodes reserved for system files.
+The number must be at least 10. Currently 10 is enough but future features may
+require additional reserved inodes. Reserving more inodes after file system is
+created requires full file system scan so it can take a long time.
+.TP
 .I undo_dir
 This relation specifies the directory where the undo file should be
 stored.  It can be overridden via the
Signature · 1 line
2.1.4
author
#8 lore raw

PATCH 8/19 ext2fs: Implement block moving in libext2fs

In reply to Jan Kara

From: Jan Kara <[email protected]>

---
 lib/ext2fs/Makefile.in  |   6 +-
 lib/ext2fs/extent_map.c | 233 ++++++++++++++
 lib/ext2fs/move.c       | 797 ++++++++++++++++++++++++++++++++++++++++++++++++
 lib/ext2fs/move.h       |  23 ++
 4 files changed, 1058 insertions(+), 1 deletion(-)
 create mode 100644 lib/ext2fs/extent_map.c
 create mode 100644 lib/ext2fs/move.c
 create mode 100644 lib/ext2fs/move.h
lib/ext2fs/Makefile.in +5 -1
index 26aaf6fb1a15..02ede7bbf856 100644
@@ -125,7 +125,9 @@ OBJS= $(DEBUGFS_LIB_OBJS) $(RESIZE_LIB_OBJS) $(E2IMAGE_LIB_OBJS) \
 	unlink.o \
 	valid_blk.o \
 	version.o \
-	rbtree.o
+	rbtree.o \
+	extent_map.o \
+	move.o
 
 SRCS= ext2_err.c \
 	$(srcdir)/alloc.c \
@@ -215,6 +217,8 @@ SRCS= ext2_err.c \
 	$(srcdir)/write_bb_file.c \
 	$(srcdir)/rbtree.c \
 	$(srcdir)/tst_libext2fs.c \
+	$(srcdir)/extent_map.c \
+	$(srcdir)/move.c \
 	$(DEBUG_SRCS)
 
 HFILES= bitops.h ext2fs.h ext2_io.h ext2_fs.h ext2_ext_attr.h ext3_extents.h \
lib/ext2fs/extent_map.c new file +233 -0

233 lines not on this page · raw message

lib/ext2fs/move.c new file +797 -0

797 lines not on this page · raw message

lib/ext2fs/move.h new file +23 -0
new file mode 100644
index 000000000000..8d66aa039ec0
@@ -0,0 +1,23 @@
+#ifndef _EXT2FS_MOVE_H
+#define _EXT2FS_MOVE_H
+
+#include "ext2fs.h"
+
+typedef struct _ext2_map_extent *ext2_map_extent;
+
+/* extent_map.c */
+extern int ext2fs_extent_table_empty(ext2_map_extent extent);
+extern errcode_t ext2fs_create_extent_table(ext2_map_extent *ret_extent,
+					    __u64 size);
+extern void ext2fs_free_extent_table(ext2_map_extent extent);
+extern errcode_t ext2fs_add_extent_entry(ext2_map_extent extent,
+					 __u64 old_loc, __u64 new_loc);
+extern __u64 ext2fs_extent_translate(ext2_map_extent extent, __u64 old_loc);
+extern errcode_t ext2fs_iterate_extent(ext2_map_extent extent, __u64 *old_loc,
+				       __u64 *new_loc, __u64 *size);
+
+/* move.c */
+errcode_t ext2fs_move_blocks(ext2_filsys fs, ext2fs_block_bitmap move_map,
+			     ext2fs_block_bitmap reuse_map);
+
+#endif
Signature · 1 line
2.1.4
author
#11 lore raw

PATCH 9/19 ext2fs: Implement inode moving in libext2fs

In reply to Jan Kara
---
 lib/ext2fs/move.c | 422 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/ext2fs/move.h |   1 +
 2 files changed, 423 insertions(+)
lib/ext2fs/move.c +422 -0
index 5fc7a5fd53b6..6e286f118465 100644
@@ -795,3 +795,425 @@ out:
 
 	return retval;
 }
+
+static int add_dir_block(ext2_filsys fs, blk64_t *block_nr,
+			 e2_blkcnt_t blockcnt,
+			 blk64_t ref_block EXT2FS_ATTR((unused)),
+			 int ref_offset EXT2FS_ATTR((unused)),
+			 void *priv_data)
+{
+	struct update_ref_process_block_struct *pb;
+	errcode_t retval;
+
+	pb = (struct update_ref_process_block_struct *) priv_data;
+	retval = ext2fs_add_dir_block2(fs->dblist, pb->ino, *block_nr,
+				       blockcnt);
+	if (retval) {
+		pb->error = retval;
+		return BLOCK_ABORT;
+	}
+	return 0;
+}
+static errcode_t build_dblist(ext2_filsys fs)
+{
+	errcode_t		retval;
+	ext2_inode_scan 	scan = NULL;
+	ext2_ino_t		ino;
+	struct ext2_inode 	*inode = NULL;
+	int			inode_size;
+	char			*block_buf = NULL;
+	struct ext2fs_numeric_progress_struct progress;
+	struct update_ref_process_block_struct pb;
+
+	retval = ext2fs_init_dblist(fs, NULL);
+	if (retval)
+		goto out;
+
+	retval = ext2fs_get_array(fs->blocksize, 3, &block_buf);
+	if (retval)
+		goto out;
+
+	retval = ext2fs_open_inode_scan(fs, 0, &scan);
+	if (retval)
+		goto out;
+
+	if (fs->progress_ops) {
+		if (fs->progress_ops->init)
+			fs->progress_ops->init(fs, &progress,
+				       "Building list of directory blocks",
+				       fs->group_desc_count);
+		ext2fs_set_inode_callback(scan, progress_callback, &progress);
+	}
+
+	inode_size = EXT2_INODE_SIZE(fs->super);
+	inode = malloc(inode_size);
+	if (!inode) {
+		retval = ENOMEM;
+		goto out;
+	}
+	pb.inode = inode;
+	pb.error = 0;
+	pb.bmap = NULL;
+
+	while (1) {
+		retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
+		if (retval)
+			goto out_progress;
+		if (!ino)
+			break;
+
+		if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
+			continue; /* inode not in use */
+
+		if (!LINUX_S_ISDIR(inode->i_mode))
+			continue;
+
+		if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
+			pb.ino = ino;
+			retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
+						       add_dir_block,
+						       &pb);
+			if (retval)
+				goto out_progress;
+			if (pb.error) {
+				retval = pb.error;
+				goto out_progress;
+			}
+		} else if (inode->i_flags & EXT4_INLINE_DATA_FL) {
+			/* Add inline directory inodes to the list */
+			retval = ext2fs_add_dir_block2(fs->dblist, ino, 0, 0);
+			if (retval)
+				goto out_progress;
+		}
+	}
+out_progress:
+	if (fs->progress_ops && fs->progress_ops->close)
+		fs->progress_ops->close(fs, &progress, NULL);
+out:
+	if (scan)
+		ext2fs_close_inode_scan(scan);
+	if (block_buf)
+		ext2fs_free_mem(&block_buf);
+	if (inode)
+		free(inode);
+	if (retval && fs->dblist) {
+		ext2fs_free_dblist(fs->dblist);
+		fs->dblist = NULL;
+	}
+	return retval;
+}
+
+/* Allocate space for inodes that need moving and move them there */
+static errcode_t alloc_copy_inodes(ext2_filsys fs, ext2fs_inode_bitmap move_map,
+				   ext2_map_extent imap)
+{
+	errcode_t		retval;
+	__u64			ino;
+	ext2_ino_t		new_ino;
+	dgrp_t			group;
+	int			inode_size;
+	struct ext2_inode	*inode = NULL;
+	ext2fs_inode_bitmap	merged_map = NULL;
+	struct ext2fs_numeric_progress_struct progress;
+
+	inode_size = EXT2_INODE_SIZE(fs->super);
+	inode = malloc(inode_size);
+	if (!inode) {
+		retval = ENOMEM;
+		goto out;
+	}
+
+	retval = ext2fs_copy_bitmap(fs->inode_map, &merged_map);
+	if (retval)
+		goto out;
+
+	for (ino = 1; ino <= fs->super->s_inodes_count; ino++) {
+		if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino) &&
+		    ext2fs_test_inode_bitmap2(move_map, ino))
+			ext2fs_mark_inode_bitmap2(merged_map, ino);
+	}
+
+	if (fs->progress_ops && fs->progress_ops->init)
+		fs->progress_ops->init(fs, &progress, "Moving inodes",
+				       fs->group_desc_count);
+	for (group = 0; group < fs->group_desc_count; group++) {
+		if (fs->progress_ops && fs->progress_ops->update) {
+			io_channel_flush(fs->io);
+			fs->progress_ops->update(fs, &progress, group);
+		}
+		if (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT))
+			continue;
+
+		for (ino = fs->super->s_inodes_per_group * group + 1;
+		     ino <= fs->super->s_inodes_count &&
+		     ino <= fs->super->s_inodes_per_group * (group + 1);
+		     ino++) {
+			if (!ext2fs_fast_test_inode_bitmap2(move_map, ino))
+				continue;
+
+			retval = ext2fs_read_inode_full(fs, ino, inode,
+							inode_size);
+			if (retval)
+				goto out_progress;
+
+			if (inode->i_links_count == 0 &&
+			    ino != EXT2_RESIZE_INO)
+				continue; /* inode not in use */
+
+			retval = ext2fs_new_inode(fs, 0, 0, merged_map,
+						  &new_ino);
+			if (retval)
+				goto out_progress;
+			ext2fs_inode_alloc_stats2(fs, new_ino, +1,
+						  LINUX_S_ISDIR(inode->i_mode));
+			ext2fs_inode_alloc_stats2(fs, ino, -1,
+						  LINUX_S_ISDIR(inode->i_mode));
+			ext2fs_mark_inode_bitmap2(merged_map, new_ino);
+			inode->i_ctime = time(0);
+			retval = ext2fs_write_inode_full(fs, new_ino, inode,
+							 inode_size);
+			if (retval)
+				goto out_progress;
+
+			retval = ext2fs_add_extent_entry(imap, ino, new_ino);
+			if (retval)
+				goto out_progress;
+		}
+	}
+	io_channel_flush(fs->io);
+out_progress:
+	if (fs->progress_ops && fs->progress_ops->close)
+		fs->progress_ops->close(fs, &progress, NULL);
+out:
+	if (inode)
+		free(inode);
+	if (merged_map)
+		ext2fs_free_inode_bitmap(merged_map);
+	return retval;
+}
+
+struct dblist_scan_data {
+	ext2_filsys fs;
+	blk_t cur_block;
+	blk_t blocks;
+	ext2_ino_t last_dir_ino;
+	int dir_moved;
+	int times_updated;
+	errcode_t error;
+	ext2_map_extent imap;
+	struct ext2fs_numeric_progress_struct progress;
+};
+
+static int remap_db_entry(ext2_filsys fs, struct ext2_db_entry2 *db_info,
+                          void *priv_data)
+{
+	struct dblist_scan_data *data = priv_data;
+	__u64 new_ino;
+
+	new_ino = ext2fs_extent_translate(data->imap, db_info->ino);
+	if (new_ino)
+		db_info->ino = new_ino;
+	if (fs->progress_ops && fs->progress_ops->update)
+		fs->progress_ops->update(fs, &data->progress, 
+					 data->cur_block++);
+	return 0;
+}
+
+/* Update inode numbers in fs->dblist */
+static errcode_t rewrite_dblist_refs(ext2_filsys fs, ext2_map_extent imap)
+{
+	errcode_t		retval;
+	struct dblist_scan_data	data;
+
+	data.fs = fs;
+	data.cur_block = 0;
+	data.blocks = ext2fs_dblist_count2(fs->dblist);
+	data.last_dir_ino = 0;
+	data.error = 0;
+	data.imap = imap;
+
+	if (fs->progress_ops && fs->progress_ops->init)
+		fs->progress_ops->init(fs, &data.progress,
+				       "Remapping list of directory blocks",
+				       data.blocks);
+
+	retval = ext2fs_dblist_iterate2(fs->dblist, remap_db_entry, &data);
+	if (retval)
+		return retval;
+	if (data.error)
+		return data.error;
+
+	if (fs->progress_ops && fs->progress_ops->close)
+		fs->progress_ops->close(fs, &data.progress, NULL);
+	return 0;
+}
+
+static int check_and_change_inodes(ext2_ino_t dir,
+				   int entry EXT2FS_ATTR((unused)),
+				   struct ext2_dir_entry *dirent, int offset,
+				   int  blocksize EXT2FS_ATTR((unused)),
+				   char *buf EXT2FS_ATTR((unused)),
+				   void *priv_data)
+{
+	struct dblist_scan_data *data = priv_data;
+	struct ext2_inode	inode;
+	ext2_ino_t		new_ino;
+	errcode_t		retval;
+	int			ret = 0;
+
+	if (data->last_dir_ino != dir) {
+		data->last_dir_ino = dir;
+		data->times_updated = 0;
+		data->dir_moved = 0;
+		/*
+		 * If we have checksums enabled and the has moved, then we must
+		 * rewrite all dir blocks with new checksums.
+		 */
+		if (EXT2_HAS_RO_COMPAT_FEATURE(data->fs->super,
+				       EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
+		    ext2fs_extent_translate(data->imap, dir))
+			data->dir_moved = 1;
+	}
+
+	if (data->dir_moved)
+		ret |= DIRENT_CHANGED;
+
+	if (!dirent->inode)
+		return ret;
+
+	new_ino = ext2fs_extent_translate(data->imap, dirent->inode);
+	if (!new_ino)
+		return ret;
+	dirent->inode = new_ino;
+	ret |= DIRENT_CHANGED;
+
+	/* Update directory mtime and ctime for each dir */
+	if (!data->times_updated) {
+		retval = ext2fs_read_inode(data->fs, dir, &inode);
+		if (retval == 0) {
+			inode.i_mtime = inode.i_ctime = time(0);
+			retval = ext2fs_write_inode(data->fs, dir, &inode);
+			if (retval) {
+				data->error = retval;
+				ret |= DIRENT_ABORT;
+			}
+		}
+		data->times_updated = 1;
+	}
+
+	if (data->fs->progress_ops && data->fs->progress_ops->update &&
+	    !offset) {
+		io_channel_flush(data->fs->io);
+		data->fs->progress_ops->update(data->fs, &data->progress,
+					       data->cur_block++);
+	}
+	return ret;
+}
+
+/* Scan all directory blocks and update inode references */
+static errcode_t fix_inode_refs(ext2_filsys fs, ext2_map_extent imap)
+{
+	errcode_t		retval;
+	struct dblist_scan_data	data;
+
+	data.fs = fs;
+	data.cur_block = 0;
+	data.blocks = ext2fs_dblist_count2(fs->dblist);
+	data.last_dir_ino = 0;
+	data.error = 0;
+	data.imap = imap;
+
+	if (fs->progress_ops && fs->progress_ops->init)
+		fs->progress_ops->init(fs, &data.progress,
+				       "Updating inode references",
+				       data.blocks);
+
+	/*
+	 * dblist still has old inode numbers so iteration will use inodes
+	 * at old positions. That is fine though because we didn't clobber
+	 * that space yet.
+	 */
+	fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
+	retval = ext2fs_dblist_dir_iterate(fs->dblist,
+					   DIRENT_FLAG_INCLUDE_EMPTY, 0,
+					   check_and_change_inodes, &data);
+	fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
+	if (fs->progress_ops && fs->progress_ops->close)
+		fs->progress_ops->close(fs, &data.progress, NULL);
+	if (retval)
+		return retval;
+	if (data.error)
+		return data.error;
+
+	return 0;
+}
+
+/*
+ * Generic inode moving function. It moves inodes specified in move_map so that
+ * they become unused (it marks these inodes as  free in the inode bitmap). It
+ * takes care of rewriting references from directory entries as well.
+ *
+ * The function uses fs->dblist for rewriting if present (the caller is
+ * responsible for it to be correct and complete in that case) and updates
+ * inode numbers there. Otherwise we build our own fs->dblist.
+ */
+errcode_t ext2fs_move_inodes(ext2_filsys fs, ext2fs_inode_bitmap move_map)
+{
+	errcode_t		retval;
+	ext2_map_extent		imap = NULL;
+	ext2_ino_t		ino;
+	unsigned int		inodes_to_move = 0, inodes_free = 0;
+
+	retval = ext2fs_read_bitmaps(fs);
+	if (retval)
+		return retval;
+
+	for (ino = 1; ino <= fs->super->s_inodes_count; ino++) {
+		int used, move;
+
+		used = ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino);
+		move = ext2fs_fast_test_inode_bitmap2(move_map, ino);
+		if (!used && !move)
+			inodes_free++;
+		else if (used && move)
+			inodes_to_move++;
+	}
+
+	if (inodes_free < inodes_to_move) {
+		retval = ENOSPC;
+		goto out;
+	}
+
+	retval = ext2fs_create_extent_table(&imap, 0);
+	if (retval)
+		goto out;
+
+	if (!fs->dblist) {
+		retval = build_dblist(fs);
+		if (retval)
+			goto out;
+	}
+
+	retval = alloc_copy_inodes(fs, move_map, imap);
+	if (retval)
+		goto out;
+
+	/* Nothing to map? */
+	if (ext2fs_extent_table_empty(imap))
+		goto out;
+
+	retval = fix_inode_refs(fs, imap);
+	if (retval)
+		goto out;
+
+	retval = rewrite_dblist_refs(fs, imap);
+out:
+	if (retval && fs->dblist) {
+		/* dblist is likely invalid, free it */
+		ext2fs_free_dblist(fs->dblist);
+		fs->dblist = NULL;
+	}
+	if (imap)
+		ext2fs_free_extent_table(imap);
+	return retval;
+}
lib/ext2fs/move.h +1 -0
index 8d66aa039ec0..9218d374c1eb 100644
@@ -19,5 +19,6 @@ extern errcode_t ext2fs_iterate_extent(ext2_map_extent extent, __u64 *old_loc,
 /* move.c */
 errcode_t ext2fs_move_blocks(ext2_filsys fs, ext2fs_block_bitmap move_map,
 			     ext2fs_block_bitmap reuse_map);
+errcode_t ext2fs_move_inodes(ext2_filsys fs, ext2fs_inode_bitmap move_map);
 
 #endif
Signature · 1 line
2.1.4
author
#19 lore raw

PATCH 12/19 libext2fs: Bump default number of reserved inodes to 64

In reply to Jan Kara

From: Jan Kara <[email protected]>

We ran out of reserved inodes so bump the default number of reserved inodes to 64 to get some breathing space. Otherwise we have to do a full fs scan when increasing number of reserved inodes when some feature needing another reserved inode is enabled. This consumes 13.5 KB on a filesystem which is negligible these days.

---
 lib/ext2fs/initialize.c | 2 +-
 misc/mke2fs.8.in        | 8 ++++----
 misc/mke2fs.conf.5.in   | 7 ++++---
 3 files changed, 9 insertions(+), 8 deletions(-)
lib/ext2fs/initialize.c +1 -1
index 75fbf8ee0061..0ecf4606ce48 100644
@@ -186,7 +186,7 @@ errcode_t ext2fs_initialize(const char *name, int flags,
 
 	set_field(s_rev_level, EXT2_GOOD_OLD_REV);
 	if (super->s_rev_level >= EXT2_DYNAMIC_REV) {
-		set_field(s_first_ino, EXT2_GOOD_OLD_FIRST_INO);
+		set_field(s_first_ino, 64);
 		set_field(s_inode_size, EXT2_GOOD_OLD_INODE_SIZE);
 		if (super->s_inode_size >= sizeof(struct ext2_inode_large)) {
 			int extra_isize = sizeof(struct ext2_inode_large) -
misc/mke2fs.8.in +4 -4
index 520a07185f9f..6eef1d97bfbf 100644
@@ -386,10 +386,10 @@ Do not attempt to discard blocks at mkfs time.
 @QUOTA_MAN_COMMENT@behavior is to initialize both user and group quotas.
 .TP
 .BI reserved_inodes= number
-Specify the number of inodes reserved for system files. This number must be
-at least 10. Currently 10 is enough but future features may require additional
-reserved inodes. Reserving more inodes after file system is created requires
-full file system scan so it can take a long time.
+Specify the number of inodes reserved for system files. This number must be at
+least 10, default is 64. Currently 10 is enough but future features may require
+additional reserved inodes. Reserving more inodes after file system is created
+requires full file system scan so it can take a long time.
 .RE
 .TP
 .BI \-f " fragment-size"
misc/mke2fs.conf.5.in +4 -3
index 06ca9e4eabc4..b35767bfedf2 100644
@@ -197,9 +197,10 @@ reserved ratio. This value can be a floating point number.
 .TP
 .I reserved_inodes
 This relation specifies the default number of inodes reserved for system files.
-The number must be at least 10. Currently 10 is enough but future features may
-require additional reserved inodes. Reserving more inodes after file system is
-created requires full file system scan so it can take a long time.
+The number must be at least 10, default is 64. Currently 10 is enough but
+future features may require additional reserved inodes. Reserving more inodes
+after file system is created requires full file system scan so it can take a
+long time.
 .TP
 .I undo_dir
 This relation specifies the directory where the undo file should be
Signature · 1 line
2.1.4
author
#25 lore raw

PATCH 13/19 tune2fs: Add support for changing number of reserved inodes

In reply to Jan Kara
---
 misc/tune2fs.8.in |   5 +++
 misc/tune2fs.c    | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 107 insertions(+)
misc/tune2fs.8.in +5 -0
index 4373fc49b255..922705329112 100644
@@ -236,6 +236,11 @@ program.
 This superblock setting is only honored in 2.6.35+ kernels;
 and not at all by the ext2 and ext3 file system drivers.
 .TP
+.BI reserved_inodes= number_of_reserved_inodes
+Set the number of inodes reserved for system files. This number must be
+at least 10. Currently 10 is enough but future features may require additional
+reserved inodes. Reserving more inodes requires full file system scan so it
+can take a long time.
 .B test_fs
 Set a flag in the filesystem superblock indicating that it may be
 mounted using experimental kernel code, such as the ext4dev filesystem.
misc/tune2fs.c +102 -0

132 lines not on this page · raw message

Signature · 1 line
2.1.4
author
#30 lore raw

PATCH 14/19 resize2fs: Rip out 64-bit feature handling from resize2fs

In reply to Jan Kara

Now that tune2fs is able to properly set / clear 64BIT feature, just rip out the handling from resize2fs as it doesn't make much sense there from user point of view. So far this functionality wasn't in any officially released e2fsprogs so just printing message that tune2fs should be used should be fine.

---
 resize/main.c         |  61 ++--------
 resize/resize2fs.8.in |  17 +--
 resize/resize2fs.c    | 331 +-------------------------------------------------
 3 files changed, 12 insertions(+), 397 deletions(-)
resize/main.c +8 -53
index 9da3a958cc01..13f62e452044 100644
@@ -43,7 +43,7 @@ static char *device_name, *io_options;
 static void usage (char *prog)
 {
 	fprintf (stderr, _("Usage: %s [-d debug_flags] [-f] [-F] [-M] [-P] "
-			   "[-p] device [-b|-s|new_size] [-z undo_file]\n\n"),
+			   "[-p] device [new_size] [-z undo_file]\n\n"),
 		 prog);
 
 	exit (1);
@@ -306,15 +306,13 @@ int main (int argc, char ** argv)
 		case 'S':
 			use_stride = atoi(optarg);
 			break;
-		case 'b':
-			flags |= RESIZE_ENABLE_64BIT;
-			break;
-		case 's':
-			flags |= RESIZE_DISABLE_64BIT;
-			break;
 		case 'z':
 			undo_file = optarg;
 			break;
+		case 'b':
+		case 's':
+			fprintf(stderr, "Please use 'tune2fs -O [^]64BIT' to "
+					"set / clear 64-bit feature.\n");
 		default:
 			usage(program_name);
 		}
@@ -503,10 +501,6 @@ int main (int argc, char ** argv)
 		if (sys_page_size > blocksize)
 			new_size &= ~((sys_page_size / blocksize)-1);
 	}
-	/* If changing 64bit, don't change the filesystem size. */
-	if (flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)) {
-		new_size = ext2fs_blocks_count(fs->super);
-	}
 	if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
 				       EXT4_FEATURE_INCOMPAT_64BIT)) {
 		/* Take 16T down to 2^32-1 blocks */
@@ -558,58 +552,19 @@ int main (int argc, char ** argv)
 			blocksize / 1024, new_size);
 		exit(1);
 	}
-	if ((flags & RESIZE_DISABLE_64BIT) && (flags & RESIZE_ENABLE_64BIT)) {
-		fprintf(stderr, _("Cannot set and unset 64bit feature.\n"));
-		exit(1);
-	} else if (flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)) {
-		if (new_size >= (1ULL << 32)) {
-			fprintf(stderr, _("Cannot change the 64bit feature "
-				"on a filesystem that is larger than "
-				"2^32 blocks.\n"));
-			exit(1);
-		}
-		if (mount_flags & EXT2_MF_MOUNTED) {
-			fprintf(stderr, _("Cannot change the 64bit feature "
-				"while the filesystem is mounted.\n"));
-			exit(1);
-		}
-		if (flags & RESIZE_ENABLE_64BIT &&
-		    !EXT2_HAS_INCOMPAT_FEATURE(fs->super,
-				EXT3_FEATURE_INCOMPAT_EXTENTS)) {
-			fprintf(stderr, _("Please enable the extents feature "
-				"with tune2fs before enabling the 64bit "
-				"feature.\n"));
-			exit(1);
-		}
-	} else if (new_size == ext2fs_blocks_count(fs->super)) {
+	if (new_size == ext2fs_blocks_count(fs->super)) {
 		fprintf(stderr, _("The filesystem is already %llu (%dk) "
 			"blocks long.  Nothing to do!\n\n"), new_size,
 			blocksize / 1024);
 		exit(0);
 	}
-	if ((flags & RESIZE_ENABLE_64BIT) &&
-	    EXT2_HAS_INCOMPAT_FEATURE(fs->super, EXT4_FEATURE_INCOMPAT_64BIT)) {
-		fprintf(stderr, _("The filesystem is already 64-bit.\n"));
-		exit(0);
-	}
-	if ((flags & RESIZE_DISABLE_64BIT) &&
-	    !EXT2_HAS_INCOMPAT_FEATURE(fs->super, EXT4_FEATURE_INCOMPAT_64BIT)) {
-		fprintf(stderr, _("The filesystem is already 32-bit.\n"));
-		exit(0);
-	}
 	if (mount_flags & EXT2_MF_MOUNTED) {
 		bigalloc_check(fs, force);
 		retval = online_resize_fs(fs, mtpt, &new_size, flags);
 	} else {
 		bigalloc_check(fs, force);
-		if (flags & RESIZE_ENABLE_64BIT)
-			printf(_("Converting the filesystem to 64-bit.\n"));
-		else if (flags & RESIZE_DISABLE_64BIT)
-			printf(_("Converting the filesystem to 32-bit.\n"));
-		else
-			printf(_("Resizing the filesystem on "
-				 "%s to %llu (%dk) blocks.\n"),
-			       device_name, new_size, blocksize / 1024);
+		printf(_("Resizing the filesystem on %s to %llu (%dk) blocks.\n"),
+		       device_name, new_size, blocksize / 1024);
 		retval = resize_fs(fs, &new_size, flags,
 				   ((flags & RESIZE_PERCENT_COMPLETE) ?
 				    resize_progress_func : 0));
resize/resize2fs.8.in +1 -16
index d2738e9cdf42..d9176ec7c874 100644
@@ -8,7 +8,7 @@ resize2fs \- ext2/ext3/ext4 file system resizer
 .SH SYNOPSIS
 .B resize2fs
 [
-.B \-fFpPMbs
+.B \-fFpPM
 ]
 [
 .B \-d
@@ -90,20 +90,8 @@ to shrink the size of filesystem.  Then you may use
 to shrink the size of the partition.  When shrinking the size of
 the partition, make sure you do not make it smaller than the new size
 of the ext2 filesystem!
-.PP
-The
-.B \-b
-and
-.B \-s
-options enable and disable the 64bit feature, respectively.  The resize2fs
-program will, of course, take care of resizing the block group descriptors
-and moving other data blocks out of the way, as needed.  It is not possible
-to resize the filesystem concurrent with changing the 64bit status.
 .SH OPTIONS
 .TP
-.B \-b
-Turns on the 64bit feature, resizes the group descriptors as necessary, and
-moves other metadata out of the way.
 .TP
 .B \-d \fIdebug-flags
 Turns on various resize2fs debugging features, if they have been compiled
@@ -144,9 +132,6 @@ of what the program is doing.
 .B \-P
 Print the minimum size of the filesystem and exit.
 .TP
-.B \-s
-Turns off the 64bit feature and frees blocks that are no longer in use.
-.TP
 .B \-S \fIRAID-stride
 The
 .B resize2fs
resize/resize2fs.c +3 -328

368 lines not on this page · raw message

Signature · 1 line
2.1.4
author
#31 lore raw

PATCH 15/19 resize2fs: Remove duplicit condition

In reply to Jan Kara
---
 resize/resize2fs.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
resize/resize2fs.c +1 -5
index 9a8be30bbce1..22300836300d 100644
@@ -682,19 +682,9 @@ static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size)
 	}
 
 	/*
-	 * If we are shrinking the number block groups, we're done and
+	 * If we are not increasing the number block groups, we're done and
 	 * can exit now.
 	 */
-	if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
-		retval = 0;
-		goto errout;
-	}
author
#34 lore raw

PATCH 16/19 ext2fs: Add extent dumping function to extent mapping code

In reply to Jan Kara

For testing extent mapping code, we will need the extent dumping function. So copy that function over from the resize2fs version of the code.

---
 lib/ext2fs/extent_map.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
lib/ext2fs/extent_map.c +17 -0
index 702172b24e2b..251212d118cb 100644
@@ -231,3 +231,20 @@ errcode_t ext2fs_iterate_extent(ext2_map_extent extent, __u64 *old_loc,
 	*size = ent->size;
 	return 0;
 }
+
+/*
+ * For debugging only
+ */
+void ext2fs_extent_dump(ext2_map_extent extent, FILE *out)
+{
+	__u64	i;
+	struct ext2_map_extent_entry *ent;
+
+	fputs("# Extent dump:\n", out);
+	fprintf(out, "#\tNum=%llu, Size=%llu, Cursor=%llu, Sorted=%llu\n",
+	       extent->num, extent->size, extent->cursor, extent->sorted);
+	for (i=0, ent=extent->list; i < extent->num; i++, ent++) {
+		fprintf(out, "#\t\t %llu -> %llu (%llu)\n", ent->old_loc,
+			ent->new_loc, ent->size);
+	}
+}
Signature · 1 line
2.1.4
author
#35 lore raw

PATCH 17/19 resize2fs: Remove extent mapping code

In reply to Jan Kara

Extent mapping code is now in libext2fs. Remove a special version from resize2fs.

---
 resize/Makefile.in |   8 +-
 resize/extent.c    | 241 -----------------------------------------------------
 resize/resize2fs.h |  11 ---
 3 files changed, 3 insertions(+), 257 deletions(-)
 delete mode 100644 resize/extent.c
resize/Makefile.in +3 -5
index ecd8619e55a5..82ae57adc4c5 100644
@@ -16,13 +16,11 @@ PROGS=		resize2fs
 TEST_PROGS=	test_extent
 MANPAGES=	resize2fs.8
 
-RESIZE_OBJS= extent.o resize2fs.o main.o online.o resource_track.o \
-		sim_progress.o
+RESIZE_OBJS= resize2fs.o main.o online.o resource_track.o sim_progress.o
 
-TEST_EXTENT_OBJS= extent.o test_extent.o
+TEST_EXTENT_OBJS= test_extent.o
 
-SRCS= $(srcdir)/extent.c \
-	$(srcdir)/resize2fs.c \
+SRCS= $(srcdir)/resize2fs.c \
 	$(srcdir)/main.c \
 	$(srcdir)/online.c \
 	$(srcdir)/resource_track.c \
resize/extent.c deleted +0 -241

241 lines not on this page · raw message

resize/resize2fs.h +0 -10
index 829fcd8ea8e1..c53b3bee8c8d 100644
@@ -152,17 +152,6 @@ extern errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
 extern blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags);
 
 
-/* extent.c */
-extern errcode_t ext2fs_create_extent_table(ext2_extent *ret_extent,
-					    __u64 size);
-extern void ext2fs_free_extent_table(ext2_extent extent);
-extern errcode_t ext2fs_add_extent_entry(ext2_extent extent,
-					 __u64 old_loc, __u64 new_loc);
-extern __u64 ext2fs_extent_translate(ext2_extent extent, __u64 old_loc);
-extern void ext2fs_extent_dump(ext2_extent extent, FILE *out);
-extern errcode_t ext2fs_iterate_extent(ext2_extent extent, __u64 *old_loc,
-				       __u64 *new_loc, __u64 *size);
author
#36 lore raw

PATCH 18/19 ext2fs: Move extent mapping test

In reply to Jan Kara

Move test program for excercising extent mapping code from resize2fs into libext2fs since the code is now generic.

---
 lib/ext2fs/Makefile.in       |  12 +++-
 lib/ext2fs/tst_extent_map.c  | 127 +++++++++++++++++++++++++++++++++++++++++++
 lib/ext2fs/tst_extent_map_in |  64 ++++++++++++++++++++++
 resize/Makefile.in           |  21 +------
 resize/test_extent.c         | 126 ------------------------------------------
 resize/test_extent.in        |  64 ----------------------
 6 files changed, 203 insertions(+), 211 deletions(-)
 create mode 100644 lib/ext2fs/tst_extent_map.c
 create mode 100644 lib/ext2fs/tst_extent_map_in
 delete mode 100644 resize/test_extent.c
 delete mode 100644 resize/test_extent.in
lib/ext2fs/Makefile.in +11 -1
index 02ede7bbf856..98d8d3764e05 100644
@@ -488,6 +488,14 @@ tst_extents: $(srcdir)/extent.c $(DEBUG_OBJS) $(DEPSTATIC_LIBSS) libext2fs.a \
 		$(STATIC_LIBEXT2FS) $(LIBBLKID) $(LIBUUID) \
 		$(STATIC_LIBCOM_ERR) $(SYSLIBS) -I $(top_srcdir)/debugfs
 
+tst_extent_map: $(srcdir)/extent_map.c $(DEPSTATIC_LIBSS) \
+		$(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBCOM_ERR)
+	$(E) "	LD $@"
+	$(Q) $(CC) -o tst_extent_map $(srcdir)/tst_extent_map.c \
+		$(ALL_CFLAGS) $(ALL_LDFLAGS) -DDEBUG \
+		$(STATIC_LIBSS) $(STATIC_LIBEXT2FS) \
+		$(STATIC_LIBCOM_ERR) $(SYSLIBS)
+
 tst_libext2fs: $(DEBUG_OBJS) \
 	$(DEPSTATIC_LIBSS) $(STATIC_LIBE2P) $(DEPLIBUUID) libext2fs.a \
 	$(DEPLIBBLKID) $(DEPSTATIC_LIBCOM_ERR) $(DEPLIBQUOTA)
@@ -528,7 +536,7 @@ mkjournal: mkjournal.c $(STATIC_LIBEXT2FS) $(DEPLIBCOM_ERR)
 check:: tst_bitops tst_badblocks tst_iscan tst_types tst_icount \
     tst_super_size tst_types tst_inode_size tst_csum tst_crc32c tst_bitmaps \
     tst_inline tst_inline_data tst_libext2fs tst_sha256 tst_sha512 \
-    tst_digest_encode
+    tst_digest_encode tst_extent_map
 	$(TESTENV) ./tst_bitops
 	$(TESTENV) ./tst_badblocks
 	$(TESTENV) ./tst_iscan
@@ -537,6 +545,8 @@ check:: tst_bitops tst_badblocks tst_iscan tst_types tst_icount \
 	$(TESTENV) ./tst_super_size
 	$(TESTENV) ./tst_inode_size
 	$(TESTENV) ./tst_csum
+	$(TESTENV) ./tst_extent_map <tst_extent_map_in >tst_extent_map_out
+	diff $(srcdir)/tst_extent_map_in tst_extent_map_out
 	$(TESTENV) ./tst_inline
 	$(TESTENV) ./tst_inline_data
 	$(TESTENV) ./tst_crc32c
lib/ext2fs/tst_extent_map.c new file +127 -0

127 lines not on this page · raw message

lib/ext2fs/tst_extent_map_in new file +64 -0

64 lines not on this page · raw message

resize/Makefile.in +1 -20
index 82ae57adc4c5..f67188dcfd42 100644
@@ -13,13 +13,10 @@ LDFLAG_STATIC = @LDFLAG_STATIC@
 @MCONFIG@
 
 PROGS=		resize2fs
-TEST_PROGS=	test_extent
 MANPAGES=	resize2fs.8
 
 RESIZE_OBJS= resize2fs.o main.o online.o resource_track.o sim_progress.o
 
-TEST_EXTENT_OBJS= test_extent.o
-
 SRCS= $(srcdir)/resize2fs.c \
 	$(srcdir)/main.c \
 	$(srcdir)/online.c \
@@ -54,10 +51,6 @@ resize2fs.8: $(DEP_SUBSTITUTE) $(srcdir)/resize2fs.8.in
 	$(E) "	SUBST $@"
 	$(Q) $(SUBSTITUTE_UPTIME) $(srcdir)/resize2fs.8.in resize2fs.8
 
-test_extent: $(TEST_EXTENT_OBJS)
-	$(E) "	LD $@"
-	$(Q) $(CC) $(ALL_LDFLAGS) -o test_extent $(TEST_EXTENT_OBJS) $(LIBS) 
-
 installdirs:
 	$(E) "	MKINSTALLDIRS $(root_sbindir) $(man8dir)"
 	$(Q) $(MKINSTALLDIRS) $(DESTDIR)$(root_sbindir) \
@@ -90,21 +83,9 @@ uninstall:
 		$(RM) -f $(DESTDIR)$(man8dir)/$$i; \
 	done
 
-test_extent.out: test_extent $(srcdir)/test_extent.in
-	$(TESTENV) ./test_extent < $(srcdir)/test_extent.in > test_extent.out
-
-check:: test_extent.out
-	$(Q) if cmp -s test_extent.out $(srcdir)/test_extent.in ; then \
-		echo "Test succeeded." ; \
-	else \
-		echo "Test failed!" ; \
-		diff test_extent.out $(srcdir)/test_extent.in ; \
-		exit 1 ; \
-	fi
-
 clean::
 	$(RM) -f $(PROGS) $(TEST_PROGS) $(MANPAGES) \#* *.s *.o *.a *~ core \
-		resize2fs.static test_extent.out
+		resize2fs.static
 
 mostlyclean: clean
 
resize/test_extent.c deleted +0 -126

126 lines not on this page · raw message

resize/test_extent.in deleted +0 -64

64 lines not on this page · raw message

Signature · 1 line
2.1.4
author
#37 lore raw

PATCH 19/19 resize2fs: Use libextfs2 helpers for resizing

In reply to Jan Kara
---
 resize/online.c    |   14 +-
 resize/resize2fs.c | 2307 ++++++++++++----------------------------------------
 resize/resize2fs.h |   15 +-
 3 files changed, 551 insertions(+), 1785 deletions(-)
resize/online.c +13 -1
index 46d86b0f3c8d..d202c0e92e6e 100644
@@ -197,6 +197,12 @@ errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt,
 		return retval;
 	}
 
+	retval = validate_fs_size(fs, new_size);
+	if (retval) {
+		close(fd);
+		return retval;
+	}
+
 	retval = ext2fs_dup_handle(fs, &new_fs);
 	if (retval) {
 		close(fd);
@@ -214,7 +220,13 @@ errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt,
 	 * but at least it allows on-line resizing to function.
 	 */
 	new_fs->super->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_FLEX_BG;
-	retval = adjust_fs_info(new_fs, fs, 0, *new_size);
+	retval = adjust_fs_info(new_fs, fs, *new_size);
+	if (retval) {
+		close(fd);
+		return retval;
+	}
+
+	retval = alloc_new_groups(new_fs, fs->group_desc_count);
 	if (retval) {
 		close(fd);
 		return retval;
resize/resize2fs.c +536 -1771

2,587 lines not on this page · raw message

resize/resize2fs.h +0 -6
index c53b3bee8c8d..c5d9c92c58ca 100644
@@ -107,19 +107,7 @@ typedef struct ext2_resize_struct *ext2_resize_t;
 struct ext2_resize_struct {
 	ext2_filsys	old_fs;
 	ext2_filsys	new_fs;
-	ext2fs_block_bitmap reserve_blocks;
-	ext2fs_block_bitmap move_blocks;
-	ext2_extent	bmap;
-	ext2_extent	imap;
-	blk64_t		needed_blocks;
 	int		flags;
-	char		*itable_buf;
#20 lore raw

Re: [PATCH 12/19] libext2fs: Bump default number of reserved inodes to 64

In reply to Jan Kara

May you don’t use a magic constants in code? It hard to maintain latter.

Quoted 64 lines · Jan Kara

From: Jan Kara <[email protected]>

We ran out of reserved inodes so bump the default number of reserved inodes to 64 to get some breathing space. Otherwise we have to do a full fs scan when increasing number of reserved inodes when some feature needing another reserved inode is enabled. This consumes 13.5 KB on a filesystem which is negligible these days.

---

lib/ext2fs/initialize.c | 2 +-
misc/mke2fs.8.in        | 8 ++++----
misc/mke2fs.conf.5.in   | 7 ++++---
3 files changed, 9 insertions(+), 8 deletions(-)
lib/ext2fs/initialize.c +1 -1
index 75fbf8ee0061..0ecf4606ce48 100644
@@ -186,7 +186,7 @@ errcode_t ext2fs_initialize(const char *name, int flags,
 
 	set_field(s_rev_level, EXT2_GOOD_OLD_REV);
 	if (super->s_rev_level >= EXT2_DYNAMIC_REV) {
-		set_field(s_first_ino, EXT2_GOOD_OLD_FIRST_INO);
+		set_field(s_first_ino, 64);
 		set_field(s_inode_size, EXT2_GOOD_OLD_INODE_SIZE);
 		if (super->s_inode_size >= sizeof(struct ext2_inode_large)) {
 			int extra_isize = sizeof(struct ext2_inode_large) -
misc/mke2fs.8.in +0 -0
index 520a07185f9f..6eef1d97bfbf 100644
@@ -386,10 +386,10 @@ Do not attempt to discard blocks at mkfs time.

@QUOTA_MAN_COMMENT@behavior is to initialize both user and group quotas. .TP
.BI reserved_inodes= number

misc/mke2fs.8.in +4 -4
-Specify the number of inodes reserved for system files. This number must be
-at least 10. Currently 10 is enough but future features may require additional
-reserved inodes. Reserving more inodes after file system is created requires
-full file system scan so it can take a long time.
+Specify the number of inodes reserved for system files. This number must be at
+least 10, default is 64. Currently 10 is enough but future features may require
+additional reserved inodes. Reserving more inodes after file system is created
+requires full file system scan so it can take a long time.

.RE
.TP
.BI \-f " fragment-size"

misc/mke2fs.conf.5.in +0 -0
index 06ca9e4eabc4..b35767bfedf2 100644
@@ -197,9 +197,10 @@ reserved ratio. This value can be a floating point number.

.TP
.I reserved_inodes
This relation specifies the default number of inodes reserved for system files.

misc/mke2fs.conf.5.in +4 -3
-The number must be at least 10. Currently 10 is enough but future features may
-require additional reserved inodes. Reserving more inodes after file system is
-created requires full file system scan so it can take a long time.
+The number must be at least 10, default is 64. Currently 10 is enough but
+future features may require additional reserved inodes. Reserving more inodes
+after file system is created requires full file system scan so it can take a
+long time.
Quoted 10 lines

.TP
.I undo_dir
This relation specifies the directory where the undo file should be

Signature · 6 lines
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#21 lore raw

Re: [PATCH 12/19] libext2fs: Bump default number of reserved inodes to 64

Quoted 1 line · Alexey Lyashkov

May you don’t use a magic constants in code? It hard to maintain latter.

I guess you mean the default number of reserved inodes. Yes, I guess I can create a define for that...

Honza

Quoted 75 lines
Quoted 74 lines · Jan Kara

From: Jan Kara <[email protected]>

We ran out of reserved inodes so bump the default number of reserved inodes to 64 to get some breathing space. Otherwise we have to do a full fs scan when increasing number of reserved inodes when some feature needing another reserved inode is enabled. This consumes 13.5 KB on a filesystem which is negligible these days.

---

lib/ext2fs/initialize.c | 2 +-
misc/mke2fs.8.in        | 8 ++++----
misc/mke2fs.conf.5.in   | 7 ++++---
3 files changed, 9 insertions(+), 8 deletions(-)
lib/ext2fs/initialize.c +1 -1
index 75fbf8ee0061..0ecf4606ce48 100644
@@ -186,7 +186,7 @@ errcode_t ext2fs_initialize(const char *name, int flags,
 
 	set_field(s_rev_level, EXT2_GOOD_OLD_REV);
 	if (super->s_rev_level >= EXT2_DYNAMIC_REV) {
-		set_field(s_first_ino, EXT2_GOOD_OLD_FIRST_INO);
+		set_field(s_first_ino, 64);
 		set_field(s_inode_size, EXT2_GOOD_OLD_INODE_SIZE);
 		if (super->s_inode_size >= sizeof(struct ext2_inode_large)) {
 			int extra_isize = sizeof(struct ext2_inode_large) -
misc/mke2fs.8.in +0 -0
index 520a07185f9f..6eef1d97bfbf 100644
@@ -386,10 +386,10 @@ Do not attempt to discard blocks at mkfs time.

@QUOTA_MAN_COMMENT@behavior is to initialize both user and group quotas. .TP
.BI reserved_inodes= number

misc/mke2fs.8.in +4 -4
-Specify the number of inodes reserved for system files. This number must be
-at least 10. Currently 10 is enough but future features may require additional
-reserved inodes. Reserving more inodes after file system is created requires
-full file system scan so it can take a long time.
+Specify the number of inodes reserved for system files. This number must be at
+least 10, default is 64. Currently 10 is enough but future features may require
+additional reserved inodes. Reserving more inodes after file system is created
+requires full file system scan so it can take a long time.

.RE
.TP
.BI \-f " fragment-size"

misc/mke2fs.conf.5.in +0 -0
index 06ca9e4eabc4..b35767bfedf2 100644
@@ -197,9 +197,10 @@ reserved ratio. This value can be a floating point number.

.TP
.I reserved_inodes
This relation specifies the default number of inodes reserved for system files.

misc/mke2fs.conf.5.in +4 -3
-The number must be at least 10. Currently 10 is enough but future features may
-require additional reserved inodes. Reserving more inodes after file system is
-created requires full file system scan so it can take a long time.
+The number must be at least 10, default is 64. Currently 10 is enough but
+future features may require additional reserved inodes. Reserving more inodes
+after file system is created requires full file system scan so it can take a
+long time.

.TP
.I undo_dir
This relation specifies the directory where the undo file should be

Signature · 6 lines
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Signature · 6 lines
Jan Kara <[email protected]>
SUSE Labs, CR
--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#13 lore raw

Re: [PATCH 10/19] tune2fs: Implement setting and disabling of 64-bit feature

In reply to Jan Kara
Quoted 7 lines · Jan Kara

Currently resize2fs is used to toggle 64-bit feature. However tune2fs should logically handle that as toggling any other feature. Since we have now factored out block moving code from resize2fs it is relatively easy to implement toggling of the feature within tune2fs.

For now we leave the same functionality also in resize2fs for testing. Later we can make resize2fs call tune2fs for backward compatibility.

There hasn't been a release of e2fsprogs with 64bit conversion support in resize2fs, so you can fix the conversion tests to use tune2fs and remove the -b/-B switches from resize2fs entirely.

--D

Quoted 433 lines
---
 misc/tune2fs.c | 367 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 345 insertions(+), 22 deletions(-)
misc/tune2fs.c +345 -22
index d2e8b20f64e2..d6bfad3374d1 100644
@@ -447,10 +447,327 @@ static void request_fsck_afterwards(ext2_filsys fs)
 		printf("%s", _("(and reboot afterwards!)\n"));
 }
 
-static void convert_64bit(ext2_filsys fs, int direction)
+static errcode_t fix_resize_inode(ext2_filsys fs)
 {
-	if (!direction)
-		return;
+	errcode_t retval;
+
+	if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE))
+		return 0;
+
+	retval = ext2fs_wipe_resize_inode(fs);
+	if (retval)
+		return retval;
+
+	return ext2fs_create_resize_inode(fs);
+}
+
+/*
+ * Make blocks that are needed for new group metadata unused and write there
+ * new group descriptors
+ */
+static errcode_t move_bg_metadata(ext2_filsys fs, ext2_filsys new_fs)
+{
+	dgrp_t i;
+	blk64_t b, c, d, old_desc_blocks, new_desc_blocks, j;
+	ext2fs_block_bitmap old_map = NULL, new_map = NULL;
+	int old, new;
+	errcode_t retval;
+	int cluster_ratio;
+	int need_new_block = 0;
+
+	retval = ext2fs_allocate_block_bitmap(fs, "old map", &old_map);
+	if (retval)
+		goto out;
+
+	retval = ext2fs_allocate_block_bitmap(fs, "new map", &new_map);
+	if (retval)
+		goto out;
+
+	if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
+				      EXT2_FEATURE_INCOMPAT_META_BG)) {
+		old_desc_blocks = fs->super->s_first_meta_bg;
+		new_desc_blocks = new_fs->super->s_first_meta_bg;
+	} else {
+		old_desc_blocks = fs->desc_blocks +
+				  fs->super->s_reserved_gdt_blocks;
+		new_desc_blocks = new_fs->desc_blocks +
+				  new_fs->super->s_reserved_gdt_blocks;
+	}
+
+	/* Construct bitmaps of super/descriptor blocks in old and new fs */
+	for (i = 0; i < fs->group_desc_count; i++) {
+		retval = ext2fs_super_and_bgd_loc2(fs, i, &b, &c, &d, NULL);
+		if (retval)
+			goto out;
+		if (b)
+			ext2fs_mark_block_bitmap2(old_map, b);
+		for (j = 0; c != 0 && j < old_desc_blocks; j++)
+			ext2fs_mark_block_bitmap2(old_map, c + j);
+		if (d)
+			ext2fs_mark_block_bitmap2(old_map, d);
+
+		retval = ext2fs_super_and_bgd_loc2(new_fs, i, &b, &c, &d, NULL);
+		if (retval)
+			goto out;
+		if (b)
+			ext2fs_mark_block_bitmap2(new_map, b);
+		for (j = 0; c != 0 && j < new_desc_blocks; j++)
+			ext2fs_mark_block_bitmap2(new_map, c + j);
+		if (d)
+			ext2fs_mark_block_bitmap2(new_map, d);
+	}
+
+	cluster_ratio = EXT2FS_CLUSTER_RATIO(new_fs);
+
+	/* Find changes in block allocations for bg metadata */
+	for (b = EXT2FS_B2C(fs, fs->super->s_first_data_block);
+	     b < ext2fs_blocks_count(new_fs->super);
+	     b += cluster_ratio) {
+		old = ext2fs_test_block_bitmap2(old_map, b);
+		new = ext2fs_test_block_bitmap2(new_map, b);
+
+		if (old && !new) {
+			/* mark old_map, unmark new_map */
+			if (cluster_ratio == 1)
+				ext2fs_block_alloc_stats2(new_fs, b, -1);
+		} else if (!old && new)
+			need_new_block = 1; /* unmark old_map, mark new_map */
+		else {
+			ext2fs_unmark_block_bitmap2(old_map, b);
+			ext2fs_unmark_block_bitmap2(new_map, b);
+		}
+	}
+
+	/*
+	 * new_map now shows blocks that have been newly allocated.
+	 * old_map now shows blocks that have been newly freed.
+	 */
+
+	if (need_new_block) {
+		/*
+		 * Now relocate metadata & data conflicting with new descriptor
+		 * blocks
+		 */
+		retval = ext2fs_move_blocks(new_fs, new_map, old_map);
+		if (retval)
+			goto out;
+
+		/* Now mark blocks for new group descriptors as used */
+		for (b = 0; b < ext2fs_blocks_count(new_fs->super);
+		     b += cluster_ratio) {
+			if (ext2fs_test_block_bitmap2(new_map, b) &&
+			    !ext2fs_test_block_bitmap2(new_fs->block_map, b))
+				ext2fs_block_alloc_stats2(new_fs, b, +1);
+		}
+	}
+
+	if (cluster_ratio > 1) {
+		/* Free unused clusters */
+		for (b = 0; b < ext2fs_blocks_count(new_fs->super);
+		     b += cluster_ratio)
+			if (ext2fs_test_block_bitmap2(old_map, b))
+				ext2fs_block_alloc_stats2(new_fs, b, -1);
+	}
+
+	/* Now update resize inode */
+	retval = fix_resize_inode(new_fs);
+out:
+	if (old_map)
+		ext2fs_free_block_bitmap(old_map);
+	if (new_map)
+		ext2fs_free_block_bitmap(new_map);
+	return retval;
+}
+
+
+/* Toggle 64bit mode */
+static errcode_t resize_group_descriptors(ext2_filsys *fs_p, int direction)
+{
+	void *o, *n, *new_group_desc;
+	dgrp_t i;
+	int copy_size;
+	errcode_t retval;
+	ext2_filsys new_fs = NULL;
+	ext2_filsys fs = *fs_p;
+
+	retval = ext2fs_dup_handle(fs, &new_fs);
+	if (retval)
+		return retval;
+
+	if (direction < 0) {
+		new_fs->super->s_feature_incompat &=
+					~EXT4_FEATURE_INCOMPAT_64BIT;
+		new_fs->super->s_desc_size = EXT2_MIN_DESC_SIZE;
+	} else {
+		new_fs->super->s_feature_incompat |=
+					EXT4_FEATURE_INCOMPAT_64BIT;
+		new_fs->super->s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
+	}
+
+	o = new_fs->group_desc;
+	new_fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
+			EXT2_DESC_PER_BLOCK(new_fs->super));
+	retval = ext2fs_get_arrayzero(new_fs->desc_blocks,
+				      fs->blocksize, &new_group_desc);
+	if (retval)
+		goto out;
+
+	n = new_group_desc;
+
+	if (EXT2_DESC_SIZE(fs->super) <= EXT2_DESC_SIZE(new_fs->super))
+		copy_size = EXT2_DESC_SIZE(fs->super);
+	else
+		copy_size = EXT2_DESC_SIZE(new_fs->super);
+	for (i = 0; i < fs->group_desc_count; i++) {
+		memcpy(n, o, copy_size);
+		n += EXT2_DESC_SIZE(new_fs->super);
+		o += EXT2_DESC_SIZE(fs->super);
+	}
+
+	ext2fs_free_mem(&new_fs->group_desc);
+	new_fs->group_desc = new_group_desc;
+
+	for (i = 0; i < fs->group_desc_count; i++)
+		ext2fs_group_desc_csum_set(new_fs, i);
+
+	/*
+	 * Update number of reserved blocks so that we need to alloc / free as
+	 * few blocks as possible
+	 */
+	if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
+	    (fs->desc_blocks != new_fs->desc_blocks)) {
+		int new;
+
+		new = ((int) new_fs->super->s_reserved_gdt_blocks) +
+			(fs->desc_blocks - new_fs->desc_blocks);
+		if (new < 0)
+			new = 0;
+		if (new > (int) fs->blocksize / 4)
+			new = fs->blocksize / 4;
+		new_fs->super->s_reserved_gdt_blocks = new;
+	}
+
+	retval = move_bg_metadata(fs, new_fs);
+
+	/* Return the modified filesystem */
+	new_fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
+	ext2fs_mark_super_dirty(new_fs);
+	ext2fs_free(fs);
+	*fs_p = new_fs;
+	new_fs = NULL;
+out:
+	if (new_fs)
+		ext2fs_free(new_fs);
+	return retval;
+}
+
+/* Zero out the high bits of extent fields */
+static errcode_t zero_high_bits_in_extents(ext2_filsys fs, ext2_ino_t ino,
+					   struct ext2_inode *inode)
+{
+	ext2_extent_handle_t	handle;
+	struct ext2fs_extent	extent;
+	int			op = EXT2_EXTENT_ROOT;
+	errcode_t		errcode;
+
+	if (!(inode->i_flags & EXT4_EXTENTS_FL))
+		return 0;
+
+	errcode = ext2fs_extent_open(fs, ino, &handle);
+	if (errcode)
+		return errcode;
+
+	while (1) {
+		errcode = ext2fs_extent_get(handle, op, &extent);
+		if (errcode)
+			break;
+
+		op = EXT2_EXTENT_NEXT_SIB;
+
+		if (extent.e_pblk > (1ULL << 32)) {
+			extent.e_pblk &= (1ULL << 32) - 1;
+			errcode = ext2fs_extent_replace(handle, 0, &extent);
+			if (errcode)
+				break;
+		}
+	}
+
+	/* Ok if we run off the end */
+	if (errcode == EXT2_ET_EXTENT_NO_NEXT)
+		errcode = 0;
+	ext2fs_extent_free(handle);
+	return errcode;
+}
+
+/* Zero out the high bits of inodes. */
+static errcode_t zero_high_bits_in_inodes(ext2_filsys fs)
+{
+	int length = EXT2_INODE_SIZE(fs->super);
+	struct ext2_inode *inode = NULL;
+	ext2_inode_scan	scan = NULL;
+	errcode_t	retval;
+	ext2_ino_t	ino;
+
+	if (fs->super->s_creator_os != EXT2_OS_LINUX)
+		return 0;
+
+	retval = ext2fs_open_inode_scan(fs, 0, &scan);
+	if (retval)
+		return retval;
+
+	retval = ext2fs_get_mem(length, &inode);
+	if (retval)
+		goto out;
+
+	do {
+		retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
+		if (retval)
+			goto out;
+		if (!ino)
+			break;
+		if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino))
+			continue;
+
+		/*
+		 * Here's how we deal with high block number fields:
+		 *
+		 *  - i_size_high has been been written out with i_size_lo
+		 *    since the ext2 days, so no conversion is needed.
+		 *
+		 *  - i_blocks_hi is guarded by both the huge_file feature and
+		 *    inode flags and has always been written out with
+		 *    i_blocks_lo if the feature is set.  The field is only
+		 *    ever read if both feature and inode flag are set, so
+		 *    we don't need to zero it now.
+		 *
+		 *  - i_file_acl_high can be uninitialized, so zero it if
+		 *    it isn't already.
+		 */
+		if (inode->osd2.linux2.l_i_file_acl_high) {
+			inode->osd2.linux2.l_i_file_acl_high = 0;
+			retval = ext2fs_write_inode_full(fs, ino, inode,
+							 length);
+			if (retval)
+				goto out;
+		}
+
+		retval = zero_high_bits_in_extents(fs, ino, inode);
+		if (retval)
+			goto out;
+	} while (ino);
+
+out:
+	if (inode)
+		ext2fs_free_mem(&inode);
+	if (scan)
+		ext2fs_close_inode_scan(scan);
+	return retval;
+}
+
+static errcode_t convert_64bit(ext2_filsys *fs_p, int direction)
+{
+	ext2_filsys fs = *fs_p;
+	errcode_t retval;
 
 	/*
 	 * Is resize2fs going to demand a fsck run? Might as well tell the
@@ -459,21 +776,25 @@ static void convert_64bit(ext2_filsys fs, int direction)
 	if (!fsck_requested &&
 	    ((fs->super->s_state & EXT2_ERROR_FS) ||
 	     !(fs->super->s_state & EXT2_VALID_FS) ||
-	     fs->super->s_lastcheck < fs->super->s_mtime))
+	     fs->super->s_lastcheck < fs->super->s_mtime)) {
 		request_fsck_afterwards(fs);
-	if (fsck_requested)
-		fprintf(stderr, _("After running e2fsck, please run `resize2fs %s %s"),
-			direction > 0 ? "-b" : "-s", fs->device_name);
-	else
-		fprintf(stderr, _("Please run `resize2fs %s %s"),
-			direction > 0 ? "-b" : "-s", fs->device_name);
+		return 0;
+	}
 
-	if (undo_file)
-		fprintf(stderr, _(" -z \"%s\""), undo_file);
-	if (direction > 0)
-		fprintf(stderr, _("' to enable 64-bit mode.\n"));
-	else
-		fprintf(stderr, _("' to disable 64-bit mode.\n"));
+	/*
+	 * Read bitmaps now since we are definitely going to need them and they
+	 * get properly duplicated when creating new fs handle in
+	 * resize_group_descriptors()
+	 */
+	retval = ext2fs_read_bitmaps(fs);
+	if (retval)
+		return retval;
+
+	retval = resize_group_descriptors(fs_p, direction);
+	if (retval)
+		return retval;
+
+	return zero_high_bits_in_inodes(*fs_p);
 }
 
 /* Rewrite extents */
@@ -1253,9 +1574,9 @@ mmp_error:
 	}
 
 	/*
-	 * We don't actually toggle 64bit; resize2fs does that.  But this
-	 * must come after the metadata_csum feature_on so that it won't
-	 * complain about the lack of 64bit.
+	 * Handle 64-bit conversion. We return the feature to the original
+	 * value for now until the conversion really takes place. Otherwise
+	 * some helper macros get confused.
 	 */
 	if (FEATURE_ON(E2P_FEATURE_INCOMPAT,
 		       EXT4_FEATURE_INCOMPAT_64BIT)) {
@@ -1264,8 +1585,8 @@ mmp_error:
 					  "while mounted!\n"));
 			exit(1);
 		}
-		sb->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_64BIT;
 		feature_64bit = 1;
+		sb->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_64BIT;
 	}
 	if (FEATURE_OFF(E2P_FEATURE_INCOMPAT,
 			EXT4_FEATURE_INCOMPAT_64BIT)) {
@@ -1274,8 +1595,8 @@ mmp_error:
 					  "while mounted!\n"));
 			exit(1);
 		}
-		sb->s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
 		feature_64bit = -1;
+		sb->s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
 	}
 
 	if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
@@ -3080,6 +3401,9 @@ retry_open:
 		}
 	}
 
+	if (feature_64bit)
+		convert_64bit(&fs, feature_64bit);
+
 	if (rewrite_checksums)
 		rewrite_metadata_checksums(fs);
 
@@ -3113,6 +3437,5 @@ closefs:
 		exit(1);
 	}
 
-	convert_64bit(fs, feature_64bit);
 	return (ext2fs_close_free(&fs) ? 1 : 0);
 }
Signature · 1 line
2.1.4
#17 lore raw

Re: [PATCH 11/19] mke2fs: Allow specifying number of reserved inodes

In reply to Jan Kara
Quoted 17 lines · Jan Kara

From: Jan Kara <[email protected]>

Add option to specify number of reserved inodes in the created filesystem.

---
 misc/mke2fs.8.in      |  6 ++++++
 misc/mke2fs.c         | 40 +++++++++++++++++++++++++++++++++++++++-
 misc/mke2fs.conf.5.in |  6 ++++++
 3 files changed, 51 insertions(+), 1 deletion(-)
misc/mke2fs.8.in +0 -0
index 40c40d3ed065..520a07185f9f 100644

misc/mke2fs.8.in Do not attempt to discard blocks at mkfs time. · line 392

Quoted 9 lines
misc/mke2fs.8.in +6 -0
@@ -384,6 +384,12 @@ Do not attempt to discard blocks at mkfs time.
 @[email protected] quota
 @QUOTA_MAN_COMMENT@feature is set. Without this extended option, the default
 @QUOTA_MAN_COMMENT@behavior is to initialize both user and group quotas.
+.TP
+.BI reserved_inodes= number
+Specify the number of inodes reserved for system files. This number must be
+at least 10. Currently 10 is enough but future features may require additional
+reserved inodes. Reserving more inodes after file system is created requires
+full file system scan so it can take a long time.

Perhaps:

"...requires a full file system scan, which can take a long time."

The "it" in "it can take" could be confused as referring to the process of reserving more inodes (vs. moving inodes around after creation, which is what takes a long time).

Same comment applies to the manpage at the bottom of the patch.

--D

Quoted 88 lines
misc/mke2fs.8.in +0 -0
 .RE
 .TP
 .BI \-f " fragment-size"
misc/mke2fs.c +39 -1
index 78b1252d8519..d61d1a332e67 100644
@@ -1024,6 +1024,34 @@ static void parse_extended_opts(struct ext2_super_block *param,
 				r_usage++;
 				continue;
 			}
+		} else if (!strcmp(token, "reserved_inodes")) {
+			unsigned int reserved_inodes;
+
+			if (!arg) {
+				r_usage++;
+				badopt = token;
+				continue;
+			}
+			reserved_inodes = strtoul(arg, &p, 0);
+			if (*p) {
+				fprintf(stderr,
+					_("Invalid number of reserved inodes "
+					  "%s\n"),
+					arg);
+				r_usage++;
+				continue;
+			}
+			/* Ino 0 is invalid so bump by 1... */
+			reserved_inodes++;
+			if (reserved_inodes < EXT2_GOOD_OLD_FIRST_INO) {
+				fprintf(stderr,
+					_("Too few reserved inodes "
+					  "%s (must be at least %u)\n"),
+					arg, EXT2_GOOD_OLD_FIRST_INO - 1);
+				r_usage++;
+				continue;
+			}
+			param->s_first_ino = reserved_inodes;
 		} else {
 			r_usage++;
 			badopt = token;
@@ -1049,7 +1077,8 @@ static void parse_extended_opts(struct ext2_super_block *param,
 			"\ttest_fs\n"
 			"\tdiscard\n"
 			"\tnodiscard\n"
-			"\tquotatype=<usr OR grp>\n\n"),
+			"\tquotatype=<usr OR grp>\n"
+			"\treserved_inodes=<number of reserved inodes>\n\n"),
 			badopt ? badopt : "");
 		free(buf);
 		exit(1);
@@ -2422,6 +2451,15 @@ profile_error:
 		exit(1);
 	}
 
+	/* Count with one more inode for lost+found */
+	if (fs_param.s_first_ino >= fs_param.s_inodes_count + 1) {
+		com_err(program_name, 0, _("asked for more reserved inodes than filesystem has "
+			"available (%u >= %u)\n"),
+			(unsigned int)fs_param.s_first_ino,
+			(unsigned int)fs_param.s_inodes_count + 1);
+		exit(1);
+	}
+
 	/*
 	 * Calculate number of blocks to reserve
 	 */
misc/mke2fs.conf.5.in +6 -0
index ad6c11b3cb7b..06ca9e4eabc4 100644
@@ -195,6 +195,12 @@ specify one on the command line, and the filesystem-type
 specific section of the configuration file does not specify a default
 reserved ratio. This value can be a floating point number.
 .TP
+.I reserved_inodes
+This relation specifies the default number of inodes reserved for system files.
+The number must be at least 10. Currently 10 is enough but future features may
+require additional reserved inodes. Reserving more inodes after file system is
+created requires full file system scan so it can take a long time.
+.TP
 .I undo_dir
 This relation specifies the directory where the undo file should be
 stored.  It can be overridden via the
Signature · 1 line
2.1.4
#14 lore raw

Re: [PATCH 10/19] tune2fs: Implement setting and disabling of 64-bit feature

In reply to Darrick J. Wong
Quoted 6 lines · Darrick J. Wong
Quoted 5 lines · Jan Kara

Currently resize2fs is used to toggle 64-bit feature. However tune2fs should logically handle that as toggling any other feature. Since we have now factored out block moving code from resize2fs it is relatively easy to implement toggling of the feature within tune2fs.

Quoted 6 lines
Quoted 2 lines

For now we leave the same functionality also in resize2fs for testing. Later we can make resize2fs call tune2fs for backward compatibility.

There hasn't been a release of e2fsprogs with 64bit conversion support in resize2fs, so you can fix the conversion tests to use tune2fs and remove the -b/-B switches from resize2fs entirely.

Oh, I didn't notice that you /do/ remove the resize2fs part later on in the patch set. Sorry for the noise about that.

I think there are still tests that need to be fixed up.

--D

Quoted 440 lines

--D

Quoted 433 lines
---
 misc/tune2fs.c | 367 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 345 insertions(+), 22 deletions(-)
misc/tune2fs.c +345 -22
index d2e8b20f64e2..d6bfad3374d1 100644
@@ -447,10 +447,327 @@ static void request_fsck_afterwards(ext2_filsys fs)
 		printf("%s", _("(and reboot afterwards!)\n"));
 }
 
-static void convert_64bit(ext2_filsys fs, int direction)
+static errcode_t fix_resize_inode(ext2_filsys fs)
 {
-	if (!direction)
-		return;
+	errcode_t retval;
+
+	if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE))
+		return 0;
+
+	retval = ext2fs_wipe_resize_inode(fs);
+	if (retval)
+		return retval;
+
+	return ext2fs_create_resize_inode(fs);
+}
+
+/*
+ * Make blocks that are needed for new group metadata unused and write there
+ * new group descriptors
+ */
+static errcode_t move_bg_metadata(ext2_filsys fs, ext2_filsys new_fs)
+{
+	dgrp_t i;
+	blk64_t b, c, d, old_desc_blocks, new_desc_blocks, j;
+	ext2fs_block_bitmap old_map = NULL, new_map = NULL;
+	int old, new;
+	errcode_t retval;
+	int cluster_ratio;
+	int need_new_block = 0;
+
+	retval = ext2fs_allocate_block_bitmap(fs, "old map", &old_map);
+	if (retval)
+		goto out;
+
+	retval = ext2fs_allocate_block_bitmap(fs, "new map", &new_map);
+	if (retval)
+		goto out;
+
+	if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
+				      EXT2_FEATURE_INCOMPAT_META_BG)) {
+		old_desc_blocks = fs->super->s_first_meta_bg;
+		new_desc_blocks = new_fs->super->s_first_meta_bg;
+	} else {
+		old_desc_blocks = fs->desc_blocks +
+				  fs->super->s_reserved_gdt_blocks;
+		new_desc_blocks = new_fs->desc_blocks +
+				  new_fs->super->s_reserved_gdt_blocks;
+	}
+
+	/* Construct bitmaps of super/descriptor blocks in old and new fs */
+	for (i = 0; i < fs->group_desc_count; i++) {
+		retval = ext2fs_super_and_bgd_loc2(fs, i, &b, &c, &d, NULL);
+		if (retval)
+			goto out;
+		if (b)
+			ext2fs_mark_block_bitmap2(old_map, b);
+		for (j = 0; c != 0 && j < old_desc_blocks; j++)
+			ext2fs_mark_block_bitmap2(old_map, c + j);
+		if (d)
+			ext2fs_mark_block_bitmap2(old_map, d);
+
+		retval = ext2fs_super_and_bgd_loc2(new_fs, i, &b, &c, &d, NULL);
+		if (retval)
+			goto out;
+		if (b)
+			ext2fs_mark_block_bitmap2(new_map, b);
+		for (j = 0; c != 0 && j < new_desc_blocks; j++)
+			ext2fs_mark_block_bitmap2(new_map, c + j);
+		if (d)
+			ext2fs_mark_block_bitmap2(new_map, d);
+	}
+
+	cluster_ratio = EXT2FS_CLUSTER_RATIO(new_fs);
+
+	/* Find changes in block allocations for bg metadata */
+	for (b = EXT2FS_B2C(fs, fs->super->s_first_data_block);
+	     b < ext2fs_blocks_count(new_fs->super);
+	     b += cluster_ratio) {
+		old = ext2fs_test_block_bitmap2(old_map, b);
+		new = ext2fs_test_block_bitmap2(new_map, b);
+
+		if (old && !new) {
+			/* mark old_map, unmark new_map */
+			if (cluster_ratio == 1)
+				ext2fs_block_alloc_stats2(new_fs, b, -1);
+		} else if (!old && new)
+			need_new_block = 1; /* unmark old_map, mark new_map */
+		else {
+			ext2fs_unmark_block_bitmap2(old_map, b);
+			ext2fs_unmark_block_bitmap2(new_map, b);
+		}
+	}
+
+	/*
+	 * new_map now shows blocks that have been newly allocated.
+	 * old_map now shows blocks that have been newly freed.
+	 */
+
+	if (need_new_block) {
+		/*
+		 * Now relocate metadata & data conflicting with new descriptor
+		 * blocks
+		 */
+		retval = ext2fs_move_blocks(new_fs, new_map, old_map);
+		if (retval)
+			goto out;
+
+		/* Now mark blocks for new group descriptors as used */
+		for (b = 0; b < ext2fs_blocks_count(new_fs->super);
+		     b += cluster_ratio) {
+			if (ext2fs_test_block_bitmap2(new_map, b) &&
+			    !ext2fs_test_block_bitmap2(new_fs->block_map, b))
+				ext2fs_block_alloc_stats2(new_fs, b, +1);
+		}
+	}
+
+	if (cluster_ratio > 1) {
+		/* Free unused clusters */
+		for (b = 0; b < ext2fs_blocks_count(new_fs->super);
+		     b += cluster_ratio)
+			if (ext2fs_test_block_bitmap2(old_map, b))
+				ext2fs_block_alloc_stats2(new_fs, b, -1);
+	}
+
+	/* Now update resize inode */
+	retval = fix_resize_inode(new_fs);
+out:
+	if (old_map)
+		ext2fs_free_block_bitmap(old_map);
+	if (new_map)
+		ext2fs_free_block_bitmap(new_map);
+	return retval;
+}
+
+
+/* Toggle 64bit mode */
+static errcode_t resize_group_descriptors(ext2_filsys *fs_p, int direction)
+{
+	void *o, *n, *new_group_desc;
+	dgrp_t i;
+	int copy_size;
+	errcode_t retval;
+	ext2_filsys new_fs = NULL;
+	ext2_filsys fs = *fs_p;
+
+	retval = ext2fs_dup_handle(fs, &new_fs);
+	if (retval)
+		return retval;
+
+	if (direction < 0) {
+		new_fs->super->s_feature_incompat &=
+					~EXT4_FEATURE_INCOMPAT_64BIT;
+		new_fs->super->s_desc_size = EXT2_MIN_DESC_SIZE;
+	} else {
+		new_fs->super->s_feature_incompat |=
+					EXT4_FEATURE_INCOMPAT_64BIT;
+		new_fs->super->s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
+	}
+
+	o = new_fs->group_desc;
+	new_fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
+			EXT2_DESC_PER_BLOCK(new_fs->super));
+	retval = ext2fs_get_arrayzero(new_fs->desc_blocks,
+				      fs->blocksize, &new_group_desc);
+	if (retval)
+		goto out;
+
+	n = new_group_desc;
+
+	if (EXT2_DESC_SIZE(fs->super) <= EXT2_DESC_SIZE(new_fs->super))
+		copy_size = EXT2_DESC_SIZE(fs->super);
+	else
+		copy_size = EXT2_DESC_SIZE(new_fs->super);
+	for (i = 0; i < fs->group_desc_count; i++) {
+		memcpy(n, o, copy_size);
+		n += EXT2_DESC_SIZE(new_fs->super);
+		o += EXT2_DESC_SIZE(fs->super);
+	}
+
+	ext2fs_free_mem(&new_fs->group_desc);
+	new_fs->group_desc = new_group_desc;
+
+	for (i = 0; i < fs->group_desc_count; i++)
+		ext2fs_group_desc_csum_set(new_fs, i);
+
+	/*
+	 * Update number of reserved blocks so that we need to alloc / free as
+	 * few blocks as possible
+	 */
+	if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
+	    (fs->desc_blocks != new_fs->desc_blocks)) {
+		int new;
+
+		new = ((int) new_fs->super->s_reserved_gdt_blocks) +
+			(fs->desc_blocks - new_fs->desc_blocks);
+		if (new < 0)
+			new = 0;
+		if (new > (int) fs->blocksize / 4)
+			new = fs->blocksize / 4;
+		new_fs->super->s_reserved_gdt_blocks = new;
+	}
+
+	retval = move_bg_metadata(fs, new_fs);
+
+	/* Return the modified filesystem */
+	new_fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
+	ext2fs_mark_super_dirty(new_fs);
+	ext2fs_free(fs);
+	*fs_p = new_fs;
+	new_fs = NULL;
+out:
+	if (new_fs)
+		ext2fs_free(new_fs);
+	return retval;
+}
+
+/* Zero out the high bits of extent fields */
+static errcode_t zero_high_bits_in_extents(ext2_filsys fs, ext2_ino_t ino,
+					   struct ext2_inode *inode)
+{
+	ext2_extent_handle_t	handle;
+	struct ext2fs_extent	extent;
+	int			op = EXT2_EXTENT_ROOT;
+	errcode_t		errcode;
+
+	if (!(inode->i_flags & EXT4_EXTENTS_FL))
+		return 0;
+
+	errcode = ext2fs_extent_open(fs, ino, &handle);
+	if (errcode)
+		return errcode;
+
+	while (1) {
+		errcode = ext2fs_extent_get(handle, op, &extent);
+		if (errcode)
+			break;
+
+		op = EXT2_EXTENT_NEXT_SIB;
+
+		if (extent.e_pblk > (1ULL << 32)) {
+			extent.e_pblk &= (1ULL << 32) - 1;
+			errcode = ext2fs_extent_replace(handle, 0, &extent);
+			if (errcode)
+				break;
+		}
+	}
+
+	/* Ok if we run off the end */
+	if (errcode == EXT2_ET_EXTENT_NO_NEXT)
+		errcode = 0;
+	ext2fs_extent_free(handle);
+	return errcode;
+}
+
+/* Zero out the high bits of inodes. */
+static errcode_t zero_high_bits_in_inodes(ext2_filsys fs)
+{
+	int length = EXT2_INODE_SIZE(fs->super);
+	struct ext2_inode *inode = NULL;
+	ext2_inode_scan	scan = NULL;
+	errcode_t	retval;
+	ext2_ino_t	ino;
+
+	if (fs->super->s_creator_os != EXT2_OS_LINUX)
+		return 0;
+
+	retval = ext2fs_open_inode_scan(fs, 0, &scan);
+	if (retval)
+		return retval;
+
+	retval = ext2fs_get_mem(length, &inode);
+	if (retval)
+		goto out;
+
+	do {
+		retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
+		if (retval)
+			goto out;
+		if (!ino)
+			break;
+		if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino))
+			continue;
+
+		/*
+		 * Here's how we deal with high block number fields:
+		 *
+		 *  - i_size_high has been been written out with i_size_lo
+		 *    since the ext2 days, so no conversion is needed.
+		 *
+		 *  - i_blocks_hi is guarded by both the huge_file feature and
+		 *    inode flags and has always been written out with
+		 *    i_blocks_lo if the feature is set.  The field is only
+		 *    ever read if both feature and inode flag are set, so
+		 *    we don't need to zero it now.
+		 *
+		 *  - i_file_acl_high can be uninitialized, so zero it if
+		 *    it isn't already.
+		 */
+		if (inode->osd2.linux2.l_i_file_acl_high) {
+			inode->osd2.linux2.l_i_file_acl_high = 0;
+			retval = ext2fs_write_inode_full(fs, ino, inode,
+							 length);
+			if (retval)
+				goto out;
+		}
+
+		retval = zero_high_bits_in_extents(fs, ino, inode);
+		if (retval)
+			goto out;
+	} while (ino);
+
+out:
+	if (inode)
+		ext2fs_free_mem(&inode);
+	if (scan)
+		ext2fs_close_inode_scan(scan);
+	return retval;
+}
+
+static errcode_t convert_64bit(ext2_filsys *fs_p, int direction)
+{
+	ext2_filsys fs = *fs_p;
+	errcode_t retval;
 
 	/*
 	 * Is resize2fs going to demand a fsck run? Might as well tell the
@@ -459,21 +776,25 @@ static void convert_64bit(ext2_filsys fs, int direction)
 	if (!fsck_requested &&
 	    ((fs->super->s_state & EXT2_ERROR_FS) ||
 	     !(fs->super->s_state & EXT2_VALID_FS) ||
-	     fs->super->s_lastcheck < fs->super->s_mtime))
+	     fs->super->s_lastcheck < fs->super->s_mtime)) {
 		request_fsck_afterwards(fs);
-	if (fsck_requested)
-		fprintf(stderr, _("After running e2fsck, please run `resize2fs %s %s"),
-			direction > 0 ? "-b" : "-s", fs->device_name);
-	else
-		fprintf(stderr, _("Please run `resize2fs %s %s"),
-			direction > 0 ? "-b" : "-s", fs->device_name);
+		return 0;
+	}
 
-	if (undo_file)
-		fprintf(stderr, _(" -z \"%s\""), undo_file);
-	if (direction > 0)
-		fprintf(stderr, _("' to enable 64-bit mode.\n"));
-	else
-		fprintf(stderr, _("' to disable 64-bit mode.\n"));
+	/*
+	 * Read bitmaps now since we are definitely going to need them and they
+	 * get properly duplicated when creating new fs handle in
+	 * resize_group_descriptors()
+	 */
+	retval = ext2fs_read_bitmaps(fs);
+	if (retval)
+		return retval;
+
+	retval = resize_group_descriptors(fs_p, direction);
+	if (retval)
+		return retval;
+
+	return zero_high_bits_in_inodes(*fs_p);
 }
 
 /* Rewrite extents */
@@ -1253,9 +1574,9 @@ mmp_error:
 	}
 
 	/*
-	 * We don't actually toggle 64bit; resize2fs does that.  But this
-	 * must come after the metadata_csum feature_on so that it won't
-	 * complain about the lack of 64bit.
+	 * Handle 64-bit conversion. We return the feature to the original
+	 * value for now until the conversion really takes place. Otherwise
+	 * some helper macros get confused.
 	 */
 	if (FEATURE_ON(E2P_FEATURE_INCOMPAT,
 		       EXT4_FEATURE_INCOMPAT_64BIT)) {
@@ -1264,8 +1585,8 @@ mmp_error:
 					  "while mounted!\n"));
 			exit(1);
 		}
-		sb->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_64BIT;
 		feature_64bit = 1;
+		sb->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_64BIT;
 	}
 	if (FEATURE_OFF(E2P_FEATURE_INCOMPAT,
 			EXT4_FEATURE_INCOMPAT_64BIT)) {
@@ -1274,8 +1595,8 @@ mmp_error:
 					  "while mounted!\n"));
 			exit(1);
 		}
-		sb->s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
 		feature_64bit = -1;
+		sb->s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
 	}
 
 	if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
@@ -3080,6 +3401,9 @@ retry_open:
 		}
 	}
 
+	if (feature_64bit)
+		convert_64bit(&fs, feature_64bit);
+
 	if (rewrite_checksums)
 		rewrite_metadata_checksums(fs);
 
@@ -3113,6 +3437,5 @@ closefs:
 		exit(1);
 	}
 
-	convert_64bit(fs, feature_64bit);
 	return (ext2fs_close_free(&fs) ? 1 : 0);
 }
Signature · 1 line
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#9 lore raw

Re: [PATCH 08/19] ext2fs: Implement block moving in libext2fs

In reply to Jan Kara
Quoted 3 lines · Jan Kara

From: Jan Kara <[email protected]>

<snip>

Quoted 7 lines
lib/ext2fs/extent_map.c new file +1 -0
new file mode 100644
index 000000000000..702172b24e2b
@@ -0,0 +1,233 @@
+/*

lib/ext2fs/extent_map.c line 11

Quoted 10 lines
lib/ext2fs/extent_map.c new file +10 -0
+ * extent.c --- ext2 extent mapping abstraction
+ *
+ * This abstraction is used to provide a compact way of representing a
+ * translation table, for moving multiple contiguous ranges (extents)
+ * of blocks or inodes.
+ *
+ * Copyright (C) 1997, 1998 by Theodore Ts'o and
+ * 	PowerQuest, Inc.
+ *
+ * Copyright (C) 1999, 2000 by Theosore Ts'o

Ted should probably weigh in on this amazing coincidence in contributors :) but this I suspect is a spelling error waiting a long time to be fixed.

Quoted 2 lines
lib/ext2fs/extent_map.c new file +2 -0
+ *
+ * %Begin-Header%

<snip>

Quoted 19 lines
lib/ext2fs/move.h new file +13 -0
new file mode 100644
index 000000000000..8d66aa039ec0
@@ -0,0 +1,23 @@
+#ifndef _EXT2FS_MOVE_H
+#define _EXT2FS_MOVE_H
+
+#include "ext2fs.h"
+
+typedef struct _ext2_map_extent *ext2_map_extent;
+
+/* extent_map.c */
+extern int ext2fs_extent_table_empty(ext2_map_extent extent);
+extern errcode_t ext2fs_create_extent_table(ext2_map_extent *ret_extent,
+					    __u64 size);
+extern void ext2fs_free_extent_table(ext2_map_extent extent);
+extern errcode_t ext2fs_add_extent_entry(ext2_map_extent extent,

lib/ext2fs/move.h line 23

Quoted 10 lines
lib/ext2fs/move.h new file +10 -0
+					 __u64 old_loc, __u64 new_loc);
+extern __u64 ext2fs_extent_translate(ext2_map_extent extent, __u64 old_loc);
+extern errcode_t ext2fs_iterate_extent(ext2_map_extent extent, __u64 *old_loc,
+				       __u64 *new_loc, __u64 *size);
+
+/* move.c */
+errcode_t ext2fs_move_blocks(ext2_filsys fs, ext2fs_block_bitmap move_map,
+			     ext2fs_block_bitmap reuse_map);
+
+#endif

Hmm... are these functions intended for use by client programs? I'm accustomed to looking in ext2fs.h for such functions.

--D

Quoted 7 lines
Signature · 6 lines
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#22 lore raw

Re: [PATCH 12/19] libext2fs: Bump default number of reserved inodes to 64

In reply to Jan Kara

It’s correct - i mean about it.
Same define may used on ext4 code to verify inode number correctness.

Quoted 80 lines · Jan Kara
Quoted 1 line · Alexey Lyashkov

May you don’t use a magic constants in code? It hard to maintain latter.

I guess you mean the default number of reserved inodes. Yes, I guess I can create a define for that...

Honza

Quoted 70 lines
Quoted 70 lines · Jan Kara

From: Jan Kara <[email protected]>

We ran out of reserved inodes so bump the default number of reserved inodes to 64 to get some breathing space. Otherwise we have to do a full fs scan when increasing number of reserved inodes when some feature needing another reserved inode is enabled. This consumes 13.5 KB on a filesystem which is negligible these days.

---

lib/ext2fs/initialize.c | 2 +-
misc/mke2fs.8.in        | 8 ++++----
misc/mke2fs.conf.5.in   | 7 ++++---
3 files changed, 9 insertions(+), 8 deletions(-)
lib/ext2fs/initialize.c +1 -1
index 75fbf8ee0061..0ecf4606ce48 100644
@@ -186,7 +186,7 @@ errcode_t ext2fs_initialize(const char *name, int flags,
 
 	set_field(s_rev_level, EXT2_GOOD_OLD_REV);
 	if (super->s_rev_level >= EXT2_DYNAMIC_REV) {
-		set_field(s_first_ino, EXT2_GOOD_OLD_FIRST_INO);
+		set_field(s_first_ino, 64);
 		set_field(s_inode_size, EXT2_GOOD_OLD_INODE_SIZE);
 		if (super->s_inode_size >= sizeof(struct ext2_inode_large)) {
 			int extra_isize = sizeof(struct ext2_inode_large) -
misc/mke2fs.8.in +0 -0
index 520a07185f9f..6eef1d97bfbf 100644
@@ -386,10 +386,10 @@ Do not attempt to discard blocks at mkfs time.

@QUOTA_MAN_COMMENT@behavior is to initialize both user and group quotas. .TP
.BI reserved_inodes= number

misc/mke2fs.8.in +4 -4
-Specify the number of inodes reserved for system files. This number must be
-at least 10. Currently 10 is enough but future features may require additional
-reserved inodes. Reserving more inodes after file system is created requires
-full file system scan so it can take a long time.
+Specify the number of inodes reserved for system files. This number must be at
+least 10, default is 64. Currently 10 is enough but future features may require
+additional reserved inodes. Reserving more inodes after file system is created
+requires full file system scan so it can take a long time.

.RE
.TP
.BI \-f " fragment-size"

misc/mke2fs.conf.5.in +0 -0
index 06ca9e4eabc4..b35767bfedf2 100644
@@ -197,9 +197,10 @@ reserved ratio. This value can be a floating point number.

.TP
.I reserved_inodes
This relation specifies the default number of inodes reserved for system files.

misc/mke2fs.conf.5.in +4 -3
-The number must be at least 10. Currently 10 is enough but future features may
-require additional reserved inodes. Reserving more inodes after file system is
-created requires full file system scan so it can take a long time.
+The number must be at least 10, default is 64. Currently 10 is enough but
+future features may require additional reserved inodes. Reserving more inodes
+after file system is created requires full file system scan so it can take a
+long time.

.TP
.I undo_dir
This relation specifies the directory where the undo file should be

Signature · 1 line
2.1.4
Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#26 lore raw

Re: [PATCH 13/19] tune2fs: Add support for changing number of reserved inodes

In reply to Jan Kara
Quoted 57 lines · Jan Kara

---
misc/tune2fs.8.in | 5 +++
misc/tune2fs.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++

2 files changed, 107 insertions(+)
misc/tune2fs.8.in +0 -0
index 4373fc49b255..922705329112 100644
@@ -236,6 +236,11 @@ program.
 This superblock setting is only honored in 2.6.35+ kernels;

and not at all by the ext2 and ext3 file system drivers. .TP
+.BI reserved_inodes= number_of_reserved_inodes

misc/tune2fs.8.in +4 -0
+Set the number of inodes reserved for system files. This number must be
+at least 10. Currently 10 is enough but future features may require additional
+reserved inodes. Reserving more inodes requires full file system scan so it
+can take a long time.

.B test_fs
Set a flag in the filesystem superblock indicating that it may be mounted using experimental kernel code, such as the ext4dev filesystem.

misc/tune2fs.c +19 -0
index d6bfad3374d1..d6e75fe00814 100644
@@ -92,6 +92,7 @@ static char *features_cmd;
 static char *mntopts_cmd;
 static int stride, stripe_width;
 static int stride_set, stripe_width_set;
+static unsigned long new_first_ino;
 static char *extended_cmd;
 static unsigned long new_inode_size;
 static char *ext_mount_opts;
@@ -2298,6 +2299,34 @@ static int parse_extended_opts(ext2_filsys fs, const char *opts)
 				continue;
 			}
 			ext_mount_opts = strdup(arg);
+		} else if (!strcmp(token, "reserved_inodes")) {
+			if (!arg) {
+				r_usage++;
+				continue;
+			}
+			new_first_ino = strtoul(arg, &p, 0);
+			if (*p) {
+				fprintf(stderr,
+					_("Invalid number of reserved inodes "
+					  "%s\n"),
+					arg);
+				r_usage++;
+				continue;
+			}
+			/* Ino 0 is invalid so bump by 1... */
+			new_first_ino++;
+			if (new_first_ino < EXT2_GOOD_OLD_FIRST_INO) {
+				fprintf(stderr,

misc/tune2fs.c static int parse_extended_opts(ext2_filsys fs, const char *opts) · line 2329

Quoted 10 lines
misc/tune2fs.c +10 -0
+					_("Too few reserved inodes "
+				          "%s (must be at least %u)\n"),
+				        arg, EXT2_GOOD_OLD_FIRST_INO - 1);
+				r_usage++;
+				continue;
+			}
+			/*
+			 * Here should go further feature tests to disallow
+			 * admin to free used system inode
+			 */

Not only should this check that the new_first_ino isn't shrinking too small to cover in-use special inodes, it should also verify that it isn't growing to cover in-use normal inodes (e.g. lost+found).

I guess one option might be to reserve "12" for the lost+found inode, since virtually all filesystems have it this way (though not all), and it would also help e2fsck in the future if it didn't need to allocate some other inode for lost+found in case of corruption.

Cheers, Andreas

Quoted 104 lines
misc/tune2fs.c +73 -0
 		} else
 			r_usage++;
 	}
@@ -2312,6 +2341,7 @@ static int parse_extended_opts(ext2_filsys fs, const char *opts)
 			"\tmount_opts=<extended default mount options>\n"
 			"\tstride=<RAID per-disk chunk size in blocks>\n"
 			"\tstripe_width=<RAID stride*data disks in blocks>\n"
+			"\treserved_inodes=<number of reserved inodes>\n"
 			"\ttest_fs\n"
 			"\t^test_fs\n"));
 		free(buf);
@@ -2986,6 +3016,75 @@ fs_update_journal_user(struct ext2_super_block *sb, __u8 old_uuid[UUID_SIZE])
 	return 0;
 }
 
+/* Zero range of inodes and mark them as free / used */
+static errcode_t zero_inodes_range(ext2_filsys fs, ext2_ino_t start,
+				   ext2_ino_t end, int inuse)
+{
+	char *inode;
+	int length = EXT2_INODE_SIZE(fs->super);
+	ext2_ino_t ino;
+	errcode_t retval;
+
+	retval = ext2fs_get_memzero(length, &inode);
+	if (retval)
+		return retval;
+
+	for (ino = start; ino <= end; ino++) {
+		ext2fs_inode_alloc_stats(fs, ino, inuse);
+		retval = ext2fs_write_inode_full(fs, ino,
+						 (struct ext2_inode *)inode,
+						 length);
+		if (retval)
+			break;
+	}
+	ext2fs_free_mem(inode);
+
+	return retval;
+}
+
+static errcode_t update_reserved_inodes(ext2_filsys fs)
+{
+	errcode_t retval = 0;
+	ext2fs_inode_bitmap imap;
+	ext2_ino_t ino, first_ino = fs->super->s_first_ino;
+
+	if (new_first_ino == first_ino)
+		return 0;
+
+	/* Group descriptors will need writing as well */
+	fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
+
+	/* Freeing reserved inodes is easy */
+	if (new_first_ino < first_ino) {
+		retval = zero_inodes_range(fs, new_first_ino, first_ino - 1,
+					   -1);
+		if (retval)
+			return retval;
+		goto out;
+	}
+
+	retval = ext2fs_allocate_inode_bitmap(fs, "inodes to move", &imap);
+	if (retval)
+		return retval;
+
+	for (ino = fs->super->s_first_ino; ino < new_first_ino; ino++)
+		ext2fs_mark_inode_bitmap2(imap, ino);
+
+	retval = ext2fs_move_inodes(fs, imap);
+	ext2fs_free_inode_bitmap(imap);
+	if (retval)
+		return retval;
+
+	retval = zero_inodes_range(fs, first_ino, new_first_ino - 1, +1);
+	if (retval)
+		return retval;
+out:
+	fs->super->s_first_ino = new_first_ino;
+	ext2fs_mark_super_dirty(fs);
+
+	return 0;
+}
+
 int main(int argc, char **argv)
 {
 	errcode_t retval;
@@ -3404,6 +3503,9 @@ retry_open:
 	if (feature_64bit)
 		convert_64bit(&fs, feature_64bit);
 
+	if (new_first_ino)
+		update_reserved_inodes(fs);
+
 	if (rewrite_checksums)
 		rewrite_metadata_checksums(fs);
 
Signature · 6 lines
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Cheers, Andreas

#32 lore raw

Re: [PATCH 15/19] resize2fs: Remove duplicit condition

In reply to Jan Kara
Quoted 1 line · Jan Kara

Subject: [PATCH 15/19] resize2fs: Remove duplicit condition

Minor typo - s/duplicit/duplicate/

Quoted 37 lines

---
resize/resize2fs.c | 12 +-----------

1 file changed, 1 insertion(+), 11 deletions(-)
resize/resize2fs.c +1 -11
index 9a8be30bbce1..22300836300d 100644
@@ -682,19 +682,9 @@ static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size)
 	}
 
 	/*
-	 * If we are shrinking the number block groups, we're done and
+	 * If we are not increasing the number block groups, we're done and
 	 * can exit now.
 	 */
-	if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
-		retval = 0;
-		goto errout;
-	}
-
-	/*
-	 * If the number of block groups is staying the same, we're
-	 * done and can exit now.  (If the number block groups is
-	 * shrinking, we had exited earlier.)
-	 */
 	if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
 		retval = 0;
 		goto errout;
Signature · 6 lines
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Cheers, Andreas

#23 lore raw

Re: [PATCH 12/19] libext2fs: Bump default number of reserved inodes to 64

In reply to Jan Kara
Quoted 8 lines · Jan Kara

From: Jan Kara <[email protected]>

We ran out of reserved inodes so bump the default number of reserved inodes to 64 to get some breathing space. Otherwise we have to do a full fs scan when increasing number of reserved inodes when some feature needing another reserved inode is enabled. This consumes 13.5 KB on a filesystem which is negligible these days.

This patch also needs to fix all of the regression tests that will fail because the inode numbers are different. Otherwise "make check" will fail.

Cheers, Andreas

Quoted 28 lines

---

lib/ext2fs/initialize.c | 2 +-
misc/mke2fs.8.in        | 8 ++++----
misc/mke2fs.conf.5.in   | 7 ++++---
3 files changed, 9 insertions(+), 8 deletions(-)
lib/ext2fs/initialize.c +1 -1
index 75fbf8ee0061..0ecf4606ce48 100644
@@ -186,7 +186,7 @@ errcode_t ext2fs_initialize(const char *name, int flags,
 
 	set_field(s_rev_level, EXT2_GOOD_OLD_REV);
 	if (super->s_rev_level >= EXT2_DYNAMIC_REV) {
-		set_field(s_first_ino, EXT2_GOOD_OLD_FIRST_INO);
+		set_field(s_first_ino, 64);
 		set_field(s_inode_size, EXT2_GOOD_OLD_INODE_SIZE);
 		if (super->s_inode_size >= sizeof(struct ext2_inode_large)) {
 			int extra_isize = sizeof(struct ext2_inode_large) -
misc/mke2fs.8.in +0 -0
index 520a07185f9f..6eef1d97bfbf 100644
@@ -386,10 +386,10 @@ Do not attempt to discard blocks at mkfs time.

@QUOTA_MAN_COMMENT@behavior is to initialize both user and group quotas. .TP
.BI reserved_inodes= number

misc/mke2fs.8.in Do not attempt to discard blocks at mkfs time.

Quoted 7 lines
misc/mke2fs.8.in +3 -4
-Specify the number of inodes reserved for system files. This number must be
-at least 10. Currently 10 is enough but future features may require additional
-reserved inodes. Reserving more inodes after file system is created requires
-full file system scan so it can take a long time.
+Specify the number of inodes reserved for system files. This number must be at
+least 10, default is 64. Currently 10 is enough but future features may require
+additional reserved inodes.

I'm not sure it is worthwhile to mention "currently 10 is enough", since this should be updated every time some new inode is reserved, but it will likely be forgotten. I don't think users really care about this in the end.

Quoted 20 lines

Reserving more inodes after file system is created +requires full file system scan so it can take a long time. .RE
.TP
.BI \-f " fragment-size"

misc/mke2fs.conf.5.in +0 -0
index 06ca9e4eabc4..b35767bfedf2 100644
@@ -197,9 +197,10 @@ reserved ratio. This value can be a floating point number.

.TP
.I reserved_inodes
This relation specifies the default number of inodes reserved for system files.

misc/mke2fs.conf.5.in +4 -3
-The number must be at least 10. Currently 10 is enough but future features may
-require additional reserved inodes. Reserving more inodes after file system is
-created requires full file system scan so it can take a long time.
+The number must be at least 10, default is 64. Currently 10 is enough but
+future features may require additional reserved inodes. Reserving more inodes
+after file system is created requires full file system scan so it can take a
+long time.
Quoted 10 lines

.TP
.I undo_dir
This relation specifies the directory where the undo file should be

Signature · 6 lines
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Cheers, Andreas

#28 lore raw

Re: [PATCH 13/19] tune2fs: Add support for changing number of reserved inodes

In reply to Jan Kara
Quoted 7 lines · Jan Kara

---

misc/tune2fs.c static int parse_extended_opts(ext2_filsys fs, const char *opts) · line 2315

Quoted 9 lines
misc/tune2fs.c +9 -0
+			new_first_ino = strtoul(arg, &p, 0);
+			if (*p) {
+				fprintf(stderr,
+					_("Invalid number of reserved inodes "
+					  "%s\n"),
+					arg);
+				r_usage++;
+				continue;
+			}

this check invalid - you need check an errno for ERANGE also. add check against maximal number inodes on FS is also good.

#18 lore raw

Re: [PATCH 11/19] mke2fs: Allow specifying number of reserved inodes

In reply to Darrick J. Wong
Quoted 30 lines · Darrick J. Wong
Quoted 17 lines · Jan Kara

From: Jan Kara <[email protected]>

Add option to specify number of reserved inodes in the created filesystem.

---
 misc/mke2fs.8.in      |  6 ++++++
 misc/mke2fs.c         | 40 +++++++++++++++++++++++++++++++++++++++-
 misc/mke2fs.conf.5.in |  6 ++++++
 3 files changed, 51 insertions(+), 1 deletion(-)
misc/mke2fs.8.in +0 -0
index 40c40d3ed065..520a07185f9f 100644

misc/mke2fs.8.in Do not attempt to discard blocks at mkfs time. · line 392

Quoted 9 lines
misc/mke2fs.8.in +6 -0
@@ -384,6 +384,12 @@ Do not attempt to discard blocks at mkfs time.
 @[email protected] quota
 @QUOTA_MAN_COMMENT@feature is set. Without this extended option, the default
 @QUOTA_MAN_COMMENT@behavior is to initialize both user and group quotas.
+.TP
+.BI reserved_inodes= number
+Specify the number of inodes reserved for system files. This number must be
+at least 10. Currently 10 is enough but future features may require additional
+reserved inodes. Reserving more inodes after file system is created requires
+full file system scan so it can take a long time.

Perhaps:

Quoted 7 lines

"...requires a full file system scan, which can take a long time."

The "it" in "it can take" could be confused as referring to the process of reserving more inodes (vs. moving inodes around after creation, which is what takes a long time).

Same comment applies to the manpage at the bottom of the patch.

OK, wording changed to what you suggested. Thanks!

Honza

Quoted 88 lines
Quoted 88 lines
misc/mke2fs.8.in +0 -0
 .RE
 .TP
 .BI \-f " fragment-size"
misc/mke2fs.c +39 -1
index 78b1252d8519..d61d1a332e67 100644
@@ -1024,6 +1024,34 @@ static void parse_extended_opts(struct ext2_super_block *param,
 				r_usage++;
 				continue;
 			}
+		} else if (!strcmp(token, "reserved_inodes")) {
+			unsigned int reserved_inodes;
+
+			if (!arg) {
+				r_usage++;
+				badopt = token;
+				continue;
+			}
+			reserved_inodes = strtoul(arg, &p, 0);
+			if (*p) {
+				fprintf(stderr,
+					_("Invalid number of reserved inodes "
+					  "%s\n"),
+					arg);
+				r_usage++;
+				continue;
+			}
+			/* Ino 0 is invalid so bump by 1... */
+			reserved_inodes++;
+			if (reserved_inodes < EXT2_GOOD_OLD_FIRST_INO) {
+				fprintf(stderr,
+					_("Too few reserved inodes "
+					  "%s (must be at least %u)\n"),
+					arg, EXT2_GOOD_OLD_FIRST_INO - 1);
+				r_usage++;
+				continue;
+			}
+			param->s_first_ino = reserved_inodes;
 		} else {
 			r_usage++;
 			badopt = token;
@@ -1049,7 +1077,8 @@ static void parse_extended_opts(struct ext2_super_block *param,
 			"\ttest_fs\n"
 			"\tdiscard\n"
 			"\tnodiscard\n"
-			"\tquotatype=<usr OR grp>\n\n"),
+			"\tquotatype=<usr OR grp>\n"
+			"\treserved_inodes=<number of reserved inodes>\n\n"),
 			badopt ? badopt : "");
 		free(buf);
 		exit(1);
@@ -2422,6 +2451,15 @@ profile_error:
 		exit(1);
 	}
 
+	/* Count with one more inode for lost+found */
+	if (fs_param.s_first_ino >= fs_param.s_inodes_count + 1) {
+		com_err(program_name, 0, _("asked for more reserved inodes than filesystem has "
+			"available (%u >= %u)\n"),
+			(unsigned int)fs_param.s_first_ino,
+			(unsigned int)fs_param.s_inodes_count + 1);
+		exit(1);
+	}
+
 	/*
 	 * Calculate number of blocks to reserve
 	 */
misc/mke2fs.conf.5.in +6 -0
index ad6c11b3cb7b..06ca9e4eabc4 100644
@@ -195,6 +195,12 @@ specify one on the command line, and the filesystem-type
 specific section of the configuration file does not specify a default
 reserved ratio. This value can be a floating point number.
 .TP
+.I reserved_inodes
+This relation specifies the default number of inodes reserved for system files.
+The number must be at least 10. Currently 10 is enough but future features may
+require additional reserved inodes. Reserving more inodes after file system is
+created requires full file system scan so it can take a long time.
+.TP
 .I undo_dir
 This relation specifies the directory where the undo file should be
 stored.  It can be overridden via the
Signature · 1 line
2.1.4
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#15 lore raw

Re: [PATCH 10/19] tune2fs: Implement setting and disabling of 64-bit feature

In reply to Darrick J. Wong
Quoted 10 lines · Darrick J. Wong
Quoted 9 lines · Darrick J. Wong
Quoted 7 lines · Jan Kara

Currently resize2fs is used to toggle 64-bit feature. However tune2fs should logically handle that as toggling any other feature. Since we have now factored out block moving code from resize2fs it is relatively easy to implement toggling of the feature within tune2fs.

For now we leave the same functionality also in resize2fs for testing. Later we can make resize2fs call tune2fs for backward compatibility.

Quoted 8 lines
Quoted 3 lines

There hasn't been a release of e2fsprogs with 64bit conversion support in resize2fs, so you can fix the conversion tests to use tune2fs and remove the -b/-B switches from resize2fs entirely.

Oh, I didn't notice that you /do/ remove the resize2fs part later on in the patch set. Sorry for the noise about that.

I think there are still tests that need to be fixed up.

Yes, tests needed fixing up (which took me much longer than I thought because they revealed couple of bugs in my new code ;). Now all the tests seem to be passing.

Honza

Quoted 444 lines

--D

Quoted 440 lines

--D

Quoted 433 lines
---
 misc/tune2fs.c | 367 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 345 insertions(+), 22 deletions(-)
misc/tune2fs.c +345 -22
index d2e8b20f64e2..d6bfad3374d1 100644
@@ -447,10 +447,327 @@ static void request_fsck_afterwards(ext2_filsys fs)
 		printf("%s", _("(and reboot afterwards!)\n"));
 }
 
-static void convert_64bit(ext2_filsys fs, int direction)
+static errcode_t fix_resize_inode(ext2_filsys fs)
 {
-	if (!direction)
-		return;
+	errcode_t retval;
+
+	if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE))
+		return 0;
+
+	retval = ext2fs_wipe_resize_inode(fs);
+	if (retval)
+		return retval;
+
+	return ext2fs_create_resize_inode(fs);
+}
+
+/*
+ * Make blocks that are needed for new group metadata unused and write there
+ * new group descriptors
+ */
+static errcode_t move_bg_metadata(ext2_filsys fs, ext2_filsys new_fs)
+{
+	dgrp_t i;
+	blk64_t b, c, d, old_desc_blocks, new_desc_blocks, j;
+	ext2fs_block_bitmap old_map = NULL, new_map = NULL;
+	int old, new;
+	errcode_t retval;
+	int cluster_ratio;
+	int need_new_block = 0;
+
+	retval = ext2fs_allocate_block_bitmap(fs, "old map", &old_map);
+	if (retval)
+		goto out;
+
+	retval = ext2fs_allocate_block_bitmap(fs, "new map", &new_map);
+	if (retval)
+		goto out;
+
+	if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
+				      EXT2_FEATURE_INCOMPAT_META_BG)) {
+		old_desc_blocks = fs->super->s_first_meta_bg;
+		new_desc_blocks = new_fs->super->s_first_meta_bg;
+	} else {
+		old_desc_blocks = fs->desc_blocks +
+				  fs->super->s_reserved_gdt_blocks;
+		new_desc_blocks = new_fs->desc_blocks +
+				  new_fs->super->s_reserved_gdt_blocks;
+	}
+
+	/* Construct bitmaps of super/descriptor blocks in old and new fs */
+	for (i = 0; i < fs->group_desc_count; i++) {
+		retval = ext2fs_super_and_bgd_loc2(fs, i, &b, &c, &d, NULL);
+		if (retval)
+			goto out;
+		if (b)
+			ext2fs_mark_block_bitmap2(old_map, b);
+		for (j = 0; c != 0 && j < old_desc_blocks; j++)
+			ext2fs_mark_block_bitmap2(old_map, c + j);
+		if (d)
+			ext2fs_mark_block_bitmap2(old_map, d);
+
+		retval = ext2fs_super_and_bgd_loc2(new_fs, i, &b, &c, &d, NULL);
+		if (retval)
+			goto out;
+		if (b)
+			ext2fs_mark_block_bitmap2(new_map, b);
+		for (j = 0; c != 0 && j < new_desc_blocks; j++)
+			ext2fs_mark_block_bitmap2(new_map, c + j);
+		if (d)
+			ext2fs_mark_block_bitmap2(new_map, d);
+	}
+
+	cluster_ratio = EXT2FS_CLUSTER_RATIO(new_fs);
+
+	/* Find changes in block allocations for bg metadata */
+	for (b = EXT2FS_B2C(fs, fs->super->s_first_data_block);
+	     b < ext2fs_blocks_count(new_fs->super);
+	     b += cluster_ratio) {
+		old = ext2fs_test_block_bitmap2(old_map, b);
+		new = ext2fs_test_block_bitmap2(new_map, b);
+
+		if (old && !new) {
+			/* mark old_map, unmark new_map */
+			if (cluster_ratio == 1)
+				ext2fs_block_alloc_stats2(new_fs, b, -1);
+		} else if (!old && new)
+			need_new_block = 1; /* unmark old_map, mark new_map */
+		else {
+			ext2fs_unmark_block_bitmap2(old_map, b);
+			ext2fs_unmark_block_bitmap2(new_map, b);
+		}
+	}
+
+	/*
+	 * new_map now shows blocks that have been newly allocated.
+	 * old_map now shows blocks that have been newly freed.
+	 */
+
+	if (need_new_block) {
+		/*
+		 * Now relocate metadata & data conflicting with new descriptor
+		 * blocks
+		 */
+		retval = ext2fs_move_blocks(new_fs, new_map, old_map);
+		if (retval)
+			goto out;
+
+		/* Now mark blocks for new group descriptors as used */
+		for (b = 0; b < ext2fs_blocks_count(new_fs->super);
+		     b += cluster_ratio) {
+			if (ext2fs_test_block_bitmap2(new_map, b) &&
+			    !ext2fs_test_block_bitmap2(new_fs->block_map, b))
+				ext2fs_block_alloc_stats2(new_fs, b, +1);
+		}
+	}
+
+	if (cluster_ratio > 1) {
+		/* Free unused clusters */
+		for (b = 0; b < ext2fs_blocks_count(new_fs->super);
+		     b += cluster_ratio)
+			if (ext2fs_test_block_bitmap2(old_map, b))
+				ext2fs_block_alloc_stats2(new_fs, b, -1);
+	}
+
+	/* Now update resize inode */
+	retval = fix_resize_inode(new_fs);
+out:
+	if (old_map)
+		ext2fs_free_block_bitmap(old_map);
+	if (new_map)
+		ext2fs_free_block_bitmap(new_map);
+	return retval;
+}
+
+
+/* Toggle 64bit mode */
+static errcode_t resize_group_descriptors(ext2_filsys *fs_p, int direction)
+{
+	void *o, *n, *new_group_desc;
+	dgrp_t i;
+	int copy_size;
+	errcode_t retval;
+	ext2_filsys new_fs = NULL;
+	ext2_filsys fs = *fs_p;
+
+	retval = ext2fs_dup_handle(fs, &new_fs);
+	if (retval)
+		return retval;
+
+	if (direction < 0) {
+		new_fs->super->s_feature_incompat &=
+					~EXT4_FEATURE_INCOMPAT_64BIT;
+		new_fs->super->s_desc_size = EXT2_MIN_DESC_SIZE;
+	} else {
+		new_fs->super->s_feature_incompat |=
+					EXT4_FEATURE_INCOMPAT_64BIT;
+		new_fs->super->s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
+	}
+
+	o = new_fs->group_desc;
+	new_fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
+			EXT2_DESC_PER_BLOCK(new_fs->super));
+	retval = ext2fs_get_arrayzero(new_fs->desc_blocks,
+				      fs->blocksize, &new_group_desc);
+	if (retval)
+		goto out;
+
+	n = new_group_desc;
+
+	if (EXT2_DESC_SIZE(fs->super) <= EXT2_DESC_SIZE(new_fs->super))
+		copy_size = EXT2_DESC_SIZE(fs->super);
+	else
+		copy_size = EXT2_DESC_SIZE(new_fs->super);
+	for (i = 0; i < fs->group_desc_count; i++) {
+		memcpy(n, o, copy_size);
+		n += EXT2_DESC_SIZE(new_fs->super);
+		o += EXT2_DESC_SIZE(fs->super);
+	}
+
+	ext2fs_free_mem(&new_fs->group_desc);
+	new_fs->group_desc = new_group_desc;
+
+	for (i = 0; i < fs->group_desc_count; i++)
+		ext2fs_group_desc_csum_set(new_fs, i);
+
+	/*
+	 * Update number of reserved blocks so that we need to alloc / free as
+	 * few blocks as possible
+	 */
+	if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
+	    (fs->desc_blocks != new_fs->desc_blocks)) {
+		int new;
+
+		new = ((int) new_fs->super->s_reserved_gdt_blocks) +
+			(fs->desc_blocks - new_fs->desc_blocks);
+		if (new < 0)
+			new = 0;
+		if (new > (int) fs->blocksize / 4)
+			new = fs->blocksize / 4;
+		new_fs->super->s_reserved_gdt_blocks = new;
+	}
+
+	retval = move_bg_metadata(fs, new_fs);
+
+	/* Return the modified filesystem */
+	new_fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
+	ext2fs_mark_super_dirty(new_fs);
+	ext2fs_free(fs);
+	*fs_p = new_fs;
+	new_fs = NULL;
+out:
+	if (new_fs)
+		ext2fs_free(new_fs);
+	return retval;
+}
+
+/* Zero out the high bits of extent fields */
+static errcode_t zero_high_bits_in_extents(ext2_filsys fs, ext2_ino_t ino,
+					   struct ext2_inode *inode)
+{
+	ext2_extent_handle_t	handle;
+	struct ext2fs_extent	extent;
+	int			op = EXT2_EXTENT_ROOT;
+	errcode_t		errcode;
+
+	if (!(inode->i_flags & EXT4_EXTENTS_FL))
+		return 0;
+
+	errcode = ext2fs_extent_open(fs, ino, &handle);
+	if (errcode)
+		return errcode;
+
+	while (1) {
+		errcode = ext2fs_extent_get(handle, op, &extent);
+		if (errcode)
+			break;
+
+		op = EXT2_EXTENT_NEXT_SIB;
+
+		if (extent.e_pblk > (1ULL << 32)) {
+			extent.e_pblk &= (1ULL << 32) - 1;
+			errcode = ext2fs_extent_replace(handle, 0, &extent);
+			if (errcode)
+				break;
+		}
+	}
+
+	/* Ok if we run off the end */
+	if (errcode == EXT2_ET_EXTENT_NO_NEXT)
+		errcode = 0;
+	ext2fs_extent_free(handle);
+	return errcode;
+}
+
+/* Zero out the high bits of inodes. */
+static errcode_t zero_high_bits_in_inodes(ext2_filsys fs)
+{
+	int length = EXT2_INODE_SIZE(fs->super);
+	struct ext2_inode *inode = NULL;
+	ext2_inode_scan	scan = NULL;
+	errcode_t	retval;
+	ext2_ino_t	ino;
+
+	if (fs->super->s_creator_os != EXT2_OS_LINUX)
+		return 0;
+
+	retval = ext2fs_open_inode_scan(fs, 0, &scan);
+	if (retval)
+		return retval;
+
+	retval = ext2fs_get_mem(length, &inode);
+	if (retval)
+		goto out;
+
+	do {
+		retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
+		if (retval)
+			goto out;
+		if (!ino)
+			break;
+		if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino))
+			continue;
+
+		/*
+		 * Here's how we deal with high block number fields:
+		 *
+		 *  - i_size_high has been been written out with i_size_lo
+		 *    since the ext2 days, so no conversion is needed.
+		 *
+		 *  - i_blocks_hi is guarded by both the huge_file feature and
+		 *    inode flags and has always been written out with
+		 *    i_blocks_lo if the feature is set.  The field is only
+		 *    ever read if both feature and inode flag are set, so
+		 *    we don't need to zero it now.
+		 *
+		 *  - i_file_acl_high can be uninitialized, so zero it if
+		 *    it isn't already.
+		 */
+		if (inode->osd2.linux2.l_i_file_acl_high) {
+			inode->osd2.linux2.l_i_file_acl_high = 0;
+			retval = ext2fs_write_inode_full(fs, ino, inode,
+							 length);
+			if (retval)
+				goto out;
+		}
+
+		retval = zero_high_bits_in_extents(fs, ino, inode);
+		if (retval)
+			goto out;
+	} while (ino);
+
+out:
+	if (inode)
+		ext2fs_free_mem(&inode);
+	if (scan)
+		ext2fs_close_inode_scan(scan);
+	return retval;
+}
+
+static errcode_t convert_64bit(ext2_filsys *fs_p, int direction)
+{
+	ext2_filsys fs = *fs_p;
+	errcode_t retval;
 
 	/*
 	 * Is resize2fs going to demand a fsck run? Might as well tell the
@@ -459,21 +776,25 @@ static void convert_64bit(ext2_filsys fs, int direction)
 	if (!fsck_requested &&
 	    ((fs->super->s_state & EXT2_ERROR_FS) ||
 	     !(fs->super->s_state & EXT2_VALID_FS) ||
-	     fs->super->s_lastcheck < fs->super->s_mtime))
+	     fs->super->s_lastcheck < fs->super->s_mtime)) {
 		request_fsck_afterwards(fs);
-	if (fsck_requested)
-		fprintf(stderr, _("After running e2fsck, please run `resize2fs %s %s"),
-			direction > 0 ? "-b" : "-s", fs->device_name);
-	else
-		fprintf(stderr, _("Please run `resize2fs %s %s"),
-			direction > 0 ? "-b" : "-s", fs->device_name);
+		return 0;
+	}
 
-	if (undo_file)
-		fprintf(stderr, _(" -z \"%s\""), undo_file);
-	if (direction > 0)
-		fprintf(stderr, _("' to enable 64-bit mode.\n"));
-	else
-		fprintf(stderr, _("' to disable 64-bit mode.\n"));
+	/*
+	 * Read bitmaps now since we are definitely going to need them and they
+	 * get properly duplicated when creating new fs handle in
+	 * resize_group_descriptors()
+	 */
+	retval = ext2fs_read_bitmaps(fs);
+	if (retval)
+		return retval;
+
+	retval = resize_group_descriptors(fs_p, direction);
+	if (retval)
+		return retval;
+
+	return zero_high_bits_in_inodes(*fs_p);
 }
 
 /* Rewrite extents */
@@ -1253,9 +1574,9 @@ mmp_error:
 	}
 
 	/*
-	 * We don't actually toggle 64bit; resize2fs does that.  But this
-	 * must come after the metadata_csum feature_on so that it won't
-	 * complain about the lack of 64bit.
+	 * Handle 64-bit conversion. We return the feature to the original
+	 * value for now until the conversion really takes place. Otherwise
+	 * some helper macros get confused.
 	 */
 	if (FEATURE_ON(E2P_FEATURE_INCOMPAT,
 		       EXT4_FEATURE_INCOMPAT_64BIT)) {
@@ -1264,8 +1585,8 @@ mmp_error:
 					  "while mounted!\n"));
 			exit(1);
 		}
-		sb->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_64BIT;
 		feature_64bit = 1;
+		sb->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_64BIT;
 	}
 	if (FEATURE_OFF(E2P_FEATURE_INCOMPAT,
 			EXT4_FEATURE_INCOMPAT_64BIT)) {
@@ -1274,8 +1595,8 @@ mmp_error:
 					  "while mounted!\n"));
 			exit(1);
 		}
-		sb->s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
 		feature_64bit = -1;
+		sb->s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
 	}
 
 	if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
@@ -3080,6 +3401,9 @@ retry_open:
 		}
 	}
 
+	if (feature_64bit)
+		convert_64bit(&fs, feature_64bit);
+
 	if (rewrite_checksums)
 		rewrite_metadata_checksums(fs);
 
@@ -3113,6 +3437,5 @@ closefs:
 		exit(1);
 	}
 
-	convert_64bit(fs, feature_64bit);
 	return (ext2fs_close_free(&fs) ? 1 : 0);
 }
Signature · 1 line
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#10 lore raw

Re: [PATCH 08/19] ext2fs: Implement block moving in libext2fs

In reply to Darrick J. Wong
Quoted 19 lines · Darrick J. Wong
Quoted 3 lines · Jan Kara

From: Jan Kara <[email protected]>

<snip>

Quoted 12 lines
lib/ext2fs/extent_map.c new file +6 -0
new file mode 100644
index 000000000000..702172b24e2b
@@ -0,0 +1,233 @@
+/*
+ * extent.c --- ext2 extent mapping abstraction
+ *
+ * This abstraction is used to provide a compact way of representing a
+ * translation table, for moving multiple contiguous ranges (extents)
+ * of blocks or inodes.
Quoted 8 lines

lib/ext2fs/extent_map.c line 11

Quoted 5 lines
lib/ext2fs/extent_map.c new file +5 -0
+ *
+ * Copyright (C) 1997, 1998 by Theodore Ts'o and
+ * 	PowerQuest, Inc.
+ *
+ * Copyright (C) 1999, 2000 by Theosore Ts'o

Ted should probably weigh in on this amazing coincidence in contributors :) but this I suspect is a spelling error waiting a long time to be fixed.

Fixed up.

Quoted 29 lines
Quoted 2 lines
lib/ext2fs/extent_map.c new file +2 -0
+ *
+ * %Begin-Header%

<snip>

Quoted 24 lines
lib/ext2fs/move.h new file +18 -0
new file mode 100644
index 000000000000..8d66aa039ec0
@@ -0,0 +1,23 @@
+#ifndef _EXT2FS_MOVE_H
+#define _EXT2FS_MOVE_H
+
+#include "ext2fs.h"
+
+typedef struct _ext2_map_extent *ext2_map_extent;
+
+/* extent_map.c */
+extern int ext2fs_extent_table_empty(ext2_map_extent extent);
+extern errcode_t ext2fs_create_extent_table(ext2_map_extent *ret_extent,
+					    __u64 size);
+extern void ext2fs_free_extent_table(ext2_map_extent extent);
+extern errcode_t ext2fs_add_extent_entry(ext2_map_extent extent,
+					 __u64 old_loc, __u64 new_loc);
+extern __u64 ext2fs_extent_translate(ext2_map_extent extent, __u64 old_loc);
+extern errcode_t ext2fs_iterate_extent(ext2_map_extent extent, __u64 *old_loc,
+				       __u64 *new_loc, __u64 *size);
+
Quoted 8 lines

lib/ext2fs/move.h line 23

Quoted 5 lines
lib/ext2fs/move.h new file +5 -0
+/* move.c */
+errcode_t ext2fs_move_blocks(ext2_filsys fs, ext2fs_block_bitmap move_map,
+			     ext2fs_block_bitmap reuse_map);
+
+#endif

Hmm... are these functions intended for use by client programs? I'm accustomed to looking in ext2fs.h for such functions.

So block moving function is definitely for use by client programs. So I'll move that to ext2fs.h. Extent mapping functions could possibly be useful but no interface in libext2fs needs these as an input / output so I'd consider these internal.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#24 lore raw

Re: [PATCH 12/19] libext2fs: Bump default number of reserved inodes to 64

In reply to Andreas Dilger
Quoted 10 lines · Andreas Dilger
Quoted 8 lines · Jan Kara

From: Jan Kara <[email protected]>

We ran out of reserved inodes so bump the default number of reserved inodes to 64 to get some breathing space. Otherwise we have to do a full fs scan when increasing number of reserved inodes when some feature needing another reserved inode is enabled. This consumes 13.5 KB on a filesystem which is negligible these days.

Quoted 3 lines

This patch also needs to fix all of the regression tests that will fail because the inode numbers are different. Otherwise "make check" will fail.

Yeah, it was a bit tedious work but now all the tests are passing. Thanks for noticing.

Honza

Quoted 82 lines

Cheers, Andreas

Quoted 28 lines

---

lib/ext2fs/initialize.c | 2 +-
misc/mke2fs.8.in        | 8 ++++----
misc/mke2fs.conf.5.in   | 7 ++++---
3 files changed, 9 insertions(+), 8 deletions(-)
lib/ext2fs/initialize.c +1 -1
index 75fbf8ee0061..0ecf4606ce48 100644
@@ -186,7 +186,7 @@ errcode_t ext2fs_initialize(const char *name, int flags,
 
 	set_field(s_rev_level, EXT2_GOOD_OLD_REV);
 	if (super->s_rev_level >= EXT2_DYNAMIC_REV) {
-		set_field(s_first_ino, EXT2_GOOD_OLD_FIRST_INO);
+		set_field(s_first_ino, 64);
 		set_field(s_inode_size, EXT2_GOOD_OLD_INODE_SIZE);
 		if (super->s_inode_size >= sizeof(struct ext2_inode_large)) {
 			int extra_isize = sizeof(struct ext2_inode_large) -
misc/mke2fs.8.in +0 -0
index 520a07185f9f..6eef1d97bfbf 100644
@@ -386,10 +386,10 @@ Do not attempt to discard blocks at mkfs time.

@QUOTA_MAN_COMMENT@behavior is to initialize both user and group quotas. .TP
.BI reserved_inodes= number

misc/mke2fs.8.in Do not attempt to discard blocks at mkfs time.

Quoted 7 lines
misc/mke2fs.8.in +3 -4
-Specify the number of inodes reserved for system files. This number must be
-at least 10. Currently 10 is enough but future features may require additional
-reserved inodes. Reserving more inodes after file system is created requires
-full file system scan so it can take a long time.
+Specify the number of inodes reserved for system files. This number must be at
+least 10, default is 64. Currently 10 is enough but future features may require
+additional reserved inodes.

I'm not sure it is worthwhile to mention "currently 10 is enough", since this should be updated every time some new inode is reserved, but it will likely be forgotten. I don't think users really care about this in the end.

Quoted 20 lines

Reserving more inodes after file system is created +requires full file system scan so it can take a long time. .RE
.TP
.BI \-f " fragment-size"

misc/mke2fs.conf.5.in +0 -0
index 06ca9e4eabc4..b35767bfedf2 100644
@@ -197,9 +197,10 @@ reserved ratio. This value can be a floating point number.

.TP
.I reserved_inodes
This relation specifies the default number of inodes reserved for system files.

misc/mke2fs.conf.5.in +4 -3
-The number must be at least 10. Currently 10 is enough but future features may
-require additional reserved inodes. Reserving more inodes after file system is
-created requires full file system scan so it can take a long time.
+The number must be at least 10, default is 64. Currently 10 is enough but
+future features may require additional reserved inodes. Reserving more inodes
+after file system is created requires full file system scan so it can take a
+long time.
Quoted 10 lines

.TP
.I undo_dir
This relation specifies the directory where the undo file should be

Signature · 6 lines
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Cheers, Andreas

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#29 lore raw

Re: [PATCH 13/19] tune2fs: Add support for changing number of reserved inodes

In reply to Alexey Lyashkov
Quoted 6 lines · Alexey Lyashkov
Quoted 6 lines · Jan Kara

---

Quoted 13 lines

misc/tune2fs.c static int parse_extended_opts(ext2_filsys fs, const char *opts) · line 2315

Quoted 10 lines
misc/tune2fs.c +9 -0
+			new_first_ino = strtoul(arg, &p, 0);
+			if (*p) {
+				fprintf(stderr,
+					_("Invalid number of reserved inodes "
+					  "%s\n"),
+					arg);
+				r_usage++;
+				continue;
+			}

this check invalid - you need check an errno for ERANGE also. add check against maximal number inodes on FS is also good.

Good point. Added both checks.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#27 lore raw

Re: [PATCH 13/19] tune2fs: Add support for changing number of reserved inodes

In reply to Andreas Dilger
Quoted 73 lines · Andreas Dilger
Quoted 57 lines · Jan Kara

---
misc/tune2fs.8.in | 5 +++
misc/tune2fs.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++

2 files changed, 107 insertions(+)
misc/tune2fs.8.in +0 -0
index 4373fc49b255..922705329112 100644
@@ -236,6 +236,11 @@ program.
 This superblock setting is only honored in 2.6.35+ kernels;

and not at all by the ext2 and ext3 file system drivers. .TP
+.BI reserved_inodes= number_of_reserved_inodes

misc/tune2fs.8.in +4 -0
+Set the number of inodes reserved for system files. This number must be
+at least 10. Currently 10 is enough but future features may require additional
+reserved inodes. Reserving more inodes requires full file system scan so it
+can take a long time.

.B test_fs
Set a flag in the filesystem superblock indicating that it may be mounted using experimental kernel code, such as the ext4dev filesystem.

misc/tune2fs.c +19 -0
index d6bfad3374d1..d6e75fe00814 100644
@@ -92,6 +92,7 @@ static char *features_cmd;
 static char *mntopts_cmd;
 static int stride, stripe_width;
 static int stride_set, stripe_width_set;
+static unsigned long new_first_ino;
 static char *extended_cmd;
 static unsigned long new_inode_size;
 static char *ext_mount_opts;
@@ -2298,6 +2299,34 @@ static int parse_extended_opts(ext2_filsys fs, const char *opts)
 				continue;
 			}
 			ext_mount_opts = strdup(arg);
+		} else if (!strcmp(token, "reserved_inodes")) {
+			if (!arg) {
+				r_usage++;
+				continue;
+			}
+			new_first_ino = strtoul(arg, &p, 0);
+			if (*p) {
+				fprintf(stderr,
+					_("Invalid number of reserved inodes "
+					  "%s\n"),
+					arg);
+				r_usage++;
+				continue;
+			}
+			/* Ino 0 is invalid so bump by 1... */
+			new_first_ino++;
+			if (new_first_ino < EXT2_GOOD_OLD_FIRST_INO) {
+				fprintf(stderr,

misc/tune2fs.c static int parse_extended_opts(ext2_filsys fs, const char *opts) · line 2329

Quoted 10 lines
misc/tune2fs.c +10 -0
+					_("Too few reserved inodes "
+				          "%s (must be at least %u)\n"),
+				        arg, EXT2_GOOD_OLD_FIRST_INO - 1);
+				r_usage++;
+				continue;
+			}
+			/*
+			 * Here should go further feature tests to disallow
+			 * admin to free used system inode
+			 */

Not only should this check that the new_first_ino isn't shrinking too small to cover in-use special inodes, it should also verify that it isn't growing to cover in-use normal inodes (e.g. lost+found).

Quoted 4 lines

I guess one option might be to reserve "12" for the lost+found inode, since virtually all filesystems have it this way (though not all), and it would also help e2fsck in the future if it didn't need to allocate some other inode for lost+found in case of corruption.

We do take care to move any normal inodes that are in the way. So this isn't an issue... That's why I had to refactor all the resize code after all...

Honza

Quoted 116 lines

Cheers, Andreas

Quoted 104 lines
misc/tune2fs.c +73 -0
 		} else
 			r_usage++;
 	}
@@ -2312,6 +2341,7 @@ static int parse_extended_opts(ext2_filsys fs, const char *opts)
 			"\tmount_opts=<extended default mount options>\n"
 			"\tstride=<RAID per-disk chunk size in blocks>\n"
 			"\tstripe_width=<RAID stride*data disks in blocks>\n"
+			"\treserved_inodes=<number of reserved inodes>\n"
 			"\ttest_fs\n"
 			"\t^test_fs\n"));
 		free(buf);
@@ -2986,6 +3016,75 @@ fs_update_journal_user(struct ext2_super_block *sb, __u8 old_uuid[UUID_SIZE])
 	return 0;
 }
 
+/* Zero range of inodes and mark them as free / used */
+static errcode_t zero_inodes_range(ext2_filsys fs, ext2_ino_t start,
+				   ext2_ino_t end, int inuse)
+{
+	char *inode;
+	int length = EXT2_INODE_SIZE(fs->super);
+	ext2_ino_t ino;
+	errcode_t retval;
+
+	retval = ext2fs_get_memzero(length, &inode);
+	if (retval)
+		return retval;
+
+	for (ino = start; ino <= end; ino++) {
+		ext2fs_inode_alloc_stats(fs, ino, inuse);
+		retval = ext2fs_write_inode_full(fs, ino,
+						 (struct ext2_inode *)inode,
+						 length);
+		if (retval)
+			break;
+	}
+	ext2fs_free_mem(inode);
+
+	return retval;
+}
+
+static errcode_t update_reserved_inodes(ext2_filsys fs)
+{
+	errcode_t retval = 0;
+	ext2fs_inode_bitmap imap;
+	ext2_ino_t ino, first_ino = fs->super->s_first_ino;
+
+	if (new_first_ino == first_ino)
+		return 0;
+
+	/* Group descriptors will need writing as well */
+	fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
+
+	/* Freeing reserved inodes is easy */
+	if (new_first_ino < first_ino) {
+		retval = zero_inodes_range(fs, new_first_ino, first_ino - 1,
+					   -1);
+		if (retval)
+			return retval;
+		goto out;
+	}
+
+	retval = ext2fs_allocate_inode_bitmap(fs, "inodes to move", &imap);
+	if (retval)
+		return retval;
+
+	for (ino = fs->super->s_first_ino; ino < new_first_ino; ino++)
+		ext2fs_mark_inode_bitmap2(imap, ino);
+
+	retval = ext2fs_move_inodes(fs, imap);
+	ext2fs_free_inode_bitmap(imap);
+	if (retval)
+		return retval;
+
+	retval = zero_inodes_range(fs, first_ino, new_first_ino - 1, +1);
+	if (retval)
+		return retval;
+out:
+	fs->super->s_first_ino = new_first_ino;
+	ext2fs_mark_super_dirty(fs);
+
+	return 0;
+}
+
 int main(int argc, char **argv)
 {
 	errcode_t retval;
@@ -3404,6 +3503,9 @@ retry_open:
 	if (feature_64bit)
 		convert_64bit(&fs, feature_64bit);
 
+	if (new_first_ino)
+		update_reserved_inodes(fs);
+
 	if (rewrite_checksums)
 		rewrite_metadata_checksums(fs);
 
Signature · 6 lines
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Cheers, Andreas

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#33 lore raw

Re: [PATCH 15/19] resize2fs: Remove duplicit condition

In reply to Andreas Dilger
Quoted 5 lines · Andreas Dilger
Quoted 1 line · Jan Kara

Subject: [PATCH 15/19] resize2fs: Remove duplicit condition

Minor typo - s/duplicit/duplicate/

Thanks. Fixed.

Honza

Quoted 47 lines
Quoted 38 lines

---
resize/resize2fs.c | 12 +-----------

1 file changed, 1 insertion(+), 11 deletions(-)
resize/resize2fs.c +1 -11
index 9a8be30bbce1..22300836300d 100644
@@ -682,19 +682,9 @@ static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size)
 	}
 
 	/*
-	 * If we are shrinking the number block groups, we're done and
+	 * If we are not increasing the number block groups, we're done and
 	 * can exit now.
 	 */
-	if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
-		retval = 0;
-		goto errout;
-	}
-
-	/*
-	 * If the number of block groups is staying the same, we're
-	 * done and can exit now.  (If the number block groups is
-	 * shrinking, we had exited earlier.)
-	 */
 	if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
 		retval = 0;
 		goto errout;
Signature · 6 lines
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Cheers, Andreas

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR