4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/utils/liblustreapi.c
33 * Author: Peter J. Braam <braam@clusterfs.com>
34 * Author: Phil Schwan <phil@clusterfs.com>
35 * Author: Robert Read <rread@clusterfs.com>
49 #include <sys/ioctl.h>
56 #include <sys/statfs.h>
57 #include <sys/syscall.h>
59 #include <sys/types.h>
60 #include <sys/xattr.h>
61 #include <sys/sysmacros.h>
64 #include <libgen.h> /* for dirname() */
65 #include <linux/limits.h>
66 #ifdef HAVE_LINUX_UNISTD_H
67 #include <linux/unistd.h>
76 #include <libcfs/util/ioctl.h>
77 #include <libcfs/util/param.h>
78 #include <libcfs/util/string.h>
79 #include <linux/lnet/lnetctl.h>
80 #include <lustre/lustreapi.h>
81 #include <linux/lustre/lustre_ostid.h>
82 #include <linux/lustre/lustre_ioctl.h>
83 #include "lustreapi_internal.h"
86 #define FORMATTED_BUF_LEN 1024
88 #ifndef INVALID_PROJID
89 #define INVALID_PROJID -1
92 static int llapi_msg_level = LLAPI_MSG_MAX;
93 const char *liblustreapi_cmd;
95 struct lustre_foreign_type lu_foreign_types[] = {
96 {.lft_type = LU_FOREIGN_TYPE_NONE, .lft_name = "none"},
97 {.lft_type = LU_FOREIGN_TYPE_SYMLINK, .lft_name = "symlink"},
98 /* must be the last element */
99 {.lft_type = LU_FOREIGN_TYPE_UNKNOWN, .lft_name = NULL}
100 /* array max dimension must be <= UINT32_MAX */
103 void llapi_msg_set_level(int level)
105 /* ensure level is in the good range */
106 if (level < LLAPI_MSG_OFF)
107 llapi_msg_level = LLAPI_MSG_OFF;
108 else if (level > LLAPI_MSG_MAX)
109 llapi_msg_level = LLAPI_MSG_MAX;
111 llapi_msg_level = level;
114 int llapi_msg_get_level(void)
116 return llapi_msg_level;
119 void llapi_set_command_name(const char *cmd)
121 liblustreapi_cmd = cmd;
124 void llapi_clear_command_name(void)
126 liblustreapi_cmd = NULL;
129 static void error_callback_default(enum llapi_message_level level, int err,
130 const char *fmt, va_list ap)
132 bool has_nl = strchr(fmt, '\n') != NULL;
134 if (liblustreapi_cmd != NULL)
135 fprintf(stderr, "%s %s: ", program_invocation_short_name,
138 fprintf(stderr, "%s: ", program_invocation_short_name);
141 if (level & LLAPI_MSG_NO_ERRNO) {
142 vfprintf(stderr, fmt, ap);
144 fprintf(stderr, "\n");
149 * Remove trailing linefeed so error string can be appended.
150 * @fmt is a const string, so we can't modify it directly.
152 if (has_nl && (newfmt = strdup(fmt)))
153 *strrchr(newfmt, '\n') = '\0';
155 newfmt = (char *)fmt;
157 vfprintf(stderr, newfmt, ap);
160 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
164 static void info_callback_default(enum llapi_message_level level, int err,
165 const char *fmt, va_list ap)
168 if (liblustreapi_cmd != NULL) {
169 fprintf(stdout, "%s %s: ",
170 program_invocation_short_name,
173 fprintf(stdout, "%s: ", program_invocation_short_name);
176 vfprintf(stdout, fmt, ap);
179 static llapi_log_callback_t llapi_error_callback = error_callback_default;
180 static llapi_log_callback_t llapi_info_callback = info_callback_default;
183 /* llapi_error will preserve errno */
184 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
187 int tmp_errno = errno;
189 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
193 llapi_error_callback(level, abs(err), fmt, args);
198 /* llapi_printf will preserve errno */
199 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
202 int tmp_errno = errno;
204 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
208 llapi_info_callback(level, 0, fmt, args);
214 * Set a custom error logging function. Passing in NULL will reset the logging
215 * callback to its default value.
217 * This function returns the value of the old callback.
219 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
221 llapi_log_callback_t old = llapi_error_callback;
224 llapi_error_callback = cb;
226 llapi_error_callback = error_callback_default;
232 * Set a custom info logging function. Passing in NULL will reset the logging
233 * callback to its default value.
235 * This function returns the value of the old callback.
237 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
239 llapi_log_callback_t old = llapi_info_callback;
242 llapi_info_callback = cb;
244 llapi_info_callback = info_callback_default;
250 * Convert a size string (with optional suffix) into binary value.
252 * \param optarg [in] string containing numeric value with optional
253 * KMGTPE suffix to specify the unit size.
254 * The \a string may be a decimal value.
255 * \param size [out] pointer to integer numeric value to be returned
256 * \param size_units [in] units of \a string if dimensionless. Must be
257 * initialized by caller. If zero, units = bytes.
258 * \param bytes_spec [in] if suffix 'b' means bytes or 512-byte sectors.
261 * \retval -EINVAL negative or too large size, or unknown suffix
263 int llapi_parse_size(const char *optarg, unsigned long long *size,
264 unsigned long long *size_units, int bytes_spec)
267 char *argbuf = (char *)optarg;
268 unsigned long long frac = 0, frac_d = 1;
270 if (strncmp(optarg, "-", 1) == 0)
273 if (*size_units == 0)
276 *size = strtoull(argbuf, &end, 0);
277 if (end != NULL && *end == '.') {
281 frac = strtoull(argbuf, &end, 10);
282 /* count decimal places */
283 for (i = 0; i < (end - argbuf); i++)
288 if ((*end == 'b') && *(end + 1) == '\0' &&
289 (*size & (~0ULL << (64 - 9))) == 0 &&
291 *size_units = 1 << 9;
292 } else if ((*end == 'b') &&
293 *(end + 1) == '\0' &&
296 } else if ((*end == 'k' || *end == 'K') &&
297 *(end + 1) == '\0' &&
298 (*size & (~0ULL << (64 - 10))) == 0) {
299 *size_units = 1 << 10;
300 } else if ((*end == 'm' || *end == 'M') &&
301 *(end + 1) == '\0' &&
302 (*size & (~0ULL << (64 - 20))) == 0) {
303 *size_units = 1 << 20;
304 } else if ((*end == 'g' || *end == 'G') &&
305 *(end + 1) == '\0' &&
306 (*size & (~0ULL << (64 - 30))) == 0) {
307 *size_units = 1 << 30;
308 } else if ((*end == 't' || *end == 'T') &&
309 *(end + 1) == '\0' &&
310 (*size & (~0ULL << (64 - 40))) == 0) {
311 *size_units = 1ULL << 40;
312 } else if ((*end == 'p' || *end == 'P') &&
313 *(end + 1) == '\0' &&
314 (*size & (~0ULL << (64 - 50))) == 0) {
315 *size_units = 1ULL << 50;
316 } else if ((*end == 'e' || *end == 'E') &&
317 *(end + 1) == '\0' &&
318 (*size & (~0ULL << (64 - 60))) == 0) {
319 *size_units = 1ULL << 60;
324 *size = *size * *size_units + frac * *size_units / frac_d;
330 * Verify the setstripe parameters before using.
331 * This is a pair method for comp_args_to_layout()/llapi_layout_sanity_cb()
332 * when just 1 component or a non-PFL layout is given.
334 * \param[in] param stripe parameters
335 * \param[in] pool_name pool name
336 * \param[in] fsname lustre FS name
339 * < 0, error code on failre
341 static int llapi_stripe_param_verify(const struct llapi_stripe_param *param,
342 const char **pool_name)
345 static int page_size;
348 if (page_size == 0) {
350 * 64 KB is the largest common page size (on ia64/PPC/ARM),
351 * but check the local page size just in case. The page_size
352 * will not change for the lifetime of this process at least.
354 page_size = LOV_MIN_STRIPE_SIZE;
355 if (getpagesize() > page_size) {
356 page_size = getpagesize();
357 llapi_err_noerrno(LLAPI_MSG_WARN,
358 "warning: page size (%u) larger than expected (%u)",
359 page_size, LOV_MIN_STRIPE_SIZE);
362 if (!llapi_stripe_size_is_aligned(param->lsp_stripe_size)) {
364 llapi_error(LLAPI_MSG_ERROR, rc,
365 "error: bad stripe_size %llu, must be an even multiple of %d bytes",
366 param->lsp_stripe_size, page_size);
369 if (!llapi_stripe_index_is_valid(param->lsp_stripe_offset)) {
371 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
372 param->lsp_stripe_offset);
375 if (llapi_stripe_size_is_too_big(param->lsp_stripe_size)) {
377 llapi_error(LLAPI_MSG_ERROR, rc,
378 "error: stripe size '%llu' over 4GB limit",
379 param->lsp_stripe_size);
383 count = param->lsp_stripe_count;
384 if (param->lsp_stripe_pattern & LOV_PATTERN_MDT) {
386 llapi_error(LLAPI_MSG_ERROR, rc,
387 "Invalid pattern: '-L mdt', must be specified "
391 if (!llapi_stripe_count_is_valid(count)) {
393 llapi_error(LLAPI_MSG_ERROR, rc,
394 "Invalid stripe count %d\n", count);
399 /* Make sure we have a good pool */
400 if (*pool_name != NULL) {
401 if (!llapi_pool_name_is_valid(pool_name)) {
403 llapi_error(LLAPI_MSG_ERROR, rc,
404 "Invalid Poolname '%s'", *pool_name);
414 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
419 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
421 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
425 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
427 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
432 if (!llapi_dir_hash_type_is_valid(hash_type)) {
434 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
442 * Trim a trailing newline from a string, if it exists.
444 int llapi_chomp_string(char *buf)
460 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
462 static int get_param_lov(const char *path, const char *param,
463 char *buf, size_t buf_size)
465 struct obd_uuid uuid;
468 rc = llapi_file_get_lov_uuid(path, &uuid);
472 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
477 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
479 static int get_param_lmv(const char *path, const char *param,
480 char *buf, size_t buf_size)
482 struct obd_uuid uuid;
485 rc = llapi_file_get_lmv_uuid(path, &uuid);
489 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
493 static int get_mds_md_size(const char *path)
495 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
498 * Rather than open the file and do the ioctl to get the
499 * instance name and close the file and search for the param
500 * file and open the param file and read the param file and
501 * parse the value and close the param file, let's just return
502 * a large enough value. It's 2020, RAM is cheap and this is
506 if (md_size < XATTR_SIZE_MAX)
507 md_size = XATTR_SIZE_MAX;
512 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
514 return get_param_lmv(path, "uuid", buf, bufsize);
518 * Open a Lustre file.
520 * \param name the name of the file to be opened
521 * \param flags access mode, see flags in open(2)
522 * \param mode permission of the file if it is created, see mode in open(2)
523 * \param param stripe pattern of the newly created file
525 * \retval file descriptor of opened file
526 * \retval negative errno on failure
528 int llapi_file_open_param(const char *name, int flags, mode_t mode,
529 const struct llapi_stripe_param *param)
531 struct lov_user_md *lum = NULL;
532 const char *pool_name = param->lsp_pool;
536 /* Check if the stripe pattern is sane. */
537 rc = llapi_stripe_param_verify(param, &pool_name);
541 if (param->lsp_is_specific)
542 lum_size = lov_user_md_size(param->lsp_stripe_count,
543 LOV_USER_MAGIC_SPECIFIC);
545 lum_size = sizeof(struct lov_user_md_v3);
547 lum_size = sizeof(*lum);
549 lum = calloc(1, lum_size);
554 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
556 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
557 flags = O_DIRECTORY | O_RDONLY;
564 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
569 /* Initialize IOCTL striping pattern structure */
570 lum->lmm_magic = LOV_USER_MAGIC_V1;
571 lum->lmm_pattern = param->lsp_stripe_pattern;
572 lum->lmm_stripe_size = param->lsp_stripe_size;
573 lum->lmm_stripe_count = param->lsp_stripe_count;
574 lum->lmm_stripe_offset = param->lsp_stripe_offset;
575 if (pool_name != NULL) {
576 struct lov_user_md_v3 *lumv3 = (void *)lum;
578 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
579 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
581 if (param->lsp_is_specific) {
582 struct lov_user_md_v3 *lumv3 = (void *)lum;
585 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
586 if (pool_name == NULL) {
588 * LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
589 * OST list, therefore if pool is not specified we have
590 * to pack a null pool name for placeholder.
592 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
595 for (i = 0; i < param->lsp_stripe_count; i++)
596 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
599 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
600 char errmsg[512] = "stripe already set";
603 if (errno != EEXIST && errno != EALREADY)
604 strncpy(errmsg, strerror(errno), sizeof(errmsg) - 1);
605 if (rc == -EREMOTEIO)
606 snprintf(errmsg, sizeof(errmsg),
607 "inactive OST among your specified %d OST(s)",
608 param->lsp_stripe_count);
610 llapi_err_noerrno(LLAPI_MSG_ERROR,
611 "setstripe error for '%s': %s", name, errmsg);
622 int llapi_file_is_encrypted(int fd)
627 rc = ioctl(fd, FS_IOC_GETFLAGS, &flags);
631 return !!(flags & LUSTRE_ENCRYPT_FL);
634 int llapi_file_open_pool(const char *name, int flags, int mode,
635 unsigned long long stripe_size, int stripe_offset,
636 int stripe_count, int stripe_pattern, char *pool_name)
638 const struct llapi_stripe_param param = {
639 .lsp_stripe_size = stripe_size,
640 .lsp_stripe_count = stripe_count,
641 .lsp_stripe_pattern = stripe_pattern,
642 .lsp_stripe_offset = stripe_offset,
643 .lsp_pool = pool_name
645 return llapi_file_open_param(name, flags, mode, ¶m);
648 int llapi_file_open(const char *name, int flags, int mode,
649 unsigned long long stripe_size, int stripe_offset,
650 int stripe_count, int stripe_pattern)
652 return llapi_file_open_pool(name, flags, mode, stripe_size,
653 stripe_offset, stripe_count,
654 stripe_pattern, NULL);
657 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
658 __u32 flags, char *foreign_lov)
661 struct lov_foreign_md *lfm;
664 if (foreign_lov == NULL) {
666 llapi_error(LLAPI_MSG_ERROR, rc,
667 "foreign LOV EA content must be provided");
671 len = strlen(foreign_lov);
672 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
675 llapi_error(LLAPI_MSG_ERROR, rc,
676 "foreign LOV EA size %zu (must be 0 < len < %zu)",
677 len, XATTR_SIZE_MAX -
678 offsetof(struct lov_foreign_md, lfm_value));
682 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
685 llapi_error(LLAPI_MSG_ERROR, rc,
686 "failed to allocate lov_foreign_md");
690 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
693 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
697 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
698 lfm->lfm_length = len;
699 lfm->lfm_type = type;
700 lfm->lfm_flags = flags;
701 memcpy(lfm->lfm_value, foreign_lov, len);
703 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
704 char *errmsg = "stripe already set";
708 errmsg = "not on a Lustre filesystem";
709 else if (errno == EEXIST || errno == EALREADY)
710 errmsg = "stripe already set";
712 errmsg = strerror(errno);
714 llapi_err_noerrno(LLAPI_MSG_ERROR,
715 "setstripe error for '%s': %s", name, errmsg);
731 int llapi_file_create(const char *name, unsigned long long stripe_size,
732 int stripe_offset, int stripe_count, int stripe_pattern)
736 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
737 stripe_offset, stripe_count, stripe_pattern,
746 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
747 int stripe_offset, int stripe_count,
748 int stripe_pattern, char *pool_name)
752 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
753 stripe_offset, stripe_count, stripe_pattern,
762 static int verify_dir_param(const char *name,
763 const struct llapi_stripe_param *param)
765 char fsname[MAX_OBD_NAME + 1] = { 0 };
766 char *pool_name = param->lsp_pool;
769 /* Make sure we are on a Lustre file system */
770 rc = llapi_search_fsname(name, fsname);
772 llapi_error(LLAPI_MSG_ERROR, rc,
773 "'%s' is not on a Lustre filesystem",
778 /* Check if the stripe pattern is sane. */
779 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
780 param->lsp_stripe_count,
781 param->lsp_stripe_pattern);
785 /* Make sure we have a good pool */
786 if (pool_name != NULL) {
788 * in case user gives the full pool name <fsname>.<poolname>,
791 char *ptr = strchr(pool_name, '.');
795 if (strcmp(pool_name, fsname) != 0) {
797 llapi_err_noerrno(LLAPI_MSG_ERROR,
798 "Pool '%s' is not on filesystem '%s'",
805 /* Make sure the pool exists and is non-empty */
806 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
808 char *err = rc == 0 ? "has no OSTs" : "does not exist";
810 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
811 fsname, pool_name, err);
816 /* sanity check of target list */
817 if (param->lsp_is_specific) {
818 char mdtname[MAX_OBD_NAME + 64];
822 for (i = 0; i < param->lsp_stripe_count; i++) {
823 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
824 fsname, param->lsp_tgts[i]);
825 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
830 llapi_error(LLAPI_MSG_ERROR, rc,
831 "%s: cannot find MDT %s in %s",
838 /* Make sure stripe offset is in MDT list. */
839 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
843 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
844 "%s: stripe offset '%d' is not in the target list",
845 __func__, param->lsp_stripe_offset);
853 static inline void param2lmu(struct lmv_user_md *lmu,
854 const struct llapi_stripe_param *param)
856 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
858 lmu->lum_stripe_count = param->lsp_stripe_count;
859 lmu->lum_stripe_offset = param->lsp_stripe_offset;
860 lmu->lum_hash_type = param->lsp_stripe_pattern;
861 lmu->lum_max_inherit = param->lsp_max_inherit;
862 lmu->lum_max_inherit_rr = param->lsp_max_inherit_rr;
863 if (param->lsp_pool != NULL)
864 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
865 if (param->lsp_is_specific) {
868 for (i = 0; i < param->lsp_stripe_count; i++)
869 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
873 int llapi_dir_set_default_lmv(const char *name,
874 const struct llapi_stripe_param *param)
876 struct lmv_user_md lmu = { 0 };
880 rc = verify_dir_param(name, param);
884 /* TODO: default lmv doesn't support specific targets yet */
885 if (param->lsp_is_specific)
888 param2lmu(&lmu, param);
890 fd = open(name, O_DIRECTORY | O_RDONLY);
893 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
897 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
899 char *errmsg = "stripe already set";
902 if (errno != EEXIST && errno != EALREADY)
903 errmsg = strerror(errno);
905 llapi_err_noerrno(LLAPI_MSG_ERROR,
906 "default dirstripe error on '%s': %s",
913 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
914 int stripe_count, int stripe_pattern,
915 const char *pool_name)
917 const struct llapi_stripe_param param = {
918 .lsp_stripe_count = stripe_count,
919 .lsp_stripe_offset = stripe_offset,
920 .lsp_stripe_pattern = stripe_pattern,
921 .lsp_pool = (char *)pool_name
924 return llapi_dir_set_default_lmv(name, ¶m);
928 * Create a Lustre directory.
930 * \param name the name of the directory to be created
931 * \param mode permission of the file if it is created, see mode in open(2)
932 * \param param stripe pattern of the newly created directory
934 * \retval 0 on success
935 * \retval negative errno on failure
937 int llapi_dir_create(const char *name, mode_t mode,
938 const struct llapi_stripe_param *param)
940 struct lmv_user_md *lmu = NULL;
942 struct obd_ioctl_data data = { 0 };
945 char *dirpath = NULL;
946 char *namepath = NULL;
951 rc = verify_dir_param(name, param);
955 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
956 param->lsp_is_specific ?
957 LMV_USER_MAGIC_SPECIFIC :
960 lmu = calloc(1, lmu_size);
964 dirpath = strdup(name);
970 namepath = strdup(name);
977 param2lmu(lmu, param);
979 filename = basename(namepath);
980 dir = dirname(dirpath);
982 data.ioc_inlbuf1 = (char *)filename;
983 data.ioc_inllen1 = strlen(filename) + 1;
984 data.ioc_inlbuf2 = (char *)lmu;
985 data.ioc_inllen2 = lmu_size;
986 data.ioc_type = mode;
987 if (param->lsp_is_create)
988 /* borrow obdo1.o_flags to store this flag */
989 data.ioc_obdo1.o_flags = OBD_FL_OBDMDEXISTS;
990 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
992 llapi_error(LLAPI_MSG_ERROR, rc,
993 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
998 fd = open(dir, O_DIRECTORY | O_RDONLY);
1001 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1005 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1006 char *errmsg = "stripe already set";
1009 if (errno != EEXIST && errno != EALREADY)
1010 errmsg = strerror(errno);
1012 llapi_err_noerrno(LLAPI_MSG_ERROR,
1013 "dirstripe error on '%s': %s", name, errmsg);
1024 * Create a foreign directory.
1026 * \param name the name of the directory to be created
1027 * \param mode permission of the file if it is created, see mode in open(2)
1028 * \param type foreign type to be set in LMV EA
1029 * \param flags foreign flags to be set in LMV EA
1030 * \param value foreign pattern to be set in LMV EA
1032 * \retval 0 on success
1033 * \retval negative errno on failure
1035 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1036 __u32 flags, const char *value)
1038 struct lmv_foreign_md *lfm = NULL;
1039 size_t lfm_size, len;
1040 struct obd_ioctl_data data = { 0 };
1043 char *dirpath = NULL;
1044 char *namepath = NULL;
1049 len = strlen(value);
1050 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1053 llapi_error(LLAPI_MSG_ERROR, rc,
1054 "invalid LOV EA length %zu (must be 0 < len < %zu)",
1055 len, XATTR_SIZE_MAX -
1056 offsetof(struct lmv_foreign_md, lfm_value));
1059 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1060 lfm = calloc(1, lfm_size);
1064 dirpath = strdup(name);
1070 namepath = strdup(name);
1077 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1078 lfm->lfm_length = len;
1079 lfm->lfm_type = type;
1080 lfm->lfm_flags = flags;
1081 memcpy(lfm->lfm_value, value, len);
1083 filename = basename(namepath);
1084 dir = dirname(dirpath);
1086 data.ioc_inlbuf1 = (char *)filename;
1087 data.ioc_inllen1 = strlen(filename) + 1;
1088 data.ioc_inlbuf2 = (char *)lfm;
1089 data.ioc_inllen2 = lfm_size;
1090 data.ioc_type = mode;
1091 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1093 llapi_error(LLAPI_MSG_ERROR, rc,
1094 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1099 fd = open(dir, O_DIRECTORY | O_RDONLY);
1102 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1106 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1107 char *errmsg = "stripe already set";
1110 if (errno != EEXIST && errno != EALREADY)
1111 errmsg = strerror(errno);
1113 llapi_err_noerrno(LLAPI_MSG_ERROR,
1114 "dirstripe error on '%s': %s", name, errmsg);
1124 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1125 int stripe_count, int stripe_pattern,
1126 const char *pool_name)
1128 const struct llapi_stripe_param param = {
1129 .lsp_stripe_count = stripe_count,
1130 .lsp_stripe_offset = stripe_offset,
1131 .lsp_stripe_pattern = stripe_pattern,
1132 .lsp_pool = (char *)pool_name
1135 return llapi_dir_create(name, mode, ¶m);
1139 * Get the list of pool members.
1140 * \param poolname string of format \<fsname\>.\<poolname\>
1141 * \param members caller-allocated array of char*
1142 * \param list_size size of the members array
1143 * \param buffer caller-allocated buffer for storing OST names
1144 * \param buffer_size size of the buffer
1146 * \return number of members retrieved for this pool
1147 * \retval -error failure
1149 int llapi_get_poolmembers(const char *poolname, char **members,
1150 int list_size, char *buffer, int buffer_size)
1152 char fsname[PATH_MAX];
1161 /* name is FSNAME.POOLNAME */
1162 if (strlen(poolname) >= sizeof(fsname))
1165 snprintf(fsname, sizeof(fsname), "%s", poolname);
1166 pool = strchr(fsname, '.');
1173 rc = poolpath(&pathname, fsname, NULL);
1175 llapi_error(LLAPI_MSG_ERROR, rc,
1176 "Lustre filesystem '%s' not found",
1181 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1182 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1183 cfs_free_param_data(&pathname);
1184 if (rc >= sizeof(buf))
1186 fd = fopen(buf, "r");
1189 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1194 while (fgets(buf, sizeof(buf), fd) != NULL) {
1195 if (nb_entries >= list_size) {
1199 buf[sizeof(buf) - 1] = '\0';
1201 tmp = strchr(buf, '\n');
1204 if (used + strlen(buf) + 1 > buffer_size) {
1209 strcpy(buffer + used, buf);
1210 members[nb_entries] = buffer + used;
1211 used += strlen(buf) + 1;
1221 * Get the list of pools in a filesystem.
1222 * \param name filesystem name or path
1223 * \param poollist caller-allocated array of char*
1224 * \param list_size size of the poollist array
1225 * \param buffer caller-allocated buffer for storing pool names
1226 * \param buffer_size size of the buffer
1228 * \return number of pools retrieved for this filesystem
1229 * \retval -error failure
1231 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1232 char *buffer, int buffer_size)
1238 struct dirent *pool;
1240 unsigned int nb_entries = 0;
1241 unsigned int used = 0;
1244 /* initialize output array */
1245 for (i = 0; i < list_size; i++)
1248 /* is name a pathname ? */
1249 ptr = strchr(name, '/');
1251 char fsname_buf[MAXNAMLEN];
1253 /* We will need fsname for printing later */
1254 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1258 ptr = strrchr(fsname_buf, '-');
1262 fsname = strdup(fsname_buf);
1266 /* name is FSNAME */
1267 fsname = strdup(name);
1272 rc = poolpath(&pathname, fsname, NULL);
1274 llapi_error(LLAPI_MSG_ERROR, rc,
1275 "Lustre filesystem '%s' not found", name);
1279 dir = opendir(pathname.gl_pathv[0]);
1282 llapi_error(LLAPI_MSG_ERROR, rc,
1283 "Could not open pool list for '%s'",
1290 pool = readdir(dir);
1296 /* ignore . and .. */
1297 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1300 /* check output bounds */
1301 if (nb_entries >= list_size) {
1303 goto free_dir_no_msg;
1306 /* +2 for '.' and final '\0' */
1307 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1310 goto free_dir_no_msg;
1313 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1314 poollist[nb_entries] = buffer + used;
1315 used += strlen(pool->d_name) + strlen(fsname) + 2;
1321 llapi_error(LLAPI_MSG_ERROR, rc,
1322 "Error reading pool list for '%s'", name);
1324 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1329 cfs_free_param_data(&pathname);
1332 return rc != 0 ? rc : nb_entries;
1335 /* wrapper for lfs.c and obd.c */
1336 int llapi_poollist(const char *name)
1338 int poolcount, rc, i;
1341 rc = llapi_get_poolbuf(name, &buf, &pools, &poolcount);
1345 for (i = 0; i < poolcount; i++)
1346 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", pools[i]);
1353 * Get buffer that holds uuids and the list of pools in a filesystem.
1355 * \param name filesystem name or path
1356 * \param buf bufffer that has to be freed if function returns 0
1357 * \param pools pointer to the list of pools in buffer
1358 * \param poolcount number of pools
1360 * \return 0 when found at least 1 pool, i.e. poolcount > 0
1361 * \retval -error failure
1363 int llapi_get_poolbuf(const char *name, char **buf,
1364 char ***pools, int *poolcount)
1367 * list of pool names (assume that pool count is smaller
1370 char **list, *buffer = NULL, *fsname = (char *)name;
1371 char *poolname = NULL, *tmp = NULL, data[16];
1372 enum param_filter type = FILTER_BY_PATH;
1373 int obdcount, bufsize, rc, nb;
1378 if (name[0] != '/') {
1379 fsname = strdup(name);
1383 poolname = strchr(fsname, '.');
1386 type = FILTER_BY_FS_NAME;
1389 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1390 data, sizeof(data));
1393 obdcount = atoi(data);
1396 * Allocate space for each fsname-OST0000_UUID, 1 per OST,
1397 * and also an array to store the pointers for all that
1401 bufsize = sizeof(struct obd_uuid) * obdcount;
1402 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1403 if (buffer == NULL) {
1407 list = (char **) (buffer + bufsize);
1410 /* name is a path or fsname */
1411 nb = llapi_get_poollist(name, list, obdcount,
1414 /* name is a pool name (<fsname>.<poolname>) */
1415 nb = llapi_get_poolmembers(name, list, obdcount,
1419 if (nb == -EOVERFLOW) {
1422 goto retry_get_pools;
1425 rc = (nb < 0 ? nb : 0);
1432 /* Don't free buffer, it will be used later */
1435 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1440 typedef int (semantic_func_t)(char *path, int p, int *d,
1441 void *data, struct dirent64 *de);
1443 #define OBD_NOT_FOUND (-1)
1445 static bool lmv_is_foreign(__u32 magic)
1447 return magic == LMV_MAGIC_FOREIGN;
1450 static void find_param_fini(struct find_param *param)
1452 if (param->fp_migrate)
1455 if (param->fp_obd_indexes) {
1456 free(param->fp_obd_indexes);
1457 param->fp_obd_indexes = NULL;
1460 if (param->fp_lmd) {
1461 free(param->fp_lmd);
1462 param->fp_lmd = NULL;
1465 if (param->fp_lmv_md) {
1466 free(param->fp_lmv_md);
1467 param->fp_lmv_md = NULL;
1471 static int common_param_init(struct find_param *param, char *path)
1473 int lum_size = get_mds_md_size(path);
1478 /* migrate has fp_lmv_md initialized outside */
1479 if (param->fp_migrate)
1482 if (lum_size < PATH_MAX + 1)
1483 lum_size = PATH_MAX + 1;
1485 param->fp_lum_size = lum_size;
1486 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1488 if (param->fp_lmd == NULL) {
1489 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1490 "error: allocate %zu bytes for layout failed",
1491 sizeof(lstat_t) + param->fp_lum_size);
1495 param->fp_lmv_stripe_count = 256;
1496 param->fp_lmv_md = calloc(1,
1497 lmv_user_md_size(param->fp_lmv_stripe_count,
1498 LMV_USER_MAGIC_SPECIFIC));
1499 if (param->fp_lmv_md == NULL) {
1500 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1501 "error: allocation of %d bytes for ioctl",
1502 lmv_user_md_size(param->fp_lmv_stripe_count,
1503 LMV_USER_MAGIC_SPECIFIC));
1504 find_param_fini(param);
1508 param->fp_got_uuids = 0;
1509 param->fp_obd_indexes = NULL;
1510 param->fp_obd_index = OBD_NOT_FOUND;
1511 param->fp_mdt_index = OBD_NOT_FOUND;
1515 static int cb_common_fini(char *path, int p, int *dp, void *data,
1516 struct dirent64 *de)
1518 struct find_param *param = data;
1524 /* set errno upon failure */
1525 static int open_parent(const char *path)
1531 path_copy = strdup(path);
1532 if (path_copy == NULL)
1535 parent_path = dirname(path_copy);
1536 parent = open(parent_path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1542 static int cb_get_dirstripe(char *path, int *d, struct find_param *param)
1545 bool did_nofollow = false;
1550 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1551 if (param->fp_get_default_lmv) {
1555 /* open() may not fetch LOOKUP lock, statx() to ensure dir depth
1558 statx(*d, "", AT_EMPTY_PATH, STATX_MODE, &stx);
1564 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1566 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1569 param->fp_lmv_md->lum_type = LMV_TYPE_RAW;
1571 ret = ioctl(*d, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1573 /* if ENOTTY likely to be a fake symlink, so try again after
1574 * new open() with O_NOFOLLOW, but only once to prevent any
1575 * loop like for the path of a file/dir not on Lustre !!
1577 if (ret < 0 && errno == ENOTTY && !did_nofollow) {
1580 did_nofollow = true;
1581 fd = open(path, O_RDONLY | O_NOFOLLOW);
1583 /* restore original errno */
1588 /* close original fd and set new */
1591 ret2 = ioctl(fd, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1592 if (ret2 < 0 && errno != E2BIG) {
1593 /* restore original errno */
1597 /* LMV is ok or need to handle E2BIG case now */
1601 if (errno == E2BIG && ret != 0) {
1605 /* if foreign LMV case, fake stripes number */
1606 if (lmv_is_foreign(param->fp_lmv_md->lum_magic)) {
1607 struct lmv_foreign_md *lfm;
1609 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1610 if (lfm->lfm_length < XATTR_SIZE_MAX -
1611 offsetof(typeof(*lfm), lfm_value)) {
1612 uint32_t size = lfm->lfm_length +
1613 offsetof(typeof(*lfm), lfm_value);
1615 stripe_count = lmv_foreign_to_md_stripes(size);
1617 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1618 "error: invalid %d foreign size returned from ioctl",
1623 stripe_count = param->fp_lmv_md->lum_stripe_count;
1625 if (stripe_count <= param->fp_lmv_stripe_count)
1628 free(param->fp_lmv_md);
1629 param->fp_lmv_stripe_count = stripe_count;
1630 lmv_size = lmv_user_md_size(stripe_count,
1631 LMV_USER_MAGIC_SPECIFIC);
1632 param->fp_lmv_md = malloc(lmv_size);
1633 if (param->fp_lmv_md == NULL) {
1634 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1635 "error: allocation of %d bytes for ioctl",
1636 lmv_user_md_size(param->fp_lmv_stripe_count,
1637 LMV_USER_MAGIC_SPECIFIC));
1646 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1649 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
1650 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
1651 lmd_v2->lmd_stx.stx_uid = st->st_uid;
1652 lmd_v2->lmd_stx.stx_gid = st->st_gid;
1653 lmd_v2->lmd_stx.stx_mode = st->st_mode;
1654 lmd_v2->lmd_stx.stx_ino = st->st_ino;
1655 lmd_v2->lmd_stx.stx_size = st->st_size;
1656 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
1657 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
1658 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
1659 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
1660 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
1661 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
1662 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
1663 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
1664 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
1666 lmd_v2->lmd_flags = 0;
1668 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1670 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
1671 if (lmd_v2->lmd_stx.stx_size)
1672 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
1673 if (lmd_v2->lmd_stx.stx_blocks)
1674 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
1676 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1677 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
1678 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
1683 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
1685 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
1686 struct lov_user_mds_data *lmd_v2 = lmdbuf;
1690 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
1694 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
1697 st = lmd_v1->lmd_st;
1698 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
1699 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
1700 convert_lmd_statx(lmd_v2, &st, false);
1701 lmd_v2->lmd_lmmsize = 0;
1702 lmd_v2->lmd_padding = 0;
1707 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
1708 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1710 struct lov_user_mds_data *lmd = lmdbuf;
1711 static bool use_old_ioctl;
1715 if (parent_fd < 0 && dir_fd < 0)
1717 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1722 * LL_IOC_MDC_GETINFO operates on the current directory inode
1723 * and returns struct lov_user_mds_data, while
1724 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
1726 if (type == GET_LMD_INFO)
1727 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_V1 :
1728 LL_IOC_MDC_GETINFO_V2;
1730 cmd = LL_IOC_LOV_GETSTRIPE;
1733 ret = ioctl(dir_fd, cmd, lmdbuf);
1734 if (ret < 0 && errno == ENOTTY &&
1735 cmd == LL_IOC_MDC_GETINFO_V2) {
1736 cmd = LL_IOC_MDC_GETINFO_V1;
1737 use_old_ioctl = true;
1741 if (cmd == LL_IOC_MDC_GETINFO_V1 && !ret)
1742 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
1744 if (ret < 0 && errno == ENOTTY && type == GET_LMD_STRIPE) {
1747 /* retry ioctl() after new open() with O_NOFOLLOW
1748 * just in case it could be a fake symlink
1749 * need using a new open() as dir_fd is being closed
1753 dir_fd2 = open(path, O_RDONLY | O_NDELAY | O_NOFOLLOW);
1755 /* return original error */
1758 ret = ioctl(dir_fd2, cmd, lmdbuf);
1759 /* pass new errno or success back to caller */
1765 } else if (parent_fd >= 0) {
1766 const char *fname = strrchr(path, '/');
1769 * IOC_MDC_GETFILEINFO takes as input the filename (relative to
1770 * the parent directory) and returns struct lov_user_mds_data,
1771 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
1773 * This avoids opening, locking, and closing each file on the
1774 * client if that is not needed. Multiple of these ioctl() can
1775 * be done on the parent dir with a single open for all
1776 * files in that directory, and it also doesn't pollute the
1777 * client dcache with millions of dentries when traversing
1778 * a large filesystem.
1780 fname = (fname == NULL ? path : fname + 1);
1782 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
1785 else if (ret >= lmdlen || ret++ == 0)
1788 if (type == GET_LMD_INFO)
1789 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_V1 :
1790 IOC_MDC_GETFILEINFO_V2;
1792 cmd = IOC_MDC_GETFILESTRIPE;
1795 ret = ioctl(parent_fd, cmd, lmdbuf);
1796 if (ret < 0 && errno == ENOTTY &&
1797 cmd == IOC_MDC_GETFILEINFO_V2) {
1798 cmd = IOC_MDC_GETFILEINFO_V1;
1799 use_old_ioctl = true;
1800 goto retry_getfileinfo;
1803 if (cmd == IOC_MDC_GETFILEINFO_V1 && !ret)
1804 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
1808 if (ret && type == GET_LMD_INFO) {
1809 if (errno == ENOTTY) {
1813 * ioctl is not supported, it is not a lustre fs.
1814 * Do the regular lstat(2) instead.
1816 ret = lstat_f(path, &st);
1819 llapi_error(LLAPI_MSG_ERROR, ret,
1820 "error: %s: lstat failed for %s",
1824 convert_lmd_statx(lmd, &st, true);
1826 * It may be wrong to set use_old_ioctl with true as
1827 * the file is not a lustre fs. So reset it with false
1830 use_old_ioctl = false;
1831 } else if (errno == ENOENT) {
1833 llapi_error(LLAPI_MSG_WARN, ret,
1834 "warning: %s does not exist", path);
1835 } else if (errno != EISDIR && errno != ENODATA) {
1837 llapi_error(LLAPI_MSG_ERROR, ret,
1838 "%s ioctl failed for %s.",
1839 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
1840 "IOC_MDC_GETFILEINFO", path);
1847 static int llapi_semantic_traverse(char *path, int size, int parent,
1848 semantic_func_t sem_init,
1849 semantic_func_t sem_fini, void *data,
1850 struct dirent64 *de)
1852 struct find_param *param = (struct find_param *)data;
1853 struct dirent64 *dent;
1854 int len, ret, d, p = -1;
1860 d = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1861 /* if an invalid fake dir symlink, opendir() will return EINVAL
1862 * instead of ENOTDIR. If a valid but dangling faked or real file/dir
1863 * symlink ENOENT will be returned. For a valid/resolved fake or real
1864 * file symlink ENOTDIR will be returned as for a regular file.
1865 * opendir() will be successful for a valid and resolved fake or real
1866 * dir simlink or a regular dir.
1868 if (d == -1 && errno != ENOTDIR && errno != EINVAL && errno != ENOENT) {
1870 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1873 } else if (d == -1) {
1874 if (errno == ENOENT || errno == EINVAL) {
1875 int old_errno = errno;
1877 /* try to open with O_NOFOLLOW this will help
1878 * differentiate fake vs real symlinks
1879 * it is ok to not use O_DIRECTORY with O_RDONLY
1880 * and it will prevent the need to deal with ENOTDIR
1881 * error, instead of ELOOP, being returned by recent
1882 * kernels for real symlinks
1884 d = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
1885 /* if a dangling real symlink should return ELOOP, or
1886 * again ENOENT if really non-existing path, or E...??
1887 * So return original error. If success or ENOTDIR, path
1888 * is likely to be a fake dir/file symlink, so continue
1898 if (parent == -1 && d == -1) {
1899 /* Open the parent dir. */
1900 p = open_parent(path);
1906 } else { /* d != -1 */
1909 /* try to reopen dir with O_NOFOLLOW just in case of a foreign
1912 d2 = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
1917 /* continue with d */
1923 ret = sem_init(path, (parent != -1) ? parent : p, &d, data, de);
1933 /* ENOTDIR if fake symlink, do not consider it as an error */
1934 if (errno != ENOTDIR)
1935 llapi_error(LLAPI_MSG_ERROR, errno,
1936 "fdopendir() failed");
1943 while ((dent = readdir64(dir)) != NULL) {
1946 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1950 if ((len + dent->d_reclen + 2) > size) {
1951 llapi_err_noerrno(LLAPI_MSG_ERROR,
1952 "error: %s: string buffer too small for %s",
1957 strcat(path, dent->d_name);
1959 if (dent->d_type == DT_UNKNOWN) {
1960 struct lov_user_mds_data *lmd = param->fp_lmd;
1962 rc = get_lmd_info_fd(path, d, -1, param->fp_lmd,
1963 param->fp_lum_size, GET_LMD_INFO);
1965 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
1973 switch (dent->d_type) {
1975 llapi_err_noerrno(LLAPI_MSG_ERROR,
1976 "error: %s: '%s' is UNKNOWN type %d",
1977 __func__, dent->d_name, dent->d_type);
1980 rc = llapi_semantic_traverse(path, size, d, sem_init,
1981 sem_fini, data, dent);
1982 if (rc != 0 && ret == 0)
1984 if (rc < 0 && rc != -EALREADY &&
1985 param->fp_stop_on_error)
1991 rc = sem_init(path, d, NULL, data, dent);
1992 if (rc < 0 && ret == 0) {
1994 if (rc && rc != -EALREADY &&
1995 param->fp_stop_on_error)
2000 if (sem_fini && rc == 0)
2001 sem_fini(path, d, NULL, data, dent);
2009 sem_fini(path, parent, &d, data, de);
2022 static int param_callback(char *path, semantic_func_t sem_init,
2023 semantic_func_t sem_fini, struct find_param *param)
2025 int ret, len = strlen(path);
2028 if (len > PATH_MAX) {
2030 llapi_error(LLAPI_MSG_ERROR, ret,
2031 "Path name '%s' is too long", path);
2035 buf = (char *)malloc(2 * PATH_MAX);
2039 snprintf(buf, PATH_MAX + 1, "%s", path);
2040 ret = common_param_init(param, buf);
2044 param->fp_depth = 0;
2046 ret = llapi_semantic_traverse(buf, 2 * PATH_MAX, -1, sem_init,
2047 sem_fini, param, NULL);
2049 find_param_fini(param);
2051 return ret < 0 ? ret : 0;
2054 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2058 rc = ioctl(fd, OBD_IOC_GETDTNAME, lov_name);
2061 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2067 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2071 rc = llapi_ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2074 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2080 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2084 /* do not follow faked symlinks */
2085 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
2087 /* real symlink should have failed with ELOOP so retry without
2088 * O_NOFOLLOW just in case
2090 fd = open(path, O_RDONLY | O_NONBLOCK);
2093 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'",
2099 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2105 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2109 fd = open(path, O_RDONLY | O_NONBLOCK);
2112 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2116 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2128 * If uuidp is NULL, return the number of available obd uuids.
2129 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2130 * there are more OSTs than allocated to uuidp, then an error is returned with
2131 * the ost_count set to number of available obd uuids.
2133 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp, int *indices,
2134 int *ost_count, enum tgt_type type)
2136 char buf[PATH_MAX], format[32];
2138 struct obd_uuid name;
2142 /* Get the lov name */
2143 if (type == LOV_TYPE)
2144 rc = llapi_file_fget_lov_uuid(fd, &name);
2146 rc = llapi_file_fget_lmv_uuid(fd, &name);
2150 /* Now get the ost uuids */
2151 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2152 FILTER_BY_EXACT, "target_obd", ¶m);
2156 fp = fopen(param.gl_pathv[0], "r");
2159 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2164 snprintf(format, sizeof(format),
2165 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2166 for (i = 0; fgets(buf, sizeof(buf), fp); i++) {
2169 if (sscanf(buf, format, &index, name.uuid) < 2)
2172 if (i < *ost_count) {
2175 if (indices != NULL)
2181 if (uuidp && (i > *ost_count))
2186 cfs_free_param_data(¶m);
2190 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2192 return llapi_get_target_uuids(fd, uuidp, NULL, ost_count, LOV_TYPE);
2195 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2200 root = open(mnt, O_RDONLY | O_DIRECTORY);
2203 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2208 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2217 * Check if user specified value matches a real uuid. Ignore _UUID,
2218 * -osc-4ba41334, other trailing gunk in comparison.
2219 * @param real_uuid ends in "_UUID"
2220 * @param search_uuid may or may not end in "_UUID"
2222 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2224 int cmplen = strlen(real_uuid);
2225 int searchlen = strlen(search_uuid);
2227 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2229 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2233 * The UUIDs may legitimately be different lengths, if
2234 * the system was upgraded from an older version.
2236 if (cmplen != searchlen)
2239 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2243 * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2244 * returned in param->fp_obd_index
2246 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2248 struct obd_uuid obd_uuid;
2255 if (param->fp_got_uuids)
2258 /* Get the lov/lmv name */
2259 if (param->fp_get_lmv)
2260 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2262 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2264 if (rc != -ENOTTY) {
2265 llapi_error(LLAPI_MSG_ERROR, rc,
2266 "error: can't get %s name: %s",
2267 param->fp_get_lmv ? "lmv" : "lov",
2275 param->fp_got_uuids = 1;
2277 /* Now get the ost uuids */
2278 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2279 obd_uuid.uuid, FILTER_BY_EXACT,
2280 "target_obd", ¶m_data);
2284 fp = fopen(param_data.gl_pathv[0], "r");
2287 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2288 param_data.gl_pathv[0]);
2292 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2293 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2294 param->fp_get_lmv ? "MDTS" : "OBDS");
2296 snprintf(format, sizeof(format),
2297 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2298 while (fgets(buf, sizeof(buf), fp) != NULL) {
2301 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2304 if (param->fp_obd_uuid) {
2305 if (llapi_uuid_match(obd_uuid.uuid,
2306 param->fp_obd_uuid->uuid)) {
2307 param->fp_obd_index = index;
2310 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2311 /* Print everything */
2312 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2315 param->fp_obds_printed = 1;
2319 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2320 llapi_err_noerrno(LLAPI_MSG_ERROR,
2321 "error: %s: unknown obduuid: %s",
2322 __func__, param->fp_obd_uuid->uuid);
2326 cfs_free_param_data(¶m_data);
2331 * In this case, param->fp_obd_uuid will be an array of obduuids and
2332 * obd index for all these obduuids will be returned in
2333 * param->fp_obd_indexes
2335 static int setup_indexes(int d, char *path, struct obd_uuid *obduuids,
2336 int num_obds, int **obdindexes, int *obdindex,
2339 int ret, obdcount, maxidx, obd_valid = 0, obdnum;
2341 struct obd_uuid *uuids = NULL;
2342 int *indices = NULL;
2346 if (type == LOV_TYPE)
2347 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2349 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2353 obdcount = atoi(buf);
2354 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2357 indices = malloc(obdcount * sizeof(int));
2358 if (indices == NULL) {
2365 ret = llapi_get_target_uuids(d, uuids, indices, &obdcount, type);
2367 if (ret == -EOVERFLOW) {
2368 struct obd_uuid *uuids_temp;
2371 uuids_temp = realloc(uuids, obdcount *
2372 sizeof(struct obd_uuid));
2373 indices_temp = realloc(indices, obdcount * sizeof(int));
2374 if (uuids_temp != NULL && indices_temp != NULL) {
2376 indices = indices_temp;
2377 goto retry_get_uuids;
2382 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2386 indexes = malloc(num_obds * sizeof(*obdindex));
2387 if (indexes == NULL) {
2392 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2395 /* The user may have specified a simple index */
2396 i = strtol(obduuids[obdnum].uuid, &end, 0);
2397 if (end && *end == '\0' && i < maxidx) {
2398 indexes[obdnum] = i;
2401 for (i = 0; i < obdcount; i++) {
2402 if (llapi_uuid_match(uuids[i].uuid,
2403 obduuids[obdnum].uuid)) {
2404 indexes[obdnum] = indices[i];
2412 indexes[obdnum] = OBD_NOT_FOUND;
2413 llapi_err_noerrno(LLAPI_MSG_ERROR,
2414 "invalid obduuid '%s'",
2415 obduuids[obdnum].uuid);
2421 *obdindex = OBD_NOT_FOUND;
2423 *obdindex = obd_valid;
2425 *obdindexes = indexes;
2435 static int setup_target_indexes(int d, char *path, struct find_param *param)
2439 if (param->fp_mdt_uuid) {
2440 ret = setup_indexes(d, path, param->fp_mdt_uuid,
2442 ¶m->fp_mdt_indexes,
2443 ¶m->fp_mdt_index, LMV_TYPE);
2448 if (param->fp_obd_uuid) {
2449 ret = setup_indexes(d, path, param->fp_obd_uuid,
2451 ¶m->fp_obd_indexes,
2452 ¶m->fp_obd_index, LOV_TYPE);
2457 param->fp_got_uuids = 1;
2462 int llapi_ostlist(char *path, struct find_param *param)
2467 fd = open(path, O_RDONLY | O_DIRECTORY);
2471 ret = setup_obd_uuid(fd, path, param);
2478 * Tries to determine the default stripe attributes for a given filesystem. The
2479 * filesystem to check should be specified by fsname, or will be determined
2482 static int sattr_get_defaults(const char *const fsname,
2483 unsigned int *scount,
2484 unsigned int *ssize,
2485 unsigned int *soffset)
2491 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2492 "stripecount", val, sizeof(val));
2495 *scount = atoi(val);
2499 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2500 "stripesize", val, sizeof(val));
2507 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2508 "stripeoffset", val, sizeof(val));
2511 *soffset = atoi(val);
2518 * Tries to gather the default stripe attributes for a given filesystem. If
2519 * the attributes can be determined, they are cached for easy retreival the
2520 * next time they are needed. Only a single filesystem's attributes are
2523 int sattr_cache_get_defaults(const char *const fsname,
2524 const char *const pathname, unsigned int *scount,
2525 unsigned int *ssize, unsigned int *soffset)
2528 char fsname[PATH_MAX + 1];
2529 unsigned int stripecount;
2530 unsigned int stripesize;
2531 unsigned int stripeoffset;
2537 char fsname_buf[PATH_MAX + 1];
2538 unsigned int tmp[3];
2540 if (fsname == NULL) {
2541 rc = llapi_search_fsname(pathname, fsname_buf);
2545 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2548 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2550 * Ensure all 3 sattrs (count, size, and offset) are
2551 * successfully retrieved and stored in tmp before writing to
2554 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2558 cache.stripecount = tmp[0];
2559 cache.stripesize = tmp[1];
2560 cache.stripeoffset = tmp[2];
2561 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2565 *scount = cache.stripecount;
2567 *ssize = cache.stripesize;
2569 *soffset = cache.stripeoffset;
2574 static char *layout2name(__u32 layout_pattern)
2576 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2578 else if (layout_pattern == LOV_PATTERN_MDT)
2580 else if (layout_pattern == LOV_PATTERN_RAID0)
2582 else if (layout_pattern ==
2583 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2584 return "raid0,overstriped";
2589 enum lov_dump_flags {
2590 LDF_IS_DIR = 0x0001,
2591 LDF_IS_RAW = 0x0002,
2592 LDF_INDENT = 0x0004,
2593 LDF_SKIP_OBJS = 0x0008,
2595 LDF_EXTENSION = 0x0020,
2596 LDF_HEX_IDX = 0x0040,
2599 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2600 struct lov_user_ost_data_v1 *objects,
2601 enum llapi_layout_verbose verbose,
2602 int depth, char *pool_name,
2603 enum lov_dump_flags flags)
2605 bool is_dir = flags & LDF_IS_DIR;
2606 bool is_raw = flags & LDF_IS_RAW;
2607 bool indent = flags & LDF_INDENT;
2608 bool yaml = flags & LDF_YAML;
2609 bool skip_objs = flags & LDF_SKIP_OBJS;
2610 bool extension = flags & LDF_EXTENSION;
2611 char *prefix = is_dir ? "" : "lmm_";
2612 char *separator = "";
2613 char *space = indent ? " " : "";
2614 char *fmt_idx = flags & LDF_HEX_IDX ? "%#x" : "%d";
2617 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2618 lmm_oi_set_seq(&lum->lmm_oi, 0);
2619 if (!indent && (verbose & VERBOSE_DETAIL))
2620 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2623 if (!yaml && !indent && depth && path &&
2624 ((verbose != VERBOSE_OBJID) || !is_dir))
2625 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2627 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2628 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2629 space, prefix, lum->lmm_magic);
2630 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2632 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2633 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2635 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2638 if (verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) {
2643 if (verbose & ~VERBOSE_DFID)
2644 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2648 struct lu_fid dir_fid;
2650 rc = llapi_path2fid(path, &dir_fid);
2652 llapi_error(LLAPI_MSG_ERROR, rc,
2653 "Cannot determine directory fid.");
2655 seq = dir_fid.f_seq;
2656 oid = dir_fid.f_oid;
2657 ver = dir_fid.f_ver;
2660 * This needs a bit of hand-holding since old 1.x
2661 * lmm_oi have { oi.oi_id = mds_inum, oi.oi_seq = 0 }
2662 * and 2.x lmm_oi have { oi.oi_id = mds_oid,
2663 * oi.oi_seq = mds_seq } instead of a real FID.
2664 * Ideally the 2.x code would have stored this like a
2665 * FID with { oi_id = mds_seq, oi_seq = mds_oid } so
2666 * the ostid union lu_fid { f_seq = mds_seq,
2667 * f_oid = mds_oid } worked properly (especially since
2668 * IGIF FIDs use mds_inum as the FID SEQ), but
2669 * unfortunately that didn't happen.
2671 * Print it to look like an IGIF FID, even though the
2672 * fields are reversed on disk, so that it makes sense
2675 * Don't use ostid_id() and ostid_seq(), since they
2676 * assume the oi_fid fields are in the right order.
2677 * This is why there are separate lmm_oi_seq() and
2678 * lmm_oi_id() routines for this.
2680 * For newer layout types hopefully this will be a
2683 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2684 lmm_oi_id(&lum->lmm_oi) :
2685 lmm_oi_seq(&lum->lmm_oi);
2686 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2687 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2688 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2692 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2693 (unsigned long long)seq, oid, ver);
2695 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2696 (unsigned long long)seq, oid, ver);
2699 if (verbose & VERBOSE_STRIPE_COUNT) {
2700 if (verbose & ~VERBOSE_STRIPE_COUNT)
2701 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2704 if (!is_raw && lum->lmm_stripe_count == 0 &&
2705 !(lov_pattern(lum->lmm_pattern) & LOV_PATTERN_MDT)){
2706 unsigned int scount;
2708 rc = sattr_cache_get_defaults(NULL, path,
2712 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2715 llapi_error(LLAPI_MSG_ERROR, rc,
2716 "Cannot determine default stripe count.");
2718 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2720 (__s16)lum->lmm_stripe_count);
2723 llapi_printf(LLAPI_MSG_NORMAL, "%i",
2725 (__s16)lum->lmm_stripe_count);
2727 if (!yaml && is_dir)
2733 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
2734 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
2735 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2736 if (verbose & ~VERBOSE_EXT_SIZE && extension)
2737 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
2739 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
2740 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2742 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2745 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2748 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2750 llapi_error(LLAPI_MSG_ERROR, rc,
2751 "Cannot determine default stripe size.");
2753 /* Extension size is in KiB */
2754 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2756 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
2757 (unsigned long long)lum->lmm_stripe_size);
2759 if (!yaml && is_dir)
2765 if ((verbose & VERBOSE_PATTERN)) {
2766 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2767 if (verbose & ~VERBOSE_PATTERN)
2768 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2770 if (lov_pattern_supported(lum->lmm_pattern))
2771 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2772 layout2name(lum->lmm_pattern));
2774 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2775 separator = (!yaml && is_dir) ? " " : "\n";
2778 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2779 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2780 if (verbose & ~VERBOSE_GENERATION)
2781 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2783 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2784 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2788 if (verbose & VERBOSE_STRIPE_OFFSET) {
2789 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2790 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2791 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2793 if (is_dir || skip_objs)
2794 if (lum->lmm_stripe_offset ==
2795 (typeof(lum->lmm_stripe_offset))(-1))
2796 llapi_printf(LLAPI_MSG_NORMAL, "-1");
2798 llapi_printf(LLAPI_MSG_NORMAL, fmt_idx,
2799 lum->lmm_stripe_offset);
2800 else if (lov_pattern(lum->lmm_pattern) & LOV_PATTERN_MDT)
2801 llapi_printf(LLAPI_MSG_NORMAL, "0");
2803 llapi_printf(LLAPI_MSG_NORMAL, fmt_idx,
2804 objects[0].l_ost_idx);
2805 if (!yaml && is_dir)
2811 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0') &&
2812 (!lov_pool_is_ignored(pool_name) || is_raw)) {
2813 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2814 if (verbose & ~VERBOSE_POOL)
2815 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2817 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2818 if (!yaml && is_dir)
2824 if (strlen(separator) != 0)
2825 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2828 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2829 struct lov_user_ost_data_v1 *objects,
2830 char *path, int obdindex, int depth,
2831 enum llapi_layout_verbose verbose,
2832 enum lov_dump_flags flags)
2834 bool is_dir = flags & LDF_IS_DIR;
2835 bool indent = flags & LDF_INDENT;
2836 bool skip_objs = flags & LDF_SKIP_OBJS;
2837 bool yaml = flags & LDF_YAML;
2838 bool hex = flags & LDF_HEX_IDX;
2839 bool obdstripe = obdindex == OBD_NOT_FOUND;
2842 if (!obdstripe && !skip_objs) {
2843 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2844 if (obdindex == objects[i].l_ost_idx) {
2854 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2857 if (!skip_objs && (verbose & VERBOSE_OBJID) &&
2858 ((!is_dir && !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2859 lov_pattern(lum->lmm_pattern) & LOV_PATTERN_MDT)) ||
2860 (is_dir && (lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC)))) {
2861 char *space = " - ";
2864 llapi_printf(LLAPI_MSG_NORMAL,
2865 "%6slmm_objects:\n", " ");
2867 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2869 llapi_printf(LLAPI_MSG_NORMAL,
2870 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2872 for (i = 0; i < lum->lmm_stripe_count; i++) {
2873 int idx = objects[i].l_ost_idx;
2874 long long oid = ostid_id(&objects[i].l_ost_oi);
2875 long long gr = ostid_seq(&objects[i].l_ost_oi);
2877 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2881 struct lu_fid fid = { 0 };
2882 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2883 llapi_printf(LLAPI_MSG_NORMAL,
2884 hex ? "%sl_ost_idx: %#x\n"
2885 : "%sl_ost_idx: %d\n",
2887 llapi_printf(LLAPI_MSG_NORMAL,
2888 "%8sl_fid: "DFID_NOBRACE"\n",
2890 } else if (indent) {
2891 struct lu_fid fid = { 0 };
2893 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2894 llapi_printf(LLAPI_MSG_NORMAL, hex ?
2895 "%s%3d: { l_ost_idx: %#5x, l_fid: "DFID" }\n" :
2896 "%s%3d: { l_ost_idx: %3d, l_fid: "DFID" }\n",
2897 space, i, idx, PFID(&fid));
2898 } else if (is_dir) {
2899 llapi_printf(LLAPI_MSG_NORMAL,
2900 "\t%6u\t%14s\t%13s\t%14s\n", idx, "N/A",
2903 char fmt[48] = { 0 };
2905 sprintf(fmt, "%s%s%s\n",
2906 hex ? "\t%#6x\t%14llu\t%#13llx\t"
2907 : "\t%6u\t%14llu\t%#13llx\t",
2908 (fid_seq_is_rsvd(gr) ||
2909 fid_seq_is_mdt0(gr)) ?
2910 "%14llu" : "%#14llx", "%s");
2911 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2913 obdindex == idx ? " *" : "");
2917 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2920 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2921 char *path, int obdindex, int depth,
2922 enum llapi_layout_verbose verbose,
2923 enum lov_dump_flags flags)
2925 struct lmv_user_mds_data *objects = lum->lum_objects;
2926 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2927 char *separator = "";
2928 bool yaml = flags & LDF_YAML;
2929 bool hex = flags & LDF_HEX_IDX;
2930 bool obdstripe = false;
2933 if (obdindex != OBD_NOT_FOUND) {
2934 if (lum->lum_stripe_count == 0) {
2935 if (obdindex == lum->lum_stripe_offset)
2938 for (i = 0; i < lum->lum_stripe_count; i++) {
2939 if (obdindex == objects[i].lum_mds) {
2940 llapi_printf(LLAPI_MSG_NORMAL,
2955 /* show all information default */
2957 if (lum->lum_magic == LMV_USER_MAGIC)
2958 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
2959 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
2961 verbose = VERBOSE_OBJID;
2964 if (depth && path && ((verbose != VERBOSE_OBJID)))
2965 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2967 if (verbose & VERBOSE_STRIPE_COUNT) {
2968 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2969 if (verbose & ~VERBOSE_STRIPE_COUNT)
2970 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2971 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2972 (int)lum->lum_stripe_count);
2973 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
2979 if (verbose & VERBOSE_STRIPE_OFFSET) {
2980 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2981 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2982 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2983 llapi_printf(LLAPI_MSG_NORMAL, hex ? "%#x" : "%d",
2984 (int)lum->lum_stripe_offset);
2985 if (verbose & VERBOSE_HASH_TYPE && !yaml)
2991 if (verbose & VERBOSE_HASH_TYPE) {
2992 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
2993 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
2995 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2996 if (verbose & ~VERBOSE_HASH_TYPE)
2997 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
2998 if (type < LMV_HASH_TYPE_MAX)
2999 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3000 mdt_hash_name[type]);
3002 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3004 if (flags & LMV_HASH_FLAG_OVERSTRIPED)
3005 llapi_printf(LLAPI_MSG_NORMAL, ",overstriped");
3006 if (flags & LMV_HASH_FLAG_MIGRATION)
3007 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3008 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3009 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3010 if (flags & LMV_HASH_FLAG_LOST_LMV)
3011 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3012 if (flags & LMV_HASH_FLAG_FIXED)
3013 llapi_printf(LLAPI_MSG_NORMAL, ",fixed");
3014 /* NB: OVERSTRIPED is not in KNOWN until implementation patch
3015 * is landed, but we do recognize it
3017 if (flags & ~(LMV_HASH_FLAG_KNOWN|LMV_HASH_FLAG_OVERSTRIPED))
3018 llapi_printf(LLAPI_MSG_NORMAL, ",unknown_%04x",
3019 flags & ~LMV_HASH_FLAG_KNOWN);
3021 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3027 if ((verbose & VERBOSE_INHERIT) && lum->lum_magic == LMV_USER_MAGIC) {
3028 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3029 if (verbose & ~VERBOSE_INHERIT)
3030 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit: ");
3031 if (lum->lum_max_inherit == LMV_INHERIT_UNLIMITED)
3032 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3033 else if (lum->lum_max_inherit == LMV_INHERIT_NONE)
3034 llapi_printf(LLAPI_MSG_NORMAL, "0");
3036 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3037 lum->lum_max_inherit);
3038 if (verbose & VERBOSE_INHERIT && !yaml)
3044 if ((verbose & VERBOSE_INHERIT_RR) &&
3045 lum->lum_magic == LMV_USER_MAGIC) {
3046 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3047 if (verbose & ~VERBOSE_INHERIT_RR)
3048 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit_rr: ");
3049 if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_UNLIMITED)
3050 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3051 else if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_NONE)
3052 llapi_printf(LLAPI_MSG_NORMAL, "0");
3054 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3055 lum->lum_max_inherit_rr);
3056 if (verbose & VERBOSE_INHERIT_RR && !yaml)
3064 if ((verbose & VERBOSE_OBJID) && lum->lum_magic != LMV_USER_MAGIC) {
3065 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3066 if (lum->lum_stripe_count > 0)
3067 llapi_printf(LLAPI_MSG_NORMAL,
3068 "mdtidx\t\t FID[seq:oid:ver]\n");
3070 char fmt[48] = { 0 };
3071 sprintf(fmt, "%s%s", hex ? "%#6x" : "%6u",
3072 "\t\t "DFID"\t\t%s\n");
3073 for (i = 0; i < lum->lum_stripe_count; i++) {
3074 int idx = objects[i].lum_mds;
3075 struct lu_fid *fid = &objects[i].lum_fid;
3077 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3078 llapi_printf(LLAPI_MSG_NORMAL, fmt,
3080 obdindex == idx ? " *" : "");
3084 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3085 pool_name[0] != '\0') {
3086 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3087 if (verbose & ~VERBOSE_POOL)
3088 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3090 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3094 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3095 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3098 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3099 enum lov_dump_flags flags)
3101 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3102 int depth = param->fp_max_depth;
3103 enum llapi_layout_verbose verbose = param->fp_verbose;
3104 bool yaml = flags & LDF_YAML;
3106 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3107 !(flags & LDF_IS_DIR)) && !yaml)
3108 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3110 if (verbose & VERBOSE_DETAIL) {
3111 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3112 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3113 " ", comp_v1->lcm_magic);
3114 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3115 " ", comp_v1->lcm_size);
3116 if (flags & LDF_IS_DIR)
3117 llapi_printf(LLAPI_MSG_NORMAL,
3118 "%2slcm_flags: %s\n", " ",
3119 comp_v1->lcm_mirror_count > 0 ?
3122 llapi_printf(LLAPI_MSG_NORMAL,
3123 "%2slcm_flags: %s\n", " ",
3124 llapi_layout_flags_string(comp_v1->lcm_flags));
3127 if (verbose & VERBOSE_GENERATION) {
3128 if (verbose & ~VERBOSE_GENERATION)
3129 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3131 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3134 if (verbose & VERBOSE_MIRROR_COUNT) {
3135 if (verbose & ~VERBOSE_MIRROR_COUNT)
3136 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3138 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3139 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3140 comp_v1->lcm_mirror_count + 1 : 1);
3143 if (verbose & VERBOSE_COMP_COUNT) {
3144 if (verbose & ~VERBOSE_COMP_COUNT)
3145 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3147 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3148 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3149 comp_v1->lcm_entry_count : 0);
3152 if (verbose & VERBOSE_DETAIL && !yaml)
3153 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3156 static void lcme_flags2str(__u32 comp_flags)
3162 llapi_printf(LLAPI_MSG_NORMAL, "0");
3165 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3166 const char *cfn_name = comp_flags_table[i].cfn_name;
3167 __u32 cfn_flag = comp_flags_table[i].cfn_flag;
3169 if ((comp_flags & cfn_flag) == cfn_flag) {
3171 llapi_printf(LLAPI_MSG_NORMAL, ",");
3172 llapi_printf(LLAPI_MSG_NORMAL, "%s", cfn_name);
3173 comp_flags &= ~comp_flags_table[i].cfn_flag;
3179 llapi_printf(LLAPI_MSG_NORMAL, ",");
3180 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3184 static void lov_dump_comp_v1_entry(struct find_param *param,
3185 enum lov_dump_flags flags, int index)
3187 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3188 struct lov_comp_md_entry_v1 *entry;
3189 char *separator = "";
3190 enum llapi_layout_verbose verbose = param->fp_verbose;
3191 bool yaml = flags & LDF_YAML;
3193 entry = &comp_v1->lcm_entries[index];
3196 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3198 if (verbose & VERBOSE_COMP_ID) {
3199 if (verbose & VERBOSE_DETAIL && !yaml)
3200 llapi_printf(LLAPI_MSG_NORMAL,
3201 "%slcme_id: ", " - ");
3202 else if (verbose & ~VERBOSE_COMP_ID)
3203 llapi_printf(LLAPI_MSG_NORMAL,
3204 "%4slcme_id: ", " ");
3205 if (entry->lcme_id != LCME_ID_INVAL)
3206 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3208 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3212 if (verbose & VERBOSE_MIRROR_ID) {
3213 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3214 if (verbose & ~VERBOSE_MIRROR_ID)
3215 llapi_printf(LLAPI_MSG_NORMAL,
3216 "%4slcme_mirror_id: ", " ");
3217 if (entry->lcme_id != LCME_ID_INVAL)
3218 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3219 mirror_id_of(entry->lcme_id));
3221 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3225 if (verbose & VERBOSE_COMP_FLAGS) {
3226 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3227 if (verbose & ~VERBOSE_COMP_FLAGS)
3228 llapi_printf(LLAPI_MSG_NORMAL,
3229 "%4slcme_flags: ", " ");
3230 lcme_flags2str(entry->lcme_flags);
3233 /* print snapshot timestamp if its a nosync comp */
3234 if ((verbose & VERBOSE_COMP_FLAGS) &&
3235 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3236 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3237 if (verbose & ~VERBOSE_COMP_FLAGS)
3238 llapi_printf(LLAPI_MSG_NORMAL,
3239 "%4slcme_timestamp: ", " ");
3241 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3242 (unsigned long long)entry->lcme_timestamp);
3244 time_t stamp = entry->lcme_timestamp;
3245 char *date_str = asctime(localtime(&stamp));
3247 date_str[strlen(date_str) - 1] = '\0';
3248 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3254 if (verbose & VERBOSE_COMP_START) {
3255 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3256 if (verbose & ~VERBOSE_COMP_START)
3257 llapi_printf(LLAPI_MSG_NORMAL,
3258 "%4slcme_extent.e_start: ", " ");
3259 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3260 (unsigned long long)entry->lcme_extent.e_start);
3264 if (verbose & VERBOSE_COMP_END) {
3265 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3266 if (verbose & ~VERBOSE_COMP_END)
3267 llapi_printf(LLAPI_MSG_NORMAL,
3268 "%4slcme_extent.e_end: ", " ");
3269 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3270 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3272 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3273 (unsigned long long)entry->lcme_extent.e_end);
3278 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3279 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3280 } else if (verbose & VERBOSE_DETAIL) {
3281 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3282 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3283 " ", entry->lcme_offset);
3284 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3285 " ", entry->lcme_size);
3286 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3288 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3293 * Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3294 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3296 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3297 * The table below gives the answers for the specified parameters (value and
3298 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3299 * --------------------------------------
3300 * 1 | file > limit; sign > 0 | -1 / -1 |
3301 * 2 | file = limit; sign > 0 | -1 / -1 |
3302 * 3 | file < limit; sign > 0 | ? / 1 |
3303 * 4 | file > limit; sign = 0 | -1 / -1 |
3304 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3305 * 6 | file < limit; sign = 0 | ? / -1 |
3306 * 7 | file > limit; sign < 0 | 1 / 1 |
3307 * 8 | file = limit; sign < 0 | ? / -1 |
3308 * 9 | file < limit; sign < 0 | ? / -1 |
3309 * --------------------------------------
3310 * Note: 5th actually means that the value is within the interval
3311 * (limit - margin, limit].
3313 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3314 int sign, int negopt, unsigned long long margin,
3320 /* Drop the fraction of margin (of days or size). */
3321 if (file + margin <= limit)
3323 } else if (sign == 0) {
3324 if (file <= limit && file + margin > limit)
3326 else if (file + margin <= limit)
3328 } else if (sign < 0) {
3335 return negopt ? ~ret + 1 : ret;
3338 static inline struct lov_user_md *
3339 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3341 return (struct lov_user_md *)((char *)comp_v1 +
3342 comp_v1->lcm_entries[ent_idx].lcme_offset);
3345 static inline struct lov_user_ost_data_v1 *
3346 lov_v1v3_objects(struct lov_user_md *v1)
3348 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3349 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3351 return v1->lmm_objects;
3355 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3357 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3358 snprintf(pool_name, LOV_MAXPOOLNAME + 1, "%s",
3359 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3361 pool_name[0] = '\0';
3365 print_last_init_comp(struct find_param *param)
3367 /* print all component info */
3368 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3371 /* print specific component info */
3372 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3373 param->fp_check_comp_start || param->fp_check_comp_end ||
3374 param->fp_check_mirror_id || param->fp_check_mirror_index)
3380 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3384 if (param->fp_comp_end == LUSTRE_EOF) {
3385 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3386 match = end == LUSTRE_EOF ? 1 : -1;
3387 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3388 match = end == LUSTRE_EOF ? -1 : 1;
3389 else /* at least EOF */
3391 if (param->fp_exclude_comp_end)
3394 unsigned long long margin;
3396 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3397 match = find_value_cmp(end, param->fp_comp_end,
3398 param->fp_comp_end_sign,
3399 param->fp_exclude_comp_end, margin, 0);
3406 * An example of "getstripe -v" for a two components PFL file:
3409 * lcm_magic: 0x0BD60BD0
3413 * lcm_entry_count: 2
3417 * lcme_extent.e_start: 0
3418 * lcme_extent.e_end: 1048576
3422 * lmm_magic: 0x0BD10BD0
3423 * lmm_seq: 0x200000401
3424 * lmm_object_id: 0x1
3425 * lmm_fid: [0x200000401:0x1:0x0]
3426 * lmm_stripe_count: 1
3427 * lmm_stripe_size: 1048576
3428 * lmm_pattern: raid0
3430 * lmm_stripe_offset: 0
3432 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3436 * lcme_extent.e_start: 1048576
3437 * lcme_extent.e_end: EOF
3441 * lmm_magic: 0x0BD10BD0
3442 * lmm_seq: 0x200000401
3443 * lmm_object_id: 0x1
3444 * lmm_fid: [0x200000401:0x1:0x0]
3445 * lmm_stripe_count: 2
3446 * lmm_stripe_size: 1048576
3447 * lmm_pattern: raid0
3449 * lmm_stripe_offset: 1
3451 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3452 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3454 static void lov_dump_comp_v1(struct find_param *param, char *path,
3455 enum lov_dump_flags flags)
3457 struct lov_comp_md_entry_v1 *entry;
3458 struct lov_user_ost_data_v1 *objects;
3459 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3460 struct lov_user_md_v1 *v1;
3461 char pool_name[LOV_MAXPOOLNAME + 1];
3462 int obdindex = param->fp_obd_index;
3463 int i, j, match, ext;
3464 bool obdstripe = false;
3465 __u16 mirror_index = 0;
3466 __u16 mirror_id = 0;
3468 if (obdindex != OBD_NOT_FOUND) {
3469 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3470 i < comp_v1->lcm_entry_count; i++) {
3471 if (!(comp_v1->lcm_entries[i].lcme_flags &
3475 v1 = lov_comp_entry(comp_v1, i);
3476 objects = lov_v1v3_objects(v1);
3478 for (j = 0; j < v1->lmm_stripe_count; j++) {
3479 if (obdindex == objects[j].l_ost_idx) {
3492 lov_dump_comp_v1_header(param, path, flags);
3494 flags |= LDF_INDENT;
3496 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3497 entry = &comp_v1->lcm_entries[i];
3499 if (param->fp_check_comp_flags) {
3500 if (((param->fp_comp_flags & entry->lcme_flags) !=
3501 param->fp_comp_flags) ||
3502 (param->fp_comp_neg_flags & entry->lcme_flags))
3506 if (param->fp_check_comp_id &&
3507 param->fp_comp_id != entry->lcme_id)
3510 if (param->fp_check_comp_start) {
3511 match = find_value_cmp(entry->lcme_extent.e_start,
3512 param->fp_comp_start,
3513 param->fp_comp_start_sign,
3515 param->fp_comp_start_units, 0);
3520 if (param->fp_check_comp_end) {
3521 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3527 if (param->fp_check_mirror_index) {
3528 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3530 mirror_id = mirror_id_of(entry->lcme_id);
3533 match = find_value_cmp(mirror_index,
3534 param->fp_mirror_index,
3535 param->fp_mirror_index_sign,
3536 param->fp_exclude_mirror_index,
3540 } else if (param->fp_check_mirror_id) {
3541 if (mirror_id != mirror_id_of(entry->lcme_id))
3542 mirror_id = mirror_id_of(entry->lcme_id);
3544 match = find_value_cmp(mirror_id,
3545 param->fp_mirror_id,
3546 param->fp_mirror_id_sign,
3547 param->fp_exclude_mirror_id,
3553 if (print_last_init_comp(param)) {
3555 * if part of stripe info is needed, we'd print only
3556 * the last instantiated component info.
3558 if (entry->lcme_flags & LCME_FL_INIT)
3561 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3562 if (entry->lcme_flags & LCME_FL_EXTENSION)
3563 /* moved back below */
3571 if (entry->lcme_flags & LCME_FL_INIT) {
3572 if (obdindex != OBD_NOT_FOUND) {
3573 flags |= LDF_SKIP_OBJS;
3574 v1 = lov_comp_entry(comp_v1, i);
3575 objects = lov_v1v3_objects(v1);
3577 for (j = 0; j < v1->lmm_stripe_count; j++) {
3578 if (obdindex == objects[j].l_ost_idx) {
3579 flags &= ~LDF_SKIP_OBJS;
3584 flags &= ~LDF_SKIP_OBJS;
3587 flags |= LDF_SKIP_OBJS;
3590 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3592 lov_dump_comp_v1_entry(param, flags, i);
3594 v1 = lov_comp_entry(comp_v1, i);
3595 objects = lov_v1v3_objects(v1);
3596 lov_v1v3_pool_name(v1, pool_name);
3598 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3599 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3600 param->fp_max_depth, param->fp_verbose,
3603 if (print_last_init_comp(param)) {
3605 * directory layout contains only layout template, print the
3609 i = comp_v1->lcm_entry_count - 1;
3612 flags &= ~LDF_SKIP_OBJS;
3614 lov_dump_comp_v1_entry(param, flags, i);
3616 v1 = lov_comp_entry(comp_v1, i);
3617 objects = lov_v1v3_objects(v1);
3618 lov_v1v3_pool_name(v1, pool_name);
3620 entry = &comp_v1->lcm_entries[i];
3621 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3622 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3623 param->fp_max_depth, param->fp_verbose,
3628 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3629 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3632 static inline bool has_any_comp_options(struct find_param *param)
3634 enum llapi_layout_verbose verbose = param->fp_verbose;
3636 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3637 param->fp_check_comp_start || param->fp_check_comp_end ||
3638 param->fp_check_comp_flags)
3641 /* show full layout information, not component specific */
3642 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3645 return verbose & VERBOSE_COMP_OPTS;
3648 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3651 struct lov_user_md *lum = &orig->lmd_lmm;
3652 struct lov_user_mds_data *new;
3653 struct lov_comp_md_v1 *comp_v1;
3654 struct lov_comp_md_entry_v1 *ent;
3655 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3656 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3659 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3661 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3665 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3666 + sizeof(new->lmd_lmmsize));
3668 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3669 comp_v1->lcm_magic = lum->lmm_magic;
3670 comp_v1->lcm_size = lum_off + lum_size;
3671 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3672 comp_v1->lcm_flags = 0;
3673 comp_v1->lcm_entry_count = 1;
3675 ent = &comp_v1->lcm_entries[0];
3677 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3678 ent->lcme_extent.e_start = 0;
3679 ent->lcme_extent.e_end = LUSTRE_EOF;
3680 ent->lcme_offset = lum_off;
3681 ent->lcme_size = lum_size;
3683 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3688 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3689 enum lov_dump_flags flags)
3691 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3693 if (has_any_comp_options(param)) {
3694 struct lov_user_mds_data *new_lmd, *orig_lmd;
3696 orig_lmd = param->fp_lmd;
3697 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3698 if (new_lmd != NULL) {
3699 param->fp_lmd = new_lmd;
3700 lov_dump_comp_v1(param, path, flags);
3701 param->fp_lmd = orig_lmd;
3707 if (magic == LOV_USER_MAGIC_V1) {
3708 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3709 param->fp_lmd->lmd_lmm.lmm_objects,
3710 path, param->fp_obd_index,
3711 param->fp_max_depth, param->fp_verbose,
3714 char pool_name[LOV_MAXPOOLNAME + 1];
3715 struct lov_user_ost_data_v1 *objects;
3716 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3718 snprintf(pool_name, sizeof(pool_name), "%s",
3719 lmmv3->lmm_pool_name);
3720 objects = lmmv3->lmm_objects;
3721 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3722 objects, path, param->fp_obd_index,
3723 param->fp_max_depth, param->fp_verbose,
3728 static uint32_t check_foreign_type(uint32_t foreign_type)
3732 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3733 if (lu_foreign_types[i].lft_name == NULL)
3735 if (foreign_type == lu_foreign_types[i].lft_type)
3739 return LU_FOREIGN_TYPE_UNKNOWN;
3742 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3743 enum lov_dump_flags flags)
3745 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3746 bool yaml = flags & LDF_YAML;
3748 if (!yaml && param->fp_depth && path)
3749 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3751 if (param->fp_verbose & VERBOSE_DETAIL) {
3752 uint32_t type = check_foreign_type(lfm->lfm_type);
3754 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3756 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3758 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3760 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3761 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3762 lu_foreign_types[type].lft_name);
3764 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3766 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3769 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3770 lfm->lfm_length, lfm->lfm_value);
3771 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3774 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3775 enum lov_dump_flags flags)
3777 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3778 bool yaml = flags & LDF_YAML;
3780 if (!yaml && param->fp_depth && path)
3781 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3783 if (param->fp_verbose & VERBOSE_DETAIL) {
3784 uint32_t type = check_foreign_type(lfm->lfm_type);
3786 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3788 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3790 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3792 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3793 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3794 lu_foreign_types[type].lft_name);
3796 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3798 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3801 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3802 lfm->lfm_length, lfm->lfm_value);
3803 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3806 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3807 enum lov_dump_flags flags)
3811 if (param->fp_get_lmv || param->fp_get_default_lmv)
3812 magic = (__u32)param->fp_lmv_md->lum_magic;
3814 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3817 flags |= LDF_IS_RAW;
3820 if (param->fp_hex_idx)
3821 flags |= LDF_HEX_IDX;
3824 case LOV_USER_MAGIC_V1:
3825 case LOV_USER_MAGIC_V3:
3826 case LOV_USER_MAGIC_SPECIFIC:
3827 lov_dump_plain_user_lmm(param, path, flags);
3829 case LOV_USER_MAGIC_FOREIGN:
3830 lov_dump_foreign_lmm(param, path, flags);
3833 case LMV_USER_MAGIC: {
3834 char pool_name[LOV_MAXPOOLNAME + 1];
3835 struct lmv_user_md *lum;
3837 lum = (struct lmv_user_md *)param->fp_lmv_md;
3838 snprintf(pool_name, sizeof(pool_name), "%s",
3839 lum->lum_pool_name);
3840 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3841 param->fp_max_depth, param->fp_verbose,
3845 case LOV_USER_MAGIC_COMP_V1:
3846 lov_dump_comp_v1(param, path, flags);
3848 case LMV_MAGIC_FOREIGN:
3849 lmv_dump_foreign_lmm(param, path, flags);
3852 llapi_printf(LLAPI_MSG_NORMAL,
3853 "unknown lmm_magic: %#x (expecting one of %#x %#x %#x %#x)\n",
3854 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3855 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3856 LMV_USER_MAGIC, LMV_MAGIC_V1);
3861 static int llapi_file_get_stripe1(const char *path, struct lov_user_md *lum)
3867 fname = strrchr(path, '/');
3869 /* It should be a file (or other non-directory) */
3870 if (fname == NULL) {
3871 dname = (char *)malloc(2);
3875 fname = (char *)path;
3877 dname = (char *)malloc(fname - path + 1);
3880 strncpy(dname, path, fname - path);
3881 dname[fname - path] = '\0';
3885 fd = open(dname, O_RDONLY | O_NONBLOCK);
3891 strcpy((char *)lum, fname);
3892 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3895 if (close(fd) == -1 && rc == 0)
3903 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3905 char *canon_path = NULL;
3908 rc = llapi_file_get_stripe1(path, lum);
3909 if (!(rc == -ENOTTY || rc == -ENODATA))
3912 /* Handle failure due to symlinks by dereferencing path manually. */
3913 canon_path = canonicalize_file_name(path);
3914 if (canon_path == NULL)
3915 goto out; /* Keep original rc. */
3917 rc2 = llapi_file_get_stripe1(canon_path, lum);
3919 goto out; /* Keep original rc. */
3928 int llapi_file_lookup(int dirfd, const char *name)
3930 struct obd_ioctl_data data = { 0 };
3935 if (dirfd < 0 || name == NULL)
3938 data.ioc_version = OBD_IOCTL_VERSION;
3939 data.ioc_len = sizeof(data);
3940 data.ioc_inlbuf1 = (char *)name;
3941 data.ioc_inllen1 = strlen(name) + 1;
3943 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3945 llapi_error(LLAPI_MSG_ERROR, rc,
3946 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3951 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
3958 * Check if the file time matches all the given criteria (e.g. --atime +/-N).
3959 * Return -1 or 1 if file timestamp does not or does match the given criteria
3960 * correspondingly. Return 0 if the MDS time is being checked and there are
3961 * attributes on OSTs and it is not yet clear if the timespamp matches.
3963 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
3964 * updated timestamps.
3966 static int find_time_check(struct find_param *param, int mds)
3968 struct lov_user_mds_data *lmd = param->fp_lmd;
3972 /* Check if file is accepted. */
3973 if (param->fp_atime) {
3974 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
3975 param->fp_atime, param->fp_asign,
3976 param->fp_exclude_atime,
3977 param->fp_time_margin, mds);
3983 if (param->fp_mtime) {
3984 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
3985 param->fp_mtime, param->fp_msign,
3986 param->fp_exclude_mtime,
3987 param->fp_time_margin, mds);
3992 * If the previous check matches, but this one is not yet clear,
3993 * we should return 0 to do an RPC on OSTs.
3999 if (param->fp_ctime) {
4000 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4001 param->fp_ctime, param->fp_csign,
4002 param->fp_exclude_ctime,
4003 param->fp_time_margin, mds);
4008 * If the previous check matches, but this one is not yet clear,
4009 * we should return 0 to do an RPC on OSTs.
4018 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4020 struct lov_user_mds_data *lmd = param->fp_lmd;
4025 for (i = 0; i < 2; i++) {
4026 /* Check if file is accepted. */
4027 if (param->fp_newery[NEWERXY_ATIME][i]) {
4028 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4029 param->fp_newery[NEWERXY_ATIME][i],
4036 if (param->fp_newery[NEWERXY_MTIME][i]) {
4037 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4038 param->fp_newery[NEWERXY_MTIME][i],
4044 * If the previous check matches, but this one is not
4045 * yet clear, we should return 0 to do an RPC on OSTs.
4051 if (param->fp_newery[NEWERXY_CTIME][i]) {
4052 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4053 param->fp_newery[NEWERXY_CTIME][i],
4059 * If the previous check matches, but this one is not
4060 * yet clear, we should return 0 to do an RPC on OSTs.
4067 * File birth time (btime) can get from MDT directly.
4068 * if @from_mdt is true, it means the input file attributs are
4069 * obtained directly from MDT.
4070 * Thus, if @from_mdt is false, we should skip the following
4076 if (param->fp_newery[NEWERXY_BTIME][i]) {
4077 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4080 rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4081 param->fp_newery[NEWERXY_BTIME][i],
4092 * Check whether the stripes matches the indexes user provided
4096 static int check_obd_match(struct find_param *param)
4098 struct lov_user_ost_data_v1 *objects;
4099 struct lov_comp_md_v1 *comp_v1 = NULL;
4100 struct lov_user_mds_data *lmd = param->fp_lmd;
4101 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4102 int i, j, k, count = 1;
4104 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4107 if (!S_ISREG(lmd->lmd_stx.stx_mode))
4110 /* exclude foreign */
4111 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4112 return param->fp_exclude_obd;
4115 * Only those files should be accepted, which have a
4116 * stripe on the specified OST.
4118 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4119 comp_v1 = (struct lov_comp_md_v1 *)v1;
4120 count = comp_v1->lcm_entry_count;
4123 for (i = 0; i < count; i++) {
4125 v1 = lov_comp_entry(comp_v1, i);
4127 objects = lov_v1v3_objects(v1);
4129 for (j = 0; j < v1->lmm_stripe_count; j++) {
4130 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4133 for (k = 0; k < param->fp_num_obds; k++) {
4134 if (param->fp_obd_indexes[k] ==
4135 objects[j].l_ost_idx)
4136 return !param->fp_exclude_obd;
4141 return param->fp_exclude_obd;
4144 static int check_mdt_match(struct find_param *param)
4148 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4151 /* FIXME: For striped dir, we should get stripe information and check */
4152 for (i = 0; i < param->fp_num_mdts; i++) {
4153 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4154 return !param->fp_exclude_mdt;
4157 if (param->fp_exclude_mdt)
4164 * Check whether the obd is active or not, if it is
4165 * not active, just print the object affected by this
4168 static void print_failed_tgt(struct find_param *param, char *path, int type)
4170 struct obd_statfs stat_buf;
4171 struct obd_uuid uuid_buf;
4172 int tgt_nr, i, *indexes;
4175 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV) {
4176 llapi_error(LLAPI_MSG_NORMAL, ret, "%s: wrong statfs type(%d)",
4181 tgt_nr = (type == LL_STATFS_LOV) ? param->fp_obd_index :
4182 param->fp_mdt_index;
4183 indexes = (type == LL_STATFS_LOV) ? param->fp_obd_indexes :
4184 param->fp_mdt_indexes;
4186 for (i = 0; i < tgt_nr; i++) {
4187 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4188 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4190 ret = llapi_obd_statfs(path, type, indexes[i], &stat_buf,
4193 llapi_error(LLAPI_MSG_NORMAL, ret,
4194 "%s: obd_uuid: %s failed",
4195 __func__, param->fp_obd_uuid->uuid);
4199 static int find_check_stripe_size(struct find_param *param)
4201 struct lov_comp_md_v1 *comp_v1 = NULL;
4202 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4203 __u32 stripe_size = 0;
4204 int ret, i, count = 1;
4206 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4207 return param->fp_exclude_stripe_size ? 1 : -1;
4209 ret = param->fp_exclude_stripe_size ? 1 : -1;
4210 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4211 comp_v1 = (struct lov_comp_md_v1 *)v1;
4212 count = comp_v1->lcm_entry_count;
4215 for (i = 0; i < count; i++) {
4216 struct lov_comp_md_entry_v1 *ent;
4219 v1 = lov_comp_entry(comp_v1, i);
4221 ent = &comp_v1->lcm_entries[i];
4222 if (ent->lcme_flags & LCME_FL_EXTENSION)
4224 if (!(ent->lcme_flags & LCME_FL_INIT))
4227 stripe_size = v1->lmm_stripe_size;
4230 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4231 param->fp_stripe_size_sign,
4232 param->fp_exclude_stripe_size,
4233 param->fp_stripe_size_units, 0);
4238 static int find_check_ext_size(struct find_param *param)
4240 struct lov_comp_md_v1 *comp_v1;
4241 struct lov_user_md_v1 *v1;
4244 ret = param->fp_exclude_ext_size ? 1 : -1;
4245 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4246 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4249 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4250 struct lov_comp_md_entry_v1 *ent;
4252 v1 = lov_comp_entry(comp_v1, i);
4254 ent = &comp_v1->lcm_entries[i];
4255 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4258 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4259 param->fp_ext_size_sign,
4260 param->fp_exclude_ext_size,
4261 param->fp_ext_size_units, 0);
4262 /* If any ext_size matches */
4270 static __u32 find_get_stripe_count(struct find_param *param)
4272 struct lov_comp_md_v1 *comp_v1 = NULL;
4273 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4275 __u32 stripe_count = 0;
4277 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4280 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4281 comp_v1 = (struct lov_comp_md_v1 *)v1;
4282 count = comp_v1->lcm_entry_count;
4285 for (i = 0; i < count; i++) {
4287 struct lov_comp_md_entry_v1 *ent;
4289 v1 = lov_comp_entry(comp_v1, i);
4291 ent = &comp_v1->lcm_entries[i];
4292 if (!(ent->lcme_flags & LCME_FL_INIT))
4295 if (ent->lcme_flags & LCME_FL_EXTENSION)
4298 stripe_count = v1->lmm_stripe_count;
4301 return stripe_count;
4304 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4306 static int find_check_layout(struct find_param *param)
4308 struct lov_comp_md_v1 *comp_v1 = NULL;
4309 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4311 bool found = false, valid = false;
4313 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4314 comp_v1 = (struct lov_comp_md_v1 *)v1;
4315 count = comp_v1->lcm_entry_count;
4318 for (i = 0; i < count; i++) {
4320 v1 = lov_comp_entry(comp_v1, i);
4322 /* foreign file have a special magic but no pattern field */
4323 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4326 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4330 if (v1->lmm_pattern & param->fp_layout) {
4339 if ((found && !param->fp_exclude_layout) ||
4340 (!found && param->fp_exclude_layout))
4347 * if no type specified, check/exclude all foreign
4348 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4350 static int find_check_foreign(struct find_param *param)
4352 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4353 struct lov_foreign_md *lfm;
4355 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4356 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4357 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4358 return param->fp_exclude_foreign ? 1 : -1;
4362 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4363 lfm->lfm_type == param->fp_foreign_type)
4364 return param->fp_exclude_foreign ? -1 : 1;
4365 return param->fp_exclude_foreign ? 1 : -1;
4368 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4369 struct lmv_foreign_md *lfm;
4371 lfm = (void *)param->fp_lmv_md;
4372 if (lmv_is_foreign(lfm->lfm_magic)) {
4373 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4374 return param->fp_exclude_foreign ? 1 : -1;
4378 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4379 lfm->lfm_type == param->fp_foreign_type)
4380 return param->fp_exclude_foreign ? -1 : 1;
4381 return param->fp_exclude_foreign ? 1 : -1;
4386 static int find_check_pool(struct find_param *param)
4388 struct lov_comp_md_v1 *comp_v1 = NULL;
4389 struct lov_user_md_v3 *v3 = (void *)¶m->fp_lmd->lmd_lmm;
4393 if (v3->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4394 comp_v1 = (struct lov_comp_md_v1 *)v3;
4395 count = comp_v1->lcm_entry_count;
4396 /* empty requested pool is taken as no pool search */
4397 if (count == 0 && param->fp_poolname[0] == '\0') {
4403 for (i = 0; i < count; i++) {
4404 if (comp_v1 != NULL) {
4405 if (!(comp_v1->lcm_entries[i].lcme_flags &
4409 v3 = (void *)lov_comp_entry(comp_v1, i);
4412 if (v3->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4415 if (((v3->lmm_magic == LOV_USER_MAGIC_V1) &&
4416 (param->fp_poolname[0] == '\0')) ||
4417 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4418 (strncmp(v3->lmm_pool_name,
4419 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4420 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4421 (strcmp(param->fp_poolname, "*") == 0))) {
4428 if ((found && !param->fp_exclude_pool) ||
4429 (!found && param->fp_exclude_pool))
4435 static int find_check_comp_options(struct find_param *param)
4437 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4438 struct lov_user_mds_data *lmd = param->fp_lmd;
4439 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4440 struct lov_comp_md_entry_v1 *entry;
4443 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4446 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4447 comp_v1 = (struct lov_comp_md_v1 *)v1;
4449 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4450 if (forged_v1 == NULL)
4452 comp_v1 = forged_v1;
4453 comp_v1->lcm_entry_count = 1;
4454 entry = &comp_v1->lcm_entries[0];
4455 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4457 entry->lcme_extent.e_start = 0;
4458 entry->lcme_extent.e_end = LUSTRE_EOF;
4461 /* invalid case, don't match for any kind of search. */
4462 if (comp_v1->lcm_entry_count == 0) {
4467 if (param->fp_check_comp_count) {
4468 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4469 param->fp_comp_count,
4470 param->fp_comp_count_sign,
4471 param->fp_exclude_comp_count, 1, 0);
4477 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4478 entry = &comp_v1->lcm_entries[i];
4480 if (param->fp_check_comp_flags) {
4482 if (((param->fp_comp_flags & entry->lcme_flags) !=
4483 param->fp_comp_flags) ||
4484 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4490 if (param->fp_check_comp_start) {
4491 ret = find_value_cmp(entry->lcme_extent.e_start,
4492 param->fp_comp_start,
4493 param->fp_comp_start_sign,
4494 param->fp_exclude_comp_start,
4495 param->fp_comp_start_units, 0);
4500 if (param->fp_check_comp_end) {
4501 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4507 /* the component matches all criteria */
4516 static int find_check_mirror_options(struct find_param *param)
4518 struct lov_comp_md_v1 *comp_v1;
4519 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4522 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4525 comp_v1 = (struct lov_comp_md_v1 *)v1;
4527 if (param->fp_check_mirror_count) {
4528 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4529 param->fp_mirror_count,
4530 param->fp_mirror_count_sign,
4531 param->fp_exclude_mirror_count, 1, 0);
4536 if (param->fp_check_mirror_state) {
4538 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4540 if ((param->fp_mirror_state != 0 &&
4541 file_state != param->fp_mirror_state) ||
4542 file_state == param->fp_mirror_neg_state)
4549 static int find_check_attr_options(struct find_param *param)
4554 attrs = param->fp_lmd->lmd_stx.stx_attributes_mask &
4555 param->fp_lmd->lmd_stx.stx_attributes;
4557 /* This is a AND between all (negated) specified attributes */
4558 if ((param->fp_attrs && (param->fp_attrs & attrs) != param->fp_attrs) ||
4559 (param->fp_neg_attrs && (param->fp_neg_attrs & attrs)))
4562 if ((found && param->fp_exclude_attrs) ||
4563 (!found && !param->fp_exclude_attrs))
4569 static bool find_check_lmm_info(struct find_param *param)
4571 return param->fp_check_pool || param->fp_check_stripe_count ||
4572 param->fp_check_stripe_size || param->fp_check_layout ||
4573 param->fp_check_comp_count || param->fp_check_comp_end ||
4574 param->fp_check_comp_start || param->fp_check_comp_flags ||
4575 param->fp_check_mirror_count || param->fp_check_foreign ||
4576 param->fp_check_mirror_state || param->fp_check_ext_size ||
4577 param->fp_check_projid;
4581 * Interpret backslash escape sequences and write output into buffer.
4582 * Anything written to the buffer will be null terminated.
4584 * @param[in] seq String being parsed for escape sequence. The leading
4585 * '\' character is not included in this string (only the
4586 * characters after it)
4587 * @param[out] buffer Location where interpreted escape sequence is written
4588 * @param[in] size Size of the available buffer. (Needs to be large enough
4589 * to handle escape sequence output plus null terminator.)
4590 * @param[out] wrote Number of bytes written to the buffer.
4591 * @return Number of characters from input string processed
4592 * as part of the escape sequence (0 for an unrecognized
4595 int printf_format_escape(char *seq, char *buffer, size_t size, int *wrote)
4598 /* For now, only handle single char escape sequences: \n, \t, \\ */
4621 * Interpret formats for timestamps (%a, %A@, etc)
4623 * @param[in] seq String being parsed for timestamp format. The leading
4624 * '%' character is not included in this string
4625 * @param[out] buffer Location where timestamp info is written
4626 * @param[in] size Size of the available buffer.
4627 * @param[out] wrote Number of bytes written to the buffer.
4628 * @return Number of characters from input string processed
4629 * as part of the format (0 for an unknown format)
4632 int printf_format_timestamp(char *seq, char *buffer, size_t size, int *wrote,
4633 struct find_param *param)
4635 struct statx_timestamp ts = { 0, 0 };
4639 char *fmt = "%c"; /* Print in ctime format by default */
4644 ts = param->fp_lmd->lmd_stx.stx_atime;
4648 if (*(seq + 1) == '@') {
4649 ts = param->fp_lmd->lmd_stx.stx_atime;
4655 ts = param->fp_lmd->lmd_stx.stx_ctime;
4659 if (*(seq + 1) == '@') {
4660 ts = param->fp_lmd->lmd_stx.stx_ctime;
4666 ts = param->fp_lmd->lmd_stx.stx_mtime;
4670 if (*(seq + 1) == '@') {
4671 ts = param->fp_lmd->lmd_stx.stx_mtime;
4677 ts = param->fp_lmd->lmd_stx.stx_btime;
4681 if (*(seq + 1) == '@') {
4682 ts = param->fp_lmd->lmd_stx.stx_btime;
4692 /* Found valid format, print to buffer */
4695 *wrote = strftime(buffer, size, fmt, tm);
4702 * Print all ost indices associated with a file layout using a commma separated
4703 * list. For a file with mutliple components, the list of indices for each
4704 * component will be enclosed in brackets.
4706 * @param[out] buffer Location where OST indices are written
4707 * @param[in] size Size of the available buffer.
4708 * @pararm[in] layout Pointer to layout structure for the file
4709 * @return Number of bytes written to output buffer
4711 static int printf_format_ost_indices(char *buffer, size_t size,
4712 struct llapi_layout *layout)
4714 uint64_t count, idx, i;
4715 int err, bytes, wrote = 0;
4717 /* Make sure to start at the first component */
4718 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
4720 llapi_error(LLAPI_MSG_ERROR, err,
4721 "error: layout component iteration failed\n");
4725 err = llapi_layout_stripe_count_get(layout, &count);
4727 llapi_error(LLAPI_MSG_ERROR, err,
4728 "error: cannot get stripe_count\n");
4732 bytes = snprintf(buffer, (size - wrote), "%s", "[");
4737 for (i = 0; i < count; i++) {
4738 err = llapi_layout_ost_index_get(layout, i, &idx);
4740 llapi_error(LLAPI_MSG_ERROR, err,
4741 "error: cannot get OST index\n");
4742 bytes = snprintf(buffer, (size - wrote),
4745 bytes = snprintf(buffer, (size - wrote),
4753 /* Overwrite last comma with closing bracket */
4754 *(buffer - 1) = ']';
4756 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
4757 if (err == 0) /* next component is found */
4760 llapi_error(LLAPI_MSG_ERROR, err,
4761 "error: layout component iteration failed\n");
4762 /* At this point, either got error or reached last component */
4773 * Print file attributes as a comma-separated list of named attribute flags,
4774 * and hex value of any unknown attributes.
4776 * @param[out] buffer Location where file attributes are written
4777 * @param[in] size Size of the available buffer.
4778 * @pararm[in] stx struct statx containing attributes to print
4779 * @return Number of bytes written to output buffer
4781 static int printf_format_file_attributes(char *buffer, size_t size,
4782 lstatx_t stx, bool longopt)
4784 uint64_t attrs = stx.stx_attributes_mask & stx.stx_attributes;
4785 int bytes = 0, wrote = 0, first = 1;
4786 uint64_t known_attrs = 0;
4787 struct attrs_name *ap;
4789 /* before all, print '---' if no attributes, and exit */
4791 bytes = snprintf(buffer, size - wrote, "---");
4796 /* first, browse list of known attributes */
4797 for (ap = (struct attrs_name *)attrs_array; ap->an_attr != 0; ap++) {
4798 known_attrs |= ap->an_attr;
4799 if (attrs & ap->an_attr) {
4801 bytes = snprintf(buffer, size - wrote, "%s%s",
4802 first ? "" : ",", ap->an_name);
4804 bytes = snprintf(buffer, size - wrote, "%c",
4814 /* second, print hex value for unknown attributes */
4815 attrs &= ~known_attrs;
4817 bytes = snprintf(buffer, size - wrote, "%s0x%lx",
4818 first ? "" : ",", attrs);
4829 * Parse Lustre-specific format sequences of the form %L{x}.
4831 * @param[in] seq String being parsed for format sequence. The leading
4832 * '%' character is not included in this string
4833 * @param[out] buffer Location where interpreted format info is written
4834 * @param[in] size Size of the available buffer.
4835 * @param[out] wrote Number of bytes written to the buffer.
4836 * @param[in] param The find_param structure associated with the file/dir
4837 * @param[in] path Pathname of the current file/dir being handled
4838 * @param[in] projid Project ID associated with the current file/dir
4839 * @param[in] d File descriptor for the directory (or -1 for a
4840 * non-directory file)
4841 * @return Number of characters from input string processed
4842 * as part of the format (0 for an unknown format)
4844 int printf_format_lustre(char *seq, char *buffer, size_t size, int *wrote,
4845 struct find_param *param, char *path, __u32 projid,
4848 struct lmv_user_md *lum;
4849 struct lmv_user_mds_data *objects;
4850 struct llapi_layout *layout = NULL;
4852 unsigned int hash_type;
4853 uint64_t str_cnt, str_size, idx;
4854 char pool_name[LOV_MAXPOOLNAME + 1] = { '\0' };
4856 bool longopt = true;
4857 int rc = 2; /* all current valid sequences are 2 chars */
4860 /* Sanity check. Formats always look like %L{X} */
4861 if (*seq++ != 'L') {
4867 * Some formats like %LF or %LP are handled the same for both files
4868 * and dirs, so handle all of those here.
4872 err = llapi_path2fid(path, &fid);
4874 llapi_error(LLAPI_MSG_ERROR, err,
4875 "error: cannot get fid\n");
4878 *wrote = snprintf(buffer, size, DFID_NOBRACE, PFID(&fid));
4881 if (projid == INVALID_PROJID)
4882 *wrote = snprintf(buffer, size, "-1");
4884 *wrote = snprintf(buffer, size, "%u", projid);
4886 case 'a': /* file attributes */
4890 *wrote = printf_format_file_attributes(buffer, size,
4891 param->fp_lmd->lmd_stx,
4896 /* Other formats for files/dirs need to be handled differently */
4897 if (d == -1) { /* file */
4898 //layout = llapi_layout_get_by_xattr(¶m->fp_lmd->lmd_lmm,
4899 // param->fp_lum_size, 0);
4900 layout = llapi_layout_get_by_path(path, 0);
4901 if (layout == NULL) {
4902 llapi_error(LLAPI_MSG_ERROR, errno,
4903 "error: cannot get file layout\n");
4908 * Set the layout pointer to the last init component
4909 * since that is the component used for most of these
4910 * formats. (This also works for non-composite files)
4912 err = llapi_layout_get_last_init_comp(layout);
4914 llapi_error(LLAPI_MSG_ERROR, err,
4915 "error: cannot get last initialized compomnent\n");
4920 case 'c': /* stripe count */
4921 err = llapi_layout_stripe_count_get(layout, &str_cnt);
4923 llapi_error(LLAPI_MSG_ERROR, err,
4924 "error: cannot get stripe_count\n");
4927 *wrote = snprintf(buffer, size, "%"PRIu64, str_cnt);
4929 case 'h': /* hash info */
4930 /* Not applicable to files. Skip it. */
4932 case 'i': /* starting index */
4933 err = llapi_layout_ost_index_get(layout, 0, &idx);
4935 llapi_error(LLAPI_MSG_ERROR, err,
4936 "error: cannot get OST index of last initialized component\n");
4939 *wrote = snprintf(buffer, size, "%"PRIu64, idx);
4941 case 'o': /* list of object indices */
4942 *wrote = printf_format_ost_indices(buffer, size, layout);
4944 case 'p': /* pool name */
4945 err = llapi_layout_pool_name_get(layout, pool_name,
4948 llapi_error(LLAPI_MSG_ERROR, rc,
4949 "error: cannot get pool name\n");
4952 *wrote = snprintf(buffer, size, "%s", pool_name);
4954 case 'S': /* stripe size */
4955 err = llapi_layout_stripe_size_get(layout, &str_size);
4957 llapi_error(LLAPI_MSG_ERROR, rc,
4958 "error: cannot get stripe_size\n");
4961 *wrote = snprintf(buffer, size, "%"PRIu64, str_size);
4967 } else { /* directory */
4968 lum = (struct lmv_user_md *)param->fp_lmv_md;
4969 objects = lum->lum_objects;
4972 case 'c': /* stripe count */
4973 *wrote = snprintf(buffer, size, "%d",
4974 (int)lum->lum_stripe_count);
4976 case 'h': /* hash info */
4977 hash_type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
4978 if (hash_type < LMV_HASH_TYPE_MAX)
4979 *wrote = snprintf(buffer, size, "%s",
4980 mdt_hash_name[hash_type]);
4982 *wrote = snprintf(buffer, size, "%#x",
4985 case 'i': /* starting index */
4986 *wrote = snprintf(buffer, size, "%d",
4987 lum->lum_stripe_offset);
4989 case 'o': /* list of object indices */
4990 str_cnt = (int) lum->lum_stripe_count;
4991 *wrote = snprintf(buffer, size, "%s", "[");
4995 for (i = 0; i < str_cnt; i++) {
4996 bytes = snprintf(buffer, (size - *wrote),
4997 "%d,", objects[i].lum_mds);
5004 /* Use lum_offset as the only list entry */
5005 bytes = snprintf(buffer, (size - *wrote),
5006 "%d]", lum->lum_stripe_offset);
5009 /* Overwrite last comma with closing bracket */
5010 *(buffer - 1) = ']';
5013 case 'p': /* pool name */
5014 *wrote = snprintf(buffer, size, "%s",
5015 lum->lum_pool_name);
5017 case 'S': /* stripe size */
5018 /* This has no meaning for directories. Skip it. */
5028 llapi_layout_free(layout);
5031 /* output of snprintf was truncated */
5038 * Interpret format specifiers beginning with '%'.
5040 * @param[in] seq String being parsed for format specifier. The leading
5041 * '%' character is not included in this string
5042 * @param[out] buffer Location where formatted info is written
5043 * @param[in] size Size of the available buffer.
5044 * @param[out] wrote Number of bytes written to the buffer.
5045 * @param[in] param The find_param structure associated with the file/dir
5046 * @param[in] path Pathname of the current file/dir being handled
5047 * @param[in] projid Project ID associated with the current file/dir
5048 * @param[in] d File descriptor for the directory (or -1 for a
5049 * non-directory file)
5050 * @return Number of characters from input string processed
5051 * as part of the format (0 for an unknown format)
5053 int printf_format_directive(char *seq, char *buffer, size_t size, int *wrote,
5054 struct find_param *param, char *path, __u32 projid,
5057 __u16 mode = param->fp_lmd->lmd_stx.stx_mode;
5058 uint64_t blocks = param->fp_lmd->lmd_stx.stx_blocks;
5059 int rc = 1; /* most specifiers are single character */
5067 case 'w': case 'W': /* timestamps */
5068 rc = printf_format_timestamp(seq, buffer, size, wrote, param);
5070 case 'b': /* file size (in 512B blocks) */
5071 *wrote = snprintf(buffer, size, "%"PRIu64, blocks);
5073 case 'G': /* GID of owner */
5074 *wrote = snprintf(buffer, size, "%u",
5075 param->fp_lmd->lmd_stx.stx_gid);
5077 case 'k': /* file size (in 1K blocks) */
5078 *wrote = snprintf(buffer, size, "%"PRIu64, (blocks + 1)/2);
5080 case 'L': /* Lustre-specific formats */
5081 rc = printf_format_lustre(seq, buffer, size, wrote, param,
5084 case 'm': /* file mode in octal */
5085 *wrote = snprintf(buffer, size, "%#o", (mode & (~S_IFMT)));
5087 case 'n': /* number of links */
5088 *wrote = snprintf(buffer, size, "%u",
5089 param->fp_lmd->lmd_stx.stx_nlink);
5091 case 'p': /* Path name of file */
5092 *wrote = snprintf(buffer, size, "%s", path);
5094 case 's': /* file size (in bytes) */
5095 *wrote = snprintf(buffer, size, "%"PRIu64,
5096 (uint64_t) param->fp_lmd->lmd_stx.stx_size);
5098 case 'U': /* UID of owner */
5099 *wrote = snprintf(buffer, size, "%u",
5100 param->fp_lmd->lmd_stx.stx_uid);
5102 case 'y': /* file type */
5105 else if (S_ISDIR(mode))
5107 else if (S_ISLNK(mode))
5109 else if (S_ISBLK(mode))
5111 else if (S_ISCHR(mode))
5113 else if (S_ISFIFO(mode))
5115 else if (S_ISSOCK(mode))
5125 default: /* invalid format specifier */
5131 /* output of snprintf was truncated */
5138 * Parse user-supplied string for the -printf option and interpret any
5139 * '%' format specifiers or '\' escape sequences.
5141 * @param[in] param The find_param struct containing the -printf string
5142 * as well as info about the current file/dir that mathced
5143 * the lfs find search criteria
5144 * @param[in] path Path name for current file/dir
5145 * @param[in] projid Project ID associated with current file/dir
5146 * @param[in] d File descriptor for current directory (or -1 for a
5147 * non-directory file)
5149 void printf_format_string(struct find_param *param, char *path,
5150 __u32 projid, int d)
5152 char output[FORMATTED_BUF_LEN];
5153 char *fmt_char = param->fp_format_printf_str;
5154 char *buff = output;
5160 buff_size = FORMATTED_BUF_LEN;
5162 /* Always leave one free byte in buffer for trailing NUL */
5163 while (*fmt_char && (buff_size > 1)) {
5166 if (*fmt_char == '%') {
5167 rc = printf_format_directive(fmt_char + 1, buff,
5168 buff_size, &written, param,
5170 } else if (*fmt_char == '\\')
5171 rc = printf_format_escape(fmt_char + 1, buff,
5172 buff_size, &written);
5175 /* Either a '\' escape or '%' format was processed.
5176 * Increment pointers accordingly.
5178 fmt_char += (rc + 1);
5180 buff_size -= written;
5182 /* Regular char or invalid escape/format.
5183 * Either way, copy current character.
5185 *buff++ = *fmt_char++;
5190 /* Terminate output buffer and print */
5192 llapi_printf(LLAPI_MSG_NORMAL, "%s", output);
5196 * Get file/directory project id.
5197 * by the open fd resides on.
5198 * Return 0 and project id on success, or -ve errno.
5200 static int fget_projid(int fd, __u32 *projid)
5205 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
5207 *projid = INVALID_PROJID;
5211 *projid = fsx.fsx_projid;
5216 * Check that the file's permissions in *st matches the one in find_param
5218 static int check_file_permissions(const struct find_param *param,
5225 switch (param->fp_perm_sign) {
5226 case LFS_FIND_PERM_EXACT:
5227 decision = (mode == param->fp_perm);
5229 case LFS_FIND_PERM_ALL:
5230 decision = ((mode & param->fp_perm) == param->fp_perm);
5232 case LFS_FIND_PERM_ANY:
5233 decision = ((mode & param->fp_perm) != 0);
5237 if ((param->fp_exclude_perm && decision)
5238 || (!param->fp_exclude_perm && !decision))
5244 static int cb_find_init(char *path, int p, int *dp,
5245 void *data, struct dirent64 *de)
5247 struct find_param *param = (struct find_param *)data;
5248 struct lov_user_mds_data *lmd = param->fp_lmd;
5249 int d = dp == NULL ? -1 : *dp;
5250 int decision = 1; /* 1 is accepted; -1 is rejected. */
5252 int checked_type = 0;
5254 __u32 stripe_count = 0;
5257 __u32 projid = INVALID_PROJID;
5258 bool gather_all = false;
5260 if (p == -1 && d == -1)
5263 /* Reset this value between invocations */
5264 param->fp_get_lmv = 0;
5266 /* Gather all file/dir info, not just what's needed for search params */
5267 if (param->fp_format_printf_str)
5270 /* If a regular expression is presented, make the initial decision */
5271 if (param->fp_pattern != NULL) {
5272 char *fname = strrchr(path, '/');
5274 fname = (fname == NULL ? path : fname + 1);
5275 ret = fnmatch(param->fp_pattern, fname, 0);
5276 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
5277 (ret == 0 && param->fp_exclude_pattern))
5281 /* See if we can check the file type from the dirent. */
5282 if (de != NULL && de->d_type != DT_UNKNOWN) {
5283 if (param->fp_type != 0) {
5286 if (DTTOIF(de->d_type) == param->fp_type) {
5287 if (param->fp_exclude_type)
5290 if (!param->fp_exclude_type)
5294 if ((param->fp_check_mdt_count || param->fp_hash_type ||
5295 param->fp_check_hash_flag) && de->d_type != DT_DIR)
5302 * Request MDS for the stat info if some of these parameters need
5305 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
5306 param->fp_check_uid || param->fp_check_gid ||
5307 param->fp_newerxy || param->fp_btime ||
5308 param->fp_atime || param->fp_mtime || param->fp_ctime ||
5309 param->fp_check_size || param->fp_check_blocks ||
5310 find_check_lmm_info(param) ||
5311 param->fp_check_mdt_count || param->fp_hash_type ||
5312 param->fp_check_hash_flag || param->fp_perm_sign ||
5313 param->fp_nlink || param->fp_attrs || param->fp_neg_attrs ||
5317 if (param->fp_type != 0 && checked_type == 0)
5320 if (decision == 0) {
5322 (param->fp_check_mdt_count || param->fp_hash_type ||
5323 param->fp_check_hash_flag || param->fp_check_foreign ||
5325 * cb_get_dirstripe is needed when checking nlink because
5326 * nlink is handled differently for multi-stripe directory
5327 * vs. single-stripe directory
5329 param->fp_nlink || gather_all)) {
5330 param->fp_get_lmv = 1;
5331 ret = cb_get_dirstripe(path, &d, param);
5333 if (errno == ENODATA) {
5334 /* Fill in struct for unstriped dir */
5336 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
5337 /* Use 0 until we find actual offset */
5338 param->fp_lmv_md->lum_stripe_offset = 0;
5339 param->fp_lmv_md->lum_stripe_count = 0;
5340 param->fp_lmv_md->lum_hash_type = 0;
5342 if (param->fp_check_foreign) {
5343 if (param->fp_exclude_foreign)
5352 if (param->fp_check_mdt_count) {
5353 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5356 decision = find_value_cmp(param->fp_lmv_md->lum_stripe_count,
5357 param->fp_mdt_count,
5358 param->fp_mdt_count_sign,
5359 param->fp_exclude_mdt_count, 1, 0);
5364 if (param->fp_hash_type) {
5366 __u32 type = param->fp_lmv_md->lum_hash_type &
5369 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5372 found = (1 << type) & param->fp_hash_type;
5373 if ((found && param->fp_exclude_hash_type) ||
5374 (!found && !param->fp_exclude_hash_type))
5378 if (param->fp_check_hash_flag) {
5379 __u32 flags = param->fp_lmv_md->lum_hash_type &
5380 ~LMV_HASH_TYPE_MASK;
5382 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5385 if (!(flags & param->fp_hash_inflags) ||
5386 (flags & param->fp_hash_exflags))
5391 param->fp_lmd->lmd_lmm.lmm_magic = 0;
5392 ret = get_lmd_info_fd(path, p, d, param->fp_lmd,
5393 param->fp_lum_size, GET_LMD_INFO);
5394 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
5395 find_check_lmm_info(param)) {
5396 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
5399 * We need to "fake" the "use the default" values
5400 * since the lmm struct is zeroed out at this point.
5402 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5403 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
5405 ostid_set_seq(&lmm->lmm_oi,
5406 FID_SEQ_LOV_DEFAULT);
5407 lmm->lmm_stripe_size = 0;
5408 lmm->lmm_stripe_count = 0;
5409 lmm->lmm_stripe_offset = -1;
5411 if (ret == 0 && (param->fp_mdt_uuid != NULL || gather_all)) {
5413 ret = llapi_file_fget_mdtidx(d,
5414 ¶m->fp_file_mdt_index);
5416 * Make sure lum_stripe_offset matches
5417 * mdt_index even for unstriped directories.
5419 if (ret == 0 && param->fp_get_lmv)
5420 param->fp_lmv_md->lum_stripe_offset =
5421 param->fp_file_mdt_index;
5422 } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
5424 * FIXME: we could get the MDT index from the
5425 * file's FID in lmd->lmd_lmm.lmm_oi without
5426 * opening the file, once we are sure that
5427 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
5428 * That would still be an ioctl() to map the
5429 * FID to the MDT, but not an open RPC.
5431 fd = open(path, O_RDONLY);
5433 ret = llapi_file_fget_mdtidx(fd,
5434 ¶m->fp_file_mdt_index);
5440 * For a special file, we assume it resides on
5441 * the same MDT as the parent directory.
5443 ret = llapi_file_fget_mdtidx(p,
5444 ¶m->fp_file_mdt_index);
5455 stripe_count = find_get_stripe_count(param);
5459 /* Check the file permissions from the stat info */
5460 if (param->fp_perm_sign) {
5461 decision = check_file_permissions(param, lmd->lmd_stx.stx_mode);
5466 if (param->fp_type && !checked_type) {
5467 if ((param->fp_check_mdt_count || param->fp_check_hash_flag ||
5468 param->fp_hash_type) && !S_ISDIR(lmd->lmd_stx.stx_mode))
5471 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
5472 if (param->fp_exclude_type)
5475 if (!param->fp_exclude_type)
5481 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
5482 if (lustre_fs && param->fp_got_uuids &&
5483 param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
5484 lmd->lmd_stx.stx_dev_minor)) {
5485 /* A lustre/lustre mount point is crossed. */
5486 param->fp_got_uuids = 0;
5487 param->fp_obds_printed = 0;
5488 param->fp_mdt_index = OBD_NOT_FOUND;
5489 param->fp_obd_index = OBD_NOT_FOUND;
5492 if (lustre_fs && !param->fp_got_uuids) {
5493 ret = setup_target_indexes((d != -1) ? d : p, path,
5498 param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
5499 lmd->lmd_stx.stx_dev_minor);
5500 } else if (!lustre_fs && param->fp_got_uuids) {
5501 /* A lustre/non-lustre mount point is crossed. */
5502 param->fp_got_uuids = 0;
5503 param->fp_mdt_index = OBD_NOT_FOUND;
5504 param->fp_obd_index = OBD_NOT_FOUND;
5508 if (param->fp_check_foreign) {
5509 decision = find_check_foreign(param);
5514 if (param->fp_check_stripe_size) {
5515 decision = find_check_stripe_size(param);
5520 if (param->fp_check_ext_size) {
5521 decision = find_check_ext_size(param);
5526 if (param->fp_check_stripe_count) {
5527 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
5528 param->fp_stripe_count_sign,
5529 param->fp_exclude_stripe_count, 1, 0);
5534 if (param->fp_check_layout) {
5535 decision = find_check_layout(param);
5540 /* If an OBD UUID is specified but none matches, skip this file. */
5541 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
5542 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
5546 * If an OST or MDT UUID is given, and some OST matches,
5549 if (param->fp_obd_index != OBD_NOT_FOUND ||
5550 param->fp_mdt_index != OBD_NOT_FOUND) {
5551 if (param->fp_obd_uuid) {
5552 if (check_obd_match(param)) {
5554 * If no mdtuuid is given, we are done.
5555 * Otherwise, fall through to the mdtuuid
5558 if (!param->fp_mdt_uuid)
5565 if (param->fp_mdt_uuid) {
5566 if (check_mdt_match(param))
5573 if (param->fp_check_uid) {
5574 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
5575 if (param->fp_exclude_uid)
5578 if (!param->fp_exclude_uid)
5583 if (param->fp_check_gid) {
5584 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
5585 if (param->fp_exclude_gid)
5588 if (!param->fp_exclude_gid)
5593 if (param->fp_check_projid || gather_all) {
5595 fd = open(path, O_RDONLY | O_NONBLOCK);
5598 ret = fget_projid(fd, &projid);
5601 if (param->fp_check_projid) {
5604 if (projid == param->fp_projid) {
5605 if (param->fp_exclude_projid)
5608 if (!param->fp_exclude_projid)
5614 if (param->fp_check_pool) {
5615 decision = find_check_pool(param);
5620 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
5621 param->fp_check_comp_start || param->fp_check_comp_end) {
5622 decision = find_check_comp_options(param);
5627 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
5628 decision = find_check_mirror_options(param);
5633 /* Check the time on mds. */
5635 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
5638 for_mds = lustre_fs ?
5639 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5640 decision = find_time_check(param, for_mds);
5645 if (param->fp_btime) {
5646 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
5651 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
5652 param->fp_btime, param->fp_bsign,
5653 param->fp_exclude_btime,
5654 param->fp_time_margin, 0);
5659 if (param->fp_newerxy) {
5662 for_mds = lustre_fs ?
5663 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5664 decision = find_newerxy_check(param, for_mds, true);
5673 if (param->fp_attrs || param->fp_neg_attrs) {
5674 decision = find_check_attr_options(param);
5679 flags = param->fp_lmd->lmd_flags;
5680 if (param->fp_check_size &&
5681 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5682 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5683 !(flags & OBD_MD_FLSIZE ||
5684 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
5687 if (param->fp_check_blocks &&
5688 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5689 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5690 !(flags & OBD_MD_FLBLOCKS ||
5691 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
5695 * When checking nlink, stat(2) is needed for multi-striped directories
5696 * because the nlink value retrieved from the MDS above comes from
5697 * the number of stripes for the dir.
5698 * The posix stat call below fills in the correct number of links.
5699 * Single-stripe directories and regular files already have the
5700 * correct nlink value.
5702 if (param->fp_nlink && S_ISDIR(lmd->lmd_stx.stx_mode) &&
5703 (param->fp_lmv_md->lum_stripe_count != 0))
5707 * If file still fits the request, ask ost for updated info.
5708 * The regular stat is almost of the same speed as some new
5709 * 'glimpse-size-ioctl'.
5711 if (!decision || gather_all) {
5715 * For regular files with the stripe the decision may have not
5716 * been taken yet if *time or size is to be checked.
5718 if (param->fp_obd_index != OBD_NOT_FOUND)
5719 print_failed_tgt(param, path, LL_STATFS_LOV);
5721 if (param->fp_mdt_index != OBD_NOT_FOUND)
5722 print_failed_tgt(param, path, LL_STATFS_LMV);
5725 ret = fstat_f(d, &st);
5726 else if (de != NULL)
5727 ret = fstatat_f(p, de->d_name, &st,
5728 AT_SYMLINK_NOFOLLOW);
5730 ret = lstat_f(path, &st);
5733 if (errno == ENOENT) {
5734 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
5735 "warning: %s: %s does not exist",
5740 llapi_error(LLAPI_MSG_ERROR, ret,
5741 "%s: stat on %s failed",
5747 convert_lmd_statx(param->fp_lmd, &st, true);
5748 /* Check the time on osc. */
5749 decision = find_time_check(param, 0);
5753 if (param->fp_newerxy) {
5754 decision = find_newerxy_check(param, 0, false);
5764 if (param->fp_nlink) {
5765 decision = find_value_cmp(lmd->lmd_stx.stx_nlink,
5766 param->fp_nlink, param->fp_nlink_sign,
5767 param->fp_exclude_nlink, 1, 0);
5772 if (param->fp_check_size) {
5773 decision = find_value_cmp(lmd->lmd_stx.stx_size,
5775 param->fp_size_sign,
5776 param->fp_exclude_size,
5777 param->fp_size_units, 0);
5782 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
5783 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
5785 param->fp_blocks_sign,
5786 param->fp_exclude_blocks,
5787 param->fp_blocks_units, 0);
5793 if (param->fp_format_printf_str)
5794 printf_format_string(param, path, projid, d);
5796 llapi_printf(LLAPI_MSG_NORMAL, "%s%c", path,
5797 param->fp_zero_end ? '\0' : '\n');
5801 /* Do not get down anymore? */
5802 if (param->fp_depth == param->fp_max_depth) {
5813 static int cb_migrate_mdt_init(char *path, int p, int *dp,
5814 void *param_data, struct dirent64 *de)
5816 struct find_param *param = (struct find_param *)param_data;
5817 struct lmv_user_md *lmu = param->fp_lmv_md;
5819 char raw[MAX_IOC_BUFLEN] = {'\0'};
5821 struct obd_ioctl_data data = { 0 };
5827 if (p == -1 && dp == NULL)
5833 if (dp != NULL && *dp != -1)
5837 tmp_p = open_parent(path);
5841 llapi_error(LLAPI_MSG_ERROR, ret,
5842 "can not open %s", path);
5847 path_copy = strdup(path);
5848 filename = basename(path_copy);
5850 data.ioc_inlbuf1 = (char *)filename;
5851 data.ioc_inllen1 = strlen(filename) + 1;
5852 data.ioc_inlbuf2 = (char *)lmu;
5853 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
5856 if (param->fp_depth == param->fp_max_depth)
5857 data.ioc_type = MDS_MIGRATE_NSONLY;
5858 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5860 llapi_error(LLAPI_MSG_ERROR, ret,
5861 "%s: error packing ioctl data", __func__);
5866 ret = ioctl(tmp_p, LL_IOC_MIGRATE, rawbuf);
5868 if (errno == EBUSY && !retry) {
5870 * because migrate may not be able to lock all involved
5871 * objects in order, for some of them it try lock, while
5872 * there may be conflicting COS locks and cause migrate
5873 * fail with EBUSY, hope a sync() could cause
5874 * transaction commit and release these COS locks.
5879 } else if (errno == EALREADY) {
5880 if (param->fp_verbose & VERBOSE_DETAIL)
5881 llapi_printf(LLAPI_MSG_NORMAL,
5882 "%s migrated to MDT%d already\n",
5883 path, lmu->lum_stripe_offset);
5887 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
5891 } else if (param->fp_verbose & VERBOSE_DETAIL) {
5892 llapi_printf(LLAPI_MSG_NORMAL,
5893 "migrate %s to MDT%d stripe count %d\n",
5894 path, lmu->lum_stripe_offset,
5895 lmu->lum_stripe_count);
5899 /* Do not get down anymore? */
5900 if (param->fp_depth == param->fp_max_depth)
5907 * If the directory is being migration, we need
5908 * close the directory after migration,
5909 * so the old directory cache will be cleanup
5910 * on the client side, and re-open to get the
5911 * new directory handle
5913 *dp = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
5916 llapi_error(LLAPI_MSG_ERROR, ret,
5917 "%s: Failed to open '%s'", __func__, path);
5929 /* dir migration finished, shrink its stripes */
5930 static int cb_migrate_mdt_fini(char *path, int p, int *dp, void *data,
5931 struct dirent64 *de)
5933 struct find_param *param = data;
5934 struct lmv_user_md *lmu = param->fp_lmv_md;
5935 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
5938 if (de && de->d_type != DT_DIR)
5943 * close it before setxattr because the latter may destroy the
5944 * original object, and cause close fail.
5952 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
5953 if (ret == -EALREADY)
5956 cb_common_fini(path, p, dp, data, de);
5960 int llapi_migrate_mdt(char *path, struct find_param *param)
5962 param->fp_stop_on_error = 1;
5963 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
5967 int llapi_mv(char *path, struct find_param *param)
5969 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
5970 static bool printed;
5973 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
5974 "%s() is deprecated, use llapi_migrate_mdt() instead",
5979 return llapi_migrate_mdt(path, param);
5983 * Check string for escape sequences and print a message to stdout
5984 * if any invalid escapes are found.
5986 * @param[in] c Pointer to character immediately following the
5987 * '\' character indicating the start of an escape
5989 * @return Number of characters examined in the escape sequence
5990 * (regardless of whether the sequence is valid or not).
5992 int validate_printf_esc(char *c)
5994 char *valid_esc = "nt\\";
5997 /* backslash at end of string */
5998 llapi_err_noerrno(LLAPI_MSG_WARN,
5999 "warning: '\\' at end of -printf format string\n");
6003 if (!strchr(valid_esc, *c))
6004 /* Invalid escape character */
6005 llapi_err_noerrno(LLAPI_MSG_WARN,
6006 "warning: unrecognized escape: '\\%c'\n", *c);
6012 * Check string for format directives and print a message to stdout
6013 * if any invalid directives are found.
6015 * @param[in] c Pointer to character immediately following the
6016 * '%' character indicating the start of a format
6018 * @return Number of characters examined in the format directive
6019 * (regardless of whether the directive is valid or not).
6021 int validate_printf_fmt(char *c)
6023 char *valid_fmt_single = "abcGkmnpstUwy%";
6024 char *valid_fmt_double = "ACTW";
6025 char *valid_fmt_lustre = "aAcFhioPpS";
6026 char curr = *c, next;
6029 llapi_err_noerrno(LLAPI_MSG_WARN,
6030 "warning: '%%' at end of -printf format string\n");
6035 if ((next == '\0') || (next == '%') || (next == '\\'))
6036 /* Treat as single char format directive */
6039 /* Check format directives with multiple characters */
6040 if (strchr(valid_fmt_double, curr)) {
6041 /* For now, only valid formats are followed by '@' char */
6043 llapi_err_noerrno(LLAPI_MSG_WARN,
6044 "warning: unrecognized format directive: '%%%c%c'\n",
6049 /* Lustre formats always start with 'L' */
6051 if (!strchr(valid_fmt_lustre, next))
6052 llapi_err_noerrno(LLAPI_MSG_WARN,
6053 "warning: unrecognized format directive: '%%%c%c'\n",
6060 if (!strchr(valid_fmt_single, curr))
6061 llapi_err_noerrno(LLAPI_MSG_WARN,
6062 "warning: unrecognized format directive: '%%%c'\n", curr);
6067 * Validate the user-supplied string for the -printf option and report
6068 * any invalid backslash escape sequences or format directives.
6070 * @param[in] param Structure containing info about invocation of lfs find
6073 void validate_printf_str(struct find_param *param)
6075 char *c = param->fp_format_printf_str;
6081 ret = validate_printf_fmt(++c);
6085 ret = validate_printf_esc(++c);
6095 int llapi_find(char *path, struct find_param *param)
6097 if (param->fp_format_printf_str)
6098 validate_printf_str(param);
6099 return param_callback(path, cb_find_init, cb_common_fini, param);
6103 * Get MDT number that the file/directory inode referenced
6104 * by the open fd resides on.
6105 * Return 0 and mdtidx on success, or -ve errno.
6107 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
6109 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
6114 static int cb_get_mdt_index(char *path, int p, int *dp, void *data,
6115 struct dirent64 *de)
6117 struct find_param *param = (struct find_param *)data;
6118 int d = dp == NULL ? -1 : *dp;
6121 bool hex = param->fp_hex_idx;
6123 if (p == -1 && d == -1)
6127 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6128 } else /* if (p != -1) */ {
6131 fd = open(path, O_RDONLY | O_NOCTTY);
6133 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
6141 if (ret == -ENODATA) {
6142 if (!param->fp_obd_uuid)
6143 llapi_printf(LLAPI_MSG_NORMAL,
6144 "'%s' has no stripe info\n", path);
6146 } else if (ret == -ENOENT) {
6147 llapi_error(LLAPI_MSG_WARN, ret,
6148 "warning: %s: '%s' does not exist",
6151 } else if (ret == -ENOTTY) {
6152 llapi_error(LLAPI_MSG_ERROR, ret,
6153 "%s: '%s' not on a Lustre fs",
6156 llapi_error(LLAPI_MSG_ERROR, ret,
6157 "error: %s: '%s' failed get_mdtidx",
6163 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
6164 llapi_printf(LLAPI_MSG_NORMAL, hex ? "%#x\n" : "%d\n", mdtidx);
6166 llapi_printf(LLAPI_MSG_NORMAL, hex ? "%s\nmdt_index:\t%#x\n"
6167 : "%s\nmdt_index:\t%d\n",
6171 /* Do not go down anymore? */
6172 if (param->fp_depth == param->fp_max_depth)
6180 static int cb_getstripe(char *path, int p, int *dp, void *data,
6181 struct dirent64 *de)
6183 struct find_param *param = (struct find_param *)data;
6184 int d = dp == NULL ? -1 : *dp, fd = -1;
6187 if (p == -1 && d == -1)
6190 if (param->fp_obd_uuid) {
6191 param->fp_quiet = 1;
6192 ret = setup_obd_uuid(d != -1 ? d : p, path, param);
6197 if (!param->fp_no_follow && de && de->d_type == DT_LNK && d == -1)
6198 d = fd = open(path, O_RDONLY | O_DIRECTORY);
6200 if (d != -1 && (param->fp_get_lmv || param->fp_get_default_lmv))
6201 ret = cb_get_dirstripe(path, &d, param);
6203 ret = get_lmd_info_fd(path, p, d, ¶m->fp_lmd->lmd_lmm,
6204 param->fp_lum_size, GET_LMD_STRIPE);
6205 else if (d == -1 && (param->fp_get_lmv || param->fp_get_default_lmv)) {
6206 /* in case of a dangling or valid faked symlink dir, opendir()
6207 * should have return either EINVAL or ENOENT, so let's try
6208 * to get LMV just in case, and by opening it as a file but
6209 * with O_NOFOLLOW ...
6211 int flag = O_RDONLY;
6213 if (param->fp_no_follow)
6214 flag = O_RDONLY | O_NOFOLLOW;
6215 fd = open(path, flag);
6219 ret = cb_get_dirstripe(path, &fd, param);
6221 llapi_lov_dump_user_lmm(param, path, LDF_IS_DIR);
6224 } else if (d == -1) {
6225 if (!param->fp_no_follow && de && de->d_type == DT_LNK) {
6226 /* open the target of symlink as a file */
6227 fd = open(path, O_RDONLY);
6231 ret = get_lmd_info_fd(path, p, fd, ¶m->fp_lmd->lmd_lmm,
6232 param->fp_lum_size, GET_LMD_STRIPE);
6240 if (errno == ENODATA && d != -1) {
6242 * We need to "fake" the "use the default" values
6243 * since the lmm struct is zeroed out at this point.
6244 * The magic needs to be set in order to satisfy
6245 * a check later on in the code path.
6246 * The object_seq needs to be set for the "(Default)"
6247 * prefix to be displayed.
6249 if (param->fp_get_default_lmv) {
6250 struct lmv_user_md *lum = param->fp_lmv_md;
6254 lum->lum_magic = LMV_USER_MAGIC;
6255 lum->lum_stripe_count = 0;
6256 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
6258 } else if (param->fp_get_lmv) {
6259 struct lmv_user_md *lum = param->fp_lmv_md;
6262 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6265 lum->lum_magic = LMV_MAGIC_V1;
6266 lum->lum_stripe_count = 0;
6267 lum->lum_stripe_offset = mdtidx;
6270 struct lov_user_md *lmm =
6271 ¶m->fp_lmd->lmd_lmm;
6273 lmm->lmm_magic = LOV_USER_MAGIC_V1;
6275 ostid_set_seq(&lmm->lmm_oi,
6276 FID_SEQ_LOV_DEFAULT);
6277 lmm->lmm_stripe_count = 0;
6278 lmm->lmm_stripe_size = 0;
6279 lmm->lmm_stripe_offset = -1;
6282 } else if (errno == ENODATA && p != -1) {
6283 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
6284 llapi_printf(LLAPI_MSG_NORMAL,
6285 "%s has no stripe info\n", path);
6287 } else if (errno == ENOENT) {
6288 llapi_error(LLAPI_MSG_WARN, -ENOENT,
6289 "warning: %s: %s does not exist",
6292 } else if (errno == ENOTTY) {
6294 llapi_error(LLAPI_MSG_ERROR, ret,
6295 "%s: '%s' not on a Lustre fs?",
6300 llapi_error(LLAPI_MSG_ERROR, ret,
6301 "error: %s: %s failed for %s",
6303 "LL_IOC_LOV_GETSTRIPE" :
6304 "IOC_MDC_GETFILESTRIPE", path);
6311 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
6312 llapi_lov_dump_user_lmm(param, path, d != -1 ? LDF_IS_DIR : 0);
6315 /* Do not get down anymore? */
6316 if (param->fp_depth == param->fp_max_depth)
6324 int llapi_getstripe(char *path, struct find_param *param)
6326 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
6327 cb_get_mdt_index : cb_getstripe,
6328 cb_common_fini, param);
6331 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
6332 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6334 char raw[MAX_IOC_BUFLEN] = {'\0'};
6336 struct obd_ioctl_data data = { 0 };
6339 data.ioc_inlbuf1 = (char *)&type;
6340 data.ioc_inllen1 = sizeof(__u32);
6341 data.ioc_inlbuf2 = (char *)&index;
6342 data.ioc_inllen2 = sizeof(__u32);
6343 data.ioc_pbuf1 = (char *)stat_buf;
6344 data.ioc_plen1 = sizeof(struct obd_statfs);
6345 data.ioc_pbuf2 = (char *)uuid_buf;
6346 data.ioc_plen2 = sizeof(struct obd_uuid);
6348 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
6350 llapi_error(LLAPI_MSG_ERROR, rc,
6351 "%s: error packing ioctl data", __func__);
6355 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
6357 return rc < 0 ? -errno : 0;
6360 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
6361 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6366 fd = open(path, O_RDONLY);
6369 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
6372 * If we can't even open a file on the filesystem (e.g. with
6373 * -ESHUTDOWN), force caller to exit or it will loop forever.
6378 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
6385 #define MAX_STRING_SIZE 128
6387 int llapi_ping(char *obd_type, char *obd_name)
6389 int flags = O_RDONLY;
6390 char buf[1] = { 0 };
6394 rc = cfs_get_param_paths(&path, "%s/%s/ping",
6395 obd_type, obd_name);
6399 fd = open(path.gl_pathv[0], flags);
6401 if (errno == EACCES && flags == O_RDONLY) {
6406 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
6411 if (flags == O_RDONLY)
6412 rc = read(fd, buf, sizeof(buf));
6414 rc = write(fd, buf, sizeof(buf));
6422 cfs_free_param_data(&path);
6426 int llapi_target_iterate(int type_num, char **obd_type,
6427 void *args, llapi_cb_t cb)
6433 for (i = 0; i < type_num; i++) {
6436 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
6440 for (j = 0; j < param.gl_pathc; j++) {
6441 char obd_uuid[UUID_MAX + 1];
6445 fp = fopen(param.gl_pathv[j], "r");
6448 llapi_error(LLAPI_MSG_ERROR, rc,
6449 "error: opening '%s'",
6454 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
6456 llapi_error(LLAPI_MSG_ERROR, rc,
6457 "error: reading '%s'",
6462 /* Extract the obd_name from the sysfs path.
6463 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
6465 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
6471 /* skip /fs/lustre/'obd_type'/ */
6472 obd_name += strlen(obd_type[i]) + 12;
6473 /* chop off after obd_name */
6474 ptr = strrchr(obd_name, '/');
6478 cb(obd_type[i], obd_name, obd_uuid, args);
6483 cfs_free_param_data(¶m);
6488 cfs_free_param_data(¶m);
6492 struct check_target_filter {
6497 static void do_target_check(char *obd_type_name, char *obd_name,
6498 char *obd_uuid, void *args)
6501 struct check_target_filter *filter = args;
6503 if (filter != NULL) {
6504 /* check nid if obd type is mgc */
6505 if (strcmp(obd_type_name, "mgc") == 0) {
6506 if (strcmp(obd_name + 3, filter->nid) != 0)
6509 /* check instance for other types of device (osc/mdc) */
6510 else if (strstr(obd_name, filter->instance) == NULL)
6514 rc = llapi_ping(obd_type_name, obd_name);
6516 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
6518 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
6520 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
6523 int llapi_target_check(int type_num, char **obd_type, char *dir)
6525 char nid[MAX_LINE_LEN], instance[MAX_INSTANCE_LEN];
6526 struct check_target_filter filter = {NULL, NULL};
6529 if (dir == NULL || dir[0] == '\0')
6530 return llapi_target_iterate(type_num, obd_type, NULL,
6533 rc = get_root_path(WANT_NID | WANT_ERROR, NULL, NULL, dir, -1, NULL,
6536 llapi_error(LLAPI_MSG_ERROR, rc,
6537 "cannot get nid of path '%s'", dir);
6542 rc = llapi_get_instance(dir, instance, ARRAY_SIZE(instance));
6545 filter.instance = instance;
6547 return llapi_target_iterate(type_num, obd_type, &filter,
6551 #undef MAX_STRING_SIZE
6553 /* Is this a lustre fs? */
6554 int llapi_is_lustre_mnttype(const char *type)
6556 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
6559 /* Is this a lustre client fs? */
6560 int llapi_is_lustre_mnt(struct mntent *mnt)
6562 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
6563 strstr(mnt->mnt_fsname, ":/") != NULL);
6566 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
6568 char fsname[PATH_MAX + 1];
6572 rc = llapi_search_fsname(mnt, fsname);
6576 root = open(mnt, O_RDONLY | O_DIRECTORY);
6579 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
6583 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
6586 if (rc == -ENOENT && LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd))
6587 llapi_error(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, rc,
6588 "Cannot find pool '%s'", qctl->qc_poolname);
6594 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
6599 root = open(mnt, O_RDONLY | O_DIRECTORY);
6602 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
6606 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
6609 llapi_error(LLAPI_MSG_ERROR, rc,
6610 "ioctl on %s for getting connect flags failed", mnt);
6617 * Flush cached pages from all clients.
6619 * \param fd File descriptor
6623 int llapi_file_flush(int fd)
6627 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);