Whamcloud - gitweb
LU-9505 llapi: treat MDT index as a hex number
[fs/lustre-release.git] / lustre / utils / liblustreapi.c
index 2226112..2df30c6 100644 (file)
@@ -41,6 +41,7 @@
 #define _GNU_SOURCE
 #endif
 
+#include <ctype.h>
 #include <mntent.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -1234,8 +1235,7 @@ int llapi_get_poollist(const char *name, char **poollist, int list_size,
        char *fsname;
         char *ptr;
         DIR *dir;
-        struct dirent pool;
-        struct dirent *cookie = NULL;
+       struct dirent *pool;
         int rc = 0;
         unsigned int nb_entries = 0;
         unsigned int used = 0;
@@ -1287,20 +1287,16 @@ int llapi_get_poollist(const char *name, char **poollist, int list_size,
                goto free_path;
        }
 
-       while(1) {
-               rc = readdir_r(dir, &pool, &cookie);
-               if (rc != 0) {
+       do {
+               errno = 0;
+               pool = readdir(dir);
+               if (pool == NULL) {
                        rc = -errno;
-                       llapi_error(LLAPI_MSG_ERROR, rc,
-                                   "Error reading pool list for '%s'", name);
-                       goto free_path;
-               } else if ((rc == 0) && (cookie == NULL)) {
-                       /* end of directory */
-                       break;
+                       goto free_dir;
                }
 
                 /* ignore . and .. */
-                if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
+               if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
                         continue;
 
                 /* check output bounds */
@@ -1310,19 +1306,22 @@ int llapi_get_poollist(const char *name, char **poollist, int list_size,
                }
 
                 /* +2 for '.' and final '\0' */
-               if (used + strlen(pool.d_name) + strlen(fsname) + 2
+               if (used + strlen(pool->d_name) + strlen(fsname) + 2
                    > buffer_size) {
                        rc = -EOVERFLOW;
                        goto free_dir;
                }
 
-                sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
+               sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
                 poollist[nb_entries] = buffer + used;
-                used += strlen(pool.d_name) + strlen(fsname) + 2;
+               used += strlen(pool->d_name) + strlen(fsname) + 2;
                 nb_entries++;
-        }
+       } while (1);
 
 free_dir:
+       if (rc)
+               llapi_error(LLAPI_MSG_ERROR, rc,
+                           "Error reading pool list for '%s'", name);
        closedir(dir);
 free_path:
        cfs_free_param_data(&pathname);
@@ -2196,6 +2195,7 @@ enum lov_dump_flags {
        LDF_IS_RAW      = 0x0002,
        LDF_INDENT      = 0x0004,
        LDF_SKIP_OBJS   = 0x0008,
+       LDF_YAML        = 0x0010,
 };
 
 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
@@ -2206,6 +2206,7 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
        bool is_dir = flags & LDF_IS_DIR;
        bool is_raw = flags & LDF_IS_RAW;
        bool indent = flags & LDF_INDENT;
+       bool yaml = flags & LDF_YAML;
        bool skip_objs = flags & LDF_SKIP_OBJS;
        char *prefix = is_dir ? "" : "lmm_";
        char *separator = "";
@@ -2218,7 +2219,8 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
                        llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
        }
 
-       if (!indent && depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
+       if (!yaml && !indent && depth && path &&
+           ((verbose != VERBOSE_OBJID) || !is_dir))
                llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
 
        if ((verbose & VERBOSE_DETAIL) && !is_dir) {
@@ -2232,6 +2234,10 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
                             (uintmax_t)lmm_oi_id(&lum->lmm_oi));
        }
        if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
+               __u64 seq;
+               __u32 oid;
+               __u32 ver;
+
                if (verbose & ~VERBOSE_DFID)
                        llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid:           ",
                                     space);
@@ -2252,13 +2258,17 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
                 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
                 *
                 * For newer layout types hopefully this will be a real FID. */
-               llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
-                            lmm_oi_seq(&lum->lmm_oi) == 0 ?
-                               lmm_oi_id(&lum->lmm_oi) :
-                               lmm_oi_seq(&lum->lmm_oi),
-                            lmm_oi_seq(&lum->lmm_oi) == 0 ?
-                               0 : (__u32)lmm_oi_id(&lum->lmm_oi),
-                            (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32));
+               seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
+                       lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
+               oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
+                       0 : (__u32)lmm_oi_id(&lum->lmm_oi);
+               ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
+               if (yaml)
+                       llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
+                                    seq, oid, ver);
+               else
+                       llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
+                                    seq, oid, ver);
        }
 
        if (verbose & VERBOSE_COUNT) {
@@ -2288,7 +2298,10 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
                        llapi_printf(LLAPI_MSG_NORMAL, "%hd",
                                     (__s16)lum->lmm_stripe_count);
                }
-               separator = is_dir ? " " : "\n";
+               if (!yaml && is_dir)
+                       separator = " ";
+               else
+                       separator = "\n";
        }
 
        if (verbose & VERBOSE_SIZE) {
@@ -2310,7 +2323,10 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
                        llapi_printf(LLAPI_MSG_NORMAL, "%u",
                                     lum->lmm_stripe_size);
                }
-               separator = is_dir ? " " : "\n";
+               if (!yaml && is_dir)
+                       separator = " ";
+               else
+                       separator = "\n";
        }
 
        if ((verbose & VERBOSE_LAYOUT) && !is_dir) {
@@ -2345,7 +2361,10 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
                else
                        llapi_printf(LLAPI_MSG_NORMAL, "%u",
                                     objects[0].l_ost_idx);
-               separator = is_dir ? " " : "\n";
+               if (!yaml && is_dir)
+                       separator = " ";
+               else
+                       separator = "\n";
        }
 
        if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
@@ -2354,7 +2373,10 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
                        llapi_printf(LLAPI_MSG_NORMAL, "%s%spool:          ",
                                     space, prefix);
                llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
-               separator = is_dir ? " " : "\n";
+               if (!yaml && is_dir)
+                       separator = " ";
+               else
+                       separator = "\n";
        }
 
        if (strlen(separator) != 0)
@@ -2369,6 +2391,7 @@ void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
        bool is_dir = flags & LDF_IS_DIR;
        bool indent = flags & LDF_INDENT;
        bool skip_objs = flags & LDF_SKIP_OBJS;
+       bool yaml = flags & LDF_YAML;
        int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
 
        if (!obdstripe) {
@@ -2393,6 +2416,8 @@ void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
                if (indent)
                        llapi_printf(LLAPI_MSG_NORMAL,
                                     "%6slmm_objects:\n", " ");
+               else if (yaml)
+                       llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
                else
                        llapi_printf(LLAPI_MSG_NORMAL,
                                "\tobdidx\t\t objid\t\t objid\t\t group\n");
@@ -2405,7 +2430,16 @@ void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
                        if (obdindex != OBD_NOT_FOUND && obdindex != idx)
                                continue;
 
-                       if (indent) {
+                       if (yaml) {
+                               struct lu_fid fid = { 0 };
+
+                               ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
+                               llapi_printf(LLAPI_MSG_NORMAL,
+                                   "%sl_ost_idx: %d\n", space, idx);
+                               llapi_printf(LLAPI_MSG_NORMAL,
+                                   "%8sl_fid:     "DFID_NOBRACE"\n",
+                                   " ", PFID(&fid));
+                       } else if (indent) {
                                struct lu_fid fid = { 0 };
 
                                ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
@@ -2429,12 +2463,14 @@ void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
 }
 
 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
-                      char *path, int obdindex, int depth, int verbose)
+                      char *path, int obdindex, int depth, int verbose,
+                      enum lov_dump_flags flags)
 {
        struct lmv_user_mds_data *objects = lum->lum_objects;
        char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
        int i, obdstripe = 0;
        char *separator = "";
+       bool yaml = flags & LDF_YAML;
 
        if (obdindex != OBD_NOT_FOUND) {
                if (lum->lum_stripe_count == 0) {
@@ -2476,7 +2512,7 @@ void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
                        llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
                llapi_printf(LLAPI_MSG_NORMAL, "%u",
                             (int)lum->lum_stripe_count);
-               if (verbose & VERBOSE_OFFSET)
+               if ((verbose & VERBOSE_OFFSET) && !yaml)
                        separator = " ";
                else
                        separator = "\n";
@@ -2488,7 +2524,7 @@ void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
                        llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
                llapi_printf(LLAPI_MSG_NORMAL, "%d",
                             (int)lum->lum_stripe_offset);
-               if (verbose & VERBOSE_HASH_TYPE)
+               if (verbose & VERBOSE_HASH_TYPE && !yaml)
                        separator = " ";
                else
                        separator = "\n";
@@ -2546,9 +2582,10 @@ static void lov_dump_comp_v1_header(struct find_param *param, char *path,
        struct lov_comp_md_v1 *comp_v1 = (void *)&param->fp_lmd->lmd_lmm;
        int depth = param->fp_max_depth;
        int verbose = param->fp_verbose;
+       bool yaml = flags & LDF_YAML;
 
        if (depth && path && ((verbose != VERBOSE_OBJID) ||
-                             !(flags & LDF_IS_DIR)))
+                             !(flags & LDF_IS_DIR)) && !yaml)
                llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
 
        if (verbose & VERBOSE_DETAIL) {
@@ -2573,13 +2610,40 @@ static void lov_dump_comp_v1_header(struct find_param *param, char *path,
                        llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
                                     " ");
                llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
-                            comp_v1->lcm_entry_count);
+                            comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
+                            comp_v1->lcm_entry_count : 0);
        }
 
-       if (verbose & VERBOSE_DETAIL)
+       if (verbose & VERBOSE_DETAIL && !yaml)
                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)
 {
@@ -2587,17 +2651,21 @@ static void lov_dump_comp_v1_entry(struct find_param *param,
        struct lov_comp_md_entry_v1 *entry;
        char *separator = "";
        int verbose = param->fp_verbose;
+       bool yaml = flags & LDF_YAML;
 
        entry = &comp_v1->lcm_entries[index];
 
+       if (yaml)
+               llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
+
        if (verbose & VERBOSE_COMP_ID) {
-               if (verbose & VERBOSE_DETAIL)
+               if (verbose & VERBOSE_DETAIL && !yaml)
                        llapi_printf(LLAPI_MSG_NORMAL,
                                     "%slcme_id:             ", "  - ");
                else if (verbose & ~VERBOSE_COMP_ID)
                        llapi_printf(LLAPI_MSG_NORMAL,
                                     "%4slcme_id:             ", " ");
-               if (!(flags & LDF_IS_DIR))
+               if (entry->lcme_id != LCME_ID_INVAL)
                        llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
                else
                        llapi_printf(LLAPI_MSG_NORMAL, "N/A");
@@ -2609,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";
        }
 
@@ -2636,7 +2704,10 @@ static void lov_dump_comp_v1_entry(struct find_param *param,
                separator = "\n";
        }
 
-       if (verbose & VERBOSE_DETAIL) {
+       if (yaml) {
+               llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
+               llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
+       } else if (verbose & VERBOSE_DETAIL) {
                llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
                llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset:         %u\n",
                             " ", entry->lcme_offset);
@@ -2718,6 +2789,46 @@ lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
                pool_name[0] = '\0';
 }
 
+static inline bool
+print_last_init_comp(struct find_param *param)
+{
+       /* print all component info */
+       if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
+               return false;
+
+       /* print specific component info */
+       if (param->fp_check_comp_id || param->fp_check_comp_flags ||
+           param->fp_check_comp_start || param->fp_check_comp_end)
+               return false;
+
+       return true;
+}
+
+static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
+{
+       int match;
+
+       if (param->fp_comp_end == LUSTRE_EOF) {
+               if (param->fp_comp_end_sign == 0) /* equal to EOF */
+                       match = end == LUSTRE_EOF ? 1 : -1;
+               else if (param->fp_comp_end_sign > 0) /* at most EOF */
+                       match = end == LUSTRE_EOF ? -1 : 1;
+               else /* at least EOF */
+                       match = -1;
+               if (param->fp_exclude_comp_end)
+                       match = ~match + 1;
+       } else {
+               unsigned long long margin;
+
+               margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
+               match = find_value_cmp(end, param->fp_comp_end,
+                                      param->fp_comp_end_sign,
+                                      param->fp_exclude_comp_end, margin, 0);
+       }
+
+       return match;
+}
+
 /**
  * An example of "getstripe -v" for a two components PFL file:
  *
@@ -2809,14 +2920,13 @@ static void lov_dump_comp_v1(struct find_param *param, char *path,
        for (i = 0; i < comp_v1->lcm_entry_count; i++) {
                entry = &comp_v1->lcm_entries[i];
 
-               if (param->fp_check_comp_flags &&
-                   !(param->fp_comp_flags & entry->lcme_flags))
-                       continue;
-
-               if (entry->lcme_flags & LCME_FL_INIT)
-                       flags &= ~LDF_SKIP_OBJS;
-               else
-                       flags |= LDF_SKIP_OBJS;
+               if (param->fp_check_comp_flags) {
+                       if ((param->fp_exclude_comp_flags &&
+                            (param->fp_comp_flags & entry->lcme_flags)) ||
+                           (!param->fp_exclude_comp_flags &&
+                            !(param->fp_comp_flags & entry->lcme_flags)))
+                               continue;
+               }
 
                if (param->fp_check_comp_id &&
                    param->fp_comp_id != entry->lcme_id)
@@ -2833,19 +2943,49 @@ static void lov_dump_comp_v1(struct find_param *param, char *path,
                }
 
                if (param->fp_check_comp_end) {
-                       unsigned long long margin;
-
-                       margin = entry->lcme_extent.e_end == LUSTRE_EOF ?
-                               0 : param->fp_comp_end_units;
-
-                       match = find_value_cmp(entry->lcme_extent.e_end,
-                                              param->fp_comp_end,
-                                              param->fp_comp_end_sign,
-                                              0, margin, 0);
+                       match = find_comp_end_cmp(entry->lcme_extent.e_end,
+                                                 param);
                        if (match == -1)
                                continue;
                }
 
+               if (print_last_init_comp(param)) {
+                       /**
+                        * if part of stripe info is needed, we'd print only
+                        * the last instantiated component info.
+                        */
+                       if (entry->lcme_flags & LCME_FL_INIT)
+                               continue;
+                       else
+                               break;
+               }
+
+               if (entry->lcme_flags & LCME_FL_INIT)
+                       flags &= ~LDF_SKIP_OBJS;
+               else
+                       flags |= LDF_SKIP_OBJS;
+
+               lov_dump_comp_v1_entry(param, flags, i);
+
+               v1 = lov_comp_entry(comp_v1, i);
+               objects = lov_v1v3_objects(v1);
+               lov_v1v3_pool_name(v1, pool_name);
+
+               lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
+                                      param->fp_max_depth, param->fp_verbose,
+                                      flags);
+       }
+       if (print_last_init_comp(param)) {
+               /**
+                * directory layout contains only layout template, print the
+                * last component.
+                */
+               if (i == 0)
+                       i = comp_v1->lcm_entry_count - 1;
+               else
+                       i--;
+               flags &= ~LDF_SKIP_OBJS;
+
                lov_dump_comp_v1_entry(param, flags, i);
 
                v1 = lov_comp_entry(comp_v1, i);
@@ -2858,28 +2998,91 @@ static void lov_dump_comp_v1(struct find_param *param, char *path,
        }
 }
 
-static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
-                                   enum lov_dump_flags flags)
+#define VERBOSE_COMP_OPTS      (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
+                                VERBOSE_COMP_START | VERBOSE_COMP_END | \
+                                VERBOSE_COMP_FLAGS)
+
+static inline bool has_any_comp_options(struct find_param *param)
 {
-       __u32 magic;
+       int verbose = param->fp_verbose;
 
-       if (param->fp_get_lmv || param->fp_get_default_lmv)
-               magic = (__u32)param->fp_lmv_md->lum_magic;
-       else
-               magic = *(__u32 *)&param->fp_lmd->lmd_lmm; /* lum->lmm_magic */
+       if (param->fp_check_comp_id || param->fp_check_comp_count ||
+           param->fp_check_comp_start || param->fp_check_comp_end ||
+           param->fp_check_comp_flags)
+               return true;
 
-       if (param->fp_raw)
-               flags |= LDF_IS_RAW;
+       /* show full layout information, not component specific */
+       if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
+               return false;
 
-       switch (magic) {
-       case LOV_USER_MAGIC_V1:
+       return verbose & VERBOSE_COMP_OPTS;
+}
+
+struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
+                                           bool is_dir)
+{
+       struct lov_user_md *lum = &orig->lmd_lmm;
+       struct lov_user_mds_data *new;
+       struct lov_comp_md_v1 *comp_v1;
+       struct lov_comp_md_entry_v1 *ent;
+       int lum_off = sizeof(*comp_v1) + sizeof(*ent);
+       int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
+                                       lum->lmm_magic);
+
+       new = malloc(sizeof(lstat_t) + lum_off + lum_size);
+       if (new == NULL) {
+               llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
+               return new;
+       }
+
+       memcpy(new, orig, sizeof(lstat_t));
+
+       comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
+       comp_v1->lcm_magic = lum->lmm_magic;
+       comp_v1->lcm_size = lum_off + lum_size;
+       comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
+       comp_v1->lcm_flags = 0;
+       comp_v1->lcm_entry_count = 1;
+
+       ent = &comp_v1->lcm_entries[0];
+       ent->lcme_id = 0;
+       ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
+       ent->lcme_extent.e_start = 0;
+       ent->lcme_extent.e_end = LUSTRE_EOF;
+       ent->lcme_offset = lum_off;
+       ent->lcme_size = lum_size;
+
+       memcpy((char *)comp_v1 + lum_off, lum, lum_size);
+
+       return new;
+}
+
+static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
+                                   enum lov_dump_flags flags)
+{
+       __u32 magic = *(__u32 *)&param->fp_lmd->lmd_lmm;
+
+       if (has_any_comp_options(param)) {
+               struct lov_user_mds_data *new_lmd, *orig_lmd;
+
+               orig_lmd = param->fp_lmd;
+               new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
+               if (new_lmd != NULL) {
+                       param->fp_lmd = new_lmd;
+                       lov_dump_comp_v1(param, path, flags);
+                       param->fp_lmd = orig_lmd;
+                       free(new_lmd);
+               }
+               return;
+       }
+
+       if (magic == LOV_USER_MAGIC_V1) {
                lov_dump_user_lmm_v1v3(&param->fp_lmd->lmd_lmm, NULL,
                                       param->fp_lmd->lmd_lmm.lmm_objects,
                                       path, param->fp_obd_index,
                                       param->fp_max_depth, param->fp_verbose,
                                       flags);
-               break;
-       case LOV_USER_MAGIC_V3: {
+       } else {
                char pool_name[LOV_MAXPOOLNAME + 1];
                struct lov_user_ost_data_v1 *objects;
                struct lov_user_md_v3 *lmmv3 = (void *)&param->fp_lmd->lmd_lmm;
@@ -2890,8 +3093,29 @@ static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
                                       objects, path, param->fp_obd_index,
                                       param->fp_max_depth, param->fp_verbose,
                                       flags);
+       }
+}
+
+static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
+                                   enum lov_dump_flags flags)
+{
+       __u32 magic;
+
+       if (param->fp_get_lmv || param->fp_get_default_lmv)
+               magic = (__u32)param->fp_lmv_md->lum_magic;
+       else
+               magic = *(__u32 *)&param->fp_lmd->lmd_lmm; /* lum->lmm_magic */
+
+       if (param->fp_raw)
+               flags |= LDF_IS_RAW;
+       if (param->fp_yaml)
+               flags |= LDF_YAML;
+
+       switch (magic) {
+       case LOV_USER_MAGIC_V1:
+       case LOV_USER_MAGIC_V3:
+               lov_dump_plain_user_lmm(param, path, flags);
                break;
-        }
        case LMV_MAGIC_V1:
        case LMV_USER_MAGIC: {
                char pool_name[LOV_MAXPOOLNAME + 1];
@@ -2900,7 +3124,8 @@ static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
                lum = (struct lmv_user_md *)param->fp_lmv_md;
                strlcpy(pool_name, lum->lum_pool_name, sizeof(pool_name));
                lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
-                                 param->fp_max_depth, param->fp_verbose);
+                                 param->fp_max_depth, param->fp_verbose,
+                                 flags);
                break;
        }
        case LOV_USER_MAGIC_COMP_V1:
@@ -3258,50 +3483,46 @@ static int find_check_pool(struct find_param *param)
 
 static int find_check_comp_options(struct find_param *param)
 {
-       struct lov_comp_md_v1 *comp_v1;
+       lstat_t *st = &param->fp_lmd->lmd_st;
+       struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
        struct lov_user_md_v1 *v1 = &param->fp_lmd->lmd_lmm;
        struct lov_comp_md_entry_v1 *entry;
-       int i, ret;
-
-       if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1) {
-               if ((param->fp_check_comp_count &&
-                    !param->fp_exclude_comp_count) ||
-                   (param->fp_check_comp_flags &&
-                    !param->fp_exclude_comp_flags) ||
-                   (param->fp_check_comp_start &&
-                    !param->fp_exclude_comp_start) ||
-                   (param->fp_check_comp_end &&
-                    !param->fp_exclude_comp_end))
+       int i, ret = 0;
+
+       if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
+               comp_v1 = (struct lov_comp_md_v1 *)v1;
+       } else {
+               forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
+               if (forged_v1 == NULL)
                        return -1;
-               else
-                       return 1;
+               comp_v1 = forged_v1;
+               comp_v1->lcm_entry_count = 1;
+               entry = &comp_v1->lcm_entries[0];
+               entry->lcme_flags = S_ISDIR(st->st_mode) ? 0 : LCME_FL_INIT;
+               entry->lcme_extent.e_start = 0;
+               entry->lcme_extent.e_end = LUSTRE_EOF;
        }
 
-       comp_v1 = (struct lov_comp_md_v1 *)v1;
+       /* invalid case, don't match for any kind of search. */
+       if (comp_v1->lcm_entry_count == 0) {
+               ret = -1;
+               goto out;
+       }
 
        if (param->fp_check_comp_count) {
-               ret = find_value_cmp(comp_v1->lcm_entry_count,
+               ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
                                     param->fp_comp_count,
                                     param->fp_comp_count_sign,
                                     param->fp_exclude_comp_count, 1, 0);
                if (ret == -1)
-                       return ret;
-       }
-
-       if (comp_v1->lcm_entry_count == 0) {
-               if ((param->fp_check_comp_flags &&
-                    !param->fp_exclude_comp_flags) ||
-                   (param->fp_check_comp_start &&
-                    !param->fp_exclude_comp_start) ||
-                   (param->fp_check_comp_end &&
-                    !param->fp_exclude_comp_end))
-                       return -1;
+                       goto out;
        }
 
-       if (param->fp_check_comp_flags) {
-               for (i = 0; i < comp_v1->lcm_entry_count; i++) {
-                       entry = &comp_v1->lcm_entries[i];
+       ret = 1;
+       for (i = 0; i < comp_v1->lcm_entry_count; i++) {
+               entry = &comp_v1->lcm_entries[i];
 
+               if (param->fp_check_comp_flags) {
                        if (((entry->lcme_flags & param->fp_comp_flags) &&
                             param->fp_exclude_comp_flags) ||
                            (!(entry->lcme_flags & param->fp_comp_flags) &&
@@ -3309,53 +3530,35 @@ static int find_check_comp_options(struct find_param *param)
                                ret = -1;
                        else
                                ret = 1;
-                       /* If any flags matches */
-                       if (ret != -1)
-                               break;
-               }
-               if (ret == -1)
-                       return ret;
-       }
 
-       if (param->fp_check_comp_start) {
-               for (i = 0; i < comp_v1->lcm_entry_count; i++) {
-                       entry = &comp_v1->lcm_entries[i];
+                       if (ret == -1)
+                               continue;
+               }
 
+               if (param->fp_check_comp_start) {
                        ret = find_value_cmp(entry->lcme_extent.e_start,
                                             param->fp_comp_start,
                                             param->fp_comp_start_sign,
                                             param->fp_exclude_comp_start,
                                             param->fp_comp_start_units, 0);
-                       /* If any extent start matches */
-                       if (ret != -1)
-                               break;
+                       if (ret == -1)
+                               continue;
                }
-               if (ret == -1)
-                       return ret;
-       }
-
-       if (param->fp_check_comp_end) {
-               for (i = 0; i < comp_v1->lcm_entry_count; i++) {
-                       unsigned long long margin;
-                       entry = &comp_v1->lcm_entries[i];
 
-                       margin = entry->lcme_extent.e_end == LUSTRE_EOF ?
-                               0 : param->fp_comp_end_units;
-
-                       ret = find_value_cmp(entry->lcme_extent.e_end,
-                                            param->fp_comp_end,
-                                            param->fp_comp_end_sign,
-                                            param->fp_exclude_comp_end, margin,
-                                            0);
-                       /* If any extent end matches */
-                       if (ret != -1)
-                               break;
+               if (param->fp_check_comp_end) {
+                       ret = find_comp_end_cmp(entry->lcme_extent.e_end,
+                                               param);
+                       if (ret == -1)
+                               continue;
                }
-               if (ret == -1)
-                       return ret;
-       }
 
-       return 1;
+               /* the component matches all criteria */
+               break;
+       }
+out:
+       if (forged_v1 != NULL)
+               free(forged_v1);
+       return ret;
 }
 
 static bool find_check_lmm_info(struct find_param *param)
@@ -3445,6 +3648,8 @@ static int cb_find_init(char *path, DIR *parent, DIR **dirp,
                        if (ret != 0)
                                return ret;
                }
+
+               param->fp_lmd->lmd_lmm.lmm_magic = 0;
                ret = get_lmd_info(path, parent, dir, param->fp_lmd,
                                   param->fp_lum_size);
                if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
@@ -4301,42 +4506,42 @@ static int get_mdtname(char *name, char *format, char *buf)
 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
               int want_error)
 {
-        char fsname[20];
-        char *ptr;
+       char fsname[20];
+       char *ptr;
        int fd, rc;
        long index;
 
-        /* Take path, fsname, or MDTname.  Assume MDT0000 in the former cases.
-         Open root and parse mdt index. */
-        if (mdtname[0] == '/') {
-                index = 0;
-                rc = get_root_path(WANT_FD | want_error, NULL, &fd,
-                                   (char *)mdtname, -1);
-        } else {
-                if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
-                        return -EINVAL;
-                ptr = fsname + strlen(fsname) - 8;
-                *ptr = '\0';
-                index = strtol(ptr + 4, NULL, 10);
-                rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
-        }
-        if (rc < 0) {
-                if (want_error)
-                        llapi_err_noerrno(LLAPI_MSG_ERROR,
-                                          "Can't open %s: %d\n", mdtname, rc);
-                return rc;
-        }
+       /* Take path, fsname, or MDTname.  Assume MDT0000 in the former cases.
+        Open root and parse mdt index. */
+       if (mdtname[0] == '/') {
+               index = 0;
+               rc = get_root_path(WANT_FD | want_error, NULL, &fd,
+                                  (char *)mdtname, -1);
+       } else {
+               if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
+                       return -EINVAL;
+               ptr = fsname + strlen(fsname) - 8;
+               *ptr = '\0';
+               index = strtol(ptr + 4, NULL, 16);
+               rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
+       }
+       if (rc < 0) {
+               if (want_error)
+                       llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                         "Can't open %s: %d\n", mdtname, rc);
+               return rc;
+       }
 
-        if (mdtidxp)
-                *mdtidxp = index;
+       if (mdtidxp)
+               *mdtidxp = index;
 
-        rc = ioctl(fd, opc, data);
-        if (rc == -1)
-                rc = -errno;
-        else
-                rc = 0;
-        close(fd);
-        return rc;
+       rc = ioctl(fd, opc, data);
+       if (rc == -1)
+               rc = -errno;
+       else
+               rc = 0;
+       close(fd);
+       return rc;
 }
 
 int llapi_fid2path(const char *device, const char *fidstr, char *buf,