Whamcloud - gitweb
ext4: add support for printing the error code associated with an error
authorTheodore Ts'o <tytso@mit.edu>
Sat, 28 Mar 2020 20:51:52 +0000 (16:51 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 10 Apr 2020 04:10:47 +0000 (00:10 -0400)
The error code allows the kernel to bucket the possible cause of an
ext4 corruption by Unix errno codes (e.g., EIO, EFSBADCRC, ESHUTDOWN,
etc.)

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/e2p/Makefile.in
lib/e2p/e2p.h
lib/e2p/errcode.c [new file with mode: 0644]
lib/e2p/ls.c
lib/ext2fs/ext2_fs.h
lib/ext2fs/tst_super_size.c

index 9e96884..3e4395e 100644 (file)
@@ -20,17 +20,20 @@ OBJS=               feature.o fgetflags.o fsetflags.o fgetversion.o fsetversion.o \
                getflags.o getversion.o hashstr.o iod.o ls.o ljs.o mntopts.o \
                parse_num.o pe.o pf.o ps.o setflags.o setversion.o uuid.o \
                ostype.o percent.o crypto_mode.o fgetproject.o fsetproject.o \
-               encoding.o
+               encoding.o errcode.o
 
 SRCS=          $(srcdir)/feature.c $(srcdir)/fgetflags.c \
                $(srcdir)/fsetflags.c $(srcdir)/fgetversion.c \
                $(srcdir)/fsetversion.c $(srcdir)/getflags.c \
                $(srcdir)/getversion.c $(srcdir)/hashstr.c $(srcdir)/iod.c \
-               $(srcdir)/ls.c $(srcdir)/ljs.c $(srcdir)/mntopts.c $(srcdir)/parse_num.c \
-               $(srcdir)/pe.c $(srcdir)/pf.c $(srcdir)/ps.c \
-               $(srcdir)/setflags.c $(srcdir)/setversion.c $(srcdir)/uuid.c \
-               $(srcdir)/ostype.c $(srcdir)/percent.c $(srcdir)/crypto_mode.c \
-               $(srcdir)/fgetproject.c $(srcdir)/fsetproject.c $(srcdir)/encoding.c
+               $(srcdir)/ls.c $(srcdir)/ljs.c $(srcdir)/mntopts.c \
+               $(srcdir)/parse_num.c $(srcdir)/pe.c $(srcdir)/pf.c \
+               $(srcdir)/ps.c $(srcdir)/setflags.c $(srcdir)/setversion.c \
+               $(srcdir)/uuid.c $(srcdir)/ostype.c $(srcdir)/percent.c \
+               $(srcdir)/crypto_mode.c $(srcdir)/fgetproject.c \
+               $(srcdir)/fsetproject.c $(srcdir)/encoding.c \
+               $(srcdir)/errcode.c
+
 HFILES= e2p.h
 
 LIBRARY= libe2p
index ae7dd74..90efb62 100644 (file)
@@ -87,3 +87,5 @@ int e2p_str2encoding(const char *string);
 const char *e2p_encoding2str(int encoding);
 int e2p_get_encoding_flags(int encoding);
 int e2p_str2encoding_flags(int encoding, char *param, __u16 *flags);
+
+const char *e2p_errcode2str(int err);
diff --git a/lib/e2p/errcode.c b/lib/e2p/errcode.c
new file mode 100644 (file)
index 0000000..27d4b15
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * errcode.c           - convert an error code to a string
+ */
+
+#include "config.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static const char *err_string[] = {
+       "",
+       "UNKNOWN",              /*  1 */
+       "EIO",                  /*  2 */
+       "ENOMEM",               /*  3 */
+       "EFSBADCRC",            /*  4 */
+       "EFSCORRUPTED",         /*  5 */
+       "ENOSPC",               /*  6 */
+       "ENOKEY",               /*  7 */
+       "EROFS",                /*  8 */
+       "EFBIG",                /*  9 */
+       "EEXIST",               /* 10 */
+       "ERANGE",               /* 11 */
+       "EOVERFLOW",            /* 12 */
+       "EBUSY",                /* 13 */
+       "ENOTDIR",              /* 14 */
+       "ENOTEMPTY",            /* 15 */
+       "ESHUTDOWN",            /* 16 */
+       "EFAULT",               /* 17 */
+};
+
+#define ARRAY_SIZE(array)                      \
+        (sizeof(array) / sizeof(array[0]))
+
+/* Return the name of an encoding or NULL */
+const char *e2p_errcode2str(int err)
+{
+       unsigned int i;
+       static char buf[32];
+
+       if (err < ARRAY_SIZE(err_string))
+               return err_string[err];
+
+       sprintf(buf, "UNKNOWN_ERRCODE_%d", err);
+       return buf;
+}
+
+
index 740bc8d..5aad15d 100644 (file)
@@ -422,10 +422,15 @@ void list_super2(struct ext2_super_block * sb, FILE *f)
                        EXT2_LEN_STR(sb->s_first_error_func));
                fprintf(f, "First error line #:       %u\n",
                        sb->s_first_error_line);
-               fprintf(f, "First error inode #:      %u\n",
-                       sb->s_first_error_ino);
-               fprintf(f, "First error block #:      %llu\n",
-                       sb->s_first_error_block);
+               if (sb->s_first_error_ino)
+                       fprintf(f, "First error inode #:      %u\n",
+                               sb->s_first_error_ino);
+               if (sb->s_first_error_block)
+                       fprintf(f, "First error block #:      %llu\n",
+                               sb->s_first_error_block);
+               if (sb->s_first_error_errcode)
+                       fprintf(f, "First error err:          %s\n",
+                               e2p_errcode2str(sb->s_first_error_errcode));
        }
        if (sb->s_last_error_time) {
                tm = sb->s_last_error_time;
@@ -434,10 +439,15 @@ void list_super2(struct ext2_super_block * sb, FILE *f)
                        EXT2_LEN_STR(sb->s_last_error_func));
                fprintf(f, "Last error line #:        %u\n",
                        sb->s_last_error_line);
-               fprintf(f, "Last error inode #:       %u\n",
-                       sb->s_last_error_ino);
-               fprintf(f, "Last error block #:       %llu\n",
-                       sb->s_last_error_block);
+               if (sb->s_last_error_ino)
+                       fprintf(f, "Last error inode #:       %u\n",
+                               sb->s_last_error_ino);
+               if (sb->s_last_error_block)
+                       fprintf(f, "Last error block #:       %llu\n",
+                               sb->s_last_error_block);
+               if (sb->s_last_error_errcode)
+                       fprintf(f, "Last error err:           %s\n",
+                               e2p_errcode2str(sb->s_last_error_errcode));
        }
        if (ext2fs_has_feature_mmp(sb)) {
                fprintf(f, "MMP block number:         %llu\n",
index 6c20ea7..0376674 100644 (file)
@@ -755,7 +755,8 @@ struct ext2_super_block {
        __u8    s_lastcheck_hi;
        __u8    s_first_error_time_hi;
        __u8    s_last_error_time_hi;
-       __u8    s_pad[2];
+       __u8    s_first_error_errcode;
+       __u8    s_last_error_errcode;
 /*27c*/ __le16 s_encoding;             /* Filename charset encoding */
        __le16  s_encoding_flags;       /* Filename charset encoding flags */
        __le32  s_reserved[95];         /* Padding to the end of the block */
index ab38dd5..80a5269 100644 (file)
@@ -148,7 +148,8 @@ int main(int argc, char **argv)
        check_field(s_lastcheck_hi, 1);
        check_field(s_first_error_time_hi, 1);
        check_field(s_last_error_time_hi, 1);
-       check_field(s_pad, 2);
+       check_field(s_first_error_errcode, 1);
+       check_field(s_last_error_errcode, 1);
        check_field(s_encoding, 2);
        check_field(s_encoding_flags, 2);
        check_field(s_reserved, 95 * 4);