Whamcloud - gitweb
LU-6245 libcfs: remove userland headers from libcfs.h
[fs/lustre-release.git] / lustre / utils / llog_reader.c
index d2f1f1a..9dc3ef8 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, 2015, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
  * Lustre is a trademark of Sun Microsystems, Inc.
  */
- /* Interpret configuration llogs */
-
+/** \defgroup llog_reader Lustre Log Reader
+ *
+ * Interpret llogs used for storing configuration and changelog data
+ *
+ * @{
+ */
 
+#include <errno.h>
 #include <stdio.h>
+#include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#ifdef HAVE_ENDIAN_H
+# include <endian.h>
+#endif
+#include <unistd.h>
 #include <fcntl.h>
-
+#include <sys/vfs.h>
+#include <linux/magic.h>
+#include <errno.h>
 #include <time.h>
-#include <liblustre.h>
+#include <lnet/nidstr.h>
 #include <lustre/lustre_idl.h>
+#include <lustre/lustreapi.h>
+#include <lustre_log_user.h>
+#include <lustre_cfg.h>
+
+static inline int ext2_test_bit(int nr, const void *addr)
+{
+#if __BYTE_ORDER == __BIG_ENDIAN
+       const unsigned char *tmp = addr;
+       return (tmp[nr >> 3] >> (nr & 7)) & 1;
+#else
+       const unsigned long *tmp = addr;
+       return ((1UL << (nr & (BITS_PER_LONG - 1))) &
+               ((tmp)[nr / BITS_PER_LONG])) != 0;
+#endif
+}
 
 int llog_pack_buffer(int fd, struct llog_log_hdr **llog_buf,
                      struct llog_rec_hdr ***recs, int *recs_number);
 
 void print_llog_header(struct llog_log_hdr *llog_buf);
-void print_records(struct llog_rec_hdr **recs_buf,int rec_number);
+static void print_records(struct llog_rec_hdr **recs_buf,
+                         int rec_number, int is_ext);
 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
                         struct llog_rec_hdr **recs_buf);
 
@@ -77,129 +105,230 @@ char* portals_command[17]=
         ""
 };
 
-int main(int argc, char **argv)
+int is_fstype_ext(int fd)
 {
-        int rc = 0;
-        int fd, rec_number;
-        struct llog_log_hdr *llog_buf = NULL;
-        struct llog_rec_hdr **recs_buf = NULL;
-
-        setlinebuf(stdout);
-
-        if(argc != 2 ){
-                printf("Usage: llog_reader filename\n");
-                return -1;
-        }
+       struct statfs           st;
+       int                     rc;
 
-        fd = open(argv[1],O_RDONLY);
-        if (fd < 0){
-                printf("Could not open the file %s\n", argv[1]);
-                goto out;
-        }
-        rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
-        if (rc < 0) {
-                printf("Could not pack buffer; rc=%d\n", rc);
-                goto out_fd;
-        }
+       rc = fstatfs(fd, &st);
+       if (rc < 0) {
+               llapi_error(LLAPI_MSG_ERROR, rc, "Got statfs error.");
+               return -errno;
+       }
 
-        print_llog_header(llog_buf);
-        print_records(recs_buf,rec_number);
-        llog_unpack_buffer(fd,llog_buf,recs_buf);
-out_fd:
-        close(fd);
-out:
-        return rc;
+       return (st.f_type == EXT4_SUPER_MAGIC);
 }
 
 
-
-int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
-                     struct llog_rec_hdr ***recs,
-                     int *recs_number)
+/**
+ * Attempt to display a path to the object (file) containing changelog entries,
+ * referred to by this changelog_catalog record.
+ *
+ * This path depends on the implementation of the OSD device; zfs-osd and
+ * ldiskfs-osd are different.
+ *
+ * Assumes that if the file system containing the changelog_catalog is
+ * ext{2,3,4}, the backend is ldiskfs-osd; otherwise it is either zfs-osd or at
+ * least names objects based on FID and the zfs-osd path (which includes the
+ * FID) will be sufficient.
+ *
+ * The Object ID stored in the record is also displayed untranslated.
+ */
+#define OSD_OI_FID_NR         (1UL << 7)
+static void print_log_path(struct llog_logid_rec *lid, int is_ext)
 {
-        int rc = 0, recs_num,rd;
-        off_t file_size;
-        struct stat st;
-        char *file_buf=NULL, *recs_buf=NULL;
-        struct llog_rec_hdr **recs_pr=NULL;
-        char *ptr=NULL;
-        int i;
 
-        rc = fstat(fd,&st);
-        if (rc < 0){
-                printf("Get file stat error.\n");
-                goto out;
-        }
-        file_size = st.st_size;
+       char                    object_path[255];
+       struct lu_fid           fid_from_logid;
 
-        file_buf = malloc(file_size);
-        if (file_buf == NULL){
-                printf("Memory Alloc for file_buf error.\n");
-                rc = -ENOMEM;
-                goto out;
-        }
-        *llog = (struct llog_log_hdr*)file_buf;
+       logid_to_fid(&lid->lid_id, &fid_from_logid);
 
-        rd = read(fd,file_buf,file_size);
-        if (rd < file_size){
-                printf("Read file error.\n");
-                rc = -EIO; /*FIXME*/
-                goto clear_file_buf;
-        }
+       if (is_ext)
+               snprintf(object_path, sizeof(object_path),
+                        "O/"LPU64"/d%u/%u", fid_from_logid.f_seq,
+                        fid_from_logid.f_oid % 32, fid_from_logid.f_oid);
+       else
+               snprintf(object_path, sizeof(object_path),
+                        "oi."LPU64"/"DFID_NOBRACE,
+                        fid_from_logid.f_seq & (OSD_OI_FID_NR - 1) ,
+                        PFID(&fid_from_logid));
 
-        /* the llog header not countable here.*/
-        recs_num = le32_to_cpu((*llog)->llh_count)-1;
+       printf("ogen=%X id="DOSTID" path=%s\n",
+               lid->lid_id.lgl_ogen, POSTID(&lid->lid_id.lgl_oi),
+               object_path);
+}
 
-        recs_buf = malloc(recs_num * sizeof(struct llog_rec_hdr *));
-        if (recs_buf == NULL){
-                printf("Memory Alloc for recs_buf error.\n");
-                rc = -ENOMEM;
-                goto clear_file_buf;
-        }
-        recs_pr = (struct llog_rec_hdr **)recs_buf;
+int main(int argc, char **argv)
+{
+       int rc = 0;
+       int is_ext;
+       int fd, rec_number;
+       struct llog_log_hdr *llog_buf = NULL;
+       struct llog_rec_hdr **recs_buf = NULL;
+
+       setlinebuf(stdout);
+
+       if (argc != 2) {
+               printf("Usage: llog_reader filename\n");
+               return -1;
+       }
+
+       fd = open(argv[1], O_RDONLY);
+       if (fd < 0) {
+               rc = -errno;
+               llapi_error(LLAPI_MSG_ERROR, rc, "Could not open the file %s.",
+                           argv[1]);
+               goto out;
+       }
+
+       is_ext = is_fstype_ext(fd);
+       if (is_ext < 0) {
+               printf("Unable to determine type of filesystem containing %s\n",
+                      argv[1]);
+               goto out;
+       }
+
+       rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
+       if (rc < 0) {
+               llapi_error(LLAPI_MSG_ERROR, rc, "Could not pack buffer.");
+               goto out_fd;
+       }
+
+       print_llog_header(llog_buf);
+       print_records(recs_buf, rec_number, is_ext);
+       llog_unpack_buffer(fd, llog_buf, recs_buf);
 
-        ptr = file_buf + le32_to_cpu((*llog)->llh_hdr.lrh_len);
-        i = 0;
+out_fd:
+       close(fd);
+out:
+       return rc;
+}
 
-        while (i < recs_num){
-                struct llog_rec_hdr *cur_rec = (struct llog_rec_hdr*)ptr;
-                int idx = le32_to_cpu(cur_rec->lrh_index);
-                recs_pr[i] = cur_rec;
 
-                if (ext2_test_bit(idx, (*llog)->llh_bitmap)) {
-                        if (le32_to_cpu(cur_rec->lrh_type) != OBD_CFG_REC) 
-                                printf("rec #%d type=%x len=%u\n", idx,
-                                       cur_rec->lrh_type, cur_rec->lrh_len);
-                } else {
-                        printf("Bit %d of %d not set\n", idx, recs_num);
-                        cur_rec->padding = CANCELLED;
-                        /* The header counts only set records */
-                        i--;
-                }
-                
-                ptr += le32_to_cpu(cur_rec->lrh_len);
-                if ((ptr - file_buf) > file_size) {
-                        printf("The log is corrupt (too big at %d)\n", i);
-                        rc = -EINVAL;
-                        goto clear_recs_buf;
-                }
-                i++;
-        }
 
-        *recs = recs_pr;
-        *recs_number = recs_num;
+int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
+                    struct llog_rec_hdr ***recs,
+                    int *recs_number)
+{
+       int rc = 0, recs_num, rd = 0;
+       long long file_size;
+       struct stat st;
+       char *file_buf = NULL, *recs_buf = NULL;
+       struct llog_rec_hdr **recs_pr = NULL;
+       char *ptr = NULL;
+       int i;
+
+       rc = fstat(fd, &st);
+       if (rc < 0) {
+               rc = -errno;
+               llapi_error(LLAPI_MSG_ERROR, rc, "Got file stat error.");
+               goto out;
+       }
+
+       file_size = st.st_size;
+       if (file_size < sizeof(**llog)) {
+               llapi_error(LLAPI_MSG_ERROR, rc,
+                           "File too small for llog header: "
+                           "need %zd, size %lld\n",
+                           sizeof(**llog), file_size);
+               rc = -EIO;
+               goto out;
+       }
+
+       file_buf = malloc(file_size);
+       if (file_buf == NULL) {
+               rc = -ENOMEM;
+               llapi_error(LLAPI_MSG_ERROR, rc, "Memory Alloc for file_buf.");
+               goto out;
+       }
+       *llog = (struct llog_log_hdr *)file_buf;
+
+       do {
+               rc = read(fd, file_buf + rd, file_size - rd);
+               if (rc > 0)
+                       rd += rc;
+       } while (rc > 0 && rd < file_size);
+
+       if (rd < file_size) {
+               rc = rc < 0 ? -errno : -EIO;
+               llapi_error(LLAPI_MSG_ERROR, rc,
+                           "Error reading llog header: need %zd, got %d",
+                           sizeof(**llog), rd);
+               goto clear_file_buf;
+       }
+
+       /* the llog header not countable here.*/
+       recs_num = le32_to_cpu((*llog)->llh_count) - 1;
+
+       recs_buf = malloc(recs_num * sizeof(**recs_pr));
+       if (recs_buf == NULL) {
+               rc = -ENOMEM;
+               llapi_error(LLAPI_MSG_ERROR, rc,
+                           "Error allocating %zd bytes for recs_buf",
+                           recs_num * sizeof(**recs_pr));
+               goto clear_file_buf;
+       }
+       recs_pr = (struct llog_rec_hdr **)recs_buf;
+
+       ptr = file_buf + le32_to_cpu((*llog)->llh_hdr.lrh_len);
+       i = 0;
+
+       while (ptr < (file_buf + file_size)) {
+               struct llog_rec_hdr *cur_rec;
+               int idx;
+               unsigned long offset;
+
+               if (ptr + sizeof(**recs_pr) > file_buf + file_size) {
+                       rc = -EINVAL;
+                       llapi_error(LLAPI_MSG_ERROR, rc,
+                                   "The log is corrupt (too big at %d)", i);
+                       goto clear_recs_buf;
+               }
+
+               cur_rec = (struct llog_rec_hdr *)ptr;
+               idx = le32_to_cpu(cur_rec->lrh_index);
+               recs_pr[i] = cur_rec;
+               offset = (unsigned long)ptr - (unsigned long)file_buf;
+               if (cur_rec->lrh_len == 0 ||
+                   cur_rec->lrh_len > (*llog)->llh_hdr.lrh_len) {
+                       cur_rec->lrh_len = (*llog)->llh_hdr.lrh_len -
+                               offset % (*llog)->llh_hdr.lrh_len;
+                       printf("off %lu skip %u to next chunk.\n", offset,
+                              cur_rec->lrh_len);
+                       i--;
+               } else if (ext2_test_bit(idx, LLOG_HDR_BITMAP(*llog))) {
+                       printf("rec #%d type=%x len=%u offset %lu\n", idx,
+                              cur_rec->lrh_type, cur_rec->lrh_len, offset);
+               } else {
+                       printf("Bit %d of %d not set\n", idx, recs_num);
+                       cur_rec->lrh_id = CANCELLED;
+                       /* The header counts only set records */
+                       i--;
+               }
+
+               ptr += le32_to_cpu(cur_rec->lrh_len);
+               if ((ptr - file_buf) > file_size) {
+                       printf("The log is corrupt (too big at %d)\n", i);
+                       rc = -EINVAL;
+                       goto clear_recs_buf;
+               }
+               i++;
+       }
+
+       *recs = recs_pr;
+       *recs_number = recs_num;
 
 out:
-        return rc;
+       return rc;
 
 clear_recs_buf:
-        free(recs_buf);
+       free(recs_buf);
 
 clear_file_buf:
-        free(file_buf);
+       free(file_buf);
 
-        *llog=NULL;
-        goto out;
+       *llog = NULL;
+       goto out;
 }
 
 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
@@ -267,7 +396,7 @@ static void print_setup_cfg(struct lustre_cfg *lcfg)
                 printf("setup     ");
                 print_1_cfg(lcfg);
         }
-        
+
         return;
 }
 
@@ -350,7 +479,10 @@ void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
         }
         case(LCFG_SET_TIMEOUT):{
                 printf("set_timeout=%d ", lcfg->lcfg_num);
-                print_1_cfg(lcfg);
+                break;
+        }
+        case(LCFG_SET_LDLM_TIMEOUT):{
+                printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
                 break;
         }
         case(LCFG_SET_UPCALL):{
@@ -358,8 +490,18 @@ void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
                 print_1_cfg(lcfg);
                 break;
         }
-        case(LCFG_PARAM):{
-                printf("param ");
+       case(LCFG_PARAM):{
+               printf("param ");
+               print_1_cfg(lcfg);
+               break;
+       }
+       case(LCFG_SET_PARAM):{
+               printf("set_param ");
+               print_1_cfg(lcfg);
+               break;
+       }
+        case(LCFG_SPTLRPC_CONF):{
+                printf("sptlrpc_conf ");
                 print_1_cfg(lcfg);
                 break;
         }
@@ -368,22 +510,21 @@ void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
                 char createtime[26], canceltime[26] = "";
                 time_t time_tmp;
 
-                if (marker->cm_flags & CM_SKIP) {
-                        if (marker->cm_flags & CM_START) {
-                                printf("SKIP START ");
-                                (*skip)++;
-                        } else {
-                                printf(     "END   ");
-                                *skip = 0;
-                        }
-                }
-
-                if (marker->cm_flags & CM_EXCLUDE) {
-                        if (marker->cm_flags & CM_START) 
-                                printf("EXCLUDE START ");
-                        else
-                                printf("EXCLUDE END   ");
-                }
+               if (marker->cm_flags & CM_START &&
+                   marker->cm_flags & CM_SKIP) {
+                       printf("SKIP START ");
+                       (*skip)++;
+               } else if (marker->cm_flags & CM_END) {
+                       printf(     "END   ");
+                       *skip = 0;
+               }
+
+               if (marker->cm_flags & CM_EXCLUDE) {
+                       if (marker->cm_flags & CM_START)
+                               printf("EXCLUDE START ");
+                       else
+                               printf("EXCLUDE END   ");
+               }
 
                 /* Handle overflow of 32-bit time_t gracefully.
                  * The copy to time_tmp is needed in any case to
@@ -447,28 +588,86 @@ void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
         return;
 }
 
-void print_records(struct llog_rec_hdr **recs, int rec_number)
+static void print_hsm_action(struct llog_agent_req_rec *larr)
 {
-        __u32 lopt;
-        int i, skip = 0;
-        
-        for(i = 0; i < rec_number; i++) {
-                printf("#%.2d (%.3d)", le32_to_cpu(recs[i]->lrh_index),
-                       le32_to_cpu(recs[i]->lrh_len));
-
-                lopt = le32_to_cpu(recs[i]->lrh_type);
-
-                if (recs[i]->padding == CANCELLED) 
-                        printf("NOT SET ");
-            
-                if (lopt == OBD_CFG_REC) {
-                        struct lustre_cfg *lcfg;
-                        lcfg = (struct lustre_cfg *)((char*)(recs[i]) +
-                                                     sizeof(struct llog_rec_hdr));
-                        print_lustre_cfg(lcfg, &skip);
-                } else if (lopt == LLOG_PAD_MAGIC) {
-                        printf("padding\n");
-                } else
-                        printf("unknown type %x\n", lopt);
-        }
+       char    buf[12];
+       int     sz;
+
+       sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
+       printf("lrh=[type=%X len=%d idx=%d] fid="DFID
+              " compound/cookie="LPX64"/"LPX64
+              " status=%s action=%s archive#=%d flags="LPX64
+              " create="LPU64" change="LPU64
+              " extent="LPX64"-"LPX64" gid="LPX64" datalen=%d"
+              " data=[%s]\n",
+              larr->arr_hdr.lrh_type,
+              larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
+              PFID(&larr->arr_hai.hai_fid),
+              larr->arr_compound_id, larr->arr_hai.hai_cookie,
+              agent_req_status2name(larr->arr_status),
+              hsm_copytool_action2name(larr->arr_hai.hai_action),
+              larr->arr_archive_id,
+              larr->arr_flags,
+              larr->arr_req_create, larr->arr_req_change,
+              larr->arr_hai.hai_extent.offset,
+              larr->arr_hai.hai_extent.length,
+              larr->arr_hai.hai_gid, sz,
+              hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
 }
+
+void print_changelog_rec(struct llog_changelog_rec *rec)
+{
+       printf("changelog record id:0x%x cr_flags:0x%x cr_type:%s(0x%x)\n",
+              le32_to_cpu(rec->cr_hdr.lrh_id),
+              le32_to_cpu(rec->cr.cr_flags),
+              changelog_type2str(le32_to_cpu(rec->cr.cr_type)),
+              le32_to_cpu(rec->cr.cr_type));
+}
+
+static void print_records(struct llog_rec_hdr **recs,
+                         int rec_number, int is_ext)
+{
+       __u32 lopt;
+       int i, skip = 0;
+
+       for (i = 0; i < rec_number; i++) {
+               printf("#%.2d (%.3d)", le32_to_cpu(recs[i]->lrh_index),
+                      le32_to_cpu(recs[i]->lrh_len));
+
+               lopt = le32_to_cpu(recs[i]->lrh_type);
+
+               if (recs[i]->lrh_id == CANCELLED)
+                       printf("NOT SET ");
+
+               switch (lopt) {
+               case OBD_CFG_REC:
+                       print_lustre_cfg(
+                               (struct lustre_cfg *)((char *)(recs[i]) +
+                               sizeof(struct llog_rec_hdr)), &skip);
+                       break;
+               case LLOG_PAD_MAGIC:
+                       printf("padding\n");
+                       break;
+               case LLOG_LOGID_MAGIC:
+                       print_log_path((struct llog_logid_rec *)recs[i],
+                                      is_ext);
+                       break;
+               case HSM_AGENT_REC:
+                       print_hsm_action((struct llog_agent_req_rec *)recs[i]);
+                       break;
+               case CHANGELOG_REC:
+                       print_changelog_rec((struct llog_changelog_rec *)
+                                           recs[i]);
+                       break;
+               case CHANGELOG_USER_REC:
+                       printf("changelog_user record id:0x%x\n",
+                              le32_to_cpu(recs[i]->lrh_id));
+                       break;
+               default:
+                       printf("unknown type %x\n", lopt);
+                       break;
+               }
+       }
+}
+
+/** @} llog_reader */