Whamcloud - gitweb
LU-930 utils: fix lfs setstripe option description
[fs/lustre-release.git] / lustre / utils / lfs.c
index 7db51ce..79d34a6 100644 (file)
@@ -71,6 +71,7 @@
 #include <libcfs/util/string.h>
 #include <libcfs/util/ioctl.h>
 #include <libcfs/util/parser.h>
+#include <libcfs/util/string.h>
 #include <lustre/lustreapi.h>
 #include <linux/lustre/lustre_ver.h>
 #include <linux/lustre/lustre_param.h>
@@ -103,6 +104,7 @@ static int lfs_changelog(int argc, char **argv);
 static int lfs_changelog_clear(int argc, char **argv);
 static int lfs_fid2path(int argc, char **argv);
 static int lfs_path2fid(int argc, char **argv);
+static int lfs_rmfid(int argc, char **argv);
 static int lfs_data_version(int argc, char **argv);
 static int lfs_hsm_state(int argc, char **argv);
 static int lfs_hsm_set(int argc, char **argv);
@@ -134,6 +136,10 @@ static int lfs_pcc_detach_fid(int argc, char **argv);
 static int lfs_pcc_state(int argc, char **argv);
 static int lfs_pcc(int argc, char **argv);
 static int lfs_pcc_list_commands(int argc, char **argv);
+static int lfs_migrate_to_dom(int fd, int fdv, char *name,
+                             __u64 migration_flags,
+                             struct llapi_stripe_param *param,
+                             struct llapi_layout *layout);
 
 enum setstripe_origin {
        SO_SETSTRIPE,
@@ -142,6 +148,7 @@ enum setstripe_origin {
        SO_MIRROR_CREATE,
        SO_MIRROR_EXTEND,
        SO_MIRROR_SPLIT,
+       SO_MIRROR_DELETE,
 };
 static int lfs_setstripe_internal(int argc, char **argv,
                                  enum setstripe_origin opc);
@@ -166,6 +173,10 @@ static inline int lfs_mirror_split(int argc, char **argv)
 {
        return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
 }
+static inline int lfs_mirror_delete(int argc, char **argv)
+{
+       return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
+}
 
 /* Setstripe and migrate share mostly the same parameters */
 #define SSM_CMD_COMMON(cmd) \
@@ -175,16 +186,17 @@ static inline int lfs_mirror_split(int argc, char **argv)
        "                 [--stripe-index|-i <start_ost_idx>]\n"        \
        "                 [--stripe-size|-S <stripe_size>]\n"           \
        "                 [--layout|-L <pattern>]\n"                    \
-       "                 [--pool|-p <pool_name>]\n"                    \
+       "                 [--mirror_count|-N[mirror_count]]\n"          \
        "                 [--ost|-o <ost_indices>]\n"                   \
+       "                 [--pool|-p <pool_name>]\n"                    \
        "                 [--yaml|-y <yaml_template_file>]\n"           \
        "                 [--copy=<lustre_src>]\n"
 
 #define SSM_HELP_COMMON \
        "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
        "\t              Using -C instead of -c allows overstriping, which\n" \
-        "\t             will place more than one stripe per OST if\n" \
-        "\t             stripe_count is greater than the number of OSTs\n" \
+       "\t              will place more than one stripe per OST if\n" \
+       "\t              stripe_count is greater than the number of OSTs\n" \
        "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
        "\tstripe_size:  Number of bytes on each OST (0=fs default)\n" \
        "\t              Can be specified with K, M or G (for KB, MB, GB\n" \
@@ -196,7 +208,7 @@ static inline int lfs_mirror_split(int argc, char **argv)
        "\t                -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n"        \
        "\t              Or:\n"                                         \
        "\t                -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n"  \
-       "\t              If --pool is set with --ost, then the OSTs\n"  \
+       "\t              If --pool is set with --ost then the OSTs\n"   \
        "\t              must be the members of the pool.\n"            \
        "\tcomp_end:     Extent end of component, start after previous end.\n"\
        "\t              Can be specified with K, M or G (for KB, MB, GB\n" \
@@ -216,7 +228,7 @@ static inline int lfs_mirror_split(int argc, char **argv)
        "\t              it must follow the option without a space.\n"         \
        "\t              The option can also be repeated multiple times to\n"  \
        "\t              separate mirrors that have different layouts.\n"      \
-       "\tsetstripe options: Mirror layout\n"                                 \
+       "\tSETSTRIPE_OPTIONS: Mirror layout as with 'setstripe'\n"             \
        "\t              It can be a plain layout or a composite layout.\n"    \
        "\t              If not specified, the stripe options inherited\n"     \
        "\t              from the previous component will be used.\n"          \
@@ -233,8 +245,8 @@ static inline int lfs_mirror_split(int argc, char **argv)
        "\t              file.\n"
 
 #define MIRROR_EXTEND_USAGE                                                   \
-       "                 <--mirror-count|-N[mirror_count]>\n"                 \
-       "                 [setstripe options|-f <victim_file>]\n"              \
+       "                 {--mirror-count|-N[mirror_count]}\n"                 \
+       "                 [SETSTRIPE_OPTIONS|-f|--file <victim_file>]\n"       \
        "                 [--no-verify]\n"
 
 #define SETSTRIPE_USAGE                                                        \
@@ -271,7 +283,7 @@ static inline int lfs_mirror_split(int argc, char **argv)
        "\tmode: the file access permission of the directory (octal)\n" \
        "To create dir with a foreign (free format) layout :\n" \
        "setdirstripe|mkdir --foreign[=<foreign_type>] -x|-xattr <string> " \
-               "[--mode|-m mode] [--flags <hex>] <dir>\n" \
+               "[--mode|-o mode] [--flags <hex>] <dir>\n" \
        "\tmode: the mode of the directory\n" \
        "\tforeign_type: none or daos\n"
 
@@ -283,19 +295,23 @@ command_t mirror_cmdlist[] = {
          .pc_help = "Create a mirrored file.\n"
                "usage: lfs mirror create "
                "<--mirror-count|-N[mirror_count]> "
-               "[setstripe options] ... <filename|directory>\n"
+               "[SETSTRIPE_OPTIONS] ... <filename|directory> ...\n"
          MIRROR_CREATE_HELP },
+       { .pc_name = "delete", .pc_func = lfs_mirror_delete,
+         .pc_help = "delete a mirror from a file.\n"
+               "usage: lfs mirror delete {--comp-id|-I <comp_id>|-p <pool>} <mirrored_file> ...\n"
+       },
        { .pc_name = "extend", .pc_func = lfs_mirror_extend,
          .pc_help = "Extend a mirrored file.\n"
                "usage: lfs mirror extend "
                "<--mirror-count|-N[mirror_count]> [--no-verify] "
-               "[setstripe options|-f <victim_file>] ... <filename>\n"
+               "[SETSTRIPE_OPTIONS|-f <victim_file>] ... <filename> ...\n"
          MIRROR_EXTEND_HELP },
        { .pc_name = "split", .pc_func = lfs_mirror_split,
          .pc_help = "Split a mirrored file.\n"
        "usage: lfs mirror split <--mirror-id <mirror_id> |\n"
        "\t             <--component-id|-I <comp_id>|-p <pool>> [--destroy|-d]\n"
-       "\t             [-f <new_file>] <mirrored file>\n"
+       "\t             [-f <new_file>] <mirrored_file> ...\n"
        "\tmirror_id:   The numerical unique identifier for a mirror. It\n"
        "\t             can be fetched by lfs getstripe command.\n"
        "\tcomp_id:     Unique component ID within a mirror.\n"
@@ -377,10 +393,10 @@ command_t cmdlist[] = {
         SSM_CMD_COMMON("setstripe --component-add")
         SSM_HELP_COMMON
         "To totally delete the default striping from an existing directory:\n"
-        "usage: setstripe -d <directory>\n"
+        "usage: setstripe [--delete|-d] <directory>\n"
         " or\n"
         "To create a mirrored file or set s default mirror layout on a directory:\n"
-        "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
+        "usage: setstripe {--mirror-count|-N}[mirror_count] [SETSTRIPE_OPTIONS] <directory|filename>\n"
         " or\n"
         "To delete the last component(s) from an existing composite file\n"
         "(note that this will also delete any data in those components):\n"
@@ -443,6 +459,7 @@ command_t cmdlist[] = {
         "usage: find <directory|filename> ...\n"
         "     [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
         "     [[!] --mtime|-M [+-]N[smhdwy]] [[!] --blocks|-b N]\n"
+        "     [[!] --newer[XY] <reference>]\n"
         "     [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
         "     [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
         "     [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
@@ -458,7 +475,7 @@ command_t cmdlist[] = {
         "     [[!] --component-count [+-]<comp_cnt>]\n"
         "     [[!] --component-start [+-]N[kMGTPE]]\n"
         "     [[!] --component-end|-E [+-]N[kMGTPE]]\n"
-        "     [[!] --component-flags <comp_flags>]\n"
+        "     [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
         "     [[!] --mirror-count|-N [+-]<n>]\n"
         "     [[!] --mirror-state <[^]state>]\n"
         "     [[!] --mdt-count|-T [+-]<stripes>]\n"
@@ -481,7 +498,8 @@ command_t cmdlist[] = {
        {"df", lfs_df, 0,
         "report filesystem disk space usage or inodes usage "
         "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
-        "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
+        "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
+        "          [--pool|-p <fsname>[.<pool>]] [path]"},
        {"getname", lfs_getname, 0,
         "list instances and specified mount points [for specified path only]\n"
         "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
@@ -496,8 +514,8 @@ command_t cmdlist[] = {
          "                [--inode-softlimit <inode-softlimit>]\n"
          "                [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
         "       setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
-         "                [--block-grace <block-grace>]\n"
-         "                [--inode-grace <inode-grace>] <filesystem>\n"
+        "                [--block-grace 'notify'|<block-grace>]\n"
+        "                [--inode-grace 'notify'|<inode-grace>] <filesystem>\n"
         "       setquota <-U|-G|-P>\n"
         "                -b <block-softlimit> -B <block-hardlimit>\n"
         "                -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
@@ -520,8 +538,11 @@ command_t cmdlist[] = {
         "      Quota space rebalancing process will stop when this mininum\n"
         "      value is reached. As a result, quota exceeded can be returned\n"
         "      while many targets still have 1MB or 1K inodes of spare\n"
-        "      quota space."},
-        {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
+        "      quota space.\n\n"
+        "      When setting the grace time, 'notify' can be used as grace to\n"
+        "      be notified after the quota is over soft limit but prevents\n"
+        "      the soft limit from becoming the hard limit."},
+       {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
         "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
                       "<ost_idx>]\n"
         "             [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
@@ -552,13 +573,13 @@ command_t cmdlist[] = {
        {"fid2path", lfs_fid2path, 0,
         "Resolve the full path(s) for given FID(s). For a specific hardlink "
         "specify link number <linkno>.\n"
-       /* "For a historical link name, specify changelog record <recno>.\n" */
-        "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
-               /* [ --rec <recno> ] */ },
+        "usage: fid2path [-c] [--link|-l <linkno>] <fsname|root> <fid> ..."},
        {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
         "usage: path2fid [--parents] <path> ..."},
+       {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
+        "usage: rmfid <fsname|rootpath> <fid> ..."},
        {"data_version", lfs_data_version, 0, "Display file data version for "
-        "a given path.\n" "usage: data_version -[n|r|w] <path>"},
+        "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
        {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
         "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
        {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
@@ -696,9 +717,6 @@ static int check_hashtype(const char *hashtype)
                if (strcmp(hashtype, mdt_hash_name[i]) == 0)
                        return i;
 
-       if (!strcmp(hashtype, LMV_HASH_NAME_SPACE))
-               return LMV_HASH_TYPE_DEFAULT | LMV_HASH_FLAG_SPACE;
-
        return 0;
 }
 
@@ -803,12 +821,12 @@ migrate_open_files(const char *name, __u64 migration_flags,
                }
 
                /* create, open a volatile file, use caching (ie no directio) */
-               if (param != NULL)
-                       fdv = llapi_file_open_param(volatile_file, open_flags,
-                                                   open_mode, param);
-               else
+               if (layout)
                        fdv = lfs_component_create(volatile_file, open_flags,
                                                   open_mode, layout);
+               else
+                       fdv = llapi_file_open_param(volatile_file, open_flags,
+                                                   open_mode, param);
        } while (fdv < 0 && (rc = fdv) == -EEXIST);
 
        if (rc < 0) {
@@ -1098,10 +1116,18 @@ static int lfs_component_set(char *fname, int comp_id,
        }
 
        rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
-       if (rc)
-               fprintf(stderr,
-                       "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
-                       progname, comp_id, fname, flags, neg_flags);
+       if (rc) {
+               if (errno == EUCLEAN) {
+                       rc = -errno;
+                       fprintf(stderr,
+                               "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
+                               progname, comp_id, fname);
+               } else {
+                       fprintf(stderr,
+                               "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
+                               progname, comp_id, fname, flags, neg_flags);
+               }
+       }
 
        return rc;
 }
@@ -1182,6 +1208,8 @@ static int lfs_migrate(char *name, __u64 migration_flags,
                       struct llapi_stripe_param *param,
                       struct llapi_layout *layout)
 {
+       struct llapi_layout *existing;
+       uint64_t dom_new, dom_cur;
        int fd = -1;
        int fdv = -1;
        int rc;
@@ -1191,6 +1219,36 @@ static int lfs_migrate(char *name, __u64 migration_flags,
        if (rc < 0)
                goto out;
 
+       rc = llapi_layout_dom_size(layout, &dom_new);
+       if (rc) {
+               error_loc = "cannot get new layout DoM size";
+               goto out;
+       }
+       /* special case for migration to DOM layout*/
+       existing = llapi_layout_get_by_fd(fd, 0);
+       if (!existing) {
+               error_loc = "cannot get existing layout";
+               goto out;
+       }
+
+       rc = llapi_layout_dom_size(existing, &dom_cur);
+       if (rc) {
+               error_loc = "cannot get current layout DoM size";
+               goto out;
+       }
+
+       /* if file has DoM layout already then migration is possible to
+        * the new layout with the same DoM component via swap layout,
+        * if new layout used bigger DOM size, then mirroring is used
+        */
+       if (dom_new > dom_cur) {
+               rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
+                                       layout);
+               if (rc)
+                       error_loc = "cannot migrate to DOM layout";
+               goto out_closed;
+       }
+
        if (!(migration_flags & MIGRATION_NONBLOCK)) {
                /* Blocking mode (forced if servers do not support file lease).
                 * It is also the default mode, since we cannot distinguish
@@ -1227,7 +1285,7 @@ out:
 
        if (fdv >= 0)
                close(fdv);
-
+out_closed:
        if (rc < 0)
                fprintf(stderr, "error: %s: %s: %s: %s\n",
                        progname, name, error_loc, strerror(-rc));
@@ -1819,6 +1877,72 @@ static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
        return LLAPI_LAYOUT_ITER_STOP;
 }
 
+struct collect_ids_data {
+       __u16   *cid_ids;
+       int     cid_count;
+       __u16   cid_exclude;
+};
+
+static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
+{
+       struct collect_ids_data *cid = cbdata;
+       uint32_t id;
+       int rc;
+
+       rc = llapi_layout_mirror_id_get(layout, &id);
+       if (rc < 0)
+               return rc;
+
+       if ((__u16)id != cid->cid_exclude) {
+               int i;
+
+               for (i = 0; i < cid->cid_count; i++) {
+                       /* already collected the mirror id */
+                       if (id == cid->cid_ids[i])
+                               return LLAPI_LAYOUT_ITER_CONT;
+               }
+               cid->cid_ids[cid->cid_count] = id;
+               cid->cid_count++;
+       }
+
+       return LLAPI_LAYOUT_ITER_CONT;
+}
+
+/**
+ * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
+ * @mirror_id: Mirror id to be checked.
+ * @layout:    Mirror component list.
+ *
+ * This function checks if a mirror with specified @mirror_id is the last
+ * non-stale mirror of a layout @layout.
+ *
+ * Return: true or false.
+ */
+static inline
+bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
+{
+       __u16 mirror_ids[128] = { 0 };
+       struct collect_ids_data cid = { .cid_ids = mirror_ids,
+                                       .cid_count = 0,
+                                       .cid_exclude = mirror_id, };
+       int i;
+
+       llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
+
+       for (i = 0; i < cid.cid_count; i++) {
+               struct llapi_resync_comp comp_array[1024] = { { 0 } };
+               int comp_size = 0;
+
+               comp_size = llapi_mirror_find_stale(layout, comp_array,
+                                                   ARRAY_SIZE(comp_array),
+                                                   &mirror_ids[i], 1);
+               if (comp_size == 0)
+                       return false;
+       }
+
+       return true;
+}
+
 static int mirror_split(const char *fname, __u32 id, const char *pool,
                        enum mirror_flags mflags, const char *victim_file)
 {
@@ -1925,6 +2049,14 @@ static int mirror_split(const char *fname, __u32 id, const char *pool,
        if (victim_file == NULL) {
                /* use a temp file to store the splitted layout */
                if (mflags & MF_DESTROY) {
+                       if (last_non_stale_mirror(id, layout)) {
+                               rc = -EUCLEAN;
+                               fprintf(stderr,
+                                       "%s: cannot destroy the last non-stale mirror of file '%s'\n",
+                                       progname, fname);
+                               goto close_fd;
+                       }
+
                        fdv = llapi_create_volatile_idx(parent, mdt_index,
                                                        O_LOV_DELAY_CREATE);
                } else {
@@ -1986,6 +2118,72 @@ free_layout:
        return rc;
 }
 
+static inline
+int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
+                          __u16 *mirror_ids, int ids_nr);
+
+static int lfs_migrate_to_dom(int fd, int fdv, char *name,
+                             __u64 migration_flags,
+                             struct llapi_stripe_param *param,
+                             struct llapi_layout *layout)
+{
+       struct ll_ioc_lease *data = NULL;
+       int rc;
+
+       rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
+       if (rc < 0) {
+               error_loc = "cannot get lease";
+               goto out_close;
+       }
+
+       /* Atomically put lease, merge layouts, resync and close. */
+       data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
+       if (!data) {
+               error_loc = "memory allocation";
+               goto out_close;
+       }
+       data->lil_mode = LL_LEASE_UNLCK;
+       data->lil_flags = LL_LEASE_LAYOUT_MERGE;
+       data->lil_count = 1;
+       data->lil_ids[0] = fdv;
+       rc = llapi_lease_set(fd, data);
+       if (rc < 0) {
+               error_loc = "cannot merge layout";
+               goto out_close;
+       } else if (rc == 0) {
+               rc = -EBUSY;
+               error_loc = "lost lease lock";
+               goto out_close;
+       }
+       close(fd);
+       close(fdv);
+
+       rc = lfs_mirror_resync_file(name, data, NULL, 0);
+       if (rc) {
+               error_loc = "cannot resync file";
+               goto out;
+       }
+
+       /* delete first mirror now */
+       rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
+       if (rc < 0)
+               error_loc = "cannot delete old layout";
+       goto out;
+
+out_close:
+       close(fd);
+       close(fdv);
+out:
+       if (rc < 0)
+               fprintf(stderr, "error: %s: %s: %s: %s\n",
+                       progname, name, error_loc, strerror(-rc));
+       else if (migration_flags & MIGRATION_VERBOSE)
+               printf("%s\n", name);
+       if (data)
+               free(data);
+       return rc;
+}
+
 /**
  * Parse a string containing an target index list into an array of integers.
  *
@@ -2275,7 +2473,8 @@ new_comp:
                rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
                if (rc) {
                        fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
-                               lsa->lsa_pattern, strerror(errno));
+                               lsa->lsa_pattern,
+                               strerror(errno));
                        return rc;
                }
                /* Data-on-MDT component has always single stripe up to end */
@@ -2284,7 +2483,8 @@ new_comp:
                rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
                if (rc) {
                        fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
-                               lsa->lsa_pattern, strerror(errno));
+                               lsa->lsa_pattern,
+                               strerror(errno));
                        return rc;
                }
        }
@@ -2332,7 +2532,8 @@ new_comp:
                    lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
                    lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
                        fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
-                               lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
+                               lsa->lsa_stripe_count,
+                               lsa->lsa_nr_tgts);
                        errno = EINVAL;
                        return -1;
                }
@@ -2782,6 +2983,7 @@ enum {
        LFS_MIRROR_INDEX_OPT,
        LFS_LAYOUT_FOREIGN_OPT,
        LFS_MODE_OPT,
+       LFS_NEWERXY_OPT,
 };
 
 /* functions */
@@ -2888,6 +3090,7 @@ static int lfs_setstripe_internal(int argc, char **argv,
        { .val = 'i',   .name = "stripe_index", .has_arg = required_argument},
        { .val = 'I',   .name = "comp-id",      .has_arg = required_argument},
        { .val = 'I',   .name = "component-id", .has_arg = required_argument},
+/* find { .val = 'l',  .name = "lazy",         .has_arg = no_argument }, */
        { .val = 'L',   .name = "layout",       .has_arg = required_argument },
        { .val = 'm',   .name = "mdt",          .has_arg = required_argument},
        { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument},
@@ -2924,6 +3127,10 @@ static int lfs_setstripe_internal(int argc, char **argv,
        migrate_mode = (opc == SO_MIGRATE);
        mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
        setstripe_mode = (opc == SO_SETSTRIPE);
+       if (opc == SO_MIRROR_DELETE) {
+               delete = 1;
+               mirror_flags = MF_DESTROY;
+       }
 
        snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
        progname = cmd;
@@ -3053,6 +3260,12 @@ static int lfs_setstripe_internal(int argc, char **argv,
                        migration_block = true;
                        break;
                case 'C':
+                       if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
+                               fprintf(stderr,
+                                       "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
+                                       progname, argv[0]);
+                               goto usage_error;
+                       }
                        lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
                        /* fall through */
                case 'c':
@@ -3278,6 +3491,12 @@ static int lfs_setstripe_internal(int argc, char **argv,
                                fprintf(stderr, "warning: '--ost-list' is "
                                        "deprecated, use '--ost' instead\n");
 #endif
+                       if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
+                               fprintf(stderr,
+                                       "%s %s: -o|--ost incompatible with DoM layout\n",
+                                       progname, argv[0]);
+                               goto usage_error;
+                       }
                        /* -o allows overstriping, and must note it because
                         * parse_targets is shared with MDT striping, which
                         * does not allow duplicates
@@ -3363,6 +3582,11 @@ static int lfs_setstripe_internal(int argc, char **argv,
                goto usage_error;
        }
 
+       /* lfs migrate $filename should keep the file's layout by default */
+       if (migrate_mode && !setstripe_args_specified(&lsa) && !layout &&
+           !from_yaml)
+               from_copy = true;
+
        if (xattr && !foreign_mode) {
                /* only print a warning as this is harmless and will be ignored
                 */
@@ -3513,8 +3737,8 @@ static int lfs_setstripe_internal(int argc, char **argv,
 
        if ((from_yaml || from_copy) &&
            (setstripe_args_specified(&lsa) || layout != NULL)) {
-               fprintf(stderr, "error: %s: can't specify --yaml with "
-                       "-c, -S, -i, -o, -p or -E options.\n",
+               fprintf(stderr, "error: %s: can't specify --yaml or --copy with"
+                       " -c, -S, -i, -o, -p or -E options.\n",
                        argv[0]);
                goto error;
        }
@@ -3526,11 +3750,11 @@ static int lfs_setstripe_internal(int argc, char **argv,
                goto usage_error;
        }
 
-       if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
-           comp_id != 0) {
+       if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
+           opc != SO_MIRROR_DELETE && comp_id != 0) {
                fprintf(stderr,
-               "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
-                       progname, argv[0]);
+                       "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
+                       progname);
                goto usage_error;
        }
 
@@ -3561,9 +3785,11 @@ static int lfs_setstripe_internal(int argc, char **argv,
                        lmu->lum_hash_type = lsa.lsa_pattern;
                else
                        lmu->lum_hash_type = LMV_HASH_TYPE_DEFAULT;
-               if (lsa.lsa_pool_name)
+               if (lsa.lsa_pool_name) {
                        strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
                                sizeof(lmu->lum_pool_name) - 1);
+                       lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
+               }
                if (lsa.lsa_nr_tgts > 1) {
                        int i;
 
@@ -3573,7 +3799,7 @@ static int lfs_setstripe_internal(int argc, char **argv,
                                fprintf(stderr,
                                        "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
                                        progname, lsa.lsa_stripe_count,
-                                               lsa.lsa_nr_tgts);
+                                       lsa.lsa_nr_tgts);
                                free(lmu);
                                goto usage_error;
                        }
@@ -3627,9 +3853,9 @@ static int lfs_setstripe_internal(int argc, char **argv,
                            lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
                            lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
                            lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
-                               fprintf(stderr, "error: %s: stripe count %lld "
-                                       "doesn't match the number of OSTs: %d\n"
-                                       argv[0], lsa.lsa_stripe_count,
+                               fprintf(stderr,
+                                       "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
+                                       argv[0], lsa.lsa_stripe_count,
                                        lsa.lsa_nr_tgts);
                                free(param);
                                goto usage_error;
@@ -3652,17 +3878,19 @@ static int lfs_setstripe_internal(int argc, char **argv,
                                argv[0], template);
                        goto error;
                }
-       } else if (from_copy) {
-               layout = llapi_layout_get_by_path(template, 0);
-               if (layout == NULL) {
-                       fprintf(stderr,
-                           "%s: can't create composite layout from file %s.\n",
-                               progname, template);
-                       goto error;
-               }
        }
 
        for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
+               if (from_copy) {
+                       layout = llapi_layout_get_by_path(template ?: fname, 0);
+                       if (layout == NULL) {
+                               fprintf(stderr, "%s: can't create composite "
+                                       "layout from file %s.\n",
+                                       progname, template ?: fname);
+                               goto error;
+                       }
+               }
+
                if (migrate_mdt_mode) {
                        result = llapi_migrate_mdt(fname, &migrate_mdt_param);
                } else if (migrate_mode) {
@@ -3683,11 +3911,11 @@ static int lfs_setstripe_internal(int argc, char **argv,
                } else if (opc == SO_MIRROR_EXTEND) {
                        result = mirror_extend(fname, mirror_list,
                                               mirror_flags);
-               } else if (opc == SO_MIRROR_SPLIT) {
+               } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
                        if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
                                fprintf(stderr,
-                       "%s %s: no mirror id or component id or pool name"
-                       " is specified\n", progname, argv[0]);
+                                       "%s: no mirror specified to delete from '%s'\n",
+                                       progname, fname);
                                goto usage_error;
                        }
                        if (lsa.lsa_pool_name)
@@ -3924,6 +4152,32 @@ static int lfs_find(int argc, char **argv)
                        .name = "mirror-state", .has_arg = required_argument },
        { .val = LFS_LAYOUT_FOREIGN_OPT,
                        .name = "foreign",      .has_arg = optional_argument},
+       { .val = LFS_NEWERXY_OPT,
+                       .name = "newer",        .has_arg = required_argument},
+       { .val = LFS_NEWERXY_OPT,
+                       .name = "neweraa",      .has_arg = required_argument},
+       { .val = LFS_NEWERXY_OPT,
+                       .name = "neweram",      .has_arg = required_argument},
+       { .val = LFS_NEWERXY_OPT,
+                       .name = "newerac",      .has_arg = required_argument},
+       { .val = LFS_NEWERXY_OPT,
+                       .name = "newerma",      .has_arg = required_argument},
+       { .val = LFS_NEWERXY_OPT,
+                       .name = "newermm",      .has_arg = required_argument},
+       { .val = LFS_NEWERXY_OPT,
+                       .name = "newermc",      .has_arg = required_argument},
+       { .val = LFS_NEWERXY_OPT,
+                       .name = "newerca",      .has_arg = required_argument},
+       { .val = LFS_NEWERXY_OPT,
+                       .name = "newercm",      .has_arg = required_argument},
+       { .val = LFS_NEWERXY_OPT,
+                       .name = "newercc",      .has_arg = required_argument},
+       { .val = LFS_NEWERXY_OPT,
+                       .name = "newerat",      .has_arg = required_argument},
+       { .val = LFS_NEWERXY_OPT,
+                       .name = "newermt",      .has_arg = required_argument},
+       { .val = LFS_NEWERXY_OPT,
+                       .name = "newerct",      .has_arg = required_argument},
        { .val = 'c',   .name = "stripe-count", .has_arg = required_argument },
        { .val = 'c',   .name = "stripe_count", .has_arg = required_argument },
        { .val = 'C',   .name = "ctime",        .has_arg = required_argument },
@@ -3941,6 +4195,7 @@ static int lfs_find(int argc, char **argv)
        { .val = 'i',   .name = "stripe-index", .has_arg = required_argument },
        { .val = 'i',   .name = "stripe_index", .has_arg = required_argument },
 /* getstripe { .val = 'I', .name = "comp-id",  .has_arg = required_argument }*/
+       { .val = 'l',   .name = "lazy",         .has_arg = no_argument },
        { .val = 'L',   .name = "layout",       .has_arg = required_argument },
        { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
        { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
@@ -3974,20 +4229,22 @@ static int lfs_find(int argc, char **argv)
 /* getstripe { .val = 'v', .name = "verbose",  .has_arg = no_argument }, */
 /* getstripe { .val = 'y', .name = "yaml",     .has_arg = no_argument }, */
        { .name = NULL } };
-        int pathstart = -1;
-        int pathend = -1;
-        int neg_opt = 0;
-        time_t *xtime;
-        int *xsign;
-        int isoption;
-        char *endptr;
+       int optidx = 0;
+       int pathstart = -1;
+       int pathend = -1;
+       int pathbad = -1;
+       int neg_opt = 0;
+       time_t *xtime;
+       int *xsign;
+       int isoption;
+       char *endptr;
 
-        time(&t);
+       time(&t);
 
        /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
        while ((c = getopt_long_only(argc, argv,
                        "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
-                       long_opts, NULL)) >= 0) {
+                       long_opts, &optidx)) >= 0) {
                 xtime = NULL;
                 xsign = NULL;
                 if (neg_opt)
@@ -4209,6 +4466,130 @@ static int lfs_find(int argc, char **argv)
                        param.fp_exclude_foreign = !!neg_opt;
                        break;
                }
+               case LFS_NEWERXY_OPT: {
+                       char x = 'm';
+                       char y = 'm';
+                       int xidx;
+                       int negidx;
+                       time_t *newery;
+                       time_t ref = time(NULL);
+
+                       /* no need to check bad options, they won't get here */
+                       if (strlen(long_opts[optidx].name) == 7) {
+                               x = long_opts[optidx].name[5];
+                               y = long_opts[optidx].name[6];
+                       }
+
+                       if (y == 't') {
+                               static const char *const fmts[] = {
+                                       "%Y-%m-%d %H:%M:%S",
+                                       "%Y-%m-%d %H:%M",
+                                       "%Y-%m-%d",
+                                       "%H:%M:%S", /* sometime today */
+                                       "%H:%M",
+                                       "@%s",
+                                       "%s",
+                                       NULL };
+                               struct tm tm;
+                               bool found = false;
+                               int i;
+
+                               for (i = 0; fmts[i] != NULL; i++) {
+                                       char *ptr;
+
+                                       /* Init for times relative to today */
+                                       if (strncmp(fmts[i], "%H", 2) == 0)
+                                               localtime_r(&ref, &tm);
+                                       else
+                                               memset(&tm, 0, sizeof(tm));
+                                       ptr = strptime(optarg, fmts[i], &tm);
+                                       /* Skip spaces */
+                                       while (ptr && isspace(*ptr))
+                                               ptr++;
+                                       if (ptr == optarg + strlen(optarg)) {
+                                               found = true;
+                                               break;
+                                       }
+                               }
+
+                               if (!found) {
+                                       fprintf(stderr,
+                                               "%s: invalid time '%s'\n",
+                                               progname, optarg);
+                                       fprintf(stderr,
+                                               "supported formats are:\n  ");
+                                       for (i = 0; fmts[i] != NULL; i++)
+                                               fprintf(stderr, "'%s', ",
+                                                       fmts[i]);
+                                       fprintf(stderr, "\n");
+                                       ret = -EINVAL;
+                                       goto err;
+                               }
+
+                               ref = mktime(&tm);
+                       } else {
+                               struct stat statbuf;
+
+                               if (stat(optarg, &statbuf) < 0) {
+                                       fprintf(stderr,
+                                               "%s: cannot stat file '%s': %s\n",
+                                               progname, optarg,
+                                               strerror(errno));
+                                       ret = -errno;
+                                       goto err;
+                               }
+
+                               switch (y) {
+                               case 'a':
+                                       ref = statbuf.st_atime;
+                                       break;
+                               case 'm':
+                                       ref = statbuf.st_mtime;
+                                       break;
+                               case 'c':
+                                       ref = statbuf.st_ctime;
+                                       break;
+                               default:
+                                       fprintf(stderr,
+                                               "%s: invalid Y argument: '%c'\n",
+                                               progname, x);
+                                       ret = -EINVAL;
+                                       goto err;
+                               }
+                       }
+
+                       switch (x) {
+                       case 'a':
+                               xidx = NEWERXY_ATIME;
+                               break;
+                       case 'm':
+                               xidx = NEWERXY_MTIME;
+                               break;
+                       case 'c':
+                               xidx = NEWERXY_CTIME;
+                               break;
+                       default:
+                               fprintf(stderr,
+                                       "%s: invalid X argument: '%c'\n",
+                                       progname, x);
+                               ret = -EINVAL;
+                               goto err;
+                       }
+
+                       negidx = !!neg_opt;
+                       newery = &param.fp_newery[xidx][negidx];
+
+                       if (*newery == 0) {
+                               *newery = ref;
+                       } else {
+                               if (negidx)
+                                       *newery = *newery > ref ? ref : *newery;
+                               else
+                                       *newery = *newery > ref ? *newery : ref;
+                       }
+                       param.fp_newerxy = 1;
+                       break;
+               }
                case 'g':
                case 'G':
                        rc = name2gid(&param.fp_gid, optarg);
@@ -4235,6 +4616,9 @@ static int lfs_find(int argc, char **argv)
                        param.fp_check_hash_type = 1;
                        param.fp_exclude_hash_type = !!neg_opt;
                        break;
+               case 'l':
+                       param.fp_lazy = 1;
+                       break;
                case 'L':
                        ret = name2layout(&param.fp_layout, optarg);
                        if (ret)
@@ -4511,32 +4895,34 @@ err_free:
                        param.fp_check_ext_size = 1;
                        param.fp_exclude_ext_size = !!neg_opt;
                        break;
+               default:
+                       ret = CMD_HELP;
+                       goto err;
+               };
+       }
 
-                default:
-                        ret = CMD_HELP;
-                        goto err;
-                };
-        }
-
-        if (pathstart == -1) {
-                fprintf(stderr, "error: %s: no filename|pathname\n",
-                        argv[0]);
-                ret = CMD_HELP;
-                goto err;
-        } else if (pathend == -1) {
-                /* no options */
-                pathend = argc;
-        }
+       if (pathstart == -1) {
+               fprintf(stderr, "error: %s: no filename|pathname\n",
+                       argv[0]);
+               ret = CMD_HELP;
+               goto err;
+       } else if (pathend == -1) {
+               /* no options */
+               pathend = argc;
+       }
 
        do {
                rc = llapi_find(argv[pathstart], &param);
-               if (rc != 0 && ret == 0)
+               if (rc && !ret) {
                        ret = rc;
+                       pathbad = pathstart;
+               }
        } while (++pathstart < pathend);
 
-        if (ret)
-                fprintf(stderr, "error: %s failed for %s.\n",
-                        argv[0], argv[optind - 1]);
+       if (ret)
+               fprintf(stderr, "%s: failed for '%s': %s\n",
+                       progname, argv[pathbad], strerror(-rc));
+
 err:
        if (param.fp_obd_uuid && param.fp_num_alloc_obds)
                free(param.fp_obd_uuid);
@@ -4584,6 +4970,7 @@ static int lfs_getstripe_internal(int argc, char **argv,
        { .val = 'i',   .name = "stripe_index", .has_arg = no_argument },
        { .val = 'I',   .name = "comp-id",      .has_arg = optional_argument },
        { .val = 'I',   .name = "component-id", .has_arg = optional_argument },
+/* find { .val = 'l',  .name = "lazy",         .has_arg = no_argument }, */
        { .val = 'L',   .name = "layout",       .has_arg = no_argument },
        { .val = 'm',   .name = "mdt",          .has_arg = no_argument },
        { .val = 'm',   .name = "mdt-index",    .has_arg = no_argument },
@@ -5380,28 +5767,6 @@ static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
        return rc;
 }
 
-static int ll_statfs_data_comp(const void *sd1, const void *sd2)
-{
-       const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
-                                               sd_st;
-       const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
-                                               sd_st;
-       int r1 = obd_statfs_ratio(st1, false);
-       int r2 = obd_statfs_ratio(st2, false);
-       int64_t result = r1 - r2;
-
-       /* if both space usage are above 90, compare free inodes */
-       if (r1 > 90 && r2 > 90)
-               result = st2->os_ffree - st1->os_ffree;
-
-       if (result < 0)
-               return -1;
-       else if (result == 0)
-               return 0;
-       else
-               return 1;
-}
-
 /* functions */
 static int lfs_setdirstripe(int argc, char **argv)
 {
@@ -5414,12 +5779,9 @@ static int lfs_setdirstripe(int argc, char **argv)
        char *mode_opt = NULL;
        bool default_stripe = false;
        bool delete = false;
-       bool auto_distributed = false;
        bool foreign_mode = false;
        mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
        mode_t previous_mode = 0;
-       struct ll_statfs_buf *lsb = NULL;
-       char mntdir[PATH_MAX] = "";
        char *xattr = NULL;
        __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
        struct option long_opts[] = {
@@ -5437,6 +5799,7 @@ static int lfs_setdirstripe(int argc, char **argv)
        { .val = 'i',   .name = "mdt-index",    .has_arg = required_argument },
        { .val = 'i',   .name = "mdt",          .has_arg = required_argument },
 #else
+/* find { .val = 'l',  .name = "lazy",         .has_arg = no_argument }, */
        { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
        { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
 #endif
@@ -5666,9 +6029,10 @@ static int lfs_setdirstripe(int argc, char **argv)
                if (lsa.lsa_stripe_count > 0 &&
                    lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
                    lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
-                       fprintf(stderr, "error: %s: stripe count %lld doesn't "
-                               "match the number of MDTs: %d\n",
-                               argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
+                       fprintf(stderr,
+                               "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
+                               argv[0], lsa.lsa_stripe_count,
+                               lsa.lsa_nr_tgts);
                        free(param);
                        return CMD_HELP;
                }
@@ -5678,21 +6042,6 @@ static int lfs_setdirstripe(int argc, char **argv)
                memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
        }
 
-       if (!default_stripe && (lsa.lsa_pattern & LMV_HASH_FLAG_SPACE)) {
-               fprintf(stderr, "%s %s: can only specify -H space with -D\n",
-                       progname, argv[0]);
-               free(param);
-               return CMD_HELP;
-       }
-
-       if (param->lsp_stripe_offset != -1 &&
-           lsa.lsa_pattern & LMV_HASH_FLAG_SPACE) {
-               fprintf(stderr, "%s %s: can only specify -H space with -i -1\n",
-                       progname, argv[0]);
-               free(param);
-               return CMD_HELP;
-       }
-
        dname = argv[optind];
        do {
                if (default_stripe) {
@@ -5704,100 +6053,6 @@ static int lfs_setdirstripe(int argc, char **argv)
                        continue;
                }
 
-               /*
-                * if current \a dname isn't under the same \a mntdir as the
-                * last one, and the last one was auto-distributed, restore
-                * \a param.
-                */
-               if (mntdir[0] != '\0' &&
-                   strncmp(dname, mntdir, strlen(mntdir)) &&
-                   auto_distributed) {
-                       param->lsp_is_specific = false;
-                       param->lsp_stripe_offset = -1;
-                       auto_distributed = false;
-               }
-
-               /*
-                * TODO: when MDT can allocate object with QoS (LU-9435), below
-                * code should be removed, instead we should let LMV to allocate
-                * the starting MDT object, and then let LOD allocate other MDT
-                * objects.
-                */
-               if (!param->lsp_is_specific && param->lsp_stripe_offset == -1) {
-                       char path[PATH_MAX] = "";
-
-                       if (!lsb) {
-                               lsb = malloc(sizeof(*lsb));
-                               if (!lsb) {
-                                       result = -ENOMEM;
-                                       break;
-                               }
-                       }
-                       lsb->sb_count = 0;
-
-                       /* use mntdir for dirname() temporarily */
-                       strncpy(mntdir, dname, sizeof(mntdir) - 1);
-                       if (!realpath(dirname(mntdir), path)) {
-                               result = -errno;
-                               fprintf(stderr,
-                                       "error: invalid path '%s': %s\n",
-                                       argv[optind], strerror(errno));
-                               break;
-                       }
-                       mntdir[0] = '\0';
-
-                       result = llapi_search_mounts(path, 0, mntdir, NULL);
-                       if (result < 0 || mntdir[0] == '\0') {
-                               fprintf(stderr,
-                                       "No suitable Lustre mount found\n");
-                               break;
-                       }
-
-                       result = mntdf(mntdir, NULL, NULL, 0, LL_STATFS_LMV,
-                                      lsb);
-                       if (result < 0)
-                               break;
-
-                       if (param->lsp_stripe_count > lsb->sb_count) {
-                               fprintf(stderr,
-                                       "error: stripe count %d is too big\n",
-                                       param->lsp_stripe_count);
-                               result = -ERANGE;
-                               break;
-                       }
-
-                       qsort(lsb->sb_buf, lsb->sb_count,
-                             sizeof(struct ll_statfs_data),
-                             ll_statfs_data_comp);
-
-                       auto_distributed = true;
-               }
-
-               if (auto_distributed) {
-                       int r;
-                       int nr = MAX(param->lsp_stripe_count,
-                                    lsb->sb_count / 2);
-
-                       /* don't use server whose usage is above 90% */
-                       while (nr != param->lsp_stripe_count &&
-                              obd_statfs_ratio(&lsb->sb_buf[nr].sd_st, false) >
-                              90)
-                               nr = MAX(param->lsp_stripe_count, nr / 2);
-
-                       /* get \a r between [0, nr) */
-                       r = rand() % nr;
-
-                       param->lsp_stripe_offset = lsb->sb_buf[r].sd_index;
-                       if (param->lsp_stripe_count > 1) {
-                               int i = 0;
-
-                               param->lsp_is_specific = true;
-                               for (; i < param->lsp_stripe_count; i++)
-                                       param->lsp_tgts[(i + r) % nr] =
-                                               lsb->sb_buf[i].sd_index;
-                       }
-               }
-
                result = llapi_dir_create(dname, mode, param);
                if (result)
                        fprintf(stderr,
@@ -5808,7 +6063,6 @@ static int lfs_setdirstripe(int argc, char **argv)
        if (mode_opt != NULL)
                umask(previous_mode);
 
-       free(lsb);
        free(param);
        return result;
 }
@@ -5881,7 +6135,7 @@ static int lfs_mv(int argc, char **argv)
                }
        }
 
-       if (lmu.lum_stripe_offset == -1) {
+       if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
                fprintf(stderr, "%s mv: MDT index must be specified\n",
                        progname);
                return CMD_HELP;
@@ -5973,43 +6227,33 @@ static int lfs_df(int argc, char **argv)
        return rc;
 }
 
-static int print_instance(const char *mntdir, char *fsname, size_t fsnamelen,
+static int print_instance(const char *mntdir, char *buf, size_t buflen,
                          bool opt_instance, bool opt_fsname, bool opt_mntdir)
 {
-       char *buf = fsname;
-
-       /* llapi_search_mounts() fills "fsname", but that is not called if
-        * explicit paths are specified on the command-line
-        */
-       if (opt_instance || (opt_fsname && fsname[0] == '\0')) {
-               int rc = llapi_getname(mntdir, fsname, fsnamelen);
+       int rc = 0;
 
-               if (rc < 0) {
-                       fprintf(stderr, "cannot get instance for '%s': %s\n",
-                               mntdir, strerror(-rc));
-                       return rc;
-               }
-               buf = fsname;
-               if (!opt_instance) {
-                       /* print only the fsname name */
-                       buf = strchr(fsname, '-');
-                       if (buf)
-                               *buf = '\0';
-                       buf = fsname;
-               } else if (!opt_fsname) {
-                       /* print only the instance name */
-                       buf = strchr(fsname, '-');
-                       if (buf)
-                               buf++;
-                       else
-                               buf = fsname;
-               }
+       if (opt_fsname == opt_instance) { /* both true or both false */
+               rc = llapi_getname(mntdir, buf, buflen);
        } else if (opt_fsname) {
-               /* print only the fsname */
-               buf = fsname;
+               /* llapi_search_mounts() fills @buf with fsname, but that is not
+                * called if explicit paths are specified on the command-line
+                */
+               if (buf[0] == '\0')
+                       rc = llapi_get_fsname(mntdir, buf, buflen);
+       } else /* if (opt_instance) */ {
+               rc = llapi_get_instance(mntdir, buf, buflen);
        }
 
-       printf("%s %s\n", buf, opt_mntdir ? mntdir : "");
+       if (rc < 0) {
+               fprintf(stderr, "cannot get instance for '%s': %s\n",
+                       mntdir, strerror(-rc));
+               return rc;
+       }
+
+       if (opt_mntdir)
+               printf("%s %s\n", buf, mntdir);
+       else
+               printf("%s\n", buf);
 
        return 0;
 }
@@ -6039,10 +6283,6 @@ static int lfs_getname(int argc, char **argv)
                }
        }
 
-       /* If neither option is given, print both instance and fsname */
-       if (!opt_instance && !opt_fsname)
-               opt_instance = opt_fsname = true;
-
        if (optind == argc) { /* no paths specified, get all paths. */
                char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
                int index = 0;
@@ -6265,28 +6505,41 @@ quota_type:
                        }
                        qctl.qc_type = qtype;
                        break;
-                case 'b':
-                        if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
-                                fprintf(stderr, "error: bad block-grace: %s\n",
-                                        optarg);
-                                return CMD_HELP;
-                        }
-                        dqb->dqb_valid |= QIF_BTIME;
-                        break;
-                case 'i':
-                        if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
-                                fprintf(stderr, "error: bad inode-grace: %s\n",
-                                        optarg);
-                                return CMD_HELP;
-                        }
-                        dqb->dqb_valid |= QIF_ITIME;
-                        break;
-                case 't': /* Yes, of course! */
-                        break;
-                default: /* getopt prints error message for us when opterr != 0 */
-                        return CMD_HELP;
-                }
-        }
+               case 'b':
+                       if (strncmp(optarg, NOTIFY_GRACE,
+                                   strlen(NOTIFY_GRACE)) == 0) {
+                               dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
+                       } else {
+                               dqi->dqi_bgrace = str2sec(optarg);
+                               if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
+                                       fprintf(stderr, "error: bad "
+                                               "block-grace: %s\n", optarg);
+                                       return CMD_HELP;
+                               }
+                       }
+                       dqb->dqb_valid |= QIF_BTIME;
+                       break;
+               case 'i':
+                       if (strncmp(optarg, NOTIFY_GRACE,
+                                   strlen(NOTIFY_GRACE)) == 0) {
+                               dqi->dqi_igrace = NOTIFY_GRACE_TIME;
+                       } else {
+                               dqi->dqi_igrace = str2sec(optarg);
+                               if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
+                                       fprintf(stderr, "error: bad "
+                                               "inode-grace: %s\n", optarg);
+                                       return CMD_HELP;
+                               }
+                       }
+                       dqb->dqb_valid |= QIF_ITIME;
+                       break;
+               case 't': /* Yes, of course! */
+                       break;
+               /* getopt prints error message for us when opterr != 0 */
+               default:
+                       return CMD_HELP;
+               }
+       }
 
        if (qctl.qc_type == ALLQUOTA) {
                fprintf(stderr, "error: neither -u, -g nor -p specified\n");
@@ -6298,12 +6551,6 @@ quota_type:
                 return CMD_HELP;
         }
 
-       if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
-           (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
-               fprintf(stderr, "error: grace time is too large\n");
-               return CMD_HELP;
-       }
-
         mnt = argv[optind];
         rc = llapi_quotactl(mnt, &qctl);
         if (rc) {
@@ -6428,7 +6675,8 @@ quota_type_def:
                                fprintf(stderr,
                                        "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
                                        "See '%s help setquota' or Lustre manual for details\n",
-                                       progname, dqb->dqb_bsoftlimit,
+                                       progname,
+                                       (unsigned long long)dqb->dqb_bsoftlimit,
                                        progname);
                        break;
                case 'B':
@@ -6440,7 +6688,8 @@ quota_type_def:
                                fprintf(stderr,
                                        "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
                                        "See '%s help setquota' or Lustre manual for details\n",
-                                       progname, dqb->dqb_bhardlimit,
+                                       progname,
+                                       (unsigned long long)dqb->dqb_bhardlimit,
                                        progname);
                        break;
                case 'i':
@@ -6451,7 +6700,8 @@ quota_type_def:
                                fprintf(stderr,
                                        "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
                                        "See '%s help setquota' or Lustre manual for details\n",
-                                       progname, dqb->dqb_isoftlimit,
+                                       progname,
+                                       (unsigned long long)dqb->dqb_isoftlimit,
                                        progname);
                        break;
                case 'I':
@@ -6462,7 +6712,8 @@ quota_type_def:
                                fprintf(stderr,
                                        "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
                                        "See '%s help setquota' or Lustre manual for details\n",
-                                       progname, dqb->dqb_ihardlimit,
+                                       progname,
+                                       (unsigned long long)dqb->dqb_ihardlimit,
                                        progname);
                        break;
                default:
@@ -6577,22 +6828,23 @@ quota_type_def:
  */
 static char * __sec2str(time_t seconds, char *buf)
 {
-        const char spec[] = "smhdw";
-        const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
-        unsigned long c;
-        char *tail = buf;
-        int i;
+       const char spec[] = "smhdw";
+       const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
+       unsigned long c;
+       char *tail = buf;
+       int i;
 
-        for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
-                c = seconds / mult[i];
+       for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
+               c = seconds / mult[i];
 
-                if (c > 0 || (i == 0 && buf == tail))
-                        tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
+               if (c > 0 || (i == 0 && buf == tail))
+                       tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
+                                         spec[i]);
 
-                seconds %= mult[i];
-        }
+               seconds %= mult[i];
+       }
 
-        return tail;
+       return tail;
 }
 
 static void sec2str(time_t seconds, char *buf, int rc)
@@ -6712,7 +6964,7 @@ static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
                        diff2str(dqb->dqb_btime, timebuf, now);
                else if (show_default)
                        snprintf(timebuf, sizeof(timebuf), "%llu",
-                                dqb->dqb_btime);
+                                (unsigned long long)dqb->dqb_btime);
 
                kbytes2str(lustre_stoqb(dqb->dqb_curspace),
                           strbuf, sizeof(strbuf), h);
@@ -6745,7 +6997,7 @@ static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
                        diff2str(dqb->dqb_itime, timebuf, now);
                else if (show_default)
                        snprintf(timebuf, sizeof(timebuf), "%llu",
-                                dqb->dqb_itime);
+                                (unsigned long long)dqb->dqb_itime);
 
                snprintf(numbuf[0], sizeof(numbuf),
                         (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
@@ -6770,17 +7022,23 @@ static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
                else
                        printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
                printf("\n");
+       } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
+                  qctl->qc_cmd == Q_GETOINFO) {
+               char bgtimebuf[40];
+               char igtimebuf[40];
 
-        } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
-                   qctl->qc_cmd == Q_GETOINFO) {
-                char bgtimebuf[40];
-                char igtimebuf[40];
+               if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
+                       strncpy(bgtimebuf, NOTIFY_GRACE, 40);
+               else
+                       sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
+               if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
+                       strncpy(igtimebuf, NOTIFY_GRACE, 40);
+               else
+                       sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
 
-                sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
-                sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
-                printf("Block grace time: %s; Inode grace time: %s\n",
-                       bgtimebuf, igtimebuf);
-        }
+               printf("Block grace time: %s; Inode grace time: %s\n",
+                      bgtimebuf, igtimebuf);
+       }
 }
 
 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
@@ -6920,8 +7178,16 @@ static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
                       "Some devices may be not working or deactivated. "
                       "The data in \"[]\" is inaccurate.\n");
 out:
-       return rc1;
+       if (rc1)
+               return rc1;
+       if (rc2)
+               return rc2;
+       if (rc3)
+               return rc3;
+       if (inacc)
+               return -EIO;
 
+       return 0;
 }
 
 static int lfs_project(int argc, char **argv)
@@ -7165,17 +7431,19 @@ quota_type:
            optind == argc - 1 && !show_default) {
 
                qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
-               qctl.qc_valid = valid;
                qctl.qc_idx = idx;
 
                for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
                        qctl.qc_type = qtype;
+                       qctl.qc_valid = valid;
                        if (qtype == USRQUOTA) {
                                qctl.qc_id = geteuid();
                                rc = uid2name(&name, qctl.qc_id);
                        } else {
                                qctl.qc_id = getegid();
                                rc = gid2name(&name, qctl.qc_id);
+                               memset(&qctl.qc_dqblk, 0,
+                                      sizeof(qctl.qc_dqblk));
                        }
                        if (rc)
                                name = "<unknown>";
@@ -7405,14 +7673,16 @@ static int lfs_changelog(int argc, char **argv)
                        struct changelog_ext_extra_flags *ef =
                                changelog_rec_extra_flags(rec);
 
-                       printf(" ef=0x%llx", ef->cr_extra_flags);
+                       printf(" ef=0x%llx",
+                              (unsigned long long)ef->cr_extra_flags);
 
                        if (ef->cr_extra_flags & CLFE_UIDGID) {
                                struct changelog_ext_uidgid *uidgid =
                                        changelog_rec_uidgid(rec);
 
                                printf(" u=%llu:%llu",
-                                      uidgid->cr_uid, uidgid->cr_gid);
+                                      (unsigned long long)uidgid->cr_uid,
+                                      (unsigned long long)uidgid->cr_gid);
                        }
                        if (ef->cr_extra_flags & CLFE_NID) {
                                struct changelog_ext_nid *nid =
@@ -7699,6 +7969,85 @@ static int lfs_path2fid(int argc, char **argv)
        return rc;
 }
 
+#define MAX_ERRNO      4095
+#define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
+
+static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
+{
+       int rc, rc2 = 0, k;
+
+       rc = llapi_rmfid(device, fa);
+       if (rc) {
+               fprintf(stderr, "rmfid(): rc = %d\n", rc);
+               return rc;
+       }
+
+       for (k = 0; k < fa->fa_nr; k++) {
+               rc = (__s32)fa->fa_fids[k].f_ver;
+               if (!IS_ERR_VALUE(rc))
+                       continue;
+               if (!rc2 && rc)
+                       rc2 = rc;
+               if (!rc)
+                       continue;
+               fa->fa_fids[k].f_ver = 0;
+               fprintf(stderr, "rmfid("DFID"): rc = %d\n",
+                       PFID(&fa->fa_fids[k]), rc);
+       }
+
+       return rc2;
+}
+
+static int lfs_rmfid(int argc, char **argv)
+{
+       char *fidstr, *device;
+       int rc = 0, rc2, nr;
+       struct fid_array *fa;
+
+       if (optind > argc - 1) {
+               fprintf(stderr, "%s rmfid: missing dirname\n", progname);
+               return CMD_HELP;
+       }
+
+       device = argv[optind++];
+
+       nr = argc - optind;
+       fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
+       if (fa == NULL)
+               return -ENOMEM;
+
+       fa->fa_nr = 0;
+       rc = 0;
+       while (optind < argc) {
+               int found;
+
+               fidstr = argv[optind++];
+               while (*fidstr == '[')
+                       fidstr++;
+               found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
+               if (found != 3) {
+                       fprintf(stderr, "unrecognized FID: %s\n",
+                               argv[optind - 1]);
+                       exit(1);
+               }
+               fa->fa_nr++;
+               if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
+                       /* start another batch */
+                       rc2 = lfs_rmfid_and_show_errors(device, fa);
+                       if (rc2 && !rc)
+                               rc = rc2;
+                       fa->fa_nr = 0;
+               }
+       }
+       if (fa->fa_nr) {
+               rc2 = lfs_rmfid_and_show_errors(device, fa);
+               if (rc2 && !rc)
+                       rc = rc2;
+       }
+
+       return rc;
+}
+
 static int lfs_data_version(int argc, char **argv)
 {
        char *path;
@@ -8032,16 +8381,10 @@ static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
        hui->hui_extent.length = -1;
 
        if (mntpath != NULL) {
-               if (*fname == '[')
-                       fname++;
-               rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
-               if (rc == 3) {
-                       rc = 0;
-               } else {
+               rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
+               if (rc)
                        fprintf(stderr, "hsm: '%s' is not a valid FID\n",
                                fname);
-                       rc = -EINVAL;
-               }
        } else {
                rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
        }
@@ -8549,7 +8892,8 @@ static int lfs_heat_get(int argc, char **argv)
 
                printf("flags: %x\n", heat->lh_flags);
                for (i = 0; i < heat->lh_count; i++)
-                       printf("%s: %llu\n", heat_names[i], heat->lh_heat[i]);
+                       printf("%s: %llu\n", heat_names[i],
+                              (unsigned long long)heat->lh_heat[i]);
 next:
                if (rc == 0 && rc2 < 0)
                        rc = rc2;
@@ -8875,6 +9219,7 @@ int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
        int idx;
        int fd;
        int rc;
+       int rc2;
 
        if (stat(fname, &stbuf) < 0) {
                fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
@@ -8939,7 +9284,7 @@ int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
                else
                        fprintf(stderr,
                            "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
-                               progname, fname, strerror(errno));
+                               progname, fname, strerror(-rc));
                goto free_layout;
        }
 
@@ -8963,10 +9308,10 @@ int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
        rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
                                      start, end);
        if (rc < 0)
-               fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
-                       progname, fname, rc);
+               fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
+                       progname, fname, strerror(-rc));
 
-       /* prepare ioc for lease put */
+       /* need to do the lease unlock even resync fails */
        ioc->lil_mode = LL_LEASE_UNLCK;
        ioc->lil_flags = LL_LEASE_RESYNC_DONE;
        ioc->lil_count = 0;
@@ -8977,19 +9322,19 @@ int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
                }
        }
 
-       rc = llapi_lease_set(fd, ioc);
-       if (rc <= 0) {
-               if (rc == 0) /* lost lease lock */
-                       rc = -EBUSY;
-               fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
-                       progname, fname, strerror(errno));
-               goto free_layout;
-       }
+       rc2 = llapi_lease_set(fd, ioc);
        /**
         * llapi_lease_set returns lease mode when it request to unlock
-        * the lease lock
+        * the lease lock.
         */
-       rc = 0;
+       if (rc2 <= 0) {
+               /* rc2 == 0 means lost lease lock */
+               if (rc2 == 0 && rc == 0)
+                       rc = -EBUSY;
+               fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
+                       progname, fname,
+                       rc2 == 0 ? "lost lease lock" : strerror(-rc2));
+       }
 
 free_layout:
        llapi_layout_free(layout);
@@ -9500,37 +9845,6 @@ close_fd:
        return rc;
 }
 
-struct collect_ids_data {
-       __u16   *cid_ids;
-       int     cid_count;
-       __u16   cid_exclude;
-};
-
-static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
-{
-       struct collect_ids_data *cid = cbdata;
-       uint32_t id;
-       int rc;
-
-       rc = llapi_layout_mirror_id_get(layout, &id);
-       if (rc < 0)
-               return rc;
-
-       if ((__u16)id != cid->cid_exclude) {
-               int i;
-
-               for (i = 0; i < cid->cid_count; i++) {
-                       /* already collected the mirror id */
-                       if (id == cid->cid_ids[i])
-                               return LLAPI_LAYOUT_ITER_CONT;
-               }
-               cid->cid_ids[cid->cid_count] = id;
-               cid->cid_count++;
-       }
-
-       return LLAPI_LAYOUT_ITER_CONT;
-}
-
 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
 {
        struct llapi_layout *layout;
@@ -10504,14 +10818,15 @@ static int lfs_getsom(int argc, char **argv)
        switch (type) {
        case LFS_SOM_ATTR_ALL:
                printf("file: %s size: %llu blocks: %llu flags: %x\n",
-                      path, attrs->lsa_size, attrs->lsa_blocks,
+                      path, (unsigned long long)attrs->lsa_size,
+                      (unsigned long long)attrs->lsa_blocks,
                       attrs->lsa_valid);
                break;
        case LFS_SOM_SIZE:
-               printf("%llu\n", attrs->lsa_size);
+               printf("%llu\n", (unsigned long long)attrs->lsa_size);
                break;
        case LFS_SOM_BLOCKS:
-               printf("%llu\n", attrs->lsa_blocks);
+               printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
                break;
        case LFS_SOM_FLAGS:
                printf("%x\n", attrs->lsa_valid);
@@ -10577,6 +10892,11 @@ static int lfs_pcc_attach(int argc, char **argv)
                }
        }
 
+       if (archive_id == 0) {
+               fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
+               return CMD_HELP;
+       }
+
        if (argc <= optind) {
                fprintf(stderr, "%s: must specify one or more file names\n",
                        argv[0]);