4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * (C) Copyright (c) 2015, Cray Inc, all rights reserved.
8 * Copyright (c) 2016, 2017, Intel Corporation.
10 * All rights reserved. This program and the accompanying materials
11 * are made available under the terms of the GNU Lesser General Public License
12 * LGPL version 2.1 or (at your discretion) any later version.
13 * LGPL version 2.1 accompanies this distribution, and is available at
14 * http://www.gnu.org/licenses/lgpl-2.1.html
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
24 * lustre/utils/liblustreapi_util.c
26 * Misc LGPL-licenced utility functions for liblustreapi.
28 * Author: Frank Zago <fzago@cray.com>
37 #include <sys/ioctl.h>
41 #include <sys/types.h>
43 #include <sys/syscall.h>
44 #include <lustre/lustreapi.h>
45 #include <linux/lustre/lustre_ver.h> /* only until LUSTRE_VERSION_CODE is gone */
46 #include "lustreapi_internal.h"
49 * Indicate whether the liblustreapi_init() constructor below has run or not.
51 * This can be used by external programs to ensure that the initialization
52 * mechanism has actually worked.
54 bool liblustreapi_initialized;
57 * Initialize the library once at startup.
59 * Initializes the random number generator (random()). Get
60 * data from different places in case one of them fails. This
61 * is enough to get reasonably random numbers, but is not
62 * strong enough to be used for cryptography.
64 static __attribute__ ((constructor)) void liblustreapi_init(void)
70 seed = syscall(SYS_gettid);
72 if (gettimeofday(&tv, NULL) == 0) {
77 fd = open("/dev/urandom", O_RDONLY | O_NOFOLLOW);
82 ret = read(fd, &rnumber, sizeof(rnumber));
83 seed ^= rnumber ^ ret;
88 liblustreapi_initialized = true;
92 * Return the release version for the Lustre modules, e.g. 2.6.92.
94 * The "version" file in /proc currently returns only the line:
97 * but in the past it also returned more lines that should be ignored:
98 * kernel: patchless_client
99 * build: v2_6_92_0-gadb3ee4-2.6.32-431.29.2.el6_lustre.g36cd22b.x86_64
101 * \param version[in,out] buffer to store build version string
102 * \param version_size[in] size of \a version
104 * \retval 0 on success
105 * \retval -1 on failure, errno set
107 int llapi_get_version_string(char *version, unsigned int version_size)
113 if (version == NULL || version_size == 0) {
118 rc = get_lustre_param_value(NULL, NULL, FILTER_BY_NONE, buffer,
119 "version", sizeof(buffer));
125 ptr = strstr(buffer, "lustre:");
127 ptr += strlen("lustre:");
128 while (*ptr == ' ' || *ptr == '\t')
133 llapi_chomp_string(ptr);
135 if (ptr[0] == '\0') {
140 if (snprintf(version, version_size, "%s", ptr) >= version_size) {
147 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 4, 53, 0)
149 * Return the build version of the Lustre code.
151 * The **version argument is pointless, so llapi_get_version_string() is
152 * better to use in the future, but give users a few versions to fix * it.
154 * \param buffer[in] temporary buffer to hold version string
155 * \param buffer_size[in] length of the \a buffer
156 * \param version[out] pointer to the start of build version string
158 * \retval 0 on success
159 * \retval -ve errno on failure
161 int llapi_get_version(char *buffer, int buffer_size, char **version)
164 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 8, 53, 0)
168 "%s deprecated, use llapi_get_version_string()\n",
174 rc = llapi_get_version_string(buffer, buffer_size);
175 /* keep old return style for this legacy function */
183 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 4, 53, 0) */
186 * fsname must be specified
187 * if poolname is NULL, search tgtname in fsname
188 * if poolname is not NULL:
189 * if poolname not found returns errno < 0
190 * if tgtname is NULL, returns 1 if pool is not empty and 0 if pool empty
191 * if tgtname is not NULL, returns 1 if target is in pool and 0 if not
193 int llapi_search_tgt(const char *fsname, const char *poolname,
194 const char *tgtname, bool is_mdt)
196 char buffer[PATH_MAX];
202 if (fsname && fsname[0] == '\0')
209 if (poolname && poolname[0] == '\0')
212 if (tgtname[0] == '\0')
215 len = strlen(tgtname);
218 /* You need one or the other to have something in it */
219 if (!poolname && !tgtname) {
225 rc = poolpath(¶m, fsname, NULL);
227 snprintf(buffer, sizeof(buffer) - 1, "%s/%s",
228 param.gl_pathv[0], poolname);
229 buffer[sizeof(buffer) - 1] = '\0';
232 rc = get_lustre_param_path(is_mdt ? "lmv" : "lov", fsname,
234 "target_obd", ¶m);
236 strncpy(buffer, param.gl_pathv[0],
238 buffer[sizeof(buffer) - 1] = '\0';
241 cfs_free_param_data(¶m);
245 fd = fopen(buffer, "r");
251 while (fgets(buffer, sizeof(buffer), fd)) {
254 /* Search for an tgtname in the list of all targets
255 * Line format is IDX: fsname-OST/MDTxxxx_UUID STATUS */
256 ptr = strchr(buffer, ' ');
257 if (ptr && strncmp(ptr + 1, tgtname, len) == 0) {
262 /* Search for an tgtname in a pool,
263 * (or an existing non-empty pool if no tgtname) */
264 if (!tgtname || strncmp(buffer, tgtname, len) == 0) {
278 int llapi_search_mdt(const char *fsname, const char *poolname,
281 return llapi_search_tgt(fsname, poolname, mdtname, true);
284 int llapi_search_ost(const char *fsname, const char *poolname,
287 return llapi_search_tgt(fsname, poolname, ostname, false);
290 int llapi_rmfid(const char *path, struct fid_array *fa)
292 char rootpath[PATH_MAX];
296 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
298 if (errno == ENOENT && path != rootpath) {
299 rc = llapi_search_rootpath(rootpath, path);
309 rc = ioctl(fd, LL_IOC_RMFID, fa);
312 return rc ? -errno : 0;
315 int llapi_get_fsname_instance(const char *path, char *fsname, size_t fsname_len,
316 char *instance, size_t instance_len)
318 struct obd_uuid uuid_buf;
319 char *uuid = uuid_buf.uuid;
323 memset(&uuid_buf, 0, sizeof(uuid_buf));
324 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
329 * We want to turn fs-foo-clilov-ffff88002738bc00 into 'fs-foo' and
330 * 'ffff88002738bc00' in a portable way that doesn't depend on what is
331 * after "-clilov-" as it may change to a UUID string in the future.
332 * Unfortunately, the "fsname" part may contain a dash, so we can't
333 * just skip to the first dash, and if the "instance" is a UUID in the
334 * future we can't necessarily go to the last dash either.
336 ptr = strstr(uuid, "-clilov-");
337 if (!ptr || (!fsname && !instance)) {
343 ptr += strlen("-clilov-");
345 snprintf(instance, instance_len, "%s", ptr);
346 if (strlen(ptr) >= instance_len)
351 snprintf(fsname, fsname_len, "%s", uuid);
352 if (strlen(uuid) >= fsname_len)
361 int llapi_getname(const char *path, char *name, size_t namelen)
367 rc = llapi_get_fsname_instance(path, fsname, sizeof(fsname),
368 instance, sizeof(instance));
372 snprintf(name, namelen, "%s-%s", fsname, instance);
373 if (strlen(fsname) + 1 + strlen(instance) >= namelen) {
381 int llapi_get_instance(const char *path, char *instance, size_t instance_len)
383 return llapi_get_fsname_instance(path, NULL, 0, instance, instance_len);
386 int llapi_get_fsname(const char *path, char *fsname, size_t fsname_len)
388 return llapi_get_fsname_instance(path, fsname, fsname_len, NULL, 0);