diff mbox

[3/4,v3] tune2fs: update journal super block when changing UUID for fs.

Message ID 1406533405-6899-4-git-send-email-a3at.mail@gmail.com
State Accepted, archived
Headers show

Commit Message

Azat Khuzhin July 28, 2014, 7:43 a.m. UTC
Using -U option you can change the UUID for fs, however it will not work
for journal device, since it have a copy of this UUID inside jsb (i.e.
journal super block). So copy UUID on change into that block.

Here is the initial thread:
http://comments.gmane.org/gmane.comp.file-systems.ext4/44532

You can reproduce this by executing following commands:
$ fallocate -l100M /tmp/dev
$ fallocate -l100M /tmp/journal
$ sudo /sbin/losetup /dev/loop1 /tmp/dev
$ sudo /sbin/losetup /dev/loop0 /tmp/journal
$ mke2fs -O journal_dev /tmp/journal
$ tune2fs -U da1f2ed0-60f6-aaaa-92fd-738701418523 /tmp/journal
$ sudo mke2fs -t ext4 -J device=/dev/loop0 /dev/loop1
$ dumpe2fs -h /tmp/dev | fgrep UUID
dumpe2fs 1.43-WIP (18-May-2014)
Filesystem UUID:          8a776be9-12eb-411f-8e88-b873575ecfb6
Journal UUID:             e3d02151-e776-4865-af25-aecb7291e8e5
$ sudo e2fsck /dev/vdc
e2fsck 1.43-WIP (18-May-2014)
External journal does not support this filesystem

/dev/loop1: ********** WARNING: Filesystem still has errors **********

Reported-by: Chin Tzung Cheng <chintzung@gmail.com>
Signed-off-by: Azat Khuzhin <a3at.mail@gmail.com>
Reviewed-by: Andreas Dilger <adilger@dilger.ca>
---
 misc/tune2fs.c | 74 ++++++++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 56 insertions(+), 18 deletions(-)

Comments

Eric Sandeen July 31, 2014, 3:16 p.m. UTC | #1
On 7/28/14, 2:43 AM, Azat Khuzhin wrote:
> Using -U option you can change the UUID for fs, however it will not work
> for journal device, since it have a copy of this UUID inside jsb (i.e.
> journal super block). So copy UUID on change into that block.
> 
> Here is the initial thread:
> http://comments.gmane.org/gmane.comp.file-systems.ext4/44532
> 
> You can reproduce this by executing following commands:
> $ fallocate -l100M /tmp/dev
> $ fallocate -l100M /tmp/journal
> $ sudo /sbin/losetup /dev/loop1 /tmp/dev
> $ sudo /sbin/losetup /dev/loop0 /tmp/journal
> $ mke2fs -O journal_dev /tmp/journal
> $ tune2fs -U da1f2ed0-60f6-aaaa-92fd-738701418523 /tmp/journal
> $ sudo mke2fs -t ext4 -J device=/dev/loop0 /dev/loop1
> $ dumpe2fs -h /tmp/dev | fgrep UUID
> dumpe2fs 1.43-WIP (18-May-2014)
> Filesystem UUID:          8a776be9-12eb-411f-8e88-b873575ecfb6
> Journal UUID:             e3d02151-e776-4865-af25-aecb7291e8e5
> $ sudo e2fsck /dev/vdc
> e2fsck 1.43-WIP (18-May-2014)
> External journal does not support this filesystem
> 
> /dev/loop1: ********** WARNING: Filesystem still has errors **********
> 
> Reported-by: Chin Tzung Cheng <chintzung@gmail.com>
> Signed-off-by: Azat Khuzhin <a3at.mail@gmail.com>
> Reviewed-by: Andreas Dilger <adilger@dilger.ca>
> ---
>  misc/tune2fs.c | 74 ++++++++++++++++++++++++++++++++++++++++++++--------------
>  1 file changed, 56 insertions(+), 18 deletions(-)
> 
> diff --git a/misc/tune2fs.c b/misc/tune2fs.c
> index 1df655a..74e57ae 100644
> --- a/misc/tune2fs.c
> +++ b/misc/tune2fs.c
> @@ -175,6 +175,38 @@ static __u32 clear_ok_features[3] = {
>  		EXT4_FEATURE_RO_COMPAT_GDT_CSUM
>  };
>  
> +/**
> + * Try to get journal super block if any
> + */
> +static int get_journal_sb(ext2_filsys jfs, char buf[SUPERBLOCK_SIZE])
> +{
> +	int retval;
> +	int start;
> +	journal_superblock_t *jsb;
> +
> +	if (!(jfs->super->s_feature_incompat &
> +	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
> +		return EXT2_ET_UNSUPP_FEATURE;
> +	}
> +
> +	/* Get the journal superblock */
> +	if ((retval = io_channel_read_blk64(jfs->io,
> +	    ext2fs_journal_sb_start(jfs->blocksize), -SUPERBLOCK_SIZE, buf))) {
> +		com_err(program_name, retval, "%s",
> +		_("while reading journal superblock"));
> +		return retval;
> +	}
> +
> +	jsb = (journal_superblock_t *) buf;
> +	if ((jsb->s_header.h_magic != (unsigned)ntohl(JFS_MAGIC_NUMBER)) ||
> +	    (jsb->s_header.h_blocktype != (unsigned)ntohl(JFS_SUPERBLOCK_V2))) {
> +		fputs(_("Journal superblock not found!\n"), stderr);
> +		return EXT2_ET_BAD_MAGIC;
> +	}
> +
> +	return 0;
> +}
> +
>  /*
>   * Remove an external journal from the filesystem
>   */
> @@ -218,29 +250,15 @@ static int remove_journal_device(ext2_filsys fs)
>  			_("while trying to open external journal"));
>  		goto no_valid_journal;
>  	}
> -	if (!(jfs->super->s_feature_incompat &
> -	      EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
> -		fprintf(stderr, _("%s is not a journal device.\n"),
> -			journal_path);
> -		goto no_valid_journal;
> -	}
>  
> -	start = ext2fs_journal_sb_start(fs->blocksize);

This is already committed (which is why Coverity whined about it), but:
removing this assignment causes an uninitialized use of "start" later
in this function:

        /* Write back the journal superblock */
        if ((retval = io_channel_write_blk64(jfs->io, start,
            -SUPERBLOCK_SIZE, buf)))

-Eric

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

Patch

diff --git a/misc/tune2fs.c b/misc/tune2fs.c
index 1df655a..74e57ae 100644
--- a/misc/tune2fs.c
+++ b/misc/tune2fs.c
@@ -175,6 +175,38 @@  static __u32 clear_ok_features[3] = {
 		EXT4_FEATURE_RO_COMPAT_GDT_CSUM
 };
 
+/**
+ * Try to get journal super block if any
+ */
+static int get_journal_sb(ext2_filsys jfs, char buf[SUPERBLOCK_SIZE])
+{
+	int retval;
+	int start;
+	journal_superblock_t *jsb;
+
+	if (!(jfs->super->s_feature_incompat &
+	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
+		return EXT2_ET_UNSUPP_FEATURE;
+	}
+
+	/* Get the journal superblock */
+	if ((retval = io_channel_read_blk64(jfs->io,
+	    ext2fs_journal_sb_start(jfs->blocksize), -SUPERBLOCK_SIZE, buf))) {
+		com_err(program_name, retval, "%s",
+		_("while reading journal superblock"));
+		return retval;
+	}
+
+	jsb = (journal_superblock_t *) buf;
+	if ((jsb->s_header.h_magic != (unsigned)ntohl(JFS_MAGIC_NUMBER)) ||
+	    (jsb->s_header.h_blocktype != (unsigned)ntohl(JFS_SUPERBLOCK_V2))) {
+		fputs(_("Journal superblock not found!\n"), stderr);
+		return EXT2_ET_BAD_MAGIC;
+	}
+
+	return 0;
+}
+
 /*
  * Remove an external journal from the filesystem
  */
@@ -218,29 +250,15 @@  static int remove_journal_device(ext2_filsys fs)
 			_("while trying to open external journal"));
 		goto no_valid_journal;
 	}
-	if (!(jfs->super->s_feature_incompat &
-	      EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
-		fprintf(stderr, _("%s is not a journal device.\n"),
-			journal_path);
-		goto no_valid_journal;
-	}
 
-	start = ext2fs_journal_sb_start(fs->blocksize);
-	/* Get the journal superblock */
-	if ((retval = io_channel_read_blk64(jfs->io, start,
-	    -SUPERBLOCK_SIZE, buf))) {
-		com_err(program_name, retval, "%s",
-			_("while reading journal superblock"));
+	if ((retval = get_journal_sb(jfs, buf))) {
+		if (retval == EXT2_ET_UNSUPP_FEATURE)
+			fprintf(stderr, _("%s is not a journal device.\n"),
+				journal_path);
 		goto no_valid_journal;
 	}
 
 	jsb = (journal_superblock_t *) buf;
-	if ((jsb->s_header.h_magic != (unsigned)ntohl(JFS_MAGIC_NUMBER)) ||
-	    (jsb->s_header.h_blocktype != (unsigned)ntohl(JFS_SUPERBLOCK_V2))) {
-		fputs(_("Journal superblock not found!\n"), stderr);
-		goto no_valid_journal;
-	}
-
 	/* Find the filesystem UUID */
 	nr_users = ntohl(jsb->s_nr_users);
 	for (i = 0; i < nr_users; i++) {
@@ -2184,6 +2202,7 @@  retry_open:
 	if (U_flag) {
 		int set_csum = 0;
 		dgrp_t i;
+		char buf[SUPERBLOCK_SIZE];
 
 		if (sb->s_feature_ro_compat &
 		    EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
@@ -2229,6 +2248,25 @@  retry_open:
 				ext2fs_group_desc_csum_set(fs, i);
 			fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
 		}
+
+		/* If this is a journal dev, we need to copy UUID into jsb */
+		if (!(rc = get_journal_sb(fs, buf))) {
+			journal_superblock_t *jsb;
+
+			jsb = (journal_superblock_t *) buf;
+			fputs(_("Need to update journal superblock.\n"), stdout);
+			memcpy(jsb->s_uuid, sb->s_uuid, sizeof(sb->s_uuid));
+
+			/* Writeback the journal superblock */
+			if ((rc = io_channel_write_blk64(fs->io,
+				ext2fs_journal_sb_start(fs->blocksize),
+					-SUPERBLOCK_SIZE, buf)))
+				goto closefs;
+		} else if (rc != EXT2_ET_UNSUPP_FEATURE)
+			goto closefs;
+		else
+			rc = 0; /** Reset rc to avoid ext2fs_mmp_stop() */
+
 		ext2fs_mark_super_dirty(fs);
 	}
 	if (I_flag) {