Whamcloud - gitweb
LU-6030 ldiskfs: support pdirop split for RHEL6.5
[fs/lustre-release.git] / lustre / utils / llog_reader.c
index a0d0b3a..cf84329 100644 (file)
@@ -27,7 +27,7 @@
  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2012, Intel Corporation.
+ * Copyright (c) 2011, 2014, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <endian.h>
+#ifdef HAVE_ENDIAN_H
+# include <endian.h>
+#endif
+#include <unistd.h>
 #include <fcntl.h>
 
 #include <time.h>
 #include <libcfs/libcfs.h>
 #include <lnet/nidstr.h>
 #include <lustre/lustre_idl.h>
+#include <lustre/lustreapi.h>
 #include <lustre_cfg.h>
 
 static inline int ext2_test_bit(int nr, const void *addr)
@@ -54,7 +58,9 @@ static inline int ext2_test_bit(int nr, const void *addr)
        const unsigned char *tmp = addr;
        return (tmp[nr >> 3] >> (nr & 7)) & 1;
 #else
-       return test_bit(nr, addr);
+       const unsigned long *tmp = addr;
+       return ((1UL << (nr & (BITS_PER_LONG - 1))) &
+               ((tmp)[nr / BITS_PER_LONG])) != 0;
 #endif
 }
 
@@ -106,12 +112,14 @@ int main(int argc, char **argv)
 
         fd = open(argv[1],O_RDONLY);
         if (fd < 0){
-                printf("Could not open the file %s\n", argv[1]);
+               rc = -errno;
+               llapi_error(LLAPI_MSG_ERROR, rc, "Could not open the file %s.",
+                           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);
+               llapi_error(LLAPI_MSG_ERROR, rc, "Could not pack buffer.");
                 goto out_fd;
         }
 
@@ -140,23 +148,29 @@ int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
 
         rc = fstat(fd,&st);
         if (rc < 0){
-                printf("Get file stat error.\n");
+               rc = -errno;
+               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.");
+               goto out;
+       }
 
         file_buf = malloc(file_size);
         if (file_buf == NULL){
-                printf("Memory Alloc for file_buf error.\n");
                 rc = -ENOMEM;
+               llapi_error(LLAPI_MSG_ERROR, rc, "Memory Alloc for file_buf.");
                 goto out;
         }
         *llog = (struct llog_log_hdr*)file_buf;
 
         rd = read(fd,file_buf,file_size);
         if (rd < file_size){
-                printf("Read file error.\n");
                 rc = -EIO; /*FIXME*/
+               llapi_error(LLAPI_MSG_ERROR, rc, "Read file error.");
                 goto clear_file_buf;
         }
 
@@ -165,8 +179,8 @@ int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
 
         recs_buf = malloc(recs_num * sizeof(struct llog_rec_hdr *));
         if (recs_buf == NULL){
-                printf("Memory Alloc for recs_buf error.\n");
                 rc = -ENOMEM;
+               llapi_error(LLAPI_MSG_ERROR, rc, "Memory Alloc for recs_buf.");
                 goto clear_file_buf;
         }
         recs_pr = (struct llog_rec_hdr **)recs_buf;
@@ -175,20 +189,31 @@ int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
         i = 0;
 
         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->lrh_id = CANCELLED;
-                        /* The header counts only set records */
-                        i--;
-                }
+               struct llog_rec_hdr *cur_rec;
+               int idx;
+
+               if (ptr + sizeof(struct llog_rec_hdr) >
+                   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;
+
+               if (ext2_test_bit(idx, LLOG_HDR_BITMAP(*llog))) {
+                       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->lrh_id = CANCELLED;
+                       /* The header counts only set records */
+                       i--;
+               }
 
                 ptr += le32_to_cpu(cur_rec->lrh_len);
                 if ((ptr - file_buf) > file_size) {