Whamcloud - gitweb
b=20101 a fix for lfs getstripe --pool
[fs/lustre-release.git] / lustre / utils / liblustreapi.c
index 7a315c7..2167131 100644 (file)
@@ -26,7 +26,7 @@
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  */
 /*
@@ -261,7 +261,98 @@ int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
         return 0;
 }
 
-static int poolpath(char *fsname, char *pathname, char *pool_pathname);
+static int find_target_obdpath(char *fsname, char *path)
+{
+        glob_t glob_info;
+        char pattern[PATH_MAX + 1];
+        int rc;
+
+        snprintf(pattern, PATH_MAX,
+                 "/proc/fs/lustre/lov/%s-*/target_obd",
+                 fsname);
+        rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
+        if (rc == GLOB_NOMATCH)
+                return -ENODEV;
+        else if (rc)
+                return -EINVAL;
+
+        strcpy(path, glob_info.gl_pathv[0]);
+        globfree(&glob_info);
+        return 0;
+}
+
+static int find_poolpath(char *fsname, char *poolname, char *poolpath)
+{
+        glob_t glob_info;
+        char pattern[PATH_MAX + 1];
+        int rc;
+
+        snprintf(pattern, PATH_MAX,
+                 "/proc/fs/lustre/lov/%s-*/pools/%s",
+                 fsname, poolname);
+        rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
+        /* If no pools, make sure the lov is available */
+        if ((rc == GLOB_NOMATCH) &&
+            (find_target_obdpath(fsname, poolpath) == -ENODEV))
+                return -ENODEV;
+        if (rc)
+                return -EINVAL;
+
+        strcpy(poolpath, glob_info.gl_pathv[0]);
+        globfree(&glob_info);
+        return 0;
+}
+
+/*
+ * if pool is NULL, search ostname in target_obd
+ * if pool is not NULL:
+ *  if pool not found returns errno < 0
+ *  if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
+ *  if ostname is not NULL, returns 1 if OST is in pool and 0 if not
+ */
+int llapi_search_ost(char *fsname, char *poolname, char *ostname)
+{
+        FILE *fd;
+        char buffer[PATH_MAX + 1];
+        int len = 0, rc;
+
+        if (ostname != NULL)
+                len = strlen(ostname);
+
+        if (poolname == NULL)
+                rc = find_target_obdpath(fsname, buffer);
+        else
+                rc = find_poolpath(fsname, poolname, buffer);
+        if (rc)
+                return rc;
+
+        if ((fd = fopen(buffer, "r")) == NULL)
+                return -EINVAL;
+
+        while (fgets(buffer, sizeof(buffer), fd) != NULL) {
+                if (poolname == NULL) {
+                        char *ptr;
+                        /* Search for an ostname in the list of OSTs
+                         Line format is IDX: fsname-OSTxxxx_UUID STATUS */
+                        ptr = strchr(buffer, ' ');
+                        if ((ptr != NULL) &&
+                            (strncmp(ptr + 1, ostname, len) == 0)) {
+                                fclose(fd);
+                                return 1;
+                        }
+                } else {
+                        /* Search for an ostname in a pool,
+                         (or an existing non-empty pool if no ostname) */
+                        if ((ostname == NULL) ||
+                            (strncmp(buffer, ostname, len) == 0)) {
+                                fclose(fd);
+                                return 1;
+                        }
+                }
+        }
+        fclose(fd);
+        return 0;
+}
 
 int llapi_file_open_pool(const char *name, int flags, int mode,
                          unsigned long long stripe_size, int stripe_offset,
@@ -270,7 +361,40 @@ int llapi_file_open_pool(const char *name, int flags, int mode,
         struct lov_user_md_v3 lum = { 0 };
         int fd, rc = 0;
         int isdir = 0;
-        char fsname[MAX_OBD_NAME + 1], *ptr;
+
+        /* Make sure we have a good pool */
+        if (pool_name != NULL) {
+                char fsname[MAX_OBD_NAME + 1], *ptr;
+
+                if (llapi_search_fsname(name, fsname)) {
+                    llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
+                              "'%s' is not on a Lustre filesystem", name);
+                    return -EINVAL;
+                }
+
+                /* in case user gives the full pool name <fsname>.<poolname>,
+                 * strip the fsname */
+                ptr = strchr(pool_name, '.');
+                if (ptr != NULL) {
+                        *ptr = '\0';
+                        if (strcmp(pool_name, fsname) != 0) {
+                                *ptr = '.';
+                                llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
+                                   "Pool '%s' is not on filesystem '%s'",
+                                   pool_name, fsname);
+                                return -EINVAL;
+                        }
+                        pool_name = ptr + 1;
+                }
+
+                /* Make sure the pool exists and is non-empty */
+                if ((rc = llapi_search_ost(fsname, pool_name, NULL)) < 1) {
+                        llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
+                                  "pool '%s.%s' %s", fsname, pool_name,
+                                  rc == 0 ? "has no OSTs" : "does not exist");
+                        return -EINVAL;
+                }
+        }
 
         fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
         if (fd < 0 && errno == EISDIR) {
@@ -296,19 +420,7 @@ int llapi_file_open_pool(const char *name, int flags, int mode,
         lum.lmm_stripe_size = stripe_size;
         lum.lmm_stripe_count = stripe_count;
         lum.lmm_stripe_offset = stripe_offset;
-
-        /* in case user give the full pool name <fsname>.<poolname>, skip
-         * the fsname */
         if (pool_name != NULL) {
-                ptr = strchr(pool_name, '.');
-                if (ptr != NULL) {
-                        strncpy(fsname, pool_name, ptr - pool_name);
-                        *ptr = '\0';
-                        /* if fsname matches a filesystem skip it
-                         * if not keep the poolname as is */
-                        if (poolpath(fsname, NULL, NULL) == 0)
-                                pool_name = ptr + 1;
-                }
                 strncpy(lum.lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
         } else {
                 /* If no pool is specified at all, use V1 request */
@@ -388,7 +500,7 @@ static int get_root_path(int want, char *fsname, int *outfd, char *path,
 {
         struct mntent mnt;
         char buf[PATH_MAX], mntdir[PATH_MAX];
-        char *name = NULL, *ptr;
+        char *ptr;
         FILE *fp;
         int idx = 0, len = 0, mntlen, fd;
         int rc = -ENODEV;
@@ -408,6 +520,9 @@ static int get_root_path(int want, char *fsname, int *outfd, char *path,
                 if (!llapi_is_lustre_mnt(&mnt))
                         continue;
 
+                if ((want & WANT_INDEX) && (idx++ != index))
+                        continue;
+
                 mntlen = strlen(mnt.mnt_dir);
                 ptr = strrchr(mnt.mnt_fsname, '/');
                 if (!ptr && !len) {
@@ -416,10 +531,7 @@ static int get_root_path(int want, char *fsname, int *outfd, char *path,
                 }
                 ptr++;
 
-                if ((want & WANT_INDEX) && (idx++ != index))
-                        continue; 
-
-                /* Check the fsname for a match */
+                /* Check the fsname for a match, if given */
                 if (!(want & WANT_FSNAME) && fsname != NULL &&
                     (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
                         continue;
@@ -427,15 +539,17 @@ static int get_root_path(int want, char *fsname, int *outfd, char *path,
                 /* If the path isn't set return the first one we find */
                 if (path == NULL || strlen(path) == 0) {
                         strcpy(mntdir, mnt.mnt_dir);
-                        name = ptr;
+                        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)) { 
+                           (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
                         strcpy(mntdir, mnt.mnt_dir);
-                        mntlen = len;
-                        name = ptr;
+                        len = mntlen;
+                        if ((want & WANT_FSNAME) && fsname != NULL)
+                                strcpy(fsname, ptr);
                         rc = 0;
                 }
         }
@@ -443,8 +557,6 @@ static int get_root_path(int want, char *fsname, int *outfd, char *path,
 
         /* Found it */
         if (rc == 0) {
-                if ((want & WANT_FSNAME) && fsname != NULL)
-                        strcpy(fsname, name);
                 if ((want & WANT_PATH) && path != NULL)
                         strcpy(path, mntdir);
                 if (want & WANT_FD) {
@@ -480,7 +592,7 @@ int llapi_search_mounts(const char *pathname, int index, char *mntdir,
 {
         int want = WANT_PATH, idx = -1;
 
-        if (!pathname) {
+        if (!pathname || pathname[0] == '\0') {
                 want |= WANT_INDEX;
                 idx = index;
         } else
@@ -491,10 +603,22 @@ int llapi_search_mounts(const char *pathname, int index, char *mntdir,
         return get_root_path(want, fsname, NULL, mntdir, idx);
 }
 
+/* Given a path, find the corresponding Lustre fsname */
 int llapi_search_fsname(const char *pathname, char *fsname)
 {
+        char *path = (char*)pathname, buf[PATH_MAX + 1];
+
+        if (pathname[0] != '/') { /* Need a absolute path */
+                memset(buf, '\0', sizeof(buf));
+                if (realpath(pathname, buf) == NULL) {
+                        llapi_err(LLAPI_MSG_ERROR, "pathname '%s' cannot expand",
+                                  pathname);
+                        return -EINVAL;
+                }
+                path = buf;
+        }
         return get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL,
-                             (char *)pathname, -1);
+                             path, -1);
 }
 
 /* return the first file matching this pattern */
@@ -548,7 +672,7 @@ static int poolpath(char *fsname, char *pathname, char *pool_pathname)
 
 /**
  * Get the list of pool members.
- * \param poolname    string of format <fsname>.<poolname>
+ * \param poolname    string of format \<fsname\>.\<poolname\>
  * \param members     caller-allocated array of char*
  * \param list_size   size of the members array
  * \param buffer      caller-allocated buffer for storing OST names
@@ -785,6 +909,205 @@ static void find_param_fini(struct find_param *param)
                 free(param->lmd);
 }
 
+static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
+                          cfs_dirent_t *de)
+{
+        struct find_param *param = (struct find_param *)data;
+        param->depth--;
+        return 0;
+}
+
+static DIR *opendir_parent(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;
+}
+
+int llapi_mds_getfileinfo(char *path, DIR *parent,
+                          struct lov_user_mds_data *lmd)
+{
+        lstat_t *st = &lmd->lmd_st;
+        char *fname = strrchr(path, '/');
+        int ret = 0;
+
+        if (parent == NULL)
+                return -EINVAL;
+
+        fname = (fname == NULL ? path : fname + 1);
+        /* retrieve needed file info */
+        strncpy((char *)lmd, fname,
+                lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC));
+        ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
+
+        if (ret) {
+                if (errno == ENOTTY) {
+                        /* ioctl is not supported, it is not a lustre fs.
+                         * Do the regular lstat(2) instead. */
+                        ret = lstat_f(path, st);
+                        if (ret) {
+                                llapi_err(LLAPI_MSG_ERROR,
+                                          "error: %s: lstat failed for %s",
+                                          __func__, path);
+                                return ret;
+                        }
+                } else if (errno == ENOENT) {
+                        llapi_err(LLAPI_MSG_WARN,
+                                  "warning: %s: %s does not exist",
+                                  __func__, path);
+                        return -ENOENT;
+                } else {
+                        llapi_err(LLAPI_MSG_ERROR,
+                                 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
+                                 __func__, path);
+                        return ret;
+                }
+        }
+
+        return 0;
+}
+
+static int llapi_semantic_traverse(char *path, int size, DIR *parent,
+                                   semantic_func_t sem_init,
+                                   semantic_func_t sem_fini, void *data,
+                                   cfs_dirent_t *de)
+{
+        cfs_dirent_t *dent;
+        int len, ret;
+        DIR *d, *p = NULL;
+
+        ret = 0;
+        len = strlen(path);
+
+        d = opendir(path);
+        if (!d && errno != ENOTDIR) {
+                llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
+                          __func__, path);
+                return -EINVAL;
+        } else if (!d && !parent) {
+                /* ENOTDIR. Open the parent dir. */
+                p = opendir_parent(path);
+                if (!p)
+                        GOTO(out, ret = -EINVAL);
+        }
+
+        if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
+                goto err;
+
+        if (!d)
+                GOTO(out, ret = 0);
+
+        while ((dent = readdir64(d)) != NULL) {
+                ((struct find_param *)data)->have_fileinfo = 0;
+
+                if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
+                        continue;
+
+                /* Don't traverse .lustre directory */
+                if (!(strcmp(dent->d_name, dot_lustre_name)))
+                        continue;
+
+                path[len] = 0;
+                if ((len + dent->d_reclen + 2) > size) {
+                        llapi_err(LLAPI_MSG_ERROR,
+                                  "error: %s: string buffer is too small",
+                                  __func__);
+                        break;
+                }
+                strcat(path, "/");
+                strcat(path, dent->d_name);
+
+                if (dent->d_type == DT_UNKNOWN) {
+                        lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
+
+                        ret = llapi_mds_getfileinfo(path, d,
+                                             ((struct find_param *)data)->lmd);
+                        if (ret == 0) {
+                                ((struct find_param *)data)->have_fileinfo = 1;
+                                dent->d_type =
+                                        llapi_filetype_dir_table[st->st_mode &
+                                                                 S_IFMT];
+                        }
+                        if (ret == -ENOENT)
+                                continue;
+                }
+
+                switch (dent->d_type) {
+                case DT_UNKNOWN:
+                        llapi_err(LLAPI_MSG_ERROR,
+                                  "error: %s: '%s' is UNKNOWN type %d",
+                                  __func__, dent->d_name, dent->d_type);
+                        break;
+                case DT_DIR:
+                        ret = llapi_semantic_traverse(path, size, d, sem_init,
+                                                      sem_fini, data, dent);
+                        if (ret < 0)
+                                goto out;
+                        break;
+                default:
+                        ret = 0;
+                        if (sem_init) {
+                                ret = sem_init(path, d, NULL, data, dent);
+                                if (ret < 0)
+                                        goto out;
+                        }
+                        if (sem_fini && ret == 0)
+                                sem_fini(path, d, NULL, data, dent);
+                }
+        }
+
+out:
+        path[len] = 0;
+
+        if (sem_fini)
+                sem_fini(path, parent, d, data, de);
+err:
+        if (d)
+                closedir(d);
+        if (p)
+                closedir(p);
+        return ret;
+}
+
+static int param_callback(char *path, semantic_func_t sem_init,
+                          semantic_func_t sem_fini, struct find_param *param)
+{
+        int ret, len = strlen(path);
+        char *buf;
+
+        if (len > PATH_MAX) {
+                llapi_err(LLAPI_MSG_ERROR, "Path name '%s' is too long", path);
+                return -EINVAL;
+        }
+
+        buf = (char *)malloc(PATH_MAX + 1);
+        if (!buf)
+                return -ENOMEM;
+
+        ret = common_param_init(param);
+        if (ret)
+                goto out;
+        param->depth = 0;
+
+        strncpy(buf, path, PATH_MAX + 1);
+        ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
+                                      sem_fini, param, NULL);
+out:
+        find_param_fini(param);
+        free(buf);
+        return ret < 0 ? ret : 0;
+}
+
 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
 {
         int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
@@ -876,6 +1199,29 @@ int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
         return rc;
 }
 
+/* Check if user specified value matches a real uuid.  Ignore _UUID,
+ * -osc-4ba41334, other trailing gunk in comparison.
+ * @param real_uuid ends in "_UUID"
+ * @param search_uuid may or may not end in "_UUID"
+ */
+int llapi_uuid_match(char *real_uuid, char *search_uuid)
+{
+        int cmplen = strlen(real_uuid);
+        int searchlen = strlen(search_uuid);
+
+        if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
+                cmplen -= 5;
+        if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
+                searchlen -= 5;
+
+        /* The UUIDs may legitimately be different lengths, if
+         * the system was upgraded from an older version. */
+        if (cmplen != searchlen)
+                return 0;
+
+        return (strncmp(search_uuid, real_uuid, cmplen) == 0);
+}
+
 /* Here, param->obduuid points to a single obduuid, the index of which is
  * returned in param->obdindex */
 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
@@ -886,6 +1232,9 @@ static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
         FILE *fp;
         int rc = 0, index;
 
+        if (param->got_uuids)
+                return rc;
+
         /* Get the lov name */
         rc = llapi_file_fget_lov_uuid(dirfd(dir), &lov_uuid);
         if (rc) {
@@ -919,8 +1268,7 @@ static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
                         break;
 
                 if (param->obduuid) {
-                        if (strncmp(param->obduuid->uuid, uuid,
-                                    sizeof(uuid)) == 0) {
+                        if (llapi_uuid_match(uuid, param->obduuid->uuid)) {
                                 param->obdindex = index;
                                 break;
                         }
@@ -933,12 +1281,11 @@ static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
 
         fclose(fp);
 
-        if (!param->quiet && param->obduuid &&
-            (param->obdindex == OBD_NOT_FOUND)) {
+        if (param->obduuid && (param->obdindex == OBD_NOT_FOUND)) {
                 llapi_err_noerrno(LLAPI_MSG_ERROR,
                                   "error: %s: unknown obduuid: %s",
-                                  __FUNCTION__, param->obduuid->uuid);
-                //rc = EINVAL;
+                                  __func__, param->obduuid->uuid);
+                rc = -EINVAL;
         }
 
         return (rc);
@@ -982,16 +1329,22 @@ retry_get_uuids:
                 return -ENOMEM;
 
         for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
-                for (i = 0; i <= obdcount; i++) {
-                        if (strcmp((char *)&param->obduuid[obdnum].uuid,
-                                   (char *)&uuids[i]) == 0) {
+                for (i = 0; i < obdcount; i++) {
+                        if (llapi_uuid_match(uuids[i].uuid,
+                                             param->obduuid[obdnum].uuid)) {
                                 param->obdindexes[obdnum] = i;
                                 obd_valid++;
                                 break;
                         }
                 }
-                if (i == obdcount)
+                if (i >= obdcount) {
                         param->obdindexes[obdnum] = OBD_NOT_FOUND;
+                        llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                          "error: %s: unknown obduuid: %s",
+                                          __func__,
+                                          param->obduuid[obdnum].uuid);
+                        ret = -EINVAL;
+                }
         }
 
         if (obd_valid == 0)
@@ -1001,38 +1354,60 @@ retry_get_uuids:
 
         param->got_uuids = 1;
 
-        return 0;
+        return ret;
+}
+
+int llapi_ostlist(char *path, struct find_param *param)
+{
+        DIR *dir;
+        int ret;
+
+        dir = opendir(path);
+        if (dir == NULL)
+                return -errno;
+
+        ret = setup_obd_uuid(dir, path, param);
+        closedir(dir);
+
+        return ret;
 }
 
 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
-                                     int is_dir, int verbose, int quiet,
+                                     struct lov_user_ost_data_v1 *objects,
+                                     int is_dir, int verbose, int depth,
                                      char *pool_name)
 {
         char *prefix = is_dir ? "" : "lmm_";
         char nl = is_dir ? ' ' : '\n';
 
-        if (verbose && path)
+        if (is_dir && lum->lmm_object_seq == LOV_OBJECT_GROUP_DEFAULT) {
+                lum->lmm_object_seq = LOV_OBJECT_GROUP_CLEAR;
+                if (verbose & VERBOSE_DETAIL)
+                        llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
+        }
+
+        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_object_gr:      "LPX64"\n",
-                             lum->lmm_object_gr);
+                llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq:            "LPX64"\n",
+                             lum->lmm_object_seq);
                 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id:      "LPX64"\n",
                              lum->lmm_object_id);
         }
 
         if (verbose & VERBOSE_COUNT) {
-                if (!quiet)
+                if (verbose & ~VERBOSE_COUNT)
                         llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count:   ",
                                      prefix);
-                llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
-                             (int)lum->lmm_stripe_count, nl);
+                llapi_printf(LLAPI_MSG_NORMAL, "%hd%c",
+                             (__s16)lum->lmm_stripe_count, nl);
         }
 
         if (verbose & VERBOSE_SIZE) {
-                if (!quiet)
+                if (verbose & ~VERBOSE_SIZE)
                         llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size:    ",
                                      prefix);
                 llapi_printf(LLAPI_MSG_NORMAL, "%u%c", lum->lmm_stripe_size,
@@ -1045,70 +1420,59 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
         }
 
         if (verbose & VERBOSE_OFFSET) {
-                if (!quiet)
-                        llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset:   ",
+                if (verbose & ~VERBOSE_OFFSET)
+                        llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset:  ",
                                      prefix);
-                llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
-                             lum->lmm_objects[0].l_ost_idx, nl);
+                if (is_dir) 
+                        llapi_printf(LLAPI_MSG_NORMAL, "%d%c",
+                                     lum->lmm_stripe_offset ==
+                                     (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
+                                     lum->lmm_stripe_offset, nl);
+                else
+                        llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
+                                     objects[0].l_ost_idx, nl);
         }
 
-        if ((verbose & VERBOSE_POOL) && (pool_name != NULL))
-                llapi_printf(LLAPI_MSG_NORMAL, "pool: %s%c", pool_name, nl);
+        if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
+                if (verbose & ~VERBOSE_POOL)
+                        llapi_printf(LLAPI_MSG_NORMAL, "%spool:           ",
+                                     prefix);
+                llapi_printf(LLAPI_MSG_NORMAL, "%s%c", pool_name, nl);
+        }
 
-        if (is_dir)
+        if (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,
                             struct lov_user_ost_data_v1 *objects,
                             char *path, int is_dir,
-                            int obdindex, int quiet, int header, int body)
+                            int obdindex, int depth, int header)
 {
-        int i, obdstripe = 0;
+        int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
 
-        if (obdindex != OBD_NOT_FOUND) {
+        if (!obdstripe) {
                 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
                         if (obdindex == objects[i].l_ost_idx) {
-                                llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
                                 obdstripe = 1;
                                 break;
                         }
                 }
-        } else {
-                if (!quiet)
-                        llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
-                obdstripe = 1;
-        }
-
-        /* if it's a directory */
-        if (is_dir) {
-                if (obdstripe == 1) {
-                        if (lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
-                                llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
-                                lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
-                        }
-                        /* maintain original behavior */
-                        if (!header)
-                                header |= VERBOSE_ALL;
-                        lov_dump_user_lmm_header(lum, path, is_dir, header,
-                                                 quiet, pool_name);
-                }
-                return;
         }
 
-        if (header && (obdstripe == 1))
-                lov_dump_user_lmm_header(lum, NULL, is_dir, header, quiet,
+        if (obdstripe == 1)
+                lov_dump_user_lmm_header(lum, path, objects, is_dir, header, depth,
                                          pool_name);
 
-        if (body) {
-                if ((!quiet) && (obdstripe == 1))
+        if (!is_dir && (header & VERBOSE_OBJID)) {
+                if (obdstripe == 1)
                         llapi_printf(LLAPI_MSG_NORMAL,
                                      "\tobdidx\t\t objid\t\tobjid\t\t group\n");
 
                 for (i = 0; i < lum->lmm_stripe_count; i++) {
                         int idx = objects[i].l_ost_idx;
                         long long oid = objects[i].l_object_id;
-                        long long gr = objects[i].l_object_gr;
+                        long long gr = objects[i].l_object_seq;
                         if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
                                 llapi_printf(LLAPI_MSG_NORMAL,
                                            "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
@@ -1119,68 +1483,6 @@ void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
         }
 }
 
-void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
-                            int is_dir, int obdindex, int quiet,
-                            int header, int body)
-{
-        struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
-        int i, obdstripe = 0;
-
-        if (obdindex != OBD_NOT_FOUND) {
-                for (i = 0; i < lumj->lmm_stripe_count; i++) {
-                        if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
-                                llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
-                                obdstripe = 1;
-                                break;
-                        }
-                }
-        } else {
-                if (!quiet)
-                        llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
-                obdstripe = 1;
-        }
-
-        if (header && obdstripe == 1) {
-                lov_dump_user_lmm_header(lum, NULL, 0, header, quiet, NULL);
-
-                llapi_printf(LLAPI_MSG_NORMAL, "lmm_extent_count:   %x\n",
-                             lumj->lmm_extent_count);
-        }
-
-        if (body) {
-                unsigned long long start = -1, end = 0;
-                if (!quiet && obdstripe == 1)
-                        llapi_printf(LLAPI_MSG_NORMAL,
-                                     "joined\tobdidx\t\t objid\t\tobjid\t\t "
-                                     "group\t\tstart\t\tend\n");
-                for (i = 0; i < lumj->lmm_stripe_count; i++) {
-                        int idx = lumj->lmm_objects[i].l_ost_idx;
-                        long long oid = lumj->lmm_objects[i].l_object_id;
-                        long long gr = lumj->lmm_objects[i].l_object_gr;
-                        if (obdindex == OBD_NOT_FOUND || obdindex == idx)
-                                llapi_printf(LLAPI_MSG_NORMAL,
-                                             "\t%6u\t%14llu\t%#13llx\t%14llu%s",
-                                             idx, oid, oid, gr,
-                                             obdindex == idx ? " *" : "");
-                        if (start != lumj->lmm_objects[i].l_extent_start ||
-                            end != lumj->lmm_objects[i].l_extent_end) {
-                                start = lumj->lmm_objects[i].l_extent_start;
-                                llapi_printf(LLAPI_MSG_NORMAL,"\t%14llu",start);
-                                end = lumj->lmm_objects[i].l_extent_end;
-                                if (end == (unsigned long long)-1)
-                                        llapi_printf(LLAPI_MSG_NORMAL,
-                                                     "\t\tEOF\n");
-                                else
-                                        llapi_printf(LLAPI_MSG_NORMAL,
-                                                     "\t\t%llu\n", end);
-                        } else {
-                                llapi_printf(LLAPI_MSG_NORMAL, "\t\t\t\t\n");
-                        }
-                }
-                llapi_printf(LLAPI_MSG_NORMAL, "\n");
-        }
-}
-
 void llapi_lov_dump_user_lmm(struct find_param *param,
                              char *path, int is_dir)
 {
@@ -1189,15 +1491,8 @@ void llapi_lov_dump_user_lmm(struct find_param *param,
                 lov_dump_user_lmm_v1v3(&param->lmd->lmd_lmm, NULL,
                                        param->lmd->lmd_lmm.lmm_objects,
                                        path, is_dir,
-                                       param->obdindex, param->quiet,
-                                       param->verbose,
-                                       (param->verbose || !param->obduuid));
-                break;
-        case LOV_USER_MAGIC_JOIN:
-                lov_dump_user_lmm_join(&param->lmd->lmd_lmm, path, is_dir,
-                                       param->obdindex, param->quiet,
-                                       param->verbose,
-                                       (param->verbose || !param->obduuid));
+                                       param->obdindex, param->maxdepth,
+                                       param->verbose);
                 break;
         case LOV_USER_MAGIC_V3: {
                 char pool_name[LOV_MAXPOOLNAME + 1];
@@ -1209,17 +1504,15 @@ void llapi_lov_dump_user_lmm(struct find_param *param,
                 objects = lmmv3->lmm_objects;
                 lov_dump_user_lmm_v1v3(&param->lmd->lmd_lmm, pool_name,
                                        objects, path, is_dir,
-                                       param->obdindex, param->quiet,
-                                       param->verbose,
-                                       (param->verbose || !param->obduuid));
+                                       param->obdindex, param->maxdepth,
+                                       param->verbose);
                 break;
         }
         default:
                 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic:  %#x "
                              "(expecting one of %#x %#x %#x)\n",
                              *(__u32 *)&param->lmd->lmd_lmm,
-                             LOV_USER_MAGIC_V1, LOV_USER_MAGIC_JOIN,
-                             LOV_USER_MAGIC_V3);
+                             LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3);
                 return;
         }
 }
@@ -1292,168 +1585,6 @@ int llapi_file_lookup(int dirfd, const char *name)
         return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
 }
 
-int llapi_mds_getfileinfo(char *path, DIR *parent,
-                          struct lov_user_mds_data *lmd)
-{
-        lstat_t *st = &lmd->lmd_st;
-        char *fname = strrchr(path, '/');
-        int ret = 0;
-
-        if (parent == NULL)
-                return -EINVAL;
-
-        fname = (fname == NULL ? path : fname + 1);
-        /* retrieve needed file info */
-        strncpy((char *)lmd, fname,
-                lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC));
-        ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
-
-        if (ret) {
-                if (errno == ENOTTY) {
-                        /* ioctl is not supported, it is not a lustre fs.
-                         * Do the regular lstat(2) instead. */
-                        ret = lstat_f(path, st);
-                        if (ret) {
-                                llapi_err(LLAPI_MSG_ERROR,
-                                          "error: %s: lstat failed for %s",
-                                          __FUNCTION__, path);
-                                return ret;
-                        }
-                } else if (errno == ENOENT) {
-                        llapi_err(LLAPI_MSG_WARN,
-                                  "warning: %s: %s does not exist",
-                                  __FUNCTION__, path);
-                        return -ENOENT;
-                } else {
-                        llapi_err(LLAPI_MSG_ERROR,
-                                 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
-                                 __FUNCTION__, path);
-                        return ret;
-                }
-        }
-
-        return 0;
-}
-
-static DIR *opendir_parent(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;
-}
-
-static int llapi_semantic_traverse(char *path, int size, DIR *parent,
-                                   semantic_func_t sem_init,
-                                   semantic_func_t sem_fini, void *data,
-                                   cfs_dirent_t *de)
-{
-        cfs_dirent_t *dent;
-        int len, ret;
-        DIR *d, *p = NULL;
-
-        ret = 0;
-        len = strlen(path);
-
-        d = opendir(path);
-        if (!d && errno != ENOTDIR) {
-                llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
-                          __FUNCTION__, path);
-                return -EINVAL;
-        } else if (!d && !parent) {
-                /* ENOTDIR. Open the parent dir. */
-                p = opendir_parent(path);
-                if (!p)
-                        GOTO(out, ret = -EINVAL);
-        }
-
-        if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
-                goto err;
-
-        if (!d)
-                GOTO(out, ret = 0);
-
-        while ((dent = readdir64(d)) != NULL) {
-                ((struct find_param *)data)->have_fileinfo = 0;
-
-                if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
-                        continue;
-
-                /* Don't traverse .lustre directory */
-                if (!(strcmp(dent->d_name, dot_lustre_name)))
-                        continue;
-
-                path[len] = 0;
-                if ((len + dent->d_reclen + 2) > size) {
-                        llapi_err(LLAPI_MSG_ERROR,
-                                  "error: %s: string buffer is too small",
-                                  __FUNCTION__);
-                        break;
-                }
-                strcat(path, "/");
-                strcat(path, dent->d_name);
-
-                if (dent->d_type == DT_UNKNOWN) {
-                        lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
-
-                        ret = llapi_mds_getfileinfo(path, d,
-                                             ((struct find_param *)data)->lmd);
-                        if (ret == 0) {
-                                ((struct find_param *)data)->have_fileinfo = 1;
-                                dent->d_type =
-                                        llapi_filetype_dir_table[st->st_mode &
-                                                                 S_IFMT];
-                        }
-                        if (ret == -ENOENT)
-                                continue;
-                }
-
-                switch (dent->d_type) {
-                case DT_UNKNOWN:
-                        llapi_err(LLAPI_MSG_ERROR,
-                                  "error: %s: '%s' is UNKNOWN type %d",
-                                  __FUNCTION__, dent->d_name, dent->d_type);
-                        break;
-                case DT_DIR:
-                        ret = llapi_semantic_traverse(path, size, d, sem_init,
-                                                      sem_fini, data, dent);
-                        if (ret < 0)
-                                goto out;
-                        break;
-                default:
-                        ret = 0;
-                        if (sem_init) {
-                                ret = sem_init(path, d, NULL, data, dent);
-                                if (ret < 0)
-                                        goto out;
-                        }
-                        if (sem_fini && ret == 0)
-                                sem_fini(path, d, NULL, data, dent);
-                }
-        }
-
-out:
-        path[len] = 0;
-
-        if (sem_fini)
-                sem_fini(path, parent, d, data, de);
-err:
-        if (d)
-                closedir(d);
-        if (p)
-                closedir(p);
-        return ret;
-}
-
 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
  * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
  *
@@ -1474,28 +1605,26 @@ err:
  * Note: 5th actually means that the value is within the interval
  * (limit - margin, limit]. */
 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
-                          unsigned long long margin, int mds)
+                          int negopt, unsigned long long margin, int mds)
 {
+        int ret = -1;
+        
         if (sign > 0) {
-                if (file < limit)
-                        return mds ? 0 : 1;
-        }
-
-        if (sign == 0) {
-                if (file <= limit && file + margin > limit)
-                        return mds ? 0 : 1;
-                if (file + margin <= limit)
-                        return mds ? 0 : -1;
-        }
-
-        if (sign < 0) {
-                if (file > limit)
-                        return 1;
-                if (mds)
-                        return 0;
+                if (file <= limit)
+                        ret = mds ? 0 : 1;
+        } else if (sign == 0) {
+                if (file <= limit && file + margin >= limit)
+                        ret = mds ? 0 : 1;
+                else if (file + margin <= limit)
+                        ret = mds ? 0 : -1;
+        } else if (sign < 0) {
+                if (file >= limit)
+                        ret = 1;
+                else if (mds)
+                        ret = 0;
         }
 
-        return -1;
+        return negopt ? ~ret + 1 : ret;
 }
 
 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
@@ -1513,7 +1642,8 @@ static int find_time_check(lstat_t *st, struct find_param *param, int mds)
         /* Check if file is accepted. */
         if (param->atime) {
                 ret = find_value_cmp(st->st_atime, param->atime,
-                                     param->asign, 24 * 60 * 60, mds);
+                                     param->asign, param->exclude_atime, 
+                                     24 * 60 * 60, mds);
                 if (ret < 0)
                         return ret;
                 rc = ret;
@@ -1521,7 +1651,8 @@ static int find_time_check(lstat_t *st, struct find_param *param, int mds)
 
         if (param->mtime) {
                 ret = find_value_cmp(st->st_mtime, param->mtime,
-                                     param->msign, 24 * 60 * 60, mds);
+                                     param->msign, param->exclude_mtime, 
+                                     24 * 60 * 60, mds);
                 if (ret < 0)
                         return ret;
 
@@ -1533,7 +1664,8 @@ static int find_time_check(lstat_t *st, struct find_param *param, int mds)
 
         if (param->ctime) {
                 ret = find_value_cmp(st->st_ctime, param->ctime,
-                                     param->csign, 24 * 60 * 60, mds);
+                                     param->csign, param->exclude_ctime,
+                                     24 * 60 * 60, mds);
                 if (ret < 0)
                         return ret;
 
@@ -1586,7 +1718,7 @@ static int cb_find_init(char *path, DIR *parent, DIR *dir,
 
         /* If a time or OST should be checked, the decision is not taken yet. */
         if (param->atime || param->ctime || param->mtime || param->obduuid ||
-            param->size)
+            param->check_size)
                 decision = 0;
 
         ret = 0;
@@ -1616,17 +1748,17 @@ static int cb_find_init(char *path, DIR *parent, DIR *dir,
                         if (ret) {
                                 llapi_err(LLAPI_MSG_ERROR,
                                           "error: %s: lstat failed for %s",
-                                          __FUNCTION__, path);
+                                          __func__, path);
                                 return ret;
                         }
                 } else if (errno == ENOENT) {
                         llapi_err(LLAPI_MSG_WARN,
                                   "warning: %s: %s does not exist",
-                                  __FUNCTION__, path);
+                                  __func__, path);
                         goto decided;
                 } else {
                         llapi_err(LLAPI_MSG_ERROR,"error: %s: %s failed for %s",
-                                  __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" :
+                                  __func__, dir ? "LL_IOC_MDC_GETINFO" :
                                   "IOC_MDC_GETFILEINFO", path);
                         return ret;
                 }
@@ -1696,13 +1828,19 @@ static int cb_find_init(char *path, DIR *parent, DIR *dir,
                              i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
                                 for (j = 0; j < param->num_obds; j++) {
                                         if (param->obdindexes[j] ==
-                                            lmm_objects[i].l_ost_idx)
+                                            lmm_objects[i].l_ost_idx) {
+                                                if (param->exclude_obd)
+                                                        goto decided;
                                                 goto obd_matches;
+                                        }
                                 }
                         }
 
-                        if (i == param->lmd->lmd_lmm.lmm_stripe_count)
+                        if (i == param->lmd->lmd_lmm.lmm_stripe_count) {
+                                if (param->exclude_obd)
+                                        goto obd_matches;
                                 goto decided;
+                        }
                 }
         }
 
@@ -1760,7 +1898,8 @@ obd_matches:
            The regular stat is almost of the same speed as some new
            'glimpse-size-ioctl'. */
         if (!decision && S_ISREG(st->st_mode) &&
-            (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) {
+            param->lmd->lmd_lmm.lmm_stripe_count &&
+            (param->check_size ||param->atime || param->mtime || param->ctime)) {
                 if (param->obdindex != OBD_NOT_FOUND) {
                         /* Check whether the obd is active or not, if it is
                          * not active, just print the object affected by this
@@ -1797,12 +1936,12 @@ obd_matches:
                         if (errno == ENOENT) {
                                 llapi_err(LLAPI_MSG_ERROR,
                                           "warning: %s: %s does not exist",
-                                          __FUNCTION__, path);
+                                          __func__, path);
                                 goto decided;
                         } else {
                                 llapi_err(LLAPI_MSG_ERROR,
                                           "%s: IOC_LOV_GETINFO on %s failed",
-                                          __FUNCTION__, path);
+                                          __func__, path);
                                 return ret;
                         }
                 }
@@ -1813,10 +1952,10 @@ obd_matches:
                         goto decided;
         }
 
-        if (param->size)
+        if (param->check_size)
                 decision = find_value_cmp(st->st_size, param->size,
-                                          param->size_sign, param->size_units,
-                                          0);
+                                          param->size_sign, param->exclude_size,
+                                          param->size_units, 0);
 
 print_path:
         if (decision != -1) {
@@ -1836,44 +1975,82 @@ decided:
         return 0;
 }
 
-static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
-                          cfs_dirent_t *de)
+int llapi_find(char *path, struct find_param *param)
+{
+        return param_callback(path, cb_find_init, cb_common_fini, param);
+}
+
+/*
+ * Get MDT number that the file/directory inode referenced
+ * by the open fd resides on.
+ * Return 0 and mdtidx on success, or -ve errno.
+ */
+int llapi_file_fget_mdtidx(int fd, int *mdtidx)
 {
-        struct find_param *param = (struct find_param *)data;
-        param->depth--;
+        if (ioctl(fd, LL_IOC_GET_MDTIDX, &mdtidx) < 0)
+                return -errno;
         return 0;
 }
 
-int llapi_find(char *path, struct find_param *param)
+static int cb_get_mdt_index(char *path, DIR *parent, DIR *d, void *data,
+                            cfs_dirent_t *de)
 {
-        char *buf;
-        int ret, len = strlen(path);
+        struct find_param *param = (struct find_param *)data;
+        int ret = 0;
+        int mdtidx;
 
-        if (len > PATH_MAX) {
-                llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
-                          __FUNCTION__, path);
-                return -EINVAL;
-        }
+        LASSERT(parent != NULL || d != NULL);
 
-        buf = (char *)malloc(PATH_MAX + 1);
-        if (!buf)
-                return -ENOMEM;
+        if (d) {
+                ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
+        } else if (parent) {
+                int fd;
+
+                fd = open(path, O_RDONLY);
+                if (fd > 0) {
+                        ret = llapi_file_fget_mdtidx(fd, &mdtidx);
+                        close(fd);
+                } else {
+                        ret = fd;
+                }
+        }
 
-        ret = common_param_init(param);
         if (ret) {
-                free(buf);
+                if (errno == ENODATA) {
+                        if (!param->obduuid)
+                                llapi_printf(LLAPI_MSG_NORMAL,
+                                             "%s has no stripe info\n", path);
+                        goto out;
+                } else if (errno == ENOTTY) {
+                        llapi_err(LLAPI_MSG_ERROR,
+                                  "%s: '%s' not on a Lustre fs?",
+                                  __func__, path);
+                } else if (errno == ENOENT) {
+                        llapi_err(LLAPI_MSG_WARN,
+                                  "warning: %s: %s does not exist",
+                                  __func__, path);
+                        goto out;
+                } else {
+                        llapi_err(LLAPI_MSG_ERROR,
+                                  "error: %s: LL_IOC_GET_MDTIDX failed for %s",
+                                   __func__, path);
+                }
                 return ret;
         }
 
-        param->depth = 0;
+        if (param->quiet)
+                llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
+        else
+                llapi_printf(LLAPI_MSG_NORMAL, "%s MDT index: %d\n",
+                             path, mdtidx);
 
-        strncpy(buf, path, PATH_MAX + 1);
-        ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
-                                      cb_common_fini, param, NULL);
+out:
+        /* Do not get down anymore? */
+        if (param->depth == param->maxdepth)
+                return 1;
 
-        find_param_fini(param);
-        free(buf);
-        return ret < 0 ? ret : 0;
+        param->depth++;
+        return 0;
 }
 
 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
@@ -1884,8 +2061,8 @@ static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
 
         LASSERT(parent != NULL || d != NULL);
 
-        /* Prepare odb. */
-        if (!param->got_uuids) {
+        if (param->obduuid) {
+                param->quiet = 1;
                 ret = setup_obd_uuid(d ? d : parent, path, param);
                 if (ret)
                         return ret;
@@ -1899,36 +2076,39 @@ static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
                 fname = (fname == NULL ? path : fname + 1);
 
                 strncpy((char *)&param->lmd->lmd_lmm, fname, param->lumlen);
+
                 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
                             (void *)&param->lmd->lmd_lmm);
         }
 
         if (ret) {
                 if (errno == ENODATA) {
-                        if (!param->obduuid && !param->quiet)
+                        if (!param->obduuid)
                                 llapi_printf(LLAPI_MSG_NORMAL,
                                              "%s has no stripe info\n", path);
                         goto out;
                 } else if (errno == ENOTTY) {
                         llapi_err(LLAPI_MSG_ERROR,
                                   "%s: '%s' not on a Lustre fs?",
-                                  __FUNCTION__, path);
+                                  __func__, path);
                 } else if (errno == ENOENT) {
                         llapi_err(LLAPI_MSG_WARN,
                                   "warning: %s: %s does not exist",
-                                  __FUNCTION__, path);
+                                  __func__, path);
                         goto out;
                 } else {
                         llapi_err(LLAPI_MSG_ERROR,
                                   "error: %s: %s failed for %s",
-                                   __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" :
+                                   __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
                                   "IOC_MDC_GETFILESTRIPE", path);
                 }
 
                 return ret;
         }
 
-        llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
+        if (!param->get_mdt_index)
+                llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
+
 out:
         /* Do not get down anymore? */
         if (param->depth == param->maxdepth)
@@ -1940,34 +2120,9 @@ out:
 
 int llapi_getstripe(char *path, struct find_param *param)
 {
-        char *buf;
-        int ret = 0, len = strlen(path);
-
-        if (len > PATH_MAX) {
-                llapi_err(LLAPI_MSG_ERROR,
-                          "%s: Path name '%s' is too long",
-                          __FUNCTION__, path);
-                return -EINVAL;
-        }
-
-        buf = (char *)malloc(PATH_MAX + 1);
-        if (!buf)
-                return -ENOMEM;
-
-        ret = common_param_init(param);
-        if (ret) {
-                free(buf);
-                return ret;
-        }
-
-        param->depth = 0;
-
-        strncpy(buf, path, PATH_MAX + 1);
-        ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
-                                      cb_common_fini, param, NULL);
-        find_param_fini(param);
-        free(buf);
-        return ret < 0 ? ret : 0;
+        return param_callback(path, param->get_mdt_index ?
+                              cb_get_mdt_index : cb_getstripe,
+                              cb_common_fini, param);
 }
 
 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
@@ -2002,7 +2157,7 @@ int llapi_obd_statfs(char *path, __u32 type, __u32 index,
         if (fd < 0) {
                 rc = errno ? -errno : -EBADF;
                 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
-                          __FUNCTION__, path);
+                          __func__, path);
                 return rc;
         }
         rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
@@ -2253,7 +2408,7 @@ static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
                 } else if (errno == ENOENT) {
                         llapi_err(LLAPI_MSG_ERROR,
                                   "warning: %s: %s does not exist",
-                                  __FUNCTION__, path);
+                                  __func__, path);
                         rc = 0;
                 } else if (errno != EISDIR) {
                         rc = errno;
@@ -2284,35 +2439,13 @@ static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
 int llapi_quotachown(char *path, int flag)
 {
         struct find_param param;
-        char *buf;
-        int ret = 0, len = strlen(path);
-
-        if (len > PATH_MAX) {
-                llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
-                          __FUNCTION__, path);
-                return -EINVAL;
-        }
-
-        buf = (char *)malloc(PATH_MAX + 1);
-        if (!buf)
-                return -ENOMEM;
 
         memset(&param, 0, sizeof(param));
         param.recursive = 1;
         param.verbose = 0;
         param.quiet = 1;
 
-        ret = common_param_init(&param);
-        if (ret)
-                goto out;
-
-        strncpy(buf, path, PATH_MAX + 1);
-        ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
-                                      NULL, &param, NULL);
-out:
-        find_param_fini(&param);
-        free(buf);
-        return ret;
+        return param_callback(path, cb_quotachown, NULL, &param);
 }
 
 #include <pwd.h>
@@ -2608,6 +2741,8 @@ int llapi_ls(int argc, char *argv[])
 
 /* Print mdtname 'name' into 'buf' using 'format'.  Add -MDT0000 if needed.
  * format must have %s%s, buf must be > 16
+ * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
+ *     then buf = "lustre-MDT0000"
  */
 static int get_mdtname(char *name, char *format, char *buf)
 {
@@ -2633,65 +2768,118 @@ static int get_mdtname(char *name, char *format, char *buf)
         return sprintf(buf, format, name, suffix);
 }
 
+/** ioctl on filsystem root, with mdtindex sent as data
+ * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
+ * \param mdtidxp pointer to integer within data to be filled in with the
+ *    mdt index (0 if no mdt is specified).  NULL won't be filled.
+ */
+static int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
+                      int want_error)
+{
+        char fsname[20];
+        char *ptr;
+        int fd, index, rc;
+
+        /* Take path, fsname, or MDTname.  Assume MDT0000 in the former cases.
+         Open root and parse mdt index. */
+        if (mdtname[0] == '/') {
+                index = 0;
+                rc = get_root_path(WANT_FD | want_error, NULL, &fd,
+                                   (char *)mdtname, -1);
+        } else {
+                if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
+                        return -EINVAL;
+                ptr = fsname + strlen(fsname) - 8;
+                *ptr = '\0';
+                index = strtol(ptr + 4, NULL, 10);
+                rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
+        }
+        if (rc < 0) {
+                if (want_error)
+                        llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
+                                  "Can't open %s: %d\n", mdtname, rc);
+                return rc;
+        }
+
+        if (mdtidxp)
+                *mdtidxp = index;
+
+        rc = ioctl(fd, opc, data);
+        if (rc && want_error)
+                llapi_err(LLAPI_MSG_ERROR, "ioctl %d err %d", opc, rc);
+
+        close(fd);
+        return rc;
+}
+
 /****** Changelog API ********/
+
+static int changelog_ioctl(const char *mdtname, int opc, int id,
+                           long long recno, int flags)
+{
+        struct ioc_changelog data;
+        int *idx;
+
+        data.icc_id = id;
+        data.icc_recno = recno;
+        data.icc_flags = flags;
+        idx = (int *)(&data.icc_mdtindex);
+
+        return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR);
+}
+
 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
 struct changelog_private {
         int magic;
-        int fd;
         int flags;
+        lustre_kernelcomm kuc;
 };
 
 /** Start reading from a changelog
  * @param priv Opaque private control structure
- * @param flags Start flags (e.g. follow)
+ * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
  * @param device Report changes recorded on this MDT
  * @param startrec Report changes beginning with this record number
+ * (just call llapi_changelog_fini when done; don't need an endrec)
  */
 int llapi_changelog_start(void **priv, int flags, const char *device,
                           long long startrec)
 {
         struct changelog_private *cp;
-        char path[256];
-        char mdtname[20];
-        int rc, fd;
-
-        if (device[0] == '/')
-                rc = llapi_search_fsname(device, mdtname);
-        else
-                strncpy(mdtname, device, sizeof(mdtname));
-
-        /* Use either the mdd changelog (preferred) or a client mdc changelog */
-        if (get_mdtname(mdtname,
-                        "/proc/fs/lustre/md[cd]/%s%s{,-mdc-*}/changelog",
-                        path) < 0)
-                return -EINVAL;
-        rc = first_match(path, path);
-        if (rc)
-                return rc;
-
-        if ((fd = open(path, O_RDONLY)) < 0) {
-                llapi_err(LLAPI_MSG_ERROR, "error: can't open |%s|\n", path);
-                return -errno;
-        }
-
-        rc = lseek(fd, (off_t)startrec, SEEK_SET);
-        if (rc < 0) {
-                llapi_err(LLAPI_MSG_ERROR, "can't seek rc=%d\n", rc);
-                return -errno;
-        }
+        int rc;
 
-        cp = malloc(sizeof(*cp));
-        if (cp == NULL) {
-                close(fd);
+        /* Set up the receiver control struct */
+        cp = calloc(1, sizeof(*cp));
+        if (cp == NULL)
                 return -ENOMEM;
-        }
 
         cp->magic = CHANGELOG_PRIV_MAGIC;
-        cp->fd = fd;
         cp->flags = flags;
+
+        /* Set up the receiver */
+        rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */);
+        if (rc < 0)
+                goto out_free;
+
         *priv = cp;
 
+        /* Tell the kernel to start sending */
+        rc = changelog_ioctl(device, OBD_IOC_CHANGELOG_SEND, cp->kuc.lk_wfd,
+                             startrec, flags);
+        /* Only the kernel reference keeps the write side open */
+        close(cp->kuc.lk_wfd);
+        cp->kuc.lk_wfd = 0;
+        if (rc < 0) {
+                /* frees and clears priv */
+                llapi_changelog_fini(priv);
+                return rc;
+        }
+
         return 0;
+
+out_free:
+        free(cp);
+        return rc;
 }
 
 /** Finish reading from a changelog */
@@ -2702,22 +2890,12 @@ int llapi_changelog_fini(void **priv)
         if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
                 return -EINVAL;
 
-        close(cp->fd);
+        libcfs_ukuc_stop(&cp->kuc);
         free(cp);
         *priv = NULL;
         return 0;
 }
 
-static int pollwait(int fd) {
-        struct pollfd pfds[1];
-        int rc;
-
-        pfds[0].fd = fd;
-        pfds[0].events = POLLIN;
-        rc = poll(pfds, 1, -1);
-        return rc < 0 ? -errno : rc;
-}
-
 /** Read the next changelog entry
  * @param priv Opaque private control structure
  * @param rech Changelog record handle; record will be allocated here
@@ -2728,49 +2906,68 @@ static int pollwait(int fd) {
 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
 {
         struct changelog_private *cp = (struct changelog_private *)priv;
-        struct changelog_rec rec, *recp;
+        struct kuc_hdr *kuch;
         int rc = 0;
 
         if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
                 return -EINVAL;
         if (rech == NULL)
                 return -EINVAL;
+        kuch = malloc(CR_MAXSIZE + sizeof(*kuch));
+        if (kuch == NULL)
+                return -ENOMEM;
 
-readrec:
-        /* Read in the rec to get the namelen */
-        rc = read(cp->fd, &rec, sizeof(rec));
+repeat:
+        rc = libcfs_ukuc_msg_get(&cp->kuc, (char *)kuch,
+                                 CR_MAXSIZE + sizeof(*kuch),
+                                 KUC_TRANSPORT_CHANGELOG);
         if (rc < 0)
-                return -errno;
-        if (rc == 0) {
-                if (cp->flags && CHANGELOG_FLAG_FOLLOW) {
-                        rc = pollwait(cp->fd);
-                        if (rc < 0)
-                                return rc;
-                        goto readrec;
-                }
-                return 1;
+                goto out_free;
+
+        if ((kuch->kuc_transport != KUC_TRANSPORT_CHANGELOG) ||
+            ((kuch->kuc_msgtype != CL_RECORD) &&
+             (kuch->kuc_msgtype != CL_EOF))) {
+                llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
+                          "Unknown changelog message type %d:%d\n",
+                          kuch->kuc_transport, kuch->kuc_msgtype);
+                rc = -EPROTO;
+                goto out_free;
         }
 
-        recp = malloc(sizeof(rec) + rec.cr_namelen);
-        if (recp == NULL)
-                return -ENOMEM;
-        memcpy(recp, &rec, sizeof(rec));
-        rc = read(cp->fd, recp->cr_name, rec.cr_namelen);
-        if (rc < 0) {
-                free(recp);
-                llapi_err(LLAPI_MSG_ERROR, "Can't read entire filename");
-                return -errno;
+        if (kuch->kuc_msgtype == CL_EOF) {
+                if (cp->flags & CHANGELOG_FLAG_FOLLOW) {
+                        /* Ignore EOFs */
+                        goto repeat;
+                } else {
+                        rc = 1;
+                        goto out_free;
+                }
         }
 
-        *rech = recp;
+        /* Our message is a changelog_rec.  Use pointer math to skip
+         * kuch_hdr and point directly to the message payload.
+         */
+        *rech = (struct changelog_rec *)(kuch + 1);
+
         return 0;
+
+out_free:
+        *rech = NULL;
+        free(kuch);
+        return rc;
 }
 
 /** Release the changelog record when done with it. */
 int llapi_changelog_free(struct changelog_rec **rech)
 {
-        if (*rech)
-                free(*rech);
+        if (*rech) {
+                /* We allocated memory starting at the kuc_hdr, but passed
+                 * the consumer a pointer to the payload.
+                 * Use pointer math to get back to the header.
+                 */
+                struct kuc_hdr *kuch = (struct kuc_hdr *)*rech - 1;
+                free(kuch);
+        }
         *rech = NULL;
         return 0;
 }
@@ -2778,10 +2975,7 @@ int llapi_changelog_free(struct changelog_rec **rech)
 int llapi_changelog_clear(const char *mdtname, const char *idstr,
                           long long endrec)
 {
-        struct ioc_changelog_clear data;
-        char fsname[17];
-        char *ptr;
-        int id, fd, index, rc;
+        int id;
 
         if (endrec < 0) {
                 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
@@ -2798,43 +2992,15 @@ int llapi_changelog_clear(const char *mdtname, const char *idstr,
                 return -EINVAL;
         }
 
-        /* Take path, fsname, or MDTNAME.  Assume MDT0000 in the former cases */
-        if (mdtname[0] == '/') {
-                index = 0;
-                fd = open(mdtname, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
-                rc = fd < 0 ? -errno : 0;
-        } else {
-                if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
-                        return -EINVAL;
-                ptr = fsname + strlen(fsname) - 8;
-                *ptr = '\0';
-                index = strtol(ptr + 4, NULL, 10);
-                rc = get_root_path(WANT_FD | WANT_ERROR, fsname, &fd, NULL, -1);
-        }
-        if (rc < 0) {
-                llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
-                          "Can't open %s: %d\n", mdtname, rc);
-                return rc;
-        }
-
-        data.icc_mdtindex = index;
-        data.icc_id = id;
-        data.icc_recno = endrec;
-        rc = ioctl(fd, OBD_IOC_CHANGELOG_CLEAR, &data);
-        if (rc)
-                llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
-
-        close(fd);
-        return rc;
+        return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0);
 }
 
 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
                    int buflen, long long *recno, int *linkno)
 {
-        char path[PATH_MAX];
         struct lu_fid fid;
         struct getinfo_fid2path *gf;
-        int fd, rc;
+        int rc;
 
         while (*fidstr == '[')
                 fidstr++;
@@ -2847,26 +3013,16 @@ int llapi_fid2path(const char *device, const char *fidstr, char *buf,
                 return -EINVAL;
         }
 
-        /* Take path or fsname */
-        if (device[0] == '/') {
-                strcpy(path, device);
-        } else {
-                rc = get_root_path(WANT_PATH | WANT_ERROR, (char *)device,
-                                   NULL, path, -1);
-                if (rc < 0)
-                        return rc;
-        }
-        sprintf(path, "%s/%s/fid/%s", path, dot_lustre_name, fidstr);
-        fd = open(path, O_RDONLY | O_NONBLOCK);
-        if (fd < 0)
-                return -errno;
-
         gf = malloc(sizeof(*gf) + buflen);
+        if (gf == NULL)
+                return -ENOMEM;
         gf->gf_fid = fid;
         gf->gf_recno = *recno;
         gf->gf_linkno = *linkno;
         gf->gf_pathlen = buflen;
-        rc = ioctl(fd, OBD_IOC_FID2PATH, gf);
+
+        /* Take path or fsname */
+        rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
         if (rc) {
                 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
         } else {
@@ -2876,7 +3032,6 @@ int llapi_fid2path(const char *device, const char *fidstr, char *buf,
         }
 
         free(gf);
-        close(fd);
         return rc;
 }
 
@@ -2890,7 +3045,7 @@ static int path2fid_from_lma(const char *path, lustre_fid *fid)
         if (rc < 0)
                 return -errno;
         lma = (struct lustre_mdt_attrs *)buf;
-        fid_be_to_cpu(fid, &lma->lma_self_fid);
+        fid_le_to_cpu(fid, &lma->lma_self_fid);
         return 0;
 }
 
@@ -2918,42 +3073,68 @@ int llapi_path2fid(const char *path, lustre_fid *fid)
 #define CT_PRIV_MAGIC 0xC0BE2001
 struct copytool_private {
         int magic;
-        lustre_netlink lnl;
-        int archive_num_count;
-        int archive_nums[0];
+        char *fsname;
+        lustre_kernelcomm kuc;
+        __u32 archives;
 };
 
 #include <libcfs/libcfs.h>
 
 /** Register a copytool
- * @param priv Opaque private control structure
+ * @param[out] priv Opaque private control structure
+ * @param fsname Lustre filesystem
  * @param flags Open flags, currently unused (e.g. O_NONBLOCK)
- * @param archive_nums Which archive numbers this copytool is responsible for
+ * @param archive_count
+ * @param archives Which archive numbers this copytool is responsible for
  */
-int llapi_copytool_start(void **priv, int flags, int archive_num_count,
-                         int *archive_nums)
+int llapi_copytool_start(void **priv, char *fsname, int flags,
+                         int archive_count, int *archives)
 {
         struct copytool_private *ct;
         int rc;
 
-        if (archive_num_count > 0 && archive_nums == NULL) {
+        if (archive_count > 0 && archives == NULL) {
                 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
                           "NULL archive numbers");
                 return -EINVAL;
         }
 
-        ct = malloc(sizeof(*ct) +
-                    archive_num_count * sizeof(ct->archive_nums[0]));
+        ct = calloc(1, sizeof(*ct));
         if (ct == NULL)
                 return -ENOMEM;
 
+        ct->fsname = malloc(strlen(fsname) + 1);
+        if (ct->fsname == NULL) {
+                rc = -ENOMEM;
+                goto out_err;
+        }
+        strcpy(ct->fsname, fsname);
         ct->magic = CT_PRIV_MAGIC;
-        ct->archive_num_count = archive_num_count;
-        if (ct->archive_num_count > 0)
-                memcpy(ct->archive_nums, archive_nums, archive_num_count *
-                       sizeof(ct->archive_nums[0]));
+        ct->archives = 0;
+        for (rc = 0; rc < archive_count; rc++) {
+                if (archives[rc] > sizeof(ct->archives)) {
+                        llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
+                                  "Maximum of %d archives supported",
+                                  sizeof(ct->archives));
+                        goto out_err;
+                }
+                ct->archives |= 1 << archives[rc];
+        }
+        /* special case: if no archives specified, default to archive #0. */
+        if (ct->archives == 0)
+                ct->archives = 1;
+
+        rc = libcfs_ukuc_start(&ct->kuc, KUC_GRP_HSM);
+        if (rc < 0)
+                goto out_err;
 
-        rc = libcfs_ulnl_start(&ct->lnl, LNL_GRP_HSM);
+        /* Storing archive(s) in lk_data; see mdc_ioc_hsm_ct_start */
+        ct->kuc.lk_data = ct->archives;
+        rc = root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL,
+                        WANT_ERROR);
+        /* Only the kernel reference keeps the write side open */
+        close(ct->kuc.lk_wfd);
+        ct->kuc.lk_wfd = 0;
         if (rc < 0)
                 goto out_err;
 
@@ -2961,6 +3142,8 @@ int llapi_copytool_start(void **priv, int flags, int archive_num_count,
         return 0;
 
 out_err:
+        if (ct->fsname)
+                free(ct->fsname);
         free(ct);
         return rc;
 }
@@ -2973,7 +3156,14 @@ int llapi_copytool_fini(void **priv)
         if (!ct || (ct->magic != CT_PRIV_MAGIC))
                 return -EINVAL;
 
-        libcfs_ulnl_stop(&ct->lnl);
+        /* Tell the kernel to stop sending us messages */
+        ct->kuc.lk_flags = LK_FLG_STOP;
+        root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL, 0);
+
+        /* Shut down the kernelcomms */
+        libcfs_ukuc_stop(&ct->kuc);
+
+        free(ct->fsname);
         free(ct);
         *priv = NULL;
         return 0;
@@ -2989,7 +3179,7 @@ int llapi_copytool_fini(void **priv)
 int llapi_copytool_recv(void *priv, struct hsm_action_list **halh, int *msgsize)
 {
         struct copytool_private *ct = (struct copytool_private *)priv;
-        struct lnl_hdr *lnlh;
+        struct kuc_hdr *kuch;
         struct hsm_action_list *hal;
         int rc = 0;
 
@@ -2998,64 +3188,107 @@ int llapi_copytool_recv(void *priv, struct hsm_action_list **halh, int *msgsize)
         if (halh == NULL || msgsize == NULL)
                 return -EINVAL;
 
-        rc = libcfs_ulnl_msg_get(&ct->lnl, HAL_MAXSIZE,
-                                 LNL_TRANSPORT_HSM, &lnlh);
+        kuch = malloc(HAL_MAXSIZE + sizeof(*kuch));
+        if (kuch == NULL)
+                return -ENOMEM;
+
+        rc = libcfs_ukuc_msg_get(&ct->kuc, (char *)kuch,
+                                 HAL_MAXSIZE + sizeof(*kuch),
+                                 KUC_TRANSPORT_HSM);
         if (rc < 0)
-                return rc;
+                goto out_free;
 
         /* Handle generic messages */
-        if (lnlh->lnl_transport == LNL_TRANSPORT_GENERIC &&
-            lnlh->lnl_msgtype == LNL_MSG_SHUTDOWN) {
+        if (kuch->kuc_transport == KUC_TRANSPORT_GENERIC &&
+            kuch->kuc_msgtype == KUC_MSG_SHUTDOWN) {
                 rc = -ESHUTDOWN;
                 goto out_free;
         }
 
-        if (lnlh->lnl_transport != LNL_TRANSPORT_HSM ||
-            lnlh->lnl_msgtype != HMT_ACTION_LIST) {
+        if (kuch->kuc_transport != KUC_TRANSPORT_HSM ||
+            kuch->kuc_msgtype != HMT_ACTION_LIST) {
                 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
                           "Unknown HSM message type %d:%d\n",
-                          lnlh->lnl_transport, lnlh->lnl_msgtype);
+                          kuch->kuc_transport, kuch->kuc_msgtype);
                 rc = -EPROTO;
                 goto out_free;
         }
 
-        /* Our message is an hsm_action_list */
-
-        hal = (struct hsm_action_list *)(lnlh + 1);
+        /* Our message is a hsm_action_list.  Use pointer math to skip
+         * kuch_hdr and point directly to the message payload.
+         */
+        hal = (struct hsm_action_list *)(kuch + 1);
 
         /* Check that we have registered for this archive # */
-        for (rc = 0; rc < ct->archive_num_count; rc++) {
-                if (hal->hal_archive_num == ct->archive_nums[rc])
-                        break;
-        }
-        if (rc >= ct->archive_num_count) {
-                CDEBUG(D_INFO, "This copytool does not service archive #%d, "
-                       "ignoring this request.\n", hal->hal_archive_num);
+        if (((1 << hal->hal_archive_num) & ct->archives) == 0) {
+                    llapi_err(LLAPI_MSG_INFO | LLAPI_MSG_NO_ERRNO,
+                          "Ignoring request for archive #%d (bitmask %#x)\n",
+                          hal->hal_archive_num, ct->archives);
                 rc = 0;
                 goto out_free;
         }
 
         *halh = hal;
-        *msgsize = lnlh->lnl_msglen - sizeof(*lnlh);
+        *msgsize = kuch->kuc_msglen - sizeof(*kuch);
         return 0;
 
 out_free:
-        libcfs_ulnl_msg_free(&lnlh);
         *halh = NULL;
         *msgsize = 0;
+        free(kuch);
         return rc;
 }
 
 /** Release the action list when done with it. */
 int llapi_copytool_free(struct hsm_action_list **hal)
 {
-        if (*hal) {
-                struct lnl_hdr *lnlh = (struct lnl_hdr *)*hal - 1;
-                libcfs_ulnl_msg_free(&lnlh);
+        /* Reuse the llapi_changelog_free function */
+        return llapi_changelog_free((struct changelog_rec **)hal);
+}
+
+int llapi_get_connect_flags(const char *mnt, __u64 *flags)
+{
+        DIR *root;
+        int rc;
+
+        root = opendir(mnt);
+        if (!root) {
+                llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
+                return -1;
         }
-        *hal = NULL;
-        return 0;
+
+        rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
+        closedir(root);
+        if (rc < 0)
+                llapi_err(LLAPI_MSG_ERROR,
+                          "ioctl on %s for getting connect flags failed", mnt);
+        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;
 
+        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);
 
+        rc = ioctl(fd, OBD_GET_VERSION, buffer);
+        if (rc == -1) {
+                rc = errno;
+                close(fd);
+                return -rc;
+        }
+        close(fd);
+        *version = data->ioc_bulk;
+        return 0;
+}