Whamcloud - gitweb
LU-19098 hsm: don't print progname twice with lhsmtool
[fs/lustre-release.git] / lustre / utils / liblustreapi_util.c
index ad69d93..311d185 100644 (file)
@@ -1,27 +1,11 @@
+// SPDX-License-Identifier: LGPL-2.1+
 /*
- * LGPL HEADER START
- *
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * (C) Copyright (c) 2015, Cray Inc, all rights reserved.
+ * Copyright (c) 2015, Cray Inc, all rights reserved.
  *
  * Copyright (c) 2016, 2017, Intel Corporation.
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the GNU Lesser General Public License
- * LGPL version 2.1 or (at your discretion) any later version.
- * LGPL version 2.1 accompanies this distribution, and is available at
- * http://www.gnu.org/licenses/lgpl-2.1.html
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * LGPL HEADER END
  */
 /*
- * lustre/utils/liblustreapi_util.c
+ * This file is part of Lustre, http://www.lustre.org/
  *
  * Misc LGPL-licenced utility functions for liblustreapi.
  *
@@ -161,16 +145,15 @@ int llapi_get_version_string(char *version, unsigned int version_size)
  */
 int llapi_get_version(char *buffer, int buffer_size, char **version)
 {
-       int rc;
-#if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 8, 53, 0)
        static bool printed;
+       int rc;
+
        if (!printed) {
                fprintf(stderr,
                        "%s deprecated, use llapi_get_version_string()\n",
                        __func__);
                printed = true;
        }
-#endif
 
        rc = llapi_get_version_string(buffer, buffer_size);
        /* keep old return style for this legacy function */
@@ -288,34 +271,74 @@ int llapi_search_ost(const char *fsname, const char *poolname,
        return llapi_search_tgt(fsname, poolname, ostname, false);
 }
 
+/**
+ * Return the open fd for a given device/path provided
+ *
+ * \param device[in]           buffer holding device or path string
+ * \param rootfd[out]          file descriptor after successful opening of
+ *                              of above path or device
+ *
+ * \retval                     0 on success
+ * \retval                     -ve on failure
+ */
+int llapi_root_path_open(const char *device, int *rootfd)
+{
+       int tmp_fd, rc;
+
+       if (*device == '/')
+               rc = get_root_path(WANT_FD, NULL, &tmp_fd,
+                                  (char *)device, -1, NULL, NULL);
+       else
+               rc = get_root_path(WANT_FD, (char *)device, &tmp_fd,
+                                  NULL, -1, NULL, NULL);
+
+       if (!rc)
+               *rootfd = dup(tmp_fd);
+
+       return rc;
+}
+
+/**
+ * Call IOCTL to remove file by fid. The fd must be valid and fa
+ * (fid_array) struct must allready be populated.
+ *
+ * \param fd[in]               valid descriptor of device/path
+ * \param fa[in]               fid_array struct holding fids
+ *
+ * \retval                     0 on success
+ * \retval                     -ve/errno on failure
+ */
+int llapi_rmfid_at(int fd, struct fid_array *fa)
+{
+       return ioctl(fd, LL_IOC_RMFID, fa) ? -errno : 0;
+}
+
 int llapi_rmfid(const char *path, struct fid_array *fa)
 {
-       char rootpath[PATH_MAX];
-       int fd, rc;
+       int rootfd, rc;
 
-retry_open:
-       fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
-       if (fd < 0) {
-               if (errno == ENOENT && path != rootpath) {
-                       rc = llapi_search_rootpath(rootpath, path);
-                       if (!rc) {
-                               path = rootpath;
-                               goto retry_open;
-                       }
-               } else {
-                       return -errno;
-               }
+       rc = llapi_root_path_open(path, &rootfd);
+       if (rc < 0) {
+               fprintf(stderr,
+                       "lfs rmfid: error opening device/fsname '%s': %s\n",
+                       path, strerror(-rc));
+               return -rc;
        }
 
-       rc = ioctl(fd, LL_IOC_RMFID, fa);
-       close(fd);
+       rc = llapi_rmfid_at(rootfd, fa);
+       close(rootfd);
+       if (rc < 0) {
+               fprintf(stderr, "lfs rmfid: cannot remove FIDs: %s\n",
+                       strerror(-rc));
+               return rc;
+       }
 
        return rc ? -errno : 0;
 }
 
 int llapi_direntry_remove(char *dname)
 {
-#ifdef HAVE_IOC_REMOVE_ENTRY
+#ifdef LL_IOC_REMOVE_ENTRY
        char *dirpath = NULL;
        char *namepath = NULL;
        char *dir;
@@ -324,10 +347,15 @@ int llapi_direntry_remove(char *dname)
        int rc = 0;
 
        dirpath = strdup(dname);
-       namepath = strdup(dname);
-       if (!dirpath || !namepath)
+       if (!dirpath)
                return -ENOMEM;
 
+       namepath = strdup(dname);
+       if (!namepath) {
+               rc = -ENOMEM;
+               goto out_dirpath;
+       }
+
        filename = basename(namepath);
 
        dir = dirname(dirpath);
@@ -344,14 +372,14 @@ int llapi_direntry_remove(char *dname)
                llapi_error(LLAPI_MSG_ERROR, errno,
                            "error on ioctl %#lx for '%s' (%d)",
                            (long)LL_IOC_LMV_SETSTRIPE, filename, fd);
+       close(fd);
 out:
-       free(dirpath);
        free(namepath);
-       if (fd != -1)
-               close(fd);
+out_dirpath:
+       free(dirpath);
        return rc;
 #else
-       return -ENOTSUP;
+       return -EOPNOTSUPP;
 #endif
 }