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
88 #ifndef INVALID_PROJID
89 #define INVALID_PROJID -1
92 static int llapi_msg_level = LLAPI_MSG_MAX;
93 const char *liblustreapi_cmd;
95 struct lustre_foreign_type lu_foreign_types[] = {
96 {.lft_type = LU_FOREIGN_TYPE_NONE, .lft_name = "none"},
97 {.lft_type = LU_FOREIGN_TYPE_SYMLINK, .lft_name = "symlink"},
98 /* must be the last element */
99 {.lft_type = LU_FOREIGN_TYPE_UNKNOWN, .lft_name = NULL}
100 /* array max dimension must be <= UINT32_MAX */
103 void llapi_msg_set_level(int level)
105 /* ensure level is in the good range */
106 if (level < LLAPI_MSG_OFF)
107 llapi_msg_level = LLAPI_MSG_OFF;
108 else if (level > LLAPI_MSG_MAX)
109 llapi_msg_level = LLAPI_MSG_MAX;
111 llapi_msg_level = level;
114 int llapi_msg_get_level(void)
116 return llapi_msg_level;
119 void llapi_set_command_name(const char *cmd)
121 liblustreapi_cmd = cmd;
124 void llapi_clear_command_name(void)
126 liblustreapi_cmd = NULL;
129 static void error_callback_default(enum llapi_message_level level, int err,
130 const char *fmt, va_list ap)
132 bool has_nl = strchr(fmt, '\n') != NULL;
134 if (liblustreapi_cmd != NULL)
135 fprintf(stderr, "%s %s: ", program_invocation_short_name,
138 fprintf(stderr, "%s: ", program_invocation_short_name);
141 if (level & LLAPI_MSG_NO_ERRNO) {
142 vfprintf(stderr, fmt, ap);
144 fprintf(stderr, "\n");
149 * Remove trailing linefeed so error string can be appended.
150 * @fmt is a const string, so we can't modify it directly.
152 if (has_nl && (newfmt = strdup(fmt)))
153 *strrchr(newfmt, '\n') = '\0';
155 newfmt = (char *)fmt;
157 vfprintf(stderr, newfmt, ap);
160 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
164 static void info_callback_default(enum llapi_message_level level, int err,
165 const char *fmt, va_list ap)
168 if (liblustreapi_cmd != NULL) {
169 fprintf(stdout, "%s %s: ",
170 program_invocation_short_name,
173 fprintf(stdout, "%s: ", program_invocation_short_name);
176 vfprintf(stdout, fmt, ap);
179 static llapi_log_callback_t llapi_error_callback = error_callback_default;
180 static llapi_log_callback_t llapi_info_callback = info_callback_default;
183 /* llapi_error will preserve errno */
184 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
187 int tmp_errno = errno;
189 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
193 llapi_error_callback(level, abs(err), fmt, args);
198 /* llapi_printf will preserve errno */
199 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
202 int tmp_errno = errno;
204 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
208 llapi_info_callback(level, 0, fmt, args);
214 * Set a custom error logging function. Passing in NULL will reset the logging
215 * callback to its default value.
217 * This function returns the value of the old callback.
219 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
221 llapi_log_callback_t old = llapi_error_callback;
224 llapi_error_callback = cb;
226 llapi_error_callback = error_callback_default;
232 * Set a custom info logging function. Passing in NULL will reset the logging
233 * callback to its default value.
235 * This function returns the value of the old callback.
237 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
239 llapi_log_callback_t old = llapi_info_callback;
242 llapi_info_callback = cb;
244 llapi_info_callback = info_callback_default;
250 * Convert a size string (with optional suffix) into binary value.
252 * \param optarg [in] string containing numeric value with optional
253 * KMGTPE suffix to specify the unit size.
254 * The \a string may be a decimal value.
255 * \param size [out] pointer to integer numeric value to be returned
256 * \param size_units [in] units of \a string if dimensionless. Must be
257 * initialized by caller. If zero, units = bytes.
258 * \param bytes_spec [in] if suffix 'b' means bytes or 512-byte sectors.
261 * \retval -EINVAL negative or too large size, or unknown suffix
263 int llapi_parse_size(const char *optarg, unsigned long long *size,
264 unsigned long long *size_units, int bytes_spec)
267 char *argbuf = (char *)optarg;
268 unsigned long long frac = 0, frac_d = 1;
270 if (strncmp(optarg, "-", 1) == 0)
273 if (*size_units == 0)
276 *size = strtoull(argbuf, &end, 0);
277 if (end != NULL && *end == '.') {
281 frac = strtoull(argbuf, &end, 10);
282 /* count decimal places */
283 for (i = 0; i < (end - argbuf); i++)
288 char next = tolower(*(end + 1));
290 switch (tolower(*end)) {
295 if (*size & (~0ULL << (64 - 9)))
297 *size_units = 1 << 9;
304 if (*size & (~0ULL << (64 - 10)))
306 *size_units = 1 << 10;
309 if (*size & (~0ULL << (64 - 20)))
311 *size_units = 1 << 20;
314 if (*size & (~0ULL << (64 - 30)))
316 *size_units = 1 << 30;
319 if (*size & (~0ULL << (64 - 40)))
321 *size_units = 1ULL << 40;
324 if (*size & (~0ULL << (64 - 50)))
326 *size_units = 1ULL << 50;
329 if (*size & (~0ULL << (64 - 60)))
331 *size_units = 1ULL << 60;
336 if (next != '\0' && next != 'i' && next != 'b')
339 *size = *size * *size_units + frac * *size_units / frac_d;
345 * Verify the setstripe parameters before using.
346 * This is a pair method for comp_args_to_layout()/llapi_layout_sanity_cb()
347 * when just 1 component or a non-PFL layout is given.
349 * \param[in] param stripe parameters
350 * \param[in] pool_name pool name
351 * \param[in] fsname lustre FS name
354 * < 0, error code on failre
356 static int llapi_stripe_param_verify(const struct llapi_stripe_param *param,
357 const char **pool_name, char *fsname)
360 static int page_size;
363 if (page_size == 0) {
365 * 64 KB is the largest common page size (on ia64/PPC/ARM),
366 * but check the local page size just in case. The page_size
367 * will not change for the lifetime of this process at least.
369 page_size = LOV_MIN_STRIPE_SIZE;
370 if (getpagesize() > page_size) {
371 page_size = getpagesize();
372 llapi_err_noerrno(LLAPI_MSG_WARN,
373 "warning: page size (%u) larger than expected (%u)",
374 page_size, LOV_MIN_STRIPE_SIZE);
377 if (!llapi_stripe_size_is_aligned(param->lsp_stripe_size)) {
379 llapi_error(LLAPI_MSG_ERROR, rc,
380 "error: bad stripe_size %llu, must be an even multiple of %d bytes",
381 param->lsp_stripe_size, page_size);
384 if (!llapi_stripe_index_is_valid(param->lsp_stripe_offset)) {
386 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
387 param->lsp_stripe_offset);
390 if (llapi_stripe_size_is_too_big(param->lsp_stripe_size)) {
392 llapi_error(LLAPI_MSG_ERROR, rc,
393 "error: stripe size '%llu' over 4GB limit",
394 param->lsp_stripe_size);
398 count = param->lsp_stripe_count;
399 if (param->lsp_stripe_pattern & LOV_PATTERN_MDT) {
401 llapi_error(LLAPI_MSG_ERROR, rc,
402 "Invalid pattern: '-L mdt', must be specified "
406 if (!llapi_stripe_count_is_valid(count)) {
408 llapi_error(LLAPI_MSG_ERROR, rc,
409 "Invalid stripe count %d\n", count);
414 /* Make sure we have a good pool */
415 if (*pool_name != NULL) {
416 if (!llapi_pool_name_is_valid(pool_name)) {
418 llapi_error(LLAPI_MSG_ERROR, rc,
419 "Invalid Poolname '%s'", *pool_name);
423 /* Make sure the pool exists */
424 rc = llapi_search_ost(fsname, *pool_name, NULL);
426 llapi_error(LLAPI_MSG_ERROR, rc,
427 "pool '%s fsname %s' does not exist",
439 static int dir_stripe_limit_check(int stripe_offset, int stripe_count,
444 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
446 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
450 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
452 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
457 if (!llapi_dir_hash_type_is_valid(hash_type)) {
459 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
467 * Trim a trailing newline from a string, if it exists.
469 int llapi_chomp_string(char *buf)
485 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
487 static int get_param_lov(const char *path, const char *param,
488 char *buf, size_t buf_size)
490 struct obd_uuid uuid;
493 rc = llapi_file_get_lov_uuid(path, &uuid);
497 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
502 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
504 static int get_param_lmv(const char *path, const char *param,
505 char *buf, size_t buf_size)
507 struct obd_uuid uuid;
510 rc = llapi_file_get_lmv_uuid(path, &uuid);
514 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
518 static int get_mds_md_size(const char *path)
520 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
523 * Rather than open the file and do the ioctl to get the
524 * instance name and close the file and search for the param
525 * file and open the param file and read the param file and
526 * parse the value and close the param file, let's just return
527 * a large enough value. It's 2020, RAM is cheap and this is
531 if (md_size < XATTR_SIZE_MAX)
532 md_size = XATTR_SIZE_MAX;
537 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
539 return get_param_lmv(path, "uuid", buf, bufsize);
543 * Open a Lustre file.
545 * \param name the name of the file to be opened
546 * \param flags access mode, see flags in open(2)
547 * \param mode permission of the file if it is created, see mode in open(2)
548 * \param param stripe pattern of the newly created file
550 * \retval file descriptor of opened file
551 * \retval negative errno on failure
553 int llapi_file_open_param(const char *name, int flags, mode_t mode,
554 const struct llapi_stripe_param *param)
556 char fsname[MAX_OBD_NAME + 1] = { 0 };
557 struct lov_user_md *lum = NULL;
558 const char *pool_name = param->lsp_pool;
562 /* Make sure we are on a Lustre file system */
563 rc = llapi_search_fsname(name, fsname);
565 llapi_error(LLAPI_MSG_ERROR, rc,
566 "'%s' is not on a Lustre filesystem", name);
570 /* Check if the stripe pattern is sane. */
571 rc = llapi_stripe_param_verify(param, &pool_name, fsname);
575 if (param->lsp_is_specific)
576 lum_size = lov_user_md_size(param->lsp_stripe_count,
577 LOV_USER_MAGIC_SPECIFIC);
579 lum_size = sizeof(struct lov_user_md_v3);
581 lum_size = sizeof(*lum);
583 lum = calloc(1, lum_size);
588 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
590 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
591 flags = O_DIRECTORY | O_RDONLY;
598 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
603 /* Initialize IOCTL striping pattern structure */
604 lum->lmm_magic = LOV_USER_MAGIC_V1;
605 lum->lmm_pattern = param->lsp_stripe_pattern;
606 lum->lmm_stripe_size = param->lsp_stripe_size;
607 lum->lmm_stripe_count = param->lsp_stripe_count;
608 lum->lmm_stripe_offset = param->lsp_stripe_offset;
609 if (pool_name != NULL) {
610 struct lov_user_md_v3 *lumv3 = (void *)lum;
612 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
613 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
615 if (param->lsp_is_specific) {
616 struct lov_user_md_v3 *lumv3 = (void *)lum;
619 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
620 if (pool_name == NULL) {
622 * LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
623 * OST list, therefore if pool is not specified we have
624 * to pack a null pool name for placeholder.
626 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
629 for (i = 0; i < param->lsp_stripe_count; i++)
630 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
633 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
634 char errmsg[512] = "stripe already set";
637 if (errno != EEXIST && errno != EALREADY)
638 strncpy(errmsg, strerror(errno), sizeof(errmsg) - 1);
639 if (rc == -EREMOTEIO)
640 snprintf(errmsg, sizeof(errmsg),
641 "inactive OST among your specified %d OST(s)",
642 param->lsp_stripe_count);
644 llapi_err_noerrno(LLAPI_MSG_ERROR,
645 "setstripe error for '%s': %s", name, errmsg);
656 int llapi_file_is_encrypted(int fd)
661 rc = ioctl(fd, FS_IOC_GETFLAGS, &flags);
665 return !!(flags & LUSTRE_ENCRYPT_FL);
668 int llapi_file_open_pool(const char *name, int flags, int mode,
669 unsigned long long stripe_size, int stripe_offset,
670 int stripe_count, int stripe_pattern, char *pool_name)
672 const struct llapi_stripe_param param = {
673 .lsp_stripe_size = stripe_size,
674 .lsp_stripe_count = stripe_count,
675 .lsp_stripe_pattern = stripe_pattern,
676 .lsp_stripe_offset = stripe_offset,
677 .lsp_pool = pool_name
679 return llapi_file_open_param(name, flags, mode, ¶m);
682 int llapi_file_open(const char *name, int flags, int mode,
683 unsigned long long stripe_size, int stripe_offset,
684 int stripe_count, int stripe_pattern)
686 return llapi_file_open_pool(name, flags, mode, stripe_size,
687 stripe_offset, stripe_count,
688 stripe_pattern, NULL);
691 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
692 __u32 flags, char *foreign_lov)
695 struct lov_foreign_md *lfm;
698 if (foreign_lov == NULL) {
700 llapi_error(LLAPI_MSG_ERROR, rc,
701 "foreign LOV EA content must be provided");
705 len = strlen(foreign_lov);
706 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
709 llapi_error(LLAPI_MSG_ERROR, rc,
710 "foreign LOV EA size %zu (must be 0 < len < %zu)",
711 len, XATTR_SIZE_MAX -
712 offsetof(struct lov_foreign_md, lfm_value));
716 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
719 llapi_error(LLAPI_MSG_ERROR, rc,
720 "failed to allocate lov_foreign_md");
724 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
727 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
731 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
732 lfm->lfm_length = len;
733 lfm->lfm_type = type;
734 lfm->lfm_flags = flags;
735 memcpy(lfm->lfm_value, foreign_lov, len);
737 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
738 char *errmsg = "stripe already set";
742 errmsg = "not on a Lustre filesystem";
743 else if (errno == EEXIST || errno == EALREADY)
744 errmsg = "stripe already set";
746 errmsg = strerror(errno);
748 llapi_err_noerrno(LLAPI_MSG_ERROR,
749 "setstripe error for '%s': %s", name, errmsg);
765 int llapi_file_create(const char *name, unsigned long long stripe_size,
766 int stripe_offset, int stripe_count, int stripe_pattern)
770 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
771 stripe_offset, stripe_count, stripe_pattern,
780 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
781 int stripe_offset, int stripe_count,
782 int stripe_pattern, char *pool_name)
786 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
787 stripe_offset, stripe_count, stripe_pattern,
796 static int verify_dir_param(const char *name,
797 const struct llapi_stripe_param *param)
799 char fsname[MAX_OBD_NAME + 1] = { 0 };
800 char *pool_name = param->lsp_pool;
803 /* Make sure we are on a Lustre file system */
804 rc = llapi_search_fsname(name, fsname);
806 llapi_error(LLAPI_MSG_ERROR, rc,
807 "'%s' is not on a Lustre filesystem",
812 /* Check if the stripe pattern is sane. */
813 rc = dir_stripe_limit_check(param->lsp_stripe_offset,
814 param->lsp_stripe_count,
815 param->lsp_stripe_pattern);
819 /* Make sure we have a good pool */
820 if (pool_name != NULL) {
822 * in case user gives the full pool name <fsname>.<poolname>,
825 char *ptr = strchr(pool_name, '.');
829 if (strcmp(pool_name, fsname) != 0) {
831 llapi_err_noerrno(LLAPI_MSG_ERROR,
832 "Pool '%s' is not on filesystem '%s'",
839 /* Make sure the pool exists and is non-empty */
840 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
842 char *err = rc == 0 ? "has no OSTs" : "does not exist";
844 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
845 fsname, pool_name, err);
850 /* sanity check of target list */
851 if (param->lsp_is_specific) {
852 char mdtname[MAX_OBD_NAME + 64];
856 for (i = 0; i < param->lsp_stripe_count; i++) {
857 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
858 fsname, param->lsp_tgts[i]);
859 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
864 llapi_error(LLAPI_MSG_ERROR, rc,
865 "%s: cannot find MDT %s in %s",
872 /* Make sure stripe offset is in MDT list. */
873 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
877 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
878 "%s: stripe offset '%d' is not in the target list",
879 __func__, param->lsp_stripe_offset);
887 static inline void param2lmu(struct lmv_user_md *lmu,
888 const struct llapi_stripe_param *param)
890 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
892 lmu->lum_stripe_count = param->lsp_stripe_count;
893 lmu->lum_stripe_offset = param->lsp_stripe_offset;
894 lmu->lum_hash_type = param->lsp_stripe_pattern;
895 lmu->lum_max_inherit = param->lsp_max_inherit;
896 lmu->lum_max_inherit_rr = param->lsp_max_inherit_rr;
897 if (param->lsp_pool != NULL)
898 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
899 if (param->lsp_is_specific) {
902 for (i = 0; i < param->lsp_stripe_count; i++)
903 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
907 int llapi_dir_set_default_lmv(const char *name,
908 const struct llapi_stripe_param *param)
910 struct lmv_user_md lmu = { 0 };
914 rc = verify_dir_param(name, param);
918 /* TODO: default lmv doesn't support specific targets yet */
919 if (param->lsp_is_specific)
922 param2lmu(&lmu, param);
924 fd = open(name, O_DIRECTORY | O_RDONLY);
927 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
931 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
933 char *errmsg = "stripe already set";
936 if (errno != EEXIST && errno != EALREADY)
937 errmsg = strerror(errno);
939 llapi_err_noerrno(LLAPI_MSG_ERROR,
940 "default dirstripe error on '%s': %s",
947 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
948 int stripe_count, int stripe_pattern,
949 const char *pool_name)
951 const struct llapi_stripe_param param = {
952 .lsp_stripe_count = stripe_count,
953 .lsp_stripe_offset = stripe_offset,
954 .lsp_stripe_pattern = stripe_pattern,
955 .lsp_pool = (char *)pool_name
958 return llapi_dir_set_default_lmv(name, ¶m);
962 * Create a Lustre directory.
964 * \param name the name of the directory to be created
965 * \param mode permission of the file if it is created, see mode in open(2)
966 * \param param stripe pattern of the newly created directory
968 * \retval 0 on success
969 * \retval negative errno on failure
971 int llapi_dir_create(const char *name, mode_t mode,
972 const struct llapi_stripe_param *param)
974 struct lmv_user_md *lmu = NULL;
976 struct obd_ioctl_data data = { 0 };
979 char *dirpath = NULL;
980 char *namepath = NULL;
985 rc = verify_dir_param(name, param);
989 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
990 param->lsp_is_specific ?
991 LMV_USER_MAGIC_SPECIFIC :
994 lmu = calloc(1, lmu_size);
998 dirpath = strdup(name);
1004 namepath = strdup(name);
1011 param2lmu(lmu, param);
1013 filename = basename(namepath);
1014 dir = dirname(dirpath);
1016 data.ioc_inlbuf1 = (char *)filename;
1017 data.ioc_inllen1 = strlen(filename) + 1;
1018 data.ioc_inlbuf2 = (char *)lmu;
1019 data.ioc_inllen2 = lmu_size;
1020 data.ioc_type = mode;
1021 if (param->lsp_is_create)
1022 /* borrow obdo1.o_flags to store this flag */
1023 data.ioc_obdo1.o_flags = OBD_FL_OBDMDEXISTS;
1024 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1026 llapi_error(LLAPI_MSG_ERROR, rc,
1027 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1032 fd = open(dir, O_DIRECTORY | O_RDONLY);
1035 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1039 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1040 char *errmsg = "stripe already set";
1043 if (errno != EEXIST && errno != EALREADY)
1044 errmsg = strerror(errno);
1046 llapi_err_noerrno(LLAPI_MSG_ERROR,
1047 "dirstripe error on '%s': %s", name, errmsg);
1058 * Create a foreign directory.
1060 * \param name the name of the directory to be created
1061 * \param mode permission of the file if it is created, see mode in open(2)
1062 * \param type foreign type to be set in LMV EA
1063 * \param flags foreign flags to be set in LMV EA
1064 * \param value foreign pattern to be set in LMV EA
1066 * \retval 0 on success
1067 * \retval negative errno on failure
1069 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1070 __u32 flags, const char *value)
1072 struct lmv_foreign_md *lfm = NULL;
1073 size_t lfm_size, len;
1074 struct obd_ioctl_data data = { 0 };
1077 char *dirpath = NULL;
1078 char *namepath = NULL;
1083 len = strlen(value);
1084 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1087 llapi_error(LLAPI_MSG_ERROR, rc,
1088 "invalid LOV EA length %zu (must be 0 < len < %zu)",
1089 len, XATTR_SIZE_MAX -
1090 offsetof(struct lmv_foreign_md, lfm_value));
1093 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1094 lfm = calloc(1, lfm_size);
1098 dirpath = strdup(name);
1104 namepath = strdup(name);
1111 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1112 lfm->lfm_length = len;
1113 lfm->lfm_type = type;
1114 lfm->lfm_flags = flags;
1115 memcpy(lfm->lfm_value, value, len);
1117 filename = basename(namepath);
1118 dir = dirname(dirpath);
1120 data.ioc_inlbuf1 = (char *)filename;
1121 data.ioc_inllen1 = strlen(filename) + 1;
1122 data.ioc_inlbuf2 = (char *)lfm;
1123 data.ioc_inllen2 = lfm_size;
1124 data.ioc_type = mode;
1125 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1127 llapi_error(LLAPI_MSG_ERROR, rc,
1128 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1133 fd = open(dir, O_DIRECTORY | O_RDONLY);
1136 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1140 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1141 char *errmsg = "stripe already set";
1144 if (errno != EEXIST && errno != EALREADY)
1145 errmsg = strerror(errno);
1147 llapi_err_noerrno(LLAPI_MSG_ERROR,
1148 "dirstripe error on '%s': %s", name, errmsg);
1158 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1159 int stripe_count, int stripe_pattern,
1160 const char *pool_name)
1162 const struct llapi_stripe_param param = {
1163 .lsp_stripe_count = stripe_count,
1164 .lsp_stripe_offset = stripe_offset,
1165 .lsp_stripe_pattern = stripe_pattern,
1166 .lsp_pool = (char *)pool_name
1169 return llapi_dir_create(name, mode, ¶m);
1173 * Get the list of pool members.
1174 * \param poolname string of format \<fsname\>.\<poolname\>
1175 * \param members caller-allocated array of char*
1176 * \param list_size size of the members array
1177 * \param buffer caller-allocated buffer for storing OST names
1178 * \param buffer_size size of the buffer
1180 * \return number of members retrieved for this pool
1181 * \retval -error failure
1183 int llapi_get_poolmembers(const char *poolname, char **members,
1184 int list_size, char *buffer, int buffer_size)
1186 char fsname[PATH_MAX];
1195 /* name is FSNAME.POOLNAME */
1196 if (strlen(poolname) >= sizeof(fsname))
1199 snprintf(fsname, sizeof(fsname), "%s", poolname);
1200 pool = strchr(fsname, '.');
1207 rc = poolpath(&pathname, fsname, NULL);
1209 llapi_error(LLAPI_MSG_ERROR, rc,
1210 "Lustre filesystem '%s' not found",
1215 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1216 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1217 cfs_free_param_data(&pathname);
1218 if (rc >= sizeof(buf))
1220 fd = fopen(buf, "r");
1223 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1228 while (fgets(buf, sizeof(buf), fd) != NULL) {
1229 if (nb_entries >= list_size) {
1233 buf[sizeof(buf) - 1] = '\0';
1235 tmp = strchr(buf, '\n');
1238 if (used + strlen(buf) + 1 > buffer_size) {
1243 strcpy(buffer + used, buf);
1244 members[nb_entries] = buffer + used;
1245 used += strlen(buf) + 1;
1255 * Get the list of pools in a filesystem.
1256 * \param name filesystem name or path
1257 * \param poollist caller-allocated array of char*
1258 * \param list_size size of the poollist array
1259 * \param buffer caller-allocated buffer for storing pool names
1260 * \param buffer_size size of the buffer
1262 * \return number of pools retrieved for this filesystem
1263 * \retval -error failure
1265 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1266 char *buffer, int buffer_size)
1272 struct dirent *pool;
1274 unsigned int nb_entries = 0;
1275 unsigned int used = 0;
1278 /* initialize output array */
1279 for (i = 0; i < list_size; i++)
1282 /* is name a pathname ? */
1283 ptr = strchr(name, '/');
1285 char fsname_buf[MAXNAMLEN];
1287 /* We will need fsname for printing later */
1288 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1292 ptr = strrchr(fsname_buf, '-');
1296 fsname = strdup(fsname_buf);
1300 /* name is FSNAME */
1301 fsname = strdup(name);
1306 rc = poolpath(&pathname, fsname, NULL);
1308 llapi_error(LLAPI_MSG_ERROR, rc,
1309 "Lustre filesystem '%s' not found", name);
1313 dir = opendir(pathname.gl_pathv[0]);
1316 llapi_error(LLAPI_MSG_ERROR, rc,
1317 "Could not open pool list for '%s'",
1324 pool = readdir(dir);
1330 /* ignore . and .. */
1331 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1334 /* check output bounds */
1335 if (nb_entries >= list_size) {
1337 goto free_dir_no_msg;
1340 /* +2 for '.' and final '\0' */
1341 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1344 goto free_dir_no_msg;
1347 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1348 poollist[nb_entries] = buffer + used;
1349 used += strlen(pool->d_name) + strlen(fsname) + 2;
1355 llapi_error(LLAPI_MSG_ERROR, rc,
1356 "Error reading pool list for '%s'", name);
1358 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1363 cfs_free_param_data(&pathname);
1366 return rc != 0 ? rc : nb_entries;
1369 /* wrapper for lfs.c and obd.c */
1370 int llapi_poollist(const char *name)
1372 int poolcount, rc, i;
1375 rc = llapi_get_poolbuf(name, &buf, &pools, &poolcount);
1379 for (i = 0; i < poolcount; i++)
1380 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", pools[i]);
1387 * Get buffer that holds uuids and the list of pools in a filesystem.
1389 * \param name filesystem name or path
1390 * \param buf bufffer that has to be freed if function returns 0
1391 * \param pools pointer to the list of pools in buffer
1392 * \param poolcount number of pools
1394 * \return 0 when found at least 1 pool, i.e. poolcount > 0
1395 * \retval -error failure
1397 int llapi_get_poolbuf(const char *name, char **buf,
1398 char ***pools, int *poolcount)
1401 * list of pool names (assume that pool count is smaller
1404 char **list, *buffer = NULL, *fsname = (char *)name;
1405 char *poolname = NULL, *tmp = NULL, data[16];
1406 enum param_filter type = FILTER_BY_PATH;
1407 int obdcount, bufsize, rc, nb;
1412 if (name[0] != '/') {
1413 fsname = strdup(name);
1417 poolname = strchr(fsname, '.');
1420 type = FILTER_BY_FS_NAME;
1423 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1424 data, sizeof(data));
1427 obdcount = atoi(data);
1430 * Allocate space for each fsname-OST0000_UUID, 1 per OST,
1431 * and also an array to store the pointers for all that
1435 bufsize = sizeof(struct obd_uuid) * obdcount;
1436 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1437 if (buffer == NULL) {
1441 list = (char **) (buffer + bufsize);
1444 /* name is a path or fsname */
1445 nb = llapi_get_poollist(name, list, obdcount,
1448 /* name is a pool name (<fsname>.<poolname>) */
1449 nb = llapi_get_poolmembers(name, list, obdcount,
1453 if (nb == -EOVERFLOW) {
1456 goto retry_get_pools;
1459 rc = (nb < 0 ? nb : 0);
1466 /* Don't free buffer, it will be used later */
1469 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1474 typedef int (semantic_func_t)(char *path, int p, int *d,
1475 void *data, struct dirent64 *de);
1477 #define OBD_NOT_FOUND (-1)
1479 static bool lmv_is_foreign(__u32 magic)
1481 return magic == LMV_MAGIC_FOREIGN;
1484 static void find_param_fini(struct find_param *param)
1486 if (param->fp_migrate)
1489 if (param->fp_obd_indexes) {
1490 free(param->fp_obd_indexes);
1491 param->fp_obd_indexes = NULL;
1494 if (param->fp_lmd) {
1495 free(param->fp_lmd);
1496 param->fp_lmd = NULL;
1499 if (param->fp_lmv_md) {
1500 free(param->fp_lmv_md);
1501 param->fp_lmv_md = NULL;
1505 static int common_param_init(struct find_param *param, char *path)
1507 int lum_size = get_mds_md_size(path);
1512 /* migrate has fp_lmv_md initialized outside */
1513 if (param->fp_migrate)
1516 if (lum_size < PATH_MAX + 1)
1517 lum_size = PATH_MAX + 1;
1519 param->fp_lum_size = lum_size;
1520 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1522 if (param->fp_lmd == NULL) {
1523 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1524 "error: allocate %zu bytes for layout failed",
1525 sizeof(lstat_t) + param->fp_lum_size);
1529 param->fp_lmv_stripe_count = 256;
1530 param->fp_lmv_md = calloc(1,
1531 lmv_user_md_size(param->fp_lmv_stripe_count,
1532 LMV_USER_MAGIC_SPECIFIC));
1533 if (param->fp_lmv_md == NULL) {
1534 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1535 "error: allocation of %d bytes for ioctl",
1536 lmv_user_md_size(param->fp_lmv_stripe_count,
1537 LMV_USER_MAGIC_SPECIFIC));
1538 find_param_fini(param);
1542 param->fp_got_uuids = 0;
1543 param->fp_obd_indexes = NULL;
1544 param->fp_obd_index = OBD_NOT_FOUND;
1545 param->fp_mdt_index = OBD_NOT_FOUND;
1549 static int cb_common_fini(char *path, int p, int *dp, void *data,
1550 struct dirent64 *de)
1552 struct find_param *param = data;
1558 /* set errno upon failure */
1559 static int open_parent(const char *path)
1565 path_copy = strdup(path);
1566 if (path_copy == NULL)
1569 parent_path = dirname(path_copy);
1570 parent = open(parent_path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1576 static int cb_get_dirstripe(char *path, int *d, struct find_param *param)
1579 bool did_nofollow = false;
1584 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1585 if (param->fp_get_default_lmv) {
1589 /* open() may not fetch LOOKUP lock, statx() to ensure dir depth
1592 statx(*d, "", AT_EMPTY_PATH, STATX_MODE, &stx);
1598 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1600 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1603 param->fp_lmv_md->lum_type = LMV_TYPE_RAW;
1605 ret = ioctl(*d, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1607 /* if ENOTTY likely to be a fake symlink, so try again after
1608 * new open() with O_NOFOLLOW, but only once to prevent any
1609 * loop like for the path of a file/dir not on Lustre !!
1611 if (ret < 0 && errno == ENOTTY && !did_nofollow) {
1614 did_nofollow = true;
1615 fd = open(path, O_RDONLY | O_NOFOLLOW);
1617 /* restore original errno */
1622 /* close original fd and set new */
1625 ret2 = ioctl(fd, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1626 if (ret2 < 0 && errno != E2BIG) {
1627 /* restore original errno */
1631 /* LMV is ok or need to handle E2BIG case now */
1635 if (errno == E2BIG && ret != 0) {
1639 /* if foreign LMV case, fake stripes number */
1640 if (lmv_is_foreign(param->fp_lmv_md->lum_magic)) {
1641 struct lmv_foreign_md *lfm;
1643 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1644 if (lfm->lfm_length < XATTR_SIZE_MAX -
1645 offsetof(typeof(*lfm), lfm_value)) {
1646 uint32_t size = lfm->lfm_length +
1647 offsetof(typeof(*lfm), lfm_value);
1649 stripe_count = lmv_foreign_to_md_stripes(size);
1651 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1652 "error: invalid %d foreign size returned from ioctl",
1657 stripe_count = param->fp_lmv_md->lum_stripe_count;
1659 if (stripe_count <= param->fp_lmv_stripe_count)
1662 free(param->fp_lmv_md);
1663 param->fp_lmv_stripe_count = stripe_count;
1664 lmv_size = lmv_user_md_size(stripe_count,
1665 LMV_USER_MAGIC_SPECIFIC);
1666 param->fp_lmv_md = malloc(lmv_size);
1667 if (param->fp_lmv_md == NULL) {
1668 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1669 "error: allocation of %d bytes for ioctl",
1670 lmv_user_md_size(param->fp_lmv_stripe_count,
1671 LMV_USER_MAGIC_SPECIFIC));
1680 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1683 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
1684 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
1685 lmd_v2->lmd_stx.stx_uid = st->st_uid;
1686 lmd_v2->lmd_stx.stx_gid = st->st_gid;
1687 lmd_v2->lmd_stx.stx_mode = st->st_mode;
1688 lmd_v2->lmd_stx.stx_ino = st->st_ino;
1689 lmd_v2->lmd_stx.stx_size = st->st_size;
1690 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
1691 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
1692 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
1693 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
1694 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
1695 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
1696 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
1697 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
1698 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
1700 lmd_v2->lmd_flags = 0;
1702 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1704 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
1705 if (lmd_v2->lmd_stx.stx_size)
1706 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
1707 if (lmd_v2->lmd_stx.stx_blocks)
1708 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
1710 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1711 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
1712 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
1717 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
1719 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
1720 struct lov_user_mds_data *lmd_v2 = lmdbuf;
1724 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
1728 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
1731 st = lmd_v1->lmd_st;
1732 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
1733 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
1734 convert_lmd_statx(lmd_v2, &st, false);
1735 lmd_v2->lmd_lmmsize = 0;
1736 lmd_v2->lmd_padding = 0;
1741 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
1742 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1744 struct lov_user_mds_data *lmd = lmdbuf;
1745 static bool use_old_ioctl;
1749 if (parent_fd < 0 && dir_fd < 0)
1751 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1756 * LL_IOC_MDC_GETINFO operates on the current directory inode
1757 * and returns struct lov_user_mds_data, while
1758 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
1760 if (type == GET_LMD_INFO)
1761 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_V1 :
1762 LL_IOC_MDC_GETINFO_V2;
1764 cmd = LL_IOC_LOV_GETSTRIPE;
1767 ret = ioctl(dir_fd, cmd, lmdbuf);
1768 if (ret < 0 && errno == ENOTTY &&
1769 cmd == LL_IOC_MDC_GETINFO_V2) {
1770 cmd = LL_IOC_MDC_GETINFO_V1;
1771 use_old_ioctl = true;
1775 if (cmd == LL_IOC_MDC_GETINFO_V1 && !ret)
1776 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
1778 if (ret < 0 && errno == ENOTTY && type == GET_LMD_STRIPE) {
1781 /* retry ioctl() after new open() with O_NOFOLLOW
1782 * just in case it could be a fake symlink
1783 * need using a new open() as dir_fd is being closed
1787 dir_fd2 = open(path, O_RDONLY | O_NDELAY | O_NOFOLLOW);
1789 /* return original error */
1792 ret = ioctl(dir_fd2, cmd, lmdbuf);
1793 /* pass new errno or success back to caller */
1799 } else if (parent_fd >= 0) {
1800 const char *fname = strrchr(path, '/');
1803 * IOC_MDC_GETFILEINFO takes as input the filename (relative to
1804 * the parent directory) and returns struct lov_user_mds_data,
1805 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
1807 * This avoids opening, locking, and closing each file on the
1808 * client if that is not needed. Multiple of these ioctl() can
1809 * be done on the parent dir with a single open for all
1810 * files in that directory, and it also doesn't pollute the
1811 * client dcache with millions of dentries when traversing
1812 * a large filesystem.
1814 fname = (fname == NULL ? path : fname + 1);
1816 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
1819 else if (ret >= lmdlen || ret++ == 0)
1822 if (type == GET_LMD_INFO)
1823 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_V1 :
1824 IOC_MDC_GETFILEINFO_V2;
1826 cmd = IOC_MDC_GETFILESTRIPE;
1829 ret = ioctl(parent_fd, cmd, lmdbuf);
1830 if (ret < 0 && errno == ENOTTY &&
1831 cmd == IOC_MDC_GETFILEINFO_V2) {
1832 cmd = IOC_MDC_GETFILEINFO_V1;
1833 use_old_ioctl = true;
1834 goto retry_getfileinfo;
1837 if (cmd == IOC_MDC_GETFILEINFO_V1 && !ret)
1838 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
1842 if (ret && type == GET_LMD_INFO) {
1843 if (errno == ENOTTY) {
1847 * ioctl is not supported, it is not a lustre fs.
1848 * Do the regular lstat(2) instead.
1850 ret = lstat_f(path, &st);
1853 llapi_error(LLAPI_MSG_ERROR, ret,
1854 "error: %s: lstat failed for %s",
1858 convert_lmd_statx(lmd, &st, true);
1860 * It may be wrong to set use_old_ioctl with true as
1861 * the file is not a lustre fs. So reset it with false
1864 use_old_ioctl = false;
1865 } else if (errno == ENOENT) {
1867 llapi_error(LLAPI_MSG_WARN, ret,
1868 "warning: %s does not exist", path);
1869 } else if (errno != EISDIR && errno != ENODATA) {
1871 llapi_error(LLAPI_MSG_ERROR, ret,
1872 "%s ioctl failed for %s.",
1873 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
1874 "IOC_MDC_GETFILEINFO", path);
1881 static int llapi_semantic_traverse(char *path, int size, int parent,
1882 semantic_func_t sem_init,
1883 semantic_func_t sem_fini, void *data,
1884 struct dirent64 *de)
1886 struct find_param *param = (struct find_param *)data;
1887 struct dirent64 *dent;
1888 int len, ret, d, p = -1;
1894 d = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1895 /* if an invalid fake dir symlink, opendir() will return EINVAL
1896 * instead of ENOTDIR. If a valid but dangling faked or real file/dir
1897 * symlink ENOENT will be returned. For a valid/resolved fake or real
1898 * file symlink ENOTDIR will be returned as for a regular file.
1899 * opendir() will be successful for a valid and resolved fake or real
1900 * dir simlink or a regular dir.
1902 if (d == -1 && errno != ENOTDIR && errno != EINVAL && errno != ENOENT) {
1904 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1907 } else if (d == -1) {
1908 if (errno == ENOENT || errno == EINVAL) {
1909 int old_errno = errno;
1911 /* try to open with O_NOFOLLOW this will help
1912 * differentiate fake vs real symlinks
1913 * it is ok to not use O_DIRECTORY with O_RDONLY
1914 * and it will prevent the need to deal with ENOTDIR
1915 * error, instead of ELOOP, being returned by recent
1916 * kernels for real symlinks
1918 d = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
1919 /* if a dangling real symlink should return ELOOP, or
1920 * again ENOENT if really non-existing path, or E...??
1921 * So return original error. If success or ENOTDIR, path
1922 * is likely to be a fake dir/file symlink, so continue
1932 if (parent == -1 && d == -1) {
1933 /* Open the parent dir. */
1934 p = open_parent(path);
1940 } else { /* d != -1 */
1943 /* try to reopen dir with O_NOFOLLOW just in case of a foreign
1946 d2 = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
1951 /* continue with d */
1957 ret = sem_init(path, (parent != -1) ? parent : p, &d, data, de);
1967 /* ENOTDIR if fake symlink, do not consider it as an error */
1968 if (errno != ENOTDIR)
1969 llapi_error(LLAPI_MSG_ERROR, errno,
1970 "fdopendir() failed");
1977 while ((dent = readdir64(dir)) != NULL) {
1980 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1984 if ((len + dent->d_reclen + 2) > size) {
1985 llapi_err_noerrno(LLAPI_MSG_ERROR,
1986 "error: %s: string buffer too small for %s",
1991 strcat(path, dent->d_name);
1993 if (dent->d_type == DT_UNKNOWN) {
1994 struct lov_user_mds_data *lmd = param->fp_lmd;
1996 rc = get_lmd_info_fd(path, d, -1, param->fp_lmd,
1997 param->fp_lum_size, GET_LMD_INFO);
1999 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2007 switch (dent->d_type) {
2009 llapi_err_noerrno(LLAPI_MSG_ERROR,
2010 "error: %s: '%s' is UNKNOWN type %d",
2011 __func__, dent->d_name, dent->d_type);
2014 rc = llapi_semantic_traverse(path, size, d, sem_init,
2015 sem_fini, data, dent);
2016 if (rc != 0 && ret == 0)
2018 if (rc < 0 && rc != -EALREADY &&
2019 param->fp_stop_on_error)
2025 rc = sem_init(path, d, NULL, data, dent);
2026 if (rc < 0 && ret == 0) {
2028 if (rc && rc != -EALREADY &&
2029 param->fp_stop_on_error)
2034 if (sem_fini && rc == 0)
2035 sem_fini(path, d, NULL, data, dent);
2043 sem_fini(path, parent, &d, data, de);
2056 static int param_callback(char *path, semantic_func_t sem_init,
2057 semantic_func_t sem_fini, struct find_param *param)
2059 int ret, len = strlen(path);
2062 if (len > PATH_MAX) {
2064 llapi_error(LLAPI_MSG_ERROR, ret,
2065 "Path name '%s' is too long", path);
2069 buf = (char *)malloc(2 * PATH_MAX);
2073 snprintf(buf, PATH_MAX + 1, "%s", path);
2074 ret = common_param_init(param, buf);
2078 param->fp_depth = 0;
2080 ret = llapi_semantic_traverse(buf, 2 * PATH_MAX, -1, sem_init,
2081 sem_fini, param, NULL);
2083 find_param_fini(param);
2085 return ret < 0 ? ret : 0;
2088 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2092 rc = llapi_ioctl(fd, OBD_IOC_GETDTNAME, lov_name);
2095 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2101 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2105 rc = llapi_ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2108 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2114 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2118 /* do not follow faked symlinks */
2119 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
2121 /* real symlink should have failed with ELOOP so retry without
2122 * O_NOFOLLOW just in case
2124 fd = open(path, O_RDONLY | O_NONBLOCK);
2127 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'",
2133 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2139 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2143 fd = open(path, O_RDONLY | O_NONBLOCK);
2146 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2150 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2162 * If uuidp is NULL, return the number of available obd uuids.
2163 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2164 * there are more OSTs than allocated to uuidp, then an error is returned with
2165 * the ost_count set to number of available obd uuids.
2167 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp, int *indices,
2168 int *ost_count, enum tgt_type type)
2170 char buf[PATH_MAX], format[32];
2172 struct obd_uuid name;
2176 /* Get the lov name */
2177 if (type == LOV_TYPE)
2178 rc = llapi_file_fget_lov_uuid(fd, &name);
2180 rc = llapi_file_fget_lmv_uuid(fd, &name);
2184 /* Now get the ost uuids */
2185 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2186 FILTER_BY_EXACT, "target_obd", ¶m);
2190 fp = fopen(param.gl_pathv[0], "r");
2193 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2198 snprintf(format, sizeof(format),
2199 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2200 for (i = 0; fgets(buf, sizeof(buf), fp); i++) {
2203 if (sscanf(buf, format, &index, name.uuid) < 2)
2206 if (i < *ost_count) {
2209 if (indices != NULL)
2215 if (uuidp && (i > *ost_count))
2220 cfs_free_param_data(¶m);
2224 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2226 return llapi_get_target_uuids(fd, uuidp, NULL, ost_count, LOV_TYPE);
2229 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2234 root = open(mnt, O_RDONLY | O_DIRECTORY);
2237 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2242 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2251 * Check if user specified value matches a real uuid. Ignore _UUID,
2252 * -osc-4ba41334, other trailing gunk in comparison.
2253 * @param real_uuid ends in "_UUID"
2254 * @param search_uuid may or may not end in "_UUID"
2256 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2258 int cmplen = strlen(real_uuid);
2259 int searchlen = strlen(search_uuid);
2261 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2263 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2267 * The UUIDs may legitimately be different lengths, if
2268 * the system was upgraded from an older version.
2270 if (cmplen != searchlen)
2273 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2277 * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2278 * returned in param->fp_obd_index
2280 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2282 struct obd_uuid obd_uuid;
2289 if (param->fp_got_uuids)
2292 /* Get the lov/lmv name */
2293 if (param->fp_get_lmv)
2294 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2296 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2298 if (rc != -ENOTTY) {
2299 llapi_error(LLAPI_MSG_ERROR, rc,
2300 "error: can't get %s name: %s",
2301 param->fp_get_lmv ? "lmv" : "lov",
2309 param->fp_got_uuids = 1;
2311 /* Now get the ost uuids */
2312 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2313 obd_uuid.uuid, FILTER_BY_EXACT,
2314 "target_obd", ¶m_data);
2318 fp = fopen(param_data.gl_pathv[0], "r");
2321 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2322 param_data.gl_pathv[0]);
2326 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2327 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2328 param->fp_get_lmv ? "MDTS" : "OBDS");
2330 snprintf(format, sizeof(format),
2331 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2332 while (fgets(buf, sizeof(buf), fp) != NULL) {
2335 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2338 if (param->fp_obd_uuid) {
2339 if (llapi_uuid_match(obd_uuid.uuid,
2340 param->fp_obd_uuid->uuid)) {
2341 param->fp_obd_index = index;
2344 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2345 /* Print everything */
2346 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2349 param->fp_obds_printed = 1;
2353 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2354 llapi_err_noerrno(LLAPI_MSG_ERROR,
2355 "error: %s: unknown obduuid: %s",
2356 __func__, param->fp_obd_uuid->uuid);
2360 cfs_free_param_data(¶m_data);
2365 * In this case, param->fp_obd_uuid will be an array of obduuids and
2366 * obd index for all these obduuids will be returned in
2367 * param->fp_obd_indexes
2369 static int setup_indexes(int d, char *path, struct obd_uuid *obduuids,
2370 int num_obds, int **obdindexes, int *obdindex,
2373 int ret, obdcount, maxidx, obd_valid = 0, obdnum;
2374 int *indices = NULL;
2375 struct obd_uuid *uuids = NULL;
2380 if (type == LOV_TYPE)
2381 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2383 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2387 obdcount = atoi(buf);
2388 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2391 indices = malloc(obdcount * sizeof(int));
2392 if (indices == NULL) {
2399 ret = llapi_get_target_uuids(d, uuids, indices, &obdcount, type);
2401 if (ret == -EOVERFLOW) {
2402 struct obd_uuid *uuids_temp;
2403 int *indices_temp = NULL;
2405 uuids_temp = realloc(uuids, obdcount *
2406 sizeof(struct obd_uuid));
2409 indices_temp = realloc(indices, obdcount * sizeof(int));
2411 indices = indices_temp;
2412 if (uuids_temp && indices_temp)
2413 goto retry_get_uuids;
2417 llapi_error(LLAPI_MSG_ERROR, ret, "cannot fetch %u OST UUIDs",
2422 indexes = malloc(num_obds * sizeof(*obdindex));
2423 if (indexes == NULL) {
2428 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2431 /* The user may have specified a simple index */
2432 i = strtol(obduuids[obdnum].uuid, &end, 0);
2433 if (end && *end == '\0' && i < maxidx) {
2434 indexes[obdnum] = i;
2437 for (i = 0; i < obdcount; i++) {
2438 if (llapi_uuid_match(uuids[i].uuid,
2439 obduuids[obdnum].uuid)) {
2440 indexes[obdnum] = indices[i];
2448 indexes[obdnum] = OBD_NOT_FOUND;
2449 llapi_err_noerrno(LLAPI_MSG_ERROR,
2450 "invalid obduuid '%s'",
2451 obduuids[obdnum].uuid);
2457 *obdindex = OBD_NOT_FOUND;
2459 *obdindex = obd_valid;
2461 *obdindexes = indexes;
2472 static int setup_target_indexes(int d, char *path, struct find_param *param)
2476 if (param->fp_mdt_uuid) {
2477 ret = setup_indexes(d, path, param->fp_mdt_uuid,
2479 ¶m->fp_mdt_indexes,
2480 ¶m->fp_mdt_index, LMV_TYPE);
2485 if (param->fp_obd_uuid) {
2486 ret = setup_indexes(d, path, param->fp_obd_uuid,
2488 ¶m->fp_obd_indexes,
2489 ¶m->fp_obd_index, LOV_TYPE);
2494 param->fp_got_uuids = 1;
2499 int llapi_ostlist(char *path, struct find_param *param)
2504 fd = open(path, O_RDONLY | O_DIRECTORY);
2508 ret = setup_obd_uuid(fd, path, param);
2515 * Tries to determine the default stripe attributes for a given filesystem. The
2516 * filesystem to check should be specified by fsname, or will be determined
2519 static int sattr_get_defaults(const char *const fsname,
2520 unsigned int *scount,
2521 unsigned int *ssize,
2522 unsigned int *soffset)
2528 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2529 "stripecount", val, sizeof(val));
2532 *scount = atoi(val);
2536 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2537 "stripesize", val, sizeof(val));
2544 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2545 "stripeoffset", val, sizeof(val));
2548 *soffset = atoi(val);
2555 * Tries to gather the default stripe attributes for a given filesystem. If
2556 * the attributes can be determined, they are cached for easy retreival the
2557 * next time they are needed. Only a single filesystem's attributes are
2560 int sattr_cache_get_defaults(const char *const fsname,
2561 const char *const pathname, unsigned int *scount,
2562 unsigned int *ssize, unsigned int *soffset)
2565 char fsname[PATH_MAX + 1];
2566 unsigned int stripecount;
2567 unsigned int stripesize;
2568 unsigned int stripeoffset;
2574 char fsname_buf[PATH_MAX + 1];
2575 unsigned int tmp[3];
2577 if (fsname == NULL) {
2578 rc = llapi_search_fsname(pathname, fsname_buf);
2582 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2585 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2587 * Ensure all 3 sattrs (count, size, and offset) are
2588 * successfully retrieved and stored in tmp before writing to
2591 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2595 cache.stripecount = tmp[0];
2596 cache.stripesize = tmp[1];
2597 cache.stripeoffset = tmp[2];
2598 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2602 *scount = cache.stripecount;
2604 *ssize = cache.stripesize;
2606 *soffset = cache.stripeoffset;
2611 static char *layout2name(__u32 layout_pattern)
2613 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2615 else if (layout_pattern == LOV_PATTERN_MDT)
2617 else if (layout_pattern == LOV_PATTERN_RAID0)
2619 else if (layout_pattern ==
2620 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2621 return "raid0,overstriped";
2626 enum lov_dump_flags {
2627 LDF_IS_DIR = 0x0001,
2628 LDF_IS_RAW = 0x0002,
2629 LDF_INDENT = 0x0004,
2630 LDF_SKIP_OBJS = 0x0008,
2632 LDF_EXTENSION = 0x0020,
2633 LDF_HEX_IDX = 0x0040,
2636 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2637 struct lov_user_ost_data_v1 *objects,
2638 enum llapi_layout_verbose verbose,
2639 int depth, char *pool_name,
2640 enum lov_dump_flags flags)
2642 bool is_dir = flags & LDF_IS_DIR;
2643 bool is_raw = flags & LDF_IS_RAW;
2644 bool indent = flags & LDF_INDENT;
2645 bool yaml = flags & LDF_YAML;
2646 bool skip_objs = flags & LDF_SKIP_OBJS;
2647 bool extension = flags & LDF_EXTENSION;
2648 char *prefix = is_dir ? "" : "lmm_";
2649 char *separator = "";
2650 char *space = indent ? " " : "";
2651 char *fmt_idx = flags & LDF_HEX_IDX ? "%#x" : "%d";
2654 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2655 lmm_oi_set_seq(&lum->lmm_oi, 0);
2656 if (!indent && (verbose & VERBOSE_DETAIL))
2657 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2660 if (!yaml && !indent && depth && path &&
2661 ((verbose != VERBOSE_OBJID) || !is_dir))
2662 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2664 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2665 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2666 space, prefix, lum->lmm_magic);
2667 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2669 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2670 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2672 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2675 if (verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) {
2680 if (verbose & ~VERBOSE_DFID)
2681 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2685 struct lu_fid dir_fid;
2687 rc = llapi_path2fid(path, &dir_fid);
2689 llapi_error(LLAPI_MSG_ERROR, rc,
2690 "Cannot determine directory fid.");
2692 seq = dir_fid.f_seq;
2693 oid = dir_fid.f_oid;
2694 ver = dir_fid.f_ver;
2697 * This needs a bit of hand-holding since old 1.x
2698 * lmm_oi have { oi.oi_id = mds_inum, oi.oi_seq = 0 }
2699 * and 2.x lmm_oi have { oi.oi_id = mds_oid,
2700 * oi.oi_seq = mds_seq } instead of a real FID.
2701 * Ideally the 2.x code would have stored this like a
2702 * FID with { oi_id = mds_seq, oi_seq = mds_oid } so
2703 * the ostid union lu_fid { f_seq = mds_seq,
2704 * f_oid = mds_oid } worked properly (especially since
2705 * IGIF FIDs use mds_inum as the FID SEQ), but
2706 * unfortunately that didn't happen.
2708 * Print it to look like an IGIF FID, even though the
2709 * fields are reversed on disk, so that it makes sense
2712 * Don't use ostid_id() and ostid_seq(), since they
2713 * assume the oi_fid fields are in the right order.
2714 * This is why there are separate lmm_oi_seq() and
2715 * lmm_oi_id() routines for this.
2717 * For newer layout types hopefully this will be a
2720 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2721 lmm_oi_id(&lum->lmm_oi) :
2722 lmm_oi_seq(&lum->lmm_oi);
2723 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2724 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2725 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2729 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2730 (unsigned long long)seq, oid, ver);
2732 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2733 (unsigned long long)seq, oid, ver);
2736 if (verbose & VERBOSE_STRIPE_COUNT) {
2737 if (verbose & ~VERBOSE_STRIPE_COUNT)
2738 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2741 if (!is_raw && lum->lmm_stripe_count == 0 &&
2742 !(lov_pattern(lum->lmm_pattern) & LOV_PATTERN_MDT)){
2743 unsigned int scount;
2745 rc = sattr_cache_get_defaults(NULL, path,
2749 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2752 llapi_error(LLAPI_MSG_ERROR, rc,
2753 "Cannot determine default stripe count.");
2755 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2757 (__s16)lum->lmm_stripe_count);
2760 llapi_printf(LLAPI_MSG_NORMAL, "%i",
2762 (__s16)lum->lmm_stripe_count);
2764 if (!yaml && is_dir)
2770 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
2771 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
2772 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2773 if (verbose & ~VERBOSE_EXT_SIZE && extension)
2774 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
2776 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
2777 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2779 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2782 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2785 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2787 llapi_error(LLAPI_MSG_ERROR, rc,
2788 "Cannot determine default stripe size.");
2790 /* Extension size is in KiB */
2791 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2793 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
2794 (unsigned long long)lum->lmm_stripe_size);
2796 if (!yaml && is_dir)
2802 if ((verbose & VERBOSE_PATTERN)) {
2803 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2804 if (verbose & ~VERBOSE_PATTERN)
2805 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2807 if (lov_pattern_supported(lum->lmm_pattern))
2808 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2809 layout2name(lum->lmm_pattern));
2811 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2812 separator = (!yaml && is_dir) ? " " : "\n";
2815 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2816 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2817 if (verbose & ~VERBOSE_GENERATION)
2818 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2820 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2821 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2825 if (verbose & VERBOSE_STRIPE_OFFSET) {
2826 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2827 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2828 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2830 if (is_dir || skip_objs)
2831 if (lum->lmm_stripe_offset ==
2832 (typeof(lum->lmm_stripe_offset))(-1))
2833 llapi_printf(LLAPI_MSG_NORMAL, "-1");
2835 llapi_printf(LLAPI_MSG_NORMAL, fmt_idx,
2836 lum->lmm_stripe_offset);
2837 else if (lov_pattern(lum->lmm_pattern) & LOV_PATTERN_MDT)
2838 llapi_printf(LLAPI_MSG_NORMAL, "0");
2840 llapi_printf(LLAPI_MSG_NORMAL, fmt_idx,
2841 objects[0].l_ost_idx);
2842 if (!yaml && is_dir)
2848 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0') &&
2849 (!lov_pool_is_ignored(pool_name) || is_raw)) {
2850 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2851 if (verbose & ~VERBOSE_POOL)
2852 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2854 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2855 if (!yaml && is_dir)
2861 if (strlen(separator) != 0)
2862 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2865 static void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2866 struct lov_user_ost_data_v1 *objects,
2867 char *path, int obdindex, int depth,
2868 enum llapi_layout_verbose verbose,
2869 enum lov_dump_flags flags)
2871 bool is_dir = flags & LDF_IS_DIR;
2872 bool indent = flags & LDF_INDENT;
2873 bool skip_objs = flags & LDF_SKIP_OBJS;
2874 bool yaml = flags & LDF_YAML;
2875 bool hex = flags & LDF_HEX_IDX;
2876 bool obdstripe = obdindex == OBD_NOT_FOUND;
2879 if (!obdstripe && !skip_objs) {
2880 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2881 if (obdindex == objects[i].l_ost_idx) {
2891 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2894 if (!skip_objs && (verbose & VERBOSE_OBJID) &&
2895 ((!is_dir && !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2896 lov_pattern(lum->lmm_pattern) & LOV_PATTERN_MDT)) ||
2897 (is_dir && (lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC)))) {
2898 char *space = " - ";
2901 llapi_printf(LLAPI_MSG_NORMAL,
2902 "%6slmm_objects:\n", " ");
2904 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2906 llapi_printf(LLAPI_MSG_NORMAL,
2907 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2909 for (i = 0; i < lum->lmm_stripe_count; i++) {
2910 int idx = objects[i].l_ost_idx;
2911 long long oid = ostid_id(&objects[i].l_ost_oi);
2912 long long gr = ostid_seq(&objects[i].l_ost_oi);
2914 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2918 struct lu_fid fid = { 0 };
2919 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2920 llapi_printf(LLAPI_MSG_NORMAL,
2921 hex ? "%sl_ost_idx: %#x\n"
2922 : "%sl_ost_idx: %d\n",
2924 llapi_printf(LLAPI_MSG_NORMAL,
2925 "%8sl_fid: "DFID_NOBRACE"\n",
2927 } else if (indent) {
2928 struct lu_fid fid = { 0 };
2930 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2931 llapi_printf(LLAPI_MSG_NORMAL, hex ?
2932 "%s%3d: { l_ost_idx: %#5x, l_fid: "DFID" }\n" :
2933 "%s%3d: { l_ost_idx: %3d, l_fid: "DFID" }\n",
2934 space, i, idx, PFID(&fid));
2935 } else if (is_dir) {
2936 llapi_printf(LLAPI_MSG_NORMAL,
2937 "\t%6u\t%14s\t%13s\t%14s\n", idx, "N/A",
2940 char fmt[48] = { 0 };
2942 sprintf(fmt, "%s%s%s\n",
2943 hex ? "\t%#6x\t%14llu\t%#13llx\t"
2944 : "\t%6u\t%14llu\t%#13llx\t",
2945 (fid_seq_is_rsvd(gr) ||
2946 fid_seq_is_mdt0(gr)) ?
2947 "%14llu" : "%#14llx", "%s");
2948 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2950 obdindex == idx ? " *" : "");
2954 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2957 static void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2958 char *path, int obdindex, int depth,
2959 enum llapi_layout_verbose verbose,
2960 enum lov_dump_flags flags)
2962 struct lmv_user_mds_data *objects = lum->lum_objects;
2963 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2964 char *separator = "";
2965 bool yaml = flags & LDF_YAML;
2966 bool hex = flags & LDF_HEX_IDX;
2967 bool obdstripe = false;
2970 if (obdindex != OBD_NOT_FOUND) {
2971 if (lum->lum_stripe_count == 0) {
2972 if (obdindex == lum->lum_stripe_offset)
2975 for (i = 0; i < lum->lum_stripe_count; i++) {
2976 if (obdindex == objects[i].lum_mds) {
2977 llapi_printf(LLAPI_MSG_NORMAL,
2992 /* show all information default */
2994 if (lum->lum_magic == LMV_USER_MAGIC)
2995 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
2996 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
2998 verbose = VERBOSE_OBJID;
3001 if (depth && path && ((verbose != VERBOSE_OBJID)))
3002 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3004 if (verbose & VERBOSE_STRIPE_COUNT) {
3005 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3006 if (verbose & ~VERBOSE_STRIPE_COUNT)
3007 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3008 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3009 (int)lum->lum_stripe_count);
3010 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3016 if (verbose & VERBOSE_STRIPE_OFFSET) {
3017 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3018 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3019 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3020 llapi_printf(LLAPI_MSG_NORMAL, hex ? "%#x" : "%d",
3021 (int)lum->lum_stripe_offset);
3022 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3028 if (verbose & VERBOSE_HASH_TYPE) {
3029 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3030 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3032 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3033 if (verbose & ~VERBOSE_HASH_TYPE)
3034 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3035 if (type < LMV_HASH_TYPE_MAX)
3036 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3037 mdt_hash_name[type]);
3039 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3041 if (flags & LMV_HASH_FLAG_OVERSTRIPED)
3042 llapi_printf(LLAPI_MSG_NORMAL, ",overstriped");
3043 if (flags & LMV_HASH_FLAG_MIGRATION)
3044 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3045 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3046 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3047 if (flags & LMV_HASH_FLAG_LOST_LMV)
3048 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3049 if (flags & LMV_HASH_FLAG_FIXED)
3050 llapi_printf(LLAPI_MSG_NORMAL, ",fixed");
3051 if (flags & ~LMV_HASH_FLAG_KNOWN)
3052 llapi_printf(LLAPI_MSG_NORMAL, ",unknown_%04x",
3053 flags & ~LMV_HASH_FLAG_KNOWN);
3055 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3061 if ((verbose & VERBOSE_INHERIT) && lum->lum_magic == LMV_USER_MAGIC) {
3062 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3063 if (verbose & ~VERBOSE_INHERIT)
3064 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit: ");
3065 if (lum->lum_max_inherit == LMV_INHERIT_UNLIMITED)
3066 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3067 else if (lum->lum_max_inherit == LMV_INHERIT_NONE)
3068 llapi_printf(LLAPI_MSG_NORMAL, "0");
3070 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3071 lum->lum_max_inherit);
3072 if (verbose & VERBOSE_INHERIT && !yaml)
3078 if ((verbose & VERBOSE_INHERIT_RR) &&
3079 lum->lum_magic == LMV_USER_MAGIC) {
3080 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3081 if (verbose & ~VERBOSE_INHERIT_RR)
3082 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit_rr: ");
3083 if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_UNLIMITED)
3084 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3085 else if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_NONE)
3086 llapi_printf(LLAPI_MSG_NORMAL, "0");
3088 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3089 lum->lum_max_inherit_rr);
3090 if (verbose & VERBOSE_INHERIT_RR && !yaml)
3098 if ((verbose & VERBOSE_OBJID) && lum->lum_magic != LMV_USER_MAGIC) {
3099 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3100 if (lum->lum_stripe_count > 0)
3101 llapi_printf(LLAPI_MSG_NORMAL,
3102 "mdtidx\t\t FID[seq:oid:ver]\n");
3104 char fmt[48] = { 0 };
3105 sprintf(fmt, "%s%s", hex ? "%#6x" : "%6u",
3106 "\t\t "DFID"\t\t%s\n");
3107 for (i = 0; i < lum->lum_stripe_count; i++) {
3108 int idx = objects[i].lum_mds;
3109 struct lu_fid *fid = &objects[i].lum_fid;
3111 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3112 llapi_printf(LLAPI_MSG_NORMAL, fmt,
3114 obdindex == idx ? " *" : "");
3118 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3119 pool_name[0] != '\0') {
3120 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3121 if (verbose & ~VERBOSE_POOL)
3122 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3124 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3128 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3129 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3132 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3133 enum lov_dump_flags flags)
3135 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3136 int depth = param->fp_max_depth;
3137 enum llapi_layout_verbose verbose = param->fp_verbose;
3138 bool yaml = flags & LDF_YAML;
3140 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3141 !(flags & LDF_IS_DIR)) && !yaml)
3142 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3144 if (verbose & VERBOSE_DETAIL) {
3145 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3146 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3147 " ", comp_v1->lcm_magic);
3148 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3149 " ", comp_v1->lcm_size);
3150 if (flags & LDF_IS_DIR)
3151 llapi_printf(LLAPI_MSG_NORMAL,
3152 "%2slcm_flags: %s\n", " ",
3153 comp_v1->lcm_mirror_count > 0 ?
3156 llapi_printf(LLAPI_MSG_NORMAL,
3157 "%2slcm_flags: %s\n", " ",
3158 llapi_layout_flags_string(comp_v1->lcm_flags));
3161 if (verbose & VERBOSE_GENERATION) {
3162 if (verbose & ~VERBOSE_GENERATION)
3163 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3165 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3168 if (verbose & VERBOSE_MIRROR_COUNT) {
3169 if (verbose & ~VERBOSE_MIRROR_COUNT)
3170 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3172 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3173 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3174 comp_v1->lcm_mirror_count + 1 : 1);
3177 if (verbose & VERBOSE_COMP_COUNT) {
3178 if (verbose & ~VERBOSE_COMP_COUNT)
3179 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3181 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3182 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3183 comp_v1->lcm_entry_count : 0);
3186 if (verbose & VERBOSE_DETAIL && !yaml)
3187 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3190 static void lcme_flags2str(__u32 comp_flags)
3196 llapi_printf(LLAPI_MSG_NORMAL, "0");
3199 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3200 const char *cfn_name = comp_flags_table[i].cfn_name;
3201 __u32 cfn_flag = comp_flags_table[i].cfn_flag;
3203 if ((comp_flags & cfn_flag) == cfn_flag) {
3205 llapi_printf(LLAPI_MSG_NORMAL, ",");
3206 llapi_printf(LLAPI_MSG_NORMAL, "%s", cfn_name);
3207 comp_flags &= ~comp_flags_table[i].cfn_flag;
3213 llapi_printf(LLAPI_MSG_NORMAL, ",");
3214 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3218 static void lov_dump_comp_v1_entry(struct find_param *param,
3219 enum lov_dump_flags flags, int index)
3221 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3222 struct lov_comp_md_entry_v1 *entry;
3223 char *separator = "";
3224 enum llapi_layout_verbose verbose = param->fp_verbose;
3225 bool yaml = flags & LDF_YAML;
3227 entry = &comp_v1->lcm_entries[index];
3230 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3232 if (verbose & VERBOSE_COMP_ID) {
3233 if (verbose & VERBOSE_DETAIL && !yaml)
3234 llapi_printf(LLAPI_MSG_NORMAL,
3235 "%slcme_id: ", " - ");
3236 else if (verbose & ~VERBOSE_COMP_ID)
3237 llapi_printf(LLAPI_MSG_NORMAL,
3238 "%4slcme_id: ", " ");
3239 if (entry->lcme_id != LCME_ID_INVAL)
3240 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3242 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3246 if (verbose & VERBOSE_MIRROR_ID) {
3247 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3248 if (verbose & ~VERBOSE_MIRROR_ID)
3249 llapi_printf(LLAPI_MSG_NORMAL,
3250 "%4slcme_mirror_id: ", " ");
3251 if (entry->lcme_id != LCME_ID_INVAL)
3252 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3253 mirror_id_of(entry->lcme_id));
3255 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3259 if (verbose & VERBOSE_COMP_FLAGS) {
3260 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3261 if (verbose & ~VERBOSE_COMP_FLAGS)
3262 llapi_printf(LLAPI_MSG_NORMAL,
3263 "%4slcme_flags: ", " ");
3264 lcme_flags2str(entry->lcme_flags);
3267 /* print snapshot timestamp if its a nosync comp */
3268 if ((verbose & VERBOSE_COMP_FLAGS) &&
3269 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3270 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3271 if (verbose & ~VERBOSE_COMP_FLAGS)
3272 llapi_printf(LLAPI_MSG_NORMAL,
3273 "%4slcme_timestamp: ", " ");
3275 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3276 (unsigned long long)entry->lcme_timestamp);
3278 time_t stamp = entry->lcme_timestamp;
3279 char *date_str = asctime(localtime(&stamp));
3281 date_str[strlen(date_str) - 1] = '\0';
3282 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3288 if (verbose & VERBOSE_COMP_START) {
3289 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3290 if (verbose & ~VERBOSE_COMP_START)
3291 llapi_printf(LLAPI_MSG_NORMAL,
3292 "%4slcme_extent.e_start: ", " ");
3293 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3294 (unsigned long long)entry->lcme_extent.e_start);
3298 if (verbose & VERBOSE_COMP_END) {
3299 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3300 if (verbose & ~VERBOSE_COMP_END)
3301 llapi_printf(LLAPI_MSG_NORMAL,
3302 "%4slcme_extent.e_end: ", " ");
3303 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3304 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3306 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3307 (unsigned long long)entry->lcme_extent.e_end);
3312 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3313 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3314 } else if (verbose & VERBOSE_DETAIL) {
3315 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3316 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3317 " ", entry->lcme_offset);
3318 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3319 " ", entry->lcme_size);
3320 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3322 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3327 * Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3328 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3330 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3331 * The table below gives the answers for the specified parameters (value and
3332 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3333 * --------------------------------------
3334 * 1 | file > limit; sign > 0 | -1 / -1 |
3335 * 2 | file = limit; sign > 0 | -1 / -1 |
3336 * 3 | file < limit; sign > 0 | ? / 1 |
3337 * 4 | file > limit; sign = 0 | -1 / -1 |
3338 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3339 * 6 | file < limit; sign = 0 | ? / -1 |
3340 * 7 | file > limit; sign < 0 | 1 / 1 |
3341 * 8 | file = limit; sign < 0 | ? / -1 |
3342 * 9 | file < limit; sign < 0 | ? / -1 |
3343 * --------------------------------------
3344 * Note: 5th actually means that the value is within the interval
3345 * (limit - margin, limit].
3347 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3348 int sign, int negopt, unsigned long long margin,
3354 /* Drop the fraction of margin (of days or size). */
3355 if (file + margin <= limit)
3357 } else if (sign == 0) {
3358 if (file <= limit && file + margin > limit)
3360 else if (file + margin <= limit)
3362 } else if (sign < 0) {
3369 return negopt ? ~ret + 1 : ret;
3372 static inline struct lov_user_md *
3373 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3375 return (struct lov_user_md *)((char *)comp_v1 +
3376 comp_v1->lcm_entries[ent_idx].lcme_offset);
3379 static inline struct lov_user_ost_data_v1 *
3380 lov_v1v3_objects(struct lov_user_md *v1)
3382 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3383 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3385 return v1->lmm_objects;
3389 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3391 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3392 snprintf(pool_name, LOV_MAXPOOLNAME + 1, "%s",
3393 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3395 pool_name[0] = '\0';
3399 print_last_init_comp(struct find_param *param)
3401 /* print all component info */
3402 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3405 /* print specific component info */
3406 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3407 param->fp_check_comp_start || param->fp_check_comp_end ||
3408 param->fp_check_mirror_id || param->fp_check_mirror_index)
3414 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3418 if (param->fp_comp_end == LUSTRE_EOF) {
3419 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3420 match = end == LUSTRE_EOF ? 1 : -1;
3421 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3422 match = end == LUSTRE_EOF ? -1 : 1;
3423 else /* at least EOF */
3425 if (param->fp_exclude_comp_end)
3428 unsigned long long margin;
3430 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3431 match = find_value_cmp(end, param->fp_comp_end,
3432 param->fp_comp_end_sign,
3433 param->fp_exclude_comp_end, margin, 0);
3440 * An example of "getstripe -v" for a two components PFL file:
3443 * lcm_magic: 0x0BD60BD0
3447 * lcm_entry_count: 2
3451 * lcme_extent.e_start: 0
3452 * lcme_extent.e_end: 1048576
3456 * lmm_magic: 0x0BD10BD0
3457 * lmm_seq: 0x200000401
3458 * lmm_object_id: 0x1
3459 * lmm_fid: [0x200000401:0x1:0x0]
3460 * lmm_stripe_count: 1
3461 * lmm_stripe_size: 1048576
3462 * lmm_pattern: raid0
3464 * lmm_stripe_offset: 0
3466 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3470 * lcme_extent.e_start: 1048576
3471 * lcme_extent.e_end: EOF
3475 * lmm_magic: 0x0BD10BD0
3476 * lmm_seq: 0x200000401
3477 * lmm_object_id: 0x1
3478 * lmm_fid: [0x200000401:0x1:0x0]
3479 * lmm_stripe_count: 2
3480 * lmm_stripe_size: 1048576
3481 * lmm_pattern: raid0
3483 * lmm_stripe_offset: 1
3485 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3486 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3488 static void lov_dump_comp_v1(struct find_param *param, char *path,
3489 enum lov_dump_flags flags)
3491 struct lov_comp_md_entry_v1 *entry;
3492 struct lov_user_ost_data_v1 *objects;
3493 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3494 struct lov_user_md_v1 *v1;
3495 char pool_name[LOV_MAXPOOLNAME + 1];
3496 int obdindex = param->fp_obd_index;
3497 int i, j, match, ext;
3498 bool obdstripe = false;
3499 __u16 mirror_index = 0;
3500 __u16 mirror_id = 0;
3502 if (obdindex != OBD_NOT_FOUND) {
3503 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3504 i < comp_v1->lcm_entry_count; i++) {
3505 if (!(comp_v1->lcm_entries[i].lcme_flags &
3509 v1 = lov_comp_entry(comp_v1, i);
3510 objects = lov_v1v3_objects(v1);
3512 for (j = 0; j < v1->lmm_stripe_count; j++) {
3513 if (obdindex == objects[j].l_ost_idx) {
3526 lov_dump_comp_v1_header(param, path, flags);
3528 flags |= LDF_INDENT;
3530 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3531 entry = &comp_v1->lcm_entries[i];
3533 if (param->fp_check_comp_flags) {
3534 if (((param->fp_comp_flags & entry->lcme_flags) !=
3535 param->fp_comp_flags) ||
3536 (param->fp_comp_neg_flags & entry->lcme_flags))
3540 if (param->fp_check_comp_id &&
3541 param->fp_comp_id != entry->lcme_id)
3544 if (param->fp_check_comp_start) {
3545 match = find_value_cmp(entry->lcme_extent.e_start,
3546 param->fp_comp_start,
3547 param->fp_comp_start_sign,
3549 param->fp_comp_start_units, 0);
3554 if (param->fp_check_comp_end) {
3555 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3561 if (param->fp_check_mirror_index) {
3562 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3564 mirror_id = mirror_id_of(entry->lcme_id);
3567 match = find_value_cmp(mirror_index,
3568 param->fp_mirror_index,
3569 param->fp_mirror_index_sign,
3570 param->fp_exclude_mirror_index,
3574 } else if (param->fp_check_mirror_id) {
3575 if (mirror_id != mirror_id_of(entry->lcme_id))
3576 mirror_id = mirror_id_of(entry->lcme_id);
3578 match = find_value_cmp(mirror_id,
3579 param->fp_mirror_id,
3580 param->fp_mirror_id_sign,
3581 param->fp_exclude_mirror_id,
3587 if (print_last_init_comp(param)) {
3589 * if part of stripe info is needed, we'd print only
3590 * the last instantiated component info.
3592 if (entry->lcme_flags & LCME_FL_INIT)
3595 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3596 if (entry->lcme_flags & LCME_FL_EXTENSION)
3597 /* moved back below */
3605 if (entry->lcme_flags & LCME_FL_INIT) {
3606 if (obdindex != OBD_NOT_FOUND) {
3607 flags |= LDF_SKIP_OBJS;
3608 v1 = lov_comp_entry(comp_v1, i);
3609 objects = lov_v1v3_objects(v1);
3611 for (j = 0; j < v1->lmm_stripe_count; j++) {
3612 if (obdindex == objects[j].l_ost_idx) {
3613 flags &= ~LDF_SKIP_OBJS;
3618 flags &= ~LDF_SKIP_OBJS;
3621 flags |= LDF_SKIP_OBJS;
3624 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3626 lov_dump_comp_v1_entry(param, flags, i);
3628 v1 = lov_comp_entry(comp_v1, i);
3629 objects = lov_v1v3_objects(v1);
3630 lov_v1v3_pool_name(v1, pool_name);
3632 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3633 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3634 param->fp_max_depth, param->fp_verbose,
3637 if (print_last_init_comp(param)) {
3639 * directory layout contains only layout template, print the
3643 i = comp_v1->lcm_entry_count - 1;
3646 flags &= ~LDF_SKIP_OBJS;
3648 lov_dump_comp_v1_entry(param, flags, i);
3650 v1 = lov_comp_entry(comp_v1, i);
3651 objects = lov_v1v3_objects(v1);
3652 lov_v1v3_pool_name(v1, pool_name);
3654 entry = &comp_v1->lcm_entries[i];
3655 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3656 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3657 param->fp_max_depth, param->fp_verbose,
3662 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3663 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3666 static inline bool has_any_comp_options(struct find_param *param)
3668 enum llapi_layout_verbose verbose = param->fp_verbose;
3670 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3671 param->fp_check_comp_start || param->fp_check_comp_end ||
3672 param->fp_check_comp_flags)
3675 /* show full layout information, not component specific */
3676 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3679 return verbose & VERBOSE_COMP_OPTS;
3682 static struct lov_user_mds_data *
3683 lov_forge_comp_v1(struct lov_user_mds_data *orig, bool is_dir)
3685 struct lov_user_md *lum = &orig->lmd_lmm;
3686 struct lov_user_mds_data *new;
3687 struct lov_comp_md_v1 *comp_v1;
3688 struct lov_comp_md_entry_v1 *ent;
3689 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3690 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3693 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3695 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3699 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3700 + sizeof(new->lmd_lmmsize));
3702 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3703 comp_v1->lcm_magic = lum->lmm_magic;
3704 comp_v1->lcm_size = lum_off + lum_size;
3705 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3706 comp_v1->lcm_flags = 0;
3707 comp_v1->lcm_entry_count = 1;
3709 ent = &comp_v1->lcm_entries[0];
3711 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3712 ent->lcme_extent.e_start = 0;
3713 ent->lcme_extent.e_end = LUSTRE_EOF;
3714 ent->lcme_offset = lum_off;
3715 ent->lcme_size = lum_size;
3717 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3722 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3723 enum lov_dump_flags flags)
3725 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3727 if (has_any_comp_options(param)) {
3728 struct lov_user_mds_data *new_lmd, *orig_lmd;
3730 orig_lmd = param->fp_lmd;
3731 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3732 if (new_lmd != NULL) {
3733 param->fp_lmd = new_lmd;
3734 lov_dump_comp_v1(param, path, flags);
3735 param->fp_lmd = orig_lmd;
3741 if (magic == LOV_USER_MAGIC_V1) {
3742 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3743 param->fp_lmd->lmd_lmm.lmm_objects,
3744 path, param->fp_obd_index,
3745 param->fp_max_depth, param->fp_verbose,
3748 char pool_name[LOV_MAXPOOLNAME + 1];
3749 struct lov_user_ost_data_v1 *objects;
3750 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3752 snprintf(pool_name, sizeof(pool_name), "%s",
3753 lmmv3->lmm_pool_name);
3754 objects = lmmv3->lmm_objects;
3755 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3756 objects, path, param->fp_obd_index,
3757 param->fp_max_depth, param->fp_verbose,
3762 static uint32_t check_foreign_type(uint32_t foreign_type)
3766 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3767 if (lu_foreign_types[i].lft_name == NULL)
3769 if (foreign_type == lu_foreign_types[i].lft_type)
3773 return LU_FOREIGN_TYPE_UNKNOWN;
3776 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3777 enum lov_dump_flags flags)
3779 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3780 bool yaml = flags & LDF_YAML;
3782 if (!yaml && param->fp_depth && path)
3783 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3785 if (param->fp_verbose & VERBOSE_DETAIL) {
3786 uint32_t type = check_foreign_type(lfm->lfm_type);
3788 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3790 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3792 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3794 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3795 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3796 lu_foreign_types[type].lft_name);
3798 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3800 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3803 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3804 lfm->lfm_length, lfm->lfm_value);
3805 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3808 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3809 enum lov_dump_flags flags)
3811 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3812 bool yaml = flags & LDF_YAML;
3814 if (!yaml && param->fp_depth && path)
3815 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3817 if (param->fp_verbose & VERBOSE_DETAIL) {
3818 uint32_t type = check_foreign_type(lfm->lfm_type);
3820 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3822 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3824 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3826 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3827 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3828 lu_foreign_types[type].lft_name);
3830 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3832 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3835 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3836 lfm->lfm_length, lfm->lfm_value);
3837 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3840 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3841 enum lov_dump_flags flags)
3845 if (param->fp_get_lmv || param->fp_get_default_lmv)
3846 magic = (__u32)param->fp_lmv_md->lum_magic;
3848 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3851 flags |= LDF_IS_RAW;
3854 if (param->fp_hex_idx)
3855 flags |= LDF_HEX_IDX;
3858 case LOV_USER_MAGIC_V1:
3859 case LOV_USER_MAGIC_V3:
3860 case LOV_USER_MAGIC_SPECIFIC:
3861 lov_dump_plain_user_lmm(param, path, flags);
3863 case LOV_USER_MAGIC_FOREIGN:
3864 lov_dump_foreign_lmm(param, path, flags);
3867 case LMV_USER_MAGIC: {
3868 char pool_name[LOV_MAXPOOLNAME + 1];
3869 struct lmv_user_md *lum;
3871 lum = (struct lmv_user_md *)param->fp_lmv_md;
3872 snprintf(pool_name, sizeof(pool_name), "%s",
3873 lum->lum_pool_name);
3874 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3875 param->fp_max_depth, param->fp_verbose,
3879 case LOV_USER_MAGIC_COMP_V1:
3880 lov_dump_comp_v1(param, path, flags);
3882 case LMV_MAGIC_FOREIGN:
3883 lmv_dump_foreign_lmm(param, path, flags);
3886 llapi_printf(LLAPI_MSG_NORMAL,
3887 "unknown lmm_magic: %#x (expecting one of %#x %#x %#x %#x)\n",
3888 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3889 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3890 LMV_USER_MAGIC, LMV_MAGIC_V1);
3895 static int llapi_file_get_stripe1(const char *path, struct lov_user_md *lum)
3901 fname = strrchr(path, '/');
3903 /* It should be a file (or other non-directory) */
3904 if (fname == NULL) {
3905 dname = (char *)malloc(2);
3909 fname = (char *)path;
3911 dname = (char *)malloc(fname - path + 1);
3914 strncpy(dname, path, fname - path);
3915 dname[fname - path] = '\0';
3919 fd = open(dname, O_RDONLY | O_NONBLOCK);
3925 strcpy((char *)lum, fname);
3926 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3929 if (close(fd) == -1 && rc == 0)
3937 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3939 char *canon_path = NULL;
3942 rc = llapi_file_get_stripe1(path, lum);
3943 if (!(rc == -ENOTTY || rc == -ENODATA))
3946 /* Handle failure due to symlinks by dereferencing path manually. */
3947 canon_path = canonicalize_file_name(path);
3948 if (canon_path == NULL)
3949 goto out; /* Keep original rc. */
3951 rc2 = llapi_file_get_stripe1(canon_path, lum);
3953 goto out; /* Keep original rc. */
3962 int llapi_file_lookup(int dirfd, const char *name)
3964 struct obd_ioctl_data data = { 0 };
3969 if (dirfd < 0 || name == NULL)
3972 data.ioc_version = OBD_IOCTL_VERSION;
3973 data.ioc_len = sizeof(data);
3974 data.ioc_inlbuf1 = (char *)name;
3975 data.ioc_inllen1 = strlen(name) + 1;
3977 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3979 llapi_error(LLAPI_MSG_ERROR, rc,
3980 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3985 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
3992 * Check if the file time matches all the given criteria (e.g. --atime +/-N).
3993 * Return -1 or 1 if file timestamp does not or does match the given criteria
3994 * correspondingly. Return 0 if the MDS time is being checked and there are
3995 * attributes on OSTs and it is not yet clear if the timespamp matches.
3997 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
3998 * updated timestamps.
4000 static int find_time_check(struct find_param *param, int mds)
4002 struct lov_user_mds_data *lmd = param->fp_lmd;
4006 /* Check if file is accepted. */
4007 if (param->fp_atime) {
4008 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4009 param->fp_atime, param->fp_asign,
4010 param->fp_exclude_atime,
4011 param->fp_time_margin, mds);
4017 if (param->fp_mtime) {
4018 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4019 param->fp_mtime, param->fp_msign,
4020 param->fp_exclude_mtime,
4021 param->fp_time_margin, mds);
4026 * If the previous check matches, but this one is not yet clear,
4027 * we should return 0 to do an RPC on OSTs.
4033 if (param->fp_ctime) {
4034 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4035 param->fp_ctime, param->fp_csign,
4036 param->fp_exclude_ctime,
4037 param->fp_time_margin, mds);
4042 * If the previous check matches, but this one is not yet clear,
4043 * we should return 0 to do an RPC on OSTs.
4052 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4054 struct lov_user_mds_data *lmd = param->fp_lmd;
4059 for (i = 0; i < 2; i++) {
4060 /* Check if file is accepted. */
4061 if (param->fp_newery[NEWERXY_ATIME][i]) {
4062 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4063 param->fp_newery[NEWERXY_ATIME][i],
4070 if (param->fp_newery[NEWERXY_MTIME][i]) {
4071 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4072 param->fp_newery[NEWERXY_MTIME][i],
4078 * If the previous check matches, but this one is not
4079 * yet clear, we should return 0 to do an RPC on OSTs.
4085 if (param->fp_newery[NEWERXY_CTIME][i]) {
4086 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4087 param->fp_newery[NEWERXY_CTIME][i],
4093 * If the previous check matches, but this one is not
4094 * yet clear, we should return 0 to do an RPC on OSTs.
4101 * File birth time (btime) can get from MDT directly.
4102 * if @from_mdt is true, it means the input file attributs are
4103 * obtained directly from MDT.
4104 * Thus, if @from_mdt is false, we should skip the following
4110 if (param->fp_newery[NEWERXY_BTIME][i]) {
4111 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4114 rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4115 param->fp_newery[NEWERXY_BTIME][i],
4126 * Check whether the stripes matches the indexes user provided
4130 static int check_obd_match(struct find_param *param)
4132 struct lov_user_ost_data_v1 *objects;
4133 struct lov_comp_md_v1 *comp_v1 = NULL;
4134 struct lov_user_mds_data *lmd = param->fp_lmd;
4135 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4136 int i, j, k, count = 1;
4138 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4141 if (!S_ISREG(lmd->lmd_stx.stx_mode))
4144 /* exclude foreign */
4145 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4146 return param->fp_exclude_obd;
4149 * Only those files should be accepted, which have a
4150 * stripe on the specified OST.
4152 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4153 comp_v1 = (struct lov_comp_md_v1 *)v1;
4154 count = comp_v1->lcm_entry_count;
4157 for (i = 0; i < count; i++) {
4159 v1 = lov_comp_entry(comp_v1, i);
4161 objects = lov_v1v3_objects(v1);
4163 for (j = 0; j < v1->lmm_stripe_count; j++) {
4164 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4167 for (k = 0; k < param->fp_num_obds; k++) {
4168 if (param->fp_obd_indexes[k] ==
4169 objects[j].l_ost_idx)
4170 return !param->fp_exclude_obd;
4175 return param->fp_exclude_obd;
4178 static int check_mdt_match(struct find_param *param)
4182 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4185 /* FIXME: For striped dir, we should get stripe information and check */
4186 for (i = 0; i < param->fp_num_mdts; i++) {
4187 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4188 return !param->fp_exclude_mdt;
4191 if (param->fp_exclude_mdt)
4198 * Check whether the obd is active or not, if it is
4199 * not active, just print the object affected by this
4202 static void print_failed_tgt(struct find_param *param, char *path, int type)
4204 struct obd_statfs stat_buf;
4205 struct obd_uuid uuid_buf;
4206 int tgt_nr, i, *indexes;
4209 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV) {
4210 llapi_error(LLAPI_MSG_NORMAL, ret, "%s: wrong statfs type(%d)",
4215 tgt_nr = (type == LL_STATFS_LOV) ? param->fp_obd_index :
4216 param->fp_mdt_index;
4217 indexes = (type == LL_STATFS_LOV) ? param->fp_obd_indexes :
4218 param->fp_mdt_indexes;
4220 for (i = 0; i < tgt_nr; i++) {
4221 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4222 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4224 ret = llapi_obd_statfs(path, type, indexes[i], &stat_buf,
4227 llapi_error(LLAPI_MSG_NORMAL, ret,
4228 "%s: obd_uuid: %s failed",
4229 __func__, param->fp_obd_uuid->uuid);
4233 static int find_check_stripe_size(struct find_param *param)
4235 struct lov_comp_md_v1 *comp_v1 = NULL;
4236 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4237 __u32 stripe_size = 0;
4238 int ret, i, count = 1;
4240 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4241 return param->fp_exclude_stripe_size ? 1 : -1;
4243 ret = param->fp_exclude_stripe_size ? 1 : -1;
4244 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4245 comp_v1 = (struct lov_comp_md_v1 *)v1;
4246 count = comp_v1->lcm_entry_count;
4249 for (i = 0; i < count; i++) {
4250 struct lov_comp_md_entry_v1 *ent;
4253 v1 = lov_comp_entry(comp_v1, i);
4255 ent = &comp_v1->lcm_entries[i];
4256 if (ent->lcme_flags & LCME_FL_EXTENSION)
4258 if (!(ent->lcme_flags & LCME_FL_INIT))
4261 stripe_size = v1->lmm_stripe_size;
4264 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4265 param->fp_stripe_size_sign,
4266 param->fp_exclude_stripe_size,
4267 param->fp_stripe_size_units, 0);
4272 static int find_check_ext_size(struct find_param *param)
4274 struct lov_comp_md_v1 *comp_v1;
4275 struct lov_user_md_v1 *v1;
4278 ret = param->fp_exclude_ext_size ? 1 : -1;
4279 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4280 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4283 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4284 struct lov_comp_md_entry_v1 *ent;
4286 v1 = lov_comp_entry(comp_v1, i);
4288 ent = &comp_v1->lcm_entries[i];
4289 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4292 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4293 param->fp_ext_size_sign,
4294 param->fp_exclude_ext_size,
4295 param->fp_ext_size_units, 0);
4296 /* If any ext_size matches */
4304 static __u32 find_get_stripe_count(struct find_param *param)
4306 struct lov_comp_md_v1 *comp_v1 = NULL;
4307 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4309 __u32 stripe_count = 0;
4311 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4314 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4315 comp_v1 = (struct lov_comp_md_v1 *)v1;
4316 count = comp_v1->lcm_entry_count;
4319 for (i = 0; i < count; i++) {
4321 struct lov_comp_md_entry_v1 *ent;
4323 v1 = lov_comp_entry(comp_v1, i);
4325 ent = &comp_v1->lcm_entries[i];
4326 if (!(ent->lcme_flags & LCME_FL_INIT))
4329 if (ent->lcme_flags & LCME_FL_EXTENSION)
4332 stripe_count = v1->lmm_stripe_count;
4335 return stripe_count;
4338 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4340 static int find_check_layout(struct find_param *param)
4342 struct lov_comp_md_v1 *comp_v1 = NULL;
4343 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4345 bool found = false, valid = false;
4347 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4348 comp_v1 = (struct lov_comp_md_v1 *)v1;
4349 count = comp_v1->lcm_entry_count;
4352 for (i = 0; i < count; i++) {
4354 v1 = lov_comp_entry(comp_v1, i);
4356 /* foreign file have a special magic but no pattern field */
4357 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4360 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4364 if (v1->lmm_pattern & param->fp_layout) {
4373 if ((found && !param->fp_exclude_layout) ||
4374 (!found && param->fp_exclude_layout))
4381 * if no type specified, check/exclude all foreign
4382 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4384 static int find_check_foreign(struct find_param *param)
4386 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4387 struct lov_foreign_md *lfm;
4389 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4390 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4391 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4392 return param->fp_exclude_foreign ? 1 : -1;
4396 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4397 lfm->lfm_type == param->fp_foreign_type)
4398 return param->fp_exclude_foreign ? -1 : 1;
4399 return param->fp_exclude_foreign ? 1 : -1;
4402 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4403 struct lmv_foreign_md *lfm;
4405 lfm = (void *)param->fp_lmv_md;
4406 if (lmv_is_foreign(lfm->lfm_magic)) {
4407 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4408 return param->fp_exclude_foreign ? 1 : -1;
4412 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4413 lfm->lfm_type == param->fp_foreign_type)
4414 return param->fp_exclude_foreign ? -1 : 1;
4415 return param->fp_exclude_foreign ? 1 : -1;
4420 static int find_check_pool(struct find_param *param)
4422 struct lov_comp_md_v1 *comp_v1 = NULL;
4423 struct lov_user_md_v3 *v3 = (void *)¶m->fp_lmd->lmd_lmm;
4427 if (v3->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4428 comp_v1 = (struct lov_comp_md_v1 *)v3;
4429 count = comp_v1->lcm_entry_count;
4430 /* empty requested pool is taken as no pool search */
4431 if (count == 0 && param->fp_poolname[0] == '\0') {
4437 for (i = 0; i < count; i++) {
4438 if (comp_v1 != NULL) {
4439 if (!(comp_v1->lcm_entries[i].lcme_flags &
4443 v3 = (void *)lov_comp_entry(comp_v1, i);
4446 if (v3->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4449 if (((v3->lmm_magic == LOV_USER_MAGIC_V1) &&
4450 (param->fp_poolname[0] == '\0')) ||
4451 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4452 (strncmp(v3->lmm_pool_name,
4453 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4454 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4455 (strcmp(param->fp_poolname, "*") == 0))) {
4462 if ((found && !param->fp_exclude_pool) ||
4463 (!found && param->fp_exclude_pool))
4469 static int find_check_comp_options(struct find_param *param)
4471 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4472 struct lov_user_mds_data *lmd = param->fp_lmd;
4473 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4474 struct lov_comp_md_entry_v1 *entry;
4477 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4480 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4481 comp_v1 = (struct lov_comp_md_v1 *)v1;
4483 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4484 if (forged_v1 == NULL)
4486 comp_v1 = forged_v1;
4487 comp_v1->lcm_entry_count = 1;
4488 entry = &comp_v1->lcm_entries[0];
4489 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4491 entry->lcme_extent.e_start = 0;
4492 entry->lcme_extent.e_end = LUSTRE_EOF;
4495 /* invalid case, don't match for any kind of search. */
4496 if (comp_v1->lcm_entry_count == 0) {
4501 if (param->fp_check_comp_count) {
4502 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4503 param->fp_comp_count,
4504 param->fp_comp_count_sign,
4505 param->fp_exclude_comp_count, 1, 0);
4511 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4512 entry = &comp_v1->lcm_entries[i];
4514 if (param->fp_check_comp_flags) {
4516 if (((param->fp_comp_flags & entry->lcme_flags) !=
4517 param->fp_comp_flags) ||
4518 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4524 if (param->fp_check_comp_start) {
4525 ret = find_value_cmp(entry->lcme_extent.e_start,
4526 param->fp_comp_start,
4527 param->fp_comp_start_sign,
4528 param->fp_exclude_comp_start,
4529 param->fp_comp_start_units, 0);
4534 if (param->fp_check_comp_end) {
4535 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4541 /* the component matches all criteria */
4550 static int find_check_mirror_options(struct find_param *param)
4552 struct lov_comp_md_v1 *comp_v1;
4553 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4556 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4559 comp_v1 = (struct lov_comp_md_v1 *)v1;
4561 if (param->fp_check_mirror_count) {
4562 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4563 param->fp_mirror_count,
4564 param->fp_mirror_count_sign,
4565 param->fp_exclude_mirror_count, 1, 0);
4570 if (param->fp_check_mirror_state) {
4572 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4574 if ((param->fp_mirror_state != 0 &&
4575 file_state != param->fp_mirror_state) ||
4576 file_state == param->fp_mirror_neg_state)
4583 static int find_check_attr_options(struct find_param *param)
4588 attrs = param->fp_lmd->lmd_stx.stx_attributes_mask &
4589 param->fp_lmd->lmd_stx.stx_attributes;
4591 /* This is a AND between all (negated) specified attributes */
4592 if ((param->fp_attrs && (param->fp_attrs & attrs) != param->fp_attrs) ||
4593 (param->fp_neg_attrs && (param->fp_neg_attrs & attrs)))
4596 if ((found && param->fp_exclude_attrs) ||
4597 (!found && !param->fp_exclude_attrs))
4604 * xattr_reg_match() - return true if the supplied string matches the pattern.
4606 * This requires the regex to match the entire supplied string, not just a
4609 * str must be null-terminated. len should be passed in anyways to avoid an
4610 * extra call to strlen(str) when the length is already known.
4612 static bool xattr_reg_match(regex_t *pattern, const char *str, int len)
4617 ret = regexec(pattern, str, 1, &pmatch, 0);
4618 if (ret == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
4625 * xattr_done_matching() - return true if all supplied patterns have been
4626 * matched, allowing to skip checking any remaining xattrs on a file.
4628 * This is only allowed if there are no "exclude" patterns.
4630 static int xattr_done_matching(struct xattr_match_info *xmi)
4634 for (i = 0; i < xmi->xattr_regex_count; i++) {
4635 /* if any pattern still undecided, need to keep going */
4636 if (!xmi->xattr_regex_matched[i])
4643 static int find_check_xattrs(char *path, struct xattr_match_info *xmi)
4645 ssize_t list_len = 0;
4646 ssize_t val_len = 0;
4651 for (i = 0; i < xmi->xattr_regex_count; i++)
4652 xmi->xattr_regex_matched[i] = false;
4654 list_len = llistxattr(path, xmi->xattr_name_buf, XATTR_LIST_MAX);
4656 llapi_error(LLAPI_MSG_ERROR, errno,
4657 "error: listxattr: %s", path);
4661 /* loop over all xattr names on the file */
4662 for (p = xmi->xattr_name_buf;
4663 p - xmi->xattr_name_buf < list_len;
4664 p = strchr(p, '\0'), p++) {
4665 fetched_val = false;
4666 /* loop over all regex patterns specified and check them */
4667 for (i = 0; i < xmi->xattr_regex_count; i++) {
4668 if (xmi->xattr_regex_matched[i])
4671 if (!xattr_reg_match(xmi->xattr_regex_name[i],
4675 if (xmi->xattr_regex_value[i] == NULL)
4679 * even if multiple patterns match the same xattr name,
4680 * don't call getxattr() more than once
4683 val_len = lgetxattr(path, p,
4684 xmi->xattr_value_buf,
4688 llapi_error(LLAPI_MSG_ERROR, errno,
4689 "error: getxattr: %s",
4695 * the value returned by getxattr might or
4696 * might not be null terminated.
4697 * if it is, then decrement val_len so it
4698 * matches what strlen() would return.
4699 * if it is not, then add a null terminator
4700 * since regexec() expects that.
4703 xmi->xattr_value_buf[val_len - 1] == '\0') {
4706 xmi->xattr_value_buf[val_len] = '\0';
4710 if (!xattr_reg_match(xmi->xattr_regex_value[i],
4711 xmi->xattr_value_buf, val_len))
4716 * if exclude this xattr, we can exit early
4719 if (xmi->xattr_regex_exclude[i])
4722 xmi->xattr_regex_matched[i] = true;
4725 * if all "include" patterns have matched, and there are
4726 * no "exclude" patterns, we can exit early with match
4728 if (xattr_done_matching(xmi) == 1)
4734 * finally, check that all supplied patterns either matched, or were
4735 * "exclude" patterns if they did not match.
4737 for (i = 0; i < xmi->xattr_regex_count; i++) {
4738 if (!xmi->xattr_regex_matched[i]) {
4739 if (!xmi->xattr_regex_exclude[i]) {
4748 static bool find_check_lmm_info(struct find_param *param)
4750 return param->fp_check_pool || param->fp_check_stripe_count ||
4751 param->fp_check_stripe_size || param->fp_check_layout ||
4752 param->fp_check_comp_count || param->fp_check_comp_end ||
4753 param->fp_check_comp_start || param->fp_check_comp_flags ||
4754 param->fp_check_mirror_count || param->fp_check_foreign ||
4755 param->fp_check_mirror_state || param->fp_check_ext_size ||
4756 param->fp_check_projid;
4760 * Interpret backslash escape sequences and write output into buffer.
4761 * Anything written to the buffer will be null terminated.
4763 * @param[in] seq String being parsed for escape sequence. The leading
4764 * '\' character is not included in this string (only the
4765 * characters after it)
4766 * @param[out] buffer Location where interpreted escape sequence is written
4767 * @param[in] size Size of the available buffer. (Needs to be large enough
4768 * to handle escape sequence output plus null terminator.)
4769 * @param[out] wrote Number of bytes written to the buffer.
4770 * @return Number of characters from input string processed
4771 * as part of the escape sequence (0 for an unrecognized
4774 static int printf_format_escape(char *seq, char *buffer, size_t size,
4778 /* For now, only handle single char escape sequences: \n, \t, \\ */
4801 * Interpret formats for timestamps (%a, %A@, etc)
4803 * @param[in] seq String being parsed for timestamp format. The leading
4804 * '%' character is not included in this string
4805 * @param[out] buffer Location where timestamp info is written
4806 * @param[in] size Size of the available buffer.
4807 * @param[out] wrote Number of bytes written to the buffer.
4808 * @return Number of characters from input string processed
4809 * as part of the format (0 for an unknown format)
4811 static int printf_format_timestamp(char *seq, char *buffer, size_t size,
4812 int *wrote, struct find_param *param)
4814 struct statx_timestamp ts = { 0, 0 };
4818 char *fmt = "%c"; /* Print in ctime format by default */
4823 ts = param->fp_lmd->lmd_stx.stx_atime;
4827 if (*(seq + 1) == '@') {
4828 ts = param->fp_lmd->lmd_stx.stx_atime;
4834 ts = param->fp_lmd->lmd_stx.stx_ctime;
4838 if (*(seq + 1) == '@') {
4839 ts = param->fp_lmd->lmd_stx.stx_ctime;
4845 ts = param->fp_lmd->lmd_stx.stx_mtime;
4849 if (*(seq + 1) == '@') {
4850 ts = param->fp_lmd->lmd_stx.stx_mtime;
4856 ts = param->fp_lmd->lmd_stx.stx_btime;
4860 if (*(seq + 1) == '@') {
4861 ts = param->fp_lmd->lmd_stx.stx_btime;
4871 /* Found valid format, print to buffer */
4874 *wrote = strftime(buffer, size, fmt, tm);
4881 * Print all ost indices associated with a file layout using a commma separated
4882 * list. For a file with mutliple components, the list of indices for each
4883 * component will be enclosed in brackets.
4885 * @param[out] buffer Location where OST indices are written
4886 * @param[in] size Size of the available buffer.
4887 * @pararm[in] layout Pointer to layout structure for the file
4888 * @return Number of bytes written to output buffer
4890 static int printf_format_ost_indices(char *buffer, size_t size,
4891 struct llapi_layout *layout)
4893 uint64_t count, idx, i;
4894 int err, bytes, wrote = 0;
4896 /* Make sure to start at the first component */
4897 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
4899 llapi_error(LLAPI_MSG_ERROR, err,
4900 "error: layout component iteration failed\n");
4904 err = llapi_layout_stripe_count_get(layout, &count);
4906 llapi_error(LLAPI_MSG_ERROR, err,
4907 "error: cannot get stripe_count\n");
4911 bytes = snprintf(buffer, (size - wrote), "%s", "[");
4916 for (i = 0; i < count; i++) {
4917 err = llapi_layout_ost_index_get(layout, i, &idx);
4919 llapi_error(LLAPI_MSG_ERROR, err,
4920 "error: cannot get OST index\n");
4921 bytes = snprintf(buffer, (size - wrote),
4924 bytes = snprintf(buffer, (size - wrote),
4932 /* Overwrite last comma with closing bracket */
4933 *(buffer - 1) = ']';
4935 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
4936 if (err == 0) /* next component is found */
4939 llapi_error(LLAPI_MSG_ERROR, err,
4940 "error: layout component iteration failed\n");
4941 /* At this point, either got error or reached last component */
4952 * Print file attributes as a comma-separated list of named attribute flags,
4953 * and hex value of any unknown attributes.
4955 * @param[out] buffer Location where file attributes are written
4956 * @param[in] size Size of the available buffer.
4957 * @pararm[in] stx struct statx containing attributes to print
4958 * @return Number of bytes written to output buffer
4960 static int printf_format_file_attributes(char *buffer, size_t size,
4961 lstatx_t stx, bool longopt)
4963 uint64_t attrs = stx.stx_attributes_mask & stx.stx_attributes;
4964 int bytes = 0, wrote = 0, first = 1;
4965 uint64_t known_attrs = 0;
4966 struct attrs_name *ap;
4968 /* before all, print '---' if no attributes, and exit */
4970 bytes = snprintf(buffer, size - wrote, "---");
4975 /* first, browse list of known attributes */
4976 for (ap = (struct attrs_name *)attrs_array; ap->an_attr != 0; ap++) {
4977 known_attrs |= ap->an_attr;
4978 if (attrs & ap->an_attr) {
4980 bytes = snprintf(buffer, size - wrote, "%s%s",
4981 first ? "" : ",", ap->an_name);
4983 bytes = snprintf(buffer, size - wrote, "%c",
4993 /* second, print hex value for unknown attributes */
4994 attrs &= ~known_attrs;
4996 bytes = snprintf(buffer, size - wrote, "%s0x%lx",
4997 first ? "" : ",", attrs);
5008 * Parse Lustre-specific format sequences of the form %L{x}.
5010 * @param[in] seq String being parsed for format sequence. The leading
5011 * '%' character is not included in this string
5012 * @param[out] buffer Location where interpreted format info is written
5013 * @param[in] size Size of the available buffer.
5014 * @param[out] wrote Number of bytes written to the buffer.
5015 * @param[in] param The find_param structure associated with the file/dir
5016 * @param[in] path Pathname of the current file/dir being handled
5017 * @param[in] projid Project ID associated with the current file/dir
5018 * @param[in] d File descriptor for the directory (or -1 for a
5019 * non-directory file)
5020 * @return Number of characters from input string processed
5021 * as part of the format (0 for an unknown format)
5023 static int printf_format_lustre(char *seq, char *buffer, size_t size,
5024 int *wrote, struct find_param *param,
5025 char *path, __u32 projid, int d)
5027 struct lmv_user_md *lum;
5028 struct lmv_user_mds_data *objects;
5029 struct llapi_layout *layout = NULL;
5031 unsigned int hash_type;
5032 uint64_t str_cnt, str_size, idx;
5033 char pool_name[LOV_MAXPOOLNAME + 1] = { '\0' };
5035 bool longopt = true;
5036 int rc = 2; /* all current valid sequences are 2 chars */
5039 /* Sanity check. Formats always look like %L{X} */
5040 if (*seq++ != 'L') {
5046 * Some formats like %LF or %LP are handled the same for both files
5047 * and dirs, so handle all of those here.
5051 err = llapi_path2fid(path, &fid);
5053 llapi_error(LLAPI_MSG_ERROR, err,
5054 "error: cannot get fid\n");
5057 *wrote = snprintf(buffer, size, DFID_NOBRACE, PFID(&fid));
5060 if (projid == INVALID_PROJID)
5061 *wrote = snprintf(buffer, size, "-1");
5063 *wrote = snprintf(buffer, size, "%u", projid);
5065 case 'a': /* file attributes */
5069 *wrote = printf_format_file_attributes(buffer, size,
5070 param->fp_lmd->lmd_stx,
5075 /* Other formats for files/dirs need to be handled differently */
5076 if (d == -1) { /* file */
5077 //layout = llapi_layout_get_by_xattr(¶m->fp_lmd->lmd_lmm,
5078 // param->fp_lum_size, 0);
5079 layout = llapi_layout_get_by_path(path, 0);
5080 if (layout == NULL) {
5081 llapi_error(LLAPI_MSG_ERROR, errno,
5082 "error: cannot get file layout\n");
5087 * Set the layout pointer to the last init component
5088 * since that is the component used for most of these
5089 * formats. (This also works for non-composite files)
5091 err = llapi_layout_get_last_init_comp(layout);
5093 llapi_error(LLAPI_MSG_ERROR, err,
5094 "error: cannot get last initialized compomnent\n");
5099 case 'c': /* stripe count */
5100 err = llapi_layout_stripe_count_get(layout, &str_cnt);
5102 llapi_error(LLAPI_MSG_ERROR, err,
5103 "error: cannot get stripe_count\n");
5106 *wrote = snprintf(buffer, size, "%"PRIu64, str_cnt);
5108 case 'h': /* hash info */
5109 /* Not applicable to files. Skip it. */
5111 case 'i': /* starting index */
5112 err = llapi_layout_ost_index_get(layout, 0, &idx);
5114 llapi_error(LLAPI_MSG_ERROR, err,
5115 "error: cannot get OST index of last initialized component\n");
5118 *wrote = snprintf(buffer, size, "%"PRIu64, idx);
5120 case 'o': /* list of object indices */
5121 *wrote = printf_format_ost_indices(buffer, size, layout);
5123 case 'p': /* pool name */
5124 err = llapi_layout_pool_name_get(layout, pool_name,
5127 llapi_error(LLAPI_MSG_ERROR, rc,
5128 "error: cannot get pool name\n");
5131 *wrote = snprintf(buffer, size, "%s", pool_name);
5133 case 'S': /* stripe size */
5134 err = llapi_layout_stripe_size_get(layout, &str_size);
5136 llapi_error(LLAPI_MSG_ERROR, rc,
5137 "error: cannot get stripe_size\n");
5140 *wrote = snprintf(buffer, size, "%"PRIu64, str_size);
5146 } else { /* directory */
5147 lum = (struct lmv_user_md *)param->fp_lmv_md;
5148 objects = lum->lum_objects;
5151 case 'c': /* stripe count */
5152 *wrote = snprintf(buffer, size, "%d",
5153 (int)lum->lum_stripe_count);
5155 case 'h': /* hash info */
5156 hash_type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
5157 if (hash_type < LMV_HASH_TYPE_MAX)
5158 *wrote = snprintf(buffer, size, "%s",
5159 mdt_hash_name[hash_type]);
5161 *wrote = snprintf(buffer, size, "%#x",
5164 case 'i': /* starting index */
5165 *wrote = snprintf(buffer, size, "%d",
5166 lum->lum_stripe_offset);
5168 case 'o': /* list of object indices */
5169 str_cnt = (int) lum->lum_stripe_count;
5170 *wrote = snprintf(buffer, size, "%s", "[");
5174 for (i = 0; i < str_cnt; i++) {
5175 bytes = snprintf(buffer, (size - *wrote),
5176 "%d,", objects[i].lum_mds);
5183 /* Use lum_offset as the only list entry */
5184 bytes = snprintf(buffer, (size - *wrote),
5185 "%d]", lum->lum_stripe_offset);
5188 /* Overwrite last comma with closing bracket */
5189 *(buffer - 1) = ']';
5192 case 'p': /* pool name */
5193 *wrote = snprintf(buffer, size, "%s",
5194 lum->lum_pool_name);
5196 case 'S': /* stripe size */
5197 /* This has no meaning for directories. Skip it. */
5207 llapi_layout_free(layout);
5210 /* output of snprintf was truncated */
5217 * Interpret format specifiers beginning with '%'.
5219 * @param[in] seq String being parsed for format specifier. The leading
5220 * '%' character is not included in this string
5221 * @param[out] buffer Location where formatted info is written
5222 * @param[in] size Size of the available buffer.
5223 * @param[out] wrote Number of bytes written to the buffer.
5224 * @param[in] param The find_param structure associated with the file/dir
5225 * @param[in] path Pathname of the current file/dir being handled
5226 * @param[in] projid Project ID associated with the current file/dir
5227 * @param[in] d File descriptor for the directory (or -1 for a
5228 * non-directory file)
5229 * @return Number of characters from input string processed
5230 * as part of the format (0 for an unknown format)
5232 static int printf_format_directive(char *seq, char *buffer, size_t size,
5233 int *wrote, struct find_param *param,
5234 char *path, __u32 projid, int d)
5236 __u16 mode = param->fp_lmd->lmd_stx.stx_mode;
5237 uint64_t blocks = param->fp_lmd->lmd_stx.stx_blocks;
5238 int rc = 1; /* most specifiers are single character */
5246 case 'w': case 'W': /* timestamps */
5247 rc = printf_format_timestamp(seq, buffer, size, wrote, param);
5249 case 'b': /* file size (in 512B blocks) */
5250 *wrote = snprintf(buffer, size, "%"PRIu64, blocks);
5252 case 'G': /* GID of owner */
5253 *wrote = snprintf(buffer, size, "%u",
5254 param->fp_lmd->lmd_stx.stx_gid);
5256 case 'k': /* file size (in 1K blocks) */
5257 *wrote = snprintf(buffer, size, "%"PRIu64, (blocks + 1)/2);
5259 case 'L': /* Lustre-specific formats */
5260 rc = printf_format_lustre(seq, buffer, size, wrote, param,
5263 case 'm': /* file mode in octal */
5264 *wrote = snprintf(buffer, size, "%#o", (mode & (~S_IFMT)));
5266 case 'n': /* number of links */
5267 *wrote = snprintf(buffer, size, "%u",
5268 param->fp_lmd->lmd_stx.stx_nlink);
5270 case 'p': /* Path name of file */
5271 *wrote = snprintf(buffer, size, "%s", path);
5273 case 's': /* file size (in bytes) */
5274 *wrote = snprintf(buffer, size, "%"PRIu64,
5275 (uint64_t) param->fp_lmd->lmd_stx.stx_size);
5277 case 'U': /* UID of owner */
5278 *wrote = snprintf(buffer, size, "%u",
5279 param->fp_lmd->lmd_stx.stx_uid);
5281 case 'y': /* file type */
5284 else if (S_ISDIR(mode))
5286 else if (S_ISLNK(mode))
5288 else if (S_ISBLK(mode))
5290 else if (S_ISCHR(mode))
5292 else if (S_ISFIFO(mode))
5294 else if (S_ISSOCK(mode))
5304 default: /* invalid format specifier */
5310 /* output of snprintf was truncated */
5317 * Parse user-supplied string for the -printf option and interpret any
5318 * '%' format specifiers or '\' escape sequences.
5320 * @param[in] param The find_param struct containing the -printf string
5321 * as well as info about the current file/dir that mathced
5322 * the lfs find search criteria
5323 * @param[in] path Path name for current file/dir
5324 * @param[in] projid Project ID associated with current file/dir
5325 * @param[in] d File descriptor for current directory (or -1 for a
5326 * non-directory file)
5328 static void printf_format_string(struct find_param *param, char *path,
5329 __u32 projid, int d)
5331 char output[FORMATTED_BUF_LEN];
5332 char *fmt_char = param->fp_format_printf_str;
5333 char *buff = output;
5339 buff_size = FORMATTED_BUF_LEN;
5341 /* Always leave one free byte in buffer for trailing NUL */
5342 while (*fmt_char && (buff_size > 1)) {
5345 if (*fmt_char == '%') {
5346 rc = printf_format_directive(fmt_char + 1, buff,
5347 buff_size, &written, param,
5349 } else if (*fmt_char == '\\')
5350 rc = printf_format_escape(fmt_char + 1, buff,
5351 buff_size, &written);
5354 /* Either a '\' escape or '%' format was processed.
5355 * Increment pointers accordingly.
5357 fmt_char += (rc + 1);
5359 buff_size -= written;
5361 /* Regular char or invalid escape/format.
5362 * Either way, copy current character.
5364 *buff++ = *fmt_char++;
5369 /* Terminate output buffer and print */
5371 llapi_printf(LLAPI_MSG_NORMAL, "%s", output);
5375 * Get file/directory project id.
5376 * by the open fd resides on.
5377 * Return 0 and project id on success, or -ve errno.
5379 static int fget_projid(int fd, __u32 *projid)
5384 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
5386 *projid = INVALID_PROJID;
5390 *projid = fsx.fsx_projid;
5395 * Check that the file's permissions in *st matches the one in find_param
5397 static int check_file_permissions(const struct find_param *param,
5404 switch (param->fp_perm_sign) {
5405 case LFS_FIND_PERM_EXACT:
5406 decision = (mode == param->fp_perm);
5408 case LFS_FIND_PERM_ALL:
5409 decision = ((mode & param->fp_perm) == param->fp_perm);
5411 case LFS_FIND_PERM_ANY:
5412 decision = ((mode & param->fp_perm) != 0);
5416 if ((param->fp_exclude_perm && decision)
5417 || (!param->fp_exclude_perm && !decision))
5423 static int cb_find_init(char *path, int p, int *dp,
5424 void *data, struct dirent64 *de)
5426 struct find_param *param = (struct find_param *)data;
5427 struct lov_user_mds_data *lmd = param->fp_lmd;
5428 int d = dp == NULL ? -1 : *dp;
5429 int decision = 1; /* 1 is accepted; -1 is rejected. */
5431 int checked_type = 0;
5433 __u32 stripe_count = 0;
5436 __u32 projid = INVALID_PROJID;
5437 bool gather_all = false;
5439 if (p == -1 && d == -1)
5442 /* Reset this value between invocations */
5443 param->fp_get_lmv = 0;
5445 /* Gather all file/dir info, not just what's needed for search params */
5446 if (param->fp_format_printf_str)
5449 /* If a regular expression is presented, make the initial decision */
5450 if (param->fp_pattern != NULL) {
5451 char *fname = strrchr(path, '/');
5453 fname = (fname == NULL ? path : fname + 1);
5454 ret = fnmatch(param->fp_pattern, fname, 0);
5455 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
5456 (ret == 0 && param->fp_exclude_pattern))
5460 /* See if we can check the file type from the dirent. */
5461 if (de != NULL && de->d_type != DT_UNKNOWN) {
5462 if (param->fp_type != 0) {
5465 if (DTTOIF(de->d_type) == param->fp_type) {
5466 if (param->fp_exclude_type)
5469 if (!param->fp_exclude_type)
5473 if ((param->fp_check_mdt_count || param->fp_hash_type ||
5474 param->fp_check_hash_flag) && de->d_type != DT_DIR)
5481 * Request MDS for the stat info if some of these parameters need
5484 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
5485 param->fp_check_uid || param->fp_check_gid ||
5486 param->fp_newerxy || param->fp_btime ||
5487 param->fp_atime || param->fp_mtime || param->fp_ctime ||
5488 param->fp_check_size || param->fp_check_blocks ||
5489 find_check_lmm_info(param) ||
5490 param->fp_check_mdt_count || param->fp_hash_type ||
5491 param->fp_check_hash_flag || param->fp_perm_sign ||
5492 param->fp_nlink || param->fp_attrs || param->fp_neg_attrs ||
5496 if (param->fp_type != 0 && checked_type == 0)
5499 if (decision == 0) {
5501 (param->fp_check_mdt_count || param->fp_hash_type ||
5502 param->fp_check_hash_flag || param->fp_check_foreign ||
5504 * cb_get_dirstripe is needed when checking nlink because
5505 * nlink is handled differently for multi-stripe directory
5506 * vs. single-stripe directory
5508 param->fp_nlink || gather_all)) {
5509 param->fp_get_lmv = 1;
5510 ret = cb_get_dirstripe(path, &d, param);
5512 if (errno == ENODATA) {
5513 /* Fill in struct for unstriped dir */
5515 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
5516 /* Use 0 until we find actual offset */
5517 param->fp_lmv_md->lum_stripe_offset = 0;
5518 param->fp_lmv_md->lum_stripe_count = 0;
5519 param->fp_lmv_md->lum_hash_type = 0;
5521 if (param->fp_check_foreign) {
5522 if (param->fp_exclude_foreign)
5531 if (param->fp_check_mdt_count) {
5532 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5535 decision = find_value_cmp(param->fp_lmv_md->lum_stripe_count,
5536 param->fp_mdt_count,
5537 param->fp_mdt_count_sign,
5538 param->fp_exclude_mdt_count, 1, 0);
5543 if (param->fp_hash_type) {
5545 __u32 type = param->fp_lmv_md->lum_hash_type &
5548 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5551 found = (1 << type) & param->fp_hash_type;
5552 if ((found && param->fp_exclude_hash_type) ||
5553 (!found && !param->fp_exclude_hash_type))
5557 if (param->fp_check_hash_flag) {
5558 __u32 flags = param->fp_lmv_md->lum_hash_type &
5559 ~LMV_HASH_TYPE_MASK;
5561 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5564 if (!(flags & param->fp_hash_inflags) ||
5565 (flags & param->fp_hash_exflags))
5570 param->fp_lmd->lmd_lmm.lmm_magic = 0;
5571 ret = get_lmd_info_fd(path, p, d, param->fp_lmd,
5572 param->fp_lum_size, GET_LMD_INFO);
5573 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
5574 find_check_lmm_info(param)) {
5575 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
5578 * We need to "fake" the "use the default" values
5579 * since the lmm struct is zeroed out at this point.
5581 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5582 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
5584 ostid_set_seq(&lmm->lmm_oi,
5585 FID_SEQ_LOV_DEFAULT);
5586 lmm->lmm_stripe_size = 0;
5587 lmm->lmm_stripe_count = 0;
5588 lmm->lmm_stripe_offset = -1;
5590 if (ret == 0 && (param->fp_mdt_uuid != NULL || gather_all)) {
5592 ret = llapi_file_fget_mdtidx(d,
5593 ¶m->fp_file_mdt_index);
5595 * Make sure lum_stripe_offset matches
5596 * mdt_index even for unstriped directories.
5598 if (ret == 0 && param->fp_get_lmv)
5599 param->fp_lmv_md->lum_stripe_offset =
5600 param->fp_file_mdt_index;
5601 } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
5603 * FIXME: we could get the MDT index from the
5604 * file's FID in lmd->lmd_lmm.lmm_oi without
5605 * opening the file, once we are sure that
5606 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
5607 * That would still be an ioctl() to map the
5608 * FID to the MDT, but not an open RPC.
5610 fd = open(path, O_RDONLY);
5612 ret = llapi_file_fget_mdtidx(fd,
5613 ¶m->fp_file_mdt_index);
5619 * For a special file, we assume it resides on
5620 * the same MDT as the parent directory.
5622 ret = llapi_file_fget_mdtidx(p,
5623 ¶m->fp_file_mdt_index);
5634 stripe_count = find_get_stripe_count(param);
5638 /* Check the file permissions from the stat info */
5639 if (param->fp_perm_sign) {
5640 decision = check_file_permissions(param, lmd->lmd_stx.stx_mode);
5645 if (param->fp_type && !checked_type) {
5646 if ((param->fp_check_mdt_count || param->fp_check_hash_flag ||
5647 param->fp_hash_type) && !S_ISDIR(lmd->lmd_stx.stx_mode))
5650 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
5651 if (param->fp_exclude_type)
5654 if (!param->fp_exclude_type)
5660 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
5661 if (lustre_fs && param->fp_got_uuids &&
5662 param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
5663 lmd->lmd_stx.stx_dev_minor)) {
5664 /* A lustre/lustre mount point is crossed. */
5665 param->fp_got_uuids = 0;
5666 param->fp_obds_printed = 0;
5667 param->fp_mdt_index = OBD_NOT_FOUND;
5668 param->fp_obd_index = OBD_NOT_FOUND;
5671 if (lustre_fs && !param->fp_got_uuids) {
5672 ret = setup_target_indexes((d != -1) ? d : p, path,
5677 param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
5678 lmd->lmd_stx.stx_dev_minor);
5679 } else if (!lustre_fs && param->fp_got_uuids) {
5680 /* A lustre/non-lustre mount point is crossed. */
5681 param->fp_got_uuids = 0;
5682 param->fp_mdt_index = OBD_NOT_FOUND;
5683 param->fp_obd_index = OBD_NOT_FOUND;
5687 if (param->fp_check_foreign) {
5688 decision = find_check_foreign(param);
5693 if (param->fp_check_stripe_size) {
5694 decision = find_check_stripe_size(param);
5699 if (param->fp_check_ext_size) {
5700 decision = find_check_ext_size(param);
5705 if (param->fp_check_stripe_count) {
5706 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
5707 param->fp_stripe_count_sign,
5708 param->fp_exclude_stripe_count, 1, 0);
5713 if (param->fp_check_layout) {
5714 decision = find_check_layout(param);
5719 /* If an OBD UUID is specified but none matches, skip this file. */
5720 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
5721 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
5725 * If an OST or MDT UUID is given, and some OST matches,
5728 if (param->fp_obd_index != OBD_NOT_FOUND ||
5729 param->fp_mdt_index != OBD_NOT_FOUND) {
5730 if (param->fp_obd_uuid) {
5731 if (check_obd_match(param)) {
5733 * If no mdtuuid is given, we are done.
5734 * Otherwise, fall through to the mdtuuid
5737 if (!param->fp_mdt_uuid)
5744 if (param->fp_mdt_uuid) {
5745 if (check_mdt_match(param))
5752 if (param->fp_check_uid) {
5753 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
5754 if (param->fp_exclude_uid)
5757 if (!param->fp_exclude_uid)
5762 if (param->fp_check_gid) {
5763 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
5764 if (param->fp_exclude_gid)
5767 if (!param->fp_exclude_gid)
5772 if (param->fp_check_projid || gather_all) {
5774 fd = open(path, O_RDONLY | O_NONBLOCK);
5777 ret = fget_projid(fd, &projid);
5780 if (param->fp_check_projid) {
5783 if (projid == param->fp_projid) {
5784 if (param->fp_exclude_projid)
5787 if (!param->fp_exclude_projid)
5793 if (param->fp_check_pool) {
5794 decision = find_check_pool(param);
5799 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
5800 param->fp_check_comp_start || param->fp_check_comp_end) {
5801 decision = find_check_comp_options(param);
5806 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
5807 decision = find_check_mirror_options(param);
5812 /* Check the time on mds. */
5814 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
5817 for_mds = lustre_fs ?
5818 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5819 decision = find_time_check(param, for_mds);
5824 if (param->fp_btime) {
5825 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
5830 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
5831 param->fp_btime, param->fp_bsign,
5832 param->fp_exclude_btime,
5833 param->fp_time_margin, 0);
5838 if (param->fp_newerxy) {
5841 for_mds = lustre_fs ?
5842 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5843 decision = find_newerxy_check(param, for_mds, true);
5852 if (param->fp_attrs || param->fp_neg_attrs) {
5853 decision = find_check_attr_options(param);
5858 flags = param->fp_lmd->lmd_flags;
5859 if (param->fp_check_size &&
5860 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5861 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5862 !(flags & OBD_MD_FLSIZE ||
5863 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
5866 if (param->fp_check_blocks &&
5867 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5868 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5869 !(flags & OBD_MD_FLBLOCKS ||
5870 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
5873 if (param->fp_xattr_match_info) {
5874 decision = find_check_xattrs(path, param->fp_xattr_match_info);
5880 * When checking nlink, stat(2) is needed for multi-striped directories
5881 * because the nlink value retrieved from the MDS above comes from
5882 * the number of stripes for the dir.
5883 * The posix stat call below fills in the correct number of links.
5884 * Single-stripe directories and regular files already have the
5885 * correct nlink value.
5887 if (param->fp_nlink && S_ISDIR(lmd->lmd_stx.stx_mode) &&
5888 (param->fp_lmv_md->lum_stripe_count != 0))
5892 * If file still fits the request, ask ost for updated info.
5893 * The regular stat is almost of the same speed as some new
5894 * 'glimpse-size-ioctl'.
5896 if (!decision || gather_all) {
5900 * For regular files with the stripe the decision may have not
5901 * been taken yet if *time or size is to be checked.
5903 if (param->fp_obd_index != OBD_NOT_FOUND)
5904 print_failed_tgt(param, path, LL_STATFS_LOV);
5906 if (param->fp_mdt_index != OBD_NOT_FOUND)
5907 print_failed_tgt(param, path, LL_STATFS_LMV);
5910 ret = fstat_f(d, &st);
5911 else if (de != NULL)
5912 ret = fstatat_f(p, de->d_name, &st,
5913 AT_SYMLINK_NOFOLLOW);
5915 ret = lstat_f(path, &st);
5918 if (errno == ENOENT) {
5919 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
5920 "warning: %s: %s does not exist",
5925 llapi_error(LLAPI_MSG_ERROR, ret,
5926 "%s: stat on %s failed",
5932 convert_lmd_statx(param->fp_lmd, &st, true);
5933 /* Check the time on osc. */
5934 decision = find_time_check(param, 0);
5938 if (param->fp_newerxy) {
5939 decision = find_newerxy_check(param, 0, false);
5949 if (param->fp_nlink) {
5950 decision = find_value_cmp(lmd->lmd_stx.stx_nlink,
5951 param->fp_nlink, param->fp_nlink_sign,
5952 param->fp_exclude_nlink, 1, 0);
5957 if (param->fp_check_size) {
5958 decision = find_value_cmp(lmd->lmd_stx.stx_size,
5960 param->fp_size_sign,
5961 param->fp_exclude_size,
5962 param->fp_size_units, 0);
5967 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
5968 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
5970 param->fp_blocks_sign,
5971 param->fp_exclude_blocks,
5972 param->fp_blocks_units, 0);
5978 if (param->fp_format_printf_str)
5979 printf_format_string(param, path, projid, d);
5981 llapi_printf(LLAPI_MSG_NORMAL, "%s%c", path,
5982 param->fp_zero_end ? '\0' : '\n');
5986 /* Do not get down anymore? */
5987 if (param->fp_depth == param->fp_max_depth) {
5998 static int cb_migrate_mdt_init(char *path, int p, int *dp,
5999 void *param_data, struct dirent64 *de)
6001 struct find_param *param = (struct find_param *)param_data;
6002 struct lmv_user_md *lmu = param->fp_lmv_md;
6004 char raw[MAX_IOC_BUFLEN] = {'\0'};
6006 struct obd_ioctl_data data = { 0 };
6012 if (p == -1 && dp == NULL)
6018 if (dp != NULL && *dp != -1)
6022 tmp_p = open_parent(path);
6026 llapi_error(LLAPI_MSG_ERROR, ret,
6027 "can not open %s", path);
6032 path_copy = strdup(path);
6033 filename = basename(path_copy);
6035 data.ioc_inlbuf1 = (char *)filename;
6036 data.ioc_inllen1 = strlen(filename) + 1;
6037 data.ioc_inlbuf2 = (char *)lmu;
6038 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
6041 if (param->fp_depth == param->fp_max_depth)
6042 data.ioc_type = MDS_MIGRATE_NSONLY;
6043 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
6045 llapi_error(LLAPI_MSG_ERROR, ret,
6046 "%s: error packing ioctl data", __func__);
6051 ret = ioctl(tmp_p, LL_IOC_MIGRATE, rawbuf);
6053 if (errno == EBUSY && !retry) {
6055 * because migrate may not be able to lock all involved
6056 * objects in order, for some of them it try lock, while
6057 * there may be conflicting COS locks and cause migrate
6058 * fail with EBUSY, hope a sync() could cause
6059 * transaction commit and release these COS locks.
6064 } else if (errno == EALREADY) {
6065 if (param->fp_verbose & VERBOSE_DETAIL)
6066 llapi_printf(LLAPI_MSG_NORMAL,
6067 "%s migrated to MDT%d already\n",
6068 path, lmu->lum_stripe_offset);
6072 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
6076 } else if (param->fp_verbose & VERBOSE_DETAIL) {
6077 llapi_printf(LLAPI_MSG_NORMAL,
6078 "migrate %s to MDT%d stripe count %d\n",
6079 path, lmu->lum_stripe_offset,
6080 lmu->lum_stripe_count);
6084 /* Do not get down anymore? */
6085 if (param->fp_depth == param->fp_max_depth)
6092 * If the directory is being migration, we need
6093 * close the directory after migration,
6094 * so the old directory cache will be cleanup
6095 * on the client side, and re-open to get the
6096 * new directory handle
6098 *dp = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
6101 llapi_error(LLAPI_MSG_ERROR, ret,
6102 "%s: Failed to open '%s'", __func__, path);
6114 /* dir migration finished, shrink its stripes */
6115 static int cb_migrate_mdt_fini(char *path, int p, int *dp, void *data,
6116 struct dirent64 *de)
6118 struct find_param *param = data;
6119 struct lmv_user_md *lmu = param->fp_lmv_md;
6120 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
6123 if (de && de->d_type != DT_DIR)
6128 * close it before setxattr because the latter may destroy the
6129 * original object, and cause close fail.
6137 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
6138 if (ret == -EALREADY)
6141 cb_common_fini(path, p, dp, data, de);
6145 int llapi_migrate_mdt(char *path, struct find_param *param)
6147 param->fp_stop_on_error = 1;
6148 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
6152 int llapi_mv(char *path, struct find_param *param)
6154 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
6155 static bool printed;
6158 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
6159 "%s() is deprecated, use llapi_migrate_mdt() instead",
6164 return llapi_migrate_mdt(path, param);
6168 * Check string for escape sequences and print a message to stdout
6169 * if any invalid escapes are found.
6171 * @param[in] c Pointer to character immediately following the
6172 * '\' character indicating the start of an escape
6174 * @return Number of characters examined in the escape sequence
6175 * (regardless of whether the sequence is valid or not).
6177 static int validate_printf_esc(char *c)
6179 char *valid_esc = "nt\\";
6182 /* backslash at end of string */
6183 llapi_err_noerrno(LLAPI_MSG_WARN,
6184 "warning: '\\' at end of -printf format string\n");
6188 if (!strchr(valid_esc, *c))
6189 /* Invalid escape character */
6190 llapi_err_noerrno(LLAPI_MSG_WARN,
6191 "warning: unrecognized escape: '\\%c'\n", *c);
6197 * Check string for format directives and print a message to stdout
6198 * if any invalid directives are found.
6200 * @param[in] c Pointer to character immediately following the
6201 * '%' character indicating the start of a format
6203 * @return Number of characters examined in the format directive
6204 * (regardless of whether the directive is valid or not).
6206 static int validate_printf_fmt(char *c)
6208 char *valid_fmt_single = "abcGkmnpstUwy%";
6209 char *valid_fmt_double = "ACTW";
6210 char *valid_fmt_lustre = "aAcFhioPpS";
6211 char curr = *c, next;
6214 llapi_err_noerrno(LLAPI_MSG_WARN,
6215 "warning: '%%' at end of -printf format string\n");
6220 if ((next == '\0') || (next == '%') || (next == '\\'))
6221 /* Treat as single char format directive */
6224 /* Check format directives with multiple characters */
6225 if (strchr(valid_fmt_double, curr)) {
6226 /* For now, only valid formats are followed by '@' char */
6228 llapi_err_noerrno(LLAPI_MSG_WARN,
6229 "warning: unrecognized format directive: '%%%c%c'\n",
6234 /* Lustre formats always start with 'L' */
6236 if (!strchr(valid_fmt_lustre, next))
6237 llapi_err_noerrno(LLAPI_MSG_WARN,
6238 "warning: unrecognized format directive: '%%%c%c'\n",
6245 if (!strchr(valid_fmt_single, curr))
6246 llapi_err_noerrno(LLAPI_MSG_WARN,
6247 "warning: unrecognized format directive: '%%%c'\n", curr);
6252 * Validate the user-supplied string for the -printf option and report
6253 * any invalid backslash escape sequences or format directives.
6255 * @param[in] param Structure containing info about invocation of lfs find
6258 static void validate_printf_str(struct find_param *param)
6260 char *c = param->fp_format_printf_str;
6266 ret = validate_printf_fmt(++c);
6270 ret = validate_printf_esc(++c);
6280 int llapi_find(char *path, struct find_param *param)
6282 if (param->fp_format_printf_str)
6283 validate_printf_str(param);
6284 return param_callback(path, cb_find_init, cb_common_fini, param);
6288 * Get MDT number that the file/directory inode referenced
6289 * by the open fd resides on.
6290 * Return 0 and mdtidx on success, or -ve errno.
6292 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
6294 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
6299 static int cb_get_mdt_index(char *path, int p, int *dp, void *data,
6300 struct dirent64 *de)
6302 struct find_param *param = (struct find_param *)data;
6303 int d = dp == NULL ? -1 : *dp;
6306 bool hex = param->fp_hex_idx;
6308 if (p == -1 && d == -1)
6312 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6313 } else /* if (p != -1) */ {
6316 fd = open(path, O_RDONLY | O_NOCTTY);
6318 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
6326 if (ret == -ENODATA) {
6327 if (!param->fp_obd_uuid)
6328 llapi_printf(LLAPI_MSG_NORMAL,
6329 "'%s' has no stripe info\n", path);
6331 } else if (ret == -ENOENT) {
6332 llapi_error(LLAPI_MSG_WARN, ret,
6333 "warning: %s: '%s' does not exist",
6336 } else if (ret == -ENOTTY) {
6337 llapi_error(LLAPI_MSG_ERROR, ret,
6338 "%s: '%s' not on a Lustre fs",
6341 llapi_error(LLAPI_MSG_ERROR, ret,
6342 "error: %s: '%s' failed get_mdtidx",
6348 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
6349 llapi_printf(LLAPI_MSG_NORMAL, hex ? "%#x\n" : "%d\n", mdtidx);
6351 llapi_printf(LLAPI_MSG_NORMAL, hex ? "%s\nmdt_index:\t%#x\n"
6352 : "%s\nmdt_index:\t%d\n",
6356 /* Do not go down anymore? */
6357 if (param->fp_depth == param->fp_max_depth)
6365 static int cb_getstripe(char *path, int p, int *dp, void *data,
6366 struct dirent64 *de)
6368 struct find_param *param = (struct find_param *)data;
6369 int d = dp == NULL ? -1 : *dp, fd = -1;
6372 if (p == -1 && d == -1)
6375 if (param->fp_obd_uuid) {
6376 param->fp_quiet = 1;
6377 ret = setup_obd_uuid(d != -1 ? d : p, path, param);
6382 if (!param->fp_no_follow && de && de->d_type == DT_LNK && d == -1)
6383 d = fd = open(path, O_RDONLY | O_DIRECTORY);
6385 if (d != -1 && (param->fp_get_lmv || param->fp_get_default_lmv))
6386 ret = cb_get_dirstripe(path, &d, param);
6388 ret = get_lmd_info_fd(path, p, d, ¶m->fp_lmd->lmd_lmm,
6389 param->fp_lum_size, GET_LMD_STRIPE);
6390 else if (d == -1 && (param->fp_get_lmv || param->fp_get_default_lmv)) {
6391 /* in case of a dangling or valid faked symlink dir, opendir()
6392 * should have return either EINVAL or ENOENT, so let's try
6393 * to get LMV just in case, and by opening it as a file but
6394 * with O_NOFOLLOW ...
6396 int flag = O_RDONLY;
6398 if (param->fp_no_follow)
6399 flag = O_RDONLY | O_NOFOLLOW;
6400 fd = open(path, flag);
6404 ret = cb_get_dirstripe(path, &fd, param);
6406 llapi_lov_dump_user_lmm(param, path, LDF_IS_DIR);
6409 } else if (d == -1) {
6410 if (!param->fp_no_follow && de && de->d_type == DT_LNK) {
6411 /* open the target of symlink as a file */
6412 fd = open(path, O_RDONLY);
6416 ret = get_lmd_info_fd(path, p, fd, ¶m->fp_lmd->lmd_lmm,
6417 param->fp_lum_size, GET_LMD_STRIPE);
6425 if (errno == ENODATA && d != -1) {
6427 * We need to "fake" the "use the default" values
6428 * since the lmm struct is zeroed out at this point.
6429 * The magic needs to be set in order to satisfy
6430 * a check later on in the code path.
6431 * The object_seq needs to be set for the "(Default)"
6432 * prefix to be displayed.
6434 if (param->fp_get_default_lmv) {
6435 struct lmv_user_md *lum = param->fp_lmv_md;
6439 lum->lum_magic = LMV_USER_MAGIC;
6440 lum->lum_stripe_count = 0;
6441 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
6443 } else if (param->fp_get_lmv) {
6444 struct lmv_user_md *lum = param->fp_lmv_md;
6447 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6450 lum->lum_magic = LMV_MAGIC_V1;
6451 lum->lum_stripe_count = 0;
6452 lum->lum_stripe_offset = mdtidx;
6455 struct lov_user_md *lmm =
6456 ¶m->fp_lmd->lmd_lmm;
6458 lmm->lmm_magic = LOV_USER_MAGIC_V1;
6460 ostid_set_seq(&lmm->lmm_oi,
6461 FID_SEQ_LOV_DEFAULT);
6462 lmm->lmm_stripe_count = 0;
6463 lmm->lmm_stripe_size = 0;
6464 lmm->lmm_stripe_offset = -1;
6467 } else if (errno == ENODATA && p != -1) {
6468 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
6469 llapi_printf(LLAPI_MSG_NORMAL,
6470 "%s has no stripe info\n", path);
6472 } else if (errno == ENOENT) {
6473 llapi_error(LLAPI_MSG_WARN, -ENOENT,
6474 "warning: %s: %s does not exist",
6477 } else if (errno == ENOTTY) {
6479 llapi_error(LLAPI_MSG_ERROR, ret,
6480 "%s: '%s' not on a Lustre fs?",
6485 llapi_error(LLAPI_MSG_ERROR, ret,
6486 "error: %s: %s failed for %s",
6488 "LL_IOC_LOV_GETSTRIPE" :
6489 "IOC_MDC_GETFILESTRIPE", path);
6496 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
6497 llapi_lov_dump_user_lmm(param, path, d != -1 ? LDF_IS_DIR : 0);
6500 /* Do not get down anymore? */
6501 if (param->fp_depth == param->fp_max_depth)
6509 int llapi_getstripe(char *path, struct find_param *param)
6511 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
6512 cb_get_mdt_index : cb_getstripe,
6513 cb_common_fini, param);
6516 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
6517 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6519 char raw[MAX_IOC_BUFLEN] = {'\0'};
6521 struct obd_ioctl_data data = { 0 };
6524 data.ioc_inlbuf1 = (char *)&type;
6525 data.ioc_inllen1 = sizeof(__u32);
6526 data.ioc_inlbuf2 = (char *)&index;
6527 data.ioc_inllen2 = sizeof(__u32);
6528 data.ioc_pbuf1 = (char *)stat_buf;
6529 data.ioc_plen1 = sizeof(struct obd_statfs);
6530 data.ioc_pbuf2 = (char *)uuid_buf;
6531 data.ioc_plen2 = sizeof(struct obd_uuid);
6533 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
6535 llapi_error(LLAPI_MSG_ERROR, rc,
6536 "%s: error packing ioctl data", __func__);
6540 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
6542 return rc < 0 ? -errno : 0;
6545 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
6546 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6551 fd = open(path, O_RDONLY);
6554 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
6557 * If we can't even open a file on the filesystem (e.g. with
6558 * -ESHUTDOWN), force caller to exit or it will loop forever.
6563 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
6570 #define MAX_STRING_SIZE 128
6572 int llapi_ping(char *obd_type, char *obd_name)
6574 int flags = O_RDONLY;
6575 char buf[1] = { 0 };
6579 rc = cfs_get_param_paths(&path, "%s/%s/ping",
6580 obd_type, obd_name);
6584 fd = open(path.gl_pathv[0], flags);
6586 if (errno == EACCES && flags == O_RDONLY) {
6591 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
6596 if (flags == O_RDONLY)
6597 rc = read(fd, buf, sizeof(buf));
6599 rc = write(fd, buf, sizeof(buf));
6607 cfs_free_param_data(&path);
6611 int llapi_target_iterate(int type_num, char **obd_type,
6612 void *args, llapi_cb_t cb)
6618 for (i = 0; i < type_num; i++) {
6621 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
6625 for (j = 0; j < param.gl_pathc; j++) {
6626 char obd_uuid[UUID_MAX + 1];
6630 fp = fopen(param.gl_pathv[j], "r");
6633 llapi_error(LLAPI_MSG_ERROR, rc,
6634 "error: opening '%s'",
6639 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
6641 llapi_error(LLAPI_MSG_ERROR, rc,
6642 "error: reading '%s'",
6647 /* Extract the obd_name from the sysfs path.
6648 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
6650 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
6656 /* skip /fs/lustre/'obd_type'/ */
6657 obd_name += strlen(obd_type[i]) + 12;
6658 /* chop off after obd_name */
6659 ptr = strrchr(obd_name, '/');
6663 cb(obd_type[i], obd_name, obd_uuid, args);
6668 cfs_free_param_data(¶m);
6673 cfs_free_param_data(¶m);
6677 struct check_target_filter {
6682 static void do_target_check(char *obd_type_name, char *obd_name,
6683 char *obd_uuid, void *args)
6686 struct check_target_filter *filter = args;
6688 if (filter != NULL) {
6689 /* check nid if obd type is mgc */
6690 if (strcmp(obd_type_name, "mgc") == 0) {
6691 if (strcmp(obd_name + 3, filter->nid) != 0)
6694 /* check instance for other types of device (osc/mdc) */
6695 else if (strstr(obd_name, filter->instance) == NULL)
6699 rc = llapi_ping(obd_type_name, obd_name);
6701 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
6703 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
6705 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
6708 int llapi_target_check(int type_num, char **obd_type, char *dir)
6710 char nid[MAX_LINE_LEN], instance[MAX_INSTANCE_LEN];
6711 struct check_target_filter filter = {NULL, NULL};
6714 if (dir == NULL || dir[0] == '\0')
6715 return llapi_target_iterate(type_num, obd_type, NULL,
6718 rc = get_root_path(WANT_NID | WANT_ERROR, NULL, NULL, dir, -1, NULL,
6721 llapi_error(LLAPI_MSG_ERROR, rc,
6722 "cannot get nid of path '%s'", dir);
6727 rc = llapi_get_instance(dir, instance, ARRAY_SIZE(instance));
6730 filter.instance = instance;
6732 return llapi_target_iterate(type_num, obd_type, &filter,
6736 #undef MAX_STRING_SIZE
6738 /* Is this a lustre fs? */
6739 int llapi_is_lustre_mnttype(const char *type)
6741 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
6744 /* Is this a lustre client fs? */
6745 int llapi_is_lustre_mnt(struct mntent *mnt)
6747 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
6748 strstr(mnt->mnt_fsname, ":/") != NULL);
6751 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
6753 char fsname[PATH_MAX + 1];
6757 rc = llapi_search_fsname(mnt, fsname);
6761 root = open(mnt, O_RDONLY | O_DIRECTORY);
6764 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
6768 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
6771 if (rc == -ENOENT && LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd))
6772 llapi_error(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, rc,
6773 "Cannot find pool '%s'", qctl->qc_poolname);
6779 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
6784 root = open(mnt, O_RDONLY | O_DIRECTORY);
6787 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
6791 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
6794 llapi_error(LLAPI_MSG_ERROR, rc,
6795 "ioctl on %s for getting connect flags failed", mnt);
6802 * Flush cached pages from all clients.
6804 * \param fd File descriptor
6808 int llapi_file_flush(int fd)
6812 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);