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_POSIX, .lft_name = "posix"},
98 {.lft_type = LU_FOREIGN_TYPE_PCCRW, .lft_name = "pccrw"},
99 {.lft_type = LU_FOREIGN_TYPE_PCCRO, .lft_name = "pccro"},
100 {.lft_type = LU_FOREIGN_TYPE_S3, .lft_name = "S3"},
101 {.lft_type = LU_FOREIGN_TYPE_SYMLINK, .lft_name = "symlink"},
102 /* must be the last element */
103 {.lft_type = LU_FOREIGN_TYPE_UNKNOWN, .lft_name = NULL}
104 /* array max dimension must be <= UINT32_MAX */
107 void llapi_msg_set_level(int level)
109 /* ensure level is in the good range */
110 if (level < LLAPI_MSG_OFF)
111 llapi_msg_level = LLAPI_MSG_OFF;
112 else if (level > LLAPI_MSG_MAX)
113 llapi_msg_level = LLAPI_MSG_MAX;
115 llapi_msg_level = level;
118 int llapi_msg_get_level(void)
120 return llapi_msg_level;
123 void llapi_set_command_name(const char *cmd)
125 liblustreapi_cmd = cmd;
128 void llapi_clear_command_name(void)
130 liblustreapi_cmd = NULL;
133 static void error_callback_default(enum llapi_message_level level, int err,
134 const char *fmt, va_list ap)
136 bool has_nl = strchr(fmt, '\n') != NULL;
138 if (liblustreapi_cmd != NULL)
139 fprintf(stderr, "%s %s: ", program_invocation_short_name,
142 fprintf(stderr, "%s: ", program_invocation_short_name);
145 if (level & LLAPI_MSG_NO_ERRNO) {
146 vfprintf(stderr, fmt, ap);
148 fprintf(stderr, "\n");
153 * Remove trailing linefeed so error string can be appended.
154 * @fmt is a const string, so we can't modify it directly.
156 if (has_nl && (newfmt = strdup(fmt)))
157 *strrchr(newfmt, '\n') = '\0';
159 newfmt = (char *)fmt;
161 vfprintf(stderr, newfmt, ap);
164 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
168 static void info_callback_default(enum llapi_message_level level, int err,
169 const char *fmt, va_list ap)
172 if (liblustreapi_cmd != NULL) {
173 fprintf(stdout, "%s %s: ",
174 program_invocation_short_name,
177 fprintf(stdout, "%s: ", program_invocation_short_name);
180 vfprintf(stdout, fmt, ap);
183 static llapi_log_callback_t llapi_error_callback = error_callback_default;
184 static llapi_log_callback_t llapi_info_callback = info_callback_default;
187 /* llapi_error will preserve errno */
188 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
191 int tmp_errno = errno;
193 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
197 llapi_error_callback(level, abs(err), fmt, args);
202 /* llapi_printf will preserve errno */
203 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
206 int tmp_errno = errno;
208 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
212 llapi_info_callback(level, 0, fmt, args);
218 * Set a custom error logging function. Passing in NULL will reset the logging
219 * callback to its default value.
221 * This function returns the value of the old callback.
223 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
225 llapi_log_callback_t old = llapi_error_callback;
228 llapi_error_callback = cb;
230 llapi_error_callback = error_callback_default;
236 * Set a custom info logging function. Passing in NULL will reset the logging
237 * callback to its default value.
239 * This function returns the value of the old callback.
241 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
243 llapi_log_callback_t old = llapi_info_callback;
246 llapi_info_callback = cb;
248 llapi_info_callback = info_callback_default;
254 * Convert a size string (with optional suffix) into binary value.
256 * \param optarg [in] string containing numeric value with optional
257 * KMGTPE suffix to specify the unit size.
258 * The \a string may be a decimal value.
259 * \param size [out] pointer to integer numeric value to be returned
260 * \param size_units [in] units of \a string if dimensionless. Must be
261 * initialized by caller. If zero, units = bytes.
262 * \param bytes_spec [in] if suffix 'b' means bytes or 512-byte sectors.
265 * \retval -EINVAL negative or too large size, or unknown suffix
267 int llapi_parse_size(const char *optarg, unsigned long long *size,
268 unsigned long long *size_units, int bytes_spec)
271 char *argbuf = (char *)optarg;
272 unsigned long long frac = 0, frac_d = 1;
274 if (strncmp(optarg, "-", 1) == 0)
277 if (*size_units == 0)
280 *size = strtoull(argbuf, &end, 0);
281 if (end != NULL && *end == '.') {
285 frac = strtoull(argbuf, &end, 10);
286 /* count decimal places */
287 for (i = 0; i < (end - argbuf); i++)
292 char next = tolower(*(end + 1));
294 switch (tolower(*end)) {
299 if (*size & (~0ULL << (64 - 9)))
301 *size_units = 1 << 9;
308 if (*size & (~0ULL << (64 - 10)))
310 *size_units = 1 << 10;
313 if (*size & (~0ULL << (64 - 20)))
315 *size_units = 1 << 20;
318 if (*size & (~0ULL << (64 - 30)))
320 *size_units = 1 << 30;
323 if (*size & (~0ULL << (64 - 40)))
325 *size_units = 1ULL << 40;
328 if (*size & (~0ULL << (64 - 50)))
330 *size_units = 1ULL << 50;
333 if (*size & (~0ULL << (64 - 60)))
335 *size_units = 1ULL << 60;
340 if (next != '\0' && next != 'i' && next != 'b')
343 *size = *size * *size_units + frac * *size_units / frac_d;
349 * Verify the setstripe parameters before using.
350 * This is a pair method for comp_args_to_layout()/llapi_layout_sanity_cb()
351 * when just 1 component or a non-PFL layout is given.
353 * \param[in] param stripe parameters
354 * \param[in] pool_name pool name
355 * \param[in] fsname lustre FS name
358 * < 0, error code on failre
360 static int llapi_stripe_param_verify(const struct llapi_stripe_param *param,
361 const char **pool_name, char *fsname)
364 static int page_size;
367 if (page_size == 0) {
369 * 64 KB is the largest common page size (on ia64/PPC/ARM),
370 * but check the local page size just in case. The page_size
371 * will not change for the lifetime of this process at least.
373 page_size = LOV_MIN_STRIPE_SIZE;
374 if (getpagesize() > page_size) {
375 page_size = getpagesize();
376 llapi_err_noerrno(LLAPI_MSG_WARN,
377 "warning: page size (%u) larger than expected (%u)",
378 page_size, LOV_MIN_STRIPE_SIZE);
381 if (!llapi_stripe_size_is_aligned(param->lsp_stripe_size)) {
383 llapi_error(LLAPI_MSG_ERROR, rc,
384 "error: bad stripe_size %llu, must be an even multiple of %d bytes",
385 param->lsp_stripe_size, page_size);
388 if (!llapi_stripe_index_is_valid(param->lsp_stripe_offset)) {
390 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
391 param->lsp_stripe_offset);
394 if (llapi_stripe_size_is_too_big(param->lsp_stripe_size)) {
396 llapi_error(LLAPI_MSG_ERROR, rc,
397 "error: stripe size '%llu' over 4GB limit",
398 param->lsp_stripe_size);
402 count = param->lsp_stripe_count;
403 if (param->lsp_stripe_pattern & LOV_PATTERN_MDT) {
405 llapi_error(LLAPI_MSG_ERROR, rc,
406 "Invalid pattern: '-L mdt', must be specified "
410 if (!llapi_stripe_count_is_valid(count)) {
412 llapi_error(LLAPI_MSG_ERROR, rc,
413 "Invalid stripe count %d\n", count);
418 /* Make sure we have a good pool */
419 if (*pool_name != NULL) {
420 if (!llapi_pool_name_is_valid(pool_name)) {
422 llapi_error(LLAPI_MSG_ERROR, rc,
423 "Invalid Poolname '%s'", *pool_name);
427 /* Make sure the pool exists */
428 rc = llapi_search_ost(fsname, *pool_name, NULL);
430 llapi_error(LLAPI_MSG_ERROR, rc,
431 "pool '%s fsname %s' does not exist",
443 static int dir_stripe_limit_check(int stripe_offset, int stripe_count,
448 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
450 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
454 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
456 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
461 if (!llapi_dir_hash_type_is_valid(hash_type)) {
463 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
471 * Trim a trailing newline from a string, if it exists.
473 int llapi_chomp_string(char *buf)
489 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
491 static int get_param_lov(const char *path, const char *param,
492 char *buf, size_t buf_size)
494 struct obd_uuid uuid;
497 rc = llapi_file_get_lov_uuid(path, &uuid);
501 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
506 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
508 static int get_param_lmv(const char *path, const char *param,
509 char *buf, size_t buf_size)
511 struct obd_uuid uuid;
514 rc = llapi_file_get_lmv_uuid(path, &uuid);
518 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
522 static int get_mds_md_size(const char *path)
524 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
527 * Rather than open the file and do the ioctl to get the
528 * instance name and close the file and search for the param
529 * file and open the param file and read the param file and
530 * parse the value and close the param file, let's just return
531 * a large enough value. It's 2020, RAM is cheap and this is
535 if (md_size < XATTR_SIZE_MAX)
536 md_size = XATTR_SIZE_MAX;
541 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
543 return get_param_lmv(path, "uuid", buf, bufsize);
547 * Open a Lustre file.
549 * \param name the name of the file to be opened
550 * \param flags access mode, see flags in open(2)
551 * \param mode permission of the file if it is created, see mode in open(2)
552 * \param param stripe pattern of the newly created file
554 * \retval file descriptor of opened file
555 * \retval negative errno on failure
557 int llapi_file_open_param(const char *name, int flags, mode_t mode,
558 const struct llapi_stripe_param *param)
560 char fsname[MAX_OBD_NAME + 1] = { 0 };
561 struct lov_user_md *lum = NULL;
562 const char *pool_name = param->lsp_pool;
566 /* Make sure we are on a Lustre file system */
567 rc = llapi_search_fsname(name, fsname);
569 llapi_error(LLAPI_MSG_ERROR, rc,
570 "'%s' is not on a Lustre filesystem", name);
574 /* Check if the stripe pattern is sane. */
575 rc = llapi_stripe_param_verify(param, &pool_name, fsname);
579 if (param->lsp_is_specific)
580 lum_size = lov_user_md_size(param->lsp_stripe_count,
581 LOV_USER_MAGIC_SPECIFIC);
583 lum_size = sizeof(struct lov_user_md_v3);
585 lum_size = sizeof(*lum);
587 lum = calloc(1, lum_size);
592 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
594 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
595 flags = O_DIRECTORY | O_RDONLY;
602 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
607 /* Initialize IOCTL striping pattern structure */
608 lum->lmm_magic = LOV_USER_MAGIC_V1;
609 lum->lmm_pattern = param->lsp_stripe_pattern;
610 lum->lmm_stripe_size = param->lsp_stripe_size;
611 lum->lmm_stripe_count = param->lsp_stripe_count;
612 lum->lmm_stripe_offset = param->lsp_stripe_offset;
613 if (pool_name != NULL) {
614 struct lov_user_md_v3 *lumv3 = (void *)lum;
616 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
617 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
619 if (param->lsp_is_specific) {
620 struct lov_user_md_v3 *lumv3 = (void *)lum;
623 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
624 if (pool_name == NULL) {
626 * LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
627 * OST list, therefore if pool is not specified we have
628 * to pack a null pool name for placeholder.
630 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
633 for (i = 0; i < param->lsp_stripe_count; i++)
634 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
637 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
638 char errmsg[512] = "stripe already set";
641 if (errno != EEXIST && errno != EALREADY)
642 strncpy(errmsg, strerror(errno), sizeof(errmsg) - 1);
643 if (rc == -EREMOTEIO)
644 snprintf(errmsg, sizeof(errmsg),
645 "inactive OST among your specified %d OST(s)",
646 param->lsp_stripe_count);
648 llapi_err_noerrno(LLAPI_MSG_ERROR,
649 "setstripe error for '%s': %s", name, errmsg);
660 int llapi_file_is_encrypted(int fd)
665 rc = ioctl(fd, FS_IOC_GETFLAGS, &flags);
669 return !!(flags & LUSTRE_ENCRYPT_FL);
672 int llapi_file_open_pool(const char *name, int flags, int mode,
673 unsigned long long stripe_size, int stripe_offset,
674 int stripe_count, int stripe_pattern, char *pool_name)
676 const struct llapi_stripe_param param = {
677 .lsp_stripe_size = stripe_size,
678 .lsp_stripe_count = stripe_count,
679 .lsp_stripe_pattern = stripe_pattern,
680 .lsp_stripe_offset = stripe_offset,
681 .lsp_pool = pool_name
683 return llapi_file_open_param(name, flags, mode, ¶m);
686 int llapi_file_open(const char *name, int flags, int mode,
687 unsigned long long stripe_size, int stripe_offset,
688 int stripe_count, int stripe_pattern)
690 return llapi_file_open_pool(name, flags, mode, stripe_size,
691 stripe_offset, stripe_count,
692 stripe_pattern, NULL);
695 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
696 __u32 flags, char *foreign_lov)
699 struct lov_foreign_md *lfm;
702 if (foreign_lov == NULL) {
704 llapi_error(LLAPI_MSG_ERROR, rc,
705 "foreign LOV EA content must be provided");
709 len = strlen(foreign_lov);
710 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
713 llapi_error(LLAPI_MSG_ERROR, rc,
714 "foreign LOV EA size %zu (must be 0 < len < %zu)",
715 len, XATTR_SIZE_MAX -
716 offsetof(struct lov_foreign_md, lfm_value));
720 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
723 llapi_error(LLAPI_MSG_ERROR, rc,
724 "failed to allocate lov_foreign_md");
728 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
731 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
735 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
736 lfm->lfm_length = len;
737 lfm->lfm_type = type;
738 lfm->lfm_flags = flags;
739 memcpy(lfm->lfm_value, foreign_lov, len);
741 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
742 char *errmsg = "stripe already set";
746 errmsg = "not on a Lustre filesystem";
747 else if (errno == EEXIST || errno == EALREADY)
748 errmsg = "stripe already set";
750 errmsg = strerror(errno);
752 llapi_err_noerrno(LLAPI_MSG_ERROR,
753 "setstripe error for '%s': %s", name, errmsg);
769 int llapi_file_create(const char *name, unsigned long long stripe_size,
770 int stripe_offset, int stripe_count, int stripe_pattern)
774 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
775 stripe_offset, stripe_count, stripe_pattern,
784 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
785 int stripe_offset, int stripe_count,
786 int stripe_pattern, char *pool_name)
790 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
791 stripe_offset, stripe_count, stripe_pattern,
800 static int verify_dir_param(const char *name,
801 const struct llapi_stripe_param *param)
803 char fsname[MAX_OBD_NAME + 1] = { 0 };
804 char *pool_name = param->lsp_pool;
807 /* Make sure we are on a Lustre file system */
808 rc = llapi_search_fsname(name, fsname);
810 llapi_error(LLAPI_MSG_ERROR, rc,
811 "'%s' is not on a Lustre filesystem",
816 /* Check if the stripe pattern is sane. */
817 rc = dir_stripe_limit_check(param->lsp_stripe_offset,
818 param->lsp_stripe_count,
819 param->lsp_stripe_pattern);
823 /* Make sure we have a good pool */
824 if (pool_name != NULL) {
826 * in case user gives the full pool name <fsname>.<poolname>,
829 char *ptr = strchr(pool_name, '.');
833 if (strcmp(pool_name, fsname) != 0) {
835 llapi_err_noerrno(LLAPI_MSG_ERROR,
836 "Pool '%s' is not on filesystem '%s'",
843 /* Make sure the pool exists and is non-empty */
844 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
846 char *err = rc == 0 ? "has no OSTs" : "does not exist";
848 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
849 fsname, pool_name, err);
854 /* sanity check of target list */
855 if (param->lsp_is_specific) {
856 char mdtname[MAX_OBD_NAME + 64];
860 for (i = 0; i < param->lsp_stripe_count; i++) {
861 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
862 fsname, param->lsp_tgts[i]);
863 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
868 llapi_error(LLAPI_MSG_ERROR, rc,
869 "%s: cannot find MDT %s in %s",
876 /* Make sure stripe offset is in MDT list. */
877 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
881 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
882 "%s: stripe offset '%d' is not in the target list",
883 __func__, param->lsp_stripe_offset);
891 static inline void param2lmu(struct lmv_user_md *lmu,
892 const struct llapi_stripe_param *param)
894 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
896 lmu->lum_stripe_count = param->lsp_stripe_count;
897 lmu->lum_stripe_offset = param->lsp_stripe_offset;
898 lmu->lum_hash_type = param->lsp_stripe_pattern;
899 lmu->lum_max_inherit = param->lsp_max_inherit;
900 lmu->lum_max_inherit_rr = param->lsp_max_inherit_rr;
901 if (param->lsp_pool != NULL)
902 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
903 if (param->lsp_is_specific) {
906 for (i = 0; i < param->lsp_stripe_count; i++)
907 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
911 int llapi_dir_set_default_lmv(const char *name,
912 const struct llapi_stripe_param *param)
914 struct lmv_user_md lmu = { 0 };
918 rc = verify_dir_param(name, param);
922 /* TODO: default lmv doesn't support specific targets yet */
923 if (param->lsp_is_specific)
926 param2lmu(&lmu, param);
928 fd = open(name, O_DIRECTORY | O_RDONLY);
931 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
935 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
937 char *errmsg = "stripe already set";
940 if (errno != EEXIST && errno != EALREADY)
941 errmsg = strerror(errno);
943 llapi_err_noerrno(LLAPI_MSG_ERROR,
944 "default dirstripe error on '%s': %s",
951 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
952 int stripe_count, int stripe_pattern,
953 const char *pool_name)
955 const struct llapi_stripe_param param = {
956 .lsp_stripe_count = stripe_count,
957 .lsp_stripe_offset = stripe_offset,
958 .lsp_stripe_pattern = stripe_pattern,
959 .lsp_pool = (char *)pool_name
962 return llapi_dir_set_default_lmv(name, ¶m);
966 * Create a Lustre directory.
968 * \param name the name of the directory to be created
969 * \param mode permission of the file if it is created, see mode in open(2)
970 * \param param stripe pattern of the newly created directory
972 * \retval 0 on success
973 * \retval negative errno on failure
975 int llapi_dir_create(const char *name, mode_t mode,
976 const struct llapi_stripe_param *param)
978 struct lmv_user_md *lmu = NULL;
980 struct obd_ioctl_data data = { 0 };
983 char *dirpath = NULL;
984 char *namepath = NULL;
989 rc = verify_dir_param(name, param);
993 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
994 param->lsp_is_specific ?
995 LMV_USER_MAGIC_SPECIFIC :
998 lmu = calloc(1, lmu_size);
1002 dirpath = strdup(name);
1008 namepath = strdup(name);
1015 param2lmu(lmu, param);
1017 filename = basename(namepath);
1018 dir = dirname(dirpath);
1020 data.ioc_inlbuf1 = (char *)filename;
1021 data.ioc_inllen1 = strlen(filename) + 1;
1022 data.ioc_inlbuf2 = (char *)lmu;
1023 data.ioc_inllen2 = lmu_size;
1024 data.ioc_type = mode;
1025 if (param->lsp_is_create)
1026 /* borrow obdo1.o_flags to store this flag */
1027 data.ioc_obdo1.o_flags = OBD_FL_OBDMDEXISTS;
1028 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1030 llapi_error(LLAPI_MSG_ERROR, rc,
1031 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1036 fd = open(dir, O_DIRECTORY | O_RDONLY);
1039 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1043 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1044 char *errmsg = "stripe already set";
1047 if (errno != EEXIST && errno != EALREADY)
1048 errmsg = strerror(errno);
1050 llapi_err_noerrno(LLAPI_MSG_ERROR,
1051 "dirstripe error on '%s': %s", name, errmsg);
1062 * Create a foreign directory.
1064 * \param name the name of the directory to be created
1065 * \param mode permission of the file if it is created, see mode in open(2)
1066 * \param type foreign type to be set in LMV EA
1067 * \param flags foreign flags to be set in LMV EA
1068 * \param value foreign pattern to be set in LMV EA
1070 * \retval 0 on success
1071 * \retval negative errno on failure
1073 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1074 __u32 flags, const char *value)
1076 struct lmv_foreign_md *lfm = NULL;
1077 size_t lfm_size, len;
1078 struct obd_ioctl_data data = { 0 };
1081 char *dirpath = NULL;
1082 char *namepath = NULL;
1087 len = strlen(value);
1088 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1091 llapi_error(LLAPI_MSG_ERROR, rc,
1092 "invalid LOV EA length %zu (must be 0 < len < %zu)",
1093 len, XATTR_SIZE_MAX -
1094 offsetof(struct lmv_foreign_md, lfm_value));
1097 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1098 lfm = calloc(1, lfm_size);
1102 dirpath = strdup(name);
1108 namepath = strdup(name);
1115 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1116 lfm->lfm_length = len;
1117 lfm->lfm_type = type;
1118 lfm->lfm_flags = flags;
1119 memcpy(lfm->lfm_value, value, len);
1121 filename = basename(namepath);
1122 dir = dirname(dirpath);
1124 data.ioc_inlbuf1 = (char *)filename;
1125 data.ioc_inllen1 = strlen(filename) + 1;
1126 data.ioc_inlbuf2 = (char *)lfm;
1127 data.ioc_inllen2 = lfm_size;
1128 data.ioc_type = mode;
1129 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1131 llapi_error(LLAPI_MSG_ERROR, rc,
1132 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1137 fd = open(dir, O_DIRECTORY | O_RDONLY);
1140 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1144 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1145 char *errmsg = "stripe already set";
1148 if (errno != EEXIST && errno != EALREADY)
1149 errmsg = strerror(errno);
1151 llapi_err_noerrno(LLAPI_MSG_ERROR,
1152 "dirstripe error on '%s': %s", name, errmsg);
1162 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1163 int stripe_count, int stripe_pattern,
1164 const char *pool_name)
1166 const struct llapi_stripe_param param = {
1167 .lsp_stripe_count = stripe_count,
1168 .lsp_stripe_offset = stripe_offset,
1169 .lsp_stripe_pattern = stripe_pattern,
1170 .lsp_pool = (char *)pool_name
1173 return llapi_dir_create(name, mode, ¶m);
1177 * Get the list of pool members.
1178 * \param poolname string of format \<fsname\>.\<poolname\>
1179 * \param members caller-allocated array of char*
1180 * \param list_size size of the members array
1181 * \param buffer caller-allocated buffer for storing OST names
1182 * \param buffer_size size of the buffer
1184 * \return number of members retrieved for this pool
1185 * \retval -error failure
1187 int llapi_get_poolmembers(const char *poolname, char **members,
1188 int list_size, char *buffer, int buffer_size)
1190 char fsname[PATH_MAX];
1199 /* name is FSNAME.POOLNAME */
1200 if (strlen(poolname) >= sizeof(fsname))
1203 snprintf(fsname, sizeof(fsname), "%s", poolname);
1204 pool = strchr(fsname, '.');
1211 rc = poolpath(&pathname, fsname, NULL);
1213 llapi_error(LLAPI_MSG_ERROR, rc,
1214 "Lustre filesystem '%s' not found",
1219 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1220 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1221 cfs_free_param_data(&pathname);
1222 if (rc >= sizeof(buf))
1224 fd = fopen(buf, "r");
1227 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1232 while (fgets(buf, sizeof(buf), fd) != NULL) {
1233 if (nb_entries >= list_size) {
1237 buf[sizeof(buf) - 1] = '\0';
1239 tmp = strchr(buf, '\n');
1242 if (used + strlen(buf) + 1 > buffer_size) {
1247 strcpy(buffer + used, buf);
1248 members[nb_entries] = buffer + used;
1249 used += strlen(buf) + 1;
1259 * Get the list of pools in a filesystem.
1260 * \param name filesystem name or path
1261 * \param poollist caller-allocated array of char*
1262 * \param list_size size of the poollist array
1263 * \param buffer caller-allocated buffer for storing pool names
1264 * \param buffer_size size of the buffer
1266 * \return number of pools retrieved for this filesystem
1267 * \retval -error failure
1269 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1270 char *buffer, int buffer_size)
1276 struct dirent *pool;
1278 unsigned int nb_entries = 0;
1279 unsigned int used = 0;
1282 /* initialize output array */
1283 for (i = 0; i < list_size; i++)
1286 /* is name a pathname ? */
1287 ptr = strchr(name, '/');
1289 char fsname_buf[MAXNAMLEN];
1291 /* We will need fsname for printing later */
1292 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1296 ptr = strrchr(fsname_buf, '-');
1300 fsname = strdup(fsname_buf);
1304 /* name is FSNAME */
1305 fsname = strdup(name);
1310 rc = poolpath(&pathname, fsname, NULL);
1312 llapi_error(LLAPI_MSG_ERROR, rc,
1313 "Lustre filesystem '%s' not found", name);
1317 dir = opendir(pathname.gl_pathv[0]);
1320 llapi_error(LLAPI_MSG_ERROR, rc,
1321 "Could not open pool list for '%s'",
1328 pool = readdir(dir);
1334 /* ignore . and .. */
1335 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1338 /* check output bounds */
1339 if (nb_entries >= list_size) {
1341 goto free_dir_no_msg;
1344 /* +2 for '.' and final '\0' */
1345 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1348 goto free_dir_no_msg;
1351 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1352 poollist[nb_entries] = buffer + used;
1353 used += strlen(pool->d_name) + strlen(fsname) + 2;
1359 llapi_error(LLAPI_MSG_ERROR, rc,
1360 "Error reading pool list for '%s'", name);
1362 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1367 cfs_free_param_data(&pathname);
1370 return rc != 0 ? rc : nb_entries;
1373 /* wrapper for lfs.c and obd.c */
1374 int llapi_poollist(const char *name)
1376 int poolcount, rc, i;
1379 rc = llapi_get_poolbuf(name, &buf, &pools, &poolcount);
1383 for (i = 0; i < poolcount; i++)
1384 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", pools[i]);
1391 * Get buffer that holds uuids and the list of pools in a filesystem.
1393 * \param name filesystem name or path
1394 * \param buf bufffer that has to be freed if function returns 0
1395 * \param pools pointer to the list of pools in buffer
1396 * \param poolcount number of pools
1398 * \return 0 when found at least 1 pool, i.e. poolcount > 0
1399 * \retval -error failure
1401 int llapi_get_poolbuf(const char *name, char **buf,
1402 char ***pools, int *poolcount)
1405 * list of pool names (assume that pool count is smaller
1408 char **list, *buffer = NULL, *fsname = (char *)name;
1409 char *poolname = NULL, *tmp = NULL, data[16];
1410 enum param_filter type = FILTER_BY_PATH;
1411 int obdcount, bufsize, rc, nb;
1416 if (name[0] != '/') {
1417 fsname = strdup(name);
1421 poolname = strchr(fsname, '.');
1424 type = FILTER_BY_FS_NAME;
1427 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1428 data, sizeof(data));
1431 obdcount = atoi(data);
1434 * Allocate space for each fsname-OST0000_UUID, 1 per OST,
1435 * and also an array to store the pointers for all that
1439 bufsize = sizeof(struct obd_uuid) * obdcount;
1440 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1441 if (buffer == NULL) {
1445 list = (char **) (buffer + bufsize);
1448 /* name is a path or fsname */
1449 nb = llapi_get_poollist(name, list, obdcount,
1452 /* name is a pool name (<fsname>.<poolname>) */
1453 nb = llapi_get_poolmembers(name, list, obdcount,
1457 if (nb == -EOVERFLOW) {
1460 goto retry_get_pools;
1463 rc = (nb < 0 ? nb : 0);
1470 /* Don't free buffer, it will be used later */
1473 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1478 typedef int (semantic_func_t)(char *path, int p, int *d,
1479 void *data, struct dirent64 *de);
1481 #define OBD_NOT_FOUND (-1)
1483 static bool lmv_is_foreign(__u32 magic)
1485 return magic == LMV_MAGIC_FOREIGN;
1488 static void find_param_fini(struct find_param *param)
1490 if (param->fp_migrate)
1493 if (param->fp_obd_indexes) {
1494 free(param->fp_obd_indexes);
1495 param->fp_obd_indexes = NULL;
1498 if (param->fp_lmd) {
1499 free(param->fp_lmd);
1500 param->fp_lmd = NULL;
1503 if (param->fp_lmv_md) {
1504 free(param->fp_lmv_md);
1505 param->fp_lmv_md = NULL;
1509 static int common_param_init(struct find_param *param, char *path)
1511 int lum_size = get_mds_md_size(path);
1516 /* migrate has fp_lmv_md initialized outside */
1517 if (param->fp_migrate)
1520 if (lum_size < PATH_MAX + 1)
1521 lum_size = PATH_MAX + 1;
1523 param->fp_lum_size = lum_size;
1524 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1526 if (param->fp_lmd == NULL) {
1527 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1528 "error: allocate %zu bytes for layout failed",
1529 sizeof(lstat_t) + param->fp_lum_size);
1533 param->fp_lmv_stripe_count = 256;
1534 param->fp_lmv_md = calloc(1,
1535 lmv_user_md_size(param->fp_lmv_stripe_count,
1536 LMV_USER_MAGIC_SPECIFIC));
1537 if (param->fp_lmv_md == NULL) {
1538 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1539 "error: allocation of %d bytes for ioctl",
1540 lmv_user_md_size(param->fp_lmv_stripe_count,
1541 LMV_USER_MAGIC_SPECIFIC));
1542 find_param_fini(param);
1546 param->fp_got_uuids = 0;
1547 param->fp_obd_indexes = NULL;
1548 param->fp_obd_index = OBD_NOT_FOUND;
1549 param->fp_mdt_index = OBD_NOT_FOUND;
1553 static int cb_common_fini(char *path, int p, int *dp, void *data,
1554 struct dirent64 *de)
1556 struct find_param *param = data;
1562 /* set errno upon failure */
1563 static int open_parent(const char *path)
1569 path_copy = strdup(path);
1570 if (path_copy == NULL)
1573 parent_path = dirname(path_copy);
1574 parent = open(parent_path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1580 static int cb_get_dirstripe(char *path, int *d, struct find_param *param)
1583 bool did_nofollow = false;
1588 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1589 if (param->fp_get_default_lmv) {
1593 /* open() may not fetch LOOKUP lock, statx() to ensure dir depth
1596 statx(*d, "", AT_EMPTY_PATH, STATX_MODE, &stx);
1602 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1604 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1607 param->fp_lmv_md->lum_type = LMV_TYPE_RAW;
1609 ret = ioctl(*d, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1611 /* if ENOTTY likely to be a fake symlink, so try again after
1612 * new open() with O_NOFOLLOW, but only once to prevent any
1613 * loop like for the path of a file/dir not on Lustre !!
1615 if (ret < 0 && errno == ENOTTY && !did_nofollow) {
1618 did_nofollow = true;
1619 fd = open(path, O_RDONLY | O_NOFOLLOW);
1621 /* restore original errno */
1626 /* close original fd and set new */
1629 ret2 = ioctl(fd, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1630 if (ret2 < 0 && errno != E2BIG) {
1631 /* restore original errno */
1635 /* LMV is ok or need to handle E2BIG case now */
1639 if (errno == E2BIG && ret != 0) {
1643 /* if foreign LMV case, fake stripes number */
1644 if (lmv_is_foreign(param->fp_lmv_md->lum_magic)) {
1645 struct lmv_foreign_md *lfm;
1647 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1648 if (lfm->lfm_length < XATTR_SIZE_MAX -
1649 offsetof(typeof(*lfm), lfm_value)) {
1650 uint32_t size = lfm->lfm_length +
1651 offsetof(typeof(*lfm), lfm_value);
1653 stripe_count = lmv_foreign_to_md_stripes(size);
1655 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1656 "error: invalid %d foreign size returned from ioctl",
1661 stripe_count = param->fp_lmv_md->lum_stripe_count;
1663 if (stripe_count <= param->fp_lmv_stripe_count)
1666 free(param->fp_lmv_md);
1667 param->fp_lmv_stripe_count = stripe_count;
1668 lmv_size = lmv_user_md_size(stripe_count,
1669 LMV_USER_MAGIC_SPECIFIC);
1670 param->fp_lmv_md = malloc(lmv_size);
1671 if (param->fp_lmv_md == NULL) {
1672 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1673 "error: allocation of %d bytes for ioctl",
1674 lmv_user_md_size(param->fp_lmv_stripe_count,
1675 LMV_USER_MAGIC_SPECIFIC));
1684 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1687 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
1688 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
1689 lmd_v2->lmd_stx.stx_uid = st->st_uid;
1690 lmd_v2->lmd_stx.stx_gid = st->st_gid;
1691 lmd_v2->lmd_stx.stx_mode = st->st_mode;
1692 lmd_v2->lmd_stx.stx_ino = st->st_ino;
1693 lmd_v2->lmd_stx.stx_size = st->st_size;
1694 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
1695 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
1696 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
1697 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
1698 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
1699 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
1700 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
1701 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
1702 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
1704 lmd_v2->lmd_flags = 0;
1706 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1708 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
1709 if (lmd_v2->lmd_stx.stx_size)
1710 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
1711 if (lmd_v2->lmd_stx.stx_blocks)
1712 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
1714 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1715 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
1716 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
1721 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
1723 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
1724 struct lov_user_mds_data *lmd_v2 = lmdbuf;
1728 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
1732 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
1735 st = lmd_v1->lmd_st;
1736 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
1737 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
1738 convert_lmd_statx(lmd_v2, &st, false);
1739 lmd_v2->lmd_lmmsize = 0;
1740 lmd_v2->lmd_padding = 0;
1745 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
1746 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1748 struct lov_user_mds_data *lmd = lmdbuf;
1749 static bool use_old_ioctl;
1753 if (parent_fd < 0 && dir_fd < 0)
1755 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1760 * LL_IOC_MDC_GETINFO operates on the current directory inode
1761 * and returns struct lov_user_mds_data, while
1762 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
1764 if (type == GET_LMD_INFO)
1765 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_V1 :
1766 LL_IOC_MDC_GETINFO_V2;
1768 cmd = LL_IOC_LOV_GETSTRIPE;
1771 ret = ioctl(dir_fd, cmd, lmdbuf);
1772 if (ret < 0 && errno == ENOTTY &&
1773 cmd == LL_IOC_MDC_GETINFO_V2) {
1774 cmd = LL_IOC_MDC_GETINFO_V1;
1775 use_old_ioctl = true;
1779 if (cmd == LL_IOC_MDC_GETINFO_V1 && !ret)
1780 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
1782 if (ret < 0 && errno == ENOTTY && type == GET_LMD_STRIPE) {
1785 /* retry ioctl() after new open() with O_NOFOLLOW
1786 * just in case it could be a fake symlink
1787 * need using a new open() as dir_fd is being closed
1791 dir_fd2 = open(path, O_RDONLY | O_NDELAY | O_NOFOLLOW);
1793 /* return original error */
1796 ret = ioctl(dir_fd2, cmd, lmdbuf);
1797 /* pass new errno or success back to caller */
1803 } else if (parent_fd >= 0) {
1804 const char *fname = strrchr(path, '/');
1807 * IOC_MDC_GETFILEINFO takes as input the filename (relative to
1808 * the parent directory) and returns struct lov_user_mds_data,
1809 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
1811 * This avoids opening, locking, and closing each file on the
1812 * client if that is not needed. Multiple of these ioctl() can
1813 * be done on the parent dir with a single open for all
1814 * files in that directory, and it also doesn't pollute the
1815 * client dcache with millions of dentries when traversing
1816 * a large filesystem.
1818 fname = (fname == NULL ? path : fname + 1);
1820 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
1823 else if (ret >= lmdlen || ret++ == 0)
1826 if (type == GET_LMD_INFO)
1827 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_V1 :
1828 IOC_MDC_GETFILEINFO_V2;
1830 cmd = IOC_MDC_GETFILESTRIPE;
1833 ret = ioctl(parent_fd, cmd, lmdbuf);
1834 if (ret < 0 && errno == ENOTTY &&
1835 cmd == IOC_MDC_GETFILEINFO_V2) {
1836 cmd = IOC_MDC_GETFILEINFO_V1;
1837 use_old_ioctl = true;
1838 goto retry_getfileinfo;
1841 if (cmd == IOC_MDC_GETFILEINFO_V1 && !ret)
1842 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
1846 if (ret && type == GET_LMD_INFO) {
1847 if (errno == ENOTTY) {
1851 * ioctl is not supported, it is not a lustre fs.
1852 * Do the regular lstat(2) instead.
1854 ret = lstat_f(path, &st);
1857 llapi_error(LLAPI_MSG_ERROR, ret,
1858 "error: %s: lstat failed for %s",
1862 convert_lmd_statx(lmd, &st, true);
1864 * It may be wrong to set use_old_ioctl with true as
1865 * the file is not a lustre fs. So reset it with false
1868 use_old_ioctl = false;
1869 } else if (errno == ENOENT) {
1871 llapi_error(LLAPI_MSG_WARN, ret,
1872 "warning: %s does not exist", path);
1873 } else if (errno != EISDIR && errno != ENODATA) {
1875 llapi_error(LLAPI_MSG_ERROR, ret,
1876 "%s ioctl failed for %s.",
1877 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
1878 "IOC_MDC_GETFILEINFO", path);
1885 static int llapi_semantic_traverse(char *path, int size, int parent,
1886 semantic_func_t sem_init,
1887 semantic_func_t sem_fini, void *data,
1888 struct dirent64 *de)
1890 struct find_param *param = (struct find_param *)data;
1891 struct dirent64 *dent;
1892 int len, ret, d, p = -1;
1898 d = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1899 /* if an invalid fake dir symlink, opendir() will return EINVAL
1900 * instead of ENOTDIR. If a valid but dangling faked or real file/dir
1901 * symlink ENOENT will be returned. For a valid/resolved fake or real
1902 * file symlink ENOTDIR will be returned as for a regular file.
1903 * opendir() will be successful for a valid and resolved fake or real
1904 * dir simlink or a regular dir.
1906 if (d == -1 && errno != ENOTDIR && errno != EINVAL && errno != ENOENT) {
1908 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1911 } else if (d == -1) {
1912 if (errno == ENOENT || errno == EINVAL) {
1913 int old_errno = errno;
1915 /* try to open with O_NOFOLLOW this will help
1916 * differentiate fake vs real symlinks
1917 * it is ok to not use O_DIRECTORY with O_RDONLY
1918 * and it will prevent the need to deal with ENOTDIR
1919 * error, instead of ELOOP, being returned by recent
1920 * kernels for real symlinks
1922 d = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
1923 /* if a dangling real symlink should return ELOOP, or
1924 * again ENOENT if really non-existing path, or E...??
1925 * So return original error. If success or ENOTDIR, path
1926 * is likely to be a fake dir/file symlink, so continue
1936 if (parent == -1 && d == -1) {
1937 /* Open the parent dir. */
1938 p = open_parent(path);
1944 } else { /* d != -1 */
1947 /* try to reopen dir with O_NOFOLLOW just in case of a foreign
1950 d2 = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
1955 /* continue with d */
1961 ret = sem_init(path, (parent != -1) ? parent : p, &d, data, de);
1971 /* ENOTDIR if fake symlink, do not consider it as an error */
1972 if (errno != ENOTDIR)
1973 llapi_error(LLAPI_MSG_ERROR, errno,
1974 "fdopendir() failed");
1981 while ((dent = readdir64(dir)) != NULL) {
1984 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1988 if ((len + dent->d_reclen + 2) > size) {
1989 llapi_err_noerrno(LLAPI_MSG_ERROR,
1990 "error: %s: string buffer too small for %s",
1995 strcat(path, dent->d_name);
1997 if (dent->d_type == DT_UNKNOWN) {
1998 struct lov_user_mds_data *lmd = param->fp_lmd;
2000 rc = get_lmd_info_fd(path, d, -1, param->fp_lmd,
2001 param->fp_lum_size, GET_LMD_INFO);
2003 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2011 switch (dent->d_type) {
2013 llapi_err_noerrno(LLAPI_MSG_ERROR,
2014 "error: %s: '%s' is UNKNOWN type %d",
2015 __func__, dent->d_name, dent->d_type);
2018 rc = llapi_semantic_traverse(path, size, d, sem_init,
2019 sem_fini, data, dent);
2020 if (rc != 0 && ret == 0)
2022 if (rc < 0 && rc != -EALREADY &&
2023 param->fp_stop_on_error)
2029 rc = sem_init(path, d, NULL, data, dent);
2030 if (rc < 0 && ret == 0) {
2032 if (rc && rc != -EALREADY &&
2033 param->fp_stop_on_error)
2038 if (sem_fini && rc == 0)
2039 sem_fini(path, d, NULL, data, dent);
2047 sem_fini(path, parent, &d, data, de);
2060 static int param_callback(char *path, semantic_func_t sem_init,
2061 semantic_func_t sem_fini, struct find_param *param)
2063 int ret, len = strlen(path);
2066 if (len > PATH_MAX) {
2068 llapi_error(LLAPI_MSG_ERROR, ret,
2069 "Path name '%s' is too long", path);
2073 buf = (char *)malloc(2 * PATH_MAX);
2077 snprintf(buf, PATH_MAX + 1, "%s", path);
2078 ret = common_param_init(param, buf);
2082 param->fp_depth = 0;
2084 ret = llapi_semantic_traverse(buf, 2 * PATH_MAX, -1, sem_init,
2085 sem_fini, param, NULL);
2087 find_param_fini(param);
2089 return ret < 0 ? ret : 0;
2092 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2096 rc = llapi_ioctl(fd, OBD_IOC_GETDTNAME, lov_name);
2099 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2105 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2109 rc = llapi_ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2112 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2118 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2122 /* do not follow faked symlinks */
2123 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
2125 /* real symlink should have failed with ELOOP so retry without
2126 * O_NOFOLLOW just in case
2128 fd = open(path, O_RDONLY | O_NONBLOCK);
2131 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'",
2137 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2143 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2147 fd = open(path, O_RDONLY | O_NONBLOCK);
2150 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2154 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2166 * If uuidp is NULL, return the number of available obd uuids.
2167 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2168 * there are more OSTs than allocated to uuidp, then an error is returned with
2169 * the ost_count set to number of available obd uuids.
2171 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp, int *indices,
2172 int *ost_count, enum tgt_type type)
2174 char buf[PATH_MAX], format[32];
2176 struct obd_uuid name;
2180 /* Get the lov name */
2181 if (type == LOV_TYPE)
2182 rc = llapi_file_fget_lov_uuid(fd, &name);
2184 rc = llapi_file_fget_lmv_uuid(fd, &name);
2188 /* Now get the ost uuids */
2189 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2190 FILTER_BY_EXACT, "target_obd", ¶m);
2194 fp = fopen(param.gl_pathv[0], "r");
2197 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2202 snprintf(format, sizeof(format),
2203 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2204 for (i = 0; fgets(buf, sizeof(buf), fp); i++) {
2207 if (sscanf(buf, format, &index, name.uuid) < 2)
2210 if (i < *ost_count) {
2213 if (indices != NULL)
2219 if (uuidp && (i > *ost_count))
2224 cfs_free_param_data(¶m);
2228 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2230 return llapi_get_target_uuids(fd, uuidp, NULL, ost_count, LOV_TYPE);
2233 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2238 root = open(mnt, O_RDONLY | O_DIRECTORY);
2241 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2246 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2255 * Check if user specified value matches a real uuid. Ignore _UUID,
2256 * -osc-4ba41334, other trailing gunk in comparison.
2257 * @param real_uuid ends in "_UUID"
2258 * @param search_uuid may or may not end in "_UUID"
2260 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2262 int cmplen = strlen(real_uuid);
2263 int searchlen = strlen(search_uuid);
2265 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2267 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2271 * The UUIDs may legitimately be different lengths, if
2272 * the system was upgraded from an older version.
2274 if (cmplen != searchlen)
2277 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2281 * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2282 * returned in param->fp_obd_index
2284 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2286 struct obd_uuid obd_uuid;
2293 if (param->fp_got_uuids)
2296 /* Get the lov/lmv name */
2297 if (param->fp_get_lmv)
2298 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2300 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2302 if (rc != -ENOTTY) {
2303 llapi_error(LLAPI_MSG_ERROR, rc,
2304 "error: can't get %s name: %s",
2305 param->fp_get_lmv ? "lmv" : "lov",
2313 param->fp_got_uuids = 1;
2315 /* Now get the ost uuids */
2316 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2317 obd_uuid.uuid, FILTER_BY_EXACT,
2318 "target_obd", ¶m_data);
2322 fp = fopen(param_data.gl_pathv[0], "r");
2325 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2326 param_data.gl_pathv[0]);
2330 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2331 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2332 param->fp_get_lmv ? "MDTS" : "OBDS");
2334 snprintf(format, sizeof(format),
2335 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2336 while (fgets(buf, sizeof(buf), fp) != NULL) {
2339 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2342 if (param->fp_obd_uuid) {
2343 if (llapi_uuid_match(obd_uuid.uuid,
2344 param->fp_obd_uuid->uuid)) {
2345 param->fp_obd_index = index;
2348 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2349 /* Print everything */
2350 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2353 param->fp_obds_printed = 1;
2357 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2358 llapi_err_noerrno(LLAPI_MSG_ERROR,
2359 "error: %s: unknown obduuid: %s",
2360 __func__, param->fp_obd_uuid->uuid);
2364 cfs_free_param_data(¶m_data);
2369 * In this case, param->fp_obd_uuid will be an array of obduuids and
2370 * obd index for all these obduuids will be returned in
2371 * param->fp_obd_indexes
2373 static int setup_indexes(int d, char *path, struct obd_uuid *obduuids,
2374 int num_obds, int **obdindexes, int *obdindex,
2377 int ret, obdcount, maxidx, obd_valid = 0, obdnum;
2378 int *indices = NULL;
2379 struct obd_uuid *uuids = NULL;
2384 if (type == LOV_TYPE)
2385 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2387 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2391 obdcount = atoi(buf);
2392 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2395 indices = malloc(obdcount * sizeof(int));
2396 if (indices == NULL) {
2403 ret = llapi_get_target_uuids(d, uuids, indices, &obdcount, type);
2405 if (ret == -EOVERFLOW) {
2406 struct obd_uuid *uuids_temp;
2407 int *indices_temp = NULL;
2409 uuids_temp = realloc(uuids, obdcount *
2410 sizeof(struct obd_uuid));
2413 indices_temp = realloc(indices, obdcount * sizeof(int));
2415 indices = indices_temp;
2416 if (uuids_temp && indices_temp)
2417 goto retry_get_uuids;
2421 llapi_error(LLAPI_MSG_ERROR, ret, "cannot fetch %u OST UUIDs",
2426 indexes = malloc(num_obds * sizeof(*obdindex));
2427 if (indexes == NULL) {
2432 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2435 /* The user may have specified a simple index */
2436 i = strtol(obduuids[obdnum].uuid, &end, 0);
2437 if (end && *end == '\0' && i < maxidx) {
2438 indexes[obdnum] = i;
2441 for (i = 0; i < obdcount; i++) {
2442 if (llapi_uuid_match(uuids[i].uuid,
2443 obduuids[obdnum].uuid)) {
2444 indexes[obdnum] = indices[i];
2452 indexes[obdnum] = OBD_NOT_FOUND;
2453 llapi_err_noerrno(LLAPI_MSG_ERROR,
2454 "invalid obduuid '%s'",
2455 obduuids[obdnum].uuid);
2461 *obdindex = OBD_NOT_FOUND;
2463 *obdindex = obd_valid;
2465 *obdindexes = indexes;
2476 static int setup_target_indexes(int d, char *path, struct find_param *param)
2480 if (param->fp_mdt_uuid) {
2481 ret = setup_indexes(d, path, param->fp_mdt_uuid,
2483 ¶m->fp_mdt_indexes,
2484 ¶m->fp_mdt_index, LMV_TYPE);
2489 if (param->fp_obd_uuid) {
2490 ret = setup_indexes(d, path, param->fp_obd_uuid,
2492 ¶m->fp_obd_indexes,
2493 ¶m->fp_obd_index, LOV_TYPE);
2498 param->fp_got_uuids = 1;
2503 int llapi_ostlist(char *path, struct find_param *param)
2508 fd = open(path, O_RDONLY | O_DIRECTORY);
2512 ret = setup_obd_uuid(fd, path, param);
2519 * Tries to determine the default stripe attributes for a given filesystem. The
2520 * filesystem to check should be specified by fsname, or will be determined
2523 static int sattr_get_defaults(const char *const fsname,
2524 unsigned int *scount,
2525 unsigned int *ssize,
2526 unsigned int *soffset)
2532 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2533 "stripecount", val, sizeof(val));
2536 *scount = atoi(val);
2540 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2541 "stripesize", val, sizeof(val));
2548 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2549 "stripeoffset", val, sizeof(val));
2552 *soffset = atoi(val);
2559 * Tries to gather the default stripe attributes for a given filesystem. If
2560 * the attributes can be determined, they are cached for easy retreival the
2561 * next time they are needed. Only a single filesystem's attributes are
2564 int sattr_cache_get_defaults(const char *const fsname,
2565 const char *const pathname, unsigned int *scount,
2566 unsigned int *ssize, unsigned int *soffset)
2569 char fsname[PATH_MAX + 1];
2570 unsigned int stripecount;
2571 unsigned int stripesize;
2572 unsigned int stripeoffset;
2578 char fsname_buf[PATH_MAX + 1];
2579 unsigned int tmp[3];
2581 if (fsname == NULL) {
2582 rc = llapi_search_fsname(pathname, fsname_buf);
2586 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2589 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2591 * Ensure all 3 sattrs (count, size, and offset) are
2592 * successfully retrieved and stored in tmp before writing to
2595 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2599 cache.stripecount = tmp[0];
2600 cache.stripesize = tmp[1];
2601 cache.stripeoffset = tmp[2];
2602 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2606 *scount = cache.stripecount;
2608 *ssize = cache.stripesize;
2610 *soffset = cache.stripeoffset;
2615 static char *layout2name(__u32 layout_pattern)
2617 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2619 else if (layout_pattern & LOV_PATTERN_FOREIGN)
2621 else if (layout_pattern == LOV_PATTERN_MDT)
2623 else if (layout_pattern == LOV_PATTERN_RAID0)
2625 else if (layout_pattern ==
2626 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2627 return "raid0,overstriped";
2632 enum lov_dump_flags {
2633 LDF_IS_DIR = 0x0001,
2634 LDF_IS_RAW = 0x0002,
2635 LDF_INDENT = 0x0004,
2636 LDF_SKIP_OBJS = 0x0008,
2638 LDF_EXTENSION = 0x0020,
2639 LDF_HEX_IDX = 0x0040,
2642 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2643 struct lov_user_ost_data_v1 *objects,
2644 enum llapi_layout_verbose verbose,
2645 int depth, char *pool_name,
2646 enum lov_dump_flags flags)
2648 bool is_dir = flags & LDF_IS_DIR;
2649 bool is_raw = flags & LDF_IS_RAW;
2650 bool indent = flags & LDF_INDENT;
2651 bool yaml = flags & LDF_YAML;
2652 bool skip_objs = flags & LDF_SKIP_OBJS;
2653 bool extension = flags & LDF_EXTENSION;
2654 char *prefix = is_dir ? "" : "lmm_";
2655 char *separator = "";
2656 char *space = indent ? " " : "";
2657 char *fmt_idx = flags & LDF_HEX_IDX ? "%#x" : "%d";
2660 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2661 lmm_oi_set_seq(&lum->lmm_oi, 0);
2662 if (!indent && (verbose & VERBOSE_DETAIL))
2663 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2666 if (!yaml && !indent && depth && path &&
2667 ((verbose != VERBOSE_OBJID) || !is_dir))
2668 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2670 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2671 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2672 space, prefix, lum->lmm_magic);
2673 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2675 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2676 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2678 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2681 if (verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) {
2686 if (verbose & ~VERBOSE_DFID)
2687 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2691 struct lu_fid dir_fid;
2693 rc = llapi_path2fid(path, &dir_fid);
2695 llapi_error(LLAPI_MSG_ERROR, rc,
2696 "Cannot determine directory fid.");
2698 seq = dir_fid.f_seq;
2699 oid = dir_fid.f_oid;
2700 ver = dir_fid.f_ver;
2703 * This needs a bit of hand-holding since old 1.x
2704 * lmm_oi have { oi.oi_id = mds_inum, oi.oi_seq = 0 }
2705 * and 2.x lmm_oi have { oi.oi_id = mds_oid,
2706 * oi.oi_seq = mds_seq } instead of a real FID.
2707 * Ideally the 2.x code would have stored this like a
2708 * FID with { oi_id = mds_seq, oi_seq = mds_oid } so
2709 * the ostid union lu_fid { f_seq = mds_seq,
2710 * f_oid = mds_oid } worked properly (especially since
2711 * IGIF FIDs use mds_inum as the FID SEQ), but
2712 * unfortunately that didn't happen.
2714 * Print it to look like an IGIF FID, even though the
2715 * fields are reversed on disk, so that it makes sense
2718 * Don't use ostid_id() and ostid_seq(), since they
2719 * assume the oi_fid fields are in the right order.
2720 * This is why there are separate lmm_oi_seq() and
2721 * lmm_oi_id() routines for this.
2723 * For newer layout types hopefully this will be a
2726 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2727 lmm_oi_id(&lum->lmm_oi) :
2728 lmm_oi_seq(&lum->lmm_oi);
2729 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2730 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2731 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2735 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2736 (unsigned long long)seq, oid, ver);
2738 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2739 (unsigned long long)seq, oid, ver);
2742 if (verbose & VERBOSE_STRIPE_COUNT) {
2743 if (verbose & ~VERBOSE_STRIPE_COUNT)
2744 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2747 if (!is_raw && lum->lmm_stripe_count == 0 &&
2748 !(lov_pattern(lum->lmm_pattern) & LOV_PATTERN_MDT)){
2749 unsigned int scount;
2751 rc = sattr_cache_get_defaults(NULL, path,
2755 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2758 llapi_error(LLAPI_MSG_ERROR, rc,
2759 "Cannot determine default stripe count.");
2761 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2763 (__s16)lum->lmm_stripe_count);
2766 llapi_printf(LLAPI_MSG_NORMAL, "%i",
2768 (__s16)lum->lmm_stripe_count);
2770 if (!yaml && is_dir)
2776 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
2777 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
2778 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2779 if (verbose & ~VERBOSE_EXT_SIZE && extension)
2780 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
2782 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
2783 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2785 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2788 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2791 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2793 llapi_error(LLAPI_MSG_ERROR, rc,
2794 "Cannot determine default stripe size.");
2796 /* Extension size is in KiB */
2797 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2799 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
2800 (unsigned long long)lum->lmm_stripe_size);
2802 if (!yaml && is_dir)
2808 if ((verbose & VERBOSE_PATTERN)) {
2809 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2810 if (verbose & ~VERBOSE_PATTERN)
2811 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2813 if (lov_pattern_supported(lum->lmm_pattern))
2814 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2815 layout2name(lum->lmm_pattern));
2817 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2818 separator = (!yaml && is_dir) ? " " : "\n";
2821 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2822 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2823 if (verbose & ~VERBOSE_GENERATION)
2824 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2826 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2827 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2831 if (verbose & VERBOSE_STRIPE_OFFSET) {
2832 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2833 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2834 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2836 if (is_dir || skip_objs)
2837 if (lum->lmm_stripe_offset ==
2838 (typeof(lum->lmm_stripe_offset))(-1))
2839 llapi_printf(LLAPI_MSG_NORMAL, "-1");
2841 llapi_printf(LLAPI_MSG_NORMAL, fmt_idx,
2842 lum->lmm_stripe_offset);
2843 else if (lov_pattern(lum->lmm_pattern) & LOV_PATTERN_MDT)
2844 llapi_printf(LLAPI_MSG_NORMAL, "0");
2846 llapi_printf(LLAPI_MSG_NORMAL, fmt_idx,
2847 objects[0].l_ost_idx);
2848 if (!yaml && is_dir)
2854 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0') &&
2855 (!lov_pool_is_ignored(pool_name) || is_raw)) {
2856 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2857 if (verbose & ~VERBOSE_POOL)
2858 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2860 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2861 if (!yaml && is_dir)
2867 if (strlen(separator) != 0)
2868 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2871 static void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2872 struct lov_user_ost_data_v1 *objects,
2873 char *path, int obdindex, int depth,
2874 enum llapi_layout_verbose verbose,
2875 enum lov_dump_flags flags)
2877 bool is_dir = flags & LDF_IS_DIR;
2878 bool indent = flags & LDF_INDENT;
2879 bool skip_objs = flags & LDF_SKIP_OBJS;
2880 bool yaml = flags & LDF_YAML;
2881 bool hex = flags & LDF_HEX_IDX;
2882 bool obdstripe = obdindex == OBD_NOT_FOUND;
2885 if (!obdstripe && !skip_objs) {
2886 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2887 if (obdindex == objects[i].l_ost_idx) {
2897 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2900 if (!skip_objs && (verbose & VERBOSE_OBJID) &&
2901 ((!is_dir && !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2902 lov_pattern(lum->lmm_pattern) & LOV_PATTERN_MDT)) ||
2903 (is_dir && (lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC)))) {
2904 char *space = " - ";
2907 llapi_printf(LLAPI_MSG_NORMAL,
2908 "%6slmm_objects:\n", " ");
2910 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2912 llapi_printf(LLAPI_MSG_NORMAL,
2913 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2915 for (i = 0; i < lum->lmm_stripe_count; i++) {
2916 int idx = objects[i].l_ost_idx;
2917 long long oid = ostid_id(&objects[i].l_ost_oi);
2918 long long gr = ostid_seq(&objects[i].l_ost_oi);
2920 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2924 struct lu_fid fid = { 0 };
2925 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2926 llapi_printf(LLAPI_MSG_NORMAL,
2927 hex ? "%sl_ost_idx: %#x\n"
2928 : "%sl_ost_idx: %d\n",
2930 llapi_printf(LLAPI_MSG_NORMAL,
2931 "%8sl_fid: "DFID_NOBRACE"\n",
2933 } else if (indent) {
2934 struct lu_fid fid = { 0 };
2936 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2937 llapi_printf(LLAPI_MSG_NORMAL, hex ?
2938 "%s%3d: { l_ost_idx: %#5x, l_fid: "DFID" }\n" :
2939 "%s%3d: { l_ost_idx: %3d, l_fid: "DFID" }\n",
2940 space, i, idx, PFID(&fid));
2941 } else if (is_dir) {
2942 llapi_printf(LLAPI_MSG_NORMAL,
2943 "\t%6u\t%14s\t%13s\t%14s\n", idx, "N/A",
2946 char fmt[48] = { 0 };
2948 sprintf(fmt, "%s%s%s\n",
2949 hex ? "\t%#6x\t%14llu\t%#13llx\t"
2950 : "\t%6u\t%14llu\t%#13llx\t",
2951 (fid_seq_is_rsvd(gr) ||
2952 fid_seq_is_mdt0(gr)) ?
2953 "%14llu" : "%#14llx", "%s");
2954 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2956 obdindex == idx ? " *" : "");
2960 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2963 static void hsm_flags2str(__u32 hsm_flags)
2969 llapi_printf(LLAPI_MSG_NORMAL, "0");
2972 for (i = 0; i < ARRAY_SIZE(hsm_flags_table); i++) {
2973 if (hsm_flags & hsm_flags_table[i].hfn_flag) {
2975 llapi_printf(LLAPI_MSG_NORMAL, ",");
2976 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2977 hsm_flags_table[i].hfn_name);
2983 llapi_printf(LLAPI_MSG_NORMAL, ",");
2984 llapi_printf(LLAPI_MSG_NORMAL, "%#x", hsm_flags);
2988 static uint32_t check_foreign_type(uint32_t foreign_type)
2992 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
2993 if (lu_foreign_types[i].lft_name == NULL)
2995 if (foreign_type == lu_foreign_types[i].lft_type)
2999 return LU_FOREIGN_TYPE_UNKNOWN;
3002 void lov_dump_hsm_lmm(void *lum, char *path, int depth,
3003 enum llapi_layout_verbose verbose,
3004 enum lov_dump_flags flags)
3006 struct lov_hsm_md *lhm = lum;
3007 bool indent = flags & LDF_INDENT;
3008 bool is_dir = flags & LDF_IS_DIR;
3009 char *space = indent ? " " : "";
3012 uint32_t type = check_foreign_type(lhm->lhm_type);
3014 llapi_printf(LLAPI_MSG_NORMAL, "%slhm_magic: 0x%08X\n",
3015 space, lhm->lhm_magic);
3016 llapi_printf(LLAPI_MSG_NORMAL, "%slhm_pattern: hsm\n",
3018 llapi_printf(LLAPI_MSG_NORMAL, "%slhm_length: %u\n",
3019 space, lhm->lhm_length);
3020 llapi_printf(LLAPI_MSG_NORMAL, "%slhm_type: 0x%08X",
3021 space, lhm->lhm_type);
3022 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3023 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3024 lu_foreign_types[type].lft_name);
3026 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3028 llapi_printf(LLAPI_MSG_NORMAL, "%slhm_flags: ", space);
3029 hsm_flags2str(lhm->lhm_flags);
3030 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3032 if (!lov_hsm_type_supported(lhm->lhm_type))
3035 llapi_printf(LLAPI_MSG_NORMAL, "%slhm_archive_id: %llu\n",
3036 space, (unsigned long long)lhm->lhm_archive_id);
3037 llapi_printf(LLAPI_MSG_NORMAL, "%slhm_archive_ver: %llu\n",
3038 space, (unsigned long long)lhm->lhm_archive_ver);
3039 llapi_printf(LLAPI_MSG_NORMAL, "%slhm_archive_uuid: '%.*s'\n",
3040 space, UUID_MAX, lhm->lhm_archive_uuid);
3044 static void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3045 char *path, int obdindex, int depth,
3046 enum llapi_layout_verbose verbose,
3047 enum lov_dump_flags flags)
3049 struct lmv_user_mds_data *objects = lum->lum_objects;
3050 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3051 char *separator = "";
3052 bool yaml = flags & LDF_YAML;
3053 bool hex = flags & LDF_HEX_IDX;
3054 bool obdstripe = false;
3057 if (obdindex != OBD_NOT_FOUND) {
3058 if (lum->lum_stripe_count == 0) {
3059 if (obdindex == lum->lum_stripe_offset)
3062 for (i = 0; i < lum->lum_stripe_count; i++) {
3063 if (obdindex == objects[i].lum_mds) {
3064 llapi_printf(LLAPI_MSG_NORMAL,
3079 /* show all information default */
3081 if (lum->lum_magic == LMV_USER_MAGIC)
3082 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3083 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3085 verbose = VERBOSE_OBJID;
3088 if (depth && path && ((verbose != VERBOSE_OBJID)))
3089 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3091 if (verbose & VERBOSE_STRIPE_COUNT) {
3092 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3093 if (verbose & ~VERBOSE_STRIPE_COUNT)
3094 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3095 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3096 (int)lum->lum_stripe_count);
3097 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3103 if (verbose & VERBOSE_STRIPE_OFFSET) {
3104 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3105 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3106 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3107 llapi_printf(LLAPI_MSG_NORMAL, hex ? "%#x" : "%d",
3108 (int)lum->lum_stripe_offset);
3109 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3115 if (verbose & VERBOSE_HASH_TYPE) {
3116 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3117 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3119 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3120 if (verbose & ~VERBOSE_HASH_TYPE)
3121 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3122 if (type < LMV_HASH_TYPE_MAX)
3123 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3124 mdt_hash_name[type]);
3126 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3128 if (flags & LMV_HASH_FLAG_OVERSTRIPED)
3129 llapi_printf(LLAPI_MSG_NORMAL, ",overstriped");
3130 if (flags & LMV_HASH_FLAG_MIGRATION)
3131 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3132 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3133 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3134 if (flags & LMV_HASH_FLAG_LOST_LMV)
3135 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3136 if (flags & LMV_HASH_FLAG_FIXED)
3137 llapi_printf(LLAPI_MSG_NORMAL, ",fixed");
3138 if (flags & ~LMV_HASH_FLAG_KNOWN)
3139 llapi_printf(LLAPI_MSG_NORMAL, ",unknown_%04x",
3140 flags & ~LMV_HASH_FLAG_KNOWN);
3142 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3148 if ((verbose & VERBOSE_INHERIT) && lum->lum_magic == LMV_USER_MAGIC) {
3149 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3150 if (verbose & ~VERBOSE_INHERIT)
3151 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit: ");
3152 if (lum->lum_max_inherit == LMV_INHERIT_UNLIMITED)
3153 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3154 else if (lum->lum_max_inherit == LMV_INHERIT_NONE)
3155 llapi_printf(LLAPI_MSG_NORMAL, "0");
3157 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3158 lum->lum_max_inherit);
3159 if (verbose & VERBOSE_INHERIT && !yaml)
3165 if ((verbose & VERBOSE_INHERIT_RR) &&
3166 lum->lum_magic == LMV_USER_MAGIC) {
3167 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3168 if (verbose & ~VERBOSE_INHERIT_RR)
3169 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit_rr: ");
3170 if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_UNLIMITED)
3171 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3172 else if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_NONE)
3173 llapi_printf(LLAPI_MSG_NORMAL, "0");
3175 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3176 lum->lum_max_inherit_rr);
3177 if (verbose & VERBOSE_INHERIT_RR && !yaml)
3185 if ((verbose & VERBOSE_OBJID) && lum->lum_magic != LMV_USER_MAGIC) {
3186 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3187 if (lum->lum_stripe_count > 0)
3188 llapi_printf(LLAPI_MSG_NORMAL,
3189 "mdtidx\t\t FID[seq:oid:ver]\n");
3191 char fmt[48] = { 0 };
3192 sprintf(fmt, "%s%s", hex ? "%#6x" : "%6u",
3193 "\t\t "DFID"\t\t%s\n");
3194 for (i = 0; i < lum->lum_stripe_count; i++) {
3195 int idx = objects[i].lum_mds;
3196 struct lu_fid *fid = &objects[i].lum_fid;
3198 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3199 llapi_printf(LLAPI_MSG_NORMAL, fmt,
3201 obdindex == idx ? " *" : "");
3205 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3206 pool_name[0] != '\0') {
3207 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3208 if (verbose & ~VERBOSE_POOL)
3209 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3211 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3215 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3216 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3219 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3220 enum lov_dump_flags flags)
3222 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3223 int depth = param->fp_max_depth;
3224 enum llapi_layout_verbose verbose = param->fp_verbose;
3225 bool yaml = flags & LDF_YAML;
3227 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3228 !(flags & LDF_IS_DIR)) && !yaml)
3229 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3231 if (verbose & VERBOSE_DETAIL) {
3232 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3233 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3234 " ", comp_v1->lcm_magic);
3235 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3236 " ", comp_v1->lcm_size);
3237 if (flags & LDF_IS_DIR)
3238 llapi_printf(LLAPI_MSG_NORMAL,
3239 "%2slcm_flags: %s\n", " ",
3240 comp_v1->lcm_mirror_count > 0 ?
3243 llapi_printf(LLAPI_MSG_NORMAL,
3244 "%2slcm_flags: %s\n", " ",
3245 llapi_layout_flags_string(comp_v1->lcm_flags));
3248 if (verbose & VERBOSE_GENERATION) {
3249 if (verbose & ~VERBOSE_GENERATION)
3250 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3252 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3255 if (verbose & VERBOSE_MIRROR_COUNT) {
3256 if (verbose & ~VERBOSE_MIRROR_COUNT)
3257 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3259 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3260 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3261 comp_v1->lcm_mirror_count + 1 : 1);
3264 if (verbose & VERBOSE_COMP_COUNT) {
3265 if (verbose & ~VERBOSE_COMP_COUNT)
3266 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3268 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3269 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3270 comp_v1->lcm_entry_count : 0);
3273 if (verbose & VERBOSE_DETAIL && !yaml)
3274 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3277 static void lcme_flags2str(__u32 comp_flags)
3283 llapi_printf(LLAPI_MSG_NORMAL, "0");
3286 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3287 const char *cfn_name = comp_flags_table[i].cfn_name;
3288 __u32 cfn_flag = comp_flags_table[i].cfn_flag;
3290 if ((comp_flags & cfn_flag) == cfn_flag) {
3292 llapi_printf(LLAPI_MSG_NORMAL, ",");
3293 llapi_printf(LLAPI_MSG_NORMAL, "%s", cfn_name);
3294 comp_flags &= ~comp_flags_table[i].cfn_flag;
3300 llapi_printf(LLAPI_MSG_NORMAL, ",");
3301 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3305 static void lov_dump_comp_v1_entry(struct find_param *param,
3306 enum lov_dump_flags flags, int index)
3308 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3309 struct lov_comp_md_entry_v1 *entry;
3310 char *separator = "";
3311 enum llapi_layout_verbose verbose = param->fp_verbose;
3312 bool yaml = flags & LDF_YAML;
3314 entry = &comp_v1->lcm_entries[index];
3317 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3319 if (verbose & VERBOSE_COMP_ID) {
3320 if (verbose & VERBOSE_DETAIL && !yaml)
3321 llapi_printf(LLAPI_MSG_NORMAL,
3322 "%slcme_id: ", " - ");
3323 else if (verbose & ~VERBOSE_COMP_ID)
3324 llapi_printf(LLAPI_MSG_NORMAL,
3325 "%4slcme_id: ", " ");
3326 if (entry->lcme_id != LCME_ID_INVAL)
3327 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3329 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3333 if (verbose & VERBOSE_MIRROR_ID) {
3334 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3335 if (verbose & ~VERBOSE_MIRROR_ID)
3336 llapi_printf(LLAPI_MSG_NORMAL,
3337 "%4slcme_mirror_id: ", " ");
3338 if (entry->lcme_id != LCME_ID_INVAL)
3339 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3340 mirror_id_of(entry->lcme_id));
3342 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3346 if (verbose & VERBOSE_COMP_FLAGS) {
3347 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3348 if (verbose & ~VERBOSE_COMP_FLAGS)
3349 llapi_printf(LLAPI_MSG_NORMAL,
3350 "%4slcme_flags: ", " ");
3351 lcme_flags2str(entry->lcme_flags);
3354 /* print snapshot timestamp if its a nosync comp */
3355 if ((verbose & VERBOSE_COMP_FLAGS) &&
3356 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3357 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3358 if (verbose & ~VERBOSE_COMP_FLAGS)
3359 llapi_printf(LLAPI_MSG_NORMAL,
3360 "%4slcme_timestamp: ", " ");
3362 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3363 (unsigned long long)entry->lcme_timestamp);
3365 time_t stamp = entry->lcme_timestamp;
3366 char *date_str = asctime(localtime(&stamp));
3368 date_str[strlen(date_str) - 1] = '\0';
3369 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3375 if (verbose & VERBOSE_COMP_START) {
3376 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3377 if (verbose & ~VERBOSE_COMP_START)
3378 llapi_printf(LLAPI_MSG_NORMAL,
3379 "%4slcme_extent.e_start: ", " ");
3380 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3381 (unsigned long long)entry->lcme_extent.e_start);
3385 if (verbose & VERBOSE_COMP_END) {
3386 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3387 if (verbose & ~VERBOSE_COMP_END)
3388 llapi_printf(LLAPI_MSG_NORMAL,
3389 "%4slcme_extent.e_end: ", " ");
3390 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3391 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3393 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3394 (unsigned long long)entry->lcme_extent.e_end);
3399 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3400 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3401 } else if (verbose & VERBOSE_DETAIL) {
3402 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3403 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3404 " ", entry->lcme_offset);
3405 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3406 " ", entry->lcme_size);
3407 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3409 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3414 * Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3415 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3417 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3418 * The table below gives the answers for the specified parameters (value and
3419 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3420 * --------------------------------------
3421 * 1 | file > limit; sign > 0 | -1 / -1 |
3422 * 2 | file = limit; sign > 0 | -1 / -1 |
3423 * 3 | file < limit; sign > 0 | ? / 1 |
3424 * 4 | file > limit; sign = 0 | -1 / -1 |
3425 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3426 * 6 | file < limit; sign = 0 | ? / -1 |
3427 * 7 | file > limit; sign < 0 | 1 / 1 |
3428 * 8 | file = limit; sign < 0 | ? / -1 |
3429 * 9 | file < limit; sign < 0 | ? / -1 |
3430 * --------------------------------------
3431 * Note: 5th actually means that the value is within the interval
3432 * (limit - margin, limit].
3434 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3435 int sign, int negopt, unsigned long long margin,
3441 /* Drop the fraction of margin (of days or size). */
3442 if (file + margin <= limit)
3444 } else if (sign == 0) {
3445 if (file <= limit && file + margin > limit)
3447 else if (file + margin <= limit)
3449 } else if (sign < 0) {
3456 return negopt ? ~ret + 1 : ret;
3459 static inline struct lov_user_md *
3460 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3462 return (struct lov_user_md *)((char *)comp_v1 +
3463 comp_v1->lcm_entries[ent_idx].lcme_offset);
3466 static inline struct lov_user_ost_data_v1 *
3467 lov_v1v3_objects(struct lov_user_md *v1)
3469 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3470 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3472 return v1->lmm_objects;
3476 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3478 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3479 snprintf(pool_name, LOV_MAXPOOLNAME + 1, "%s",
3480 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3482 pool_name[0] = '\0';
3486 print_last_init_comp(struct find_param *param)
3488 /* print all component info */
3489 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3492 /* print specific component info */
3493 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3494 param->fp_check_comp_start || param->fp_check_comp_end ||
3495 param->fp_check_mirror_id || param->fp_check_mirror_index)
3501 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3505 if (param->fp_comp_end == LUSTRE_EOF) {
3506 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3507 match = end == LUSTRE_EOF ? 1 : -1;
3508 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3509 match = end == LUSTRE_EOF ? -1 : 1;
3510 else /* at least EOF */
3512 if (param->fp_exclude_comp_end)
3515 unsigned long long margin;
3517 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3518 match = find_value_cmp(end, param->fp_comp_end,
3519 param->fp_comp_end_sign,
3520 param->fp_exclude_comp_end, margin, 0);
3527 * An example of "getstripe -v" for a two components PFL file:
3530 * lcm_magic: 0x0BD60BD0
3534 * lcm_entry_count: 2
3538 * lcme_extent.e_start: 0
3539 * lcme_extent.e_end: 1048576
3543 * lmm_magic: 0x0BD10BD0
3544 * lmm_seq: 0x200000401
3545 * lmm_object_id: 0x1
3546 * lmm_fid: [0x200000401:0x1:0x0]
3547 * lmm_stripe_count: 1
3548 * lmm_stripe_size: 1048576
3549 * lmm_pattern: raid0
3551 * lmm_stripe_offset: 0
3553 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3557 * lcme_extent.e_start: 1048576
3558 * lcme_extent.e_end: EOF
3562 * lmm_magic: 0x0BD10BD0
3563 * lmm_seq: 0x200000401
3564 * lmm_object_id: 0x1
3565 * lmm_fid: [0x200000401:0x1:0x0]
3566 * lmm_stripe_count: 2
3567 * lmm_stripe_size: 1048576
3568 * lmm_pattern: raid0
3570 * lmm_stripe_offset: 1
3572 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3573 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3575 static void lov_dump_comp_v1(struct find_param *param, char *path,
3576 enum lov_dump_flags flags)
3578 struct lov_comp_md_entry_v1 *entry;
3579 struct lov_user_ost_data_v1 *objects;
3580 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3581 struct lov_user_md_v1 *v1;
3582 char pool_name[LOV_MAXPOOLNAME + 1];
3583 int obdindex = param->fp_obd_index;
3584 int i, j, match, ext;
3585 bool obdstripe = false;
3586 __u16 mirror_index = 0;
3587 __u16 mirror_id = 0;
3589 if (obdindex != OBD_NOT_FOUND) {
3590 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3591 i < comp_v1->lcm_entry_count; i++) {
3592 if (!(comp_v1->lcm_entries[i].lcme_flags &
3596 v1 = lov_comp_entry(comp_v1, i);
3597 if (v1->lmm_magic == LOV_MAGIC_FOREIGN)
3600 objects = lov_v1v3_objects(v1);
3602 for (j = 0; j < v1->lmm_stripe_count; j++) {
3603 if (obdindex == objects[j].l_ost_idx) {
3616 lov_dump_comp_v1_header(param, path, flags);
3618 flags |= LDF_INDENT;
3620 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3621 entry = &comp_v1->lcm_entries[i];
3623 if (param->fp_check_comp_flags) {
3624 if (((param->fp_comp_flags & entry->lcme_flags) !=
3625 param->fp_comp_flags) ||
3626 (param->fp_comp_neg_flags & entry->lcme_flags))
3630 if (param->fp_check_comp_id &&
3631 param->fp_comp_id != entry->lcme_id)
3634 if (param->fp_check_comp_start) {
3635 match = find_value_cmp(entry->lcme_extent.e_start,
3636 param->fp_comp_start,
3637 param->fp_comp_start_sign,
3639 param->fp_comp_start_units, 0);
3644 if (param->fp_check_comp_end) {
3645 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3651 if (param->fp_check_mirror_index) {
3652 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3654 mirror_id = mirror_id_of(entry->lcme_id);
3657 match = find_value_cmp(mirror_index,
3658 param->fp_mirror_index,
3659 param->fp_mirror_index_sign,
3660 param->fp_exclude_mirror_index,
3664 } else if (param->fp_check_mirror_id) {
3665 if (mirror_id != mirror_id_of(entry->lcme_id))
3666 mirror_id = mirror_id_of(entry->lcme_id);
3668 match = find_value_cmp(mirror_id,
3669 param->fp_mirror_id,
3670 param->fp_mirror_id_sign,
3671 param->fp_exclude_mirror_id,
3677 if (print_last_init_comp(param)) {
3679 * if part of stripe info is needed, we'd print only
3680 * the last instantiated component info.
3682 if (entry->lcme_flags & LCME_FL_INIT)
3685 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3686 if (entry->lcme_flags & LCME_FL_EXTENSION)
3687 /* moved back below */
3695 if (entry->lcme_flags & LCME_FL_INIT) {
3696 if (obdindex != OBD_NOT_FOUND) {
3697 flags |= LDF_SKIP_OBJS;
3698 v1 = lov_comp_entry(comp_v1, i);
3699 if (v1->lmm_magic == LOV_MAGIC_FOREIGN)
3702 objects = lov_v1v3_objects(v1);
3704 for (j = 0; j < v1->lmm_stripe_count; j++) {
3705 if (obdindex == objects[j].l_ost_idx) {
3706 flags &= ~LDF_SKIP_OBJS;
3711 flags &= ~LDF_SKIP_OBJS;
3714 flags |= LDF_SKIP_OBJS;
3717 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3719 lov_dump_comp_v1_entry(param, flags, i);
3721 v1 = lov_comp_entry(comp_v1, i);
3722 if (v1->lmm_magic == LOV_MAGIC_FOREIGN) {
3723 lov_dump_hsm_lmm(v1, path, param->fp_max_depth,
3724 param->fp_verbose, flags);
3726 objects = lov_v1v3_objects(v1);
3727 lov_v1v3_pool_name(v1, pool_name);
3729 ext = entry->lcme_flags & LCME_FL_EXTENSION ?
3731 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path,
3732 obdindex, param->fp_max_depth,
3733 param->fp_verbose, flags | ext);
3736 if (print_last_init_comp(param)) {
3738 * directory layout contains only layout template, print the
3742 i = comp_v1->lcm_entry_count - 1;
3745 flags &= ~LDF_SKIP_OBJS;
3747 lov_dump_comp_v1_entry(param, flags, i);
3749 v1 = lov_comp_entry(comp_v1, i);
3750 if (v1->lmm_magic == LOV_MAGIC_FOREIGN) {
3751 lov_dump_hsm_lmm(v1, path, param->fp_max_depth,
3752 param->fp_verbose, flags);
3754 objects = lov_v1v3_objects(v1);
3755 lov_v1v3_pool_name(v1, pool_name);
3757 entry = &comp_v1->lcm_entries[i];
3758 ext = entry->lcme_flags & LCME_FL_EXTENSION ?
3760 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path,
3761 obdindex, param->fp_max_depth,
3762 param->fp_verbose, flags | ext);
3767 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3768 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3771 static inline bool has_any_comp_options(struct find_param *param)
3773 enum llapi_layout_verbose verbose = param->fp_verbose;
3775 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3776 param->fp_check_comp_start || param->fp_check_comp_end ||
3777 param->fp_check_comp_flags)
3780 /* show full layout information, not component specific */
3781 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3784 return verbose & VERBOSE_COMP_OPTS;
3787 static struct lov_user_mds_data *
3788 lov_forge_comp_v1(struct lov_user_mds_data *orig, bool is_dir)
3790 struct lov_user_md *lum = &orig->lmd_lmm;
3791 struct lov_user_mds_data *new;
3792 struct lov_comp_md_v1 *comp_v1;
3793 struct lov_comp_md_entry_v1 *ent;
3794 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3795 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3798 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3800 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3804 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3805 + sizeof(new->lmd_lmmsize));
3807 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3808 comp_v1->lcm_magic = lum->lmm_magic;
3809 comp_v1->lcm_size = lum_off + lum_size;
3810 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3811 comp_v1->lcm_flags = 0;
3812 comp_v1->lcm_entry_count = 1;
3814 ent = &comp_v1->lcm_entries[0];
3816 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3817 ent->lcme_extent.e_start = 0;
3818 ent->lcme_extent.e_end = LUSTRE_EOF;
3819 ent->lcme_offset = lum_off;
3820 ent->lcme_size = lum_size;
3822 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3827 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3828 enum lov_dump_flags flags)
3830 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3832 if (has_any_comp_options(param)) {
3833 struct lov_user_mds_data *new_lmd, *orig_lmd;
3835 orig_lmd = param->fp_lmd;
3836 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3837 if (new_lmd != NULL) {
3838 param->fp_lmd = new_lmd;
3839 lov_dump_comp_v1(param, path, flags);
3840 param->fp_lmd = orig_lmd;
3846 if (magic == LOV_USER_MAGIC_V1) {
3847 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3848 param->fp_lmd->lmd_lmm.lmm_objects,
3849 path, param->fp_obd_index,
3850 param->fp_max_depth, param->fp_verbose,
3853 char pool_name[LOV_MAXPOOLNAME + 1];
3854 struct lov_user_ost_data_v1 *objects;
3855 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3857 snprintf(pool_name, sizeof(pool_name), "%s",
3858 lmmv3->lmm_pool_name);
3859 objects = lmmv3->lmm_objects;
3860 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3861 objects, path, param->fp_obd_index,
3862 param->fp_max_depth, param->fp_verbose,
3867 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3868 enum lov_dump_flags flags)
3870 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3871 bool yaml = flags & LDF_YAML;
3873 if (!yaml && param->fp_depth && path)
3874 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3876 if (param->fp_verbose & VERBOSE_DETAIL) {
3877 uint32_t type = check_foreign_type(lfm->lfm_type);
3879 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3881 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3883 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3885 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3886 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3887 lu_foreign_types[type].lft_name);
3889 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3891 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3894 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3895 lfm->lfm_length, lfm->lfm_value);
3896 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3899 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3900 enum lov_dump_flags flags)
3902 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3903 bool yaml = flags & LDF_YAML;
3905 if (!yaml && param->fp_depth && path)
3906 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3908 if (param->fp_verbose & VERBOSE_DETAIL) {
3909 uint32_t type = check_foreign_type(lfm->lfm_type);
3911 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3913 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3915 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3917 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3918 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3919 lu_foreign_types[type].lft_name);
3921 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3923 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3926 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3927 lfm->lfm_length, lfm->lfm_value);
3928 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3931 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3932 enum lov_dump_flags flags)
3936 if (param->fp_get_lmv || param->fp_get_default_lmv)
3937 magic = (__u32)param->fp_lmv_md->lum_magic;
3939 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3942 flags |= LDF_IS_RAW;
3945 if (param->fp_hex_idx)
3946 flags |= LDF_HEX_IDX;
3949 case LOV_USER_MAGIC_V1:
3950 case LOV_USER_MAGIC_V3:
3951 case LOV_USER_MAGIC_SPECIFIC:
3952 lov_dump_plain_user_lmm(param, path, flags);
3954 case LOV_USER_MAGIC_FOREIGN:
3955 lov_dump_foreign_lmm(param, path, flags);
3958 case LMV_USER_MAGIC: {
3959 char pool_name[LOV_MAXPOOLNAME + 1];
3960 struct lmv_user_md *lum;
3962 lum = (struct lmv_user_md *)param->fp_lmv_md;
3963 snprintf(pool_name, sizeof(pool_name), "%s",
3964 lum->lum_pool_name);
3965 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3966 param->fp_max_depth, param->fp_verbose,
3970 case LOV_USER_MAGIC_COMP_V1:
3971 lov_dump_comp_v1(param, path, flags);
3973 case LMV_MAGIC_FOREIGN:
3974 lmv_dump_foreign_lmm(param, path, flags);
3977 llapi_printf(LLAPI_MSG_NORMAL,
3978 "unknown lmm_magic: %#x (expecting one of %#x %#x %#x %#x)\n",
3979 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3980 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3981 LMV_USER_MAGIC, LMV_MAGIC_V1);
3986 static int llapi_file_get_stripe1(const char *path, struct lov_user_md *lum)
3992 fname = strrchr(path, '/');
3994 /* It should be a file (or other non-directory) */
3995 if (fname == NULL) {
3996 dname = (char *)malloc(2);
4000 fname = (char *)path;
4002 dname = (char *)malloc(fname - path + 1);
4005 strncpy(dname, path, fname - path);
4006 dname[fname - path] = '\0';
4010 fd = open(dname, O_RDONLY | O_NONBLOCK);
4016 strcpy((char *)lum, fname);
4017 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
4020 if (close(fd) == -1 && rc == 0)
4028 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
4030 char *canon_path = NULL;
4033 rc = llapi_file_get_stripe1(path, lum);
4034 if (!(rc == -ENOTTY || rc == -ENODATA))
4037 /* Handle failure due to symlinks by dereferencing path manually. */
4038 canon_path = canonicalize_file_name(path);
4039 if (canon_path == NULL)
4040 goto out; /* Keep original rc. */
4042 rc2 = llapi_file_get_stripe1(canon_path, lum);
4044 goto out; /* Keep original rc. */
4053 int llapi_file_lookup(int dirfd, const char *name)
4055 struct obd_ioctl_data data = { 0 };
4060 if (dirfd < 0 || name == NULL)
4063 data.ioc_version = OBD_IOCTL_VERSION;
4064 data.ioc_len = sizeof(data);
4065 data.ioc_inlbuf1 = (char *)name;
4066 data.ioc_inllen1 = strlen(name) + 1;
4068 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
4070 llapi_error(LLAPI_MSG_ERROR, rc,
4071 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
4076 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
4083 * Check if the file time matches all the given criteria (e.g. --atime +/-N).
4084 * Return -1 or 1 if file timestamp does not or does match the given criteria
4085 * correspondingly. Return 0 if the MDS time is being checked and there are
4086 * attributes on OSTs and it is not yet clear if the timespamp matches.
4088 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
4089 * updated timestamps.
4091 static int find_time_check(struct find_param *param, int mds)
4093 struct lov_user_mds_data *lmd = param->fp_lmd;
4097 /* Check if file is accepted. */
4098 if (param->fp_atime) {
4099 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4100 param->fp_atime, param->fp_asign,
4101 param->fp_exclude_atime,
4102 param->fp_time_margin, mds);
4108 if (param->fp_mtime) {
4109 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4110 param->fp_mtime, param->fp_msign,
4111 param->fp_exclude_mtime,
4112 param->fp_time_margin, mds);
4117 * If the previous check matches, but this one is not yet clear,
4118 * we should return 0 to do an RPC on OSTs.
4124 if (param->fp_ctime) {
4125 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4126 param->fp_ctime, param->fp_csign,
4127 param->fp_exclude_ctime,
4128 param->fp_time_margin, mds);
4133 * If the previous check matches, but this one is not yet clear,
4134 * we should return 0 to do an RPC on OSTs.
4143 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4145 struct lov_user_mds_data *lmd = param->fp_lmd;
4150 for (i = 0; i < 2; i++) {
4151 /* Check if file is accepted. */
4152 if (param->fp_newery[NEWERXY_ATIME][i]) {
4153 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4154 param->fp_newery[NEWERXY_ATIME][i],
4161 if (param->fp_newery[NEWERXY_MTIME][i]) {
4162 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4163 param->fp_newery[NEWERXY_MTIME][i],
4169 * If the previous check matches, but this one is not
4170 * yet clear, we should return 0 to do an RPC on OSTs.
4176 if (param->fp_newery[NEWERXY_CTIME][i]) {
4177 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4178 param->fp_newery[NEWERXY_CTIME][i],
4184 * If the previous check matches, but this one is not
4185 * yet clear, we should return 0 to do an RPC on OSTs.
4192 * File birth time (btime) can get from MDT directly.
4193 * if @from_mdt is true, it means the input file attributs are
4194 * obtained directly from MDT.
4195 * Thus, if @from_mdt is false, we should skip the following
4201 if (param->fp_newery[NEWERXY_BTIME][i]) {
4202 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4205 rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4206 param->fp_newery[NEWERXY_BTIME][i],
4217 * Check whether the stripes matches the indexes user provided
4221 static int check_obd_match(struct find_param *param)
4223 struct lov_user_ost_data_v1 *objects;
4224 struct lov_comp_md_v1 *comp_v1 = NULL;
4225 struct lov_user_mds_data *lmd = param->fp_lmd;
4226 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4227 int i, j, k, count = 1;
4229 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4232 if (!S_ISREG(lmd->lmd_stx.stx_mode))
4235 /* exclude foreign */
4236 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4237 return param->fp_exclude_obd;
4240 * Only those files should be accepted, which have a
4241 * stripe on the specified OST.
4243 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4244 comp_v1 = (struct lov_comp_md_v1 *)v1;
4245 count = comp_v1->lcm_entry_count;
4248 for (i = 0; i < count; i++) {
4250 v1 = lov_comp_entry(comp_v1, i);
4252 objects = lov_v1v3_objects(v1);
4254 for (j = 0; j < v1->lmm_stripe_count; j++) {
4255 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4258 for (k = 0; k < param->fp_num_obds; k++) {
4259 if (param->fp_obd_indexes[k] ==
4260 objects[j].l_ost_idx)
4261 return !param->fp_exclude_obd;
4266 return param->fp_exclude_obd;
4269 static int check_mdt_match(struct find_param *param)
4273 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4276 /* FIXME: For striped dir, we should get stripe information and check */
4277 for (i = 0; i < param->fp_num_mdts; i++) {
4278 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4279 return !param->fp_exclude_mdt;
4282 if (param->fp_exclude_mdt)
4289 * Check whether the obd is active or not, if it is
4290 * not active, just print the object affected by this
4293 static void print_failed_tgt(struct find_param *param, char *path, int type)
4295 struct obd_statfs stat_buf;
4296 struct obd_uuid uuid_buf;
4297 int tgt_nr, i, *indexes;
4300 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV) {
4301 llapi_error(LLAPI_MSG_NORMAL, ret, "%s: wrong statfs type(%d)",
4306 tgt_nr = (type == LL_STATFS_LOV) ? param->fp_obd_index :
4307 param->fp_mdt_index;
4308 indexes = (type == LL_STATFS_LOV) ? param->fp_obd_indexes :
4309 param->fp_mdt_indexes;
4311 for (i = 0; i < tgt_nr; i++) {
4312 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4313 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4315 ret = llapi_obd_statfs(path, type, indexes[i], &stat_buf,
4318 llapi_error(LLAPI_MSG_NORMAL, ret,
4319 "%s: obd_uuid: %s failed",
4320 __func__, param->fp_obd_uuid->uuid);
4324 static int find_check_stripe_size(struct find_param *param)
4326 struct lov_comp_md_v1 *comp_v1 = NULL;
4327 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4328 __u32 stripe_size = 0;
4329 int ret, i, count = 1;
4331 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4332 return param->fp_exclude_stripe_size ? 1 : -1;
4334 ret = param->fp_exclude_stripe_size ? 1 : -1;
4335 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4336 comp_v1 = (struct lov_comp_md_v1 *)v1;
4337 count = comp_v1->lcm_entry_count;
4340 for (i = 0; i < count; i++) {
4341 struct lov_comp_md_entry_v1 *ent;
4344 v1 = lov_comp_entry(comp_v1, i);
4346 ent = &comp_v1->lcm_entries[i];
4347 if (ent->lcme_flags & LCME_FL_EXTENSION)
4349 if (!(ent->lcme_flags & LCME_FL_INIT))
4352 stripe_size = v1->lmm_stripe_size;
4355 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4356 param->fp_stripe_size_sign,
4357 param->fp_exclude_stripe_size,
4358 param->fp_stripe_size_units, 0);
4363 static int find_check_ext_size(struct find_param *param)
4365 struct lov_comp_md_v1 *comp_v1;
4366 struct lov_user_md_v1 *v1;
4369 ret = param->fp_exclude_ext_size ? 1 : -1;
4370 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4371 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4374 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4375 struct lov_comp_md_entry_v1 *ent;
4377 v1 = lov_comp_entry(comp_v1, i);
4379 ent = &comp_v1->lcm_entries[i];
4380 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4383 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4384 param->fp_ext_size_sign,
4385 param->fp_exclude_ext_size,
4386 param->fp_ext_size_units, 0);
4387 /* If any ext_size matches */
4395 static __u32 find_get_stripe_count(struct find_param *param)
4397 struct lov_comp_md_v1 *comp_v1 = NULL;
4398 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4400 __u32 stripe_count = 0;
4402 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4405 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4406 comp_v1 = (struct lov_comp_md_v1 *)v1;
4407 count = comp_v1->lcm_entry_count;
4410 for (i = 0; i < count; i++) {
4412 struct lov_comp_md_entry_v1 *ent;
4414 v1 = lov_comp_entry(comp_v1, i);
4416 ent = &comp_v1->lcm_entries[i];
4417 if (!(ent->lcme_flags & LCME_FL_INIT))
4420 if (ent->lcme_flags & LCME_FL_EXTENSION)
4423 stripe_count = v1->lmm_stripe_count;
4426 return stripe_count;
4429 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4431 static int find_check_layout(struct find_param *param)
4433 struct lov_comp_md_v1 *comp_v1 = NULL;
4434 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4436 bool found = false, valid = false;
4438 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4439 comp_v1 = (struct lov_comp_md_v1 *)v1;
4440 count = comp_v1->lcm_entry_count;
4443 for (i = 0; i < count; i++) {
4445 v1 = lov_comp_entry(comp_v1, i);
4447 /* foreign file have a special magic but no pattern field */
4448 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4451 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4455 if (v1->lmm_pattern & param->fp_layout) {
4464 if ((found && !param->fp_exclude_layout) ||
4465 (!found && param->fp_exclude_layout))
4472 * if no type specified, check/exclude all foreign
4473 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4475 static int find_check_foreign(struct find_param *param)
4477 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4478 struct lov_foreign_md *lfm;
4480 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4481 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4482 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4483 return param->fp_exclude_foreign ? 1 : -1;
4487 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4488 lfm->lfm_type == param->fp_foreign_type)
4489 return param->fp_exclude_foreign ? -1 : 1;
4490 return param->fp_exclude_foreign ? 1 : -1;
4493 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4494 struct lmv_foreign_md *lfm;
4496 lfm = (void *)param->fp_lmv_md;
4497 if (lmv_is_foreign(lfm->lfm_magic)) {
4498 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4499 return param->fp_exclude_foreign ? 1 : -1;
4503 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4504 lfm->lfm_type == param->fp_foreign_type)
4505 return param->fp_exclude_foreign ? -1 : 1;
4506 return param->fp_exclude_foreign ? 1 : -1;
4511 static int find_check_pool(struct find_param *param)
4513 struct lov_comp_md_v1 *comp_v1 = NULL;
4514 struct lov_user_md_v3 *v3 = (void *)¶m->fp_lmd->lmd_lmm;
4518 if (v3->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4519 comp_v1 = (struct lov_comp_md_v1 *)v3;
4520 count = comp_v1->lcm_entry_count;
4521 /* empty requested pool is taken as no pool search */
4522 if (count == 0 && param->fp_poolname[0] == '\0') {
4528 for (i = 0; i < count; i++) {
4529 if (comp_v1 != NULL) {
4530 if (!(comp_v1->lcm_entries[i].lcme_flags &
4534 v3 = (void *)lov_comp_entry(comp_v1, i);
4537 if (v3->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4540 if (((v3->lmm_magic == LOV_USER_MAGIC_V1) &&
4541 (param->fp_poolname[0] == '\0')) ||
4542 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4543 (strncmp(v3->lmm_pool_name,
4544 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4545 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4546 (strcmp(param->fp_poolname, "*") == 0))) {
4553 if ((found && !param->fp_exclude_pool) ||
4554 (!found && param->fp_exclude_pool))
4560 static int find_check_comp_options(struct find_param *param)
4562 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4563 struct lov_user_mds_data *lmd = param->fp_lmd;
4564 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4565 struct lov_comp_md_entry_v1 *entry;
4568 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4571 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4572 comp_v1 = (struct lov_comp_md_v1 *)v1;
4574 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4575 if (forged_v1 == NULL)
4577 comp_v1 = forged_v1;
4578 comp_v1->lcm_entry_count = 1;
4579 entry = &comp_v1->lcm_entries[0];
4580 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4582 entry->lcme_extent.e_start = 0;
4583 entry->lcme_extent.e_end = LUSTRE_EOF;
4586 /* invalid case, don't match for any kind of search. */
4587 if (comp_v1->lcm_entry_count == 0) {
4592 if (param->fp_check_comp_count) {
4593 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4594 param->fp_comp_count,
4595 param->fp_comp_count_sign,
4596 param->fp_exclude_comp_count, 1, 0);
4602 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4603 entry = &comp_v1->lcm_entries[i];
4605 if (param->fp_check_comp_flags) {
4607 if (((param->fp_comp_flags & entry->lcme_flags) !=
4608 param->fp_comp_flags) ||
4609 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4615 if (param->fp_check_comp_start) {
4616 ret = find_value_cmp(entry->lcme_extent.e_start,
4617 param->fp_comp_start,
4618 param->fp_comp_start_sign,
4619 param->fp_exclude_comp_start,
4620 param->fp_comp_start_units, 0);
4625 if (param->fp_check_comp_end) {
4626 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4632 /* the component matches all criteria */
4641 static int find_check_mirror_options(struct find_param *param)
4643 struct lov_comp_md_v1 *comp_v1;
4644 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4647 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4650 comp_v1 = (struct lov_comp_md_v1 *)v1;
4652 if (param->fp_check_mirror_count) {
4653 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4654 param->fp_mirror_count,
4655 param->fp_mirror_count_sign,
4656 param->fp_exclude_mirror_count, 1, 0);
4661 if (param->fp_check_mirror_state) {
4663 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4665 if ((param->fp_mirror_state != 0 &&
4666 file_state != param->fp_mirror_state) ||
4667 file_state == param->fp_mirror_neg_state)
4674 static int find_check_attr_options(struct find_param *param)
4679 attrs = param->fp_lmd->lmd_stx.stx_attributes_mask &
4680 param->fp_lmd->lmd_stx.stx_attributes;
4682 /* This is a AND between all (negated) specified attributes */
4683 if ((param->fp_attrs && (param->fp_attrs & attrs) != param->fp_attrs) ||
4684 (param->fp_neg_attrs && (param->fp_neg_attrs & attrs)))
4687 if ((found && param->fp_exclude_attrs) ||
4688 (!found && !param->fp_exclude_attrs))
4695 * xattr_reg_match() - return true if the supplied string matches the pattern.
4697 * This requires the regex to match the entire supplied string, not just a
4700 * str must be null-terminated. len should be passed in anyways to avoid an
4701 * extra call to strlen(str) when the length is already known.
4703 static bool xattr_reg_match(regex_t *pattern, const char *str, int len)
4708 ret = regexec(pattern, str, 1, &pmatch, 0);
4709 if (ret == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
4716 * xattr_done_matching() - return true if all supplied patterns have been
4717 * matched, allowing to skip checking any remaining xattrs on a file.
4719 * This is only allowed if there are no "exclude" patterns.
4721 static int xattr_done_matching(struct xattr_match_info *xmi)
4725 for (i = 0; i < xmi->xattr_regex_count; i++) {
4726 /* if any pattern still undecided, need to keep going */
4727 if (!xmi->xattr_regex_matched[i])
4734 static int find_check_xattrs(char *path, struct xattr_match_info *xmi)
4736 ssize_t list_len = 0;
4737 ssize_t val_len = 0;
4742 for (i = 0; i < xmi->xattr_regex_count; i++)
4743 xmi->xattr_regex_matched[i] = false;
4745 list_len = llistxattr(path, xmi->xattr_name_buf, XATTR_LIST_MAX);
4747 llapi_error(LLAPI_MSG_ERROR, errno,
4748 "error: listxattr: %s", path);
4752 /* loop over all xattr names on the file */
4753 for (p = xmi->xattr_name_buf;
4754 p - xmi->xattr_name_buf < list_len;
4755 p = strchr(p, '\0'), p++) {
4756 fetched_val = false;
4757 /* loop over all regex patterns specified and check them */
4758 for (i = 0; i < xmi->xattr_regex_count; i++) {
4759 if (xmi->xattr_regex_matched[i])
4762 if (!xattr_reg_match(xmi->xattr_regex_name[i],
4766 if (xmi->xattr_regex_value[i] == NULL)
4770 * even if multiple patterns match the same xattr name,
4771 * don't call getxattr() more than once
4774 val_len = lgetxattr(path, p,
4775 xmi->xattr_value_buf,
4779 llapi_error(LLAPI_MSG_ERROR, errno,
4780 "error: getxattr: %s",
4786 * the value returned by getxattr might or
4787 * might not be null terminated.
4788 * if it is, then decrement val_len so it
4789 * matches what strlen() would return.
4790 * if it is not, then add a null terminator
4791 * since regexec() expects that.
4794 xmi->xattr_value_buf[val_len - 1] == '\0') {
4797 xmi->xattr_value_buf[val_len] = '\0';
4801 if (!xattr_reg_match(xmi->xattr_regex_value[i],
4802 xmi->xattr_value_buf, val_len))
4807 * if exclude this xattr, we can exit early
4810 if (xmi->xattr_regex_exclude[i])
4813 xmi->xattr_regex_matched[i] = true;
4816 * if all "include" patterns have matched, and there are
4817 * no "exclude" patterns, we can exit early with match
4819 if (xattr_done_matching(xmi) == 1)
4825 * finally, check that all supplied patterns either matched, or were
4826 * "exclude" patterns if they did not match.
4828 for (i = 0; i < xmi->xattr_regex_count; i++) {
4829 if (!xmi->xattr_regex_matched[i]) {
4830 if (!xmi->xattr_regex_exclude[i]) {
4839 static bool find_check_lmm_info(struct find_param *param)
4841 return param->fp_check_pool || param->fp_check_stripe_count ||
4842 param->fp_check_stripe_size || param->fp_check_layout ||
4843 param->fp_check_comp_count || param->fp_check_comp_end ||
4844 param->fp_check_comp_start || param->fp_check_comp_flags ||
4845 param->fp_check_mirror_count || param->fp_check_foreign ||
4846 param->fp_check_mirror_state || param->fp_check_ext_size ||
4847 param->fp_check_projid;
4851 * Interpret backslash escape sequences and write output into buffer.
4852 * Anything written to the buffer will be null terminated.
4854 * @param[in] seq String being parsed for escape sequence. The leading
4855 * '\' character is not included in this string (only the
4856 * characters after it)
4857 * @param[out] buffer Location where interpreted escape sequence is written
4858 * @param[in] size Size of the available buffer. (Needs to be large enough
4859 * to handle escape sequence output plus null terminator.)
4860 * @param[out] wrote Number of bytes written to the buffer.
4861 * @return Number of characters from input string processed
4862 * as part of the escape sequence (0 for an unrecognized
4865 static int printf_format_escape(char *seq, char *buffer, size_t size,
4869 /* For now, only handle single char escape sequences: \n, \t, \\ */
4892 * Interpret formats for timestamps (%a, %A@, etc)
4894 * @param[in] seq String being parsed for timestamp format. The leading
4895 * '%' character is not included in this string
4896 * @param[out] buffer Location where timestamp info is written
4897 * @param[in] size Size of the available buffer.
4898 * @param[out] wrote Number of bytes written to the buffer.
4899 * @return Number of characters from input string processed
4900 * as part of the format (0 for an unknown format)
4902 static int printf_format_timestamp(char *seq, char *buffer, size_t size,
4903 int *wrote, struct find_param *param)
4905 struct statx_timestamp ts = { 0, 0 };
4909 char *fmt = "%c"; /* Print in ctime format by default */
4914 ts = param->fp_lmd->lmd_stx.stx_atime;
4918 if (*(seq + 1) == '@') {
4919 ts = param->fp_lmd->lmd_stx.stx_atime;
4925 ts = param->fp_lmd->lmd_stx.stx_ctime;
4929 if (*(seq + 1) == '@') {
4930 ts = param->fp_lmd->lmd_stx.stx_ctime;
4936 ts = param->fp_lmd->lmd_stx.stx_mtime;
4940 if (*(seq + 1) == '@') {
4941 ts = param->fp_lmd->lmd_stx.stx_mtime;
4947 ts = param->fp_lmd->lmd_stx.stx_btime;
4951 if (*(seq + 1) == '@') {
4952 ts = param->fp_lmd->lmd_stx.stx_btime;
4962 /* Found valid format, print to buffer */
4965 *wrote = strftime(buffer, size, fmt, tm);
4972 * Print all ost indices associated with a file layout using a commma separated
4973 * list. For a file with mutliple components, the list of indices for each
4974 * component will be enclosed in brackets.
4976 * @param[out] buffer Location where OST indices are written
4977 * @param[in] size Size of the available buffer.
4978 * @pararm[in] layout Pointer to layout structure for the file
4979 * @return Number of bytes written to output buffer
4981 static int printf_format_ost_indices(char *buffer, size_t size,
4982 struct llapi_layout *layout)
4984 uint64_t count, idx, i;
4985 int err, bytes, wrote = 0;
4987 /* Make sure to start at the first component */
4988 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
4990 llapi_error(LLAPI_MSG_ERROR, err,
4991 "error: layout component iteration failed\n");
4995 err = llapi_layout_stripe_count_get(layout, &count);
4997 llapi_error(LLAPI_MSG_ERROR, err,
4998 "error: cannot get stripe_count\n");
5002 bytes = snprintf(buffer, (size - wrote), "%s", "[");
5007 for (i = 0; i < count; i++) {
5008 err = llapi_layout_ost_index_get(layout, i, &idx);
5010 llapi_error(LLAPI_MSG_ERROR, err,
5011 "error: cannot get OST index\n");
5012 bytes = snprintf(buffer, (size - wrote),
5015 bytes = snprintf(buffer, (size - wrote),
5023 /* Overwrite last comma with closing bracket */
5024 *(buffer - 1) = ']';
5026 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
5027 if (err == 0) /* next component is found */
5030 llapi_error(LLAPI_MSG_ERROR, err,
5031 "error: layout component iteration failed\n");
5032 /* At this point, either got error or reached last component */
5043 * Print file attributes as a comma-separated list of named attribute flags,
5044 * and hex value of any unknown attributes.
5046 * @param[out] buffer Location where file attributes are written
5047 * @param[in] size Size of the available buffer.
5048 * @pararm[in] lstx Void pointer holding address of struct statx. Which is
5049 * containing attributes to be printed
5050 * @return Number of bytes written to output buffer
5052 static int printf_format_file_attributes(char *buffer, size_t size,
5053 void *lstx, bool longopt)
5055 lstatx_t *stx = (lstatx_t *)lstx;
5056 uint64_t attrs = stx->stx_attributes_mask & stx->stx_attributes;
5057 int bytes = 0, wrote = 0, first = 1;
5058 uint64_t known_attrs = 0;
5059 struct attrs_name *ap;
5061 /* before all, print '---' if no attributes, and exit */
5063 bytes = snprintf(buffer, size - wrote, "---");
5068 /* first, browse list of known attributes */
5069 for (ap = (struct attrs_name *)attrs_array; ap->an_attr != 0; ap++) {
5070 known_attrs |= ap->an_attr;
5071 if (attrs & ap->an_attr) {
5073 bytes = snprintf(buffer, size - wrote, "%s%s",
5074 first ? "" : ",", ap->an_name);
5076 bytes = snprintf(buffer, size - wrote, "%c",
5086 /* second, print hex value for unknown attributes */
5087 attrs &= ~known_attrs;
5089 bytes = snprintf(buffer, size - wrote, "%s0x%lx",
5090 first ? "" : ",", attrs);
5101 * Parse Lustre-specific format sequences of the form %L{x}.
5103 * @param[in] seq String being parsed for format sequence. The leading
5104 * '%' character is not included in this string
5105 * @param[out] buffer Location where interpreted format info is written
5106 * @param[in] size Size of the available buffer.
5107 * @param[out] wrote Number of bytes written to the buffer.
5108 * @param[in] param The find_param structure associated with the file/dir
5109 * @param[in] path Pathname of the current file/dir being handled
5110 * @param[in] projid Project ID associated with the current file/dir
5111 * @param[in] d File descriptor for the directory (or -1 for a
5112 * non-directory file)
5113 * @return Number of characters from input string processed
5114 * as part of the format (0 for an unknown format)
5116 static int printf_format_lustre(char *seq, char *buffer, size_t size,
5117 int *wrote, struct find_param *param,
5118 char *path, __u32 projid, int d)
5120 struct lmv_user_md *lum;
5121 struct lmv_user_mds_data *objects;
5122 struct llapi_layout *layout = NULL;
5124 unsigned int hash_type;
5125 uint64_t str_cnt, str_size, idx;
5126 char pool_name[LOV_MAXPOOLNAME + 1] = { '\0' };
5128 bool longopt = true;
5129 int rc = 2; /* all current valid sequences are 2 chars */
5133 /* Sanity check. Formats always look like %L{X} */
5134 if (*seq++ != 'L') {
5140 * Some formats like %LF or %LP are handled the same for both files
5141 * and dirs, so handle all of those here.
5145 err = llapi_path2fid(path, &fid);
5147 llapi_error(LLAPI_MSG_ERROR, err,
5148 "error: cannot get fid\n");
5151 *wrote = snprintf(buffer, size, DFID_NOBRACE, PFID(&fid));
5154 if (projid == INVALID_PROJID)
5155 *wrote = snprintf(buffer, size, "-1");
5157 *wrote = snprintf(buffer, size, "%u", projid);
5159 case 'a': /* file attributes */
5163 lstx = ¶m->fp_lmd->lmd_stx;
5165 *wrote = printf_format_file_attributes(buffer, size, lstx,
5170 /* Other formats for files/dirs need to be handled differently */
5171 if (d == -1) { /* file */
5172 //layout = llapi_layout_get_by_xattr(¶m->fp_lmd->lmd_lmm,
5173 // param->fp_lum_size, 0);
5174 layout = llapi_layout_get_by_path(path, 0);
5175 if (layout == NULL) {
5176 llapi_error(LLAPI_MSG_ERROR, errno,
5177 "error: cannot get file layout\n");
5182 * Set the layout pointer to the last init component
5183 * since that is the component used for most of these
5184 * formats. (This also works for non-composite files)
5186 err = llapi_layout_get_last_init_comp(layout);
5188 llapi_error(LLAPI_MSG_ERROR, err,
5189 "error: cannot get last initialized compomnent\n");
5194 case 'c': /* stripe count */
5195 err = llapi_layout_stripe_count_get(layout, &str_cnt);
5197 llapi_error(LLAPI_MSG_ERROR, err,
5198 "error: cannot get stripe_count\n");
5201 *wrote = snprintf(buffer, size, "%"PRIu64, str_cnt);
5203 case 'h': /* hash info */
5204 /* Not applicable to files. Skip it. */
5206 case 'i': /* starting index */
5207 err = llapi_layout_ost_index_get(layout, 0, &idx);
5209 llapi_error(LLAPI_MSG_ERROR, err,
5210 "error: cannot get OST index of last initialized component\n");
5213 *wrote = snprintf(buffer, size, "%"PRIu64, idx);
5215 case 'o': /* list of object indices */
5216 *wrote = printf_format_ost_indices(buffer, size, layout);
5218 case 'p': /* pool name */
5219 err = llapi_layout_pool_name_get(layout, pool_name,
5222 llapi_error(LLAPI_MSG_ERROR, rc,
5223 "error: cannot get pool name\n");
5226 *wrote = snprintf(buffer, size, "%s", pool_name);
5228 case 'S': /* stripe size */
5229 err = llapi_layout_stripe_size_get(layout, &str_size);
5231 llapi_error(LLAPI_MSG_ERROR, rc,
5232 "error: cannot get stripe_size\n");
5235 *wrote = snprintf(buffer, size, "%"PRIu64, str_size);
5241 } else { /* directory */
5242 lum = (struct lmv_user_md *)param->fp_lmv_md;
5243 objects = lum->lum_objects;
5246 case 'c': /* stripe count */
5247 *wrote = snprintf(buffer, size, "%d",
5248 (int)lum->lum_stripe_count);
5250 case 'h': /* hash info */
5251 hash_type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
5252 if (hash_type < LMV_HASH_TYPE_MAX)
5253 *wrote = snprintf(buffer, size, "%s",
5254 mdt_hash_name[hash_type]);
5256 *wrote = snprintf(buffer, size, "%#x",
5259 case 'i': /* starting index */
5260 *wrote = snprintf(buffer, size, "%d",
5261 lum->lum_stripe_offset);
5263 case 'o': /* list of object indices */
5264 str_cnt = (int) lum->lum_stripe_count;
5265 *wrote = snprintf(buffer, size, "%s", "[");
5269 for (i = 0; i < str_cnt; i++) {
5270 bytes = snprintf(buffer, (size - *wrote),
5271 "%d,", objects[i].lum_mds);
5278 /* Use lum_offset as the only list entry */
5279 bytes = snprintf(buffer, (size - *wrote),
5280 "%d]", lum->lum_stripe_offset);
5283 /* Overwrite last comma with closing bracket */
5284 *(buffer - 1) = ']';
5287 case 'p': /* pool name */
5288 *wrote = snprintf(buffer, size, "%s",
5289 lum->lum_pool_name);
5291 case 'S': /* stripe size */
5292 /* This has no meaning for directories. Skip it. */
5302 llapi_layout_free(layout);
5305 /* output of snprintf was truncated */
5312 * Interpret format specifiers beginning with '%'.
5314 * @param[in] seq String being parsed for format specifier. The leading
5315 * '%' character is not included in this string
5316 * @param[out] buffer Location where formatted info is written
5317 * @param[in] size Size of the available buffer.
5318 * @param[out] wrote Number of bytes written to the buffer.
5319 * @param[in] param The find_param structure associated with the file/dir
5320 * @param[in] path Pathname of the current file/dir being handled
5321 * @param[in] projid Project ID associated with the current file/dir
5322 * @param[in] d File descriptor for the directory (or -1 for a
5323 * non-directory file)
5324 * @return Number of characters from input string processed
5325 * as part of the format (0 for an unknown format)
5327 static int printf_format_directive(char *seq, char *buffer, size_t size,
5328 int *wrote, struct find_param *param,
5329 char *path, __u32 projid, int d)
5331 __u16 mode = param->fp_lmd->lmd_stx.stx_mode;
5332 uint64_t blocks = param->fp_lmd->lmd_stx.stx_blocks;
5333 int rc = 1; /* most specifiers are single character */
5341 case 'w': case 'W': /* timestamps */
5342 rc = printf_format_timestamp(seq, buffer, size, wrote, param);
5344 case 'b': /* file size (in 512B blocks) */
5345 *wrote = snprintf(buffer, size, "%"PRIu64, blocks);
5347 case 'G': /* GID of owner */
5348 *wrote = snprintf(buffer, size, "%u",
5349 param->fp_lmd->lmd_stx.stx_gid);
5351 case 'k': /* file size (in 1K blocks) */
5352 *wrote = snprintf(buffer, size, "%"PRIu64, (blocks + 1)/2);
5354 case 'L': /* Lustre-specific formats */
5355 rc = printf_format_lustre(seq, buffer, size, wrote, param,
5358 case 'm': /* file mode in octal */
5359 *wrote = snprintf(buffer, size, "%#o", (mode & (~S_IFMT)));
5361 case 'n': /* number of links */
5362 *wrote = snprintf(buffer, size, "%u",
5363 param->fp_lmd->lmd_stx.stx_nlink);
5365 case 'p': /* Path name of file */
5366 *wrote = snprintf(buffer, size, "%s", path);
5368 case 's': /* file size (in bytes) */
5369 *wrote = snprintf(buffer, size, "%"PRIu64,
5370 (uint64_t) param->fp_lmd->lmd_stx.stx_size);
5372 case 'U': /* UID of owner */
5373 *wrote = snprintf(buffer, size, "%u",
5374 param->fp_lmd->lmd_stx.stx_uid);
5376 case 'y': /* file type */
5379 else if (S_ISDIR(mode))
5381 else if (S_ISLNK(mode))
5383 else if (S_ISBLK(mode))
5385 else if (S_ISCHR(mode))
5387 else if (S_ISFIFO(mode))
5389 else if (S_ISSOCK(mode))
5399 default: /* invalid format specifier */
5405 /* output of snprintf was truncated */
5412 * Parse user-supplied string for the -printf option and interpret any
5413 * '%' format specifiers or '\' escape sequences.
5415 * @param[in] param The find_param struct containing the -printf string
5416 * as well as info about the current file/dir that mathced
5417 * the lfs find search criteria
5418 * @param[in] path Path name for current file/dir
5419 * @param[in] projid Project ID associated with current file/dir
5420 * @param[in] d File descriptor for current directory (or -1 for a
5421 * non-directory file)
5423 static void printf_format_string(struct find_param *param, char *path,
5424 __u32 projid, int d)
5426 char output[FORMATTED_BUF_LEN];
5427 char *fmt_char = param->fp_format_printf_str;
5428 char *buff = output;
5434 buff_size = FORMATTED_BUF_LEN;
5436 /* Always leave one free byte in buffer for trailing NUL */
5437 while (*fmt_char && (buff_size > 1)) {
5440 if (*fmt_char == '%') {
5441 rc = printf_format_directive(fmt_char + 1, buff,
5442 buff_size, &written, param,
5444 } else if (*fmt_char == '\\')
5445 rc = printf_format_escape(fmt_char + 1, buff,
5446 buff_size, &written);
5449 /* Either a '\' escape or '%' format was processed.
5450 * Increment pointers accordingly.
5452 fmt_char += (rc + 1);
5454 buff_size -= written;
5456 /* Regular char or invalid escape/format.
5457 * Either way, copy current character.
5459 *buff++ = *fmt_char++;
5464 /* Terminate output buffer and print */
5466 llapi_printf(LLAPI_MSG_NORMAL, "%s", output);
5470 * Get file/directory project id.
5471 * by the open fd resides on.
5472 * Return 0 and project id on success, or -ve errno.
5474 static int fget_projid(int fd, __u32 *projid)
5479 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
5481 *projid = INVALID_PROJID;
5485 *projid = fsx.fsx_projid;
5490 * Check that the file's permissions in *st matches the one in find_param
5492 static int check_file_permissions(const struct find_param *param,
5499 switch (param->fp_perm_sign) {
5500 case LFS_FIND_PERM_EXACT:
5501 decision = (mode == param->fp_perm);
5503 case LFS_FIND_PERM_ALL:
5504 decision = ((mode & param->fp_perm) == param->fp_perm);
5506 case LFS_FIND_PERM_ANY:
5507 decision = ((mode & param->fp_perm) != 0);
5511 if ((param->fp_exclude_perm && decision)
5512 || (!param->fp_exclude_perm && !decision))
5518 static int cb_find_init(char *path, int p, int *dp,
5519 void *data, struct dirent64 *de)
5521 struct find_param *param = (struct find_param *)data;
5522 struct lov_user_mds_data *lmd = param->fp_lmd;
5523 int d = dp == NULL ? -1 : *dp;
5524 int decision = 1; /* 1 is accepted; -1 is rejected. */
5526 int checked_type = 0;
5528 __u32 stripe_count = 0;
5531 __u32 projid = INVALID_PROJID;
5532 bool gather_all = false;
5534 if (p == -1 && d == -1)
5537 /* Reset this value between invocations */
5538 param->fp_get_lmv = 0;
5540 /* Gather all file/dir info, not just what's needed for search params */
5541 if (param->fp_format_printf_str)
5544 /* If a regular expression is presented, make the initial decision */
5545 if (param->fp_pattern != NULL) {
5546 char *fname = strrchr(path, '/');
5548 fname = (fname == NULL ? path : fname + 1);
5549 ret = fnmatch(param->fp_pattern, fname, 0);
5550 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
5551 (ret == 0 && param->fp_exclude_pattern))
5555 /* See if we can check the file type from the dirent. */
5556 if (de != NULL && de->d_type != DT_UNKNOWN) {
5557 if (param->fp_type != 0) {
5560 if (DTTOIF(de->d_type) == param->fp_type) {
5561 if (param->fp_exclude_type)
5564 if (!param->fp_exclude_type)
5568 if ((param->fp_check_mdt_count || param->fp_hash_type ||
5569 param->fp_check_hash_flag) && de->d_type != DT_DIR)
5576 * Request MDS for the stat info if some of these parameters need
5579 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
5580 param->fp_check_uid || param->fp_check_gid ||
5581 param->fp_newerxy || param->fp_btime ||
5582 param->fp_atime || param->fp_mtime || param->fp_ctime ||
5583 param->fp_check_size || param->fp_check_blocks ||
5584 find_check_lmm_info(param) ||
5585 param->fp_check_mdt_count || param->fp_hash_type ||
5586 param->fp_check_hash_flag || param->fp_perm_sign ||
5587 param->fp_nlink || param->fp_attrs || param->fp_neg_attrs ||
5591 if (param->fp_type != 0 && checked_type == 0)
5594 if (decision == 0) {
5596 (param->fp_check_mdt_count || param->fp_hash_type ||
5597 param->fp_check_hash_flag || param->fp_check_foreign ||
5599 * cb_get_dirstripe is needed when checking nlink because
5600 * nlink is handled differently for multi-stripe directory
5601 * vs. single-stripe directory
5603 param->fp_nlink || gather_all)) {
5604 param->fp_get_lmv = 1;
5605 ret = cb_get_dirstripe(path, &d, param);
5607 if (errno == ENODATA) {
5608 /* Fill in struct for unstriped dir */
5610 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
5611 /* Use 0 until we find actual offset */
5612 param->fp_lmv_md->lum_stripe_offset = 0;
5613 param->fp_lmv_md->lum_stripe_count = 0;
5614 param->fp_lmv_md->lum_hash_type = 0;
5616 if (param->fp_check_foreign) {
5617 if (param->fp_exclude_foreign)
5626 if (param->fp_check_mdt_count) {
5627 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5630 decision = find_value_cmp(param->fp_lmv_md->lum_stripe_count,
5631 param->fp_mdt_count,
5632 param->fp_mdt_count_sign,
5633 param->fp_exclude_mdt_count, 1, 0);
5638 if (param->fp_hash_type) {
5640 __u32 type = param->fp_lmv_md->lum_hash_type &
5643 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5646 found = (1 << type) & param->fp_hash_type;
5647 if ((found && param->fp_exclude_hash_type) ||
5648 (!found && !param->fp_exclude_hash_type))
5652 if (param->fp_check_hash_flag) {
5653 __u32 flags = param->fp_lmv_md->lum_hash_type &
5654 ~LMV_HASH_TYPE_MASK;
5656 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5659 if (!(flags & param->fp_hash_inflags) ||
5660 (flags & param->fp_hash_exflags))
5665 param->fp_lmd->lmd_lmm.lmm_magic = 0;
5666 ret = get_lmd_info_fd(path, p, d, param->fp_lmd,
5667 param->fp_lum_size, GET_LMD_INFO);
5668 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
5669 find_check_lmm_info(param)) {
5670 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
5673 * We need to "fake" the "use the default" values
5674 * since the lmm struct is zeroed out at this point.
5676 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5677 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
5679 ostid_set_seq(&lmm->lmm_oi,
5680 FID_SEQ_LOV_DEFAULT);
5681 lmm->lmm_stripe_size = 0;
5682 lmm->lmm_stripe_count = 0;
5683 lmm->lmm_stripe_offset = -1;
5685 if (ret == 0 && (param->fp_mdt_uuid != NULL || gather_all)) {
5687 ret = llapi_file_fget_mdtidx(d,
5688 ¶m->fp_file_mdt_index);
5690 * Make sure lum_stripe_offset matches
5691 * mdt_index even for unstriped directories.
5693 if (ret == 0 && param->fp_get_lmv)
5694 param->fp_lmv_md->lum_stripe_offset =
5695 param->fp_file_mdt_index;
5696 } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
5698 * FIXME: we could get the MDT index from the
5699 * file's FID in lmd->lmd_lmm.lmm_oi without
5700 * opening the file, once we are sure that
5701 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
5702 * That would still be an ioctl() to map the
5703 * FID to the MDT, but not an open RPC.
5705 fd = open(path, O_RDONLY);
5707 ret = llapi_file_fget_mdtidx(fd,
5708 ¶m->fp_file_mdt_index);
5714 * For a special file, we assume it resides on
5715 * the same MDT as the parent directory.
5717 ret = llapi_file_fget_mdtidx(p,
5718 ¶m->fp_file_mdt_index);
5729 stripe_count = find_get_stripe_count(param);
5733 /* Check the file permissions from the stat info */
5734 if (param->fp_perm_sign) {
5735 decision = check_file_permissions(param, lmd->lmd_stx.stx_mode);
5740 if (param->fp_type && !checked_type) {
5741 if ((param->fp_check_mdt_count || param->fp_check_hash_flag ||
5742 param->fp_hash_type) && !S_ISDIR(lmd->lmd_stx.stx_mode))
5745 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
5746 if (param->fp_exclude_type)
5749 if (!param->fp_exclude_type)
5755 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
5756 if (lustre_fs && param->fp_got_uuids &&
5757 param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
5758 lmd->lmd_stx.stx_dev_minor)) {
5759 /* A lustre/lustre mount point is crossed. */
5760 param->fp_got_uuids = 0;
5761 param->fp_obds_printed = 0;
5762 param->fp_mdt_index = OBD_NOT_FOUND;
5763 param->fp_obd_index = OBD_NOT_FOUND;
5766 if (lustre_fs && !param->fp_got_uuids) {
5767 ret = setup_target_indexes((d != -1) ? d : p, path,
5772 param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
5773 lmd->lmd_stx.stx_dev_minor);
5774 } else if (!lustre_fs && param->fp_got_uuids) {
5775 /* A lustre/non-lustre mount point is crossed. */
5776 param->fp_got_uuids = 0;
5777 param->fp_mdt_index = OBD_NOT_FOUND;
5778 param->fp_obd_index = OBD_NOT_FOUND;
5782 if (param->fp_check_foreign) {
5783 decision = find_check_foreign(param);
5788 if (param->fp_check_stripe_size) {
5789 decision = find_check_stripe_size(param);
5794 if (param->fp_check_ext_size) {
5795 decision = find_check_ext_size(param);
5800 if (param->fp_check_stripe_count) {
5801 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
5802 param->fp_stripe_count_sign,
5803 param->fp_exclude_stripe_count, 1, 0);
5808 if (param->fp_check_layout) {
5809 decision = find_check_layout(param);
5814 /* If an OBD UUID is specified but none matches, skip this file. */
5815 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
5816 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
5820 * If an OST or MDT UUID is given, and some OST matches,
5823 if (param->fp_obd_index != OBD_NOT_FOUND ||
5824 param->fp_mdt_index != OBD_NOT_FOUND) {
5825 if (param->fp_obd_uuid) {
5826 if (check_obd_match(param)) {
5828 * If no mdtuuid is given, we are done.
5829 * Otherwise, fall through to the mdtuuid
5832 if (!param->fp_mdt_uuid)
5839 if (param->fp_mdt_uuid) {
5840 if (check_mdt_match(param))
5847 if (param->fp_check_uid) {
5848 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
5849 if (param->fp_exclude_uid)
5852 if (!param->fp_exclude_uid)
5857 if (param->fp_check_gid) {
5858 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
5859 if (param->fp_exclude_gid)
5862 if (!param->fp_exclude_gid)
5867 if (param->fp_check_projid || gather_all) {
5869 fd = open(path, O_RDONLY | O_NONBLOCK);
5872 ret = fget_projid(fd, &projid);
5875 if (param->fp_check_projid) {
5878 if (projid == param->fp_projid) {
5879 if (param->fp_exclude_projid)
5882 if (!param->fp_exclude_projid)
5888 if (param->fp_check_pool) {
5889 decision = find_check_pool(param);
5894 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
5895 param->fp_check_comp_start || param->fp_check_comp_end) {
5896 decision = find_check_comp_options(param);
5901 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
5902 decision = find_check_mirror_options(param);
5907 /* Check the time on mds. */
5909 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
5912 for_mds = lustre_fs ?
5913 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5914 decision = find_time_check(param, for_mds);
5919 if (param->fp_btime) {
5920 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
5925 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
5926 param->fp_btime, param->fp_bsign,
5927 param->fp_exclude_btime,
5928 param->fp_time_margin, 0);
5933 if (param->fp_newerxy) {
5936 for_mds = lustre_fs ?
5937 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5938 decision = find_newerxy_check(param, for_mds, true);
5947 if (param->fp_attrs || param->fp_neg_attrs) {
5948 decision = find_check_attr_options(param);
5953 flags = param->fp_lmd->lmd_flags;
5954 if (param->fp_check_size &&
5955 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5956 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5957 !(flags & OBD_MD_FLSIZE ||
5958 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
5961 if (param->fp_check_blocks &&
5962 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5963 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5964 !(flags & OBD_MD_FLBLOCKS ||
5965 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
5968 if (param->fp_xattr_match_info) {
5969 decision = find_check_xattrs(path, param->fp_xattr_match_info);
5975 * When checking nlink, stat(2) is needed for multi-striped directories
5976 * because the nlink value retrieved from the MDS above comes from
5977 * the number of stripes for the dir.
5978 * The posix stat call below fills in the correct number of links.
5979 * Single-stripe directories and regular files already have the
5980 * correct nlink value.
5982 if (param->fp_nlink && S_ISDIR(lmd->lmd_stx.stx_mode) &&
5983 (param->fp_lmv_md->lum_stripe_count != 0))
5987 * If file still fits the request, ask ost for updated info.
5988 * The regular stat is almost of the same speed as some new
5989 * 'glimpse-size-ioctl'.
5991 if (!decision || gather_all) {
5995 * For regular files with the stripe the decision may have not
5996 * been taken yet if *time or size is to be checked.
5998 if (param->fp_obd_index != OBD_NOT_FOUND)
5999 print_failed_tgt(param, path, LL_STATFS_LOV);
6001 if (param->fp_mdt_index != OBD_NOT_FOUND)
6002 print_failed_tgt(param, path, LL_STATFS_LMV);
6005 ret = fstat_f(d, &st);
6006 else if (de != NULL)
6007 ret = fstatat_f(p, de->d_name, &st,
6008 AT_SYMLINK_NOFOLLOW);
6010 ret = lstat_f(path, &st);
6013 if (errno == ENOENT) {
6014 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
6015 "warning: %s: %s does not exist",
6020 llapi_error(LLAPI_MSG_ERROR, ret,
6021 "%s: stat on %s failed",
6027 convert_lmd_statx(param->fp_lmd, &st, true);
6028 /* Check the time on osc. */
6029 decision = find_time_check(param, 0);
6033 if (param->fp_newerxy) {
6034 decision = find_newerxy_check(param, 0, false);
6044 if (param->fp_nlink) {
6045 decision = find_value_cmp(lmd->lmd_stx.stx_nlink,
6046 param->fp_nlink, param->fp_nlink_sign,
6047 param->fp_exclude_nlink, 1, 0);
6052 if (param->fp_check_size) {
6053 decision = find_value_cmp(lmd->lmd_stx.stx_size,
6055 param->fp_size_sign,
6056 param->fp_exclude_size,
6057 param->fp_size_units, 0);
6062 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
6063 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
6065 param->fp_blocks_sign,
6066 param->fp_exclude_blocks,
6067 param->fp_blocks_units, 0);
6073 if (param->fp_format_printf_str)
6074 printf_format_string(param, path, projid, d);
6076 llapi_printf(LLAPI_MSG_NORMAL, "%s%c", path,
6077 param->fp_zero_end ? '\0' : '\n');
6081 /* Do not get down anymore? */
6082 if (param->fp_depth == param->fp_max_depth) {
6093 static int cb_migrate_mdt_init(char *path, int p, int *dp,
6094 void *param_data, struct dirent64 *de)
6096 struct find_param *param = (struct find_param *)param_data;
6097 struct lmv_user_md *lmu = param->fp_lmv_md;
6099 char raw[MAX_IOC_BUFLEN] = {'\0'};
6101 struct obd_ioctl_data data = { 0 };
6107 if (p == -1 && dp == NULL)
6113 if (dp != NULL && *dp != -1)
6117 tmp_p = open_parent(path);
6121 llapi_error(LLAPI_MSG_ERROR, ret,
6122 "can not open %s", path);
6127 path_copy = strdup(path);
6128 filename = basename(path_copy);
6130 data.ioc_inlbuf1 = (char *)filename;
6131 data.ioc_inllen1 = strlen(filename) + 1;
6132 data.ioc_inlbuf2 = (char *)lmu;
6133 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
6136 if (param->fp_depth == param->fp_max_depth)
6137 data.ioc_type = MDS_MIGRATE_NSONLY;
6138 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
6140 llapi_error(LLAPI_MSG_ERROR, ret,
6141 "%s: error packing ioctl data", __func__);
6146 ret = ioctl(tmp_p, LL_IOC_MIGRATE, rawbuf);
6148 if (errno == EBUSY && !retry) {
6150 * because migrate may not be able to lock all involved
6151 * objects in order, for some of them it try lock, while
6152 * there may be conflicting COS locks and cause migrate
6153 * fail with EBUSY, hope a sync() could cause
6154 * transaction commit and release these COS locks.
6159 } else if (errno == EALREADY) {
6160 if (param->fp_verbose & VERBOSE_DETAIL)
6161 llapi_printf(LLAPI_MSG_NORMAL,
6162 "%s migrated to MDT%d already\n",
6163 path, lmu->lum_stripe_offset);
6167 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
6171 } else if (param->fp_verbose & VERBOSE_DETAIL) {
6172 llapi_printf(LLAPI_MSG_NORMAL,
6173 "migrate %s to MDT%d stripe count %d\n",
6174 path, lmu->lum_stripe_offset,
6175 lmu->lum_stripe_count);
6179 /* Do not get down anymore? */
6180 if (param->fp_depth == param->fp_max_depth)
6187 * If the directory is being migration, we need
6188 * close the directory after migration,
6189 * so the old directory cache will be cleanup
6190 * on the client side, and re-open to get the
6191 * new directory handle
6193 *dp = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
6196 llapi_error(LLAPI_MSG_ERROR, ret,
6197 "%s: Failed to open '%s'", __func__, path);
6209 /* dir migration finished, shrink its stripes */
6210 static int cb_migrate_mdt_fini(char *path, int p, int *dp, void *data,
6211 struct dirent64 *de)
6213 struct find_param *param = data;
6214 struct lmv_user_md *lmu = param->fp_lmv_md;
6215 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
6218 if (de && de->d_type != DT_DIR)
6223 * close it before setxattr because the latter may destroy the
6224 * original object, and cause close fail.
6232 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
6233 if (ret == -EALREADY)
6236 cb_common_fini(path, p, dp, data, de);
6240 int llapi_migrate_mdt(char *path, struct find_param *param)
6242 param->fp_stop_on_error = 1;
6243 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
6247 int llapi_mv(char *path, struct find_param *param)
6249 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
6250 static bool printed;
6253 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
6254 "%s() is deprecated, use llapi_migrate_mdt() instead",
6259 return llapi_migrate_mdt(path, param);
6263 * Check string for escape sequences and print a message to stdout
6264 * if any invalid escapes are found.
6266 * @param[in] c Pointer to character immediately following the
6267 * '\' character indicating the start of an escape
6269 * @return Number of characters examined in the escape sequence
6270 * (regardless of whether the sequence is valid or not).
6272 static int validate_printf_esc(char *c)
6274 char *valid_esc = "nt\\";
6277 /* backslash at end of string */
6278 llapi_err_noerrno(LLAPI_MSG_WARN,
6279 "warning: '\\' at end of -printf format string\n");
6283 if (!strchr(valid_esc, *c))
6284 /* Invalid escape character */
6285 llapi_err_noerrno(LLAPI_MSG_WARN,
6286 "warning: unrecognized escape: '\\%c'\n", *c);
6292 * Check string for format directives and print a message to stdout
6293 * if any invalid directives are found.
6295 * @param[in] c Pointer to character immediately following the
6296 * '%' character indicating the start of a format
6298 * @return Number of characters examined in the format directive
6299 * (regardless of whether the directive is valid or not).
6301 static int validate_printf_fmt(char *c)
6303 char *valid_fmt_single = "abcGkmnpstUwy%";
6304 char *valid_fmt_double = "ACTW";
6305 char *valid_fmt_lustre = "aAcFhioPpS";
6306 char curr = *c, next;
6309 llapi_err_noerrno(LLAPI_MSG_WARN,
6310 "warning: '%%' at end of -printf format string\n");
6315 if ((next == '\0') || (next == '%') || (next == '\\'))
6316 /* Treat as single char format directive */
6319 /* Check format directives with multiple characters */
6320 if (strchr(valid_fmt_double, curr)) {
6321 /* For now, only valid formats are followed by '@' char */
6323 llapi_err_noerrno(LLAPI_MSG_WARN,
6324 "warning: unrecognized format directive: '%%%c%c'\n",
6329 /* Lustre formats always start with 'L' */
6331 if (!strchr(valid_fmt_lustre, next))
6332 llapi_err_noerrno(LLAPI_MSG_WARN,
6333 "warning: unrecognized format directive: '%%%c%c'\n",
6340 if (!strchr(valid_fmt_single, curr))
6341 llapi_err_noerrno(LLAPI_MSG_WARN,
6342 "warning: unrecognized format directive: '%%%c'\n", curr);
6347 * Validate the user-supplied string for the -printf option and report
6348 * any invalid backslash escape sequences or format directives.
6350 * @param[in] param Structure containing info about invocation of lfs find
6353 static void validate_printf_str(struct find_param *param)
6355 char *c = param->fp_format_printf_str;
6361 ret = validate_printf_fmt(++c);
6365 ret = validate_printf_esc(++c);
6375 int llapi_find(char *path, struct find_param *param)
6377 if (param->fp_format_printf_str)
6378 validate_printf_str(param);
6379 return param_callback(path, cb_find_init, cb_common_fini, param);
6383 * Get MDT number that the file/directory inode referenced
6384 * by the open fd resides on.
6385 * Return 0 and mdtidx on success, or -ve errno.
6387 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
6389 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
6394 static int cb_get_mdt_index(char *path, int p, int *dp, void *data,
6395 struct dirent64 *de)
6397 struct find_param *param = (struct find_param *)data;
6398 int d = dp == NULL ? -1 : *dp;
6401 bool hex = param->fp_hex_idx;
6403 if (p == -1 && d == -1)
6407 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6408 } else /* if (p != -1) */ {
6411 fd = open(path, O_RDONLY | O_NOCTTY);
6413 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
6421 if (ret == -ENODATA) {
6422 if (!param->fp_obd_uuid)
6423 llapi_printf(LLAPI_MSG_NORMAL,
6424 "'%s' has no stripe info\n", path);
6426 } else if (ret == -ENOENT) {
6427 llapi_error(LLAPI_MSG_WARN, ret,
6428 "warning: %s: '%s' does not exist",
6431 } else if (ret == -ENOTTY) {
6432 llapi_error(LLAPI_MSG_ERROR, ret,
6433 "%s: '%s' not on a Lustre fs",
6436 llapi_error(LLAPI_MSG_ERROR, ret,
6437 "error: %s: '%s' failed get_mdtidx",
6443 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
6444 llapi_printf(LLAPI_MSG_NORMAL, hex ? "%#x\n" : "%d\n", mdtidx);
6446 llapi_printf(LLAPI_MSG_NORMAL, hex ? "%s\nmdt_index:\t%#x\n"
6447 : "%s\nmdt_index:\t%d\n",
6451 /* Do not go down anymore? */
6452 if (param->fp_depth == param->fp_max_depth)
6460 static int cb_getstripe(char *path, int p, int *dp, void *data,
6461 struct dirent64 *de)
6463 struct find_param *param = (struct find_param *)data;
6464 int d = dp == NULL ? -1 : *dp, fd = -1;
6467 if (p == -1 && d == -1)
6470 if (param->fp_obd_uuid) {
6471 param->fp_quiet = 1;
6472 ret = setup_obd_uuid(d != -1 ? d : p, path, param);
6477 if (!param->fp_no_follow && de && de->d_type == DT_LNK && d == -1)
6478 d = fd = open(path, O_RDONLY | O_DIRECTORY);
6480 if (d != -1 && (param->fp_get_lmv || param->fp_get_default_lmv))
6481 ret = cb_get_dirstripe(path, &d, param);
6483 ret = get_lmd_info_fd(path, p, d, ¶m->fp_lmd->lmd_lmm,
6484 param->fp_lum_size, GET_LMD_STRIPE);
6485 else if (d == -1 && (param->fp_get_lmv || param->fp_get_default_lmv)) {
6486 /* in case of a dangling or valid faked symlink dir, opendir()
6487 * should have return either EINVAL or ENOENT, so let's try
6488 * to get LMV just in case, and by opening it as a file but
6489 * with O_NOFOLLOW ...
6491 int flag = O_RDONLY;
6493 if (param->fp_no_follow)
6494 flag = O_RDONLY | O_NOFOLLOW;
6495 fd = open(path, flag);
6499 ret = cb_get_dirstripe(path, &fd, param);
6501 llapi_lov_dump_user_lmm(param, path, LDF_IS_DIR);
6504 } else if (d == -1) {
6505 if (!param->fp_no_follow && de && de->d_type == DT_LNK) {
6506 /* open the target of symlink as a file */
6507 fd = open(path, O_RDONLY);
6511 ret = get_lmd_info_fd(path, p, fd, ¶m->fp_lmd->lmd_lmm,
6512 param->fp_lum_size, GET_LMD_STRIPE);
6520 if (errno == ENODATA && d != -1) {
6522 * We need to "fake" the "use the default" values
6523 * since the lmm struct is zeroed out at this point.
6524 * The magic needs to be set in order to satisfy
6525 * a check later on in the code path.
6526 * The object_seq needs to be set for the "(Default)"
6527 * prefix to be displayed.
6529 if (param->fp_get_default_lmv) {
6530 struct lmv_user_md *lum = param->fp_lmv_md;
6534 lum->lum_magic = LMV_USER_MAGIC;
6535 lum->lum_stripe_count = 0;
6536 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
6538 } else if (param->fp_get_lmv) {
6539 struct lmv_user_md *lum = param->fp_lmv_md;
6542 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6545 lum->lum_magic = LMV_MAGIC_V1;
6546 lum->lum_stripe_count = 0;
6547 lum->lum_stripe_offset = mdtidx;
6550 struct lov_user_md *lmm =
6551 ¶m->fp_lmd->lmd_lmm;
6553 lmm->lmm_magic = LOV_USER_MAGIC_V1;
6555 ostid_set_seq(&lmm->lmm_oi,
6556 FID_SEQ_LOV_DEFAULT);
6557 lmm->lmm_stripe_count = 0;
6558 lmm->lmm_stripe_size = 0;
6559 lmm->lmm_stripe_offset = -1;
6562 } else if (errno == ENODATA && p != -1) {
6563 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
6564 llapi_printf(LLAPI_MSG_NORMAL,
6565 "%s has no stripe info\n", path);
6567 } else if (errno == ENOENT) {
6568 llapi_error(LLAPI_MSG_WARN, -ENOENT,
6569 "warning: %s: %s does not exist",
6572 } else if (errno == ENOTTY) {
6574 llapi_error(LLAPI_MSG_ERROR, ret,
6575 "%s: '%s' not on a Lustre fs?",
6580 llapi_error(LLAPI_MSG_ERROR, ret,
6581 "error: %s: %s failed for %s",
6583 "LL_IOC_LOV_GETSTRIPE" :
6584 "IOC_MDC_GETFILESTRIPE", path);
6591 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
6592 llapi_lov_dump_user_lmm(param, path, d != -1 ? LDF_IS_DIR : 0);
6595 /* Do not get down anymore? */
6596 if (param->fp_depth == param->fp_max_depth)
6604 int llapi_getstripe(char *path, struct find_param *param)
6606 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
6607 cb_get_mdt_index : cb_getstripe,
6608 cb_common_fini, param);
6611 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
6612 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6614 char raw[MAX_IOC_BUFLEN] = {'\0'};
6616 struct obd_ioctl_data data = { 0 };
6619 data.ioc_inlbuf1 = (char *)&type;
6620 data.ioc_inllen1 = sizeof(__u32);
6621 data.ioc_inlbuf2 = (char *)&index;
6622 data.ioc_inllen2 = sizeof(__u32);
6623 data.ioc_pbuf1 = (char *)stat_buf;
6624 data.ioc_plen1 = sizeof(struct obd_statfs);
6625 data.ioc_pbuf2 = (char *)uuid_buf;
6626 data.ioc_plen2 = sizeof(struct obd_uuid);
6628 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
6630 llapi_error(LLAPI_MSG_ERROR, rc,
6631 "%s: error packing ioctl data", __func__);
6635 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
6637 return rc < 0 ? -errno : 0;
6640 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
6641 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6646 fd = open(path, O_RDONLY);
6649 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
6652 * If we can't even open a file on the filesystem (e.g. with
6653 * -ESHUTDOWN), force caller to exit or it will loop forever.
6658 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
6665 #define MAX_STRING_SIZE 128
6667 int llapi_ping(char *obd_type, char *obd_name)
6669 int flags = O_RDONLY;
6670 char buf[1] = { 0 };
6674 rc = cfs_get_param_paths(&path, "%s/%s/ping",
6675 obd_type, obd_name);
6679 fd = open(path.gl_pathv[0], flags);
6681 if (errno == EACCES && flags == O_RDONLY) {
6686 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
6691 if (flags == O_RDONLY)
6692 rc = read(fd, buf, sizeof(buf));
6694 rc = write(fd, buf, sizeof(buf));
6702 cfs_free_param_data(&path);
6706 int llapi_target_iterate(int type_num, char **obd_type,
6707 void *args, llapi_cb_t cb)
6713 for (i = 0; i < type_num; i++) {
6716 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
6720 for (j = 0; j < param.gl_pathc; j++) {
6721 char obd_uuid[UUID_MAX + 1];
6725 fp = fopen(param.gl_pathv[j], "r");
6728 llapi_error(LLAPI_MSG_ERROR, rc,
6729 "error: opening '%s'",
6734 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
6736 llapi_error(LLAPI_MSG_ERROR, rc,
6737 "error: reading '%s'",
6742 /* Extract the obd_name from the sysfs path.
6743 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
6745 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
6751 /* skip /fs/lustre/'obd_type'/ */
6752 obd_name += strlen(obd_type[i]) + 12;
6753 /* chop off after obd_name */
6754 ptr = strrchr(obd_name, '/');
6758 cb(obd_type[i], obd_name, obd_uuid, args);
6763 cfs_free_param_data(¶m);
6768 cfs_free_param_data(¶m);
6772 struct check_target_filter {
6777 static void do_target_check(char *obd_type_name, char *obd_name,
6778 char *obd_uuid, void *args)
6781 struct check_target_filter *filter = args;
6783 if (filter != NULL) {
6784 /* check nid if obd type is mgc */
6785 if (strcmp(obd_type_name, "mgc") == 0) {
6786 if (strcmp(obd_name + 3, filter->nid) != 0)
6789 /* check instance for other types of device (osc/mdc) */
6790 else if (strstr(obd_name, filter->instance) == NULL)
6794 rc = llapi_ping(obd_type_name, obd_name);
6796 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
6798 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
6800 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
6803 int llapi_target_check(int type_num, char **obd_type, char *dir)
6805 char nid[MAX_LINE_LEN], instance[MAX_INSTANCE_LEN];
6806 struct check_target_filter filter = {NULL, NULL};
6809 if (dir == NULL || dir[0] == '\0')
6810 return llapi_target_iterate(type_num, obd_type, NULL,
6813 rc = get_root_path(WANT_NID | WANT_ERROR, NULL, NULL, dir, -1, NULL,
6816 llapi_error(LLAPI_MSG_ERROR, rc,
6817 "cannot get nid of path '%s'", dir);
6822 rc = llapi_get_instance(dir, instance, ARRAY_SIZE(instance));
6825 filter.instance = instance;
6827 return llapi_target_iterate(type_num, obd_type, &filter,
6831 #undef MAX_STRING_SIZE
6833 /* Is this a lustre fs? */
6834 int llapi_is_lustre_mnttype(const char *type)
6836 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
6839 /* Is this a lustre client fs? */
6840 int llapi_is_lustre_mnt(struct mntent *mnt)
6842 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
6843 strstr(mnt->mnt_fsname, ":/") != NULL);
6846 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
6848 char fsname[PATH_MAX + 1];
6852 rc = llapi_search_fsname(mnt, fsname);
6856 root = open(mnt, O_RDONLY | O_DIRECTORY);
6859 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
6863 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
6866 if (rc == -ENOENT && LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd))
6867 llapi_error(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, rc,
6868 "Cannot find pool '%s'", qctl->qc_poolname);
6874 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
6879 root = open(mnt, O_RDONLY | O_DIRECTORY);
6882 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
6886 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
6889 llapi_error(LLAPI_MSG_ERROR, rc,
6890 "ioctl on %s for getting connect flags failed", mnt);
6897 * Flush cached pages from all clients.
6899 * \param fd File descriptor
6903 int llapi_file_flush(int fd)
6907 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);