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_is_encrypted(int fd)
784 rc = ioctl(fd, FS_IOC_GETFLAGS, &flags);
788 return !!(flags & LUSTRE_ENCRYPT_FL);
791 int llapi_file_open_pool(const char *name, int flags, int mode,
792 unsigned long long stripe_size, int stripe_offset,
793 int stripe_count, int stripe_pattern, char *pool_name)
795 const struct llapi_stripe_param param = {
796 .lsp_stripe_size = stripe_size,
797 .lsp_stripe_count = stripe_count,
798 .lsp_stripe_pattern = stripe_pattern,
799 .lsp_stripe_offset = stripe_offset,
800 .lsp_pool = pool_name
802 return llapi_file_open_param(name, flags, mode, ¶m);
805 int llapi_file_open(const char *name, int flags, int mode,
806 unsigned long long stripe_size, int stripe_offset,
807 int stripe_count, int stripe_pattern)
809 return llapi_file_open_pool(name, flags, mode, stripe_size,
810 stripe_offset, stripe_count,
811 stripe_pattern, NULL);
814 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
815 __u32 flags, char *foreign_lov)
818 struct lov_foreign_md *lfm;
821 if (foreign_lov == NULL) {
823 llapi_error(LLAPI_MSG_ERROR, rc,
824 "foreign LOV EA content must be provided");
828 len = strlen(foreign_lov);
829 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
832 llapi_error(LLAPI_MSG_ERROR, rc,
833 "foreign LOV EA size %zu (must be 0 < len < %zu)",
834 len, XATTR_SIZE_MAX -
835 offsetof(struct lov_foreign_md, lfm_value));
839 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
842 llapi_error(LLAPI_MSG_ERROR, rc,
843 "failed to allocate lov_foreign_md");
847 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
850 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
854 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
855 lfm->lfm_length = len;
856 lfm->lfm_type = type;
857 lfm->lfm_flags = flags;
858 memcpy(lfm->lfm_value, foreign_lov, len);
860 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
861 char *errmsg = "stripe already set";
865 errmsg = "not on a Lustre filesystem";
866 else if (errno == EEXIST || errno == EALREADY)
867 errmsg = "stripe already set";
869 errmsg = strerror(errno);
871 llapi_err_noerrno(LLAPI_MSG_ERROR,
872 "setstripe error for '%s': %s", name, errmsg);
888 int llapi_file_create(const char *name, unsigned long long stripe_size,
889 int stripe_offset, int stripe_count, int stripe_pattern)
893 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
894 stripe_offset, stripe_count, stripe_pattern,
903 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
904 int stripe_offset, int stripe_count,
905 int stripe_pattern, char *pool_name)
909 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
910 stripe_offset, stripe_count, stripe_pattern,
919 static int verify_dir_param(const char *name,
920 const struct llapi_stripe_param *param)
922 char fsname[MAX_OBD_NAME + 1] = { 0 };
923 char *pool_name = param->lsp_pool;
926 /* Make sure we are on a Lustre file system */
927 rc = llapi_search_fsname(name, fsname);
929 llapi_error(LLAPI_MSG_ERROR, rc,
930 "'%s' is not on a Lustre filesystem",
935 /* Check if the stripe pattern is sane. */
936 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
937 param->lsp_stripe_count,
938 param->lsp_stripe_pattern);
942 /* Make sure we have a good pool */
943 if (pool_name != NULL) {
945 * in case user gives the full pool name <fsname>.<poolname>,
948 char *ptr = strchr(pool_name, '.');
952 if (strcmp(pool_name, fsname) != 0) {
954 llapi_err_noerrno(LLAPI_MSG_ERROR,
955 "Pool '%s' is not on filesystem '%s'",
962 /* Make sure the pool exists and is non-empty */
963 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
965 char *err = rc == 0 ? "has no OSTs" : "does not exist";
967 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
968 fsname, pool_name, err);
973 /* sanity check of target list */
974 if (param->lsp_is_specific) {
975 char mdtname[MAX_OBD_NAME + 64];
979 for (i = 0; i < param->lsp_stripe_count; i++) {
980 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
981 fsname, param->lsp_tgts[i]);
982 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
987 llapi_error(LLAPI_MSG_ERROR, rc,
988 "%s: cannot find MDT %s in %s",
995 /* Make sure stripe offset is in MDT list. */
996 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
1000 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1001 "%s: stripe offset '%d' is not in the target list",
1002 __func__, param->lsp_stripe_offset);
1010 static inline void param2lmu(struct lmv_user_md *lmu,
1011 const struct llapi_stripe_param *param)
1013 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
1015 lmu->lum_stripe_count = param->lsp_stripe_count;
1016 lmu->lum_stripe_offset = param->lsp_stripe_offset;
1017 lmu->lum_hash_type = param->lsp_stripe_pattern;
1018 lmu->lum_max_inherit = param->lsp_max_inherit;
1019 lmu->lum_max_inherit_rr = param->lsp_max_inherit_rr;
1020 if (param->lsp_pool != NULL)
1021 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
1022 if (param->lsp_is_specific) {
1025 for (i = 0; i < param->lsp_stripe_count; i++)
1026 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
1030 int llapi_dir_set_default_lmv(const char *name,
1031 const struct llapi_stripe_param *param)
1033 struct lmv_user_md lmu = { 0 };
1037 rc = verify_dir_param(name, param);
1041 /* TODO: default lmv doesn't support specific targets yet */
1042 if (param->lsp_is_specific)
1045 param2lmu(&lmu, param);
1047 fd = open(name, O_DIRECTORY | O_RDONLY);
1050 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1054 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
1056 char *errmsg = "stripe already set";
1059 if (errno != EEXIST && errno != EALREADY)
1060 errmsg = strerror(errno);
1062 llapi_err_noerrno(LLAPI_MSG_ERROR,
1063 "default dirstripe error on '%s': %s",
1070 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
1071 int stripe_count, int stripe_pattern,
1072 const char *pool_name)
1074 const struct llapi_stripe_param param = {
1075 .lsp_stripe_count = stripe_count,
1076 .lsp_stripe_offset = stripe_offset,
1077 .lsp_stripe_pattern = stripe_pattern,
1078 .lsp_pool = (char *)pool_name
1081 return llapi_dir_set_default_lmv(name, ¶m);
1085 * Create a Lustre directory.
1087 * \param name the name of the directory to be created
1088 * \param mode permission of the file if it is created, see mode in open(2)
1089 * \param param stripe pattern of the newly created directory
1091 * \retval 0 on success
1092 * \retval negative errno on failure
1094 int llapi_dir_create(const char *name, mode_t mode,
1095 const struct llapi_stripe_param *param)
1097 struct lmv_user_md *lmu = NULL;
1099 struct obd_ioctl_data data = { 0 };
1102 char *dirpath = NULL;
1103 char *namepath = NULL;
1108 rc = verify_dir_param(name, param);
1112 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1113 param->lsp_is_specific ?
1114 LMV_USER_MAGIC_SPECIFIC :
1117 lmu = calloc(1, lmu_size);
1121 dirpath = strdup(name);
1127 namepath = strdup(name);
1134 param2lmu(lmu, param);
1136 filename = basename(namepath);
1137 dir = dirname(dirpath);
1139 data.ioc_inlbuf1 = (char *)filename;
1140 data.ioc_inllen1 = strlen(filename) + 1;
1141 data.ioc_inlbuf2 = (char *)lmu;
1142 data.ioc_inllen2 = lmu_size;
1143 data.ioc_type = mode;
1144 if (param->lsp_is_create)
1145 /* borrow obdo1.o_flags to store this flag */
1146 data.ioc_obdo1.o_flags = OBD_FL_OBDMDEXISTS;
1147 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1149 llapi_error(LLAPI_MSG_ERROR, rc,
1150 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1155 fd = open(dir, O_DIRECTORY | O_RDONLY);
1158 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1162 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1163 char *errmsg = "stripe already set";
1166 if (errno != EEXIST && errno != EALREADY)
1167 errmsg = strerror(errno);
1169 llapi_err_noerrno(LLAPI_MSG_ERROR,
1170 "dirstripe error on '%s': %s", name, errmsg);
1181 * Create a foreign directory.
1183 * \param name the name of the directory to be created
1184 * \param mode permission of the file if it is created, see mode in open(2)
1185 * \param type foreign type to be set in LMV EA
1186 * \param flags foreign flags to be set in LMV EA
1187 * \param value foreign pattern to be set in LMV EA
1189 * \retval 0 on success
1190 * \retval negative errno on failure
1192 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1193 __u32 flags, const char *value)
1195 struct lmv_foreign_md *lfm = NULL;
1196 size_t lfm_size, len;
1197 struct obd_ioctl_data data = { 0 };
1200 char *dirpath = NULL;
1201 char *namepath = NULL;
1206 len = strlen(value);
1207 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1210 llapi_error(LLAPI_MSG_ERROR, rc,
1211 "invalid LOV EA length %zu (must be 0 < len < %zu)",
1212 len, XATTR_SIZE_MAX -
1213 offsetof(struct lmv_foreign_md, lfm_value));
1216 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1217 lfm = calloc(1, lfm_size);
1221 dirpath = strdup(name);
1227 namepath = strdup(name);
1234 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1235 lfm->lfm_length = len;
1236 lfm->lfm_type = type;
1237 lfm->lfm_flags = flags;
1238 memcpy(lfm->lfm_value, value, len);
1240 filename = basename(namepath);
1241 dir = dirname(dirpath);
1243 data.ioc_inlbuf1 = (char *)filename;
1244 data.ioc_inllen1 = strlen(filename) + 1;
1245 data.ioc_inlbuf2 = (char *)lfm;
1246 data.ioc_inllen2 = lfm_size;
1247 data.ioc_type = mode;
1248 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1250 llapi_error(LLAPI_MSG_ERROR, rc,
1251 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1256 fd = open(dir, O_DIRECTORY | O_RDONLY);
1259 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1263 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1264 char *errmsg = "stripe already set";
1267 if (errno != EEXIST && errno != EALREADY)
1268 errmsg = strerror(errno);
1270 llapi_err_noerrno(LLAPI_MSG_ERROR,
1271 "dirstripe error on '%s': %s", name, errmsg);
1281 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1282 int stripe_count, int stripe_pattern,
1283 const char *pool_name)
1285 const struct llapi_stripe_param param = {
1286 .lsp_stripe_count = stripe_count,
1287 .lsp_stripe_offset = stripe_offset,
1288 .lsp_stripe_pattern = stripe_pattern,
1289 .lsp_pool = (char *)pool_name
1292 return llapi_dir_create(name, mode, ¶m);
1296 * Find the fsname, the full path, and/or an open fd.
1297 * Either the fsname or path must not be NULL
1299 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1302 char buf[PATH_MAX], mntdir[PATH_MAX];
1303 char *ptr, *ptr_end;
1305 int idx = 0, mntlen = 0, fd;
1307 int fsnamelen, mountlen;
1309 /* get the mount point */
1310 fp = setmntent(PROC_MOUNTS, "r");
1313 llapi_error(LLAPI_MSG_ERROR, rc,
1314 "cannot retrieve filesystem mount point");
1318 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1321 if (!llapi_is_lustre_mnt(&mnt))
1324 if ((want & WANT_INDEX) && (idx++ != index))
1327 mntlen = strlen(mnt.mnt_dir);
1328 ptr = strchr(mnt.mnt_fsname, '/');
1329 while (ptr && *ptr == '/')
1332 * thanks to the call to llapi_is_lustre_mnt() above,
1333 * we are sure that mnt.mnt_fsname contains ":/",
1334 * so ptr should never be NULL
1339 while (*ptr_end != '/' && *ptr_end != '\0')
1342 /* Check the fsname for a match, if given */
1343 mountlen = ptr_end - ptr;
1344 if (!(want & WANT_FSNAME) && fsname != NULL &&
1345 (fsnamelen = strlen(fsname)) > 0 &&
1346 (fsnamelen != mountlen ||
1347 (strncmp(ptr, fsname, mountlen) != 0)))
1350 /* If the path isn't set return the first one we find */
1351 if (path == NULL || strlen(path) == 0) {
1352 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1353 mntdir[sizeof(mntdir) - 1] = '\0';
1354 if ((want & WANT_FSNAME) && fsname != NULL) {
1355 strncpy(fsname, ptr, mountlen);
1356 fsname[mountlen] = '\0';
1360 /* Otherwise find the longest matching path */
1361 } else if ((strlen(path) >= mntlen) &&
1362 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1363 /* check the path format */
1364 if (strlen(path) > mntlen && path[mntlen] != '/')
1366 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1367 mntdir[sizeof(mntdir) - 1] = '\0';
1368 if ((want & WANT_FSNAME) && fsname != NULL) {
1369 strncpy(fsname, ptr, mountlen);
1370 fsname[mountlen] = '\0';
1380 if ((want & WANT_PATH) && path != NULL) {
1381 strncpy(path, mntdir, mntlen);
1382 path[mntlen] = '\0';
1384 if (want & WANT_FD) {
1385 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1388 llapi_error(LLAPI_MSG_ERROR, rc,
1389 "cannot open '%s'", mntdir);
1395 } else if (want & WANT_ERROR)
1396 llapi_err_noerrno(LLAPI_MSG_ERROR,
1397 "'%s' not on a mounted Lustre filesystem",
1398 (want & WANT_PATH) ? fsname : path);
1403 * search lustre mounts
1405 * Calling this function will return to the user the mount point, mntdir, and
1406 * the file system name, fsname, if the user passed a buffer to this routine.
1408 * The user inputs are pathname and index. If the pathname is supplied then
1409 * the value of the index will be ignored. The pathname will return data if
1410 * the pathname is located on a lustre mount. Index is used to pick which
1411 * mount point you want in the case of multiple mounted lustre file systems.
1412 * See function lfs_osts in lfs.c for an example of the index use.
1414 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1417 int want = WANT_PATH, idx = -1;
1419 if (!pathname || pathname[0] == '\0') {
1423 strcpy(mntdir, pathname);
1427 want |= WANT_FSNAME;
1428 return get_root_path(want, fsname, NULL, mntdir, idx);
1431 /* Given a path, find the corresponding Lustre fsname */
1432 int llapi_search_fsname(const char *pathname, char *fsname)
1437 path = realpath(pathname, NULL);
1439 char tmp[PATH_MAX - 1];
1445 if (pathname[0] != '/') {
1447 * Need an absolute path, but realpath() only works for
1448 * pathnames that actually exist. We go through the
1449 * extra hurdle of dirname(getcwd() + pathname) in
1450 * case the relative pathname contains ".." in it.
1452 char realpath[PATH_MAX - 1];
1454 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1456 llapi_error(LLAPI_MSG_ERROR, rc,
1457 "cannot get current working directory");
1461 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1462 if (rc >= sizeof(tmp)) {
1464 llapi_error(LLAPI_MSG_ERROR, rc,
1465 "invalid parent path '%s'",
1471 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1472 if (rc >= sizeof(buf)) {
1474 llapi_error(LLAPI_MSG_ERROR, rc,
1475 "invalid path '%s'", pathname);
1478 path = realpath(buf, NULL);
1480 ptr = strrchr(buf, '/');
1482 llapi_error(LLAPI_MSG_ERROR |
1483 LLAPI_MSG_NO_ERRNO, 0,
1484 "cannot resolve path '%s'",
1489 path = realpath(buf, NULL);
1492 llapi_error(LLAPI_MSG_ERROR, rc,
1493 "cannot resolve path '%s'",
1499 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1504 int llapi_search_rootpath(char *pathname, const char *fsname)
1507 * pathname can be used as an argument by get_root_path(),
1508 * clear it for safety
1511 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1515 * Get the list of pool members.
1516 * \param poolname string of format \<fsname\>.\<poolname\>
1517 * \param members caller-allocated array of char*
1518 * \param list_size size of the members array
1519 * \param buffer caller-allocated buffer for storing OST names
1520 * \param buffer_size size of the buffer
1522 * \return number of members retrieved for this pool
1523 * \retval -error failure
1525 int llapi_get_poolmembers(const char *poolname, char **members,
1526 int list_size, char *buffer, int buffer_size)
1528 char fsname[PATH_MAX];
1537 /* name is FSNAME.POOLNAME */
1538 if (strlen(poolname) >= sizeof(fsname))
1541 snprintf(fsname, sizeof(fsname), "%s", poolname);
1542 pool = strchr(fsname, '.');
1549 rc = poolpath(&pathname, fsname, NULL);
1551 llapi_error(LLAPI_MSG_ERROR, rc,
1552 "Lustre filesystem '%s' not found",
1557 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1558 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1559 cfs_free_param_data(&pathname);
1560 if (rc >= sizeof(buf))
1562 fd = fopen(buf, "r");
1565 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1570 while (fgets(buf, sizeof(buf), fd) != NULL) {
1571 if (nb_entries >= list_size) {
1575 buf[sizeof(buf) - 1] = '\0';
1577 tmp = strchr(buf, '\n');
1580 if (used + strlen(buf) + 1 > buffer_size) {
1585 strcpy(buffer + used, buf);
1586 members[nb_entries] = buffer + used;
1587 used += strlen(buf) + 1;
1597 * Get the list of pools in a filesystem.
1598 * \param name filesystem name or path
1599 * \param poollist caller-allocated array of char*
1600 * \param list_size size of the poollist array
1601 * \param buffer caller-allocated buffer for storing pool names
1602 * \param buffer_size size of the buffer
1604 * \return number of pools retrieved for this filesystem
1605 * \retval -error failure
1607 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1608 char *buffer, int buffer_size)
1614 struct dirent *pool;
1616 unsigned int nb_entries = 0;
1617 unsigned int used = 0;
1620 /* initialize output array */
1621 for (i = 0; i < list_size; i++)
1624 /* is name a pathname ? */
1625 ptr = strchr(name, '/');
1627 char fsname_buf[MAXNAMLEN];
1629 /* We will need fsname for printing later */
1630 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1634 ptr = strrchr(fsname_buf, '-');
1638 fsname = strdup(fsname_buf);
1642 /* name is FSNAME */
1643 fsname = strdup(name);
1648 rc = poolpath(&pathname, fsname, NULL);
1650 llapi_error(LLAPI_MSG_ERROR, rc,
1651 "Lustre filesystem '%s' not found", name);
1655 dir = opendir(pathname.gl_pathv[0]);
1658 llapi_error(LLAPI_MSG_ERROR, rc,
1659 "Could not open pool list for '%s'",
1666 pool = readdir(dir);
1672 /* ignore . and .. */
1673 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1676 /* check output bounds */
1677 if (nb_entries >= list_size) {
1679 goto free_dir_no_msg;
1682 /* +2 for '.' and final '\0' */
1683 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1686 goto free_dir_no_msg;
1689 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1690 poollist[nb_entries] = buffer + used;
1691 used += strlen(pool->d_name) + strlen(fsname) + 2;
1697 llapi_error(LLAPI_MSG_ERROR, rc,
1698 "Error reading pool list for '%s'", name);
1700 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1705 cfs_free_param_data(&pathname);
1708 return rc != 0 ? rc : nb_entries;
1711 /* wrapper for lfs.c and obd.c */
1712 int llapi_poollist(const char *name)
1714 int poolcount, rc, i;
1717 rc = llapi_get_poolbuf(name, &buf, &pools, &poolcount);
1721 for (i = 0; i < poolcount; i++)
1722 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", pools[i]);
1729 * Get buffer that holds uuids and the list of pools in a filesystem.
1731 * \param name filesystem name or path
1732 * \param buf bufffer that has to be freed if function returns 0
1733 * \param pools pointer to the list of pools in buffer
1734 * \param poolcount number of pools
1736 * \return 0 when found at least 1 pool, i.e. poolcount > 0
1737 * \retval -error failure
1739 int llapi_get_poolbuf(const char *name, char **buf,
1740 char ***pools, int *poolcount)
1743 * list of pool names (assume that pool count is smaller
1746 char **list, *buffer = NULL, *fsname = (char *)name;
1747 char *poolname = NULL, *tmp = NULL, data[16];
1748 enum param_filter type = FILTER_BY_PATH;
1749 int obdcount, bufsize, rc, nb;
1754 if (name[0] != '/') {
1755 fsname = strdup(name);
1759 poolname = strchr(fsname, '.');
1762 type = FILTER_BY_FS_NAME;
1765 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1766 data, sizeof(data));
1769 obdcount = atoi(data);
1772 * Allocate space for each fsname-OST0000_UUID, 1 per OST,
1773 * and also an array to store the pointers for all that
1777 bufsize = sizeof(struct obd_uuid) * obdcount;
1778 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1779 if (buffer == NULL) {
1783 list = (char **) (buffer + bufsize);
1786 /* name is a path or fsname */
1787 nb = llapi_get_poollist(name, list, obdcount,
1790 /* name is a pool name (<fsname>.<poolname>) */
1791 nb = llapi_get_poolmembers(name, list, obdcount,
1795 if (nb == -EOVERFLOW) {
1798 goto retry_get_pools;
1801 rc = (nb < 0 ? nb : 0);
1808 /* Don't free buffer, it will be used later */
1811 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1816 typedef int (semantic_func_t)(char *path, int p, int *d,
1817 void *data, struct dirent64 *de);
1819 #define OBD_NOT_FOUND (-1)
1821 static bool lmv_is_foreign(__u32 magic)
1823 return magic == LMV_MAGIC_FOREIGN;
1826 static void find_param_fini(struct find_param *param)
1828 if (param->fp_migrate)
1831 if (param->fp_obd_indexes) {
1832 free(param->fp_obd_indexes);
1833 param->fp_obd_indexes = NULL;
1836 if (param->fp_lmd) {
1837 free(param->fp_lmd);
1838 param->fp_lmd = NULL;
1841 if (param->fp_lmv_md) {
1842 free(param->fp_lmv_md);
1843 param->fp_lmv_md = NULL;
1847 static int common_param_init(struct find_param *param, char *path)
1849 int lum_size = get_mds_md_size(path);
1854 /* migrate has fp_lmv_md initialized outside */
1855 if (param->fp_migrate)
1858 if (lum_size < PATH_MAX + 1)
1859 lum_size = PATH_MAX + 1;
1861 param->fp_lum_size = lum_size;
1862 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1864 if (param->fp_lmd == NULL) {
1865 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1866 "error: allocate %zu bytes for layout failed",
1867 sizeof(lstat_t) + param->fp_lum_size);
1871 param->fp_lmv_stripe_count = 256;
1872 param->fp_lmv_md = calloc(1,
1873 lmv_user_md_size(param->fp_lmv_stripe_count,
1874 LMV_USER_MAGIC_SPECIFIC));
1875 if (param->fp_lmv_md == NULL) {
1876 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1877 "error: allocation of %d bytes for ioctl",
1878 lmv_user_md_size(param->fp_lmv_stripe_count,
1879 LMV_USER_MAGIC_SPECIFIC));
1880 find_param_fini(param);
1884 param->fp_got_uuids = 0;
1885 param->fp_obd_indexes = NULL;
1886 param->fp_obd_index = OBD_NOT_FOUND;
1887 param->fp_mdt_index = OBD_NOT_FOUND;
1891 static int cb_common_fini(char *path, int p, int *dp, void *data,
1892 struct dirent64 *de)
1894 struct find_param *param = data;
1900 /* set errno upon failure */
1901 static int open_parent(const char *path)
1907 path_copy = strdup(path);
1908 if (path_copy == NULL)
1911 parent_path = dirname(path_copy);
1912 parent = open(parent_path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1918 static int cb_get_dirstripe(char *path, int *d, struct find_param *param)
1921 bool did_nofollow = false;
1926 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1927 if (param->fp_get_default_lmv)
1928 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1930 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1932 ret = ioctl(*d, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1934 /* if ENOTTY likely to be a fake symlink, so try again after
1935 * new open() with O_NOFOLLOW, but only once to prevent any
1936 * loop like for the path of a file/dir not on Lustre !!
1938 if (ret < 0 && errno == ENOTTY && !did_nofollow) {
1941 did_nofollow = true;
1942 fd = open(path, O_RDONLY | O_NOFOLLOW);
1944 /* restore original errno */
1949 /* close original fd and set new */
1952 ret2 = ioctl(fd, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1953 if (ret2 < 0 && errno != E2BIG) {
1954 /* restore original errno */
1958 /* LMV is ok or need to handle E2BIG case now */
1962 if (errno == E2BIG && ret != 0) {
1966 /* if foreign LMV case, fake stripes number */
1967 if (lmv_is_foreign(param->fp_lmv_md->lum_magic)) {
1968 struct lmv_foreign_md *lfm;
1970 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1971 if (lfm->lfm_length < XATTR_SIZE_MAX -
1972 offsetof(typeof(*lfm), lfm_value)) {
1973 uint32_t size = lfm->lfm_length +
1974 offsetof(typeof(*lfm), lfm_value);
1976 stripe_count = lmv_foreign_to_md_stripes(size);
1978 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1979 "error: invalid %d foreign size returned from ioctl",
1984 stripe_count = param->fp_lmv_md->lum_stripe_count;
1986 if (stripe_count <= param->fp_lmv_stripe_count)
1989 free(param->fp_lmv_md);
1990 param->fp_lmv_stripe_count = stripe_count;
1991 lmv_size = lmv_user_md_size(stripe_count,
1992 LMV_USER_MAGIC_SPECIFIC);
1993 param->fp_lmv_md = malloc(lmv_size);
1994 if (param->fp_lmv_md == NULL) {
1995 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1996 "error: allocation of %d bytes for ioctl",
1997 lmv_user_md_size(param->fp_lmv_stripe_count,
1998 LMV_USER_MAGIC_SPECIFIC));
2007 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
2010 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
2011 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
2012 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
2013 lmd_v2->lmd_stx.stx_uid = st->st_uid;
2014 lmd_v2->lmd_stx.stx_gid = st->st_gid;
2015 lmd_v2->lmd_stx.stx_mode = st->st_mode;
2016 lmd_v2->lmd_stx.stx_ino = st->st_ino;
2017 lmd_v2->lmd_stx.stx_size = st->st_size;
2018 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
2019 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
2020 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
2021 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
2022 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
2023 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
2024 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
2025 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
2026 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
2028 lmd_v2->lmd_flags = 0;
2030 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
2032 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
2033 if (lmd_v2->lmd_stx.stx_size)
2034 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
2035 if (lmd_v2->lmd_stx.stx_blocks)
2036 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
2038 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
2039 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
2040 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
2045 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
2047 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
2048 struct lov_user_mds_data *lmd_v2 = lmdbuf;
2052 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
2056 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
2059 st = lmd_v1->lmd_st;
2060 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
2061 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
2062 convert_lmd_statx(lmd_v2, &st, false);
2063 lmd_v2->lmd_lmmsize = 0;
2064 lmd_v2->lmd_padding = 0;
2069 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
2070 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
2072 struct lov_user_mds_data *lmd = lmdbuf;
2073 static bool use_old_ioctl;
2077 if (parent_fd < 0 && dir_fd < 0)
2079 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
2084 * LL_IOC_MDC_GETINFO operates on the current directory inode
2085 * and returns struct lov_user_mds_data, while
2086 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
2088 if (type == GET_LMD_INFO)
2089 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_V1 :
2090 LL_IOC_MDC_GETINFO_V2;
2092 cmd = LL_IOC_LOV_GETSTRIPE;
2095 ret = ioctl(dir_fd, cmd, lmdbuf);
2096 if (ret < 0 && errno == ENOTTY &&
2097 cmd == LL_IOC_MDC_GETINFO_V2) {
2098 cmd = LL_IOC_MDC_GETINFO_V1;
2099 use_old_ioctl = true;
2103 if (cmd == LL_IOC_MDC_GETINFO_V1 && !ret)
2104 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2106 if (ret < 0 && errno == ENOTTY && type == GET_LMD_STRIPE) {
2109 /* retry ioctl() after new open() with O_NOFOLLOW
2110 * just in case it could be a fake symlink
2111 * need using a new open() as dir_fd is being closed
2115 dir_fd2 = open(path, O_RDONLY | O_NDELAY | O_NOFOLLOW);
2117 /* return original error */
2120 ret = ioctl(dir_fd2, cmd, lmdbuf);
2121 /* pass new errno or success back to caller */
2127 } else if (parent_fd >= 0) {
2128 const char *fname = strrchr(path, '/');
2131 * IOC_MDC_GETFILEINFO takes as input the filename (relative to
2132 * the parent directory) and returns struct lov_user_mds_data,
2133 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2135 * This avoids opening, locking, and closing each file on the
2136 * client if that is not needed. Multiple of these ioctl() can
2137 * be done on the parent dir with a single open for all
2138 * files in that directory, and it also doesn't pollute the
2139 * client dcache with millions of dentries when traversing
2140 * a large filesystem.
2142 fname = (fname == NULL ? path : fname + 1);
2144 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2147 else if (ret >= lmdlen || ret++ == 0)
2150 if (type == GET_LMD_INFO)
2151 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_V1 :
2152 IOC_MDC_GETFILEINFO_V2;
2154 cmd = IOC_MDC_GETFILESTRIPE;
2157 ret = ioctl(parent_fd, cmd, lmdbuf);
2158 if (ret < 0 && errno == ENOTTY &&
2159 cmd == IOC_MDC_GETFILEINFO_V2) {
2160 cmd = IOC_MDC_GETFILEINFO_V1;
2161 use_old_ioctl = true;
2162 goto retry_getfileinfo;
2165 if (cmd == IOC_MDC_GETFILEINFO_V1 && !ret)
2166 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2170 if (ret && type == GET_LMD_INFO) {
2171 if (errno == ENOTTY) {
2175 * ioctl is not supported, it is not a lustre fs.
2176 * Do the regular lstat(2) instead.
2178 ret = lstat_f(path, &st);
2181 llapi_error(LLAPI_MSG_ERROR, ret,
2182 "error: %s: lstat failed for %s",
2186 convert_lmd_statx(lmd, &st, true);
2188 * It may be wrong to set use_old_ioctl with true as
2189 * the file is not a lustre fs. So reset it with false
2192 use_old_ioctl = false;
2193 } else if (errno == ENOENT) {
2195 llapi_error(LLAPI_MSG_WARN, ret,
2196 "warning: %s does not exist", path);
2197 } else if (errno != EISDIR && errno != ENODATA) {
2199 llapi_error(LLAPI_MSG_ERROR, ret,
2200 "%s ioctl failed for %s.",
2201 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2202 "IOC_MDC_GETFILEINFO", path);
2209 static int llapi_semantic_traverse(char *path, int size, int parent,
2210 semantic_func_t sem_init,
2211 semantic_func_t sem_fini, void *data,
2212 struct dirent64 *de)
2214 struct find_param *param = (struct find_param *)data;
2215 struct dirent64 *dent;
2216 int len, ret, d, p = -1;
2222 d = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
2223 /* if an invalid fake dir symlink, opendir() will return EINVAL
2224 * instead of ENOTDIR. If a valid but dangling faked or real file/dir
2225 * symlink ENOENT will be returned. For a valid/resolved fake or real
2226 * file symlink ENOTDIR will be returned as for a regular file.
2227 * opendir() will be successful for a valid and resolved fake or real
2228 * dir simlink or a regular dir.
2230 if (d == -1 && errno != ENOTDIR && errno != EINVAL && errno != ENOENT) {
2232 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2235 } else if (d == -1) {
2236 if (errno == ENOENT || errno == EINVAL) {
2237 int old_errno = errno;
2239 /* try to open with O_NOFOLLOW this will help
2240 * differentiate fake vs real symlinks
2241 * it is ok to not use O_DIRECTORY with O_RDONLY
2242 * and it will prevent the need to deal with ENOTDIR
2243 * error, instead of ELOOP, being returned by recent
2244 * kernels for real symlinks
2246 d = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2247 /* if a dangling real symlink should return ELOOP, or
2248 * again ENOENT if really non-existing path, or E...??
2249 * So return original error. If success or ENOTDIR, path
2250 * is likely to be a fake dir/file symlink, so continue
2260 if (parent == -1 && d == -1) {
2261 /* Open the parent dir. */
2262 p = open_parent(path);
2268 } else { /* d != -1 */
2271 /* try to reopen dir with O_NOFOLLOW just in case of a foreign
2274 d2 = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2279 /* continue with d */
2285 ret = sem_init(path, (parent != -1) ? parent : p, &d, data, de);
2295 /* ENOTDIR if fake symlink, do not consider it as an error */
2296 if (errno != ENOTDIR)
2297 llapi_error(LLAPI_MSG_ERROR, errno,
2298 "fdopendir() failed");
2305 while ((dent = readdir64(dir)) != NULL) {
2308 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2312 if ((len + dent->d_reclen + 2) > size) {
2313 llapi_err_noerrno(LLAPI_MSG_ERROR,
2314 "error: %s: string buffer too small for %s",
2319 strcat(path, dent->d_name);
2321 if (dent->d_type == DT_UNKNOWN) {
2322 struct lov_user_mds_data *lmd = param->fp_lmd;
2324 rc = get_lmd_info_fd(path, d, -1, param->fp_lmd,
2325 param->fp_lum_size, GET_LMD_INFO);
2327 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2334 switch (dent->d_type) {
2336 llapi_err_noerrno(LLAPI_MSG_ERROR,
2337 "error: %s: '%s' is UNKNOWN type %d",
2338 __func__, dent->d_name, dent->d_type);
2341 rc = llapi_semantic_traverse(path, size, d, sem_init,
2342 sem_fini, data, dent);
2343 if (rc != 0 && ret == 0)
2349 rc = sem_init(path, d, NULL, data, dent);
2350 if (rc < 0 && ret == 0) {
2355 if (sem_fini && rc == 0)
2356 sem_fini(path, d, NULL, data, dent);
2364 sem_fini(path, parent, &d, data, de);
2377 static int param_callback(char *path, semantic_func_t sem_init,
2378 semantic_func_t sem_fini, struct find_param *param)
2380 int ret, len = strlen(path);
2383 if (len > PATH_MAX) {
2385 llapi_error(LLAPI_MSG_ERROR, ret,
2386 "Path name '%s' is too long", path);
2390 buf = (char *)malloc(2 * PATH_MAX);
2394 snprintf(buf, PATH_MAX + 1, "%s", path);
2395 ret = common_param_init(param, buf);
2399 param->fp_depth = 0;
2401 ret = llapi_semantic_traverse(buf, 2 * PATH_MAX, -1, sem_init,
2402 sem_fini, param, NULL);
2404 find_param_fini(param);
2406 return ret < 0 ? ret : 0;
2409 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2413 rc = ioctl(fd, OBD_IOC_GETDTNAME, lov_name);
2414 if (rc && errno == ENOTTY)
2415 rc = ioctl(fd, OBD_IOC_GETNAME_OLD, lov_name);
2418 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2424 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2428 rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2431 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2437 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2441 /* do not follow faked symlinks */
2442 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
2444 /* real symlink should have failed with ELOOP so retry without
2445 * O_NOFOLLOW just in case
2447 fd = open(path, O_RDONLY | O_NONBLOCK);
2450 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'",
2456 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2462 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2466 fd = open(path, O_RDONLY | O_NONBLOCK);
2469 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2473 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2485 * If uuidp is NULL, return the number of available obd uuids.
2486 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2487 * there are more OSTs than allocated to uuidp, then an error is returned with
2488 * the ost_count set to number of available obd uuids.
2490 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2491 int *ost_count, enum tgt_type type)
2493 char buf[PATH_MAX], format[32];
2494 int rc = 0, index = 0;
2495 struct obd_uuid name;
2499 /* Get the lov name */
2500 if (type == LOV_TYPE)
2501 rc = llapi_file_fget_lov_uuid(fd, &name);
2503 rc = llapi_file_fget_lmv_uuid(fd, &name);
2507 /* Now get the ost uuids */
2508 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2509 FILTER_BY_EXACT, "target_obd", ¶m);
2513 fp = fopen(param.gl_pathv[0], "r");
2516 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2521 snprintf(format, sizeof(format),
2522 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2523 while (fgets(buf, sizeof(buf), fp) != NULL) {
2524 if (uuidp && (index < *ost_count)) {
2525 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2533 if (uuidp && (index > *ost_count))
2538 cfs_free_param_data(¶m);
2542 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2544 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2547 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2552 root = open(mnt, O_RDONLY | O_DIRECTORY);
2555 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2560 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2569 * Check if user specified value matches a real uuid. Ignore _UUID,
2570 * -osc-4ba41334, other trailing gunk in comparison.
2571 * @param real_uuid ends in "_UUID"
2572 * @param search_uuid may or may not end in "_UUID"
2574 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2576 int cmplen = strlen(real_uuid);
2577 int searchlen = strlen(search_uuid);
2579 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2581 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2585 * The UUIDs may legitimately be different lengths, if
2586 * the system was upgraded from an older version.
2588 if (cmplen != searchlen)
2591 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2595 * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2596 * returned in param->fp_obd_index
2598 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2600 struct obd_uuid obd_uuid;
2607 if (param->fp_got_uuids)
2610 /* Get the lov/lmv name */
2611 if (param->fp_get_lmv)
2612 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2614 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2616 if (rc != -ENOTTY) {
2617 llapi_error(LLAPI_MSG_ERROR, rc,
2618 "error: can't get %s name: %s",
2619 param->fp_get_lmv ? "lmv" : "lov",
2627 param->fp_got_uuids = 1;
2629 /* Now get the ost uuids */
2630 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2631 obd_uuid.uuid, FILTER_BY_EXACT,
2632 "target_obd", ¶m_data);
2636 fp = fopen(param_data.gl_pathv[0], "r");
2639 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2640 param_data.gl_pathv[0]);
2644 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2645 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2646 param->fp_get_lmv ? "MDTS" : "OBDS");
2648 snprintf(format, sizeof(format),
2649 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2650 while (fgets(buf, sizeof(buf), fp) != NULL) {
2653 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2656 if (param->fp_obd_uuid) {
2657 if (llapi_uuid_match(obd_uuid.uuid,
2658 param->fp_obd_uuid->uuid)) {
2659 param->fp_obd_index = index;
2662 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2663 /* Print everything */
2664 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2667 param->fp_obds_printed = 1;
2671 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2672 llapi_err_noerrno(LLAPI_MSG_ERROR,
2673 "error: %s: unknown obduuid: %s",
2674 __func__, param->fp_obd_uuid->uuid);
2678 cfs_free_param_data(¶m_data);
2683 * In this case, param->fp_obd_uuid will be an array of obduuids and
2684 * obd index for all these obduuids will be returned in
2685 * param->fp_obd_indexes
2687 static int setup_indexes(int d, char *path, struct obd_uuid *obduuids,
2688 int num_obds, int **obdindexes, int *obdindex,
2691 int ret, obdcount, obd_valid = 0, obdnum;
2693 struct obd_uuid *uuids = NULL;
2697 if (type == LOV_TYPE)
2698 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2700 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2704 obdcount = atoi(buf);
2705 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2710 ret = llapi_get_target_uuids(d, uuids, &obdcount, type);
2712 if (ret == -EOVERFLOW) {
2713 struct obd_uuid *uuids_temp;
2715 uuids_temp = realloc(uuids, obdcount *
2716 sizeof(struct obd_uuid));
2717 if (uuids_temp != NULL) {
2719 goto retry_get_uuids;
2724 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2728 indexes = malloc(num_obds * sizeof(*obdindex));
2729 if (indexes == NULL) {
2734 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2737 /* The user may have specified a simple index */
2738 i = strtol(obduuids[obdnum].uuid, &end, 0);
2739 if (end && *end == '\0' && i < obdcount) {
2740 indexes[obdnum] = i;
2743 for (i = 0; i < obdcount; i++) {
2744 if (llapi_uuid_match(uuids[i].uuid,
2745 obduuids[obdnum].uuid)) {
2746 indexes[obdnum] = i;
2752 if (i >= obdcount) {
2753 indexes[obdnum] = OBD_NOT_FOUND;
2754 llapi_err_noerrno(LLAPI_MSG_ERROR,
2755 "invalid obduuid '%s'",
2756 obduuids[obdnum].uuid);
2762 *obdindex = OBD_NOT_FOUND;
2764 *obdindex = obd_valid;
2766 *obdindexes = indexes;
2774 static int setup_target_indexes(int d, char *path, struct find_param *param)
2778 if (param->fp_mdt_uuid) {
2779 ret = setup_indexes(d, path, param->fp_mdt_uuid,
2781 ¶m->fp_mdt_indexes,
2782 ¶m->fp_mdt_index, LMV_TYPE);
2787 if (param->fp_obd_uuid) {
2788 ret = setup_indexes(d, path, param->fp_obd_uuid,
2790 ¶m->fp_obd_indexes,
2791 ¶m->fp_obd_index, LOV_TYPE);
2796 param->fp_got_uuids = 1;
2801 int llapi_ostlist(char *path, struct find_param *param)
2806 fd = open(path, O_RDONLY | O_DIRECTORY);
2810 ret = setup_obd_uuid(fd, path, param);
2817 * Tries to determine the default stripe attributes for a given filesystem. The
2818 * filesystem to check should be specified by fsname, or will be determined
2821 static int sattr_get_defaults(const char *const fsname,
2822 unsigned int *scount,
2823 unsigned int *ssize,
2824 unsigned int *soffset)
2830 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2831 "stripecount", val, sizeof(val));
2834 *scount = atoi(val);
2838 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2839 "stripesize", val, sizeof(val));
2846 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2847 "stripeoffset", val, sizeof(val));
2850 *soffset = atoi(val);
2857 * Tries to gather the default stripe attributes for a given filesystem. If
2858 * the attributes can be determined, they are cached for easy retreival the
2859 * next time they are needed. Only a single filesystem's attributes are
2862 int sattr_cache_get_defaults(const char *const fsname,
2863 const char *const pathname, unsigned int *scount,
2864 unsigned int *ssize, unsigned int *soffset)
2867 char fsname[PATH_MAX + 1];
2868 unsigned int stripecount;
2869 unsigned int stripesize;
2870 unsigned int stripeoffset;
2876 char fsname_buf[PATH_MAX + 1];
2877 unsigned int tmp[3];
2879 if (fsname == NULL) {
2880 rc = llapi_search_fsname(pathname, fsname_buf);
2884 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2887 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2889 * Ensure all 3 sattrs (count, size, and offset) are
2890 * successfully retrieved and stored in tmp before writing to
2893 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2897 cache.stripecount = tmp[0];
2898 cache.stripesize = tmp[1];
2899 cache.stripeoffset = tmp[2];
2900 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2904 *scount = cache.stripecount;
2906 *ssize = cache.stripesize;
2908 *soffset = cache.stripeoffset;
2913 static char *layout2name(__u32 layout_pattern)
2915 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2917 else if (layout_pattern == LOV_PATTERN_MDT)
2919 else if (layout_pattern == LOV_PATTERN_RAID0)
2921 else if (layout_pattern ==
2922 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2923 return "raid0,overstriped";
2928 enum lov_dump_flags {
2929 LDF_IS_DIR = 0x0001,
2930 LDF_IS_RAW = 0x0002,
2931 LDF_INDENT = 0x0004,
2932 LDF_SKIP_OBJS = 0x0008,
2934 LDF_EXTENSION = 0x0020,
2937 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2938 struct lov_user_ost_data_v1 *objects,
2939 enum llapi_layout_verbose verbose,
2940 int depth, char *pool_name,
2941 enum lov_dump_flags flags)
2943 bool is_dir = flags & LDF_IS_DIR;
2944 bool is_raw = flags & LDF_IS_RAW;
2945 bool indent = flags & LDF_INDENT;
2946 bool yaml = flags & LDF_YAML;
2947 bool skip_objs = flags & LDF_SKIP_OBJS;
2948 bool extension = flags & LDF_EXTENSION;
2949 char *prefix = is_dir ? "" : "lmm_";
2950 char *separator = "";
2951 char *space = indent ? " " : "";
2954 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2955 lmm_oi_set_seq(&lum->lmm_oi, 0);
2956 if (!indent && (verbose & VERBOSE_DETAIL))
2957 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2960 if (!yaml && !indent && depth && path &&
2961 ((verbose != VERBOSE_OBJID) || !is_dir))
2962 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2964 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2965 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2966 space, prefix, lum->lmm_magic);
2967 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2969 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2970 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2972 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2975 if (verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) {
2980 if (verbose & ~VERBOSE_DFID)
2981 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2985 struct lu_fid dir_fid;
2987 rc = llapi_path2fid(path, &dir_fid);
2989 llapi_error(LLAPI_MSG_ERROR, rc,
2990 "Cannot determine directory fid.");
2992 seq = dir_fid.f_seq;
2993 oid = dir_fid.f_oid;
2994 ver = dir_fid.f_ver;
2997 * This needs a bit of hand-holding since old 1.x
2998 * lmm_oi have { oi.oi_id = mds_inum, oi.oi_seq = 0 }
2999 * and 2.x lmm_oi have { oi.oi_id = mds_oid,
3000 * oi.oi_seq = mds_seq } instead of a real FID.
3001 * Ideally the 2.x code would have stored this like a
3002 * FID with { oi_id = mds_seq, oi_seq = mds_oid } so
3003 * the ostid union lu_fid { f_seq = mds_seq,
3004 * f_oid = mds_oid } worked properly (especially since
3005 * IGIF FIDs use mds_inum as the FID SEQ), but
3006 * unfortunately that didn't happen.
3008 * Print it to look like an IGIF FID, even though the
3009 * fields are reversed on disk, so that it makes sense
3012 * Don't use ostid_id() and ostid_seq(), since they
3013 * assume the oi_fid fields are in the right order.
3014 * This is why there are separate lmm_oi_seq() and
3015 * lmm_oi_id() routines for this.
3017 * For newer layout types hopefully this will be a
3020 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
3021 lmm_oi_id(&lum->lmm_oi) :
3022 lmm_oi_seq(&lum->lmm_oi);
3023 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
3024 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
3025 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
3029 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
3030 (unsigned long long)seq, oid, ver);
3032 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
3033 (unsigned long long)seq, oid, ver);
3036 if (verbose & VERBOSE_STRIPE_COUNT) {
3037 if (verbose & ~VERBOSE_STRIPE_COUNT)
3038 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
3041 if (!is_raw && lum->lmm_stripe_count == 0 &&
3042 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
3043 unsigned int scount;
3045 rc = sattr_cache_get_defaults(NULL, path,
3049 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3052 llapi_error(LLAPI_MSG_ERROR, rc,
3053 "Cannot determine default stripe count.");
3055 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3057 (__s16)lum->lmm_stripe_count);
3060 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
3062 (__s16)lum->lmm_stripe_count);
3064 if (!yaml && is_dir)
3070 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
3071 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
3072 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3073 if (verbose & ~VERBOSE_EXT_SIZE && extension)
3074 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
3076 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
3077 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
3079 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
3082 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
3085 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
3087 llapi_error(LLAPI_MSG_ERROR, rc,
3088 "Cannot determine default stripe size.");
3090 /* Extension size is in KiB */
3091 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3093 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
3094 (unsigned long long)lum->lmm_stripe_size);
3096 if (!yaml && is_dir)
3102 if ((verbose & VERBOSE_PATTERN)) {
3103 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3104 if (verbose & ~VERBOSE_PATTERN)
3105 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
3107 if (lov_pattern_supported(lum->lmm_pattern))
3108 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3109 layout2name(lum->lmm_pattern));
3111 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
3112 separator = (!yaml && is_dir) ? " " : "\n";
3115 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
3116 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3117 if (verbose & ~VERBOSE_GENERATION)
3118 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
3120 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3121 skip_objs ? 0 : (int)lum->lmm_layout_gen);
3125 if (verbose & VERBOSE_STRIPE_OFFSET) {
3126 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3127 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3128 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
3130 if (is_dir || skip_objs)
3131 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3132 lum->lmm_stripe_offset ==
3133 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
3134 lum->lmm_stripe_offset);
3135 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
3136 llapi_printf(LLAPI_MSG_NORMAL, "0");
3138 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3139 objects[0].l_ost_idx);
3140 if (!yaml && is_dir)
3146 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
3147 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3148 if (verbose & ~VERBOSE_POOL)
3149 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
3151 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
3152 if (!yaml && is_dir)
3158 if (strlen(separator) != 0)
3159 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3162 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
3163 struct lov_user_ost_data_v1 *objects,
3164 char *path, int obdindex, int depth,
3165 enum llapi_layout_verbose verbose,
3166 enum lov_dump_flags flags)
3168 bool is_dir = flags & LDF_IS_DIR;
3169 bool indent = flags & LDF_INDENT;
3170 bool skip_objs = flags & LDF_SKIP_OBJS;
3171 bool yaml = flags & LDF_YAML;
3172 bool obdstripe = obdindex == OBD_NOT_FOUND;
3175 if (!obdstripe && !skip_objs) {
3176 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
3177 if (obdindex == objects[i].l_ost_idx) {
3187 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
3190 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
3191 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
3192 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
3193 char *space = " - ";
3196 llapi_printf(LLAPI_MSG_NORMAL,
3197 "%6slmm_objects:\n", " ");
3199 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
3201 llapi_printf(LLAPI_MSG_NORMAL,
3202 "\tobdidx\t\t objid\t\t objid\t\t group\n");
3204 for (i = 0; i < lum->lmm_stripe_count; i++) {
3205 int idx = objects[i].l_ost_idx;
3206 long long oid = ostid_id(&objects[i].l_ost_oi);
3207 long long gr = ostid_seq(&objects[i].l_ost_oi);
3209 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
3213 struct lu_fid fid = { 0 };
3215 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3216 llapi_printf(LLAPI_MSG_NORMAL,
3217 "%sl_ost_idx: %d\n", space, idx);
3218 llapi_printf(LLAPI_MSG_NORMAL,
3219 "%8sl_fid: "DFID_NOBRACE"\n",
3221 } else if (indent) {
3222 struct lu_fid fid = { 0 };
3224 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3225 llapi_printf(LLAPI_MSG_NORMAL,
3226 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
3227 space, i, idx, PFID(&fid));
3231 sprintf(fmt, "%s%s%s\n",
3232 "\t%6u\t%14llu\t%#13llx\t",
3233 (fid_seq_is_rsvd(gr) ||
3234 fid_seq_is_mdt0(gr)) ?
3235 "%14llu" : "%#14llx", "%s");
3236 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
3238 obdindex == idx ? " *" : "");
3242 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3245 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3246 char *path, int obdindex, int depth,
3247 enum llapi_layout_verbose verbose,
3248 enum lov_dump_flags flags)
3250 struct lmv_user_mds_data *objects = lum->lum_objects;
3251 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3252 char *separator = "";
3253 bool yaml = flags & LDF_YAML;
3254 bool obdstripe = false;
3257 if (obdindex != OBD_NOT_FOUND) {
3258 if (lum->lum_stripe_count == 0) {
3259 if (obdindex == lum->lum_stripe_offset)
3262 for (i = 0; i < lum->lum_stripe_count; i++) {
3263 if (obdindex == objects[i].lum_mds) {
3264 llapi_printf(LLAPI_MSG_NORMAL,
3279 /* show all information default */
3281 if (lum->lum_magic == LMV_USER_MAGIC)
3282 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3283 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3285 verbose = VERBOSE_OBJID;
3288 if (depth && path && ((verbose != VERBOSE_OBJID)))
3289 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3291 if (verbose & VERBOSE_STRIPE_COUNT) {
3292 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3293 if (verbose & ~VERBOSE_STRIPE_COUNT)
3294 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3295 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3296 (int)lum->lum_stripe_count);
3297 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3303 if (verbose & VERBOSE_STRIPE_OFFSET) {
3304 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3305 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3306 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3307 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3308 (int)lum->lum_stripe_offset);
3309 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3315 if (verbose & VERBOSE_HASH_TYPE) {
3316 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3317 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3319 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3320 if (verbose & ~VERBOSE_HASH_TYPE)
3321 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3322 if (type < LMV_HASH_TYPE_MAX)
3323 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3324 mdt_hash_name[type]);
3326 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3328 if (flags & LMV_HASH_FLAG_MIGRATION)
3329 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3330 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3331 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3332 if (flags & LMV_HASH_FLAG_LOST_LMV)
3333 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3334 if (flags & LMV_HASH_FLAG_FIXED)
3335 llapi_printf(LLAPI_MSG_NORMAL, ",fixed");
3336 if (flags & ~LMV_HASH_FLAG_KNOWN)
3337 llapi_printf(LLAPI_MSG_NORMAL, ",unknown_%04x",
3338 flags & ~LMV_HASH_FLAG_KNOWN);
3340 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3346 if ((verbose & VERBOSE_INHERIT) && lum->lum_magic == LMV_USER_MAGIC) {
3347 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3348 if (verbose & ~VERBOSE_INHERIT)
3349 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit: ");
3350 if (lum->lum_max_inherit == LMV_INHERIT_UNLIMITED)
3351 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3352 else if (lum->lum_max_inherit == LMV_INHERIT_NONE)
3353 llapi_printf(LLAPI_MSG_NORMAL, "0");
3355 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3356 lum->lum_max_inherit);
3357 if (verbose & VERBOSE_INHERIT && !yaml)
3363 if ((verbose & VERBOSE_INHERIT_RR) &&
3364 lum->lum_magic == LMV_USER_MAGIC &&
3365 lum->lum_stripe_offset == LMV_OFFSET_DEFAULT) {
3366 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3367 if (verbose & ~VERBOSE_INHERIT_RR)
3368 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit_rr: ");
3369 if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_UNLIMITED)
3370 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3371 else if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_NONE)
3372 llapi_printf(LLAPI_MSG_NORMAL, "0");
3374 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3375 lum->lum_max_inherit_rr);
3376 if (verbose & VERBOSE_INHERIT_RR && !yaml)
3384 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3385 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3386 if (lum->lum_stripe_count > 0)
3387 llapi_printf(LLAPI_MSG_NORMAL,
3388 "mdtidx\t\t FID[seq:oid:ver]\n");
3389 for (i = 0; i < lum->lum_stripe_count; i++) {
3390 int idx = objects[i].lum_mds;
3391 struct lu_fid *fid = &objects[i].lum_fid;
3393 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3394 llapi_printf(LLAPI_MSG_NORMAL,
3395 "%6u\t\t "DFID"\t\t%s\n",
3397 obdindex == idx ? " *" : "");
3401 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3402 pool_name[0] != '\0') {
3403 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3404 if (verbose & ~VERBOSE_POOL)
3405 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3407 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3411 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3412 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3415 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3416 enum lov_dump_flags flags)
3418 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3419 int depth = param->fp_max_depth;
3420 enum llapi_layout_verbose verbose = param->fp_verbose;
3421 bool yaml = flags & LDF_YAML;
3423 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3424 !(flags & LDF_IS_DIR)) && !yaml)
3425 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3427 if (verbose & VERBOSE_DETAIL) {
3428 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3429 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3430 " ", comp_v1->lcm_magic);
3431 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3432 " ", comp_v1->lcm_size);
3433 if (flags & LDF_IS_DIR)
3434 llapi_printf(LLAPI_MSG_NORMAL,
3435 "%2slcm_flags: %s\n", " ",
3436 comp_v1->lcm_mirror_count > 0 ?
3439 llapi_printf(LLAPI_MSG_NORMAL,
3440 "%2slcm_flags: %s\n", " ",
3441 llapi_layout_flags_string(comp_v1->lcm_flags));
3444 if (verbose & VERBOSE_GENERATION) {
3445 if (verbose & ~VERBOSE_GENERATION)
3446 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3448 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3451 if (verbose & VERBOSE_MIRROR_COUNT) {
3452 if (verbose & ~VERBOSE_MIRROR_COUNT)
3453 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3455 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3456 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3457 comp_v1->lcm_mirror_count + 1 : 1);
3460 if (verbose & VERBOSE_COMP_COUNT) {
3461 if (verbose & ~VERBOSE_COMP_COUNT)
3462 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3464 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3465 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3466 comp_v1->lcm_entry_count : 0);
3469 if (verbose & VERBOSE_DETAIL && !yaml)
3470 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3473 static void lcme_flags2str(__u32 comp_flags)
3479 llapi_printf(LLAPI_MSG_NORMAL, "0");
3482 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3483 if (comp_flags & comp_flags_table[i].cfn_flag) {
3485 llapi_printf(LLAPI_MSG_NORMAL, ",");
3486 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3487 comp_flags_table[i].cfn_name);
3488 comp_flags &= ~comp_flags_table[i].cfn_flag;
3494 llapi_printf(LLAPI_MSG_NORMAL, ",");
3495 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3499 static void lov_dump_comp_v1_entry(struct find_param *param,
3500 enum lov_dump_flags flags, int index)
3502 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3503 struct lov_comp_md_entry_v1 *entry;
3504 char *separator = "";
3505 enum llapi_layout_verbose verbose = param->fp_verbose;
3506 bool yaml = flags & LDF_YAML;
3508 entry = &comp_v1->lcm_entries[index];
3511 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3513 if (verbose & VERBOSE_COMP_ID) {
3514 if (verbose & VERBOSE_DETAIL && !yaml)
3515 llapi_printf(LLAPI_MSG_NORMAL,
3516 "%slcme_id: ", " - ");
3517 else if (verbose & ~VERBOSE_COMP_ID)
3518 llapi_printf(LLAPI_MSG_NORMAL,
3519 "%4slcme_id: ", " ");
3520 if (entry->lcme_id != LCME_ID_INVAL)
3521 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3523 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3527 if (verbose & VERBOSE_MIRROR_ID) {
3528 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3529 if (verbose & ~VERBOSE_MIRROR_ID)
3530 llapi_printf(LLAPI_MSG_NORMAL,
3531 "%4slcme_mirror_id: ", " ");
3532 if (entry->lcme_id != LCME_ID_INVAL)
3533 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3534 mirror_id_of(entry->lcme_id));
3536 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3540 if (verbose & VERBOSE_COMP_FLAGS) {
3541 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3542 if (verbose & ~VERBOSE_COMP_FLAGS)
3543 llapi_printf(LLAPI_MSG_NORMAL,
3544 "%4slcme_flags: ", " ");
3545 lcme_flags2str(entry->lcme_flags);
3548 /* print snapshot timestamp if its a nosync comp */
3549 if ((verbose & VERBOSE_COMP_FLAGS) &&
3550 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3551 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3552 if (verbose & ~VERBOSE_COMP_FLAGS)
3553 llapi_printf(LLAPI_MSG_NORMAL,
3554 "%4slcme_timestamp: ", " ");
3556 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3557 (unsigned long long)entry->lcme_timestamp);
3559 time_t stamp = entry->lcme_timestamp;
3560 char *date_str = asctime(localtime(&stamp));
3562 date_str[strlen(date_str) - 1] = '\0';
3563 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3569 if (verbose & VERBOSE_COMP_START) {
3570 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3571 if (verbose & ~VERBOSE_COMP_START)
3572 llapi_printf(LLAPI_MSG_NORMAL,
3573 "%4slcme_extent.e_start: ", " ");
3574 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3575 (unsigned long long)entry->lcme_extent.e_start);
3579 if (verbose & VERBOSE_COMP_END) {
3580 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3581 if (verbose & ~VERBOSE_COMP_END)
3582 llapi_printf(LLAPI_MSG_NORMAL,
3583 "%4slcme_extent.e_end: ", " ");
3584 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3585 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3587 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3588 (unsigned long long)entry->lcme_extent.e_end);
3593 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3594 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3595 } else if (verbose & VERBOSE_DETAIL) {
3596 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3597 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3598 " ", entry->lcme_offset);
3599 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3600 " ", entry->lcme_size);
3601 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3603 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3608 * Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3609 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3611 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3612 * The table below gives the answers for the specified parameters (value and
3613 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3614 * --------------------------------------
3615 * 1 | file > limit; sign > 0 | -1 / -1 |
3616 * 2 | file = limit; sign > 0 | -1 / -1 |
3617 * 3 | file < limit; sign > 0 | ? / 1 |
3618 * 4 | file > limit; sign = 0 | -1 / -1 |
3619 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3620 * 6 | file < limit; sign = 0 | ? / -1 |
3621 * 7 | file > limit; sign < 0 | 1 / 1 |
3622 * 8 | file = limit; sign < 0 | ? / -1 |
3623 * 9 | file < limit; sign < 0 | ? / -1 |
3624 * --------------------------------------
3625 * Note: 5th actually means that the value is within the interval
3626 * (limit - margin, limit].
3628 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3629 int sign, int negopt, unsigned long long margin,
3635 /* Drop the fraction of margin (of days or size). */
3636 if (file + margin <= limit)
3638 } else if (sign == 0) {
3639 if (file <= limit && file + margin > limit)
3641 else if (file + margin <= limit)
3643 } else if (sign < 0) {
3650 return negopt ? ~ret + 1 : ret;
3653 static inline struct lov_user_md *
3654 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3656 return (struct lov_user_md *)((char *)comp_v1 +
3657 comp_v1->lcm_entries[ent_idx].lcme_offset);
3660 static inline struct lov_user_ost_data_v1 *
3661 lov_v1v3_objects(struct lov_user_md *v1)
3663 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3664 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3666 return v1->lmm_objects;
3670 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3672 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3673 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3674 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3676 pool_name[0] = '\0';
3680 print_last_init_comp(struct find_param *param)
3682 /* print all component info */
3683 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3686 /* print specific component info */
3687 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3688 param->fp_check_comp_start || param->fp_check_comp_end ||
3689 param->fp_check_mirror_id || param->fp_check_mirror_index)
3695 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3699 if (param->fp_comp_end == LUSTRE_EOF) {
3700 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3701 match = end == LUSTRE_EOF ? 1 : -1;
3702 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3703 match = end == LUSTRE_EOF ? -1 : 1;
3704 else /* at least EOF */
3706 if (param->fp_exclude_comp_end)
3709 unsigned long long margin;
3711 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3712 match = find_value_cmp(end, param->fp_comp_end,
3713 param->fp_comp_end_sign,
3714 param->fp_exclude_comp_end, margin, 0);
3721 * An example of "getstripe -v" for a two components PFL file:
3724 * lcm_magic: 0x0BD60BD0
3728 * lcm_entry_count: 2
3732 * lcme_extent.e_start: 0
3733 * lcme_extent.e_end: 1048576
3737 * lmm_magic: 0x0BD10BD0
3738 * lmm_seq: 0x200000401
3739 * lmm_object_id: 0x1
3740 * lmm_fid: [0x200000401:0x1:0x0]
3741 * lmm_stripe_count: 1
3742 * lmm_stripe_size: 1048576
3743 * lmm_pattern: raid0
3745 * lmm_stripe_offset: 0
3747 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3751 * lcme_extent.e_start: 1048576
3752 * lcme_extent.e_end: EOF
3756 * lmm_magic: 0x0BD10BD0
3757 * lmm_seq: 0x200000401
3758 * lmm_object_id: 0x1
3759 * lmm_fid: [0x200000401:0x1:0x0]
3760 * lmm_stripe_count: 2
3761 * lmm_stripe_size: 1048576
3762 * lmm_pattern: raid0
3764 * lmm_stripe_offset: 1
3766 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3767 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3769 static void lov_dump_comp_v1(struct find_param *param, char *path,
3770 enum lov_dump_flags flags)
3772 struct lov_comp_md_entry_v1 *entry;
3773 struct lov_user_ost_data_v1 *objects;
3774 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3775 struct lov_user_md_v1 *v1;
3776 char pool_name[LOV_MAXPOOLNAME + 1];
3777 int obdindex = param->fp_obd_index;
3778 int i, j, match, ext;
3779 bool obdstripe = false;
3780 __u16 mirror_index = 0;
3781 __u16 mirror_id = 0;
3783 if (obdindex != OBD_NOT_FOUND) {
3784 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3785 i < comp_v1->lcm_entry_count; i++) {
3786 if (!(comp_v1->lcm_entries[i].lcme_flags &
3790 v1 = lov_comp_entry(comp_v1, i);
3791 objects = lov_v1v3_objects(v1);
3793 for (j = 0; j < v1->lmm_stripe_count; j++) {
3794 if (obdindex == objects[j].l_ost_idx) {
3807 lov_dump_comp_v1_header(param, path, flags);
3809 flags |= LDF_INDENT;
3811 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3812 entry = &comp_v1->lcm_entries[i];
3814 if (param->fp_check_comp_flags) {
3815 if (((param->fp_comp_flags & entry->lcme_flags) !=
3816 param->fp_comp_flags) ||
3817 (param->fp_comp_neg_flags & entry->lcme_flags))
3821 if (param->fp_check_comp_id &&
3822 param->fp_comp_id != entry->lcme_id)
3825 if (param->fp_check_comp_start) {
3826 match = find_value_cmp(entry->lcme_extent.e_start,
3827 param->fp_comp_start,
3828 param->fp_comp_start_sign,
3830 param->fp_comp_start_units, 0);
3835 if (param->fp_check_comp_end) {
3836 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3842 if (param->fp_check_mirror_index) {
3843 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3845 mirror_id = mirror_id_of(entry->lcme_id);
3848 match = find_value_cmp(mirror_index,
3849 param->fp_mirror_index,
3850 param->fp_mirror_index_sign,
3851 param->fp_exclude_mirror_index,
3855 } else if (param->fp_check_mirror_id) {
3856 if (mirror_id != mirror_id_of(entry->lcme_id))
3857 mirror_id = mirror_id_of(entry->lcme_id);
3859 match = find_value_cmp(mirror_id,
3860 param->fp_mirror_id,
3861 param->fp_mirror_id_sign,
3862 param->fp_exclude_mirror_id,
3868 if (print_last_init_comp(param)) {
3870 * if part of stripe info is needed, we'd print only
3871 * the last instantiated component info.
3873 if (entry->lcme_flags & LCME_FL_INIT)
3876 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3877 if (entry->lcme_flags & LCME_FL_EXTENSION)
3878 /* moved back below */
3886 if (entry->lcme_flags & LCME_FL_INIT) {
3887 if (obdindex != OBD_NOT_FOUND) {
3888 flags |= LDF_SKIP_OBJS;
3889 v1 = lov_comp_entry(comp_v1, i);
3890 objects = lov_v1v3_objects(v1);
3892 for (j = 0; j < v1->lmm_stripe_count; j++) {
3893 if (obdindex == objects[j].l_ost_idx) {
3894 flags &= ~LDF_SKIP_OBJS;
3899 flags &= ~LDF_SKIP_OBJS;
3902 flags |= LDF_SKIP_OBJS;
3905 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3907 lov_dump_comp_v1_entry(param, flags, i);
3909 v1 = lov_comp_entry(comp_v1, i);
3910 objects = lov_v1v3_objects(v1);
3911 lov_v1v3_pool_name(v1, pool_name);
3913 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3914 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3915 param->fp_max_depth, param->fp_verbose,
3918 if (print_last_init_comp(param)) {
3920 * directory layout contains only layout template, print the
3924 i = comp_v1->lcm_entry_count - 1;
3927 flags &= ~LDF_SKIP_OBJS;
3929 lov_dump_comp_v1_entry(param, flags, i);
3931 v1 = lov_comp_entry(comp_v1, i);
3932 objects = lov_v1v3_objects(v1);
3933 lov_v1v3_pool_name(v1, pool_name);
3935 entry = &comp_v1->lcm_entries[i];
3936 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3937 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3938 param->fp_max_depth, param->fp_verbose,
3943 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3944 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3947 static inline bool has_any_comp_options(struct find_param *param)
3949 enum llapi_layout_verbose verbose = param->fp_verbose;
3951 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3952 param->fp_check_comp_start || param->fp_check_comp_end ||
3953 param->fp_check_comp_flags)
3956 /* show full layout information, not component specific */
3957 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3960 return verbose & VERBOSE_COMP_OPTS;
3963 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3966 struct lov_user_md *lum = &orig->lmd_lmm;
3967 struct lov_user_mds_data *new;
3968 struct lov_comp_md_v1 *comp_v1;
3969 struct lov_comp_md_entry_v1 *ent;
3970 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3971 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3974 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3976 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3980 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3981 + sizeof(new->lmd_lmmsize));
3983 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3984 comp_v1->lcm_magic = lum->lmm_magic;
3985 comp_v1->lcm_size = lum_off + lum_size;
3986 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3987 comp_v1->lcm_flags = 0;
3988 comp_v1->lcm_entry_count = 1;
3990 ent = &comp_v1->lcm_entries[0];
3992 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3993 ent->lcme_extent.e_start = 0;
3994 ent->lcme_extent.e_end = LUSTRE_EOF;
3995 ent->lcme_offset = lum_off;
3996 ent->lcme_size = lum_size;
3998 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
4003 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
4004 enum lov_dump_flags flags)
4006 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
4008 if (has_any_comp_options(param)) {
4009 struct lov_user_mds_data *new_lmd, *orig_lmd;
4011 orig_lmd = param->fp_lmd;
4012 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
4013 if (new_lmd != NULL) {
4014 param->fp_lmd = new_lmd;
4015 lov_dump_comp_v1(param, path, flags);
4016 param->fp_lmd = orig_lmd;
4022 if (magic == LOV_USER_MAGIC_V1) {
4023 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
4024 param->fp_lmd->lmd_lmm.lmm_objects,
4025 path, param->fp_obd_index,
4026 param->fp_max_depth, param->fp_verbose,
4029 char pool_name[LOV_MAXPOOLNAME + 1];
4030 struct lov_user_ost_data_v1 *objects;
4031 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
4033 snprintf(pool_name, sizeof(pool_name), "%s",
4034 lmmv3->lmm_pool_name);
4035 objects = lmmv3->lmm_objects;
4036 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
4037 objects, path, param->fp_obd_index,
4038 param->fp_max_depth, param->fp_verbose,
4043 static uint32_t check_foreign_type(uint32_t foreign_type)
4047 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
4048 if (lu_foreign_types[i].lft_name == NULL)
4050 if (foreign_type == lu_foreign_types[i].lft_type)
4054 return LU_FOREIGN_TYPE_UNKNOWN;
4057 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
4058 enum lov_dump_flags flags)
4060 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
4061 bool yaml = flags & LDF_YAML;
4063 if (!yaml && param->fp_depth && path)
4064 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4066 if (param->fp_verbose & VERBOSE_DETAIL) {
4067 uint32_t type = check_foreign_type(lfm->lfm_type);
4069 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
4071 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
4073 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
4075 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4076 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4077 lu_foreign_types[type].lft_name);
4079 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4081 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
4084 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
4085 lfm->lfm_length, lfm->lfm_value);
4086 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4089 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
4090 enum lov_dump_flags flags)
4092 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
4093 bool yaml = flags & LDF_YAML;
4095 if (!yaml && param->fp_depth && path)
4096 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4098 if (param->fp_verbose & VERBOSE_DETAIL) {
4099 uint32_t type = check_foreign_type(lfm->lfm_type);
4101 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
4103 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
4105 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
4107 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4108 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4109 lu_foreign_types[type].lft_name);
4111 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4113 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
4116 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
4117 lfm->lfm_length, lfm->lfm_value);
4118 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4121 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
4122 enum lov_dump_flags flags)
4126 if (param->fp_get_lmv || param->fp_get_default_lmv)
4127 magic = (__u32)param->fp_lmv_md->lum_magic;
4129 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
4132 flags |= LDF_IS_RAW;
4137 case LOV_USER_MAGIC_V1:
4138 case LOV_USER_MAGIC_V3:
4139 case LOV_USER_MAGIC_SPECIFIC:
4140 lov_dump_plain_user_lmm(param, path, flags);
4142 case LOV_USER_MAGIC_FOREIGN:
4143 lov_dump_foreign_lmm(param, path, flags);
4146 case LMV_USER_MAGIC: {
4147 char pool_name[LOV_MAXPOOLNAME + 1];
4148 struct lmv_user_md *lum;
4150 lum = (struct lmv_user_md *)param->fp_lmv_md;
4151 snprintf(pool_name, sizeof(pool_name), "%s",
4152 lum->lum_pool_name);
4153 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
4154 param->fp_max_depth, param->fp_verbose,
4158 case LOV_USER_MAGIC_COMP_V1:
4159 lov_dump_comp_v1(param, path, flags);
4161 case LMV_MAGIC_FOREIGN:
4162 lmv_dump_foreign_lmm(param, path, flags);
4165 llapi_printf(LLAPI_MSG_NORMAL,
4166 "unknown lmm_magic: %#x (expecting one of %#x %#x %#x %#x)\n",
4167 *(__u32 *)¶m->fp_lmd->lmd_lmm,
4168 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
4169 LMV_USER_MAGIC, LMV_MAGIC_V1);
4174 static int llapi_file_get_stripe1(const char *path, struct lov_user_md *lum)
4180 fname = strrchr(path, '/');
4182 /* It should be a file (or other non-directory) */
4183 if (fname == NULL) {
4184 dname = (char *)malloc(2);
4188 fname = (char *)path;
4190 dname = (char *)malloc(fname - path + 1);
4193 strncpy(dname, path, fname - path);
4194 dname[fname - path] = '\0';
4198 fd = open(dname, O_RDONLY | O_NONBLOCK);
4204 strcpy((char *)lum, fname);
4205 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
4208 if (close(fd) == -1 && rc == 0)
4216 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
4218 char *canon_path = NULL;
4221 rc = llapi_file_get_stripe1(path, lum);
4222 if (!(rc == -ENOTTY || rc == -ENODATA))
4225 /* Handle failure due to symlinks by dereferencing path manually. */
4226 canon_path = canonicalize_file_name(path);
4227 if (canon_path == NULL)
4228 goto out; /* Keep original rc. */
4230 rc2 = llapi_file_get_stripe1(canon_path, lum);
4232 goto out; /* Keep original rc. */
4241 int llapi_file_lookup(int dirfd, const char *name)
4243 struct obd_ioctl_data data = { 0 };
4248 if (dirfd < 0 || name == NULL)
4251 data.ioc_version = OBD_IOCTL_VERSION;
4252 data.ioc_len = sizeof(data);
4253 data.ioc_inlbuf1 = (char *)name;
4254 data.ioc_inllen1 = strlen(name) + 1;
4256 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
4258 llapi_error(LLAPI_MSG_ERROR, rc,
4259 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
4264 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
4271 * Check if the file time matches all the given criteria (e.g. --atime +/-N).
4272 * Return -1 or 1 if file timestamp does not or does match the given criteria
4273 * correspondingly. Return 0 if the MDS time is being checked and there are
4274 * attributes on OSTs and it is not yet clear if the timespamp matches.
4276 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
4277 * updated timestamps.
4279 static int find_time_check(struct find_param *param, int mds)
4281 struct lov_user_mds_data *lmd = param->fp_lmd;
4285 /* Check if file is accepted. */
4286 if (param->fp_atime) {
4287 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4288 param->fp_atime, param->fp_asign,
4289 param->fp_exclude_atime,
4290 param->fp_time_margin, mds);
4296 if (param->fp_mtime) {
4297 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4298 param->fp_mtime, param->fp_msign,
4299 param->fp_exclude_mtime,
4300 param->fp_time_margin, mds);
4305 * If the previous check matches, but this one is not yet clear,
4306 * we should return 0 to do an RPC on OSTs.
4312 if (param->fp_ctime) {
4313 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4314 param->fp_ctime, param->fp_csign,
4315 param->fp_exclude_ctime,
4316 param->fp_time_margin, mds);
4321 * If the previous check matches, but this one is not yet clear,
4322 * we should return 0 to do an RPC on OSTs.
4331 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4333 struct lov_user_mds_data *lmd = param->fp_lmd;
4338 for (i = 0; i < 2; i++) {
4339 /* Check if file is accepted. */
4340 if (param->fp_newery[NEWERXY_ATIME][i]) {
4341 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4342 param->fp_newery[NEWERXY_ATIME][i],
4349 if (param->fp_newery[NEWERXY_MTIME][i]) {
4350 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4351 param->fp_newery[NEWERXY_MTIME][i],
4357 * If the previous check matches, but this one is not
4358 * yet clear, we should return 0 to do an RPC on OSTs.
4364 if (param->fp_newery[NEWERXY_CTIME][i]) {
4365 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4366 param->fp_newery[NEWERXY_CTIME][i],
4372 * If the previous check matches, but this one is not
4373 * yet clear, we should return 0 to do an RPC on OSTs.
4380 * File birth time (btime) can get from MDT directly.
4381 * if @from_mdt is true, it means the input file attributs are
4382 * obtained directly from MDT.
4383 * Thus, if @from_mdt is false, we should skip the following
4389 if (param->fp_newery[NEWERXY_BTIME][i]) {
4390 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4393 rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4394 param->fp_newery[NEWERXY_BTIME][i],
4405 * Check whether the stripes matches the indexes user provided
4409 static int check_obd_match(struct find_param *param)
4411 struct lov_user_ost_data_v1 *objects;
4412 struct lov_comp_md_v1 *comp_v1 = NULL;
4413 struct lov_user_mds_data *lmd = param->fp_lmd;
4414 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4415 int i, j, k, count = 1;
4417 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4420 if (!S_ISREG(lmd->lmd_stx.stx_mode))
4423 /* exclude foreign */
4424 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4425 return param->fp_exclude_obd;
4428 * Only those files should be accepted, which have a
4429 * stripe on the specified OST.
4431 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4432 comp_v1 = (struct lov_comp_md_v1 *)v1;
4433 count = comp_v1->lcm_entry_count;
4436 for (i = 0; i < count; i++) {
4438 v1 = lov_comp_entry(comp_v1, i);
4440 objects = lov_v1v3_objects(v1);
4442 for (j = 0; j < v1->lmm_stripe_count; j++) {
4443 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4446 for (k = 0; k < param->fp_num_obds; k++) {
4447 if (param->fp_obd_indexes[k] ==
4448 objects[j].l_ost_idx)
4449 return !param->fp_exclude_obd;
4454 return param->fp_exclude_obd;
4457 static int check_mdt_match(struct find_param *param)
4461 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4464 /* FIXME: For striped dir, we should get stripe information and check */
4465 for (i = 0; i < param->fp_num_mdts; i++) {
4466 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4467 return !param->fp_exclude_mdt;
4470 if (param->fp_exclude_mdt)
4477 * Check whether the obd is active or not, if it is
4478 * not active, just print the object affected by this
4481 static void print_failed_tgt(struct find_param *param, char *path, int type)
4483 struct obd_statfs stat_buf;
4484 struct obd_uuid uuid_buf;
4485 int tgt_nr, i, *indexes;
4488 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV) {
4489 llapi_error(LLAPI_MSG_NORMAL, ret, "%s: wrong statfs type(%d)",
4494 tgt_nr = (type == LL_STATFS_LOV) ? param->fp_obd_index :
4495 param->fp_mdt_index;
4496 indexes = (type == LL_STATFS_LOV) ? param->fp_obd_indexes :
4497 param->fp_mdt_indexes;
4499 for (i = 0; i < tgt_nr; i++) {
4500 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4501 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4503 ret = llapi_obd_statfs(path, type, indexes[i], &stat_buf,
4506 llapi_error(LLAPI_MSG_NORMAL, ret,
4507 "%s: obd_uuid: %s failed",
4508 __func__, param->fp_obd_uuid->uuid);
4512 static int find_check_stripe_size(struct find_param *param)
4514 struct lov_comp_md_v1 *comp_v1 = NULL;
4515 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4516 __u32 stripe_size = 0;
4517 int ret, i, count = 1;
4519 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4520 return param->fp_exclude_stripe_size ? 1 : -1;
4522 ret = param->fp_exclude_stripe_size ? 1 : -1;
4523 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4524 comp_v1 = (struct lov_comp_md_v1 *)v1;
4525 count = comp_v1->lcm_entry_count;
4528 for (i = 0; i < count; i++) {
4529 struct lov_comp_md_entry_v1 *ent;
4532 v1 = lov_comp_entry(comp_v1, i);
4534 ent = &comp_v1->lcm_entries[i];
4535 if (ent->lcme_flags & LCME_FL_EXTENSION)
4537 if (!(ent->lcme_flags & LCME_FL_INIT))
4540 stripe_size = v1->lmm_stripe_size;
4543 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4544 param->fp_stripe_size_sign,
4545 param->fp_exclude_stripe_size,
4546 param->fp_stripe_size_units, 0);
4551 static int find_check_ext_size(struct find_param *param)
4553 struct lov_comp_md_v1 *comp_v1;
4554 struct lov_user_md_v1 *v1;
4557 ret = param->fp_exclude_ext_size ? 1 : -1;
4558 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4559 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4562 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4563 struct lov_comp_md_entry_v1 *ent;
4565 v1 = lov_comp_entry(comp_v1, i);
4567 ent = &comp_v1->lcm_entries[i];
4568 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4571 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4572 param->fp_ext_size_sign,
4573 param->fp_exclude_ext_size,
4574 param->fp_ext_size_units, 0);
4575 /* If any ext_size matches */
4583 static __u32 find_get_stripe_count(struct find_param *param)
4585 struct lov_comp_md_v1 *comp_v1 = NULL;
4586 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4588 __u32 stripe_count = 0;
4590 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4593 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4594 comp_v1 = (struct lov_comp_md_v1 *)v1;
4595 count = comp_v1->lcm_entry_count;
4598 for (i = 0; i < count; i++) {
4600 struct lov_comp_md_entry_v1 *ent;
4602 v1 = lov_comp_entry(comp_v1, i);
4604 ent = &comp_v1->lcm_entries[i];
4605 if (!(ent->lcme_flags & LCME_FL_INIT))
4608 if (ent->lcme_flags & LCME_FL_EXTENSION)
4611 stripe_count = v1->lmm_stripe_count;
4614 return stripe_count;
4617 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4619 static int find_check_layout(struct find_param *param)
4621 struct lov_comp_md_v1 *comp_v1 = NULL;
4622 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4624 bool found = false, valid = false;
4626 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4627 comp_v1 = (struct lov_comp_md_v1 *)v1;
4628 count = comp_v1->lcm_entry_count;
4631 for (i = 0; i < count; i++) {
4633 v1 = lov_comp_entry(comp_v1, i);
4635 /* foreign file have a special magic but no pattern field */
4636 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4639 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4643 if (v1->lmm_pattern & param->fp_layout) {
4652 if ((found && !param->fp_exclude_layout) ||
4653 (!found && param->fp_exclude_layout))
4660 * if no type specified, check/exclude all foreign
4661 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4663 static int find_check_foreign(struct find_param *param)
4665 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4666 struct lov_foreign_md *lfm;
4668 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4669 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4670 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4671 return param->fp_exclude_foreign ? 1 : -1;
4675 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4676 lfm->lfm_type == param->fp_foreign_type)
4677 return param->fp_exclude_foreign ? -1 : 1;
4678 return param->fp_exclude_foreign ? 1 : -1;
4681 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4682 struct lmv_foreign_md *lfm;
4684 lfm = (void *)param->fp_lmv_md;
4685 if (lmv_is_foreign(lfm->lfm_magic)) {
4686 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4687 return param->fp_exclude_foreign ? 1 : -1;
4691 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4692 lfm->lfm_type == param->fp_foreign_type)
4693 return param->fp_exclude_foreign ? -1 : 1;
4694 return param->fp_exclude_foreign ? 1 : -1;
4699 static int find_check_pool(struct find_param *param)
4701 struct lov_comp_md_v1 *comp_v1 = NULL;
4702 struct lov_user_md_v3 *v3 = (void *)¶m->fp_lmd->lmd_lmm;
4706 if (v3->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4707 comp_v1 = (struct lov_comp_md_v1 *)v3;
4708 count = comp_v1->lcm_entry_count;
4709 /* empty requested pool is taken as no pool search */
4710 if (count == 0 && param->fp_poolname[0] == '\0') {
4716 for (i = 0; i < count; i++) {
4717 if (comp_v1 != NULL) {
4718 if (!(comp_v1->lcm_entries[i].lcme_flags &
4722 v3 = (void *)lov_comp_entry(comp_v1, i);
4725 if (v3->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4728 if (((v3->lmm_magic == LOV_USER_MAGIC_V1) &&
4729 (param->fp_poolname[0] == '\0')) ||
4730 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4731 (strncmp(v3->lmm_pool_name,
4732 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4733 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4734 (strcmp(param->fp_poolname, "*") == 0))) {
4741 if ((found && !param->fp_exclude_pool) ||
4742 (!found && param->fp_exclude_pool))
4748 static int find_check_comp_options(struct find_param *param)
4750 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4751 struct lov_user_mds_data *lmd = param->fp_lmd;
4752 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4753 struct lov_comp_md_entry_v1 *entry;
4756 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4759 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4760 comp_v1 = (struct lov_comp_md_v1 *)v1;
4762 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4763 if (forged_v1 == NULL)
4765 comp_v1 = forged_v1;
4766 comp_v1->lcm_entry_count = 1;
4767 entry = &comp_v1->lcm_entries[0];
4768 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4770 entry->lcme_extent.e_start = 0;
4771 entry->lcme_extent.e_end = LUSTRE_EOF;
4774 /* invalid case, don't match for any kind of search. */
4775 if (comp_v1->lcm_entry_count == 0) {
4780 if (param->fp_check_comp_count) {
4781 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4782 param->fp_comp_count,
4783 param->fp_comp_count_sign,
4784 param->fp_exclude_comp_count, 1, 0);
4790 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4791 entry = &comp_v1->lcm_entries[i];
4793 if (param->fp_check_comp_flags) {
4795 if (((param->fp_comp_flags & entry->lcme_flags) !=
4796 param->fp_comp_flags) ||
4797 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4803 if (param->fp_check_comp_start) {
4804 ret = find_value_cmp(entry->lcme_extent.e_start,
4805 param->fp_comp_start,
4806 param->fp_comp_start_sign,
4807 param->fp_exclude_comp_start,
4808 param->fp_comp_start_units, 0);
4813 if (param->fp_check_comp_end) {
4814 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4820 /* the component matches all criteria */
4829 static int find_check_mirror_options(struct find_param *param)
4831 struct lov_comp_md_v1 *comp_v1;
4832 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4835 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4838 comp_v1 = (struct lov_comp_md_v1 *)v1;
4840 if (param->fp_check_mirror_count) {
4841 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4842 param->fp_mirror_count,
4843 param->fp_mirror_count_sign,
4844 param->fp_exclude_mirror_count, 1, 0);
4849 if (param->fp_check_mirror_state) {
4851 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4853 if ((param->fp_mirror_state != 0 &&
4854 file_state != param->fp_mirror_state) ||
4855 file_state == param->fp_mirror_neg_state)
4862 static bool find_check_lmm_info(struct find_param *param)
4864 return param->fp_check_pool || param->fp_check_stripe_count ||
4865 param->fp_check_stripe_size || param->fp_check_layout ||
4866 param->fp_check_comp_count || param->fp_check_comp_end ||
4867 param->fp_check_comp_start || param->fp_check_comp_flags ||
4868 param->fp_check_mirror_count || param->fp_check_foreign ||
4869 param->fp_check_mirror_state || param->fp_check_ext_size ||
4870 param->fp_check_projid;
4874 * Get file/directory project id.
4875 * by the open fd resides on.
4876 * Return 0 and project id on success, or -ve errno.
4878 static int fget_projid(int fd, int *projid)
4883 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
4887 *projid = fsx.fsx_projid;
4892 * Check that the file's permissions in *st matches the one in find_param
4894 static int check_file_permissions(const struct find_param *param,
4901 switch (param->fp_perm_sign) {
4902 case LFS_FIND_PERM_EXACT:
4903 decision = (mode == param->fp_perm);
4905 case LFS_FIND_PERM_ALL:
4906 decision = ((mode & param->fp_perm) == param->fp_perm);
4908 case LFS_FIND_PERM_ANY:
4909 decision = ((mode & param->fp_perm) != 0);
4913 if ((param->fp_exclude_perm && decision)
4914 || (!param->fp_exclude_perm && !decision))
4920 static int cb_find_init(char *path, int p, int *dp,
4921 void *data, struct dirent64 *de)
4923 struct find_param *param = (struct find_param *)data;
4924 struct lov_user_mds_data *lmd = param->fp_lmd;
4925 int d = dp == NULL ? -1 : *dp;
4926 int decision = 1; /* 1 is accepted; -1 is rejected. */
4928 int checked_type = 0;
4930 __u32 stripe_count = 0;
4934 if (p == -1 && d == -1)
4937 /* If a regular expression is presented, make the initial decision */
4938 if (param->fp_pattern != NULL) {
4939 char *fname = strrchr(path, '/');
4941 fname = (fname == NULL ? path : fname + 1);
4942 ret = fnmatch(param->fp_pattern, fname, 0);
4943 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4944 (ret == 0 && param->fp_exclude_pattern))
4948 /* See if we can check the file type from the dirent. */
4949 if (de != NULL && de->d_type != DT_UNKNOWN) {
4950 if (param->fp_type != 0) {
4953 if (DTTOIF(de->d_type) == param->fp_type) {
4954 if (param->fp_exclude_type)
4957 if (!param->fp_exclude_type)
4961 if ((param->fp_check_mdt_count || param->fp_hash_type ||
4962 param->fp_check_hash_flag) && de->d_type != DT_DIR)
4969 * Request MDS for the stat info if some of these parameters need
4972 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4973 param->fp_check_uid || param->fp_check_gid ||
4974 param->fp_newerxy || param->fp_btime ||
4975 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4976 param->fp_check_size || param->fp_check_blocks ||
4977 find_check_lmm_info(param) ||
4978 param->fp_check_mdt_count || param->fp_hash_type ||
4979 param->fp_check_hash_flag || param->fp_perm_sign)
4982 if (param->fp_type != 0 && checked_type == 0)
4985 if (decision == 0) {
4987 (param->fp_check_mdt_count || param->fp_hash_type ||
4988 param->fp_check_hash_flag || param->fp_check_foreign)) {
4989 param->fp_get_lmv = 1;
4990 ret = cb_get_dirstripe(path, &d, param);
4992 if (errno == ENODATA) {
4994 if (param->fp_check_mdt_count ||
4995 param->fp_hash_type ||
4996 param->fp_check_hash_flag) {
4997 param->fp_lmv_md->lum_stripe_count = 0;
4998 param->fp_lmv_md->lum_hash_type = 0;
5000 if (param->fp_check_foreign) {
5001 if (param->fp_exclude_foreign)
5010 if (param->fp_check_mdt_count) {
5011 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5014 decision = find_value_cmp(param->fp_lmv_md->lum_stripe_count,
5015 param->fp_mdt_count,
5016 param->fp_mdt_count_sign,
5017 param->fp_exclude_mdt_count, 1, 0);
5022 if (param->fp_hash_type) {
5024 __u32 type = param->fp_lmv_md->lum_hash_type &
5027 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5030 found = (1 << type) & param->fp_hash_type;
5031 if ((found && param->fp_exclude_hash_type) ||
5032 (!found && !param->fp_exclude_hash_type))
5036 if (param->fp_check_hash_flag) {
5037 __u32 flags = param->fp_lmv_md->lum_hash_type &
5038 ~LMV_HASH_TYPE_MASK;
5040 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5043 if (!(flags & param->fp_hash_inflags) ||
5044 (flags & param->fp_hash_exflags))
5049 param->fp_lmd->lmd_lmm.lmm_magic = 0;
5050 ret = get_lmd_info_fd(path, p, d, param->fp_lmd,
5051 param->fp_lum_size, GET_LMD_INFO);
5052 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
5053 find_check_lmm_info(param)) {
5054 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
5057 * We need to "fake" the "use the default" values
5058 * since the lmm struct is zeroed out at this point.
5060 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5061 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
5063 ostid_set_seq(&lmm->lmm_oi,
5064 FID_SEQ_LOV_DEFAULT);
5065 lmm->lmm_stripe_size = 0;
5066 lmm->lmm_stripe_count = 0;
5067 lmm->lmm_stripe_offset = -1;
5069 if (ret == 0 && param->fp_mdt_uuid != NULL) {
5071 ret = llapi_file_fget_mdtidx(d,
5072 ¶m->fp_file_mdt_index);
5073 } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
5075 * FIXME: we could get the MDT index from the
5076 * file's FID in lmd->lmd_lmm.lmm_oi without
5077 * opening the file, once we are sure that
5078 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
5079 * That would still be an ioctl() to map the
5080 * FID to the MDT, but not an open RPC.
5082 fd = open(path, O_RDONLY);
5084 ret = llapi_file_fget_mdtidx(fd,
5085 ¶m->fp_file_mdt_index);
5091 * For a special file, we assume it resides on
5092 * the same MDT as the parent directory.
5094 ret = llapi_file_fget_mdtidx(p,
5095 ¶m->fp_file_mdt_index);
5106 stripe_count = find_get_stripe_count(param);
5110 /* Check the file permissions from the stat info */
5111 if (param->fp_perm_sign) {
5112 decision = check_file_permissions(param, lmd->lmd_stx.stx_mode);
5117 if (param->fp_type && !checked_type) {
5118 if ((param->fp_check_mdt_count || param->fp_check_hash_flag ||
5119 param->fp_hash_type) && !S_ISDIR(lmd->lmd_stx.stx_mode))
5122 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
5123 if (param->fp_exclude_type)
5126 if (!param->fp_exclude_type)
5132 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
5133 if (lustre_fs && param->fp_got_uuids &&
5134 param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
5135 lmd->lmd_stx.stx_dev_minor)) {
5136 /* A lustre/lustre mount point is crossed. */
5137 param->fp_got_uuids = 0;
5138 param->fp_obds_printed = 0;
5139 param->fp_mdt_index = OBD_NOT_FOUND;
5140 param->fp_obd_index = OBD_NOT_FOUND;
5143 if (lustre_fs && !param->fp_got_uuids) {
5144 ret = setup_target_indexes((d != -1) ? d : p, path,
5149 param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
5150 lmd->lmd_stx.stx_dev_minor);
5151 } else if (!lustre_fs && param->fp_got_uuids) {
5152 /* A lustre/non-lustre mount point is crossed. */
5153 param->fp_got_uuids = 0;
5154 param->fp_mdt_index = OBD_NOT_FOUND;
5155 param->fp_obd_index = OBD_NOT_FOUND;
5159 if (param->fp_check_foreign) {
5160 decision = find_check_foreign(param);
5165 if (param->fp_check_stripe_size) {
5166 decision = find_check_stripe_size(param);
5171 if (param->fp_check_ext_size) {
5172 decision = find_check_ext_size(param);
5177 if (param->fp_check_stripe_count) {
5178 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
5179 param->fp_stripe_count_sign,
5180 param->fp_exclude_stripe_count, 1, 0);
5185 if (param->fp_check_layout) {
5186 decision = find_check_layout(param);
5191 /* If an OBD UUID is specified but none matches, skip this file. */
5192 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
5193 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
5197 * If an OST or MDT UUID is given, and some OST matches,
5200 if (param->fp_obd_index != OBD_NOT_FOUND ||
5201 param->fp_mdt_index != OBD_NOT_FOUND) {
5202 if (param->fp_obd_uuid) {
5203 if (check_obd_match(param)) {
5205 * If no mdtuuid is given, we are done.
5206 * Otherwise, fall through to the mdtuuid
5209 if (!param->fp_mdt_uuid)
5216 if (param->fp_mdt_uuid) {
5217 if (check_mdt_match(param))
5224 if (param->fp_check_uid) {
5225 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
5226 if (param->fp_exclude_uid)
5229 if (!param->fp_exclude_uid)
5234 if (param->fp_check_gid) {
5235 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
5236 if (param->fp_exclude_gid)
5239 if (!param->fp_exclude_gid)
5244 if (param->fp_check_projid) {
5248 fd = open(path, O_RDONLY);
5251 ret = fget_projid(fd, &projid);
5256 if (projid == param->fp_projid) {
5257 if (param->fp_exclude_projid)
5260 if (!param->fp_exclude_projid)
5265 if (param->fp_check_pool) {
5266 decision = find_check_pool(param);
5271 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
5272 param->fp_check_comp_start || param->fp_check_comp_end) {
5273 decision = find_check_comp_options(param);
5278 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
5279 decision = find_check_mirror_options(param);
5284 /* Check the time on mds. */
5286 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
5289 for_mds = lustre_fs ?
5290 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5291 decision = find_time_check(param, for_mds);
5296 if (param->fp_btime) {
5297 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
5302 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
5303 param->fp_btime, param->fp_bsign,
5304 param->fp_exclude_btime,
5305 param->fp_time_margin, 0);
5310 if (param->fp_newerxy) {
5313 for_mds = lustre_fs ?
5314 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5315 decision = find_newerxy_check(param, for_mds, true);
5324 flags = param->fp_lmd->lmd_flags;
5325 if (param->fp_check_size &&
5326 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5327 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5328 !(flags & OBD_MD_FLSIZE ||
5329 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
5332 if (param->fp_check_blocks &&
5333 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5334 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5335 !(flags & OBD_MD_FLBLOCKS ||
5336 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
5340 * If file still fits the request, ask ost for updated info.
5341 * The regular stat is almost of the same speed as some new
5342 * 'glimpse-size-ioctl'.
5348 * For regular files with the stripe the decision may have not
5349 * been taken yet if *time or size is to be checked.
5351 if (param->fp_obd_index != OBD_NOT_FOUND)
5352 print_failed_tgt(param, path, LL_STATFS_LOV);
5354 if (param->fp_mdt_index != OBD_NOT_FOUND)
5355 print_failed_tgt(param, path, LL_STATFS_LMV);
5358 ret = fstat_f(d, &st);
5359 else if (de != NULL)
5360 ret = fstatat_f(p, de->d_name, &st,
5361 AT_SYMLINK_NOFOLLOW);
5363 ret = lstat_f(path, &st);
5366 if (errno == ENOENT) {
5367 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
5368 "warning: %s: %s does not exist",
5373 llapi_error(LLAPI_MSG_ERROR, ret,
5374 "%s: stat on %s failed",
5380 convert_lmd_statx(param->fp_lmd, &st, true);
5381 /* Check the time on osc. */
5382 decision = find_time_check(param, 0);
5386 if (param->fp_newerxy) {
5387 decision = find_newerxy_check(param, 0, false);
5397 if (param->fp_check_size) {
5398 decision = find_value_cmp(lmd->lmd_stx.stx_size,
5400 param->fp_size_sign,
5401 param->fp_exclude_size,
5402 param->fp_size_units, 0);
5407 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
5408 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
5410 param->fp_blocks_sign,
5411 param->fp_exclude_blocks,
5412 param->fp_blocks_units, 0);
5418 llapi_printf(LLAPI_MSG_NORMAL, "%s%c", path,
5419 param->fp_zero_end ? '\0' : '\n');
5423 /* Do not get down anymore? */
5424 if (param->fp_depth == param->fp_max_depth) {
5435 static int cb_migrate_mdt_init(char *path, int p, int *dp,
5436 void *param_data, struct dirent64 *de)
5438 struct find_param *param = (struct find_param *)param_data;
5439 struct lmv_user_md *lmu = param->fp_lmv_md;
5441 char raw[MAX_IOC_BUFLEN] = {'\0'};
5443 struct obd_ioctl_data data = { 0 };
5449 if (p == -1 && dp == NULL)
5455 if (dp != NULL && *dp != -1)
5459 tmp_p = open_parent(path);
5463 llapi_error(LLAPI_MSG_ERROR, ret,
5464 "can not open %s", path);
5469 path_copy = strdup(path);
5470 filename = basename(path_copy);
5472 data.ioc_inlbuf1 = (char *)filename;
5473 data.ioc_inllen1 = strlen(filename) + 1;
5474 data.ioc_inlbuf2 = (char *)lmu;
5475 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
5477 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5479 llapi_error(LLAPI_MSG_ERROR, ret,
5480 "%s: error packing ioctl data", __func__);
5485 ret = ioctl(tmp_p, LL_IOC_MIGRATE, rawbuf);
5487 if (errno == EBUSY && !retry) {
5489 * because migrate may not be able to lock all involved
5490 * objects in order, for some of them it try lock, while
5491 * there may be conflicting COS locks and cause migrate
5492 * fail with EBUSY, hope a sync() could cause
5493 * transaction commit and release these COS locks.
5498 } else if (errno == EALREADY) {
5499 if (param->fp_verbose & VERBOSE_DETAIL)
5500 llapi_printf(LLAPI_MSG_NORMAL,
5501 "%s migrated to MDT%d already\n",
5502 path, lmu->lum_stripe_offset);
5506 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
5510 } else if (param->fp_verbose & VERBOSE_DETAIL) {
5511 llapi_printf(LLAPI_MSG_NORMAL,
5512 "migrate %s to MDT%d stripe count %d\n",
5513 path, lmu->lum_stripe_offset,
5514 lmu->lum_stripe_count);
5520 * If the directory is being migration, we need
5521 * close the directory after migration,
5522 * so the old directory cache will be cleanup
5523 * on the client side, and re-open to get the
5524 * new directory handle
5526 *dp = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
5529 llapi_error(LLAPI_MSG_ERROR, ret,
5530 "%s: Failed to open '%s'", __func__, path);
5542 /* dir migration finished, shrink its stripes */
5543 static int cb_migrate_mdt_fini(char *path, int p, int *dp, void *data,
5544 struct dirent64 *de)
5546 struct find_param *param = data;
5547 struct lmv_user_md *lmu = param->fp_lmv_md;
5548 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
5551 if (de && de->d_type != DT_DIR)
5556 * close it before setxattr because the latter may destroy the
5557 * original object, and cause close fail.
5565 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
5566 if (ret == -EALREADY)
5569 cb_common_fini(path, p, dp, data, de);
5573 int llapi_migrate_mdt(char *path, struct find_param *param)
5575 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
5579 int llapi_mv(char *path, struct find_param *param)
5581 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
5582 static bool printed;
5585 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
5586 "%s() is deprecated, use llapi_migrate_mdt() instead",
5591 return llapi_migrate_mdt(path, param);
5594 int llapi_find(char *path, struct find_param *param)
5596 return param_callback(path, cb_find_init, cb_common_fini, param);
5600 * Get MDT number that the file/directory inode referenced
5601 * by the open fd resides on.
5602 * Return 0 and mdtidx on success, or -ve errno.
5604 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
5606 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
5611 static int cb_get_mdt_index(char *path, int p, int *dp, void *data,
5612 struct dirent64 *de)
5614 struct find_param *param = (struct find_param *)data;
5615 int d = dp == NULL ? -1 : *dp;
5619 if (p == -1 && d == -1)
5623 ret = llapi_file_fget_mdtidx(d, &mdtidx);
5624 } else /* if (p != -1) */ {
5627 fd = open(path, O_RDONLY | O_NOCTTY);
5629 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5637 if (ret == -ENODATA) {
5638 if (!param->fp_obd_uuid)
5639 llapi_printf(LLAPI_MSG_NORMAL,
5640 "'%s' has no stripe info\n", path);
5642 } else if (ret == -ENOENT) {
5643 llapi_error(LLAPI_MSG_WARN, ret,
5644 "warning: %s: '%s' does not exist",
5647 } else if (ret == -ENOTTY) {
5648 llapi_error(LLAPI_MSG_ERROR, ret,
5649 "%s: '%s' not on a Lustre fs",
5652 llapi_error(LLAPI_MSG_ERROR, ret,
5653 "error: %s: '%s' failed get_mdtidx",
5659 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5660 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5662 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5666 /* Do not go down anymore? */
5667 if (param->fp_depth == param->fp_max_depth)
5675 static int cb_getstripe(char *path, int p, int *dp, void *data,
5676 struct dirent64 *de)
5678 struct find_param *param = (struct find_param *)data;
5679 int d = dp == NULL ? -1 : *dp;
5682 if (p == -1 && d == -1)
5685 if (param->fp_obd_uuid) {
5686 param->fp_quiet = 1;
5687 ret = setup_obd_uuid(d != -1 ? d : p, path, param);
5692 if (d != -1 && (param->fp_get_lmv || param->fp_get_default_lmv))
5693 ret = cb_get_dirstripe(path, &d, param);
5695 (p != -1 && !param->fp_get_lmv && !param->fp_get_default_lmv))
5696 ret = get_lmd_info_fd(path, p, d, ¶m->fp_lmd->lmd_lmm,
5697 param->fp_lum_size, GET_LMD_STRIPE);
5698 else if (d == -1 && (param->fp_get_lmv || param->fp_get_default_lmv)) {
5699 /* in case of a dangling or valid faked symlink dir, opendir()
5700 * should have return either EINVAL or ENOENT, so let's try
5701 * to get LMV just in case, and by opening it as a file but
5702 * with O_NOFOLLOW ...
5704 int fd = open(path, O_RDONLY | O_NOFOLLOW);
5708 ret = cb_get_dirstripe(path, &fd, param);
5710 llapi_lov_dump_user_lmm(param, path, LDF_IS_DIR);
5717 if (errno == ENODATA && d != -1) {
5719 * We need to "fake" the "use the default" values
5720 * since the lmm struct is zeroed out at this point.
5721 * The magic needs to be set in order to satisfy
5722 * a check later on in the code path.
5723 * The object_seq needs to be set for the "(Default)"
5724 * prefix to be displayed.
5726 if (param->fp_get_default_lmv) {
5727 struct lmv_user_md *lum = param->fp_lmv_md;
5729 lum->lum_magic = LMV_USER_MAGIC;
5730 lum->lum_stripe_count = 0;
5731 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
5733 } else if (param->fp_get_lmv) {
5734 struct lmv_user_md *lum = param->fp_lmv_md;
5737 ret = llapi_file_fget_mdtidx(d, &mdtidx);
5740 lum->lum_magic = LMV_MAGIC_V1;
5741 lum->lum_stripe_count = 0;
5742 lum->lum_stripe_offset = mdtidx;
5745 struct lov_user_md *lmm =
5746 ¶m->fp_lmd->lmd_lmm;
5748 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5750 ostid_set_seq(&lmm->lmm_oi,
5751 FID_SEQ_LOV_DEFAULT);
5752 lmm->lmm_stripe_count = 0;
5753 lmm->lmm_stripe_size = 0;
5754 lmm->lmm_stripe_offset = -1;
5757 } else if (errno == ENODATA && p != -1) {
5758 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5759 llapi_printf(LLAPI_MSG_NORMAL,
5760 "%s has no stripe info\n", path);
5762 } else if (errno == ENOENT) {
5763 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5764 "warning: %s: %s does not exist",
5767 } else if (errno == ENOTTY) {
5769 llapi_error(LLAPI_MSG_ERROR, ret,
5770 "%s: '%s' not on a Lustre fs?",
5775 llapi_error(LLAPI_MSG_ERROR, ret,
5776 "error: %s: %s failed for %s",
5778 "LL_IOC_LOV_GETSTRIPE" :
5779 "IOC_MDC_GETFILESTRIPE", path);
5786 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5787 llapi_lov_dump_user_lmm(param, path, d != -1 ? LDF_IS_DIR : 0);
5790 /* Do not get down anymore? */
5791 if (param->fp_depth == param->fp_max_depth)
5799 int llapi_getstripe(char *path, struct find_param *param)
5801 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5802 cb_get_mdt_index : cb_getstripe,
5803 cb_common_fini, param);
5806 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5807 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5809 char raw[MAX_IOC_BUFLEN] = {'\0'};
5811 struct obd_ioctl_data data = { 0 };
5814 data.ioc_inlbuf1 = (char *)&type;
5815 data.ioc_inllen1 = sizeof(__u32);
5816 data.ioc_inlbuf2 = (char *)&index;
5817 data.ioc_inllen2 = sizeof(__u32);
5818 data.ioc_pbuf1 = (char *)stat_buf;
5819 data.ioc_plen1 = sizeof(struct obd_statfs);
5820 data.ioc_pbuf2 = (char *)uuid_buf;
5821 data.ioc_plen2 = sizeof(struct obd_uuid);
5823 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5825 llapi_error(LLAPI_MSG_ERROR, rc,
5826 "%s: error packing ioctl data", __func__);
5830 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5832 return rc < 0 ? -errno : 0;
5835 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5836 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5841 fd = open(path, O_RDONLY);
5844 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5847 * If we can't even open a file on the filesystem (e.g. with
5848 * -ESHUTDOWN), force caller to exit or it will loop forever.
5853 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5860 #define MAX_STRING_SIZE 128
5862 int llapi_ping(char *obd_type, char *obd_name)
5864 int flags = O_RDONLY;
5865 char buf[1] = { 0 };
5869 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5870 obd_type, obd_name);
5874 fd = open(path.gl_pathv[0], flags);
5876 if (errno == EACCES && flags == O_RDONLY) {
5881 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5886 if (flags == O_RDONLY)
5887 rc = read(fd, buf, sizeof(buf));
5889 rc = write(fd, buf, sizeof(buf));
5897 cfs_free_param_data(&path);
5901 int llapi_target_iterate(int type_num, char **obd_type,
5902 void *args, llapi_cb_t cb)
5908 for (i = 0; i < type_num; i++) {
5911 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5915 for (j = 0; j < param.gl_pathc; j++) {
5916 char obd_uuid[UUID_MAX + 1];
5920 fp = fopen(param.gl_pathv[j], "r");
5923 llapi_error(LLAPI_MSG_ERROR, rc,
5924 "error: opening '%s'",
5929 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5931 llapi_error(LLAPI_MSG_ERROR, rc,
5932 "error: reading '%s'",
5937 /* Extract the obd_name from the sysfs path.
5938 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5940 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5946 /* skip /fs/lustre/'obd_type'/ */
5947 obd_name += strlen(obd_type[i]) + 12;
5948 /* chop off after obd_name */
5949 ptr = strrchr(obd_name, '/');
5953 cb(obd_type[i], obd_name, obd_uuid, args);
5962 cfs_free_param_data(¶m);
5966 static void do_target_check(char *obd_type_name, char *obd_name,
5967 char *obd_uuid, void *args)
5971 rc = llapi_ping(obd_type_name, obd_name);
5973 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5975 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5977 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5980 int llapi_target_check(int type_num, char **obd_type, char *dir)
5982 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5985 #undef MAX_STRING_SIZE
5987 /* Is this a lustre fs? */
5988 int llapi_is_lustre_mnttype(const char *type)
5990 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
5993 /* Is this a lustre client fs? */
5994 int llapi_is_lustre_mnt(struct mntent *mnt)
5996 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5997 strstr(mnt->mnt_fsname, ":/") != NULL);
6000 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
6002 char fsname[PATH_MAX + 1];
6006 rc = llapi_search_fsname(mnt, fsname);
6010 root = open(mnt, O_RDONLY | O_DIRECTORY);
6013 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
6017 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
6020 if (rc == -ENOENT && LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd))
6021 llapi_error(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, rc,
6022 "Cannot find pool '%s'", qctl->qc_poolname);
6028 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
6033 root = open(mnt, O_RDONLY | O_DIRECTORY);
6036 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
6040 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
6043 llapi_error(LLAPI_MSG_ERROR, rc,
6044 "ioctl on %s for getting connect flags failed", mnt);
6051 * Flush cached pages from all clients.
6053 * \param fd File descriptor
6057 int llapi_file_flush(int fd)
6061 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);