Whamcloud - gitweb
LU-6081 user: use random() instead of /dev/urandom
[fs/lustre-release.git] / lustre / utils / lfs.c
index 7ca4a7e..3c41251 100644 (file)
@@ -27,7 +27,7 @@
  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2013, Intel Corporation.
+ * Copyright (c) 2011, 2014, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -121,13 +121,21 @@ static int lfs_mv(int argc, char **argv);
        "                 [--stripe-size|-S <stripe_size>]\n"\
        "                 [--pool|-p <pool_name>]\n"\
        "                 [--block|-b] "_tgt"\n"\
+       "                 [--ost-list|-o <ost_indices>]\n"\
        "\tstripe_size:  Number of bytes on each OST (0 filesystem default)\n"\
        "\t              Can be specified with k, m or g (in KB, MB and GB\n"\
        "\t              respectively)\n"\
        "\tstart_ost_idx: OST index of first stripe (-1 default)\n"\
        "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n"\
        "\tpool_name:    Name of OST pool to use (default none)\n"\
-       "\tblock:        Block file access during data migration"
+       "\tblock:        Block file access during data migration\n"\
+       "\tost_indices:  List of OST indices, can be repeated multiple times\n"\
+       "\t              Indices be specified in a format of:\n"\
+       "\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-list, then the OSTs\n"\
+       "\t              must be the members of the pool."
 
 /* all avaialable commands */
 command_t cmdlist[] = {
@@ -298,7 +306,7 @@ command_t cmdlist[] = {
         "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
                /* [ --rec <recno> ] */ },
        {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
-        "usage: path2fid <path> ..."},
+        "usage: path2fid [--parents] <path> ..."},
        {"data_version", lfs_data_version, 0, "Display file data version for "
         "a given path.\n" "usage: data_version -[n|r|w] <path>"},
        {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
@@ -347,10 +355,8 @@ command_t cmdlist[] = {
 
 #define MIGRATION_BLOCKS 1
 
-static int lfs_migrate(char *name, unsigned long long stripe_size,
-                      int stripe_offset, int stripe_count,
-                      int stripe_pattern, char *pool_name,
-                      __u64 migration_flags)
+static int lfs_migrate(char *name, __u64 migration_flags,
+                      struct llapi_stripe_param *param)
 {
        int                      fd, fdv;
        char                     volatile_file[PATH_MAX +
@@ -365,7 +371,7 @@ static int lfs_migrate(char *name, unsigned long long stripe_size,
        void                    *buf = NULL;
        int                      rsize, wsize;
        __u64                    rpos, wpos, bufoff;
-       int                      gid = 0, sz;
+       int                      gid;
        int                      have_gl = 0;
        struct stat              st, stv;
 
@@ -392,26 +398,6 @@ static int lfs_migrate(char *name, unsigned long long stripe_size,
                goto free;
        }
 
-       if (migration_flags & MIGRATION_BLOCKS) {
-               /* generate a random id for the grouplock */
-               fd = open("/dev/urandom", O_RDONLY);
-               if (fd == -1) {
-                       rc = -errno;
-                       fprintf(stderr, "cannot open /dev/urandom (%s)\n",
-                               strerror(-rc));
-                       goto free;
-               }
-               sz = sizeof(gid);
-               rc = read(fd, &gid, sz);
-               close(fd);
-               if (rc < sz) {
-                       rc = -errno;
-                       fprintf(stderr, "cannot read %d bytes from"
-                               " /dev/urandom (%s)\n", sz, strerror(-rc));
-                       goto free;
-               }
-       }
-
        /* search for file directory pathname */
        if (strlen(name) > sizeof(parent)-1) {
                rc = -E2BIG;
@@ -440,9 +426,8 @@ static int lfs_migrate(char *name, unsigned long long stripe_size,
        /* create, open a volatile file, use caching (ie no directio) */
        /* exclusive create is not needed because volatile files cannot
         * conflict on name by construction */
-       fdv = llapi_file_open_pool(volatile_file, O_CREAT | O_WRONLY,
-                                  0644, stripe_size, stripe_offset,
-                                  stripe_count, stripe_pattern, pool_name);
+       fdv = llapi_file_open_param(volatile_file, O_CREAT | O_WRONLY, 0644,
+                                   param);
        if (fdv < 0) {
                rc = fdv;
                fprintf(stderr, "cannot create volatile file in %s (%s)\n",
@@ -497,14 +482,17 @@ static int lfs_migrate(char *name, unsigned long long stripe_size,
                goto error;
        }
 
+       do
+               gid = random();
+       while (gid == 0);
        if (migration_flags & MIGRATION_BLOCKS) {
                /* take group lock to limit concurent access
                 * this will be no more needed when exclusive access will
                 * be implemented (see LU-2919) */
                /* group lock is taken after data version read because it
                 * blocks data version call */
-               if (ioctl(fd, LL_IOC_GROUP_LOCK, gid) == -1) {
-                       rc = -errno;
+               rc = llapi_group_lock(fd, gid);
+               if (rc < 0) {
                        fprintf(stderr, "cannot get group lock on %s (%s)\n",
                                name, strerror(-rc));
                        goto error;
@@ -551,11 +539,10 @@ static int lfs_migrate(char *name, unsigned long long stripe_size,
 
        if (migration_flags & MIGRATION_BLOCKS) {
                /* give back group lock */
-               if (ioctl(fd, LL_IOC_GROUP_UNLOCK, gid) == -1) {
-                       rc = -errno;
+               rc = llapi_group_unlock(fd, gid);
+               if (rc < 0)
                        fprintf(stderr, "cannot put group lock on %s (%s)\n",
                                name, strerror(-rc));
-               }
                have_gl = 0;
        }
 
@@ -580,11 +567,14 @@ static int lfs_migrate(char *name, unsigned long long stripe_size,
 
 error:
        /* give back group lock */
-       if ((migration_flags & MIGRATION_BLOCKS) && have_gl &&
-           (ioctl(fd, LL_IOC_GROUP_UNLOCK, gid) == -1)) {
-               /* we keep in rc the original error */
-               fprintf(stderr, "cannot put group lock on %s (%s)\n",
-                       name, strerror(-errno));
+       if ((migration_flags & MIGRATION_BLOCKS) && have_gl) {
+               int rc2;
+
+               /* we keep the original error in rc */
+               rc2 = llapi_group_unlock(fd, gid);
+               if (rc2 < 0)
+                       fprintf(stderr, "cannot put group lock on %s (%s)\n",
+                               name, strerror(-rc2));
        }
 
        close(fdv);
@@ -597,23 +587,110 @@ free:
        return rc;
 }
 
+/**
+ * Parse a string containing an OST index list into an array of integers.
+ *
+ * The input string contains a comma delimited list of individual
+ * indices and ranges, for example "1,2-4,7". Add the indices into the
+ * \a osts array and remove duplicates.
+ *
+ * \param[out] osts    array to store indices in
+ * \param[in] size     size of \a osts array
+ * \param[in] offset   starting index in \a osts
+ * \param[in] arg      string containing OST index list
+ *
+ * \retval positive    number of indices in \a osts
+ * \retval -EINVAL     unable to parse \a arg
+ */
+static int parse_targets(__u32 *osts, int size, int offset, char *arg)
+{
+       int rc;
+       int nr = offset;
+       int slots = size - offset;
+       char *ptr = NULL;
+       bool end_of_loop;
+
+       if (arg == NULL)
+               return -EINVAL;
+
+       end_of_loop = false;
+       while (!end_of_loop) {
+               int start_index;
+               int end_index;
+               int i;
+               char *endptr = NULL;
+
+               rc = -EINVAL;
+
+               ptr = strchrnul(arg, ',');
+
+               end_of_loop = *ptr == '\0';
+               *ptr = '\0';
+
+               start_index = strtol(arg, &endptr, 0);
+               if (endptr == arg) /* no data at all */
+                       break;
+               if (*endptr != '-' && *endptr != '\0') /* has invalid data */
+                       break;
+               if (start_index < 0)
+                       break;
+
+               end_index = start_index;
+               if (*endptr == '-') {
+                       end_index = strtol(endptr + 1, &endptr, 0);
+                       if (*endptr != '\0')
+                               break;
+                       if (end_index < start_index)
+                               break;
+               }
+
+               for (i = start_index; i <= end_index && slots > 0; i++) {
+                       int j;
+
+                       /* remove duplicate */
+                       for (j = 0; j < offset; j++) {
+                               if (osts[j] == i)
+                                       break;
+                       }
+                       if (j == offset) { /* no duplicate */
+                               osts[nr++] = i;
+                               --slots;
+                       }
+               }
+               if (slots == 0 && i < end_index)
+                       break;
+
+               *ptr = ',';
+               arg = ++ptr;
+               offset = nr;
+               rc = 0;
+       }
+       if (!end_of_loop && ptr != NULL)
+               *ptr = ',';
+
+       return rc < 0 ? rc : nr;
+}
+
 /* functions */
 static int lfs_setstripe(int argc, char **argv)
 {
-       char                    *fname;
-       int                      result;
-       unsigned long long       st_size;
-       int                      st_offset, st_count;
-       char                    *end;
-       int                      c;
-       int                      delete = 0;
-       char                    *stripe_size_arg = NULL;
-       char                    *stripe_off_arg = NULL;
-       char                    *stripe_count_arg = NULL;
-       char                    *pool_name_arg = NULL;
-       unsigned long long       size_units = 1;
-       int                      migrate_mode = 0;
-       __u64                    migration_flags = 0;
+       struct llapi_stripe_param       *param;
+       char                            *fname;
+       int                              result;
+       unsigned long long               st_size;
+       int                              st_offset, st_count;
+       char                            *end;
+       int                              c;
+       int                              delete = 0;
+       char                            *stripe_size_arg = NULL;
+       char                            *stripe_off_arg = NULL;
+       char                            *stripe_count_arg = NULL;
+       char                            *pool_name_arg = NULL;
+       unsigned long long               size_units = 1;
+       bool                             migrate_mode = false;
+       __u64                            migration_flags = 0;
+       __u32                            osts[LOV_MAX_STRIPE_COUNT] = { 0 };
+       int                              nr_osts = 0;
 
        struct option            long_opts[] = {
                /* valid only in migrate mode */
@@ -635,12 +712,8 @@ static int lfs_setstripe(int argc, char **argv)
 #endif
                {"stripe-index", required_argument, 0, 'i'},
                {"stripe_index", required_argument, 0, 'i'},
-#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
-               /* This formerly implied "stripe-index", but was confusing
-                * with "file offset" (which will eventually be needed for
-                * with different layouts by offset), so deprecate it. */
-               {"offset",       required_argument, 0, 'o'},
-#endif
+               {"ost-list",     required_argument, 0, 'o'},
+               {"ost_list",     required_argument, 0, 'o'},
                {"pool",         required_argument, 0, 'p'},
 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
                /* This formerly implied "--stripe-size", but was confusing
@@ -653,14 +726,13 @@ static int lfs_setstripe(int argc, char **argv)
                {0, 0, 0, 0}
        };
 
-        st_size = 0;
-        st_offset = -1;
-        st_count = 0;
+       st_size = 0;
+       st_offset = -1;
+       st_count = 0;
 
        if (strcmp(argv[0], "migrate") == 0)
-               migrate_mode = 1;
+               migrate_mode = true;
 
-       optind = 0;
        while ((c = getopt_long(argc, argv, "c:di:o:p:s:S:",
                                long_opts, NULL)) >= 0) {
                switch (c) {
@@ -668,7 +740,7 @@ static int lfs_setstripe(int argc, char **argv)
                        /* Long options. */
                        break;
                case 'b':
-                       if (migrate_mode == 0) {
+                       if (!migrate_mode) {
                                fprintf(stderr, "--block is valid only for"
                                                " migrate mode");
                                return CMD_HELP;
@@ -687,11 +759,19 @@ static int lfs_setstripe(int argc, char **argv)
                        /* delete the default striping pattern */
                        delete = 1;
                        break;
-#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
                case 'o':
-                       fprintf(stderr, "warning: '--offset|-o' deprecated, "
-                               "use '--stripe-index|-i' instead\n");
-#endif
+                       nr_osts = parse_targets(osts, ARRAY_SIZE(osts), nr_osts,
+                                               optarg);
+                       if (nr_osts < 0) {
+                               fprintf(stderr,
+                                       "error: %s: bad OST indices '%s'\n",
+                                       argv[0], optarg);
+                               return CMD_HELP;
+                       }
+
+                       if (st_offset == -1) /* first in the command line */
+                               st_offset = osts[0];
+                       break;
                case 'i':
 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
                        if (strcmp(argv[optind - 1], "--index") == 0)
@@ -735,6 +815,13 @@ static int lfs_setstripe(int argc, char **argv)
                return CMD_HELP;
        }
 
+       if (pool_name_arg && strlen(pool_name_arg) > LOV_MAXPOOLNAME) {
+               fprintf(stderr,
+                       "error: %s: pool name '%s' is too long (max is %d characters)\n",
+                       argv[0], pool_name_arg, LOV_MAXPOOLNAME);
+               return CMD_HELP;
+       }
+
        /* get the stripe size */
        if (stripe_size_arg != NULL) {
                result = llapi_parse_size(stripe_size_arg, &st_size,
@@ -742,7 +829,7 @@ static int lfs_setstripe(int argc, char **argv)
                if (result) {
                        fprintf(stderr, "error: %s: bad stripe size '%s'\n",
                                argv[0], stripe_size_arg);
-                       return result;
+                       return CMD_HELP;
                }
        }
         /* get the stripe offset */
@@ -764,15 +851,44 @@ static int lfs_setstripe(int argc, char **argv)
                 }
         }
 
+       /* initialize stripe parameters */
+       param = calloc(1, offsetof(typeof(*param), lsp_osts[nr_osts]));
+       if (param == NULL) {
+               fprintf(stderr, "error: %s: run out of memory\n", argv[0]);
+               return CMD_HELP;
+       }
+
+       param->lsp_stripe_size = st_size;
+       param->lsp_stripe_offset = st_offset;
+       param->lsp_stripe_count = st_count;
+       param->lsp_stripe_pattern = 0;
+       param->lsp_pool = pool_name_arg;
+       param->lsp_is_specific = false;
+       if (nr_osts > 0) {
+               if (st_count > 0 && nr_osts != st_count) {
+                       fprintf(stderr, "error: %s: stripe count '%d' doesn't "
+                               "match the number of OSTs: %d\n",
+                               argv[0], st_count, nr_osts);
+                       return CMD_HELP;
+               }
+
+               param->lsp_is_specific = true;
+               param->lsp_stripe_count = nr_osts;
+               memcpy(param->lsp_osts, osts, sizeof(*osts) * nr_osts);
+       }
+
        do {
-               if (migrate_mode)
-                       result = lfs_migrate(fname, st_size, st_offset,
-                                            st_count, 0, pool_name_arg,
-                                            migration_flags);
-               else
-                       result = llapi_file_create_pool(fname, st_size,
-                                                       st_offset, st_count,
-                                                       0, pool_name_arg);
+               if (!migrate_mode) {
+                       result = llapi_file_open_param(fname,
+                                                      O_CREAT | O_WRONLY,
+                                                      0644, param);
+                       if (result >= 0) {
+                               close(result);
+                               result = 0;
+                       }
+               } else {
+                       result = lfs_migrate(fname, migration_flags, param);
+               }
                if (result) {
                        fprintf(stderr,
                                "error: %s: %s stripe file '%s' failed\n",
@@ -783,6 +899,7 @@ static int lfs_setstripe(int argc, char **argv)
                fname = argv[++optind];
        } while (fname != NULL);
 
+       free(param);
        return result;
 }
 
@@ -903,7 +1020,7 @@ static int lfs_find(int argc, char **argv)
         time_t t;
        struct find_param param = {
                .fp_max_depth = -1,
-               .quiet = 1,
+               .fp_quiet = 1,
        };
         struct option long_opts[] = {
                 {"atime",        required_argument, 0, 'A'},
@@ -944,7 +1061,6 @@ static int lfs_find(int argc, char **argv)
 
         time(&t);
 
-       optind = 0;
        /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
        while ((c = getopt_long_only(argc, argv,
                                     "-A:c:C:D:g:G:i:L:m:M:n:O:Ppqrs:S:t:u:U:v",
@@ -1011,22 +1127,22 @@ static int lfs_find(int argc, char **argv)
                        break;
                 case 'c':
                         if (optarg[0] == '+') {
-                                param.stripecount_sign = -1;
+                               param.fp_stripe_count_sign = -1;
                                 optarg++;
                         } else if (optarg[0] == '-') {
-                                param.stripecount_sign =  1;
+                               param.fp_stripe_count_sign =  1;
                                 optarg++;
                         }
 
-                        param.stripecount = strtoul(optarg, &endptr, 0);
+                       param.fp_stripe_count = strtoul(optarg, &endptr, 0);
                         if (*endptr != '\0') {
                                 fprintf(stderr,"error: bad stripe_count '%s'\n",
                                         optarg);
                                 ret = -1;
                                 goto err;
                         }
-                        param.check_stripecount = 1;
-                        param.exclude_stripecount = !!neg_opt;
+                       param.fp_check_stripe_count = 1;
+                       param.fp_exclude_stripe_count = !!neg_opt;
                         break;
                case 'D':
                        param.fp_max_depth = strtol(optarg, 0, 0);
@@ -1047,11 +1163,11 @@ static int lfs_find(int argc, char **argv)
                        param.fp_check_gid = 1;
                         break;
                case 'L':
-                       ret = name2layout(&param.layout, optarg);
+                       ret = name2layout(&param.fp_layout, optarg);
                        if (ret)
                                goto err;
-                       param.exclude_layout = !!neg_opt;
-                       param.check_layout = 1;
+                       param.fp_exclude_layout = !!neg_opt;
+                       param.fp_check_layout = 1;
                        break;
                 case 'u':
                 case 'U':
@@ -1079,14 +1195,14 @@ static int lfs_find(int argc, char **argv)
                         }
                         /* we do check for empty pool because empty pool
                          * is used to find V1 lov attributes */
-                        strncpy(param.poolname, optarg, LOV_MAXPOOLNAME);
-                        param.poolname[LOV_MAXPOOLNAME] = '\0';
-                        param.exclude_pool = !!neg_opt;
-                        param.check_pool = 1;
+                       strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
+                       param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
+                       param.fp_exclude_pool = !!neg_opt;
+                       param.fp_check_pool = 1;
                         break;
                 case 'n':
-                        param.pattern = (char *)optarg;
-                        param.exclude_pattern = !!neg_opt;
+                       param.fp_pattern = (char *)optarg;
+                       param.fp_exclude_pattern = !!neg_opt;
                         break;
                 case 'm':
                 case 'i':
@@ -1101,7 +1217,7 @@ static int lfs_find(int argc, char **argv)
                                 goto err;
                         }
 
-                        param.exclude_obd = !!neg_opt;
+                       param.fp_exclude_obd = !!neg_opt;
 
                         token = buf;
                         while (token && *token) {
@@ -1112,38 +1228,38 @@ static int lfs_find(int argc, char **argv)
                                 }
                         }
                         if (c == 'm') {
-                                param.exclude_mdt = !!neg_opt;
-                                param.num_alloc_mdts += len;
-                                tmp = realloc(param.mdtuuid,
-                                              param.num_alloc_mdts *
-                                              sizeof(*param.mdtuuid));
+                               param.fp_exclude_mdt = !!neg_opt;
+                               param.fp_num_alloc_mdts += len;
+                               tmp = realloc(param.fp_mdt_uuid,
+                                             param.fp_num_alloc_mdts *
+                                             sizeof(*param.fp_mdt_uuid));
                                if (tmp == NULL) {
                                        ret = -ENOMEM;
                                        goto err_free;
                                }
 
-                                param.mdtuuid = tmp;
+                               param.fp_mdt_uuid = tmp;
                         } else {
-                                param.exclude_obd = !!neg_opt;
-                                param.num_alloc_obds += len;
-                                tmp = realloc(param.obduuid,
-                                              param.num_alloc_obds *
-                                              sizeof(*param.obduuid));
+                               param.fp_exclude_obd = !!neg_opt;
+                               param.fp_num_alloc_obds += len;
+                               tmp = realloc(param.fp_obd_uuid,
+                                             param.fp_num_alloc_obds *
+                                             sizeof(*param.fp_obd_uuid));
                                if (tmp == NULL) {
                                        ret = -ENOMEM;
                                        goto err_free;
                                }
 
-                                param.obduuid = tmp;
+                               param.fp_obd_uuid = tmp;
                         }
                         for (token = buf; token && *token; token = next) {
                                struct obd_uuid *puuid;
                                if (c == 'm') {
                                        puuid =
-                                         &param.mdtuuid[param.num_mdts++];
+                                       &param.fp_mdt_uuid[param.fp_num_mdts++];
                                } else {
                                        puuid =
-                                         &param.obduuid[param.num_obds++];
+                                       &param.fp_obd_uuid[param.fp_num_obds++];
                                }
                                 p = strchr(token, ',');
                                 next = 0;
@@ -1166,47 +1282,47 @@ err_free:
                         break;
                 }
                 case 'p':
-                        param.zeroend = 1;
+                       param.fp_zero_end = 1;
                         break;
                 case 'P':
                         break;
                case 's':
                        if (optarg[0] == '+') {
-                               param.size_sign = -1;
+                               param.fp_size_sign = -1;
                                optarg++;
                        } else if (optarg[0] == '-') {
-                               param.size_sign =  1;
+                               param.fp_size_sign =  1;
                                optarg++;
                        }
 
-                       ret = llapi_parse_size(optarg, &param.size,
-                                              &param.size_units, 0);
+                       ret = llapi_parse_size(optarg, &param.fp_size,
+                                              &param.fp_size_units, 0);
                        if (ret) {
                                fprintf(stderr, "error: bad file size '%s'\n",
                                        optarg);
                                goto err;
                        }
-                       param.check_size = 1;
-                       param.exclude_size = !!neg_opt;
+                       param.fp_check_size = 1;
+                       param.fp_exclude_size = !!neg_opt;
                        break;
                case 'S':
                        if (optarg[0] == '+') {
-                               param.stripesize_sign = -1;
+                               param.fp_stripe_size_sign = -1;
                                optarg++;
                        } else if (optarg[0] == '-') {
-                               param.stripesize_sign =  1;
+                               param.fp_stripe_size_sign =  1;
                                optarg++;
                        }
 
-                       ret = llapi_parse_size(optarg, &param.stripesize,
-                                              &param.stripesize_units, 0);
+                       ret = llapi_parse_size(optarg, &param.fp_stripe_size,
+                                              &param.fp_stripe_size_units, 0);
                        if (ret) {
                                fprintf(stderr, "error: bad stripe_size '%s'\n",
                                        optarg);
                                goto err;
                        }
-                       param.check_stripesize = 1;
-                       param.exclude_stripesize = !!neg_opt;
+                       param.fp_check_stripe_size = 1;
+                       param.fp_exclude_stripe_size = !!neg_opt;
                        break;
                case 't':
                        param.fp_exclude_type = !!neg_opt;
@@ -1265,11 +1381,11 @@ err_free:
                 fprintf(stderr, "error: %s failed for %s.\n",
                         argv[0], argv[optind - 1]);
 err:
-        if (param.obduuid && param.num_alloc_obds)
-                free(param.obduuid);
+       if (param.fp_obd_uuid && param.fp_num_alloc_obds)
+               free(param.fp_obd_uuid);
 
-        if (param.mdtuuid && param.num_alloc_mdts)
-                free(param.mdtuuid);
+       if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
+               free(param.fp_mdt_uuid);
 
         return ret;
 }
@@ -1326,33 +1442,32 @@ static int lfs_getstripe_internal(int argc, char **argv,
        int c, rc;
 
        param->fp_max_depth = 1;
-       optind = 0;
        while ((c = getopt_long(argc, argv, "cdDghiLMoO:pqrRsSv",
                                long_opts, NULL)) != -1) {
                switch (c) {
                case 'O':
-                       if (param->obduuid) {
+                       if (param->fp_obd_uuid) {
                                fprintf(stderr,
                                        "error: %s: only one obduuid allowed",
                                        argv[0]);
                                return CMD_HELP;
                        }
-                       param->obduuid = (struct obd_uuid *)optarg;
+                       param->fp_obd_uuid = (struct obd_uuid *)optarg;
                        break;
                case 'q':
-                       param->quiet++;
+                       param->fp_quiet++;
                        break;
                case 'd':
                        param->fp_max_depth = 0;
                        break;
                case 'D':
-                       param->get_default_lmv = 1;
+                       param->fp_get_default_lmv = 1;
                        break;
                case 'r':
-                       param->recursive = 1;
+                       param->fp_recursive = 1;
                        break;
                case 'v':
-                       param->verbose = VERBOSE_ALL | VERBOSE_DETAIL;
+                       param->fp_verbose = VERBOSE_ALL | VERBOSE_DETAIL;
                        break;
                case 'c':
 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
@@ -1360,8 +1475,8 @@ static int lfs_getstripe_internal(int argc, char **argv,
                                fprintf(stderr, "warning: '--count' deprecated,"
                                        " use '--stripe-count' instead\n");
 #endif
-                       if (!(param->verbose & VERBOSE_DETAIL)) {
-                               param->verbose |= VERBOSE_COUNT;
+                       if (!(param->fp_verbose & VERBOSE_DETAIL)) {
+                               param->fp_verbose |= VERBOSE_COUNT;
                                param->fp_max_depth = 0;
                        }
                        break;
@@ -1373,8 +1488,8 @@ static int lfs_getstripe_internal(int argc, char **argv,
 #endif
 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0) */
                case 'S':
-                       if (!(param->verbose & VERBOSE_DETAIL)) {
-                               param->verbose |= VERBOSE_SIZE;
+                       if (!(param->fp_verbose & VERBOSE_DETAIL)) {
+                               param->fp_verbose |= VERBOSE_SIZE;
                                param->fp_max_depth = 0;
                        }
                        break;
@@ -1389,36 +1504,36 @@ static int lfs_getstripe_internal(int argc, char **argv,
                                fprintf(stderr, "warning: '--index' deprecated"
                                        ", use '--stripe-index' instead\n");
 #endif
-                       if (!(param->verbose & VERBOSE_DETAIL)) {
-                               param->verbose |= VERBOSE_OFFSET;
+                       if (!(param->fp_verbose & VERBOSE_DETAIL)) {
+                               param->fp_verbose |= VERBOSE_OFFSET;
                                param->fp_max_depth = 0;
                        }
                        break;
                case 'p':
-                       if (!(param->verbose & VERBOSE_DETAIL)) {
-                               param->verbose |= VERBOSE_POOL;
+                       if (!(param->fp_verbose & VERBOSE_DETAIL)) {
+                               param->fp_verbose |= VERBOSE_POOL;
                                param->fp_max_depth = 0;
                        }
                        break;
                case 'g':
-                       if (!(param->verbose & VERBOSE_DETAIL)) {
-                               param->verbose |= VERBOSE_GENERATION;
+                       if (!(param->fp_verbose & VERBOSE_DETAIL)) {
+                               param->fp_verbose |= VERBOSE_GENERATION;
                                param->fp_max_depth = 0;
                        }
                        break;
                case 'L':
-                       if (!(param->verbose & VERBOSE_DETAIL)) {
-                               param->verbose |= VERBOSE_LAYOUT;
+                       if (!(param->fp_verbose & VERBOSE_DETAIL)) {
+                               param->fp_verbose |= VERBOSE_LAYOUT;
                                param->fp_max_depth = 0;
                        }
                        break;
                case 'M':
-                       if (!(param->verbose & VERBOSE_DETAIL))
+                       if (!(param->fp_verbose & VERBOSE_DETAIL))
                                param->fp_max_depth = 0;
-                       param->verbose |= VERBOSE_MDTINDEX;
+                       param->fp_verbose |= VERBOSE_MDTINDEX;
                        break;
                case 'R':
-                       param->raw = 1;
+                       param->fp_raw = 1;
                        break;
                default:
                        return CMD_HELP;
@@ -1428,13 +1543,13 @@ static int lfs_getstripe_internal(int argc, char **argv,
        if (optind >= argc)
                return CMD_HELP;
 
-       if (param->recursive)
+       if (param->fp_recursive)
                param->fp_max_depth = -1;
 
-       if (!param->verbose)
-               param->verbose = VERBOSE_ALL;
-       if (param->quiet)
-               param->verbose = VERBOSE_OBJID;
+       if (!param->fp_verbose)
+               param->fp_verbose = VERBOSE_ALL;
+       if (param->fp_quiet)
+               param->fp_verbose = VERBOSE_OBJID;
 
        do {
                rc = llapi_getstripe(argv[optind], param);
@@ -1469,7 +1584,7 @@ static int lfs_tgts(int argc, char **argv)
 
                 memset(&param, 0, sizeof(param));
                 if (!strcmp(argv[0], "mdts"))
-                        param.get_lmv = 1;
+                       param.fp_get_lmv = 1;
 
                 rc = llapi_ostlist(mntdir, &param);
                 if (rc) {
@@ -1495,7 +1610,7 @@ static int lfs_getdirstripe(int argc, char **argv)
 {
        struct find_param param = { 0 };
 
-       param.get_lmv = 1;
+       param.fp_get_lmv = 1;
        return lfs_getstripe_internal(argc, argv, &param);
 }
 
@@ -1526,8 +1641,6 @@ static int lfs_setdirstripe(int argc, char **argv)
                {0, 0, 0, 0}
        };
 
-       optind = 0;
-
        while ((c = getopt_long(argc, argv, "c:Di:m:t:", long_opts,
                                NULL)) >= 0) {
                switch (c) {
@@ -1668,7 +1781,7 @@ static int lfs_mv(int argc, char **argv)
 {
        struct  find_param param = {
                .fp_max_depth = -1,
-               .mdtindex = -1,
+               .fp_mdt_index = -1,
        };
        char   *end;
        int     c;
@@ -1682,7 +1795,7 @@ static int lfs_mv(int argc, char **argv)
        while ((c = getopt_long(argc, argv, "M:v", long_opts, NULL)) != -1) {
                switch (c) {
                case 'M': {
-                       param.mdtindex = strtoul(optarg, &end, 0);
+                       param.fp_mdt_index = strtoul(optarg, &end, 0);
                        if (*end != '\0') {
                                fprintf(stderr, "%s: invalid MDT index'%s'\n",
                                        argv[0], optarg);
@@ -1691,7 +1804,7 @@ static int lfs_mv(int argc, char **argv)
                        break;
                }
                case 'v': {
-                       param.verbose = VERBOSE_DETAIL;
+                       param.fp_verbose = VERBOSE_DETAIL;
                        break;
                }
                default:
@@ -1701,7 +1814,7 @@ static int lfs_mv(int argc, char **argv)
                }
        }
 
-       if (param.mdtindex == -1) {
+       if (param.fp_mdt_index == -1) {
                fprintf(stderr, "%s MDT index must be indicated\n", argv[0]);
                return CMD_HELP;
        }
@@ -1711,11 +1824,11 @@ static int lfs_mv(int argc, char **argv)
                return CMD_HELP;
        }
 
-       param.migrate = 1;
+       param.fp_migrate = 1;
        rc = llapi_mv(argv[optind], &param);
        if (rc != 0)
                fprintf(stderr, "cannot migrate '%s' to MDT%04x: %s\n",
-                       argv[optind], param.mdtindex, strerror(-rc));
+                       argv[optind], param.fp_mdt_index, strerror(-rc));
        return rc;
 }
 
@@ -1880,6 +1993,9 @@ static int mntdf(char *mntdir, char *fsname, char *pool, int ishow,
                         if (rc == -ENODEV)
                                 break;
 
+                       if (rc == -EAGAIN)
+                               continue;
+
                         if (poolname && tp->st_op == LL_STATFS_LOV &&
                             llapi_search_ost(fsname, poolname,
                                              obd_uuid2str(&uuid_buf)) != 1)
@@ -1942,7 +2058,6 @@ static int lfs_df(int argc, char **argv)
                 {0, 0, 0, 0}
         };
 
-       optind = 0;
        while ((c = getopt_long(argc, argv, "hilp:", long_opts, NULL)) != -1) {
                switch (c) {
                case 'i':
@@ -1989,7 +2104,6 @@ static int lfs_getname(int argc, char **argv)
         int rc = 0, index = 0, c;
         char buf[sizeof(struct obd_uuid)];
 
-        optind = 0;
         while ((c = getopt(argc, argv, "h")) != -1)
                 return CMD_HELP;
 
@@ -2086,7 +2200,6 @@ static int lfs_quotacheck(int argc, char **argv)
 
         memset(&qchk, 0, sizeof(qchk));
 
-        optind = 0;
         while ((c = getopt(argc, argv, "gu")) != -1) {
                 switch (c) {
                 case 'u':
@@ -2161,7 +2274,6 @@ static int lfs_quotaon(int argc, char **argv)
         memset(&qctl, 0, sizeof(qctl));
         qctl.qc_cmd = LUSTRE_Q_QUOTAON;
 
-        optind = 0;
         while ((c = getopt(argc, argv, "fgu")) != -1) {
                 switch (c) {
                 case 'u':
@@ -2229,7 +2341,6 @@ static int lfs_quotaoff(int argc, char **argv)
         memset(&qctl, 0, sizeof(qctl));
         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
 
-        optind = 0;
         while ((c = getopt(argc, argv, "gu")) != -1) {
                 switch (c) {
                 case 'u':
@@ -2391,7 +2502,6 @@ int lfs_setquota_times(int argc, char **argv)
         qctl.qc_cmd  = LUSTRE_Q_SETINFO;
         qctl.qc_type = UGQUOTA;
 
-        optind = 0;
         while ((c = getopt_long(argc, argv, "b:gi:tu", long_opts, NULL)) != -1) {
                 switch (c) {
                 case 'u':
@@ -2481,7 +2591,6 @@ int lfs_setquota(int argc, char **argv)
                                  * so it can be used as a marker that qc_type
                                  * isn't reinitialized from command line */
 
-        optind = 0;
         while ((c = getopt_long(argc, argv, "b:B:g:i:I:u:", long_opts, NULL)) != -1) {
                 switch (c) {
                 case 'u':
@@ -2868,7 +2977,6 @@ static int lfs_quota(int argc, char **argv)
        __u64 total_ialloc = 0, total_balloc = 0;
        bool human_readable = false;
 
-       optind = 0;
        while ((c = getopt(argc, argv, "gi:I:o:qtuvh")) != -1) {
                 switch (c) {
                 case 'u':
@@ -3047,7 +3155,6 @@ static int lfs_flushctx(int argc, char **argv)
        int     index = 0;
        int     rc = 0;
 
-        optind = 0;
         while ((c = getopt(argc, argv, "k")) != -1) {
                 switch (c) {
                 case 'k':
@@ -3126,7 +3233,7 @@ static int lfs_ls(int argc, char **argv)
 static int lfs_changelog(int argc, char **argv)
 {
         void *changelog_priv;
-       struct changelog_ext_rec *rec;
+       struct changelog_rec *rec;
         long long startrec = 0, endrec = 0;
         char *mdd;
         struct option long_opts[] = {
@@ -3136,7 +3243,6 @@ static int lfs_changelog(int argc, char **argv)
         char short_opts[] = "f";
         int rc, follow = 0;
 
-        optind = 0;
         while ((rc = getopt_long(argc, argv, short_opts,
                                 long_opts, NULL)) != -1) {
                 switch (rc) {
@@ -3160,15 +3266,16 @@ static int lfs_changelog(int argc, char **argv)
         if (argc > optind)
                 endrec = strtoll(argv[optind++], NULL, 10);
 
-        rc = llapi_changelog_start(&changelog_priv,
-                                   CHANGELOG_FLAG_BLOCK |
-                                   (follow ? CHANGELOG_FLAG_FOLLOW : 0),
-                                   mdd, startrec);
-        if (rc < 0) {
-                fprintf(stderr, "Can't start changelog: %s\n",
-                        strerror(errno = -rc));
-                return rc;
-        }
+       rc = llapi_changelog_start(&changelog_priv,
+                                  CHANGELOG_FLAG_BLOCK |
+                                  CHANGELOG_FLAG_JOBID |
+                                  (follow ? CHANGELOG_FLAG_FOLLOW : 0),
+                                  mdd, startrec);
+       if (rc < 0) {
+               fprintf(stderr, "Can't start changelog: %s\n",
+                       strerror(errno = -rc));
+               return rc;
+       }
 
         while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
                 time_t secs;
@@ -3183,28 +3290,42 @@ static int lfs_changelog(int argc, char **argv)
                         continue;
                 }
 
-                secs = rec->cr_time >> 30;
-                gmtime_r(&secs, &ts);
-                printf(LPU64" %02d%-5s %02d:%02d:%02d.%06d %04d.%02d.%02d "
-                       "0x%x t="DFID, rec->cr_index, rec->cr_type,
-                       changelog_type2str(rec->cr_type),
-                       ts.tm_hour, ts.tm_min, ts.tm_sec,
-                       (int)(rec->cr_time & ((1<<30) - 1)),
-                       ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
-                       rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
-                if (rec->cr_namelen)
-                        /* namespace rec includes parent and filename */
-                       printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
-                               rec->cr_namelen, rec->cr_name);
+               secs = rec->cr_time >> 30;
+               gmtime_r(&secs, &ts);
+               printf(LPU64" %02d%-5s %02d:%02d:%02d.%06d %04d.%02d.%02d "
+                      "0x%x t="DFID, rec->cr_index, rec->cr_type,
+                      changelog_type2str(rec->cr_type),
+                      ts.tm_hour, ts.tm_min, ts.tm_sec,
+                      (int)(rec->cr_time & ((1<<30) - 1)),
+                      ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
+                      rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
+
+               if (rec->cr_flags & CLF_JOBID) {
+                       struct changelog_ext_jobid *jid =
+                               changelog_rec_jobid(rec);
+
+                       if (jid->cr_jobid[0] != '\0')
+                               printf(" j=%s", jid->cr_jobid);
+               }
 
-               if (!fid_is_zero(&rec->cr_sfid))
-                       printf(" s="DFID" sp="DFID" %.*s",
-                               PFID(&rec->cr_sfid), PFID(&rec->cr_spfid),
-                               changelog_rec_snamelen(rec),
-                               changelog_rec_sname(rec));
+               if (rec->cr_namelen)
+                       printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
+                              rec->cr_namelen, changelog_rec_name(rec));
+
+               if (rec->cr_flags & CLF_RENAME) {
+                       struct changelog_ext_rename *rnm =
+                               changelog_rec_rename(rec);
+
+                       if (!fid_is_zero(&rnm->cr_sfid))
+                               printf(" s="DFID" sp="DFID" %.*s",
+                                      PFID(&rnm->cr_sfid),
+                                      PFID(&rnm->cr_spfid),
+                                      (int)changelog_rec_snamelen(rec),
+                                      changelog_rec_sname(rec));
+               }
                printf("\n");
 
-                llapi_changelog_free(&rec);
+               llapi_changelog_free(&rec);
         }
 
         llapi_changelog_fini(&changelog_priv);
@@ -3248,8 +3369,6 @@ static int lfs_fid2path(int argc, char **argv)
         int printcur = 0;
        int rc = 0;
 
-        optind = 0;
-
         while ((rc = getopt_long(argc, argv, short_opts,
                                 long_opts, NULL)) != -1) {
                 switch (rc) {
@@ -3326,33 +3445,74 @@ static int lfs_fid2path(int argc, char **argv)
 
 static int lfs_path2fid(int argc, char **argv)
 {
-       char **path;
-       const char *sep = "";
-       lustre_fid fid;
-       int rc = 0;
+       struct option     long_opts[] = {
+               {"parents", no_argument, 0, 'p'},
+               {0, 0, 0, 0}
+       };
+       char            **path;
+       const char        short_opts[] = "p";
+       const char       *sep = "";
+       lustre_fid        fid;
+       int               rc = 0;
+       bool              show_parents = false;
+
+       while ((rc = getopt_long(argc, argv, short_opts,
+                                long_opts, NULL)) != -1) {
+               switch (rc) {
+               case 'p':
+                       show_parents = true;
+                       break;
+               default:
+                       fprintf(stderr, "error: %s: option '%s' unrecognized\n",
+                               argv[0], argv[optind - 1]);
+                       return CMD_HELP;
+               }
+       }
 
-       if (argc < 2)
+       if (optind > argc - 1)
                return CMD_HELP;
-       else if (argc > 2)
+       else if (optind < argc - 1)
                sep = ": ";
 
-       path = argv + 1;
-       while (*path != NULL) {
-               int err = llapi_path2fid(*path, &fid);
+       rc = 0;
+       for (path = argv + optind; *path != NULL; path++) {
+               int err = 0;
+               if (!show_parents) {
+                       err = llapi_path2fid(*path, &fid);
+                       if (!err)
+                               printf("%s%s"DFID"\n",
+                                      *sep != '\0' ? *path : "", sep,
+                                      PFID(&fid));
+               } else {
+                       char            name[NAME_MAX + 1];
+                       unsigned int    linkno = 0;
+
+                       while ((err = llapi_path2parent(*path, linkno, &fid,
+                                               name, sizeof(name))) == 0) {
+                               if (*sep != '\0' && linkno == 0)
+                                       printf("%s%s", *path, sep);
+
+                               printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
+                                      PFID(&fid), name);
+                               linkno++;
+                       }
+
+                       /* err == -ENODATA is end-of-loop */
+                       if (linkno > 0 && err == -ENODATA) {
+                               printf("\n");
+                               err = 0;
+                       }
+               }
 
                if (err) {
-                       fprintf(stderr, "%s: can't get fid for %s: %s\n",
-                               argv[0], *path, strerror(-err));
+                       fprintf(stderr, "%s: can't get %sfid for %s: %s\n",
+                               argv[0], show_parents ? "parent " : "", *path,
+                               strerror(-err));
                        if (rc == 0) {
                                rc = err;
                                errno = -err;
                        }
-                       goto out;
                }
-               printf("%s%s"DFID"\n", *sep != '\0' ? *path : "", sep,
-                      PFID(&fid));
-out:
-               path++;
        }
 
        return rc;
@@ -3370,7 +3530,6 @@ static int lfs_data_version(int argc, char **argv)
        if (argc < 2)
                return CMD_HELP;
 
-       optind = 0;
        while ((c = getopt(argc, argv, "nrw")) != -1) {
                switch (c) {
                case 'n':
@@ -3480,7 +3639,6 @@ static int lfs_hsm_change_flags(int argc, char **argv, int mode)
        if (argc < 3)
                return CMD_HELP;
 
-       optind = 0;
        while ((c = getopt_long(argc, argv, short_opts,
                                long_opts, NULL)) != -1) {
                switch (c) {
@@ -3616,6 +3774,13 @@ static int lfs_hsm_prepare_file(char *file, struct lu_fid *fid,
                fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
                return -errno;
        }
+       /* Checking for regular file as archiving as posix copytool
+        * rejects archiving files other than regular files
+        */
+       if (!S_ISREG(st.st_mode)) {
+               fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
+               return CMD_HELP;
+       }
        /* A request should be ... */
        if (*last_dev != st.st_dev && *last_dev != 0) {
                fprintf(stderr, "All files should be "
@@ -3661,7 +3826,6 @@ static int lfs_hsm_request(int argc, char **argv, int action)
        if (argc < 2)
                return CMD_HELP;
 
-       optind = 0;
        while ((c = getopt_long(argc, argv, short_opts,
                                long_opts, NULL)) != -1) {
                switch (c) {
@@ -3755,7 +3919,7 @@ static int lfs_hsm_request(int argc, char **argv, int action)
                                hur = llapi_hsm_user_request_alloc(nbfile_alloc,
                                                                   opaque_len);
                                if (hur == NULL) {
-                                       fprintf(stderr, "Cannot allocate "
+                                       fprintf(stderr, "hsm: cannot allocate "
                                                "the request: %s\n",
                                                strerror(errno));
                                        hur = oldhur;
@@ -3765,8 +3929,11 @@ static int lfs_hsm_request(int argc, char **argv, int action)
                                }
                                size = hur_len(oldhur);
                                if (size < 0) {
-                                       fprintf(stderr, "Cannot allocate "
-                                               "the requested size\n");
+                                       fprintf(stderr, "hsm: cannot allocate "
+                                               "%u files + %u bytes data\n",
+                                           oldhur->hur_request.hr_itemcount,
+                                           oldhur->hur_request.hr_data_len);
+                                       free(hur);
                                        hur = oldhur;
                                        rc = -E2BIG;
                                        fclose(fp);
@@ -3862,6 +4029,10 @@ int main(int argc, char **argv)
 {
         int rc;
 
+       /* Ensure that liblustreapi constructor has run */
+       if (!liblustreapi_initialized)
+               fprintf(stderr, "liblustreapi was not properly initialized\n");
+
         setlinebuf(stdout);
 
        Parser_init("lfs > ", cmdlist);