From: Andreas Dilger Date: Fri, 7 Jun 2019 03:39:36 +0000 (-0600) Subject: LU-11264 llapi: clean up llapi_search_tgt() code X-Git-Tag: 2.12.56~29 X-Git-Url: https://git.whamcloud.com/?p=fs%2Flustre-release.git;a=commitdiff_plain;h=096db80e0810b4abb8e268a58623c23bf0b57d2c LU-11264 llapi: clean up llapi_search_tgt() code 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 Change-Id: Ieb2e93208fbc1b1492f632d8ce1383ca9fdec5f2 Reviewed-on: https://review.whamcloud.com/35092 Tested-by: jenkins Reviewed-by: Olaf Faaland-LLNL Tested-by: Maloo Reviewed-by: Arshad Hussain Reviewed-by: Emoly Liu Reviewed-by: Oleg Drokin --- diff --git a/lustre/doc/llapi_search_mdt.3 b/lustre/doc/llapi_search_mdt.3 new file mode 100644 index 0000000..698f95f --- /dev/null +++ b/lustre/doc/llapi_search_mdt.3 @@ -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 index 0000000..698f95f --- /dev/null +++ b/lustre/doc/llapi_search_ost.3 @@ -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 index 0000000..3647e25 --- /dev/null +++ b/lustre/doc/llapi_search_tgt.3 @@ -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 +.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) diff --git a/lustre/doc/lustreapi.7 b/lustre/doc/lustreapi.7 index 261c024..78f13e6 100644 --- a/lustre/doc/lustreapi.7 +++ b/lustre/doc/lustreapi.7 @@ -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 diff --git a/lustre/include/lustre/lustreapi.h b/lustre/include/lustre/lustreapi.h index 420b606..72358b9 100644 --- a/lustre/include/lustre/lustreapi.h +++ b/lustre/include/lustre/lustreapi.h @@ -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); diff --git a/lustre/utils/liblustreapi.c b/lustre/utils/liblustreapi.c index 3d305f0..d84bbe2 100644 --- a/lustre/utils/liblustreapi.c +++ b/lustre/utils/liblustreapi.c @@ -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(¶m, 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", ¶m); - if (rc == 0) { - strncpy(buffer, param.gl_pathv[0], - sizeof(buffer) - 1); - } - } else { - return -EINVAL; - } - cfs_free_param_data(¶m); - 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. * diff --git a/lustre/utils/liblustreapi_util.c b/lustre/utils/liblustreapi_util.c index f0f1991..d206b02 100644 --- a/lustre/utils/liblustreapi_util.c +++ b/lustre/utils/liblustreapi_util.c @@ -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(¶m, 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", ¶m); + if (!rc) { + strncpy(buffer, param.gl_pathv[0], + sizeof(buffer) - 1); + buffer[sizeof(buffer) - 1] = '\0'; + } + } + cfs_free_param_data(¶m); + 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); +} +