Whamcloud - gitweb
LU-12378 ptlrpc: always reset generation for idle reconnect
[fs/lustre-release.git] / lustre / utils / lfs.c
index 28e80cd..6160ea4 100644 (file)
@@ -135,6 +135,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,
@@ -143,6 +147,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);
@@ -167,6 +172,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) \
@@ -284,19 +293,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"
@@ -553,9 +566,7 @@ 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"
@@ -699,9 +710,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;
 }
 
@@ -1101,10 +1109,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;
 }
@@ -1185,6 +1201,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;
@@ -1194,6 +1212,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
@@ -1230,7 +1278,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));
@@ -1822,6 +1870,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)
 {
@@ -1928,6 +2042,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 {
@@ -1989,6 +2111,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.
  *
@@ -2278,7 +2466,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 */
@@ -2287,7 +2476,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;
                }
        }
@@ -2335,7 +2525,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;
                }
@@ -2891,6 +3082,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},
@@ -2927,6 +3119,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;
@@ -3529,11 +3725,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;
        }
 
@@ -3578,7 +3774,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;
                        }
@@ -3632,9 +3828,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;
@@ -3688,11 +3884,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)
@@ -3946,6 +4142,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 },
@@ -4241,6 +4438,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)
@@ -4517,22 +4717,21 @@ 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);
@@ -4593,6 +4792,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 },
@@ -5389,28 +5589,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)
 {
@@ -5423,12 +5601,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[] = {
@@ -5446,6 +5621,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
@@ -5675,9 +5851,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;
                }
@@ -5687,21 +5864,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) {
@@ -5713,100 +5875,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,
@@ -5817,7 +5885,6 @@ static int lfs_setdirstripe(int argc, char **argv)
        if (mode_opt != NULL)
                umask(previous_mode);
 
-       free(lsb);
        free(param);
        return result;
 }
@@ -5890,7 +5957,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;
@@ -6437,7 +6504,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':
@@ -6449,7 +6517,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':
@@ -6460,7 +6529,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':
@@ -6471,7 +6541,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:
@@ -6721,7 +6792,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);
@@ -6754,7 +6825,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]",
@@ -6929,8 +7000,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)
@@ -7416,14 +7495,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 =
@@ -8122,16 +8203,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);
        }
@@ -8639,7 +8714,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;
@@ -9591,37 +9667,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;
@@ -10595,14 +10640,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);
@@ -10668,6 +10714,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]);