Whamcloud - gitweb
LU-8724 utils: Use open() instead of opendir() with dirfd()
[fs/lustre-release.git] / lustre / utils / liblustreapi.c
index bac6a68..44747e6 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, 2015, Intel Corporation.
+ * Copyright (c) 2011, 2016, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 #include <stdarg.h>
 #include <sys/stat.h>
 #include <sys/statfs.h>
-#include <sys/types.h>
 #include <sys/syscall.h>
+#include <sys/time.h>
+#include <sys/types.h>
 #include <sys/xattr.h>
+#include <time.h>
 #include <fnmatch.h>
 #include <libgen.h> /* for dirname() */
 #ifdef HAVE_LINUX_UNISTD_H
@@ -611,8 +609,8 @@ retry_open:
                        errmsg = strerror(errno);
 
                llapi_err_noerrno(LLAPI_MSG_ERROR,
-                                 "error on ioctl "LPX64" for '%s' (%d): %s",
-                                 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,
+                                 "error on ioctl %#jx for '%s' (%d): %s",
+                                 (uintmax_t)LL_IOC_LOV_SETSTRIPE, name, fd,
                                  errmsg);
 
                close(fd);
@@ -827,8 +825,8 @@ int llapi_direntry_remove(char *dname)
        if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
                char *errmsg = strerror(errno);
                llapi_err_noerrno(LLAPI_MSG_ERROR,
-                                 "error on ioctl "LPX64" for '%s' (%d): %s",
-                                 (__u64)LL_IOC_LMV_SETSTRIPE, filename,
+                                 "error on ioctl %#jx for '%s' (%d): %s",
+                                 (uintmax_t)LL_IOC_LMV_SETSTRIPE, filename,
                                  fd, errmsg);
        }
 out:
@@ -845,21 +843,21 @@ out:
  */
 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
 {
-        struct mntent mnt;
-        char buf[PATH_MAX], mntdir[PATH_MAX];
-        char *ptr;
-        FILE *fp;
-        int idx = 0, len = 0, mntlen, fd;
-        int rc = -ENODEV;
+       struct mntent mnt;
+       char buf[PATH_MAX], mntdir[PATH_MAX];
+       char *ptr, *ptr_end;
+       FILE *fp;
+       int idx = 0, len = 0, mntlen, fd;
+       int rc = -ENODEV;
 
         /* get the mount point */
-        fp = setmntent(MOUNTED, "r");
-        if (fp == NULL) {
-                rc = -EIO;
-                llapi_error(LLAPI_MSG_ERROR, rc,
-                            "setmntent(%s) failed", MOUNTED);
-                return rc;
-        }
+       fp = setmntent(PROC_MOUNTS, "r");
+       if (fp == NULL) {
+               rc = -EIO;
+               llapi_error(LLAPI_MSG_ERROR, rc,
+                            "setmntent(%s) failed", PROC_MOUNTS);
+               return rc;
+       }
         while (1) {
                 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
                         break;
@@ -871,58 +869,71 @@ int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
                         continue;
 
                 mntlen = strlen(mnt.mnt_dir);
-                ptr = strrchr(mnt.mnt_fsname, '/');
+               ptr = strchr(mnt.mnt_fsname, '/');
+               while (ptr && *ptr == '/')
+                       ptr++;
                /* thanks to the call to llapi_is_lustre_mnt() above,
                 * we are sure that mnt.mnt_fsname contains ":/",
                 * so ptr should never be NULL */
                if (ptr == NULL)
                        continue;
-                ptr++;
+               ptr_end = ptr;
+               while (*ptr_end != '/' && *ptr_end != '\0')
+                       ptr_end++;
 
-                /* Check the fsname for a match, if given */
+               /* Check the fsname for a match, if given */
                 if (!(want & WANT_FSNAME) && fsname != NULL &&
-                    (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
+                   (strlen(fsname) > 0) &&
+                   (strncmp(ptr, fsname, ptr_end - ptr) != 0))
                         continue;
 
                 /* If the path isn't set return the first one we find */
-                if (path == NULL || strlen(path) == 0) {
-                        strcpy(mntdir, mnt.mnt_dir);
-                        if ((want & WANT_FSNAME) && fsname != NULL)
-                                strcpy(fsname, ptr);
-                        rc = 0;
-                        break;
-                /* Otherwise find the longest matching path */
-                } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
-                           (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
-                        strcpy(mntdir, mnt.mnt_dir);
-                        len = mntlen;
-                        if ((want & WANT_FSNAME) && fsname != NULL)
-                                strcpy(fsname, ptr);
-                        rc = 0;
-                }
-        }
-        endmntent(fp);
-
-        /* Found it */
-        if (rc == 0) {
-                if ((want & WANT_PATH) && path != NULL)
-                        strcpy(path, mntdir);
-                if (want & WANT_FD) {
-                        fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
-                        if (fd < 0) {
-                                rc = -errno;
-                                llapi_error(LLAPI_MSG_ERROR, rc,
-                                            "error opening '%s'", mntdir);
+               if (path == NULL || strlen(path) == 0) {
+                       strncpy(mntdir, mnt.mnt_dir, strlen(mnt.mnt_dir));
+                       mntdir[strlen(mnt.mnt_dir)] = '\0';
+                       if ((want & WANT_FSNAME) && fsname != NULL) {
+                               strncpy(fsname, ptr, ptr_end - ptr);
+                               fsname[ptr_end - ptr] = '\0';
+                       }
+                       rc = 0;
+                       break;
+               /* Otherwise find the longest matching path */
+               } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
+                          (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
+                       strncpy(mntdir, mnt.mnt_dir, strlen(mnt.mnt_dir));
+                       mntdir[strlen(mnt.mnt_dir)] = '\0';
+                       len = mntlen;
+                       if ((want & WANT_FSNAME) && fsname != NULL) {
+                               strncpy(fsname, ptr, ptr_end - ptr);
+                               fsname[ptr_end - ptr] = '\0';
+                       }
+                       rc = 0;
+               }
+       }
+       endmntent(fp);
 
-                        } else {
-                                *outfd = fd;
-                        }
-                }
-        } else if (want & WANT_ERROR)
-                llapi_err_noerrno(LLAPI_MSG_ERROR,
-                                  "can't find fs root for '%s': %d",
-                                  (want & WANT_PATH) ? fsname : path, rc);
-        return rc;
+       /* Found it */
+       if (rc == 0) {
+               if ((want & WANT_PATH) && path != NULL) {
+                       strncpy(path, mntdir, strlen(mntdir));
+                       path[strlen(mntdir)] = '\0';
+               }
+               if (want & WANT_FD) {
+                       fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
+                       if (fd < 0) {
+                               rc = -errno;
+                               llapi_error(LLAPI_MSG_ERROR, rc,
+                                           "error opening '%s'", mntdir);
+
+                       } else {
+                               *outfd = fd;
+                       }
+               }
+       } else if (want & WANT_ERROR)
+               llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                 "can't find fs root for '%s': %d",
+                                 (want & WANT_PATH) ? fsname : path, rc);
+       return rc;
 }
 
 /*
@@ -1357,21 +1368,21 @@ static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
 }
 
 /* set errno upon failure */
-static DIR *opendir_parent(char *path)
+static DIR *opendir_parent(const char *path)
 {
-        DIR *parent;
-        char *fname;
-        char c;
-
-        fname = strrchr(path, '/');
-        if (fname == NULL)
-                return opendir(".");
-
-        c = fname[1];
-        fname[1] = '\0';
-        parent = opendir(path);
-        fname[1] = c;
-        return parent;
+       char *path_copy;
+       char *parent_path;
+       DIR *parent;
+
+       path_copy = strdup(path);
+       if (path_copy == NULL)
+               return NULL;
+
+       parent_path = dirname(path_copy);
+       parent = opendir(parent_path);
+       free(path_copy);
+
+       return parent;
 }
 
 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
@@ -1626,26 +1637,26 @@ int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
 
 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
 {
-        int fd, rc;
+       int fd, rc;
 
-        fd = open(path, O_RDONLY);
-        if (fd < 0) {
-                rc = -errno;
-                llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
-                return rc;
-        }
+       fd = open(path, O_RDONLY | O_NONBLOCK);
+       if (fd < 0) {
+               rc = -errno;
+               llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
+               return rc;
+       }
 
-        rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
+       rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
 
-        close(fd);
-        return rc;
+       close(fd);
+       return rc;
 }
 
 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
 {
        int fd, rc;
 
-       fd = open(path, O_RDONLY);
+       fd = open(path, O_RDONLY | O_NONBLOCK);
        if (fd < 0) {
                rc = -errno;
                llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
@@ -1728,23 +1739,23 @@ int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
 
 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
 {
-        DIR *root;
-        int rc;
+       int root;
+       int rc;
 
-        root = opendir(mnt);
-        if (!root) {
-                rc = -errno;
-                llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
-                return rc;
-        }
+       root = open(mnt, O_RDONLY | O_DIRECTORY);
+       if (root < 0) {
+               rc = -errno;
+               llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
+               return rc;
+       }
 
-        *count = is_mdt;
-        rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
-        if (rc < 0)
-                rc = -errno;
+       *count = is_mdt;
+       rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
+       if (rc < 0)
+               rc = -errno;
 
-        closedir(root);
-        return rc;
+       close(root);
+       return rc;
 }
 
 /* Check if user specified value matches a real uuid.  Ignore _UUID,
@@ -1772,7 +1783,7 @@ int llapi_uuid_match(char *real_uuid, char *search_uuid)
 
 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
  * returned in param->fp_obd_index */
-static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
+static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
 {
        struct obd_uuid obd_uuid;
        char buf[PATH_MAX];
@@ -1786,9 +1797,9 @@ static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
 
        /* Get the lov/lmv name */
        if (param->fp_get_lmv)
-               rc = llapi_file_fget_lmv_uuid(dirfd(dir), &obd_uuid);
+               rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
        else
-               rc = llapi_file_fget_lov_uuid(dirfd(dir), &obd_uuid);
+               rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
        if (rc) {
                if (rc != -ENOTTY) {
                        llapi_error(LLAPI_MSG_ERROR, rc,
@@ -1890,7 +1901,6 @@ retry_get_uuids:
                        uuids_temp = realloc(uuids, obdcount *
                                             sizeof(struct obd_uuid));
                        if (uuids_temp != NULL) {
-                               free(uuids);
                                uuids = uuids_temp;
                                goto retry_get_uuids;
                        }
@@ -1976,17 +1986,17 @@ static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
 
 int llapi_ostlist(char *path, struct find_param *param)
 {
-        DIR *dir;
-        int ret;
+       int fd;
+       int ret;
 
-        dir = opendir(path);
-        if (dir == NULL)
-                return -errno;
+       fd = open(path, O_RDONLY | O_DIRECTORY);
+       if (fd < 0)
+               return -errno;
 
-        ret = setup_obd_uuid(dir, path, param);
-        closedir(dir);
+       ret = setup_obd_uuid(fd, path, param);
+       close(fd);
 
-        return ret;
+       return ret;
 }
 
 /*
@@ -2089,13 +2099,13 @@ static int sattr_cache_get_defaults(const char *const fsname,
 }
 
 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
-                                     struct lov_user_ost_data_v1 *objects,
-                                     int is_dir, int verbose, int depth,
-                                     int raw, char *pool_name)
+                                    struct lov_user_ost_data_v1 *objects,
+                                    int is_dir, int verbose, int depth,
+                                    int raw, char *pool_name)
 {
-        char *prefix = is_dir ? "" : "lmm_";
+       char *prefix = is_dir ? "" : "lmm_";
        char *separator = "";
-        int rc;
+       int rc;
 
        if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
                lmm_oi_set_seq(&lum->lmm_oi, 0);
@@ -2103,69 +2113,97 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
                        llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
        }
 
-        if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
-                llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
+       if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
+               llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
 
        if ((verbose & VERBOSE_DETAIL) && !is_dir) {
                llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic:          0x%08X\n",
                             lum->lmm_magic);
-               llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq:            "LPX64"\n",
-                            lmm_oi_seq(&lum->lmm_oi));
-               llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id:      "LPX64"\n",
-                            lmm_oi_id(&lum->lmm_oi));
+               llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq:            %#jx\n",
+                            (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
+               llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id:      %#jx\n",
+                            (uintmax_t)lmm_oi_id(&lum->lmm_oi));
+       }
+       if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
+               if (verbose & ~VERBOSE_DFID)
+                       llapi_printf(LLAPI_MSG_NORMAL, "lmm_fid:            ");
+               /* This needs a bit of hand-holding since old 1.x lmm_oi
+                * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
+                * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
+                * a real FID.  Ideally the 2.x code would have stored this
+                * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
+                * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
+                * worked properly (especially since IGIF FIDs use mds_inum as
+                * the FID SEQ), but unfortunately that didn't happen.
+                *
+                * Print it to look like an IGIF FID, even though the fields
+                * are reversed on disk, so that it makes sense to userspace.
+                *
+                * Don't use ostid_id() and ostid_seq(), since they assume the
+                * oi_fid fields are in the right order.  This is why there are
+                * separate lmm_oi_seq() and lmm_oi_id() routines for this.
+                *
+                * For newer layout types hopefully this will be a real FID. */
+               llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
+                            lmm_oi_seq(&lum->lmm_oi) == 0 ?
+                               lmm_oi_id(&lum->lmm_oi) :
+                               lmm_oi_seq(&lum->lmm_oi),
+                            lmm_oi_seq(&lum->lmm_oi) == 0 ?
+                               0 : (__u32)lmm_oi_id(&lum->lmm_oi),
+                            (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32));
        }
 
-        if (verbose & VERBOSE_COUNT) {
-                if (verbose & ~VERBOSE_COUNT)
-                        llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count:   ",
-                                     prefix);
-                if (is_dir) {
-                        if (!raw && lum->lmm_stripe_count == 0) {
-                                unsigned int scount;
-                                rc = sattr_cache_get_defaults(NULL, path,
-                                                              &scount, NULL,
-                                                              NULL);
-                                if (rc == 0)
+       if (verbose & VERBOSE_COUNT) {
+               if (verbose & ~VERBOSE_COUNT)
+                       llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count:   ",
+                                    prefix);
+               if (is_dir) {
+                       if (!raw && lum->lmm_stripe_count == 0) {
+                               unsigned int scount;
+                               rc = sattr_cache_get_defaults(NULL, path,
+                                                             &scount, NULL,
+                                                             NULL);
+                               if (rc == 0)
                                        llapi_printf(LLAPI_MSG_NORMAL, "%d",
                                                     scount);
-                                else
-                                        llapi_error(LLAPI_MSG_ERROR, rc,
-                                                    "Cannot determine default"
-                                                    " stripe count.");
-                        } else {
+                               else
+                                       llapi_error(LLAPI_MSG_ERROR, rc,
+                                                   "Cannot determine default"
+                                                   " stripe count.");
+                       } else {
                                llapi_printf(LLAPI_MSG_NORMAL, "%d",
-                                             lum->lmm_stripe_count ==
-                                             (typeof(lum->lmm_stripe_count))(-1)
+                                            lum->lmm_stripe_count ==
+                                            (typeof(lum->lmm_stripe_count))(-1)
                                             ? -1 : lum->lmm_stripe_count);
-                        }
-                } else {
+                       }
+               } else {
                        llapi_printf(LLAPI_MSG_NORMAL, "%hd",
                                     (__s16)lum->lmm_stripe_count);
-                }
+               }
                separator = is_dir ? " " : "\n";
-        }
+       }
 
-        if (verbose & VERBOSE_SIZE) {
+       if (verbose & VERBOSE_SIZE) {
                llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
-                if (verbose & ~VERBOSE_SIZE)
-                        llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size:    ",
-                                     prefix);
-                if (is_dir && !raw && lum->lmm_stripe_size == 0) {
-                        unsigned int ssize;
-                        rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
-                                                      NULL);
-                        if (rc == 0)
+               if (verbose & ~VERBOSE_SIZE)
+                       llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size:    ",
+                                    prefix);
+               if (is_dir && !raw && lum->lmm_stripe_size == 0) {
+                       unsigned int ssize;
+                       rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
+                                                     NULL);
+                       if (rc == 0)
                                llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
-                        else
-                                llapi_error(LLAPI_MSG_ERROR, rc,
-                                            "Cannot determine default"
-                                            " stripe size.");
-                } else {
+                       else
+                               llapi_error(LLAPI_MSG_ERROR, rc,
+                                           "Cannot determine default"
+                                           " stripe size.");
+               } else {
                        llapi_printf(LLAPI_MSG_NORMAL, "%u",
                                     lum->lmm_stripe_size);
-                }
+               }
                separator = is_dir ? " " : "\n";
-        }
+       }
 
        if ((verbose & VERBOSE_LAYOUT) && !is_dir) {
                llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
@@ -2176,42 +2214,42 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
                separator = "\n";
        }
 
-        if ((verbose & VERBOSE_GENERATION) && !is_dir) {
+       if ((verbose & VERBOSE_GENERATION) && !is_dir) {
                llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
-                if (verbose & ~VERBOSE_GENERATION)
-                        llapi_printf(LLAPI_MSG_NORMAL, "%slayout_gen:     ",
-                                     prefix);
+               if (verbose & ~VERBOSE_GENERATION)
+                       llapi_printf(LLAPI_MSG_NORMAL, "%slayout_gen:     ",
+                                    prefix);
                llapi_printf(LLAPI_MSG_NORMAL, "%u",
                             (int)lum->lmm_layout_gen);
                separator = "\n";
-        }
+       }
 
-        if (verbose & VERBOSE_OFFSET) {
+       if (verbose & VERBOSE_OFFSET) {
                llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
-                if (verbose & ~VERBOSE_OFFSET)
-                        llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset:  ",
-                                     prefix);
-                if (is_dir)
+               if (verbose & ~VERBOSE_OFFSET)
+                       llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset:  ",
+                                    prefix);
+               if (is_dir)
                        llapi_printf(LLAPI_MSG_NORMAL, "%d",
-                                     lum->lmm_stripe_offset ==
-                                     (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
+                                    lum->lmm_stripe_offset ==
+                                    (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
                                     lum->lmm_stripe_offset);
-                else
+               else
                        llapi_printf(LLAPI_MSG_NORMAL, "%u",
                                     objects[0].l_ost_idx);
                separator = is_dir ? " " : "\n";
-        }
+       }
 
-        if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
+       if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
                llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
-                if (verbose & ~VERBOSE_POOL)
-                        llapi_printf(LLAPI_MSG_NORMAL, "%spool:           ",
-                                     prefix);
+               if (verbose & ~VERBOSE_POOL)
+                       llapi_printf(LLAPI_MSG_NORMAL, "%spool:           ",
+                                    prefix);
                llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
-        }
+       }
 
-        if (!is_dir || (is_dir && (verbose != VERBOSE_OBJID)))
-                llapi_printf(LLAPI_MSG_NORMAL, "\n");
+       if (!is_dir || (is_dir && (verbose != VERBOSE_OBJID)))
+               llapi_printf(LLAPI_MSG_NORMAL, "\n");
 }
 
 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
@@ -2410,44 +2448,44 @@ void llapi_lov_dump_user_lmm(struct find_param *param, char *path, int is_dir)
 
 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
 {
-        const char *fname;
-        char *dname;
-        int fd, rc = 0;
-
-        fname = strrchr(path, '/');
-
-        /* It should be a file (or other non-directory) */
-        if (fname == NULL) {
-                dname = (char *)malloc(2);
-                if (dname == NULL)
-                        return -ENOMEM;
-                strcpy(dname, ".");
-                fname = (char *)path;
-        } else {
-                dname = (char *)malloc(fname - path + 1);
-                if (dname == NULL)
-                        return -ENOMEM;
-                strncpy(dname, path, fname - path);
-                dname[fname - path] = '\0';
-                fname++;
-        }
+       const char *fname;
+       char *dname;
+       int fd, rc = 0;
 
-        fd = open(dname, O_RDONLY);
-        if (fd == -1) {
-                rc = -errno;
-                free(dname);
-                return rc;
-        }
+       fname = strrchr(path, '/');
 
-        strcpy((char *)lum, fname);
-        if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
-                rc = -errno;
+       /* It should be a file (or other non-directory) */
+       if (fname == NULL) {
+               dname = (char *)malloc(2);
+               if (dname == NULL)
+                       return -ENOMEM;
+               strcpy(dname, ".");
+               fname = (char *)path;
+       } else {
+               dname = (char *)malloc(fname - path + 1);
+               if (dname == NULL)
+                       return -ENOMEM;
+               strncpy(dname, path, fname - path);
+               dname[fname - path] = '\0';
+               fname++;
+       }
 
-        if (close(fd) == -1 && rc == 0)
-                rc = -errno;
+       fd = open(dname, O_RDONLY | O_NONBLOCK);
+       if (fd == -1) {
+               rc = -errno;
+               free(dname);
+               return rc;
+       }
 
-        free(dname);
-        return rc;
+       strcpy((char *)lum, fname);
+       if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
+               rc = -errno;
+
+       if (close(fd) == -1 && rc == 0)
+               rc = -errno;
+
+       free(dname);
+       return rc;
 }
 
 int llapi_file_lookup(int dirfd, const char *name)
@@ -3007,24 +3045,26 @@ decided:
 }
 
 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
-                     void *param_data, struct dirent64 *de)
+                              void *param_data, struct dirent64 *de)
 {
        struct find_param       *param = (struct find_param *)param_data;
-       DIR                     *dir = parent;
+       DIR                     *tmp_parent = parent;
        char                    raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
        char                    *rawbuf = raw;
        struct obd_ioctl_data   data = { 0 };
        int                     fd;
        int                     ret;
+       char                    *path_copy;
        char                    *filename;
+       bool                    retry = false;
 
        LASSERT(parent != NULL || dirp != NULL);
        if (dirp != NULL)
                closedir(*dirp);
 
        if (parent == NULL) {
-               dir = opendir_parent(path);
-               if (dir == NULL) {
+               tmp_parent = opendir_parent(path);
+               if (tmp_parent == NULL) {
                        *dirp = NULL;
                        ret = -errno;
                        llapi_error(LLAPI_MSG_ERROR, ret,
@@ -3033,9 +3073,10 @@ static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
                }
        }
 
-       fd = dirfd(dir);
+       fd = dirfd(tmp_parent);
 
-       filename = basename(path);
+       path_copy = strdup(path);
+       filename = basename(path_copy);
        data.ioc_inlbuf1 = (char *)filename;
        data.ioc_inllen1 = strlen(filename) + 1;
        data.ioc_inlbuf2 = (char *)&param->fp_mdt_index;
@@ -3047,8 +3088,19 @@ static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
                goto out;
        }
 
+migrate:
        ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
        if (ret != 0) {
+               if (errno == EBUSY && !retry) {
+                       /* because migrate may not be able to lock all involved
+                        * objects in order, for some of them it try lock, while
+                        * there may be conflicting COS locks and cause migrate
+                        * fail with EBUSY, hope a sync() could cause
+                        * transaction commit and release these COS locks. */
+                       sync();
+                       retry = true;
+                       goto migrate;
+               }
                ret = -errno;
                fprintf(stderr, "%s migrate failed: %s (%d)\n",
                        path, strerror(-ret), ret);
@@ -3066,16 +3118,17 @@ out:
                 * on the client side, and re-open to get the
                 * new directory handle */
                *dirp = opendir(path);
-               if (dirp == NULL) {
+               if (*dirp == NULL) {
                        ret = -errno;
                        llapi_error(LLAPI_MSG_ERROR, ret,
                                    "%s: Failed to open '%s'", __func__, path);
-                       return ret;
                }
        }
 
        if (parent == NULL)
-               closedir(dir);
+               closedir(tmp_parent);
+
+       free(path_copy);
 
        return ret;
 }
@@ -3190,10 +3243,10 @@ static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
 
        if (param->fp_obd_uuid) {
                param->fp_quiet = 1;
-                ret = setup_obd_uuid(d ? d : parent, path, param);
-                if (ret)
-                        return ret;
-        }
+               ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
+               if (ret)
+                       return ret;
+       }
 
        if (d) {
                if (param->fp_get_lmv || param->fp_get_default_lmv) {
@@ -3303,11 +3356,9 @@ int llapi_getstripe(char *path, struct find_param *param)
                               cb_common_fini, param);
 }
 
-int llapi_obd_statfs(char *path, __u32 type, __u32 index,
-                     struct obd_statfs *stat_buf,
-                     struct obd_uuid *uuid_buf)
+int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
+                     struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
 {
-        int fd;
         char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
         char *rawbuf = raw;
         struct obd_ioctl_data data = { 0 };
@@ -3329,23 +3380,31 @@ int llapi_obd_statfs(char *path, __u32 type, __u32 index,
                 return rc;
         }
 
-        fd = open(path, O_RDONLY);
-        if (errno == EISDIR)
-                fd = open(path, O_DIRECTORY | O_RDONLY);
+       rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
+
+       return rc < 0 ? -errno : 0;
+}
 
+int llapi_obd_statfs(char *path, __u32 type, __u32 index,
+                    struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
+{
+       int fd;
+       int rc;
+
+       fd = open(path, O_RDONLY);
        if (fd < 0) {
-               rc = errno ? -errno : -EBADF;
+               rc = -errno;
                llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
                            __func__, path);
                /* If we can't even open a file on the filesystem (e.g. with
                 * -ESHUTDOWN), force caller to exit or it will loop forever. */
                return -ENODEV;
        }
-       rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
-       if (rc)
-               rc = errno ? -errno : -EINVAL;
+
+       rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
 
        close(fd);
+
        return rc;
 }
 
@@ -3472,333 +3531,30 @@ int llapi_is_lustre_mnt(struct mntent *mnt)
 
 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
 {
-        DIR *root;
-        int rc;
-
-        root = opendir(mnt);
-        if (!root) {
-                rc = -errno;
-                llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
-                return rc;
-        }
-
-       rc = ioctl(dirfd(root), OBD_IOC_QUOTACTL, qctl);
-        if (rc < 0)
-                rc = -errno;
-
-        closedir(root);
-        return rc;
-}
-
-#include <pwd.h>
-#include <grp.h>
-#include <mntent.h>
-#include <sys/wait.h>
-#include <errno.h>
-#include <ctype.h>
-
-static int rmtacl_notify(int ops)
-{
-        FILE *fp;
-        struct mntent *mnt;
-       int found = 0, fd = 0, rc = 0;
-
-        fp = setmntent(MOUNTED, "r");
-        if (fp == NULL) {
-                rc = -errno;
-                llapi_error(LLAPI_MSG_ERROR, rc,
-                            "error setmntent(%s)", MOUNTED);
-                return rc;
-        }
-
-        while (1) {
-                mnt = getmntent(fp);
-                if (!mnt)
-                        break;
-
-               if (!llapi_is_lustre_mnt(mnt))
-                        continue;
-
-                fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
-                if (fd < 0) {
-                        rc = -errno;
-                        llapi_error(LLAPI_MSG_ERROR, rc,
-                                   "Can't open '%s'", mnt->mnt_dir);
-                       goto out;
-                }
-
-                rc = ioctl(fd, LL_IOC_RMTACL, ops);
-               close(fd);
-                if (rc < 0) {
-                        rc = -errno;
-                       llapi_error(LLAPI_MSG_ERROR, rc,
-                                   "ioctl RMTACL on '%s' err %d",
-                                   mnt->mnt_dir, rc);
-                       goto out;
-                }
-
-                found++;
-        }
-
-out:
-        endmntent(fp);
-       return ((rc != 0) ? rc : found);
-}
-
-static char *next_token(char *p, int div)
-{
-        if (p == NULL)
-                return NULL;
-
-        if (div)
-                while (*p && *p != ':' && !isspace(*p))
-                        p++;
-        else
-                while (*p == ':' || isspace(*p))
-                        p++;
-
-        return *p ? p : NULL;
-}
-
-static int rmtacl_name2id(char *name, int is_user)
-{
-        if (is_user) {
-                struct passwd *pw;
-
-                pw = getpwnam(name);
-                if (pw == NULL)
-                        return INVALID_ID;
-                else
-                        return (int)(pw->pw_uid);
-        } else {
-                struct group *gr;
-
-                gr = getgrnam(name);
-                if (gr == NULL)
-                        return INVALID_ID;
-                else
-                        return (int)(gr->gr_gid);
-        }
-}
-
-static int isodigit(int c)
-{
-        return (c >= '0' && c <= '7') ? 1 : 0;
-}
-
-/*
- * Whether the name is just digits string (uid/gid) already or not.
- * Return value:
- * 1: str is id
- * 0: str is not id
- */
-static int str_is_id(char *str)
-{
-        if (str == NULL)
-                return 0;
-
-        if (*str == '0') {
-                str++;
-                if (*str == 'x' || *str == 'X') { /* for Hex. */
-                        if (!isxdigit(*(++str)))
-                                return 0;
-
-                        while (isxdigit(*(++str)));
-                } else if (isodigit(*str)) { /* for Oct. */
-                        while (isodigit(*(++str)));
-                }
-        } else if (isdigit(*str)) { /* for Dec. */
-                while (isdigit(*(++str)));
-        }
-
-        return (*str == 0) ? 1 : 0;
-}
-
-typedef struct {
-        char *name;
-        int   length;
-        int   is_user;
-        int   next_token;
-} rmtacl_name_t;
-
-#define RMTACL_OPTNAME(name) name, sizeof(name) - 1
-
-static rmtacl_name_t rmtacl_namelist[] = {
-        { RMTACL_OPTNAME("user:"),            1,      0 },
-        { RMTACL_OPTNAME("group:"),           0,      0 },
-        { RMTACL_OPTNAME("default:user:"),    1,      0 },
-        { RMTACL_OPTNAME("default:group:"),   0,      0 },
-        /* for --tabular option */
-        { RMTACL_OPTNAME("user"),             1,      1 },
-        { RMTACL_OPTNAME("group"),            0,      1 },
-        { 0 }
-};
-
-static int rgetfacl_output(char *str)
-{
-        char *start = NULL, *end = NULL;
-        int is_user = 0, n, id;
-        char c;
-        rmtacl_name_t *rn;
-
-        if (str == NULL)
-                return -1;
-
-        for (rn = rmtacl_namelist; rn->name; rn++) {
-                if(strncmp(str, rn->name, rn->length) == 0) {
-                        if (!rn->next_token)
-                                start = str + rn->length;
-                        else
-                                start = next_token(str + rn->length, 0);
-                        is_user = rn->is_user;
-                        break;
-                }
-        }
-
-        end = next_token(start, 1);
-        if (end == NULL || start == end) {
-                n = printf("%s", str);
-                return n;
-        }
-
-        c = *end;
-        *end = 0;
-        id = rmtacl_name2id(start, is_user);
-        if (id == INVALID_ID) {
-                if (str_is_id(start)) {
-                        *end = c;
-                        n = printf("%s", str);
-                } else
-                        return -1;
-        } else if ((id == NOBODY_UID && is_user) ||
-                   (id == NOBODY_GID && !is_user)) {
-                *end = c;
-                n = printf("%s", str);
-        } else {
-                *end = c;
-                *start = 0;
-                n = printf("%s%d%s", str, id, end);
-        }
-        return n;
-}
-
-static int child_status(int status)
-{
-        return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
-}
-
-static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
-{
-        pid_t pid = 0;
-        int fd[2], status, rc;
-        FILE *fp;
-        char buf[PIPE_BUF];
-
-        if (output_func) {
-                if (pipe(fd) < 0) {
-                        rc = -errno;
-                       llapi_error(LLAPI_MSG_ERROR, rc, "Can't create pipe");
-                        return rc;
-                }
-
-                pid = fork();
-                if (pid < 0) {
-                        rc = -errno;
-                       llapi_error(LLAPI_MSG_ERROR, rc, "Can't fork");
-                        close(fd[0]);
-                        close(fd[1]);
-                        return rc;
-                } else if (!pid) {
-                        /* child process redirects its output. */
-                        close(fd[0]);
-                        close(1);
-                        if (dup2(fd[1], 1) < 0) {
-                                rc = -errno;
-                                llapi_error(LLAPI_MSG_ERROR, rc,
-                                           "Can't dup2 %d", fd[1]);
-                                close(fd[1]);
-                                return rc;
-                        }
-                } else {
-                        close(fd[1]);
-                }
-        }
-
-        if (!pid) {
-                status = rmtacl_notify(ops);
-                if (status < 0)
-                        return -errno;
-
-                exit(execvp(argv[0], argv));
-        }
-
-        /* the following is parent process */
-        fp = fdopen(fd[0], "r");
-        if (fp == NULL) {
-                rc = -errno;
-               llapi_error(LLAPI_MSG_ERROR, rc, "fdopen %d failed", fd[0]);
-                kill(pid, SIGKILL);
-                close(fd[0]);
-                return rc;
-        }
-
-        while (fgets(buf, PIPE_BUF, fp) != NULL) {
-                if (output_func(buf) < 0)
-                        fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
-                                buf);
-        }
-        fclose(fp);
-        close(fd[0]);
-
-        if (waitpid(pid, &status, 0) < 0) {
-                rc = -errno;
-               llapi_error(LLAPI_MSG_ERROR, rc, "waitpid %d failed", pid);
-                return rc;
-        }
-
-        return child_status(status);
-}
-
-int llapi_lsetfacl(int argc, char *argv[])
-{
-        return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
-}
-
-int llapi_lgetfacl(int argc, char *argv[])
-{
-        return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
-}
-
-int llapi_rsetfacl(int argc, char *argv[])
-{
-        return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
-}
-
-int llapi_rgetfacl(int argc, char *argv[])
-{
-        return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
-}
-
-int llapi_cp(int argc, char *argv[])
-{
-        int rc;
-
-        rc = rmtacl_notify(RMT_RSETFACL);
-        if (rc < 0)
-                return rc;
+       char fsname[PATH_MAX + 1];
+       int root;
+       int rc;
 
-        exit(execvp(argv[0], argv));
-}
+       rc = llapi_search_fsname(mnt, fsname);
+       if (rc) {
+               llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                 "'%s' isn't on Lustre filesystem", mnt);
+               return rc;
+       }
 
-int llapi_ls(int argc, char *argv[])
-{
-        int rc;
+       root = open(mnt, O_RDONLY | O_DIRECTORY);
+       if (root < 0) {
+               rc = -errno;
+               llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
+               return rc;
+       }
 
-        rc = rmtacl_notify(RMT_LGETFACL);
-        if (rc < 0)
-                return rc;
+       rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
+       if (rc < 0)
+               rc = -errno;
 
-        exit(execvp(argv[0], argv));
+       close(root);
+       return rc;
 }
 
 /* Print mdtname 'name' into 'buf' using 'format'.  Add -MDT0000 if needed.
@@ -3872,9 +3628,6 @@ int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
                 rc = -errno;
         else
                 rc = 0;
-        if (rc && want_error)
-                llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d err %d", opc, rc);
-
         close(fd);
         return rc;
 }
@@ -4089,6 +3842,7 @@ int llapi_changelog_clear(const char *mdtname, const char *idstr,
 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
                   int buflen, long long *recno, int *linkno)
 {
+       const char *fidstr_orig = fidstr;
        struct lu_fid fid;
        struct getinfo_fid2path *gf;
        int rc;
@@ -4099,8 +3853,8 @@ int llapi_fid2path(const char *device, const char *fidstr, char *buf,
        sscanf(fidstr, SFID, RFID(&fid));
        if (!fid_is_sane(&fid)) {
                llapi_err_noerrno(LLAPI_MSG_ERROR,
-                                 "bad FID format [%s], should be [seq:oid:ver]"
-                                 " (e.g. "DFID")\n", fidstr,
+                                 "bad FID format '%s', should be [seq:oid:ver]"
+                                 " (e.g. "DFID")\n", fidstr_orig,
                                  (unsigned long long)FID_SEQ_NORMAL, 2, 0);
                return -EINVAL;
        }
@@ -4108,6 +3862,7 @@ int llapi_fid2path(const char *device, const char *fidstr, char *buf,
        gf = malloc(sizeof(*gf) + buflen);
        if (gf == NULL)
                return -ENOMEM;
+
        gf->gf_fid = fid;
        gf->gf_recno = *recno;
        gf->gf_linkno = *linkno;
@@ -4115,21 +3870,20 @@ int llapi_fid2path(const char *device, const char *fidstr, char *buf,
 
        /* Take path or fsname */
        rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
-       if (rc) {
-               if (rc != -ENOENT)
-                       llapi_error(LLAPI_MSG_ERROR, rc, "ioctl err %d", rc);
-       } else {
-               memcpy(buf, gf->gf_path, gf->gf_pathlen);
-               if (buf[0] == '\0') { /* ROOT path */
-                       buf[0] = '/';
-                       buf[1] = '\0';
-               }
-               *recno = gf->gf_recno;
-               *linkno = gf->gf_linkno;
-        }
+       if (rc)
+               goto out_free;
 
-        free(gf);
-        return rc;
+       memcpy(buf, gf->gf_u.gf_path, gf->gf_pathlen);
+       if (buf[0] == '\0') { /* ROOT path */
+               buf[0] = '/';
+               buf[1] = '\0';
+       }
+       *recno = gf->gf_recno;
+       *linkno = gf->gf_linkno;
+
+out_free:
+       free(gf);
+       return rc;
 }
 
 static int fid_from_lma(const char *path, const int fd, lustre_fid *fid)
@@ -4242,52 +3996,24 @@ int llapi_path2parent(const char *path, unsigned int linkno,
 
 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
 {
-        DIR *root;
-        int rc;
-
-        root = opendir(mnt);
-        if (!root) {
-                rc = -errno;
-                llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
-                return rc;
-        }
-
-        rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
-        if (rc < 0) {
-                rc = -errno;
-                llapi_error(LLAPI_MSG_ERROR, rc,
-                            "ioctl on %s for getting connect flags failed", mnt);
-        }
-        closedir(root);
-        return rc;
-}
-
-int llapi_get_version(char *buffer, int buffer_size,
-                      char **version)
-{
-        int rc;
-        int fd;
-        struct obd_ioctl_data *data = (struct obd_ioctl_data *)buffer;
-
-        fd = open(OBD_DEV_PATH, O_RDONLY);
-        if (fd == -1)
-                return -errno;
+       int root;
+       int rc;
 
-        memset(buffer, 0, buffer_size);
-        data->ioc_version = OBD_IOCTL_VERSION;
-        data->ioc_inllen1 = buffer_size - cfs_size_round(sizeof(*data));
-        data->ioc_inlbuf1 = buffer + cfs_size_round(sizeof(*data));
-        data->ioc_len = obd_ioctl_packlen(data);
+       root = open(mnt, O_RDONLY | O_DIRECTORY);
+       if (root < 0) {
+               rc = -errno;
+               llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
+               return rc;
+       }
 
-        rc = ioctl(fd, OBD_GET_VERSION, buffer);
-        if (rc == -1) {
-                rc = -errno;
-                close(fd);
-                return rc;
-        }
-        close(fd);
-        *version = data->ioc_bulk;
-        return 0;
+       rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
+       if (rc < 0) {
+               rc = -errno;
+               llapi_error(LLAPI_MSG_ERROR, rc,
+                       "ioctl on %s for getting connect flags failed", mnt);
+       }
+       close(root);
+       return rc;
 }
 
 /**