Whamcloud - gitweb
LU-9463 utils: print lcme_flags in text format 58/27058/7
authorEmoly Liu <emoly.liu@intel.com>
Wed, 17 May 2017 02:33:37 +0000 (10:33 +0800)
committerOleg Drokin <oleg.drokin@intel.com>
Sat, 3 Jun 2017 03:58:58 +0000 (03:58 +0000)
Print lcme_flags in comma-separated text format(e.g. init)
instead of hex numbers.

Signed-off-by: Emoly Liu <emoly.liu@intel.com>
Change-Id: Iae8b679aef8f7c0b86af3e94d5bb996d4d98757b
Reviewed-on: https://review.whamcloud.com/27058
Tested-by: Jenkins
Reviewed-by: Andreas Dilger <andreas.dilger@intel.com>
Reviewed-by: Niu Yawei <yawei.niu@intel.com>
Tested-by: Maloo <hpdd-maloo@intel.com>
lustre/include/lustre/lustreapi.h
lustre/utils/lfs.c
lustre/utils/liblustreapi.c

index e38956e..67df286 100644 (file)
 #define LL_MAXQUOTAS 3
 #endif
 
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
+#endif
+
 extern bool liblustreapi_initialized;
 
 
@@ -737,6 +741,21 @@ int llapi_layout_comp_extent_get(const struct llapi_layout *layout,
  */
 int llapi_layout_comp_extent_set(struct llapi_layout *layout,
                                 uint64_t start, uint64_t end);
+
+/* PFL component flags table */
+static const struct comp_flag_name {
+       enum lov_comp_md_entry_flags cfn_flag;
+       const char *cfn_name;
+} comp_flags_table[] = {
+       { LCME_FL_INIT,         "init" },
+       /* For now, only "init" is supported
+       { LCME_FL_PRIMARY,      "primary" },
+       { LCME_FL_STALE,        "stale" },
+       { LCME_FL_OFFLINE,      "offline" },
+       { LCME_FL_PREFERRED,    "preferred" }
+       */
+};
+
 /**
  * Gets the attribute flags of the current component.
  */
index 3eb8cec..7fa9c5c 100644 (file)
@@ -1254,25 +1254,37 @@ static inline void comp_flags_clear_neg(__u32 *flags)
        *flags &= ~LCME_FL_NEG;
 }
 
-static int comp_name2flags(__u32 *flags, char *name)
+static int comp_str2flags(__u32 *flags, char *string)
 {
-       char *ptr;
+       char *name;
        __u32 neg_flags = 0;
 
-       if (name == NULL)
+       if (string == NULL)
                return -EINVAL;
 
        *flags = 0;
-       for (ptr = name; ; ptr = NULL) {
-               char *flg = strtok(ptr, ",");
-               if (flg == NULL)
-                       break;
-               if (strcmp(flg, "init") == 0)
-                       *flags |= LCME_FL_INIT;
-               else if (strcmp(flg, "^init") == 0)
-                       neg_flags |= LCME_FL_INIT;
-               else
+       for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
+               bool found = false;
+               int i;
+
+               for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
+                       __u32 comp_flag = comp_flags_table[i].cfn_flag;
+                       const char *comp_name = comp_flags_table[i].cfn_name;
+
+                       if (strcmp(name, comp_name) == 0) {
+                               *flags |= comp_flag;
+                               found = true;
+                       } else if (strncmp(name, "^", 1) == 0 &&
+                                  strcmp(name + 1, comp_name) == 0) {
+                               neg_flags |= comp_flag;
+                               found = true;
+                       }
+               }
+               if (!found) {
+                       llapi_printf(LLAPI_MSG_ERROR, "Component flag "
+                                    "'%s' is not supported.\n", name);
                        return -EINVAL;
+               }
        }
 
        if (*flags == 0 && neg_flags == 0)
@@ -1409,7 +1421,7 @@ static int lfs_setstripe(int argc, char **argv)
                        comp_del = 1;
                        break;
                case LFS_COMP_FLAGS_OPT:
-                       result = comp_name2flags(&lsa.lsa_comp_flags, optarg);
+                       result = comp_str2flags(&lsa.lsa_comp_flags, optarg);
                        if (result != 0) {
                                fprintf(stderr, "error: %s: bad comp flags "
                                        "'%s'\n", argv[0], optarg);
@@ -1999,7 +2011,7 @@ static int lfs_find(int argc, char **argv)
                        param.fp_exclude_comp_count = !!neg_opt;
                        break;
                case LFS_COMP_FLAGS_OPT:
-                       rc = comp_name2flags(&param.fp_comp_flags, optarg);
+                       rc = comp_str2flags(&param.fp_comp_flags, optarg);
                        if (rc || comp_flags_is_neg(param.fp_comp_flags)) {
                                fprintf(stderr, "error: bad component flags "
                                        "'%s'\n", optarg);
@@ -2453,7 +2465,7 @@ static int lfs_getstripe_internal(int argc, char **argv,
                case LFS_COMP_FLAGS_OPT:
                        if (optarg != NULL) {
                                __u32 *flags = &param->fp_comp_flags;
-                               rc = comp_name2flags(flags, optarg);
+                               rc = comp_str2flags(flags, optarg);
                                if (rc != 0) {
                                        fprintf(stderr, "error: %s bad "
                                                "component flags '%s'.\n",
index 31494a3..f4ddd26 100644 (file)
@@ -2618,6 +2618,32 @@ static void lov_dump_comp_v1_header(struct find_param *param, char *path,
                llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
 }
 
+static void comp_flags2str(__u32 comp_flags)
+{
+       bool found = false;
+       int i = 0;
+
+       if (!comp_flags) {
+               llapi_printf(LLAPI_MSG_NORMAL, "0");
+               return;
+       }
+       for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
+               if (comp_flags & comp_flags_table[i].cfn_flag) {
+                       if (found)
+                               llapi_printf(LLAPI_MSG_NORMAL, ",");
+                       llapi_printf(LLAPI_MSG_NORMAL, "%s",
+                                    comp_flags_table[i].cfn_name);
+                       comp_flags &= ~comp_flags_table[i].cfn_flag;
+                       found = true;
+               }
+       }
+       if (comp_flags) {
+               if (found)
+                       llapi_printf(LLAPI_MSG_NORMAL, ",");
+               llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
+       }
+}
+
 static void lov_dump_comp_v1_entry(struct find_param *param,
                                   enum lov_dump_flags flags, int index)
 {
@@ -2651,7 +2677,7 @@ static void lov_dump_comp_v1_entry(struct find_param *param,
                if (verbose & ~VERBOSE_COMP_FLAGS)
                        llapi_printf(LLAPI_MSG_NORMAL,
                                     "%4slcme_flags:          ", " ");
-               llapi_printf(LLAPI_MSG_NORMAL, "%#x", entry->lcme_flags);
+               comp_flags2str(entry->lcme_flags);
                separator = "\n";
        }