]> git.ipfire.org Git - thirdparty/e2fsprogs.git/commitdiff
e4defrag: handle failure to open the file system gracefully
authorTheodore Ts'o <tytso@mit.edu>
Sun, 21 Oct 2018 11:35:53 +0000 (07:35 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Sun, 21 Oct 2018 11:52:29 +0000 (07:52 -0400)
If e4defrag is run by root, it will try to open the underlying file
system for files that it is trying to defrag so it can get the file
system parameters.  It's currently doing this by searching /etc/mtab.
This isn't the best way to go about doing things, but we'll leave it
for now, at least for a maintenance release.  (The better way to do
things would be to look up the device using the blkid library, but
that's a more involved change.)

Since the file system parameters isn't strictly speaking necessary
(after all we get by without them when not running as root), we'll
allow e4defrag to continue running if we can't find the file system.
This can happen if /etc/mtab is pointing at /proc/mounts, and the
kernel can't properly identify the root file system, it is reported as
"/dev/root".

Addresses-Debian-Bug: #907634

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
misc/e4defrag.c

index 5ac251dc5c1aa33faee0808567bfee46768653a9..9d237da24ba2d5f1c610ea589e7bf3f4d8bce1b1 100644 (file)
@@ -1016,7 +1016,9 @@ static int get_best_count(ext4_fsblk_t block_count)
        int ret;
        unsigned int flex_bg_num;
 
-       /* Calculate best extents count */
+       if (blocks_per_group == 0)
+               return 1;
+
        if (feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
                flex_bg_num = 1 << log_groups_per_flex;
                ret = ((block_count - 1) /
@@ -1508,10 +1510,7 @@ static int file_defrag(const char *file, const struct stat64 *buf,
                goto out;
        }
 
-       if (current_uid == ROOT_UID)
-               best = get_best_count(blk_count);
-       else
-               best = 1;
+       best = get_best_count(blk_count);
 
        if (file_frags_start <= best)
                goto check_improvement;
@@ -1805,17 +1804,16 @@ int main(int argc, char *argv[])
                                          block_size, unix_io_manager, &fs);
                        if (ret) {
                                if (mode_flag & DETAIL)
-                                       com_err(argv[1], ret,
-                                               "while trying to open file system: %s",
-                                               dev_name);
-                               continue;
+                                       fprintf(stderr,
+                                               "Warning: couldn't get file "
+                                               "system details for %s: %s\n",
+                                               dev_name, error_message(ret));
+                       } else {
+                               blocks_per_group = fs->super->s_blocks_per_group;
+                               feature_incompat = fs->super->s_feature_incompat;
+                               log_groups_per_flex = fs->super->s_log_groups_per_flex;
+                               ext2fs_close_free(&fs);
                        }
-
-                       blocks_per_group = fs->super->s_blocks_per_group;
-                       feature_incompat = fs->super->s_feature_incompat;
-                       log_groups_per_flex = fs->super->s_log_groups_per_flex;
-
-                       ext2fs_close_free(&fs);
                }
 
                switch (arg_type) {