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, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/utils/liblustreapi.c
33 * Author: Peter J. Braam <braam@clusterfs.com>
34 * Author: Phil Schwan <phil@clusterfs.com>
35 * 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>
61 #include <sys/sysmacros.h>
64 #include <libgen.h> /* for dirname() */
65 #include <linux/limits.h>
66 #ifdef HAVE_LINUX_UNISTD_H
67 #include <linux/unistd.h>
75 #include <libcfs/util/ioctl.h>
76 #include <libcfs/util/param.h>
77 #include <libcfs/util/string.h>
78 #include <linux/lnet/lnetctl.h>
79 #include <lustre/lustreapi.h>
80 #include <linux/lustre/lustre_ostid.h>
81 #include <linux/lustre/lustre_ioctl.h>
82 #include "lustreapi_internal.h"
85 static int llapi_msg_level = LLAPI_MSG_MAX;
86 const char *liblustreapi_cmd;
88 struct lustre_foreign_type lu_foreign_types[] = {
89 {.lft_type = LU_FOREIGN_TYPE_NONE, .lft_name = "none"},
90 {.lft_type = LU_FOREIGN_TYPE_SYMLINK, .lft_name = "symlink"},
91 /* must be the last element */
92 {.lft_type = LU_FOREIGN_TYPE_UNKNOWN, .lft_name = NULL}
93 /* array max dimension must be <= UINT32_MAX */
96 void llapi_msg_set_level(int level)
98 /* ensure level is in the good range */
99 if (level < LLAPI_MSG_OFF)
100 llapi_msg_level = LLAPI_MSG_OFF;
101 else if (level > LLAPI_MSG_MAX)
102 llapi_msg_level = LLAPI_MSG_MAX;
104 llapi_msg_level = level;
107 int llapi_msg_get_level(void)
109 return llapi_msg_level;
112 void llapi_set_command_name(const char *cmd)
114 liblustreapi_cmd = cmd;
117 void llapi_clear_command_name(void)
119 liblustreapi_cmd = NULL;
122 static void error_callback_default(enum llapi_message_level level, int err,
123 const char *fmt, va_list ap)
125 bool has_nl = strchr(fmt, '\n') != NULL;
127 if (liblustreapi_cmd != NULL)
128 fprintf(stderr, "%s %s: ", program_invocation_short_name,
131 fprintf(stderr, "%s: ", program_invocation_short_name);
134 if (level & LLAPI_MSG_NO_ERRNO) {
135 vfprintf(stderr, fmt, ap);
137 fprintf(stderr, "\n");
142 * Remove trailing linefeed so error string can be appended.
143 * @fmt is a const string, so we can't modify it directly.
145 if (has_nl && (newfmt = strdup(fmt)))
146 *strrchr(newfmt, '\n') = '\0';
148 newfmt = (char *)fmt;
150 vfprintf(stderr, newfmt, ap);
153 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
157 static void info_callback_default(enum llapi_message_level level, int err,
158 const char *fmt, va_list ap)
161 if (liblustreapi_cmd != NULL) {
162 fprintf(stdout, "%s %s: ",
163 program_invocation_short_name,
166 fprintf(stdout, "%s: ", program_invocation_short_name);
169 vfprintf(stdout, fmt, ap);
172 static llapi_log_callback_t llapi_error_callback = error_callback_default;
173 static llapi_log_callback_t llapi_info_callback = info_callback_default;
176 /* llapi_error will preserve errno */
177 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
180 int tmp_errno = errno;
182 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
186 llapi_error_callback(level, abs(err), fmt, args);
191 /* llapi_printf will preserve errno */
192 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
195 int tmp_errno = errno;
197 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
201 llapi_info_callback(level, 0, fmt, args);
207 * Set a custom error logging function. Passing in NULL will reset the logging
208 * callback to its default value.
210 * This function returns the value of the old callback.
212 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
214 llapi_log_callback_t old = llapi_error_callback;
217 llapi_error_callback = cb;
219 llapi_error_callback = error_callback_default;
225 * Set a custom info logging function. Passing in NULL will reset the logging
226 * callback to its default value.
228 * This function returns the value of the old callback.
230 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
232 llapi_log_callback_t old = llapi_info_callback;
235 llapi_info_callback = cb;
237 llapi_info_callback = info_callback_default;
243 * size_units is to be initialized (or zeroed) by caller.
245 int llapi_parse_size(const char *optarg, unsigned long long *size,
246 unsigned long long *size_units, int bytes_spec)
249 char *argbuf = (char *)optarg;
250 unsigned long long frac = 0, frac_d = 1;
252 if (strncmp(optarg, "-", 1) == 0)
255 if (*size_units == 0)
258 *size = strtoull(argbuf, &end, 0);
259 if (end != NULL && *end == '.') {
263 frac = strtoull(argbuf, &end, 10);
264 /* count decimal places */
265 for (i = 0; i < (end - argbuf); i++)
270 if ((*end == 'b') && *(end + 1) == '\0' &&
271 (*size & (~0ULL << (64 - 9))) == 0 &&
273 *size_units = 1 << 9;
274 } else if ((*end == 'b') &&
275 *(end + 1) == '\0' &&
278 } else if ((*end == 'k' || *end == 'K') &&
279 *(end + 1) == '\0' &&
280 (*size & (~0ULL << (64 - 10))) == 0) {
281 *size_units = 1 << 10;
282 } else if ((*end == 'm' || *end == 'M') &&
283 *(end + 1) == '\0' &&
284 (*size & (~0ULL << (64 - 20))) == 0) {
285 *size_units = 1 << 20;
286 } else if ((*end == 'g' || *end == 'G') &&
287 *(end + 1) == '\0' &&
288 (*size & (~0ULL << (64 - 30))) == 0) {
289 *size_units = 1 << 30;
290 } else if ((*end == 't' || *end == 'T') &&
291 *(end + 1) == '\0' &&
292 (*size & (~0ULL << (64 - 40))) == 0) {
293 *size_units = 1ULL << 40;
294 } else if ((*end == 'p' || *end == 'P') &&
295 *(end + 1) == '\0' &&
296 (*size & (~0ULL << (64 - 50))) == 0) {
297 *size_units = 1ULL << 50;
298 } else if ((*end == 'e' || *end == 'E') &&
299 *(end + 1) == '\0' &&
300 (*size & (~0ULL << (64 - 60))) == 0) {
301 *size_units = 1ULL << 60;
306 *size = *size * *size_units + frac * *size_units / frac_d;
311 int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
313 struct obd_ioctl_data *overlay;
316 data->ioc_len = obd_ioctl_packlen(data);
317 data->ioc_version = OBD_IOCTL_VERSION;
319 if (*pbuf != NULL && data->ioc_len > max_len) {
320 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
321 "pbuf = %p, ioc_len = %u, max_len = %d",
322 *pbuf, data->ioc_len, max_len);
327 *pbuf = malloc(data->ioc_len);
332 overlay = (struct obd_ioctl_data *)*pbuf;
333 memcpy(*pbuf, data, sizeof(*data));
335 ptr = overlay->ioc_bulk;
336 if (data->ioc_inlbuf1) {
337 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
338 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
341 if (data->ioc_inlbuf2) {
342 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
343 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
346 if (data->ioc_inlbuf3) {
347 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
348 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
351 if (data->ioc_inlbuf4) {
352 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
353 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
359 int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
361 struct obd_ioctl_data *overlay;
367 overlay = (struct obd_ioctl_data *)pbuf;
369 /* Preserve the caller's buffer pointers */
370 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
371 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
372 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
373 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
375 memcpy(data, pbuf, sizeof(*data));
377 ptr = overlay->ioc_bulk;
378 if (data->ioc_inlbuf1) {
379 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
380 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
383 if (data->ioc_inlbuf2) {
384 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
385 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
388 if (data->ioc_inlbuf3) {
389 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
390 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
393 if (data->ioc_inlbuf4) {
394 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
395 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
401 /* XXX: llapi_xxx() functions return negative values upon failure */
403 int llapi_layout_search_ost(__u32 ost, char *pname, char *fsname)
405 char ostname[MAX_OBD_NAME + 64];
406 char *pool_name = pname;
410 * The current policy is that the pool does not have to exist at the
411 * setstripe time, see sanity-pfl/-flr tests.
412 * If this logic will change, re-enable it.
414 * if (pname && strlen(pname) == 0)
418 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
420 rc = llapi_search_ost(fsname, pool_name, ostname);
425 llapi_error(LLAPI_MSG_ERROR, rc,
426 "%s: cannot find OST %s in %s", __func__, ostname,
427 pool_name != NULL ? "pool" : "system");
435 * Verify the setstripe parameters before using.
436 * This is a pair method for comp_args_to_layout()/llapi_layout_sanity_cb()
437 * when just 1 component or a non-PFL layout is given.
439 * \param[in] param stripe parameters
440 * \param[in] pool_name pool name
441 * \param[in] fsname lustre FS name
444 * < 0, error code on failre
446 static int llapi_stripe_param_verify(const struct llapi_stripe_param *param,
451 static int page_size;
454 if (page_size == 0) {
456 * 64 KB is the largest common page size (on ia64/PPC/ARM),
457 * but check the local page size just in case. The page_size
458 * will not change for the lifetime of this process at least.
460 page_size = LOV_MIN_STRIPE_SIZE;
461 if (getpagesize() > page_size) {
462 page_size = getpagesize();
463 llapi_err_noerrno(LLAPI_MSG_WARN,
464 "warning: page size (%u) larger than expected (%u)",
465 page_size, LOV_MIN_STRIPE_SIZE);
468 if (!llapi_stripe_size_is_aligned(param->lsp_stripe_size)) {
470 llapi_error(LLAPI_MSG_ERROR, rc,
471 "error: bad stripe_size %llu, must be an even multiple of %d bytes",
472 param->lsp_stripe_size, page_size);
475 if (!llapi_stripe_index_is_valid(param->lsp_stripe_offset)) {
477 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
478 param->lsp_stripe_offset);
481 if (llapi_stripe_size_is_too_big(param->lsp_stripe_size)) {
483 llapi_error(LLAPI_MSG_ERROR, rc,
484 "error: stripe size '%llu' over 4GB limit",
485 param->lsp_stripe_size);
489 count = param->lsp_stripe_count;
490 if (param->lsp_stripe_pattern == LOV_PATTERN_MDT) {
492 llapi_error(LLAPI_MSG_ERROR, rc,
493 "Invalid pattern: %d, must be specified with -E\n",
494 param->lsp_stripe_pattern);
497 if (!llapi_stripe_count_is_valid(count)) {
499 llapi_error(LLAPI_MSG_ERROR, rc,
500 "Invalid stripe count %d\n", count);
505 /* Make sure we have a good pool */
506 if (*pool_name != NULL) {
507 if (!llapi_pool_name_is_valid(pool_name, fsname)) {
509 llapi_error(LLAPI_MSG_ERROR, rc,
510 "Pool '%s' is not on filesystem '%s'",
515 /* Make sure the pool exists and is non-empty */
516 rc = llapi_search_ost(fsname, *pool_name, NULL);
518 char *err = rc == 0 ? "has no OSTs" : "does not exist";
521 llapi_error(LLAPI_MSG_ERROR, rc, "pool '%s.%s' %s",
522 fsname, *pool_name, err);
528 /* sanity check of target list */
529 if (param->lsp_is_specific) {
533 for (i = 0; i < count; i++) {
534 rc = llapi_layout_search_ost(param->lsp_osts[i],
539 /* Make sure stripe offset is in OST list. */
540 if (param->lsp_osts[i] == param->lsp_stripe_offset)
545 llapi_error(LLAPI_MSG_ERROR, rc,
546 "%s: stripe offset '%d' is not in the target list",
547 __func__, param->lsp_stripe_offset);
550 } else if (param->lsp_stripe_offset != -1) {
551 rc = llapi_layout_search_ost(param->lsp_stripe_offset,
561 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
566 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
568 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
572 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
574 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
579 if (!llapi_dir_hash_type_is_valid(hash_type)) {
581 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
589 * Trim a trailing newline from a string, if it exists.
591 int llapi_chomp_string(char *buf)
607 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
609 static int get_param_lov(const char *path, const char *param,
610 char *buf, size_t buf_size)
612 struct obd_uuid uuid;
615 rc = llapi_file_get_lov_uuid(path, &uuid);
619 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
624 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
626 static int get_param_lmv(const char *path, const char *param,
627 char *buf, size_t buf_size)
629 struct obd_uuid uuid;
632 rc = llapi_file_get_lmv_uuid(path, &uuid);
636 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
640 static int get_mds_md_size(const char *path)
642 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
645 * Rather than open the file and do the ioctl to get the
646 * instance name and close the file and search for the param
647 * file and open the param file and read the param file and
648 * parse the value and close the param file, let's just return
649 * a large enough value. It's 2020, RAM is cheap and this is
653 if (md_size < XATTR_SIZE_MAX)
654 md_size = XATTR_SIZE_MAX;
659 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
661 return get_param_lmv(path, "uuid", buf, bufsize);
665 * Open a Lustre file.
667 * \param name the name of the file to be opened
668 * \param flags access mode, see flags in open(2)
669 * \param mode permission of the file if it is created, see mode in open(2)
670 * \param param stripe pattern of the newly created file
672 * \retval file descriptor of opened file
673 * \retval negative errno on failure
675 int llapi_file_open_param(const char *name, int flags, mode_t mode,
676 const struct llapi_stripe_param *param)
678 char fsname[MAX_OBD_NAME + 1] = { 0 };
679 struct lov_user_md *lum = NULL;
680 char *pool_name = param->lsp_pool;
684 /* Make sure we are on a Lustre file system */
685 rc = llapi_search_fsname(name, fsname);
687 llapi_error(LLAPI_MSG_ERROR, rc,
688 "'%s' is not on a Lustre filesystem",
693 /* Check if the stripe pattern is sane. */
694 rc = llapi_stripe_param_verify(param, &pool_name, fsname);
698 if (param->lsp_is_specific)
699 lum_size = lov_user_md_size(param->lsp_stripe_count,
700 LOV_USER_MAGIC_SPECIFIC);
702 lum_size = sizeof(struct lov_user_md_v3);
704 lum_size = sizeof(*lum);
706 lum = calloc(1, lum_size);
711 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
713 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
714 flags = O_DIRECTORY | O_RDONLY;
721 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
726 /* Initialize IOCTL striping pattern structure */
727 lum->lmm_magic = LOV_USER_MAGIC_V1;
728 lum->lmm_pattern = param->lsp_stripe_pattern;
729 lum->lmm_stripe_size = param->lsp_stripe_size;
730 lum->lmm_stripe_count = param->lsp_stripe_count;
731 lum->lmm_stripe_offset = param->lsp_stripe_offset;
732 if (pool_name != NULL) {
733 struct lov_user_md_v3 *lumv3 = (void *)lum;
735 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
736 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
738 if (param->lsp_is_specific) {
739 struct lov_user_md_v3 *lumv3 = (void *)lum;
742 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
743 if (pool_name == NULL) {
745 * LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
746 * OST list, therefore if pool is not specified we have
747 * to pack a null pool name for placeholder.
749 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
752 for (i = 0; i < param->lsp_stripe_count; i++)
753 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
756 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
757 char errmsg[512] = "stripe already set";
760 if (errno != EEXIST && errno != EALREADY)
761 strncpy(errmsg, strerror(errno), sizeof(errmsg) - 1);
762 if (rc == -EREMOTEIO)
763 snprintf(errmsg, sizeof(errmsg),
764 "inactive OST among your specified %d OST(s)",
765 param->lsp_stripe_count);
767 llapi_err_noerrno(LLAPI_MSG_ERROR,
768 "setstripe error for '%s': %s", name, errmsg);
779 int llapi_file_open_pool(const char *name, int flags, int mode,
780 unsigned long long stripe_size, int stripe_offset,
781 int stripe_count, int stripe_pattern, char *pool_name)
783 const struct llapi_stripe_param param = {
784 .lsp_stripe_size = stripe_size,
785 .lsp_stripe_count = stripe_count,
786 .lsp_stripe_pattern = stripe_pattern,
787 .lsp_stripe_offset = stripe_offset,
788 .lsp_pool = pool_name
790 return llapi_file_open_param(name, flags, mode, ¶m);
793 int llapi_file_open(const char *name, int flags, int mode,
794 unsigned long long stripe_size, int stripe_offset,
795 int stripe_count, int stripe_pattern)
797 return llapi_file_open_pool(name, flags, mode, stripe_size,
798 stripe_offset, stripe_count,
799 stripe_pattern, NULL);
802 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
803 __u32 flags, char *foreign_lov)
806 struct lov_foreign_md *lfm;
809 if (foreign_lov == NULL) {
811 llapi_error(LLAPI_MSG_ERROR, rc,
812 "foreign LOV EA content must be provided");
816 len = strlen(foreign_lov);
817 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
820 llapi_error(LLAPI_MSG_ERROR, rc,
821 "foreign LOV EA size %zu (must be 0 < len < %zu)",
822 len, XATTR_SIZE_MAX -
823 offsetof(struct lov_foreign_md, lfm_value));
827 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
830 llapi_error(LLAPI_MSG_ERROR, rc,
831 "failed to allocate lov_foreign_md");
835 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
838 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
842 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
843 lfm->lfm_length = len;
844 lfm->lfm_type = type;
845 lfm->lfm_flags = flags;
846 memcpy(lfm->lfm_value, foreign_lov, len);
848 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
849 char *errmsg = "stripe already set";
853 errmsg = "not on a Lustre filesystem";
854 else if (errno == EEXIST || errno == EALREADY)
855 errmsg = "stripe already set";
857 errmsg = strerror(errno);
859 llapi_err_noerrno(LLAPI_MSG_ERROR,
860 "setstripe error for '%s': %s", name, errmsg);
876 int llapi_file_create(const char *name, unsigned long long stripe_size,
877 int stripe_offset, int stripe_count, int stripe_pattern)
881 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
882 stripe_offset, stripe_count, stripe_pattern,
891 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
892 int stripe_offset, int stripe_count,
893 int stripe_pattern, char *pool_name)
897 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
898 stripe_offset, stripe_count, stripe_pattern,
907 static int verify_dir_param(const char *name,
908 const struct llapi_stripe_param *param)
910 char fsname[MAX_OBD_NAME + 1] = { 0 };
911 char *pool_name = param->lsp_pool;
914 /* Make sure we are on a Lustre file system */
915 rc = llapi_search_fsname(name, fsname);
917 llapi_error(LLAPI_MSG_ERROR, rc,
918 "'%s' is not on a Lustre filesystem",
923 /* Check if the stripe pattern is sane. */
924 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
925 param->lsp_stripe_count,
926 param->lsp_stripe_pattern);
930 /* Make sure we have a good pool */
931 if (pool_name != NULL) {
933 * in case user gives the full pool name <fsname>.<poolname>,
936 char *ptr = strchr(pool_name, '.');
940 if (strcmp(pool_name, fsname) != 0) {
942 llapi_err_noerrno(LLAPI_MSG_ERROR,
943 "Pool '%s' is not on filesystem '%s'",
950 /* Make sure the pool exists and is non-empty */
951 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
953 char *err = rc == 0 ? "has no OSTs" : "does not exist";
955 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
956 fsname, pool_name, err);
961 /* sanity check of target list */
962 if (param->lsp_is_specific) {
963 char mdtname[MAX_OBD_NAME + 64];
967 for (i = 0; i < param->lsp_stripe_count; i++) {
968 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
969 fsname, param->lsp_tgts[i]);
970 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
975 llapi_error(LLAPI_MSG_ERROR, rc,
976 "%s: cannot find MDT %s in %s",
983 /* Make sure stripe offset is in MDT list. */
984 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
988 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
989 "%s: stripe offset '%d' is not in the target list",
990 __func__, param->lsp_stripe_offset);
998 static inline void param2lmu(struct lmv_user_md *lmu,
999 const struct llapi_stripe_param *param)
1001 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
1003 lmu->lum_stripe_count = param->lsp_stripe_count;
1004 lmu->lum_stripe_offset = param->lsp_stripe_offset;
1005 lmu->lum_hash_type = param->lsp_stripe_pattern;
1006 lmu->lum_max_inherit = param->lsp_max_inherit;
1007 lmu->lum_max_inherit_rr = param->lsp_max_inherit_rr;
1008 if (param->lsp_pool != NULL)
1009 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
1010 if (param->lsp_is_specific) {
1013 for (i = 0; i < param->lsp_stripe_count; i++)
1014 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
1018 int llapi_dir_set_default_lmv(const char *name,
1019 const struct llapi_stripe_param *param)
1021 struct lmv_user_md lmu = { 0 };
1025 rc = verify_dir_param(name, param);
1029 /* TODO: default lmv doesn't support specific targets yet */
1030 if (param->lsp_is_specific)
1033 param2lmu(&lmu, param);
1035 fd = open(name, O_DIRECTORY | O_RDONLY);
1038 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1042 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
1044 char *errmsg = "stripe already set";
1047 if (errno != EEXIST && errno != EALREADY)
1048 errmsg = strerror(errno);
1050 llapi_err_noerrno(LLAPI_MSG_ERROR,
1051 "default dirstripe error on '%s': %s",
1058 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
1059 int stripe_count, int stripe_pattern,
1060 const char *pool_name)
1062 const struct llapi_stripe_param param = {
1063 .lsp_stripe_count = stripe_count,
1064 .lsp_stripe_offset = stripe_offset,
1065 .lsp_stripe_pattern = stripe_pattern,
1066 .lsp_pool = (char *)pool_name
1069 return llapi_dir_set_default_lmv(name, ¶m);
1073 * Create a Lustre directory.
1075 * \param name the name of the directory to be created
1076 * \param mode permission of the file if it is created, see mode in open(2)
1077 * \param param stripe pattern of the newly created directory
1079 * \retval 0 on success
1080 * \retval negative errno on failure
1082 int llapi_dir_create(const char *name, mode_t mode,
1083 const struct llapi_stripe_param *param)
1085 struct lmv_user_md *lmu = NULL;
1087 struct obd_ioctl_data data = { 0 };
1090 char *dirpath = NULL;
1091 char *namepath = NULL;
1096 rc = verify_dir_param(name, param);
1100 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1101 param->lsp_is_specific ?
1102 LMV_USER_MAGIC_SPECIFIC :
1105 lmu = calloc(1, lmu_size);
1109 dirpath = strdup(name);
1115 namepath = strdup(name);
1122 param2lmu(lmu, param);
1124 filename = basename(namepath);
1125 dir = dirname(dirpath);
1127 data.ioc_inlbuf1 = (char *)filename;
1128 data.ioc_inllen1 = strlen(filename) + 1;
1129 data.ioc_inlbuf2 = (char *)lmu;
1130 data.ioc_inllen2 = lmu_size;
1131 data.ioc_type = mode;
1132 if (param->lsp_is_create)
1133 /* borrow obdo1.o_flags to store this flag */
1134 data.ioc_obdo1.o_flags = OBD_FL_OBDMDEXISTS;
1135 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1137 llapi_error(LLAPI_MSG_ERROR, rc,
1138 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1143 fd = open(dir, O_DIRECTORY | O_RDONLY);
1146 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1150 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1151 char *errmsg = "stripe already set";
1154 if (errno != EEXIST && errno != EALREADY)
1155 errmsg = strerror(errno);
1157 llapi_err_noerrno(LLAPI_MSG_ERROR,
1158 "dirstripe error on '%s': %s", name, errmsg);
1169 * Create a foreign directory.
1171 * \param name the name of the directory to be created
1172 * \param mode permission of the file if it is created, see mode in open(2)
1173 * \param type foreign type to be set in LMV EA
1174 * \param flags foreign flags to be set in LMV EA
1175 * \param value foreign pattern to be set in LMV EA
1177 * \retval 0 on success
1178 * \retval negative errno on failure
1180 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1181 __u32 flags, const char *value)
1183 struct lmv_foreign_md *lfm = NULL;
1184 size_t lfm_size, len;
1185 struct obd_ioctl_data data = { 0 };
1188 char *dirpath = NULL;
1189 char *namepath = NULL;
1194 len = strlen(value);
1195 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1198 llapi_error(LLAPI_MSG_ERROR, rc,
1199 "invalid LOV EA length %zu (must be 0 < len < %zu)",
1200 len, XATTR_SIZE_MAX -
1201 offsetof(struct lmv_foreign_md, lfm_value));
1204 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1205 lfm = calloc(1, lfm_size);
1209 dirpath = strdup(name);
1215 namepath = strdup(name);
1222 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1223 lfm->lfm_length = len;
1224 lfm->lfm_type = type;
1225 lfm->lfm_flags = flags;
1226 memcpy(lfm->lfm_value, value, len);
1228 filename = basename(namepath);
1229 dir = dirname(dirpath);
1231 data.ioc_inlbuf1 = (char *)filename;
1232 data.ioc_inllen1 = strlen(filename) + 1;
1233 data.ioc_inlbuf2 = (char *)lfm;
1234 data.ioc_inllen2 = lfm_size;
1235 data.ioc_type = mode;
1236 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1238 llapi_error(LLAPI_MSG_ERROR, rc,
1239 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1244 fd = open(dir, O_DIRECTORY | O_RDONLY);
1247 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1251 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1252 char *errmsg = "stripe already set";
1255 if (errno != EEXIST && errno != EALREADY)
1256 errmsg = strerror(errno);
1258 llapi_err_noerrno(LLAPI_MSG_ERROR,
1259 "dirstripe error on '%s': %s", name, errmsg);
1269 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1270 int stripe_count, int stripe_pattern,
1271 const char *pool_name)
1273 const struct llapi_stripe_param param = {
1274 .lsp_stripe_count = stripe_count,
1275 .lsp_stripe_offset = stripe_offset,
1276 .lsp_stripe_pattern = stripe_pattern,
1277 .lsp_pool = (char *)pool_name
1280 return llapi_dir_create(name, mode, ¶m);
1284 * Find the fsname, the full path, and/or an open fd.
1285 * Either the fsname or path must not be NULL
1287 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1290 char buf[PATH_MAX], mntdir[PATH_MAX];
1291 char *ptr, *ptr_end;
1293 int idx = 0, mntlen = 0, fd;
1295 int fsnamelen, mountlen;
1297 /* get the mount point */
1298 fp = setmntent(PROC_MOUNTS, "r");
1301 llapi_error(LLAPI_MSG_ERROR, rc,
1302 "cannot retrieve filesystem mount point");
1306 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1309 if (!llapi_is_lustre_mnt(&mnt))
1312 if ((want & WANT_INDEX) && (idx++ != index))
1315 mntlen = strlen(mnt.mnt_dir);
1316 ptr = strchr(mnt.mnt_fsname, '/');
1317 while (ptr && *ptr == '/')
1320 * thanks to the call to llapi_is_lustre_mnt() above,
1321 * we are sure that mnt.mnt_fsname contains ":/",
1322 * so ptr should never be NULL
1327 while (*ptr_end != '/' && *ptr_end != '\0')
1330 /* Check the fsname for a match, if given */
1331 mountlen = ptr_end - ptr;
1332 if (!(want & WANT_FSNAME) && fsname != NULL &&
1333 (fsnamelen = strlen(fsname)) > 0 &&
1334 (fsnamelen != mountlen ||
1335 (strncmp(ptr, fsname, mountlen) != 0)))
1338 /* If the path isn't set return the first one we find */
1339 if (path == NULL || strlen(path) == 0) {
1340 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1341 mntdir[sizeof(mntdir) - 1] = '\0';
1342 if ((want & WANT_FSNAME) && fsname != NULL) {
1343 strncpy(fsname, ptr, mountlen);
1344 fsname[mountlen] = '\0';
1348 /* Otherwise find the longest matching path */
1349 } else if ((strlen(path) >= mntlen) &&
1350 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1351 /* check the path format */
1352 if (strlen(path) > mntlen && path[mntlen] != '/')
1354 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1355 mntdir[sizeof(mntdir) - 1] = '\0';
1356 if ((want & WANT_FSNAME) && fsname != NULL) {
1357 strncpy(fsname, ptr, mountlen);
1358 fsname[mountlen] = '\0';
1368 if ((want & WANT_PATH) && path != NULL) {
1369 strncpy(path, mntdir, mntlen);
1370 path[mntlen] = '\0';
1372 if (want & WANT_FD) {
1373 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1376 llapi_error(LLAPI_MSG_ERROR, rc,
1377 "cannot open '%s'", mntdir);
1383 } else if (want & WANT_ERROR)
1384 llapi_err_noerrno(LLAPI_MSG_ERROR,
1385 "'%s' not on a mounted Lustre filesystem",
1386 (want & WANT_PATH) ? fsname : path);
1391 * search lustre mounts
1393 * Calling this function will return to the user the mount point, mntdir, and
1394 * the file system name, fsname, if the user passed a buffer to this routine.
1396 * The user inputs are pathname and index. If the pathname is supplied then
1397 * the value of the index will be ignored. The pathname will return data if
1398 * the pathname is located on a lustre mount. Index is used to pick which
1399 * mount point you want in the case of multiple mounted lustre file systems.
1400 * See function lfs_osts in lfs.c for an example of the index use.
1402 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1405 int want = WANT_PATH, idx = -1;
1407 if (!pathname || pathname[0] == '\0') {
1411 strcpy(mntdir, pathname);
1415 want |= WANT_FSNAME;
1416 return get_root_path(want, fsname, NULL, mntdir, idx);
1419 /* Given a path, find the corresponding Lustre fsname */
1420 int llapi_search_fsname(const char *pathname, char *fsname)
1425 path = realpath(pathname, NULL);
1427 char tmp[PATH_MAX - 1];
1433 if (pathname[0] != '/') {
1435 * Need an absolute path, but realpath() only works for
1436 * pathnames that actually exist. We go through the
1437 * extra hurdle of dirname(getcwd() + pathname) in
1438 * case the relative pathname contains ".." in it.
1440 char realpath[PATH_MAX - 1];
1442 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1444 llapi_error(LLAPI_MSG_ERROR, rc,
1445 "cannot get current working directory");
1449 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1450 if (rc >= sizeof(tmp)) {
1452 llapi_error(LLAPI_MSG_ERROR, rc,
1453 "invalid parent path '%s'",
1459 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1460 if (rc >= sizeof(buf)) {
1462 llapi_error(LLAPI_MSG_ERROR, rc,
1463 "invalid path '%s'", pathname);
1466 path = realpath(buf, NULL);
1468 ptr = strrchr(buf, '/');
1470 llapi_error(LLAPI_MSG_ERROR |
1471 LLAPI_MSG_NO_ERRNO, 0,
1472 "cannot resolve path '%s'",
1477 path = realpath(buf, NULL);
1480 llapi_error(LLAPI_MSG_ERROR, rc,
1481 "cannot resolve path '%s'",
1487 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1492 int llapi_search_rootpath(char *pathname, const char *fsname)
1495 * pathname can be used as an argument by get_root_path(),
1496 * clear it for safety
1499 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1503 * Get the list of pool members.
1504 * \param poolname string of format \<fsname\>.\<poolname\>
1505 * \param members caller-allocated array of char*
1506 * \param list_size size of the members array
1507 * \param buffer caller-allocated buffer for storing OST names
1508 * \param buffer_size size of the buffer
1510 * \return number of members retrieved for this pool
1511 * \retval -error failure
1513 int llapi_get_poolmembers(const char *poolname, char **members,
1514 int list_size, char *buffer, int buffer_size)
1516 char fsname[PATH_MAX];
1525 /* name is FSNAME.POOLNAME */
1526 if (strlen(poolname) >= sizeof(fsname))
1529 snprintf(fsname, sizeof(fsname), "%s", poolname);
1530 pool = strchr(fsname, '.');
1537 rc = poolpath(&pathname, fsname, NULL);
1539 llapi_error(LLAPI_MSG_ERROR, rc,
1540 "Lustre filesystem '%s' not found",
1545 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1546 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1547 cfs_free_param_data(&pathname);
1548 if (rc >= sizeof(buf))
1550 fd = fopen(buf, "r");
1553 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1558 while (fgets(buf, sizeof(buf), fd) != NULL) {
1559 if (nb_entries >= list_size) {
1563 buf[sizeof(buf) - 1] = '\0';
1565 tmp = strchr(buf, '\n');
1568 if (used + strlen(buf) + 1 > buffer_size) {
1573 strcpy(buffer + used, buf);
1574 members[nb_entries] = buffer + used;
1575 used += strlen(buf) + 1;
1585 * Get the list of pools in a filesystem.
1586 * \param name filesystem name or path
1587 * \param poollist caller-allocated array of char*
1588 * \param list_size size of the poollist array
1589 * \param buffer caller-allocated buffer for storing pool names
1590 * \param buffer_size size of the buffer
1592 * \return number of pools retrieved for this filesystem
1593 * \retval -error failure
1595 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1596 char *buffer, int buffer_size)
1602 struct dirent *pool;
1604 unsigned int nb_entries = 0;
1605 unsigned int used = 0;
1608 /* initialize output array */
1609 for (i = 0; i < list_size; i++)
1612 /* is name a pathname ? */
1613 ptr = strchr(name, '/');
1615 char fsname_buf[MAXNAMLEN];
1617 /* We will need fsname for printing later */
1618 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1622 ptr = strrchr(fsname_buf, '-');
1626 fsname = strdup(fsname_buf);
1630 /* name is FSNAME */
1631 fsname = strdup(name);
1636 rc = poolpath(&pathname, fsname, NULL);
1638 llapi_error(LLAPI_MSG_ERROR, rc,
1639 "Lustre filesystem '%s' not found", name);
1643 dir = opendir(pathname.gl_pathv[0]);
1646 llapi_error(LLAPI_MSG_ERROR, rc,
1647 "Could not open pool list for '%s'",
1654 pool = readdir(dir);
1660 /* ignore . and .. */
1661 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1664 /* check output bounds */
1665 if (nb_entries >= list_size) {
1667 goto free_dir_no_msg;
1670 /* +2 for '.' and final '\0' */
1671 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1674 goto free_dir_no_msg;
1677 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1678 poollist[nb_entries] = buffer + used;
1679 used += strlen(pool->d_name) + strlen(fsname) + 2;
1685 llapi_error(LLAPI_MSG_ERROR, rc,
1686 "Error reading pool list for '%s'", name);
1688 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1693 cfs_free_param_data(&pathname);
1696 return rc != 0 ? rc : nb_entries;
1699 /* wrapper for lfs.c and obd.c */
1700 int llapi_poollist(const char *name)
1702 int poolcount, rc, i;
1705 rc = llapi_get_poolbuf(name, &buf, &pools, &poolcount);
1709 for (i = 0; i < poolcount; i++)
1710 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", pools[i]);
1717 * Get buffer that holds uuids and the list of pools in a filesystem.
1719 * \param name filesystem name or path
1720 * \param buf bufffer that has to be freed if function returns 0
1721 * \param pools pointer to the list of pools in buffer
1722 * \param poolcount number of pools
1724 * \return 0 when found at least 1 pool, i.e. poolcount > 0
1725 * \retval -error failure
1727 int llapi_get_poolbuf(const char *name, char **buf,
1728 char ***pools, int *poolcount)
1731 * list of pool names (assume that pool count is smaller
1734 char **list, *buffer = NULL, *fsname = (char *)name;
1735 char *poolname = NULL, *tmp = NULL, data[16];
1736 enum param_filter type = FILTER_BY_PATH;
1737 int obdcount, bufsize, rc, nb;
1742 if (name[0] != '/') {
1743 fsname = strdup(name);
1747 poolname = strchr(fsname, '.');
1750 type = FILTER_BY_FS_NAME;
1753 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1754 data, sizeof(data));
1757 obdcount = atoi(data);
1760 * Allocate space for each fsname-OST0000_UUID, 1 per OST,
1761 * and also an array to store the pointers for all that
1765 bufsize = sizeof(struct obd_uuid) * obdcount;
1766 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1767 if (buffer == NULL) {
1771 list = (char **) (buffer + bufsize);
1774 /* name is a path or fsname */
1775 nb = llapi_get_poollist(name, list, obdcount,
1778 /* name is a pool name (<fsname>.<poolname>) */
1779 nb = llapi_get_poolmembers(name, list, obdcount,
1783 if (nb == -EOVERFLOW) {
1786 goto retry_get_pools;
1789 rc = (nb < 0 ? nb : 0);
1796 /* Don't free buffer, it will be used later */
1799 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1804 typedef int (semantic_func_t)(char *path, int p, int *d,
1805 void *data, struct dirent64 *de);
1807 #define OBD_NOT_FOUND (-1)
1809 static bool lmv_is_foreign(__u32 magic)
1811 return magic == LMV_MAGIC_FOREIGN;
1814 static void find_param_fini(struct find_param *param)
1816 if (param->fp_migrate)
1819 if (param->fp_obd_indexes) {
1820 free(param->fp_obd_indexes);
1821 param->fp_obd_indexes = NULL;
1824 if (param->fp_lmd) {
1825 free(param->fp_lmd);
1826 param->fp_lmd = NULL;
1829 if (param->fp_lmv_md) {
1830 free(param->fp_lmv_md);
1831 param->fp_lmv_md = NULL;
1835 static int common_param_init(struct find_param *param, char *path)
1837 int lum_size = get_mds_md_size(path);
1842 /* migrate has fp_lmv_md initialized outside */
1843 if (param->fp_migrate)
1846 if (lum_size < PATH_MAX + 1)
1847 lum_size = PATH_MAX + 1;
1849 param->fp_lum_size = lum_size;
1850 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1852 if (param->fp_lmd == NULL) {
1853 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1854 "error: allocate %zu bytes for layout failed",
1855 sizeof(lstat_t) + param->fp_lum_size);
1859 param->fp_lmv_stripe_count = 256;
1860 param->fp_lmv_md = calloc(1,
1861 lmv_user_md_size(param->fp_lmv_stripe_count,
1862 LMV_USER_MAGIC_SPECIFIC));
1863 if (param->fp_lmv_md == NULL) {
1864 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1865 "error: allocation of %d bytes for ioctl",
1866 lmv_user_md_size(param->fp_lmv_stripe_count,
1867 LMV_USER_MAGIC_SPECIFIC));
1868 find_param_fini(param);
1872 param->fp_got_uuids = 0;
1873 param->fp_obd_indexes = NULL;
1874 param->fp_obd_index = OBD_NOT_FOUND;
1875 param->fp_mdt_index = OBD_NOT_FOUND;
1879 static int cb_common_fini(char *path, int p, int *dp, void *data,
1880 struct dirent64 *de)
1882 struct find_param *param = data;
1888 /* set errno upon failure */
1889 static int open_parent(const char *path)
1895 path_copy = strdup(path);
1896 if (path_copy == NULL)
1899 parent_path = dirname(path_copy);
1900 parent = open(parent_path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1906 static int cb_get_dirstripe(char *path, int *d, struct find_param *param)
1909 bool did_nofollow = false;
1914 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1915 if (param->fp_get_default_lmv)
1916 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1918 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1920 ret = ioctl(*d, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1922 /* if ENOTTY likely to be a fake symlink, so try again after
1923 * new open() with O_NOFOLLOW, but only once to prevent any
1924 * loop like for the path of a file/dir not on Lustre !!
1926 if (ret < 0 && errno == ENOTTY && !did_nofollow) {
1929 did_nofollow = true;
1930 fd = open(path, O_RDONLY | O_NOFOLLOW);
1932 /* restore original errno */
1937 /* close original fd and set new */
1940 ret2 = ioctl(fd, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1941 if (ret2 < 0 && errno != E2BIG) {
1942 /* restore original errno */
1946 /* LMV is ok or need to handle E2BIG case now */
1950 if (errno == E2BIG && ret != 0) {
1954 /* if foreign LMV case, fake stripes number */
1955 if (lmv_is_foreign(param->fp_lmv_md->lum_magic)) {
1956 struct lmv_foreign_md *lfm;
1958 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1959 if (lfm->lfm_length < XATTR_SIZE_MAX -
1960 offsetof(typeof(*lfm), lfm_value)) {
1961 uint32_t size = lfm->lfm_length +
1962 offsetof(typeof(*lfm), lfm_value);
1964 stripe_count = lmv_foreign_to_md_stripes(size);
1966 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1967 "error: invalid %d foreign size returned from ioctl",
1972 stripe_count = param->fp_lmv_md->lum_stripe_count;
1974 if (stripe_count <= param->fp_lmv_stripe_count)
1977 free(param->fp_lmv_md);
1978 param->fp_lmv_stripe_count = stripe_count;
1979 lmv_size = lmv_user_md_size(stripe_count,
1980 LMV_USER_MAGIC_SPECIFIC);
1981 param->fp_lmv_md = malloc(lmv_size);
1982 if (param->fp_lmv_md == NULL) {
1983 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1984 "error: allocation of %d bytes for ioctl",
1985 lmv_user_md_size(param->fp_lmv_stripe_count,
1986 LMV_USER_MAGIC_SPECIFIC));
1995 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1998 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
1999 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
2000 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
2001 lmd_v2->lmd_stx.stx_uid = st->st_uid;
2002 lmd_v2->lmd_stx.stx_gid = st->st_gid;
2003 lmd_v2->lmd_stx.stx_mode = st->st_mode;
2004 lmd_v2->lmd_stx.stx_ino = st->st_ino;
2005 lmd_v2->lmd_stx.stx_size = st->st_size;
2006 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
2007 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
2008 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
2009 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
2010 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
2011 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
2012 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
2013 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
2014 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
2016 lmd_v2->lmd_flags = 0;
2018 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
2020 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
2021 if (lmd_v2->lmd_stx.stx_size)
2022 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
2023 if (lmd_v2->lmd_stx.stx_blocks)
2024 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
2026 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
2027 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
2028 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
2033 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
2035 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
2036 struct lov_user_mds_data *lmd_v2 = lmdbuf;
2040 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
2044 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
2047 st = lmd_v1->lmd_st;
2048 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
2049 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
2050 convert_lmd_statx(lmd_v2, &st, false);
2051 lmd_v2->lmd_lmmsize = 0;
2052 lmd_v2->lmd_padding = 0;
2057 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
2058 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
2060 struct lov_user_mds_data *lmd = lmdbuf;
2061 static bool use_old_ioctl;
2065 if (parent_fd < 0 && dir_fd < 0)
2067 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
2072 * LL_IOC_MDC_GETINFO operates on the current directory inode
2073 * and returns struct lov_user_mds_data, while
2074 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
2076 if (type == GET_LMD_INFO)
2077 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_V1 :
2078 LL_IOC_MDC_GETINFO_V2;
2080 cmd = LL_IOC_LOV_GETSTRIPE;
2083 ret = ioctl(dir_fd, cmd, lmdbuf);
2084 if (ret < 0 && errno == ENOTTY &&
2085 cmd == LL_IOC_MDC_GETINFO_V2) {
2086 cmd = LL_IOC_MDC_GETINFO_V1;
2087 use_old_ioctl = true;
2091 if (cmd == LL_IOC_MDC_GETINFO_V1 && !ret)
2092 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2094 if (ret < 0 && errno == ENOTTY && type == GET_LMD_STRIPE) {
2097 /* retry ioctl() after new open() with O_NOFOLLOW
2098 * just in case it could be a fake symlink
2099 * need using a new open() as dir_fd is being closed
2103 dir_fd2 = open(path, O_RDONLY | O_NDELAY | O_NOFOLLOW);
2105 /* return original error */
2108 ret = ioctl(dir_fd2, cmd, lmdbuf);
2109 /* pass new errno or success back to caller */
2115 } else if (parent_fd >= 0) {
2116 const char *fname = strrchr(path, '/');
2119 * IOC_MDC_GETFILEINFO takes as input the filename (relative to
2120 * the parent directory) and returns struct lov_user_mds_data,
2121 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2123 * This avoids opening, locking, and closing each file on the
2124 * client if that is not needed. Multiple of these ioctl() can
2125 * be done on the parent dir with a single open for all
2126 * files in that directory, and it also doesn't pollute the
2127 * client dcache with millions of dentries when traversing
2128 * a large filesystem.
2130 fname = (fname == NULL ? path : fname + 1);
2132 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2135 else if (ret >= lmdlen || ret++ == 0)
2138 if (type == GET_LMD_INFO)
2139 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_V1 :
2140 IOC_MDC_GETFILEINFO_V2;
2142 cmd = IOC_MDC_GETFILESTRIPE;
2145 ret = ioctl(parent_fd, cmd, lmdbuf);
2146 if (ret < 0 && errno == ENOTTY &&
2147 cmd == IOC_MDC_GETFILEINFO_V2) {
2148 cmd = IOC_MDC_GETFILEINFO_V1;
2149 use_old_ioctl = true;
2150 goto retry_getfileinfo;
2153 if (cmd == IOC_MDC_GETFILEINFO_V1 && !ret)
2154 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2158 if (ret && type == GET_LMD_INFO) {
2159 if (errno == ENOTTY) {
2163 * ioctl is not supported, it is not a lustre fs.
2164 * Do the regular lstat(2) instead.
2166 ret = lstat_f(path, &st);
2169 llapi_error(LLAPI_MSG_ERROR, ret,
2170 "error: %s: lstat failed for %s",
2174 convert_lmd_statx(lmd, &st, true);
2176 * It may be wrong to set use_old_ioctl with true as
2177 * the file is not a lustre fs. So reset it with false
2180 use_old_ioctl = false;
2181 } else if (errno == ENOENT) {
2183 llapi_error(LLAPI_MSG_WARN, ret,
2184 "warning: %s does not exist", path);
2185 } else if (errno != EISDIR && errno != ENODATA) {
2187 llapi_error(LLAPI_MSG_ERROR, ret,
2188 "%s ioctl failed for %s.",
2189 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2190 "IOC_MDC_GETFILEINFO", path);
2197 static int llapi_semantic_traverse(char *path, int size, int parent,
2198 semantic_func_t sem_init,
2199 semantic_func_t sem_fini, void *data,
2200 struct dirent64 *de)
2202 struct find_param *param = (struct find_param *)data;
2203 struct dirent64 *dent;
2204 int len, ret, d, p = -1;
2210 d = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
2211 /* if an invalid fake dir symlink, opendir() will return EINVAL
2212 * instead of ENOTDIR. If a valid but dangling faked or real file/dir
2213 * symlink ENOENT will be returned. For a valid/resolved fake or real
2214 * file symlink ENOTDIR will be returned as for a regular file.
2215 * opendir() will be successful for a valid and resolved fake or real
2216 * dir simlink or a regular dir.
2218 if (d == -1 && errno != ENOTDIR && errno != EINVAL && errno != ENOENT) {
2220 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2223 } else if (d == -1) {
2224 if (errno == ENOENT || errno == EINVAL) {
2225 int old_errno = errno;
2227 /* try to open with O_NOFOLLOW this will help
2228 * differentiate fake vs real symlinks
2229 * it is ok to not use O_DIRECTORY with O_RDONLY
2230 * and it will prevent the need to deal with ENOTDIR
2231 * error, instead of ELOOP, being returned by recent
2232 * kernels for real symlinks
2234 d = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2235 /* if a dangling real symlink should return ELOOP, or
2236 * again ENOENT if really non-existing path, or E...??
2237 * So return original error. If success or ENOTDIR, path
2238 * is likely to be a fake dir/file symlink, so continue
2248 if (parent == -1 && d == -1) {
2249 /* Open the parent dir. */
2250 p = open_parent(path);
2256 } else { /* d != -1 */
2259 /* try to reopen dir with O_NOFOLLOW just in case of a foreign
2262 d2 = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2267 /* continue with d */
2273 ret = sem_init(path, (parent != -1) ? parent : p, &d, data, de);
2283 /* ENOTDIR if fake symlink, do not consider it as an error */
2284 if (errno != ENOTDIR)
2285 llapi_error(LLAPI_MSG_ERROR, errno,
2286 "fdopendir() failed");
2293 while ((dent = readdir64(dir)) != NULL) {
2296 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2300 if ((len + dent->d_reclen + 2) > size) {
2301 llapi_err_noerrno(LLAPI_MSG_ERROR,
2302 "error: %s: string buffer too small for %s",
2307 strcat(path, dent->d_name);
2309 if (dent->d_type == DT_UNKNOWN) {
2310 struct lov_user_mds_data *lmd = param->fp_lmd;
2312 rc = get_lmd_info_fd(path, d, -1, param->fp_lmd,
2313 param->fp_lum_size, GET_LMD_INFO);
2315 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2322 switch (dent->d_type) {
2324 llapi_err_noerrno(LLAPI_MSG_ERROR,
2325 "error: %s: '%s' is UNKNOWN type %d",
2326 __func__, dent->d_name, dent->d_type);
2329 rc = llapi_semantic_traverse(path, size, d, sem_init,
2330 sem_fini, data, dent);
2331 if (rc != 0 && ret == 0)
2337 rc = sem_init(path, d, NULL, data, dent);
2338 if (rc < 0 && ret == 0) {
2343 if (sem_fini && rc == 0)
2344 sem_fini(path, d, NULL, data, dent);
2352 sem_fini(path, parent, &d, data, de);
2365 static int param_callback(char *path, semantic_func_t sem_init,
2366 semantic_func_t sem_fini, struct find_param *param)
2368 int ret, len = strlen(path);
2371 if (len > PATH_MAX) {
2373 llapi_error(LLAPI_MSG_ERROR, ret,
2374 "Path name '%s' is too long", path);
2378 buf = (char *)malloc(2 * PATH_MAX);
2382 snprintf(buf, PATH_MAX + 1, "%s", path);
2383 ret = common_param_init(param, buf);
2387 param->fp_depth = 0;
2389 ret = llapi_semantic_traverse(buf, 2 * PATH_MAX, -1, sem_init,
2390 sem_fini, param, NULL);
2392 find_param_fini(param);
2394 return ret < 0 ? ret : 0;
2397 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2401 rc = ioctl(fd, OBD_IOC_GETDTNAME, lov_name);
2402 if (rc && errno == ENOTTY)
2403 rc = ioctl(fd, OBD_IOC_GETNAME_OLD, lov_name);
2406 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2412 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2416 rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2419 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2425 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2429 /* do not follow faked symlinks */
2430 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
2432 /* real symlink should have failed with ELOOP so retry without
2433 * O_NOFOLLOW just in case
2435 fd = open(path, O_RDONLY | O_NONBLOCK);
2438 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'",
2444 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2450 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2454 fd = open(path, O_RDONLY | O_NONBLOCK);
2457 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2461 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2473 * If uuidp is NULL, return the number of available obd uuids.
2474 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2475 * there are more OSTs than allocated to uuidp, then an error is returned with
2476 * the ost_count set to number of available obd uuids.
2478 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2479 int *ost_count, enum tgt_type type)
2481 char buf[PATH_MAX], format[32];
2482 int rc = 0, index = 0;
2483 struct obd_uuid name;
2487 /* Get the lov name */
2488 if (type == LOV_TYPE)
2489 rc = llapi_file_fget_lov_uuid(fd, &name);
2491 rc = llapi_file_fget_lmv_uuid(fd, &name);
2495 /* Now get the ost uuids */
2496 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2497 FILTER_BY_EXACT, "target_obd", ¶m);
2501 fp = fopen(param.gl_pathv[0], "r");
2504 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2509 snprintf(format, sizeof(format),
2510 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2511 while (fgets(buf, sizeof(buf), fp) != NULL) {
2512 if (uuidp && (index < *ost_count)) {
2513 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2521 if (uuidp && (index > *ost_count))
2526 cfs_free_param_data(¶m);
2530 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2532 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2535 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2540 root = open(mnt, O_RDONLY | O_DIRECTORY);
2543 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2548 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2557 * Check if user specified value matches a real uuid. Ignore _UUID,
2558 * -osc-4ba41334, other trailing gunk in comparison.
2559 * @param real_uuid ends in "_UUID"
2560 * @param search_uuid may or may not end in "_UUID"
2562 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2564 int cmplen = strlen(real_uuid);
2565 int searchlen = strlen(search_uuid);
2567 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2569 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2573 * The UUIDs may legitimately be different lengths, if
2574 * the system was upgraded from an older version.
2576 if (cmplen != searchlen)
2579 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2583 * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2584 * returned in param->fp_obd_index
2586 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2588 struct obd_uuid obd_uuid;
2595 if (param->fp_got_uuids)
2598 /* Get the lov/lmv name */
2599 if (param->fp_get_lmv)
2600 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2602 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2604 if (rc != -ENOTTY) {
2605 llapi_error(LLAPI_MSG_ERROR, rc,
2606 "error: can't get %s name: %s",
2607 param->fp_get_lmv ? "lmv" : "lov",
2615 param->fp_got_uuids = 1;
2617 /* Now get the ost uuids */
2618 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2619 obd_uuid.uuid, FILTER_BY_EXACT,
2620 "target_obd", ¶m_data);
2624 fp = fopen(param_data.gl_pathv[0], "r");
2627 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2628 param_data.gl_pathv[0]);
2632 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2633 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2634 param->fp_get_lmv ? "MDTS" : "OBDS");
2636 snprintf(format, sizeof(format),
2637 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2638 while (fgets(buf, sizeof(buf), fp) != NULL) {
2641 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2644 if (param->fp_obd_uuid) {
2645 if (llapi_uuid_match(obd_uuid.uuid,
2646 param->fp_obd_uuid->uuid)) {
2647 param->fp_obd_index = index;
2650 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2651 /* Print everything */
2652 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2655 param->fp_obds_printed = 1;
2659 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2660 llapi_err_noerrno(LLAPI_MSG_ERROR,
2661 "error: %s: unknown obduuid: %s",
2662 __func__, param->fp_obd_uuid->uuid);
2666 cfs_free_param_data(¶m_data);
2671 * In this case, param->fp_obd_uuid will be an array of obduuids and
2672 * obd index for all these obduuids will be returned in
2673 * param->fp_obd_indexes
2675 static int setup_indexes(int d, char *path, struct obd_uuid *obduuids,
2676 int num_obds, int **obdindexes, int *obdindex,
2679 int ret, obdcount, obd_valid = 0, obdnum;
2681 struct obd_uuid *uuids = NULL;
2685 if (type == LOV_TYPE)
2686 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2688 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2692 obdcount = atoi(buf);
2693 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2698 ret = llapi_get_target_uuids(d, uuids, &obdcount, type);
2700 if (ret == -EOVERFLOW) {
2701 struct obd_uuid *uuids_temp;
2703 uuids_temp = realloc(uuids, obdcount *
2704 sizeof(struct obd_uuid));
2705 if (uuids_temp != NULL) {
2707 goto retry_get_uuids;
2712 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2716 indexes = malloc(num_obds * sizeof(*obdindex));
2717 if (indexes == NULL) {
2722 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2725 /* The user may have specified a simple index */
2726 i = strtol(obduuids[obdnum].uuid, &end, 0);
2727 if (end && *end == '\0' && i < obdcount) {
2728 indexes[obdnum] = i;
2731 for (i = 0; i < obdcount; i++) {
2732 if (llapi_uuid_match(uuids[i].uuid,
2733 obduuids[obdnum].uuid)) {
2734 indexes[obdnum] = i;
2740 if (i >= obdcount) {
2741 indexes[obdnum] = OBD_NOT_FOUND;
2742 llapi_err_noerrno(LLAPI_MSG_ERROR,
2743 "invalid obduuid '%s'",
2744 obduuids[obdnum].uuid);
2750 *obdindex = OBD_NOT_FOUND;
2752 *obdindex = obd_valid;
2754 *obdindexes = indexes;
2762 static int setup_target_indexes(int d, char *path, struct find_param *param)
2766 if (param->fp_mdt_uuid) {
2767 ret = setup_indexes(d, path, param->fp_mdt_uuid,
2769 ¶m->fp_mdt_indexes,
2770 ¶m->fp_mdt_index, LMV_TYPE);
2775 if (param->fp_obd_uuid) {
2776 ret = setup_indexes(d, path, param->fp_obd_uuid,
2778 ¶m->fp_obd_indexes,
2779 ¶m->fp_obd_index, LOV_TYPE);
2784 param->fp_got_uuids = 1;
2789 int llapi_ostlist(char *path, struct find_param *param)
2794 fd = open(path, O_RDONLY | O_DIRECTORY);
2798 ret = setup_obd_uuid(fd, path, param);
2805 * Tries to determine the default stripe attributes for a given filesystem. The
2806 * filesystem to check should be specified by fsname, or will be determined
2809 static int sattr_get_defaults(const char *const fsname,
2810 unsigned int *scount,
2811 unsigned int *ssize,
2812 unsigned int *soffset)
2818 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2819 "stripecount", val, sizeof(val));
2822 *scount = atoi(val);
2826 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2827 "stripesize", val, sizeof(val));
2834 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2835 "stripeoffset", val, sizeof(val));
2838 *soffset = atoi(val);
2845 * Tries to gather the default stripe attributes for a given filesystem. If
2846 * the attributes can be determined, they are cached for easy retreival the
2847 * next time they are needed. Only a single filesystem's attributes are
2850 int sattr_cache_get_defaults(const char *const fsname,
2851 const char *const pathname, unsigned int *scount,
2852 unsigned int *ssize, unsigned int *soffset)
2855 char fsname[PATH_MAX + 1];
2856 unsigned int stripecount;
2857 unsigned int stripesize;
2858 unsigned int stripeoffset;
2864 char fsname_buf[PATH_MAX + 1];
2865 unsigned int tmp[3];
2867 if (fsname == NULL) {
2868 rc = llapi_search_fsname(pathname, fsname_buf);
2872 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2875 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2877 * Ensure all 3 sattrs (count, size, and offset) are
2878 * successfully retrieved and stored in tmp before writing to
2881 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2885 cache.stripecount = tmp[0];
2886 cache.stripesize = tmp[1];
2887 cache.stripeoffset = tmp[2];
2888 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2892 *scount = cache.stripecount;
2894 *ssize = cache.stripesize;
2896 *soffset = cache.stripeoffset;
2901 static char *layout2name(__u32 layout_pattern)
2903 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2905 else if (layout_pattern == LOV_PATTERN_MDT)
2907 else if (layout_pattern == LOV_PATTERN_RAID0)
2909 else if (layout_pattern ==
2910 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2911 return "raid0,overstriped";
2916 enum lov_dump_flags {
2917 LDF_IS_DIR = 0x0001,
2918 LDF_IS_RAW = 0x0002,
2919 LDF_INDENT = 0x0004,
2920 LDF_SKIP_OBJS = 0x0008,
2922 LDF_EXTENSION = 0x0020,
2925 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2926 struct lov_user_ost_data_v1 *objects,
2927 enum llapi_layout_verbose verbose,
2928 int depth, char *pool_name,
2929 enum lov_dump_flags flags)
2931 bool is_dir = flags & LDF_IS_DIR;
2932 bool is_raw = flags & LDF_IS_RAW;
2933 bool indent = flags & LDF_INDENT;
2934 bool yaml = flags & LDF_YAML;
2935 bool skip_objs = flags & LDF_SKIP_OBJS;
2936 bool extension = flags & LDF_EXTENSION;
2937 char *prefix = is_dir ? "" : "lmm_";
2938 char *separator = "";
2939 char *space = indent ? " " : "";
2942 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2943 lmm_oi_set_seq(&lum->lmm_oi, 0);
2944 if (!indent && (verbose & VERBOSE_DETAIL))
2945 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2948 if (!yaml && !indent && depth && path &&
2949 ((verbose != VERBOSE_OBJID) || !is_dir))
2950 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2952 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2953 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2954 space, prefix, lum->lmm_magic);
2955 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2957 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2958 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2960 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2963 if (verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) {
2968 if (verbose & ~VERBOSE_DFID)
2969 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2973 struct lu_fid dir_fid;
2975 rc = llapi_path2fid(path, &dir_fid);
2977 llapi_error(LLAPI_MSG_ERROR, rc,
2978 "Cannot determine directory fid.");
2980 seq = dir_fid.f_seq;
2981 oid = dir_fid.f_oid;
2982 ver = dir_fid.f_ver;
2985 * This needs a bit of hand-holding since old 1.x
2986 * lmm_oi have { oi.oi_id = mds_inum, oi.oi_seq = 0 }
2987 * and 2.x lmm_oi have { oi.oi_id = mds_oid,
2988 * oi.oi_seq = mds_seq } instead of a real FID.
2989 * Ideally the 2.x code would have stored this like a
2990 * FID with { oi_id = mds_seq, oi_seq = mds_oid } so
2991 * the ostid union lu_fid { f_seq = mds_seq,
2992 * f_oid = mds_oid } worked properly (especially since
2993 * IGIF FIDs use mds_inum as the FID SEQ), but
2994 * unfortunately that didn't happen.
2996 * Print it to look like an IGIF FID, even though the
2997 * fields are reversed on disk, so that it makes sense
3000 * Don't use ostid_id() and ostid_seq(), since they
3001 * assume the oi_fid fields are in the right order.
3002 * This is why there are separate lmm_oi_seq() and
3003 * lmm_oi_id() routines for this.
3005 * For newer layout types hopefully this will be a
3008 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
3009 lmm_oi_id(&lum->lmm_oi) :
3010 lmm_oi_seq(&lum->lmm_oi);
3011 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
3012 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
3013 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
3017 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
3018 (unsigned long long)seq, oid, ver);
3020 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
3021 (unsigned long long)seq, oid, ver);
3024 if (verbose & VERBOSE_STRIPE_COUNT) {
3025 if (verbose & ~VERBOSE_STRIPE_COUNT)
3026 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
3029 if (!is_raw && lum->lmm_stripe_count == 0 &&
3030 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
3031 unsigned int scount;
3033 rc = sattr_cache_get_defaults(NULL, path,
3037 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3040 llapi_error(LLAPI_MSG_ERROR, rc,
3041 "Cannot determine default stripe count.");
3043 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3045 (__s16)lum->lmm_stripe_count);
3048 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
3050 (__s16)lum->lmm_stripe_count);
3052 if (!yaml && is_dir)
3058 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
3059 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
3060 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3061 if (verbose & ~VERBOSE_EXT_SIZE && extension)
3062 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
3064 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
3065 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
3067 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
3070 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
3073 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
3075 llapi_error(LLAPI_MSG_ERROR, rc,
3076 "Cannot determine default stripe size.");
3078 /* Extension size is in KiB */
3079 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3081 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
3082 (unsigned long long)lum->lmm_stripe_size);
3084 if (!yaml && is_dir)
3090 if ((verbose & VERBOSE_PATTERN)) {
3091 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3092 if (verbose & ~VERBOSE_PATTERN)
3093 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
3095 if (lov_pattern_supported(lum->lmm_pattern))
3096 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3097 layout2name(lum->lmm_pattern));
3099 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
3100 separator = (!yaml && is_dir) ? " " : "\n";
3103 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
3104 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3105 if (verbose & ~VERBOSE_GENERATION)
3106 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
3108 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3109 skip_objs ? 0 : (int)lum->lmm_layout_gen);
3113 if (verbose & VERBOSE_STRIPE_OFFSET) {
3114 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3115 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3116 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
3118 if (is_dir || skip_objs)
3119 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3120 lum->lmm_stripe_offset ==
3121 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
3122 lum->lmm_stripe_offset);
3123 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
3124 llapi_printf(LLAPI_MSG_NORMAL, "0");
3126 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3127 objects[0].l_ost_idx);
3128 if (!yaml && is_dir)
3134 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
3135 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3136 if (verbose & ~VERBOSE_POOL)
3137 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
3139 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
3140 if (!yaml && is_dir)
3146 if (strlen(separator) != 0)
3147 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3150 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
3151 struct lov_user_ost_data_v1 *objects,
3152 char *path, int obdindex, int depth,
3153 enum llapi_layout_verbose verbose,
3154 enum lov_dump_flags flags)
3156 bool is_dir = flags & LDF_IS_DIR;
3157 bool indent = flags & LDF_INDENT;
3158 bool skip_objs = flags & LDF_SKIP_OBJS;
3159 bool yaml = flags & LDF_YAML;
3160 bool obdstripe = obdindex == OBD_NOT_FOUND;
3163 if (!obdstripe && !skip_objs) {
3164 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
3165 if (obdindex == objects[i].l_ost_idx) {
3175 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
3178 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
3179 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
3180 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
3181 char *space = " - ";
3184 llapi_printf(LLAPI_MSG_NORMAL,
3185 "%6slmm_objects:\n", " ");
3187 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
3189 llapi_printf(LLAPI_MSG_NORMAL,
3190 "\tobdidx\t\t objid\t\t objid\t\t group\n");
3192 for (i = 0; i < lum->lmm_stripe_count; i++) {
3193 int idx = objects[i].l_ost_idx;
3194 long long oid = ostid_id(&objects[i].l_ost_oi);
3195 long long gr = ostid_seq(&objects[i].l_ost_oi);
3197 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
3201 struct lu_fid fid = { 0 };
3203 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3204 llapi_printf(LLAPI_MSG_NORMAL,
3205 "%sl_ost_idx: %d\n", space, idx);
3206 llapi_printf(LLAPI_MSG_NORMAL,
3207 "%8sl_fid: "DFID_NOBRACE"\n",
3209 } else if (indent) {
3210 struct lu_fid fid = { 0 };
3212 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3213 llapi_printf(LLAPI_MSG_NORMAL,
3214 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
3215 space, i, idx, PFID(&fid));
3219 sprintf(fmt, "%s%s%s\n",
3220 "\t%6u\t%14llu\t%#13llx\t",
3221 (fid_seq_is_rsvd(gr) ||
3222 fid_seq_is_mdt0(gr)) ?
3223 "%14llu" : "%#14llx", "%s");
3224 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
3226 obdindex == idx ? " *" : "");
3230 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3233 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3234 char *path, int obdindex, int depth,
3235 enum llapi_layout_verbose verbose,
3236 enum lov_dump_flags flags)
3238 struct lmv_user_mds_data *objects = lum->lum_objects;
3239 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3240 char *separator = "";
3241 bool yaml = flags & LDF_YAML;
3242 bool obdstripe = false;
3245 if (obdindex != OBD_NOT_FOUND) {
3246 if (lum->lum_stripe_count == 0) {
3247 if (obdindex == lum->lum_stripe_offset)
3250 for (i = 0; i < lum->lum_stripe_count; i++) {
3251 if (obdindex == objects[i].lum_mds) {
3252 llapi_printf(LLAPI_MSG_NORMAL,
3267 /* show all information default */
3269 if (lum->lum_magic == LMV_USER_MAGIC)
3270 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3271 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3273 verbose = VERBOSE_OBJID;
3276 if (depth && path && ((verbose != VERBOSE_OBJID)))
3277 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3279 if (verbose & VERBOSE_STRIPE_COUNT) {
3280 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3281 if (verbose & ~VERBOSE_STRIPE_COUNT)
3282 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3283 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3284 (int)lum->lum_stripe_count);
3285 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3291 if (verbose & VERBOSE_STRIPE_OFFSET) {
3292 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3293 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3294 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3295 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3296 (int)lum->lum_stripe_offset);
3297 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3303 if (verbose & VERBOSE_HASH_TYPE) {
3304 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3305 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3307 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3308 if (verbose & ~VERBOSE_HASH_TYPE)
3309 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3310 if (type < LMV_HASH_TYPE_MAX)
3311 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3312 mdt_hash_name[type]);
3314 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3316 if (flags & LMV_HASH_FLAG_MIGRATION)
3317 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3318 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3319 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3320 if (flags & LMV_HASH_FLAG_LOST_LMV)
3321 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3322 if (flags & LMV_HASH_FLAG_FIXED)
3323 llapi_printf(LLAPI_MSG_NORMAL, ",fixed");
3324 if (flags & ~LMV_HASH_FLAG_KNOWN)
3325 llapi_printf(LLAPI_MSG_NORMAL, ",unknown_%04x",
3326 flags & ~LMV_HASH_FLAG_KNOWN);
3328 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3334 if ((verbose & VERBOSE_INHERIT) && lum->lum_magic == LMV_USER_MAGIC) {
3335 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3336 if (verbose & ~VERBOSE_INHERIT)
3337 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit: ");
3338 if (lum->lum_max_inherit == LMV_INHERIT_UNLIMITED)
3339 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3340 else if (lum->lum_max_inherit == LMV_INHERIT_NONE)
3341 llapi_printf(LLAPI_MSG_NORMAL, "0");
3343 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3344 lum->lum_max_inherit);
3345 if (verbose & VERBOSE_INHERIT && !yaml)
3351 if ((verbose & VERBOSE_INHERIT_RR) &&
3352 lum->lum_magic == LMV_USER_MAGIC &&
3353 lum->lum_stripe_offset == LMV_OFFSET_DEFAULT) {
3354 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3355 if (verbose & ~VERBOSE_INHERIT_RR)
3356 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit_rr: ");
3357 if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_UNLIMITED)
3358 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3359 else if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_NONE)
3360 llapi_printf(LLAPI_MSG_NORMAL, "0");
3362 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3363 lum->lum_max_inherit_rr);
3364 if (verbose & VERBOSE_INHERIT_RR && !yaml)
3372 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3373 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3374 if (lum->lum_stripe_count > 0)
3375 llapi_printf(LLAPI_MSG_NORMAL,
3376 "mdtidx\t\t FID[seq:oid:ver]\n");
3377 for (i = 0; i < lum->lum_stripe_count; i++) {
3378 int idx = objects[i].lum_mds;
3379 struct lu_fid *fid = &objects[i].lum_fid;
3381 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3382 llapi_printf(LLAPI_MSG_NORMAL,
3383 "%6u\t\t "DFID"\t\t%s\n",
3385 obdindex == idx ? " *" : "");
3389 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3390 pool_name[0] != '\0') {
3391 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3392 if (verbose & ~VERBOSE_POOL)
3393 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3395 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3399 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3400 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3403 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3404 enum lov_dump_flags flags)
3406 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3407 int depth = param->fp_max_depth;
3408 enum llapi_layout_verbose verbose = param->fp_verbose;
3409 bool yaml = flags & LDF_YAML;
3411 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3412 !(flags & LDF_IS_DIR)) && !yaml)
3413 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3415 if (verbose & VERBOSE_DETAIL) {
3416 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3417 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3418 " ", comp_v1->lcm_magic);
3419 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3420 " ", comp_v1->lcm_size);
3421 if (flags & LDF_IS_DIR)
3422 llapi_printf(LLAPI_MSG_NORMAL,
3423 "%2slcm_flags: %s\n", " ",
3424 comp_v1->lcm_mirror_count > 0 ?
3427 llapi_printf(LLAPI_MSG_NORMAL,
3428 "%2slcm_flags: %s\n", " ",
3429 llapi_layout_flags_string(comp_v1->lcm_flags));
3432 if (verbose & VERBOSE_GENERATION) {
3433 if (verbose & ~VERBOSE_GENERATION)
3434 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3436 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3439 if (verbose & VERBOSE_MIRROR_COUNT) {
3440 if (verbose & ~VERBOSE_MIRROR_COUNT)
3441 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3443 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3444 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3445 comp_v1->lcm_mirror_count + 1 : 1);
3448 if (verbose & VERBOSE_COMP_COUNT) {
3449 if (verbose & ~VERBOSE_COMP_COUNT)
3450 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3452 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3453 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3454 comp_v1->lcm_entry_count : 0);
3457 if (verbose & VERBOSE_DETAIL && !yaml)
3458 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3461 static void lcme_flags2str(__u32 comp_flags)
3467 llapi_printf(LLAPI_MSG_NORMAL, "0");
3470 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3471 if (comp_flags & comp_flags_table[i].cfn_flag) {
3473 llapi_printf(LLAPI_MSG_NORMAL, ",");
3474 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3475 comp_flags_table[i].cfn_name);
3476 comp_flags &= ~comp_flags_table[i].cfn_flag;
3482 llapi_printf(LLAPI_MSG_NORMAL, ",");
3483 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3487 static void lov_dump_comp_v1_entry(struct find_param *param,
3488 enum lov_dump_flags flags, int index)
3490 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3491 struct lov_comp_md_entry_v1 *entry;
3492 char *separator = "";
3493 enum llapi_layout_verbose verbose = param->fp_verbose;
3494 bool yaml = flags & LDF_YAML;
3496 entry = &comp_v1->lcm_entries[index];
3499 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3501 if (verbose & VERBOSE_COMP_ID) {
3502 if (verbose & VERBOSE_DETAIL && !yaml)
3503 llapi_printf(LLAPI_MSG_NORMAL,
3504 "%slcme_id: ", " - ");
3505 else if (verbose & ~VERBOSE_COMP_ID)
3506 llapi_printf(LLAPI_MSG_NORMAL,
3507 "%4slcme_id: ", " ");
3508 if (entry->lcme_id != LCME_ID_INVAL)
3509 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3511 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3515 if (verbose & VERBOSE_MIRROR_ID) {
3516 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3517 if (verbose & ~VERBOSE_MIRROR_ID)
3518 llapi_printf(LLAPI_MSG_NORMAL,
3519 "%4slcme_mirror_id: ", " ");
3520 if (entry->lcme_id != LCME_ID_INVAL)
3521 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3522 mirror_id_of(entry->lcme_id));
3524 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3528 if (verbose & VERBOSE_COMP_FLAGS) {
3529 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3530 if (verbose & ~VERBOSE_COMP_FLAGS)
3531 llapi_printf(LLAPI_MSG_NORMAL,
3532 "%4slcme_flags: ", " ");
3533 lcme_flags2str(entry->lcme_flags);
3536 /* print snapshot timestamp if its a nosync comp */
3537 if ((verbose & VERBOSE_COMP_FLAGS) &&
3538 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3539 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3540 if (verbose & ~VERBOSE_COMP_FLAGS)
3541 llapi_printf(LLAPI_MSG_NORMAL,
3542 "%4slcme_timestamp: ", " ");
3544 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3545 (unsigned long long)entry->lcme_timestamp);
3547 time_t stamp = entry->lcme_timestamp;
3548 char *date_str = asctime(localtime(&stamp));
3550 date_str[strlen(date_str) - 1] = '\0';
3551 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3557 if (verbose & VERBOSE_COMP_START) {
3558 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3559 if (verbose & ~VERBOSE_COMP_START)
3560 llapi_printf(LLAPI_MSG_NORMAL,
3561 "%4slcme_extent.e_start: ", " ");
3562 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3563 (unsigned long long)entry->lcme_extent.e_start);
3567 if (verbose & VERBOSE_COMP_END) {
3568 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3569 if (verbose & ~VERBOSE_COMP_END)
3570 llapi_printf(LLAPI_MSG_NORMAL,
3571 "%4slcme_extent.e_end: ", " ");
3572 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3573 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3575 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3576 (unsigned long long)entry->lcme_extent.e_end);
3581 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3582 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3583 } else if (verbose & VERBOSE_DETAIL) {
3584 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3585 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3586 " ", entry->lcme_offset);
3587 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3588 " ", entry->lcme_size);
3589 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3591 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3596 * Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3597 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3599 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3600 * The table below gives the answers for the specified parameters (value and
3601 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3602 * --------------------------------------
3603 * 1 | file > limit; sign > 0 | -1 / -1 |
3604 * 2 | file = limit; sign > 0 | -1 / -1 |
3605 * 3 | file < limit; sign > 0 | ? / 1 |
3606 * 4 | file > limit; sign = 0 | -1 / -1 |
3607 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3608 * 6 | file < limit; sign = 0 | ? / -1 |
3609 * 7 | file > limit; sign < 0 | 1 / 1 |
3610 * 8 | file = limit; sign < 0 | ? / -1 |
3611 * 9 | file < limit; sign < 0 | ? / -1 |
3612 * --------------------------------------
3613 * Note: 5th actually means that the value is within the interval
3614 * (limit - margin, limit].
3616 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3617 int sign, int negopt, unsigned long long margin,
3623 /* Drop the fraction of margin (of days or size). */
3624 if (file + margin <= limit)
3626 } else if (sign == 0) {
3627 if (file <= limit && file + margin > limit)
3629 else if (file + margin <= limit)
3631 } else if (sign < 0) {
3638 return negopt ? ~ret + 1 : ret;
3641 static inline struct lov_user_md *
3642 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3644 return (struct lov_user_md *)((char *)comp_v1 +
3645 comp_v1->lcm_entries[ent_idx].lcme_offset);
3648 static inline struct lov_user_ost_data_v1 *
3649 lov_v1v3_objects(struct lov_user_md *v1)
3651 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3652 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3654 return v1->lmm_objects;
3658 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3660 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3661 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3662 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3664 pool_name[0] = '\0';
3668 print_last_init_comp(struct find_param *param)
3670 /* print all component info */
3671 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3674 /* print specific component info */
3675 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3676 param->fp_check_comp_start || param->fp_check_comp_end ||
3677 param->fp_check_mirror_id || param->fp_check_mirror_index)
3683 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3687 if (param->fp_comp_end == LUSTRE_EOF) {
3688 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3689 match = end == LUSTRE_EOF ? 1 : -1;
3690 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3691 match = end == LUSTRE_EOF ? -1 : 1;
3692 else /* at least EOF */
3694 if (param->fp_exclude_comp_end)
3697 unsigned long long margin;
3699 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3700 match = find_value_cmp(end, param->fp_comp_end,
3701 param->fp_comp_end_sign,
3702 param->fp_exclude_comp_end, margin, 0);
3709 * An example of "getstripe -v" for a two components PFL file:
3712 * lcm_magic: 0x0BD60BD0
3716 * lcm_entry_count: 2
3720 * lcme_extent.e_start: 0
3721 * lcme_extent.e_end: 1048576
3725 * lmm_magic: 0x0BD10BD0
3726 * lmm_seq: 0x200000401
3727 * lmm_object_id: 0x1
3728 * lmm_fid: [0x200000401:0x1:0x0]
3729 * lmm_stripe_count: 1
3730 * lmm_stripe_size: 1048576
3731 * lmm_pattern: raid0
3733 * lmm_stripe_offset: 0
3735 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3739 * lcme_extent.e_start: 1048576
3740 * lcme_extent.e_end: EOF
3744 * lmm_magic: 0x0BD10BD0
3745 * lmm_seq: 0x200000401
3746 * lmm_object_id: 0x1
3747 * lmm_fid: [0x200000401:0x1:0x0]
3748 * lmm_stripe_count: 2
3749 * lmm_stripe_size: 1048576
3750 * lmm_pattern: raid0
3752 * lmm_stripe_offset: 1
3754 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3755 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3757 static void lov_dump_comp_v1(struct find_param *param, char *path,
3758 enum lov_dump_flags flags)
3760 struct lov_comp_md_entry_v1 *entry;
3761 struct lov_user_ost_data_v1 *objects;
3762 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3763 struct lov_user_md_v1 *v1;
3764 char pool_name[LOV_MAXPOOLNAME + 1];
3765 int obdindex = param->fp_obd_index;
3766 int i, j, match, ext;
3767 bool obdstripe = false;
3768 __u16 mirror_index = 0;
3769 __u16 mirror_id = 0;
3771 if (obdindex != OBD_NOT_FOUND) {
3772 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3773 i < comp_v1->lcm_entry_count; i++) {
3774 if (!(comp_v1->lcm_entries[i].lcme_flags &
3778 v1 = lov_comp_entry(comp_v1, i);
3779 objects = lov_v1v3_objects(v1);
3781 for (j = 0; j < v1->lmm_stripe_count; j++) {
3782 if (obdindex == objects[j].l_ost_idx) {
3795 lov_dump_comp_v1_header(param, path, flags);
3797 flags |= LDF_INDENT;
3799 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3800 entry = &comp_v1->lcm_entries[i];
3802 if (param->fp_check_comp_flags) {
3803 if (((param->fp_comp_flags & entry->lcme_flags) !=
3804 param->fp_comp_flags) ||
3805 (param->fp_comp_neg_flags & entry->lcme_flags))
3809 if (param->fp_check_comp_id &&
3810 param->fp_comp_id != entry->lcme_id)
3813 if (param->fp_check_comp_start) {
3814 match = find_value_cmp(entry->lcme_extent.e_start,
3815 param->fp_comp_start,
3816 param->fp_comp_start_sign,
3818 param->fp_comp_start_units, 0);
3823 if (param->fp_check_comp_end) {
3824 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3830 if (param->fp_check_mirror_index) {
3831 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3833 mirror_id = mirror_id_of(entry->lcme_id);
3836 match = find_value_cmp(mirror_index,
3837 param->fp_mirror_index,
3838 param->fp_mirror_index_sign,
3839 param->fp_exclude_mirror_index,
3843 } else if (param->fp_check_mirror_id) {
3844 if (mirror_id != mirror_id_of(entry->lcme_id))
3845 mirror_id = mirror_id_of(entry->lcme_id);
3847 match = find_value_cmp(mirror_id,
3848 param->fp_mirror_id,
3849 param->fp_mirror_id_sign,
3850 param->fp_exclude_mirror_id,
3856 if (print_last_init_comp(param)) {
3858 * if part of stripe info is needed, we'd print only
3859 * the last instantiated component info.
3861 if (entry->lcme_flags & LCME_FL_INIT)
3864 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3865 if (entry->lcme_flags & LCME_FL_EXTENSION)
3866 /* moved back below */
3874 if (entry->lcme_flags & LCME_FL_INIT) {
3875 if (obdindex != OBD_NOT_FOUND) {
3876 flags |= LDF_SKIP_OBJS;
3877 v1 = lov_comp_entry(comp_v1, i);
3878 objects = lov_v1v3_objects(v1);
3880 for (j = 0; j < v1->lmm_stripe_count; j++) {
3881 if (obdindex == objects[j].l_ost_idx) {
3882 flags &= ~LDF_SKIP_OBJS;
3887 flags &= ~LDF_SKIP_OBJS;
3890 flags |= LDF_SKIP_OBJS;
3893 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3895 lov_dump_comp_v1_entry(param, flags, i);
3897 v1 = lov_comp_entry(comp_v1, i);
3898 objects = lov_v1v3_objects(v1);
3899 lov_v1v3_pool_name(v1, pool_name);
3901 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3902 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3903 param->fp_max_depth, param->fp_verbose,
3906 if (print_last_init_comp(param)) {
3908 * directory layout contains only layout template, print the
3912 i = comp_v1->lcm_entry_count - 1;
3915 flags &= ~LDF_SKIP_OBJS;
3917 lov_dump_comp_v1_entry(param, flags, i);
3919 v1 = lov_comp_entry(comp_v1, i);
3920 objects = lov_v1v3_objects(v1);
3921 lov_v1v3_pool_name(v1, pool_name);
3923 entry = &comp_v1->lcm_entries[i];
3924 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3925 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3926 param->fp_max_depth, param->fp_verbose,
3931 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3932 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3935 static inline bool has_any_comp_options(struct find_param *param)
3937 enum llapi_layout_verbose verbose = param->fp_verbose;
3939 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3940 param->fp_check_comp_start || param->fp_check_comp_end ||
3941 param->fp_check_comp_flags)
3944 /* show full layout information, not component specific */
3945 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3948 return verbose & VERBOSE_COMP_OPTS;
3951 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3954 struct lov_user_md *lum = &orig->lmd_lmm;
3955 struct lov_user_mds_data *new;
3956 struct lov_comp_md_v1 *comp_v1;
3957 struct lov_comp_md_entry_v1 *ent;
3958 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3959 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3962 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3964 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3968 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3969 + sizeof(new->lmd_lmmsize));
3971 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3972 comp_v1->lcm_magic = lum->lmm_magic;
3973 comp_v1->lcm_size = lum_off + lum_size;
3974 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3975 comp_v1->lcm_flags = 0;
3976 comp_v1->lcm_entry_count = 1;
3978 ent = &comp_v1->lcm_entries[0];
3980 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3981 ent->lcme_extent.e_start = 0;
3982 ent->lcme_extent.e_end = LUSTRE_EOF;
3983 ent->lcme_offset = lum_off;
3984 ent->lcme_size = lum_size;
3986 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3991 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3992 enum lov_dump_flags flags)
3994 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3996 if (has_any_comp_options(param)) {
3997 struct lov_user_mds_data *new_lmd, *orig_lmd;
3999 orig_lmd = param->fp_lmd;
4000 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
4001 if (new_lmd != NULL) {
4002 param->fp_lmd = new_lmd;
4003 lov_dump_comp_v1(param, path, flags);
4004 param->fp_lmd = orig_lmd;
4010 if (magic == LOV_USER_MAGIC_V1) {
4011 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
4012 param->fp_lmd->lmd_lmm.lmm_objects,
4013 path, param->fp_obd_index,
4014 param->fp_max_depth, param->fp_verbose,
4017 char pool_name[LOV_MAXPOOLNAME + 1];
4018 struct lov_user_ost_data_v1 *objects;
4019 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
4021 snprintf(pool_name, sizeof(pool_name), "%s",
4022 lmmv3->lmm_pool_name);
4023 objects = lmmv3->lmm_objects;
4024 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
4025 objects, path, param->fp_obd_index,
4026 param->fp_max_depth, param->fp_verbose,
4031 static uint32_t check_foreign_type(uint32_t foreign_type)
4035 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
4036 if (lu_foreign_types[i].lft_name == NULL)
4038 if (foreign_type == lu_foreign_types[i].lft_type)
4042 return LU_FOREIGN_TYPE_UNKNOWN;
4045 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
4046 enum lov_dump_flags flags)
4048 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
4049 bool yaml = flags & LDF_YAML;
4051 if (!yaml && param->fp_depth && path)
4052 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4054 if (param->fp_verbose & VERBOSE_DETAIL) {
4055 uint32_t type = check_foreign_type(lfm->lfm_type);
4057 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
4059 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
4061 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
4063 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4064 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4065 lu_foreign_types[type].lft_name);
4067 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4069 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
4072 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
4073 lfm->lfm_length, lfm->lfm_value);
4074 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4077 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
4078 enum lov_dump_flags flags)
4080 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
4081 bool yaml = flags & LDF_YAML;
4083 if (!yaml && param->fp_depth && path)
4084 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4086 if (param->fp_verbose & VERBOSE_DETAIL) {
4087 uint32_t type = check_foreign_type(lfm->lfm_type);
4089 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
4091 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
4093 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
4095 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4096 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4097 lu_foreign_types[type].lft_name);
4099 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4101 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
4104 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
4105 lfm->lfm_length, lfm->lfm_value);
4106 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4109 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
4110 enum lov_dump_flags flags)
4114 if (param->fp_get_lmv || param->fp_get_default_lmv)
4115 magic = (__u32)param->fp_lmv_md->lum_magic;
4117 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
4120 flags |= LDF_IS_RAW;
4125 case LOV_USER_MAGIC_V1:
4126 case LOV_USER_MAGIC_V3:
4127 case LOV_USER_MAGIC_SPECIFIC:
4128 lov_dump_plain_user_lmm(param, path, flags);
4130 case LOV_USER_MAGIC_FOREIGN:
4131 lov_dump_foreign_lmm(param, path, flags);
4134 case LMV_USER_MAGIC: {
4135 char pool_name[LOV_MAXPOOLNAME + 1];
4136 struct lmv_user_md *lum;
4138 lum = (struct lmv_user_md *)param->fp_lmv_md;
4139 snprintf(pool_name, sizeof(pool_name), "%s",
4140 lum->lum_pool_name);
4141 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
4142 param->fp_max_depth, param->fp_verbose,
4146 case LOV_USER_MAGIC_COMP_V1:
4147 lov_dump_comp_v1(param, path, flags);
4149 case LMV_MAGIC_FOREIGN:
4150 lmv_dump_foreign_lmm(param, path, flags);
4153 llapi_printf(LLAPI_MSG_NORMAL,
4154 "unknown lmm_magic: %#x (expecting one of %#x %#x %#x %#x)\n",
4155 *(__u32 *)¶m->fp_lmd->lmd_lmm,
4156 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
4157 LMV_USER_MAGIC, LMV_MAGIC_V1);
4162 static int llapi_file_get_stripe1(const char *path, struct lov_user_md *lum)
4168 fname = strrchr(path, '/');
4170 /* It should be a file (or other non-directory) */
4171 if (fname == NULL) {
4172 dname = (char *)malloc(2);
4176 fname = (char *)path;
4178 dname = (char *)malloc(fname - path + 1);
4181 strncpy(dname, path, fname - path);
4182 dname[fname - path] = '\0';
4186 fd = open(dname, O_RDONLY | O_NONBLOCK);
4192 strcpy((char *)lum, fname);
4193 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
4196 if (close(fd) == -1 && rc == 0)
4204 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
4206 char *canon_path = NULL;
4209 rc = llapi_file_get_stripe1(path, lum);
4210 if (!(rc == -ENOTTY || rc == -ENODATA))
4213 /* Handle failure due to symlinks by dereferencing path manually. */
4214 canon_path = canonicalize_file_name(path);
4215 if (canon_path == NULL)
4216 goto out; /* Keep original rc. */
4218 rc2 = llapi_file_get_stripe1(canon_path, lum);
4220 goto out; /* Keep original rc. */
4229 int llapi_file_lookup(int dirfd, const char *name)
4231 struct obd_ioctl_data data = { 0 };
4236 if (dirfd < 0 || name == NULL)
4239 data.ioc_version = OBD_IOCTL_VERSION;
4240 data.ioc_len = sizeof(data);
4241 data.ioc_inlbuf1 = (char *)name;
4242 data.ioc_inllen1 = strlen(name) + 1;
4244 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
4246 llapi_error(LLAPI_MSG_ERROR, rc,
4247 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
4252 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
4259 * Check if the file time matches all the given criteria (e.g. --atime +/-N).
4260 * Return -1 or 1 if file timestamp does not or does match the given criteria
4261 * correspondingly. Return 0 if the MDS time is being checked and there are
4262 * attributes on OSTs and it is not yet clear if the timespamp matches.
4264 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
4265 * updated timestamps.
4267 static int find_time_check(struct find_param *param, int mds)
4269 struct lov_user_mds_data *lmd = param->fp_lmd;
4273 /* Check if file is accepted. */
4274 if (param->fp_atime) {
4275 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4276 param->fp_atime, param->fp_asign,
4277 param->fp_exclude_atime,
4278 param->fp_time_margin, mds);
4284 if (param->fp_mtime) {
4285 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4286 param->fp_mtime, param->fp_msign,
4287 param->fp_exclude_mtime,
4288 param->fp_time_margin, mds);
4293 * If the previous check matches, but this one is not yet clear,
4294 * we should return 0 to do an RPC on OSTs.
4300 if (param->fp_ctime) {
4301 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4302 param->fp_ctime, param->fp_csign,
4303 param->fp_exclude_ctime,
4304 param->fp_time_margin, mds);
4309 * If the previous check matches, but this one is not yet clear,
4310 * we should return 0 to do an RPC on OSTs.
4319 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4321 struct lov_user_mds_data *lmd = param->fp_lmd;
4326 for (i = 0; i < 2; i++) {
4327 /* Check if file is accepted. */
4328 if (param->fp_newery[NEWERXY_ATIME][i]) {
4329 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4330 param->fp_newery[NEWERXY_ATIME][i],
4337 if (param->fp_newery[NEWERXY_MTIME][i]) {
4338 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4339 param->fp_newery[NEWERXY_MTIME][i],
4345 * If the previous check matches, but this one is not
4346 * yet clear, we should return 0 to do an RPC on OSTs.
4352 if (param->fp_newery[NEWERXY_CTIME][i]) {
4353 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4354 param->fp_newery[NEWERXY_CTIME][i],
4360 * If the previous check matches, but this one is not
4361 * yet clear, we should return 0 to do an RPC on OSTs.
4368 * File birth time (btime) can get from MDT directly.
4369 * if @from_mdt is true, it means the input file attributs are
4370 * obtained directly from MDT.
4371 * Thus, if @from_mdt is false, we should skip the following
4377 if (param->fp_newery[NEWERXY_BTIME][i]) {
4378 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4381 rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4382 param->fp_newery[NEWERXY_BTIME][i],
4393 * Check whether the stripes matches the indexes user provided
4397 static int check_obd_match(struct find_param *param)
4399 struct lov_user_ost_data_v1 *objects;
4400 struct lov_comp_md_v1 *comp_v1 = NULL;
4401 struct lov_user_mds_data *lmd = param->fp_lmd;
4402 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4403 int i, j, k, count = 1;
4405 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4408 if (!S_ISREG(lmd->lmd_stx.stx_mode))
4411 /* exclude foreign */
4412 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4413 return param->fp_exclude_obd;
4416 * Only those files should be accepted, which have a
4417 * stripe on the specified OST.
4419 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4420 comp_v1 = (struct lov_comp_md_v1 *)v1;
4421 count = comp_v1->lcm_entry_count;
4424 for (i = 0; i < count; i++) {
4426 v1 = lov_comp_entry(comp_v1, i);
4428 objects = lov_v1v3_objects(v1);
4430 for (j = 0; j < v1->lmm_stripe_count; j++) {
4431 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4434 for (k = 0; k < param->fp_num_obds; k++) {
4435 if (param->fp_obd_indexes[k] ==
4436 objects[j].l_ost_idx)
4437 return !param->fp_exclude_obd;
4442 return param->fp_exclude_obd;
4445 static int check_mdt_match(struct find_param *param)
4449 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4452 /* FIXME: For striped dir, we should get stripe information and check */
4453 for (i = 0; i < param->fp_num_mdts; i++) {
4454 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4455 return !param->fp_exclude_mdt;
4458 if (param->fp_exclude_mdt)
4465 * Check whether the obd is active or not, if it is
4466 * not active, just print the object affected by this
4469 static void print_failed_tgt(struct find_param *param, char *path, int type)
4471 struct obd_statfs stat_buf;
4472 struct obd_uuid uuid_buf;
4473 int tgt_nr, i, *indexes;
4476 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV) {
4477 llapi_error(LLAPI_MSG_NORMAL, ret, "%s: wrong statfs type(%d)",
4482 tgt_nr = (type == LL_STATFS_LOV) ? param->fp_obd_index :
4483 param->fp_mdt_index;
4484 indexes = (type == LL_STATFS_LOV) ? param->fp_obd_indexes :
4485 param->fp_mdt_indexes;
4487 for (i = 0; i < tgt_nr; i++) {
4488 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4489 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4491 ret = llapi_obd_statfs(path, type, indexes[i], &stat_buf,
4494 llapi_error(LLAPI_MSG_NORMAL, ret,
4495 "%s: obd_uuid: %s failed",
4496 __func__, param->fp_obd_uuid->uuid);
4500 static int find_check_stripe_size(struct find_param *param)
4502 struct lov_comp_md_v1 *comp_v1 = NULL;
4503 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4504 __u32 stripe_size = 0;
4505 int ret, i, count = 1;
4507 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4508 return param->fp_exclude_stripe_size ? 1 : -1;
4510 ret = param->fp_exclude_stripe_size ? 1 : -1;
4511 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4512 comp_v1 = (struct lov_comp_md_v1 *)v1;
4513 count = comp_v1->lcm_entry_count;
4516 for (i = 0; i < count; i++) {
4517 struct lov_comp_md_entry_v1 *ent;
4520 v1 = lov_comp_entry(comp_v1, i);
4522 ent = &comp_v1->lcm_entries[i];
4523 if (ent->lcme_flags & LCME_FL_EXTENSION)
4525 if (!(ent->lcme_flags & LCME_FL_INIT))
4528 stripe_size = v1->lmm_stripe_size;
4531 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4532 param->fp_stripe_size_sign,
4533 param->fp_exclude_stripe_size,
4534 param->fp_stripe_size_units, 0);
4539 static int find_check_ext_size(struct find_param *param)
4541 struct lov_comp_md_v1 *comp_v1;
4542 struct lov_user_md_v1 *v1;
4545 ret = param->fp_exclude_ext_size ? 1 : -1;
4546 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4547 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4550 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4551 struct lov_comp_md_entry_v1 *ent;
4553 v1 = lov_comp_entry(comp_v1, i);
4555 ent = &comp_v1->lcm_entries[i];
4556 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4559 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4560 param->fp_ext_size_sign,
4561 param->fp_exclude_ext_size,
4562 param->fp_ext_size_units, 0);
4563 /* If any ext_size matches */
4571 static __u32 find_get_stripe_count(struct find_param *param)
4573 struct lov_comp_md_v1 *comp_v1 = NULL;
4574 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4576 __u32 stripe_count = 0;
4578 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4581 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4582 comp_v1 = (struct lov_comp_md_v1 *)v1;
4583 count = comp_v1->lcm_entry_count;
4586 for (i = 0; i < count; i++) {
4588 struct lov_comp_md_entry_v1 *ent;
4590 v1 = lov_comp_entry(comp_v1, i);
4592 ent = &comp_v1->lcm_entries[i];
4593 if (!(ent->lcme_flags & LCME_FL_INIT))
4596 if (ent->lcme_flags & LCME_FL_EXTENSION)
4599 stripe_count = v1->lmm_stripe_count;
4602 return stripe_count;
4605 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4607 static int find_check_layout(struct find_param *param)
4609 struct lov_comp_md_v1 *comp_v1 = NULL;
4610 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4612 bool found = false, valid = false;
4614 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4615 comp_v1 = (struct lov_comp_md_v1 *)v1;
4616 count = comp_v1->lcm_entry_count;
4619 for (i = 0; i < count; i++) {
4621 v1 = lov_comp_entry(comp_v1, i);
4623 /* foreign file have a special magic but no pattern field */
4624 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4627 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4631 if (v1->lmm_pattern & param->fp_layout) {
4640 if ((found && !param->fp_exclude_layout) ||
4641 (!found && param->fp_exclude_layout))
4648 * if no type specified, check/exclude all foreign
4649 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4651 static int find_check_foreign(struct find_param *param)
4653 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4654 struct lov_foreign_md *lfm;
4656 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4657 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4658 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4659 return param->fp_exclude_foreign ? 1 : -1;
4663 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4664 lfm->lfm_type == param->fp_foreign_type)
4665 return param->fp_exclude_foreign ? -1 : 1;
4666 return param->fp_exclude_foreign ? 1 : -1;
4669 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4670 struct lmv_foreign_md *lfm;
4672 lfm = (void *)param->fp_lmv_md;
4673 if (lmv_is_foreign(lfm->lfm_magic)) {
4674 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4675 return param->fp_exclude_foreign ? 1 : -1;
4679 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4680 lfm->lfm_type == param->fp_foreign_type)
4681 return param->fp_exclude_foreign ? -1 : 1;
4682 return param->fp_exclude_foreign ? 1 : -1;
4687 static int find_check_pool(struct find_param *param)
4689 struct lov_comp_md_v1 *comp_v1 = NULL;
4690 struct lov_user_md_v3 *v3 = (void *)¶m->fp_lmd->lmd_lmm;
4694 if (v3->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4695 comp_v1 = (struct lov_comp_md_v1 *)v3;
4696 count = comp_v1->lcm_entry_count;
4697 /* empty requested pool is taken as no pool search */
4698 if (count == 0 && param->fp_poolname[0] == '\0') {
4704 for (i = 0; i < count; i++) {
4705 if (comp_v1 != NULL) {
4706 if (!(comp_v1->lcm_entries[i].lcme_flags &
4710 v3 = (void *)lov_comp_entry(comp_v1, i);
4713 if (v3->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4716 if (((v3->lmm_magic == LOV_USER_MAGIC_V1) &&
4717 (param->fp_poolname[0] == '\0')) ||
4718 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4719 (strncmp(v3->lmm_pool_name,
4720 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4721 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4722 (strcmp(param->fp_poolname, "*") == 0))) {
4729 if ((found && !param->fp_exclude_pool) ||
4730 (!found && param->fp_exclude_pool))
4736 static int find_check_comp_options(struct find_param *param)
4738 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4739 struct lov_user_mds_data *lmd = param->fp_lmd;
4740 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4741 struct lov_comp_md_entry_v1 *entry;
4744 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4747 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4748 comp_v1 = (struct lov_comp_md_v1 *)v1;
4750 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4751 if (forged_v1 == NULL)
4753 comp_v1 = forged_v1;
4754 comp_v1->lcm_entry_count = 1;
4755 entry = &comp_v1->lcm_entries[0];
4756 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4758 entry->lcme_extent.e_start = 0;
4759 entry->lcme_extent.e_end = LUSTRE_EOF;
4762 /* invalid case, don't match for any kind of search. */
4763 if (comp_v1->lcm_entry_count == 0) {
4768 if (param->fp_check_comp_count) {
4769 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4770 param->fp_comp_count,
4771 param->fp_comp_count_sign,
4772 param->fp_exclude_comp_count, 1, 0);
4778 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4779 entry = &comp_v1->lcm_entries[i];
4781 if (param->fp_check_comp_flags) {
4783 if (((param->fp_comp_flags & entry->lcme_flags) !=
4784 param->fp_comp_flags) ||
4785 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4791 if (param->fp_check_comp_start) {
4792 ret = find_value_cmp(entry->lcme_extent.e_start,
4793 param->fp_comp_start,
4794 param->fp_comp_start_sign,
4795 param->fp_exclude_comp_start,
4796 param->fp_comp_start_units, 0);
4801 if (param->fp_check_comp_end) {
4802 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4808 /* the component matches all criteria */
4817 static int find_check_mirror_options(struct find_param *param)
4819 struct lov_comp_md_v1 *comp_v1;
4820 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4823 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4826 comp_v1 = (struct lov_comp_md_v1 *)v1;
4828 if (param->fp_check_mirror_count) {
4829 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4830 param->fp_mirror_count,
4831 param->fp_mirror_count_sign,
4832 param->fp_exclude_mirror_count, 1, 0);
4837 if (param->fp_check_mirror_state) {
4839 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4841 if ((param->fp_mirror_state != 0 &&
4842 file_state != param->fp_mirror_state) ||
4843 file_state == param->fp_mirror_neg_state)
4850 static bool find_check_lmm_info(struct find_param *param)
4852 return param->fp_check_pool || param->fp_check_stripe_count ||
4853 param->fp_check_stripe_size || param->fp_check_layout ||
4854 param->fp_check_comp_count || param->fp_check_comp_end ||
4855 param->fp_check_comp_start || param->fp_check_comp_flags ||
4856 param->fp_check_mirror_count || param->fp_check_foreign ||
4857 param->fp_check_mirror_state || param->fp_check_ext_size ||
4858 param->fp_check_projid;
4862 * Get file/directory project id.
4863 * by the open fd resides on.
4864 * Return 0 and project id on success, or -ve errno.
4866 static int fget_projid(int fd, int *projid)
4871 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
4875 *projid = fsx.fsx_projid;
4880 * Check that the file's permissions in *st matches the one in find_param
4882 static int check_file_permissions(const struct find_param *param,
4885 const mode_t st_mode = st->st_mode & 07777;
4888 switch (param->fp_perm_sign) {
4889 case LFS_FIND_PERM_EXACT:
4890 decision = (st_mode == param->fp_perm);
4892 case LFS_FIND_PERM_ALL:
4893 decision = ((st_mode & param->fp_perm) == param->fp_perm);
4895 case LFS_FIND_PERM_ANY:
4896 decision = ((st_mode & param->fp_perm) != 0);
4900 if ((param->fp_exclude_perm && decision)
4901 || (!param->fp_exclude_perm && !decision))
4907 static int cb_find_init(char *path, int p, int *dp,
4908 void *data, struct dirent64 *de)
4910 struct find_param *param = (struct find_param *)data;
4911 struct lov_user_mds_data *lmd = param->fp_lmd;
4912 int d = dp == NULL ? -1 : *dp;
4913 int decision = 1; /* 1 is accepted; -1 is rejected. */
4915 int checked_type = 0;
4917 __u32 stripe_count = 0;
4921 if (p == -1 && d == -1)
4924 /* If a regular expression is presented, make the initial decision */
4925 if (param->fp_pattern != NULL) {
4926 char *fname = strrchr(path, '/');
4928 fname = (fname == NULL ? path : fname + 1);
4929 ret = fnmatch(param->fp_pattern, fname, 0);
4930 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4931 (ret == 0 && param->fp_exclude_pattern))
4935 /* See if we can check the file type from the dirent. */
4936 if (de != NULL && de->d_type != DT_UNKNOWN) {
4937 if (param->fp_type != 0) {
4940 if (DTTOIF(de->d_type) == param->fp_type) {
4941 if (param->fp_exclude_type)
4944 if (!param->fp_exclude_type)
4948 if ((param->fp_check_mdt_count || param->fp_hash_type ||
4949 param->fp_check_hash_flag) && de->d_type != DT_DIR)
4956 * Request MDS for the stat info if some of these parameters need
4959 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4960 param->fp_check_uid || param->fp_check_gid ||
4961 param->fp_newerxy || param->fp_btime ||
4962 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4963 param->fp_check_size || param->fp_check_blocks ||
4964 find_check_lmm_info(param) ||
4965 param->fp_check_mdt_count || param->fp_hash_type ||
4966 param->fp_check_hash_flag || param->fp_perm_sign)
4969 if (param->fp_type != 0 && checked_type == 0)
4972 if (decision == 0) {
4974 (param->fp_check_mdt_count || param->fp_hash_type ||
4975 param->fp_check_hash_flag || param->fp_check_foreign)) {
4976 param->fp_get_lmv = 1;
4977 ret = cb_get_dirstripe(path, &d, param);
4979 if (errno == ENODATA) {
4981 if (param->fp_check_mdt_count ||
4982 param->fp_hash_type ||
4983 param->fp_check_hash_flag) {
4984 param->fp_lmv_md->lum_stripe_count = 0;
4985 param->fp_lmv_md->lum_hash_type = 0;
4987 if (param->fp_check_foreign) {
4988 if (param->fp_exclude_foreign)
4997 if (param->fp_check_mdt_count) {
4998 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5001 decision = find_value_cmp(param->fp_lmv_md->lum_stripe_count,
5002 param->fp_mdt_count,
5003 param->fp_mdt_count_sign,
5004 param->fp_exclude_mdt_count, 1, 0);
5009 if (param->fp_hash_type) {
5011 __u32 type = param->fp_lmv_md->lum_hash_type &
5014 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5017 found = (1 << type) & param->fp_hash_type;
5018 if ((found && param->fp_exclude_hash_type) ||
5019 (!found && !param->fp_exclude_hash_type))
5023 if (param->fp_check_hash_flag) {
5024 __u32 flags = param->fp_lmv_md->lum_hash_type &
5025 ~LMV_HASH_TYPE_MASK;
5027 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5030 if (!(flags & param->fp_hash_inflags) ||
5031 (flags & param->fp_hash_exflags))
5036 param->fp_lmd->lmd_lmm.lmm_magic = 0;
5037 ret = get_lmd_info_fd(path, p, d, param->fp_lmd,
5038 param->fp_lum_size, GET_LMD_INFO);
5039 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
5040 find_check_lmm_info(param)) {
5041 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
5044 * We need to "fake" the "use the default" values
5045 * since the lmm struct is zeroed out at this point.
5047 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5048 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
5050 ostid_set_seq(&lmm->lmm_oi,
5051 FID_SEQ_LOV_DEFAULT);
5052 lmm->lmm_stripe_size = 0;
5053 lmm->lmm_stripe_count = 0;
5054 lmm->lmm_stripe_offset = -1;
5056 if (ret == 0 && param->fp_mdt_uuid != NULL) {
5058 ret = llapi_file_fget_mdtidx(d,
5059 ¶m->fp_file_mdt_index);
5060 } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
5062 * FIXME: we could get the MDT index from the
5063 * file's FID in lmd->lmd_lmm.lmm_oi without
5064 * opening the file, once we are sure that
5065 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
5066 * That would still be an ioctl() to map the
5067 * FID to the MDT, but not an open RPC.
5069 fd = open(path, O_RDONLY);
5071 ret = llapi_file_fget_mdtidx(fd,
5072 ¶m->fp_file_mdt_index);
5078 * For a special file, we assume it resides on
5079 * the same MDT as the parent directory.
5081 ret = llapi_file_fget_mdtidx(p,
5082 ¶m->fp_file_mdt_index);
5093 stripe_count = find_get_stripe_count(param);
5097 if (param->fp_type && !checked_type) {
5098 if ((param->fp_check_mdt_count || param->fp_check_hash_flag ||
5099 param->fp_hash_type) && !S_ISDIR(lmd->lmd_stx.stx_mode))
5102 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
5103 if (param->fp_exclude_type)
5106 if (!param->fp_exclude_type)
5112 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
5113 if (lustre_fs && param->fp_got_uuids &&
5114 param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
5115 lmd->lmd_stx.stx_dev_minor)) {
5116 /* A lustre/lustre mount point is crossed. */
5117 param->fp_got_uuids = 0;
5118 param->fp_obds_printed = 0;
5119 param->fp_mdt_index = OBD_NOT_FOUND;
5120 param->fp_obd_index = OBD_NOT_FOUND;
5123 if (lustre_fs && !param->fp_got_uuids) {
5124 ret = setup_target_indexes((d != -1) ? d : p, path,
5129 param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
5130 lmd->lmd_stx.stx_dev_minor);
5131 } else if (!lustre_fs && param->fp_got_uuids) {
5132 /* A lustre/non-lustre mount point is crossed. */
5133 param->fp_got_uuids = 0;
5134 param->fp_mdt_index = OBD_NOT_FOUND;
5135 param->fp_obd_index = OBD_NOT_FOUND;
5139 if (param->fp_check_foreign) {
5140 decision = find_check_foreign(param);
5145 if (param->fp_check_stripe_size) {
5146 decision = find_check_stripe_size(param);
5151 if (param->fp_check_ext_size) {
5152 decision = find_check_ext_size(param);
5157 if (param->fp_check_stripe_count) {
5158 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
5159 param->fp_stripe_count_sign,
5160 param->fp_exclude_stripe_count, 1, 0);
5165 if (param->fp_check_layout) {
5166 decision = find_check_layout(param);
5171 /* If an OBD UUID is specified but none matches, skip this file. */
5172 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
5173 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
5177 * If an OST or MDT UUID is given, and some OST matches,
5180 if (param->fp_obd_index != OBD_NOT_FOUND ||
5181 param->fp_mdt_index != OBD_NOT_FOUND) {
5182 if (param->fp_obd_uuid) {
5183 if (check_obd_match(param)) {
5185 * If no mdtuuid is given, we are done.
5186 * Otherwise, fall through to the mdtuuid
5189 if (!param->fp_mdt_uuid)
5196 if (param->fp_mdt_uuid) {
5197 if (check_mdt_match(param))
5204 if (param->fp_check_uid) {
5205 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
5206 if (param->fp_exclude_uid)
5209 if (!param->fp_exclude_uid)
5214 if (param->fp_check_gid) {
5215 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
5216 if (param->fp_exclude_gid)
5219 if (!param->fp_exclude_gid)
5224 if (param->fp_check_projid) {
5228 fd = open(path, O_RDONLY);
5231 ret = fget_projid(fd, &projid);
5236 if (projid == param->fp_projid) {
5237 if (param->fp_exclude_projid)
5240 if (!param->fp_exclude_projid)
5245 if (param->fp_check_pool) {
5246 decision = find_check_pool(param);
5251 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
5252 param->fp_check_comp_start || param->fp_check_comp_end) {
5253 decision = find_check_comp_options(param);
5258 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
5259 decision = find_check_mirror_options(param);
5264 /* Check the time on mds. */
5266 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
5269 for_mds = lustre_fs ?
5270 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5271 decision = find_time_check(param, for_mds);
5276 if (param->fp_btime) {
5277 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
5282 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
5283 param->fp_btime, param->fp_bsign,
5284 param->fp_exclude_btime,
5285 param->fp_time_margin, 0);
5290 if (param->fp_newerxy) {
5293 for_mds = lustre_fs ?
5294 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5295 decision = find_newerxy_check(param, for_mds, true);
5304 flags = param->fp_lmd->lmd_flags;
5305 if (param->fp_check_size &&
5306 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5307 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5308 !(flags & OBD_MD_FLSIZE ||
5309 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
5312 if (param->fp_check_blocks &&
5313 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5314 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5315 !(flags & OBD_MD_FLBLOCKS ||
5316 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
5319 if (param->fp_perm_sign)
5323 * If file still fits the request, ask ost for updated info.
5324 * The regular stat is almost of the same speed as some new
5325 * 'glimpse-size-ioctl'.
5331 * For regular files with the stripe the decision may have not
5332 * been taken yet if *time or size is to be checked.
5334 if (param->fp_obd_index != OBD_NOT_FOUND)
5335 print_failed_tgt(param, path, LL_STATFS_LOV);
5337 if (param->fp_mdt_index != OBD_NOT_FOUND)
5338 print_failed_tgt(param, path, LL_STATFS_LMV);
5341 ret = fstat_f(d, &st);
5342 else if (de != NULL)
5343 ret = fstatat_f(p, de->d_name, &st,
5344 AT_SYMLINK_NOFOLLOW);
5346 ret = lstat_f(path, &st);
5349 if (errno == ENOENT) {
5350 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
5351 "warning: %s: %s does not exist",
5356 llapi_error(LLAPI_MSG_ERROR, ret,
5357 "%s: stat on %s failed",
5363 convert_lmd_statx(param->fp_lmd, &st, true);
5364 /* Check the time on osc. */
5365 decision = find_time_check(param, 0);
5369 if (param->fp_newerxy) {
5370 decision = find_newerxy_check(param, 0, false);
5379 /* Check the file permissions from the stat info */
5380 if (param->fp_perm_sign) {
5381 decision = check_file_permissions(param, &st);
5387 if (param->fp_check_size) {
5388 decision = find_value_cmp(lmd->lmd_stx.stx_size,
5390 param->fp_size_sign,
5391 param->fp_exclude_size,
5392 param->fp_size_units, 0);
5397 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
5398 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
5400 param->fp_blocks_sign,
5401 param->fp_exclude_blocks,
5402 param->fp_blocks_units, 0);
5408 llapi_printf(LLAPI_MSG_NORMAL, "%s%c", path,
5409 param->fp_zero_end ? '\0' : '\n');
5413 /* Do not get down anymore? */
5414 if (param->fp_depth == param->fp_max_depth) {
5425 static int cb_migrate_mdt_init(char *path, int p, int *dp,
5426 void *param_data, struct dirent64 *de)
5428 struct find_param *param = (struct find_param *)param_data;
5429 struct lmv_user_md *lmu = param->fp_lmv_md;
5431 char raw[MAX_IOC_BUFLEN] = {'\0'};
5433 struct obd_ioctl_data data = { 0 };
5439 if (p == -1 && dp == NULL)
5445 if (dp != NULL && *dp != -1)
5449 tmp_p = open_parent(path);
5453 llapi_error(LLAPI_MSG_ERROR, ret,
5454 "can not open %s", path);
5459 path_copy = strdup(path);
5460 filename = basename(path_copy);
5462 data.ioc_inlbuf1 = (char *)filename;
5463 data.ioc_inllen1 = strlen(filename) + 1;
5464 data.ioc_inlbuf2 = (char *)lmu;
5465 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
5467 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5469 llapi_error(LLAPI_MSG_ERROR, ret,
5470 "%s: error packing ioctl data", __func__);
5475 ret = ioctl(tmp_p, LL_IOC_MIGRATE, rawbuf);
5477 if (errno == EBUSY && !retry) {
5479 * because migrate may not be able to lock all involved
5480 * objects in order, for some of them it try lock, while
5481 * there may be conflicting COS locks and cause migrate
5482 * fail with EBUSY, hope a sync() could cause
5483 * transaction commit and release these COS locks.
5488 } else if (errno == EALREADY) {
5489 if (param->fp_verbose & VERBOSE_DETAIL)
5490 llapi_printf(LLAPI_MSG_NORMAL,
5491 "%s migrated to MDT%d already\n",
5492 path, lmu->lum_stripe_offset);
5496 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
5500 } else if (param->fp_verbose & VERBOSE_DETAIL) {
5501 llapi_printf(LLAPI_MSG_NORMAL,
5502 "migrate %s to MDT%d stripe count %d\n",
5503 path, lmu->lum_stripe_offset,
5504 lmu->lum_stripe_count);
5510 * If the directory is being migration, we need
5511 * close the directory after migration,
5512 * so the old directory cache will be cleanup
5513 * on the client side, and re-open to get the
5514 * new directory handle
5516 *dp = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
5519 llapi_error(LLAPI_MSG_ERROR, ret,
5520 "%s: Failed to open '%s'", __func__, path);
5532 /* dir migration finished, shrink its stripes */
5533 static int cb_migrate_mdt_fini(char *path, int p, int *dp, void *data,
5534 struct dirent64 *de)
5536 struct find_param *param = data;
5537 struct lmv_user_md *lmu = param->fp_lmv_md;
5538 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
5541 if (de && de->d_type != DT_DIR)
5546 * close it before setxattr because the latter may destroy the
5547 * original object, and cause close fail.
5555 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
5556 if (ret == -EALREADY)
5559 cb_common_fini(path, p, dp, data, de);
5563 int llapi_migrate_mdt(char *path, struct find_param *param)
5565 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
5569 int llapi_mv(char *path, struct find_param *param)
5571 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
5572 static bool printed;
5575 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
5576 "%s() is deprecated, use llapi_migrate_mdt() instead",
5581 return llapi_migrate_mdt(path, param);
5584 int llapi_find(char *path, struct find_param *param)
5586 return param_callback(path, cb_find_init, cb_common_fini, param);
5590 * Get MDT number that the file/directory inode referenced
5591 * by the open fd resides on.
5592 * Return 0 and mdtidx on success, or -ve errno.
5594 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
5596 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
5601 static int cb_get_mdt_index(char *path, int p, int *dp, void *data,
5602 struct dirent64 *de)
5604 struct find_param *param = (struct find_param *)data;
5605 int d = dp == NULL ? -1 : *dp;
5609 if (p == -1 && d == -1)
5613 ret = llapi_file_fget_mdtidx(d, &mdtidx);
5614 } else /* if (p != -1) */ {
5617 fd = open(path, O_RDONLY | O_NOCTTY);
5619 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5627 if (ret == -ENODATA) {
5628 if (!param->fp_obd_uuid)
5629 llapi_printf(LLAPI_MSG_NORMAL,
5630 "'%s' has no stripe info\n", path);
5632 } else if (ret == -ENOENT) {
5633 llapi_error(LLAPI_MSG_WARN, ret,
5634 "warning: %s: '%s' does not exist",
5637 } else if (ret == -ENOTTY) {
5638 llapi_error(LLAPI_MSG_ERROR, ret,
5639 "%s: '%s' not on a Lustre fs",
5642 llapi_error(LLAPI_MSG_ERROR, ret,
5643 "error: %s: '%s' failed get_mdtidx",
5649 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5650 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5652 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5656 /* Do not go down anymore? */
5657 if (param->fp_depth == param->fp_max_depth)
5665 static int cb_getstripe(char *path, int p, int *dp, void *data,
5666 struct dirent64 *de)
5668 struct find_param *param = (struct find_param *)data;
5669 int d = dp == NULL ? -1 : *dp;
5672 if (p == -1 && d == -1)
5675 if (param->fp_obd_uuid) {
5676 param->fp_quiet = 1;
5677 ret = setup_obd_uuid(d != -1 ? d : p, path, param);
5682 if (d != -1 && (param->fp_get_lmv || param->fp_get_default_lmv))
5683 ret = cb_get_dirstripe(path, &d, param);
5685 (p != -1 && !param->fp_get_lmv && !param->fp_get_default_lmv))
5686 ret = get_lmd_info_fd(path, p, d, ¶m->fp_lmd->lmd_lmm,
5687 param->fp_lum_size, GET_LMD_STRIPE);
5688 else if (d == -1 && (param->fp_get_lmv || param->fp_get_default_lmv)) {
5689 /* in case of a dangling or valid faked symlink dir, opendir()
5690 * should have return either EINVAL or ENOENT, so let's try
5691 * to get LMV just in case, and by opening it as a file but
5692 * with O_NOFOLLOW ...
5694 int fd = open(path, O_RDONLY | O_NOFOLLOW);
5698 ret = cb_get_dirstripe(path, &fd, param);
5700 llapi_lov_dump_user_lmm(param, path, LDF_IS_DIR);
5707 if (errno == ENODATA && d != -1) {
5709 * We need to "fake" the "use the default" values
5710 * since the lmm struct is zeroed out at this point.
5711 * The magic needs to be set in order to satisfy
5712 * a check later on in the code path.
5713 * The object_seq needs to be set for the "(Default)"
5714 * prefix to be displayed.
5716 if (param->fp_get_default_lmv) {
5717 struct lmv_user_md *lum = param->fp_lmv_md;
5719 lum->lum_magic = LMV_USER_MAGIC;
5720 lum->lum_stripe_count = 0;
5721 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
5723 } else if (param->fp_get_lmv) {
5724 struct lmv_user_md *lum = param->fp_lmv_md;
5727 ret = llapi_file_fget_mdtidx(d, &mdtidx);
5730 lum->lum_magic = LMV_MAGIC_V1;
5731 lum->lum_stripe_count = 0;
5732 lum->lum_stripe_offset = mdtidx;
5735 struct lov_user_md *lmm =
5736 ¶m->fp_lmd->lmd_lmm;
5738 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5740 ostid_set_seq(&lmm->lmm_oi,
5741 FID_SEQ_LOV_DEFAULT);
5742 lmm->lmm_stripe_count = 0;
5743 lmm->lmm_stripe_size = 0;
5744 lmm->lmm_stripe_offset = -1;
5747 } else if (errno == ENODATA && p != -1) {
5748 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5749 llapi_printf(LLAPI_MSG_NORMAL,
5750 "%s has no stripe info\n", path);
5752 } else if (errno == ENOENT) {
5753 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5754 "warning: %s: %s does not exist",
5757 } else if (errno == ENOTTY) {
5759 llapi_error(LLAPI_MSG_ERROR, ret,
5760 "%s: '%s' not on a Lustre fs?",
5765 llapi_error(LLAPI_MSG_ERROR, ret,
5766 "error: %s: %s failed for %s",
5768 "LL_IOC_LOV_GETSTRIPE" :
5769 "IOC_MDC_GETFILESTRIPE", path);
5776 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5777 llapi_lov_dump_user_lmm(param, path, d != -1 ? LDF_IS_DIR : 0);
5780 /* Do not get down anymore? */
5781 if (param->fp_depth == param->fp_max_depth)
5789 int llapi_getstripe(char *path, struct find_param *param)
5791 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5792 cb_get_mdt_index : cb_getstripe,
5793 cb_common_fini, param);
5796 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5797 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5799 char raw[MAX_IOC_BUFLEN] = {'\0'};
5801 struct obd_ioctl_data data = { 0 };
5804 data.ioc_inlbuf1 = (char *)&type;
5805 data.ioc_inllen1 = sizeof(__u32);
5806 data.ioc_inlbuf2 = (char *)&index;
5807 data.ioc_inllen2 = sizeof(__u32);
5808 data.ioc_pbuf1 = (char *)stat_buf;
5809 data.ioc_plen1 = sizeof(struct obd_statfs);
5810 data.ioc_pbuf2 = (char *)uuid_buf;
5811 data.ioc_plen2 = sizeof(struct obd_uuid);
5813 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5815 llapi_error(LLAPI_MSG_ERROR, rc,
5816 "%s: error packing ioctl data", __func__);
5820 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5822 return rc < 0 ? -errno : 0;
5825 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5826 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5831 fd = open(path, O_RDONLY);
5834 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5837 * If we can't even open a file on the filesystem (e.g. with
5838 * -ESHUTDOWN), force caller to exit or it will loop forever.
5843 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5850 #define MAX_STRING_SIZE 128
5852 int llapi_ping(char *obd_type, char *obd_name)
5854 int flags = O_RDONLY;
5855 char buf[1] = { 0 };
5859 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5860 obd_type, obd_name);
5864 fd = open(path.gl_pathv[0], flags);
5866 if (errno == EACCES && flags == O_RDONLY) {
5871 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5876 if (flags == O_RDONLY)
5877 rc = read(fd, buf, sizeof(buf));
5879 rc = write(fd, buf, sizeof(buf));
5887 cfs_free_param_data(&path);
5891 int llapi_target_iterate(int type_num, char **obd_type,
5892 void *args, llapi_cb_t cb)
5898 for (i = 0; i < type_num; i++) {
5901 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5905 for (j = 0; j < param.gl_pathc; j++) {
5906 char obd_uuid[UUID_MAX + 1];
5910 fp = fopen(param.gl_pathv[j], "r");
5913 llapi_error(LLAPI_MSG_ERROR, rc,
5914 "error: opening '%s'",
5919 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5921 llapi_error(LLAPI_MSG_ERROR, rc,
5922 "error: reading '%s'",
5927 /* Extract the obd_name from the sysfs path.
5928 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5930 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5936 /* skip /fs/lustre/'obd_type'/ */
5937 obd_name += strlen(obd_type[i]) + 12;
5938 /* chop off after obd_name */
5939 ptr = strrchr(obd_name, '/');
5943 cb(obd_type[i], obd_name, obd_uuid, args);
5952 cfs_free_param_data(¶m);
5956 static void do_target_check(char *obd_type_name, char *obd_name,
5957 char *obd_uuid, void *args)
5961 rc = llapi_ping(obd_type_name, obd_name);
5963 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5965 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5967 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5970 int llapi_target_check(int type_num, char **obd_type, char *dir)
5972 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5975 #undef MAX_STRING_SIZE
5977 /* Is this a lustre fs? */
5978 int llapi_is_lustre_mnttype(const char *type)
5980 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
5983 /* Is this a lustre client fs? */
5984 int llapi_is_lustre_mnt(struct mntent *mnt)
5986 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5987 strstr(mnt->mnt_fsname, ":/") != NULL);
5990 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5992 char fsname[PATH_MAX + 1];
5996 rc = llapi_search_fsname(mnt, fsname);
6000 root = open(mnt, O_RDONLY | O_DIRECTORY);
6003 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
6007 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
6010 if (rc == -ENOENT && LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd))
6011 llapi_error(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, rc,
6012 "Cannot find pool '%s'", qctl->qc_poolname);
6018 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
6023 root = open(mnt, O_RDONLY | O_DIRECTORY);
6026 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
6030 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
6033 llapi_error(LLAPI_MSG_ERROR, rc,
6034 "ioctl on %s for getting connect flags failed", mnt);
6041 * Flush cached pages from all clients.
6043 * \param fd File descriptor
6047 int llapi_file_flush(int fd)
6051 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);