Whamcloud - gitweb
LU-11213 uapi: Remove unused CONNECT flag
[fs/lustre-release.git] / lustre / utils / llog_reader.c
index 88f2880..6c776e2 100644 (file)
@@ -196,8 +196,10 @@ int main(int argc, char **argv)
                goto out_fd;
        }
 
-       print_llog_header(llog_buf);
-       print_records(recs_buf, rec_number, is_ext);
+       if (llog_buf != NULL)
+               print_llog_header(llog_buf);
+       if (recs_buf != NULL)
+               print_records(recs_buf, rec_number, is_ext);
        llog_unpack_buffer(fd, llog_buf, recs_buf);
 
 out_fd:
@@ -218,6 +220,7 @@ int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
        char *file_buf = NULL, *recs_buf = NULL;
        struct llog_rec_hdr **recs_pr = NULL;
        char *ptr = NULL;
+       int count;
        int i;
 
        rc = fstat(fd, &st);
@@ -259,10 +262,23 @@ int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
                goto clear_file_buf;
        }
 
+       count = __le32_to_cpu((*llog)->llh_count);
+       if (count < 0) {
+               rc = -EINVAL;
+               llapi_error(LLAPI_MSG_ERROR, rc,
+                           "corrupted llog: negative record number %d",
+                           count);
+               goto clear_file_buf;
+       } else if (count == 0) {
+               llapi_printf(LLAPI_MSG_NORMAL,
+                            "uninitialized llog: zero record number\n");
+               *recs_number = 0;
+               goto clear_file_buf;
+       }
        /* the llog header not countable here.*/
-       recs_num = __le32_to_cpu((*llog)->llh_count) - 1;
+       recs_num = count - 1;
 
-       recs_buf = malloc(recs_num * sizeof(**recs_pr));
+       recs_buf = calloc(recs_num, sizeof(**recs_pr));
        if (recs_buf == NULL) {
                rc = -ENOMEM;
                llapi_error(LLAPI_MSG_ERROR, rc,
@@ -334,11 +350,13 @@ clear_file_buf:
 }
 
 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
-                        struct llog_rec_hdr **recs_buf)
+                       struct llog_rec_hdr **recs_buf)
 {
-        free(llog_buf);
-        free(recs_buf);
-        return;
+       if (llog_buf != NULL)
+               free(llog_buf);
+       if (recs_buf != NULL)
+               free(recs_buf);
+       return;
 }
 
 void print_llog_header(struct llog_log_hdr *llog_buf)
@@ -659,9 +677,10 @@ void print_changelog_rec(struct llog_changelog_rec *rec)
 
        secs = __le64_to_cpu(rec->cr.cr_time) >> 30;
        gmtime_r(&secs, &ts);
-       printf("changelog record id:0x%x cr_flags:0x%x cr_type:%s(0x%x) "
-              "date:'%02d:%02d:%02d.%09d %04d.%02d.%02d' target:"DFID,
-              __le32_to_cpu(rec->cr_hdr.lrh_id),
+       printf("changelog record id:0x%x index:%llu cr_flags:0x%x "
+              "cr_type:%s(0x%x) date:'%02d:%02d:%02d.%09d %04d.%02d.%02d' "
+              "target:"DFID, __le32_to_cpu(rec->cr_hdr.lrh_id),
+              __le64_to_cpu(rec->cr.cr_index),
               __le32_to_cpu(rec->cr.cr_flags),
               changelog_type2str(__le32_to_cpu(rec->cr.cr_type)),
               __le32_to_cpu(rec->cr.cr_type),
@@ -711,10 +730,10 @@ void print_changelog_rec(struct llog_changelog_rec *rec)
                                mode[2] = 'x';
                        } else {
                                if (__le32_to_cpu(omd->cr_openflags) &
-                                   FMODE_READ)
+                                   MDS_FMODE_READ)
                                        mode[0] = 'r';
                                if (__le32_to_cpu(omd->cr_openflags) &
-                              (FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
+                          (MDS_FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
                                        mode[1] = 'w';
                        }
 
@@ -760,6 +779,12 @@ static void print_records(struct llog_rec_hdr **recs,
        int i, skip = 0;
 
        for (i = 0; i < rec_number; i++) {
+               if (recs[i] == NULL) {
+                       llapi_printf(LLAPI_MSG_NORMAL,
+                                    "uninitialized llog record at index %d\n",
+                                    i);
+                       break;
+               }
                printf("#%.2d (%.3d)", __le32_to_cpu(recs[i]->lrh_index),
                       __le32_to_cpu(recs[i]->lrh_len));