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>
76 #include <libcfs/util/ioctl.h>
77 #include <libcfs/util/param.h>
78 #include <libcfs/util/string.h>
79 #include <linux/lnet/lnetctl.h>
80 #include <lustre/lustreapi.h>
81 #include <linux/lustre/lustre_ostid.h>
82 #include <linux/lustre/lustre_ioctl.h>
83 #include "lustreapi_internal.h"
86 #define FORMATTED_BUF_LEN 1024
87 #define MAX_LINE_LEN 256
88 #define MAX_INSTANCE_LEN 32
90 static int llapi_msg_level = LLAPI_MSG_MAX;
91 const char *liblustreapi_cmd;
93 struct lustre_foreign_type lu_foreign_types[] = {
94 {.lft_type = LU_FOREIGN_TYPE_NONE, .lft_name = "none"},
95 {.lft_type = LU_FOREIGN_TYPE_SYMLINK, .lft_name = "symlink"},
96 /* must be the last element */
97 {.lft_type = LU_FOREIGN_TYPE_UNKNOWN, .lft_name = NULL}
98 /* array max dimension must be <= UINT32_MAX */
101 void llapi_msg_set_level(int level)
103 /* ensure level is in the good range */
104 if (level < LLAPI_MSG_OFF)
105 llapi_msg_level = LLAPI_MSG_OFF;
106 else if (level > LLAPI_MSG_MAX)
107 llapi_msg_level = LLAPI_MSG_MAX;
109 llapi_msg_level = level;
112 int llapi_msg_get_level(void)
114 return llapi_msg_level;
117 void llapi_set_command_name(const char *cmd)
119 liblustreapi_cmd = cmd;
122 void llapi_clear_command_name(void)
124 liblustreapi_cmd = NULL;
127 static void error_callback_default(enum llapi_message_level level, int err,
128 const char *fmt, va_list ap)
130 bool has_nl = strchr(fmt, '\n') != NULL;
132 if (liblustreapi_cmd != NULL)
133 fprintf(stderr, "%s %s: ", program_invocation_short_name,
136 fprintf(stderr, "%s: ", program_invocation_short_name);
139 if (level & LLAPI_MSG_NO_ERRNO) {
140 vfprintf(stderr, fmt, ap);
142 fprintf(stderr, "\n");
147 * Remove trailing linefeed so error string can be appended.
148 * @fmt is a const string, so we can't modify it directly.
150 if (has_nl && (newfmt = strdup(fmt)))
151 *strrchr(newfmt, '\n') = '\0';
153 newfmt = (char *)fmt;
155 vfprintf(stderr, newfmt, ap);
158 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
162 static void info_callback_default(enum llapi_message_level level, int err,
163 const char *fmt, va_list ap)
166 if (liblustreapi_cmd != NULL) {
167 fprintf(stdout, "%s %s: ",
168 program_invocation_short_name,
171 fprintf(stdout, "%s: ", program_invocation_short_name);
174 vfprintf(stdout, fmt, ap);
177 static llapi_log_callback_t llapi_error_callback = error_callback_default;
178 static llapi_log_callback_t llapi_info_callback = info_callback_default;
181 /* llapi_error will preserve errno */
182 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
185 int tmp_errno = errno;
187 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
191 llapi_error_callback(level, abs(err), fmt, args);
196 /* llapi_printf will preserve errno */
197 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
200 int tmp_errno = errno;
202 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
206 llapi_info_callback(level, 0, fmt, args);
212 * Set a custom error logging function. Passing in NULL will reset the logging
213 * callback to its default value.
215 * This function returns the value of the old callback.
217 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
219 llapi_log_callback_t old = llapi_error_callback;
222 llapi_error_callback = cb;
224 llapi_error_callback = error_callback_default;
230 * Set a custom info logging function. Passing in NULL will reset the logging
231 * callback to its default value.
233 * This function returns the value of the old callback.
235 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
237 llapi_log_callback_t old = llapi_info_callback;
240 llapi_info_callback = cb;
242 llapi_info_callback = info_callback_default;
248 * size_units is to be initialized (or zeroed) by caller.
250 int llapi_parse_size(const char *optarg, unsigned long long *size,
251 unsigned long long *size_units, int bytes_spec)
254 char *argbuf = (char *)optarg;
255 unsigned long long frac = 0, frac_d = 1;
257 if (strncmp(optarg, "-", 1) == 0)
260 if (*size_units == 0)
263 *size = strtoull(argbuf, &end, 0);
264 if (end != NULL && *end == '.') {
268 frac = strtoull(argbuf, &end, 10);
269 /* count decimal places */
270 for (i = 0; i < (end - argbuf); i++)
275 if ((*end == 'b') && *(end + 1) == '\0' &&
276 (*size & (~0ULL << (64 - 9))) == 0 &&
278 *size_units = 1 << 9;
279 } else if ((*end == 'b') &&
280 *(end + 1) == '\0' &&
283 } else if ((*end == 'k' || *end == 'K') &&
284 *(end + 1) == '\0' &&
285 (*size & (~0ULL << (64 - 10))) == 0) {
286 *size_units = 1 << 10;
287 } else if ((*end == 'm' || *end == 'M') &&
288 *(end + 1) == '\0' &&
289 (*size & (~0ULL << (64 - 20))) == 0) {
290 *size_units = 1 << 20;
291 } else if ((*end == 'g' || *end == 'G') &&
292 *(end + 1) == '\0' &&
293 (*size & (~0ULL << (64 - 30))) == 0) {
294 *size_units = 1 << 30;
295 } else if ((*end == 't' || *end == 'T') &&
296 *(end + 1) == '\0' &&
297 (*size & (~0ULL << (64 - 40))) == 0) {
298 *size_units = 1ULL << 40;
299 } else if ((*end == 'p' || *end == 'P') &&
300 *(end + 1) == '\0' &&
301 (*size & (~0ULL << (64 - 50))) == 0) {
302 *size_units = 1ULL << 50;
303 } else if ((*end == 'e' || *end == 'E') &&
304 *(end + 1) == '\0' &&
305 (*size & (~0ULL << (64 - 60))) == 0) {
306 *size_units = 1ULL << 60;
311 *size = *size * *size_units + frac * *size_units / frac_d;
316 int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
318 struct obd_ioctl_data *overlay;
321 data->ioc_len = obd_ioctl_packlen(data);
322 data->ioc_version = OBD_IOCTL_VERSION;
324 if (*pbuf != NULL && data->ioc_len > max_len) {
325 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
326 "pbuf = %p, ioc_len = %u, max_len = %d",
327 *pbuf, data->ioc_len, max_len);
332 *pbuf = malloc(data->ioc_len);
337 overlay = (struct obd_ioctl_data *)*pbuf;
338 memcpy(*pbuf, data, sizeof(*data));
340 ptr = overlay->ioc_bulk;
341 if (data->ioc_inlbuf1) {
342 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
343 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
346 if (data->ioc_inlbuf2) {
347 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
348 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
351 if (data->ioc_inlbuf3) {
352 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
353 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
356 if (data->ioc_inlbuf4) {
357 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
358 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
364 int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
366 struct obd_ioctl_data *overlay;
372 overlay = (struct obd_ioctl_data *)pbuf;
374 /* Preserve the caller's buffer pointers */
375 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
376 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
377 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
378 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
380 memcpy(data, pbuf, sizeof(*data));
382 ptr = overlay->ioc_bulk;
383 if (data->ioc_inlbuf1) {
384 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
385 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
388 if (data->ioc_inlbuf2) {
389 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
390 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
393 if (data->ioc_inlbuf3) {
394 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
395 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
398 if (data->ioc_inlbuf4) {
399 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
400 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
407 * Verify the setstripe parameters before using.
408 * This is a pair method for comp_args_to_layout()/llapi_layout_sanity_cb()
409 * when just 1 component or a non-PFL layout is given.
411 * \param[in] param stripe parameters
412 * \param[in] pool_name pool name
413 * \param[in] fsname lustre FS name
416 * < 0, error code on failre
418 static int llapi_stripe_param_verify(const struct llapi_stripe_param *param,
419 const char **pool_name)
422 static int page_size;
425 if (page_size == 0) {
427 * 64 KB is the largest common page size (on ia64/PPC/ARM),
428 * but check the local page size just in case. The page_size
429 * will not change for the lifetime of this process at least.
431 page_size = LOV_MIN_STRIPE_SIZE;
432 if (getpagesize() > page_size) {
433 page_size = getpagesize();
434 llapi_err_noerrno(LLAPI_MSG_WARN,
435 "warning: page size (%u) larger than expected (%u)",
436 page_size, LOV_MIN_STRIPE_SIZE);
439 if (!llapi_stripe_size_is_aligned(param->lsp_stripe_size)) {
441 llapi_error(LLAPI_MSG_ERROR, rc,
442 "error: bad stripe_size %llu, must be an even multiple of %d bytes",
443 param->lsp_stripe_size, page_size);
446 if (!llapi_stripe_index_is_valid(param->lsp_stripe_offset)) {
448 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
449 param->lsp_stripe_offset);
452 if (llapi_stripe_size_is_too_big(param->lsp_stripe_size)) {
454 llapi_error(LLAPI_MSG_ERROR, rc,
455 "error: stripe size '%llu' over 4GB limit",
456 param->lsp_stripe_size);
460 count = param->lsp_stripe_count;
461 if (param->lsp_stripe_pattern == LOV_PATTERN_MDT) {
463 llapi_error(LLAPI_MSG_ERROR, rc,
464 "Invalid pattern: '-L mdt', must be specified "
468 if (!llapi_stripe_count_is_valid(count)) {
470 llapi_error(LLAPI_MSG_ERROR, rc,
471 "Invalid stripe count %d\n", count);
476 /* Make sure we have a good pool */
477 if (*pool_name != NULL) {
478 if (!llapi_pool_name_is_valid(pool_name)) {
480 llapi_error(LLAPI_MSG_ERROR, rc,
481 "Invalid Poolname '%s'", *pool_name);
491 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
496 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
498 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
502 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
504 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
509 if (!llapi_dir_hash_type_is_valid(hash_type)) {
511 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
519 * Trim a trailing newline from a string, if it exists.
521 int llapi_chomp_string(char *buf)
537 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
539 static int get_param_lov(const char *path, const char *param,
540 char *buf, size_t buf_size)
542 struct obd_uuid uuid;
545 rc = llapi_file_get_lov_uuid(path, &uuid);
549 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
554 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
556 static int get_param_lmv(const char *path, const char *param,
557 char *buf, size_t buf_size)
559 struct obd_uuid uuid;
562 rc = llapi_file_get_lmv_uuid(path, &uuid);
566 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
570 static int get_mds_md_size(const char *path)
572 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
575 * Rather than open the file and do the ioctl to get the
576 * instance name and close the file and search for the param
577 * file and open the param file and read the param file and
578 * parse the value and close the param file, let's just return
579 * a large enough value. It's 2020, RAM is cheap and this is
583 if (md_size < XATTR_SIZE_MAX)
584 md_size = XATTR_SIZE_MAX;
589 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
591 return get_param_lmv(path, "uuid", buf, bufsize);
595 * Open a Lustre file.
597 * \param name the name of the file to be opened
598 * \param flags access mode, see flags in open(2)
599 * \param mode permission of the file if it is created, see mode in open(2)
600 * \param param stripe pattern of the newly created file
602 * \retval file descriptor of opened file
603 * \retval negative errno on failure
605 int llapi_file_open_param(const char *name, int flags, mode_t mode,
606 const struct llapi_stripe_param *param)
608 struct lov_user_md *lum = NULL;
609 const char *pool_name = param->lsp_pool;
613 /* Check if the stripe pattern is sane. */
614 rc = llapi_stripe_param_verify(param, &pool_name);
618 if (param->lsp_is_specific)
619 lum_size = lov_user_md_size(param->lsp_stripe_count,
620 LOV_USER_MAGIC_SPECIFIC);
622 lum_size = sizeof(struct lov_user_md_v3);
624 lum_size = sizeof(*lum);
626 lum = calloc(1, lum_size);
631 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
633 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
634 flags = O_DIRECTORY | O_RDONLY;
641 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
646 /* Initialize IOCTL striping pattern structure */
647 lum->lmm_magic = LOV_USER_MAGIC_V1;
648 lum->lmm_pattern = param->lsp_stripe_pattern;
649 lum->lmm_stripe_size = param->lsp_stripe_size;
650 lum->lmm_stripe_count = param->lsp_stripe_count;
651 lum->lmm_stripe_offset = param->lsp_stripe_offset;
652 if (pool_name != NULL) {
653 struct lov_user_md_v3 *lumv3 = (void *)lum;
655 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
656 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
658 if (param->lsp_is_specific) {
659 struct lov_user_md_v3 *lumv3 = (void *)lum;
662 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
663 if (pool_name == NULL) {
665 * LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
666 * OST list, therefore if pool is not specified we have
667 * to pack a null pool name for placeholder.
669 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
672 for (i = 0; i < param->lsp_stripe_count; i++)
673 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
676 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
677 char errmsg[512] = "stripe already set";
680 if (errno != EEXIST && errno != EALREADY)
681 strncpy(errmsg, strerror(errno), sizeof(errmsg) - 1);
682 if (rc == -EREMOTEIO)
683 snprintf(errmsg, sizeof(errmsg),
684 "inactive OST among your specified %d OST(s)",
685 param->lsp_stripe_count);
687 llapi_err_noerrno(LLAPI_MSG_ERROR,
688 "setstripe error for '%s': %s", name, errmsg);
699 int llapi_file_is_encrypted(int fd)
704 rc = ioctl(fd, FS_IOC_GETFLAGS, &flags);
708 return !!(flags & LUSTRE_ENCRYPT_FL);
711 int llapi_file_open_pool(const char *name, int flags, int mode,
712 unsigned long long stripe_size, int stripe_offset,
713 int stripe_count, int stripe_pattern, char *pool_name)
715 const struct llapi_stripe_param param = {
716 .lsp_stripe_size = stripe_size,
717 .lsp_stripe_count = stripe_count,
718 .lsp_stripe_pattern = stripe_pattern,
719 .lsp_stripe_offset = stripe_offset,
720 .lsp_pool = pool_name
722 return llapi_file_open_param(name, flags, mode, ¶m);
725 int llapi_file_open(const char *name, int flags, int mode,
726 unsigned long long stripe_size, int stripe_offset,
727 int stripe_count, int stripe_pattern)
729 return llapi_file_open_pool(name, flags, mode, stripe_size,
730 stripe_offset, stripe_count,
731 stripe_pattern, NULL);
734 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
735 __u32 flags, char *foreign_lov)
738 struct lov_foreign_md *lfm;
741 if (foreign_lov == NULL) {
743 llapi_error(LLAPI_MSG_ERROR, rc,
744 "foreign LOV EA content must be provided");
748 len = strlen(foreign_lov);
749 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
752 llapi_error(LLAPI_MSG_ERROR, rc,
753 "foreign LOV EA size %zu (must be 0 < len < %zu)",
754 len, XATTR_SIZE_MAX -
755 offsetof(struct lov_foreign_md, lfm_value));
759 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
762 llapi_error(LLAPI_MSG_ERROR, rc,
763 "failed to allocate lov_foreign_md");
767 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
770 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
774 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
775 lfm->lfm_length = len;
776 lfm->lfm_type = type;
777 lfm->lfm_flags = flags;
778 memcpy(lfm->lfm_value, foreign_lov, len);
780 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
781 char *errmsg = "stripe already set";
785 errmsg = "not on a Lustre filesystem";
786 else if (errno == EEXIST || errno == EALREADY)
787 errmsg = "stripe already set";
789 errmsg = strerror(errno);
791 llapi_err_noerrno(LLAPI_MSG_ERROR,
792 "setstripe error for '%s': %s", name, errmsg);
808 int llapi_file_create(const char *name, unsigned long long stripe_size,
809 int stripe_offset, int stripe_count, int stripe_pattern)
813 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
814 stripe_offset, stripe_count, stripe_pattern,
823 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
824 int stripe_offset, int stripe_count,
825 int stripe_pattern, char *pool_name)
829 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
830 stripe_offset, stripe_count, stripe_pattern,
839 static int verify_dir_param(const char *name,
840 const struct llapi_stripe_param *param)
842 char fsname[MAX_OBD_NAME + 1] = { 0 };
843 char *pool_name = param->lsp_pool;
846 /* Make sure we are on a Lustre file system */
847 rc = llapi_search_fsname(name, fsname);
849 llapi_error(LLAPI_MSG_ERROR, rc,
850 "'%s' is not on a Lustre filesystem",
855 /* Check if the stripe pattern is sane. */
856 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
857 param->lsp_stripe_count,
858 param->lsp_stripe_pattern);
862 /* Make sure we have a good pool */
863 if (pool_name != NULL) {
865 * in case user gives the full pool name <fsname>.<poolname>,
868 char *ptr = strchr(pool_name, '.');
872 if (strcmp(pool_name, fsname) != 0) {
874 llapi_err_noerrno(LLAPI_MSG_ERROR,
875 "Pool '%s' is not on filesystem '%s'",
882 /* Make sure the pool exists and is non-empty */
883 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
885 char *err = rc == 0 ? "has no OSTs" : "does not exist";
887 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
888 fsname, pool_name, err);
893 /* sanity check of target list */
894 if (param->lsp_is_specific) {
895 char mdtname[MAX_OBD_NAME + 64];
899 for (i = 0; i < param->lsp_stripe_count; i++) {
900 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
901 fsname, param->lsp_tgts[i]);
902 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
907 llapi_error(LLAPI_MSG_ERROR, rc,
908 "%s: cannot find MDT %s in %s",
915 /* Make sure stripe offset is in MDT list. */
916 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
920 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
921 "%s: stripe offset '%d' is not in the target list",
922 __func__, param->lsp_stripe_offset);
930 static inline void param2lmu(struct lmv_user_md *lmu,
931 const struct llapi_stripe_param *param)
933 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
935 lmu->lum_stripe_count = param->lsp_stripe_count;
936 lmu->lum_stripe_offset = param->lsp_stripe_offset;
937 lmu->lum_hash_type = param->lsp_stripe_pattern;
938 lmu->lum_max_inherit = param->lsp_max_inherit;
939 lmu->lum_max_inherit_rr = param->lsp_max_inherit_rr;
940 if (param->lsp_pool != NULL)
941 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
942 if (param->lsp_is_specific) {
945 for (i = 0; i < param->lsp_stripe_count; i++)
946 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
950 int llapi_dir_set_default_lmv(const char *name,
951 const struct llapi_stripe_param *param)
953 struct lmv_user_md lmu = { 0 };
957 rc = verify_dir_param(name, param);
961 /* TODO: default lmv doesn't support specific targets yet */
962 if (param->lsp_is_specific)
965 param2lmu(&lmu, param);
967 fd = open(name, O_DIRECTORY | O_RDONLY);
970 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
974 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
976 char *errmsg = "stripe already set";
979 if (errno != EEXIST && errno != EALREADY)
980 errmsg = strerror(errno);
982 llapi_err_noerrno(LLAPI_MSG_ERROR,
983 "default dirstripe error on '%s': %s",
990 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
991 int stripe_count, int stripe_pattern,
992 const char *pool_name)
994 const struct llapi_stripe_param param = {
995 .lsp_stripe_count = stripe_count,
996 .lsp_stripe_offset = stripe_offset,
997 .lsp_stripe_pattern = stripe_pattern,
998 .lsp_pool = (char *)pool_name
1001 return llapi_dir_set_default_lmv(name, ¶m);
1005 * Create a Lustre directory.
1007 * \param name the name of the directory to be created
1008 * \param mode permission of the file if it is created, see mode in open(2)
1009 * \param param stripe pattern of the newly created directory
1011 * \retval 0 on success
1012 * \retval negative errno on failure
1014 int llapi_dir_create(const char *name, mode_t mode,
1015 const struct llapi_stripe_param *param)
1017 struct lmv_user_md *lmu = NULL;
1019 struct obd_ioctl_data data = { 0 };
1022 char *dirpath = NULL;
1023 char *namepath = NULL;
1028 rc = verify_dir_param(name, param);
1032 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1033 param->lsp_is_specific ?
1034 LMV_USER_MAGIC_SPECIFIC :
1037 lmu = calloc(1, lmu_size);
1041 dirpath = strdup(name);
1047 namepath = strdup(name);
1054 param2lmu(lmu, param);
1056 filename = basename(namepath);
1057 dir = dirname(dirpath);
1059 data.ioc_inlbuf1 = (char *)filename;
1060 data.ioc_inllen1 = strlen(filename) + 1;
1061 data.ioc_inlbuf2 = (char *)lmu;
1062 data.ioc_inllen2 = lmu_size;
1063 data.ioc_type = mode;
1064 if (param->lsp_is_create)
1065 /* borrow obdo1.o_flags to store this flag */
1066 data.ioc_obdo1.o_flags = OBD_FL_OBDMDEXISTS;
1067 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1069 llapi_error(LLAPI_MSG_ERROR, rc,
1070 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1075 fd = open(dir, O_DIRECTORY | O_RDONLY);
1078 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1082 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1083 char *errmsg = "stripe already set";
1086 if (errno != EEXIST && errno != EALREADY)
1087 errmsg = strerror(errno);
1089 llapi_err_noerrno(LLAPI_MSG_ERROR,
1090 "dirstripe error on '%s': %s", name, errmsg);
1101 * Create a foreign directory.
1103 * \param name the name of the directory to be created
1104 * \param mode permission of the file if it is created, see mode in open(2)
1105 * \param type foreign type to be set in LMV EA
1106 * \param flags foreign flags to be set in LMV EA
1107 * \param value foreign pattern to be set in LMV EA
1109 * \retval 0 on success
1110 * \retval negative errno on failure
1112 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1113 __u32 flags, const char *value)
1115 struct lmv_foreign_md *lfm = NULL;
1116 size_t lfm_size, len;
1117 struct obd_ioctl_data data = { 0 };
1120 char *dirpath = NULL;
1121 char *namepath = NULL;
1126 len = strlen(value);
1127 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1130 llapi_error(LLAPI_MSG_ERROR, rc,
1131 "invalid LOV EA length %zu (must be 0 < len < %zu)",
1132 len, XATTR_SIZE_MAX -
1133 offsetof(struct lmv_foreign_md, lfm_value));
1136 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1137 lfm = calloc(1, lfm_size);
1141 dirpath = strdup(name);
1147 namepath = strdup(name);
1154 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1155 lfm->lfm_length = len;
1156 lfm->lfm_type = type;
1157 lfm->lfm_flags = flags;
1158 memcpy(lfm->lfm_value, value, len);
1160 filename = basename(namepath);
1161 dir = dirname(dirpath);
1163 data.ioc_inlbuf1 = (char *)filename;
1164 data.ioc_inllen1 = strlen(filename) + 1;
1165 data.ioc_inlbuf2 = (char *)lfm;
1166 data.ioc_inllen2 = lfm_size;
1167 data.ioc_type = mode;
1168 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1170 llapi_error(LLAPI_MSG_ERROR, rc,
1171 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1176 fd = open(dir, O_DIRECTORY | O_RDONLY);
1179 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1183 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1184 char *errmsg = "stripe already set";
1187 if (errno != EEXIST && errno != EALREADY)
1188 errmsg = strerror(errno);
1190 llapi_err_noerrno(LLAPI_MSG_ERROR,
1191 "dirstripe error on '%s': %s", name, errmsg);
1201 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1202 int stripe_count, int stripe_pattern,
1203 const char *pool_name)
1205 const struct llapi_stripe_param param = {
1206 .lsp_stripe_count = stripe_count,
1207 .lsp_stripe_offset = stripe_offset,
1208 .lsp_stripe_pattern = stripe_pattern,
1209 .lsp_pool = (char *)pool_name
1212 return llapi_dir_create(name, mode, ¶m);
1215 static int get_file_dev(const char *path, dev_t *dev)
1222 if (statx(AT_FDCWD, path, 0, 0, &stx))
1224 *dev = makedev(stx.stx_dev_major, stx.stx_dev_minor);
1230 if (stat(path, &st) != 0)
1238 static int get_root_fd(const char *rootpath, int *outfd)
1243 fd = open(rootpath, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1246 llapi_error(LLAPI_MSG_ERROR, rc,
1247 "cannot open '%s'", rootpath);
1257 char fsname[PATH_MAX];
1258 char mnt_dir[PATH_MAX];
1259 char nid[MAX_LINE_LEN];
1260 } root_cached = { 0 };
1262 static pthread_rwlock_t root_cached_lock = PTHREAD_RWLOCK_INITIALIZER;
1264 static int get_root_path_fast(int want, char *fsname, int *outfd, char *path,
1265 dev_t *dev, char *nid)
1271 pthread_rwlock_rdlock(&root_cached_lock);
1272 if (root_cached.dev == 0)
1275 fsnamelen = strlen(root_cached.fsname);
1276 mntlen = strlen(root_cached.mnt_dir);
1278 /* Check the fsname for a match, if given */
1279 if (!(want & WANT_FSNAME) && fsname &&
1280 strlen(fsname) == fsnamelen &&
1281 (strncmp(root_cached.fsname, fsname, fsnamelen) == 0)) {
1283 /* Check the dev for a match, if given */
1284 } else if (!(want & WANT_DEV) && dev && *dev == root_cached.dev) {
1286 /* Otherwise find the longest matching path */
1287 } else if (path && strlen(path) >= mntlen &&
1288 (strncmp(root_cached.mnt_dir, path, mntlen) == 0) &&
1289 (strlen(path) == mntlen || path[mntlen] == '/')) {
1296 if ((want & WANT_FSNAME) && fsname)
1297 strcpy(fsname, root_cached.fsname);
1298 if ((want & WANT_PATH) && path)
1299 strcpy(path, root_cached.mnt_dir);
1300 if ((want & WANT_DEV) && dev)
1301 *dev = root_cached.dev;
1302 if ((want & WANT_FD) && outfd)
1303 rc = get_root_fd(root_cached.mnt_dir, outfd);
1304 if ((want & WANT_NID) && nid)
1305 strcpy(nid, root_cached.nid);
1307 pthread_rwlock_unlock(&root_cached_lock);
1311 static int get_root_path_slow(int want, char *fsname, int *outfd, char *path,
1312 int index, dev_t *dev, char *nid)
1316 char *ptr, *ptr_end;
1318 int idx = -1, mntlen = 0;
1323 /* get the mount point */
1324 fp = setmntent(PROC_MOUNTS, "r");
1327 llapi_error(LLAPI_MSG_ERROR, rc,
1328 "cannot retrieve filesystem mount point");
1331 while (getmntent_r(fp, &mnt, buf, sizeof(buf))) {
1333 if (!llapi_is_lustre_mnt(&mnt))
1337 mntlen = strlen(mnt.mnt_dir);
1338 ptr = strchr(mnt.mnt_fsname, '/');
1339 while (ptr && *ptr == '/')
1342 * thanks to the call to llapi_is_lustre_mnt() above,
1343 * we are sure that mnt.mnt_fsname contains ":/",
1344 * so ptr should never be NULL
1349 while (*ptr_end != '/' && *ptr_end != '\0')
1352 fsnamelen = ptr_end - ptr;
1354 /* ignore unaccessible filesystem */
1355 if (get_file_dev(mnt.mnt_dir, &devmnt))
1358 if ((want & WANT_INDEX) && idx == index) {
1363 /* Check the fsname for a match, if given */
1364 if (!(want & WANT_FSNAME) && fsname &&
1365 strlen(fsname) == fsnamelen &&
1366 (strncmp(ptr, fsname, fsnamelen) == 0)) {
1371 /* Check the dev for a match, if given */
1372 if (!(want & WANT_DEV) && dev && *dev == devmnt) {
1377 /* Otherwise find the longest matching path */
1378 if (path && strlen(path) >= mntlen &&
1379 (strncmp(mnt.mnt_dir, path, mntlen) == 0) &&
1380 (strlen(path) == mntlen || path[mntlen] == '/')) {
1390 if (!(want & WANT_INDEX)) {
1391 /* Cache the mount point information */
1392 pthread_rwlock_wrlock(&root_cached_lock);
1394 strncpy(root_cached.fsname, ptr, fsnamelen);
1395 root_cached.fsname[fsnamelen] = '\0';
1396 strncpy(root_cached.mnt_dir, mnt.mnt_dir, mntlen);
1397 root_cached.mnt_dir[mntlen] = '\0';
1398 root_cached.dev = devmnt;
1399 ptr_end = strchr(mnt.mnt_fsname, ':');
1400 strncpy(root_cached.nid, mnt.mnt_fsname,
1401 ptr_end - mnt.mnt_fsname);
1402 root_cached.nid[ptr_end - mnt.mnt_fsname] = '\0';
1404 pthread_rwlock_unlock(&root_cached_lock);
1407 if ((want & WANT_FSNAME) && fsname) {
1408 strncpy(fsname, ptr, fsnamelen);
1409 fsname[fsnamelen] = '\0';
1411 if ((want & WANT_PATH) && path) {
1412 strncpy(path, mnt.mnt_dir, mntlen);
1413 path[mntlen] = '\0';
1415 if ((want & WANT_DEV) && dev)
1417 if ((want & WANT_FD) && outfd)
1418 rc = get_root_fd(mnt.mnt_dir, outfd);
1419 if ((want & WANT_NID) && nid) {
1420 ptr_end = strchr(mnt.mnt_fsname, ':');
1421 strncpy(nid, mnt.mnt_fsname, ptr_end - mnt.mnt_fsname);
1422 nid[ptr_end - mnt.mnt_fsname] = '\0';
1431 * Find the fsname, the full path, and/or an open fd.
1432 * Either the fsname or path must not be NULL
1434 int get_root_path(int want, char *fsname, int *outfd, char *path, int index,
1435 dev_t *dev, char *nid)
1439 if (!(want & WANT_INDEX))
1440 rc = get_root_path_fast(want, fsname, outfd, path, dev, nid);
1442 rc = get_root_path_slow(want, fsname, outfd, path, index, dev,
1445 if (!rc || !(want & WANT_ERROR))
1448 if (dev || !(want & WANT_DEV))
1449 llapi_err_noerrno(LLAPI_MSG_ERROR,
1450 "'%u/%u' dev not on a mounted Lustre filesystem",
1451 major(*dev), minor(*dev));
1453 llapi_err_noerrno(LLAPI_MSG_ERROR,
1454 "'%s' not on a mounted Lustre filesystem",
1455 (want & WANT_PATH) ? fsname : path);
1460 * search lustre mounts
1462 * Calling this function will return to the user the mount point, mntdir, and
1463 * the file system name, fsname, if the user passed a buffer to this routine.
1465 * The user inputs are pathname and index. If the pathname is supplied then
1466 * the value of the index will be ignored. The pathname will return data if
1467 * the pathname is located on a lustre mount. Index is used to pick which
1468 * mount point you want in the case of multiple mounted lustre file systems.
1469 * See function lfs_osts in lfs.c for an example of the index use.
1471 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1474 int want = WANT_PATH, idx = -1;
1476 if (!pathname || pathname[0] == '\0') {
1480 strcpy(mntdir, pathname);
1484 want |= WANT_FSNAME;
1485 return get_root_path(want, fsname, NULL, mntdir, idx, NULL, NULL);
1488 /* Given a path, find the corresponding Lustre fsname */
1489 int llapi_search_fsname(const char *pathname, char *fsname)
1494 rc = get_file_dev(pathname, &dev);
1500 /* file does not exist try the parent */
1501 len = readlink(pathname, tmp, PATH_MAX);
1505 strncpy(tmp, pathname, PATH_MAX - 1);
1507 parent = dirname(tmp);
1508 rc = get_file_dev(parent, &dev);
1512 llapi_error(LLAPI_MSG_ERROR, rc,
1513 "cannot resolve path '%s'", pathname);
1517 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, NULL, -1,
1523 int llapi_search_rootpath(char *pathname, const char *fsname)
1529 * pathname can be used as an argument by get_root_path(),
1530 * clear it for safety
1533 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1,
1537 int llapi_search_rootpath_by_dev(char *pathname, dev_t dev)
1543 * pathname can be used as an argument by get_root_path(),
1544 * clear it for safety
1547 return get_root_path(WANT_PATH, NULL, NULL, pathname, -1, &dev, NULL);
1551 * Get the list of pool members.
1552 * \param poolname string of format \<fsname\>.\<poolname\>
1553 * \param members caller-allocated array of char*
1554 * \param list_size size of the members array
1555 * \param buffer caller-allocated buffer for storing OST names
1556 * \param buffer_size size of the buffer
1558 * \return number of members retrieved for this pool
1559 * \retval -error failure
1561 int llapi_get_poolmembers(const char *poolname, char **members,
1562 int list_size, char *buffer, int buffer_size)
1564 char fsname[PATH_MAX];
1573 /* name is FSNAME.POOLNAME */
1574 if (strlen(poolname) >= sizeof(fsname))
1577 snprintf(fsname, sizeof(fsname), "%s", poolname);
1578 pool = strchr(fsname, '.');
1585 rc = poolpath(&pathname, fsname, NULL);
1587 llapi_error(LLAPI_MSG_ERROR, rc,
1588 "Lustre filesystem '%s' not found",
1593 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1594 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1595 cfs_free_param_data(&pathname);
1596 if (rc >= sizeof(buf))
1598 fd = fopen(buf, "r");
1601 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1606 while (fgets(buf, sizeof(buf), fd) != NULL) {
1607 if (nb_entries >= list_size) {
1611 buf[sizeof(buf) - 1] = '\0';
1613 tmp = strchr(buf, '\n');
1616 if (used + strlen(buf) + 1 > buffer_size) {
1621 strcpy(buffer + used, buf);
1622 members[nb_entries] = buffer + used;
1623 used += strlen(buf) + 1;
1633 * Get the list of pools in a filesystem.
1634 * \param name filesystem name or path
1635 * \param poollist caller-allocated array of char*
1636 * \param list_size size of the poollist array
1637 * \param buffer caller-allocated buffer for storing pool names
1638 * \param buffer_size size of the buffer
1640 * \return number of pools retrieved for this filesystem
1641 * \retval -error failure
1643 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1644 char *buffer, int buffer_size)
1650 struct dirent *pool;
1652 unsigned int nb_entries = 0;
1653 unsigned int used = 0;
1656 /* initialize output array */
1657 for (i = 0; i < list_size; i++)
1660 /* is name a pathname ? */
1661 ptr = strchr(name, '/');
1663 char fsname_buf[MAXNAMLEN];
1665 /* We will need fsname for printing later */
1666 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1670 ptr = strrchr(fsname_buf, '-');
1674 fsname = strdup(fsname_buf);
1678 /* name is FSNAME */
1679 fsname = strdup(name);
1684 rc = poolpath(&pathname, fsname, NULL);
1686 llapi_error(LLAPI_MSG_ERROR, rc,
1687 "Lustre filesystem '%s' not found", name);
1691 dir = opendir(pathname.gl_pathv[0]);
1694 llapi_error(LLAPI_MSG_ERROR, rc,
1695 "Could not open pool list for '%s'",
1702 pool = readdir(dir);
1708 /* ignore . and .. */
1709 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1712 /* check output bounds */
1713 if (nb_entries >= list_size) {
1715 goto free_dir_no_msg;
1718 /* +2 for '.' and final '\0' */
1719 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1722 goto free_dir_no_msg;
1725 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1726 poollist[nb_entries] = buffer + used;
1727 used += strlen(pool->d_name) + strlen(fsname) + 2;
1733 llapi_error(LLAPI_MSG_ERROR, rc,
1734 "Error reading pool list for '%s'", name);
1736 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1741 cfs_free_param_data(&pathname);
1744 return rc != 0 ? rc : nb_entries;
1747 /* wrapper for lfs.c and obd.c */
1748 int llapi_poollist(const char *name)
1750 int poolcount, rc, i;
1753 rc = llapi_get_poolbuf(name, &buf, &pools, &poolcount);
1757 for (i = 0; i < poolcount; i++)
1758 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", pools[i]);
1765 * Get buffer that holds uuids and the list of pools in a filesystem.
1767 * \param name filesystem name or path
1768 * \param buf bufffer that has to be freed if function returns 0
1769 * \param pools pointer to the list of pools in buffer
1770 * \param poolcount number of pools
1772 * \return 0 when found at least 1 pool, i.e. poolcount > 0
1773 * \retval -error failure
1775 int llapi_get_poolbuf(const char *name, char **buf,
1776 char ***pools, int *poolcount)
1779 * list of pool names (assume that pool count is smaller
1782 char **list, *buffer = NULL, *fsname = (char *)name;
1783 char *poolname = NULL, *tmp = NULL, data[16];
1784 enum param_filter type = FILTER_BY_PATH;
1785 int obdcount, bufsize, rc, nb;
1790 if (name[0] != '/') {
1791 fsname = strdup(name);
1795 poolname = strchr(fsname, '.');
1798 type = FILTER_BY_FS_NAME;
1801 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1802 data, sizeof(data));
1805 obdcount = atoi(data);
1808 * Allocate space for each fsname-OST0000_UUID, 1 per OST,
1809 * and also an array to store the pointers for all that
1813 bufsize = sizeof(struct obd_uuid) * obdcount;
1814 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1815 if (buffer == NULL) {
1819 list = (char **) (buffer + bufsize);
1822 /* name is a path or fsname */
1823 nb = llapi_get_poollist(name, list, obdcount,
1826 /* name is a pool name (<fsname>.<poolname>) */
1827 nb = llapi_get_poolmembers(name, list, obdcount,
1831 if (nb == -EOVERFLOW) {
1834 goto retry_get_pools;
1837 rc = (nb < 0 ? nb : 0);
1844 /* Don't free buffer, it will be used later */
1847 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1852 typedef int (semantic_func_t)(char *path, int p, int *d,
1853 void *data, struct dirent64 *de);
1855 #define OBD_NOT_FOUND (-1)
1857 static bool lmv_is_foreign(__u32 magic)
1859 return magic == LMV_MAGIC_FOREIGN;
1862 static void find_param_fini(struct find_param *param)
1864 if (param->fp_migrate)
1867 if (param->fp_obd_indexes) {
1868 free(param->fp_obd_indexes);
1869 param->fp_obd_indexes = NULL;
1872 if (param->fp_lmd) {
1873 free(param->fp_lmd);
1874 param->fp_lmd = NULL;
1877 if (param->fp_lmv_md) {
1878 free(param->fp_lmv_md);
1879 param->fp_lmv_md = NULL;
1883 static int common_param_init(struct find_param *param, char *path)
1885 int lum_size = get_mds_md_size(path);
1890 /* migrate has fp_lmv_md initialized outside */
1891 if (param->fp_migrate)
1894 if (lum_size < PATH_MAX + 1)
1895 lum_size = PATH_MAX + 1;
1897 param->fp_lum_size = lum_size;
1898 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1900 if (param->fp_lmd == NULL) {
1901 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1902 "error: allocate %zu bytes for layout failed",
1903 sizeof(lstat_t) + param->fp_lum_size);
1907 param->fp_lmv_stripe_count = 256;
1908 param->fp_lmv_md = calloc(1,
1909 lmv_user_md_size(param->fp_lmv_stripe_count,
1910 LMV_USER_MAGIC_SPECIFIC));
1911 if (param->fp_lmv_md == NULL) {
1912 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1913 "error: allocation of %d bytes for ioctl",
1914 lmv_user_md_size(param->fp_lmv_stripe_count,
1915 LMV_USER_MAGIC_SPECIFIC));
1916 find_param_fini(param);
1920 param->fp_got_uuids = 0;
1921 param->fp_obd_indexes = NULL;
1922 param->fp_obd_index = OBD_NOT_FOUND;
1923 param->fp_mdt_index = OBD_NOT_FOUND;
1927 static int cb_common_fini(char *path, int p, int *dp, void *data,
1928 struct dirent64 *de)
1930 struct find_param *param = data;
1936 /* set errno upon failure */
1937 static int open_parent(const char *path)
1943 path_copy = strdup(path);
1944 if (path_copy == NULL)
1947 parent_path = dirname(path_copy);
1948 parent = open(parent_path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1954 static int cb_get_dirstripe(char *path, int *d, struct find_param *param)
1957 bool did_nofollow = false;
1962 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1963 if (param->fp_get_default_lmv) {
1967 /* open() may not fetch LOOKUP lock, statx() to ensure dir depth
1970 statx(*d, "", AT_EMPTY_PATH, STATX_MODE, &stx);
1976 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1978 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1981 ret = ioctl(*d, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1983 /* if ENOTTY likely to be a fake symlink, so try again after
1984 * new open() with O_NOFOLLOW, but only once to prevent any
1985 * loop like for the path of a file/dir not on Lustre !!
1987 if (ret < 0 && errno == ENOTTY && !did_nofollow) {
1990 did_nofollow = true;
1991 fd = open(path, O_RDONLY | O_NOFOLLOW);
1993 /* restore original errno */
1998 /* close original fd and set new */
2001 ret2 = ioctl(fd, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
2002 if (ret2 < 0 && errno != E2BIG) {
2003 /* restore original errno */
2007 /* LMV is ok or need to handle E2BIG case now */
2011 if (errno == E2BIG && ret != 0) {
2015 /* if foreign LMV case, fake stripes number */
2016 if (lmv_is_foreign(param->fp_lmv_md->lum_magic)) {
2017 struct lmv_foreign_md *lfm;
2019 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
2020 if (lfm->lfm_length < XATTR_SIZE_MAX -
2021 offsetof(typeof(*lfm), lfm_value)) {
2022 uint32_t size = lfm->lfm_length +
2023 offsetof(typeof(*lfm), lfm_value);
2025 stripe_count = lmv_foreign_to_md_stripes(size);
2027 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
2028 "error: invalid %d foreign size returned from ioctl",
2033 stripe_count = param->fp_lmv_md->lum_stripe_count;
2035 if (stripe_count <= param->fp_lmv_stripe_count)
2038 free(param->fp_lmv_md);
2039 param->fp_lmv_stripe_count = stripe_count;
2040 lmv_size = lmv_user_md_size(stripe_count,
2041 LMV_USER_MAGIC_SPECIFIC);
2042 param->fp_lmv_md = malloc(lmv_size);
2043 if (param->fp_lmv_md == NULL) {
2044 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
2045 "error: allocation of %d bytes for ioctl",
2046 lmv_user_md_size(param->fp_lmv_stripe_count,
2047 LMV_USER_MAGIC_SPECIFIC));
2056 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
2059 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
2060 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
2061 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
2062 lmd_v2->lmd_stx.stx_uid = st->st_uid;
2063 lmd_v2->lmd_stx.stx_gid = st->st_gid;
2064 lmd_v2->lmd_stx.stx_mode = st->st_mode;
2065 lmd_v2->lmd_stx.stx_ino = st->st_ino;
2066 lmd_v2->lmd_stx.stx_size = st->st_size;
2067 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
2068 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
2069 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
2070 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
2071 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
2072 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
2073 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
2074 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
2075 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
2077 lmd_v2->lmd_flags = 0;
2079 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
2081 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
2082 if (lmd_v2->lmd_stx.stx_size)
2083 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
2084 if (lmd_v2->lmd_stx.stx_blocks)
2085 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
2087 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
2088 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
2089 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
2094 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
2096 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
2097 struct lov_user_mds_data *lmd_v2 = lmdbuf;
2101 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
2105 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
2108 st = lmd_v1->lmd_st;
2109 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
2110 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
2111 convert_lmd_statx(lmd_v2, &st, false);
2112 lmd_v2->lmd_lmmsize = 0;
2113 lmd_v2->lmd_padding = 0;
2118 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
2119 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
2121 struct lov_user_mds_data *lmd = lmdbuf;
2122 static bool use_old_ioctl;
2126 if (parent_fd < 0 && dir_fd < 0)
2128 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
2133 * LL_IOC_MDC_GETINFO operates on the current directory inode
2134 * and returns struct lov_user_mds_data, while
2135 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
2137 if (type == GET_LMD_INFO)
2138 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_V1 :
2139 LL_IOC_MDC_GETINFO_V2;
2141 cmd = LL_IOC_LOV_GETSTRIPE;
2144 ret = ioctl(dir_fd, cmd, lmdbuf);
2145 if (ret < 0 && errno == ENOTTY &&
2146 cmd == LL_IOC_MDC_GETINFO_V2) {
2147 cmd = LL_IOC_MDC_GETINFO_V1;
2148 use_old_ioctl = true;
2152 if (cmd == LL_IOC_MDC_GETINFO_V1 && !ret)
2153 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2155 if (ret < 0 && errno == ENOTTY && type == GET_LMD_STRIPE) {
2158 /* retry ioctl() after new open() with O_NOFOLLOW
2159 * just in case it could be a fake symlink
2160 * need using a new open() as dir_fd is being closed
2164 dir_fd2 = open(path, O_RDONLY | O_NDELAY | O_NOFOLLOW);
2166 /* return original error */
2169 ret = ioctl(dir_fd2, cmd, lmdbuf);
2170 /* pass new errno or success back to caller */
2176 } else if (parent_fd >= 0) {
2177 const char *fname = strrchr(path, '/');
2180 * IOC_MDC_GETFILEINFO takes as input the filename (relative to
2181 * the parent directory) and returns struct lov_user_mds_data,
2182 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2184 * This avoids opening, locking, and closing each file on the
2185 * client if that is not needed. Multiple of these ioctl() can
2186 * be done on the parent dir with a single open for all
2187 * files in that directory, and it also doesn't pollute the
2188 * client dcache with millions of dentries when traversing
2189 * a large filesystem.
2191 fname = (fname == NULL ? path : fname + 1);
2193 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2196 else if (ret >= lmdlen || ret++ == 0)
2199 if (type == GET_LMD_INFO)
2200 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_V1 :
2201 IOC_MDC_GETFILEINFO_V2;
2203 cmd = IOC_MDC_GETFILESTRIPE;
2206 ret = ioctl(parent_fd, cmd, lmdbuf);
2207 if (ret < 0 && errno == ENOTTY &&
2208 cmd == IOC_MDC_GETFILEINFO_V2) {
2209 cmd = IOC_MDC_GETFILEINFO_V1;
2210 use_old_ioctl = true;
2211 goto retry_getfileinfo;
2214 if (cmd == IOC_MDC_GETFILEINFO_V1 && !ret)
2215 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2219 if (ret && type == GET_LMD_INFO) {
2220 if (errno == ENOTTY) {
2224 * ioctl is not supported, it is not a lustre fs.
2225 * Do the regular lstat(2) instead.
2227 ret = lstat_f(path, &st);
2230 llapi_error(LLAPI_MSG_ERROR, ret,
2231 "error: %s: lstat failed for %s",
2235 convert_lmd_statx(lmd, &st, true);
2237 * It may be wrong to set use_old_ioctl with true as
2238 * the file is not a lustre fs. So reset it with false
2241 use_old_ioctl = false;
2242 } else if (errno == ENOENT) {
2244 llapi_error(LLAPI_MSG_WARN, ret,
2245 "warning: %s does not exist", path);
2246 } else if (errno != EISDIR && errno != ENODATA) {
2248 llapi_error(LLAPI_MSG_ERROR, ret,
2249 "%s ioctl failed for %s.",
2250 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2251 "IOC_MDC_GETFILEINFO", path);
2258 static int llapi_semantic_traverse(char *path, int size, int parent,
2259 semantic_func_t sem_init,
2260 semantic_func_t sem_fini, void *data,
2261 struct dirent64 *de)
2263 struct find_param *param = (struct find_param *)data;
2264 struct dirent64 *dent;
2265 int len, ret, d, p = -1;
2271 d = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
2272 /* if an invalid fake dir symlink, opendir() will return EINVAL
2273 * instead of ENOTDIR. If a valid but dangling faked or real file/dir
2274 * symlink ENOENT will be returned. For a valid/resolved fake or real
2275 * file symlink ENOTDIR will be returned as for a regular file.
2276 * opendir() will be successful for a valid and resolved fake or real
2277 * dir simlink or a regular dir.
2279 if (d == -1 && errno != ENOTDIR && errno != EINVAL && errno != ENOENT) {
2281 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2284 } else if (d == -1) {
2285 if (errno == ENOENT || errno == EINVAL) {
2286 int old_errno = errno;
2288 /* try to open with O_NOFOLLOW this will help
2289 * differentiate fake vs real symlinks
2290 * it is ok to not use O_DIRECTORY with O_RDONLY
2291 * and it will prevent the need to deal with ENOTDIR
2292 * error, instead of ELOOP, being returned by recent
2293 * kernels for real symlinks
2295 d = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2296 /* if a dangling real symlink should return ELOOP, or
2297 * again ENOENT if really non-existing path, or E...??
2298 * So return original error. If success or ENOTDIR, path
2299 * is likely to be a fake dir/file symlink, so continue
2309 if (parent == -1 && d == -1) {
2310 /* Open the parent dir. */
2311 p = open_parent(path);
2317 } else { /* d != -1 */
2320 /* try to reopen dir with O_NOFOLLOW just in case of a foreign
2323 d2 = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2328 /* continue with d */
2334 ret = sem_init(path, (parent != -1) ? parent : p, &d, data, de);
2344 /* ENOTDIR if fake symlink, do not consider it as an error */
2345 if (errno != ENOTDIR)
2346 llapi_error(LLAPI_MSG_ERROR, errno,
2347 "fdopendir() failed");
2354 while ((dent = readdir64(dir)) != NULL) {
2357 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2361 if ((len + dent->d_reclen + 2) > size) {
2362 llapi_err_noerrno(LLAPI_MSG_ERROR,
2363 "error: %s: string buffer too small for %s",
2368 strcat(path, dent->d_name);
2370 if (dent->d_type == DT_UNKNOWN) {
2371 struct lov_user_mds_data *lmd = param->fp_lmd;
2373 rc = get_lmd_info_fd(path, d, -1, param->fp_lmd,
2374 param->fp_lum_size, GET_LMD_INFO);
2376 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2383 switch (dent->d_type) {
2385 llapi_err_noerrno(LLAPI_MSG_ERROR,
2386 "error: %s: '%s' is UNKNOWN type %d",
2387 __func__, dent->d_name, dent->d_type);
2390 rc = llapi_semantic_traverse(path, size, d, sem_init,
2391 sem_fini, data, dent);
2392 if (rc != 0 && ret == 0)
2394 if (rc < 0 && rc != -EALREADY &&
2395 param->fp_stop_on_error)
2401 rc = sem_init(path, d, NULL, data, dent);
2402 if (rc < 0 && ret == 0) {
2404 if (rc && rc != -EALREADY &&
2405 param->fp_stop_on_error)
2410 if (sem_fini && rc == 0)
2411 sem_fini(path, d, NULL, data, dent);
2419 sem_fini(path, parent, &d, data, de);
2432 static int param_callback(char *path, semantic_func_t sem_init,
2433 semantic_func_t sem_fini, struct find_param *param)
2435 int ret, len = strlen(path);
2438 if (len > PATH_MAX) {
2440 llapi_error(LLAPI_MSG_ERROR, ret,
2441 "Path name '%s' is too long", path);
2445 buf = (char *)malloc(2 * PATH_MAX);
2449 snprintf(buf, PATH_MAX + 1, "%s", path);
2450 ret = common_param_init(param, buf);
2454 param->fp_depth = 0;
2456 ret = llapi_semantic_traverse(buf, 2 * PATH_MAX, -1, sem_init,
2457 sem_fini, param, NULL);
2459 find_param_fini(param);
2461 return ret < 0 ? ret : 0;
2464 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2468 rc = ioctl(fd, OBD_IOC_GETDTNAME, lov_name);
2469 if (rc && errno == ENOTTY)
2470 rc = ioctl(fd, OBD_IOC_GETNAME_OLD, lov_name);
2473 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2479 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2483 rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2486 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2492 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2496 /* do not follow faked symlinks */
2497 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
2499 /* real symlink should have failed with ELOOP so retry without
2500 * O_NOFOLLOW just in case
2502 fd = open(path, O_RDONLY | O_NONBLOCK);
2505 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'",
2511 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2517 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2521 fd = open(path, O_RDONLY | O_NONBLOCK);
2524 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2528 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2540 * If uuidp is NULL, return the number of available obd uuids.
2541 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2542 * there are more OSTs than allocated to uuidp, then an error is returned with
2543 * the ost_count set to number of available obd uuids.
2545 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2546 int *ost_count, enum tgt_type type)
2548 char buf[PATH_MAX], format[32];
2549 int rc = 0, index = 0;
2550 struct obd_uuid name;
2554 /* Get the lov name */
2555 if (type == LOV_TYPE)
2556 rc = llapi_file_fget_lov_uuid(fd, &name);
2558 rc = llapi_file_fget_lmv_uuid(fd, &name);
2562 /* Now get the ost uuids */
2563 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2564 FILTER_BY_EXACT, "target_obd", ¶m);
2568 fp = fopen(param.gl_pathv[0], "r");
2571 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2576 snprintf(format, sizeof(format),
2577 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2578 while (fgets(buf, sizeof(buf), fp) != NULL) {
2579 if (uuidp && (index < *ost_count)) {
2580 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2588 if (uuidp && (index > *ost_count))
2593 cfs_free_param_data(¶m);
2597 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2599 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2602 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2607 root = open(mnt, O_RDONLY | O_DIRECTORY);
2610 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2615 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2624 * Check if user specified value matches a real uuid. Ignore _UUID,
2625 * -osc-4ba41334, other trailing gunk in comparison.
2626 * @param real_uuid ends in "_UUID"
2627 * @param search_uuid may or may not end in "_UUID"
2629 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2631 int cmplen = strlen(real_uuid);
2632 int searchlen = strlen(search_uuid);
2634 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2636 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2640 * The UUIDs may legitimately be different lengths, if
2641 * the system was upgraded from an older version.
2643 if (cmplen != searchlen)
2646 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2650 * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2651 * returned in param->fp_obd_index
2653 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2655 struct obd_uuid obd_uuid;
2662 if (param->fp_got_uuids)
2665 /* Get the lov/lmv name */
2666 if (param->fp_get_lmv)
2667 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2669 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2671 if (rc != -ENOTTY) {
2672 llapi_error(LLAPI_MSG_ERROR, rc,
2673 "error: can't get %s name: %s",
2674 param->fp_get_lmv ? "lmv" : "lov",
2682 param->fp_got_uuids = 1;
2684 /* Now get the ost uuids */
2685 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2686 obd_uuid.uuid, FILTER_BY_EXACT,
2687 "target_obd", ¶m_data);
2691 fp = fopen(param_data.gl_pathv[0], "r");
2694 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2695 param_data.gl_pathv[0]);
2699 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2700 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2701 param->fp_get_lmv ? "MDTS" : "OBDS");
2703 snprintf(format, sizeof(format),
2704 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2705 while (fgets(buf, sizeof(buf), fp) != NULL) {
2708 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2711 if (param->fp_obd_uuid) {
2712 if (llapi_uuid_match(obd_uuid.uuid,
2713 param->fp_obd_uuid->uuid)) {
2714 param->fp_obd_index = index;
2717 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2718 /* Print everything */
2719 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2722 param->fp_obds_printed = 1;
2726 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2727 llapi_err_noerrno(LLAPI_MSG_ERROR,
2728 "error: %s: unknown obduuid: %s",
2729 __func__, param->fp_obd_uuid->uuid);
2733 cfs_free_param_data(¶m_data);
2738 * In this case, param->fp_obd_uuid will be an array of obduuids and
2739 * obd index for all these obduuids will be returned in
2740 * param->fp_obd_indexes
2742 static int setup_indexes(int d, char *path, struct obd_uuid *obduuids,
2743 int num_obds, int **obdindexes, int *obdindex,
2746 int ret, obdcount, obd_valid = 0, obdnum;
2748 struct obd_uuid *uuids = NULL;
2752 if (type == LOV_TYPE)
2753 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2755 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2759 obdcount = atoi(buf);
2760 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2765 ret = llapi_get_target_uuids(d, uuids, &obdcount, type);
2767 if (ret == -EOVERFLOW) {
2768 struct obd_uuid *uuids_temp;
2770 uuids_temp = realloc(uuids, obdcount *
2771 sizeof(struct obd_uuid));
2772 if (uuids_temp != NULL) {
2774 goto retry_get_uuids;
2779 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2783 indexes = malloc(num_obds * sizeof(*obdindex));
2784 if (indexes == NULL) {
2789 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2792 /* The user may have specified a simple index */
2793 i = strtol(obduuids[obdnum].uuid, &end, 0);
2794 if (end && *end == '\0' && i < obdcount) {
2795 indexes[obdnum] = i;
2798 for (i = 0; i < obdcount; i++) {
2799 if (llapi_uuid_match(uuids[i].uuid,
2800 obduuids[obdnum].uuid)) {
2801 indexes[obdnum] = i;
2807 if (i >= obdcount) {
2808 indexes[obdnum] = OBD_NOT_FOUND;
2809 llapi_err_noerrno(LLAPI_MSG_ERROR,
2810 "invalid obduuid '%s'",
2811 obduuids[obdnum].uuid);
2817 *obdindex = OBD_NOT_FOUND;
2819 *obdindex = obd_valid;
2821 *obdindexes = indexes;
2829 static int setup_target_indexes(int d, char *path, struct find_param *param)
2833 if (param->fp_mdt_uuid) {
2834 ret = setup_indexes(d, path, param->fp_mdt_uuid,
2836 ¶m->fp_mdt_indexes,
2837 ¶m->fp_mdt_index, LMV_TYPE);
2842 if (param->fp_obd_uuid) {
2843 ret = setup_indexes(d, path, param->fp_obd_uuid,
2845 ¶m->fp_obd_indexes,
2846 ¶m->fp_obd_index, LOV_TYPE);
2851 param->fp_got_uuids = 1;
2856 int llapi_ostlist(char *path, struct find_param *param)
2861 fd = open(path, O_RDONLY | O_DIRECTORY);
2865 ret = setup_obd_uuid(fd, path, param);
2872 * Tries to determine the default stripe attributes for a given filesystem. The
2873 * filesystem to check should be specified by fsname, or will be determined
2876 static int sattr_get_defaults(const char *const fsname,
2877 unsigned int *scount,
2878 unsigned int *ssize,
2879 unsigned int *soffset)
2885 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2886 "stripecount", val, sizeof(val));
2889 *scount = atoi(val);
2893 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2894 "stripesize", val, sizeof(val));
2901 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2902 "stripeoffset", val, sizeof(val));
2905 *soffset = atoi(val);
2912 * Tries to gather the default stripe attributes for a given filesystem. If
2913 * the attributes can be determined, they are cached for easy retreival the
2914 * next time they are needed. Only a single filesystem's attributes are
2917 int sattr_cache_get_defaults(const char *const fsname,
2918 const char *const pathname, unsigned int *scount,
2919 unsigned int *ssize, unsigned int *soffset)
2922 char fsname[PATH_MAX + 1];
2923 unsigned int stripecount;
2924 unsigned int stripesize;
2925 unsigned int stripeoffset;
2931 char fsname_buf[PATH_MAX + 1];
2932 unsigned int tmp[3];
2934 if (fsname == NULL) {
2935 rc = llapi_search_fsname(pathname, fsname_buf);
2939 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2942 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2944 * Ensure all 3 sattrs (count, size, and offset) are
2945 * successfully retrieved and stored in tmp before writing to
2948 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2952 cache.stripecount = tmp[0];
2953 cache.stripesize = tmp[1];
2954 cache.stripeoffset = tmp[2];
2955 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2959 *scount = cache.stripecount;
2961 *ssize = cache.stripesize;
2963 *soffset = cache.stripeoffset;
2968 static char *layout2name(__u32 layout_pattern)
2970 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2972 else if (layout_pattern == LOV_PATTERN_MDT)
2974 else if (layout_pattern == LOV_PATTERN_RAID0)
2976 else if (layout_pattern ==
2977 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2978 return "raid0,overstriped";
2983 enum lov_dump_flags {
2984 LDF_IS_DIR = 0x0001,
2985 LDF_IS_RAW = 0x0002,
2986 LDF_INDENT = 0x0004,
2987 LDF_SKIP_OBJS = 0x0008,
2989 LDF_EXTENSION = 0x0020,
2992 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2993 struct lov_user_ost_data_v1 *objects,
2994 enum llapi_layout_verbose verbose,
2995 int depth, char *pool_name,
2996 enum lov_dump_flags flags)
2998 bool is_dir = flags & LDF_IS_DIR;
2999 bool is_raw = flags & LDF_IS_RAW;
3000 bool indent = flags & LDF_INDENT;
3001 bool yaml = flags & LDF_YAML;
3002 bool skip_objs = flags & LDF_SKIP_OBJS;
3003 bool extension = flags & LDF_EXTENSION;
3004 char *prefix = is_dir ? "" : "lmm_";
3005 char *separator = "";
3006 char *space = indent ? " " : "";
3009 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
3010 lmm_oi_set_seq(&lum->lmm_oi, 0);
3011 if (!indent && (verbose & VERBOSE_DETAIL))
3012 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
3015 if (!yaml && !indent && depth && path &&
3016 ((verbose != VERBOSE_OBJID) || !is_dir))
3017 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3019 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
3020 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
3021 space, prefix, lum->lmm_magic);
3022 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
3024 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
3025 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
3027 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
3030 if (verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) {
3035 if (verbose & ~VERBOSE_DFID)
3036 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
3040 struct lu_fid dir_fid;
3042 rc = llapi_path2fid(path, &dir_fid);
3044 llapi_error(LLAPI_MSG_ERROR, rc,
3045 "Cannot determine directory fid.");
3047 seq = dir_fid.f_seq;
3048 oid = dir_fid.f_oid;
3049 ver = dir_fid.f_ver;
3052 * This needs a bit of hand-holding since old 1.x
3053 * lmm_oi have { oi.oi_id = mds_inum, oi.oi_seq = 0 }
3054 * and 2.x lmm_oi have { oi.oi_id = mds_oid,
3055 * oi.oi_seq = mds_seq } instead of a real FID.
3056 * Ideally the 2.x code would have stored this like a
3057 * FID with { oi_id = mds_seq, oi_seq = mds_oid } so
3058 * the ostid union lu_fid { f_seq = mds_seq,
3059 * f_oid = mds_oid } worked properly (especially since
3060 * IGIF FIDs use mds_inum as the FID SEQ), but
3061 * unfortunately that didn't happen.
3063 * Print it to look like an IGIF FID, even though the
3064 * fields are reversed on disk, so that it makes sense
3067 * Don't use ostid_id() and ostid_seq(), since they
3068 * assume the oi_fid fields are in the right order.
3069 * This is why there are separate lmm_oi_seq() and
3070 * lmm_oi_id() routines for this.
3072 * For newer layout types hopefully this will be a
3075 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
3076 lmm_oi_id(&lum->lmm_oi) :
3077 lmm_oi_seq(&lum->lmm_oi);
3078 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
3079 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
3080 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
3084 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
3085 (unsigned long long)seq, oid, ver);
3087 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
3088 (unsigned long long)seq, oid, ver);
3091 if (verbose & VERBOSE_STRIPE_COUNT) {
3092 if (verbose & ~VERBOSE_STRIPE_COUNT)
3093 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
3096 if (!is_raw && lum->lmm_stripe_count == 0 &&
3097 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
3098 unsigned int scount;
3100 rc = sattr_cache_get_defaults(NULL, path,
3104 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3107 llapi_error(LLAPI_MSG_ERROR, rc,
3108 "Cannot determine default stripe count.");
3110 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3112 (__s16)lum->lmm_stripe_count);
3115 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
3117 (__s16)lum->lmm_stripe_count);
3119 if (!yaml && is_dir)
3125 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
3126 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
3127 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3128 if (verbose & ~VERBOSE_EXT_SIZE && extension)
3129 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
3131 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
3132 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
3134 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
3137 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
3140 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
3142 llapi_error(LLAPI_MSG_ERROR, rc,
3143 "Cannot determine default stripe size.");
3145 /* Extension size is in KiB */
3146 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3148 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
3149 (unsigned long long)lum->lmm_stripe_size);
3151 if (!yaml && is_dir)
3157 if ((verbose & VERBOSE_PATTERN)) {
3158 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3159 if (verbose & ~VERBOSE_PATTERN)
3160 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
3162 if (lov_pattern_supported(lum->lmm_pattern))
3163 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3164 layout2name(lum->lmm_pattern));
3166 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
3167 separator = (!yaml && is_dir) ? " " : "\n";
3170 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
3171 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3172 if (verbose & ~VERBOSE_GENERATION)
3173 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
3175 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3176 skip_objs ? 0 : (int)lum->lmm_layout_gen);
3180 if (verbose & VERBOSE_STRIPE_OFFSET) {
3181 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3182 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3183 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
3185 if (is_dir || skip_objs)
3186 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3187 lum->lmm_stripe_offset ==
3188 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
3189 lum->lmm_stripe_offset);
3190 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
3191 llapi_printf(LLAPI_MSG_NORMAL, "0");
3193 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3194 objects[0].l_ost_idx);
3195 if (!yaml && is_dir)
3201 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
3202 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3203 if (verbose & ~VERBOSE_POOL)
3204 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
3206 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
3207 if (!yaml && is_dir)
3213 if (strlen(separator) != 0)
3214 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3217 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
3218 struct lov_user_ost_data_v1 *objects,
3219 char *path, int obdindex, int depth,
3220 enum llapi_layout_verbose verbose,
3221 enum lov_dump_flags flags)
3223 bool is_dir = flags & LDF_IS_DIR;
3224 bool indent = flags & LDF_INDENT;
3225 bool skip_objs = flags & LDF_SKIP_OBJS;
3226 bool yaml = flags & LDF_YAML;
3227 bool obdstripe = obdindex == OBD_NOT_FOUND;
3230 if (!obdstripe && !skip_objs) {
3231 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
3232 if (obdindex == objects[i].l_ost_idx) {
3242 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
3245 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
3246 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
3247 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
3248 char *space = " - ";
3251 llapi_printf(LLAPI_MSG_NORMAL,
3252 "%6slmm_objects:\n", " ");
3254 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
3256 llapi_printf(LLAPI_MSG_NORMAL,
3257 "\tobdidx\t\t objid\t\t objid\t\t group\n");
3259 for (i = 0; i < lum->lmm_stripe_count; i++) {
3260 int idx = objects[i].l_ost_idx;
3261 long long oid = ostid_id(&objects[i].l_ost_oi);
3262 long long gr = ostid_seq(&objects[i].l_ost_oi);
3264 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
3268 struct lu_fid fid = { 0 };
3270 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3271 llapi_printf(LLAPI_MSG_NORMAL,
3272 "%sl_ost_idx: %d\n", space, idx);
3273 llapi_printf(LLAPI_MSG_NORMAL,
3274 "%8sl_fid: "DFID_NOBRACE"\n",
3276 } else if (indent) {
3277 struct lu_fid fid = { 0 };
3279 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3280 llapi_printf(LLAPI_MSG_NORMAL,
3281 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
3282 space, i, idx, PFID(&fid));
3286 sprintf(fmt, "%s%s%s\n",
3287 "\t%6u\t%14llu\t%#13llx\t",
3288 (fid_seq_is_rsvd(gr) ||
3289 fid_seq_is_mdt0(gr)) ?
3290 "%14llu" : "%#14llx", "%s");
3291 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
3293 obdindex == idx ? " *" : "");
3297 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3300 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3301 char *path, int obdindex, int depth,
3302 enum llapi_layout_verbose verbose,
3303 enum lov_dump_flags flags)
3305 struct lmv_user_mds_data *objects = lum->lum_objects;
3306 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3307 char *separator = "";
3308 bool yaml = flags & LDF_YAML;
3309 bool obdstripe = false;
3312 if (obdindex != OBD_NOT_FOUND) {
3313 if (lum->lum_stripe_count == 0) {
3314 if (obdindex == lum->lum_stripe_offset)
3317 for (i = 0; i < lum->lum_stripe_count; i++) {
3318 if (obdindex == objects[i].lum_mds) {
3319 llapi_printf(LLAPI_MSG_NORMAL,
3334 /* show all information default */
3336 if (lum->lum_magic == LMV_USER_MAGIC)
3337 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3338 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3340 verbose = VERBOSE_OBJID;
3343 if (depth && path && ((verbose != VERBOSE_OBJID)))
3344 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3346 if (verbose & VERBOSE_STRIPE_COUNT) {
3347 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3348 if (verbose & ~VERBOSE_STRIPE_COUNT)
3349 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3350 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3351 (int)lum->lum_stripe_count);
3352 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3358 if (verbose & VERBOSE_STRIPE_OFFSET) {
3359 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3360 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3361 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3362 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3363 (int)lum->lum_stripe_offset);
3364 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3370 if (verbose & VERBOSE_HASH_TYPE) {
3371 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3372 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3374 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3375 if (verbose & ~VERBOSE_HASH_TYPE)
3376 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3377 if (type < LMV_HASH_TYPE_MAX)
3378 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3379 mdt_hash_name[type]);
3381 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3383 if (flags & LMV_HASH_FLAG_MIGRATION)
3384 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3385 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3386 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3387 if (flags & LMV_HASH_FLAG_LOST_LMV)
3388 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3389 if (flags & LMV_HASH_FLAG_FIXED)
3390 llapi_printf(LLAPI_MSG_NORMAL, ",fixed");
3391 if (flags & ~LMV_HASH_FLAG_KNOWN)
3392 llapi_printf(LLAPI_MSG_NORMAL, ",unknown_%04x",
3393 flags & ~LMV_HASH_FLAG_KNOWN);
3395 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3401 if ((verbose & VERBOSE_INHERIT) && lum->lum_magic == LMV_USER_MAGIC) {
3402 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3403 if (verbose & ~VERBOSE_INHERIT)
3404 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit: ");
3405 if (lum->lum_max_inherit == LMV_INHERIT_UNLIMITED)
3406 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3407 else if (lum->lum_max_inherit == LMV_INHERIT_NONE)
3408 llapi_printf(LLAPI_MSG_NORMAL, "0");
3410 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3411 lum->lum_max_inherit);
3412 if (verbose & VERBOSE_INHERIT && !yaml)
3418 if ((verbose & VERBOSE_INHERIT_RR) &&
3419 lum->lum_magic == LMV_USER_MAGIC &&
3420 lum->lum_stripe_offset == LMV_OFFSET_DEFAULT) {
3421 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3422 if (verbose & ~VERBOSE_INHERIT_RR)
3423 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit_rr: ");
3424 if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_UNLIMITED)
3425 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3426 else if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_NONE)
3427 llapi_printf(LLAPI_MSG_NORMAL, "0");
3429 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3430 lum->lum_max_inherit_rr);
3431 if (verbose & VERBOSE_INHERIT_RR && !yaml)
3439 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3440 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3441 if (lum->lum_stripe_count > 0)
3442 llapi_printf(LLAPI_MSG_NORMAL,
3443 "mdtidx\t\t FID[seq:oid:ver]\n");
3444 for (i = 0; i < lum->lum_stripe_count; i++) {
3445 int idx = objects[i].lum_mds;
3446 struct lu_fid *fid = &objects[i].lum_fid;
3448 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3449 llapi_printf(LLAPI_MSG_NORMAL,
3450 "%6u\t\t "DFID"\t\t%s\n",
3452 obdindex == idx ? " *" : "");
3456 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3457 pool_name[0] != '\0') {
3458 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3459 if (verbose & ~VERBOSE_POOL)
3460 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3462 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3466 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3467 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3470 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3471 enum lov_dump_flags flags)
3473 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3474 int depth = param->fp_max_depth;
3475 enum llapi_layout_verbose verbose = param->fp_verbose;
3476 bool yaml = flags & LDF_YAML;
3478 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3479 !(flags & LDF_IS_DIR)) && !yaml)
3480 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3482 if (verbose & VERBOSE_DETAIL) {
3483 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3484 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3485 " ", comp_v1->lcm_magic);
3486 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3487 " ", comp_v1->lcm_size);
3488 if (flags & LDF_IS_DIR)
3489 llapi_printf(LLAPI_MSG_NORMAL,
3490 "%2slcm_flags: %s\n", " ",
3491 comp_v1->lcm_mirror_count > 0 ?
3494 llapi_printf(LLAPI_MSG_NORMAL,
3495 "%2slcm_flags: %s\n", " ",
3496 llapi_layout_flags_string(comp_v1->lcm_flags));
3499 if (verbose & VERBOSE_GENERATION) {
3500 if (verbose & ~VERBOSE_GENERATION)
3501 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3503 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3506 if (verbose & VERBOSE_MIRROR_COUNT) {
3507 if (verbose & ~VERBOSE_MIRROR_COUNT)
3508 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3510 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3511 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3512 comp_v1->lcm_mirror_count + 1 : 1);
3515 if (verbose & VERBOSE_COMP_COUNT) {
3516 if (verbose & ~VERBOSE_COMP_COUNT)
3517 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3519 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3520 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3521 comp_v1->lcm_entry_count : 0);
3524 if (verbose & VERBOSE_DETAIL && !yaml)
3525 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3528 static void lcme_flags2str(__u32 comp_flags)
3534 llapi_printf(LLAPI_MSG_NORMAL, "0");
3537 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3538 if (comp_flags & comp_flags_table[i].cfn_flag) {
3540 llapi_printf(LLAPI_MSG_NORMAL, ",");
3541 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3542 comp_flags_table[i].cfn_name);
3543 comp_flags &= ~comp_flags_table[i].cfn_flag;
3549 llapi_printf(LLAPI_MSG_NORMAL, ",");
3550 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3554 static void lov_dump_comp_v1_entry(struct find_param *param,
3555 enum lov_dump_flags flags, int index)
3557 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3558 struct lov_comp_md_entry_v1 *entry;
3559 char *separator = "";
3560 enum llapi_layout_verbose verbose = param->fp_verbose;
3561 bool yaml = flags & LDF_YAML;
3563 entry = &comp_v1->lcm_entries[index];
3566 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3568 if (verbose & VERBOSE_COMP_ID) {
3569 if (verbose & VERBOSE_DETAIL && !yaml)
3570 llapi_printf(LLAPI_MSG_NORMAL,
3571 "%slcme_id: ", " - ");
3572 else if (verbose & ~VERBOSE_COMP_ID)
3573 llapi_printf(LLAPI_MSG_NORMAL,
3574 "%4slcme_id: ", " ");
3575 if (entry->lcme_id != LCME_ID_INVAL)
3576 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3578 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3582 if (verbose & VERBOSE_MIRROR_ID) {
3583 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3584 if (verbose & ~VERBOSE_MIRROR_ID)
3585 llapi_printf(LLAPI_MSG_NORMAL,
3586 "%4slcme_mirror_id: ", " ");
3587 if (entry->lcme_id != LCME_ID_INVAL)
3588 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3589 mirror_id_of(entry->lcme_id));
3591 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3595 if (verbose & VERBOSE_COMP_FLAGS) {
3596 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3597 if (verbose & ~VERBOSE_COMP_FLAGS)
3598 llapi_printf(LLAPI_MSG_NORMAL,
3599 "%4slcme_flags: ", " ");
3600 lcme_flags2str(entry->lcme_flags);
3603 /* print snapshot timestamp if its a nosync comp */
3604 if ((verbose & VERBOSE_COMP_FLAGS) &&
3605 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3606 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3607 if (verbose & ~VERBOSE_COMP_FLAGS)
3608 llapi_printf(LLAPI_MSG_NORMAL,
3609 "%4slcme_timestamp: ", " ");
3611 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3612 (unsigned long long)entry->lcme_timestamp);
3614 time_t stamp = entry->lcme_timestamp;
3615 char *date_str = asctime(localtime(&stamp));
3617 date_str[strlen(date_str) - 1] = '\0';
3618 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3624 if (verbose & VERBOSE_COMP_START) {
3625 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3626 if (verbose & ~VERBOSE_COMP_START)
3627 llapi_printf(LLAPI_MSG_NORMAL,
3628 "%4slcme_extent.e_start: ", " ");
3629 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3630 (unsigned long long)entry->lcme_extent.e_start);
3634 if (verbose & VERBOSE_COMP_END) {
3635 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3636 if (verbose & ~VERBOSE_COMP_END)
3637 llapi_printf(LLAPI_MSG_NORMAL,
3638 "%4slcme_extent.e_end: ", " ");
3639 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3640 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3642 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3643 (unsigned long long)entry->lcme_extent.e_end);
3648 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3649 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3650 } else if (verbose & VERBOSE_DETAIL) {
3651 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3652 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3653 " ", entry->lcme_offset);
3654 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3655 " ", entry->lcme_size);
3656 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3658 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3663 * Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3664 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3666 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3667 * The table below gives the answers for the specified parameters (value and
3668 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3669 * --------------------------------------
3670 * 1 | file > limit; sign > 0 | -1 / -1 |
3671 * 2 | file = limit; sign > 0 | -1 / -1 |
3672 * 3 | file < limit; sign > 0 | ? / 1 |
3673 * 4 | file > limit; sign = 0 | -1 / -1 |
3674 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3675 * 6 | file < limit; sign = 0 | ? / -1 |
3676 * 7 | file > limit; sign < 0 | 1 / 1 |
3677 * 8 | file = limit; sign < 0 | ? / -1 |
3678 * 9 | file < limit; sign < 0 | ? / -1 |
3679 * --------------------------------------
3680 * Note: 5th actually means that the value is within the interval
3681 * (limit - margin, limit].
3683 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3684 int sign, int negopt, unsigned long long margin,
3690 /* Drop the fraction of margin (of days or size). */
3691 if (file + margin <= limit)
3693 } else if (sign == 0) {
3694 if (file <= limit && file + margin > limit)
3696 else if (file + margin <= limit)
3698 } else if (sign < 0) {
3705 return negopt ? ~ret + 1 : ret;
3708 static inline struct lov_user_md *
3709 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3711 return (struct lov_user_md *)((char *)comp_v1 +
3712 comp_v1->lcm_entries[ent_idx].lcme_offset);
3715 static inline struct lov_user_ost_data_v1 *
3716 lov_v1v3_objects(struct lov_user_md *v1)
3718 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3719 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3721 return v1->lmm_objects;
3725 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3727 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3728 snprintf(pool_name, LOV_MAXPOOLNAME + 1, "%s",
3729 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3731 pool_name[0] = '\0';
3735 print_last_init_comp(struct find_param *param)
3737 /* print all component info */
3738 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3741 /* print specific component info */
3742 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3743 param->fp_check_comp_start || param->fp_check_comp_end ||
3744 param->fp_check_mirror_id || param->fp_check_mirror_index)
3750 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3754 if (param->fp_comp_end == LUSTRE_EOF) {
3755 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3756 match = end == LUSTRE_EOF ? 1 : -1;
3757 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3758 match = end == LUSTRE_EOF ? -1 : 1;
3759 else /* at least EOF */
3761 if (param->fp_exclude_comp_end)
3764 unsigned long long margin;
3766 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3767 match = find_value_cmp(end, param->fp_comp_end,
3768 param->fp_comp_end_sign,
3769 param->fp_exclude_comp_end, margin, 0);
3776 * An example of "getstripe -v" for a two components PFL file:
3779 * lcm_magic: 0x0BD60BD0
3783 * lcm_entry_count: 2
3787 * lcme_extent.e_start: 0
3788 * lcme_extent.e_end: 1048576
3792 * lmm_magic: 0x0BD10BD0
3793 * lmm_seq: 0x200000401
3794 * lmm_object_id: 0x1
3795 * lmm_fid: [0x200000401:0x1:0x0]
3796 * lmm_stripe_count: 1
3797 * lmm_stripe_size: 1048576
3798 * lmm_pattern: raid0
3800 * lmm_stripe_offset: 0
3802 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3806 * lcme_extent.e_start: 1048576
3807 * lcme_extent.e_end: EOF
3811 * lmm_magic: 0x0BD10BD0
3812 * lmm_seq: 0x200000401
3813 * lmm_object_id: 0x1
3814 * lmm_fid: [0x200000401:0x1:0x0]
3815 * lmm_stripe_count: 2
3816 * lmm_stripe_size: 1048576
3817 * lmm_pattern: raid0
3819 * lmm_stripe_offset: 1
3821 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3822 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3824 static void lov_dump_comp_v1(struct find_param *param, char *path,
3825 enum lov_dump_flags flags)
3827 struct lov_comp_md_entry_v1 *entry;
3828 struct lov_user_ost_data_v1 *objects;
3829 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3830 struct lov_user_md_v1 *v1;
3831 char pool_name[LOV_MAXPOOLNAME + 1];
3832 int obdindex = param->fp_obd_index;
3833 int i, j, match, ext;
3834 bool obdstripe = false;
3835 __u16 mirror_index = 0;
3836 __u16 mirror_id = 0;
3838 if (obdindex != OBD_NOT_FOUND) {
3839 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3840 i < comp_v1->lcm_entry_count; i++) {
3841 if (!(comp_v1->lcm_entries[i].lcme_flags &
3845 v1 = lov_comp_entry(comp_v1, i);
3846 objects = lov_v1v3_objects(v1);
3848 for (j = 0; j < v1->lmm_stripe_count; j++) {
3849 if (obdindex == objects[j].l_ost_idx) {
3862 lov_dump_comp_v1_header(param, path, flags);
3864 flags |= LDF_INDENT;
3866 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3867 entry = &comp_v1->lcm_entries[i];
3869 if (param->fp_check_comp_flags) {
3870 if (((param->fp_comp_flags & entry->lcme_flags) !=
3871 param->fp_comp_flags) ||
3872 (param->fp_comp_neg_flags & entry->lcme_flags))
3876 if (param->fp_check_comp_id &&
3877 param->fp_comp_id != entry->lcme_id)
3880 if (param->fp_check_comp_start) {
3881 match = find_value_cmp(entry->lcme_extent.e_start,
3882 param->fp_comp_start,
3883 param->fp_comp_start_sign,
3885 param->fp_comp_start_units, 0);
3890 if (param->fp_check_comp_end) {
3891 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3897 if (param->fp_check_mirror_index) {
3898 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3900 mirror_id = mirror_id_of(entry->lcme_id);
3903 match = find_value_cmp(mirror_index,
3904 param->fp_mirror_index,
3905 param->fp_mirror_index_sign,
3906 param->fp_exclude_mirror_index,
3910 } else if (param->fp_check_mirror_id) {
3911 if (mirror_id != mirror_id_of(entry->lcme_id))
3912 mirror_id = mirror_id_of(entry->lcme_id);
3914 match = find_value_cmp(mirror_id,
3915 param->fp_mirror_id,
3916 param->fp_mirror_id_sign,
3917 param->fp_exclude_mirror_id,
3923 if (print_last_init_comp(param)) {
3925 * if part of stripe info is needed, we'd print only
3926 * the last instantiated component info.
3928 if (entry->lcme_flags & LCME_FL_INIT)
3931 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3932 if (entry->lcme_flags & LCME_FL_EXTENSION)
3933 /* moved back below */
3941 if (entry->lcme_flags & LCME_FL_INIT) {
3942 if (obdindex != OBD_NOT_FOUND) {
3943 flags |= LDF_SKIP_OBJS;
3944 v1 = lov_comp_entry(comp_v1, i);
3945 objects = lov_v1v3_objects(v1);
3947 for (j = 0; j < v1->lmm_stripe_count; j++) {
3948 if (obdindex == objects[j].l_ost_idx) {
3949 flags &= ~LDF_SKIP_OBJS;
3954 flags &= ~LDF_SKIP_OBJS;
3957 flags |= LDF_SKIP_OBJS;
3960 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3962 lov_dump_comp_v1_entry(param, flags, i);
3964 v1 = lov_comp_entry(comp_v1, i);
3965 objects = lov_v1v3_objects(v1);
3966 lov_v1v3_pool_name(v1, pool_name);
3968 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3969 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3970 param->fp_max_depth, param->fp_verbose,
3973 if (print_last_init_comp(param)) {
3975 * directory layout contains only layout template, print the
3979 i = comp_v1->lcm_entry_count - 1;
3982 flags &= ~LDF_SKIP_OBJS;
3984 lov_dump_comp_v1_entry(param, flags, i);
3986 v1 = lov_comp_entry(comp_v1, i);
3987 objects = lov_v1v3_objects(v1);
3988 lov_v1v3_pool_name(v1, pool_name);
3990 entry = &comp_v1->lcm_entries[i];
3991 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3992 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3993 param->fp_max_depth, param->fp_verbose,
3998 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3999 VERBOSE_COMP_START | VERBOSE_COMP_END | \
4002 static inline bool has_any_comp_options(struct find_param *param)
4004 enum llapi_layout_verbose verbose = param->fp_verbose;
4006 if (param->fp_check_comp_id || param->fp_check_comp_count ||
4007 param->fp_check_comp_start || param->fp_check_comp_end ||
4008 param->fp_check_comp_flags)
4011 /* show full layout information, not component specific */
4012 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
4015 return verbose & VERBOSE_COMP_OPTS;
4018 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
4021 struct lov_user_md *lum = &orig->lmd_lmm;
4022 struct lov_user_mds_data *new;
4023 struct lov_comp_md_v1 *comp_v1;
4024 struct lov_comp_md_entry_v1 *ent;
4025 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
4026 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
4029 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
4031 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
4035 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
4036 + sizeof(new->lmd_lmmsize));
4038 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
4039 comp_v1->lcm_magic = lum->lmm_magic;
4040 comp_v1->lcm_size = lum_off + lum_size;
4041 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
4042 comp_v1->lcm_flags = 0;
4043 comp_v1->lcm_entry_count = 1;
4045 ent = &comp_v1->lcm_entries[0];
4047 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
4048 ent->lcme_extent.e_start = 0;
4049 ent->lcme_extent.e_end = LUSTRE_EOF;
4050 ent->lcme_offset = lum_off;
4051 ent->lcme_size = lum_size;
4053 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
4058 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
4059 enum lov_dump_flags flags)
4061 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
4063 if (has_any_comp_options(param)) {
4064 struct lov_user_mds_data *new_lmd, *orig_lmd;
4066 orig_lmd = param->fp_lmd;
4067 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
4068 if (new_lmd != NULL) {
4069 param->fp_lmd = new_lmd;
4070 lov_dump_comp_v1(param, path, flags);
4071 param->fp_lmd = orig_lmd;
4077 if (magic == LOV_USER_MAGIC_V1) {
4078 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
4079 param->fp_lmd->lmd_lmm.lmm_objects,
4080 path, param->fp_obd_index,
4081 param->fp_max_depth, param->fp_verbose,
4084 char pool_name[LOV_MAXPOOLNAME + 1];
4085 struct lov_user_ost_data_v1 *objects;
4086 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
4088 snprintf(pool_name, sizeof(pool_name), "%s",
4089 lmmv3->lmm_pool_name);
4090 objects = lmmv3->lmm_objects;
4091 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
4092 objects, path, param->fp_obd_index,
4093 param->fp_max_depth, param->fp_verbose,
4098 static uint32_t check_foreign_type(uint32_t foreign_type)
4102 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
4103 if (lu_foreign_types[i].lft_name == NULL)
4105 if (foreign_type == lu_foreign_types[i].lft_type)
4109 return LU_FOREIGN_TYPE_UNKNOWN;
4112 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
4113 enum lov_dump_flags flags)
4115 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
4116 bool yaml = flags & LDF_YAML;
4118 if (!yaml && param->fp_depth && path)
4119 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4121 if (param->fp_verbose & VERBOSE_DETAIL) {
4122 uint32_t type = check_foreign_type(lfm->lfm_type);
4124 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
4126 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
4128 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
4130 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4131 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4132 lu_foreign_types[type].lft_name);
4134 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4136 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
4139 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
4140 lfm->lfm_length, lfm->lfm_value);
4141 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4144 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
4145 enum lov_dump_flags flags)
4147 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
4148 bool yaml = flags & LDF_YAML;
4150 if (!yaml && param->fp_depth && path)
4151 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4153 if (param->fp_verbose & VERBOSE_DETAIL) {
4154 uint32_t type = check_foreign_type(lfm->lfm_type);
4156 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
4158 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
4160 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
4162 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4163 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4164 lu_foreign_types[type].lft_name);
4166 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4168 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
4171 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
4172 lfm->lfm_length, lfm->lfm_value);
4173 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4176 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
4177 enum lov_dump_flags flags)
4181 if (param->fp_get_lmv || param->fp_get_default_lmv)
4182 magic = (__u32)param->fp_lmv_md->lum_magic;
4184 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
4187 flags |= LDF_IS_RAW;
4192 case LOV_USER_MAGIC_V1:
4193 case LOV_USER_MAGIC_V3:
4194 case LOV_USER_MAGIC_SPECIFIC:
4195 lov_dump_plain_user_lmm(param, path, flags);
4197 case LOV_USER_MAGIC_FOREIGN:
4198 lov_dump_foreign_lmm(param, path, flags);
4201 case LMV_USER_MAGIC: {
4202 char pool_name[LOV_MAXPOOLNAME + 1];
4203 struct lmv_user_md *lum;
4205 lum = (struct lmv_user_md *)param->fp_lmv_md;
4206 snprintf(pool_name, sizeof(pool_name), "%s",
4207 lum->lum_pool_name);
4208 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
4209 param->fp_max_depth, param->fp_verbose,
4213 case LOV_USER_MAGIC_COMP_V1:
4214 lov_dump_comp_v1(param, path, flags);
4216 case LMV_MAGIC_FOREIGN:
4217 lmv_dump_foreign_lmm(param, path, flags);
4220 llapi_printf(LLAPI_MSG_NORMAL,
4221 "unknown lmm_magic: %#x (expecting one of %#x %#x %#x %#x)\n",
4222 *(__u32 *)¶m->fp_lmd->lmd_lmm,
4223 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
4224 LMV_USER_MAGIC, LMV_MAGIC_V1);
4229 static int llapi_file_get_stripe1(const char *path, struct lov_user_md *lum)
4235 fname = strrchr(path, '/');
4237 /* It should be a file (or other non-directory) */
4238 if (fname == NULL) {
4239 dname = (char *)malloc(2);
4243 fname = (char *)path;
4245 dname = (char *)malloc(fname - path + 1);
4248 strncpy(dname, path, fname - path);
4249 dname[fname - path] = '\0';
4253 fd = open(dname, O_RDONLY | O_NONBLOCK);
4259 strcpy((char *)lum, fname);
4260 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
4263 if (close(fd) == -1 && rc == 0)
4271 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
4273 char *canon_path = NULL;
4276 rc = llapi_file_get_stripe1(path, lum);
4277 if (!(rc == -ENOTTY || rc == -ENODATA))
4280 /* Handle failure due to symlinks by dereferencing path manually. */
4281 canon_path = canonicalize_file_name(path);
4282 if (canon_path == NULL)
4283 goto out; /* Keep original rc. */
4285 rc2 = llapi_file_get_stripe1(canon_path, lum);
4287 goto out; /* Keep original rc. */
4296 int llapi_file_lookup(int dirfd, const char *name)
4298 struct obd_ioctl_data data = { 0 };
4303 if (dirfd < 0 || name == NULL)
4306 data.ioc_version = OBD_IOCTL_VERSION;
4307 data.ioc_len = sizeof(data);
4308 data.ioc_inlbuf1 = (char *)name;
4309 data.ioc_inllen1 = strlen(name) + 1;
4311 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
4313 llapi_error(LLAPI_MSG_ERROR, rc,
4314 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
4319 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
4326 * Check if the file time matches all the given criteria (e.g. --atime +/-N).
4327 * Return -1 or 1 if file timestamp does not or does match the given criteria
4328 * correspondingly. Return 0 if the MDS time is being checked and there are
4329 * attributes on OSTs and it is not yet clear if the timespamp matches.
4331 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
4332 * updated timestamps.
4334 static int find_time_check(struct find_param *param, int mds)
4336 struct lov_user_mds_data *lmd = param->fp_lmd;
4340 /* Check if file is accepted. */
4341 if (param->fp_atime) {
4342 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4343 param->fp_atime, param->fp_asign,
4344 param->fp_exclude_atime,
4345 param->fp_time_margin, mds);
4351 if (param->fp_mtime) {
4352 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4353 param->fp_mtime, param->fp_msign,
4354 param->fp_exclude_mtime,
4355 param->fp_time_margin, mds);
4360 * If the previous check matches, but this one is not yet clear,
4361 * we should return 0 to do an RPC on OSTs.
4367 if (param->fp_ctime) {
4368 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4369 param->fp_ctime, param->fp_csign,
4370 param->fp_exclude_ctime,
4371 param->fp_time_margin, mds);
4376 * If the previous check matches, but this one is not yet clear,
4377 * we should return 0 to do an RPC on OSTs.
4386 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4388 struct lov_user_mds_data *lmd = param->fp_lmd;
4393 for (i = 0; i < 2; i++) {
4394 /* Check if file is accepted. */
4395 if (param->fp_newery[NEWERXY_ATIME][i]) {
4396 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4397 param->fp_newery[NEWERXY_ATIME][i],
4404 if (param->fp_newery[NEWERXY_MTIME][i]) {
4405 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4406 param->fp_newery[NEWERXY_MTIME][i],
4412 * If the previous check matches, but this one is not
4413 * yet clear, we should return 0 to do an RPC on OSTs.
4419 if (param->fp_newery[NEWERXY_CTIME][i]) {
4420 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4421 param->fp_newery[NEWERXY_CTIME][i],
4427 * If the previous check matches, but this one is not
4428 * yet clear, we should return 0 to do an RPC on OSTs.
4435 * File birth time (btime) can get from MDT directly.
4436 * if @from_mdt is true, it means the input file attributs are
4437 * obtained directly from MDT.
4438 * Thus, if @from_mdt is false, we should skip the following
4444 if (param->fp_newery[NEWERXY_BTIME][i]) {
4445 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4448 rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4449 param->fp_newery[NEWERXY_BTIME][i],
4460 * Check whether the stripes matches the indexes user provided
4464 static int check_obd_match(struct find_param *param)
4466 struct lov_user_ost_data_v1 *objects;
4467 struct lov_comp_md_v1 *comp_v1 = NULL;
4468 struct lov_user_mds_data *lmd = param->fp_lmd;
4469 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4470 int i, j, k, count = 1;
4472 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4475 if (!S_ISREG(lmd->lmd_stx.stx_mode))
4478 /* exclude foreign */
4479 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4480 return param->fp_exclude_obd;
4483 * Only those files should be accepted, which have a
4484 * stripe on the specified OST.
4486 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4487 comp_v1 = (struct lov_comp_md_v1 *)v1;
4488 count = comp_v1->lcm_entry_count;
4491 for (i = 0; i < count; i++) {
4493 v1 = lov_comp_entry(comp_v1, i);
4495 objects = lov_v1v3_objects(v1);
4497 for (j = 0; j < v1->lmm_stripe_count; j++) {
4498 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4501 for (k = 0; k < param->fp_num_obds; k++) {
4502 if (param->fp_obd_indexes[k] ==
4503 objects[j].l_ost_idx)
4504 return !param->fp_exclude_obd;
4509 return param->fp_exclude_obd;
4512 static int check_mdt_match(struct find_param *param)
4516 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4519 /* FIXME: For striped dir, we should get stripe information and check */
4520 for (i = 0; i < param->fp_num_mdts; i++) {
4521 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4522 return !param->fp_exclude_mdt;
4525 if (param->fp_exclude_mdt)
4532 * Check whether the obd is active or not, if it is
4533 * not active, just print the object affected by this
4536 static void print_failed_tgt(struct find_param *param, char *path, int type)
4538 struct obd_statfs stat_buf;
4539 struct obd_uuid uuid_buf;
4540 int tgt_nr, i, *indexes;
4543 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV) {
4544 llapi_error(LLAPI_MSG_NORMAL, ret, "%s: wrong statfs type(%d)",
4549 tgt_nr = (type == LL_STATFS_LOV) ? param->fp_obd_index :
4550 param->fp_mdt_index;
4551 indexes = (type == LL_STATFS_LOV) ? param->fp_obd_indexes :
4552 param->fp_mdt_indexes;
4554 for (i = 0; i < tgt_nr; i++) {
4555 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4556 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4558 ret = llapi_obd_statfs(path, type, indexes[i], &stat_buf,
4561 llapi_error(LLAPI_MSG_NORMAL, ret,
4562 "%s: obd_uuid: %s failed",
4563 __func__, param->fp_obd_uuid->uuid);
4567 static int find_check_stripe_size(struct find_param *param)
4569 struct lov_comp_md_v1 *comp_v1 = NULL;
4570 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4571 __u32 stripe_size = 0;
4572 int ret, i, count = 1;
4574 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4575 return param->fp_exclude_stripe_size ? 1 : -1;
4577 ret = param->fp_exclude_stripe_size ? 1 : -1;
4578 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4579 comp_v1 = (struct lov_comp_md_v1 *)v1;
4580 count = comp_v1->lcm_entry_count;
4583 for (i = 0; i < count; i++) {
4584 struct lov_comp_md_entry_v1 *ent;
4587 v1 = lov_comp_entry(comp_v1, i);
4589 ent = &comp_v1->lcm_entries[i];
4590 if (ent->lcme_flags & LCME_FL_EXTENSION)
4592 if (!(ent->lcme_flags & LCME_FL_INIT))
4595 stripe_size = v1->lmm_stripe_size;
4598 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4599 param->fp_stripe_size_sign,
4600 param->fp_exclude_stripe_size,
4601 param->fp_stripe_size_units, 0);
4606 static int find_check_ext_size(struct find_param *param)
4608 struct lov_comp_md_v1 *comp_v1;
4609 struct lov_user_md_v1 *v1;
4612 ret = param->fp_exclude_ext_size ? 1 : -1;
4613 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4614 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4617 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4618 struct lov_comp_md_entry_v1 *ent;
4620 v1 = lov_comp_entry(comp_v1, i);
4622 ent = &comp_v1->lcm_entries[i];
4623 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4626 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4627 param->fp_ext_size_sign,
4628 param->fp_exclude_ext_size,
4629 param->fp_ext_size_units, 0);
4630 /* If any ext_size matches */
4638 static __u32 find_get_stripe_count(struct find_param *param)
4640 struct lov_comp_md_v1 *comp_v1 = NULL;
4641 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4643 __u32 stripe_count = 0;
4645 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4648 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4649 comp_v1 = (struct lov_comp_md_v1 *)v1;
4650 count = comp_v1->lcm_entry_count;
4653 for (i = 0; i < count; i++) {
4655 struct lov_comp_md_entry_v1 *ent;
4657 v1 = lov_comp_entry(comp_v1, i);
4659 ent = &comp_v1->lcm_entries[i];
4660 if (!(ent->lcme_flags & LCME_FL_INIT))
4663 if (ent->lcme_flags & LCME_FL_EXTENSION)
4666 stripe_count = v1->lmm_stripe_count;
4669 return stripe_count;
4672 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4674 static int find_check_layout(struct find_param *param)
4676 struct lov_comp_md_v1 *comp_v1 = NULL;
4677 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4679 bool found = false, valid = false;
4681 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4682 comp_v1 = (struct lov_comp_md_v1 *)v1;
4683 count = comp_v1->lcm_entry_count;
4686 for (i = 0; i < count; i++) {
4688 v1 = lov_comp_entry(comp_v1, i);
4690 /* foreign file have a special magic but no pattern field */
4691 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4694 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4698 if (v1->lmm_pattern & param->fp_layout) {
4707 if ((found && !param->fp_exclude_layout) ||
4708 (!found && param->fp_exclude_layout))
4715 * if no type specified, check/exclude all foreign
4716 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4718 static int find_check_foreign(struct find_param *param)
4720 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4721 struct lov_foreign_md *lfm;
4723 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4724 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4725 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4726 return param->fp_exclude_foreign ? 1 : -1;
4730 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4731 lfm->lfm_type == param->fp_foreign_type)
4732 return param->fp_exclude_foreign ? -1 : 1;
4733 return param->fp_exclude_foreign ? 1 : -1;
4736 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4737 struct lmv_foreign_md *lfm;
4739 lfm = (void *)param->fp_lmv_md;
4740 if (lmv_is_foreign(lfm->lfm_magic)) {
4741 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4742 return param->fp_exclude_foreign ? 1 : -1;
4746 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4747 lfm->lfm_type == param->fp_foreign_type)
4748 return param->fp_exclude_foreign ? -1 : 1;
4749 return param->fp_exclude_foreign ? 1 : -1;
4754 static int find_check_pool(struct find_param *param)
4756 struct lov_comp_md_v1 *comp_v1 = NULL;
4757 struct lov_user_md_v3 *v3 = (void *)¶m->fp_lmd->lmd_lmm;
4761 if (v3->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4762 comp_v1 = (struct lov_comp_md_v1 *)v3;
4763 count = comp_v1->lcm_entry_count;
4764 /* empty requested pool is taken as no pool search */
4765 if (count == 0 && param->fp_poolname[0] == '\0') {
4771 for (i = 0; i < count; i++) {
4772 if (comp_v1 != NULL) {
4773 if (!(comp_v1->lcm_entries[i].lcme_flags &
4777 v3 = (void *)lov_comp_entry(comp_v1, i);
4780 if (v3->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4783 if (((v3->lmm_magic == LOV_USER_MAGIC_V1) &&
4784 (param->fp_poolname[0] == '\0')) ||
4785 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4786 (strncmp(v3->lmm_pool_name,
4787 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4788 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4789 (strcmp(param->fp_poolname, "*") == 0))) {
4796 if ((found && !param->fp_exclude_pool) ||
4797 (!found && param->fp_exclude_pool))
4803 static int find_check_comp_options(struct find_param *param)
4805 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4806 struct lov_user_mds_data *lmd = param->fp_lmd;
4807 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4808 struct lov_comp_md_entry_v1 *entry;
4811 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4814 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4815 comp_v1 = (struct lov_comp_md_v1 *)v1;
4817 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4818 if (forged_v1 == NULL)
4820 comp_v1 = forged_v1;
4821 comp_v1->lcm_entry_count = 1;
4822 entry = &comp_v1->lcm_entries[0];
4823 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4825 entry->lcme_extent.e_start = 0;
4826 entry->lcme_extent.e_end = LUSTRE_EOF;
4829 /* invalid case, don't match for any kind of search. */
4830 if (comp_v1->lcm_entry_count == 0) {
4835 if (param->fp_check_comp_count) {
4836 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4837 param->fp_comp_count,
4838 param->fp_comp_count_sign,
4839 param->fp_exclude_comp_count, 1, 0);
4845 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4846 entry = &comp_v1->lcm_entries[i];
4848 if (param->fp_check_comp_flags) {
4850 if (((param->fp_comp_flags & entry->lcme_flags) !=
4851 param->fp_comp_flags) ||
4852 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4858 if (param->fp_check_comp_start) {
4859 ret = find_value_cmp(entry->lcme_extent.e_start,
4860 param->fp_comp_start,
4861 param->fp_comp_start_sign,
4862 param->fp_exclude_comp_start,
4863 param->fp_comp_start_units, 0);
4868 if (param->fp_check_comp_end) {
4869 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4875 /* the component matches all criteria */
4884 static int find_check_mirror_options(struct find_param *param)
4886 struct lov_comp_md_v1 *comp_v1;
4887 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4890 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4893 comp_v1 = (struct lov_comp_md_v1 *)v1;
4895 if (param->fp_check_mirror_count) {
4896 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4897 param->fp_mirror_count,
4898 param->fp_mirror_count_sign,
4899 param->fp_exclude_mirror_count, 1, 0);
4904 if (param->fp_check_mirror_state) {
4906 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4908 if ((param->fp_mirror_state != 0 &&
4909 file_state != param->fp_mirror_state) ||
4910 file_state == param->fp_mirror_neg_state)
4917 static bool find_check_lmm_info(struct find_param *param)
4919 return param->fp_check_pool || param->fp_check_stripe_count ||
4920 param->fp_check_stripe_size || param->fp_check_layout ||
4921 param->fp_check_comp_count || param->fp_check_comp_end ||
4922 param->fp_check_comp_start || param->fp_check_comp_flags ||
4923 param->fp_check_mirror_count || param->fp_check_foreign ||
4924 param->fp_check_mirror_state || param->fp_check_ext_size ||
4925 param->fp_check_projid;
4929 * Interpret backslash escape sequences and write output into buffer.
4930 * Anything written to the buffer will be null terminated.
4932 * @param[in] seq String being parsed for escape sequence. The leading
4933 * '\' character is not included in this string (only the
4934 * characters after it)
4935 * @param[out] buffer Location where interpreted escape sequence is written
4936 * @param[in] size Size of the available buffer. (Needs to be large enough
4937 * to handle escape sequence output plus null terminator.)
4938 * @param[out] wrote Number of bytes written to the buffer.
4939 * @return Number of characters from input string processed
4940 * as part of the escape sequence (0 for an unrecognized
4943 int printf_format_escape(char *seq, char *buffer, size_t size, int *wrote)
4946 /* For now, only handle single char escape sequences: \n, \t, \\ */
4969 * Interpret formats for timestamps (%a, %A@, etc)
4971 * @param[in] seq String being parsed for timestamp format. The leading
4972 * '%' character is not included in this string
4973 * @param[out] buffer Location where timestamp info is written
4974 * @param[in] size Size of the available buffer.
4975 * @param[out] wrote Number of bytes written to the buffer.
4976 * @return Number of characters from input string processed
4977 * as part of the format (0 for an unknown format)
4980 int printf_format_timestamp(char *seq, char *buffer, size_t size, int *wrote,
4981 struct find_param *param)
4983 struct statx_timestamp ts = { 0, 0 };
4987 char *fmt = "%c"; /* Print in ctime format by default */
4992 ts = param->fp_lmd->lmd_stx.stx_atime;
4996 if (*(seq + 1) == '@') {
4997 ts = param->fp_lmd->lmd_stx.stx_atime;
5003 ts = param->fp_lmd->lmd_stx.stx_ctime;
5007 if (*(seq + 1) == '@') {
5008 ts = param->fp_lmd->lmd_stx.stx_ctime;
5014 ts = param->fp_lmd->lmd_stx.stx_mtime;
5018 if (*(seq + 1) == '@') {
5019 ts = param->fp_lmd->lmd_stx.stx_mtime;
5025 ts = param->fp_lmd->lmd_stx.stx_btime;
5029 if (*(seq + 1) == '@') {
5030 ts = param->fp_lmd->lmd_stx.stx_btime;
5040 /* Found valid format, print to buffer */
5043 *wrote = strftime(buffer, size, fmt, tm);
5050 * Print all ost indices associated with a file layout using a commma separated
5051 * list. For a file with mutliple components, the list of indices for each
5052 * component will be enclosed in brackets.
5054 * @param[out] buffer Location where OST indices are written
5055 * @param[in] size Size of the available buffer.
5056 * @pararm[in] layout Pointer to layout structure for the file
5057 * @return Number of bytes written to output buffer
5059 static int printf_format_ost_indices(char *buffer, size_t size,
5060 struct llapi_layout *layout)
5062 uint64_t count, idx, i;
5063 int err, bytes, wrote = 0;
5065 /* Make sure to start at the first component */
5066 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
5068 llapi_error(LLAPI_MSG_ERROR, err,
5069 "error: layout component iteration failed\n");
5073 err = llapi_layout_stripe_count_get(layout, &count);
5075 llapi_error(LLAPI_MSG_ERROR, err,
5076 "error: cannot get stripe_count\n");
5080 bytes = snprintf(buffer, (size - wrote), "%s", "[");
5085 for (i = 0; i < count; i++) {
5086 err = llapi_layout_ost_index_get(layout, i, &idx);
5088 llapi_error(LLAPI_MSG_ERROR, err,
5089 "error: cannot get OST index\n");
5090 bytes = snprintf(buffer, (size - wrote),
5093 bytes = snprintf(buffer, (size - wrote),
5101 /* Overwrite last comma with closing bracket */
5102 *(buffer - 1) = ']';
5104 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
5105 if (err == 0) /* next component is found */
5108 llapi_error(LLAPI_MSG_ERROR, err,
5109 "error: layout component iteration failed\n");
5110 /* At this point, either got error or reached last component */
5121 * Parse Lustre-specific format sequences of the form %L{x}.
5123 * @param[in] seq String being parsed for format sequence. The leading
5124 * '%' character is not included in this string
5125 * @param[out] buffer Location where interpreted format info is written
5126 * @param[in] size Size of the available buffer.
5127 * @param[out] wrote Number of bytes written to the buffer.
5128 * @param[in] param The find_param structure associated with the file/dir
5129 * @param[in] path Pathname of the current file/dir being handled
5130 * @param[in] projid Project ID associated with the current file/dir
5131 * @param[in] d File descriptor for the directory (or -1 for a
5132 * non-directory file)
5133 * @return Number of characters from input string processed
5134 * as part of the format (0 for an unknown format)
5136 int printf_format_lustre(char *seq, char *buffer, size_t size, int *wrote,
5137 struct find_param *param, char *path, int projid,
5140 struct lmv_user_md *lum;
5141 struct lmv_user_mds_data *objects;
5142 struct llapi_layout *layout = NULL;
5144 unsigned int hash_type;
5145 uint64_t str_cnt, str_size, idx;
5146 char pool_name[LOV_MAXPOOLNAME + 1] = { '\0' };
5148 int rc = 2; /* all current valid sequences are 2 chars */
5151 /* Sanity check. Formats always look like %L{X} */
5152 if (*seq++ != 'L') {
5158 * Some formats like %LF or %LP are handled the same for both files
5159 * and dirs, so handle all of those here.
5163 err = llapi_path2fid(path, &fid);
5165 llapi_error(LLAPI_MSG_ERROR, err,
5166 "error: cannot get fid\n");
5169 *wrote = snprintf(buffer, size, DFID_NOBRACE, PFID(&fid));
5172 *wrote = snprintf(buffer, size, "%d", projid);
5176 /* Other formats for files/dirs need to be handled differently */
5177 if (d == -1) { /* file */
5178 //layout = llapi_layout_get_by_xattr(¶m->fp_lmd->lmd_lmm,
5179 // param->fp_lum_size, 0);
5180 layout = llapi_layout_get_by_path(path, 0);
5181 if (layout == NULL) {
5182 llapi_error(LLAPI_MSG_ERROR, errno,
5183 "error: cannot get file layout\n");
5188 * Set the layout pointer to the last init component
5189 * since that is the component used for most of these
5190 * formats. (This also works for non-composite files)
5192 err = llapi_layout_get_last_init_comp(layout);
5194 llapi_error(LLAPI_MSG_ERROR, err,
5195 "error: cannot get last initialized compomnent\n");
5200 case 'c': /* stripe count */
5201 err = llapi_layout_stripe_count_get(layout, &str_cnt);
5203 llapi_error(LLAPI_MSG_ERROR, err,
5204 "error: cannot get stripe_count\n");
5207 *wrote = snprintf(buffer, size, "%"PRIu64, str_cnt);
5209 case 'h': /* hash info */
5210 /* Not applicable to files. Skip it. */
5212 case 'i': /* starting index */
5213 err = llapi_layout_ost_index_get(layout, 0, &idx);
5215 llapi_error(LLAPI_MSG_ERROR, err,
5216 "error: cannot get OST index of last initialized component\n");
5219 *wrote = snprintf(buffer, size, "%"PRIu64, idx);
5221 case 'o': /* list of object indices */
5222 *wrote = printf_format_ost_indices(buffer, size, layout);
5224 case 'p': /* pool name */
5225 err = llapi_layout_pool_name_get(layout, pool_name,
5228 llapi_error(LLAPI_MSG_ERROR, rc,
5229 "error: cannot get pool name\n");
5232 *wrote = snprintf(buffer, size, "%s", pool_name);
5234 case 'S': /* stripe size */
5235 err = llapi_layout_stripe_size_get(layout, &str_size);
5237 llapi_error(LLAPI_MSG_ERROR, rc,
5238 "error: cannot get stripe_size\n");
5241 *wrote = snprintf(buffer, size, "%"PRIu64, str_size);
5247 } else { /* directory */
5248 lum = (struct lmv_user_md *)param->fp_lmv_md;
5249 objects = lum->lum_objects;
5252 case 'c': /* stripe count */
5253 *wrote = snprintf(buffer, size, "%d",
5254 (int)lum->lum_stripe_count);
5256 case 'h': /* hash info */
5257 hash_type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
5258 if (hash_type < LMV_HASH_TYPE_MAX)
5259 *wrote = snprintf(buffer, size, "%s",
5260 mdt_hash_name[hash_type]);
5262 *wrote = snprintf(buffer, size, "%#x",
5265 case 'i': /* starting index */
5266 *wrote = snprintf(buffer, size, "%d",
5267 lum->lum_stripe_offset);
5269 case 'o': /* list of object indices */
5270 str_cnt = (int) lum->lum_stripe_count;
5271 *wrote = snprintf(buffer, size, "%s", "[");
5275 for (i = 0; i < str_cnt; i++) {
5276 bytes = snprintf(buffer, (size - *wrote),
5277 "%d,", objects[i].lum_mds);
5284 /* Use lum_offset as the only list entry */
5285 bytes = snprintf(buffer, (size - *wrote),
5286 "%d]", lum->lum_stripe_offset);
5289 /* Overwrite last comma with closing bracket */
5290 *(buffer - 1) = ']';
5293 case 'p': /* pool name */
5294 *wrote = snprintf(buffer, size, "%s",
5295 lum->lum_pool_name);
5297 case 'S': /* stripe size */
5298 /* This has no meaning for directories. Skip it. */
5308 llapi_layout_free(layout);
5311 /* output of snprintf was truncated */
5318 * Interpret format specifiers beginning with '%'.
5320 * @param[in] seq String being parsed for format specifier. The leading
5321 * '%' character is not included in this string
5322 * @param[out] buffer Location where formatted info is written
5323 * @param[in] size Size of the available buffer.
5324 * @param[out] wrote Number of bytes written to the buffer.
5325 * @param[in] param The find_param structure associated with the file/dir
5326 * @param[in] path Pathname of the current file/dir being handled
5327 * @param[in] projid Project ID associated with the current file/dir
5328 * @param[in] d File descriptor for the directory (or -1 for a
5329 * non-directory file)
5330 * @return Number of characters from input string processed
5331 * as part of the format (0 for an unknown format)
5333 int printf_format_directive(char *seq, char *buffer, size_t size, int *wrote,
5334 struct find_param *param, char *path, int projid,
5337 __u16 mode = param->fp_lmd->lmd_stx.stx_mode;
5338 uint64_t blocks = param->fp_lmd->lmd_stx.stx_blocks;
5339 int rc = 1; /* most specifiers are single character */
5347 case 'w': case 'W': /* timestamps */
5348 rc = printf_format_timestamp(seq, buffer, size, wrote, param);
5350 case 'b': /* file size (in 512B blocks) */
5351 *wrote = snprintf(buffer, size, "%"PRIu64, blocks);
5353 case 'G': /* GID of owner */
5354 *wrote = snprintf(buffer, size, "%u",
5355 param->fp_lmd->lmd_stx.stx_gid);
5357 case 'k': /* file size (in 1K blocks) */
5358 *wrote = snprintf(buffer, size, "%"PRIu64, (blocks + 1)/2);
5360 case 'L': /* Lustre-specific formats */
5361 rc = printf_format_lustre(seq, buffer, size, wrote, param,
5364 case 'm': /* file mode in octal */
5365 *wrote = snprintf(buffer, size, "%#o", (mode & (~S_IFMT)));
5367 case 'p': /* Path name of file */
5368 *wrote = snprintf(buffer, size, "%s", path);
5370 case 's': /* file size (in bytes) */
5371 *wrote = snprintf(buffer, size, "%"PRIu64,
5372 (uint64_t) param->fp_lmd->lmd_stx.stx_size);
5374 case 'U': /* UID of owner */
5375 *wrote = snprintf(buffer, size, "%u",
5376 param->fp_lmd->lmd_stx.stx_uid);
5378 case 'y': /* file type */
5381 else if (S_ISDIR(mode))
5383 else if (S_ISLNK(mode))
5385 else if (S_ISBLK(mode))
5387 else if (S_ISCHR(mode))
5389 else if (S_ISFIFO(mode))
5391 else if (S_ISSOCK(mode))
5401 default: /* invalid format specifier */
5407 /* output of snprintf was truncated */
5414 * Parse user-supplied string for the -printf option and interpret any
5415 * '%' format specifiers or '\' escape sequences.
5417 * @param[in] param The find_param struct containing the -printf string
5418 * as well as info about the current file/dir that mathced
5419 * the lfs find search criteria
5420 * @param[in] path Path name for current file/dir
5421 * @param[in] projid Project ID associated with current file/dir
5422 * @param[in] d File descriptor for current directory (or -1 for a
5423 * non-directory file)
5425 void printf_format_string(struct find_param *param, char *path,
5428 char output[FORMATTED_BUF_LEN];
5429 char *fmt_char = param->fp_format_printf_str;
5430 char *buff = output;
5436 buff_size = FORMATTED_BUF_LEN;
5438 /* Always leave one free byte in buffer for trailing NUL */
5439 while (*fmt_char && (buff_size > 1)) {
5442 if (*fmt_char == '%') {
5443 rc = printf_format_directive(fmt_char + 1, buff,
5444 buff_size, &written, param,
5446 } else if (*fmt_char == '\\')
5447 rc = printf_format_escape(fmt_char + 1, buff,
5448 buff_size, &written);
5451 /* Either a '\' escape or '%' format was processed.
5452 * Increment pointers accordingly.
5454 fmt_char += (rc + 1);
5456 buff_size -= written;
5458 /* Regular char or invalid escape/format.
5459 * Either way, copy current character.
5461 *buff++ = *fmt_char++;
5466 /* Terminate output buffer and print */
5468 llapi_printf(LLAPI_MSG_NORMAL, "%s", output);
5472 * Get file/directory project id.
5473 * by the open fd resides on.
5474 * Return 0 and project id on success, or -ve errno.
5476 static int fget_projid(int fd, int *projid)
5481 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
5485 *projid = fsx.fsx_projid;
5490 * Check that the file's permissions in *st matches the one in find_param
5492 static int check_file_permissions(const struct find_param *param,
5499 switch (param->fp_perm_sign) {
5500 case LFS_FIND_PERM_EXACT:
5501 decision = (mode == param->fp_perm);
5503 case LFS_FIND_PERM_ALL:
5504 decision = ((mode & param->fp_perm) == param->fp_perm);
5506 case LFS_FIND_PERM_ANY:
5507 decision = ((mode & param->fp_perm) != 0);
5511 if ((param->fp_exclude_perm && decision)
5512 || (!param->fp_exclude_perm && !decision))
5518 static int cb_find_init(char *path, int p, int *dp,
5519 void *data, struct dirent64 *de)
5521 struct find_param *param = (struct find_param *)data;
5522 struct lov_user_mds_data *lmd = param->fp_lmd;
5523 int d = dp == NULL ? -1 : *dp;
5524 int decision = 1; /* 1 is accepted; -1 is rejected. */
5526 int checked_type = 0;
5528 __u32 stripe_count = 0;
5532 bool gather_all = false;
5534 if (p == -1 && d == -1)
5537 /* Reset this value between invocations */
5538 param->fp_get_lmv = 0;
5540 /* Gather all file/dir info, not just what's needed for search params */
5541 if (param->fp_format_printf_str)
5544 /* If a regular expression is presented, make the initial decision */
5545 if (param->fp_pattern != NULL) {
5546 char *fname = strrchr(path, '/');
5548 fname = (fname == NULL ? path : fname + 1);
5549 ret = fnmatch(param->fp_pattern, fname, 0);
5550 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
5551 (ret == 0 && param->fp_exclude_pattern))
5555 /* See if we can check the file type from the dirent. */
5556 if (de != NULL && de->d_type != DT_UNKNOWN) {
5557 if (param->fp_type != 0) {
5560 if (DTTOIF(de->d_type) == param->fp_type) {
5561 if (param->fp_exclude_type)
5564 if (!param->fp_exclude_type)
5568 if ((param->fp_check_mdt_count || param->fp_hash_type ||
5569 param->fp_check_hash_flag) && de->d_type != DT_DIR)
5576 * Request MDS for the stat info if some of these parameters need
5579 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
5580 param->fp_check_uid || param->fp_check_gid ||
5581 param->fp_newerxy || param->fp_btime ||
5582 param->fp_atime || param->fp_mtime || param->fp_ctime ||
5583 param->fp_check_size || param->fp_check_blocks ||
5584 find_check_lmm_info(param) ||
5585 param->fp_check_mdt_count || param->fp_hash_type ||
5586 param->fp_check_hash_flag || param->fp_perm_sign ||
5590 if (param->fp_type != 0 && checked_type == 0)
5593 if (decision == 0) {
5595 (param->fp_check_mdt_count || param->fp_hash_type ||
5596 param->fp_check_hash_flag || param->fp_check_foreign ||
5598 param->fp_get_lmv = 1;
5599 ret = cb_get_dirstripe(path, &d, param);
5601 if (errno == ENODATA) {
5602 /* Fill in struct for unstriped dir */
5604 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
5605 /* Use 0 until we find actual offset */
5606 param->fp_lmv_md->lum_stripe_offset = 0;
5607 param->fp_lmv_md->lum_stripe_count = 0;
5608 param->fp_lmv_md->lum_hash_type = 0;
5610 if (param->fp_check_foreign) {
5611 if (param->fp_exclude_foreign)
5620 if (param->fp_check_mdt_count) {
5621 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5624 decision = find_value_cmp(param->fp_lmv_md->lum_stripe_count,
5625 param->fp_mdt_count,
5626 param->fp_mdt_count_sign,
5627 param->fp_exclude_mdt_count, 1, 0);
5632 if (param->fp_hash_type) {
5634 __u32 type = param->fp_lmv_md->lum_hash_type &
5637 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5640 found = (1 << type) & param->fp_hash_type;
5641 if ((found && param->fp_exclude_hash_type) ||
5642 (!found && !param->fp_exclude_hash_type))
5646 if (param->fp_check_hash_flag) {
5647 __u32 flags = param->fp_lmv_md->lum_hash_type &
5648 ~LMV_HASH_TYPE_MASK;
5650 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5653 if (!(flags & param->fp_hash_inflags) ||
5654 (flags & param->fp_hash_exflags))
5659 param->fp_lmd->lmd_lmm.lmm_magic = 0;
5660 ret = get_lmd_info_fd(path, p, d, param->fp_lmd,
5661 param->fp_lum_size, GET_LMD_INFO);
5662 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
5663 find_check_lmm_info(param)) {
5664 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
5667 * We need to "fake" the "use the default" values
5668 * since the lmm struct is zeroed out at this point.
5670 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5671 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
5673 ostid_set_seq(&lmm->lmm_oi,
5674 FID_SEQ_LOV_DEFAULT);
5675 lmm->lmm_stripe_size = 0;
5676 lmm->lmm_stripe_count = 0;
5677 lmm->lmm_stripe_offset = -1;
5679 if (ret == 0 && (param->fp_mdt_uuid != NULL || gather_all)) {
5681 ret = llapi_file_fget_mdtidx(d,
5682 ¶m->fp_file_mdt_index);
5684 * Make sure lum_stripe_offset matches
5685 * mdt_index even for unstriped directories.
5687 if (ret == 0 && param->fp_get_lmv)
5688 param->fp_lmv_md->lum_stripe_offset =
5689 param->fp_file_mdt_index;
5690 } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
5692 * FIXME: we could get the MDT index from the
5693 * file's FID in lmd->lmd_lmm.lmm_oi without
5694 * opening the file, once we are sure that
5695 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
5696 * That would still be an ioctl() to map the
5697 * FID to the MDT, but not an open RPC.
5699 fd = open(path, O_RDONLY);
5701 ret = llapi_file_fget_mdtidx(fd,
5702 ¶m->fp_file_mdt_index);
5708 * For a special file, we assume it resides on
5709 * the same MDT as the parent directory.
5711 ret = llapi_file_fget_mdtidx(p,
5712 ¶m->fp_file_mdt_index);
5723 stripe_count = find_get_stripe_count(param);
5727 /* Check the file permissions from the stat info */
5728 if (param->fp_perm_sign) {
5729 decision = check_file_permissions(param, lmd->lmd_stx.stx_mode);
5734 if (param->fp_type && !checked_type) {
5735 if ((param->fp_check_mdt_count || param->fp_check_hash_flag ||
5736 param->fp_hash_type) && !S_ISDIR(lmd->lmd_stx.stx_mode))
5739 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
5740 if (param->fp_exclude_type)
5743 if (!param->fp_exclude_type)
5749 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
5750 if (lustre_fs && param->fp_got_uuids &&
5751 param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
5752 lmd->lmd_stx.stx_dev_minor)) {
5753 /* A lustre/lustre mount point is crossed. */
5754 param->fp_got_uuids = 0;
5755 param->fp_obds_printed = 0;
5756 param->fp_mdt_index = OBD_NOT_FOUND;
5757 param->fp_obd_index = OBD_NOT_FOUND;
5760 if (lustre_fs && !param->fp_got_uuids) {
5761 ret = setup_target_indexes((d != -1) ? d : p, path,
5766 param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
5767 lmd->lmd_stx.stx_dev_minor);
5768 } else if (!lustre_fs && param->fp_got_uuids) {
5769 /* A lustre/non-lustre mount point is crossed. */
5770 param->fp_got_uuids = 0;
5771 param->fp_mdt_index = OBD_NOT_FOUND;
5772 param->fp_obd_index = OBD_NOT_FOUND;
5776 if (param->fp_check_foreign) {
5777 decision = find_check_foreign(param);
5782 if (param->fp_check_stripe_size) {
5783 decision = find_check_stripe_size(param);
5788 if (param->fp_check_ext_size) {
5789 decision = find_check_ext_size(param);
5794 if (param->fp_check_stripe_count) {
5795 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
5796 param->fp_stripe_count_sign,
5797 param->fp_exclude_stripe_count, 1, 0);
5802 if (param->fp_check_layout) {
5803 decision = find_check_layout(param);
5808 /* If an OBD UUID is specified but none matches, skip this file. */
5809 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
5810 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
5814 * If an OST or MDT UUID is given, and some OST matches,
5817 if (param->fp_obd_index != OBD_NOT_FOUND ||
5818 param->fp_mdt_index != OBD_NOT_FOUND) {
5819 if (param->fp_obd_uuid) {
5820 if (check_obd_match(param)) {
5822 * If no mdtuuid is given, we are done.
5823 * Otherwise, fall through to the mdtuuid
5826 if (!param->fp_mdt_uuid)
5833 if (param->fp_mdt_uuid) {
5834 if (check_mdt_match(param))
5841 if (param->fp_check_uid) {
5842 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
5843 if (param->fp_exclude_uid)
5846 if (!param->fp_exclude_uid)
5851 if (param->fp_check_gid) {
5852 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
5853 if (param->fp_exclude_gid)
5856 if (!param->fp_exclude_gid)
5861 if (param->fp_check_projid || gather_all) {
5865 fd = open(path, O_RDONLY);
5868 ret = fget_projid(fd, &projid);
5873 if (projid == param->fp_projid) {
5874 if (param->fp_exclude_projid)
5877 if (!param->fp_exclude_projid)
5882 if (param->fp_check_pool) {
5883 decision = find_check_pool(param);
5888 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
5889 param->fp_check_comp_start || param->fp_check_comp_end) {
5890 decision = find_check_comp_options(param);
5895 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
5896 decision = find_check_mirror_options(param);
5901 /* Check the time on mds. */
5903 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
5906 for_mds = lustre_fs ?
5907 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5908 decision = find_time_check(param, for_mds);
5913 if (param->fp_btime) {
5914 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
5919 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
5920 param->fp_btime, param->fp_bsign,
5921 param->fp_exclude_btime,
5922 param->fp_time_margin, 0);
5927 if (param->fp_newerxy) {
5930 for_mds = lustre_fs ?
5931 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5932 decision = find_newerxy_check(param, for_mds, true);
5941 flags = param->fp_lmd->lmd_flags;
5942 if (param->fp_check_size &&
5943 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5944 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5945 !(flags & OBD_MD_FLSIZE ||
5946 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
5949 if (param->fp_check_blocks &&
5950 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5951 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5952 !(flags & OBD_MD_FLBLOCKS ||
5953 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
5957 * If file still fits the request, ask ost for updated info.
5958 * The regular stat is almost of the same speed as some new
5959 * 'glimpse-size-ioctl'.
5961 if (!decision || gather_all) {
5965 * For regular files with the stripe the decision may have not
5966 * been taken yet if *time or size is to be checked.
5968 if (param->fp_obd_index != OBD_NOT_FOUND)
5969 print_failed_tgt(param, path, LL_STATFS_LOV);
5971 if (param->fp_mdt_index != OBD_NOT_FOUND)
5972 print_failed_tgt(param, path, LL_STATFS_LMV);
5975 ret = fstat_f(d, &st);
5976 else if (de != NULL)
5977 ret = fstatat_f(p, de->d_name, &st,
5978 AT_SYMLINK_NOFOLLOW);
5980 ret = lstat_f(path, &st);
5983 if (errno == ENOENT) {
5984 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
5985 "warning: %s: %s does not exist",
5990 llapi_error(LLAPI_MSG_ERROR, ret,
5991 "%s: stat on %s failed",
5997 convert_lmd_statx(param->fp_lmd, &st, true);
5998 /* Check the time on osc. */
5999 decision = find_time_check(param, 0);
6003 if (param->fp_newerxy) {
6004 decision = find_newerxy_check(param, 0, false);
6014 if (param->fp_check_size) {
6015 decision = find_value_cmp(lmd->lmd_stx.stx_size,
6017 param->fp_size_sign,
6018 param->fp_exclude_size,
6019 param->fp_size_units, 0);
6024 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
6025 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
6027 param->fp_blocks_sign,
6028 param->fp_exclude_blocks,
6029 param->fp_blocks_units, 0);
6035 if (param->fp_format_printf_str)
6036 printf_format_string(param, path, projid, d);
6038 llapi_printf(LLAPI_MSG_NORMAL, "%s%c", path,
6039 param->fp_zero_end ? '\0' : '\n');
6043 /* Do not get down anymore? */
6044 if (param->fp_depth == param->fp_max_depth) {
6055 static int cb_migrate_mdt_init(char *path, int p, int *dp,
6056 void *param_data, struct dirent64 *de)
6058 struct find_param *param = (struct find_param *)param_data;
6059 struct lmv_user_md *lmu = param->fp_lmv_md;
6061 char raw[MAX_IOC_BUFLEN] = {'\0'};
6063 struct obd_ioctl_data data = { 0 };
6069 if (p == -1 && dp == NULL)
6075 if (dp != NULL && *dp != -1)
6079 tmp_p = open_parent(path);
6083 llapi_error(LLAPI_MSG_ERROR, ret,
6084 "can not open %s", path);
6089 path_copy = strdup(path);
6090 filename = basename(path_copy);
6092 data.ioc_inlbuf1 = (char *)filename;
6093 data.ioc_inllen1 = strlen(filename) + 1;
6094 data.ioc_inlbuf2 = (char *)lmu;
6095 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
6098 if (param->fp_depth == param->fp_max_depth)
6099 data.ioc_type = MDS_MIGRATE_NSONLY;
6100 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
6102 llapi_error(LLAPI_MSG_ERROR, ret,
6103 "%s: error packing ioctl data", __func__);
6108 ret = ioctl(tmp_p, LL_IOC_MIGRATE, rawbuf);
6110 if (errno == EBUSY && !retry) {
6112 * because migrate may not be able to lock all involved
6113 * objects in order, for some of them it try lock, while
6114 * there may be conflicting COS locks and cause migrate
6115 * fail with EBUSY, hope a sync() could cause
6116 * transaction commit and release these COS locks.
6121 } else if (errno == EALREADY) {
6122 if (param->fp_verbose & VERBOSE_DETAIL)
6123 llapi_printf(LLAPI_MSG_NORMAL,
6124 "%s migrated to MDT%d already\n",
6125 path, lmu->lum_stripe_offset);
6129 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
6133 } else if (param->fp_verbose & VERBOSE_DETAIL) {
6134 llapi_printf(LLAPI_MSG_NORMAL,
6135 "migrate %s to MDT%d stripe count %d\n",
6136 path, lmu->lum_stripe_offset,
6137 lmu->lum_stripe_count);
6141 /* Do not get down anymore? */
6142 if (param->fp_depth == param->fp_max_depth)
6149 * If the directory is being migration, we need
6150 * close the directory after migration,
6151 * so the old directory cache will be cleanup
6152 * on the client side, and re-open to get the
6153 * new directory handle
6155 *dp = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
6158 llapi_error(LLAPI_MSG_ERROR, ret,
6159 "%s: Failed to open '%s'", __func__, path);
6171 /* dir migration finished, shrink its stripes */
6172 static int cb_migrate_mdt_fini(char *path, int p, int *dp, void *data,
6173 struct dirent64 *de)
6175 struct find_param *param = data;
6176 struct lmv_user_md *lmu = param->fp_lmv_md;
6177 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
6180 if (de && de->d_type != DT_DIR)
6185 * close it before setxattr because the latter may destroy the
6186 * original object, and cause close fail.
6194 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
6195 if (ret == -EALREADY)
6198 cb_common_fini(path, p, dp, data, de);
6202 int llapi_migrate_mdt(char *path, struct find_param *param)
6204 param->fp_stop_on_error = 1;
6205 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
6209 int llapi_mv(char *path, struct find_param *param)
6211 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
6212 static bool printed;
6215 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
6216 "%s() is deprecated, use llapi_migrate_mdt() instead",
6221 return llapi_migrate_mdt(path, param);
6225 * Check string for escape sequences and print a message to stdout
6226 * if any invalid escapes are found.
6228 * @param[in] c Pointer to character immediately following the
6229 * '\' character indicating the start of an escape
6231 * @return Number of characters examined in the escape sequence
6232 * (regardless of whether the sequence is valid or not).
6234 int validate_printf_esc(char *c)
6236 char *valid_esc = "nt\\";
6239 /* backslash at end of string */
6240 llapi_err_noerrno(LLAPI_MSG_WARN,
6241 "warning: '\\' at end of -printf format string\n");
6245 if (!strchr(valid_esc, *c))
6246 /* Invalid escape character */
6247 llapi_err_noerrno(LLAPI_MSG_WARN,
6248 "warning: unrecognized escape: '\\%c'\n", *c);
6254 * Check string for format directives and print a message to stdout
6255 * if any invalid directives are found.
6257 * @param[in] c Pointer to character immediately following the
6258 * '%' character indicating the start of a format
6260 * @return Number of characters examined in the format directive
6261 * (regardless of whether the directive is valid or not).
6263 int validate_printf_fmt(char *c)
6265 char *valid_fmt_single = "abcGkmpstUwy%";
6266 char *valid_fmt_double = "ACTW";
6267 char *valid_fmt_lustre = "cFhioPpS";
6268 char curr = *c, next;
6271 llapi_err_noerrno(LLAPI_MSG_WARN,
6272 "warning: '%%' at end of -printf format string\n");
6277 if ((next == '\0') || (next == '%') || (next == '\\'))
6278 /* Treat as single char format directive */
6281 /* Check format directives with multiple characters */
6282 if (strchr(valid_fmt_double, curr)) {
6283 /* For now, only valid formats are followed by '@' char */
6285 llapi_err_noerrno(LLAPI_MSG_WARN,
6286 "warning: unrecognized format directive: '%%%c%c'\n",
6291 /* Lustre formats always start with 'L' */
6293 if (!strchr(valid_fmt_lustre, next))
6294 llapi_err_noerrno(LLAPI_MSG_WARN,
6295 "warning: unrecognized format directive: '%%%c%c'\n",
6302 if (!strchr(valid_fmt_single, curr))
6303 llapi_err_noerrno(LLAPI_MSG_WARN,
6304 "warning: unrecognized format directive: '%%%c'\n", curr);
6309 * Validate the user-supplied string for the -printf option and report
6310 * any invalid backslash escape sequences or format directives.
6312 * @param[in] param Structure containing info about invocation of lfs find
6315 void validate_printf_str(struct find_param *param)
6317 char *c = param->fp_format_printf_str;
6323 ret = validate_printf_fmt(++c);
6327 ret = validate_printf_esc(++c);
6337 int llapi_find(char *path, struct find_param *param)
6339 if (param->fp_format_printf_str)
6340 validate_printf_str(param);
6341 return param_callback(path, cb_find_init, cb_common_fini, param);
6345 * Get MDT number that the file/directory inode referenced
6346 * by the open fd resides on.
6347 * Return 0 and mdtidx on success, or -ve errno.
6349 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
6351 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
6356 static int cb_get_mdt_index(char *path, int p, int *dp, void *data,
6357 struct dirent64 *de)
6359 struct find_param *param = (struct find_param *)data;
6360 int d = dp == NULL ? -1 : *dp;
6364 if (p == -1 && d == -1)
6368 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6369 } else /* if (p != -1) */ {
6372 fd = open(path, O_RDONLY | O_NOCTTY);
6374 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
6382 if (ret == -ENODATA) {
6383 if (!param->fp_obd_uuid)
6384 llapi_printf(LLAPI_MSG_NORMAL,
6385 "'%s' has no stripe info\n", path);
6387 } else if (ret == -ENOENT) {
6388 llapi_error(LLAPI_MSG_WARN, ret,
6389 "warning: %s: '%s' does not exist",
6392 } else if (ret == -ENOTTY) {
6393 llapi_error(LLAPI_MSG_ERROR, ret,
6394 "%s: '%s' not on a Lustre fs",
6397 llapi_error(LLAPI_MSG_ERROR, ret,
6398 "error: %s: '%s' failed get_mdtidx",
6404 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
6405 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
6407 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
6411 /* Do not go down anymore? */
6412 if (param->fp_depth == param->fp_max_depth)
6420 static int cb_getstripe(char *path, int p, int *dp, void *data,
6421 struct dirent64 *de)
6423 struct find_param *param = (struct find_param *)data;
6424 int d = dp == NULL ? -1 : *dp;
6427 if (p == -1 && d == -1)
6430 if (param->fp_obd_uuid) {
6431 param->fp_quiet = 1;
6432 ret = setup_obd_uuid(d != -1 ? d : p, path, param);
6437 if (d != -1 && (param->fp_get_lmv || param->fp_get_default_lmv))
6438 ret = cb_get_dirstripe(path, &d, param);
6440 (p != -1 && !param->fp_get_lmv && !param->fp_get_default_lmv))
6441 ret = get_lmd_info_fd(path, p, d, ¶m->fp_lmd->lmd_lmm,
6442 param->fp_lum_size, GET_LMD_STRIPE);
6443 else if (d == -1 && (param->fp_get_lmv || param->fp_get_default_lmv)) {
6444 /* in case of a dangling or valid faked symlink dir, opendir()
6445 * should have return either EINVAL or ENOENT, so let's try
6446 * to get LMV just in case, and by opening it as a file but
6447 * with O_NOFOLLOW ...
6449 int fd = open(path, O_RDONLY | O_NOFOLLOW);
6453 ret = cb_get_dirstripe(path, &fd, param);
6455 llapi_lov_dump_user_lmm(param, path, LDF_IS_DIR);
6462 if (errno == ENODATA && d != -1) {
6464 * We need to "fake" the "use the default" values
6465 * since the lmm struct is zeroed out at this point.
6466 * The magic needs to be set in order to satisfy
6467 * a check later on in the code path.
6468 * The object_seq needs to be set for the "(Default)"
6469 * prefix to be displayed.
6471 if (param->fp_get_default_lmv) {
6472 struct lmv_user_md *lum = param->fp_lmv_md;
6474 lum->lum_magic = LMV_USER_MAGIC;
6475 lum->lum_stripe_count = 0;
6476 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
6478 } else if (param->fp_get_lmv) {
6479 struct lmv_user_md *lum = param->fp_lmv_md;
6482 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6485 lum->lum_magic = LMV_MAGIC_V1;
6486 lum->lum_stripe_count = 0;
6487 lum->lum_stripe_offset = mdtidx;
6490 struct lov_user_md *lmm =
6491 ¶m->fp_lmd->lmd_lmm;
6493 lmm->lmm_magic = LOV_USER_MAGIC_V1;
6495 ostid_set_seq(&lmm->lmm_oi,
6496 FID_SEQ_LOV_DEFAULT);
6497 lmm->lmm_stripe_count = 0;
6498 lmm->lmm_stripe_size = 0;
6499 lmm->lmm_stripe_offset = -1;
6502 } else if (errno == ENODATA && p != -1) {
6503 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
6504 llapi_printf(LLAPI_MSG_NORMAL,
6505 "%s has no stripe info\n", path);
6507 } else if (errno == ENOENT) {
6508 llapi_error(LLAPI_MSG_WARN, -ENOENT,
6509 "warning: %s: %s does not exist",
6512 } else if (errno == ENOTTY) {
6514 llapi_error(LLAPI_MSG_ERROR, ret,
6515 "%s: '%s' not on a Lustre fs?",
6520 llapi_error(LLAPI_MSG_ERROR, ret,
6521 "error: %s: %s failed for %s",
6523 "LL_IOC_LOV_GETSTRIPE" :
6524 "IOC_MDC_GETFILESTRIPE", path);
6531 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
6532 llapi_lov_dump_user_lmm(param, path, d != -1 ? LDF_IS_DIR : 0);
6535 /* Do not get down anymore? */
6536 if (param->fp_depth == param->fp_max_depth)
6544 int llapi_getstripe(char *path, struct find_param *param)
6546 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
6547 cb_get_mdt_index : cb_getstripe,
6548 cb_common_fini, param);
6551 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
6552 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6554 char raw[MAX_IOC_BUFLEN] = {'\0'};
6556 struct obd_ioctl_data data = { 0 };
6559 data.ioc_inlbuf1 = (char *)&type;
6560 data.ioc_inllen1 = sizeof(__u32);
6561 data.ioc_inlbuf2 = (char *)&index;
6562 data.ioc_inllen2 = sizeof(__u32);
6563 data.ioc_pbuf1 = (char *)stat_buf;
6564 data.ioc_plen1 = sizeof(struct obd_statfs);
6565 data.ioc_pbuf2 = (char *)uuid_buf;
6566 data.ioc_plen2 = sizeof(struct obd_uuid);
6568 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
6570 llapi_error(LLAPI_MSG_ERROR, rc,
6571 "%s: error packing ioctl data", __func__);
6575 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
6577 return rc < 0 ? -errno : 0;
6580 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
6581 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6586 fd = open(path, O_RDONLY);
6589 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
6592 * If we can't even open a file on the filesystem (e.g. with
6593 * -ESHUTDOWN), force caller to exit or it will loop forever.
6598 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
6605 #define MAX_STRING_SIZE 128
6607 int llapi_ping(char *obd_type, char *obd_name)
6609 int flags = O_RDONLY;
6610 char buf[1] = { 0 };
6614 rc = cfs_get_param_paths(&path, "%s/%s/ping",
6615 obd_type, obd_name);
6619 fd = open(path.gl_pathv[0], flags);
6621 if (errno == EACCES && flags == O_RDONLY) {
6626 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
6631 if (flags == O_RDONLY)
6632 rc = read(fd, buf, sizeof(buf));
6634 rc = write(fd, buf, sizeof(buf));
6642 cfs_free_param_data(&path);
6646 int llapi_target_iterate(int type_num, char **obd_type,
6647 void *args, llapi_cb_t cb)
6653 for (i = 0; i < type_num; i++) {
6656 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
6660 for (j = 0; j < param.gl_pathc; j++) {
6661 char obd_uuid[UUID_MAX + 1];
6665 fp = fopen(param.gl_pathv[j], "r");
6668 llapi_error(LLAPI_MSG_ERROR, rc,
6669 "error: opening '%s'",
6674 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
6676 llapi_error(LLAPI_MSG_ERROR, rc,
6677 "error: reading '%s'",
6682 /* Extract the obd_name from the sysfs path.
6683 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
6685 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
6691 /* skip /fs/lustre/'obd_type'/ */
6692 obd_name += strlen(obd_type[i]) + 12;
6693 /* chop off after obd_name */
6694 ptr = strrchr(obd_name, '/');
6698 cb(obd_type[i], obd_name, obd_uuid, args);
6707 cfs_free_param_data(¶m);
6711 struct check_target_filter {
6716 static void do_target_check(char *obd_type_name, char *obd_name,
6717 char *obd_uuid, void *args)
6720 struct check_target_filter *filter = args;
6722 if (filter != NULL) {
6723 /* check nid if obd type is mgc */
6724 if (strcmp(obd_type_name, "mgc") == 0) {
6725 if (strcmp(obd_name + 3, filter->nid) != 0)
6728 /* check instance for other types of device (osc/mdc) */
6729 else if (strstr(obd_name, filter->instance) == NULL)
6733 rc = llapi_ping(obd_type_name, obd_name);
6735 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
6737 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
6739 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
6742 int llapi_target_check(int type_num, char **obd_type, char *dir)
6744 char nid[MAX_LINE_LEN], instance[MAX_INSTANCE_LEN];
6745 struct check_target_filter filter = {NULL, NULL};
6748 if (dir == NULL || dir[0] == '\0')
6749 return llapi_target_iterate(type_num, obd_type, NULL,
6752 rc = get_root_path(WANT_NID | WANT_ERROR, NULL, NULL, dir, -1, NULL,
6755 llapi_error(LLAPI_MSG_ERROR, rc,
6756 "cannot get nid of path '%s'", dir);
6761 rc = llapi_get_instance(dir, instance, ARRAY_SIZE(instance));
6764 filter.instance = instance;
6766 return llapi_target_iterate(type_num, obd_type, &filter,
6770 #undef MAX_STRING_SIZE
6772 /* Is this a lustre fs? */
6773 int llapi_is_lustre_mnttype(const char *type)
6775 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
6778 /* Is this a lustre client fs? */
6779 int llapi_is_lustre_mnt(struct mntent *mnt)
6781 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
6782 strstr(mnt->mnt_fsname, ":/") != NULL);
6785 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
6787 char fsname[PATH_MAX + 1];
6791 rc = llapi_search_fsname(mnt, fsname);
6795 root = open(mnt, O_RDONLY | O_DIRECTORY);
6798 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
6802 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
6805 if (rc == -ENOENT && LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd))
6806 llapi_error(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, rc,
6807 "Cannot find pool '%s'", qctl->qc_poolname);
6813 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
6818 root = open(mnt, O_RDONLY | O_DIRECTORY);
6821 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
6825 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
6828 llapi_error(LLAPI_MSG_ERROR, rc,
6829 "ioctl on %s for getting connect flags failed", mnt);
6836 * Flush cached pages from all clients.
6838 * \param fd File descriptor
6842 int llapi_file_flush(int fd)
6846 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);