]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blobdiff - misc/tune2fs.c
tune2fs, mke2fs: add the ability to control the location of the journal
[thirdparty/e2fsprogs.git] / misc / tune2fs.c
index 8bcf667210a2e34c2ce396300470523e8accab34..64a76f1e4864889ef1c0561ee3e6cda67adaf248 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>
@@ -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);
@@ -141,8 +149,10 @@ static __u32 ok_features[3] = {
                EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
                EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
                EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
-               EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER |
-               EXT4_FEATURE_RO_COMPAT_QUOTA
+#ifdef CONFIG_QUOTA
+               EXT4_FEATURE_RO_COMPAT_QUOTA |
+#endif
+               EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
 };
 
 static __u32 clear_ok_features[3] = {
@@ -159,8 +169,10 @@ static __u32 clear_ok_features[3] = {
                EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
                EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
                EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
-               EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
-               EXT4_FEATURE_RO_COMPAT_QUOTA
+#ifdef CONFIG_QUOTA
+               EXT4_FEATURE_RO_COMPAT_QUOTA |
+#endif
+               EXT4_FEATURE_RO_COMPAT_GDT_CSUM
 };
 
 /*
@@ -201,7 +213,7 @@ 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;
        }
@@ -214,7 +226,7 @@ static int remove_journal_device(ext2_filsys fs)
 
        /* Get the journal superblock */
        if ((retval = io_channel_read_blk64(jfs->io, 1, -1024, buf))) {
-               com_err(program_name, retval,
+               com_err(program_name, retval, "%s",
                        _("while reading journal superblock"));
                goto no_valid_journal;
        }
@@ -298,14 +310,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;
                }
@@ -313,7 +325,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;
                }
@@ -324,7 +336,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;
        }
@@ -351,6 +363,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(_("(and reboot afterwards!)\n"));
+       return 1;
+}
+
 static void request_fsck_afterwards(ext2_filsys fs)
 {
        static int requested = 0;
@@ -360,7 +382,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"));
 }
 
 /*
@@ -371,7 +393,8 @@ static int update_feature_set(ext2_filsys fs, char *features)
        struct ext2_super_block *sb = fs->super;
        struct ext2_group_desc *gd;
        __u32           old_features[3];
-       int             i, type_err;
+       dgrp_t          i;
+       int             type_err;
        unsigned int    mask_err;
 
 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
@@ -483,14 +506,14 @@ 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;
                }
 
                /* We need to force out the group descriptors as well */
                fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
-               ext2fs_block_alloc_stats(fs, sb->s_mmp_block, -1);
+               ext2fs_block_alloc_stats2(fs, sb->s_mmp_block, -1);
 mmp_error:
                sb->s_mmp_block = 0;
                sb->s_mmp_update_interval = 0;
@@ -672,11 +695,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
@@ -697,10 +725,9 @@ err:
        return 1;
 }
 
-void handle_quota_options(ext2_filsys fs)
+static void handle_quota_options(ext2_filsys fs)
 {
        quota_ctx_t qctx;
-       errcode_t retval;
        ext2_ino_t qf_ino;
 
        if (!usrquota && !grpquota)
@@ -712,22 +739,35 @@ void handle_quota_options(ext2_filsys fs)
        if (usrquota == QOPT_ENABLE || grpquota == QOPT_ENABLE)
                quota_compute_usage(qctx);
 
-       if (usrquota == QOPT_ENABLE)
+       if (usrquota == QOPT_ENABLE && !fs->super->s_usr_quota_inum) {
+               if ((qf_ino = quota_file_exists(fs, USRQUOTA,
+                                               QFMT_VFS_V1)) > 0)
+                       quota_update_limits(qctx, qf_ino, USRQUOTA);
                quota_write_inode(qctx, USRQUOTA);
-       else if (usrquota == QOPT_DISABLE)
+       } else if (usrquota == QOPT_DISABLE) {
                quota_remove_inode(fs, USRQUOTA);
+       }
 
-       if (grpquota == QOPT_ENABLE)
+       if (grpquota == QOPT_ENABLE && !fs->super->s_grp_quota_inum) {
+               if ((qf_ino = quota_file_exists(fs, GRPQUOTA,
+                                               QFMT_VFS_V1)) > 0)
+                       quota_update_limits(qctx, qf_ino, GRPQUOTA);
                quota_write_inode(qctx, GRPQUOTA);
-       else if (grpquota == QOPT_DISABLE)
+       } else if (grpquota == QOPT_DISABLE) {
                quota_remove_inode(fs, GRPQUOTA);
+       }
 
        quota_release_context(&qctx);
 
        if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
+               fprintf(stderr, "%s", _("\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 ((usrquota == QOPT_DISABLE) && (grpquota == QOPT_DISABLE)) {
+       } else if (!fs->super->s_usr_quota_inum &&
+                  !fs->super->s_grp_quota_inum) {
                fs->super->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
                ext2fs_mark_super_dirty(fs);
        }
@@ -735,9 +775,10 @@ void handle_quota_options(ext2_filsys fs)
        return;
 }
 
-void parse_quota_opts(const char *opts)
+#ifdef CONFIG_QUOTA
+static void parse_quota_opts(const char *opts)
 {
-       char    *buf, *token, *next, *p, *arg;
+       char    *buf, *token, *next, *p;
        int     len;
 
        len = strlen(opts);
@@ -777,8 +818,7 @@ void parse_quota_opts(const char *opts)
        }
        free(buf);
 }
-
-
+#endif
 
 static void parse_e2label_options(int argc, char ** argv)
 {
@@ -840,11 +880,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);
@@ -982,7 +1026,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();
                        }
@@ -991,18 +1035,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) {
@@ -1080,7 +1126,7 @@ static void parse_tune2fs_options(int argc, char **argv)
                *io_options++ = 0;
        device_name = blkid_get_devname(NULL, argv[optind], NULL);
        if (!device_name) {
-               com_err("tune2fs", 0, _("Unable to resolve '%s'"),
+               com_err(program_name, 0, _("Unable to resolve '%s'"),
                        argv[optind]);
                exit(1);
        }
@@ -1116,7 +1162,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;
        }
@@ -1137,12 +1183,12 @@ static int parse_extended_opts(ext2_filsys fs, const char *opts)
                    strcmp(token, "clear_mmp") == 0) {
                        clear_mmp = 1;
                } else if (strcmp(token, "mmp_update_interval") == 0) {
-                       unsigned long interval;
+                       unsigned long intv;
                        if (!arg) {
                                r_usage++;
                                continue;
                        }
-                       interval = strtoul(arg, &p, 0);
+                       intv = strtoul(arg, &p, 0);
                        if (*p) {
                                fprintf(stderr,
                                        _("Invalid mmp_update_interval: %s\n"),
@@ -1150,21 +1196,21 @@ static int parse_extended_opts(ext2_filsys fs, const char *opts)
                                r_usage++;
                                continue;
                        }
-                       if (interval == 0) {
-                               interval = EXT4_MMP_UPDATE_INTERVAL;
-                       } else if (interval > EXT4_MMP_MAX_UPDATE_INTERVAL) {
+                       if (intv == 0) {
+                               intv = EXT4_MMP_UPDATE_INTERVAL;
+                       } else if (intv > EXT4_MMP_MAX_UPDATE_INTERVAL) {
                                fprintf(stderr,
                                        _("mmp_update_interval too big: %lu\n"),
-                                       interval);
+                                       intv);
                                r_usage++;
                                continue;
                        }
                        printf(P_("Setting multiple mount protection update "
                                  "interval to %lu second\n",
                                  "Setting multiple mount protection update "
-                                 "interval to %lu seconds\n", interval),
-                              interval);
-                       fs->super->s_mmp_update_interval = interval;
+                                 "interval to %lu seconds\n", intv),
+                              intv);
+                       fs->super->s_mmp_update_interval = intv;
                        ext2fs_mark_super_dirty(fs);
                } else if (!strcmp(token, "test_fs")) {
                        fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
@@ -1237,7 +1283,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"
@@ -1310,10 +1356,10 @@ static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
        return 0;
 }
 
-static int ext2fs_is_meta_block(ext2_filsys fs, blk_t blk)
+static int ext2fs_is_meta_block(ext2_filsys fs, blk64_t blk)
 {
        dgrp_t group;
-       group = ext2fs_group_of_blk(fs, blk);
+       group = ext2fs_group_of_blk2(fs, blk);
        if (ext2fs_block_bitmap_loc(fs, group) == blk)
                return 1;
        if (ext2fs_inode_bitmap_loc(fs, group) == blk)
@@ -1321,9 +1367,9 @@ static int ext2fs_is_meta_block(ext2_filsys fs, blk_t blk)
        return 0;
 }
 
-static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk_t blk)
+static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk64_t blk)
 {
-       blk_t start_blk, end_blk;
+       blk64_t start_blk, end_blk;
        start_blk = fs->super->s_first_data_block +
                        EXT2_BLOCKS_PER_GROUP(fs->super) * group;
        /*
@@ -1363,7 +1409,7 @@ static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
                         * the respective fs metadata pointers. Otherwise
                         * fail
                         */
-                       group = ext2fs_group_of_blk(fs, blk);
+                       group = ext2fs_group_of_blk2(fs, blk);
                        goal = ext2fs_group_first_block2(fs, group);
                        meta_data = 1;
 
@@ -1524,7 +1570,7 @@ err_out:
 static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
 {
        dgrp_t i;
-       blk_t blk, new_blk;
+       blk64_t blk, new_blk;
 
        for (i = 0; i < fs->group_desc_count; i++) {
                blk = ext2fs_block_bitmap_loc(fs, i);
@@ -1797,7 +1843,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;
        }
@@ -1816,15 +1862,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);
@@ -1902,12 +1945,13 @@ 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);
                exit(1);
        }
+       fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
 
        if (I_flag && !io_ptr_orig) {
                /*
@@ -1922,8 +1966,14 @@ 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;
+               }
+               if (new_inode_size > fs->blocksize) {
+                       fprintf(stderr, _("Invalid inode size %lu (max %d)\n"),
+                               new_inode_size, fs->blocksize);
                        rc = 1;
                        goto closefs;
                }
@@ -2113,6 +2163,19 @@ retry_open:
 
                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
@@ -2132,7 +2195,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;
                }
@@ -2169,7 +2233,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;
                }