Whamcloud - gitweb
LU-11264 llapi: clean up llapi_search_tgt() code 92/35092/5
authorAndreas Dilger <adilger@whamcloud.com>
Fri, 7 Jun 2019 03:39:36 +0000 (21:39 -0600)
committerOleg Drokin <green@whamcloud.com>
Fri, 12 Jul 2019 05:19:31 +0000 (05:19 +0000)
Clean up llapi_search_tgt() and helper functions llapi_search_ost()
and llapi_search_mdt() to set errno on return.

Add man pages for all of these functions.

Signed-off-by: Andreas Dilger <adilger@whamcloud.com>
Change-Id: Ieb2e93208fbc1b1492f632d8ce1383ca9fdec5f2
Reviewed-on: https://review.whamcloud.com/35092
Tested-by: jenkins <devops@whamcloud.com>
Reviewed-by: Olaf Faaland-LLNL <faaland1@llnl.gov>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Arshad Hussain <arshad.super@gmail.com>
Reviewed-by: Emoly Liu <emoly@whamcloud.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
lustre/doc/llapi_search_mdt.3 [new file with mode: 0644]
lustre/doc/llapi_search_ost.3 [new file with mode: 0644]
lustre/doc/llapi_search_tgt.3 [new file with mode: 0644]
lustre/doc/lustreapi.7
lustre/include/lustre/lustreapi.h
lustre/utils/liblustreapi.c
lustre/utils/liblustreapi_util.c

diff --git a/lustre/doc/llapi_search_mdt.3 b/lustre/doc/llapi_search_mdt.3
new file mode 100644 (file)
index 0000000..698f95f
--- /dev/null
@@ -0,0 +1 @@
+.so man3/llapi_search_tgt.3
diff --git a/lustre/doc/llapi_search_ost.3 b/lustre/doc/llapi_search_ost.3
new file mode 100644 (file)
index 0000000..698f95f
--- /dev/null
@@ -0,0 +1 @@
+.so man3/llapi_search_tgt.3
diff --git a/lustre/doc/llapi_search_tgt.3 b/lustre/doc/llapi_search_tgt.3
new file mode 100644 (file)
index 0000000..3647e25
--- /dev/null
@@ -0,0 +1,96 @@
+.TH llapi_search_tgt 3 "2019 Jun 06" "Lustre User API"
+.SH NAME
+llapi_search_tgt \- verify OST or MDT is in specified pool
+.SH SYNOPSIS
+.nf
+.B #include <lustre/lustreapi.h>
+.PP
+.IB int " llapi_search_tgt(" "const char *fsname" ", " "const char *pool_name" ,
+.BI                             "const char *tgt_name" ", " "bool is_mdt" );
+.PP
+.IB int " llapi_search_mdt(" "const char *fsname" ", " "const char *pool_name" ,
+.BI                             "const char *tgt_name" );
+.PP
+.IB int " llapi_search_ost(" "const char *fsname" ", " "const char *pool_name" ,
+.BI                             "const char *tgt_name" );
+.SH DESCRIPTION
+.PP
+.B llapi_search_tgt()
+verifies that the specified
+.I tgt_name
+is a valid MDT or OST (depending on whether
+.I is_mdt
+is
+.B true
+or
+.BR false ,
+respectively) in the filesystem named
+.I fsname
+and (if specified) in the pool named
+.I pool_name
+of that filesystem.  At least
+.I fsname
+must be non-NULL, but
+.I pool_name
+may be
+.B NULL
+if just checking for the existence of the target, and
+.I tgt_name
+may be
+.BR NULL ,
+if only checking the existence of
+.IR pool_name .
+.PP
+The
+.B llapi_search_mdt()
+and
+.B llapi_search_ost()
+functions are simple convenience/compatibility wrappers of
+.B llapi_search_tgt()
+that set
+.I is_mdt
+appropriately for the target type.
+.SH RETURN VALUES
+.LP
+.B llapi_search_tgt()
+returns 1 if
+.I tgt_name
+(or
+.I pool_name
+if
+.I tgt_name
+is
+.BR NULL )
+is found in the specified pool
+(or
+.I filesystem
+if
+.I pool_name
+is
+.BR NULL ).
+It returns 0 if
+.I tgt_name
+.RI ( pool_name )
+if it is not found in the specified pool
+.RI ( filesystem ).
+A negative errno is returned if an error occurred, in which case errno
+is also set appropriately.
+.SH ERRORS
+.TP 15
+.SM EINVAL
+.I fsname
+is NULL or zero-length, or both
+.I pool_name
+and
+.I tgt_name
+are
+.BR NULL .
+.TP 15
+.SM ENOENT
+.I pool_name
+is specified but is not part of
+.IR fsname .
+.SH "SEE ALSO"
+.BR llapi_pool_name_verify (3),
+.BR lustreapi (7),
+.BR lustre (7)
index 261c024..78f13e6 100644 (file)
@@ -61,6 +61,7 @@ quotas, file layouts, etc).  See the referenced man pages for details.
 .BR llapi_path2fid (3),
 .BR llapi_path2parent (3),
 .BR llapi_quotactl (3),
+.BR llapi_search_tgt (3),
 .BR lustre (7)
 .SH AUTHOR
 Lustre contributors
index 420b606..72358b9 100644 (file)
@@ -362,8 +362,12 @@ int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_uuid);
 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count);
 int llapi_lmv_get_uuids(int fd, struct obd_uuid *uuidp, int *mdt_count);
 int llapi_is_lustre_mnttype(const char *type);
-int llapi_search_tgt(char *fsname, char *poolname, char *tgtname, bool is_mdt);
-int llapi_search_ost(char *fsname, char *poolname, char *ostname);
+int llapi_search_tgt(const char *fsname, const char *poolname,
+                    const char *tgtname, bool is_mdt);
+int llapi_search_mdt(const char *fsname, const char *poolname,
+                    const char *mdtname);
+int llapi_search_ost(const char *fsname, const char *poolname,
+                    const char *ostname);
 int llapi_get_obd_count(char *mnt, int *count, int is_mdt);
 int llapi_parse_size(const char *optarg, unsigned long long *size,
                     unsigned long long *size_units, int bytes_spec);
index 3d305f0..d84bbe2 100644 (file)
@@ -539,87 +539,6 @@ int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
        return get_param_lmv(path, "uuid", buf, bufsize);
 }
 
-/*
- * if pool is NULL, search tgtname in target_obd
- * if pool is not NULL:
- *  if pool not found returns errno < 0
- *  if tgtname is NULL, returns 1 if pool is not empty and 0 if pool empty
- *  if tgtname is not NULL, returns 1 if OST is in pool and 0 if not
- */
-int llapi_search_tgt(char *fsname, char *poolname, char *tgtname, bool is_mdt)
-{
-       char buffer[PATH_MAX];
-       size_t len = 0;
-       glob_t param;
-       FILE *fd;
-       int rc;
-
-       /* You need one or the other */
-       if (poolname == NULL && fsname == NULL)
-               return -EINVAL;
-
-       if (tgtname != NULL)
-               len = strlen(tgtname);
-
-       if (poolname == NULL && len == 0)
-               return -EINVAL;
-
-       /* Search by poolname and fsname if is not NULL */
-       if (poolname != NULL) {
-               rc = poolpath(&param, fsname, NULL);
-               if (rc == 0) {
-                       snprintf(buffer, sizeof(buffer), "%s/%s",
-                                param.gl_pathv[0], poolname);
-               }
-       } else if (fsname != NULL) {
-               rc = get_lustre_param_path(is_mdt ? "lmv" : "lov", fsname,
-                                          FILTER_BY_FS_NAME,
-                                          "target_obd", &param);
-               if (rc == 0) {
-                       strncpy(buffer, param.gl_pathv[0],
-                               sizeof(buffer) - 1);
-               }
-       } else {
-               return -EINVAL;
-       }
-       cfs_free_param_data(&param);
-       if (rc)
-               return rc;
-
-       fd = fopen(buffer, "r");
-       if (fd == NULL)
-               return -errno;
-
-       while (fgets(buffer, sizeof(buffer), fd) != NULL) {
-               if (poolname == NULL) {
-                       char *ptr;
-                       /* Search for an tgtname in the list of targets
-                        * Line format is IDX: fsname-OST/MDTxxxx_UUID STATUS */
-                       ptr = strchr(buffer, ' ');
-                       if ((ptr != NULL) &&
-                           (strncmp(ptr + 1, tgtname, len) == 0)) {
-                               fclose(fd);
-                               return 1;
-                       }
-               } else {
-                       /* Search for an tgtname in a pool,
-                        * (or an existing non-empty pool if no tgtname) */
-                       if ((tgtname == NULL) ||
-                           (strncmp(buffer, tgtname, len) == 0)) {
-                               fclose(fd);
-                               return 1;
-                       }
-               }
-       }
-       fclose(fd);
-       return 0;
-}
-
-int llapi_search_ost(char *fsname, char *poolname, char *ostname)
-{
-       return llapi_search_tgt(fsname, poolname, ostname, false);
-}
-
 /**
  * Open a Lustre file.
  *
index f0f1991..d206b02 100644 (file)
@@ -180,3 +180,109 @@ int llapi_get_version(char *buffer, int buffer_size, char **version)
        return rc;
 }
 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 4, 53, 0) */
+
+/*
+ * fsname must be specified
+ * if poolname is NULL, search tgtname in fsname
+ * if poolname is not NULL:
+ *  if poolname not found returns errno < 0
+ *  if tgtname is NULL, returns 1 if pool is not empty and 0 if pool empty
+ *  if tgtname is not NULL, returns 1 if target is in pool and 0 if not
+ */
+int llapi_search_tgt(const char *fsname, const char *poolname,
+                    const char *tgtname, bool is_mdt)
+{
+       char buffer[PATH_MAX];
+       size_t len = 0;
+       glob_t param;
+       FILE *fd;
+       int rc;
+
+       if (fsname && fsname[0] == '\0')
+               fsname = NULL;
+       if (!fsname) {
+               rc = -EINVAL;
+               goto out;
+       }
+
+       if (poolname && poolname[0] == '\0')
+               poolname = NULL;
+       if (tgtname) {
+               if (tgtname[0] == '\0')
+                       tgtname = NULL;
+               else
+                       len = strlen(tgtname);
+       }
+
+       /* You need one or the other to have something in it */
+       if (!poolname && !tgtname) {
+               rc = -EINVAL;
+               goto out;
+       }
+
+       if (poolname) {
+               rc = poolpath(&param, fsname, NULL);
+               if (!rc) {
+                       snprintf(buffer, sizeof(buffer) - 1, "%s/%s",
+                                param.gl_pathv[0], poolname);
+                       buffer[sizeof(buffer) - 1] = '\0';
+               }
+       } else {
+               rc = get_lustre_param_path(is_mdt ? "lmv" : "lov", fsname,
+                                          FILTER_BY_FS_NAME,
+                                          "target_obd", &param);
+               if (!rc) {
+                       strncpy(buffer, param.gl_pathv[0],
+                               sizeof(buffer) - 1);
+                       buffer[sizeof(buffer) - 1] = '\0';
+               }
+       }
+       cfs_free_param_data(&param);
+       if (rc)
+               goto out;
+
+       fd = fopen(buffer, "r");
+       if (!fd) {
+               rc = -errno;
+               goto out;
+       }
+
+       while (fgets(buffer, sizeof(buffer), fd)) {
+               if (!poolname) {
+                       char *ptr;
+                       /* Search for an tgtname in the list of all targets
+                        * Line format is IDX: fsname-OST/MDTxxxx_UUID STATUS */
+                       ptr = strchr(buffer, ' ');
+                       if (ptr && strncmp(ptr + 1, tgtname, len) == 0) {
+                               rc = 1;
+                               goto out_close;
+                       }
+               } else {
+                       /* Search for an tgtname in a pool,
+                        * (or an existing non-empty pool if no tgtname) */
+                       if (!tgtname || strncmp(buffer, tgtname, len) == 0) {
+                               rc = 1;
+                               goto out_close;
+                       }
+               }
+       }
+out_close:
+       fclose(fd);
+out:
+       if (rc < 0)
+               errno = -rc;
+       return rc;
+}
+
+int llapi_search_mdt(const char *fsname, const char *poolname,
+                    const char *mdtname)
+{
+       return llapi_search_tgt(fsname, poolname, mdtname, true);
+}
+
+int llapi_search_ost(const char *fsname, const char *poolname,
+                    const char *ostname)
+{
+       return llapi_search_tgt(fsname, poolname, ostname, false);
+}
+