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 if (!lov_pool_is_ignored((const char *) *pool_name)) {
428 /* Make sure the pool exists */
429 rc = llapi_search_ost(fsname, *pool_name, NULL);
431 llapi_error(LLAPI_MSG_ERROR, rc,
432 "pool '%s fsname %s' does not exist",
445 static int dir_stripe_limit_check(int stripe_offset, int stripe_count,
450 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
452 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
456 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
458 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
463 if (!llapi_dir_hash_type_is_valid(hash_type)) {
465 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
473 * Trim a trailing newline from a string, if it exists.
475 int llapi_chomp_string(char *buf)
491 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
493 static int get_param_lov(const char *path, const char *param,
494 char *buf, size_t buf_size)
496 struct obd_uuid uuid;
499 rc = llapi_file_get_lov_uuid(path, &uuid);
503 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
508 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
510 static int get_param_lmv(const char *path, const char *param,
511 char *buf, size_t buf_size)
513 struct obd_uuid uuid;
516 rc = llapi_file_get_lmv_uuid(path, &uuid);
520 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
524 static int get_mds_md_size(const char *path)
526 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
529 * Rather than open the file and do the ioctl to get the
530 * instance name and close the file and search for the param
531 * file and open the param file and read the param file and
532 * parse the value and close the param file, let's just return
533 * a large enough value. It's 2020, RAM is cheap and this is
537 if (md_size < XATTR_SIZE_MAX)
538 md_size = XATTR_SIZE_MAX;
543 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
545 return get_param_lmv(path, "uuid", buf, bufsize);
549 * Open a Lustre file.
551 * \param name the name of the file to be opened
552 * \param flags access mode, see flags in open(2)
553 * \param mode permission of the file if it is created, see mode in open(2)
554 * \param param stripe pattern of the newly created file
556 * \retval file descriptor of opened file
557 * \retval negative errno on failure
559 int llapi_file_open_param(const char *name, int flags, mode_t mode,
560 const struct llapi_stripe_param *param)
562 char fsname[MAX_OBD_NAME + 1] = { 0 };
563 struct lov_user_md *lum = NULL;
564 const char *pool_name = param->lsp_pool;
568 /* Make sure we are on a Lustre file system */
569 if (pool_name && !lov_pool_is_ignored(pool_name)) {
570 rc = llapi_search_fsname(name, fsname);
572 llapi_error(LLAPI_MSG_ERROR, rc,
573 "'%s' is not on a Lustre filesystem", name);
578 /* Check if the stripe pattern is sane. */
579 rc = llapi_stripe_param_verify(param, &pool_name, fsname);
583 if (param->lsp_is_specific)
584 lum_size = lov_user_md_size(param->lsp_stripe_count,
585 LOV_USER_MAGIC_SPECIFIC);
587 lum_size = sizeof(struct lov_user_md_v3);
589 lum_size = sizeof(*lum);
591 lum = calloc(1, lum_size);
596 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
598 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
599 flags = O_DIRECTORY | O_RDONLY;
606 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
611 /* Initialize IOCTL striping pattern structure */
612 lum->lmm_magic = LOV_USER_MAGIC_V1;
613 lum->lmm_pattern = param->lsp_stripe_pattern;
614 lum->lmm_stripe_size = param->lsp_stripe_size;
615 lum->lmm_stripe_count = param->lsp_stripe_count;
616 lum->lmm_stripe_offset = param->lsp_stripe_offset;
617 if (pool_name != NULL) {
618 struct lov_user_md_v3 *lumv3 = (void *)lum;
620 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
621 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
623 if (param->lsp_is_specific) {
624 struct lov_user_md_v3 *lumv3 = (void *)lum;
627 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
628 if (pool_name == NULL) {
630 * LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
631 * OST list, therefore if pool is not specified we have
632 * to pack a null pool name for placeholder.
634 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
637 for (i = 0; i < param->lsp_stripe_count; i++)
638 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
641 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
642 char errmsg[512] = "stripe already set";
645 if (errno != EEXIST && errno != EALREADY)
646 strncpy(errmsg, strerror(errno), sizeof(errmsg) - 1);
647 if (rc == -EREMOTEIO)
648 snprintf(errmsg, sizeof(errmsg),
649 "inactive OST among your specified %d OST(s)",
650 param->lsp_stripe_count);
652 llapi_err_noerrno(LLAPI_MSG_ERROR,
653 "setstripe error for '%s': %s", name, errmsg);
664 int llapi_file_is_encrypted(int fd)
669 rc = ioctl(fd, FS_IOC_GETFLAGS, &flags);
673 return !!(flags & LUSTRE_ENCRYPT_FL);
676 int llapi_file_open_pool(const char *name, int flags, int mode,
677 unsigned long long stripe_size, int stripe_offset,
678 int stripe_count, int stripe_pattern, char *pool_name)
680 const struct llapi_stripe_param param = {
681 .lsp_stripe_size = stripe_size,
682 .lsp_stripe_count = stripe_count,
683 .lsp_stripe_pattern = stripe_pattern,
684 .lsp_stripe_offset = stripe_offset,
685 .lsp_pool = pool_name
687 return llapi_file_open_param(name, flags, mode, ¶m);
690 int llapi_file_open(const char *name, int flags, int mode,
691 unsigned long long stripe_size, int stripe_offset,
692 int stripe_count, int stripe_pattern)
694 return llapi_file_open_pool(name, flags, mode, stripe_size,
695 stripe_offset, stripe_count,
696 stripe_pattern, NULL);
699 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
700 __u32 flags, char *foreign_lov)
703 struct lov_foreign_md *lfm;
706 if (foreign_lov == NULL) {
708 llapi_error(LLAPI_MSG_ERROR, rc,
709 "foreign LOV EA content must be provided");
713 len = strlen(foreign_lov);
714 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
717 llapi_error(LLAPI_MSG_ERROR, rc,
718 "foreign LOV EA size %zu (must be 0 < len < %zu)",
719 len, XATTR_SIZE_MAX -
720 offsetof(struct lov_foreign_md, lfm_value));
724 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
727 llapi_error(LLAPI_MSG_ERROR, rc,
728 "failed to allocate lov_foreign_md");
732 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
735 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
739 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
740 lfm->lfm_length = len;
741 lfm->lfm_type = type;
742 lfm->lfm_flags = flags;
743 memcpy(lfm->lfm_value, foreign_lov, len);
745 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
746 char *errmsg = "stripe already set";
750 errmsg = "not on a Lustre filesystem";
751 else if (errno == EEXIST || errno == EALREADY)
752 errmsg = "stripe already set";
754 errmsg = strerror(errno);
756 llapi_err_noerrno(LLAPI_MSG_ERROR,
757 "setstripe error for '%s': %s", name, errmsg);
773 int llapi_file_create(const char *name, unsigned long long stripe_size,
774 int stripe_offset, int stripe_count, int stripe_pattern)
778 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
779 stripe_offset, stripe_count, stripe_pattern,
788 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
789 int stripe_offset, int stripe_count,
790 int stripe_pattern, char *pool_name)
794 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
795 stripe_offset, stripe_count, stripe_pattern,
804 static int verify_dir_param(const char *name,
805 const struct llapi_stripe_param *param)
807 char fsname[MAX_OBD_NAME + 1] = { 0 };
808 char *pool_name = param->lsp_pool;
811 /* Make sure we are on a Lustre file system */
812 rc = llapi_search_fsname(name, fsname);
814 llapi_error(LLAPI_MSG_ERROR, rc,
815 "'%s' is not on a Lustre filesystem",
820 /* Check if the stripe pattern is sane. */
821 rc = dir_stripe_limit_check(param->lsp_stripe_offset,
822 param->lsp_stripe_count,
823 param->lsp_stripe_pattern);
827 /* Make sure we have a good pool */
828 if (pool_name != NULL) {
830 * in case user gives the full pool name <fsname>.<poolname>,
833 char *ptr = strchr(pool_name, '.');
837 if (strcmp(pool_name, fsname) != 0) {
839 llapi_err_noerrno(LLAPI_MSG_ERROR,
840 "Pool '%s' is not on filesystem '%s'",
847 /* Make sure the pool exists and is non-empty */
848 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
850 char *err = rc == 0 ? "has no OSTs" : "does not exist";
852 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
853 fsname, pool_name, err);
858 /* sanity check of target list */
859 if (param->lsp_is_specific) {
860 char mdtname[MAX_OBD_NAME + 64];
864 for (i = 0; i < param->lsp_stripe_count; i++) {
865 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
866 fsname, param->lsp_tgts[i]);
867 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
872 llapi_error(LLAPI_MSG_ERROR, rc,
873 "%s: cannot find MDT %s in %s",
880 /* Make sure stripe offset is in MDT list. */
881 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
885 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
886 "%s: stripe offset '%d' is not in the target list",
887 __func__, param->lsp_stripe_offset);
895 static inline void param2lmu(struct lmv_user_md *lmu,
896 const struct llapi_stripe_param *param)
898 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
900 lmu->lum_stripe_count = param->lsp_stripe_count;
901 lmu->lum_stripe_offset = param->lsp_stripe_offset;
902 lmu->lum_hash_type = param->lsp_stripe_pattern;
903 lmu->lum_max_inherit = param->lsp_max_inherit;
904 lmu->lum_max_inherit_rr = param->lsp_max_inherit_rr;
905 if (param->lsp_pool != NULL)
906 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
907 if (param->lsp_is_specific) {
910 for (i = 0; i < param->lsp_stripe_count; i++)
911 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
915 int llapi_dir_set_default_lmv(const char *name,
916 const struct llapi_stripe_param *param)
918 struct lmv_user_md lmu = { 0 };
922 rc = verify_dir_param(name, param);
926 /* TODO: default lmv doesn't support specific targets yet */
927 if (param->lsp_is_specific)
930 param2lmu(&lmu, param);
932 fd = open(name, O_DIRECTORY | O_RDONLY);
935 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
939 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
941 char *errmsg = "stripe already set";
944 if (errno != EEXIST && errno != EALREADY)
945 errmsg = strerror(errno);
947 llapi_err_noerrno(LLAPI_MSG_ERROR,
948 "default dirstripe error on '%s': %s",
955 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
956 int stripe_count, int stripe_pattern,
957 const char *pool_name)
959 const struct llapi_stripe_param param = {
960 .lsp_stripe_count = stripe_count,
961 .lsp_stripe_offset = stripe_offset,
962 .lsp_stripe_pattern = stripe_pattern,
963 .lsp_pool = (char *)pool_name
966 return llapi_dir_set_default_lmv(name, ¶m);
970 * Create a Lustre directory.
972 * \param name the name of the directory to be created
973 * \param mode permission of the file if it is created, see mode in open(2)
974 * \param param stripe pattern of the newly created directory
976 * \retval 0 on success
977 * \retval negative errno on failure
979 int llapi_dir_create(const char *name, mode_t mode,
980 const struct llapi_stripe_param *param)
982 struct lmv_user_md *lmu = NULL;
984 struct obd_ioctl_data data = { 0 };
987 char *dirpath = NULL;
988 char *namepath = NULL;
993 rc = verify_dir_param(name, param);
997 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
998 param->lsp_is_specific ?
999 LMV_USER_MAGIC_SPECIFIC :
1002 lmu = calloc(1, lmu_size);
1006 dirpath = strdup(name);
1012 namepath = strdup(name);
1019 param2lmu(lmu, param);
1021 filename = basename(namepath);
1022 dir = dirname(dirpath);
1024 data.ioc_inlbuf1 = (char *)filename;
1025 data.ioc_inllen1 = strlen(filename) + 1;
1026 data.ioc_inlbuf2 = (char *)lmu;
1027 data.ioc_inllen2 = lmu_size;
1028 data.ioc_type = mode;
1029 if (param->lsp_is_create)
1030 /* borrow obdo1.o_flags to store this flag */
1031 data.ioc_obdo1.o_flags = OBD_FL_OBDMDEXISTS;
1032 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1034 llapi_error(LLAPI_MSG_ERROR, rc,
1035 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1040 fd = open(dir, O_DIRECTORY | O_RDONLY);
1043 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1047 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1048 char *errmsg = "stripe already set";
1051 if (errno != EEXIST && errno != EALREADY)
1052 errmsg = strerror(errno);
1054 llapi_err_noerrno(LLAPI_MSG_ERROR,
1055 "dirstripe error on '%s': %s", name, errmsg);
1066 * Create a foreign directory.
1068 * \param name the name of the directory to be created
1069 * \param mode permission of the file if it is created, see mode in open(2)
1070 * \param type foreign type to be set in LMV EA
1071 * \param flags foreign flags to be set in LMV EA
1072 * \param value foreign pattern to be set in LMV EA
1074 * \retval 0 on success
1075 * \retval negative errno on failure
1077 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1078 __u32 flags, const char *value)
1080 struct lmv_foreign_md *lfm = NULL;
1081 size_t lfm_size, len;
1082 struct obd_ioctl_data data = { 0 };
1085 char *dirpath = NULL;
1086 char *namepath = NULL;
1091 len = strlen(value);
1092 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1095 llapi_error(LLAPI_MSG_ERROR, rc,
1096 "invalid LOV EA length %zu (must be 0 < len < %zu)",
1097 len, XATTR_SIZE_MAX -
1098 offsetof(struct lmv_foreign_md, lfm_value));
1101 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1102 lfm = calloc(1, lfm_size);
1106 dirpath = strdup(name);
1112 namepath = strdup(name);
1119 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1120 lfm->lfm_length = len;
1121 lfm->lfm_type = type;
1122 lfm->lfm_flags = flags;
1123 memcpy(lfm->lfm_value, value, len);
1125 filename = basename(namepath);
1126 dir = dirname(dirpath);
1128 data.ioc_inlbuf1 = (char *)filename;
1129 data.ioc_inllen1 = strlen(filename) + 1;
1130 data.ioc_inlbuf2 = (char *)lfm;
1131 data.ioc_inllen2 = lfm_size;
1132 data.ioc_type = mode;
1133 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1135 llapi_error(LLAPI_MSG_ERROR, rc,
1136 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1141 fd = open(dir, O_DIRECTORY | O_RDONLY);
1144 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1148 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1149 char *errmsg = "stripe already set";
1152 if (errno != EEXIST && errno != EALREADY)
1153 errmsg = strerror(errno);
1155 llapi_err_noerrno(LLAPI_MSG_ERROR,
1156 "dirstripe error on '%s': %s", name, errmsg);
1166 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1167 int stripe_count, int stripe_pattern,
1168 const char *pool_name)
1170 const struct llapi_stripe_param param = {
1171 .lsp_stripe_count = stripe_count,
1172 .lsp_stripe_offset = stripe_offset,
1173 .lsp_stripe_pattern = stripe_pattern,
1174 .lsp_pool = (char *)pool_name
1177 return llapi_dir_create(name, mode, ¶m);
1181 * Get the list of pool members.
1182 * \param poolname string of format \<fsname\>.\<poolname\>
1183 * \param members caller-allocated array of char*
1184 * \param list_size size of the members array
1185 * \param buffer caller-allocated buffer for storing OST names
1186 * \param buffer_size size of the buffer
1188 * \return number of members retrieved for this pool
1189 * \retval -error failure
1191 int llapi_get_poolmembers(const char *poolname, char **members,
1192 int list_size, char *buffer, int buffer_size)
1194 char fsname[PATH_MAX];
1203 /* name is FSNAME.POOLNAME */
1204 if (strlen(poolname) >= sizeof(fsname))
1207 snprintf(fsname, sizeof(fsname), "%s", poolname);
1208 pool = strchr(fsname, '.');
1215 rc = poolpath(&pathname, fsname, NULL);
1217 llapi_error(LLAPI_MSG_ERROR, rc,
1218 "Lustre filesystem '%s' not found",
1223 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1224 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1225 cfs_free_param_data(&pathname);
1226 if (rc >= sizeof(buf))
1228 fd = fopen(buf, "r");
1231 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1236 while (fgets(buf, sizeof(buf), fd) != NULL) {
1237 if (nb_entries >= list_size) {
1241 buf[sizeof(buf) - 1] = '\0';
1243 tmp = strchr(buf, '\n');
1246 if (used + strlen(buf) + 1 > buffer_size) {
1251 strcpy(buffer + used, buf);
1252 members[nb_entries] = buffer + used;
1253 used += strlen(buf) + 1;
1263 * Get the list of pools in a filesystem.
1264 * \param name filesystem name or path
1265 * \param poollist caller-allocated array of char*
1266 * \param list_size size of the poollist array
1267 * \param buffer caller-allocated buffer for storing pool names
1268 * \param buffer_size size of the buffer
1270 * \return number of pools retrieved for this filesystem
1271 * \retval -error failure
1273 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1274 char *buffer, int buffer_size)
1280 struct dirent *pool;
1282 unsigned int nb_entries = 0;
1283 unsigned int used = 0;
1286 /* initialize output array */
1287 for (i = 0; i < list_size; i++)
1290 /* is name a pathname ? */
1291 ptr = strchr(name, '/');
1293 char fsname_buf[MAXNAMLEN];
1295 /* We will need fsname for printing later */
1296 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1300 ptr = strrchr(fsname_buf, '-');
1304 fsname = strdup(fsname_buf);
1308 /* name is FSNAME */
1309 fsname = strdup(name);
1314 rc = poolpath(&pathname, fsname, NULL);
1316 llapi_error(LLAPI_MSG_ERROR, rc,
1317 "Lustre filesystem '%s' not found", name);
1321 dir = opendir(pathname.gl_pathv[0]);
1324 llapi_error(LLAPI_MSG_ERROR, rc,
1325 "Could not open pool list for '%s'",
1332 pool = readdir(dir);
1338 /* ignore . and .. */
1339 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1342 /* check output bounds */
1343 if (nb_entries >= list_size) {
1345 goto free_dir_no_msg;
1348 /* +2 for '.' and final '\0' */
1349 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1352 goto free_dir_no_msg;
1355 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1356 poollist[nb_entries] = buffer + used;
1357 used += strlen(pool->d_name) + strlen(fsname) + 2;
1363 llapi_error(LLAPI_MSG_ERROR, rc,
1364 "Error reading pool list for '%s'", name);
1366 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1371 cfs_free_param_data(&pathname);
1374 return rc != 0 ? rc : nb_entries;
1377 /* wrapper for lfs.c and obd.c */
1378 int llapi_poollist(const char *name)
1380 int poolcount, rc, i;
1383 rc = llapi_get_poolbuf(name, &buf, &pools, &poolcount);
1387 for (i = 0; i < poolcount; i++)
1388 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", pools[i]);
1395 * Get buffer that holds uuids and the list of pools in a filesystem.
1397 * \param name filesystem name or path
1398 * \param buf bufffer that has to be freed if function returns 0
1399 * \param pools pointer to the list of pools in buffer
1400 * \param poolcount number of pools
1402 * \return 0 when found at least 1 pool, i.e. poolcount > 0
1403 * \retval -error failure
1405 int llapi_get_poolbuf(const char *name, char **buf,
1406 char ***pools, int *poolcount)
1409 * list of pool names (assume that pool count is smaller
1412 char **list, *buffer = NULL, *fsname = (char *)name;
1413 char *poolname = NULL, *tmp = NULL, data[16];
1414 enum param_filter type = FILTER_BY_PATH;
1415 int obdcount, bufsize, rc, nb;
1420 if (name[0] != '/') {
1421 fsname = strdup(name);
1425 poolname = strchr(fsname, '.');
1428 type = FILTER_BY_FS_NAME;
1431 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1432 data, sizeof(data));
1435 obdcount = atoi(data);
1438 * Allocate space for each fsname-OST0000_UUID, 1 per OST,
1439 * and also an array to store the pointers for all that
1443 bufsize = sizeof(struct obd_uuid) * obdcount;
1444 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1445 if (buffer == NULL) {
1449 list = (char **) (buffer + bufsize);
1452 /* name is a path or fsname */
1453 nb = llapi_get_poollist(name, list, obdcount,
1456 /* name is a pool name (<fsname>.<poolname>) */
1457 nb = llapi_get_poolmembers(name, list, obdcount,
1461 if (nb == -EOVERFLOW) {
1464 goto retry_get_pools;
1467 rc = (nb < 0 ? nb : 0);
1474 /* Don't free buffer, it will be used later */
1477 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1482 typedef int (semantic_func_t)(char *path, int p, int *d,
1483 void *data, struct dirent64 *de);
1485 #define OBD_NOT_FOUND (-1)
1487 static bool lmv_is_foreign(__u32 magic)
1489 return magic == LMV_MAGIC_FOREIGN;
1492 static void find_param_fini(struct find_param *param)
1494 if (param->fp_migrate)
1497 if (param->fp_obd_indexes) {
1498 free(param->fp_obd_indexes);
1499 param->fp_obd_indexes = NULL;
1502 if (param->fp_lmd) {
1503 free(param->fp_lmd);
1504 param->fp_lmd = NULL;
1507 if (param->fp_lmv_md) {
1508 free(param->fp_lmv_md);
1509 param->fp_lmv_md = NULL;
1513 static int common_param_init(struct find_param *param, char *path)
1515 int lum_size = get_mds_md_size(path);
1520 /* migrate has fp_lmv_md initialized outside */
1521 if (param->fp_migrate)
1524 if (lum_size < PATH_MAX + 1)
1525 lum_size = PATH_MAX + 1;
1527 param->fp_lum_size = lum_size;
1528 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1530 if (param->fp_lmd == NULL) {
1531 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1532 "error: allocate %zu bytes for layout failed",
1533 sizeof(lstat_t) + param->fp_lum_size);
1537 param->fp_lmv_stripe_count = 256;
1538 param->fp_lmv_md = calloc(1,
1539 lmv_user_md_size(param->fp_lmv_stripe_count,
1540 LMV_USER_MAGIC_SPECIFIC));
1541 if (param->fp_lmv_md == NULL) {
1542 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1543 "error: allocation of %d bytes for ioctl",
1544 lmv_user_md_size(param->fp_lmv_stripe_count,
1545 LMV_USER_MAGIC_SPECIFIC));
1546 find_param_fini(param);
1550 param->fp_got_uuids = 0;
1551 param->fp_obd_indexes = NULL;
1552 param->fp_obd_index = OBD_NOT_FOUND;
1553 param->fp_mdt_index = OBD_NOT_FOUND;
1557 static int cb_common_fini(char *path, int p, int *dp, void *data,
1558 struct dirent64 *de)
1560 struct find_param *param = data;
1566 /* set errno upon failure */
1567 static int open_parent(const char *path)
1573 path_copy = strdup(path);
1574 if (path_copy == NULL)
1577 parent_path = dirname(path_copy);
1578 parent = open(parent_path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1584 static int cb_get_dirstripe(char *path, int *d, struct find_param *param)
1587 bool did_nofollow = false;
1592 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1593 if (param->fp_get_default_lmv) {
1597 /* open() may not fetch LOOKUP lock, statx() to ensure dir depth
1600 statx(*d, "", AT_EMPTY_PATH, STATX_MODE, &stx);
1606 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1608 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1611 param->fp_lmv_md->lum_type = LMV_TYPE_RAW;
1613 ret = ioctl(*d, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1615 /* if ENOTTY likely to be a fake symlink, so try again after
1616 * new open() with O_NOFOLLOW, but only once to prevent any
1617 * loop like for the path of a file/dir not on Lustre !!
1619 if (ret < 0 && errno == ENOTTY && !did_nofollow) {
1622 did_nofollow = true;
1623 fd = open(path, O_RDONLY | O_NOFOLLOW);
1625 /* restore original errno */
1630 /* close original fd and set new */
1633 ret2 = ioctl(fd, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1634 if (ret2 < 0 && errno != E2BIG) {
1635 /* restore original errno */
1639 /* LMV is ok or need to handle E2BIG case now */
1643 if (errno == E2BIG && ret != 0) {
1647 /* if foreign LMV case, fake stripes number */
1648 if (lmv_is_foreign(param->fp_lmv_md->lum_magic)) {
1649 struct lmv_foreign_md *lfm;
1651 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1652 if (lfm->lfm_length < XATTR_SIZE_MAX -
1653 offsetof(typeof(*lfm), lfm_value)) {
1654 uint32_t size = lfm->lfm_length +
1655 offsetof(typeof(*lfm), lfm_value);
1657 stripe_count = lmv_foreign_to_md_stripes(size);
1659 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1660 "error: invalid %d foreign size returned from ioctl",
1665 stripe_count = param->fp_lmv_md->lum_stripe_count;
1667 if (stripe_count <= param->fp_lmv_stripe_count)
1670 free(param->fp_lmv_md);
1671 param->fp_lmv_stripe_count = stripe_count;
1672 lmv_size = lmv_user_md_size(stripe_count,
1673 LMV_USER_MAGIC_SPECIFIC);
1674 param->fp_lmv_md = malloc(lmv_size);
1675 if (param->fp_lmv_md == NULL) {
1676 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1677 "error: allocation of %d bytes for ioctl",
1678 lmv_user_md_size(param->fp_lmv_stripe_count,
1679 LMV_USER_MAGIC_SPECIFIC));
1688 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1691 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
1692 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
1693 lmd_v2->lmd_stx.stx_uid = st->st_uid;
1694 lmd_v2->lmd_stx.stx_gid = st->st_gid;
1695 lmd_v2->lmd_stx.stx_mode = st->st_mode;
1696 lmd_v2->lmd_stx.stx_ino = st->st_ino;
1697 lmd_v2->lmd_stx.stx_size = st->st_size;
1698 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
1699 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
1700 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
1701 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
1702 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
1703 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
1704 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
1705 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
1706 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
1708 lmd_v2->lmd_flags = 0;
1710 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1712 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
1713 if (lmd_v2->lmd_stx.stx_size)
1714 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
1715 if (lmd_v2->lmd_stx.stx_blocks)
1716 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
1718 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1719 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
1720 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
1725 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
1727 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
1728 struct lov_user_mds_data *lmd_v2 = lmdbuf;
1732 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
1736 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
1739 st = lmd_v1->lmd_st;
1740 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
1741 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
1742 convert_lmd_statx(lmd_v2, &st, false);
1743 lmd_v2->lmd_lmmsize = 0;
1744 lmd_v2->lmd_padding = 0;
1749 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
1750 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1752 struct lov_user_mds_data *lmd = lmdbuf;
1753 static bool use_old_ioctl;
1757 if (parent_fd < 0 && dir_fd < 0)
1759 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1764 * LL_IOC_MDC_GETINFO operates on the current directory inode
1765 * and returns struct lov_user_mds_data, while
1766 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
1768 if (type == GET_LMD_INFO)
1769 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_V1 :
1770 LL_IOC_MDC_GETINFO_V2;
1772 cmd = LL_IOC_LOV_GETSTRIPE;
1775 ret = ioctl(dir_fd, cmd, lmdbuf);
1776 if (ret < 0 && errno == ENOTTY &&
1777 cmd == LL_IOC_MDC_GETINFO_V2) {
1778 cmd = LL_IOC_MDC_GETINFO_V1;
1779 use_old_ioctl = true;
1783 if (cmd == LL_IOC_MDC_GETINFO_V1 && !ret)
1784 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
1786 if (ret < 0 && errno == ENOTTY && type == GET_LMD_STRIPE) {
1789 /* retry ioctl() after new open() with O_NOFOLLOW
1790 * just in case it could be a fake symlink
1791 * need using a new open() as dir_fd is being closed
1795 dir_fd2 = open(path, O_RDONLY | O_NDELAY | O_NOFOLLOW);
1797 /* return original error */
1800 ret = ioctl(dir_fd2, cmd, lmdbuf);
1801 /* pass new errno or success back to caller */
1807 } else if (parent_fd >= 0) {
1808 const char *fname = strrchr(path, '/');
1811 * IOC_MDC_GETFILEINFO takes as input the filename (relative to
1812 * the parent directory) and returns struct lov_user_mds_data,
1813 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
1815 * This avoids opening, locking, and closing each file on the
1816 * client if that is not needed. Multiple of these ioctl() can
1817 * be done on the parent dir with a single open for all
1818 * files in that directory, and it also doesn't pollute the
1819 * client dcache with millions of dentries when traversing
1820 * a large filesystem.
1822 fname = (fname == NULL ? path : fname + 1);
1824 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
1827 else if (ret >= lmdlen || ret++ == 0)
1830 if (type == GET_LMD_INFO)
1831 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_V1 :
1832 IOC_MDC_GETFILEINFO_V2;
1834 cmd = IOC_MDC_GETFILESTRIPE;
1837 ret = ioctl(parent_fd, cmd, lmdbuf);
1838 if (ret < 0 && errno == ENOTTY &&
1839 cmd == IOC_MDC_GETFILEINFO_V2) {
1840 cmd = IOC_MDC_GETFILEINFO_V1;
1841 use_old_ioctl = true;
1842 goto retry_getfileinfo;
1845 if (cmd == IOC_MDC_GETFILEINFO_V1 && !ret)
1846 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
1850 if (ret && type == GET_LMD_INFO) {
1851 if (errno == ENOTTY) {
1855 * ioctl is not supported, it is not a lustre fs.
1856 * Do the regular lstat(2) instead.
1858 ret = lstat_f(path, &st);
1861 llapi_error(LLAPI_MSG_ERROR, ret,
1862 "error: %s: lstat failed for %s",
1866 convert_lmd_statx(lmd, &st, true);
1868 * It may be wrong to set use_old_ioctl with true as
1869 * the file is not a lustre fs. So reset it with false
1872 use_old_ioctl = false;
1873 } else if (errno == ENOENT) {
1875 llapi_error(LLAPI_MSG_WARN, ret,
1876 "warning: %s does not exist", path);
1877 } else if (errno != EISDIR && errno != ENODATA) {
1879 llapi_error(LLAPI_MSG_ERROR, ret,
1880 "%s ioctl failed for %s.",
1881 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
1882 "IOC_MDC_GETFILEINFO", path);
1889 static int llapi_semantic_traverse(char *path, int size, int parent,
1890 semantic_func_t sem_init,
1891 semantic_func_t sem_fini, void *data,
1892 struct dirent64 *de)
1894 struct find_param *param = (struct find_param *)data;
1895 struct dirent64 *dent;
1896 int len, ret, d, p = -1;
1902 d = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1903 /* if an invalid fake dir symlink, opendir() will return EINVAL
1904 * instead of ENOTDIR. If a valid but dangling faked or real file/dir
1905 * symlink ENOENT will be returned. For a valid/resolved fake or real
1906 * file symlink ENOTDIR will be returned as for a regular file.
1907 * opendir() will be successful for a valid and resolved fake or real
1908 * dir simlink or a regular dir.
1910 if (d == -1 && errno != ENOTDIR && errno != EINVAL && errno != ENOENT) {
1912 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1915 } else if (d == -1) {
1916 if (errno == ENOENT || errno == EINVAL) {
1917 int old_errno = errno;
1919 /* try to open with O_NOFOLLOW this will help
1920 * differentiate fake vs real symlinks
1921 * it is ok to not use O_DIRECTORY with O_RDONLY
1922 * and it will prevent the need to deal with ENOTDIR
1923 * error, instead of ELOOP, being returned by recent
1924 * kernels for real symlinks
1926 d = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
1927 /* if a dangling real symlink should return ELOOP, or
1928 * again ENOENT if really non-existing path, or E...??
1929 * So return original error. If success or ENOTDIR, path
1930 * is likely to be a fake dir/file symlink, so continue
1940 if (parent == -1 && d == -1) {
1941 /* Open the parent dir. */
1942 p = open_parent(path);
1948 } else { /* d != -1 */
1951 /* try to reopen dir with O_NOFOLLOW just in case of a foreign
1954 d2 = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
1959 /* continue with d */
1965 ret = sem_init(path, (parent != -1) ? parent : p, &d, data, de);
1975 /* ENOTDIR if fake symlink, do not consider it as an error */
1976 if (errno != ENOTDIR)
1977 llapi_error(LLAPI_MSG_ERROR, errno,
1978 "fdopendir() failed");
1985 while ((dent = readdir64(dir)) != NULL) {
1988 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1992 if ((len + dent->d_reclen + 2) > size) {
1993 llapi_err_noerrno(LLAPI_MSG_ERROR,
1994 "error: %s: string buffer too small for %s",
1999 strcat(path, dent->d_name);
2001 if (dent->d_type == DT_UNKNOWN) {
2002 struct lov_user_mds_data *lmd = param->fp_lmd;
2004 rc = get_lmd_info_fd(path, d, -1, param->fp_lmd,
2005 param->fp_lum_size, GET_LMD_INFO);
2007 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2015 switch (dent->d_type) {
2017 llapi_err_noerrno(LLAPI_MSG_ERROR,
2018 "error: %s: '%s' is UNKNOWN type %d",
2019 __func__, dent->d_name, dent->d_type);
2022 rc = llapi_semantic_traverse(path, size, d, sem_init,
2023 sem_fini, data, dent);
2024 if (rc != 0 && ret == 0)
2026 if (rc < 0 && rc != -EALREADY &&
2027 param->fp_stop_on_error)
2033 rc = sem_init(path, d, NULL, data, dent);
2034 if (rc < 0 && ret == 0) {
2036 if (rc && rc != -EALREADY &&
2037 param->fp_stop_on_error)
2042 if (sem_fini && rc == 0)
2043 sem_fini(path, d, NULL, data, dent);
2051 sem_fini(path, parent, &d, data, de);
2064 static int param_callback(char *path, semantic_func_t sem_init,
2065 semantic_func_t sem_fini, struct find_param *param)
2067 int ret, len = strlen(path);
2070 if (len > PATH_MAX) {
2072 llapi_error(LLAPI_MSG_ERROR, ret,
2073 "Path name '%s' is too long", path);
2077 buf = (char *)malloc(2 * PATH_MAX);
2081 snprintf(buf, PATH_MAX + 1, "%s", path);
2082 ret = common_param_init(param, buf);
2086 param->fp_depth = 0;
2088 ret = llapi_semantic_traverse(buf, 2 * PATH_MAX, -1, sem_init,
2089 sem_fini, param, NULL);
2091 find_param_fini(param);
2093 return ret < 0 ? ret : 0;
2096 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2100 rc = llapi_ioctl(fd, OBD_IOC_GETDTNAME, lov_name);
2103 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2109 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2113 rc = llapi_ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2116 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2122 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2126 /* do not follow faked symlinks */
2127 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
2129 /* real symlink should have failed with ELOOP so retry without
2130 * O_NOFOLLOW just in case
2132 fd = open(path, O_RDONLY | O_NONBLOCK);
2135 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'",
2141 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2147 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2151 fd = open(path, O_RDONLY | O_NONBLOCK);
2154 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2158 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2170 * If uuidp is NULL, return the number of available obd uuids.
2171 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2172 * there are more OSTs than allocated to uuidp, then an error is returned with
2173 * the ost_count set to number of available obd uuids.
2175 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp, int *indices,
2176 int *ost_count, enum tgt_type type)
2178 char buf[PATH_MAX], format[32];
2180 struct obd_uuid name;
2184 /* Get the lov name */
2185 if (type == LOV_TYPE)
2186 rc = llapi_file_fget_lov_uuid(fd, &name);
2188 rc = llapi_file_fget_lmv_uuid(fd, &name);
2192 /* Now get the ost uuids */
2193 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2194 FILTER_BY_EXACT, "target_obd", ¶m);
2198 fp = fopen(param.gl_pathv[0], "r");
2201 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2206 snprintf(format, sizeof(format),
2207 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2208 for (i = 0; fgets(buf, sizeof(buf), fp); i++) {
2211 if (sscanf(buf, format, &index, name.uuid) < 2)
2214 if (i < *ost_count) {
2217 if (indices != NULL)
2223 if (uuidp && (i > *ost_count))
2228 cfs_free_param_data(¶m);
2232 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2234 return llapi_get_target_uuids(fd, uuidp, NULL, ost_count, LOV_TYPE);
2237 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2242 root = open(mnt, O_RDONLY | O_DIRECTORY);
2245 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2250 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2259 * Check if user specified value matches a real uuid. Ignore _UUID,
2260 * -osc-4ba41334, other trailing gunk in comparison.
2261 * @param real_uuid ends in "_UUID"
2262 * @param search_uuid may or may not end in "_UUID"
2264 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2266 int cmplen = strlen(real_uuid);
2267 int searchlen = strlen(search_uuid);
2269 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2271 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2275 * The UUIDs may legitimately be different lengths, if
2276 * the system was upgraded from an older version.
2278 if (cmplen != searchlen)
2281 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2285 * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2286 * returned in param->fp_obd_index
2288 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2290 struct obd_uuid obd_uuid;
2297 if (param->fp_got_uuids)
2300 /* Get the lov/lmv name */
2301 if (param->fp_get_lmv)
2302 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2304 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2306 if (rc != -ENOTTY) {
2307 llapi_error(LLAPI_MSG_ERROR, rc,
2308 "error: can't get %s name: %s",
2309 param->fp_get_lmv ? "lmv" : "lov",
2317 param->fp_got_uuids = 1;
2319 /* Now get the ost uuids */
2320 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2321 obd_uuid.uuid, FILTER_BY_EXACT,
2322 "target_obd", ¶m_data);
2326 fp = fopen(param_data.gl_pathv[0], "r");
2329 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2330 param_data.gl_pathv[0]);
2334 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2335 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2336 param->fp_get_lmv ? "MDTS" : "OBDS");
2338 snprintf(format, sizeof(format),
2339 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2340 while (fgets(buf, sizeof(buf), fp) != NULL) {
2343 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2346 if (param->fp_obd_uuid) {
2347 if (llapi_uuid_match(obd_uuid.uuid,
2348 param->fp_obd_uuid->uuid)) {
2349 param->fp_obd_index = index;
2352 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2353 /* Print everything */
2354 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2357 param->fp_obds_printed = 1;
2361 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2362 llapi_err_noerrno(LLAPI_MSG_ERROR,
2363 "error: %s: unknown obduuid: %s",
2364 __func__, param->fp_obd_uuid->uuid);
2368 cfs_free_param_data(¶m_data);
2373 * In this case, param->fp_obd_uuid will be an array of obduuids and
2374 * obd index for all these obduuids will be returned in
2375 * param->fp_obd_indexes
2377 static int setup_indexes(int d, char *path, struct obd_uuid *obduuids,
2378 int num_obds, int **obdindexes, int *obdindex,
2381 int ret, obdcount, maxidx, obd_valid = 0, obdnum;
2382 int *indices = NULL;
2383 struct obd_uuid *uuids = NULL;
2388 if (type == LOV_TYPE)
2389 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2391 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2395 obdcount = atoi(buf);
2396 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2399 indices = malloc(obdcount * sizeof(int));
2400 if (indices == NULL) {
2407 ret = llapi_get_target_uuids(d, uuids, indices, &obdcount, type);
2409 if (ret == -EOVERFLOW) {
2410 struct obd_uuid *uuids_temp;
2411 int *indices_temp = NULL;
2413 uuids_temp = realloc(uuids, obdcount *
2414 sizeof(struct obd_uuid));
2417 indices_temp = realloc(indices, obdcount * sizeof(int));
2419 indices = indices_temp;
2420 if (uuids_temp && indices_temp)
2421 goto retry_get_uuids;
2425 llapi_error(LLAPI_MSG_ERROR, ret, "cannot fetch %u OST UUIDs",
2430 indexes = malloc(num_obds * sizeof(*obdindex));
2431 if (indexes == NULL) {
2436 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2439 /* The user may have specified a simple index */
2440 i = strtol(obduuids[obdnum].uuid, &end, 0);
2441 if (end && *end == '\0' && i < maxidx) {
2442 indexes[obdnum] = i;
2445 for (i = 0; i < obdcount; i++) {
2446 if (llapi_uuid_match(uuids[i].uuid,
2447 obduuids[obdnum].uuid)) {
2448 indexes[obdnum] = indices[i];
2456 indexes[obdnum] = OBD_NOT_FOUND;
2457 llapi_err_noerrno(LLAPI_MSG_ERROR,
2458 "invalid obduuid '%s'",
2459 obduuids[obdnum].uuid);
2465 *obdindex = OBD_NOT_FOUND;
2467 *obdindex = obd_valid;
2469 *obdindexes = indexes;
2480 static int setup_target_indexes(int d, char *path, struct find_param *param)
2484 if (param->fp_mdt_uuid) {
2485 ret = setup_indexes(d, path, param->fp_mdt_uuid,
2487 ¶m->fp_mdt_indexes,
2488 ¶m->fp_mdt_index, LMV_TYPE);
2493 if (param->fp_obd_uuid) {
2494 ret = setup_indexes(d, path, param->fp_obd_uuid,
2496 ¶m->fp_obd_indexes,
2497 ¶m->fp_obd_index, LOV_TYPE);
2502 param->fp_got_uuids = 1;
2507 int llapi_ostlist(char *path, struct find_param *param)
2512 fd = open(path, O_RDONLY | O_DIRECTORY);
2516 ret = setup_obd_uuid(fd, path, param);
2523 * Tries to determine the default stripe attributes for a given filesystem. The
2524 * filesystem to check should be specified by fsname, or will be determined
2527 static int sattr_get_defaults(const char *const fsname,
2528 unsigned int *scount,
2529 unsigned int *ssize,
2530 unsigned int *soffset)
2536 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2537 "stripecount", val, sizeof(val));
2540 *scount = atoi(val);
2544 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2545 "stripesize", val, sizeof(val));
2552 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2553 "stripeoffset", val, sizeof(val));
2556 *soffset = atoi(val);
2563 * Tries to gather the default stripe attributes for a given filesystem. If
2564 * the attributes can be determined, they are cached for easy retreival the
2565 * next time they are needed. Only a single filesystem's attributes are
2568 int sattr_cache_get_defaults(const char *const fsname,
2569 const char *const pathname, unsigned int *scount,
2570 unsigned int *ssize, unsigned int *soffset)
2573 char fsname[PATH_MAX + 1];
2574 unsigned int stripecount;
2575 unsigned int stripesize;
2576 unsigned int stripeoffset;
2582 char fsname_buf[PATH_MAX + 1];
2583 unsigned int tmp[3];
2585 if (fsname == NULL) {
2586 rc = llapi_search_fsname(pathname, fsname_buf);
2590 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2593 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2595 * Ensure all 3 sattrs (count, size, and offset) are
2596 * successfully retrieved and stored in tmp before writing to
2599 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2603 cache.stripecount = tmp[0];
2604 cache.stripesize = tmp[1];
2605 cache.stripeoffset = tmp[2];
2606 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2610 *scount = cache.stripecount;
2612 *ssize = cache.stripesize;
2614 *soffset = cache.stripeoffset;
2619 static char *layout2name(__u32 layout_pattern)
2621 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2623 else if (layout_pattern & LOV_PATTERN_FOREIGN)
2625 else if (layout_pattern == LOV_PATTERN_MDT)
2627 else if (layout_pattern == LOV_PATTERN_RAID0)
2629 else if (layout_pattern ==
2630 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2631 return "raid0,overstriped";
2636 enum lov_dump_flags {
2637 LDF_IS_DIR = 0x0001,
2638 LDF_IS_RAW = 0x0002,
2639 LDF_INDENT = 0x0004,
2640 LDF_SKIP_OBJS = 0x0008,
2642 LDF_EXTENSION = 0x0020,
2643 LDF_HEX_IDX = 0x0040,
2646 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2647 struct lov_user_ost_data_v1 *objects,
2648 enum llapi_layout_verbose verbose,
2649 int depth, char *pool_name,
2650 enum lov_dump_flags flags)
2652 bool is_dir = flags & LDF_IS_DIR;
2653 bool is_raw = flags & LDF_IS_RAW;
2654 bool indent = flags & LDF_INDENT;
2655 bool yaml = flags & LDF_YAML;
2656 bool skip_objs = flags & LDF_SKIP_OBJS;
2657 bool extension = flags & LDF_EXTENSION;
2658 char *prefix = is_dir ? "" : "lmm_";
2659 char *separator = "";
2660 char *space = indent ? " " : "";
2661 char *fmt_idx = flags & LDF_HEX_IDX ? "%#x" : "%d";
2664 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2665 lmm_oi_set_seq(&lum->lmm_oi, 0);
2666 if (!indent && (verbose & VERBOSE_DETAIL))
2667 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2670 if (!yaml && !indent && depth && path &&
2671 ((verbose != VERBOSE_OBJID) || !is_dir))
2672 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2674 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2675 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2676 space, prefix, lum->lmm_magic);
2677 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2679 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2680 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2682 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2685 if (verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) {
2690 if (verbose & ~VERBOSE_DFID)
2691 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2695 struct lu_fid dir_fid;
2697 rc = llapi_path2fid(path, &dir_fid);
2699 llapi_error(LLAPI_MSG_ERROR, rc,
2700 "Cannot determine directory fid.");
2702 seq = dir_fid.f_seq;
2703 oid = dir_fid.f_oid;
2704 ver = dir_fid.f_ver;
2707 * This needs a bit of hand-holding since old 1.x
2708 * lmm_oi have { oi.oi_id = mds_inum, oi.oi_seq = 0 }
2709 * and 2.x lmm_oi have { oi.oi_id = mds_oid,
2710 * oi.oi_seq = mds_seq } instead of a real FID.
2711 * Ideally the 2.x code would have stored this like a
2712 * FID with { oi_id = mds_seq, oi_seq = mds_oid } so
2713 * the ostid union lu_fid { f_seq = mds_seq,
2714 * f_oid = mds_oid } worked properly (especially since
2715 * IGIF FIDs use mds_inum as the FID SEQ), but
2716 * unfortunately that didn't happen.
2718 * Print it to look like an IGIF FID, even though the
2719 * fields are reversed on disk, so that it makes sense
2722 * Don't use ostid_id() and ostid_seq(), since they
2723 * assume the oi_fid fields are in the right order.
2724 * This is why there are separate lmm_oi_seq() and
2725 * lmm_oi_id() routines for this.
2727 * For newer layout types hopefully this will be a
2730 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2731 lmm_oi_id(&lum->lmm_oi) :
2732 lmm_oi_seq(&lum->lmm_oi);
2733 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2734 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2735 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2739 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2740 (unsigned long long)seq, oid, ver);
2742 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2743 (unsigned long long)seq, oid, ver);
2746 if (verbose & VERBOSE_STRIPE_COUNT) {
2747 if (verbose & ~VERBOSE_STRIPE_COUNT)
2748 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2751 if (!is_raw && lum->lmm_stripe_count == 0 &&
2752 !(lov_pattern(lum->lmm_pattern) & LOV_PATTERN_MDT)){
2753 unsigned int scount;
2755 rc = sattr_cache_get_defaults(NULL, path,
2759 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2762 llapi_error(LLAPI_MSG_ERROR, rc,
2763 "Cannot determine default stripe count.");
2765 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2767 (__s16)lum->lmm_stripe_count);
2770 llapi_printf(LLAPI_MSG_NORMAL, "%i",
2772 (__s16)lum->lmm_stripe_count);
2774 if (!yaml && is_dir)
2780 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
2781 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
2782 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2783 if (verbose & ~VERBOSE_EXT_SIZE && extension)
2784 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
2786 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
2787 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2789 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2792 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2795 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2797 llapi_error(LLAPI_MSG_ERROR, rc,
2798 "Cannot determine default stripe size.");
2800 /* Extension size is in KiB */
2801 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2803 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
2804 (unsigned long long)lum->lmm_stripe_size);
2806 if (!yaml && is_dir)
2812 if ((verbose & VERBOSE_PATTERN)) {
2813 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2814 if (verbose & ~VERBOSE_PATTERN)
2815 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2817 if (lov_pattern_supported(lum->lmm_pattern))
2818 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2819 layout2name(lum->lmm_pattern));
2821 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2822 separator = (!yaml && is_dir) ? " " : "\n";
2825 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2826 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2827 if (verbose & ~VERBOSE_GENERATION)
2828 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2830 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2831 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2835 if (verbose & VERBOSE_STRIPE_OFFSET) {
2836 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2837 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2838 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2840 if (is_dir || skip_objs)
2841 if (lum->lmm_stripe_offset ==
2842 (typeof(lum->lmm_stripe_offset))(-1))
2843 llapi_printf(LLAPI_MSG_NORMAL, "-1");
2845 llapi_printf(LLAPI_MSG_NORMAL, fmt_idx,
2846 lum->lmm_stripe_offset);
2847 else if (lov_pattern(lum->lmm_pattern) & LOV_PATTERN_MDT)
2848 llapi_printf(LLAPI_MSG_NORMAL, "0");
2850 llapi_printf(LLAPI_MSG_NORMAL, fmt_idx,
2851 objects[0].l_ost_idx);
2852 if (!yaml && is_dir)
2858 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0') &&
2859 (!lov_pool_is_ignored(pool_name) || is_raw)) {
2860 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2861 if (verbose & ~VERBOSE_POOL)
2862 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2864 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2865 if (!yaml && is_dir)
2871 if (strlen(separator) != 0)
2872 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2875 static void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2876 struct lov_user_ost_data_v1 *objects,
2877 char *path, int obdindex, int depth,
2878 enum llapi_layout_verbose verbose,
2879 enum lov_dump_flags flags)
2881 bool is_dir = flags & LDF_IS_DIR;
2882 bool indent = flags & LDF_INDENT;
2883 bool skip_objs = flags & LDF_SKIP_OBJS;
2884 bool yaml = flags & LDF_YAML;
2885 bool hex = flags & LDF_HEX_IDX;
2886 bool obdstripe = obdindex == OBD_NOT_FOUND;
2889 if (!obdstripe && !skip_objs) {
2890 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2891 if (obdindex == objects[i].l_ost_idx) {
2901 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2904 if (!skip_objs && (verbose & VERBOSE_OBJID) &&
2905 ((!is_dir && !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2906 lov_pattern(lum->lmm_pattern) & LOV_PATTERN_MDT)) ||
2907 (is_dir && (lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC)))) {
2908 char *space = " - ";
2911 llapi_printf(LLAPI_MSG_NORMAL,
2912 "%6slmm_objects:\n", " ");
2914 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2916 llapi_printf(LLAPI_MSG_NORMAL,
2917 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2919 for (i = 0; i < lum->lmm_stripe_count; i++) {
2920 int idx = objects[i].l_ost_idx;
2921 long long oid = ostid_id(&objects[i].l_ost_oi);
2922 long long gr = ostid_seq(&objects[i].l_ost_oi);
2924 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2928 struct lu_fid fid = { 0 };
2929 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2930 llapi_printf(LLAPI_MSG_NORMAL,
2931 hex ? "%sl_ost_idx: %#x\n"
2932 : "%sl_ost_idx: %d\n",
2934 llapi_printf(LLAPI_MSG_NORMAL,
2935 "%8sl_fid: "DFID_NOBRACE"\n",
2937 } else if (indent) {
2938 struct lu_fid fid = { 0 };
2940 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2941 llapi_printf(LLAPI_MSG_NORMAL, hex ?
2942 "%s%3d: { l_ost_idx: %#5x, l_fid: "DFID" }\n" :
2943 "%s%3d: { l_ost_idx: %3d, l_fid: "DFID" }\n",
2944 space, i, idx, PFID(&fid));
2945 } else if (is_dir) {
2946 llapi_printf(LLAPI_MSG_NORMAL,
2947 "\t%6u\t%14s\t%13s\t%14s\n", idx, "N/A",
2950 char fmt[48] = { 0 };
2952 sprintf(fmt, "%s%s%s\n",
2953 hex ? "\t%#6x\t%14llu\t%#13llx\t"
2954 : "\t%6u\t%14llu\t%#13llx\t",
2955 (fid_seq_is_rsvd(gr) ||
2956 fid_seq_is_mdt0(gr)) ?
2957 "%14llu" : "%#14llx", "%s");
2958 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2960 obdindex == idx ? " *" : "");
2964 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2967 static void hsm_flags2str(__u32 hsm_flags)
2973 llapi_printf(LLAPI_MSG_NORMAL, "0");
2976 for (i = 0; i < ARRAY_SIZE(hsm_flags_table); i++) {
2977 if (hsm_flags & hsm_flags_table[i].hfn_flag) {
2979 llapi_printf(LLAPI_MSG_NORMAL, ",");
2980 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2981 hsm_flags_table[i].hfn_name);
2987 llapi_printf(LLAPI_MSG_NORMAL, ",");
2988 llapi_printf(LLAPI_MSG_NORMAL, "%#x", hsm_flags);
2992 static uint32_t check_foreign_type(uint32_t foreign_type)
2996 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
2997 if (lu_foreign_types[i].lft_name == NULL)
2999 if (foreign_type == lu_foreign_types[i].lft_type)
3003 return LU_FOREIGN_TYPE_UNKNOWN;
3006 void lov_dump_hsm_lmm(void *lum, char *path, int depth,
3007 enum llapi_layout_verbose verbose,
3008 enum lov_dump_flags flags)
3010 struct lov_hsm_md *lhm = lum;
3011 bool indent = flags & LDF_INDENT;
3012 bool is_dir = flags & LDF_IS_DIR;
3013 char *space = indent ? " " : "";
3016 uint32_t type = check_foreign_type(lhm->lhm_type);
3018 llapi_printf(LLAPI_MSG_NORMAL, "%slhm_magic: 0x%08X\n",
3019 space, lhm->lhm_magic);
3020 llapi_printf(LLAPI_MSG_NORMAL, "%slhm_pattern: hsm\n",
3022 llapi_printf(LLAPI_MSG_NORMAL, "%slhm_length: %u\n",
3023 space, lhm->lhm_length);
3024 llapi_printf(LLAPI_MSG_NORMAL, "%slhm_type: 0x%08X",
3025 space, lhm->lhm_type);
3026 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3027 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3028 lu_foreign_types[type].lft_name);
3030 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3032 llapi_printf(LLAPI_MSG_NORMAL, "%slhm_flags: ", space);
3033 hsm_flags2str(lhm->lhm_flags);
3034 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3036 if (!lov_hsm_type_supported(lhm->lhm_type))
3039 llapi_printf(LLAPI_MSG_NORMAL, "%slhm_archive_id: %llu\n",
3040 space, (unsigned long long)lhm->lhm_archive_id);
3041 llapi_printf(LLAPI_MSG_NORMAL, "%slhm_archive_ver: %llu\n",
3042 space, (unsigned long long)lhm->lhm_archive_ver);
3043 llapi_printf(LLAPI_MSG_NORMAL, "%slhm_archive_uuid: '%.*s'\n",
3044 space, UUID_MAX, lhm->lhm_archive_uuid);
3048 static void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3049 char *path, int obdindex, int depth,
3050 enum llapi_layout_verbose verbose,
3051 enum lov_dump_flags flags)
3053 struct lmv_user_mds_data *objects = lum->lum_objects;
3054 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3055 char *separator = "";
3056 bool yaml = flags & LDF_YAML;
3057 bool hex = flags & LDF_HEX_IDX;
3058 bool obdstripe = false;
3061 if (obdindex != OBD_NOT_FOUND) {
3062 if (lum->lum_stripe_count == 0) {
3063 if (obdindex == lum->lum_stripe_offset)
3066 for (i = 0; i < lum->lum_stripe_count; i++) {
3067 if (obdindex == objects[i].lum_mds) {
3068 llapi_printf(LLAPI_MSG_NORMAL,
3083 /* show all information default */
3085 if (lum->lum_magic == LMV_USER_MAGIC)
3086 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3087 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3089 verbose = VERBOSE_OBJID;
3092 if (depth && path && ((verbose != VERBOSE_OBJID)))
3093 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3095 if (verbose & VERBOSE_STRIPE_COUNT) {
3096 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3097 if (verbose & ~VERBOSE_STRIPE_COUNT)
3098 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3099 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3100 (int)lum->lum_stripe_count);
3101 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3107 if (verbose & VERBOSE_STRIPE_OFFSET) {
3108 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3109 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3110 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3111 llapi_printf(LLAPI_MSG_NORMAL, hex ? "%#x" : "%d",
3112 (int)lum->lum_stripe_offset);
3113 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3119 if (verbose & VERBOSE_HASH_TYPE) {
3120 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3121 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3123 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3124 if (verbose & ~VERBOSE_HASH_TYPE)
3125 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3126 if (type < LMV_HASH_TYPE_MAX)
3127 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3128 mdt_hash_name[type]);
3130 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3132 if (flags & LMV_HASH_FLAG_OVERSTRIPED)
3133 llapi_printf(LLAPI_MSG_NORMAL, ",overstriped");
3134 if (flags & LMV_HASH_FLAG_MIGRATION)
3135 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3136 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3137 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3138 if (flags & LMV_HASH_FLAG_LOST_LMV)
3139 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3140 if (flags & LMV_HASH_FLAG_FIXED)
3141 llapi_printf(LLAPI_MSG_NORMAL, ",fixed");
3142 if (flags & ~LMV_HASH_FLAG_KNOWN)
3143 llapi_printf(LLAPI_MSG_NORMAL, ",unknown_%04x",
3144 flags & ~LMV_HASH_FLAG_KNOWN);
3146 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3152 if ((verbose & VERBOSE_INHERIT) && lum->lum_magic == LMV_USER_MAGIC) {
3153 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3154 if (verbose & ~VERBOSE_INHERIT)
3155 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit: ");
3156 if (lum->lum_max_inherit == LMV_INHERIT_UNLIMITED)
3157 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3158 else if (lum->lum_max_inherit == LMV_INHERIT_NONE)
3159 llapi_printf(LLAPI_MSG_NORMAL, "0");
3161 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3162 lum->lum_max_inherit);
3163 if (verbose & VERBOSE_INHERIT && !yaml)
3169 if ((verbose & VERBOSE_INHERIT_RR) &&
3170 lum->lum_magic == LMV_USER_MAGIC) {
3171 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3172 if (verbose & ~VERBOSE_INHERIT_RR)
3173 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit_rr: ");
3174 if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_UNLIMITED)
3175 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3176 else if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_NONE)
3177 llapi_printf(LLAPI_MSG_NORMAL, "0");
3179 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3180 lum->lum_max_inherit_rr);
3181 if (verbose & VERBOSE_INHERIT_RR && !yaml)
3189 if ((verbose & VERBOSE_OBJID) && lum->lum_magic != LMV_USER_MAGIC) {
3190 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3191 if (lum->lum_stripe_count > 0)
3192 llapi_printf(LLAPI_MSG_NORMAL,
3193 "mdtidx\t\t FID[seq:oid:ver]\n");
3195 char fmt[48] = { 0 };
3196 sprintf(fmt, "%s%s", hex ? "%#6x" : "%6u",
3197 "\t\t "DFID"\t\t%s\n");
3198 for (i = 0; i < lum->lum_stripe_count; i++) {
3199 int idx = objects[i].lum_mds;
3200 struct lu_fid *fid = &objects[i].lum_fid;
3202 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3203 llapi_printf(LLAPI_MSG_NORMAL, fmt,
3205 obdindex == idx ? " *" : "");
3209 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3210 pool_name[0] != '\0') {
3211 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3212 if (verbose & ~VERBOSE_POOL)
3213 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3215 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3219 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3220 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3223 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3224 enum lov_dump_flags flags)
3226 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3227 int depth = param->fp_max_depth;
3228 enum llapi_layout_verbose verbose = param->fp_verbose;
3229 bool yaml = flags & LDF_YAML;
3231 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3232 !(flags & LDF_IS_DIR)) && !yaml)
3233 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3235 if (verbose & VERBOSE_DETAIL) {
3236 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3237 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3238 " ", comp_v1->lcm_magic);
3239 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3240 " ", comp_v1->lcm_size);
3241 if (flags & LDF_IS_DIR)
3242 llapi_printf(LLAPI_MSG_NORMAL,
3243 "%2slcm_flags: %s\n", " ",
3244 comp_v1->lcm_mirror_count > 0 ?
3247 llapi_printf(LLAPI_MSG_NORMAL,
3248 "%2slcm_flags: %s\n", " ",
3249 llapi_layout_flags_string(comp_v1->lcm_flags));
3252 if (verbose & VERBOSE_GENERATION) {
3253 if (verbose & ~VERBOSE_GENERATION)
3254 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3256 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3259 if (verbose & VERBOSE_MIRROR_COUNT) {
3260 if (verbose & ~VERBOSE_MIRROR_COUNT)
3261 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3263 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3264 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3265 comp_v1->lcm_mirror_count + 1 : 1);
3268 if (verbose & VERBOSE_COMP_COUNT) {
3269 if (verbose & ~VERBOSE_COMP_COUNT)
3270 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3272 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3273 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3274 comp_v1->lcm_entry_count : 0);
3277 if (verbose & VERBOSE_DETAIL && !yaml)
3278 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3281 static void lcme_flags2str(__u32 comp_flags)
3287 llapi_printf(LLAPI_MSG_NORMAL, "0");
3290 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3291 const char *cfn_name = comp_flags_table[i].cfn_name;
3292 __u32 cfn_flag = comp_flags_table[i].cfn_flag;
3294 if ((comp_flags & cfn_flag) == cfn_flag) {
3296 llapi_printf(LLAPI_MSG_NORMAL, ",");
3297 llapi_printf(LLAPI_MSG_NORMAL, "%s", cfn_name);
3298 comp_flags &= ~comp_flags_table[i].cfn_flag;
3304 llapi_printf(LLAPI_MSG_NORMAL, ",");
3305 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3309 static void lov_dump_comp_v1_entry(struct find_param *param,
3310 enum lov_dump_flags flags, int index)
3312 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3313 struct lov_comp_md_entry_v1 *entry;
3314 char *separator = "";
3315 enum llapi_layout_verbose verbose = param->fp_verbose;
3316 bool yaml = flags & LDF_YAML;
3318 entry = &comp_v1->lcm_entries[index];
3321 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3323 if (verbose & VERBOSE_COMP_ID) {
3324 if (verbose & VERBOSE_DETAIL && !yaml)
3325 llapi_printf(LLAPI_MSG_NORMAL,
3326 "%slcme_id: ", " - ");
3327 else if (verbose & ~VERBOSE_COMP_ID)
3328 llapi_printf(LLAPI_MSG_NORMAL,
3329 "%4slcme_id: ", " ");
3330 if (entry->lcme_id != LCME_ID_INVAL)
3331 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3333 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3337 if (verbose & VERBOSE_MIRROR_ID) {
3338 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3339 if (verbose & ~VERBOSE_MIRROR_ID)
3340 llapi_printf(LLAPI_MSG_NORMAL,
3341 "%4slcme_mirror_id: ", " ");
3342 if (entry->lcme_id != LCME_ID_INVAL)
3343 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3344 mirror_id_of(entry->lcme_id));
3346 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3350 if (verbose & VERBOSE_COMP_FLAGS) {
3351 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3352 if (verbose & ~VERBOSE_COMP_FLAGS)
3353 llapi_printf(LLAPI_MSG_NORMAL,
3354 "%4slcme_flags: ", " ");
3355 lcme_flags2str(entry->lcme_flags);
3358 /* print snapshot timestamp if its a nosync comp */
3359 if ((verbose & VERBOSE_COMP_FLAGS) &&
3360 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3361 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3362 if (verbose & ~VERBOSE_COMP_FLAGS)
3363 llapi_printf(LLAPI_MSG_NORMAL,
3364 "%4slcme_timestamp: ", " ");
3366 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3367 (unsigned long long)entry->lcme_timestamp);
3369 time_t stamp = entry->lcme_timestamp;
3370 char *date_str = asctime(localtime(&stamp));
3372 date_str[strlen(date_str) - 1] = '\0';
3373 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3379 if (verbose & VERBOSE_COMP_START) {
3380 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3381 if (verbose & ~VERBOSE_COMP_START)
3382 llapi_printf(LLAPI_MSG_NORMAL,
3383 "%4slcme_extent.e_start: ", " ");
3384 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3385 (unsigned long long)entry->lcme_extent.e_start);
3389 if (verbose & VERBOSE_COMP_END) {
3390 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3391 if (verbose & ~VERBOSE_COMP_END)
3392 llapi_printf(LLAPI_MSG_NORMAL,
3393 "%4slcme_extent.e_end: ", " ");
3394 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3395 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3397 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3398 (unsigned long long)entry->lcme_extent.e_end);
3403 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3404 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3405 } else if (verbose & VERBOSE_DETAIL) {
3406 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3407 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3408 " ", entry->lcme_offset);
3409 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3410 " ", entry->lcme_size);
3411 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3413 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3418 * Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3419 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3421 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3422 * The table below gives the answers for the specified parameters (value and
3423 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3424 * --------------------------------------
3425 * 1 | file > limit; sign > 0 | -1 / -1 |
3426 * 2 | file = limit; sign > 0 | -1 / -1 |
3427 * 3 | file < limit; sign > 0 | ? / 1 |
3428 * 4 | file > limit; sign = 0 | -1 / -1 |
3429 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3430 * 6 | file < limit; sign = 0 | ? / -1 |
3431 * 7 | file > limit; sign < 0 | 1 / 1 |
3432 * 8 | file = limit; sign < 0 | ? / -1 |
3433 * 9 | file < limit; sign < 0 | ? / -1 |
3434 * --------------------------------------
3435 * Note: 5th actually means that the value is within the interval
3436 * (limit - margin, limit].
3438 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3439 int sign, int negopt, unsigned long long margin,
3445 /* Drop the fraction of margin (of days or size). */
3446 if (file + margin <= limit)
3448 } else if (sign == 0) {
3449 if (file <= limit && file + margin > limit)
3451 else if (file + margin <= limit)
3453 } else if (sign < 0) {
3460 return negopt ? ~ret + 1 : ret;
3463 static inline struct lov_user_md *
3464 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3466 return (struct lov_user_md *)((char *)comp_v1 +
3467 comp_v1->lcm_entries[ent_idx].lcme_offset);
3470 static inline struct lov_user_ost_data_v1 *
3471 lov_v1v3_objects(struct lov_user_md *v1)
3473 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3474 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3476 return v1->lmm_objects;
3480 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3482 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3483 snprintf(pool_name, LOV_MAXPOOLNAME + 1, "%s",
3484 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3486 pool_name[0] = '\0';
3490 print_last_init_comp(struct find_param *param)
3492 /* print all component info */
3493 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3496 /* print specific component info */
3497 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3498 param->fp_check_comp_start || param->fp_check_comp_end ||
3499 param->fp_check_mirror_id || param->fp_check_mirror_index)
3505 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3509 if (param->fp_comp_end == LUSTRE_EOF) {
3510 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3511 match = end == LUSTRE_EOF ? 1 : -1;
3512 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3513 match = end == LUSTRE_EOF ? -1 : 1;
3514 else /* at least EOF */
3516 if (param->fp_exclude_comp_end)
3519 unsigned long long margin;
3521 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3522 match = find_value_cmp(end, param->fp_comp_end,
3523 param->fp_comp_end_sign,
3524 param->fp_exclude_comp_end, margin, 0);
3531 * An example of "getstripe -v" for a two components PFL file:
3534 * lcm_magic: 0x0BD60BD0
3538 * lcm_entry_count: 2
3542 * lcme_extent.e_start: 0
3543 * lcme_extent.e_end: 1048576
3547 * lmm_magic: 0x0BD10BD0
3548 * lmm_seq: 0x200000401
3549 * lmm_object_id: 0x1
3550 * lmm_fid: [0x200000401:0x1:0x0]
3551 * lmm_stripe_count: 1
3552 * lmm_stripe_size: 1048576
3553 * lmm_pattern: raid0
3555 * lmm_stripe_offset: 0
3557 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3561 * lcme_extent.e_start: 1048576
3562 * lcme_extent.e_end: EOF
3566 * lmm_magic: 0x0BD10BD0
3567 * lmm_seq: 0x200000401
3568 * lmm_object_id: 0x1
3569 * lmm_fid: [0x200000401:0x1:0x0]
3570 * lmm_stripe_count: 2
3571 * lmm_stripe_size: 1048576
3572 * lmm_pattern: raid0
3574 * lmm_stripe_offset: 1
3576 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3577 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3579 static void lov_dump_comp_v1(struct find_param *param, char *path,
3580 enum lov_dump_flags flags)
3582 struct lov_comp_md_entry_v1 *entry;
3583 struct lov_user_ost_data_v1 *objects;
3584 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3585 struct lov_user_md_v1 *v1;
3586 char pool_name[LOV_MAXPOOLNAME + 1];
3587 int obdindex = param->fp_obd_index;
3588 int i, j, match, ext;
3589 bool obdstripe = false;
3590 __u16 mirror_index = 0;
3591 __u16 mirror_id = 0;
3593 if (obdindex != OBD_NOT_FOUND) {
3594 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3595 i < comp_v1->lcm_entry_count; i++) {
3596 if (!(comp_v1->lcm_entries[i].lcme_flags &
3600 v1 = lov_comp_entry(comp_v1, i);
3601 if (v1->lmm_magic == LOV_MAGIC_FOREIGN)
3604 objects = lov_v1v3_objects(v1);
3606 for (j = 0; j < v1->lmm_stripe_count; j++) {
3607 if (obdindex == objects[j].l_ost_idx) {
3620 lov_dump_comp_v1_header(param, path, flags);
3622 flags |= LDF_INDENT;
3624 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3625 entry = &comp_v1->lcm_entries[i];
3627 if (param->fp_check_comp_flags) {
3628 if (((param->fp_comp_flags & entry->lcme_flags) !=
3629 param->fp_comp_flags) ||
3630 (param->fp_comp_neg_flags & entry->lcme_flags))
3634 if (param->fp_check_comp_id &&
3635 param->fp_comp_id != entry->lcme_id)
3638 if (param->fp_check_comp_start) {
3639 match = find_value_cmp(entry->lcme_extent.e_start,
3640 param->fp_comp_start,
3641 param->fp_comp_start_sign,
3643 param->fp_comp_start_units, 0);
3648 if (param->fp_check_comp_end) {
3649 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3655 if (param->fp_check_mirror_index) {
3656 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3658 mirror_id = mirror_id_of(entry->lcme_id);
3661 match = find_value_cmp(mirror_index,
3662 param->fp_mirror_index,
3663 param->fp_mirror_index_sign,
3664 param->fp_exclude_mirror_index,
3668 } else if (param->fp_check_mirror_id) {
3669 if (mirror_id != mirror_id_of(entry->lcme_id))
3670 mirror_id = mirror_id_of(entry->lcme_id);
3672 match = find_value_cmp(mirror_id,
3673 param->fp_mirror_id,
3674 param->fp_mirror_id_sign,
3675 param->fp_exclude_mirror_id,
3681 if (print_last_init_comp(param)) {
3683 * if part of stripe info is needed, we'd print only
3684 * the last instantiated component info.
3686 if (entry->lcme_flags & LCME_FL_INIT)
3689 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3690 if (entry->lcme_flags & LCME_FL_EXTENSION)
3691 /* moved back below */
3699 if (entry->lcme_flags & LCME_FL_INIT) {
3700 if (obdindex != OBD_NOT_FOUND) {
3701 flags |= LDF_SKIP_OBJS;
3702 v1 = lov_comp_entry(comp_v1, i);
3703 if (v1->lmm_magic == LOV_MAGIC_FOREIGN)
3706 objects = lov_v1v3_objects(v1);
3708 for (j = 0; j < v1->lmm_stripe_count; j++) {
3709 if (obdindex == objects[j].l_ost_idx) {
3710 flags &= ~LDF_SKIP_OBJS;
3715 flags &= ~LDF_SKIP_OBJS;
3718 flags |= LDF_SKIP_OBJS;
3721 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3723 lov_dump_comp_v1_entry(param, flags, i);
3725 v1 = lov_comp_entry(comp_v1, i);
3726 if (v1->lmm_magic == LOV_MAGIC_FOREIGN) {
3727 lov_dump_hsm_lmm(v1, path, param->fp_max_depth,
3728 param->fp_verbose, flags);
3730 objects = lov_v1v3_objects(v1);
3731 lov_v1v3_pool_name(v1, pool_name);
3733 ext = entry->lcme_flags & LCME_FL_EXTENSION ?
3735 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path,
3736 obdindex, param->fp_max_depth,
3737 param->fp_verbose, flags | ext);
3740 if (print_last_init_comp(param)) {
3742 * directory layout contains only layout template, print the
3746 i = comp_v1->lcm_entry_count - 1;
3749 flags &= ~LDF_SKIP_OBJS;
3751 lov_dump_comp_v1_entry(param, flags, i);
3753 v1 = lov_comp_entry(comp_v1, i);
3754 if (v1->lmm_magic == LOV_MAGIC_FOREIGN) {
3755 lov_dump_hsm_lmm(v1, path, param->fp_max_depth,
3756 param->fp_verbose, flags);
3758 objects = lov_v1v3_objects(v1);
3759 lov_v1v3_pool_name(v1, pool_name);
3761 entry = &comp_v1->lcm_entries[i];
3762 ext = entry->lcme_flags & LCME_FL_EXTENSION ?
3764 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path,
3765 obdindex, param->fp_max_depth,
3766 param->fp_verbose, flags | ext);
3771 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3772 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3775 static inline bool has_any_comp_options(struct find_param *param)
3777 enum llapi_layout_verbose verbose = param->fp_verbose;
3779 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3780 param->fp_check_comp_start || param->fp_check_comp_end ||
3781 param->fp_check_comp_flags)
3784 /* show full layout information, not component specific */
3785 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3788 return verbose & VERBOSE_COMP_OPTS;
3791 static struct lov_user_mds_data *
3792 lov_forge_comp_v1(struct lov_user_mds_data *orig, bool is_dir)
3794 struct lov_user_md *lum = &orig->lmd_lmm;
3795 struct lov_user_mds_data *new;
3796 struct lov_comp_md_v1 *comp_v1;
3797 struct lov_comp_md_entry_v1 *ent;
3798 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3799 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3802 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3804 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3808 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3809 + sizeof(new->lmd_lmmsize));
3811 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3812 comp_v1->lcm_magic = lum->lmm_magic;
3813 comp_v1->lcm_size = lum_off + lum_size;
3814 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3815 comp_v1->lcm_flags = 0;
3816 comp_v1->lcm_entry_count = 1;
3818 ent = &comp_v1->lcm_entries[0];
3820 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3821 ent->lcme_extent.e_start = 0;
3822 ent->lcme_extent.e_end = LUSTRE_EOF;
3823 ent->lcme_offset = lum_off;
3824 ent->lcme_size = lum_size;
3826 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3831 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3832 enum lov_dump_flags flags)
3834 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3836 if (has_any_comp_options(param)) {
3837 struct lov_user_mds_data *new_lmd, *orig_lmd;
3839 orig_lmd = param->fp_lmd;
3840 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3841 if (new_lmd != NULL) {
3842 param->fp_lmd = new_lmd;
3843 lov_dump_comp_v1(param, path, flags);
3844 param->fp_lmd = orig_lmd;
3850 if (magic == LOV_USER_MAGIC_V1) {
3851 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3852 param->fp_lmd->lmd_lmm.lmm_objects,
3853 path, param->fp_obd_index,
3854 param->fp_max_depth, param->fp_verbose,
3857 char pool_name[LOV_MAXPOOLNAME + 1];
3858 struct lov_user_ost_data_v1 *objects;
3859 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3861 snprintf(pool_name, sizeof(pool_name), "%s",
3862 lmmv3->lmm_pool_name);
3863 objects = lmmv3->lmm_objects;
3864 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3865 objects, path, param->fp_obd_index,
3866 param->fp_max_depth, param->fp_verbose,
3871 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3872 enum lov_dump_flags flags)
3874 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3875 bool yaml = flags & LDF_YAML;
3877 if (!yaml && param->fp_depth && path)
3878 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3880 if (param->fp_verbose & VERBOSE_DETAIL) {
3881 uint32_t type = check_foreign_type(lfm->lfm_type);
3883 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3885 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3887 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3889 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3890 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3891 lu_foreign_types[type].lft_name);
3893 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3895 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3898 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3899 lfm->lfm_length, lfm->lfm_value);
3900 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3903 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3904 enum lov_dump_flags flags)
3906 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3907 bool yaml = flags & LDF_YAML;
3909 if (!yaml && param->fp_depth && path)
3910 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3912 if (param->fp_verbose & VERBOSE_DETAIL) {
3913 uint32_t type = check_foreign_type(lfm->lfm_type);
3915 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3917 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3919 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3921 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3922 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3923 lu_foreign_types[type].lft_name);
3925 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3927 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3930 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3931 lfm->lfm_length, lfm->lfm_value);
3932 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3935 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3936 enum lov_dump_flags flags)
3940 if (param->fp_get_lmv || param->fp_get_default_lmv)
3941 magic = (__u32)param->fp_lmv_md->lum_magic;
3943 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3946 flags |= LDF_IS_RAW;
3949 if (param->fp_hex_idx)
3950 flags |= LDF_HEX_IDX;
3953 case LOV_USER_MAGIC_V1:
3954 case LOV_USER_MAGIC_V3:
3955 case LOV_USER_MAGIC_SPECIFIC:
3956 lov_dump_plain_user_lmm(param, path, flags);
3958 case LOV_USER_MAGIC_FOREIGN:
3959 lov_dump_foreign_lmm(param, path, flags);
3962 case LMV_USER_MAGIC: {
3963 char pool_name[LOV_MAXPOOLNAME + 1];
3964 struct lmv_user_md *lum;
3966 lum = (struct lmv_user_md *)param->fp_lmv_md;
3967 snprintf(pool_name, sizeof(pool_name), "%s",
3968 lum->lum_pool_name);
3969 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3970 param->fp_max_depth, param->fp_verbose,
3974 case LOV_USER_MAGIC_COMP_V1:
3975 lov_dump_comp_v1(param, path, flags);
3977 case LMV_MAGIC_FOREIGN:
3978 lmv_dump_foreign_lmm(param, path, flags);
3981 llapi_printf(LLAPI_MSG_NORMAL,
3982 "unknown lmm_magic: %#x (expecting one of %#x %#x %#x %#x)\n",
3983 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3984 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3985 LMV_USER_MAGIC, LMV_MAGIC_V1);
3990 static int llapi_file_get_stripe1(const char *path, struct lov_user_md *lum)
3996 fname = strrchr(path, '/');
3998 /* It should be a file (or other non-directory) */
3999 if (fname == NULL) {
4000 dname = (char *)malloc(2);
4004 fname = (char *)path;
4006 dname = (char *)malloc(fname - path + 1);
4009 strncpy(dname, path, fname - path);
4010 dname[fname - path] = '\0';
4014 fd = open(dname, O_RDONLY | O_NONBLOCK);
4020 strcpy((char *)lum, fname);
4021 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
4024 if (close(fd) == -1 && rc == 0)
4032 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
4034 char *canon_path = NULL;
4037 rc = llapi_file_get_stripe1(path, lum);
4038 if (!(rc == -ENOTTY || rc == -ENODATA))
4041 /* Handle failure due to symlinks by dereferencing path manually. */
4042 canon_path = canonicalize_file_name(path);
4043 if (canon_path == NULL)
4044 goto out; /* Keep original rc. */
4046 rc2 = llapi_file_get_stripe1(canon_path, lum);
4048 goto out; /* Keep original rc. */
4057 int llapi_file_lookup(int dirfd, const char *name)
4059 struct obd_ioctl_data data = { 0 };
4064 if (dirfd < 0 || name == NULL)
4067 data.ioc_version = OBD_IOCTL_VERSION;
4068 data.ioc_len = sizeof(data);
4069 data.ioc_inlbuf1 = (char *)name;
4070 data.ioc_inllen1 = strlen(name) + 1;
4072 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
4074 llapi_error(LLAPI_MSG_ERROR, rc,
4075 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
4080 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
4087 * Check if the file time matches all the given criteria (e.g. --atime +/-N).
4088 * Return -1 or 1 if file timestamp does not or does match the given criteria
4089 * correspondingly. Return 0 if the MDS time is being checked and there are
4090 * attributes on OSTs and it is not yet clear if the timespamp matches.
4092 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
4093 * updated timestamps.
4095 static int find_time_check(struct find_param *param, int mds)
4097 struct lov_user_mds_data *lmd = param->fp_lmd;
4101 /* Check if file is accepted. */
4102 if (param->fp_atime) {
4103 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4104 param->fp_atime, param->fp_asign,
4105 param->fp_exclude_atime,
4106 param->fp_time_margin, mds);
4112 if (param->fp_mtime) {
4113 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4114 param->fp_mtime, param->fp_msign,
4115 param->fp_exclude_mtime,
4116 param->fp_time_margin, mds);
4121 * If the previous check matches, but this one is not yet clear,
4122 * we should return 0 to do an RPC on OSTs.
4128 if (param->fp_ctime) {
4129 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4130 param->fp_ctime, param->fp_csign,
4131 param->fp_exclude_ctime,
4132 param->fp_time_margin, mds);
4137 * If the previous check matches, but this one is not yet clear,
4138 * we should return 0 to do an RPC on OSTs.
4147 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4149 struct lov_user_mds_data *lmd = param->fp_lmd;
4154 for (i = 0; i < 2; i++) {
4155 /* Check if file is accepted. */
4156 if (param->fp_newery[NEWERXY_ATIME][i]) {
4157 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4158 param->fp_newery[NEWERXY_ATIME][i],
4165 if (param->fp_newery[NEWERXY_MTIME][i]) {
4166 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4167 param->fp_newery[NEWERXY_MTIME][i],
4173 * If the previous check matches, but this one is not
4174 * yet clear, we should return 0 to do an RPC on OSTs.
4180 if (param->fp_newery[NEWERXY_CTIME][i]) {
4181 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4182 param->fp_newery[NEWERXY_CTIME][i],
4188 * If the previous check matches, but this one is not
4189 * yet clear, we should return 0 to do an RPC on OSTs.
4196 * File birth time (btime) can get from MDT directly.
4197 * if @from_mdt is true, it means the input file attributs are
4198 * obtained directly from MDT.
4199 * Thus, if @from_mdt is false, we should skip the following
4205 if (param->fp_newery[NEWERXY_BTIME][i]) {
4206 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4209 rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4210 param->fp_newery[NEWERXY_BTIME][i],
4221 * Check whether the stripes matches the indexes user provided
4225 static int check_obd_match(struct find_param *param)
4227 struct lov_user_ost_data_v1 *objects;
4228 struct lov_comp_md_v1 *comp_v1 = NULL;
4229 struct lov_user_mds_data *lmd = param->fp_lmd;
4230 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4231 int i, j, k, count = 1;
4233 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4236 if (!S_ISREG(lmd->lmd_stx.stx_mode))
4239 /* exclude foreign */
4240 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4241 return param->fp_exclude_obd;
4244 * Only those files should be accepted, which have a
4245 * stripe on the specified OST.
4247 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4248 comp_v1 = (struct lov_comp_md_v1 *)v1;
4249 count = comp_v1->lcm_entry_count;
4252 for (i = 0; i < count; i++) {
4254 v1 = lov_comp_entry(comp_v1, i);
4256 objects = lov_v1v3_objects(v1);
4258 for (j = 0; j < v1->lmm_stripe_count; j++) {
4259 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4262 for (k = 0; k < param->fp_num_obds; k++) {
4263 if (param->fp_obd_indexes[k] ==
4264 objects[j].l_ost_idx)
4265 return !param->fp_exclude_obd;
4270 return param->fp_exclude_obd;
4273 static int check_mdt_match(struct find_param *param)
4277 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4280 /* FIXME: For striped dir, we should get stripe information and check */
4281 for (i = 0; i < param->fp_num_mdts; i++) {
4282 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4283 return !param->fp_exclude_mdt;
4286 if (param->fp_exclude_mdt)
4293 * Check whether the obd is active or not, if it is
4294 * not active, just print the object affected by this
4297 static void print_failed_tgt(struct find_param *param, char *path, int type)
4299 struct obd_statfs stat_buf;
4300 struct obd_uuid uuid_buf;
4301 int tgt_nr, i, *indexes;
4304 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV) {
4305 llapi_error(LLAPI_MSG_NORMAL, ret, "%s: wrong statfs type(%d)",
4310 tgt_nr = (type == LL_STATFS_LOV) ? param->fp_obd_index :
4311 param->fp_mdt_index;
4312 indexes = (type == LL_STATFS_LOV) ? param->fp_obd_indexes :
4313 param->fp_mdt_indexes;
4315 for (i = 0; i < tgt_nr; i++) {
4316 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4317 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4319 ret = llapi_obd_statfs(path, type, indexes[i], &stat_buf,
4322 llapi_error(LLAPI_MSG_NORMAL, ret,
4323 "%s: obd_uuid: %s failed",
4324 __func__, param->fp_obd_uuid->uuid);
4328 static int find_check_stripe_size(struct find_param *param)
4330 struct lov_comp_md_v1 *comp_v1 = NULL;
4331 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4332 __u32 stripe_size = 0;
4333 int ret, i, count = 1;
4335 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4336 return param->fp_exclude_stripe_size ? 1 : -1;
4338 ret = param->fp_exclude_stripe_size ? 1 : -1;
4339 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4340 comp_v1 = (struct lov_comp_md_v1 *)v1;
4341 count = comp_v1->lcm_entry_count;
4344 for (i = 0; i < count; i++) {
4345 struct lov_comp_md_entry_v1 *ent;
4348 v1 = lov_comp_entry(comp_v1, i);
4350 ent = &comp_v1->lcm_entries[i];
4351 if (ent->lcme_flags & LCME_FL_EXTENSION)
4353 if (!(ent->lcme_flags & LCME_FL_INIT))
4356 stripe_size = v1->lmm_stripe_size;
4359 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4360 param->fp_stripe_size_sign,
4361 param->fp_exclude_stripe_size,
4362 param->fp_stripe_size_units, 0);
4367 static int find_check_ext_size(struct find_param *param)
4369 struct lov_comp_md_v1 *comp_v1;
4370 struct lov_user_md_v1 *v1;
4373 ret = param->fp_exclude_ext_size ? 1 : -1;
4374 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4375 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4378 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4379 struct lov_comp_md_entry_v1 *ent;
4381 v1 = lov_comp_entry(comp_v1, i);
4383 ent = &comp_v1->lcm_entries[i];
4384 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4387 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4388 param->fp_ext_size_sign,
4389 param->fp_exclude_ext_size,
4390 param->fp_ext_size_units, 0);
4391 /* If any ext_size matches */
4399 static __u32 find_get_stripe_count(struct find_param *param)
4401 struct lov_comp_md_v1 *comp_v1 = NULL;
4402 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4404 __u32 stripe_count = 0;
4406 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4409 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4410 comp_v1 = (struct lov_comp_md_v1 *)v1;
4411 count = comp_v1->lcm_entry_count;
4414 for (i = 0; i < count; i++) {
4416 struct lov_comp_md_entry_v1 *ent;
4418 v1 = lov_comp_entry(comp_v1, i);
4420 ent = &comp_v1->lcm_entries[i];
4421 if (!(ent->lcme_flags & LCME_FL_INIT))
4424 if (ent->lcme_flags & LCME_FL_EXTENSION)
4427 stripe_count = v1->lmm_stripe_count;
4430 return stripe_count;
4433 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4435 static int find_check_layout(struct find_param *param)
4437 struct lov_comp_md_v1 *comp_v1 = NULL;
4438 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4440 bool found = false, valid = false;
4442 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4443 comp_v1 = (struct lov_comp_md_v1 *)v1;
4444 count = comp_v1->lcm_entry_count;
4447 for (i = 0; i < count; i++) {
4449 v1 = lov_comp_entry(comp_v1, i);
4451 /* foreign file have a special magic but no pattern field */
4452 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4455 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4459 if (v1->lmm_pattern & param->fp_layout) {
4468 if ((found && !param->fp_exclude_layout) ||
4469 (!found && param->fp_exclude_layout))
4476 * if no type specified, check/exclude all foreign
4477 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4479 static int find_check_foreign(struct find_param *param)
4481 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4482 struct lov_foreign_md *lfm;
4484 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4485 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4486 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4487 return param->fp_exclude_foreign ? 1 : -1;
4491 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4492 lfm->lfm_type == param->fp_foreign_type)
4493 return param->fp_exclude_foreign ? -1 : 1;
4494 return param->fp_exclude_foreign ? 1 : -1;
4497 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4498 struct lmv_foreign_md *lfm;
4500 lfm = (void *)param->fp_lmv_md;
4501 if (lmv_is_foreign(lfm->lfm_magic)) {
4502 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4503 return param->fp_exclude_foreign ? 1 : -1;
4507 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4508 lfm->lfm_type == param->fp_foreign_type)
4509 return param->fp_exclude_foreign ? -1 : 1;
4510 return param->fp_exclude_foreign ? 1 : -1;
4515 static int find_check_pool(struct find_param *param)
4517 struct lov_comp_md_v1 *comp_v1 = NULL;
4518 struct lov_user_md_v3 *v3 = (void *)¶m->fp_lmd->lmd_lmm;
4522 if (v3->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4523 comp_v1 = (struct lov_comp_md_v1 *)v3;
4524 count = comp_v1->lcm_entry_count;
4525 /* empty requested pool is taken as no pool search */
4526 if (count == 0 && param->fp_poolname[0] == '\0') {
4532 for (i = 0; i < count; i++) {
4533 if (comp_v1 != NULL) {
4534 if (!(comp_v1->lcm_entries[i].lcme_flags &
4538 v3 = (void *)lov_comp_entry(comp_v1, i);
4541 if (v3->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4544 if (((v3->lmm_magic == LOV_USER_MAGIC_V1) &&
4545 (param->fp_poolname[0] == '\0')) ||
4546 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4547 (strncmp(v3->lmm_pool_name,
4548 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4549 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4550 (strcmp(param->fp_poolname, "*") == 0))) {
4557 if ((found && !param->fp_exclude_pool) ||
4558 (!found && param->fp_exclude_pool))
4564 static int find_check_comp_options(struct find_param *param)
4566 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4567 struct lov_user_mds_data *lmd = param->fp_lmd;
4568 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4569 struct lov_comp_md_entry_v1 *entry;
4572 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4575 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4576 comp_v1 = (struct lov_comp_md_v1 *)v1;
4578 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4579 if (forged_v1 == NULL)
4581 comp_v1 = forged_v1;
4582 comp_v1->lcm_entry_count = 1;
4583 entry = &comp_v1->lcm_entries[0];
4584 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4586 entry->lcme_extent.e_start = 0;
4587 entry->lcme_extent.e_end = LUSTRE_EOF;
4590 /* invalid case, don't match for any kind of search. */
4591 if (comp_v1->lcm_entry_count == 0) {
4596 if (param->fp_check_comp_count) {
4597 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4598 param->fp_comp_count,
4599 param->fp_comp_count_sign,
4600 param->fp_exclude_comp_count, 1, 0);
4606 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4607 entry = &comp_v1->lcm_entries[i];
4609 if (param->fp_check_comp_flags) {
4611 if (((param->fp_comp_flags & entry->lcme_flags) !=
4612 param->fp_comp_flags) ||
4613 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4619 if (param->fp_check_comp_start) {
4620 ret = find_value_cmp(entry->lcme_extent.e_start,
4621 param->fp_comp_start,
4622 param->fp_comp_start_sign,
4623 param->fp_exclude_comp_start,
4624 param->fp_comp_start_units, 0);
4629 if (param->fp_check_comp_end) {
4630 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4636 /* the component matches all criteria */
4645 static int find_check_mirror_options(struct find_param *param)
4647 struct lov_comp_md_v1 *comp_v1;
4648 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4651 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4654 comp_v1 = (struct lov_comp_md_v1 *)v1;
4656 if (param->fp_check_mirror_count) {
4657 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4658 param->fp_mirror_count,
4659 param->fp_mirror_count_sign,
4660 param->fp_exclude_mirror_count, 1, 0);
4665 if (param->fp_check_mirror_state) {
4667 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4669 if ((param->fp_mirror_state != 0 &&
4670 file_state != param->fp_mirror_state) ||
4671 file_state == param->fp_mirror_neg_state)
4678 static int find_check_attr_options(struct find_param *param)
4683 attrs = param->fp_lmd->lmd_stx.stx_attributes_mask &
4684 param->fp_lmd->lmd_stx.stx_attributes;
4686 /* This is a AND between all (negated) specified attributes */
4687 if ((param->fp_attrs && (param->fp_attrs & attrs) != param->fp_attrs) ||
4688 (param->fp_neg_attrs && (param->fp_neg_attrs & attrs)))
4691 if ((found && param->fp_exclude_attrs) ||
4692 (!found && !param->fp_exclude_attrs))
4699 * xattr_reg_match() - return true if the supplied string matches the pattern.
4701 * This requires the regex to match the entire supplied string, not just a
4704 * str must be null-terminated. len should be passed in anyways to avoid an
4705 * extra call to strlen(str) when the length is already known.
4707 static bool xattr_reg_match(regex_t *pattern, const char *str, int len)
4712 ret = regexec(pattern, str, 1, &pmatch, 0);
4713 if (ret == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
4720 * xattr_done_matching() - return true if all supplied patterns have been
4721 * matched, allowing to skip checking any remaining xattrs on a file.
4723 * This is only allowed if there are no "exclude" patterns.
4725 static int xattr_done_matching(struct xattr_match_info *xmi)
4729 for (i = 0; i < xmi->xattr_regex_count; i++) {
4730 /* if any pattern still undecided, need to keep going */
4731 if (!xmi->xattr_regex_matched[i])
4738 static int find_check_xattrs(char *path, struct xattr_match_info *xmi)
4740 ssize_t list_len = 0;
4741 ssize_t val_len = 0;
4746 for (i = 0; i < xmi->xattr_regex_count; i++)
4747 xmi->xattr_regex_matched[i] = false;
4749 list_len = llistxattr(path, xmi->xattr_name_buf, XATTR_LIST_MAX);
4751 llapi_error(LLAPI_MSG_ERROR, errno,
4752 "error: listxattr: %s", path);
4756 /* loop over all xattr names on the file */
4757 for (p = xmi->xattr_name_buf;
4758 p - xmi->xattr_name_buf < list_len;
4759 p = strchr(p, '\0'), p++) {
4760 fetched_val = false;
4761 /* loop over all regex patterns specified and check them */
4762 for (i = 0; i < xmi->xattr_regex_count; i++) {
4763 if (xmi->xattr_regex_matched[i])
4766 if (!xattr_reg_match(xmi->xattr_regex_name[i],
4770 if (xmi->xattr_regex_value[i] == NULL)
4774 * even if multiple patterns match the same xattr name,
4775 * don't call getxattr() more than once
4778 val_len = lgetxattr(path, p,
4779 xmi->xattr_value_buf,
4783 llapi_error(LLAPI_MSG_ERROR, errno,
4784 "error: getxattr: %s",
4790 * the value returned by getxattr might or
4791 * might not be null terminated.
4792 * if it is, then decrement val_len so it
4793 * matches what strlen() would return.
4794 * if it is not, then add a null terminator
4795 * since regexec() expects that.
4798 xmi->xattr_value_buf[val_len - 1] == '\0') {
4801 xmi->xattr_value_buf[val_len] = '\0';
4805 if (!xattr_reg_match(xmi->xattr_regex_value[i],
4806 xmi->xattr_value_buf, val_len))
4811 * if exclude this xattr, we can exit early
4814 if (xmi->xattr_regex_exclude[i])
4817 xmi->xattr_regex_matched[i] = true;
4820 * if all "include" patterns have matched, and there are
4821 * no "exclude" patterns, we can exit early with match
4823 if (xattr_done_matching(xmi) == 1)
4829 * finally, check that all supplied patterns either matched, or were
4830 * "exclude" patterns if they did not match.
4832 for (i = 0; i < xmi->xattr_regex_count; i++) {
4833 if (!xmi->xattr_regex_matched[i]) {
4834 if (!xmi->xattr_regex_exclude[i]) {
4843 static bool find_check_lmm_info(struct find_param *param)
4845 return param->fp_check_pool || param->fp_check_stripe_count ||
4846 param->fp_check_stripe_size || param->fp_check_layout ||
4847 param->fp_check_comp_count || param->fp_check_comp_end ||
4848 param->fp_check_comp_start || param->fp_check_comp_flags ||
4849 param->fp_check_mirror_count || param->fp_check_foreign ||
4850 param->fp_check_mirror_state || param->fp_check_ext_size ||
4851 param->fp_check_projid;
4855 * Interpret backslash escape sequences and write output into buffer.
4856 * Anything written to the buffer will be null terminated.
4858 * @param[in] seq String being parsed for escape sequence. The leading
4859 * '\' character is not included in this string (only the
4860 * characters after it)
4861 * @param[out] buffer Location where interpreted escape sequence is written
4862 * @param[in] size Size of the available buffer. (Needs to be large enough
4863 * to handle escape sequence output plus null terminator.)
4864 * @param[out] wrote Number of bytes written to the buffer.
4865 * @return Number of characters from input string processed
4866 * as part of the escape sequence (0 for an unrecognized
4869 static int printf_format_escape(char *seq, char *buffer, size_t size,
4873 /* For now, only handle single char escape sequences: \n, \t, \\ */
4896 * Interpret formats for timestamps (%a, %A@, etc)
4898 * @param[in] seq String being parsed for timestamp format. The leading
4899 * '%' character is not included in this string
4900 * @param[out] buffer Location where timestamp info is written
4901 * @param[in] size Size of the available buffer.
4902 * @param[out] wrote Number of bytes written to the buffer.
4903 * @return Number of characters from input string processed
4904 * as part of the format (0 for an unknown format)
4906 static int printf_format_timestamp(char *seq, char *buffer, size_t size,
4907 int *wrote, struct find_param *param)
4909 struct statx_timestamp ts = { 0, 0 };
4913 char *fmt = "%c"; /* Print in ctime format by default */
4918 ts = param->fp_lmd->lmd_stx.stx_atime;
4922 if (*(seq + 1) == '@') {
4923 ts = param->fp_lmd->lmd_stx.stx_atime;
4929 ts = param->fp_lmd->lmd_stx.stx_ctime;
4933 if (*(seq + 1) == '@') {
4934 ts = param->fp_lmd->lmd_stx.stx_ctime;
4940 ts = param->fp_lmd->lmd_stx.stx_mtime;
4944 if (*(seq + 1) == '@') {
4945 ts = param->fp_lmd->lmd_stx.stx_mtime;
4951 ts = param->fp_lmd->lmd_stx.stx_btime;
4955 if (*(seq + 1) == '@') {
4956 ts = param->fp_lmd->lmd_stx.stx_btime;
4966 /* Found valid format, print to buffer */
4969 *wrote = strftime(buffer, size, fmt, tm);
4976 * Print all ost indices associated with a file layout using a commma separated
4977 * list. For a file with mutliple components, the list of indices for each
4978 * component will be enclosed in brackets.
4980 * @param[out] buffer Location where OST indices are written
4981 * @param[in] size Size of the available buffer.
4982 * @pararm[in] layout Pointer to layout structure for the file
4983 * @return Number of bytes written to output buffer
4985 static int printf_format_ost_indices(char *buffer, size_t size,
4986 struct llapi_layout *layout)
4988 uint64_t count, idx, i;
4989 int err, bytes, wrote = 0;
4991 /* Make sure to start at the first component */
4992 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
4994 llapi_error(LLAPI_MSG_ERROR, err,
4995 "error: layout component iteration failed\n");
4999 err = llapi_layout_stripe_count_get(layout, &count);
5001 llapi_error(LLAPI_MSG_ERROR, err,
5002 "error: cannot get stripe_count\n");
5006 bytes = snprintf(buffer, (size - wrote), "%s", "[");
5011 for (i = 0; i < count; i++) {
5012 err = llapi_layout_ost_index_get(layout, i, &idx);
5014 llapi_error(LLAPI_MSG_ERROR, err,
5015 "error: cannot get OST index\n");
5016 bytes = snprintf(buffer, (size - wrote),
5019 bytes = snprintf(buffer, (size - wrote),
5027 /* Overwrite last comma with closing bracket */
5028 *(buffer - 1) = ']';
5030 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
5031 if (err == 0) /* next component is found */
5034 llapi_error(LLAPI_MSG_ERROR, err,
5035 "error: layout component iteration failed\n");
5036 /* At this point, either got error or reached last component */
5047 * Print file attributes as a comma-separated list of named attribute flags,
5048 * and hex value of any unknown attributes.
5050 * @param[out] buffer Location where file attributes are written
5051 * @param[in] size Size of the available buffer.
5052 * @pararm[in] lstx Void pointer holding address of struct statx. Which is
5053 * containing attributes to be printed
5054 * @return Number of bytes written to output buffer
5056 static int printf_format_file_attributes(char *buffer, size_t size,
5057 void *lstx, bool longopt)
5059 lstatx_t *stx = (lstatx_t *)lstx;
5060 uint64_t attrs = stx->stx_attributes_mask & stx->stx_attributes;
5061 int bytes = 0, wrote = 0, first = 1;
5062 uint64_t known_attrs = 0;
5063 struct attrs_name *ap;
5065 /* before all, print '---' if no attributes, and exit */
5067 bytes = snprintf(buffer, size - wrote, "---");
5072 /* first, browse list of known attributes */
5073 for (ap = (struct attrs_name *)attrs_array; ap->an_attr != 0; ap++) {
5074 known_attrs |= ap->an_attr;
5075 if (attrs & ap->an_attr) {
5077 bytes = snprintf(buffer, size - wrote, "%s%s",
5078 first ? "" : ",", ap->an_name);
5080 bytes = snprintf(buffer, size - wrote, "%c",
5090 /* second, print hex value for unknown attributes */
5091 attrs &= ~known_attrs;
5093 bytes = snprintf(buffer, size - wrote, "%s0x%lx",
5094 first ? "" : ",", attrs);
5105 * Parse Lustre-specific format sequences of the form %L{x}.
5107 * @param[in] seq String being parsed for format sequence. The leading
5108 * '%' character is not included in this string
5109 * @param[out] buffer Location where interpreted format info is written
5110 * @param[in] size Size of the available buffer.
5111 * @param[out] wrote Number of bytes written to the buffer.
5112 * @param[in] param The find_param structure associated with the file/dir
5113 * @param[in] path Pathname of the current file/dir being handled
5114 * @param[in] projid Project ID associated with the current file/dir
5115 * @param[in] d File descriptor for the directory (or -1 for a
5116 * non-directory file)
5117 * @return Number of characters from input string processed
5118 * as part of the format (0 for an unknown format)
5120 static int printf_format_lustre(char *seq, char *buffer, size_t size,
5121 int *wrote, struct find_param *param,
5122 char *path, __u32 projid, int d)
5124 struct lmv_user_md *lum;
5125 struct lmv_user_mds_data *objects;
5126 struct llapi_layout *layout = NULL;
5128 unsigned int hash_type;
5129 uint64_t str_cnt, str_size, idx;
5130 char pool_name[LOV_MAXPOOLNAME + 1] = { '\0' };
5132 bool longopt = true;
5133 int rc = 2; /* all current valid sequences are 2 chars */
5137 /* Sanity check. Formats always look like %L{X} */
5138 if (*seq++ != 'L') {
5144 * Some formats like %LF or %LP are handled the same for both files
5145 * and dirs, so handle all of those here.
5149 err = llapi_path2fid(path, &fid);
5151 llapi_error(LLAPI_MSG_ERROR, err,
5152 "error: cannot get fid\n");
5155 *wrote = snprintf(buffer, size, DFID_NOBRACE, PFID(&fid));
5158 if (projid == INVALID_PROJID)
5159 *wrote = snprintf(buffer, size, "-1");
5161 *wrote = snprintf(buffer, size, "%u", projid);
5163 case 'a': /* file attributes */
5167 lstx = ¶m->fp_lmd->lmd_stx;
5169 *wrote = printf_format_file_attributes(buffer, size, lstx,
5174 /* Other formats for files/dirs need to be handled differently */
5175 if (d == -1) { /* file */
5176 //layout = llapi_layout_get_by_xattr(¶m->fp_lmd->lmd_lmm,
5177 // param->fp_lum_size, 0);
5178 layout = llapi_layout_get_by_path(path, 0);
5179 if (layout == NULL) {
5180 llapi_error(LLAPI_MSG_ERROR, errno,
5181 "error: cannot get file layout\n");
5186 * Set the layout pointer to the last init component
5187 * since that is the component used for most of these
5188 * formats. (This also works for non-composite files)
5190 err = llapi_layout_get_last_init_comp(layout);
5192 llapi_error(LLAPI_MSG_ERROR, err,
5193 "error: cannot get last initialized compomnent\n");
5198 case 'c': /* stripe count */
5199 err = llapi_layout_stripe_count_get(layout, &str_cnt);
5201 llapi_error(LLAPI_MSG_ERROR, err,
5202 "error: cannot get stripe_count\n");
5205 *wrote = snprintf(buffer, size, "%"PRIu64, str_cnt);
5207 case 'h': /* hash info */
5208 /* Not applicable to files. Skip it. */
5210 case 'i': /* starting index */
5211 err = llapi_layout_ost_index_get(layout, 0, &idx);
5213 llapi_error(LLAPI_MSG_ERROR, err,
5214 "error: cannot get OST index of last initialized component\n");
5217 *wrote = snprintf(buffer, size, "%"PRIu64, idx);
5219 case 'o': /* list of object indices */
5220 *wrote = printf_format_ost_indices(buffer, size, layout);
5222 case 'p': /* pool name */
5223 err = llapi_layout_pool_name_get(layout, pool_name,
5226 llapi_error(LLAPI_MSG_ERROR, rc,
5227 "error: cannot get pool name\n");
5230 *wrote = snprintf(buffer, size, "%s", pool_name);
5232 case 'S': /* stripe size */
5233 err = llapi_layout_stripe_size_get(layout, &str_size);
5235 llapi_error(LLAPI_MSG_ERROR, rc,
5236 "error: cannot get stripe_size\n");
5239 *wrote = snprintf(buffer, size, "%"PRIu64, str_size);
5245 } else { /* directory */
5246 lum = (struct lmv_user_md *)param->fp_lmv_md;
5247 objects = lum->lum_objects;
5250 case 'c': /* stripe count */
5251 *wrote = snprintf(buffer, size, "%d",
5252 (int)lum->lum_stripe_count);
5254 case 'h': /* hash info */
5255 hash_type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
5256 if (hash_type < LMV_HASH_TYPE_MAX)
5257 *wrote = snprintf(buffer, size, "%s",
5258 mdt_hash_name[hash_type]);
5260 *wrote = snprintf(buffer, size, "%#x",
5263 case 'i': /* starting index */
5264 *wrote = snprintf(buffer, size, "%d",
5265 lum->lum_stripe_offset);
5267 case 'o': /* list of object indices */
5268 str_cnt = (int) lum->lum_stripe_count;
5269 *wrote = snprintf(buffer, size, "%s", "[");
5273 for (i = 0; i < str_cnt; i++) {
5274 bytes = snprintf(buffer, (size - *wrote),
5275 "%d,", objects[i].lum_mds);
5282 /* Use lum_offset as the only list entry */
5283 bytes = snprintf(buffer, (size - *wrote),
5284 "%d]", lum->lum_stripe_offset);
5287 /* Overwrite last comma with closing bracket */
5288 *(buffer - 1) = ']';
5291 case 'p': /* pool name */
5292 *wrote = snprintf(buffer, size, "%s",
5293 lum->lum_pool_name);
5295 case 'S': /* stripe size */
5296 /* This has no meaning for directories. Skip it. */
5306 llapi_layout_free(layout);
5309 /* output of snprintf was truncated */
5316 * Interpret format specifiers beginning with '%'.
5318 * @param[in] seq String being parsed for format specifier. The leading
5319 * '%' character is not included in this string
5320 * @param[out] buffer Location where formatted info is written
5321 * @param[in] size Size of the available buffer.
5322 * @param[out] wrote Number of bytes written to the buffer.
5323 * @param[in] param The find_param structure associated with the file/dir
5324 * @param[in] path Pathname of the current file/dir being handled
5325 * @param[in] projid Project ID associated with the current file/dir
5326 * @param[in] d File descriptor for the directory (or -1 for a
5327 * non-directory file)
5328 * @return Number of characters from input string processed
5329 * as part of the format (0 for an unknown format)
5331 static int printf_format_directive(char *seq, char *buffer, size_t size,
5332 int *wrote, struct find_param *param,
5333 char *path, __u32 projid, int d)
5335 __u16 mode = param->fp_lmd->lmd_stx.stx_mode;
5336 uint64_t blocks = param->fp_lmd->lmd_stx.stx_blocks;
5337 int rc = 1; /* most specifiers are single character */
5345 case 'w': case 'W': /* timestamps */
5346 rc = printf_format_timestamp(seq, buffer, size, wrote, param);
5348 case 'b': /* file size (in 512B blocks) */
5349 *wrote = snprintf(buffer, size, "%"PRIu64, blocks);
5351 case 'G': /* GID of owner */
5352 *wrote = snprintf(buffer, size, "%u",
5353 param->fp_lmd->lmd_stx.stx_gid);
5355 case 'k': /* file size (in 1K blocks) */
5356 *wrote = snprintf(buffer, size, "%"PRIu64, (blocks + 1)/2);
5358 case 'L': /* Lustre-specific formats */
5359 rc = printf_format_lustre(seq, buffer, size, wrote, param,
5362 case 'm': /* file mode in octal */
5363 *wrote = snprintf(buffer, size, "%#o", (mode & (~S_IFMT)));
5365 case 'n': /* number of links */
5366 *wrote = snprintf(buffer, size, "%u",
5367 param->fp_lmd->lmd_stx.stx_nlink);
5369 case 'p': /* Path name of file */
5370 *wrote = snprintf(buffer, size, "%s", path);
5372 case 's': /* file size (in bytes) */
5373 *wrote = snprintf(buffer, size, "%"PRIu64,
5374 (uint64_t) param->fp_lmd->lmd_stx.stx_size);
5376 case 'U': /* UID of owner */
5377 *wrote = snprintf(buffer, size, "%u",
5378 param->fp_lmd->lmd_stx.stx_uid);
5380 case 'y': /* file type */
5383 else if (S_ISDIR(mode))
5385 else if (S_ISLNK(mode))
5387 else if (S_ISBLK(mode))
5389 else if (S_ISCHR(mode))
5391 else if (S_ISFIFO(mode))
5393 else if (S_ISSOCK(mode))
5403 default: /* invalid format specifier */
5409 /* output of snprintf was truncated */
5416 * Parse user-supplied string for the -printf option and interpret any
5417 * '%' format specifiers or '\' escape sequences.
5419 * @param[in] param The find_param struct containing the -printf string
5420 * as well as info about the current file/dir that mathced
5421 * the lfs find search criteria
5422 * @param[in] path Path name for current file/dir
5423 * @param[in] projid Project ID associated with current file/dir
5424 * @param[in] d File descriptor for current directory (or -1 for a
5425 * non-directory file)
5427 static void printf_format_string(struct find_param *param, char *path,
5428 __u32 projid, int d)
5430 char output[FORMATTED_BUF_LEN];
5431 char *fmt_char = param->fp_format_printf_str;
5432 char *buff = output;
5438 buff_size = FORMATTED_BUF_LEN;
5440 /* Always leave one free byte in buffer for trailing NUL */
5441 while (*fmt_char && (buff_size > 1)) {
5444 if (*fmt_char == '%') {
5445 rc = printf_format_directive(fmt_char + 1, buff,
5446 buff_size, &written, param,
5448 } else if (*fmt_char == '\\')
5449 rc = printf_format_escape(fmt_char + 1, buff,
5450 buff_size, &written);
5453 /* Either a '\' escape or '%' format was processed.
5454 * Increment pointers accordingly.
5456 fmt_char += (rc + 1);
5458 buff_size -= written;
5460 /* Regular char or invalid escape/format.
5461 * Either way, copy current character.
5463 *buff++ = *fmt_char++;
5468 /* Terminate output buffer and print */
5470 llapi_printf(LLAPI_MSG_NORMAL, "%s", output);
5474 * Get file/directory project id.
5475 * by the open fd resides on.
5476 * Return 0 and project id on success, or -ve errno.
5478 static int fget_projid(int fd, __u32 *projid)
5483 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
5485 *projid = INVALID_PROJID;
5489 *projid = fsx.fsx_projid;
5494 * Check that the file's permissions in *st matches the one in find_param
5496 static int check_file_permissions(const struct find_param *param,
5503 switch (param->fp_perm_sign) {
5504 case LFS_FIND_PERM_EXACT:
5505 decision = (mode == param->fp_perm);
5507 case LFS_FIND_PERM_ALL:
5508 decision = ((mode & param->fp_perm) == param->fp_perm);
5510 case LFS_FIND_PERM_ANY:
5511 decision = ((mode & param->fp_perm) != 0);
5515 if ((param->fp_exclude_perm && decision)
5516 || (!param->fp_exclude_perm && !decision))
5522 static int cb_find_init(char *path, int p, int *dp,
5523 void *data, struct dirent64 *de)
5525 struct find_param *param = (struct find_param *)data;
5526 struct lov_user_mds_data *lmd = param->fp_lmd;
5527 int d = dp == NULL ? -1 : *dp;
5528 int decision = 1; /* 1 is accepted; -1 is rejected. */
5530 int checked_type = 0;
5532 __u32 stripe_count = 0;
5535 __u32 projid = INVALID_PROJID;
5536 bool gather_all = false;
5538 if (p == -1 && d == -1)
5541 /* Reset this value between invocations */
5542 param->fp_get_lmv = 0;
5544 /* Gather all file/dir info, not just what's needed for search params */
5545 if (param->fp_format_printf_str)
5548 /* If a regular expression is presented, make the initial decision */
5549 if (param->fp_pattern != NULL) {
5550 char *fname = strrchr(path, '/');
5552 fname = (fname == NULL ? path : fname + 1);
5553 ret = fnmatch(param->fp_pattern, fname, 0);
5554 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
5555 (ret == 0 && param->fp_exclude_pattern))
5559 /* See if we can check the file type from the dirent. */
5560 if (de != NULL && de->d_type != DT_UNKNOWN) {
5561 if (param->fp_type != 0) {
5564 if (DTTOIF(de->d_type) == param->fp_type) {
5565 if (param->fp_exclude_type)
5568 if (!param->fp_exclude_type)
5572 if ((param->fp_check_mdt_count || param->fp_hash_type ||
5573 param->fp_check_hash_flag) && de->d_type != DT_DIR)
5580 * Request MDS for the stat info if some of these parameters need
5583 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
5584 param->fp_check_uid || param->fp_check_gid ||
5585 param->fp_newerxy || param->fp_btime ||
5586 param->fp_atime || param->fp_mtime || param->fp_ctime ||
5587 param->fp_check_size || param->fp_check_blocks ||
5588 find_check_lmm_info(param) ||
5589 param->fp_check_mdt_count || param->fp_hash_type ||
5590 param->fp_check_hash_flag || param->fp_perm_sign ||
5591 param->fp_nlink || param->fp_attrs || param->fp_neg_attrs ||
5595 if (param->fp_type != 0 && checked_type == 0)
5598 if (decision == 0) {
5600 (param->fp_check_mdt_count || param->fp_hash_type ||
5601 param->fp_check_hash_flag || param->fp_check_foreign ||
5603 * cb_get_dirstripe is needed when checking nlink because
5604 * nlink is handled differently for multi-stripe directory
5605 * vs. single-stripe directory
5607 param->fp_nlink || gather_all)) {
5608 param->fp_get_lmv = 1;
5609 ret = cb_get_dirstripe(path, &d, param);
5611 if (errno == ENODATA) {
5612 /* Fill in struct for unstriped dir */
5614 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
5615 /* Use 0 until we find actual offset */
5616 param->fp_lmv_md->lum_stripe_offset = 0;
5617 param->fp_lmv_md->lum_stripe_count = 0;
5618 param->fp_lmv_md->lum_hash_type = 0;
5620 if (param->fp_check_foreign) {
5621 if (param->fp_exclude_foreign)
5630 if (param->fp_check_mdt_count) {
5631 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5634 decision = find_value_cmp(param->fp_lmv_md->lum_stripe_count,
5635 param->fp_mdt_count,
5636 param->fp_mdt_count_sign,
5637 param->fp_exclude_mdt_count, 1, 0);
5642 if (param->fp_hash_type) {
5644 __u32 type = param->fp_lmv_md->lum_hash_type &
5647 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5650 found = (1 << type) & param->fp_hash_type;
5651 if ((found && param->fp_exclude_hash_type) ||
5652 (!found && !param->fp_exclude_hash_type))
5656 if (param->fp_check_hash_flag) {
5657 __u32 flags = param->fp_lmv_md->lum_hash_type &
5658 ~LMV_HASH_TYPE_MASK;
5660 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5663 if (!(flags & param->fp_hash_inflags) ||
5664 (flags & param->fp_hash_exflags))
5669 param->fp_lmd->lmd_lmm.lmm_magic = 0;
5670 ret = get_lmd_info_fd(path, p, d, param->fp_lmd,
5671 param->fp_lum_size, GET_LMD_INFO);
5672 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
5673 find_check_lmm_info(param)) {
5674 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
5677 * We need to "fake" the "use the default" values
5678 * since the lmm struct is zeroed out at this point.
5680 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5681 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
5683 ostid_set_seq(&lmm->lmm_oi,
5684 FID_SEQ_LOV_DEFAULT);
5685 lmm->lmm_stripe_size = 0;
5686 lmm->lmm_stripe_count = 0;
5687 lmm->lmm_stripe_offset = -1;
5689 if (ret == 0 && (param->fp_mdt_uuid != NULL || gather_all)) {
5691 ret = llapi_file_fget_mdtidx(d,
5692 ¶m->fp_file_mdt_index);
5694 * Make sure lum_stripe_offset matches
5695 * mdt_index even for unstriped directories.
5697 if (ret == 0 && param->fp_get_lmv)
5698 param->fp_lmv_md->lum_stripe_offset =
5699 param->fp_file_mdt_index;
5700 } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
5702 * FIXME: we could get the MDT index from the
5703 * file's FID in lmd->lmd_lmm.lmm_oi without
5704 * opening the file, once we are sure that
5705 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
5706 * That would still be an ioctl() to map the
5707 * FID to the MDT, but not an open RPC.
5709 fd = open(path, O_RDONLY);
5711 ret = llapi_file_fget_mdtidx(fd,
5712 ¶m->fp_file_mdt_index);
5718 * For a special file, we assume it resides on
5719 * the same MDT as the parent directory.
5721 ret = llapi_file_fget_mdtidx(p,
5722 ¶m->fp_file_mdt_index);
5733 stripe_count = find_get_stripe_count(param);
5737 /* Check the file permissions from the stat info */
5738 if (param->fp_perm_sign) {
5739 decision = check_file_permissions(param, lmd->lmd_stx.stx_mode);
5744 if (param->fp_type && !checked_type) {
5745 if ((param->fp_check_mdt_count || param->fp_check_hash_flag ||
5746 param->fp_hash_type) && !S_ISDIR(lmd->lmd_stx.stx_mode))
5749 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
5750 if (param->fp_exclude_type)
5753 if (!param->fp_exclude_type)
5759 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
5760 if (lustre_fs && param->fp_got_uuids &&
5761 param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
5762 lmd->lmd_stx.stx_dev_minor)) {
5763 /* A lustre/lustre mount point is crossed. */
5764 param->fp_got_uuids = 0;
5765 param->fp_obds_printed = 0;
5766 param->fp_mdt_index = OBD_NOT_FOUND;
5767 param->fp_obd_index = OBD_NOT_FOUND;
5770 if (lustre_fs && !param->fp_got_uuids) {
5771 ret = setup_target_indexes((d != -1) ? d : p, path,
5776 param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
5777 lmd->lmd_stx.stx_dev_minor);
5778 } else if (!lustre_fs && param->fp_got_uuids) {
5779 /* A lustre/non-lustre mount point is crossed. */
5780 param->fp_got_uuids = 0;
5781 param->fp_mdt_index = OBD_NOT_FOUND;
5782 param->fp_obd_index = OBD_NOT_FOUND;
5786 if (param->fp_check_foreign) {
5787 decision = find_check_foreign(param);
5792 if (param->fp_check_stripe_size) {
5793 decision = find_check_stripe_size(param);
5798 if (param->fp_check_ext_size) {
5799 decision = find_check_ext_size(param);
5804 if (param->fp_check_stripe_count) {
5805 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
5806 param->fp_stripe_count_sign,
5807 param->fp_exclude_stripe_count, 1, 0);
5812 if (param->fp_check_layout) {
5813 decision = find_check_layout(param);
5818 /* If an OBD UUID is specified but none matches, skip this file. */
5819 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
5820 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
5824 * If an OST or MDT UUID is given, and some OST matches,
5827 if (param->fp_obd_index != OBD_NOT_FOUND ||
5828 param->fp_mdt_index != OBD_NOT_FOUND) {
5829 if (param->fp_obd_uuid) {
5830 if (check_obd_match(param)) {
5832 * If no mdtuuid is given, we are done.
5833 * Otherwise, fall through to the mdtuuid
5836 if (!param->fp_mdt_uuid)
5843 if (param->fp_mdt_uuid) {
5844 if (check_mdt_match(param))
5851 if (param->fp_check_uid) {
5852 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
5853 if (param->fp_exclude_uid)
5856 if (!param->fp_exclude_uid)
5861 if (param->fp_check_gid) {
5862 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
5863 if (param->fp_exclude_gid)
5866 if (!param->fp_exclude_gid)
5871 if (param->fp_check_projid || gather_all) {
5873 fd = open(path, O_RDONLY | O_NONBLOCK);
5876 ret = fget_projid(fd, &projid);
5879 if (param->fp_check_projid) {
5882 if (projid == param->fp_projid) {
5883 if (param->fp_exclude_projid)
5886 if (!param->fp_exclude_projid)
5892 if (param->fp_check_pool) {
5893 decision = find_check_pool(param);
5898 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
5899 param->fp_check_comp_start || param->fp_check_comp_end) {
5900 decision = find_check_comp_options(param);
5905 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
5906 decision = find_check_mirror_options(param);
5911 /* Check the time on mds. */
5913 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
5916 for_mds = lustre_fs ?
5917 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5918 decision = find_time_check(param, for_mds);
5923 if (param->fp_btime) {
5924 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
5929 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
5930 param->fp_btime, param->fp_bsign,
5931 param->fp_exclude_btime,
5932 param->fp_time_margin, 0);
5937 if (param->fp_newerxy) {
5940 for_mds = lustre_fs ?
5941 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5942 decision = find_newerxy_check(param, for_mds, true);
5951 if (param->fp_attrs || param->fp_neg_attrs) {
5952 decision = find_check_attr_options(param);
5957 flags = param->fp_lmd->lmd_flags;
5958 if (param->fp_check_size &&
5959 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5960 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5961 !(flags & OBD_MD_FLSIZE ||
5962 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
5965 if (param->fp_check_blocks &&
5966 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5967 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5968 !(flags & OBD_MD_FLBLOCKS ||
5969 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
5972 if (param->fp_xattr_match_info) {
5973 decision = find_check_xattrs(path, param->fp_xattr_match_info);
5979 * When checking nlink, stat(2) is needed for multi-striped directories
5980 * because the nlink value retrieved from the MDS above comes from
5981 * the number of stripes for the dir.
5982 * The posix stat call below fills in the correct number of links.
5983 * Single-stripe directories and regular files already have the
5984 * correct nlink value.
5986 if (param->fp_nlink && S_ISDIR(lmd->lmd_stx.stx_mode) &&
5987 (param->fp_lmv_md->lum_stripe_count != 0))
5991 * If file still fits the request, ask ost for updated info.
5992 * The regular stat is almost of the same speed as some new
5993 * 'glimpse-size-ioctl'.
5995 if (!decision || gather_all) {
5999 * For regular files with the stripe the decision may have not
6000 * been taken yet if *time or size is to be checked.
6002 if (param->fp_obd_index != OBD_NOT_FOUND)
6003 print_failed_tgt(param, path, LL_STATFS_LOV);
6005 if (param->fp_mdt_index != OBD_NOT_FOUND)
6006 print_failed_tgt(param, path, LL_STATFS_LMV);
6009 ret = fstat_f(d, &st);
6010 else if (de != NULL)
6011 ret = fstatat_f(p, de->d_name, &st,
6012 AT_SYMLINK_NOFOLLOW);
6014 ret = lstat_f(path, &st);
6017 if (errno == ENOENT) {
6018 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
6019 "warning: %s: %s does not exist",
6024 llapi_error(LLAPI_MSG_ERROR, ret,
6025 "%s: stat on %s failed",
6031 convert_lmd_statx(param->fp_lmd, &st, true);
6032 /* Check the time on osc. */
6033 decision = find_time_check(param, 0);
6037 if (param->fp_newerxy) {
6038 decision = find_newerxy_check(param, 0, false);
6048 if (param->fp_nlink) {
6049 decision = find_value_cmp(lmd->lmd_stx.stx_nlink,
6050 param->fp_nlink, param->fp_nlink_sign,
6051 param->fp_exclude_nlink, 1, 0);
6056 if (param->fp_check_size) {
6057 decision = find_value_cmp(lmd->lmd_stx.stx_size,
6059 param->fp_size_sign,
6060 param->fp_exclude_size,
6061 param->fp_size_units, 0);
6066 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
6067 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
6069 param->fp_blocks_sign,
6070 param->fp_exclude_blocks,
6071 param->fp_blocks_units, 0);
6077 if (param->fp_format_printf_str)
6078 printf_format_string(param, path, projid, d);
6080 llapi_printf(LLAPI_MSG_NORMAL, "%s%c", path,
6081 param->fp_zero_end ? '\0' : '\n');
6085 /* Do not get down anymore? */
6086 if (param->fp_depth == param->fp_max_depth) {
6097 static int cb_migrate_mdt_init(char *path, int p, int *dp,
6098 void *param_data, struct dirent64 *de)
6100 struct find_param *param = (struct find_param *)param_data;
6101 struct lmv_user_md *lmu = param->fp_lmv_md;
6103 char raw[MAX_IOC_BUFLEN] = {'\0'};
6105 struct obd_ioctl_data data = { 0 };
6111 if (p == -1 && dp == NULL)
6117 if (dp != NULL && *dp != -1)
6121 tmp_p = open_parent(path);
6125 llapi_error(LLAPI_MSG_ERROR, ret,
6126 "can not open %s", path);
6131 path_copy = strdup(path);
6132 filename = basename(path_copy);
6134 data.ioc_inlbuf1 = (char *)filename;
6135 data.ioc_inllen1 = strlen(filename) + 1;
6136 data.ioc_inlbuf2 = (char *)lmu;
6137 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
6140 if (param->fp_depth == param->fp_max_depth)
6141 data.ioc_type = MDS_MIGRATE_NSONLY;
6142 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
6144 llapi_error(LLAPI_MSG_ERROR, ret,
6145 "%s: error packing ioctl data", __func__);
6150 ret = ioctl(tmp_p, LL_IOC_MIGRATE, rawbuf);
6152 if (errno == EBUSY && !retry) {
6154 * because migrate may not be able to lock all involved
6155 * objects in order, for some of them it try lock, while
6156 * there may be conflicting COS locks and cause migrate
6157 * fail with EBUSY, hope a sync() could cause
6158 * transaction commit and release these COS locks.
6163 } else if (errno == EALREADY) {
6164 if (param->fp_verbose & VERBOSE_DETAIL)
6165 llapi_printf(LLAPI_MSG_NORMAL,
6166 "%s migrated to MDT%d already\n",
6167 path, lmu->lum_stripe_offset);
6171 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
6175 } else if (param->fp_verbose & VERBOSE_DETAIL) {
6176 llapi_printf(LLAPI_MSG_NORMAL,
6177 "migrate %s to MDT%d stripe count %d\n",
6178 path, lmu->lum_stripe_offset,
6179 lmu->lum_stripe_count);
6183 /* Do not get down anymore? */
6184 if (param->fp_depth == param->fp_max_depth)
6191 * If the directory is being migration, we need
6192 * close the directory after migration,
6193 * so the old directory cache will be cleanup
6194 * on the client side, and re-open to get the
6195 * new directory handle
6197 *dp = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
6200 llapi_error(LLAPI_MSG_ERROR, ret,
6201 "%s: Failed to open '%s'", __func__, path);
6213 /* dir migration finished, shrink its stripes */
6214 static int cb_migrate_mdt_fini(char *path, int p, int *dp, void *data,
6215 struct dirent64 *de)
6217 struct find_param *param = data;
6218 struct lmv_user_md *lmu = param->fp_lmv_md;
6219 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
6222 if (de && de->d_type != DT_DIR)
6227 * close it before setxattr because the latter may destroy the
6228 * original object, and cause close fail.
6236 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
6237 if (ret == -EALREADY)
6240 cb_common_fini(path, p, dp, data, de);
6244 int llapi_migrate_mdt(char *path, struct find_param *param)
6246 param->fp_stop_on_error = 1;
6247 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
6251 int llapi_mv(char *path, struct find_param *param)
6253 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
6254 static bool printed;
6257 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
6258 "%s() is deprecated, use llapi_migrate_mdt() instead",
6263 return llapi_migrate_mdt(path, param);
6267 * Check string for escape sequences and print a message to stdout
6268 * if any invalid escapes are found.
6270 * @param[in] c Pointer to character immediately following the
6271 * '\' character indicating the start of an escape
6273 * @return Number of characters examined in the escape sequence
6274 * (regardless of whether the sequence is valid or not).
6276 static int validate_printf_esc(char *c)
6278 char *valid_esc = "nt\\";
6281 /* backslash at end of string */
6282 llapi_err_noerrno(LLAPI_MSG_WARN,
6283 "warning: '\\' at end of -printf format string\n");
6287 if (!strchr(valid_esc, *c))
6288 /* Invalid escape character */
6289 llapi_err_noerrno(LLAPI_MSG_WARN,
6290 "warning: unrecognized escape: '\\%c'\n", *c);
6296 * Check string for format directives and print a message to stdout
6297 * if any invalid directives are found.
6299 * @param[in] c Pointer to character immediately following the
6300 * '%' character indicating the start of a format
6302 * @return Number of characters examined in the format directive
6303 * (regardless of whether the directive is valid or not).
6305 static int validate_printf_fmt(char *c)
6307 char *valid_fmt_single = "abcGkmnpstUwy%";
6308 char *valid_fmt_double = "ACTW";
6309 char *valid_fmt_lustre = "aAcFhioPpS";
6310 char curr = *c, next;
6313 llapi_err_noerrno(LLAPI_MSG_WARN,
6314 "warning: '%%' at end of -printf format string\n");
6319 if ((next == '\0') || (next == '%') || (next == '\\'))
6320 /* Treat as single char format directive */
6323 /* Check format directives with multiple characters */
6324 if (strchr(valid_fmt_double, curr)) {
6325 /* For now, only valid formats are followed by '@' char */
6327 llapi_err_noerrno(LLAPI_MSG_WARN,
6328 "warning: unrecognized format directive: '%%%c%c'\n",
6333 /* Lustre formats always start with 'L' */
6335 if (!strchr(valid_fmt_lustre, next))
6336 llapi_err_noerrno(LLAPI_MSG_WARN,
6337 "warning: unrecognized format directive: '%%%c%c'\n",
6344 if (!strchr(valid_fmt_single, curr))
6345 llapi_err_noerrno(LLAPI_MSG_WARN,
6346 "warning: unrecognized format directive: '%%%c'\n", curr);
6351 * Validate the user-supplied string for the -printf option and report
6352 * any invalid backslash escape sequences or format directives.
6354 * @param[in] param Structure containing info about invocation of lfs find
6357 static void validate_printf_str(struct find_param *param)
6359 char *c = param->fp_format_printf_str;
6365 ret = validate_printf_fmt(++c);
6369 ret = validate_printf_esc(++c);
6379 int llapi_find(char *path, struct find_param *param)
6381 if (param->fp_format_printf_str)
6382 validate_printf_str(param);
6383 return param_callback(path, cb_find_init, cb_common_fini, param);
6387 * Get MDT number that the file/directory inode referenced
6388 * by the open fd resides on.
6389 * Return 0 and mdtidx on success, or -ve errno.
6391 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
6393 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
6398 static int cb_get_mdt_index(char *path, int p, int *dp, void *data,
6399 struct dirent64 *de)
6401 struct find_param *param = (struct find_param *)data;
6402 int d = dp == NULL ? -1 : *dp;
6405 bool hex = param->fp_hex_idx;
6407 if (p == -1 && d == -1)
6411 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6412 } else /* if (p != -1) */ {
6415 fd = open(path, O_RDONLY | O_NOCTTY);
6417 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
6425 if (ret == -ENODATA) {
6426 if (!param->fp_obd_uuid)
6427 llapi_printf(LLAPI_MSG_NORMAL,
6428 "'%s' has no stripe info\n", path);
6430 } else if (ret == -ENOENT) {
6431 llapi_error(LLAPI_MSG_WARN, ret,
6432 "warning: %s: '%s' does not exist",
6435 } else if (ret == -ENOTTY) {
6436 llapi_error(LLAPI_MSG_ERROR, ret,
6437 "%s: '%s' not on a Lustre fs",
6440 llapi_error(LLAPI_MSG_ERROR, ret,
6441 "error: %s: '%s' failed get_mdtidx",
6447 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
6448 llapi_printf(LLAPI_MSG_NORMAL, hex ? "%#x\n" : "%d\n", mdtidx);
6450 llapi_printf(LLAPI_MSG_NORMAL, hex ? "%s\nmdt_index:\t%#x\n"
6451 : "%s\nmdt_index:\t%d\n",
6455 /* Do not go down anymore? */
6456 if (param->fp_depth == param->fp_max_depth)
6464 static int cb_getstripe(char *path, int p, int *dp, void *data,
6465 struct dirent64 *de)
6467 struct find_param *param = (struct find_param *)data;
6468 int d = dp == NULL ? -1 : *dp, fd = -1;
6471 if (p == -1 && d == -1)
6474 if (param->fp_obd_uuid) {
6475 param->fp_quiet = 1;
6476 ret = setup_obd_uuid(d != -1 ? d : p, path, param);
6481 if (!param->fp_no_follow && de && de->d_type == DT_LNK && d == -1)
6482 d = fd = open(path, O_RDONLY | O_DIRECTORY);
6484 if (d != -1 && (param->fp_get_lmv || param->fp_get_default_lmv))
6485 ret = cb_get_dirstripe(path, &d, param);
6487 ret = get_lmd_info_fd(path, p, d, ¶m->fp_lmd->lmd_lmm,
6488 param->fp_lum_size, GET_LMD_STRIPE);
6489 else if (d == -1 && (param->fp_get_lmv || param->fp_get_default_lmv)) {
6490 /* in case of a dangling or valid faked symlink dir, opendir()
6491 * should have return either EINVAL or ENOENT, so let's try
6492 * to get LMV just in case, and by opening it as a file but
6493 * with O_NOFOLLOW ...
6495 int flag = O_RDONLY;
6497 if (param->fp_no_follow)
6498 flag = O_RDONLY | O_NOFOLLOW;
6499 fd = open(path, flag);
6503 ret = cb_get_dirstripe(path, &fd, param);
6505 llapi_lov_dump_user_lmm(param, path, LDF_IS_DIR);
6508 } else if (d == -1) {
6509 if (!param->fp_no_follow && de && de->d_type == DT_LNK) {
6510 /* open the target of symlink as a file */
6511 fd = open(path, O_RDONLY);
6515 ret = get_lmd_info_fd(path, p, fd, ¶m->fp_lmd->lmd_lmm,
6516 param->fp_lum_size, GET_LMD_STRIPE);
6524 if (errno == ENODATA && d != -1) {
6526 * We need to "fake" the "use the default" values
6527 * since the lmm struct is zeroed out at this point.
6528 * The magic needs to be set in order to satisfy
6529 * a check later on in the code path.
6530 * The object_seq needs to be set for the "(Default)"
6531 * prefix to be displayed.
6533 if (param->fp_get_default_lmv) {
6534 struct lmv_user_md *lum = param->fp_lmv_md;
6538 lum->lum_magic = LMV_USER_MAGIC;
6539 lum->lum_stripe_count = 0;
6540 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
6542 } else if (param->fp_get_lmv) {
6543 struct lmv_user_md *lum = param->fp_lmv_md;
6546 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6549 lum->lum_magic = LMV_MAGIC_V1;
6550 lum->lum_stripe_count = 0;
6551 lum->lum_stripe_offset = mdtidx;
6554 struct lov_user_md *lmm =
6555 ¶m->fp_lmd->lmd_lmm;
6557 lmm->lmm_magic = LOV_USER_MAGIC_V1;
6559 ostid_set_seq(&lmm->lmm_oi,
6560 FID_SEQ_LOV_DEFAULT);
6561 lmm->lmm_stripe_count = 0;
6562 lmm->lmm_stripe_size = 0;
6563 lmm->lmm_stripe_offset = -1;
6566 } else if (errno == ENODATA && p != -1) {
6567 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
6568 llapi_printf(LLAPI_MSG_NORMAL,
6569 "%s has no stripe info\n", path);
6571 } else if (errno == ENOENT) {
6572 llapi_error(LLAPI_MSG_WARN, -ENOENT,
6573 "warning: %s: %s does not exist",
6576 } else if (errno == ENOTTY) {
6578 llapi_error(LLAPI_MSG_ERROR, ret,
6579 "%s: '%s' not on a Lustre fs?",
6584 llapi_error(LLAPI_MSG_ERROR, ret,
6585 "error: %s: %s failed for %s",
6587 "LL_IOC_LOV_GETSTRIPE" :
6588 "IOC_MDC_GETFILESTRIPE", path);
6595 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
6596 llapi_lov_dump_user_lmm(param, path, d != -1 ? LDF_IS_DIR : 0);
6599 /* Do not get down anymore? */
6600 if (param->fp_depth == param->fp_max_depth)
6608 int llapi_getstripe(char *path, struct find_param *param)
6610 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
6611 cb_get_mdt_index : cb_getstripe,
6612 cb_common_fini, param);
6615 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
6616 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6618 char raw[MAX_IOC_BUFLEN] = {'\0'};
6620 struct obd_ioctl_data data = { 0 };
6623 data.ioc_inlbuf1 = (char *)&type;
6624 data.ioc_inllen1 = sizeof(__u32);
6625 data.ioc_inlbuf2 = (char *)&index;
6626 data.ioc_inllen2 = sizeof(__u32);
6627 data.ioc_pbuf1 = (char *)stat_buf;
6628 data.ioc_plen1 = sizeof(struct obd_statfs);
6629 data.ioc_pbuf2 = (char *)uuid_buf;
6630 data.ioc_plen2 = sizeof(struct obd_uuid);
6632 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
6634 llapi_error(LLAPI_MSG_ERROR, rc,
6635 "%s: error packing ioctl data", __func__);
6639 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
6641 return rc < 0 ? -errno : 0;
6644 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
6645 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6650 fd = open(path, O_RDONLY);
6653 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
6656 * If we can't even open a file on the filesystem (e.g. with
6657 * -ESHUTDOWN), force caller to exit or it will loop forever.
6662 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
6669 #define MAX_STRING_SIZE 128
6671 int llapi_ping(char *obd_type, char *obd_name)
6673 int flags = O_RDONLY;
6674 char buf[1] = { 0 };
6678 rc = cfs_get_param_paths(&path, "%s/%s/ping",
6679 obd_type, obd_name);
6683 fd = open(path.gl_pathv[0], flags);
6685 if (errno == EACCES && flags == O_RDONLY) {
6690 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
6695 if (flags == O_RDONLY)
6696 rc = read(fd, buf, sizeof(buf));
6698 rc = write(fd, buf, sizeof(buf));
6706 cfs_free_param_data(&path);
6710 int llapi_target_iterate(int type_num, char **obd_type,
6711 void *args, llapi_cb_t cb)
6717 for (i = 0; i < type_num; i++) {
6720 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
6724 for (j = 0; j < param.gl_pathc; j++) {
6725 char obd_uuid[UUID_MAX + 1];
6729 fp = fopen(param.gl_pathv[j], "r");
6732 llapi_error(LLAPI_MSG_ERROR, rc,
6733 "error: opening '%s'",
6738 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
6740 llapi_error(LLAPI_MSG_ERROR, rc,
6741 "error: reading '%s'",
6746 /* Extract the obd_name from the sysfs path.
6747 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
6749 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
6755 /* skip /fs/lustre/'obd_type'/ */
6756 obd_name += strlen(obd_type[i]) + 12;
6757 /* chop off after obd_name */
6758 ptr = strrchr(obd_name, '/');
6762 cb(obd_type[i], obd_name, obd_uuid, args);
6767 cfs_free_param_data(¶m);
6772 cfs_free_param_data(¶m);
6776 struct check_target_filter {
6781 static void do_target_check(char *obd_type_name, char *obd_name,
6782 char *obd_uuid, void *args)
6785 struct check_target_filter *filter = args;
6787 if (filter != NULL) {
6788 /* check nid if obd type is mgc */
6789 if (strcmp(obd_type_name, "mgc") == 0) {
6790 if (strcmp(obd_name + 3, filter->nid) != 0)
6793 /* check instance for other types of device (osc/mdc) */
6794 else if (strstr(obd_name, filter->instance) == NULL)
6798 rc = llapi_ping(obd_type_name, obd_name);
6800 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
6802 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
6804 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
6807 int llapi_target_check(int type_num, char **obd_type, char *dir)
6809 char nid[MAX_LINE_LEN], instance[MAX_INSTANCE_LEN];
6810 struct check_target_filter filter = {NULL, NULL};
6813 if (dir == NULL || dir[0] == '\0')
6814 return llapi_target_iterate(type_num, obd_type, NULL,
6817 rc = get_root_path(WANT_NID | WANT_ERROR, NULL, NULL, dir, -1, NULL,
6820 llapi_error(LLAPI_MSG_ERROR, rc,
6821 "cannot get nid of path '%s'", dir);
6826 rc = llapi_get_instance(dir, instance, ARRAY_SIZE(instance));
6829 filter.instance = instance;
6831 return llapi_target_iterate(type_num, obd_type, &filter,
6835 #undef MAX_STRING_SIZE
6837 /* Is this a lustre fs? */
6838 int llapi_is_lustre_mnttype(const char *type)
6840 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
6843 /* Is this a lustre client fs? */
6844 int llapi_is_lustre_mnt(struct mntent *mnt)
6846 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
6847 strstr(mnt->mnt_fsname, ":/") != NULL);
6850 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
6852 char fsname[PATH_MAX + 1];
6856 rc = llapi_search_fsname(mnt, fsname);
6860 root = open(mnt, O_RDONLY | O_DIRECTORY);
6863 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
6867 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
6870 if (rc == -ENOENT && LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd))
6871 llapi_error(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, rc,
6872 "Cannot find pool '%s'", qctl->qc_poolname);
6878 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
6883 root = open(mnt, O_RDONLY | O_DIRECTORY);
6886 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
6890 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
6893 llapi_error(LLAPI_MSG_ERROR, rc,
6894 "ioctl on %s for getting connect flags failed", mnt);
6901 * Flush cached pages from all clients.
6903 * \param fd File descriptor
6907 int llapi_file_flush(int fd)
6911 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);