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 <libgen.h> /* for dirname() */
45 #include <lustre/lustreapi.h>
46 #include <linux/lustre/lustre_ver.h> /* only until LUSTRE_VERSION_CODE is gone */
47 #include "lustreapi_internal.h"
50 * Indicate whether the liblustreapi_init() constructor below has run or not.
52 * This can be used by external programs to ensure that the initialization
53 * mechanism has actually worked.
55 bool liblustreapi_initialized;
58 * Initialize the library once at startup.
60 * Initializes the random number generator (random()). Get
61 * data from different places in case one of them fails. This
62 * is enough to get reasonably random numbers, but is not
63 * strong enough to be used for cryptography.
65 static __attribute__ ((constructor)) void liblustreapi_init(void)
71 seed = syscall(SYS_gettid);
73 if (gettimeofday(&tv, NULL) == 0) {
78 fd = open("/dev/urandom", O_RDONLY | O_NOFOLLOW);
83 ret = read(fd, &rnumber, sizeof(rnumber));
84 seed ^= rnumber ^ ret;
89 liblustreapi_initialized = true;
93 * Return the release version for the Lustre modules, e.g. 2.6.92.
95 * The "version" file in /proc currently returns only the line:
98 * but in the past it also returned more lines that should be ignored:
99 * kernel: patchless_client
100 * build: v2_6_92_0-gadb3ee4-2.6.32-431.29.2.el6_lustre.g36cd22b.x86_64
102 * \param version[in,out] buffer to store build version string
103 * \param version_size[in] size of \a version
105 * \retval 0 on success
106 * \retval -1 on failure, errno set
108 int llapi_get_version_string(char *version, unsigned int version_size)
114 if (version == NULL || version_size == 0) {
119 rc = get_lustre_param_value(NULL, NULL, FILTER_BY_NONE, "version",
120 buffer, sizeof(buffer));
126 ptr = strstr(buffer, "lustre:");
128 ptr += strlen("lustre:");
129 while (*ptr == ' ' || *ptr == '\t')
134 llapi_chomp_string(ptr);
136 if (ptr[0] == '\0') {
141 if (snprintf(version, version_size, "%s", ptr) >= version_size) {
148 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 4, 53, 0)
150 * Return the build version of the Lustre code.
152 * The **version argument is pointless, so llapi_get_version_string() is
153 * better to use in the future, but give users a few versions to fix * it.
155 * \param buffer[in] temporary buffer to hold version string
156 * \param buffer_size[in] length of the \a buffer
157 * \param version[out] pointer to the start of build version string
159 * \retval 0 on success
160 * \retval -ve errno on failure
162 int llapi_get_version(char *buffer, int buffer_size, char **version)
165 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 8, 53, 0)
169 "%s deprecated, use llapi_get_version_string()\n",
175 rc = llapi_get_version_string(buffer, buffer_size);
176 /* keep old return style for this legacy function */
184 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 4, 53, 0) */
187 * fsname must be specified
188 * if poolname is NULL, search tgtname in fsname
189 * if poolname is not NULL:
190 * if poolname not found returns errno < 0
191 * if tgtname is NULL, returns 1 if pool is not empty and 0 if pool empty
192 * if tgtname is not NULL, returns 1 if target is in pool and 0 if not
194 int llapi_search_tgt(const char *fsname, const char *poolname,
195 const char *tgtname, bool is_mdt)
197 char buffer[PATH_MAX];
203 if (fsname && fsname[0] == '\0')
210 if (poolname && poolname[0] == '\0')
213 if (tgtname[0] == '\0')
216 len = strlen(tgtname);
219 /* You need one or the other to have something in it */
220 if (!poolname && !tgtname) {
226 rc = poolpath(¶m, fsname, NULL);
228 snprintf(buffer, sizeof(buffer) - 1, "%s/%s",
229 param.gl_pathv[0], poolname);
230 buffer[sizeof(buffer) - 1] = '\0';
233 rc = get_lustre_param_path(is_mdt ? "lmv" : "lov", fsname,
235 "target_obd", ¶m);
237 strncpy(buffer, param.gl_pathv[0],
239 buffer[sizeof(buffer) - 1] = '\0';
242 cfs_free_param_data(¶m);
246 fd = fopen(buffer, "r");
252 while (fgets(buffer, sizeof(buffer), fd)) {
255 /* Search for an tgtname in the list of all targets
256 * Line format is IDX: fsname-OST/MDTxxxx_UUID STATUS */
257 ptr = strchr(buffer, ' ');
258 if (ptr && strncmp(ptr + 1, tgtname, len) == 0) {
263 /* Search for an tgtname in a pool,
264 * (or an existing non-empty pool if no tgtname) */
265 if (!tgtname || strncmp(buffer, tgtname, len) == 0) {
279 int llapi_search_mdt(const char *fsname, const char *poolname,
282 return llapi_search_tgt(fsname, poolname, mdtname, true);
285 int llapi_search_ost(const char *fsname, const char *poolname,
288 return llapi_search_tgt(fsname, poolname, ostname, false);
291 int llapi_rmfid(const char *path, struct fid_array *fa)
293 char rootpath[PATH_MAX];
297 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
299 if (errno == ENOENT && path != rootpath) {
300 rc = llapi_search_rootpath(rootpath, path);
310 rc = ioctl(fd, LL_IOC_RMFID, fa);
313 return rc ? -errno : 0;
316 int llapi_direntry_remove(char *dname)
318 #ifdef HAVE_IOC_REMOVE_ENTRY
319 char *dirpath = NULL;
320 char *namepath = NULL;
326 dirpath = strdup(dname);
327 namepath = strdup(dname);
328 if (!dirpath || !namepath)
331 filename = basename(namepath);
333 dir = dirname(dirpath);
335 fd = open(dir, O_DIRECTORY | O_RDONLY);
338 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
343 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename))
344 llapi_error(LLAPI_MSG_ERROR, errno,
345 "error on ioctl %#lx for '%s' (%d)",
346 (long)LL_IOC_LMV_SETSTRIPE, filename, fd);
358 int llapi_unlink_foreign(char *name)
363 fd = open(name, O_DIRECTORY | O_RDONLY | O_NOFOLLOW);
364 if (fd < 0 && errno != ENOTDIR) {
366 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
368 } else if (errno == ENOTDIR) {
369 fd = open(name, O_RDONLY | O_NOFOLLOW);
372 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
378 /* allow foreign symlink file/dir to be unlinked */
379 if (ioctl(fd, LL_IOC_UNLOCK_FOREIGN)) {
380 llapi_error(LLAPI_MSG_ERROR, errno,
381 "error on ioctl %#lx for '%s' (%d)",
382 (long)LL_IOC_UNLOCK_FOREIGN, name, fd);
386 /* XXX do not set AT_REMOVEDIR in flags even for a dir, as due to the
387 * hack for foreign symlink it will fail the directory check in
388 * Kernel's syscall code and return ENOTDIR, so treat all as files
390 rc = unlinkat(AT_FDCWD, name, 0);
391 if (rc == -1 && errno == EISDIR)
392 rc = unlinkat(AT_FDCWD, name, AT_REMOVEDIR);
395 llapi_error(LLAPI_MSG_ERROR, errno,
396 "error on unlinkat for '%s' (%d)", name, fd);
406 int llapi_get_fsname_instance(const char *path, char *fsname, size_t fsname_len,
407 char *instance, size_t instance_len)
409 struct obd_uuid uuid_buf;
410 char *uuid = uuid_buf.uuid;
414 memset(&uuid_buf, 0, sizeof(uuid_buf));
415 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
420 * We want to turn fs-foo-clilov-ffff88002738bc00 into 'fs-foo' and
421 * 'ffff88002738bc00' in a portable way that doesn't depend on what is
422 * after "-clilov-" as it may change to a UUID string in the future.
423 * Unfortunately, the "fsname" part may contain a dash, so we can't
424 * just skip to the first dash, and if the "instance" is a UUID in the
425 * future we can't necessarily go to the last dash either.
427 ptr = strstr(uuid, "-clilov-");
428 if (!ptr || (!fsname && !instance)) {
434 ptr += strlen("-clilov-");
436 snprintf(instance, instance_len, "%s", ptr);
437 if (strlen(ptr) >= instance_len)
442 snprintf(fsname, fsname_len, "%s", uuid);
443 if (strlen(uuid) >= fsname_len)
452 int llapi_getname(const char *path, char *name, size_t namelen)
458 rc = llapi_get_fsname_instance(path, fsname, sizeof(fsname),
459 instance, sizeof(instance));
463 snprintf(name, namelen, "%s-%s", fsname, instance);
464 if (strlen(fsname) + 1 + strlen(instance) >= namelen) {
472 int llapi_get_instance(const char *path, char *instance, size_t instance_len)
474 return llapi_get_fsname_instance(path, NULL, 0, instance, instance_len);
477 int llapi_get_fsname(const char *path, char *fsname, size_t fsname_len)
479 return llapi_get_fsname_instance(path, fsname, fsname_len, NULL, 0);