Whamcloud - gitweb
debugfs: add better error checking when printing extended attributes
[tools/e2fsprogs.git] / misc / tune2fs.c
index 5feb25b..b65dab9 100644 (file)
@@ -26,7 +26,6 @@
  */
 
 #define _XOPEN_SOURCE 600 /* for inclusion of strptime() */
-#define _BSD_SOURCE /* for inclusion of strcasecmp() */
 #include "config.h"
 #include <fcntl.h>
 #include <grp.h>
@@ -41,6 +40,11 @@ extern int optind;
 #ifdef HAVE_STDLIB_H
 #include <stdlib.h>
 #endif
+#ifdef HAVE_STRINGS_H
+#include <strings.h>   /* for strcasecmp() */
+#else
+#define _BSD_SOURCE    /* for inclusion of strcasecmp() via <string.h> */
+#endif
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
@@ -56,7 +60,7 @@ extern int optind;
 #include "jfs_user.h"
 #include "util.h"
 #include "blkid/blkid.h"
-#include "quota/mkquota.h"
+#include "quota/quotaio.h"
 
 #include "../version.h"
 #include "nls-enable.h"
@@ -94,6 +98,7 @@ static int usrquota, grpquota;
 
 int journal_size, journal_flags;
 char *journal_device;
+static blk64_t journal_location = ~0LL;
 
 static struct list_head blk_move_list;
 
@@ -121,6 +126,9 @@ static void usage(void)
                  "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
                  "[-L volume_label]\n"
                  "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
+#ifdef CONFIG_QUOTA
+                 "\t[-Q quota_options]\n"
+#endif
                  "\t[-E extended-option[,...]] [-T last_check_time] "
                  "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name);
        exit(1);
@@ -167,6 +175,50 @@ 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;
+}
+
+static __u8 *journal_user(__u8 uuid[UUID_SIZE], __u8 s_users[JFS_USERS_SIZE],
+                         int nr_users)
+{
+       int i;
+       for (i = 0; i < nr_users; i++) {
+               if (memcmp(uuid, &s_users[i * UUID_SIZE], UUID_SIZE) == 0)
+                       return &s_users[i * UUID_SIZE];
+       }
+
+       return NULL;
+}
+
 /*
  * Remove an external journal from the filesystem
  */
@@ -174,7 +226,7 @@ static int remove_journal_device(ext2_filsys fs)
 {
        char            *journal_path;
        ext2_filsys     jfs;
-       char            buf[1024];
+       char            buf[SUPERBLOCK_SIZE];
        journal_superblock_t    *jsb;
        int             i, nr_users;
        errcode_t       retval;
@@ -205,38 +257,23 @@ static int remove_journal_device(ext2_filsys fs)
                             EXT2_FLAG_JOURNAL_DEV_OK, 0,
                             fs->blocksize, io_ptr, &jfs);
        if (retval) {
-               com_err(program_name, retval,
+               com_err(program_name, retval, "%s",
                        _("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;
-       }
 
-       /* Get the journal superblock */
-       if ((retval = io_channel_read_blk64(jfs->io, 1, -1024, buf))) {
-               com_err(program_name, retval,
-                       _("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++) {
-               if (memcmp(fs->super->s_uuid, &jsb->s_users[i * 16], 16) == 0)
-                       break;
-       }
-       if (i >= nr_users) {
+
+       if (!journal_user(fs->super->s_uuid, jsb->s_users, nr_users)) {
                fputs(_("Filesystem's UUID not found on journal device.\n"),
                      stderr);
                commit_remove_journal = 1;
@@ -248,7 +285,10 @@ static int remove_journal_device(ext2_filsys fs)
        jsb->s_nr_users = htonl(nr_users);
 
        /* Write back the journal superblock */
-       if ((retval = io_channel_write_blk64(jfs->io, 1, -1024, buf))) {
+       retval = io_channel_write_blk64(jfs->io,
+                                       ext2fs_journal_sb_start(fs->blocksize),
+                                       -SUPERBLOCK_SIZE, buf);
+       if (retval) {
                com_err(program_name, retval,
                        "while writing journal superblock.");
                goto no_valid_journal;
@@ -302,14 +342,14 @@ static errcode_t remove_journal_inode(ext2_filsys fs)
 
        retval = ext2fs_read_inode(fs, ino,  &inode);
        if (retval) {
-               com_err(program_name, retval,
+               com_err(program_name, retval, "%s",
                        _("while reading journal inode"));
                return retval;
        }
        if (ino == EXT2_JOURNAL_INO) {
                retval = ext2fs_read_bitmaps(fs);
                if (retval) {
-                       com_err(program_name, retval,
+                       com_err(program_name, retval, "%s",
                                _("while reading bitmaps"));
                        return retval;
                }
@@ -317,7 +357,7 @@ static errcode_t remove_journal_inode(ext2_filsys fs)
                                               BLOCK_FLAG_READ_ONLY, NULL,
                                               release_blocks_proc, NULL);
                if (retval) {
-                       com_err(program_name, retval,
+                       com_err(program_name, retval, "%s",
                                _("while clearing journal inode"));
                        return retval;
                }
@@ -328,7 +368,7 @@ static errcode_t remove_journal_inode(ext2_filsys fs)
                inode.i_flags &= ~EXT2_IMMUTABLE_FL;
        retval = ext2fs_write_inode(fs, ino, &inode);
        if (retval) {
-               com_err(program_name, retval,
+               com_err(program_name, retval, "%s",
                        _("while writing journal inode"));
                return retval;
        }
@@ -355,6 +395,16 @@ static int update_mntopts(ext2_filsys fs, char *mntopts)
        return 0;
 }
 
+static int check_fsck_needed(ext2_filsys fs)
+{
+       if (fs->super->s_state & EXT2_VALID_FS)
+               return 0;
+       printf("\n%s\n", _(please_fsck));
+       if (mount_flags & EXT2_MF_READONLY)
+               printf("%s", _("(and reboot afterwards!)\n"));
+       return 1;
+}
+
 static void request_fsck_afterwards(ext2_filsys fs)
 {
        static int requested = 0;
@@ -364,7 +414,7 @@ static void request_fsck_afterwards(ext2_filsys fs)
        fs->super->s_state &= ~EXT2_VALID_FS;
        printf("\n%s\n", _(please_fsck));
        if (mount_flags & EXT2_MF_READONLY)
-               printf(_("(and reboot afterwards!)\n"));
+               printf("%s", _("(and reboot afterwards!)\n"));
 }
 
 /*
@@ -419,8 +469,9 @@ static int update_feature_set(ext2_filsys fs, char *features)
                                "read-only.\n"), stderr);
                        return 1;
                }
-               if (sb->s_feature_incompat &
-                   EXT3_FEATURE_INCOMPAT_RECOVER) {
+               if ((sb->s_feature_incompat &
+                   EXT3_FEATURE_INCOMPAT_RECOVER) &&
+                   f_flag < 2) {
                        fputs(_("The needs_recovery flag is set.  "
                                "Please run e2fsck before clearing\n"
                                "the has_journal flag.\n"), stderr);
@@ -435,6 +486,19 @@ static int update_feature_set(ext2_filsys fs, char *features)
                                return 1;
                }
        }
+
+       if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
+               EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
+               if (sb->s_feature_incompat &
+                       EXT2_FEATURE_INCOMPAT_META_BG) {
+                       fputs(_("Setting filesystem feature 'sparse_super' "
+                               "not supported\nfor filesystems with "
+                               "the meta_bg feature enabled.\n"),
+                               stderr);
+                       return 1;
+               }
+       }
+
        if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
                int error;
 
@@ -488,7 +552,7 @@ static int update_feature_set(ext2_filsys fs, char *features)
                                         "match. expected: %x, actual: %x\n"),
                                         EXT4_MMP_MAGIC, mmp_cmp->mmp_magic);
                        else
-                               com_err(program_name, error,
+                               com_err(program_name, error, "%s",
                                        _("while reading MMP block."));
                        goto mmp_error;
                }
@@ -641,7 +705,9 @@ static int add_journal(ext2_filsys fs)
                goto err;
        }
        if (journal_device) {
-               check_plausibility(journal_device);
+               if (!check_plausibility(journal_device, CHECK_BLOCK_DEV,
+                                       NULL))
+                       proceed_question(-1);
                check_mount(journal_device, 0, _("journal"));
 #ifdef CONFIG_TESTIO_DEBUG
                if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
@@ -664,7 +730,7 @@ static int add_journal(ext2_filsys fs)
                fflush(stdout);
 
                retval = ext2fs_add_journal_device(fs, jfs);
-               ext2fs_close(jfs);
+               ext2fs_close_free(&jfs);
                if (retval) {
                        com_err(program_name, retval,
                                _("while adding filesystem to journal on %s"),
@@ -677,11 +743,16 @@ static int add_journal(ext2_filsys fs)
                fflush(stdout);
                journal_blocks = figure_journal_size(journal_size, fs);
 
-               retval = ext2fs_add_journal_inode(fs, journal_blocks,
-                                                 journal_flags);
+               if (journal_location_string)
+                       journal_location =
+                               parse_num_blocks2(journal_location_string,
+                                                 fs->super->s_log_block_size);
+               retval = ext2fs_add_journal_inode2(fs, journal_blocks,
+                                                  journal_location,
+                                                  journal_flags);
                if (retval) {
                        fprintf(stderr, "\n");
-                       com_err(program_name, retval,
+                       com_err(program_name, retval, "%s",
                                _("\n\twhile trying to create journal file"));
                        return retval;
                } else
@@ -737,10 +808,6 @@ static void handle_quota_options(ext2_filsys fs)
        quota_release_context(&qctx);
 
        if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
-               fprintf(stderr, _("\nWarning: the quota feature is still "
-                                 "under development\n"
-                                 "See https://ext4.wiki.kernel.org/"
-                                 "index.php/Quota for more information\n\n"));
                fs->super->s_feature_ro_compat |= EXT4_FEATURE_RO_COMPAT_QUOTA;
                ext2fs_mark_super_dirty(fs);
        } else if (!fs->super->s_usr_quota_inum &&
@@ -752,6 +819,7 @@ static void handle_quota_options(ext2_filsys fs)
        return;
 }
 
+#ifdef CONFIG_QUOTA
 static void parse_quota_opts(const char *opts)
 {
        char    *buf, *token, *next, *p;
@@ -794,8 +862,7 @@ static void parse_quota_opts(const char *opts)
        }
        free(buf);
 }
-
-
+#endif
 
 static void parse_e2label_options(int argc, char ** argv)
 {
@@ -857,11 +924,15 @@ static void parse_tune2fs_options(int argc, char **argv)
        char *tmp;
        struct group *gr;
        struct passwd *pw;
+       char optstring[100] = "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:";
 
+#ifdef CONFIG_QUOTA
+       strcat(optstring, "Q:");
+#endif
        open_flag = 0;
 
        printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
-       while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:Q:T:U:")) != EOF)
+       while ((c = getopt(argc, argv, optstring)) != EOF)
                switch (c) {
                case 'c':
                        max_mount_count = strtol(optarg, &tmp, 0);
@@ -908,7 +979,7 @@ static void parse_tune2fs_options(int argc, char **argv)
                        open_flag |= EXT2_FLAG_RW;
                        break;
                case 'f': /* Force */
-                       f_flag = 1;
+                       f_flag++;
                        break;
                case 'g':
                        resgid = strtoul(optarg, &tmp, 0);
@@ -999,7 +1070,7 @@ static void parse_tune2fs_options(int argc, char **argv)
                        break;
                case 'o':
                        if (mntopts_cmd) {
-                               com_err(program_name, 0,
+                               com_err(program_name, 0, "%s",
                                        _("-o may only be specified once"));
                                usage();
                        }
@@ -1008,18 +1079,20 @@ static void parse_tune2fs_options(int argc, char **argv)
                        break;
                case 'O':
                        if (features_cmd) {
-                               com_err(program_name, 0,
+                               com_err(program_name, 0, "%s",
                                        _("-O may only be specified once"));
                                usage();
                        }
                        features_cmd = optarg;
                        open_flag = EXT2_FLAG_RW;
                        break;
+#ifdef CONFIG_QUOTA
                case 'Q':
                        Q_flag = 1;
                        parse_quota_opts(optarg);
                        open_flag = EXT2_FLAG_RW;
                        break;
+#endif
                case 'r':
                        reserved_blocks = strtoul(optarg, &tmp, 0);
                        if (*tmp) {
@@ -1133,7 +1206,7 @@ static int parse_extended_opts(ext2_filsys fs, const char *opts)
        len = strlen(opts);
        buf = malloc(len+1);
        if (!buf) {
-               fprintf(stderr,
+               fprintf(stderr, "%s",
                        _("Couldn't allocate memory to parse options!\n"));
                return 1;
        }
@@ -1254,7 +1327,7 @@ static int parse_extended_opts(ext2_filsys fs, const char *opts)
                        r_usage++;
        }
        if (r_usage) {
-               fprintf(stderr, _("\nBad options specified.\n\n"
+               fprintf(stderr, "%s", _("\nBad options specified.\n\n"
                        "Extended options are separated by commas, "
                        "and may take an argument which\n"
                        "\tis set off by an equals ('=') sign.\n\n"
@@ -1342,7 +1415,7 @@ static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk64_t blk)
 {
        blk64_t start_blk, end_blk;
        start_blk = fs->super->s_first_data_block +
-                       EXT2_BLOCKS_PER_GROUP(fs->super) * group;
+                       EXT2_GROUPS_TO_BLOCKS(fs->super, group);
        /*
         * We cannot get new block beyond end_blk for for the last block group
         * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
@@ -1814,7 +1887,7 @@ static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
        tmp_name = strdup(name);
        if (!tmp_name) {
        alloc_fn_fail:
-               com_err(program_name, ENOMEM, 
+               com_err(program_name, ENOMEM, "%s",
                        _("Couldn't allocate memory for tdb filename\n"));
                return ENOMEM;
        }
@@ -1833,15 +1906,12 @@ static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
                goto alloc_fn_fail;
        sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
 
-       if (!access(tdb_file, F_OK)) {
-               if (unlink(tdb_file) < 0) {
-                       retval = errno;
-                       com_err(program_name, retval,
-                               _("while trying to delete %s"),
-                               tdb_file);
-                       free(tdb_file);
-                       return retval;
-               }
+       if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
+               retval = errno;
+               com_err(program_name, retval,
+                       _("while trying to delete %s"), tdb_file);
+               free(tdb_file);
+               return retval;
        }
 
        set_undo_io_backing_manager(*io_ptr);
@@ -1855,6 +1925,71 @@ static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
        return retval;
 }
 
+int
+fs_update_journal_user(struct ext2_super_block *sb, __u8 old_uuid[UUID_SIZE])
+{
+       int retval, nr_users, start;
+       journal_superblock_t *jsb;
+       ext2_filsys jfs;
+       __u8 *j_uuid;
+       char *journal_path;
+       char uuid[UUID_STR_SIZE];
+       char buf[SUPERBLOCK_SIZE];
+
+       if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) ||
+               uuid_is_null(sb->s_journal_uuid))
+               return 0;
+
+       uuid_unparse(sb->s_journal_uuid, uuid);
+       journal_path = blkid_get_devname(NULL, "UUID", uuid);
+       if (!journal_path)
+               return 0;
+
+       retval = ext2fs_open2(journal_path, io_options,
+                             EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_RW,
+                             0, 0, unix_io_manager, &jfs);
+       if (retval) {
+               com_err(program_name, retval,
+                       _("while trying to open %s"),
+                       journal_path);
+               return retval;
+       }
+
+       retval = get_journal_sb(jfs, buf);
+       if (retval != 0) {
+               if (retval == EXT2_ET_UNSUPP_FEATURE)
+                       fprintf(stderr, _("%s is not a journal device.\n"),
+                               journal_path);
+               return retval;
+       }
+
+       jsb = (journal_superblock_t *) buf;
+       /* Find the filesystem UUID */
+       nr_users = ntohl(jsb->s_nr_users);
+
+       j_uuid = journal_user(old_uuid, jsb->s_users, nr_users);
+       if (j_uuid == NULL) {
+               fputs(_("Filesystem's UUID not found on journal device.\n"),
+                     stderr);
+               return EXT2_ET_LOAD_EXT_JOURNAL;
+       }
+
+       memcpy(j_uuid, sb->s_uuid, UUID_SIZE);
+
+       start = ext2fs_journal_sb_start(jfs->blocksize);
+       /* Write back the journal superblock */
+       retval = io_channel_write_blk64(jfs->io, start, -SUPERBLOCK_SIZE, buf);
+       if (retval != 0) {
+               com_err(program_name, retval,
+                       "while writing journal superblock.");
+               return retval;
+       }
+
+       ext2fs_close(jfs);
+
+       return 0;
+}
+
 int main(int argc, char **argv)
 {
        errcode_t retval;
@@ -1919,7 +2054,7 @@ retry_open:
                                _("MMP block magic is bad. Try to fix it by "
                                  "running:\n'e2fsck -f %s'\n"), device_name);
                else if (retval != EXT2_ET_MMP_FAILED)
-                       fprintf(stderr,
+                       fprintf(stderr, "%s",
                             _("Couldn't find valid filesystem superblock.\n"));
 
                ext2fs_free(fs);
@@ -1940,8 +2075,8 @@ retry_open:
                        goto closefs;
                }
                if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
-                       fprintf(stderr, _("Shrinking the inode size is "
-                                         "not supported\n"));
+                       fprintf(stderr, "%s",
+                               _("Shrinking inode size is not supported\n"));
                        rc = 1;
                        goto closefs;
                }
@@ -1963,7 +2098,7 @@ retry_open:
                        goto closefs;
                }
                if (io_ptr != io_ptr_orig) {
-                       ext2fs_close(fs);
+                       ext2fs_close_free(&fs);
                        goto retry_open;
                }
        }
@@ -2012,7 +2147,7 @@ retry_open:
                printf(_("Setting reserved blocks gid to %lu\n"), resgid);
        }
        if (i_flag) {
-               if (interval >= (1ULL << 32)) {
+               if ((unsigned long long)interval >= (1ULL << 32)) {
                        com_err(program_name, 0,
                                _("interval between checks is too big (%lu)"),
                                interval);
@@ -2046,10 +2181,18 @@ retry_open:
        }
        if (s_flag == 1) {
                if (sb->s_feature_ro_compat &
-                   EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
+                   EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) {
                        fputs(_("\nThe filesystem already has sparse "
                                "superblocks.\n"), stderr);
-               else {
+               } else if (sb->s_feature_incompat &
+                       EXT2_FEATURE_INCOMPAT_META_BG) {
+                       fputs(_("\nSetting the sparse superblock flag not "
+                               "supported\nfor filesystems with "
+                               "the meta_bg feature enabled.\n"),
+                               stderr);
+                       rc = 1;
+                       goto closefs;
+               } else {
                        sb->s_feature_ro_compat |=
                                EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
                        sb->s_state &= ~EXT2_VALID_FS;
@@ -2059,7 +2202,7 @@ retry_open:
                }
        }
        if (s_flag == 0) {
-               fputs(_("\nClearing the sparse superflag not supported.\n"),
+               fputs(_("\nClearing the sparse superblock flag not supported.\n"),
                      stderr);
                rc = 1;
                goto closefs;
@@ -2134,10 +2277,25 @@ retry_open:
        if (U_flag) {
                int set_csum = 0;
                dgrp_t i;
+               char buf[SUPERBLOCK_SIZE];
+               __u8 old_uuid[UUID_SIZE];
 
                if (sb->s_feature_ro_compat &
                    EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
                        /*
+                        * Changing the UUID requires rewriting all metadata,
+                        * which can race with a mounted fs.  Don't allow that.
+                        */
+                       if (mount_flags & EXT2_MF_MOUNTED) {
+                               fputs(_("The UUID may only be "
+                                       "changed when the filesystem is "
+                                       "unmounted.\n"), stderr);
+                               exit(1);
+                       }
+                       if (check_fsck_needed(fs))
+                               exit(1);
+
+                       /*
                         * Determine if the block group checksums are
                         * correct so we know whether or not to set
                         * them later on.
@@ -2148,6 +2306,8 @@ retry_open:
                        if (i >= fs->group_desc_count)
                                set_csum = 1;
                }
+
+               memcpy(old_uuid, sb->s_uuid, UUID_SIZE);
                if ((strcasecmp(new_UUID, "null") == 0) ||
                    (strcasecmp(new_UUID, "clear") == 0)) {
                        uuid_clear(sb->s_uuid);
@@ -2156,7 +2316,8 @@ retry_open:
                } else if (strcasecmp(new_UUID, "random") == 0) {
                        uuid_generate(sb->s_uuid);
                } else if (uuid_parse(new_UUID, sb->s_uuid)) {
-                       com_err(program_name, 0, _("Invalid UUID format\n"));
+                       com_err(program_name, 0, "%s",
+                               _("Invalid UUID format\n"));
                        rc = 1;
                        goto closefs;
                }
@@ -2165,6 +2326,29 @@ 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() */
+
+                       if ((rc = fs_update_journal_user(sb, old_uuid)))
+                               goto closefs;
+               }
+
                ext2fs_mark_super_dirty(fs);
        }
        if (I_flag) {
@@ -2193,7 +2377,7 @@ retry_open:
                        printf(_("Setting inode size %lu\n"),
                                                        new_inode_size);
                } else {
-                       printf(_("Failed to change inode size\n"));
+                       printf("%s", _("Failed to change inode size\n"));
                        rc = 1;
                        goto closefs;
                }
@@ -2229,5 +2413,5 @@ closefs:
                exit(1);
        }
 
-       return (ext2fs_close(fs) ? 1 : 0);
+       return (ext2fs_close_free(&fs) ? 1 : 0);
 }