Whamcloud - gitweb
b=20101 a fix for lfs getstripe --pool
[fs/lustre-release.git] / lustre / utils / liblustreapi.c
index 96d31e6..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.
  */
 /*
@@ -520,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) {
@@ -528,9 +531,6 @@ 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, if given */
                 if (!(want & WANT_FSNAME) && fsname != NULL &&
                     (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
@@ -592,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
@@ -603,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 */
@@ -1194,13 +1206,18 @@ int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
  */
 int llapi_uuid_match(char *real_uuid, char *search_uuid)
 {
-        int cmplen = strlen(real_uuid) - 5;
+        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;
 
-        if ((strlen(search_uuid) > cmplen) && isxdigit(search_uuid[cmplen])) {
-                /* OST00000003 doesn't match OST0000 */
-                llapi_err(LLAPI_MSG_ERROR, "Bad UUID format '%s'", search_uuid);
+        /* 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);
 }
@@ -1340,31 +1357,31 @@ retry_get_uuids:
         return ret;
 }
 
-static int cb_ostlist(char *path, DIR *parent, DIR *d, void *data,
-                      struct dirent64 *de)
+int llapi_ostlist(char *path, struct find_param *param)
 {
-        struct find_param *param = (struct find_param *)data;
+        DIR *dir;
+        int ret;
 
-        LASSERT(parent != NULL || d != NULL);
+        dir = opendir(path);
+        if (dir == NULL)
+                return -errno;
 
-        /* Prepare odb. */
-        return setup_obd_uuid(d ? d : parent, path, param);
-}
+        ret = setup_obd_uuid(dir, path, param);
+        closedir(dir);
 
-int llapi_ostlist(char *path, struct find_param *param)
-{
-        return param_callback(path, cb_ostlist, cb_common_fini, param);
+        return ret;
 }
 
 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,
                                      char *pool_name)
 {
         char *prefix = is_dir ? "" : "lmm_";
         char nl = is_dir ? ' ' : '\n';
 
-        if (is_dir && lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
-                lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
+        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) ");
         }
@@ -1375,8 +1392,8 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *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);
         }
@@ -1406,13 +1423,21 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
                 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", pool_name);
-                is_dir = 1;
+                if (verbose & ~VERBOSE_POOL)
+                        llapi_printf(LLAPI_MSG_NORMAL, "%spool:           ",
+                                     prefix);
+                llapi_printf(LLAPI_MSG_NORMAL, "%s%c", pool_name, nl);
         }
 
         if (is_dir && (verbose != VERBOSE_OBJID))
@@ -1436,7 +1461,7 @@ void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
         }
 
         if (obdstripe == 1)
-                lov_dump_user_lmm_header(lum, path, is_dir, header, depth,
+                lov_dump_user_lmm_header(lum, path, objects, is_dir, header, depth,
                                          pool_name);
 
         if (!is_dir && (header & VERBOSE_OBJID)) {
@@ -1447,7 +1472,7 @@ void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
                 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",
@@ -1580,28 +1605,26 @@ int llapi_file_lookup(int dirfd, const char *name)
  * 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).
@@ -1619,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;
@@ -1627,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;
 
@@ -1639,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;
 
@@ -1692,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;
@@ -1802,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;
+                        }
                 }
         }
 
@@ -1866,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
@@ -1919,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) {
@@ -2708,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)
 {
@@ -2733,12 +2768,71 @@ 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 flags;
-        lustre_netlink lnl;
+        lustre_kernelcomm kuc;
 };
 
 /** Start reading from a changelog
@@ -2752,75 +2846,39 @@ int llapi_changelog_start(void **priv, int flags, const char *device,
                           long long startrec)
 {
         struct changelog_private *cp;
-        struct changelog_show cs = {};
-        char mdtname[20];
-        char pattern[PATH_MAX];
-        char trigger[PATH_MAX];
-        int fd, rc, pid;
-
-        /* Find mdtname from path, fsname, mdtname, or mdtname_UUID */
-        if (device[0] == '/') {
-                if ((rc = llapi_search_fsname(device, mdtname)))
-                        return rc;
-                if ((rc = get_mdtname(mdtname, "%s%s", mdtname)) < 0)
-                        return rc;
-        } else {
-                if ((rc = get_mdtname((char *)device, "%s%s", mdtname)) < 0)
-                        return rc;
-        }
-
-        /* Find corresponding mdc trigger */
-        snprintf(pattern, PATH_MAX,
-                 "/proc/fs/lustre/mdc/%s-*/changelog_trigger", mdtname);
-        rc = first_match(pattern, trigger);
-        if (rc)
-                return rc;
-
-        /* Make sure we can write the trigger */
-        fd = open(trigger, O_WRONLY);
-        if (fd < 0)
-                return -errno;
+        int rc;
 
         /* Set up the receiver control struct */
-        cp = malloc(sizeof(*cp));
-        if (cp == NULL) {
-                close(fd);
+        cp = calloc(1, sizeof(*cp));
+        if (cp == NULL)
                 return -ENOMEM;
-        }
 
         cp->magic = CHANGELOG_PRIV_MAGIC;
         cp->flags = flags;
-        /* Start the receiver */
-        rc = libcfs_ulnl_start(&cp->lnl, 0 /* unicast */);
+
+        /* Set up the receiver */
+        rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */);
         if (rc < 0)
                 goto out_free;
 
-        /* We need to trigger Lustre to start sending messages now.
-           We could send a lnl message to a kernel listener,
-           or write into proc.  Proc has the advantage of running in this
-           context, avoiding the need for a kernel thread. */
-        cs.cs_pid = getpid();
-        cs.cs_startrec = startrec;
-        cs.cs_flags = flags & CHANGELOG_FLAG_BLOCK ? LNL_FL_BLOCK : 0;
-        if ((pid = fork()) < 0) {
-                goto out_free;
-        } else if (!pid) {
-                /* Write triggers Lustre to start sending, but it
-                   won't return until it is complete, meaning everything
-                   got shipped through lnl (or error).  So we trigger it
-                   from a child process here, allowing the llapi call to
-                   return and wait for the lnl messages. */
-                rc = write(fd, &cs, sizeof(cs));
-                exit(rc);
+        *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;
         }
 
-        close(fd);
-        *priv = cp;
         return 0;
 
 out_free:
         free(cp);
-        close(fd);
         return rc;
 }
 
@@ -2832,7 +2890,7 @@ int llapi_changelog_fini(void **priv)
         if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
                 return -EINVAL;
 
-        libcfs_ulnl_stop(&cp->lnl);
+        libcfs_ukuc_stop(&cp->kuc);
         free(cp);
         *priv = NULL;
         return 0;
@@ -2848,31 +2906,35 @@ int llapi_changelog_fini(void **priv)
 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
 {
         struct changelog_private *cp = (struct changelog_private *)priv;
-        struct lnl_hdr *lnlh;
+        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;
 
 repeat:
-        rc = libcfs_ulnl_msg_get(&cp->lnl, CR_MAXSIZE, LNL_TRANSPORT_CHANGELOG,
-                                 &lnlh);
+        rc = libcfs_ukuc_msg_get(&cp->kuc, (char *)kuch,
+                                 CR_MAXSIZE + sizeof(*kuch),
+                                 KUC_TRANSPORT_CHANGELOG);
         if (rc < 0)
-                return rc;
+                goto out_free;
 
-        if ((lnlh->lnl_transport != LNL_TRANSPORT_CHANGELOG) ||
-            ((lnlh->lnl_msgtype != CL_RECORD) &&
-             (lnlh->lnl_msgtype != CL_EOF))) {
+        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",
-                          lnlh->lnl_transport, lnlh->lnl_msgtype);
+                          kuch->kuc_transport, kuch->kuc_msgtype);
                 rc = -EPROTO;
                 goto out_free;
         }
 
-        if (lnlh->lnl_msgtype == CL_EOF) {
+        if (kuch->kuc_msgtype == CL_EOF) {
                 if (cp->flags & CHANGELOG_FLAG_FOLLOW) {
                         /* Ignore EOFs */
                         goto repeat;
@@ -2882,14 +2944,16 @@ repeat:
                 }
         }
 
-        /* Our message is a changelog_rec */
-        *rech = (struct changelog_rec *)(lnlh + 1);
+        /* 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:
-        libcfs_ulnl_msg_free(&lnlh);
         *rech = NULL;
+        free(kuch);
         return rc;
 }
 
@@ -2897,8 +2961,12 @@ out_free:
 int llapi_changelog_free(struct changelog_rec **rech)
 {
         if (*rech) {
-                struct lnl_hdr *lnlh = (struct lnl_hdr *)*rech - 1;
-                libcfs_ulnl_msg_free(&lnlh);
+                /* 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;
@@ -2907,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,
@@ -2927,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++;
@@ -2976,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 {
@@ -3005,7 +3032,6 @@ int llapi_fid2path(const char *device, const char *fidstr, char *buf,
         }
 
         free(gf);
-        close(fd);
         return rc;
 }
 
@@ -3047,43 +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_num_count
- * @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;
 
@@ -3091,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;
 }
@@ -3103,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;
@@ -3119,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;
 
@@ -3128,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;
+}