Whamcloud - gitweb
LU-8021 tests: In interop, ensure to save/restore correct debug flags
[fs/lustre-release.git] / lustre / utils / llog_reader.c
index 6ebbe02..c5922c1 100644 (file)
  * @{
  */
 
+#include <errno.h>
+#include <limits.h>
+#include <stdbool.h>
 #include <stdio.h>
+#include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #ifdef HAVE_ENDIAN_H
@@ -50,7 +54,7 @@
 #include <fcntl.h>
 #include <sys/vfs.h>
 #include <linux/magic.h>
-
+#include <errno.h>
 #include <time.h>
 #include <lnet/nidstr.h>
 #include <lustre/lustre_idl.h>
@@ -65,8 +69,8 @@ static inline int ext2_test_bit(int nr, const void *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;
+       return ((1UL << (nr & (__WORDSIZE - 1))) &
+               ((tmp)[nr / __WORDSIZE])) != 0;
 #endif
 }
 
@@ -143,12 +147,13 @@ static void print_log_path(struct llog_logid_rec *lid, int is_ext)
 
        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);
+                        "O/%ju/d%u/%u", (uintmax_t)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) ,
+                        "oi.%ju/"DFID_NOBRACE,
+                        (uintmax_t)(fid_from_logid.f_seq & (OSD_OI_FID_NR - 1)),
                         PFID(&fid_from_logid));
 
        printf("ogen=%X id="DOSTID" path=%s\n",
@@ -181,9 +186,10 @@ int main(int argc, char **argv)
 
        is_ext = is_fstype_ext(fd);
        if (is_ext < 0) {
+               rc = is_ext;
                printf("Unable to determine type of filesystem containing %s\n",
                       argv[1]);
-               goto out;
+               goto out_fd;
        }
 
        rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
@@ -208,8 +214,8 @@ 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;
-       off_t file_size;
+       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;
@@ -222,10 +228,14 @@ int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
                llapi_error(LLAPI_MSG_ERROR, rc, "Got file stat error.");
                goto out;
        }
+
        file_size = st.st_size;
-       if (file_size == 0) {
-               rc = -1;
-               llapi_error(LLAPI_MSG_ERROR, rc, "File is empty.");
+       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;
        }
 
@@ -237,20 +247,29 @@ int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
        }
        *llog = (struct llog_log_hdr *)file_buf;
 
-       rd = read(fd, file_buf, file_size);
+       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 = -EIO; /*FIXME*/
-               llapi_error(LLAPI_MSG_ERROR, rc, "Read file error.");
+               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(struct llog_rec_hdr *));
+       recs_buf = malloc(recs_num * sizeof(**recs_pr));
        if (recs_buf == NULL) {
                rc = -ENOMEM;
-               llapi_error(LLAPI_MSG_ERROR, rc, "Memory Alloc for recs_buf.");
+               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;
@@ -263,8 +282,7 @@ int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
                int idx;
                unsigned long offset;
 
-               if (ptr + sizeof(struct llog_rec_hdr) >
-                   file_buf + file_size) {
+               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);
@@ -352,8 +370,8 @@ static void print_1_cfg(struct lustre_cfg *lcfg)
         int i;
 
         if (lcfg->lcfg_nid)
-                printf("nid=%s("LPX64")  ", libcfs_nid2str(lcfg->lcfg_nid),
-                       lcfg->lcfg_nid);
+               printf("nid=%s(%#jx)  ", libcfs_nid2str(lcfg->lcfg_nid),
+                      (uintmax_t)lcfg->lcfg_nid);
         if (lcfg->lcfg_nal)
                 printf("nal=%d ", lcfg->lcfg_nal);
         for (i = 0; i <  lcfg->lcfg_bufcount; i++)
@@ -375,8 +393,9 @@ static void print_setup_cfg(struct lustre_cfg *lcfg)
                 desc = (struct lov_desc*)(lustre_cfg_string(lcfg, 1));
                 printf("\t\tuuid=%s  ", (char*)desc->ld_uuid.uuid);
                 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
-                printf("size="LPU64" ", desc->ld_default_stripe_size);
-                printf("offset="LPU64" ", desc->ld_default_stripe_offset);
+               printf("size=%ju ", (uintmax_t)desc->ld_default_stripe_size);
+               printf("offset=%ju ",
+                      (uintmax_t)desc->ld_default_stripe_offset);
                 printf("pattern=%#x", desc->ld_pattern);
         } else {
                 printf("setup     ");
@@ -581,23 +600,25 @@ static void print_hsm_action(struct llog_agent_req_rec *larr)
 
        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"
+              " compound/cookie=%#jx/%#jx"
+              " status=%s action=%s archive#=%d flags=%#jx"
+              " create=%ju change=%ju"
+              " extent=%#jx-%#jx gid=%#jx 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,
+              (uintmax_t)larr->arr_compound_id,
+              (uintmax_t)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,
+              (uintmax_t)larr->arr_flags,
+              (uintmax_t)larr->arr_req_create,
+              (uintmax_t)larr->arr_req_change,
+              (uintmax_t)larr->arr_hai.hai_extent.offset,
+              (uintmax_t)larr->arr_hai.hai_extent.length,
+              (uintmax_t)larr->arr_hai.hai_gid, sz,
               hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
 }