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");
3323 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3329 if ((verbose & VERBOSE_INHERIT) && lum->lum_magic == LMV_USER_MAGIC) {
3330 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3331 if (verbose & ~VERBOSE_INHERIT)
3332 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit: ");
3333 if (lum->lum_max_inherit == LMV_INHERIT_UNLIMITED)
3334 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3335 else if (lum->lum_max_inherit == LMV_INHERIT_NONE)
3336 llapi_printf(LLAPI_MSG_NORMAL, "0");
3338 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3339 lum->lum_max_inherit);
3340 if (verbose & VERBOSE_INHERIT && !yaml)
3346 if ((verbose & VERBOSE_INHERIT_RR) &&
3347 lum->lum_magic == LMV_USER_MAGIC &&
3348 lum->lum_stripe_offset == LMV_OFFSET_DEFAULT) {
3349 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3350 if (verbose & ~VERBOSE_INHERIT_RR)
3351 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit_rr: ");
3352 if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_UNLIMITED)
3353 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3354 else if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_NONE)
3355 llapi_printf(LLAPI_MSG_NORMAL, "0");
3357 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3358 lum->lum_max_inherit_rr);
3359 if (verbose & VERBOSE_INHERIT_RR && !yaml)
3367 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3368 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3369 if (lum->lum_stripe_count > 0)
3370 llapi_printf(LLAPI_MSG_NORMAL,
3371 "mdtidx\t\t FID[seq:oid:ver]\n");
3372 for (i = 0; i < lum->lum_stripe_count; i++) {
3373 int idx = objects[i].lum_mds;
3374 struct lu_fid *fid = &objects[i].lum_fid;
3376 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3377 llapi_printf(LLAPI_MSG_NORMAL,
3378 "%6u\t\t "DFID"\t\t%s\n",
3380 obdindex == idx ? " *" : "");
3384 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3385 pool_name[0] != '\0') {
3386 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3387 if (verbose & ~VERBOSE_POOL)
3388 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3390 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3394 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3395 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3398 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3399 enum lov_dump_flags flags)
3401 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3402 int depth = param->fp_max_depth;
3403 enum llapi_layout_verbose verbose = param->fp_verbose;
3404 bool yaml = flags & LDF_YAML;
3406 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3407 !(flags & LDF_IS_DIR)) && !yaml)
3408 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3410 if (verbose & VERBOSE_DETAIL) {
3411 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3412 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3413 " ", comp_v1->lcm_magic);
3414 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3415 " ", comp_v1->lcm_size);
3416 if (flags & LDF_IS_DIR)
3417 llapi_printf(LLAPI_MSG_NORMAL,
3418 "%2slcm_flags: %s\n", " ",
3419 comp_v1->lcm_mirror_count > 0 ?
3422 llapi_printf(LLAPI_MSG_NORMAL,
3423 "%2slcm_flags: %s\n", " ",
3424 llapi_layout_flags_string(comp_v1->lcm_flags));
3427 if (verbose & VERBOSE_GENERATION) {
3428 if (verbose & ~VERBOSE_GENERATION)
3429 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3431 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3434 if (verbose & VERBOSE_MIRROR_COUNT) {
3435 if (verbose & ~VERBOSE_MIRROR_COUNT)
3436 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3438 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3439 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3440 comp_v1->lcm_mirror_count + 1 : 1);
3443 if (verbose & VERBOSE_COMP_COUNT) {
3444 if (verbose & ~VERBOSE_COMP_COUNT)
3445 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3447 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3448 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3449 comp_v1->lcm_entry_count : 0);
3452 if (verbose & VERBOSE_DETAIL && !yaml)
3453 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3456 static void lcme_flags2str(__u32 comp_flags)
3462 llapi_printf(LLAPI_MSG_NORMAL, "0");
3465 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3466 if (comp_flags & comp_flags_table[i].cfn_flag) {
3468 llapi_printf(LLAPI_MSG_NORMAL, ",");
3469 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3470 comp_flags_table[i].cfn_name);
3471 comp_flags &= ~comp_flags_table[i].cfn_flag;
3477 llapi_printf(LLAPI_MSG_NORMAL, ",");
3478 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3482 static void lov_dump_comp_v1_entry(struct find_param *param,
3483 enum lov_dump_flags flags, int index)
3485 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3486 struct lov_comp_md_entry_v1 *entry;
3487 char *separator = "";
3488 enum llapi_layout_verbose verbose = param->fp_verbose;
3489 bool yaml = flags & LDF_YAML;
3491 entry = &comp_v1->lcm_entries[index];
3494 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3496 if (verbose & VERBOSE_COMP_ID) {
3497 if (verbose & VERBOSE_DETAIL && !yaml)
3498 llapi_printf(LLAPI_MSG_NORMAL,
3499 "%slcme_id: ", " - ");
3500 else if (verbose & ~VERBOSE_COMP_ID)
3501 llapi_printf(LLAPI_MSG_NORMAL,
3502 "%4slcme_id: ", " ");
3503 if (entry->lcme_id != LCME_ID_INVAL)
3504 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3506 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3510 if (verbose & VERBOSE_MIRROR_ID) {
3511 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3512 if (verbose & ~VERBOSE_MIRROR_ID)
3513 llapi_printf(LLAPI_MSG_NORMAL,
3514 "%4slcme_mirror_id: ", " ");
3515 if (entry->lcme_id != LCME_ID_INVAL)
3516 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3517 mirror_id_of(entry->lcme_id));
3519 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3523 if (verbose & VERBOSE_COMP_FLAGS) {
3524 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3525 if (verbose & ~VERBOSE_COMP_FLAGS)
3526 llapi_printf(LLAPI_MSG_NORMAL,
3527 "%4slcme_flags: ", " ");
3528 lcme_flags2str(entry->lcme_flags);
3531 /* print snapshot timestamp if its a nosync comp */
3532 if ((verbose & VERBOSE_COMP_FLAGS) &&
3533 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3534 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3535 if (verbose & ~VERBOSE_COMP_FLAGS)
3536 llapi_printf(LLAPI_MSG_NORMAL,
3537 "%4slcme_timestamp: ", " ");
3539 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3540 (unsigned long long)entry->lcme_timestamp);
3542 time_t stamp = entry->lcme_timestamp;
3543 char *date_str = asctime(localtime(&stamp));
3545 date_str[strlen(date_str) - 1] = '\0';
3546 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3552 if (verbose & VERBOSE_COMP_START) {
3553 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3554 if (verbose & ~VERBOSE_COMP_START)
3555 llapi_printf(LLAPI_MSG_NORMAL,
3556 "%4slcme_extent.e_start: ", " ");
3557 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3558 (unsigned long long)entry->lcme_extent.e_start);
3562 if (verbose & VERBOSE_COMP_END) {
3563 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3564 if (verbose & ~VERBOSE_COMP_END)
3565 llapi_printf(LLAPI_MSG_NORMAL,
3566 "%4slcme_extent.e_end: ", " ");
3567 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3568 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3570 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3571 (unsigned long long)entry->lcme_extent.e_end);
3576 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3577 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3578 } else if (verbose & VERBOSE_DETAIL) {
3579 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3580 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3581 " ", entry->lcme_offset);
3582 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3583 " ", entry->lcme_size);
3584 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3586 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3591 * Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3592 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3594 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3595 * The table below gives the answers for the specified parameters (value and
3596 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3597 * --------------------------------------
3598 * 1 | file > limit; sign > 0 | -1 / -1 |
3599 * 2 | file = limit; sign > 0 | -1 / -1 |
3600 * 3 | file < limit; sign > 0 | ? / 1 |
3601 * 4 | file > limit; sign = 0 | -1 / -1 |
3602 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3603 * 6 | file < limit; sign = 0 | ? / -1 |
3604 * 7 | file > limit; sign < 0 | 1 / 1 |
3605 * 8 | file = limit; sign < 0 | ? / -1 |
3606 * 9 | file < limit; sign < 0 | ? / -1 |
3607 * --------------------------------------
3608 * Note: 5th actually means that the value is within the interval
3609 * (limit - margin, limit].
3611 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3612 int sign, int negopt, unsigned long long margin,
3618 /* Drop the fraction of margin (of days or size). */
3619 if (file + margin <= limit)
3621 } else if (sign == 0) {
3622 if (file <= limit && file + margin > limit)
3624 else if (file + margin <= limit)
3626 } else if (sign < 0) {
3633 return negopt ? ~ret + 1 : ret;
3636 static inline struct lov_user_md *
3637 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3639 return (struct lov_user_md *)((char *)comp_v1 +
3640 comp_v1->lcm_entries[ent_idx].lcme_offset);
3643 static inline struct lov_user_ost_data_v1 *
3644 lov_v1v3_objects(struct lov_user_md *v1)
3646 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3647 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3649 return v1->lmm_objects;
3653 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3655 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3656 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3657 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3659 pool_name[0] = '\0';
3663 print_last_init_comp(struct find_param *param)
3665 /* print all component info */
3666 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3669 /* print specific component info */
3670 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3671 param->fp_check_comp_start || param->fp_check_comp_end ||
3672 param->fp_check_mirror_id || param->fp_check_mirror_index)
3678 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3682 if (param->fp_comp_end == LUSTRE_EOF) {
3683 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3684 match = end == LUSTRE_EOF ? 1 : -1;
3685 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3686 match = end == LUSTRE_EOF ? -1 : 1;
3687 else /* at least EOF */
3689 if (param->fp_exclude_comp_end)
3692 unsigned long long margin;
3694 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3695 match = find_value_cmp(end, param->fp_comp_end,
3696 param->fp_comp_end_sign,
3697 param->fp_exclude_comp_end, margin, 0);
3704 * An example of "getstripe -v" for a two components PFL file:
3707 * lcm_magic: 0x0BD60BD0
3711 * lcm_entry_count: 2
3715 * lcme_extent.e_start: 0
3716 * lcme_extent.e_end: 1048576
3720 * lmm_magic: 0x0BD10BD0
3721 * lmm_seq: 0x200000401
3722 * lmm_object_id: 0x1
3723 * lmm_fid: [0x200000401:0x1:0x0]
3724 * lmm_stripe_count: 1
3725 * lmm_stripe_size: 1048576
3726 * lmm_pattern: raid0
3728 * lmm_stripe_offset: 0
3730 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3734 * lcme_extent.e_start: 1048576
3735 * lcme_extent.e_end: EOF
3739 * lmm_magic: 0x0BD10BD0
3740 * lmm_seq: 0x200000401
3741 * lmm_object_id: 0x1
3742 * lmm_fid: [0x200000401:0x1:0x0]
3743 * lmm_stripe_count: 2
3744 * lmm_stripe_size: 1048576
3745 * lmm_pattern: raid0
3747 * lmm_stripe_offset: 1
3749 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3750 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3752 static void lov_dump_comp_v1(struct find_param *param, char *path,
3753 enum lov_dump_flags flags)
3755 struct lov_comp_md_entry_v1 *entry;
3756 struct lov_user_ost_data_v1 *objects;
3757 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3758 struct lov_user_md_v1 *v1;
3759 char pool_name[LOV_MAXPOOLNAME + 1];
3760 int obdindex = param->fp_obd_index;
3761 int i, j, match, ext;
3762 bool obdstripe = false;
3763 __u16 mirror_index = 0;
3764 __u16 mirror_id = 0;
3766 if (obdindex != OBD_NOT_FOUND) {
3767 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3768 i < comp_v1->lcm_entry_count; i++) {
3769 if (!(comp_v1->lcm_entries[i].lcme_flags &
3773 v1 = lov_comp_entry(comp_v1, i);
3774 objects = lov_v1v3_objects(v1);
3776 for (j = 0; j < v1->lmm_stripe_count; j++) {
3777 if (obdindex == objects[j].l_ost_idx) {
3790 lov_dump_comp_v1_header(param, path, flags);
3792 flags |= LDF_INDENT;
3794 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3795 entry = &comp_v1->lcm_entries[i];
3797 if (param->fp_check_comp_flags) {
3798 if (((param->fp_comp_flags & entry->lcme_flags) !=
3799 param->fp_comp_flags) ||
3800 (param->fp_comp_neg_flags & entry->lcme_flags))
3804 if (param->fp_check_comp_id &&
3805 param->fp_comp_id != entry->lcme_id)
3808 if (param->fp_check_comp_start) {
3809 match = find_value_cmp(entry->lcme_extent.e_start,
3810 param->fp_comp_start,
3811 param->fp_comp_start_sign,
3813 param->fp_comp_start_units, 0);
3818 if (param->fp_check_comp_end) {
3819 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3825 if (param->fp_check_mirror_index) {
3826 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3828 mirror_id = mirror_id_of(entry->lcme_id);
3831 match = find_value_cmp(mirror_index,
3832 param->fp_mirror_index,
3833 param->fp_mirror_index_sign,
3834 param->fp_exclude_mirror_index,
3838 } else if (param->fp_check_mirror_id) {
3839 if (mirror_id != mirror_id_of(entry->lcme_id))
3840 mirror_id = mirror_id_of(entry->lcme_id);
3842 match = find_value_cmp(mirror_id,
3843 param->fp_mirror_id,
3844 param->fp_mirror_id_sign,
3845 param->fp_exclude_mirror_id,
3851 if (print_last_init_comp(param)) {
3853 * if part of stripe info is needed, we'd print only
3854 * the last instantiated component info.
3856 if (entry->lcme_flags & LCME_FL_INIT)
3859 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3860 if (entry->lcme_flags & LCME_FL_EXTENSION)
3861 /* moved back below */
3869 if (entry->lcme_flags & LCME_FL_INIT) {
3870 if (obdindex != OBD_NOT_FOUND) {
3871 flags |= LDF_SKIP_OBJS;
3872 v1 = lov_comp_entry(comp_v1, i);
3873 objects = lov_v1v3_objects(v1);
3875 for (j = 0; j < v1->lmm_stripe_count; j++) {
3876 if (obdindex == objects[j].l_ost_idx) {
3877 flags &= ~LDF_SKIP_OBJS;
3882 flags &= ~LDF_SKIP_OBJS;
3885 flags |= LDF_SKIP_OBJS;
3888 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3890 lov_dump_comp_v1_entry(param, flags, i);
3892 v1 = lov_comp_entry(comp_v1, i);
3893 objects = lov_v1v3_objects(v1);
3894 lov_v1v3_pool_name(v1, pool_name);
3896 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3897 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3898 param->fp_max_depth, param->fp_verbose,
3901 if (print_last_init_comp(param)) {
3903 * directory layout contains only layout template, print the
3907 i = comp_v1->lcm_entry_count - 1;
3910 flags &= ~LDF_SKIP_OBJS;
3912 lov_dump_comp_v1_entry(param, flags, i);
3914 v1 = lov_comp_entry(comp_v1, i);
3915 objects = lov_v1v3_objects(v1);
3916 lov_v1v3_pool_name(v1, pool_name);
3918 entry = &comp_v1->lcm_entries[i];
3919 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3920 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3921 param->fp_max_depth, param->fp_verbose,
3926 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3927 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3930 static inline bool has_any_comp_options(struct find_param *param)
3932 enum llapi_layout_verbose verbose = param->fp_verbose;
3934 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3935 param->fp_check_comp_start || param->fp_check_comp_end ||
3936 param->fp_check_comp_flags)
3939 /* show full layout information, not component specific */
3940 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3943 return verbose & VERBOSE_COMP_OPTS;
3946 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3949 struct lov_user_md *lum = &orig->lmd_lmm;
3950 struct lov_user_mds_data *new;
3951 struct lov_comp_md_v1 *comp_v1;
3952 struct lov_comp_md_entry_v1 *ent;
3953 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3954 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3957 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3959 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3963 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3964 + sizeof(new->lmd_lmmsize));
3966 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3967 comp_v1->lcm_magic = lum->lmm_magic;
3968 comp_v1->lcm_size = lum_off + lum_size;
3969 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3970 comp_v1->lcm_flags = 0;
3971 comp_v1->lcm_entry_count = 1;
3973 ent = &comp_v1->lcm_entries[0];
3975 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3976 ent->lcme_extent.e_start = 0;
3977 ent->lcme_extent.e_end = LUSTRE_EOF;
3978 ent->lcme_offset = lum_off;
3979 ent->lcme_size = lum_size;
3981 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3986 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3987 enum lov_dump_flags flags)
3989 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3991 if (has_any_comp_options(param)) {
3992 struct lov_user_mds_data *new_lmd, *orig_lmd;
3994 orig_lmd = param->fp_lmd;
3995 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3996 if (new_lmd != NULL) {
3997 param->fp_lmd = new_lmd;
3998 lov_dump_comp_v1(param, path, flags);
3999 param->fp_lmd = orig_lmd;
4005 if (magic == LOV_USER_MAGIC_V1) {
4006 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
4007 param->fp_lmd->lmd_lmm.lmm_objects,
4008 path, param->fp_obd_index,
4009 param->fp_max_depth, param->fp_verbose,
4012 char pool_name[LOV_MAXPOOLNAME + 1];
4013 struct lov_user_ost_data_v1 *objects;
4014 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
4016 snprintf(pool_name, sizeof(pool_name), "%s",
4017 lmmv3->lmm_pool_name);
4018 objects = lmmv3->lmm_objects;
4019 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
4020 objects, path, param->fp_obd_index,
4021 param->fp_max_depth, param->fp_verbose,
4026 static uint32_t check_foreign_type(uint32_t foreign_type)
4030 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
4031 if (lu_foreign_types[i].lft_name == NULL)
4033 if (foreign_type == lu_foreign_types[i].lft_type)
4037 return LU_FOREIGN_TYPE_UNKNOWN;
4040 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
4041 enum lov_dump_flags flags)
4043 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
4044 bool yaml = flags & LDF_YAML;
4046 if (!yaml && param->fp_depth && path)
4047 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4049 if (param->fp_verbose & VERBOSE_DETAIL) {
4050 uint32_t type = check_foreign_type(lfm->lfm_type);
4052 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
4054 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
4056 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
4058 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4059 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4060 lu_foreign_types[type].lft_name);
4062 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4064 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
4067 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
4068 lfm->lfm_length, lfm->lfm_value);
4069 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4072 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
4073 enum lov_dump_flags flags)
4075 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
4076 bool yaml = flags & LDF_YAML;
4078 if (!yaml && param->fp_depth && path)
4079 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4081 if (param->fp_verbose & VERBOSE_DETAIL) {
4082 uint32_t type = check_foreign_type(lfm->lfm_type);
4084 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
4086 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
4088 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
4090 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4091 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4092 lu_foreign_types[type].lft_name);
4094 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4096 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
4099 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
4100 lfm->lfm_length, lfm->lfm_value);
4101 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4104 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
4105 enum lov_dump_flags flags)
4109 if (param->fp_get_lmv || param->fp_get_default_lmv)
4110 magic = (__u32)param->fp_lmv_md->lum_magic;
4112 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
4115 flags |= LDF_IS_RAW;
4120 case LOV_USER_MAGIC_V1:
4121 case LOV_USER_MAGIC_V3:
4122 case LOV_USER_MAGIC_SPECIFIC:
4123 lov_dump_plain_user_lmm(param, path, flags);
4125 case LOV_USER_MAGIC_FOREIGN:
4126 lov_dump_foreign_lmm(param, path, flags);
4129 case LMV_USER_MAGIC: {
4130 char pool_name[LOV_MAXPOOLNAME + 1];
4131 struct lmv_user_md *lum;
4133 lum = (struct lmv_user_md *)param->fp_lmv_md;
4134 snprintf(pool_name, sizeof(pool_name), "%s",
4135 lum->lum_pool_name);
4136 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
4137 param->fp_max_depth, param->fp_verbose,
4141 case LOV_USER_MAGIC_COMP_V1:
4142 lov_dump_comp_v1(param, path, flags);
4144 case LMV_MAGIC_FOREIGN:
4145 lmv_dump_foreign_lmm(param, path, flags);
4148 llapi_printf(LLAPI_MSG_NORMAL,
4149 "unknown lmm_magic: %#x (expecting one of %#x %#x %#x %#x)\n",
4150 *(__u32 *)¶m->fp_lmd->lmd_lmm,
4151 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
4152 LMV_USER_MAGIC, LMV_MAGIC_V1);
4157 static int llapi_file_get_stripe1(const char *path, struct lov_user_md *lum)
4163 fname = strrchr(path, '/');
4165 /* It should be a file (or other non-directory) */
4166 if (fname == NULL) {
4167 dname = (char *)malloc(2);
4171 fname = (char *)path;
4173 dname = (char *)malloc(fname - path + 1);
4176 strncpy(dname, path, fname - path);
4177 dname[fname - path] = '\0';
4181 fd = open(dname, O_RDONLY | O_NONBLOCK);
4187 strcpy((char *)lum, fname);
4188 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
4191 if (close(fd) == -1 && rc == 0)
4199 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
4201 char *canon_path = NULL;
4204 rc = llapi_file_get_stripe1(path, lum);
4205 if (!(rc == -ENOTTY || rc == -ENODATA))
4208 /* Handle failure due to symlinks by dereferencing path manually. */
4209 canon_path = canonicalize_file_name(path);
4210 if (canon_path == NULL)
4211 goto out; /* Keep original rc. */
4213 rc2 = llapi_file_get_stripe1(canon_path, lum);
4215 goto out; /* Keep original rc. */
4224 int llapi_file_lookup(int dirfd, const char *name)
4226 struct obd_ioctl_data data = { 0 };
4231 if (dirfd < 0 || name == NULL)
4234 data.ioc_version = OBD_IOCTL_VERSION;
4235 data.ioc_len = sizeof(data);
4236 data.ioc_inlbuf1 = (char *)name;
4237 data.ioc_inllen1 = strlen(name) + 1;
4239 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
4241 llapi_error(LLAPI_MSG_ERROR, rc,
4242 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
4247 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
4254 * Check if the file time matches all the given criteria (e.g. --atime +/-N).
4255 * Return -1 or 1 if file timestamp does not or does match the given criteria
4256 * correspondingly. Return 0 if the MDS time is being checked and there are
4257 * attributes on OSTs and it is not yet clear if the timespamp matches.
4259 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
4260 * updated timestamps.
4262 static int find_time_check(struct find_param *param, int mds)
4264 struct lov_user_mds_data *lmd = param->fp_lmd;
4268 /* Check if file is accepted. */
4269 if (param->fp_atime) {
4270 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4271 param->fp_atime, param->fp_asign,
4272 param->fp_exclude_atime,
4273 param->fp_time_margin, mds);
4279 if (param->fp_mtime) {
4280 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4281 param->fp_mtime, param->fp_msign,
4282 param->fp_exclude_mtime,
4283 param->fp_time_margin, mds);
4288 * If the previous check matches, but this one is not yet clear,
4289 * we should return 0 to do an RPC on OSTs.
4295 if (param->fp_ctime) {
4296 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4297 param->fp_ctime, param->fp_csign,
4298 param->fp_exclude_ctime,
4299 param->fp_time_margin, mds);
4304 * If the previous check matches, but this one is not yet clear,
4305 * we should return 0 to do an RPC on OSTs.
4314 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4316 struct lov_user_mds_data *lmd = param->fp_lmd;
4321 for (i = 0; i < 2; i++) {
4322 /* Check if file is accepted. */
4323 if (param->fp_newery[NEWERXY_ATIME][i]) {
4324 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4325 param->fp_newery[NEWERXY_ATIME][i],
4332 if (param->fp_newery[NEWERXY_MTIME][i]) {
4333 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4334 param->fp_newery[NEWERXY_MTIME][i],
4340 * If the previous check matches, but this one is not
4341 * yet clear, we should return 0 to do an RPC on OSTs.
4347 if (param->fp_newery[NEWERXY_CTIME][i]) {
4348 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4349 param->fp_newery[NEWERXY_CTIME][i],
4355 * If the previous check matches, but this one is not
4356 * yet clear, we should return 0 to do an RPC on OSTs.
4363 * File birth time (btime) can get from MDT directly.
4364 * if @from_mdt is true, it means the input file attributs are
4365 * obtained directly from MDT.
4366 * Thus, if @from_mdt is false, we should skip the following
4372 if (param->fp_newery[NEWERXY_BTIME][i]) {
4373 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4376 rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4377 param->fp_newery[NEWERXY_BTIME][i],
4388 * Check whether the stripes matches the indexes user provided
4392 static int check_obd_match(struct find_param *param)
4394 struct lov_user_ost_data_v1 *objects;
4395 struct lov_comp_md_v1 *comp_v1 = NULL;
4396 struct lov_user_mds_data *lmd = param->fp_lmd;
4397 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4398 int i, j, k, count = 1;
4400 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4403 if (!S_ISREG(lmd->lmd_stx.stx_mode))
4406 /* exclude foreign */
4407 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4408 return param->fp_exclude_obd;
4411 * Only those files should be accepted, which have a
4412 * stripe on the specified OST.
4414 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4415 comp_v1 = (struct lov_comp_md_v1 *)v1;
4416 count = comp_v1->lcm_entry_count;
4419 for (i = 0; i < count; i++) {
4421 v1 = lov_comp_entry(comp_v1, i);
4423 objects = lov_v1v3_objects(v1);
4425 for (j = 0; j < v1->lmm_stripe_count; j++) {
4426 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4429 for (k = 0; k < param->fp_num_obds; k++) {
4430 if (param->fp_obd_indexes[k] ==
4431 objects[j].l_ost_idx)
4432 return !param->fp_exclude_obd;
4437 return param->fp_exclude_obd;
4440 static int check_mdt_match(struct find_param *param)
4444 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4447 /* FIXME: For striped dir, we should get stripe information and check */
4448 for (i = 0; i < param->fp_num_mdts; i++) {
4449 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4450 return !param->fp_exclude_mdt;
4453 if (param->fp_exclude_mdt)
4460 * Check whether the obd is active or not, if it is
4461 * not active, just print the object affected by this
4464 static void print_failed_tgt(struct find_param *param, char *path, int type)
4466 struct obd_statfs stat_buf;
4467 struct obd_uuid uuid_buf;
4468 int tgt_nr, i, *indexes;
4471 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV) {
4472 llapi_error(LLAPI_MSG_NORMAL, ret, "%s: wrong statfs type(%d)",
4477 tgt_nr = (type == LL_STATFS_LOV) ? param->fp_obd_index :
4478 param->fp_mdt_index;
4479 indexes = (type == LL_STATFS_LOV) ? param->fp_obd_indexes :
4480 param->fp_mdt_indexes;
4482 for (i = 0; i < tgt_nr; i++) {
4483 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4484 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4486 ret = llapi_obd_statfs(path, type, indexes[i], &stat_buf,
4489 llapi_error(LLAPI_MSG_NORMAL, ret,
4490 "%s: obd_uuid: %s failed",
4491 __func__, param->fp_obd_uuid->uuid);
4495 static int find_check_stripe_size(struct find_param *param)
4497 struct lov_comp_md_v1 *comp_v1 = NULL;
4498 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4499 __u32 stripe_size = 0;
4500 int ret, i, count = 1;
4502 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4503 return param->fp_exclude_stripe_size ? 1 : -1;
4505 ret = param->fp_exclude_stripe_size ? 1 : -1;
4506 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4507 comp_v1 = (struct lov_comp_md_v1 *)v1;
4508 count = comp_v1->lcm_entry_count;
4511 for (i = 0; i < count; i++) {
4512 struct lov_comp_md_entry_v1 *ent;
4515 v1 = lov_comp_entry(comp_v1, i);
4517 ent = &comp_v1->lcm_entries[i];
4518 if (ent->lcme_flags & LCME_FL_EXTENSION)
4520 if (!(ent->lcme_flags & LCME_FL_INIT))
4523 stripe_size = v1->lmm_stripe_size;
4526 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4527 param->fp_stripe_size_sign,
4528 param->fp_exclude_stripe_size,
4529 param->fp_stripe_size_units, 0);
4534 static int find_check_ext_size(struct find_param *param)
4536 struct lov_comp_md_v1 *comp_v1;
4537 struct lov_user_md_v1 *v1;
4540 ret = param->fp_exclude_ext_size ? 1 : -1;
4541 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4542 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4545 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4546 struct lov_comp_md_entry_v1 *ent;
4548 v1 = lov_comp_entry(comp_v1, i);
4550 ent = &comp_v1->lcm_entries[i];
4551 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4554 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4555 param->fp_ext_size_sign,
4556 param->fp_exclude_ext_size,
4557 param->fp_ext_size_units, 0);
4558 /* If any ext_size matches */
4566 static __u32 find_get_stripe_count(struct find_param *param)
4568 struct lov_comp_md_v1 *comp_v1 = NULL;
4569 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4571 __u32 stripe_count = 0;
4573 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4576 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4577 comp_v1 = (struct lov_comp_md_v1 *)v1;
4578 count = comp_v1->lcm_entry_count;
4581 for (i = 0; i < count; i++) {
4583 struct lov_comp_md_entry_v1 *ent;
4585 v1 = lov_comp_entry(comp_v1, i);
4587 ent = &comp_v1->lcm_entries[i];
4588 if (!(ent->lcme_flags & LCME_FL_INIT))
4591 if (ent->lcme_flags & LCME_FL_EXTENSION)
4594 stripe_count = v1->lmm_stripe_count;
4597 return stripe_count;
4600 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4602 static int find_check_layout(struct find_param *param)
4604 struct lov_comp_md_v1 *comp_v1 = NULL;
4605 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4607 bool found = false, valid = false;
4609 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4610 comp_v1 = (struct lov_comp_md_v1 *)v1;
4611 count = comp_v1->lcm_entry_count;
4614 for (i = 0; i < count; i++) {
4616 v1 = lov_comp_entry(comp_v1, i);
4618 /* foreign file have a special magic but no pattern field */
4619 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4622 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4626 if (v1->lmm_pattern & param->fp_layout) {
4635 if ((found && !param->fp_exclude_layout) ||
4636 (!found && param->fp_exclude_layout))
4643 * if no type specified, check/exclude all foreign
4644 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4646 static int find_check_foreign(struct find_param *param)
4648 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4649 struct lov_foreign_md *lfm;
4651 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4652 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4653 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4654 return param->fp_exclude_foreign ? 1 : -1;
4658 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4659 lfm->lfm_type == param->fp_foreign_type)
4660 return param->fp_exclude_foreign ? -1 : 1;
4661 return param->fp_exclude_foreign ? 1 : -1;
4664 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4665 struct lmv_foreign_md *lfm;
4667 lfm = (void *)param->fp_lmv_md;
4668 if (lmv_is_foreign(lfm->lfm_magic)) {
4669 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4670 return param->fp_exclude_foreign ? 1 : -1;
4674 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4675 lfm->lfm_type == param->fp_foreign_type)
4676 return param->fp_exclude_foreign ? -1 : 1;
4677 return param->fp_exclude_foreign ? 1 : -1;
4682 static int find_check_pool(struct find_param *param)
4684 struct lov_comp_md_v1 *comp_v1 = NULL;
4685 struct lov_user_md_v3 *v3 = (void *)¶m->fp_lmd->lmd_lmm;
4689 if (v3->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4690 comp_v1 = (struct lov_comp_md_v1 *)v3;
4691 count = comp_v1->lcm_entry_count;
4692 /* empty requested pool is taken as no pool search */
4693 if (count == 0 && param->fp_poolname[0] == '\0') {
4699 for (i = 0; i < count; i++) {
4700 if (comp_v1 != NULL) {
4701 if (!(comp_v1->lcm_entries[i].lcme_flags &
4705 v3 = (void *)lov_comp_entry(comp_v1, i);
4708 if (v3->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4711 if (((v3->lmm_magic == LOV_USER_MAGIC_V1) &&
4712 (param->fp_poolname[0] == '\0')) ||
4713 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4714 (strncmp(v3->lmm_pool_name,
4715 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4716 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4717 (strcmp(param->fp_poolname, "*") == 0))) {
4724 if ((found && !param->fp_exclude_pool) ||
4725 (!found && param->fp_exclude_pool))
4731 static int find_check_comp_options(struct find_param *param)
4733 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4734 struct lov_user_mds_data *lmd = param->fp_lmd;
4735 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4736 struct lov_comp_md_entry_v1 *entry;
4739 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4742 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4743 comp_v1 = (struct lov_comp_md_v1 *)v1;
4745 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4746 if (forged_v1 == NULL)
4748 comp_v1 = forged_v1;
4749 comp_v1->lcm_entry_count = 1;
4750 entry = &comp_v1->lcm_entries[0];
4751 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4753 entry->lcme_extent.e_start = 0;
4754 entry->lcme_extent.e_end = LUSTRE_EOF;
4757 /* invalid case, don't match for any kind of search. */
4758 if (comp_v1->lcm_entry_count == 0) {
4763 if (param->fp_check_comp_count) {
4764 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4765 param->fp_comp_count,
4766 param->fp_comp_count_sign,
4767 param->fp_exclude_comp_count, 1, 0);
4773 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4774 entry = &comp_v1->lcm_entries[i];
4776 if (param->fp_check_comp_flags) {
4778 if (((param->fp_comp_flags & entry->lcme_flags) !=
4779 param->fp_comp_flags) ||
4780 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4786 if (param->fp_check_comp_start) {
4787 ret = find_value_cmp(entry->lcme_extent.e_start,
4788 param->fp_comp_start,
4789 param->fp_comp_start_sign,
4790 param->fp_exclude_comp_start,
4791 param->fp_comp_start_units, 0);
4796 if (param->fp_check_comp_end) {
4797 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4803 /* the component matches all criteria */
4812 static int find_check_mirror_options(struct find_param *param)
4814 struct lov_comp_md_v1 *comp_v1;
4815 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4818 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4821 comp_v1 = (struct lov_comp_md_v1 *)v1;
4823 if (param->fp_check_mirror_count) {
4824 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4825 param->fp_mirror_count,
4826 param->fp_mirror_count_sign,
4827 param->fp_exclude_mirror_count, 1, 0);
4832 if (param->fp_check_mirror_state) {
4834 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4836 if ((param->fp_mirror_state != 0 &&
4837 file_state != param->fp_mirror_state) ||
4838 file_state == param->fp_mirror_neg_state)
4845 static bool find_check_lmm_info(struct find_param *param)
4847 return param->fp_check_pool || param->fp_check_stripe_count ||
4848 param->fp_check_stripe_size || param->fp_check_layout ||
4849 param->fp_check_comp_count || param->fp_check_comp_end ||
4850 param->fp_check_comp_start || param->fp_check_comp_flags ||
4851 param->fp_check_mirror_count || param->fp_check_foreign ||
4852 param->fp_check_mirror_state || param->fp_check_ext_size ||
4853 param->fp_check_projid;
4857 * Get file/directory project id.
4858 * by the open fd resides on.
4859 * Return 0 and project id on success, or -ve errno.
4861 static int fget_projid(int fd, int *projid)
4866 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
4870 *projid = fsx.fsx_projid;
4875 * Check that the file's permissions in *st matches the one in find_param
4877 static int check_file_permissions(const struct find_param *param,
4880 const mode_t st_mode = st->st_mode & 07777;
4883 switch (param->fp_perm_sign) {
4884 case LFS_FIND_PERM_EXACT:
4885 decision = (st_mode == param->fp_perm);
4887 case LFS_FIND_PERM_ALL:
4888 decision = ((st_mode & param->fp_perm) == param->fp_perm);
4890 case LFS_FIND_PERM_ANY:
4891 decision = ((st_mode & param->fp_perm) != 0);
4895 if ((param->fp_exclude_perm && decision)
4896 || (!param->fp_exclude_perm && !decision))
4902 static int cb_find_init(char *path, int p, int *dp,
4903 void *data, struct dirent64 *de)
4905 struct find_param *param = (struct find_param *)data;
4906 struct lov_user_mds_data *lmd = param->fp_lmd;
4907 int d = dp == NULL ? -1 : *dp;
4908 int decision = 1; /* 1 is accepted; -1 is rejected. */
4910 int checked_type = 0;
4912 __u32 stripe_count = 0;
4916 if (p == -1 && d == -1)
4919 /* If a regular expression is presented, make the initial decision */
4920 if (param->fp_pattern != NULL) {
4921 char *fname = strrchr(path, '/');
4923 fname = (fname == NULL ? path : fname + 1);
4924 ret = fnmatch(param->fp_pattern, fname, 0);
4925 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4926 (ret == 0 && param->fp_exclude_pattern))
4930 /* See if we can check the file type from the dirent. */
4931 if (de != NULL && de->d_type != DT_UNKNOWN) {
4932 if (param->fp_type != 0) {
4935 if (DTTOIF(de->d_type) == param->fp_type) {
4936 if (param->fp_exclude_type)
4939 if (!param->fp_exclude_type)
4943 if ((param->fp_check_mdt_count || param->fp_hash_type ||
4944 param->fp_check_hash_flag) && de->d_type != DT_DIR)
4951 * Request MDS for the stat info if some of these parameters need
4954 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4955 param->fp_check_uid || param->fp_check_gid ||
4956 param->fp_newerxy || param->fp_btime ||
4957 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4958 param->fp_check_size || param->fp_check_blocks ||
4959 find_check_lmm_info(param) ||
4960 param->fp_check_mdt_count || param->fp_hash_type ||
4961 param->fp_check_hash_flag || param->fp_perm_sign)
4964 if (param->fp_type != 0 && checked_type == 0)
4967 if (decision == 0) {
4969 (param->fp_check_mdt_count || param->fp_hash_type ||
4970 param->fp_check_hash_flag || param->fp_check_foreign)) {
4971 param->fp_get_lmv = 1;
4972 ret = cb_get_dirstripe(path, &d, param);
4974 if (errno == ENODATA) {
4976 if (param->fp_check_mdt_count ||
4977 param->fp_hash_type ||
4978 param->fp_check_hash_flag) {
4979 param->fp_lmv_md->lum_stripe_count = 0;
4980 param->fp_lmv_md->lum_hash_type = 0;
4982 if (param->fp_check_foreign) {
4983 if (param->fp_exclude_foreign)
4992 if (param->fp_check_mdt_count) {
4993 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
4996 decision = find_value_cmp(param->fp_lmv_md->lum_stripe_count,
4997 param->fp_mdt_count,
4998 param->fp_mdt_count_sign,
4999 param->fp_exclude_mdt_count, 1, 0);
5004 if (param->fp_hash_type) {
5006 __u32 type = param->fp_lmv_md->lum_hash_type &
5009 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5012 found = (1 << type) & param->fp_hash_type;
5013 if ((found && param->fp_exclude_hash_type) ||
5014 (!found && !param->fp_exclude_hash_type))
5018 if (param->fp_check_hash_flag) {
5019 __u32 flags = param->fp_lmv_md->lum_hash_type &
5020 ~LMV_HASH_TYPE_MASK;
5022 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5025 if (!(flags & param->fp_hash_inflags) ||
5026 (flags & param->fp_hash_exflags))
5031 param->fp_lmd->lmd_lmm.lmm_magic = 0;
5032 ret = get_lmd_info_fd(path, p, d, param->fp_lmd,
5033 param->fp_lum_size, GET_LMD_INFO);
5034 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
5035 find_check_lmm_info(param)) {
5036 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
5039 * We need to "fake" the "use the default" values
5040 * since the lmm struct is zeroed out at this point.
5042 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5043 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
5045 ostid_set_seq(&lmm->lmm_oi,
5046 FID_SEQ_LOV_DEFAULT);
5047 lmm->lmm_stripe_size = 0;
5048 lmm->lmm_stripe_count = 0;
5049 lmm->lmm_stripe_offset = -1;
5051 if (ret == 0 && param->fp_mdt_uuid != NULL) {
5053 ret = llapi_file_fget_mdtidx(d,
5054 ¶m->fp_file_mdt_index);
5055 } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
5057 * FIXME: we could get the MDT index from the
5058 * file's FID in lmd->lmd_lmm.lmm_oi without
5059 * opening the file, once we are sure that
5060 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
5061 * That would still be an ioctl() to map the
5062 * FID to the MDT, but not an open RPC.
5064 fd = open(path, O_RDONLY);
5066 ret = llapi_file_fget_mdtidx(fd,
5067 ¶m->fp_file_mdt_index);
5073 * For a special file, we assume it resides on
5074 * the same MDT as the parent directory.
5076 ret = llapi_file_fget_mdtidx(p,
5077 ¶m->fp_file_mdt_index);
5088 stripe_count = find_get_stripe_count(param);
5092 if (param->fp_type && !checked_type) {
5093 if ((param->fp_check_mdt_count || param->fp_check_hash_flag ||
5094 param->fp_hash_type) && !S_ISDIR(lmd->lmd_stx.stx_mode))
5097 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
5098 if (param->fp_exclude_type)
5101 if (!param->fp_exclude_type)
5107 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
5108 if (lustre_fs && param->fp_got_uuids &&
5109 param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
5110 lmd->lmd_stx.stx_dev_minor)) {
5111 /* A lustre/lustre mount point is crossed. */
5112 param->fp_got_uuids = 0;
5113 param->fp_obds_printed = 0;
5114 param->fp_mdt_index = OBD_NOT_FOUND;
5115 param->fp_obd_index = OBD_NOT_FOUND;
5118 if (lustre_fs && !param->fp_got_uuids) {
5119 ret = setup_target_indexes((d != -1) ? d : p, path,
5124 param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
5125 lmd->lmd_stx.stx_dev_minor);
5126 } else if (!lustre_fs && param->fp_got_uuids) {
5127 /* A lustre/non-lustre mount point is crossed. */
5128 param->fp_got_uuids = 0;
5129 param->fp_mdt_index = OBD_NOT_FOUND;
5130 param->fp_obd_index = OBD_NOT_FOUND;
5134 if (param->fp_check_foreign) {
5135 decision = find_check_foreign(param);
5140 if (param->fp_check_stripe_size) {
5141 decision = find_check_stripe_size(param);
5146 if (param->fp_check_ext_size) {
5147 decision = find_check_ext_size(param);
5152 if (param->fp_check_stripe_count) {
5153 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
5154 param->fp_stripe_count_sign,
5155 param->fp_exclude_stripe_count, 1, 0);
5160 if (param->fp_check_layout) {
5161 decision = find_check_layout(param);
5166 /* If an OBD UUID is specified but none matches, skip this file. */
5167 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
5168 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
5172 * If an OST or MDT UUID is given, and some OST matches,
5175 if (param->fp_obd_index != OBD_NOT_FOUND ||
5176 param->fp_mdt_index != OBD_NOT_FOUND) {
5177 if (param->fp_obd_uuid) {
5178 if (check_obd_match(param)) {
5180 * If no mdtuuid is given, we are done.
5181 * Otherwise, fall through to the mdtuuid
5184 if (!param->fp_mdt_uuid)
5191 if (param->fp_mdt_uuid) {
5192 if (check_mdt_match(param))
5199 if (param->fp_check_uid) {
5200 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
5201 if (param->fp_exclude_uid)
5204 if (!param->fp_exclude_uid)
5209 if (param->fp_check_gid) {
5210 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
5211 if (param->fp_exclude_gid)
5214 if (!param->fp_exclude_gid)
5219 if (param->fp_check_projid) {
5223 fd = open(path, O_RDONLY);
5226 ret = fget_projid(fd, &projid);
5231 if (projid == param->fp_projid) {
5232 if (param->fp_exclude_projid)
5235 if (!param->fp_exclude_projid)
5240 if (param->fp_check_pool) {
5241 decision = find_check_pool(param);
5246 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
5247 param->fp_check_comp_start || param->fp_check_comp_end) {
5248 decision = find_check_comp_options(param);
5253 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
5254 decision = find_check_mirror_options(param);
5259 /* Check the time on mds. */
5261 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
5264 for_mds = lustre_fs ?
5265 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5266 decision = find_time_check(param, for_mds);
5271 if (param->fp_btime) {
5272 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
5277 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
5278 param->fp_btime, param->fp_bsign,
5279 param->fp_exclude_btime,
5280 param->fp_time_margin, 0);
5285 if (param->fp_newerxy) {
5288 for_mds = lustre_fs ?
5289 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5290 decision = find_newerxy_check(param, for_mds, true);
5299 flags = param->fp_lmd->lmd_flags;
5300 if (param->fp_check_size &&
5301 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5302 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5303 !(flags & OBD_MD_FLSIZE ||
5304 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
5307 if (param->fp_check_blocks &&
5308 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5309 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5310 !(flags & OBD_MD_FLBLOCKS ||
5311 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
5314 if (param->fp_perm_sign)
5318 * If file still fits the request, ask ost for updated info.
5319 * The regular stat is almost of the same speed as some new
5320 * 'glimpse-size-ioctl'.
5326 * For regular files with the stripe the decision may have not
5327 * been taken yet if *time or size is to be checked.
5329 if (param->fp_obd_index != OBD_NOT_FOUND)
5330 print_failed_tgt(param, path, LL_STATFS_LOV);
5332 if (param->fp_mdt_index != OBD_NOT_FOUND)
5333 print_failed_tgt(param, path, LL_STATFS_LMV);
5336 ret = fstat_f(d, &st);
5337 else if (de != NULL)
5338 ret = fstatat_f(p, de->d_name, &st,
5339 AT_SYMLINK_NOFOLLOW);
5341 ret = lstat_f(path, &st);
5344 if (errno == ENOENT) {
5345 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
5346 "warning: %s: %s does not exist",
5351 llapi_error(LLAPI_MSG_ERROR, ret,
5352 "%s: stat on %s failed",
5358 convert_lmd_statx(param->fp_lmd, &st, true);
5359 /* Check the time on osc. */
5360 decision = find_time_check(param, 0);
5364 if (param->fp_newerxy) {
5365 decision = find_newerxy_check(param, 0, false);
5374 /* Check the file permissions from the stat info */
5375 if (param->fp_perm_sign) {
5376 decision = check_file_permissions(param, &st);
5382 if (param->fp_check_size) {
5383 decision = find_value_cmp(lmd->lmd_stx.stx_size,
5385 param->fp_size_sign,
5386 param->fp_exclude_size,
5387 param->fp_size_units, 0);
5392 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
5393 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
5395 param->fp_blocks_sign,
5396 param->fp_exclude_blocks,
5397 param->fp_blocks_units, 0);
5403 llapi_printf(LLAPI_MSG_NORMAL, "%s%c", path,
5404 param->fp_zero_end ? '\0' : '\n');
5408 /* Do not get down anymore? */
5409 if (param->fp_depth == param->fp_max_depth) {
5420 static int cb_migrate_mdt_init(char *path, int p, int *dp,
5421 void *param_data, struct dirent64 *de)
5423 struct find_param *param = (struct find_param *)param_data;
5424 struct lmv_user_md *lmu = param->fp_lmv_md;
5426 char raw[MAX_IOC_BUFLEN] = {'\0'};
5428 struct obd_ioctl_data data = { 0 };
5434 if (p == -1 && dp == NULL)
5440 if (dp != NULL && *dp != -1)
5444 tmp_p = open_parent(path);
5448 llapi_error(LLAPI_MSG_ERROR, ret,
5449 "can not open %s", path);
5454 path_copy = strdup(path);
5455 filename = basename(path_copy);
5457 data.ioc_inlbuf1 = (char *)filename;
5458 data.ioc_inllen1 = strlen(filename) + 1;
5459 data.ioc_inlbuf2 = (char *)lmu;
5460 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
5462 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5464 llapi_error(LLAPI_MSG_ERROR, ret,
5465 "%s: error packing ioctl data", __func__);
5470 ret = ioctl(tmp_p, LL_IOC_MIGRATE, rawbuf);
5472 if (errno == EBUSY && !retry) {
5474 * because migrate may not be able to lock all involved
5475 * objects in order, for some of them it try lock, while
5476 * there may be conflicting COS locks and cause migrate
5477 * fail with EBUSY, hope a sync() could cause
5478 * transaction commit and release these COS locks.
5483 } else if (errno == EALREADY) {
5484 if (param->fp_verbose & VERBOSE_DETAIL)
5485 llapi_printf(LLAPI_MSG_NORMAL,
5486 "%s migrated to MDT%d already\n",
5487 path, lmu->lum_stripe_offset);
5491 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
5495 } else if (param->fp_verbose & VERBOSE_DETAIL) {
5496 llapi_printf(LLAPI_MSG_NORMAL,
5497 "migrate %s to MDT%d stripe count %d\n",
5498 path, lmu->lum_stripe_offset,
5499 lmu->lum_stripe_count);
5505 * If the directory is being migration, we need
5506 * close the directory after migration,
5507 * so the old directory cache will be cleanup
5508 * on the client side, and re-open to get the
5509 * new directory handle
5511 *dp = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
5514 llapi_error(LLAPI_MSG_ERROR, ret,
5515 "%s: Failed to open '%s'", __func__, path);
5527 /* dir migration finished, shrink its stripes */
5528 static int cb_migrate_mdt_fini(char *path, int p, int *dp, void *data,
5529 struct dirent64 *de)
5531 struct find_param *param = data;
5532 struct lmv_user_md *lmu = param->fp_lmv_md;
5533 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
5536 if (de && de->d_type != DT_DIR)
5541 * close it before setxattr because the latter may destroy the
5542 * original object, and cause close fail.
5550 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
5551 if (ret == -EALREADY)
5554 cb_common_fini(path, p, dp, data, de);
5558 int llapi_migrate_mdt(char *path, struct find_param *param)
5560 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
5564 int llapi_mv(char *path, struct find_param *param)
5566 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
5567 static bool printed;
5570 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
5571 "%s() is deprecated, use llapi_migrate_mdt() instead",
5576 return llapi_migrate_mdt(path, param);
5579 int llapi_find(char *path, struct find_param *param)
5581 return param_callback(path, cb_find_init, cb_common_fini, param);
5585 * Get MDT number that the file/directory inode referenced
5586 * by the open fd resides on.
5587 * Return 0 and mdtidx on success, or -ve errno.
5589 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
5591 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
5596 static int cb_get_mdt_index(char *path, int p, int *dp, void *data,
5597 struct dirent64 *de)
5599 struct find_param *param = (struct find_param *)data;
5600 int d = dp == NULL ? -1 : *dp;
5604 if (p == -1 && d == -1)
5608 ret = llapi_file_fget_mdtidx(d, &mdtidx);
5609 } else /* if (p != -1) */ {
5612 fd = open(path, O_RDONLY | O_NOCTTY);
5614 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5622 if (ret == -ENODATA) {
5623 if (!param->fp_obd_uuid)
5624 llapi_printf(LLAPI_MSG_NORMAL,
5625 "'%s' has no stripe info\n", path);
5627 } else if (ret == -ENOENT) {
5628 llapi_error(LLAPI_MSG_WARN, ret,
5629 "warning: %s: '%s' does not exist",
5632 } else if (ret == -ENOTTY) {
5633 llapi_error(LLAPI_MSG_ERROR, ret,
5634 "%s: '%s' not on a Lustre fs",
5637 llapi_error(LLAPI_MSG_ERROR, ret,
5638 "error: %s: '%s' failed get_mdtidx",
5644 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5645 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5647 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5651 /* Do not go down anymore? */
5652 if (param->fp_depth == param->fp_max_depth)
5660 static int cb_getstripe(char *path, int p, int *dp, void *data,
5661 struct dirent64 *de)
5663 struct find_param *param = (struct find_param *)data;
5664 int d = dp == NULL ? -1 : *dp;
5667 if (p == -1 && d == -1)
5670 if (param->fp_obd_uuid) {
5671 param->fp_quiet = 1;
5672 ret = setup_obd_uuid(d != -1 ? d : p, path, param);
5677 if (d != -1 && (param->fp_get_lmv || param->fp_get_default_lmv))
5678 ret = cb_get_dirstripe(path, &d, param);
5680 (p != -1 && !param->fp_get_lmv && !param->fp_get_default_lmv))
5681 ret = get_lmd_info_fd(path, p, d, ¶m->fp_lmd->lmd_lmm,
5682 param->fp_lum_size, GET_LMD_STRIPE);
5683 else if (d == -1 && (param->fp_get_lmv || param->fp_get_default_lmv)) {
5684 /* in case of a dangling or valid faked symlink dir, opendir()
5685 * should have return either EINVAL or ENOENT, so let's try
5686 * to get LMV just in case, and by opening it as a file but
5687 * with O_NOFOLLOW ...
5689 int fd = open(path, O_RDONLY | O_NOFOLLOW);
5693 ret = cb_get_dirstripe(path, &fd, param);
5695 llapi_lov_dump_user_lmm(param, path, LDF_IS_DIR);
5702 if (errno == ENODATA && d != -1) {
5704 * We need to "fake" the "use the default" values
5705 * since the lmm struct is zeroed out at this point.
5706 * The magic needs to be set in order to satisfy
5707 * a check later on in the code path.
5708 * The object_seq needs to be set for the "(Default)"
5709 * prefix to be displayed.
5711 if (param->fp_get_default_lmv) {
5712 struct lmv_user_md *lum = param->fp_lmv_md;
5714 lum->lum_magic = LMV_USER_MAGIC;
5715 lum->lum_stripe_count = 0;
5716 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
5718 } else if (param->fp_get_lmv) {
5719 struct lmv_user_md *lum = param->fp_lmv_md;
5722 ret = llapi_file_fget_mdtidx(d, &mdtidx);
5725 lum->lum_magic = LMV_MAGIC_V1;
5726 lum->lum_stripe_count = 0;
5727 lum->lum_stripe_offset = mdtidx;
5730 struct lov_user_md *lmm =
5731 ¶m->fp_lmd->lmd_lmm;
5733 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5735 ostid_set_seq(&lmm->lmm_oi,
5736 FID_SEQ_LOV_DEFAULT);
5737 lmm->lmm_stripe_count = 0;
5738 lmm->lmm_stripe_size = 0;
5739 lmm->lmm_stripe_offset = -1;
5742 } else if (errno == ENODATA && p != -1) {
5743 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5744 llapi_printf(LLAPI_MSG_NORMAL,
5745 "%s has no stripe info\n", path);
5747 } else if (errno == ENOENT) {
5748 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5749 "warning: %s: %s does not exist",
5752 } else if (errno == ENOTTY) {
5754 llapi_error(LLAPI_MSG_ERROR, ret,
5755 "%s: '%s' not on a Lustre fs?",
5760 llapi_error(LLAPI_MSG_ERROR, ret,
5761 "error: %s: %s failed for %s",
5763 "LL_IOC_LOV_GETSTRIPE" :
5764 "IOC_MDC_GETFILESTRIPE", path);
5771 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5772 llapi_lov_dump_user_lmm(param, path, d != -1 ? LDF_IS_DIR : 0);
5775 /* Do not get down anymore? */
5776 if (param->fp_depth == param->fp_max_depth)
5784 int llapi_getstripe(char *path, struct find_param *param)
5786 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5787 cb_get_mdt_index : cb_getstripe,
5788 cb_common_fini, param);
5791 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5792 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5794 char raw[MAX_IOC_BUFLEN] = {'\0'};
5796 struct obd_ioctl_data data = { 0 };
5799 data.ioc_inlbuf1 = (char *)&type;
5800 data.ioc_inllen1 = sizeof(__u32);
5801 data.ioc_inlbuf2 = (char *)&index;
5802 data.ioc_inllen2 = sizeof(__u32);
5803 data.ioc_pbuf1 = (char *)stat_buf;
5804 data.ioc_plen1 = sizeof(struct obd_statfs);
5805 data.ioc_pbuf2 = (char *)uuid_buf;
5806 data.ioc_plen2 = sizeof(struct obd_uuid);
5808 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5810 llapi_error(LLAPI_MSG_ERROR, rc,
5811 "%s: error packing ioctl data", __func__);
5815 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5817 return rc < 0 ? -errno : 0;
5820 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5821 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5826 fd = open(path, O_RDONLY);
5829 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5832 * If we can't even open a file on the filesystem (e.g. with
5833 * -ESHUTDOWN), force caller to exit or it will loop forever.
5838 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5845 #define MAX_STRING_SIZE 128
5847 int llapi_ping(char *obd_type, char *obd_name)
5849 int flags = O_RDONLY;
5850 char buf[1] = { 0 };
5854 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5855 obd_type, obd_name);
5859 fd = open(path.gl_pathv[0], flags);
5861 if (errno == EACCES && flags == O_RDONLY) {
5866 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5871 if (flags == O_RDONLY)
5872 rc = read(fd, buf, sizeof(buf));
5874 rc = write(fd, buf, sizeof(buf));
5882 cfs_free_param_data(&path);
5886 int llapi_target_iterate(int type_num, char **obd_type,
5887 void *args, llapi_cb_t cb)
5893 for (i = 0; i < type_num; i++) {
5896 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5900 for (j = 0; j < param.gl_pathc; j++) {
5901 char obd_uuid[UUID_MAX + 1];
5905 fp = fopen(param.gl_pathv[j], "r");
5908 llapi_error(LLAPI_MSG_ERROR, rc,
5909 "error: opening '%s'",
5914 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5916 llapi_error(LLAPI_MSG_ERROR, rc,
5917 "error: reading '%s'",
5922 /* Extract the obd_name from the sysfs path.
5923 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5925 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5931 /* skip /fs/lustre/'obd_type'/ */
5932 obd_name += strlen(obd_type[i]) + 12;
5933 /* chop off after obd_name */
5934 ptr = strrchr(obd_name, '/');
5938 cb(obd_type[i], obd_name, obd_uuid, args);
5947 cfs_free_param_data(¶m);
5951 static void do_target_check(char *obd_type_name, char *obd_name,
5952 char *obd_uuid, void *args)
5956 rc = llapi_ping(obd_type_name, obd_name);
5958 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5960 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5962 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5965 int llapi_target_check(int type_num, char **obd_type, char *dir)
5967 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5970 #undef MAX_STRING_SIZE
5972 /* Is this a lustre fs? */
5973 int llapi_is_lustre_mnttype(const char *type)
5975 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
5978 /* Is this a lustre client fs? */
5979 int llapi_is_lustre_mnt(struct mntent *mnt)
5981 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5982 strstr(mnt->mnt_fsname, ":/") != NULL);
5985 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5987 char fsname[PATH_MAX + 1];
5991 rc = llapi_search_fsname(mnt, fsname);
5995 root = open(mnt, O_RDONLY | O_DIRECTORY);
5998 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
6002 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
6005 if (rc == -ENOENT && LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd))
6006 llapi_error(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, rc,
6007 "Cannot find pool '%s'", qctl->qc_poolname);
6013 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
6018 root = open(mnt, O_RDONLY | O_DIRECTORY);
6021 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
6025 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
6028 llapi_error(LLAPI_MSG_ERROR, rc,
6029 "ioctl on %s for getting connect flags failed", mnt);
6036 * Flush cached pages from all clients.
6038 * \param fd File descriptor
6042 int llapi_file_flush(int fd)
6046 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);