Whamcloud - gitweb
LU-8631 quota: better error message for 'lfs quota'
[fs/lustre-release.git] / lustre / utils / lfs.c
index 3e42beb..9ba0757 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
@@ -27,7 +23,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, 2015, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 #include <getopt.h>
 #include <string.h>
 #include <mntent.h>
+#include <unistd.h>
 #include <errno.h>
 #include <err.h>
 #include <pwd.h>
 #include <grp.h>
+#include <sys/ioctl.h>
 #include <sys/quota.h>
+#include <sys/time.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 # include <sys/quota.h>
 #endif
 
-#include <libcfs/libcfs.h>
-#include <libcfs/libcfsutil.h>
-#include <lnet/lnetctl.h>
+#include <libcfs/util/string.h>
+#include <libcfs/util/ioctl.h>
+#include <libcfs/util/parser.h>
 #include <lustre/lustreapi.h>
 #include <lustre_ver.h>
-#include "obdctl.h"
+#include <lustre_param.h>
+
+#ifndef ARRAY_SIZE
+# define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
+#endif /* !ARRAY_SIZE */
 
 /* all functions */
 static int lfs_setstripe(int argc, char **argv);
@@ -85,9 +88,6 @@ static int lfs_df(int argc, char **argv);
 static int lfs_getname(int argc, char **argv);
 static int lfs_check(int argc, char **argv);
 #ifdef HAVE_SYS_QUOTA_H
-static int lfs_quotacheck(int argc, char **argv);
-static int lfs_quotaon(int argc, char **argv);
-static int lfs_quotaoff(int argc, char **argv);
 static int lfs_setquota(int argc, char **argv);
 static int lfs_quota(int argc, char **argv);
 #endif
@@ -116,22 +116,63 @@ static int lfs_hsm_remove(int argc, char **argv);
 static int lfs_hsm_cancel(int argc, char **argv);
 static int lfs_swap_layouts(int argc, char **argv);
 static int lfs_mv(int argc, char **argv);
-
-#define SETSTRIPE_USAGE(_cmd, _tgt) \
-       "usage: "_cmd" [--stripe-count|-c <stripe_count>]\n"\
-       "                 [--stripe-index|-i <start_ost_idx>]\n"\
-       "                 [--stripe-size|-S <stripe_size>]\n"\
-       "                 [--pool|-p <pool_name>]\n"\
-       "                 [--block|-b] "_tgt"\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"
-
-/* all avaialable commands */
+static int lfs_ladvise(int argc, char **argv);
+
+/* Setstripe and migrate share mostly the same parameters */
+#define SSM_CMD_COMMON(cmd) \
+       "usage: "cmd" [--stripe-count|-c <stripe_count>]\n"             \
+       "                 [--stripe-index|-i <start_ost_idx>]\n"        \
+       "                 [--stripe-size|-S <stripe_size>]\n"           \
+       "                 [--pool|-p <pool_name>]\n"                    \
+       "                 [--ost|-o <ost_indices>]\n"
+
+#define SSM_HELP_COMMON \
+       "\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"      \
+       "\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, then the OSTs\n" \
+       "\t              must be the members of the pool."
+
+#define SETSTRIPE_USAGE                                                \
+       SSM_CMD_COMMON("setstripe")                             \
+       "                 <directory|filename>\n"               \
+       SSM_HELP_COMMON
+
+#define MIGRATE_USAGE                                                  \
+       SSM_CMD_COMMON("migrate  ")                                     \
+       "                 [--block|-b]\n"                               \
+       "                 [--non-block|-n]\n"                           \
+       "                 <filename>\n"                                 \
+       SSM_HELP_COMMON                                                 \
+       "\n"                                                            \
+       "\tblock:        Block file access during data migration (default)\n" \
+       "\tnon-block:    Abort migrations if concurrent access is detected\n" \
+
+#define SETDIRSTRIPE_USAGE                                     \
+       "               [--mdt-count|-c stripe_count>\n"        \
+       "               [--mdt-index|-i mdt_index]\n"           \
+       "               [--mdt-hash|-t mdt_hash]\n"             \
+       "               [--default_stripe|-D] [--mode|-m mode] <dir>\n" \
+       "\tstripe_count: stripe count of the striped directory\n"       \
+       "\tmdt_index: MDT index of first stripe\n"                      \
+       "\tmdt_hash:  hash type of the striped directory. mdt types:\n" \
+       "       fnv_1a_64 FNV-1a hash algorithm (default)\n"            \
+       "       all_char  sum of characters % MDT_COUNT (not recommended)\n" \
+       "\tdefault_stripe: set default dirstripe of the directory\n"    \
+       "\tmode: the mode of the directory\n"
+
+static const char      *progname;
+static bool             file_lease_supported = true;
+
+/* all available commands */
 command_t cmdlist[] = {
        {"setstripe", lfs_setstripe, 0,
         "Create a new file with a specific striping pattern or\n"
@@ -139,25 +180,21 @@ command_t cmdlist[] = {
         "delete the default striping pattern from an existing directory\n"
         "usage: setstripe -d <directory>   (to delete default striping)\n"\
         " or\n"
-        SETSTRIPE_USAGE("setstripe", "<directory|filename>")},
+        SETSTRIPE_USAGE},
        {"getstripe", lfs_getstripe, 0,
         "To list the striping info for a given file or files in a\n"
         "directory or recursively for all files in a directory tree.\n"
-        "usage: getstripe [--ost|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
+        "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
         "                 [--stripe-count|-c] [--stripe-index|-i]\n"
         "                 [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
-        "                 [--mdt-index|-M] [--recursive|-r] [--raw|-R]\n"
-        "                 [--layout|-L]\n"
+        "                 [--mdt|-m] [--recursive|-r] [--raw|-R]\n"
+        "                 [--layout|-L] [--fid|-F] [--generation|-g]\n"
         "                 <directory|filename> ..."},
        {"setdirstripe", lfs_setdirstripe, 0,
-        "To create a remote directory on a specified MDT.\n"
-        "usage: setdirstripe <--count|-c stripe_count>\n"
-        "[--index|-i mdt_index] [--hash-type|-t hash_type]\n"
-        "[--default_stripe|-D ] <dir>\n"
-        "\tstripe_count: stripe count of the striped directory\n"
-        "\tmdt_index:  MDT index of first stripe\n"
-        "\thash_type:  hash type of the striped directory\n"
-        "\tdefault_stripe: set default dirstripe of the directory\n"},
+        "To create a striped directory on a specified MDT. This can only\n"
+        "be done on MDT0 with the right of administrator.\n"
+        "usage: setdirstripe [OPTION] <directory>\n"
+        SETDIRSTRIPE_USAGE},
        {"getdirstripe", lfs_getdirstripe, 0,
         "To list the striping info for a given directory\n"
         "or recursively for all directories in a directory tree.\n"
@@ -165,10 +202,10 @@ command_t cmdlist[] = {
         "               [--count|-c ] [--index|-i ] [--raw|-R]\n"
         "               [--recursive | -r] [ --default_stripe | -D ] <dir> "},
        {"mkdir", lfs_setdirstripe, 0,
-        "To create a remote directory on a specified MDT. And this can only\n"
-        "be done on MDT0 by administrator.\n"
-        "usage: mkdir <--index|-i mdt_index> <dir>\n"
-        "\tmdt_index:    MDT index of the remote directory.\n"},
+        "To create a striped directory on a specified MDT. This can only\n"
+        "be done on MDT0 with the right of administrator.\n"
+        "usage: mkdir [OPTION] <directory>\n"
+        SETDIRSTRIPE_USAGE},
        {"rm_entry", lfs_rmentry, 0,
         "To remove the name entry of the remote directory. Note: This\n"
         "command will only delete the name entry, i.e. the remote directory\n"
@@ -214,16 +251,6 @@ command_t cmdlist[] = {
          "[for specified path only]\n"
          "Usage: getname [-h]|[path ...] "},
 #ifdef HAVE_SYS_QUOTA_H
-        {"quotacheck", lfs_quotacheck, 0,
-         "Scan the specified filesystem for disk usage, and create,\n"
-         "or update quota files. Deprecated as of 2.4.0.\n"
-         "usage: quotacheck [ -ug ] <filesystem>"},
-        {"quotaon", lfs_quotaon, 0, "Turn filesystem"
-         " quotas on. Deprecated as of 2.4.0.\n"
-         "usage: quotaon [ -ugf ] <filesystem>"},
-        {"quotaoff", lfs_quotaoff, 0, "Turn filesystem"
-         " quotas off. Deprecated as of 2.4.0.\n"
-         "usage: quotaoff [ -ug ] <filesystem>"},
         {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
          "usage: setquota <-u|-g> <uname>|<uid>|<gname>|<gid>\n"
          "                -b <block-softlimit> -B <block-hardlimit>\n"
@@ -288,7 +315,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, "
@@ -314,102 +341,381 @@ command_t cmdlist[] = {
         "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
        {"hsm_remove", lfs_hsm_remove, 0,
         "Remove file copy from external storage.\n"
-        "usage: hsm_remove [--filelist FILELIST] [--data DATA] <file> ..."},
+        "usage: hsm_remove [--filelist FILELIST] [--data DATA]\n"
+        "                  [--mntpath MOUNTPATH] [--archive NUM] <file|FID> ...\n"
+        "\n"
+        "Note: To remove files from the archive that have been deleted on\n"
+        "Lustre, set mntpath and optionally archive. In that case, all the\n"
+        "positional arguments and entries in the file list must be FIDs."
+       },
        {"hsm_cancel", lfs_hsm_cancel, 0,
         "Cancel requests related to specified files.\n"
         "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
        {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
         "usage: swap_layouts <path1> <path2>"},
-       {"migrate", lfs_setstripe, 0, "migrate file from one OST layout to "
-        "another (may be not safe with concurent writes).\n"
-        SETSTRIPE_USAGE("migrate  ", "<filename>")},
+       {"migrate", lfs_setstripe, 0,
+        "migrate a directory between MDTs.\n"
+        "usage: migrate --mdt-index <mdt_idx> [--verbose|-v] "
+        "<directory>\n"
+        "\tmdt_idx:      index of the destination MDT\n"
+        "\n"
+        "migrate file objects from one OST "
+        "layout\nto another (may be not safe with concurent writes).\n"
+        "usage: migrate  "
+        "[--stripe-count|-c] <stripe_count>\n"
+        "              [--stripe-index|-i] <start_ost_index>\n"
+        "              [--stripe-size|-S] <stripe_size>\n"
+        "              [--pool|-p] <pool_name>\n"
+        "              [--ost-list|-o] <ost_indices>\n"
+        "              [--block|-b]\n"
+        "              [--non-block|-n]\n"
+        "              <file|directory>\n"
+        "\tstripe_count:     number of OSTs to stripe a file over\n"
+        "\tstripe_ost_index: index of the first OST to stripe a file over\n"
+        "\tstripe_size:      number of bytes to store before moving to the next OST\n"
+        "\tpool_name:        name of the predefined pool of OSTs\n"
+        "\tost_indices:      OSTs to stripe over, in order\n"
+        "\tblock:            wait for the operation to return before continuing\n"
+        "\tnon-block:        do not wait for the operation to return.\n"},
        {"mv", lfs_mv, 0,
-        "To move directories between MDTs.\n"
+        "To move directories between MDTs. This command is deprecated, "
+        "use \"migrate\" instead.\n"
         "usage: mv <directory|filename> [--mdt-index|-M] <mdt_index> "
         "[--verbose|-v]\n"},
+       {"ladvise", lfs_ladvise, 0,
+        "Provide servers with advice about access patterns for a file.\n"
+        "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
+        "               [--background|-b]\n"
+        "               {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
+        "               <file> ..."},
        {"help", Parser_help, 0, "help"},
        {"exit", Parser_quit, 0, "quit"},
        {"quit", Parser_quit, 0, "quit"},
+       {"--version", Parser_version, 0,
+        "output build version of the utility and exit"},
        { 0, 0, 0, NULL }
 };
 
-#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)
+#define MIGRATION_NONBLOCK     1
+
+/**
+ * Internal helper for migrate_copy_data(). Check lease and report error if
+ * need be.
+ *
+ * \param[in]  fd           File descriptor on which to check the lease.
+ * \param[out] lease_broken Set to true if the lease was broken.
+ * \param[in]  group_locked Whether a group lock was taken or not.
+ * \param[in]  path         Name of the file being processed, for error
+ *                         reporting
+ *
+ * \retval 0       Migration can keep on going.
+ * \retval -errno  Error occurred, abort migration.
+ */
+static int check_lease(int fd, bool *lease_broken, bool group_locked,
+                      const char *path)
+{
+       int rc;
+
+       if (!file_lease_supported)
+               return 0;
+
+       rc = llapi_lease_check(fd);
+       if (rc > 0)
+               return 0; /* llapi_check_lease returns > 0 on success. */
+
+       if (!group_locked) {
+               fprintf(stderr, "%s: cannot migrate '%s': file busy\n",
+                       progname, path);
+               rc = rc ? rc : -EAGAIN;
+       } else {
+               fprintf(stderr, "%s: external attempt to access file '%s' "
+                       "blocked until migration ends.\n", progname, path);
+               rc = 0;
+       }
+       *lease_broken = true;
+       return rc;
+}
+
+static int migrate_copy_data(int fd_src, int fd_dst, size_t buf_size,
+                            bool group_locked, const char *fname)
+{
+       void    *buf = NULL;
+       ssize_t  rsize = -1;
+       ssize_t  wsize = 0;
+       size_t   rpos = 0;
+       size_t   wpos = 0;
+       off_t    bufoff = 0;
+       int      rc;
+       bool     lease_broken = false;
+
+       /* Use a page-aligned buffer for direct I/O */
+       rc = posix_memalign(&buf, getpagesize(), buf_size);
+       if (rc != 0)
+               return -rc;
+
+       while (1) {
+               /* read new data only if we have written all
+                * previously read data */
+               if (wpos == rpos) {
+                       if (!lease_broken) {
+                               rc = check_lease(fd_src, &lease_broken,
+                                                group_locked, fname);
+                               if (rc < 0)
+                                       goto out;
+                       }
+                       rsize = read(fd_src, buf, buf_size);
+                       if (rsize < 0) {
+                               rc = -errno;
+                               fprintf(stderr, "%s: %s: read failed: %s\n",
+                                       progname, fname, strerror(-rc));
+                               goto out;
+                       }
+                       rpos += rsize;
+                       bufoff = 0;
+               }
+               /* eof ? */
+               if (rsize == 0)
+                       break;
+
+               wsize = write(fd_dst, buf + bufoff, rpos - wpos);
+               if (wsize < 0) {
+                       rc = -errno;
+                       fprintf(stderr,
+                               "%s: %s: write failed on volatile: %s\n",
+                               progname, fname, strerror(-rc));
+                       goto out;
+               }
+               wpos += wsize;
+               bufoff += wsize;
+       }
+
+       rc = fsync(fd_dst);
+       if (rc < 0) {
+               rc = -errno;
+               fprintf(stderr, "%s: %s: fsync failed: %s\n",
+                       progname, fname, strerror(-rc));
+       }
+
+out:
+       free(buf);
+       return rc;
+}
+
+static int migrate_copy_timestamps(int fdv, const struct stat *st)
 {
-       int                      fd, fdv;
-       char                     volatile_file[PATH_MAX];
+       struct timeval  tv[2] = {
+               {.tv_sec = st->st_atime},
+               {.tv_sec = st->st_mtime}
+       };
+
+       return futimes(fdv, tv);
+}
+
+static int migrate_block(int fd, int fdv, const struct stat *st,
+                        size_t buf_size, const char *name)
+{
+       __u64   dv1;
+       int     gid;
+       int     rc;
+       int     rc2;
+
+       rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
+       if (rc < 0) {
+               fprintf(stderr, "%s: %s: cannot get dataversion: %s\n",
+                       progname, name, strerror(-rc));
+               return rc;
+       }
+
+       do
+               gid = random();
+       while (gid == 0);
+
+       /* The grouplock blocks all concurrent accesses to the file.
+        * It has to be taken after llapi_get_data_version as it would
+        * block it too. */
+       rc = llapi_group_lock(fd, gid);
+       if (rc < 0) {
+               fprintf(stderr, "%s: %s: cannot get group lock: %s\n",
+                       progname, name, strerror(-rc));
+               return rc;
+       }
+
+       rc = migrate_copy_data(fd, fdv, buf_size, true, name);
+       if (rc < 0) {
+               fprintf(stderr, "%s: %s: data copy failed\n", progname, name);
+               goto out_unlock;
+       }
+
+       /* Make sure we keep original atime/mtime values */
+       rc = migrate_copy_timestamps(fdv, st);
+       if (rc < 0) {
+               fprintf(stderr, "%s: %s: timestamp copy failed\n",
+                       progname, name);
+               goto out_unlock;
+       }
+
+       /* swap layouts
+        * for a migration we need to check data version on file did
+        * not change.
+        *
+        * Pass in gid=0 since we already own grouplock. */
+       rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
+                                          SWAP_LAYOUTS_CHECK_DV1);
+       if (rc == -EAGAIN) {
+               fprintf(stderr, "%s: %s: dataversion changed during copy, "
+                       "migration aborted\n", progname, name);
+               goto out_unlock;
+       } else if (rc < 0) {
+               fprintf(stderr, "%s: %s: cannot swap layouts: %s\n", progname,
+                       name, strerror(-rc));
+               goto out_unlock;
+       }
+
+out_unlock:
+       rc2 = llapi_group_unlock(fd, gid);
+       if (rc2 < 0 && rc == 0) {
+               fprintf(stderr, "%s: %s: putting group lock failed: %s\n",
+                       progname, name, strerror(-rc2));
+               rc = rc2;
+       }
+
+       return rc;
+}
+
+static int migrate_nonblock(int fd, int fdv, const struct stat *st,
+                           size_t buf_size, const char *name)
+{
+       __u64   dv1;
+       __u64   dv2;
+       int     rc;
+
+       rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
+       if (rc < 0) {
+               fprintf(stderr, "%s: %s: cannot get data version: %s\n",
+                       progname, name, strerror(-rc));
+               return rc;
+       }
+
+       rc = migrate_copy_data(fd, fdv, buf_size, false, name);
+       if (rc < 0) {
+               fprintf(stderr, "%s: %s: data copy failed\n", progname, name);
+               return rc;
+       }
+
+       rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
+       if (rc != 0) {
+               fprintf(stderr, "%s: %s: cannot get data version: %s\n",
+                       progname, name, strerror(-rc));
+               return rc;
+       }
+
+       if (dv1 != dv2) {
+               rc = -EAGAIN;
+               fprintf(stderr, "%s: %s: data version changed during "
+                               "migration\n",
+                       progname, name);
+               return rc;
+       }
+
+       /* Make sure we keep original atime/mtime values */
+       rc = migrate_copy_timestamps(fdv, st);
+       if (rc < 0) {
+               fprintf(stderr, "%s: %s: timestamp copy failed\n",
+                       progname, name);
+               return rc;
+       }
+
+       /* Atomically put lease, swap layouts and close.
+        * for a migration we need to check data version on file did
+        * not change. */
+       rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
+       if (rc < 0) {
+               fprintf(stderr, "%s: %s: cannot swap layouts: %s\n",
+                       progname, name, strerror(-rc));
+               return rc;
+       }
+
+       return 0;
+}
+
+static int lfs_migrate(char *name, __u64 migration_flags,
+                      struct llapi_stripe_param *param)
+{
+       int                      fd = -1;
+       int                      fdv = -1;
        char                     parent[PATH_MAX];
+       int                      mdt_index;
+       int                      random_value;
+       char                     volatile_file[sizeof(parent) +
+                                              LUSTRE_VOLATILE_HDR_LEN +
+                                              2 * sizeof(mdt_index) +
+                                              2 * sizeof(random_value) + 4];
        char                    *ptr;
        int                      rc;
-       __u64                    dv1;
        struct lov_user_md      *lum = NULL;
-       int                      lumsz;
-       int                      bufsz;
-       void                    *buf = NULL;
-       int                      rsize, wsize;
-       __u64                    rpos, wpos, bufoff;
-       int                      gid = 0, sz;
-       int                      have_gl = 0;
-       struct stat              st, stv;
+       int                      lum_size;
+       int                      buf_size;
+       bool                     have_lease_rdlck = false;
+       struct stat              st;
+       struct stat              stv;
 
        /* find the right size for the IO and allocate the buffer */
-       lumsz = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
-       lum = malloc(lumsz);
+       lum_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
+       lum = malloc(lum_size);
        if (lum == NULL) {
                rc = -ENOMEM;
                goto free;
        }
 
        rc = llapi_file_get_stripe(name, lum);
-       /* failure can come from may case and some may be not real error
+       /* failure can happen for many reasons and some may be not real errors
         * (eg: no stripe)
-        * in case of a real error, a later call will failed with a better
+        * in case of a real error, a later call will fail with better
         * error management */
        if (rc < 0)
-               bufsz = 1024*1024;
+               buf_size = 1024 * 1024;
        else
-               bufsz = lum->lmm_stripe_size;
-       rc = posix_memalign(&buf, getpagesize(), bufsz);
-       if (rc != 0) {
-               rc = -rc;
+               buf_size = lum->lmm_stripe_size;
+
+       /* open file, direct io */
+       /* even if the file is only read, WR mode is nedeed to allow
+        * layout swap on fd */
+       fd = open(name, O_RDWR | O_DIRECT);
+       if (fd == -1) {
+               rc = -errno;
+               fprintf(stderr, "%s: %s: cannot open: %s\n", progname, name,
+                       strerror(-rc));
                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;
+       if (file_lease_supported) {
+               rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
+               if (rc == -EOPNOTSUPP) {
+                       /* Older servers do not support file lease.
+                        * Disable related checks. This opens race conditions
+                        * as explained in LU-4840 */
+                       file_lease_supported = false;
+               } else if (rc < 0) {
+                       fprintf(stderr, "%s: %s: cannot get open lease: %s\n",
+                               progname, name, strerror(-rc));
+                       goto error;
+               } else {
+                       have_lease_rdlck = true;
                }
        }
 
        /* search for file directory pathname */
        if (strlen(name) > sizeof(parent)-1) {
                rc = -E2BIG;
-               goto free;
+               goto error;
        }
        strncpy(parent, name, sizeof(parent));
        ptr = strrchr(parent, '/');
        if (ptr == NULL) {
                if (getcwd(parent, sizeof(parent)) == NULL) {
                        rc = -errno;
-                       goto free;
+                       goto error;
                }
        } else {
                if (ptr == parent)
@@ -417,30 +723,36 @@ static int lfs_migrate(char *name, unsigned long long stripe_size,
                else
                        *ptr = '\0';
        }
-       sprintf(volatile_file, "%s/%s::", parent, LUSTRE_VOLATILE_HDR);
 
-       /* 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);
-       if (fdv < 0) {
-               rc = fdv;
-               fprintf(stderr, "cannot create volatile file in %s (%s)\n",
-                       parent, strerror(-rc));
-               goto free;
+       rc = llapi_file_fget_mdtidx(fd, &mdt_index);
+       if (rc < 0) {
+               fprintf(stderr, "%s: %s: cannot get MDT index: %s\n",
+                       progname, name, strerror(-rc));
+               goto error;
        }
 
-       /* open file, direct io */
-       /* even if the file is only read, WR mode is nedeed to allow
-        * layout swap on fd */
-       fd = open(name, O_RDWR | O_DIRECT);
-       if (fd == -1) {
-               rc = -errno;
-               fprintf(stderr, "cannot open %s (%s)\n", name, strerror(-rc));
-               close(fdv);
-               goto free;
+       do {
+               random_value = random();
+               rc = snprintf(volatile_file, sizeof(volatile_file),
+                             "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
+                             mdt_index, random_value);
+               if (rc >= sizeof(volatile_file)) {
+                       rc = -E2BIG;
+                       goto error;
+               }
+
+               /* create, open a volatile file, use caching (ie no directio) */
+               fdv = llapi_file_open_param(volatile_file,
+                               O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW,
+                                           S_IRUSR | S_IWUSR, param);
+       } while (fdv == -EEXIST);
+
+       if (fdv < 0) {
+               rc = fdv;
+               fprintf(stderr, "%s: %s: cannot create volatile file in"
+                               " directory: %s\n",
+                       progname, parent, strerror(-rc));
+               goto error;
        }
 
        /* Not-owner (root?) special case.
@@ -450,155 +762,173 @@ static int lfs_migrate(char *name, unsigned long long stripe_size,
        rc = fstat(fd, &st);
        if (rc != 0) {
                rc = -errno;
-               fprintf(stderr, "cannot stat %s (%s)\n", name,
+               fprintf(stderr, "%s: %s: cannot stat: %s\n", progname, name,
                        strerror(errno));
                goto error;
        }
        rc = fstat(fdv, &stv);
        if (rc != 0) {
                rc = -errno;
-               fprintf(stderr, "cannot stat %s (%s)\n", volatile_file,
-                       strerror(errno));
+               fprintf(stderr, "%s: %s: cannot stat: %s\n", progname,
+                       volatile_file, strerror(errno));
                goto error;
        }
        if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
                rc = fchown(fdv, st.st_uid, st.st_gid);
                if (rc != 0) {
                        rc = -errno;
-                       fprintf(stderr, "cannot chown %s (%s)\n", name,
-                               strerror(errno));
+                       fprintf(stderr, "%s: %s: cannot chown: %s\n", progname,
+                               name, strerror(errno));
                        goto error;
                }
        }
 
-       /* get file data version */
-       rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
-       if (rc != 0) {
-               fprintf(stderr, "cannot get dataversion on %s (%s)\n",
-                       name, strerror(-rc));
-               goto error;
-       }
-
-       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;
-                       fprintf(stderr, "cannot get group lock on %s (%s)\n",
-                               name, strerror(-rc));
-                       goto error;
+       if (migration_flags & MIGRATION_NONBLOCK && file_lease_supported) {
+               rc = migrate_nonblock(fd, fdv, &st, buf_size, name);
+               if (rc == 0) {
+                       have_lease_rdlck = false;
+                       fdv = -1; /* The volatile file is closed as we put the
+                                  * lease in non-blocking mode. */
                }
-               have_gl = 1;
+       } else {
+               /* Blocking mode (forced if servers do not support file lease).
+                * It is also the default mode, since we cannot distinguish
+                * between a broken lease and a server that does not support
+                * atomic swap/close (LU-6785) */
+               rc = migrate_block(fd, fdv, &st, buf_size, name);
        }
 
-       /* copy data */
-       rpos = 0;
-       wpos = 0;
-       bufoff = 0;
-       rsize = -1;
-       do {
-               /* read new data only if we have written all
-                * previously read data */
-               if (wpos == rpos) {
-                       rsize = read(fd, buf, bufsz);
-                       if (rsize < 0) {
-                               rc = -errno;
-                               fprintf(stderr, "read failed on %s"
-                                       " (%s)\n", name,
-                                       strerror(-rc));
-                               goto error;
-                       }
-                       rpos += rsize;
-                       bufoff = 0;
-               }
-               /* eof ? */
-               if (rsize == 0)
+error:
+       if (have_lease_rdlck)
+               llapi_lease_put(fd);
+
+       if (fd >= 0)
+               close(fd);
+
+       if (fdv >= 0)
+               close(fdv);
+
+free:
+       if (lum)
+               free(lum);
+
+       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;
-               wsize = write(fdv, buf + bufoff, rpos - wpos);
-               if (wsize < 0) {
-                       rc = -errno;
-                       fprintf(stderr, "write failed on volatile"
-                               " for %s (%s)\n", name, strerror(-rc));
-                       goto error;
+               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;
                }
-               wpos += wsize;
-               bufoff += wsize;
-       } while (1);
 
-       /* flush data */
-       fsync(fdv);
+               for (i = start_index; i <= end_index && slots > 0; i++) {
+                       int j;
 
-       if (migration_flags & MIGRATION_BLOCKS) {
-               /* give back group lock */
-               if (ioctl(fd, LL_IOC_GROUP_UNLOCK, gid) == -1) {
-                       rc = -errno;
-                       fprintf(stderr, "cannot put group lock on %s (%s)\n",
-                               name, strerror(-rc));
+                       /* remove duplicate */
+                       for (j = 0; j < offset; j++) {
+                               if (osts[j] == i)
+                                       break;
+                       }
+                       if (j == offset) { /* no duplicate */
+                               osts[nr++] = i;
+                               --slots;
+                       }
                }
-               have_gl = 0;
-       }
-
-       /* swap layouts
-        * for a migration we need to:
-        * - check data version on file did not change
-        * - keep file mtime
-        * - keep file atime
-        */
-       rc = llapi_fswap_layouts(fd, fdv, dv1, 0,
-                                SWAP_LAYOUTS_CHECK_DV1 |
-                                SWAP_LAYOUTS_KEEP_MTIME |
-                                SWAP_LAYOUTS_KEEP_ATIME);
-       if (rc == -EAGAIN) {
-               fprintf(stderr, "%s: dataversion changed during copy, "
-                       "migration aborted\n", name);
-               goto error;
-       }
-       if (rc != 0)
-               fprintf(stderr, "%s: swap layout to new file failed: %s\n",
-                       name, strerror(-rc));
+               if (slots == 0 && i < end_index)
+                       break;
 
-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));
+               *ptr = ',';
+               arg = ++ptr;
+               offset = nr;
+               rc = 0;
        }
+       if (!end_of_loop && ptr != NULL)
+               *ptr = ',';
 
-       close(fdv);
-       close(fd);
-free:
-       if (lum)
-               free(lum);
-       if (buf)
-               free(buf);
-       return rc;
+       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 = NULL;
+       struct find_param                migrate_mdt_param = {
+               .fp_max_depth = -1,
+               .fp_mdt_index = -1,
+       };
+       char                            *fname;
+       int                              result;
+       int                              result2 = 0;
+       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;
+       char                            *mdt_idx_arg = NULL;
+       unsigned long long               size_units = 1;
+       bool                             migrate_mode = false;
+       bool                             migration_block = 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 */
+               /* --block is only valid in migrate mode */
                {"block",        no_argument,       0, 'b'},
 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
                /* This formerly implied "stripe-count", but was explicitly
@@ -617,11 +947,15 @@ 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'},
+               {"mdt",          required_argument, 0, 'm'},
+               {"mdt-index",    required_argument, 0, 'm'},
+               {"mdt_index",    required_argument, 0, 'm'},
+               /* --non-block is only valid in migrate mode */
+               {"non-block",    no_argument,       0, 'n'},
+               {"ost",          required_argument, 0, 'o'},
+#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
+               {"ost-list",     required_argument, 0, 'o'},
+               {"ost_list",     required_argument, 0, 'o'},
 #endif
                {"pool",         required_argument, 0, 'p'},
 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
@@ -632,30 +966,31 @@ static int lfs_setstripe(int argc, char **argv)
 #endif
                {"stripe-size",  required_argument, 0, 'S'},
                {"stripe_size",  required_argument, 0, 'S'},
+               /* --verbose is only valid in migrate mode */
+               {"verbose",      no_argument,       0, 'v'},
                {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:",
+       while ((c = getopt_long(argc, argv, "bc:di:m:no:p:s:S:v",
                                long_opts, NULL)) >= 0) {
                switch (c) {
                case 0:
                        /* Long options. */
                        break;
                case 'b':
-                       if (migrate_mode == 0) {
+                       if (!migrate_mode) {
                                fprintf(stderr, "--block is valid only for"
-                                               " migrate mode");
+                                               " migrate mode\n");
                                return CMD_HELP;
                        }
-                       migration_flags |= MIGRATION_BLOCKS;
+                       migration_block = true;
                        break;
                case 'c':
 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
@@ -669,11 +1004,20 @@ 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,
+                                               sizeof(osts) / sizeof(__u32),
+                                               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)
@@ -682,6 +1026,22 @@ static int lfs_setstripe(int argc, char **argv)
 #endif
                        stripe_off_arg = optarg;
                        break;
+               case 'm':
+                       if (!migrate_mode) {
+                               fprintf(stderr, "--mdt-index is valid only for"
+                                               " migrate mode\n");
+                               return CMD_HELP;
+                       }
+                       mdt_idx_arg = optarg;
+                       break;
+               case 'n':
+                       if (!migrate_mode) {
+                               fprintf(stderr, "--non-block is valid only for"
+                                               " migrate mode\n");
+                               return CMD_HELP;
+                       }
+                       migration_flags |= MIGRATION_NONBLOCK;
+                       break;
 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
                case 's':
 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
@@ -695,6 +1055,14 @@ static int lfs_setstripe(int argc, char **argv)
                case 'p':
                        pool_name_arg = optarg;
                        break;
+               case 'v':
+                       if (!migrate_mode) {
+                               fprintf(stderr, "--verbose is valid only for"
+                                               " migrate mode\n");
+                               return CMD_HELP;
+                       }
+                       migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
+                       break;
                default:
                        return CMD_HELP;
                }
@@ -717,6 +1085,56 @@ static int lfs_setstripe(int argc, char **argv)
                return CMD_HELP;
        }
 
+       if (mdt_idx_arg != NULL && optind > 3) {
+               fprintf(stderr, "error: %s: cannot specify -m with other "
+                       "options\n", argv[0]);
+               return CMD_HELP;
+       }
+
+       if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
+               fprintf(stderr,
+                       "error: %s: cannot specify --non-block and --block\n",
+                       argv[0]);
+               return CMD_HELP;
+       }
+
+       if (pool_name_arg != NULL) {
+               char    *ptr;
+               int     rc;
+
+               ptr = strchr(pool_name_arg, '.');
+               if (ptr == NULL) {
+                       ptr = pool_name_arg;
+               } else {
+                       if ((ptr - pool_name_arg) == 0) {
+                               fprintf(stderr, "error: %s: fsname is empty "
+                                       "in pool name '%s'\n",
+                                       argv[0], pool_name_arg);
+                               return CMD_HELP;
+                       }
+
+                       ++ptr;
+               }
+
+               rc = lustre_is_poolname_valid(ptr, 1, LOV_MAXPOOLNAME);
+               if (rc == -1) {
+                       fprintf(stderr, "error: %s: poolname '%s' is "
+                               "empty\n",
+                               argv[0], pool_name_arg);
+                       return CMD_HELP;
+               } else if (rc == -2) {
+                       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;
+               } else if (rc > 0) {
+                       fprintf(stderr, "error: %s: char '%c' not allowed in "
+                               "pool name '%s'\n",
+                               argv[0], rc, pool_name_arg);
+                       return CMD_HELP;
+               }
+       }
+
        /* get the stripe size */
        if (stripe_size_arg != NULL) {
                result = llapi_parse_size(stripe_size_arg, &st_size,
@@ -724,7 +1142,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 */
@@ -746,26 +1164,74 @@ static int lfs_setstripe(int argc, char **argv)
                 }
         }
 
-       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 (mdt_idx_arg != NULL) {
+               /* initialize migrate mdt parameters */
+               migrate_mdt_param.fp_mdt_index = strtoul(mdt_idx_arg, &end, 0);
+               if (*end != '\0') {
+                       fprintf(stderr, "error: %s: bad MDT index '%s'\n",
+                               argv[0], mdt_idx_arg);
+                       return CMD_HELP;
+               }
+               migrate_mdt_param.fp_migrate = 1;
+       } else {
+               /* 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);
+                               free(param);
+                               return CMD_HELP;
+                       }
+
+                       param->lsp_is_specific = true;
+                       param->lsp_stripe_count = nr_osts;
+                       memcpy(param->lsp_osts, osts, sizeof(*osts) * nr_osts);
+               }
+       }
+
+       for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
+               if (!migrate_mode) {
+                       result = llapi_file_open_param(fname,
+                                                      O_CREAT | O_WRONLY,
+                                                      0644, param);
+                       if (result >= 0) {
+                               close(result);
+                               result = 0;
+                       }
+               } else if (mdt_idx_arg != NULL) {
+                       result = llapi_migrate_mdt(fname, &migrate_mdt_param);
+               } else {
+                       result = lfs_migrate(fname, migration_flags, param);
+               }
                if (result) {
-                       fprintf(stderr,
-                               "error: %s: %s stripe file '%s' failed\n",
+                       /* Save the first error encountered. */
+                       if (result2 == 0)
+                               result2 = result;
+                       fprintf(stderr, "error: %s: %s file '%s' failed: %s\n",
                                argv[0], migrate_mode ? "migrate" : "create",
-                               fname);
-                       break;
+                               fname,
+                               pool_name_arg != NULL && result == EINVAL ?
+                               "OST not in pool?" : strerror(errno));
+                       continue;
                }
-               fname = argv[++optind];
-       } while (fname != NULL);
+       }
 
-       return result;
+       free(param);
+       return result2;
 }
 
 static int lfs_poollist(int argc, char **argv)
@@ -880,11 +1346,12 @@ static int name2layout(__u32 *layout, char *name)
 #define FIND_POOL_OPT 3
 static int lfs_find(int argc, char **argv)
 {
-        int c, ret;
+       int c, rc;
+       int ret = 0;
         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'},
@@ -898,6 +1365,8 @@ static int lfs_find(int argc, char **argv)
                 {"stripe_index", required_argument, 0, 'i'},
                {"layout",       required_argument, 0, 'L'},
                 {"mdt",          required_argument, 0, 'm'},
+                {"mdt-index",    required_argument, 0, 'm'},
+                {"mdt_index",    required_argument, 0, 'm'},
                 {"mtime",        required_argument, 0, 'M'},
                 {"name",         required_argument, 0, 'n'},
      /* reserve {"or",           no_argument,     , 0, 'o'}, to match find(1) */
@@ -925,7 +1394,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",
@@ -982,40 +1450,40 @@ static int lfs_find(int argc, char **argv)
                                xsign = &param.fp_msign;
                                param.fp_exclude_mtime = !!neg_opt;
                        }
-                        ret = set_time(&t, xtime, optarg);
-                        if (ret == INT_MAX) {
-                                ret = -1;
-                                goto err;
-                        }
-                        if (ret)
-                                *xsign = ret;
-                        break;
+                       rc = set_time(&t, xtime, optarg);
+                       if (rc == INT_MAX) {
+                               ret = -1;
+                               goto err;
+                       }
+                       if (rc)
+                               *xsign = rc;
+                       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);
                        break;
                case 'g':
                case 'G':
-                       ret = name2id(&param.fp_gid, optarg, GROUP);
-                       if (ret) {
+                       rc = name2id(&param.fp_gid, optarg, GROUP);
+                       if (rc) {
                                param.fp_gid = strtoul(optarg, &endptr, 10);
                                 if (*endptr != '\0') {
                                         fprintf(stderr, "Group/GID: %s cannot "
@@ -1028,16 +1496,16 @@ 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':
-                       ret = name2id(&param.fp_uid, optarg, USER);
-                       if (ret) {
+                       rc = name2id(&param.fp_uid, optarg, USER);
+                       if (rc) {
                                param.fp_uid = strtoul(optarg, &endptr, 10);
                                 if (*endptr != '\0') {
                                         fprintf(stderr, "User/UID: %s cannot "
@@ -1060,14 +1528,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':
@@ -1082,7 +1550,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) {
@@ -1093,32 +1561,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));
-                                if (tmp == NULL)
-                                        GOTO(err_free, ret = -ENOMEM);
-                                param.mdtuuid = tmp;
+                               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.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));
-                                if (tmp == NULL)
-                                        GOTO(err_free, ret = -ENOMEM);
-                                param.obduuid = tmp;
+                               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.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;
@@ -1126,8 +1600,12 @@ static int lfs_find(int argc, char **argv)
                                         *p = 0;
                                         next = p+1;
                                 }
-                               if (strlen(token) > sizeof(puuid->uuid)-1)
-                                       GOTO(err_free, ret = -E2BIG);
+
+                               if (strlen(token) > sizeof(puuid->uuid) - 1) {
+                                       ret = -E2BIG;
+                                       goto err_free;
+                               }
+
                                strncpy(puuid->uuid, token,
                                        sizeof(puuid->uuid));
                         }
@@ -1137,47 +1615,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;
@@ -1226,19 +1704,21 @@ err_free:
                 pathend = argc;
         }
 
-        do {
-                ret = llapi_find(argv[pathstart], &param);
-        } while (++pathstart < pathend && !ret);
+       do {
+               rc = llapi_find(argv[pathstart], &param);
+               if (rc != 0 && ret == 0)
+                       ret = rc;
+       } while (++pathstart < pathend);
 
         if (ret)
                 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;
 }
@@ -1257,6 +1737,7 @@ static int lfs_getstripe_internal(int argc, char **argv,
                {"stripe_count",        no_argument,            0, 'c'},
                {"directory",           no_argument,            0, 'd'},
                {"default",             no_argument,            0, 'D'},
+               {"fid",                 no_argument,            0, 'F'},
                {"generation",          no_argument,            0, 'g'},
 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
                /* This formerly implied "stripe-index", but was explicitly
@@ -1267,8 +1748,13 @@ static int lfs_getstripe_internal(int argc, char **argv,
                {"stripe-index",        no_argument,            0, 'i'},
                {"stripe_index",        no_argument,            0, 'i'},
                {"layout",              no_argument,            0, 'L'},
+               {"mdt",                 no_argument,            0, 'm'},
+               {"mdt-index",           no_argument,            0, 'm'},
+               {"mdt_index",           no_argument,            0, 'm'},
+#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
                {"mdt-index",           no_argument,            0, 'M'},
                {"mdt_index",           no_argument,            0, 'M'},
+#endif
 #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
@@ -1294,34 +1780,38 @@ 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",
+       while ((c = getopt_long(argc, argv, "cdDFghiLmMoO: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 'F':
+                       if (!(param->fp_verbose & VERBOSE_DETAIL)) {
+                               param->fp_verbose |= VERBOSE_DFID;
+                               param->fp_max_depth = 0;
+                       }
                        break;
                case 'r':
-                       param->recursive = 1;
+                       param->fp_recursive = 1;
                        break;
                case 'v':
-                       param->verbose = VERBOSE_ALL | VERBOSE_DETAIL;
+                       param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
                        break;
                case 'c':
 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
@@ -1329,8 +1819,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;
@@ -1342,8 +1832,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;
@@ -1358,36 +1848,43 @@ 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;
+#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
                case 'M':
-                       if (!(param->verbose & VERBOSE_DETAIL))
+#if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
+                       fprintf(stderr, "warning: '-M' deprecated"
+                               ", use '-m' instead\n");
+#endif
+#endif
+               case 'm':
+                       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;
@@ -1397,13 +1894,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_DEFAULT;
+       if (param->fp_quiet)
+               param->fp_verbose = VERBOSE_OBJID;
 
        do {
                rc = llapi_getstripe(argv[optind], param);
@@ -1438,7 +1935,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) {
@@ -1456,6 +1953,8 @@ static int lfs_tgts(int argc, char **argv)
 static int lfs_getstripe(int argc, char **argv)
 {
        struct find_param param = { 0 };
+
+       param.fp_max_depth = 1;
        return lfs_getstripe_internal(argc, argv, &param);
 }
 
@@ -1464,7 +1963,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);
 }
 
@@ -1481,33 +1980,70 @@ static int lfs_setdirstripe(int argc, char **argv)
        char                    *stripe_offset_opt = NULL;
        char                    *stripe_count_opt = NULL;
        char                    *stripe_hash_opt = NULL;
-       int                     default_stripe = 0;
+       char                    *mode_opt = NULL;
+       bool                    default_stripe = false;
+       mode_t                  mode = S_IRWXU | S_IRWXG | S_IRWXO;
+       mode_t                  previous_mode = 0;
+       bool                    delete = false;
 
        struct option long_opts[] = {
+#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
                {"count",       required_argument, 0, 'c'},
+#endif
+               {"mdt-count",   required_argument, 0, 'c'},
+               {"delete",      no_argument, 0, 'd'},
+#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
                {"index",       required_argument, 0, 'i'},
+#endif
+               {"mdt-index",   required_argument, 0, 'i'},
+               {"mode",        required_argument, 0, 'm'},
+#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
                {"hash-type",   required_argument, 0, 't'},
-               {"default_stripe", required_argument, 0, 'D'},
+#endif
+               {"mdt-hash",    required_argument, 0, 't'},
+               {"default_stripe", no_argument, 0, 'D'},
                {0, 0, 0, 0}
        };
 
-       optind = 0;
-
-       while ((c = getopt_long(argc, argv, "c:Di:t:", long_opts, NULL)) >= 0) {
+       while ((c = getopt_long(argc, argv, "c:dDi:m:t:", long_opts,
+                               NULL)) >= 0) {
                switch (c) {
                case 0:
                        /* Long options. */
                        break;
                case 'c':
+#if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
+                       if (strcmp(argv[optind - 1], "--count") == 0)
+                               fprintf(stderr, "warning: '--count' deprecated"
+                                       ", use '--mdt-count' instead\n");
+#endif
                        stripe_count_opt = optarg;
                        break;
+               case 'd':
+                       delete = true;
+                       default_stripe = true;
+                       break;
                case 'D':
-                       default_stripe = 1;
+                       default_stripe = true;
                        break;
                case 'i':
+#if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
+                       if (strcmp(argv[optind - 1], "--index") == 0)
+                               fprintf(stderr, "warning: '--index' deprecated"
+                                       ", use '--mdt-index' instead\n");
+#endif
                        stripe_offset_opt = optarg;
                        break;
+               case 'm':
+                       mode_opt = optarg;
+                       break;
                case 't':
+#if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
+                       if (strcmp(argv[optind - 1], "--hash-type") == 0)
+                               fprintf(stderr, "warning: '--hash-type' "
+                                       "deprecated, use '--mdt-hash' "
+                                       "instead\n");
+#endif
                        stripe_hash_opt = optarg;
                        break;
                default:
@@ -1524,7 +2060,7 @@ static int lfs_setdirstripe(int argc, char **argv)
                return CMD_HELP;
        }
 
-       if (stripe_offset_opt == NULL && stripe_count_opt == NULL) {
+       if (!delete && stripe_offset_opt == NULL && stripe_count_opt == NULL) {
                fprintf(stderr, "error: %s: missing stripe offset and count.\n",
                        argv[0]);
                return CMD_HELP;
@@ -1540,6 +2076,27 @@ static int lfs_setdirstripe(int argc, char **argv)
                }
        }
 
+       if (delete) {
+               if (stripe_offset_opt != NULL || stripe_count_opt != NULL) {
+                       fprintf(stderr, "error: %s: cannot specify -d with -s,"
+                               " or -i options.\n", argv[0]);
+                       return CMD_HELP;
+               } else {
+                       stripe_count = 0;
+               }
+       }
+
+
+       if (mode_opt != NULL) {
+               mode = strtoul(mode_opt, &end, 8);
+               if (*end != '\0') {
+                       fprintf(stderr, "error: %s: bad mode '%s'\n",
+                               argv[0], mode_opt);
+                       return CMD_HELP;
+               }
+               previous_mode = umask(0);
+       }
+
        if (stripe_hash_opt == NULL ||
            strcmp(stripe_hash_opt, LMV_HASH_NAME_FNV_1A_64) == 0) {
                hash_type = LMV_HASH_TYPE_FNV_1A_64;
@@ -1563,12 +2120,13 @@ static int lfs_setdirstripe(int argc, char **argv)
 
        dname = argv[optind];
        do {
-               if (default_stripe == 1) {
+               if (default_stripe) {
                        result = llapi_dir_set_default_lmv_stripe(dname,
                                                    stripe_offset, stripe_count,
                                                    hash_type, NULL);
                } else {
-                       result = llapi_dir_create_pool(dname, 0, stripe_offset,
+                       result = llapi_dir_create_pool(dname, mode,
+                                                      stripe_offset,
                                                       stripe_count, hash_type,
                                                       NULL);
                }
@@ -1581,6 +2139,9 @@ static int lfs_setdirstripe(int argc, char **argv)
                dname = argv[++optind];
        } while (dname != NULL);
 
+       if (mode_opt != NULL)
+               umask(previous_mode);
+
        return result;
 }
 
@@ -1615,13 +2176,13 @@ 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;
        int     rc = 0;
        struct option long_opts[] = {
-               {"--mdt-index", required_argument, 0, 'M'},
+               {"mdt-index", required_argument, 0, 'M'},
                {"verbose",     no_argument,       0, 'v'},
                {0, 0, 0, 0}
        };
@@ -1629,7 +2190,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);
@@ -1638,7 +2199,7 @@ static int lfs_mv(int argc, char **argv)
                        break;
                }
                case 'v': {
-                       param.verbose = VERBOSE_DETAIL;
+                       param.fp_verbose = VERBOSE_DETAIL;
                        break;
                }
                default:
@@ -1648,21 +2209,22 @@ static int lfs_mv(int argc, char **argv)
                }
        }
 
-       if (param.mdtindex == -1) {
-               fprintf(stderr, "%s MDT index must be indicated\n", argv[0]);
+       if (param.fp_mdt_index == -1) {
+               fprintf(stderr, "%s: MDT index must be specified\n", argv[0]);
                return CMD_HELP;
        }
 
        if (optind >= argc) {
-               fprintf(stderr, "%s missing operand path\n", argv[0]);
+               fprintf(stderr, "%s: missing operand path\n", argv[0]);
                return CMD_HELP;
        }
 
-       param.migrate = 1;
-       rc = llapi_mv(argv[optind], &param);
+       param.fp_migrate = 1;
+       rc = llapi_migrate_mdt(argv[optind], &param);
        if (rc != 0)
-               fprintf(stderr, "cannot migrate '%s' to MDT%04x: %s\n",
-                       argv[optind], param.mdtindex, strerror(-rc));
+               fprintf(stderr, "%s: cannot migrate '%s' to MDT%04x: %s\n",
+                       argv[0], argv[optind], param.fp_mdt_index,
+                       strerror(-rc));
        return rc;
 }
 
@@ -1700,7 +2262,10 @@ static int showdf(char *mntdir, struct obd_statfs *stat,
         double ratio = 0;
         char *suffix = "KMGTPEZY";
         /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
-        char tbuf[20], ubuf[20], abuf[20], rbuf[20];
+       char tbuf[3 * sizeof(__u64)];
+       char ubuf[3 * sizeof(__u64)];
+       char abuf[3 * sizeof(__u64)];
+       char rbuf[3 * sizeof(__u64)];
 
         if (!uuid || !stat)
                 return -EINVAL;
@@ -1824,6 +2389,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)
@@ -1886,7 +2454,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':
@@ -1933,7 +2500,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;
 
@@ -1949,281 +2515,76 @@ static int lfs_getname(int argc, char **argv)
 
                         printf("%s %s\n", buf, mntdir);
 
-                        path[0] = fsname[0] = mntdir[0] = 0;
-                }
-        } else { /* paths specified, only attempt to search these. */
-                for (; optind < argc; optind++) {
-                        rc = llapi_getname(argv[optind], buf, sizeof(buf));
-                        if (rc < 0) {
-                                fprintf(stderr,
-                                        "cannot get name for `%s': %s\n",
-                                        argv[optind], strerror(-rc));
-                                break;
-                        }
-
-                        printf("%s %s\n", buf, argv[optind]);
-                }
-        }
-        return rc;
-}
-
-static int lfs_check(int argc, char **argv)
-{
-        int rc;
-        char mntdir[PATH_MAX] = {'\0'};
-        int num_types = 1;
-        char *obd_types[2];
-        char obd_type1[4];
-        char obd_type2[4];
-
-        if (argc != 2)
-                return CMD_HELP;
-
-        obd_types[0] = obd_type1;
-        obd_types[1] = obd_type2;
-
-        if (strcmp(argv[1], "osts") == 0) {
-                strcpy(obd_types[0], "osc");
-        } else if (strcmp(argv[1], "mds") == 0) {
-                strcpy(obd_types[0], "mdc");
-        } else if (strcmp(argv[1], "servers") == 0) {
-                num_types = 2;
-                strcpy(obd_types[0], "osc");
-                strcpy(obd_types[1], "mdc");
-        } else {
-                fprintf(stderr, "error: %s: option '%s' unrecognized\n",
-                                argv[0], argv[1]);
-                        return CMD_HELP;
-        }
-
-        rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
-        if (rc < 0 || mntdir[0] == '\0') {
-                fprintf(stderr, "No suitable Lustre mount found\n");
-                return rc;
-        }
-
-        rc = llapi_target_iterate(num_types, obd_types,
-                                  mntdir, llapi_ping_target);
-
-        if (rc)
-                fprintf(stderr, "error: %s: %s status failed\n",
-                                argv[0],argv[1]);
-
-        return rc;
-
-}
-
-static int lfs_join(int argc, char **argv)
-{
-        fprintf(stderr, "join two lustre files into one.\n"
-                        "obsolete, HEAD does not support it anymore.\n");
-        return 0;
-}
-
-#ifdef HAVE_SYS_QUOTA_H
-static int lfs_quotacheck(int argc, char **argv)
-{
-        int c, check_type = 0;
-        char *mnt;
-        struct if_quotacheck qchk;
-        struct if_quotactl qctl;
-        char *obd_type = (char *)qchk.obd_type;
-        int rc;
-
-        memset(&qchk, 0, sizeof(qchk));
-
-        optind = 0;
-        while ((c = getopt(argc, argv, "gu")) != -1) {
-                switch (c) {
-                case 'u':
-                        check_type |= 0x01;
-                        break;
-                case 'g':
-                        check_type |= 0x02;
-                        break;
-                default:
-                        fprintf(stderr, "error: %s: option '-%c' "
-                                        "unrecognized\n", argv[0], c);
-                        return CMD_HELP;
-                }
-        }
-
-        if (check_type)
-                check_type--;
-        else    /* do quotacheck for both user & group quota by default */
-                check_type = 0x02;
-
-        if (argc == optind)
-                return CMD_HELP;
-
-        mnt = argv[optind];
-
-        rc = llapi_quotacheck(mnt, check_type);
-       if (rc == -EOPNOTSUPP) {
-               fprintf(stderr, "error: quotacheck not supported by the quota "
-                       "master.\nPlease note that quotacheck is deprecated as "
-                       "of lustre 2.4.0 since space accounting is always "
-                       "enabled.\nFilesystems not formatted with 2.4 utils or "
-                       "beyond can be upgraded with tunefs.lustre --quota.\n");
-               return rc;
-       } else if (rc) {
-                fprintf(stderr, "quotacheck failed: %s\n", strerror(-rc));
-                return rc;
-        }
-
-        rc = llapi_poll_quotacheck(mnt, &qchk);
-        if (rc) {
-                if (*obd_type)
-                        fprintf(stderr, "%s %s ", obd_type,
-                                obd_uuid2str(&qchk.obd_uuid));
-                fprintf(stderr, "quota check failed: %s\n", strerror(-rc));
-                return rc;
-        }
-
-        memset(&qctl, 0, sizeof(qctl));
-        qctl.qc_cmd = LUSTRE_Q_QUOTAON;
-        qctl.qc_type = check_type;
-        rc = llapi_quotactl(mnt, &qctl);
-        if (rc && rc != -EALREADY) {
-                if (*obd_type)
-                        fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
-                                obd_uuid2str(&qctl.obd_uuid));
-                fprintf(stderr, "%s turn on quota failed: %s\n",
-                        argv[0], strerror(-rc));
-                return rc;
-        }
-
-        return 0;
-}
-
-static int lfs_quotaon(int argc, char **argv)
-{
-        int c;
-        char *mnt;
-        struct if_quotactl qctl;
-        char *obd_type = (char *)qctl.obd_type;
-        int rc;
-
-        memset(&qctl, 0, sizeof(qctl));
-        qctl.qc_cmd = LUSTRE_Q_QUOTAON;
-
-        optind = 0;
-        while ((c = getopt(argc, argv, "fgu")) != -1) {
-                switch (c) {
-                case 'u':
-                        qctl.qc_type |= 0x01;
-                        break;
-                case 'g':
-                        qctl.qc_type |= 0x02;
-                        break;
-                case 'f':
-                        qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
-                        break;
-                default:
-                        fprintf(stderr, "error: %s: option '-%c' "
-                                        "unrecognized\n", argv[0], c);
-                        return CMD_HELP;
-                }
-        }
-
-        if (qctl.qc_type)
-                qctl.qc_type--;
-        else /* by default, enable quota for both user & group */
-                qctl.qc_type = 0x02;
-
-        if (argc == optind)
-                return CMD_HELP;
-
-        mnt = argv[optind];
+                        path[0] = fsname[0] = mntdir[0] = 0;
+                }
+        } else { /* paths specified, only attempt to search these. */
+                for (; optind < argc; optind++) {
+                        rc = llapi_getname(argv[optind], buf, sizeof(buf));
+                        if (rc < 0) {
+                                fprintf(stderr,
+                                        "cannot get name for `%s': %s\n",
+                                        argv[optind], strerror(-rc));
+                                break;
+                        }
 
-        rc = llapi_quotactl(mnt, &qctl);
-        if (rc) {
-                if (rc == -EOPNOTSUPP) {
-                        fprintf(stderr, "error: quotaon not supported by the "
-                                "quota master.\nPlease note that quotaon/off is"
-                                " deprecated as of lustre 2.4.0.\nQuota "
-                                "enforcement should now be enabled on the MGS "
-                                "via:\nmgs# lctl conf_param ${FSNAME}.quota."
-                                "<ost|mdt>=<u|g|ug>\n(ost for block quota, mdt "
-                                "for inode quota, u for user and g for group"
-                                "\n");
-                } else if (rc == -EALREADY) {
-                        rc = 0;
-                } else if (rc == -ENOENT) {
-                        fprintf(stderr, "error: cannot find quota database, "
-                                        "make sure you have run quotacheck\n");
-                } else {
-                        if (*obd_type)
-                                fprintf(stderr, "%s %s ", obd_type,
-                                        obd_uuid2str(&qctl.obd_uuid));
-                        fprintf(stderr, "%s failed: %s\n", argv[0],
-                                strerror(-rc));
+                        printf("%s %s\n", buf, argv[optind]);
                 }
         }
-
         return rc;
 }
 
-static int lfs_quotaoff(int argc, char **argv)
+static int lfs_check(int argc, char **argv)
 {
-        int c;
-        char *mnt;
-        struct if_quotactl qctl;
-        char *obd_type = (char *)qctl.obd_type;
         int rc;
+        char mntdir[PATH_MAX] = {'\0'};
+        int num_types = 1;
+        char *obd_types[2];
+        char obd_type1[4];
+        char obd_type2[4];
 
-        memset(&qctl, 0, sizeof(qctl));
-        qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
+        if (argc != 2)
+                return CMD_HELP;
 
-        optind = 0;
-        while ((c = getopt(argc, argv, "gu")) != -1) {
-                switch (c) {
-                case 'u':
-                        qctl.qc_type |= 0x01;
-                        break;
-                case 'g':
-                        qctl.qc_type |= 0x02;
-                        break;
-                default:
-                        fprintf(stderr, "error: %s: option '-%c' "
-                                        "unrecognized\n", argv[0], c);
+        obd_types[0] = obd_type1;
+        obd_types[1] = obd_type2;
+
+        if (strcmp(argv[1], "osts") == 0) {
+                strcpy(obd_types[0], "osc");
+        } else if (strcmp(argv[1], "mds") == 0) {
+                strcpy(obd_types[0], "mdc");
+        } else if (strcmp(argv[1], "servers") == 0) {
+                num_types = 2;
+                strcpy(obd_types[0], "osc");
+                strcpy(obd_types[1], "mdc");
+        } else {
+                fprintf(stderr, "error: %s: option '%s' unrecognized\n",
+                                argv[0], argv[1]);
                         return CMD_HELP;
-                }
         }
 
-        if (qctl.qc_type)
-                qctl.qc_type--;
-        else /* by default, disable quota for both user & group */
-                qctl.qc_type = 0x02;
+        rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
+        if (rc < 0 || mntdir[0] == '\0') {
+                fprintf(stderr, "No suitable Lustre mount found\n");
+                return rc;
+        }
 
-        if (argc == optind)
-                return CMD_HELP;
+       rc = llapi_target_check(num_types, obd_types, mntdir);
+        if (rc)
+                fprintf(stderr, "error: %s: %s status failed\n",
+                                argv[0],argv[1]);
 
-        mnt = argv[optind];
+        return rc;
 
-        rc = llapi_quotactl(mnt, &qctl);
-        if (rc) {
-                if (rc == -EOPNOTSUPP) {
-                        fprintf(stderr, "error: quotaoff not supported by the "
-                                "quota master.\nPlease note that quotaon/off is"
-                                " deprecated as of lustre 2.4.0.\nQuota "
-                                "enforcement can be disabled on the MGS via:\n"
-                                "mgs# lctl conf_param ${FSNAME}.quota.<ost|mdt>"
-                                "=\"\"\n");
-                } else if (rc == -EALREADY) {
-                        rc = 0;
-                } else {
-                        if (*obd_type)
-                                fprintf(stderr, "%s %s ", obd_type,
-                                        obd_uuid2str(&qctl.obd_uuid));
-                        fprintf(stderr, "quotaoff failed: %s\n",
-                                strerror(-rc));
-                }
-        }
+}
 
-        return rc;
+static int lfs_join(int argc, char **argv)
+{
+        fprintf(stderr, "join two lustre files into one.\n"
+                        "obsolete, HEAD does not support it anymore.\n");
+        return 0;
 }
 
+#ifdef HAVE_SYS_QUOTA_H
 #define ARG2INT(nr, str, msg)                                           \
 do {                                                                    \
         char *endp;                                                     \
@@ -2337,7 +2698,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':
@@ -2427,7 +2787,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':
@@ -2641,22 +3000,29 @@ static void print_quota_title(char *name, struct if_quotactl *qctl,
               "files", "quota", "limit", "grace");
 }
 
-static void kbytes2str(__u64 num, char *buf, bool h)
+static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
 {
        if (!h) {
-               sprintf(buf, LPU64, num);
+               snprintf(buf, buflen, "%ju", (uintmax_t)num);
        } else {
-               if (num >> 30)
-                       sprintf(buf, "%5.4gT", (double)num / (1 << 30));
+               if (num >> 40)
+                       snprintf(buf, buflen, "%5.4gP",
+                                (double)num / ((__u64)1 << 40));
+               else if (num >> 30)
+                       snprintf(buf, buflen, "%5.4gT",
+                                (double)num / (1 << 30));
                else if (num >> 20)
-                       sprintf(buf, "%5.4gG", (double)num / (1 << 20));
+                       snprintf(buf, buflen, "%5.4gG",
+                                (double)num / (1 << 20));
                else if (num >> 10)
-                       sprintf(buf, "%5.4gM", (double)num / (1 << 10));
+                       snprintf(buf, buflen, "%5.4gM",
+                                (double)num / (1 << 10));
                else
-                       sprintf(buf, LPU64"%s", num, "k");
+                       snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
        }
 }
 
+#define STRBUF_LEN     32
 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
                        int rc, bool h)
 {
@@ -2667,9 +3033,9 @@ static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
                int bover = 0, iover = 0;
                struct obd_dqblk *dqb = &qctl->qc_dqblk;
-               char numbuf[3][32];
+               char numbuf[3][STRBUF_LEN];
                char timebuf[40];
-               char strbuf[32];
+               char strbuf[STRBUF_LEN];
 
                 if (dqb->dqb_bhardlimit &&
                    lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
@@ -2702,21 +3068,22 @@ static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
                if (bover)
                        diff2str(dqb->dqb_btime, timebuf, now);
 
-               kbytes2str(lustre_stoqb(dqb->dqb_curspace), strbuf, h);
+               kbytes2str(lustre_stoqb(dqb->dqb_curspace),
+                          strbuf, sizeof(strbuf), h);
                if (rc == -EREMOTEIO)
                        sprintf(numbuf[0], "%s*", strbuf);
                else
                        sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
                                "%s" : "[%s]", strbuf);
 
-               kbytes2str(dqb->dqb_bsoftlimit, strbuf, h);
+               kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
                if (type == QC_GENERAL)
                        sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
                                "%s" : "[%s]", strbuf);
                else
                        sprintf(numbuf[1], "%s", "-");
 
-               kbytes2str(dqb->dqb_bhardlimit, strbuf, h);
+               kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
                sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
                        "%s" : "[%s]", strbuf);
 
@@ -2728,16 +3095,17 @@ static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
                        diff2str(dqb->dqb_itime, timebuf, now);
 
                sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
-                       LPU64 : "["LPU64"]", dqb->dqb_curinodes);
+                       "%ju" : "[%ju]", (uintmax_t)dqb->dqb_curinodes);
 
                if (type == QC_GENERAL)
                        sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
-                               LPU64 : "["LPU64"]", dqb->dqb_isoftlimit);
+                               "%ju" : "[%ju]",
+                               (uintmax_t)dqb->dqb_isoftlimit);
                else
                        sprintf(numbuf[1], "%s", "-");
 
                sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
-                       LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
+                       "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
 
                if (type != QC_OSTIDX)
                        printf(" %7s%c %6s %7s %7s",
@@ -2814,7 +3182,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':
@@ -2836,7 +3203,7 @@ static int lfs_quota(int argc, char **argv)
                         break;
                 case 'o':
                         valid = qctl.qc_valid = QC_UUID;
-                        strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
+                       strlcpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
                         break;
                 case 'i':
                         valid = qctl.qc_valid = QC_MDTIDX;
@@ -2908,22 +3275,23 @@ ug_output:
         mnt = argv[optind];
 
         rc1 = llapi_quotactl(mnt, &qctl);
-        if (rc1 < 0) {
-                switch (rc1) {
-                case -ESRCH:
-                        fprintf(stderr, "%s quotas are not enabled.\n",
-                                qctl.qc_type == USRQUOTA ? "user" : "group");
-                        goto out;
-                case -EPERM:
-                        fprintf(stderr, "Permission denied.\n");
-                case -ENOENT:
-                        /* We already got a "No such file..." message. */
-                        goto out;
-                default:
-                        fprintf(stderr, "Unexpected quotactl error: %s\n",
-                                strerror(-rc1));
-                }
-        }
+       if (rc1 < 0) {
+               switch (rc1) {
+               case -ESRCH:
+                       fprintf(stderr, "%s quotas are not enabled.\n",
+                               qctl.qc_type == USRQUOTA ? "user" : "group");
+                       goto out;
+               case -EPERM:
+                       fprintf(stderr, "Permission denied.\n");
+               case -ENODEV:
+               case -ENOENT:
+                       /* We already got error message. */
+                       goto out;
+               default:
+                       fprintf(stderr, "Unexpected quotactl error: %s\n",
+                               strerror(-rc1));
+               }
+       }
 
        if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && !quiet)
                print_quota_title(name, &qctl, human_readable);
@@ -2942,15 +3310,17 @@ ug_output:
 
        if (qctl.qc_valid == QC_GENERAL && qctl.qc_cmd != LUSTRE_Q_GETINFO &&
            verbose) {
-               char strbuf[32];
+               char strbuf[STRBUF_LEN];
 
                rc2 = print_obd_quota(mnt, &qctl, 1, human_readable,
                                      &total_ialloc);
                rc3 = print_obd_quota(mnt, &qctl, 0, human_readable,
                                      &total_balloc);
-               kbytes2str(total_balloc, strbuf, human_readable);
-               printf("Total allocated inode limit: "LPU64", total "
-                      "allocated block limit: %s\n", total_ialloc, strbuf);
+               kbytes2str(total_balloc, strbuf, sizeof(strbuf),
+                          human_readable);
+               printf("Total allocated inode limit: %ju, total "
+                      "allocated block limit: %s\n", (uintmax_t)total_ialloc,
+                      strbuf);
        }
 
         if (rc1 || rc2 || rc3 || inacc)
@@ -2988,12 +3358,11 @@ static int flushctx_ioctl(char *mp)
 
 static int lfs_flushctx(int argc, char **argv)
 {
-        int     kdestroy = 0, c;
-       FILE   *proc = NULL;
-        char    procline[PATH_MAX], *line;
-        int     rc = 0;
+       int     kdestroy = 0, c;
+       char    mntdir[PATH_MAX] = {'\0'};
+       int     index = 0;
+       int     rc = 0;
 
-        optind = 0;
         while ((c = getopt(argc, argv, "k")) != -1) {
                 switch (c) {
                 case 'k':
@@ -3007,46 +3376,24 @@ static int lfs_flushctx(int argc, char **argv)
         }
 
         if (kdestroy) {
-            int rc;
             if ((rc = system("kdestroy > /dev/null")) != 0) {
                 rc = WEXITSTATUS(rc);
                 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
             }
         }
 
-        if (optind >= argc) {
-                /* flush for all mounted lustre fs. */
-                proc = fopen("/proc/mounts", "r");
-                if (!proc) {
-                        fprintf(stderr, "error: %s: can't open /proc/mounts\n",
-                                argv[0]);
-                        return -1;
-                }
-
-                while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
-                        char dev[PATH_MAX];
-                        char mp[PATH_MAX];
-                        char fs[PATH_MAX];
+       if (optind >= argc) {
+               /* flush for all mounted lustre fs. */
+               while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
+                       /* Check if we have a mount point */
+                       if (mntdir[0] == '\0')
+                               continue;
 
-                        if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
-                                fprintf(stderr, "%s: unexpected format in "
-                                                "/proc/mounts\n",
-                                        argv[0]);
+                       if (flushctx_ioctl(mntdir))
                                rc = -1;
-                               goto out;
-                        }
-
-                        if (strcmp(fs, "lustre") != 0)
-                                continue;
-                        /* we use '@' to determine it's a client. are there
-                         * any other better way?
-                         */
-                        if (strchr(dev, '@') == NULL)
-                                continue;
 
-                        if (flushctx_ioctl(mp))
-                                rc = -1;
-                }
+                       mntdir[0] = '\0'; /* avoid matching in next loop */
+               }
         } else {
                 /* flush fs as specified */
                 while (optind < argc) {
@@ -3054,51 +3401,55 @@ static int lfs_flushctx(int argc, char **argv)
                                 rc = -1;
                 }
         }
-
-out:
-       if (proc != NULL)
-               fclose(proc);
         return rc;
 }
 
 static int lfs_lsetfacl(int argc, char **argv)
 {
-        argv[0]++;
-        return(llapi_lsetfacl(argc, argv));
+       fprintf(stderr, "local client sets facl for remote client.\n"
+               "obsolete, does not support it anymore.\n");
+       return 0;
 }
 
 static int lfs_lgetfacl(int argc, char **argv)
 {
-        argv[0]++;
-        return(llapi_lgetfacl(argc, argv));
+       fprintf(stderr, "local client gets facl for remote client.\n"
+               "obsolete, does not support it anymore.\n");
+       return 0;
 }
 
 static int lfs_rsetfacl(int argc, char **argv)
 {
-        argv[0]++;
-        return(llapi_rsetfacl(argc, argv));
+       fprintf(stderr, "remote client sets facl for remote client.\n"
+               "obsolete, does not support it anymore.\n");
+       return 0;
 }
 
 static int lfs_rgetfacl(int argc, char **argv)
 {
-        argv[0]++;
-        return(llapi_rgetfacl(argc, argv));
+       fprintf(stderr, "remote client gets facl for remote client.\n"
+               "obsolete, does not support it anymore.\n");
+       return 0;
 }
 
 static int lfs_cp(int argc, char **argv)
 {
-        return(llapi_cp(argc, argv));
+       fprintf(stderr, "remote client copy file(s).\n"
+               "obsolete, does not support it anymore.\n");
+       return 0;
 }
 
 static int lfs_ls(int argc, char **argv)
 {
-        return(llapi_ls(argc, argv));
+       fprintf(stderr, "remote client lists directory contents.\n"
+               "obsolete, does not support it anymore.\n");
+       return 0;
 }
 
 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[] = {
@@ -3108,7 +3459,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) {
@@ -3132,15 +3482,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;
@@ -3155,28 +3506,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("%ju %02d%-5s %02d:%02d:%02d.%06d %04d.%02d.%02d "
+                      "0x%x t="DFID, (uintmax_t) 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);
@@ -3220,8 +3585,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) {
@@ -3248,6 +3611,10 @@ static int lfs_fid2path(int argc, char **argv)
 
        device = argv[optind++];
        path = calloc(1, PATH_MAX);
+       if (path == NULL) {
+               fprintf(stderr, "error: Not enough memory\n");
+               return -errno;
+       }
 
        rc = 0;
        while (optind < argc) {
@@ -3294,33 +3661,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;
@@ -3338,7 +3746,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':
@@ -3366,7 +3773,7 @@ static int lfs_data_version(int argc, char **argv)
        if (rc < 0)
                err(errno, "cannot get version for %s", path);
        else
-               printf(LPU64 "\n", data_version);
+               printf("%ju" "\n", (uintmax_t)data_version);
 
        close(fd);
        return rc;
@@ -3448,7 +3855,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) {
@@ -3542,12 +3948,15 @@ static int lfs_hsm_action(int argc, char **argv)
 
                if ((hps == HPS_RUNNING) &&
                    (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
-                       printf("("LPX64 " bytes moved)\n", he.length);
+                       printf("(%llu bytes moved)\n",
+                              (unsigned long long)he.length);
                else if ((he.offset + he.length) == LUSTRE_EOF)
-                       printf("(from "LPX64 " to EOF)\n", he.offset);
+                       printf("(from %llu to EOF)\n",
+                              (unsigned long long)he.offset);
                else
-                       printf("(from "LPX64 " to "LPX64")\n",
-                              he.offset, he.offset + he.length);
+                       printf("(from %llu to %llu)\n",
+                              (unsigned long long)he.offset,
+                              (unsigned long long)(he.offset + he.length));
 
        } while (++i < argc);
 
@@ -3573,7 +3982,7 @@ static int lfs_hsm_clear(int argc, char **argv)
  *
  * \return 0 on success.
  */
-static int lfs_hsm_prepare_file(char *file, struct lu_fid *fid,
+static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
                                dev_t *last_dev)
 {
        struct stat     st;
@@ -3584,6 +3993,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 "
@@ -3601,16 +4017,62 @@ static int lfs_hsm_prepare_file(char *file, struct lu_fid *fid,
        return 0;
 }
 
+/* Fill an HSM HUR item with a given file name.
+ *
+ * If mntpath is set, then the filename is actually a FID, and no
+ * lookup on the filesystem will be performed.
+ *
+ * \param[in]  hur         the user request to fill
+ * \param[in]  idx         index of the item inside the HUR to fill
+ * \param[in]  mntpath     mountpoint of Lustre
+ * \param[in]  fname       filename (if mtnpath is NULL)
+ *                         or FID (if mntpath is set)
+ * \param[in]  last_dev    pointer to last device id used
+ *
+ * \retval 0 on success
+ * \retval CMD_HELP or a negative errno on error
+ */
+static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
+                        const char *mntpath, const char *fname,
+                        dev_t *last_dev)
+{
+       struct hsm_user_item *hui = &hur->hur_user_item[idx];
+       int rc;
+
+       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 {
+                       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);
+       }
+
+       if (rc == 0)
+               hur->hur_request.hr_itemcount++;
+
+       return rc;
+}
+
 static int lfs_hsm_request(int argc, char **argv, int action)
 {
        struct option            long_opts[] = {
                {"filelist", 1, 0, 'l'},
                {"data", 1, 0, 'D'},
                {"archive", 1, 0, 'a'},
+               {"mntpath", 1, 0, 'm'},
                {0, 0, 0, 0}
        };
        dev_t                    last_dev = 0;
-       char                     short_opts[] = "l:D:a:";
+       char                     short_opts[] = "l:D:a:m:";
        struct hsm_user_request *hur, *oldhur;
        int                      c, i;
        size_t                   len;
@@ -3623,13 +4085,13 @@ static int lfs_hsm_request(int argc, char **argv, int action)
        int                      archive_id = 0;
        FILE                    *fp;
        int                      nbfile_alloc = 0;
-       char                     some_file[PATH_MAX+1] = "";
+       char                    *some_file = NULL;
+       char                    *mntpath = NULL;
        int                      rc;
 
        if (argc < 2)
                return CMD_HELP;
 
-       optind = 0;
        while ((c = getopt_long(argc, argv, short_opts,
                                long_opts, NULL)) != -1) {
                switch (c) {
@@ -3640,14 +4102,21 @@ static int lfs_hsm_request(int argc, char **argv, int action)
                        opaque = optarg;
                        break;
                case 'a':
-                       if (action != HUA_ARCHIVE) {
+                       if (action != HUA_ARCHIVE &&
+                           action != HUA_REMOVE) {
                                fprintf(stderr,
                                        "error: -a is supported only "
-                                       "when archiving\n");
+                                       "when archiving or removing\n");
                                return CMD_HELP;
                        }
                        archive_id = atoi(optarg);
                        break;
+               case 'm':
+                       if (some_file == NULL) {
+                               mntpath = optarg;
+                               some_file = strdup(optarg);
+                       }
+                       break;
                case '?':
                        return CMD_HELP;
                default:
@@ -3681,20 +4150,12 @@ static int lfs_hsm_request(int argc, char **argv, int action)
        hur->hur_request.hr_flags = 0;
 
        /* All remaining args are files, add them */
-       if (nbfile != 0) {
-               if (strlen(argv[optind]) > sizeof(some_file)-1) {
-                       free(hur);
-                       return -E2BIG;
-               }
-               strncpy(some_file, argv[optind], sizeof(some_file));
-       }
+       if (nbfile != 0 && some_file == NULL)
+               some_file = strdup(argv[optind]);
 
        for (i = 0; i < nbfile; i++) {
-               hur->hur_user_item[i].hui_extent.length = -1;
-               rc = lfs_hsm_prepare_file(argv[optind + i],
-                                         &hur->hur_user_item[i].hui_fid,
-                                         &last_dev);
-               hur->hur_request.hr_itemcount++;
+               rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
+                                  &last_dev);
                if (rc)
                        goto out_free;
        }
@@ -3712,17 +4173,17 @@ static int lfs_hsm_request(int argc, char **argv, int action)
                }
 
                while ((rc = getline(&line, &len, fp)) != -1) {
-                       struct hsm_user_item *hui;
-
-                       /* If allocated buffer was too small, gets something
-                        * bigger */
+                       /* If allocated buffer was too small, get something
+                        * larger */
                        if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
+                               ssize_t size;
+
                                nbfile_alloc = nbfile_alloc * 2 + 1;
                                oldhur = hur;
                                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;
@@ -3730,7 +4191,19 @@ static int lfs_hsm_request(int argc, char **argv, int action)
                                        fclose(fp);
                                        goto out_free;
                                }
-                               memcpy(hur, oldhur, hur_len(oldhur));
+                               size = hur_len(oldhur);
+                               if (size < 0) {
+                                       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);
+                                       goto out_free;
+                               }
+                               memcpy(hur, oldhur, size);
                                free(oldhur);
                        }
 
@@ -3738,25 +4211,21 @@ static int lfs_hsm_request(int argc, char **argv, int action)
                        if (line[strlen(line) - 1] == '\n')
                                line[strlen(line) - 1] = '\0';
 
-                       hui =
-                            &hur->hur_user_item[hur->hur_request.hr_itemcount];
-                       hui->hui_extent.length = -1;
-                       rc = lfs_hsm_prepare_file(line, &hui->hui_fid,
-                                                 &last_dev);
-                       hur->hur_request.hr_itemcount++;
+                       rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
+                                          mntpath, line, &last_dev);
                        if (rc) {
                                fclose(fp);
                                goto out_free;
                        }
 
-                       if ((some_file[0] == '\0') &&
-                           (strlen(line) < sizeof(some_file)))
-                               strcpy(some_file, line);
+                       if (some_file == NULL) {
+                               some_file = line;
+                               line = NULL;
+                       }
                }
 
                rc = fclose(fp);
-               if (line)
-                       free(line);
+               free(line);
        }
 
        /* If a --data was used, add it to the request */
@@ -3777,6 +4246,7 @@ static int lfs_hsm_request(int argc, char **argv, int action)
        }
 
 out_free:
+       free(some_file);
        free(hur);
        return rc;
 }
@@ -3816,25 +4286,200 @@ static int lfs_swap_layouts(int argc, char **argv)
                                  SWAP_LAYOUTS_KEEP_ATIME);
 }
 
+static const char *const ladvise_names[] = LU_LADVISE_NAMES;
+
+static enum lu_ladvise_type lfs_get_ladvice(const char *string)
+{
+       enum lu_ladvise_type advice;
+
+       for (advice = 0;
+            advice < ARRAY_SIZE(ladvise_names); advice++) {
+               if (ladvise_names[advice] == NULL)
+                       continue;
+               if (strcmp(string, ladvise_names[advice]) == 0)
+                       return advice;
+       }
+
+       return LU_LADVISE_INVALID;
+}
+
+static int lfs_ladvise(int argc, char **argv)
+{
+       struct option            long_opts[] = {
+               {"advice",      required_argument,      0, 'a'},
+               {"background",  no_argument,            0, 'b'},
+               {"end",         required_argument,      0, 'e'},
+               {"start",       required_argument,      0, 's'},
+               {"length",      required_argument,      0, 'l'},
+               {0, 0, 0, 0}
+       };
+       char                     short_opts[] = "a:be:l:s:";
+       int                      c;
+       int                      rc = 0;
+       const char              *path;
+       int                      fd;
+       struct llapi_lu_ladvise  advice;
+       enum lu_ladvise_type     advice_type = LU_LADVISE_INVALID;
+       unsigned long long       start = 0;
+       unsigned long long       end = LUSTRE_EOF;
+       unsigned long long       length = 0;
+       unsigned long long       size_units;
+       unsigned long long       flags = 0;
+
+       optind = 0;
+       while ((c = getopt_long(argc, argv, short_opts,
+                               long_opts, NULL)) != -1) {
+               switch (c) {
+               case 'a':
+                       advice_type = lfs_get_ladvice(optarg);
+                       if (advice_type == LU_LADVISE_INVALID) {
+                               fprintf(stderr, "%s: invalid advice type "
+                                       "'%s'\n", argv[0], optarg);
+                               fprintf(stderr, "Valid types:");
+
+                               for (advice_type = 0;
+                                    advice_type < ARRAY_SIZE(ladvise_names);
+                                    advice_type++) {
+                                       if (ladvise_names[advice_type] == NULL)
+                                               continue;
+                                       fprintf(stderr, " %s",
+                                               ladvise_names[advice_type]);
+                               }
+                               fprintf(stderr, "\n");
+
+                               return CMD_HELP;
+                       }
+                       break;
+               case 'b':
+                       flags |= LF_ASYNC;
+                       break;
+               case 'e':
+                       size_units = 1;
+                       rc = llapi_parse_size(optarg, &end,
+                                             &size_units, 0);
+                       if (rc) {
+                               fprintf(stderr, "%s: bad end offset '%s'\n",
+                                       argv[0], optarg);
+                               return CMD_HELP;
+                       }
+                       break;
+               case 's':
+                       size_units = 1;
+                       rc = llapi_parse_size(optarg, &start,
+                                             &size_units, 0);
+                       if (rc) {
+                               fprintf(stderr, "%s: bad start offset "
+                                       "'%s'\n", argv[0], optarg);
+                               return CMD_HELP;
+                       }
+                       break;
+               case 'l':
+                       size_units = 1;
+                       rc = llapi_parse_size(optarg, &length,
+                                             &size_units, 0);
+                       if (rc) {
+                               fprintf(stderr, "%s: bad length '%s'\n",
+                                       argv[0], optarg);
+                               return CMD_HELP;
+                       }
+                       break;
+               case '?':
+                       return CMD_HELP;
+               default:
+                       fprintf(stderr, "%s: option '%s' unrecognized\n",
+                               argv[0], argv[optind - 1]);
+                       return CMD_HELP;
+               }
+       }
+
+       if (advice_type == LU_LADVISE_INVALID) {
+               fprintf(stderr, "%s: please give an advice type\n", argv[0]);
+               fprintf(stderr, "Valid types:");
+               for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
+                    advice_type++) {
+                       if (ladvise_names[advice_type] == NULL)
+                               continue;
+                       fprintf(stderr, " %s", ladvise_names[advice_type]);
+               }
+               fprintf(stderr, "\n");
+               return CMD_HELP;
+       }
+
+       if (argc <= optind) {
+               fprintf(stderr, "%s: please give one or more file names\n",
+                       argv[0]);
+               return CMD_HELP;
+       }
+
+       if (end != LUSTRE_EOF && length != 0 && end != start + length) {
+               fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
+                       argv[0]);
+               return CMD_HELP;
+       }
+
+       if (end == LUSTRE_EOF && length != 0)
+               end = start + length;
+
+       if (end <= start) {
+               fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
+                       argv[0], start, end);
+               return CMD_HELP;
+       }
+
+       while (optind < argc) {
+               int rc2;
+
+               path = argv[optind++];
+
+               fd = open(path, O_RDONLY);
+               if (fd < 0) {
+                       fprintf(stderr, "%s: cannot open file '%s': %s\n",
+                               argv[0], path, strerror(errno));
+                       rc2 = -errno;
+                       goto next;
+               }
+
+               advice.lla_start = start;
+               advice.lla_end = end;
+               advice.lla_advice = advice_type;
+               advice.lla_value1 = 0;
+               advice.lla_value2 = 0;
+               advice.lla_value3 = 0;
+               advice.lla_value4 = 0;
+               rc2 = llapi_ladvise(fd, flags, 1, &advice);
+               close(fd);
+               if (rc2 < 0) {
+                       fprintf(stderr, "%s: cannot give advice '%s' to file "
+                               "'%s': %s\n", argv[0],
+                               ladvise_names[advice_type],
+                               path, strerror(errno));
+               }
+next:
+               if (rc == 0 && rc2 < 0)
+                       rc = rc2;
+       }
+       return rc;
+}
+
 int main(int argc, char **argv)
 {
         int rc;
 
-        setlinebuf(stdout);
+       /* Ensure that liblustreapi constructor has run */
+       if (!liblustreapi_initialized)
+               fprintf(stderr, "liblustreapi was not properly initialized\n");
 
-        ptl_initialize(argc, argv);
-        if (obd_initialize(argc, argv) < 0)
-                exit(2);
+        setlinebuf(stdout);
 
-        Parser_init("lfs > ", cmdlist);
+       Parser_init("lfs > ", cmdlist);
 
+       progname = argv[0]; /* Used in error messages */
         if (argc > 1) {
                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
         } else {
                 rc = Parser_commands();
         }
 
-        obd_finalize(argc, argv);
         return rc < 0 ? -rc : rc;
 }