4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/utils/liblustreapi.c
34 * Author: Peter J. Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Robert Read <rread@clusterfs.com>
49 #include <sys/ioctl.h>
56 #include <sys/statfs.h>
57 #include <sys/syscall.h>
59 #include <sys/types.h>
60 #include <sys/xattr.h>
63 #include <libgen.h> /* for dirname() */
64 #ifdef HAVE_LINUX_UNISTD_H
65 #include <linux/unistd.h>
71 #include <libcfs/util/param.h>
72 #include <libcfs/util/string.h>
73 #include <lnet/lnetctl.h>
74 #include <lustre/lustreapi.h>
75 #include <lustre_ioctl.h>
76 #include "lustreapi_internal.h"
78 static int llapi_msg_level = LLAPI_MSG_MAX;
80 char *mdt_hash_name[] = { "none",
81 LMV_HASH_NAME_ALL_CHARS,
82 LMV_HASH_NAME_FNV_1A_64 };
84 void llapi_msg_set_level(int level)
86 /* ensure level is in the good range */
87 if (level < LLAPI_MSG_OFF)
88 llapi_msg_level = LLAPI_MSG_OFF;
89 else if (level > LLAPI_MSG_MAX)
90 llapi_msg_level = LLAPI_MSG_MAX;
92 llapi_msg_level = level;
95 int llapi_msg_get_level(void)
97 return llapi_msg_level;
100 static void error_callback_default(enum llapi_message_level level, int err,
101 const char *fmt, va_list ap)
103 vfprintf(stderr, fmt, ap);
104 if (level & LLAPI_MSG_NO_ERRNO)
105 fprintf(stderr, "\n");
107 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
110 static void info_callback_default(enum llapi_message_level level, int err,
111 const char *fmt, va_list ap)
113 vfprintf(stdout, fmt, ap);
116 static llapi_log_callback_t llapi_error_callback = error_callback_default;
117 static llapi_log_callback_t llapi_info_callback = info_callback_default;
120 /* llapi_error will preserve errno */
121 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
124 int tmp_errno = errno;
126 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
130 llapi_error_callback(level, abs(err), fmt, args);
135 /* llapi_printf will preserve errno */
136 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
139 int tmp_errno = errno;
141 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
145 llapi_info_callback(level, 0, fmt, args);
151 * Set a custom error logging function. Passing in NULL will reset the logging
152 * callback to its default value.
154 * This function returns the value of the old callback.
156 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
158 llapi_log_callback_t old = llapi_error_callback;
161 llapi_error_callback = cb;
163 llapi_error_callback = error_callback_default;
169 * Set a custom info logging function. Passing in NULL will reset the logging
170 * callback to its default value.
172 * This function returns the value of the old callback.
174 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
176 llapi_log_callback_t old = llapi_info_callback;
179 llapi_info_callback = cb;
181 llapi_info_callback = info_callback_default;
187 * size_units is to be initialized (or zeroed) by caller.
189 int llapi_parse_size(const char *optarg, unsigned long long *size,
190 unsigned long long *size_units, int bytes_spec)
193 char *argbuf = (char *)optarg;
194 unsigned long long frac = 0, frac_d = 1;
196 if (strncmp(optarg, "-", 1) == 0)
199 if (*size_units == 0)
202 *size = strtoull(argbuf, &end, 0);
203 if (end != NULL && *end == '.') {
207 frac = strtoull(argbuf, &end, 10);
208 /* count decimal places */
209 for (i = 0; i < (end - argbuf); i++)
214 if ((*end == 'b') && *(end + 1) == '\0' &&
215 (*size & (~0ULL << (64 - 9))) == 0 &&
217 *size_units = 1 << 9;
218 } else if ((*end == 'b') &&
219 *(end + 1) == '\0' &&
222 } else if ((*end == 'k' || *end == 'K') &&
223 *(end + 1) == '\0' &&
224 (*size & (~0ULL << (64 - 10))) == 0) {
225 *size_units = 1 << 10;
226 } else if ((*end == 'm' || *end == 'M') &&
227 *(end + 1) == '\0' &&
228 (*size & (~0ULL << (64 - 20))) == 0) {
229 *size_units = 1 << 20;
230 } else if ((*end == 'g' || *end == 'G') &&
231 *(end + 1) == '\0' &&
232 (*size & (~0ULL << (64 - 30))) == 0) {
233 *size_units = 1 << 30;
234 } else if ((*end == 't' || *end == 'T') &&
235 *(end + 1) == '\0' &&
236 (*size & (~0ULL << (64 - 40))) == 0) {
237 *size_units = 1ULL << 40;
238 } else if ((*end == 'p' || *end == 'P') &&
239 *(end + 1) == '\0' &&
240 (*size & (~0ULL << (64 - 50))) == 0) {
241 *size_units = 1ULL << 50;
242 } else if ((*end == 'e' || *end == 'E') &&
243 *(end + 1) == '\0' &&
244 (*size & (~0ULL << (64 - 60))) == 0) {
245 *size_units = 1ULL << 60;
250 *size = *size * *size_units + frac * *size_units / frac_d;
255 /* XXX: llapi_xxx() functions return negative values upon failure */
257 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
258 int stripe_count, int stripe_pattern)
262 /* 64 KB is the largest common page size I'm aware of (on ia64), but
263 * check the local page size just in case. */
264 page_size = LOV_MIN_STRIPE_SIZE;
265 if (getpagesize() > page_size) {
266 page_size = getpagesize();
267 llapi_err_noerrno(LLAPI_MSG_WARN,
268 "warning: your page size (%u) is "
269 "larger than expected (%u)", page_size,
270 LOV_MIN_STRIPE_SIZE);
272 if (!llapi_stripe_size_is_aligned(stripe_size)) {
274 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
275 "must be an even multiple of %d bytes",
276 stripe_size, page_size);
279 if (!llapi_stripe_index_is_valid(stripe_offset)) {
281 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
285 if (!llapi_stripe_count_is_valid(stripe_count)) {
287 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
291 if (llapi_stripe_size_is_too_big(stripe_size)) {
293 llapi_error(LLAPI_MSG_ERROR, rc,
294 "warning: stripe size 4G or larger "
295 "is not currently supported and would wrap");
302 * Trim a trailing newline from a string, if it exists.
304 int llapi_chomp_string(char *buf)
320 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
322 static int get_param_lov(const char *path, const char *param,
323 char *buf, size_t buf_size)
325 struct obd_uuid uuid;
328 rc = llapi_file_get_lov_uuid(path, &uuid);
332 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
337 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
339 static int get_param_lmv(const char *path, const char *param,
340 char *buf, size_t buf_size)
342 struct obd_uuid uuid;
345 rc = llapi_file_get_lmv_uuid(path, &uuid);
349 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
353 static int get_mds_md_size(const char *path)
355 char buf[PATH_MAX], inst[PATH_MAX];
356 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
359 rc = llapi_getname(path, inst, sizeof(inst));
363 /* Get the max ea size from llite parameters. */
364 rc = get_lustre_param_value("llite", inst, FILTER_BY_EXACT,
365 "max_easize", buf, sizeof(buf));
371 return rc > 0 ? rc : md_size;
374 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
376 return get_param_lmv(path, "uuid", buf, bufsize);
380 * if pool is NULL, search ostname in target_obd
381 * if pool is not NULL:
382 * if pool not found returns errno < 0
383 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
384 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
386 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
388 char buffer[PATH_MAX];
394 /* You need one or the other */
395 if (poolname == NULL && fsname == NULL)
399 len = strlen(ostname);
401 if (poolname == NULL && len == 0)
404 /* Search by poolname and fsname if is not NULL */
405 if (poolname != NULL) {
406 rc = poolpath(¶m, fsname, NULL);
408 snprintf(buffer, sizeof(buffer), "%s/%s",
409 param.gl_pathv[0], poolname);
411 } else if (fsname != NULL) {
412 rc = get_lustre_param_path("lov", fsname,
414 "target_obd", ¶m);
416 strncpy(buffer, param.gl_pathv[0],
422 cfs_free_param_data(¶m);
426 fd = fopen(buffer, "r");
430 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
431 if (poolname == NULL) {
433 /* Search for an ostname in the list of OSTs
434 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
435 ptr = strchr(buffer, ' ');
437 (strncmp(ptr + 1, ostname, len) == 0)) {
442 /* Search for an ostname in a pool,
443 (or an existing non-empty pool if no ostname) */
444 if ((ostname == NULL) ||
445 (strncmp(buffer, ostname, len) == 0)) {
456 * Open a Lustre file.
458 * \param name the name of the file to be opened
459 * \param flags access mode, see flags in open(2)
460 * \param mode permission of the file if it is created, see mode in open(2)
461 * \param param stripe pattern of the newly created file
463 * \retval file descriptor of opened file
464 * \retval negative errno on failure
466 int llapi_file_open_param(const char *name, int flags, mode_t mode,
467 const struct llapi_stripe_param *param)
469 char fsname[MAX_OBD_NAME + 1] = { 0 };
470 char *pool_name = param->lsp_pool;
471 struct lov_user_md *lum = NULL;
472 size_t lum_size = sizeof(*lum);
475 /* Make sure we are on a Lustre file system */
476 rc = llapi_search_fsname(name, fsname);
478 llapi_error(LLAPI_MSG_ERROR, rc,
479 "'%s' is not on a Lustre filesystem",
484 /* Check if the stripe pattern is sane. */
485 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
486 param->lsp_stripe_offset,
487 param->lsp_stripe_count,
488 param->lsp_stripe_pattern);
492 /* Make sure we have a good pool */
493 if (pool_name != NULL) {
494 /* in case user gives the full pool name <fsname>.<poolname>,
495 * strip the fsname */
496 char *ptr = strchr(pool_name, '.');
499 if (strcmp(pool_name, fsname) != 0) {
501 llapi_err_noerrno(LLAPI_MSG_ERROR,
502 "Pool '%s' is not on filesystem '%s'",
509 /* Make sure the pool exists and is non-empty */
510 rc = llapi_search_ost(fsname, pool_name, NULL);
512 char *err = rc == 0 ? "has no OSTs" : "does not exist";
514 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
515 fsname, pool_name, err);
519 lum_size = sizeof(struct lov_user_md_v3);
522 /* sanity check of target list */
523 if (param->lsp_is_specific) {
524 char ostname[MAX_OBD_NAME + 1];
528 for (i = 0; i < param->lsp_stripe_count; i++) {
529 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
530 fsname, param->lsp_osts[i]);
531 rc = llapi_search_ost(fsname, pool_name, ostname);
536 llapi_error(LLAPI_MSG_ERROR, rc,
537 "%s: cannot find OST %s in %s",
544 /* Make sure stripe offset is in OST list. */
545 if (param->lsp_osts[i] == param->lsp_stripe_offset)
549 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
550 "%s: stripe offset '%d' is not in the "
552 __func__, param->lsp_stripe_offset);
556 lum_size = lov_user_md_size(param->lsp_stripe_count,
557 LOV_USER_MAGIC_SPECIFIC);
560 lum = calloc(1, lum_size);
565 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
567 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
568 flags = O_DIRECTORY | O_RDONLY;
575 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
580 /* Initialize IOCTL striping pattern structure */
581 lum->lmm_magic = LOV_USER_MAGIC_V1;
582 lum->lmm_pattern = param->lsp_stripe_pattern;
583 lum->lmm_stripe_size = param->lsp_stripe_size;
584 lum->lmm_stripe_count = param->lsp_stripe_count;
585 lum->lmm_stripe_offset = param->lsp_stripe_offset;
586 if (pool_name != NULL) {
587 struct lov_user_md_v3 *lumv3 = (void *)lum;
589 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
590 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
592 if (param->lsp_is_specific) {
593 struct lov_user_md_v3 *lumv3 = (void *)lum;
596 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
597 if (pool_name == NULL) {
598 /* LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
599 * OST list, therefore if pool is not specified we have
600 * to pack a null pool name for placeholder. */
601 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
604 for (i = 0; i < param->lsp_stripe_count; i++)
605 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
608 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
609 char *errmsg = "stripe already set";
612 if (errno != EEXIST && errno != EALREADY)
613 errmsg = strerror(errno);
615 llapi_err_noerrno(LLAPI_MSG_ERROR,
616 "error on ioctl %#jx for '%s' (%d): %s",
617 (uintmax_t)LL_IOC_LOV_SETSTRIPE, name, fd,
629 int llapi_file_open_pool(const char *name, int flags, int mode,
630 unsigned long long stripe_size, int stripe_offset,
631 int stripe_count, int stripe_pattern, char *pool_name)
633 const struct llapi_stripe_param param = {
634 .lsp_stripe_size = stripe_size,
635 .lsp_stripe_count = stripe_count,
636 .lsp_stripe_pattern = stripe_pattern,
637 .lsp_stripe_offset = stripe_offset,
638 .lsp_pool = pool_name
640 return llapi_file_open_param(name, flags, mode, ¶m);
643 int llapi_file_open(const char *name, int flags, int mode,
644 unsigned long long stripe_size, int stripe_offset,
645 int stripe_count, int stripe_pattern)
647 return llapi_file_open_pool(name, flags, mode, stripe_size,
648 stripe_offset, stripe_count,
649 stripe_pattern, NULL);
652 int llapi_file_create(const char *name, unsigned long long stripe_size,
653 int stripe_offset, int stripe_count, int stripe_pattern)
657 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
658 stripe_offset, stripe_count, stripe_pattern,
667 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
668 int stripe_offset, int stripe_count,
669 int stripe_pattern, char *pool_name)
673 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
674 stripe_offset, stripe_count, stripe_pattern,
683 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
684 int stripe_count, int stripe_pattern,
685 const char *pool_name)
687 struct lmv_user_md lum = { 0 };
691 lum.lum_magic = LMV_USER_MAGIC;
692 lum.lum_stripe_offset = stripe_offset;
693 lum.lum_stripe_count = stripe_count;
694 lum.lum_hash_type = stripe_pattern;
695 if (pool_name != NULL) {
696 if (strlen(pool_name) >= sizeof(lum.lum_pool_name)) {
697 llapi_err_noerrno(LLAPI_MSG_ERROR,
698 "error LL_IOC_LMV_SET_DEFAULT_STRIPE '%s'"
699 ": too large pool name: %s", name, pool_name);
702 strncpy(lum.lum_pool_name, pool_name,
703 sizeof(lum.lum_pool_name));
706 fd = open(name, O_DIRECTORY | O_RDONLY);
709 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
713 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lum);
715 char *errmsg = "stripe already set";
717 if (errno != EEXIST && errno != EALREADY)
718 errmsg = strerror(errno);
720 llapi_err_noerrno(LLAPI_MSG_ERROR,
721 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
728 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
729 int stripe_count, int stripe_pattern,
730 const char *pool_name)
732 struct lmv_user_md lmu = { 0 };
733 struct obd_ioctl_data data = { 0 };
736 char *dirpath = NULL;
737 char *namepath = NULL;
743 dirpath = strdup(name);
744 namepath = strdup(name);
745 if (!dirpath || !namepath)
748 lmu.lum_magic = LMV_USER_MAGIC;
749 lmu.lum_stripe_offset = stripe_offset;
750 lmu.lum_stripe_count = stripe_count;
751 lmu.lum_hash_type = stripe_pattern;
752 if (pool_name != NULL) {
753 if (strlen(pool_name) > LOV_MAXPOOLNAME) {
754 llapi_err_noerrno(LLAPI_MSG_ERROR,
755 "error LL_IOC_LMV_SETSTRIPE '%s' : too large"
756 "pool name: %s", name, pool_name);
760 memcpy(lmu.lum_pool_name, pool_name, strlen(pool_name));
763 filename = basename(namepath);
764 dir = dirname(dirpath);
766 data.ioc_inlbuf1 = (char *)filename;
767 data.ioc_inllen1 = strlen(filename) + 1;
768 data.ioc_inlbuf2 = (char *)&lmu;
769 data.ioc_inllen2 = sizeof(struct lmv_user_md);
770 data.ioc_type = mode;
771 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
773 llapi_error(LLAPI_MSG_ERROR, rc,
774 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
779 fd = open(dir, O_DIRECTORY | O_RDONLY);
782 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
786 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
787 char *errmsg = "stripe already set";
789 if (errno != EEXIST && errno != EALREADY)
790 errmsg = strerror(errno);
792 llapi_err_noerrno(LLAPI_MSG_ERROR,
793 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
803 int llapi_direntry_remove(char *dname)
805 char *dirpath = NULL;
806 char *namepath = NULL;
812 dirpath = strdup(dname);
813 namepath = strdup(dname);
814 if (!dirpath || !namepath)
817 filename = basename(namepath);
819 dir = dirname(dirpath);
821 fd = open(dir, O_DIRECTORY | O_RDONLY);
824 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
829 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
830 char *errmsg = strerror(errno);
831 llapi_err_noerrno(LLAPI_MSG_ERROR,
832 "error on ioctl %#jx for '%s' (%d): %s",
833 (uintmax_t)LL_IOC_LMV_SETSTRIPE, filename,
845 * Find the fsname, the full path, and/or an open fd.
846 * Either the fsname or path must not be NULL
848 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
851 char buf[PATH_MAX], mntdir[PATH_MAX];
854 int idx = 0, len = 0, mntlen, fd;
857 /* get the mount point */
858 fp = setmntent(PROC_MOUNTS, "r");
861 llapi_error(LLAPI_MSG_ERROR, rc,
862 "setmntent(%s) failed", PROC_MOUNTS);
866 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
869 if (!llapi_is_lustre_mnt(&mnt))
872 if ((want & WANT_INDEX) && (idx++ != index))
875 mntlen = strlen(mnt.mnt_dir);
876 ptr = strchr(mnt.mnt_fsname, '/');
877 while (ptr && *ptr == '/')
879 /* thanks to the call to llapi_is_lustre_mnt() above,
880 * we are sure that mnt.mnt_fsname contains ":/",
881 * so ptr should never be NULL */
885 while (*ptr_end != '/' && *ptr_end != '\0')
888 /* Check the fsname for a match, if given */
889 if (!(want & WANT_FSNAME) && fsname != NULL &&
890 (strlen(fsname) > 0) &&
891 (strncmp(ptr, fsname, ptr_end - ptr) != 0))
894 /* If the path isn't set return the first one we find */
895 if (path == NULL || strlen(path) == 0) {
896 strncpy(mntdir, mnt.mnt_dir, strlen(mnt.mnt_dir));
897 mntdir[strlen(mnt.mnt_dir)] = '\0';
898 if ((want & WANT_FSNAME) && fsname != NULL) {
899 strncpy(fsname, ptr, ptr_end - ptr);
900 fsname[ptr_end - ptr] = '\0';
904 /* Otherwise find the longest matching path */
905 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
906 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
907 strncpy(mntdir, mnt.mnt_dir, strlen(mnt.mnt_dir));
908 mntdir[strlen(mnt.mnt_dir)] = '\0';
910 if ((want & WANT_FSNAME) && fsname != NULL) {
911 strncpy(fsname, ptr, ptr_end - ptr);
912 fsname[ptr_end - ptr] = '\0';
921 if ((want & WANT_PATH) && path != NULL) {
922 strncpy(path, mntdir, strlen(mntdir));
923 path[strlen(mntdir)] = '\0';
925 if (want & WANT_FD) {
926 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
929 llapi_error(LLAPI_MSG_ERROR, rc,
930 "error opening '%s'", mntdir);
936 } else if (want & WANT_ERROR)
937 llapi_err_noerrno(LLAPI_MSG_ERROR,
938 "can't find fs root for '%s': %d",
939 (want & WANT_PATH) ? fsname : path, rc);
944 * search lustre mounts
946 * Calling this function will return to the user the mount point, mntdir, and
947 * the file system name, fsname, if the user passed a buffer to this routine.
949 * The user inputs are pathname and index. If the pathname is supplied then
950 * the value of the index will be ignored. The pathname will return data if
951 * the pathname is located on a lustre mount. Index is used to pick which
952 * mount point you want in the case of multiple mounted lustre file systems.
953 * See function lfs_osts in lfs.c for an example of the index use.
955 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
958 int want = WANT_PATH, idx = -1;
960 if (!pathname || pathname[0] == '\0') {
964 strcpy(mntdir, pathname);
968 return get_root_path(want, fsname, NULL, mntdir, idx);
971 /* Given a path, find the corresponding Lustre fsname */
972 int llapi_search_fsname(const char *pathname, char *fsname)
977 path = realpath(pathname, NULL);
979 char buf[PATH_MAX], *ptr;
982 if (pathname[0] != '/') {
983 /* Need an absolute path, but realpath() only works for
984 * pathnames that actually exist. We go through the
985 * extra hurdle of dirname(getcwd() + pathname) in
986 * case the relative pathname contains ".." in it. */
987 if (getcwd(buf, sizeof(buf) - 2) == NULL)
989 rc = strlcat(buf, "/", sizeof(buf));
990 if (rc >= sizeof(buf))
993 rc = strlcat(buf, pathname, sizeof(buf));
994 if (rc >= sizeof(buf))
996 path = realpath(buf, NULL);
998 ptr = strrchr(buf, '/');
1002 path = realpath(buf, NULL);
1005 llapi_error(LLAPI_MSG_ERROR, rc,
1006 "pathname '%s' cannot expand",
1012 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1017 int llapi_search_rootpath(char *pathname, const char *fsname)
1019 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1022 int llapi_getname(const char *path, char *buf, size_t size)
1024 struct obd_uuid uuid_buf;
1025 char *uuid = uuid_buf.uuid;
1028 memset(&uuid_buf, 0, sizeof(uuid_buf));
1029 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1033 /* We want to turn lustre-clilov-ffff88002738bc00 into
1034 * lustre-ffff88002738bc00. */
1036 nr = snprintf(buf, size, "%.*s-%s",
1037 (int) (strlen(uuid) - 24), uuid,
1038 uuid + strlen(uuid) - 16);
1047 * Get the list of pool members.
1048 * \param poolname string of format \<fsname\>.\<poolname\>
1049 * \param members caller-allocated array of char*
1050 * \param list_size size of the members array
1051 * \param buffer caller-allocated buffer for storing OST names
1052 * \param buffer_size size of the buffer
1054 * \return number of members retrieved for this pool
1055 * \retval -error failure
1057 int llapi_get_poolmembers(const char *poolname, char **members,
1058 int list_size, char *buffer, int buffer_size)
1060 char fsname[PATH_MAX];
1069 /* name is FSNAME.POOLNAME */
1070 if (strlen(poolname) >= sizeof(fsname))
1072 strlcpy(fsname, poolname, sizeof(fsname));
1073 pool = strchr(fsname, '.');
1080 rc = poolpath(&pathname, fsname, NULL);
1082 llapi_error(LLAPI_MSG_ERROR, rc,
1083 "Lustre filesystem '%s' not found",
1088 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1089 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1090 cfs_free_param_data(&pathname);
1091 if (rc >= sizeof(buf))
1093 fd = fopen(buf, "r");
1096 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1101 while (fgets(buf, sizeof(buf), fd) != NULL) {
1102 if (nb_entries >= list_size) {
1106 buf[sizeof(buf) - 1] = '\0';
1108 tmp = strchr(buf, '\n');
1111 if (used + strlen(buf) + 1 > buffer_size) {
1116 strcpy(buffer + used, buf);
1117 members[nb_entries] = buffer + used;
1118 used += strlen(buf) + 1;
1128 * Get the list of pools in a filesystem.
1129 * \param name filesystem name or path
1130 * \param poollist caller-allocated array of char*
1131 * \param list_size size of the poollist array
1132 * \param buffer caller-allocated buffer for storing pool names
1133 * \param buffer_size size of the buffer
1135 * \return number of pools retrieved for this filesystem
1136 * \retval -error failure
1138 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1139 char *buffer, int buffer_size)
1141 char rname[PATH_MAX];
1147 struct dirent *cookie = NULL;
1149 unsigned int nb_entries = 0;
1150 unsigned int used = 0;
1153 /* initialize output array */
1154 for (i = 0; i < list_size; i++)
1157 /* is name a pathname ? */
1158 ptr = strchr(name, '/');
1160 /* only absolute pathname is supported */
1164 if (!realpath(name, rname)) {
1166 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1171 fsname = strdup(rname);
1175 rc = poolpath(&pathname, NULL, rname);
1177 /* name is FSNAME */
1178 fsname = strdup(name);
1181 rc = poolpath(&pathname, fsname, NULL);
1184 llapi_error(LLAPI_MSG_ERROR, rc,
1185 "Lustre filesystem '%s' not found", name);
1189 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1190 dir = opendir(pathname.gl_pathv[0]);
1193 llapi_error(LLAPI_MSG_ERROR, rc,
1194 "Could not open pool list for '%s'",
1200 rc = readdir_r(dir, &pool, &cookie);
1203 llapi_error(LLAPI_MSG_ERROR, rc,
1204 "Error reading pool list for '%s'", name);
1206 } else if ((rc == 0) && (cookie == NULL)) {
1207 /* end of directory */
1211 /* ignore . and .. */
1212 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
1215 /* check output bounds */
1216 if (nb_entries >= list_size) {
1221 /* +2 for '.' and final '\0' */
1222 if (used + strlen(pool.d_name) + strlen(fsname) + 2
1228 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
1229 poollist[nb_entries] = buffer + used;
1230 used += strlen(pool.d_name) + strlen(fsname) + 2;
1237 cfs_free_param_data(&pathname);
1240 return rc != 0 ? rc : nb_entries;
1243 /* wrapper for lfs.c and obd.c */
1244 int llapi_poollist(const char *name)
1246 /* list of pool names (assume that pool count is smaller
1248 char **list, *buffer = NULL, *fsname = (char *)name;
1249 char *poolname = NULL, *tmp = NULL, data[16];
1250 enum param_filter type = FILTER_BY_PATH;
1251 int obdcount, bufsize, rc, nb, i;
1256 if (name[0] != '/') {
1257 fsname = strdup(name);
1261 poolname = strchr(fsname, '.');
1264 type = FILTER_BY_FS_NAME;
1267 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1268 data, sizeof(data));
1271 obdcount = atoi(data);
1273 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1274 * and also an array to store the pointers for all that
1275 * allocated space. */
1277 bufsize = sizeof(struct obd_uuid) * obdcount;
1278 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1279 if (buffer == NULL) {
1283 list = (char **) (buffer + bufsize);
1286 /* name is a path or fsname */
1287 nb = llapi_get_poollist(name, list, obdcount,
1290 /* name is a pool name (<fsname>.<poolname>) */
1291 nb = llapi_get_poolmembers(name, list, obdcount,
1295 if (nb == -EOVERFLOW) {
1298 goto retry_get_pools;
1301 for (i = 0; i < nb; i++)
1302 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1303 rc = (nb < 0 ? nb : 0);
1307 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1312 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1313 void *data, struct dirent64 *de);
1315 #define OBD_NOT_FOUND (-1)
1317 static int common_param_init(struct find_param *param, char *path)
1319 int lum_size = get_mds_md_size(path);
1321 if (lum_size < PATH_MAX + 1)
1322 lum_size = PATH_MAX + 1;
1324 param->fp_lum_size = lum_size;
1325 param->fp_lmd = calloc(1, sizeof(lstat_t) + param->fp_lum_size);
1326 if (param->fp_lmd == NULL) {
1327 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1328 "error: allocation of %zu bytes for ioctl",
1329 sizeof(lstat_t) + param->fp_lum_size);
1333 param->fp_lmv_stripe_count = 256;
1334 param->fp_lmv_md = calloc(1,
1335 lmv_user_md_size(param->fp_lmv_stripe_count,
1337 if (param->fp_lmv_md == NULL) {
1338 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1339 "error: allocation of %d bytes for ioctl",
1340 lmv_user_md_size(param->fp_lmv_stripe_count,
1345 param->fp_got_uuids = 0;
1346 param->fp_obd_indexes = NULL;
1347 param->fp_obd_index = OBD_NOT_FOUND;
1348 if (!param->fp_migrate)
1349 param->fp_mdt_index = OBD_NOT_FOUND;
1353 static void find_param_fini(struct find_param *param)
1355 if (param->fp_obd_indexes)
1356 free(param->fp_obd_indexes);
1359 free(param->fp_lmd);
1361 if (param->fp_lmv_md)
1362 free(param->fp_lmv_md);
1365 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1366 struct dirent64 *de)
1368 struct find_param *param = data;
1374 /* set errno upon failure */
1375 static DIR *opendir_parent(const char *path)
1381 path_copy = strdup(path);
1382 if (path_copy == NULL)
1385 parent_path = dirname(path_copy);
1386 parent = opendir(parent_path);
1392 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1397 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1398 if (param->fp_get_default_lmv)
1399 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1401 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1403 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1404 if (errno == E2BIG && ret != 0) {
1408 stripe_count = (__u32)param->fp_lmv_md->lum_stripe_count;
1409 if (stripe_count <= param->fp_lmv_stripe_count)
1412 free(param->fp_lmv_md);
1413 param->fp_lmv_stripe_count = stripe_count;
1414 lmv_size = lmv_user_md_size(stripe_count, LMV_MAGIC_V1);
1415 param->fp_lmv_md = malloc(lmv_size);
1416 if (param->fp_lmv_md == NULL) {
1417 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1418 "error: allocation of %d bytes for ioctl",
1419 lmv_user_md_size(param->fp_lmv_stripe_count,
1428 static int get_lmd_info(char *path, DIR *parent, DIR *dir,
1429 struct lov_user_mds_data *lmd, int lumlen)
1431 lstat_t *st = &lmd->lmd_st;
1434 if (parent == NULL && dir == NULL)
1438 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO, (void *)lmd);
1439 } else if (parent) {
1440 char *fname = strrchr(path, '/');
1442 /* To avoid opening, locking, and closing each file on the
1443 * client if that is not needed. The GETFILEINFO ioctl can
1444 * be done on the patent dir with a single open for all
1445 * files in that directory, and it also doesn't pollute the
1446 * client dcache with millions of dentries when traversing
1447 * a large filesystem. */
1448 fname = (fname == NULL ? path : fname + 1);
1449 /* retrieve needed file info */
1450 strlcpy((char *)lmd, fname, lumlen);
1451 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1455 if (errno == ENOTTY) {
1456 /* ioctl is not supported, it is not a lustre fs.
1457 * Do the regular lstat(2) instead. */
1458 ret = lstat_f(path, st);
1461 llapi_error(LLAPI_MSG_ERROR, ret,
1462 "error: %s: lstat failed for %s",
1465 } else if (errno == ENOENT) {
1467 llapi_error(LLAPI_MSG_WARN, ret,
1468 "warning: %s: %s does not exist",
1470 } else if (errno != EISDIR) {
1472 llapi_error(LLAPI_MSG_ERROR, ret,
1473 "%s ioctl failed for %s.",
1474 dir ? "LL_IOC_MDC_GETINFO" :
1475 "IOC_MDC_GETFILEINFO", path);
1478 llapi_error(LLAPI_MSG_ERROR, ret,
1479 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1486 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1487 semantic_func_t sem_init,
1488 semantic_func_t sem_fini, void *data,
1489 struct dirent64 *de)
1491 struct find_param *param = (struct find_param *)data;
1492 struct dirent64 *dent;
1500 if (!d && errno != ENOTDIR) {
1502 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1505 } else if (!d && !parent) {
1506 /* ENOTDIR. Open the parent dir. */
1507 p = opendir_parent(path);
1514 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
1520 while ((dent = readdir64(d)) != NULL) {
1523 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1526 /* Don't traverse .lustre directory */
1527 if (!(strcmp(dent->d_name, dot_lustre_name)))
1531 if ((len + dent->d_reclen + 2) > size) {
1532 llapi_err_noerrno(LLAPI_MSG_ERROR,
1533 "error: %s: string buffer is too small",
1538 strcat(path, dent->d_name);
1540 if (dent->d_type == DT_UNKNOWN) {
1541 lstat_t *st = ¶m->fp_lmd->lmd_st;
1543 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
1544 param->fp_lum_size);
1546 dent->d_type = IFTODT(st->st_mode);
1553 switch (dent->d_type) {
1555 llapi_err_noerrno(LLAPI_MSG_ERROR,
1556 "error: %s: '%s' is UNKNOWN type %d",
1557 __func__, dent->d_name, dent->d_type);
1560 rc = llapi_semantic_traverse(path, size, d, sem_init,
1561 sem_fini, data, dent);
1562 if (rc != 0 && ret == 0)
1568 rc = sem_init(path, d, NULL, data, dent);
1569 if (rc < 0 && ret == 0)
1572 if (sem_fini && rc == 0)
1573 sem_fini(path, d, NULL, data, dent);
1581 sem_fini(path, parent, &d, data, de);
1590 static int param_callback(char *path, semantic_func_t sem_init,
1591 semantic_func_t sem_fini, struct find_param *param)
1593 int ret, len = strlen(path);
1596 if (len > PATH_MAX) {
1598 llapi_error(LLAPI_MSG_ERROR, ret,
1599 "Path name '%s' is too long", path);
1603 buf = (char *)malloc(PATH_MAX + 1);
1607 strlcpy(buf, path, PATH_MAX + 1);
1608 ret = common_param_init(param, buf);
1612 param->fp_depth = 0;
1614 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1615 sem_fini, param, NULL);
1617 find_param_fini(param);
1619 return ret < 0 ? ret : 0;
1622 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1624 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1627 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lov name.");
1632 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
1634 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
1637 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
1642 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1646 fd = open(path, O_RDONLY | O_NONBLOCK);
1649 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1653 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1659 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
1663 fd = open(path, O_RDONLY | O_NONBLOCK);
1666 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1670 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
1682 * If uuidp is NULL, return the number of available obd uuids.
1683 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1684 * there are more OSTs than allocated to uuidp, then an error is returned with
1685 * the ost_count set to number of available obd uuids.
1687 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
1688 int *ost_count, enum tgt_type type)
1690 char buf[PATH_MAX], format[32];
1691 int rc = 0, index = 0;
1692 struct obd_uuid name;
1696 /* Get the lov name */
1697 if (type == LOV_TYPE)
1698 rc = llapi_file_fget_lov_uuid(fd, &name);
1700 rc = llapi_file_fget_lmv_uuid(fd, &name);
1704 /* Now get the ost uuids */
1705 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
1706 FILTER_BY_EXACT, "target_obd", ¶m);
1710 fp = fopen(param.gl_pathv[0], "r");
1713 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
1718 snprintf(format, sizeof(format),
1719 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
1720 while (fgets(buf, sizeof(buf), fp) != NULL) {
1721 if (uuidp && (index < *ost_count)) {
1722 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
1730 if (uuidp && (index > *ost_count))
1735 cfs_free_param_data(¶m);
1739 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1741 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
1744 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1749 root = open(mnt, O_RDONLY | O_DIRECTORY);
1752 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
1757 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
1765 /* Check if user specified value matches a real uuid. Ignore _UUID,
1766 * -osc-4ba41334, other trailing gunk in comparison.
1767 * @param real_uuid ends in "_UUID"
1768 * @param search_uuid may or may not end in "_UUID"
1770 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1772 int cmplen = strlen(real_uuid);
1773 int searchlen = strlen(search_uuid);
1775 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
1777 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
1780 /* The UUIDs may legitimately be different lengths, if
1781 * the system was upgraded from an older version. */
1782 if (cmplen != searchlen)
1785 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
1788 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
1789 * returned in param->fp_obd_index */
1790 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
1792 struct obd_uuid obd_uuid;
1799 if (param->fp_got_uuids)
1802 /* Get the lov/lmv name */
1803 if (param->fp_get_lmv)
1804 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
1806 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
1808 if (rc != -ENOTTY) {
1809 llapi_error(LLAPI_MSG_ERROR, rc,
1810 "error: can't get %s name: %s",
1811 param->fp_get_lmv ? "lmv" : "lov",
1819 param->fp_got_uuids = 1;
1821 /* Now get the ost uuids */
1822 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
1823 obd_uuid.uuid, FILTER_BY_EXACT,
1824 "target_obd", ¶m_data);
1828 fp = fopen(param_data.gl_pathv[0], "r");
1831 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
1832 param_data.gl_pathv[0]);
1836 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
1837 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
1838 param->fp_get_lmv ? "MDTS" : "OBDS");
1840 snprintf(format, sizeof(format),
1841 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
1842 while (fgets(buf, sizeof(buf), fp) != NULL) {
1845 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
1848 if (param->fp_obd_uuid) {
1849 if (llapi_uuid_match(obd_uuid.uuid,
1850 param->fp_obd_uuid->uuid)) {
1851 param->fp_obd_index = index;
1854 } else if (!param->fp_quiet && !param->fp_obds_printed) {
1855 /* Print everything */
1856 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
1859 param->fp_obds_printed = 1;
1863 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
1864 llapi_err_noerrno(LLAPI_MSG_ERROR,
1865 "error: %s: unknown obduuid: %s",
1866 __func__, param->fp_obd_uuid->uuid);
1870 cfs_free_param_data(¶m_data);
1874 /* In this case, param->fp_obd_uuid will be an array of obduuids and
1875 * obd index for all these obduuids will be returned in
1876 * param->fp_obd_indexes */
1877 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
1878 int num_obds, int **obdindexes, int *obdindex,
1881 int ret, obdcount, obd_valid = 0, obdnum;
1883 struct obd_uuid *uuids = NULL;
1887 if (type == LOV_TYPE)
1888 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
1890 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
1894 obdcount = atoi(buf);
1895 uuids = malloc(obdcount * sizeof(struct obd_uuid));
1900 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
1902 if (ret == -EOVERFLOW) {
1903 struct obd_uuid *uuids_temp;
1905 uuids_temp = realloc(uuids, obdcount *
1906 sizeof(struct obd_uuid));
1907 if (uuids_temp != NULL) {
1909 goto retry_get_uuids;
1914 llapi_error(LLAPI_MSG_ERROR, ret, "get ost uuid failed");
1918 indexes = malloc(num_obds * sizeof(*obdindex));
1919 if (indexes == NULL) {
1924 for (obdnum = 0; obdnum < num_obds; obdnum++) {
1927 /* The user may have specified a simple index */
1928 i = strtol(obduuids[obdnum].uuid, &end, 0);
1929 if (end && *end == '\0' && i < obdcount) {
1930 indexes[obdnum] = i;
1933 for (i = 0; i < obdcount; i++) {
1934 if (llapi_uuid_match(uuids[i].uuid,
1935 obduuids[obdnum].uuid)) {
1936 indexes[obdnum] = i;
1942 if (i >= obdcount) {
1943 indexes[obdnum] = OBD_NOT_FOUND;
1944 llapi_err_noerrno(LLAPI_MSG_ERROR,
1945 "error: %s: unknown obduuid: %s",
1946 __func__, obduuids[obdnum].uuid);
1952 *obdindex = OBD_NOT_FOUND;
1954 *obdindex = obd_valid;
1956 *obdindexes = indexes;
1964 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
1968 if (param->fp_mdt_uuid) {
1969 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
1971 ¶m->fp_mdt_indexes,
1972 ¶m->fp_mdt_index, LMV_TYPE);
1977 if (param->fp_obd_uuid) {
1978 ret = setup_indexes(dir, path, param->fp_obd_uuid,
1980 ¶m->fp_obd_indexes,
1981 ¶m->fp_obd_index, LOV_TYPE);
1986 param->fp_got_uuids = 1;
1991 int llapi_ostlist(char *path, struct find_param *param)
1996 fd = open(path, O_RDONLY | O_DIRECTORY);
2000 ret = setup_obd_uuid(fd, path, param);
2007 * Tries to determine the default stripe attributes for a given filesystem. The
2008 * filesystem to check should be specified by fsname, or will be determined
2011 static int sattr_get_defaults(const char *const fsname,
2012 unsigned int *scount,
2013 unsigned int *ssize,
2014 unsigned int *soffset)
2020 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2021 "stripecount", val, sizeof(val));
2024 *scount = atoi(val);
2028 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2029 "stripesize", val, sizeof(val));
2036 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2037 "stripeoffset", val, sizeof(val));
2040 *soffset = atoi(val);
2047 * Tries to gather the default stripe attributes for a given filesystem. If
2048 * the attributes can be determined, they are cached for easy retreival the
2049 * next time they are needed. Only a single filesystem's attributes are
2052 static int sattr_cache_get_defaults(const char *const fsname,
2053 const char *const pathname,
2054 unsigned int *scount,
2055 unsigned int *ssize,
2056 unsigned int *soffset)
2059 char fsname[PATH_MAX + 1];
2060 unsigned int stripecount;
2061 unsigned int stripesize;
2062 unsigned int stripeoffset;
2068 char fsname_buf[PATH_MAX + 1];
2069 unsigned int tmp[3];
2071 if (fsname == NULL) {
2072 rc = llapi_search_fsname(pathname, fsname_buf);
2076 strlcpy(fsname_buf, fsname, sizeof(fsname_buf));
2079 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2081 * Ensure all 3 sattrs (count, size, and offset) are
2082 * successfully retrieved and stored in tmp before writing to
2085 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2089 cache.stripecount = tmp[0];
2090 cache.stripesize = tmp[1];
2091 cache.stripeoffset = tmp[2];
2092 strlcpy(cache.fsname, fsname_buf, sizeof(cache.fsname));
2096 *scount = cache.stripecount;
2098 *ssize = cache.stripesize;
2100 *soffset = cache.stripeoffset;
2105 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2106 struct lov_user_ost_data_v1 *objects,
2107 int is_dir, int verbose, int depth,
2108 int raw, char *pool_name)
2110 char *prefix = is_dir ? "" : "lmm_";
2111 char *separator = "";
2114 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2115 lmm_oi_set_seq(&lum->lmm_oi, 0);
2116 if (verbose & VERBOSE_DETAIL)
2117 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
2120 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
2121 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2123 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2124 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
2126 llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq: %#jx\n",
2127 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2128 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: %#jx\n",
2129 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2131 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2132 if (verbose & ~VERBOSE_DFID)
2133 llapi_printf(LLAPI_MSG_NORMAL, "lmm_fid: ");
2134 /* This needs a bit of hand-holding since old 1.x lmm_oi
2135 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2136 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2137 * a real FID. Ideally the 2.x code would have stored this
2138 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2139 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2140 * worked properly (especially since IGIF FIDs use mds_inum as
2141 * the FID SEQ), but unfortunately that didn't happen.
2143 * Print it to look like an IGIF FID, even though the fields
2144 * are reversed on disk, so that it makes sense to userspace.
2146 * Don't use ostid_id() and ostid_seq(), since they assume the
2147 * oi_fid fields are in the right order. This is why there are
2148 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2150 * For newer layout types hopefully this will be a real FID. */
2151 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2152 lmm_oi_seq(&lum->lmm_oi) == 0 ?
2153 lmm_oi_id(&lum->lmm_oi) :
2154 lmm_oi_seq(&lum->lmm_oi),
2155 lmm_oi_seq(&lum->lmm_oi) == 0 ?
2156 0 : (__u32)lmm_oi_id(&lum->lmm_oi),
2157 (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32));
2160 if (verbose & VERBOSE_COUNT) {
2161 if (verbose & ~VERBOSE_COUNT)
2162 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
2165 if (!raw && lum->lmm_stripe_count == 0) {
2166 unsigned int scount;
2167 rc = sattr_cache_get_defaults(NULL, path,
2171 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2174 llapi_error(LLAPI_MSG_ERROR, rc,
2175 "Cannot determine default"
2178 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2179 lum->lmm_stripe_count ==
2180 (typeof(lum->lmm_stripe_count))(-1)
2181 ? -1 : lum->lmm_stripe_count);
2184 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2185 (__s16)lum->lmm_stripe_count);
2187 separator = is_dir ? " " : "\n";
2190 if (verbose & VERBOSE_SIZE) {
2191 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2192 if (verbose & ~VERBOSE_SIZE)
2193 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
2195 if (is_dir && !raw && lum->lmm_stripe_size == 0) {
2197 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2200 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2202 llapi_error(LLAPI_MSG_ERROR, rc,
2203 "Cannot determine default"
2206 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2207 lum->lmm_stripe_size);
2209 separator = is_dir ? " " : "\n";
2212 if ((verbose & VERBOSE_LAYOUT) && !is_dir) {
2213 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2214 if (verbose & ~VERBOSE_LAYOUT)
2215 llapi_printf(LLAPI_MSG_NORMAL, "%spattern: ",
2217 llapi_printf(LLAPI_MSG_NORMAL, "%.x", lum->lmm_pattern);
2221 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2222 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2223 if (verbose & ~VERBOSE_GENERATION)
2224 llapi_printf(LLAPI_MSG_NORMAL, "%slayout_gen: ",
2226 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2227 (int)lum->lmm_layout_gen);
2231 if (verbose & VERBOSE_OFFSET) {
2232 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2233 if (verbose & ~VERBOSE_OFFSET)
2234 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
2237 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2238 lum->lmm_stripe_offset ==
2239 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2240 lum->lmm_stripe_offset);
2242 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2243 objects[0].l_ost_idx);
2244 separator = is_dir ? " " : "\n";
2247 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
2248 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2249 if (verbose & ~VERBOSE_POOL)
2250 llapi_printf(LLAPI_MSG_NORMAL, "%spool: ",
2252 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2255 if (!is_dir || (is_dir && (verbose != VERBOSE_OBJID)))
2256 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2259 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2260 struct lov_user_ost_data_v1 *objects,
2261 char *path, int is_dir, int obdindex,
2262 int depth, int header, int raw)
2264 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
2267 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2268 if (obdindex == objects[i].l_ost_idx) {
2276 lov_dump_user_lmm_header(lum, path, objects, is_dir, header,
2277 depth, raw, pool_name);
2279 if (!is_dir && (header & VERBOSE_OBJID) &&
2280 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
2282 llapi_printf(LLAPI_MSG_NORMAL,
2283 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2285 for (i = 0; i < lum->lmm_stripe_count; i++) {
2286 int idx = objects[i].l_ost_idx;
2287 long long oid = ostid_id(&objects[i].l_ost_oi);
2288 long long gr = ostid_seq(&objects[i].l_ost_oi);
2289 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx)) {
2291 sprintf(fmt, "%s%s%s\n",
2292 "\t%6u\t%14llu\t%#13llx\t",
2293 (fid_seq_is_rsvd(gr) ||
2294 fid_seq_is_mdt0(gr)) ?
2295 "%14llu" : "%#14llx", "%s");
2296 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2298 obdindex == idx ? " *" : "");
2302 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2306 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2307 char *path, int obdindex, int depth, int verbose)
2309 struct lmv_user_mds_data *objects = lum->lum_objects;
2310 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2311 int i, obdstripe = 0;
2312 char *separator = "";
2314 if (obdindex != OBD_NOT_FOUND) {
2315 if (lum->lum_stripe_count == 0) {
2316 if (obdindex == lum->lum_stripe_offset)
2319 for (i = 0; i < lum->lum_stripe_count; i++) {
2320 if (obdindex == objects[i].lum_mds) {
2321 llapi_printf(LLAPI_MSG_NORMAL,
2336 /* show all information default */
2338 if (lum->lum_magic == LMV_USER_MAGIC)
2339 verbose = VERBOSE_POOL | VERBOSE_COUNT |
2340 VERBOSE_OFFSET | VERBOSE_HASH_TYPE;
2342 verbose = VERBOSE_OBJID;
2345 if (depth && path && ((verbose != VERBOSE_OBJID)))
2346 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2348 if (verbose & VERBOSE_COUNT) {
2349 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2350 if (verbose & ~VERBOSE_COUNT)
2351 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2352 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2353 (int)lum->lum_stripe_count);
2354 if (verbose & VERBOSE_OFFSET)
2360 if (verbose & VERBOSE_OFFSET) {
2361 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2362 if (verbose & ~VERBOSE_OFFSET)
2363 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2364 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2365 (int)lum->lum_stripe_offset);
2366 if (verbose & VERBOSE_HASH_TYPE)
2372 if (verbose & VERBOSE_HASH_TYPE) {
2373 unsigned int type = lum->lum_hash_type;
2375 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2376 if (verbose & ~VERBOSE_HASH_TYPE)
2377 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
2378 if (type < LMV_HASH_TYPE_MAX)
2379 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2380 mdt_hash_name[type]);
2382 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2387 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
2388 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2389 if (obdstripe == 1 && lum->lum_stripe_count > 0)
2390 llapi_printf(LLAPI_MSG_NORMAL,
2391 "mdtidx\t\t FID[seq:oid:ver]\n");
2392 for (i = 0; i < lum->lum_stripe_count; i++) {
2393 int idx = objects[i].lum_mds;
2394 struct lu_fid *fid = &objects[i].lum_fid;
2395 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
2396 llapi_printf(LLAPI_MSG_NORMAL,
2397 "%6u\t\t "DFID"\t\t%s\n",
2399 obdindex == idx ? " *" : "");
2404 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
2405 pool_name[0] != '\0') {
2406 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2407 if (verbose & ~VERBOSE_POOL)
2408 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
2410 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
2414 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
2415 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2418 void llapi_lov_dump_user_lmm(struct find_param *param, char *path, int is_dir)
2422 if (param->fp_get_lmv || param->fp_get_default_lmv)
2423 magic = (__u32)param->fp_lmv_md->lum_magic;
2425 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
2428 case LOV_USER_MAGIC_V1:
2429 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
2430 param->fp_lmd->lmd_lmm.lmm_objects,
2432 param->fp_obd_index, param->fp_max_depth,
2433 param->fp_verbose, param->fp_raw);
2435 case LOV_USER_MAGIC_V3: {
2436 char pool_name[LOV_MAXPOOLNAME + 1];
2437 struct lov_user_ost_data_v1 *objects;
2438 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
2440 strlcpy(pool_name, lmmv3->lmm_pool_name, sizeof(pool_name));
2441 objects = lmmv3->lmm_objects;
2442 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm,
2443 pool_name[0] == '\0' ? NULL : pool_name,
2444 objects, path, is_dir,
2445 param->fp_obd_index, param->fp_max_depth,
2446 param->fp_verbose, param->fp_raw);
2450 case LMV_USER_MAGIC: {
2451 char pool_name[LOV_MAXPOOLNAME + 1];
2452 struct lmv_user_md *lum;
2454 lum = (struct lmv_user_md *)param->fp_lmv_md;
2455 strlcpy(pool_name, lum->lum_pool_name, sizeof(pool_name));
2456 lmv_dump_user_lmm(lum,
2457 pool_name[0] == '\0' ? NULL : pool_name,
2458 path, param->fp_obd_index,
2459 param->fp_max_depth, param->fp_verbose);
2463 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
2464 "(expecting one of %#x %#x %#x %#x)\n",
2465 *(__u32 *)¶m->fp_lmd->lmd_lmm,
2466 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
2467 LMV_USER_MAGIC, LMV_MAGIC_V1);
2472 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
2478 fname = strrchr(path, '/');
2480 /* It should be a file (or other non-directory) */
2481 if (fname == NULL) {
2482 dname = (char *)malloc(2);
2486 fname = (char *)path;
2488 dname = (char *)malloc(fname - path + 1);
2491 strncpy(dname, path, fname - path);
2492 dname[fname - path] = '\0';
2496 fd = open(dname, O_RDONLY | O_NONBLOCK);
2503 strcpy((char *)lum, fname);
2504 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
2507 if (close(fd) == -1 && rc == 0)
2514 int llapi_file_lookup(int dirfd, const char *name)
2516 struct obd_ioctl_data data = { 0 };
2521 if (dirfd < 0 || name == NULL)
2524 data.ioc_version = OBD_IOCTL_VERSION;
2525 data.ioc_len = sizeof(data);
2526 data.ioc_inlbuf1 = (char *)name;
2527 data.ioc_inllen1 = strlen(name) + 1;
2529 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2531 llapi_error(LLAPI_MSG_ERROR, rc,
2532 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
2537 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
2543 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
2544 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
2546 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
2547 * The table below gives the answers for the specified parameters (value and
2548 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
2549 * --------------------------------------
2550 * 1 | file > limit; sign > 0 | -1 / -1 |
2551 * 2 | file = limit; sign > 0 | -1 / -1 |
2552 * 3 | file < limit; sign > 0 | ? / 1 |
2553 * 4 | file > limit; sign = 0 | -1 / -1 |
2554 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
2555 * 6 | file < limit; sign = 0 | ? / -1 |
2556 * 7 | file > limit; sign < 0 | 1 / 1 |
2557 * 8 | file = limit; sign < 0 | ? / -1 |
2558 * 9 | file < limit; sign < 0 | ? / -1 |
2559 * --------------------------------------
2560 * Note: 5th actually means that the value is within the interval
2561 * (limit - margin, limit]. */
2562 static int find_value_cmp(unsigned long long file, unsigned long long limit,
2563 int sign, int negopt, unsigned long long margin,
2569 /* Drop the fraction of margin (of days). */
2570 if (file + margin <= limit)
2572 } else if (sign == 0) {
2573 if (file <= limit && file + margin > limit)
2575 else if (file + margin <= limit)
2577 } else if (sign < 0) {
2584 return negopt ? ~ret + 1 : ret;
2587 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
2588 * Return -1 or 1 if file timestamp does not or does match the given criteria
2589 * correspondingly. Return 0 if the MDS time is being checked and there are
2590 * attributes on OSTs and it is not yet clear if the timespamp matches.
2592 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
2593 * updated timestamps. */
2594 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
2599 /* Check if file is accepted. */
2600 if (param->fp_atime) {
2601 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
2602 param->fp_asign, param->fp_exclude_atime,
2609 if (param->fp_mtime) {
2610 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
2611 param->fp_msign, param->fp_exclude_mtime,
2616 /* If the previous check matches, but this one is not yet clear,
2617 * we should return 0 to do an RPC on OSTs. */
2622 if (param->fp_ctime) {
2623 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
2624 param->fp_csign, param->fp_exclude_ctime,
2629 /* If the previous check matches, but this one is not yet clear,
2630 * we should return 0 to do an RPC on OSTs. */
2639 * Check whether the stripes matches the indexes user provided
2643 static int check_obd_match(struct find_param *param)
2645 lstat_t *st = ¶m->fp_lmd->lmd_st;
2646 struct lov_user_ost_data_v1 *lmm_objects;
2649 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
2652 if (!S_ISREG(st->st_mode))
2655 /* Only those files should be accepted, which have a
2656 * stripe on the specified OST. */
2657 if (!param->fp_lmd->lmd_lmm.lmm_stripe_count)
2660 if (param->fp_lmd->lmd_lmm.lmm_magic ==
2661 LOV_USER_MAGIC_V3) {
2662 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
2664 lmm_objects = lmmv3->lmm_objects;
2665 } else if (param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) {
2666 lmm_objects = param->fp_lmd->lmd_lmm.lmm_objects;
2668 llapi_err_noerrno(LLAPI_MSG_ERROR, "%s:Unknown magic: 0x%08X\n",
2669 __func__, param->fp_lmd->lmd_lmm.lmm_magic);
2673 for (i = 0; i < param->fp_lmd->lmd_lmm.lmm_stripe_count; i++) {
2674 for (j = 0; j < param->fp_num_obds; j++) {
2675 if (param->fp_obd_indexes[j] ==
2676 lmm_objects[i].l_ost_idx) {
2677 if (param->fp_exclude_obd)
2684 if (param->fp_exclude_obd)
2690 static int check_mdt_match(struct find_param *param)
2694 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
2697 /* FIXME: For striped dir, we should get stripe information and check */
2698 for (i = 0; i < param->fp_num_mdts; i++) {
2699 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
2700 return !param->fp_exclude_mdt;
2703 if (param->fp_exclude_mdt)
2710 * Check whether the obd is active or not, if it is
2711 * not active, just print the object affected by this
2714 static int print_failed_tgt(struct find_param *param, char *path, int type)
2716 struct obd_statfs stat_buf;
2717 struct obd_uuid uuid_buf;
2720 LASSERT(type == LL_STATFS_LOV || type == LL_STATFS_LMV);
2722 memset(&stat_buf, 0, sizeof(struct obd_statfs));
2723 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
2724 ret = llapi_obd_statfs(path, type,
2725 param->fp_obd_index, &stat_buf,
2728 llapi_printf(LLAPI_MSG_NORMAL,
2729 "obd_uuid: %s failed %s ",
2730 param->fp_obd_uuid->uuid,
2737 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
2738 void *data, struct dirent64 *de)
2740 struct find_param *param = (struct find_param *)data;
2741 DIR *dir = dirp == NULL ? NULL : *dirp;
2742 int decision = 1; /* 1 is accepted; -1 is rejected. */
2743 lstat_t *st = ¶m->fp_lmd->lmd_st;
2745 int checked_type = 0;
2748 LASSERT(parent != NULL || dir != NULL);
2750 param->fp_lmd->lmd_lmm.lmm_stripe_count = 0;
2752 /* If a regular expression is presented, make the initial decision */
2753 if (param->fp_pattern != NULL) {
2754 char *fname = strrchr(path, '/');
2755 fname = (fname == NULL ? path : fname + 1);
2756 ret = fnmatch(param->fp_pattern, fname, 0);
2757 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
2758 (ret == 0 && param->fp_exclude_pattern))
2762 /* See if we can check the file type from the dirent. */
2763 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
2766 if (DTTOIF(de->d_type) == param->fp_type) {
2767 if (param->fp_exclude_type)
2770 if (!param->fp_exclude_type)
2777 /* Request MDS for the stat info if some of these parameters need
2778 * to be compared. */
2779 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
2780 param->fp_check_uid || param->fp_check_gid ||
2781 param->fp_atime || param->fp_mtime || param->fp_ctime ||
2782 param->fp_check_pool || param->fp_check_size ||
2783 param->fp_check_stripe_count || param->fp_check_stripe_size ||
2784 param->fp_check_layout)
2787 if (param->fp_type != 0 && checked_type == 0)
2790 if (decision == 0) {
2791 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
2792 param->fp_lum_size);
2793 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
2794 (param->fp_check_pool || param->fp_check_stripe_count ||
2795 param->fp_check_stripe_size || param->fp_check_layout)) {
2796 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
2798 /* We need to "fake" the "use the default" values
2799 * since the lmm struct is zeroed out at this point. */
2800 lmm->lmm_magic = LOV_USER_MAGIC_V1;
2801 lmm->lmm_pattern = 0xFFFFFFFF;
2803 ostid_set_seq(&lmm->lmm_oi,
2804 FID_SEQ_LOV_DEFAULT);
2805 lmm->lmm_stripe_size = 0;
2806 lmm->lmm_stripe_count = 0;
2807 lmm->lmm_stripe_offset = -1;
2809 if (ret == 0 && param->fp_mdt_uuid != NULL) {
2811 ret = llapi_file_fget_mdtidx(dirfd(dir),
2812 ¶m->fp_file_mdt_index);
2813 } else if (S_ISREG(st->st_mode)) {
2816 /* FIXME: we could get the MDT index from the
2817 * file's FID in lmd->lmd_lmm.lmm_oi without
2818 * opening the file, once we are sure that
2819 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
2820 * That would still be an ioctl() to map the
2821 * FID to the MDT, but not an open RPC. */
2822 fd = open(path, O_RDONLY);
2824 ret = llapi_file_fget_mdtidx(fd,
2825 ¶m->fp_file_mdt_index);
2831 /* For a special file, we assume it resides on
2832 * the same MDT as the parent directory. */
2833 ret = llapi_file_fget_mdtidx(dirfd(parent),
2834 ¶m->fp_file_mdt_index);
2847 if (param->fp_type && !checked_type) {
2848 if ((st->st_mode & S_IFMT) == param->fp_type) {
2849 if (param->fp_exclude_type)
2852 if (!param->fp_exclude_type)
2858 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
2859 if (lustre_fs && param->fp_got_uuids &&
2860 param->fp_dev != st->st_dev) {
2861 /* A lustre/lustre mount point is crossed. */
2862 param->fp_got_uuids = 0;
2863 param->fp_obds_printed = 0;
2864 param->fp_mdt_index = OBD_NOT_FOUND;
2865 param->fp_obd_index = OBD_NOT_FOUND;
2868 if (lustre_fs && !param->fp_got_uuids) {
2869 ret = setup_target_indexes(dir ? dir : parent, path,
2874 param->fp_dev = st->st_dev;
2875 } else if (!lustre_fs && param->fp_got_uuids) {
2876 /* A lustre/non-lustre mount point is crossed. */
2877 param->fp_got_uuids = 0;
2878 param->fp_mdt_index = OBD_NOT_FOUND;
2879 param->fp_obd_index = OBD_NOT_FOUND;
2883 if (param->fp_check_stripe_size) {
2884 decision = find_value_cmp(
2885 param->fp_lmd->lmd_lmm.lmm_stripe_size,
2886 param->fp_stripe_size,
2887 param->fp_stripe_size_sign,
2888 param->fp_exclude_stripe_size,
2889 param->fp_stripe_size_units, 0);
2894 if (param->fp_check_stripe_count) {
2895 decision = find_value_cmp(
2896 param->fp_lmd->lmd_lmm.lmm_stripe_count,
2897 param->fp_stripe_count,
2898 param->fp_stripe_count_sign,
2899 param->fp_exclude_stripe_count, 1, 0);
2904 if (param->fp_check_layout) {
2907 found = (param->fp_lmd->lmd_lmm.lmm_pattern & param->fp_layout);
2908 if ((param->fp_lmd->lmd_lmm.lmm_pattern == 0xFFFFFFFF) ||
2909 (found && param->fp_exclude_layout) ||
2910 (!found && !param->fp_exclude_layout)) {
2916 /* If an OBD UUID is specified but none matches, skip this file. */
2917 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
2918 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
2921 /* If an OST or MDT UUID is given, and some OST matches,
2923 if (param->fp_obd_index != OBD_NOT_FOUND ||
2924 param->fp_mdt_index != OBD_NOT_FOUND) {
2925 if (param->fp_obd_uuid) {
2926 if (check_obd_match(param)) {
2927 /* If no mdtuuid is given, we are done.
2928 * Otherwise, fall through to the mdtuuid
2930 if (!param->fp_mdt_uuid)
2937 if (param->fp_mdt_uuid) {
2938 if (check_mdt_match(param))
2945 if (param->fp_check_uid) {
2946 if (st->st_uid == param->fp_uid) {
2947 if (param->fp_exclude_uid)
2950 if (!param->fp_exclude_uid)
2955 if (param->fp_check_gid) {
2956 if (st->st_gid == param->fp_gid) {
2957 if (param->fp_exclude_gid)
2960 if (!param->fp_exclude_gid)
2965 if (param->fp_check_pool) {
2966 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
2968 /* empty requested pool is taken as no pool search => V1 */
2969 if (((param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
2970 (param->fp_poolname[0] == '\0')) ||
2971 ((param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
2972 (strncmp(lmmv3->lmm_pool_name,
2973 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
2974 ((param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
2975 (strcmp(param->fp_poolname, "*") == 0))) {
2976 if (param->fp_exclude_pool)
2979 if (!param->fp_exclude_pool)
2984 /* Check the time on mds. */
2986 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
2989 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
2990 param->fp_lmd->lmd_lmm.lmm_stripe_count)
2992 decision = find_time_check(st, param, for_mds);
2997 /* If file still fits the request, ask ost for updated info.
2998 The regular stat is almost of the same speed as some new
2999 'glimpse-size-ioctl'. */
3001 if (param->fp_check_size && S_ISREG(st->st_mode) &&
3002 param->fp_lmd->lmd_lmm.lmm_stripe_count)
3005 if (param->fp_check_size && S_ISDIR(st->st_mode))
3009 /* For regular files with the stripe the decision may have not
3010 * been taken yet if *time or size is to be checked. */
3011 if (param->fp_obd_index != OBD_NOT_FOUND)
3012 print_failed_tgt(param, path, LL_STATFS_LOV);
3014 if (param->fp_mdt_index != OBD_NOT_FOUND)
3015 print_failed_tgt(param, path, LL_STATFS_LMV);
3018 ret = fstat_f(dirfd(dir), st);
3019 else if (de != NULL)
3020 ret = fstatat_f(dirfd(parent), de->d_name, st,
3021 AT_SYMLINK_NOFOLLOW);
3023 ret = lstat_f(path, st);
3026 if (errno == ENOENT) {
3027 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
3028 "warning: %s: %s does not exist",
3033 llapi_error(LLAPI_MSG_ERROR, ret,
3034 "%s: IOC_LOV_GETINFO on %s failed",
3040 /* Check the time on osc. */
3041 decision = find_time_check(st, param, 0);
3046 if (param->fp_check_size)
3047 decision = find_value_cmp(st->st_size, param->fp_size,
3048 param->fp_size_sign,
3049 param->fp_exclude_size,
3050 param->fp_size_units, 0);
3052 if (decision != -1) {
3053 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
3054 if (param->fp_zero_end)
3055 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
3057 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3061 /* Do not get down anymore? */
3062 if (param->fp_depth == param->fp_max_depth)
3070 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
3071 void *param_data, struct dirent64 *de)
3073 struct find_param *param = (struct find_param *)param_data;
3074 DIR *tmp_parent = parent;
3075 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3077 struct obd_ioctl_data data = { 0 };
3084 LASSERT(parent != NULL || dirp != NULL);
3088 if (parent == NULL) {
3089 tmp_parent = opendir_parent(path);
3090 if (tmp_parent == NULL) {
3093 llapi_error(LLAPI_MSG_ERROR, ret,
3094 "can not open %s", path);
3099 fd = dirfd(tmp_parent);
3101 path_copy = strdup(path);
3102 filename = basename(path_copy);
3103 data.ioc_inlbuf1 = (char *)filename;
3104 data.ioc_inllen1 = strlen(filename) + 1;
3105 data.ioc_inlbuf2 = (char *)¶m->fp_mdt_index;
3106 data.ioc_inllen2 = sizeof(param->fp_mdt_index);
3107 ret = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3109 llapi_error(LLAPI_MSG_ERROR, ret,
3110 "llapi_obd_statfs: error packing ioctl data");
3115 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
3117 if (errno == EBUSY && !retry) {
3118 /* because migrate may not be able to lock all involved
3119 * objects in order, for some of them it try lock, while
3120 * there may be conflicting COS locks and cause migrate
3121 * fail with EBUSY, hope a sync() could cause
3122 * transaction commit and release these COS locks. */
3128 fprintf(stderr, "%s migrate failed: %s (%d)\n",
3129 path, strerror(-ret), ret);
3131 } else if (param->fp_verbose & VERBOSE_DETAIL) {
3132 fprintf(stdout, "migrate %s to MDT%d\n",
3133 path, param->fp_mdt_index);
3138 /* If the directory is being migration, we need
3139 * close the directory after migration,
3140 * so the old directory cache will be cleanup
3141 * on the client side, and re-open to get the
3142 * new directory handle */
3143 *dirp = opendir(path);
3144 if (*dirp == NULL) {
3146 llapi_error(LLAPI_MSG_ERROR, ret,
3147 "%s: Failed to open '%s'", __func__, path);
3152 closedir(tmp_parent);
3159 int llapi_migrate_mdt(char *path, struct find_param *param)
3161 return param_callback(path, cb_migrate_mdt_init, cb_common_fini, param);
3164 int llapi_mv(char *path, struct find_param *param)
3166 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 53, 0)
3167 static bool printed;
3170 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
3171 "llapi_mv() is deprecated, use llapi_migrate_mdt()\n");
3175 return llapi_migrate_mdt(path, param);
3178 int llapi_find(char *path, struct find_param *param)
3180 return param_callback(path, cb_find_init, cb_common_fini, param);
3184 * Get MDT number that the file/directory inode referenced
3185 * by the open fd resides on.
3186 * Return 0 and mdtidx on success, or -ve errno.
3188 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
3190 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
3195 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
3196 struct dirent64 *de)
3198 struct find_param *param = (struct find_param *)data;
3199 DIR *d = dirp == NULL ? NULL : *dirp;
3203 LASSERT(parent != NULL || d != NULL);
3206 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3207 } else /* if (parent) */ {
3210 fd = open(path, O_RDONLY | O_NOCTTY);
3212 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
3220 if (ret == -ENODATA) {
3221 if (!param->fp_obd_uuid)
3222 llapi_printf(LLAPI_MSG_NORMAL,
3223 "'%s' has no stripe info\n", path);
3225 } else if (ret == -ENOENT) {
3226 llapi_error(LLAPI_MSG_WARN, ret,
3227 "warning: %s: '%s' does not exist",
3230 } else if (ret == -ENOTTY) {
3231 llapi_error(LLAPI_MSG_ERROR, ret,
3232 "%s: '%s' not on a Lustre fs",
3235 llapi_error(LLAPI_MSG_ERROR, ret,
3236 "error: %s: '%s' failed get_mdtidx",
3242 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
3243 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
3245 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
3249 /* Do not go down anymore? */
3250 if (param->fp_depth == param->fp_max_depth)
3258 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
3259 struct dirent64 *de)
3261 struct find_param *param = (struct find_param *)data;
3262 DIR *d = dirp == NULL ? NULL : *dirp;
3265 LASSERT(parent != NULL || d != NULL);
3267 if (param->fp_obd_uuid) {
3268 param->fp_quiet = 1;
3269 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
3275 if (param->fp_get_lmv || param->fp_get_default_lmv) {
3276 ret = cb_get_dirstripe(path, d, param);
3278 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
3279 (void *)¶m->fp_lmd->lmd_lmm);
3282 } else if (parent && !param->fp_get_lmv && !param->fp_get_default_lmv) {
3283 char *fname = strrchr(path, '/');
3284 fname = (fname == NULL ? path : fname + 1);
3286 strlcpy((char *)¶m->fp_lmd->lmd_lmm, fname,
3287 param->fp_lum_size);
3289 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
3290 (void *)¶m->fp_lmd->lmd_lmm);
3296 if (errno == ENODATA && d != NULL) {
3297 /* We need to "fake" the "use the default" values
3298 * since the lmm struct is zeroed out at this point.
3299 * The magic needs to be set in order to satisfy
3300 * a check later on in the code path.
3301 * The object_seq needs to be set for the "(Default)"
3302 * prefix to be displayed. */
3303 if (param->fp_get_default_lmv) {
3304 struct lmv_user_md *lum = param->fp_lmv_md;
3306 lum->lum_magic = LMV_USER_MAGIC;
3307 lum->lum_stripe_count = 0;
3308 lum->lum_stripe_offset = -1;
3310 } else if (param->fp_get_lmv) {
3311 struct lmv_user_md *lum = param->fp_lmv_md;
3314 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3317 lum->lum_magic = LMV_MAGIC_V1;
3318 lum->lum_stripe_count = 0;
3319 lum->lum_stripe_offset = mdtidx;
3322 struct lov_user_md *lmm =
3323 ¶m->fp_lmd->lmd_lmm;
3325 lmm->lmm_magic = LOV_USER_MAGIC_V1;
3327 ostid_set_seq(&lmm->lmm_oi,
3328 FID_SEQ_LOV_DEFAULT);
3329 lmm->lmm_stripe_count = 0;
3330 lmm->lmm_stripe_size = 0;
3331 lmm->lmm_stripe_offset = -1;
3334 } else if (errno == ENODATA && parent != NULL) {
3335 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
3336 llapi_printf(LLAPI_MSG_NORMAL,
3337 "%s has no stripe info\n", path);
3339 } else if (errno == ENOENT) {
3340 llapi_error(LLAPI_MSG_WARN, -ENOENT,
3341 "warning: %s: %s does not exist",
3344 } else if (errno == ENOTTY) {
3346 llapi_error(LLAPI_MSG_ERROR, ret,
3347 "%s: '%s' not on a Lustre fs?",
3352 llapi_error(LLAPI_MSG_ERROR, ret,
3353 "error: %s: %s failed for %s",
3354 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
3355 "IOC_MDC_GETFILESTRIPE", path);
3362 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
3363 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
3366 /* Do not get down anymore? */
3367 if (param->fp_depth == param->fp_max_depth)
3375 int llapi_getstripe(char *path, struct find_param *param)
3377 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
3378 cb_get_mdt_index : cb_getstripe,
3379 cb_common_fini, param);
3382 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
3383 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
3385 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3387 struct obd_ioctl_data data = { 0 };
3390 data.ioc_inlbuf1 = (char *)&type;
3391 data.ioc_inllen1 = sizeof(__u32);
3392 data.ioc_inlbuf2 = (char *)&index;
3393 data.ioc_inllen2 = sizeof(__u32);
3394 data.ioc_pbuf1 = (char *)stat_buf;
3395 data.ioc_plen1 = sizeof(struct obd_statfs);
3396 data.ioc_pbuf2 = (char *)uuid_buf;
3397 data.ioc_plen2 = sizeof(struct obd_uuid);
3399 rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3401 llapi_error(LLAPI_MSG_ERROR, rc,
3402 "llapi_obd_statfs: error packing ioctl data");
3406 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
3408 return rc < 0 ? -errno : 0;
3411 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
3412 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
3417 fd = open(path, O_RDONLY);
3420 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
3422 /* If we can't even open a file on the filesystem (e.g. with
3423 * -ESHUTDOWN), force caller to exit or it will loop forever. */
3427 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
3434 #define MAX_STRING_SIZE 128
3436 int llapi_ping(char *obd_type, char *obd_name)
3442 rc = cfs_get_param_paths(&path, "%s/%s/ping",
3443 obd_type, obd_name);
3447 fd = open(path.gl_pathv[0], O_WRONLY);
3450 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
3455 /* The purpose is to send a byte as a ping, whatever this byte is. */
3456 /* coverity[uninit_use_in_call] */
3457 rc = write(fd, buf, 1);
3465 cfs_free_param_data(&path);
3469 int llapi_target_iterate(int type_num, char **obd_type,
3470 void *args, llapi_cb_t cb)
3472 char buf[MAX_STRING_SIZE];
3477 rc = cfs_get_param_paths(¶m, "devices");
3481 fp = fopen(param.gl_pathv[0], "r");
3484 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
3489 while (fgets(buf, sizeof(buf), fp) != NULL) {
3490 char *obd_type_name = NULL;
3491 char *obd_name = NULL;
3492 char *obd_uuid = NULL;
3494 struct obd_statfs osfs_buffer;
3496 while(bufp[0] == ' ')
3499 for(i = 0; i < 3; i++) {
3500 obd_type_name = strsep(&bufp, " ");
3502 obd_name = strsep(&bufp, " ");
3503 obd_uuid = strsep(&bufp, " ");
3505 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
3507 for (i = 0; i < type_num; i++) {
3508 if (strcmp(obd_type_name, obd_type[i]) != 0)
3511 cb(obd_type_name, obd_name, obd_uuid, args);
3516 cfs_free_param_data(¶m);
3520 static void do_target_check(char *obd_type_name, char *obd_name,
3521 char *obd_uuid, void *args)
3525 rc = llapi_ping(obd_type_name, obd_name);
3526 if (rc == ENOTCONN) {
3527 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
3529 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
3531 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
3535 int llapi_target_check(int type_num, char **obd_type, char *dir)
3537 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
3540 #undef MAX_STRING_SIZE
3542 /* Is this a lustre fs? */
3543 int llapi_is_lustre_mnttype(const char *type)
3545 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
3548 /* Is this a lustre client fs? */
3549 int llapi_is_lustre_mnt(struct mntent *mnt)
3551 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
3552 strstr(mnt->mnt_fsname, ":/") != NULL);
3555 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
3557 char fsname[PATH_MAX + 1];
3561 rc = llapi_search_fsname(mnt, fsname);
3563 llapi_err_noerrno(LLAPI_MSG_ERROR,
3564 "'%s' isn't on Lustre filesystem", mnt);
3568 root = open(mnt, O_RDONLY | O_DIRECTORY);
3571 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3575 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
3583 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
3584 * format must have %s%s, buf must be > 16
3585 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
3586 * then buf = "lustre-MDT0000"
3588 static int get_mdtname(char *name, char *format, char *buf)
3590 char suffix[]="-MDT0000";
3591 int len = strlen(name);
3593 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
3594 name[len - 5] = '\0';
3599 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
3602 /* Not enough room to add suffix */
3603 llapi_err_noerrno(LLAPI_MSG_ERROR,
3604 "MDT name too long |%s|", name);
3609 return sprintf(buf, format, name, suffix);
3612 /** ioctl on filsystem root, with mdtindex sent as data
3613 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
3614 * \param mdtidxp pointer to integer within data to be filled in with the
3615 * mdt index (0 if no mdt is specified). NULL won't be filled.
3617 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
3625 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
3626 Open root and parse mdt index. */
3627 if (mdtname[0] == '/') {
3629 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
3630 (char *)mdtname, -1);
3632 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
3634 ptr = fsname + strlen(fsname) - 8;
3636 index = strtol(ptr + 4, NULL, 10);
3637 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
3641 llapi_err_noerrno(LLAPI_MSG_ERROR,
3642 "Can't open %s: %d\n", mdtname, rc);
3649 rc = ioctl(fd, opc, data);
3658 /****** Changelog API ********/
3660 static int changelog_ioctl(const char *mdtname, int opc, int id,
3661 long long recno, int flags)
3663 struct ioc_changelog data;
3667 data.icc_recno = recno;
3668 data.icc_flags = flags;
3669 idx = (int *)(&data.icc_mdtindex);
3671 return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR);
3674 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
3675 struct changelog_private {
3677 enum changelog_send_flag flags;
3678 struct lustre_kernelcomm kuc;
3681 /** Start reading from a changelog
3682 * @param priv Opaque private control structure
3683 * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
3684 * @param device Report changes recorded on this MDT
3685 * @param startrec Report changes beginning with this record number
3686 * (just call llapi_changelog_fini when done; don't need an endrec)
3688 int llapi_changelog_start(void **priv, enum changelog_send_flag flags,
3689 const char *device, long long startrec)
3691 struct changelog_private *cp;
3695 /* Set up the receiver control struct */
3696 cp = calloc(1, sizeof(*cp));
3700 cp->magic = CHANGELOG_PRIV_MAGIC;
3703 /* Set up the receiver */
3704 rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */, 0);
3710 /* CHANGELOG_FLAG_JOBID will eventually become mandatory. Display a
3711 * warning if it's missing. */
3712 if (!(flags & CHANGELOG_FLAG_JOBID) && !warned) {
3713 llapi_err_noerrno(LLAPI_MSG_WARN, "warning: %s() called "
3714 "w/o CHANGELOG_FLAG_JOBID", __func__);
3718 /* Tell the kernel to start sending */
3719 rc = changelog_ioctl(device, OBD_IOC_CHANGELOG_SEND, cp->kuc.lk_wfd,
3721 /* Only the kernel reference keeps the write side open */
3722 close(cp->kuc.lk_wfd);
3723 cp->kuc.lk_wfd = LK_NOFD;
3725 /* frees and clears priv */
3726 llapi_changelog_fini(priv);
3737 /** Finish reading from a changelog */
3738 int llapi_changelog_fini(void **priv)
3740 struct changelog_private *cp = (struct changelog_private *)*priv;
3742 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
3745 libcfs_ukuc_stop(&cp->kuc);
3752 * Convert all records to a same format according to the caller's wishes.
3753 * Default is CLF_VERSION | CLF_RENAME.
3754 * Add CLF_JOBID if explicitely requested.
3756 * \param rec The record to remap. It is expected to be big enough to
3757 * properly handle the final format.
3758 * \return 1 if anything changed. 0 otherwise.
3760 /** Read the next changelog entry
3761 * @param priv Opaque private control structure
3762 * @param rech Changelog record handle; record will be allocated here
3763 * @return 0 valid message received; rec is set
3767 #define DEFAULT_RECORD_FMT (CLF_VERSION | CLF_RENAME)
3768 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
3770 struct changelog_private *cp = (struct changelog_private *)priv;
3771 struct kuc_hdr *kuch;
3772 enum changelog_rec_flags rec_fmt = DEFAULT_RECORD_FMT;
3775 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
3779 kuch = malloc(KUC_CHANGELOG_MSG_MAXSIZE);
3783 if (cp->flags & CHANGELOG_FLAG_JOBID)
3784 rec_fmt |= CLF_JOBID;
3787 rc = libcfs_ukuc_msg_get(&cp->kuc, (char *)kuch,
3788 KUC_CHANGELOG_MSG_MAXSIZE,
3789 KUC_TRANSPORT_CHANGELOG);
3793 if ((kuch->kuc_transport != KUC_TRANSPORT_CHANGELOG) ||
3794 ((kuch->kuc_msgtype != CL_RECORD) &&
3795 (kuch->kuc_msgtype != CL_EOF))) {
3796 llapi_err_noerrno(LLAPI_MSG_ERROR,
3797 "Unknown changelog message type %d:%d\n",
3798 kuch->kuc_transport, kuch->kuc_msgtype);
3803 if (kuch->kuc_msgtype == CL_EOF) {
3804 if (cp->flags & CHANGELOG_FLAG_FOLLOW) {
3813 /* Our message is a changelog_rec. Use pointer math to skip
3814 * kuch_hdr and point directly to the message payload. */
3815 *rech = (struct changelog_rec *)(kuch + 1);
3816 changelog_remap_rec(*rech, rec_fmt);
3826 /** Release the changelog record when done with it. */
3827 int llapi_changelog_free(struct changelog_rec **rech)
3830 /* We allocated memory starting at the kuc_hdr, but passed
3831 * the consumer a pointer to the payload.
3832 * Use pointer math to get back to the header.
3834 struct kuc_hdr *kuch = (struct kuc_hdr *)*rech - 1;
3841 int llapi_changelog_clear(const char *mdtname, const char *idstr,
3847 llapi_err_noerrno(LLAPI_MSG_ERROR,
3848 "can't purge negative records\n");
3852 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
3853 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
3854 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
3855 llapi_err_noerrno(LLAPI_MSG_ERROR,
3856 "expecting id of the form '"
3857 CHANGELOG_USER_PREFIX
3858 "<num>'; got '%s'\n", idstr);
3862 return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0);
3865 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
3866 int buflen, long long *recno, int *linkno)
3868 const char *fidstr_orig = fidstr;
3870 struct getinfo_fid2path *gf;
3873 while (*fidstr == '[')
3876 sscanf(fidstr, SFID, RFID(&fid));
3877 if (!fid_is_sane(&fid)) {
3878 llapi_err_noerrno(LLAPI_MSG_ERROR,
3879 "bad FID format '%s', should be [seq:oid:ver]"
3880 " (e.g. "DFID")\n", fidstr_orig,
3881 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
3885 gf = malloc(sizeof(*gf) + buflen);
3890 gf->gf_recno = *recno;
3891 gf->gf_linkno = *linkno;
3892 gf->gf_pathlen = buflen;
3894 /* Take path or fsname */
3895 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
3899 memcpy(buf, gf->gf_u.gf_path, gf->gf_pathlen);
3900 if (buf[0] == '\0') { /* ROOT path */
3904 *recno = gf->gf_recno;
3905 *linkno = gf->gf_linkno;
3912 static int fid_from_lma(const char *path, const int fd, lustre_fid *fid)
3915 struct lustre_mdt_attrs *lma;
3919 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
3921 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
3924 lma = (struct lustre_mdt_attrs *)buf;
3925 fid_le_to_cpu(fid, &lma->lma_self_fid);
3929 int llapi_get_mdt_index_by_fid(int fd, const lustre_fid *fid,
3934 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
3943 int llapi_fd2fid(const int fd, lustre_fid *fid)
3947 memset(fid, 0, sizeof(*fid));
3949 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
3950 if (rc == -EINVAL || rc == -ENOTTY)
3951 rc = fid_from_lma(NULL, fd, fid);
3956 int llapi_path2fid(const char *path, lustre_fid *fid)
3960 memset(fid, 0, sizeof(*fid));
3961 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
3963 if (errno == ELOOP || errno == ENXIO)
3964 return fid_from_lma(path, -1, fid);
3968 rc = llapi_fd2fid(fd, fid);
3969 if (rc == -EINVAL || rc == -ENOTTY)
3970 rc = fid_from_lma(path, -1, fid);
3976 int llapi_fd2parent(int fd, unsigned int linkno, lustre_fid *parent_fid,
3977 char *name, size_t name_size)
3979 struct getparent *gp;
3982 gp = malloc(sizeof(*gp) + name_size);
3986 gp->gp_linkno = linkno;
3987 gp->gp_name_size = name_size;
3989 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
3995 *parent_fid = gp->gp_fid;
3997 strncpy(name, gp->gp_name, name_size);
3998 name[name_size - 1] = '\0';
4005 int llapi_path2parent(const char *path, unsigned int linkno,
4006 lustre_fid *parent_fid, char *name, size_t name_size)
4011 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
4015 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
4020 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
4025 root = open(mnt, O_RDONLY | O_DIRECTORY);
4028 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
4032 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
4035 llapi_error(LLAPI_MSG_ERROR, rc,
4036 "ioctl on %s for getting connect flags failed", mnt);
4043 * Get a 64-bit value representing the version of file data pointed by fd.
4045 * Each write or truncate, flushed on OST, will change this value. You can use
4046 * this value to verify if file data was modified. This only checks the file
4047 * data, not metadata.
4049 * \param flags 0: no flush pages, usually used it the process has already
4051 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
4053 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
4054 * pages from clients.
4056 * \retval 0 on success.
4057 * \retval -errno on error.
4059 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
4062 struct ioc_data_version idv;
4064 idv.idv_flags = flags;
4066 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
4070 *data_version = idv.idv_version;
4076 * Create a file without any name open it for read/write
4078 * - file is created as if it were a standard file in the given \a directory
4079 * - file does not appear in \a directory and mtime does not change because
4080 * the filename is handled specially by the Lustre MDS.
4081 * - file is removed at final close
4082 * - file modes are rw------- since it doesn't make sense to have a read-only
4083 * or write-only file that cannot be opened again.
4084 * - if user wants another mode it must use fchmod() on the open file, no
4085 * security problems arise because it cannot be opened by another process.
4087 * \param[in] directory directory from which to inherit layout/MDT idx
4088 * \param[in] idx MDT index on which the file is created,
4089 * \a idx == -1 means no specific MDT is requested
4090 * \param[in] open_flags standard open(2) flags
4092 * \retval 0 on success.
4093 * \retval -errno on error.
4095 int llapi_create_volatile_idx(char *directory, int idx, int open_flags)
4097 char file_path[PATH_MAX];
4098 char filename[PATH_MAX];
4099 int saved_errno = errno;
4107 snprintf(filename, sizeof(filename),
4108 LUSTRE_VOLATILE_HDR"::%.4X", rnumber);
4110 snprintf(filename, sizeof(filename),
4111 LUSTRE_VOLATILE_HDR":%.4X:%.4X", idx, rnumber);
4113 rc = snprintf(file_path, sizeof(file_path),
4114 "%s/%s", directory, filename);
4115 if (rc >= sizeof(file_path))
4118 fd = open(file_path,
4119 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | open_flags,
4121 } while (fd < 0 && errno == EEXIST);
4124 llapi_error(LLAPI_MSG_ERROR, errno,
4125 "Cannot create volatile file '%s' in '%s'",
4126 filename + LUSTRE_VOLATILE_HDR_LEN,
4131 /* Unlink file in case this wasn't a Lustre filesystem and the
4132 * magic volatile filename wasn't handled as intended. The
4133 * effect is the same. If volatile open was supported then we
4134 * expect unlink() to return -ENOENT. */
4135 (void)unlink(file_path);
4137 /* Since we are returning successfully we restore errno (and
4138 * mask out possible EEXIST from open() and ENOENT from
4140 errno = saved_errno;
4146 * Swap the layouts between 2 file descriptors
4147 * the 2 files must be open for writing
4148 * first fd received the ioctl, second fd is passed as arg
4149 * this is assymetric but avoid use of root path for ioctl
4151 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
4152 int gid, __u64 flags)
4154 struct lustre_swap_layouts lsl;
4159 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
4160 rc = fstat(fd1, &st1);
4164 rc = fstat(fd2, &st2);
4169 lsl.sl_flags = flags;
4173 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
4177 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
4178 struct timeval tv1[2];
4179 struct timeval tv2[2];
4181 memset(tv1, 0, sizeof(tv1));
4182 memset(tv2, 0, sizeof(tv2));
4184 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
4185 tv1[0].tv_sec = st1.st_atime;
4186 tv2[0].tv_sec = st2.st_atime;
4188 tv1[0].tv_sec = st2.st_atime;
4189 tv2[0].tv_sec = st1.st_atime;
4192 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
4193 tv1[1].tv_sec = st1.st_mtime;
4194 tv2[1].tv_sec = st2.st_mtime;
4196 tv1[1].tv_sec = st2.st_mtime;
4197 tv2[1].tv_sec = st1.st_mtime;
4200 rc = futimes(fd1, tv1);
4204 rc = futimes(fd2, tv2);
4212 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
4219 while (grp_id == 0);
4221 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
4229 * Swap the layouts between 2 files
4230 * the 2 files are open in write
4232 int llapi_swap_layouts(const char *path1, const char *path2,
4233 __u64 dv1, __u64 dv2, __u64 flags)
4237 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
4240 llapi_error(LLAPI_MSG_ERROR, rc,
4241 "error: cannot open '%s' for write", path1);
4245 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
4248 llapi_error(LLAPI_MSG_ERROR, rc,
4249 "error: cannot open '%s' for write", path2);
4253 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
4255 llapi_error(LLAPI_MSG_ERROR, rc,
4256 "error: cannot swap layout between '%s' and '%s'",
4267 * Attempt to open a file with Lustre file identifier \a fid
4268 * and return an open file descriptor.
4270 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
4271 * \param[in] fid Lustre file identifier of file to open
4272 * \param[in] flags open() flags
4274 * \retval non-negative file descriptor on successful open
4275 * \retval -1 if an error occurred
4277 int llapi_open_by_fid(const char *lustre_dir, const lustre_fid *fid, int flags)
4279 char mntdir[PATH_MAX];
4280 char path[PATH_MAX];
4283 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
4287 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
4288 return open(path, flags);
4294 * \param fd File to lock.
4295 * \param gid Group Identifier.
4297 * \retval 0 on success.
4298 * \retval -errno on failure.
4300 int llapi_group_lock(int fd, int gid)
4304 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
4307 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
4315 * \param fd File to unlock.
4316 * \param gid Group Identifier.
4318 * \retval 0 on success.
4319 * \retval -errno on failure.
4321 int llapi_group_unlock(int fd, int gid)
4325 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
4328 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");