linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 5/7] [v2] lib: deduplicate buffer_head/kernel emulation
@ 2023-09-19  9:09 Andreas Dilger
  2023-09-19  9:09 ` [PATCH 6/7] misc: deduplicate log2/log10 functions Andreas Dilger
  2023-09-19  9:09 ` [PATCH 7/7] lib: make journal.c more consistent Andreas Dilger
  0 siblings, 2 replies; 3+ messages in thread
From: Andreas Dilger @ 2023-09-19  9:09 UTC (permalink / raw)
  To: tytso; +Cc: linux-ext4, Alexey Lyashkov, Andreas Dilger

From: Alexey Lyashkov <alexey.lyashkov@gmail.com>

Move buffer_head and device code into libsupport to allow it
to be shared between debugfs and e2fsck.

Signed-off-by: Alexey Lyashkov <alexey.lyashkov@gmail.com>
Reviewed-by: Andreas Dilger <adilger@dilger.ca>
---
 debugfs/journal.c | 147 ----------------------------------------------
 e2fsck/journal.c  | 143 --------------------------------------------
 2 files changed, 290 deletions(-)

diff --git a/debugfs/journal.c b/debugfs/journal.c
index 8c84be25..6e8dec35 100644
--- a/debugfs/journal.c
+++ b/debugfs/journal.c
@@ -26,8 +26,6 @@
 #include "uuid/uuid.h"
 #include "journal.h"
 
-static int bh_count = 0;
-
 #if EXT2_FLAT_INCLUDES
 #include "blkid.h"
 #else
@@ -47,151 +45,6 @@ static int bh_count = 0;
  * to use the recovery.c file virtually unchanged from the kernel, so we
  * don't have to do much to keep kernel and user recovery in sync.
  */
-int jbd2_journal_bmap(journal_t *journal, unsigned long block,
-		      unsigned long long *phys)
-{
-#ifdef USE_INODE_IO
-	*phys = block;
-	return 0;
-#else
-	struct inode	*inode = journal->j_inode;
-	errcode_t	retval;
-	blk64_t		pblk;
-
-	if (!inode) {
-		*phys = block;
-		return 0;
-	}
-
-	retval = ext2fs_bmap2(inode->i_fs, inode->i_ino,
-			      &inode->i_ext2, NULL, 0, (blk64_t) block,
-			      0, &pblk);
-	*phys = pblk;
-	return (int) retval;
-#endif
-}
-
-struct buffer_head *getblk(kdev_t kdev, unsigned long long blocknr,
-			   int blocksize)
-{
-	struct buffer_head *bh;
-	int bufsize = sizeof(*bh) + kdev->k_fs->blocksize -
-		sizeof(bh->b_data);
-	errcode_t retval;
-
-	retval = ext2fs_get_memzero(bufsize, &bh);
-	if (retval)
-		return NULL;
-
-	if (journal_enable_debug >= 3)
-		bh_count++;
-	jfs_debug(4, "getblk for block %llu (%d bytes)(total %d)\n",
-		  blocknr, blocksize, bh_count);
-
-	bh->b_fs = kdev->k_fs;
-	if (kdev->k_dev == K_DEV_FS)
-		bh->b_io = kdev->k_fs->io;
-	else
-		bh->b_io = kdev->k_fs->journal_io;
-	bh->b_size = blocksize;
-	bh->b_blocknr = blocknr;
-
-	return bh;
-}
-
-int sync_blockdev(kdev_t kdev)
-{
-	io_channel	io;
-
-	if (kdev->k_dev == K_DEV_FS)
-		io = kdev->k_fs->io;
-	else
-		io = kdev->k_fs->journal_io;
-
-	return io_channel_flush(io) ? EIO : 0;
-}
-
-void ll_rw_block(int rw, int op_flags EXT2FS_ATTR((unused)), int nr,
-		 struct buffer_head *bhp[])
-{
-	errcode_t retval;
-	struct buffer_head *bh;
-
-	for (; nr > 0; --nr) {
-		bh = *bhp++;
-		if (rw == REQ_OP_READ && !bh->b_uptodate) {
-			jfs_debug(3, "reading block %llu/%p\n",
-				  bh->b_blocknr, (void *) bh);
-			retval = io_channel_read_blk64(bh->b_io,
-						     bh->b_blocknr,
-						     1, bh->b_data);
-			if (retval) {
-				com_err(bh->b_fs->device_name, retval,
-					"while reading block %llu\n",
-					bh->b_blocknr);
-				bh->b_err = (int) retval;
-				continue;
-			}
-			bh->b_uptodate = 1;
-		} else if (rw == REQ_OP_WRITE && bh->b_dirty) {
-			jfs_debug(3, "writing block %llu/%p\n",
-				  bh->b_blocknr,
-				  (void *) bh);
-			retval = io_channel_write_blk64(bh->b_io,
-						      bh->b_blocknr,
-						      1, bh->b_data);
-			if (retval) {
-				com_err(bh->b_fs->device_name, retval,
-					"while writing block %llu\n",
-					bh->b_blocknr);
-				bh->b_err = (int) retval;
-				continue;
-			}
-			bh->b_dirty = 0;
-			bh->b_uptodate = 1;
-		} else {
-			jfs_debug(3, "no-op %s for block %llu\n",
-				  rw == REQ_OP_READ ? "read" : "write",
-				  bh->b_blocknr);
-		}
-	}
-}
-
-void mark_buffer_dirty(struct buffer_head *bh)
-{
-	bh->b_dirty = 1;
-}
-
-static void mark_buffer_clean(struct buffer_head *bh)
-{
-	bh->b_dirty = 0;
-}
-
-void brelse(struct buffer_head *bh)
-{
-	if (bh->b_dirty)
-		ll_rw_block(REQ_OP_WRITE, 0, 1, &bh);
-	jfs_debug(3, "freeing block %llu/%p (total %d)\n",
-		  bh->b_blocknr, (void *) bh, --bh_count);
-	ext2fs_free_mem(&bh);
-}
-
-int buffer_uptodate(struct buffer_head *bh)
-{
-	return bh->b_uptodate;
-}
-
-void mark_buffer_uptodate(struct buffer_head *bh, int val)
-{
-	bh->b_uptodate = val;
-}
-
-void wait_on_buffer(struct buffer_head *bh)
-{
-	if (!bh->b_uptodate)
-		ll_rw_block(REQ_OP_READ, 0, 1, &bh);
-}
-
 
 static void ext2fs_clear_recover(ext2_filsys fs, int error)
 {
diff --git a/e2fsck/journal.c b/e2fsck/journal.c
index d6129ed1..02251956 100644
--- a/e2fsck/journal.c
+++ b/e2fsck/journal.c
@@ -27,8 +27,6 @@
 #include "problem.h"
 #include "uuid/uuid.h"
 
-static int bh_count = 0;
-
 /*
  * Define USE_INODE_IO to use the inode_io.c / fileio.c codepaths.
  * This creates a larger static binary, and a smaller binary using
@@ -42,147 +40,6 @@ static int bh_count = 0;
  * to use the recovery.c file virtually unchanged from the kernel, so we
  * don't have to do much to keep kernel and user recovery in sync.
  */
-int jbd2_journal_bmap(journal_t *journal, unsigned long block,
-		      unsigned long long *phys)
-{
-#ifdef USE_INODE_IO
-	*phys = block;
-	return 0;
-#else
-	struct inode 	*inode = journal->j_inode;
-	errcode_t	retval;
-	blk64_t		pblk;
-
-	if (!inode) {
-		*phys = block;
-		return 0;
-	}
-
-	retval= ext2fs_bmap2(inode->i_fs, inode->i_ino,
-			     &inode->i_ext2, NULL, 0, (blk64_t) block,
-			     0, &pblk);
-	*phys = pblk;
-	return -1 * ((int) retval);
-#endif
-}
-
-struct buffer_head *getblk(kdev_t kdev, unsigned long long blocknr,
-			   int blocksize)
-{
-	struct buffer_head *bh;
-	int bufsize = sizeof(*bh) + kdev->k_fs->blocksize -
-		sizeof(bh->b_data);
-	errcode_t retval;
-
-	retval = ext2fs_get_memzero(bufsize, &bh);
-	if (retval)
-		return NULL;
-
-	if (journal_enable_debug >= 3)
-		bh_count++;
-	jfs_debug(4, "getblk for block %llu (%d bytes)(total %d)\n",
-		  blocknr, blocksize, bh_count);
-
-	bh->b_fs = kdev->k_fs;
-	if (kdev->k_dev == K_DEV_FS)
-		bh->b_io = kdev->k_fs->io;
-	else
-		bh->b_io = kdev->k_fs->journal_io;
-	bh->b_size = blocksize;
-	bh->b_blocknr = blocknr;
-
-	return bh;
-}
-
-int sync_blockdev(kdev_t kdev)
-{
-	io_channel	io;
-
-	if (kdev->k_dev == K_DEV_FS)
-		io = kdev->k_fs->io;
-	else
-		io = kdev->k_fs->journal_io;
-
-	return io_channel_flush(io) ? -EIO : 0;
-}
-
-void ll_rw_block(int rw, int op_flags EXT2FS_ATTR((unused)), int nr,
-		 struct buffer_head *bhp[])
-{
-	errcode_t retval;
-	struct buffer_head *bh;
-
-	for (; nr > 0; --nr) {
-		bh = *bhp++;
-		if (rw == REQ_OP_READ && !bh->b_uptodate) {
-			jfs_debug(3, "reading block %llu/%p\n",
-				  bh->b_blocknr, (void *) bh);
-			retval = io_channel_read_blk64(bh->b_io,
-						     bh->b_blocknr,
-						     1, bh->b_data);
-			if (retval) {
-				com_err(bh->b_fs->device_name, retval,
-					"while reading block %llu\n",
-					bh->b_blocknr);
-				bh->b_err = (int) retval;
-				continue;
-			}
-			bh->b_uptodate = 1;
-		} else if (rw == REQ_OP_WRITE && bh->b_dirty) {
-			jfs_debug(3, "writing block %llu/%p\n",
-				  bh->b_blocknr,
-				  (void *) bh);
-			retval = io_channel_write_blk64(bh->b_io,
-						      bh->b_blocknr,
-						      1, bh->b_data);
-			if (retval) {
-				com_err(bh->b_fs->device_name, retval,
-					"while writing block %llu\n",
-					bh->b_blocknr);
-				bh->b_err = (int) retval;
-				continue;
-			}
-			bh->b_dirty = 0;
-			bh->b_uptodate = 1;
-		} else {
-			jfs_debug(3, "no-op %s for block %llu\n",
-				  rw == REQ_OP_READ ? "read" : "write",
-				  bh->b_blocknr);
-		}
-	}
-}
-
-void mark_buffer_dirty(struct buffer_head *bh)
-{
-	bh->b_dirty = 1;
-}
-
-void brelse(struct buffer_head *bh)
-{
-	if (bh->b_dirty)
-		ll_rw_block(REQ_OP_WRITE, 0, 1, &bh);
-	jfs_debug(3, "freeing block %llu/%p (total %d)\n",
-		  bh->b_blocknr, (void *) bh, --bh_count);
-	ext2fs_free_mem(&bh);
-}
-
-int buffer_uptodate(struct buffer_head *bh)
-{
-	return bh->b_uptodate;
-}
-
-void mark_buffer_uptodate(struct buffer_head *bh, int val)
-{
-	bh->b_uptodate = val;
-}
-
-void wait_on_buffer(struct buffer_head *bh)
-{
-	if (!bh->b_uptodate)
-		ll_rw_block(REQ_OP_READ, 0, 1, &bh);
-}
-
-
 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
 {
 	ext2fs_clear_feature_journal_needs_recovery(ctx->fs->super);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 6/7] misc: deduplicate log2/log10 functions
  2023-09-19  9:09 [PATCH 5/7] [v2] lib: deduplicate buffer_head/kernel emulation Andreas Dilger
@ 2023-09-19  9:09 ` Andreas Dilger
  2023-09-19  9:09 ` [PATCH 7/7] lib: make journal.c more consistent Andreas Dilger
  1 sibling, 0 replies; 3+ messages in thread
From: Andreas Dilger @ 2023-09-19  9:09 UTC (permalink / raw)
  To: tytso; +Cc: linux-ext4, Andreas Dilger

Remove duplicate log2() and log10() functions and replace them
with a single pair of functions ext2fs_log2() and ext2fs_log10().

Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Change-Id: Ifc86efe7e5f0243eb914c6d24319cc7dee3ebbe5
---
 debugfs/debugfs.c   | 16 ++--------------
 debugfs/filefrag.c  | 18 +++---------------
 lib/ext2fs/ext2fs.h | 24 ++++++++++++++++++++++++
 lib/ext2fs/extent.c | 17 +++--------------
 misc/e2freefrag.c   | 20 ++++----------------
 misc/e4crypt.c      | 14 +-------------
 misc/filefrag.c     | 32 ++++----------------------------
 misc/mk_hugefiles.c |  2 +-
 misc/mke2fs.c       | 35 +++++++----------------------------
 misc/mke2fs.h       |  1 -
 10 files changed, 49 insertions(+), 130 deletions(-)

diff --git a/debugfs/debugfs.c b/debugfs/debugfs.c
index 742bf794..66428f7d 100644
--- a/debugfs/debugfs.c
+++ b/debugfs/debugfs.c
@@ -652,18 +652,6 @@ static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
 	fprintf(f,"\n");
 }
 
-static int int_log10(unsigned long long arg)
-{
-	int     l = 0;
-
-	arg = arg / 10;
-	while (arg) {
-		l++;
-		arg = arg / 10;
-	}
-	return l;
-}
-
 #define DUMP_LEAF_EXTENTS	0x01
 #define DUMP_NODE_EXTENTS	0x02
 #define DUMP_EXTENT_TABLE	0x04
@@ -1065,11 +1053,11 @@ void do_dump_extents(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
 		return;
 	}
 
-	logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
+	logical_width = ext2fs_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
 				  current_fs->blocksize) + 1;
 	if (logical_width < 5)
 		logical_width = 5;
-	physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
+	physical_width = ext2fs_log10(ext2fs_blocks_count(current_fs->super)) + 1;
 	if (physical_width < 5)
 		physical_width = 5;
 
diff --git a/debugfs/filefrag.c b/debugfs/filefrag.c
index 31c1440c..080c2c37 100644
--- a/debugfs/filefrag.c
+++ b/debugfs/filefrag.c
@@ -54,18 +54,6 @@ struct filefrag_struct {
 	struct dir_list *dir_list, *dir_last;
 };
 
-static int int_log10(unsigned long long arg)
-{
-	int     l = 0;
-
-	arg = arg / 10;
-	while (arg) {
-		l++;
-		arg = arg / 10;
-	}
-	return l;
-}
-
 static void print_header(struct filefrag_struct *fs)
 {
 	if (fs->options & VERBOSE_OPT) {
@@ -135,8 +123,8 @@ static void filefrag(ext2_ino_t ino, struct ext2_inode *inode,
 	errcode_t	retval;
 	int		blocksize = current_fs->blocksize;
 
-	fs->logical_width = int_log10((EXT2_I_SIZE(inode) + blocksize - 1) /
-				      blocksize) + 1;
+	fs->logical_width = ext2fs_log10((EXT2_I_SIZE(inode) + blocksize - 1) /
+					 blocksize) + 1;
 	if (fs->logical_width < 7)
 		fs->logical_width = 7;
 	fs->ext = 0;
@@ -313,7 +301,7 @@ void do_filefrag(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
 		return;
 
 	fs.f = open_pager();
-	fs.physical_width = int_log10(ext2fs_blocks_count(current_fs->super));
+	fs.physical_width = ext2fs_log10(ext2fs_blocks_count(current_fs->super));
 	fs.physical_width++;
 	if (fs.physical_width < 8)
 		fs.physical_width = 8;
diff --git a/lib/ext2fs/ext2fs.h b/lib/ext2fs/ext2fs.h
index b5477c10..8e02d6b6 100644
--- a/lib/ext2fs/ext2fs.h
+++ b/lib/ext2fs/ext2fs.h
@@ -2119,6 +2119,30 @@ _INLINE_ int ext2fs_htree_intnode_maxrecs(ext2_filsys fs, int blocks)
 						sizeof(struct ext2_dx_entry));
 }
 
+_INLINE_ int ext2fs_log2(unsigned long long arg)
+{
+	int l = 0;
+
+	arg >>= 1;
+	while (arg) {
+		l++;
+		arg >>= 1;
+	}
+	return l;
+}
+
+_INLINE_ int ext2fs_log10(unsigned long long arg)
+{
+	int l = 0;
+
+	arg /= 10;
+	while (arg) {
+		l++;
+		arg /= 10;
+	}
+	return l;
+}
+
 /*
  * This is an efficient, overflow safe way of calculating ceil((1.0 * a) / b)
  */
diff --git a/lib/ext2fs/extent.c b/lib/ext2fs/extent.c
index 82e75ccd..f747a561 100644
--- a/lib/ext2fs/extent.c
+++ b/lib/ext2fs/extent.c
@@ -1720,18 +1720,6 @@ errcode_t ext2fs_extent_get_info(ext2_extent_handle_t handle,
 	return 0;
 }
 
-static int ul_log2(unsigned long arg)
-{
-	int	l = 0;
-
-	arg >>= 1;
-	while (arg) {
-		l++;
-		arg >>= 1;
-	}
-	return l;
-}
-
 size_t ext2fs_max_extent_depth(ext2_extent_handle_t handle)
 {
 	size_t iblock_sz = sizeof(((struct ext2_inode *)NULL)->i_block);
@@ -1746,8 +1734,9 @@ size_t ext2fs_max_extent_depth(ext2_extent_handle_t handle)
 	if (last_blocksize && last_blocksize == handle->fs->blocksize)
 		return last_result;
 
-	last_result = 1 + ((ul_log2(EXT_MAX_EXTENT_LBLK) - ul_log2(iblock_extents)) /
-		    ul_log2(extents_per_block));
+	last_result = 1 + ((ext2fs_log2(EXT_MAX_EXTENT_LBLK) -
+			    ext2fs_log2(iblock_extents)) /
+			   ext2fs_log2(extents_per_block));
 	last_blocksize = handle->fs->blocksize;
 	return last_result;
 }
diff --git a/misc/e2freefrag.c b/misc/e2freefrag.c
index 04f155b6..903789f0 100644
--- a/misc/e2freefrag.c
+++ b/misc/e2freefrag.c
@@ -57,28 +57,16 @@ static void usage(const char *prog)
 #endif
 }
 
-static int ul_log2(unsigned long arg)
-{
-        int     l = 0;
-
-        arg >>= 1;
-        while (arg) {
-                l++;
-                arg >>= 1;
-        }
-        return l;
-}
-
 static void init_chunk_info(ext2_filsys fs, struct chunk_info *info)
 {
 	int i;
 
-	info->blocksize_bits = ul_log2((unsigned long)fs->blocksize);
+	info->blocksize_bits = ext2fs_log2(fs->blocksize);
 	if (info->chunkbytes) {
-		info->chunkbits = ul_log2(info->chunkbytes);
+		info->chunkbits = ext2fs_log2(info->chunkbytes);
 		info->blks_in_chunk = info->chunkbytes >> info->blocksize_bits;
 	} else {
-		info->chunkbits = ul_log2(DEFAULT_CHUNKSIZE);
+		info->chunkbits = ext2fs_log2(DEFAULT_CHUNKSIZE);
 		info->blks_in_chunk = DEFAULT_CHUNKSIZE >> info->blocksize_bits;
 	}
 
@@ -97,7 +85,7 @@ static void update_chunk_stats(struct chunk_info *info,
 {
 	unsigned long idx;
 
-	idx = ul_log2(chunk_size) + 1;
+	idx = ext2fs_log2(chunk_size) + 1;
 	if (idx >= MAX_HIST)
 		idx = MAX_HIST-1;
 	info->histogram.fc_chunks[idx]++;
diff --git a/misc/e4crypt.c b/misc/e4crypt.c
index 67d25d88..6f23927d 100644
--- a/misc/e4crypt.c
+++ b/misc/e4crypt.c
@@ -114,18 +114,6 @@ static const size_t hexchars_size = 16;
 #define EXT4_IOC_SET_ENCRYPTION_POLICY      _IOR('f', 19, struct ext4_encryption_policy)
 #define EXT4_IOC_GET_ENCRYPTION_POLICY      _IOW('f', 21, struct ext4_encryption_policy)
 
-static int int_log2(int arg)
-{
-	int     l = 0;
-
-	arg >>= 1;
-	while (arg) {
-		l++;
-		arg >>= 1;
-	}
-	return l;
-}
-
 static void validate_paths(int argc, char *argv[], int path_start_index)
 {
 	int x;
@@ -386,7 +374,7 @@ static void set_policy(struct salt *set_salt, int pad,
 			EXT4_ENCRYPTION_MODE_AES_256_XTS;
 		policy.filenames_encryption_mode =
 			EXT4_ENCRYPTION_MODE_AES_256_CTS;
-		policy.flags = int_log2(pad >> 2);
+		policy.flags = ext2fs_log2(pad >> 2);
 		memcpy(policy.master_key_descriptor, salt->key_desc,
 		       EXT4_KEY_DESCRIPTOR_SIZE);
 		rc = ioctl(fd, EXT4_IOC_SET_ENCRYPTION_POLICY, &policy);
diff --git a/misc/filefrag.c b/misc/filefrag.c
index eaaa90a8..13a533e5 100644
--- a/misc/filefrag.c
+++ b/misc/filefrag.c
@@ -76,30 +76,6 @@ const char *hex_fmt = "%4d: %*llx..%*llx: %*llx..%*llx: %6llx: %s\n";
 #define	EXT4_EXTENTS_FL			0x00080000 /* Inode uses extents */
 #define	EXT3_IOC_GETFLAGS		_IOR('f', 1, long)
 
-static int ulong_log2(unsigned long arg)
-{
-	int     l = 0;
-
-	arg >>= 1;
-	while (arg) {
-		l++;
-		arg >>= 1;
-	}
-	return l;
-}
-
-static int ulong_log10(unsigned long long arg)
-{
-	int     l = 0;
-
-	arg = arg / 10;
-	while (arg) {
-		l++;
-		arg = arg / 10;
-	}
-	return l;
-}
-
 static unsigned int div_ceil(unsigned int a, unsigned int b)
 {
 	if (!a)
@@ -483,20 +459,20 @@ static int frag_report(const char *filename)
 	}
 	last_device = st.st_dev;
 
-	width = ulong_log10(fsinfo.f_blocks);
+	width = ext2fs_log10(fsinfo.f_blocks);
 	if (width > physical_width)
 		physical_width = width;
 
 	numblocks = (st.st_size + blksize - 1) / blksize;
 	if (blocksize != 0)
-		blk_shift = ulong_log2(blocksize);
+		blk_shift = ext2fs_log2(blocksize);
 	else
-		blk_shift = ulong_log2(blksize);
+		blk_shift = ext2fs_log2(blksize);
 
 	if (use_extent_cache)
 		width = 10;
 	else
-		width = ulong_log10(numblocks);
+		width = ext2fs_log10(numblocks);
 	if (width > logical_width)
 		logical_width = width;
 	if (verbose) {
diff --git a/misc/mk_hugefiles.c b/misc/mk_hugefiles.c
index 3caaf1b6..17788bcd 100644
--- a/misc/mk_hugefiles.c
+++ b/misc/mk_hugefiles.c
@@ -417,7 +417,7 @@ errcode_t mk_hugefiles(ext2_filsys fs, const char *device_name)
 	fn_prefix = get_string_from_profile(fs_types, "hugefiles_name",
 					    "hugefile");
 	idx_digits = get_int_from_profile(fs_types, "hugefiles_digits", 5);
-	d = int_log10(num_files) + 1;
+	d = ext2fs_log10(num_files) + 1;
 	if (idx_digits > d)
 		d = idx_digits;
 	dsize = strlen(fn_prefix) + d + 16;
diff --git a/misc/mke2fs.c b/misc/mke2fs.c
index ba5f179a..c6e26e70 100644
--- a/misc/mke2fs.c
+++ b/misc/mke2fs.c
@@ -144,27 +144,6 @@ static void usage(void)
 	exit(1);
 }
 
-static int int_log2(unsigned long long arg)
-{
-	int	l = 0;
-
-	arg >>= 1;
-	while (arg) {
-		l++;
-		arg >>= 1;
-	}
-	return l;
-}
-
-int int_log10(unsigned long long arg)
-{
-	int	l;
-
-	for (l=0; arg ; l++)
-		arg = arg / 10;
-	return l;
-}
-
 #ifdef __linux__
 static int parse_version_number(const char *s)
 {
@@ -743,7 +722,7 @@ skip_details:
 			continue;
 		if (i != 1)
 			printf(", ");
-		need = int_log10(group_block) + 2;
+		need = ext2fs_log10(group_block) + 2;
 		if (need > col_left) {
 			printf("\n\t");
 			col_left = 72;
@@ -1669,8 +1648,8 @@ profile_error:
 					blocksize);
 			if (blocksize > 0)
 				fs_param.s_log_block_size =
-					int_log2(blocksize >>
-						 EXT2_MIN_BLOCK_LOG_SIZE);
+					ext2fs_log2(blocksize >>
+						    EXT2_MIN_BLOCK_LOG_SIZE);
 			break;
 		case 'c':	/* Check for bad blocks */
 			cflag++;
@@ -1949,7 +1928,7 @@ profile_error:
 		blocksize = jfs->blocksize;
 		printf(_("Using journal device's blocksize: %d\n"), blocksize);
 		fs_param.s_log_block_size =
-			int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
+			ext2fs_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
 		ext2fs_close_free(&jfs);
 	}
 
@@ -2188,7 +2167,7 @@ profile_error:
 	}
 
 	fs_param.s_log_block_size =
-		int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
+		ext2fs_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
 
 	/*
 	 * We now need to do a sanity check of fs_blocks_count for
@@ -2312,7 +2291,7 @@ profile_error:
 							    "cluster_size",
 							    blocksize*16);
 		fs_param.s_log_cluster_size =
-			int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
+			ext2fs_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
 		if (fs_param.s_log_cluster_size &&
 		    fs_param.s_log_cluster_size < fs_param.s_log_block_size) {
 			com_err(program_name, 0, "%s",
@@ -2580,7 +2559,7 @@ profile_error:
 				  "flex_bg size may not be specified"));
 			exit(1);
 		}
-		fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
+		fs_param.s_log_groups_per_flex = ext2fs_log2(flex_bg_size);
 	}
 
 	if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
diff --git a/misc/mke2fs.h b/misc/mke2fs.h
index ce72cb3f..c718fceb 100644
--- a/misc/mke2fs.h
+++ b/misc/mke2fs.h
@@ -21,7 +21,6 @@ extern char *get_string_from_profile(char **types, const char *opt,
 				     const char *def_val);
 extern int get_int_from_profile(char **types, const char *opt, int def_val);
 extern int get_bool_from_profile(char **types, const char *opt, int def_val);
-extern int int_log10(unsigned long long arg);
 
 /* mk_hugefiles.c */
 extern errcode_t mk_hugefiles(ext2_filsys fs, const char *device_name);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 7/7] lib: make journal.c more consistent
  2023-09-19  9:09 [PATCH 5/7] [v2] lib: deduplicate buffer_head/kernel emulation Andreas Dilger
  2023-09-19  9:09 ` [PATCH 6/7] misc: deduplicate log2/log10 functions Andreas Dilger
@ 2023-09-19  9:09 ` Andreas Dilger
  1 sibling, 0 replies; 3+ messages in thread
From: Andreas Dilger @ 2023-09-19  9:09 UTC (permalink / raw)
  To: tytso; +Cc: linux-ext4, Andreas Dilger

Make the journal.c files more consistent between e2fsck and
debugfs.  Declare a local "ext2_filsys fs" variable in many
functions so that the common use of "ctx->fs" can be removed.

The file cannot be exactly the same, but removing a number of
minor differences makes it more clear where real differences
are when comparing the files with vimdiff, and will simplify
resolution of remaining differences later.

Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Change-Id: I27fd60e955967665746a6cca60f60e76f23ebbe5
---
 debugfs/journal.c |  65 ++++++++----
 e2fsck/journal.c  | 259 ++++++++++++++++++++++++++--------------------
 2 files changed, 186 insertions(+), 138 deletions(-)

diff --git a/debugfs/journal.c b/debugfs/journal.c
index 6e8dec35..bb95e9d1 100644
--- a/debugfs/journal.c
+++ b/debugfs/journal.c
@@ -23,8 +23,6 @@
 #endif
 
 #define E2FSCK_INCLUDE_INLINE_FUNCS
-#include "uuid/uuid.h"
-#include "journal.h"
 
 #if EXT2_FLAT_INCLUDES
 #include "blkid.h"
@@ -32,6 +30,9 @@
 #include "blkid/blkid.h"
 #endif
 
+#include "uuid/uuid.h"
+#include "journal.h"
+
 /*
  * Define USE_INODE_IO to use the inode_io.c / fileio.c codepaths.
  * This creates a larger static binary, and a smaller binary using
@@ -107,12 +108,14 @@ static errcode_t ext2fs_get_journal(ext2_filsys fs, journal_t **ret_journal)
 	int			tried_backup_jnl = 0;
 
 	retval = ext2fs_get_memzero(sizeof(journal_t), &journal);
-	if (retval)
+	if (retval) {
 		return retval;
+	}
 
 	retval = ext2fs_get_memzero(2 * sizeof(struct kdev_s), &dev_fs);
-	if (retval)
+	if (retval) {
 		goto errout;
+	}
 	dev_journal = dev_fs+1;
 
 	dev_fs->k_fs = dev_journal->k_fs = fs;
@@ -125,13 +128,21 @@ static errcode_t ext2fs_get_journal(ext2_filsys fs, journal_t **ret_journal)
 	journal->j_blocksize = fs->blocksize;
 
 	if (uuid_is_null(sb->s_journal_uuid)) {
-		if (!sb->s_journal_inum) {
+		/*
+		 * The full set of superblock sanity checks haven't
+		 * been performed yet, so we need to do some basic
+		 * checks here to avoid potential array overruns.
+		 */
+		if (!sb->s_journal_inum ||
+		    (sb->s_journal_inum >
+		     (fs->group_desc_count * sb->s_inodes_per_group))) {
 			retval = EXT2_ET_BAD_INODE_NUM;
 			goto errout;
 		}
 		retval = ext2fs_get_memzero(sizeof(*j_inode), &j_inode);
-		if (retval)
+		if (retval) {
 			goto errout;
+		}
 
 		j_inode->i_fs = fs;
 		j_inode->i_ino = sb->s_journal_inum;
@@ -153,7 +164,8 @@ try_backup_journal:
 			tried_backup_jnl++;
 		}
 		if (!j_inode->i_ext2.i_links_count ||
-		    !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) {
+		    !LINUX_S_ISREG(j_inode->i_ext2.i_mode) ||
+		    (j_inode->i_ext2.i_flags & EXT4_ENCRYPT_FL)) {
 			retval = EXT2_ET_NO_JOURNAL;
 			goto try_backup_journal;
 		}
@@ -229,7 +241,8 @@ try_backup_journal:
 	if (ext_journal)
 #endif
 	{
-		retval = io_ptr->open(journal_name, fs->flags & EXT2_FLAG_RW,
+		int flags = fs->flags & EXT2_FLAG_RW;
+		retval = io_ptr->open(journal_name, flags,
 				      &fs->journal_io);
 	}
 	if (retval)
@@ -294,7 +307,7 @@ try_backup_journal:
 
 		maxlen = ext2fs_blocks_count(&jsuper);
 		journal->j_total_len = (maxlen < 1ULL << 32) ? maxlen :
-				    (1ULL << 32) - 1;
+				(1ULL << 32) - 1;
 		start++;
 	}
 
@@ -358,8 +371,9 @@ static errcode_t ext2fs_journal_load(journal_t *journal)
 	struct buffer_head *jbh = journal->j_sb_buffer;
 
 	ll_rw_block(REQ_OP_READ, 0, 1, &jbh);
-	if (jbh->b_err)
+	if (jbh->b_err) {
 		return jbh->b_err;
+	}
 
 	jsb = journal->j_superblock;
 	/* If we don't even have JBD2_MAGIC, we probably have a wrong inode */
@@ -381,8 +395,9 @@ static errcode_t ext2fs_journal_load(journal_t *journal)
 		if (ntohl(jsb->s_nr_users) > 1 &&
 		    uuid_is_null(fs->super->s_journal_uuid))
 			clear_v2_journal_fields(journal);
-		if (ntohl(jsb->s_nr_users) > 1)
+		if (ntohl(jsb->s_nr_users) > 1) {
 			return EXT2_ET_JOURNAL_UNSUPP_VERSION;
+		}
 		break;
 
 	/*
@@ -426,13 +441,15 @@ static errcode_t ext2fs_journal_load(journal_t *journal)
 	/* We have now checked whether we know enough about the journal
 	 * format to be able to proceed safely, so any other checks that
 	 * fail we should attempt to recover from. */
-	if (jsb->s_blocksize != htonl(journal->j_blocksize))
+	if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
 		return EXT2_ET_CORRUPT_JOURNAL_SB;
+	}
 
 	if (ntohl(jsb->s_maxlen) < journal->j_total_len)
 		journal->j_total_len = ntohl(jsb->s_maxlen);
-	else if (ntohl(jsb->s_maxlen) > journal->j_total_len)
+	else if (ntohl(jsb->s_maxlen) > journal->j_total_len) {
 		return EXT2_ET_CORRUPT_JOURNAL_SB;
+	}
 
 	journal->j_tail_sequence = ntohl(jsb->s_sequence);
 	journal->j_transaction_sequence = journal->j_tail_sequence;
@@ -461,12 +478,14 @@ static errcode_t ext2fs_check_ext3_journal(ext2_filsys fs)
 		return 0;
 
 	retval = ext2fs_get_journal(fs, &journal);
-	if (retval)
+	if (retval) {
 		return retval;
+	}
 
 	retval = ext2fs_journal_load(journal);
-	if (retval)
+	if (retval) {
 		goto err;
+	}
 
 	/*
 	 * We want to make the flags consistent here.  We will not leave with
@@ -552,15 +571,17 @@ errcode_t ext2fs_run_ext3_journal(ext2_filsys *fsp)
 {
 	ext2_filsys fs = *fsp;
 	io_manager io_ptr = fs->io->manager;
+	int blocksize = fs->blocksize;
 	errcode_t	retval, recover_retval;
 	io_stats	stats = 0;
 	unsigned long long kbytes_written = 0;
-	char *fsname;
-	int fsflags;
-	int fsblocksize;
+	char *fsname = fs->device_name;
+	int fsflags = fs->flags;
+	int superblock = 0;
 
-	if (!(fs->flags & EXT2_FLAG_RW))
+	if (!(fs->flags & EXT2_FLAG_RW)) {
 		return EXT2_ET_FILE_RO;
+	}
 
 	if (fs->flags & EXT2_FLAG_DIRTY)
 		ext2fs_flush(fs);	/* Force out any modifications */
@@ -578,13 +599,11 @@ errcode_t ext2fs_run_ext3_journal(ext2_filsys *fsp)
 		kbytes_written = stats->bytes_written >> 10;
 
 	ext2fs_mmp_stop(fs);
-	fsname = fs->device_name;
 	fs->device_name = NULL;
-	fsflags = fs->flags;
-	fsblocksize = fs->blocksize;
 	ext2fs_free(fs);
 	*fsp = NULL;
-	retval = ext2fs_open(fsname, fsflags, 0, fsblocksize, io_ptr, fsp);
+	retval = ext2fs_open(fsname, fsflags, superblock, blocksize, io_ptr,
+			     fsp);
 	ext2fs_free_mem(&fsname);
 	if (retval)
 		return retval;
diff --git a/e2fsck/journal.c b/e2fsck/journal.c
index 02251956..f2db63e5 100644
--- a/e2fsck/journal.c
+++ b/e2fsck/journal.c
@@ -42,12 +42,19 @@
  */
 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
 {
-	ext2fs_clear_feature_journal_needs_recovery(ctx->fs->super);
+	ext2_filsys fs = ctx->fs;
+	ext2fs_clear_feature_journal_needs_recovery(fs->super);
 
 	/* if we had an error doing journal recovery, we need a full fsck */
 	if (error)
-		ctx->fs->super->s_state &= ~EXT2_VALID_FS;
-	ext2fs_mark_super_dirty(ctx->fs);
+		fs->super->s_state &= ~EXT2_VALID_FS;
+	/*
+	 * If we replayed the journal by definition the file system
+	 * was mounted since the last time it was checked
+	 */
+	if (fs->super->s_lastcheck >= fs->super->s_mtime)
+		fs->super->s_lastcheck = fs->super->s_mtime - 1;
+	ext2fs_mark_super_dirty(fs);
 }
 
 /*
@@ -294,8 +301,9 @@ static void ex_sort_and_merge(struct extent_list *list)
 
 /* must free blocks that are released */
 static int ext4_modify_extent_list(e2fsck_t ctx, struct extent_list *list,
-					struct ext2fs_extent *ex, int del)
+				   struct ext2fs_extent *ex, int del)
 {
+	ext2_filsys fs = ctx->fs;
 	int ret, offset;
 	unsigned int i;
 	struct ext2fs_extent add_ex = *ex;
@@ -312,7 +320,7 @@ static int ext4_modify_extent_list(e2fsck_t ctx, struct extent_list *list,
 		 * Unmark all the blocks in bb now. All the blocks get marked
 		 * before we exit this function.
 		 */
-		ext2fs_unmark_block_bitmap_range2(ctx->fs->block_map,
+		ext2fs_unmark_block_bitmap_range2(fs->block_map,
 			list->extents[i].e_pblk, list->extents[i].e_len);
 		/* Case 2: Split */
 		if (list->extents[i].e_lblk < add_ex.e_lblk &&
@@ -362,9 +370,9 @@ static int ext4_modify_extent_list(e2fsck_t ctx, struct extent_list *list,
 
 	/* Mark all occupied blocks allocated */
 	for (i = 0; i < list->count; i++)
-		ext2fs_mark_block_bitmap_range2(ctx->fs->block_map,
+		ext2fs_mark_block_bitmap_range2(fs->block_map,
 			list->extents[i].e_pblk, list->extents[i].e_len);
-	ext2fs_mark_bb_dirty(ctx->fs);
+	ext2fs_mark_bb_dirty(fs);
 
 	return 0;
 }
@@ -447,6 +455,7 @@ static inline int tl_to_darg(struct dentry_info_args *darg,
 
 static int ext4_fc_handle_unlink(e2fsck_t ctx, struct ext4_fc_tl *tl, __u8 *val)
 {
+	ext2_filsys fs = ctx->fs;
 	struct dentry_info_args darg;
 	int ret;
 
@@ -454,7 +463,7 @@ static int ext4_fc_handle_unlink(e2fsck_t ctx, struct ext4_fc_tl *tl, __u8 *val)
 	if (ret)
 		return ret;
 	ext4_fc_flush_extents(ctx, darg.ino);
-	ret = errcode_to_errno(ext2fs_unlink(ctx->fs, darg.parent_ino,
+	ret = errcode_to_errno(ext2fs_unlink(fs, darg.parent_ino,
 					     darg.dname, darg.ino, 0));
 	/* It's okay if the above call fails */
 	free(darg.dname);
@@ -539,6 +548,7 @@ static void ext4_fc_replay_fixup_iblocks(struct ext2_inode_large *ondisk_inode,
 
 static int ext4_fc_handle_inode(e2fsck_t ctx, __u8 *val)
 {
+	ext2_filsys fs = ctx->fs;
 	int ino, inode_len = EXT2_GOOD_OLD_INODE_SIZE;
 	struct ext2_inode_large *inode = NULL, *fc_inode = NULL;
 	__le32 fc_ino;
@@ -550,13 +560,13 @@ static int ext4_fc_handle_inode(e2fsck_t ctx, __u8 *val)
 	fc_raw_inode = val + sizeof(fc_ino);
 	ino = le32_to_cpu(fc_ino);
 
-	if (EXT2_INODE_SIZE(ctx->fs->super) > EXT2_GOOD_OLD_INODE_SIZE) {
+	if (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE) {
 		__u16 extra_isize = ext2fs_le16_to_cpu(
 			((struct ext2_inode_large *)fc_raw_inode)->i_extra_isize);
 
 		if ((extra_isize < (sizeof(inode->i_extra_isize) +
 				    sizeof(inode->i_checksum_hi))) ||
-		    (extra_isize > (EXT2_INODE_SIZE(ctx->fs->super) -
+		    (extra_isize > (EXT2_INODE_SIZE(fs->super) -
 				    EXT2_GOOD_OLD_INODE_SIZE))) {
 			err = EFSCORRUPTED;
 			goto out;
@@ -571,33 +581,33 @@ static int ext4_fc_handle_inode(e2fsck_t ctx, __u8 *val)
 		goto out;
 	ext4_fc_flush_extents(ctx, ino);
 
-	err = ext2fs_read_inode_full(ctx->fs, ino, (struct ext2_inode *)inode,
+	err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)inode,
 					inode_len);
 	if (err)
 		goto out;
 	memcpy(fc_inode, fc_raw_inode, inode_len);
 #ifdef WORDS_BIGENDIAN
-	ext2fs_swap_inode_full(ctx->fs, fc_inode, fc_inode, 0, inode_len);
+	ext2fs_swap_inode_full(fs, fc_inode, fc_inode, 0, inode_len);
 #endif
 	memcpy(inode, fc_inode, offsetof(struct ext2_inode_large, i_block));
 	memcpy(&inode->i_generation, &fc_inode->i_generation,
 		inode_len - offsetof(struct ext2_inode_large, i_generation));
 	ext4_fc_replay_fixup_iblocks(inode, fc_inode);
-	err = ext2fs_count_blocks(ctx->fs, ino, EXT2_INODE(inode), &blks);
+	err = ext2fs_count_blocks(fs, ino, EXT2_INODE(inode), &blks);
 	if (err)
 		goto out;
-	ext2fs_iblk_set(ctx->fs, EXT2_INODE(inode), blks);
-	ext2fs_inode_csum_set(ctx->fs, ino, inode);
+	ext2fs_iblk_set(fs, EXT2_INODE(inode), blks);
+	ext2fs_inode_csum_set(fs, ino, inode);
 
-	err = ext2fs_write_inode_full(ctx->fs, ino, (struct ext2_inode *)inode,
+	err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)inode,
 					inode_len);
 	if (err)
 		goto out;
 	if (inode->i_links_count)
-		ext2fs_mark_inode_bitmap2(ctx->fs->inode_map, ino);
+		ext2fs_mark_inode_bitmap2(fs->inode_map, ino);
 	else
-		ext2fs_unmark_inode_bitmap2(ctx->fs->inode_map, ino);
-	ext2fs_mark_ib_dirty(ctx->fs);
+		ext2fs_unmark_inode_bitmap2(fs->inode_map, ino);
+	ext2fs_mark_ib_dirty(fs);
 
 out:
 	ext2fs_free_mem(&inode);
@@ -664,6 +674,7 @@ static int ext4_fc_replay(journal_t *journal, struct buffer_head *bh,
 				enum passtype pass, int off, tid_t expected_tid)
 {
 	e2fsck_t ctx = journal->j_ctx;
+	ext2_filsys fs = ctx->fs;
 	struct e2fsck_fc_replay_state *state = &ctx->fc_replay_state;
 	int ret = JBD2_FC_REPLAY_CONTINUE;
 	struct ext4_fc_tl tl;
@@ -681,23 +692,23 @@ static int ext4_fc_replay(journal_t *journal, struct buffer_head *bh,
 		/* Starting replay phase */
 		state->fc_current_pass = pass;
 		/* We will reset checksums */
-		ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
-		ret = errcode_to_errno(ext2fs_read_bitmaps(ctx->fs));
+		fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
+		ret = errcode_to_errno(ext2fs_read_bitmaps(fs));
 		if (ret) {
 			jbd_debug(1, "Error %d while reading bitmaps\n", ret);
 			return ret;
 		}
-		state->fc_super_state = ctx->fs->super->s_state;
+		state->fc_super_state = fs->super->s_state;
 		/*
 		 * Mark the file system to indicate it contains errors. That's
 		 * because the updates performed by fast commit replay code are
 		 * not atomic and may result in inconsistent file system if it
 		 * crashes before the replay is complete.
 		 */
-		ctx->fs->super->s_state |= EXT2_ERROR_FS;
-		ctx->fs->super->s_state |= EXT4_FC_REPLAY;
-		ext2fs_mark_super_dirty(ctx->fs);
-		ext2fs_flush(ctx->fs);
+		fs->super->s_state |= EXT2_ERROR_FS;
+		fs->super->s_state |= EXT4_FC_REPLAY;
+		ext2fs_mark_super_dirty(fs);
+		ext2fs_flush(fs);
 	}
 
 	start = (__u8 *)bh->b_data;
@@ -748,24 +759,25 @@ replay_done:
 	if (state->fc_current_pass != pass)
 		return JBD2_FC_REPLAY_STOP;
 
-	ext2fs_calculate_summary_stats(ctx->fs, 0 /* update bg also */);
-	ext2fs_write_block_bitmap(ctx->fs);
-	ext2fs_write_inode_bitmap(ctx->fs);
-	ext2fs_mark_super_dirty(ctx->fs);
-	ext2fs_set_gdt_csum(ctx->fs);
-	ctx->fs->super->s_state = state->fc_super_state;
-	ext2fs_flush(ctx->fs);
+	ext2fs_calculate_summary_stats(fs, 0 /* update bg also */);
+	ext2fs_write_block_bitmap(fs);
+	ext2fs_write_inode_bitmap(fs);
+	ext2fs_mark_super_dirty(fs);
+	ext2fs_set_gdt_csum(fs);
+	fs->super->s_state = state->fc_super_state;
+	ext2fs_flush(fs);
 
 	return JBD2_FC_REPLAY_STOP;
 }
 
 static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
 {
+	ext2_filsys fs = ctx->fs;
+	struct problem_context	pctx;
 	struct process_block_struct pb;
-	struct ext2_super_block *sb = ctx->fs->super;
+	struct ext2_super_block *sb = fs->super;
 	struct ext2_super_block jsuper;
-	struct problem_context	pctx;
-	struct buffer_head 	*bh;
+	struct buffer_head	*bh;
 	struct inode		*j_inode = NULL;
 	struct kdev_s		*dev_fs = NULL, *dev_journal;
 	const char		*journal_name = 0;
@@ -773,7 +785,6 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
 	errcode_t		retval = 0;
 	io_manager		io_ptr = 0;
 	unsigned long long	start = 0;
-	int			ret;
 	int			ext_journal = 0;
 	int			tried_backup_jnl = 0;
 
@@ -791,7 +802,7 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
 	}
 	dev_journal = dev_fs+1;
 
-	dev_fs->k_fs = dev_journal->k_fs = ctx->fs;
+	dev_fs->k_fs = dev_journal->k_fs = fs;
 	dev_fs->k_dev = K_DEV_FS;
 	dev_journal->k_dev = K_DEV_JOURNAL;
 
@@ -799,7 +810,7 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
 	journal->j_dev = dev_journal;
 	journal->j_fs_dev = dev_fs;
 	journal->j_inode = NULL;
-	journal->j_blocksize = ctx->fs->blocksize;
+	journal->j_blocksize = fs->blocksize;
 
 	if (uuid_is_null(sb->s_journal_uuid)) {
 		/*
@@ -809,7 +820,7 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
 		 */
 		if (!sb->s_journal_inum ||
 		    (sb->s_journal_inum >
-		     (ctx->fs->group_desc_count * sb->s_inodes_per_group))) {
+		     (fs->group_desc_count * sb->s_inodes_per_group))) {
 			retval = EXT2_ET_BAD_INODE_NUM;
 			goto errout;
 		}
@@ -820,13 +831,13 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
 			goto errout;
 		}
 
-		j_inode->i_fs = ctx->fs;
+		j_inode->i_fs = fs;
 		j_inode->i_ino = sb->s_journal_inum;
 
-		if ((retval = ext2fs_read_inode(ctx->fs,
-						sb->s_journal_inum,
-						&j_inode->i_ext2))) {
-		try_backup_journal:
+		retval = ext2fs_read_inode(fs, sb->s_journal_inum,
+					   &j_inode->i_ext2);
+		if (retval) {
+try_backup_journal:
 			if (sb->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS ||
 			    tried_backup_jnl)
 				goto errout;
@@ -854,16 +865,16 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
 			goto try_backup_journal;
 		}
 		pb.last_block = -1;
-		retval = ext2fs_block_iterate3(ctx->fs, j_inode->i_ino,
+		retval = ext2fs_block_iterate3(fs, j_inode->i_ino,
 					       BLOCK_FLAG_HOLE, 0,
 					       process_journal_block, &pb);
-		if ((pb.last_block + 1) * ctx->fs->blocksize <
+		if ((pb.last_block + 1) * fs->blocksize <
 		    (int) EXT2_I_SIZE(&j_inode->i_ext2)) {
 			retval = EXT2_ET_JOURNAL_TOO_SMALL;
 			goto try_backup_journal;
 		}
-		if (tried_backup_jnl && !(ctx->options & E2F_OPT_READONLY)) {
-			retval = ext2fs_write_inode(ctx->fs, sb->s_journal_inum,
+		if (tried_backup_jnl && (fs->flags & EXT2_FLAG_RW)) {
+			retval = ext2fs_write_inode(fs, sb->s_journal_inum,
 						    &j_inode->i_ext2);
 			if (retval)
 				goto errout;
@@ -873,7 +884,7 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
 			journal->j_blocksize;
 
 #ifdef USE_INODE_IO
-		retval = ext2fs_inode_io_intern2(ctx->fs, sb->s_journal_inum,
+		retval = ext2fs_inode_io_intern2(fs, sb->s_journal_inum,
 						 &j_inode->i_ext2,
 						 &journal_name);
 		if (retval)
@@ -882,11 +893,10 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
 		io_ptr = inode_io_manager;
 #else
 		journal->j_inode = j_inode;
-		ctx->fs->journal_io = ctx->fs->io;
-		if ((ret = jbd2_journal_bmap(journal, 0, &start)) != 0) {
-			retval = (errcode_t) (-1 * ret);
+		fs->journal_io = fs->io;
+		retval = (errcode_t)-jbd2_journal_bmap(journal, 0, &start);
+		if (retval)
 			goto errout;
-		}
 #endif
 	} else {
 		ext_journal = 1;
@@ -929,28 +939,30 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
 
 
 		retval = io_ptr->open(journal_name, flags,
-				      &ctx->fs->journal_io);
+				      &fs->journal_io);
 	}
 	if (retval)
 		goto errout;
 
-	io_channel_set_blksize(ctx->fs->journal_io, ctx->fs->blocksize);
+	io_channel_set_blksize(fs->journal_io, fs->blocksize);
 
 	if (ext_journal) {
 		blk64_t maxlen;
 
-		start = ext2fs_journal_sb_start(ctx->fs->blocksize) - 1;
-		bh = getblk(dev_journal, start, ctx->fs->blocksize);
+		start = ext2fs_journal_sb_start(fs->blocksize) - 1;
+		bh = getblk(dev_journal, start, fs->blocksize);
 		if (!bh) {
 			retval = EXT2_ET_NO_MEMORY;
 			goto errout;
 		}
 		ll_rw_block(REQ_OP_READ, 0, 1, &bh);
-		if ((retval = bh->b_err) != 0) {
+		retval = bh->b_err;
+		if (retval) {
 			brelse(bh);
 			goto errout;
 		}
-		memcpy(&jsuper, start ? bh->b_data :  bh->b_data + SUPERBLOCK_OFFSET,
+		memcpy(&jsuper, start ? bh->b_data :
+				bh->b_data + SUPERBLOCK_OFFSET,
 		       sizeof(jsuper));
 #ifdef WORDS_BIGENDIAN
 		if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
@@ -964,7 +976,7 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
 			goto errout;
 		}
 		/* Make sure the journal UUID is correct */
-		if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid,
+		if (memcmp(jsuper.s_uuid, fs->super->s_journal_uuid,
 			   sizeof(jsuper.s_uuid))) {
 			fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx);
 			retval = EXT2_ET_LOAD_EXT_JOURNAL;
@@ -979,8 +991,8 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
 			void *p;
 
 			p = start ? bh->b_data : bh->b_data + SUPERBLOCK_OFFSET;
-			memcpy(&fsx, ctx->fs, sizeof(fsx));
-			memcpy(&superx, ctx->fs->super, sizeof(superx));
+			memcpy(&fsx, fs, sizeof(fsx));
+			memcpy(&superx, fs->super, sizeof(superx));
 			fsx.super = &superx;
 			ext2fs_set_feature_metadata_csum(fsx.super);
 			if (!ext2fs_superblock_csum_verify(&fsx, p) &&
@@ -993,18 +1005,20 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
 		brelse(bh);
 
 		maxlen = ext2fs_blocks_count(&jsuper);
-		journal->j_total_len = (maxlen < 1ULL << 32) ? maxlen : (1ULL << 32) - 1;
+		journal->j_total_len = (maxlen < 1ULL << 32) ? maxlen :
+				(1ULL << 32) - 1;
 		start++;
 	}
 
-	if (!(bh = getblk(dev_journal, start, journal->j_blocksize))) {
+	bh = getblk(dev_journal, start, journal->j_blocksize);
+	if (!bh) {
 		retval = EXT2_ET_NO_MEMORY;
 		goto errout;
 	}
 
 	journal->j_sb_buffer = bh;
 	journal->j_superblock = (journal_superblock_t *)bh->b_data;
-	if (ext2fs_has_feature_fast_commit(ctx->fs->super))
+	if (ext2fs_has_feature_fast_commit(fs->super))
 		journal->j_fc_replay_callback = ext4_fc_replay;
 	else
 		journal->j_fc_replay_callback = NULL;
@@ -1032,9 +1046,10 @@ errout:
 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
 					      struct problem_context *pctx)
 {
-	struct ext2_super_block *sb = ctx->fs->super;
-	int recover = ext2fs_has_feature_journal_needs_recovery(ctx->fs->super);
-	int has_journal = ext2fs_has_feature_journal(ctx->fs->super);
+	ext2_filsys fs = ctx->fs;
+	struct ext2_super_block *sb = fs->super;
+	int recover = ext2fs_has_feature_journal_needs_recovery(fs->super);
+	int has_journal = ext2fs_has_feature_journal(fs->super);
 
 	if (has_journal || sb->s_journal_inum) {
 		/* The journal inode is bogus, remove and force full fsck */
@@ -1046,7 +1061,7 @@ static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
 			sb->s_journal_inum = 0;
 			memset(sb->s_jnl_blocks, 0, sizeof(sb->s_jnl_blocks));
 			ctx->flags |= E2F_FLAG_JOURNAL_INODE;
-			ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
+			fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
 			e2fsck_clear_recover(ctx, 1);
 			return 0;
 		}
@@ -1062,8 +1077,10 @@ static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
 }
 
 #define V1_SB_SIZE	0x0024
-static void clear_v2_journal_fields(e2fsck_t ctx, journal_t *journal)
+static void clear_v2_journal_fields(journal_t *journal)
 {
+	e2fsck_t ctx = journal->j_ctx; 
+	ext2_filsys fs = ctx->fs;
 	struct problem_context pctx;
 
 	clear_problem_context(&pctx);
@@ -1073,13 +1090,15 @@ static void clear_v2_journal_fields(e2fsck_t ctx, journal_t *journal)
 
 	ctx->flags |= E2F_FLAG_PROBLEMS_FIXED;
 	memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
-	       ctx->fs->blocksize-V1_SB_SIZE);
+	       fs->blocksize-V1_SB_SIZE);
 	mark_buffer_dirty(journal->j_sb_buffer);
 }
 
 
-static errcode_t e2fsck_journal_load(e2fsck_t ctx, journal_t *journal)
+static errcode_t e2fsck_journal_load(journal_t *journal)
 {
+	e2fsck_t ctx = journal->j_ctx; 
+	ext2_filsys fs = ctx->fs;
 	journal_superblock_t *jsb;
 	struct buffer_head *jbh = journal->j_sb_buffer;
 	struct problem_context pctx;
@@ -1105,14 +1124,14 @@ static errcode_t e2fsck_journal_load(e2fsck_t ctx, journal_t *journal)
 		    jsb->s_feature_incompat ||
 		    jsb->s_feature_ro_compat ||
 		    jsb->s_nr_users)
-			clear_v2_journal_fields(ctx, journal);
+			clear_v2_journal_fields(journal);
 		break;
 
 	case JBD2_SUPERBLOCK_V2:
 		journal->j_format_version = 2;
 		if (ntohl(jsb->s_nr_users) > 1 &&
-		    uuid_is_null(ctx->fs->super->s_journal_uuid))
-			clear_v2_journal_fields(ctx, journal);
+		    uuid_is_null(fs->super->s_journal_uuid))
+			clear_v2_journal_fields(journal);
 		if (ntohl(jsb->s_nr_users) > 1) {
 			fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
 			return EXT2_ET_JOURNAL_UNSUPP_VERSION;
@@ -1199,9 +1218,11 @@ static errcode_t e2fsck_journal_load(e2fsck_t ctx, journal_t *journal)
 	return 0;
 }
 
-static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
+static void e2fsck_journal_reset_super(journal_superblock_t *jsb,
 				       journal_t *journal)
 {
+	e2fsck_t ctx = journal->j_ctx; 
+	ext2_filsys fs = ctx->fs;
 	char *p;
 	union {
 		uuid_t uuid;
@@ -1223,9 +1244,9 @@ static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
 	/* Zero out everything else beyond the superblock header */
 
 	p = ((char *) jsb) + sizeof(journal_header_t);
-	memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
+	memset (p, 0, fs->blocksize-sizeof(journal_header_t));
 
-	jsb->s_blocksize = htonl(ctx->fs->blocksize);
+	jsb->s_blocksize = htonl(fs->blocksize);
 	jsb->s_maxlen = htonl(journal->j_total_len);
 	jsb->s_first = htonl(1);
 
@@ -1248,12 +1269,13 @@ static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
 						  journal_t *journal,
 						  struct problem_context *pctx)
 {
-	struct ext2_super_block *sb = ctx->fs->super;
-	int recover = ext2fs_has_feature_journal_needs_recovery(ctx->fs->super);
+	ext2_filsys fs = ctx->fs;
+	struct ext2_super_block *sb = fs->super;
+	int recover = ext2fs_has_feature_journal_needs_recovery(fs->super);
 
 	if (ext2fs_has_feature_journal(sb)) {
 		if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
-			e2fsck_journal_reset_super(ctx, journal->j_superblock,
+			e2fsck_journal_reset_super(journal->j_superblock,
 						   journal);
 			journal->j_transaction_sequence = 1;
 			e2fsck_clear_recover(ctx, recover);
@@ -1272,9 +1294,10 @@ static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
  */
 errcode_t e2fsck_check_ext3_journal(e2fsck_t ctx)
 {
-	struct ext2_super_block *sb = ctx->fs->super;
+	ext2_filsys fs = ctx->fs;
+	struct ext2_super_block *sb = fs->super;
 	journal_t *journal;
-	int recover = ext2fs_has_feature_journal_needs_recovery(ctx->fs->super);
+	int recover = ext2fs_has_feature_journal_needs_recovery(fs->super);
 	struct problem_context pctx;
 	problem_t problem;
 	int reset = 0, force_fsck = 0;
@@ -1284,7 +1307,7 @@ errcode_t e2fsck_check_ext3_journal(e2fsck_t ctx)
 	if (!ext2fs_has_feature_journal(sb) &&
 	    !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
 	    uuid_is_null(sb->s_journal_uuid))
- 		return 0;
+		return 0;
 
 	clear_problem_context(&pctx);
 	pctx.num = sb->s_journal_inum;
@@ -1299,7 +1322,7 @@ errcode_t e2fsck_check_ext3_journal(e2fsck_t ctx)
 		return retval;
 	}
 
-	retval = e2fsck_journal_load(ctx, journal);
+	retval = e2fsck_journal_load(journal);
 	if (retval) {
 		if ((retval == EXT2_ET_CORRUPT_JOURNAL_SB) ||
 		    ((retval == EXT2_ET_UNSUPP_FEATURE) &&
@@ -1312,7 +1335,7 @@ errcode_t e2fsck_check_ext3_journal(e2fsck_t ctx)
 		    (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
 			retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
 								  &pctx);
-		ext2fs_journal_release(ctx->fs, journal, 0, 1);
+		ext2fs_journal_release(fs, journal, 0, 1);
 		return retval;
 	}
 
@@ -1342,8 +1365,8 @@ no_has_journal:
 			e2fsck_clear_recover(ctx, force_fsck);
 		} else if (!(ctx->options & E2F_OPT_READONLY)) {
 			ext2fs_set_feature_journal(sb);
-			ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
-			ext2fs_mark_super_dirty(ctx->fs);
+			fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
+			ext2fs_mark_super_dirty(fs);
 		}
 	}
 
@@ -1359,12 +1382,12 @@ no_has_journal:
 		if (fix_problem(ctx, problem, &pctx)) {
 			ctx->options |= E2F_OPT_FORCE;
 			ext2fs_set_feature_journal_needs_recovery(sb);
-			ext2fs_mark_super_dirty(ctx->fs);
+			ext2fs_mark_super_dirty(fs);
 		} else if (fix_problem(ctx,
 				       PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
 			reset = 1;
 			sb->s_state &= ~EXT2_VALID_FS;
-			ext2fs_mark_super_dirty(ctx->fs);
+			ext2fs_mark_super_dirty(fs);
 		}
 		/*
 		 * If the user answers no to the above question, we
@@ -1386,19 +1409,20 @@ no_has_journal:
 	 */
 	if (!ext2fs_has_feature_journal_needs_recovery(sb) &&
 	    journal->j_superblock->s_errno) {
-		ctx->fs->super->s_state |= EXT2_ERROR_FS;
-		ext2fs_mark_super_dirty(ctx->fs);
+		fs->super->s_state |= EXT2_ERROR_FS;
+		ext2fs_mark_super_dirty(fs);
 		journal->j_superblock->s_errno = 0;
 		ext2fs_journal_sb_csum_set(journal, journal->j_superblock);
 		mark_buffer_dirty(journal->j_sb_buffer);
 	}
 
-	ext2fs_journal_release(ctx->fs, journal, reset, 0);
+	ext2fs_journal_release(fs, journal, reset, 0);
 	return retval;
 }
 
 static errcode_t recover_ext3_journal(e2fsck_t ctx)
 {
+	ext2_filsys fs = ctx->fs;
 	struct problem_context	pctx;
 	journal_t *journal;
 	errcode_t retval;
@@ -1417,7 +1441,7 @@ static errcode_t recover_ext3_journal(e2fsck_t ctx)
 	if (retval)
 		return retval;
 
-	retval = e2fsck_journal_load(ctx, journal);
+	retval = e2fsck_journal_load(journal);
 	if (retval)
 		goto errout;
 
@@ -1442,17 +1466,21 @@ errout:
 	jbd2_journal_destroy_revoke(journal);
 	jbd2_journal_destroy_revoke_record_cache();
 	jbd2_journal_destroy_revoke_table_cache();
-	ext2fs_journal_release(ctx->fs, journal, 1, 0);
+	ext2fs_journal_release(fs, journal, 1, 0);
 	return retval;
 }
 
 errcode_t e2fsck_run_ext3_journal(e2fsck_t ctx)
 {
-	io_manager io_ptr = ctx->fs->io->manager;
-	int blocksize = ctx->fs->blocksize;
+	ext2_filsys fs = ctx->fs;
+	io_manager io_ptr = fs->io->manager;
+	int blocksize = fs->blocksize;
 	errcode_t	retval, recover_retval;
 	io_stats	stats = 0;
 	unsigned long long kbytes_written = 0;
+	char *fsname = ctx->filesystem_name;
+	int fsflags = ctx->openfs_flags;
+	int superblock = ctx->superblock;
 
 	printf(_("%s: recovering journal\n"), ctx->device_name);
 	if (ctx->options & E2F_OPT_READONLY) {
@@ -1461,8 +1489,8 @@ errcode_t e2fsck_run_ext3_journal(e2fsck_t ctx)
 		return EXT2_ET_FILE_RO;
 	}
 
-	if (ctx->fs->flags & EXT2_FLAG_DIRTY)
-		ext2fs_flush(ctx->fs);	/* Force out any modifications */
+	if (fs->flags & EXT2_FLAG_DIRTY)
+		ext2fs_flush(fs);	/* Force out any modifications */
 
 	recover_retval = recover_ext3_journal(ctx);
 
@@ -1470,16 +1498,15 @@ errcode_t e2fsck_run_ext3_journal(e2fsck_t ctx)
 	 * Reload the filesystem context to get up-to-date data from disk
 	 * because journal recovery will change the filesystem under us.
 	 */
-	if (ctx->fs->super->s_kbytes_written &&
-	    ctx->fs->io->manager->get_stats)
-		ctx->fs->io->manager->get_stats(ctx->fs->io, &stats);
+	if (fs->super->s_kbytes_written &&
+	    fs->io->manager->get_stats)
+		fs->io->manager->get_stats(fs->io, &stats);
 	if (stats && stats->bytes_written)
 		kbytes_written = stats->bytes_written >> 10;
 
-	ext2fs_mmp_stop(ctx->fs);
-	ext2fs_free(ctx->fs);
-	retval = ext2fs_open(ctx->filesystem_name, ctx->openfs_flags,
-			     ctx->superblock, blocksize, io_ptr,
+	ext2fs_mmp_stop(fs);
+	ext2fs_free(fs);
+	retval = ext2fs_open(fsname, fsflags, superblock, blocksize, io_ptr,
 			     &ctx->fs);
 	if (retval) {
 		com_err(ctx->program_name, retval,
@@ -1487,10 +1514,11 @@ errcode_t e2fsck_run_ext3_journal(e2fsck_t ctx)
 			ctx->device_name);
 		fatal_error(ctx, 0);
 	}
-	ctx->fs->priv_data = ctx;
-	ctx->fs->now = ctx->now;
-	ctx->fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
-	ctx->fs->super->s_kbytes_written += kbytes_written;
+	fs = ctx->fs;
+	fs->priv_data = ctx;
+	fs->now = ctx->now;
+	fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
+	fs->super->s_kbytes_written += kbytes_written;
 
 	/* Set the superblock flags */
 	e2fsck_clear_recover(ctx, recover_retval != 0);
@@ -1512,10 +1540,10 @@ static const char * const journal_names[] = {
 
 void e2fsck_move_ext3_journal(e2fsck_t ctx)
 {
-	struct ext2_super_block *sb = ctx->fs->super;
+	ext2_filsys fs = ctx->fs;
+	struct ext2_super_block *sb = fs->super;
 	struct problem_context	pctx;
 	struct ext2_inode 	inode;
-	ext2_filsys		fs = ctx->fs;
 	ext2_ino_t		ino;
 	errcode_t		retval;
 	const char * const *	cpp;
@@ -1637,7 +1665,8 @@ err_out:
  */
 int e2fsck_fix_ext3_journal_hint(e2fsck_t ctx)
 {
-	struct ext2_super_block *sb = ctx->fs->super;
+	ext2_filsys fs = ctx->fs;
+	struct ext2_super_block *sb = fs->super;
 	struct problem_context pctx;
 	char uuid[37], *journal_name;
 	struct stat st;
@@ -1661,7 +1690,7 @@ int e2fsck_fix_ext3_journal_hint(e2fsck_t ctx)
 		pctx.num = st.st_rdev;
 		if (fix_problem(ctx, PR_0_EXTERNAL_JOURNAL_HINT, &pctx)) {
 			sb->s_journal_dev = st.st_rdev;
-			ext2fs_mark_super_dirty(ctx->fs);
+			ext2fs_mark_super_dirty(fs);
 		}
 	}
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-09-19  9:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-19  9:09 [PATCH 5/7] [v2] lib: deduplicate buffer_head/kernel emulation Andreas Dilger
2023-09-19  9:09 ` [PATCH 6/7] misc: deduplicate log2/log10 functions Andreas Dilger
2023-09-19  9:09 ` [PATCH 7/7] lib: make journal.c more consistent Andreas Dilger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).