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>
75 #include <libcfs/util/ioctl.h>
76 #include <libcfs/util/param.h>
77 #include <libcfs/util/string.h>
78 #include <linux/lnet/lnetctl.h>
79 #include <lustre/lustreapi.h>
80 #include <linux/lustre/lustre_ostid.h>
81 #include <linux/lustre/lustre_ioctl.h>
82 #include "lustreapi_internal.h"
85 #define FORMATTED_BUF_LEN 1024
87 static int llapi_msg_level = LLAPI_MSG_MAX;
88 const char *liblustreapi_cmd;
90 struct lustre_foreign_type lu_foreign_types[] = {
91 {.lft_type = LU_FOREIGN_TYPE_NONE, .lft_name = "none"},
92 {.lft_type = LU_FOREIGN_TYPE_SYMLINK, .lft_name = "symlink"},
93 /* must be the last element */
94 {.lft_type = LU_FOREIGN_TYPE_UNKNOWN, .lft_name = NULL}
95 /* array max dimension must be <= UINT32_MAX */
98 void llapi_msg_set_level(int level)
100 /* ensure level is in the good range */
101 if (level < LLAPI_MSG_OFF)
102 llapi_msg_level = LLAPI_MSG_OFF;
103 else if (level > LLAPI_MSG_MAX)
104 llapi_msg_level = LLAPI_MSG_MAX;
106 llapi_msg_level = level;
109 int llapi_msg_get_level(void)
111 return llapi_msg_level;
114 void llapi_set_command_name(const char *cmd)
116 liblustreapi_cmd = cmd;
119 void llapi_clear_command_name(void)
121 liblustreapi_cmd = NULL;
124 static void error_callback_default(enum llapi_message_level level, int err,
125 const char *fmt, va_list ap)
127 bool has_nl = strchr(fmt, '\n') != NULL;
129 if (liblustreapi_cmd != NULL)
130 fprintf(stderr, "%s %s: ", program_invocation_short_name,
133 fprintf(stderr, "%s: ", program_invocation_short_name);
136 if (level & LLAPI_MSG_NO_ERRNO) {
137 vfprintf(stderr, fmt, ap);
139 fprintf(stderr, "\n");
144 * Remove trailing linefeed so error string can be appended.
145 * @fmt is a const string, so we can't modify it directly.
147 if (has_nl && (newfmt = strdup(fmt)))
148 *strrchr(newfmt, '\n') = '\0';
150 newfmt = (char *)fmt;
152 vfprintf(stderr, newfmt, ap);
155 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
159 static void info_callback_default(enum llapi_message_level level, int err,
160 const char *fmt, va_list ap)
163 if (liblustreapi_cmd != NULL) {
164 fprintf(stdout, "%s %s: ",
165 program_invocation_short_name,
168 fprintf(stdout, "%s: ", program_invocation_short_name);
171 vfprintf(stdout, fmt, ap);
174 static llapi_log_callback_t llapi_error_callback = error_callback_default;
175 static llapi_log_callback_t llapi_info_callback = info_callback_default;
178 /* llapi_error will preserve errno */
179 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
182 int tmp_errno = errno;
184 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
188 llapi_error_callback(level, abs(err), fmt, args);
193 /* llapi_printf will preserve errno */
194 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
197 int tmp_errno = errno;
199 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
203 llapi_info_callback(level, 0, fmt, args);
209 * Set a custom error logging function. Passing in NULL will reset the logging
210 * callback to its default value.
212 * This function returns the value of the old callback.
214 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
216 llapi_log_callback_t old = llapi_error_callback;
219 llapi_error_callback = cb;
221 llapi_error_callback = error_callback_default;
227 * Set a custom info logging function. Passing in NULL will reset the logging
228 * callback to its default value.
230 * This function returns the value of the old callback.
232 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
234 llapi_log_callback_t old = llapi_info_callback;
237 llapi_info_callback = cb;
239 llapi_info_callback = info_callback_default;
245 * size_units is to be initialized (or zeroed) by caller.
247 int llapi_parse_size(const char *optarg, unsigned long long *size,
248 unsigned long long *size_units, int bytes_spec)
251 char *argbuf = (char *)optarg;
252 unsigned long long frac = 0, frac_d = 1;
254 if (strncmp(optarg, "-", 1) == 0)
257 if (*size_units == 0)
260 *size = strtoull(argbuf, &end, 0);
261 if (end != NULL && *end == '.') {
265 frac = strtoull(argbuf, &end, 10);
266 /* count decimal places */
267 for (i = 0; i < (end - argbuf); i++)
272 if ((*end == 'b') && *(end + 1) == '\0' &&
273 (*size & (~0ULL << (64 - 9))) == 0 &&
275 *size_units = 1 << 9;
276 } else if ((*end == 'b') &&
277 *(end + 1) == '\0' &&
280 } else if ((*end == 'k' || *end == 'K') &&
281 *(end + 1) == '\0' &&
282 (*size & (~0ULL << (64 - 10))) == 0) {
283 *size_units = 1 << 10;
284 } else if ((*end == 'm' || *end == 'M') &&
285 *(end + 1) == '\0' &&
286 (*size & (~0ULL << (64 - 20))) == 0) {
287 *size_units = 1 << 20;
288 } else if ((*end == 'g' || *end == 'G') &&
289 *(end + 1) == '\0' &&
290 (*size & (~0ULL << (64 - 30))) == 0) {
291 *size_units = 1 << 30;
292 } else if ((*end == 't' || *end == 'T') &&
293 *(end + 1) == '\0' &&
294 (*size & (~0ULL << (64 - 40))) == 0) {
295 *size_units = 1ULL << 40;
296 } else if ((*end == 'p' || *end == 'P') &&
297 *(end + 1) == '\0' &&
298 (*size & (~0ULL << (64 - 50))) == 0) {
299 *size_units = 1ULL << 50;
300 } else if ((*end == 'e' || *end == 'E') &&
301 *(end + 1) == '\0' &&
302 (*size & (~0ULL << (64 - 60))) == 0) {
303 *size_units = 1ULL << 60;
308 *size = *size * *size_units + frac * *size_units / frac_d;
313 int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
315 struct obd_ioctl_data *overlay;
318 data->ioc_len = obd_ioctl_packlen(data);
319 data->ioc_version = OBD_IOCTL_VERSION;
321 if (*pbuf != NULL && data->ioc_len > max_len) {
322 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
323 "pbuf = %p, ioc_len = %u, max_len = %d",
324 *pbuf, data->ioc_len, max_len);
329 *pbuf = malloc(data->ioc_len);
334 overlay = (struct obd_ioctl_data *)*pbuf;
335 memcpy(*pbuf, data, sizeof(*data));
337 ptr = overlay->ioc_bulk;
338 if (data->ioc_inlbuf1) {
339 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
340 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
343 if (data->ioc_inlbuf2) {
344 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
345 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
348 if (data->ioc_inlbuf3) {
349 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
350 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
353 if (data->ioc_inlbuf4) {
354 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
355 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
361 int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
363 struct obd_ioctl_data *overlay;
369 overlay = (struct obd_ioctl_data *)pbuf;
371 /* Preserve the caller's buffer pointers */
372 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
373 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
374 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
375 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
377 memcpy(data, pbuf, sizeof(*data));
379 ptr = overlay->ioc_bulk;
380 if (data->ioc_inlbuf1) {
381 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
382 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
385 if (data->ioc_inlbuf2) {
386 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
387 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
390 if (data->ioc_inlbuf3) {
391 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
392 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
395 if (data->ioc_inlbuf4) {
396 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
397 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
403 /* XXX: llapi_xxx() functions return negative values upon failure */
405 int llapi_layout_search_ost(__u32 ost, char *pname, char *fsname)
407 char ostname[MAX_OBD_NAME + 64];
408 char *pool_name = pname;
412 * The current policy is that the pool does not have to exist at the
413 * setstripe time, see sanity-pfl/-flr tests.
414 * If this logic will change, re-enable it.
416 * if (pname && strlen(pname) == 0)
420 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
422 rc = llapi_search_ost(fsname, pool_name, ostname);
427 llapi_error(LLAPI_MSG_ERROR, rc,
428 "%s: cannot find OST %s in %s", __func__, ostname,
429 pool_name != NULL ? "pool" : "system");
437 * Verify the setstripe parameters before using.
438 * This is a pair method for comp_args_to_layout()/llapi_layout_sanity_cb()
439 * when just 1 component or a non-PFL layout is given.
441 * \param[in] param stripe parameters
442 * \param[in] pool_name pool name
443 * \param[in] fsname lustre FS name
446 * < 0, error code on failre
448 static int llapi_stripe_param_verify(const struct llapi_stripe_param *param,
453 static int page_size;
456 if (page_size == 0) {
458 * 64 KB is the largest common page size (on ia64/PPC/ARM),
459 * but check the local page size just in case. The page_size
460 * will not change for the lifetime of this process at least.
462 page_size = LOV_MIN_STRIPE_SIZE;
463 if (getpagesize() > page_size) {
464 page_size = getpagesize();
465 llapi_err_noerrno(LLAPI_MSG_WARN,
466 "warning: page size (%u) larger than expected (%u)",
467 page_size, LOV_MIN_STRIPE_SIZE);
470 if (!llapi_stripe_size_is_aligned(param->lsp_stripe_size)) {
472 llapi_error(LLAPI_MSG_ERROR, rc,
473 "error: bad stripe_size %llu, must be an even multiple of %d bytes",
474 param->lsp_stripe_size, page_size);
477 if (!llapi_stripe_index_is_valid(param->lsp_stripe_offset)) {
479 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
480 param->lsp_stripe_offset);
483 if (llapi_stripe_size_is_too_big(param->lsp_stripe_size)) {
485 llapi_error(LLAPI_MSG_ERROR, rc,
486 "error: stripe size '%llu' over 4GB limit",
487 param->lsp_stripe_size);
491 count = param->lsp_stripe_count;
492 if (param->lsp_stripe_pattern == LOV_PATTERN_MDT) {
494 llapi_error(LLAPI_MSG_ERROR, rc,
495 "Invalid pattern: %d, must be specified with -E\n",
496 param->lsp_stripe_pattern);
499 if (!llapi_stripe_count_is_valid(count)) {
501 llapi_error(LLAPI_MSG_ERROR, rc,
502 "Invalid stripe count %d\n", count);
507 /* Make sure we have a good pool */
508 if (*pool_name != NULL) {
509 if (!llapi_pool_name_is_valid(pool_name, fsname)) {
511 llapi_error(LLAPI_MSG_ERROR, rc,
512 "Pool '%s' is not on filesystem '%s'",
517 /* Make sure the pool exists and is non-empty */
518 rc = llapi_search_ost(fsname, *pool_name, NULL);
520 char *err = rc == 0 ? "has no OSTs" : "does not exist";
523 llapi_error(LLAPI_MSG_ERROR, rc, "pool '%s.%s' %s",
524 fsname, *pool_name, err);
530 /* sanity check of target list */
531 if (param->lsp_is_specific) {
535 for (i = 0; i < count; i++) {
536 rc = llapi_layout_search_ost(param->lsp_osts[i],
541 /* Make sure stripe offset is in OST list. */
542 if (param->lsp_osts[i] == param->lsp_stripe_offset)
547 llapi_error(LLAPI_MSG_ERROR, rc,
548 "%s: stripe offset '%d' is not in the target list",
549 __func__, param->lsp_stripe_offset);
552 } else if (param->lsp_stripe_offset != -1) {
553 rc = llapi_layout_search_ost(param->lsp_stripe_offset,
563 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
568 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
570 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
574 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
576 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
581 if (!llapi_dir_hash_type_is_valid(hash_type)) {
583 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
591 * Trim a trailing newline from a string, if it exists.
593 int llapi_chomp_string(char *buf)
609 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
611 static int get_param_lov(const char *path, const char *param,
612 char *buf, size_t buf_size)
614 struct obd_uuid uuid;
617 rc = llapi_file_get_lov_uuid(path, &uuid);
621 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
626 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
628 static int get_param_lmv(const char *path, const char *param,
629 char *buf, size_t buf_size)
631 struct obd_uuid uuid;
634 rc = llapi_file_get_lmv_uuid(path, &uuid);
638 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
642 static int get_mds_md_size(const char *path)
644 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
647 * Rather than open the file and do the ioctl to get the
648 * instance name and close the file and search for the param
649 * file and open the param file and read the param file and
650 * parse the value and close the param file, let's just return
651 * a large enough value. It's 2020, RAM is cheap and this is
655 if (md_size < XATTR_SIZE_MAX)
656 md_size = XATTR_SIZE_MAX;
661 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
663 return get_param_lmv(path, "uuid", buf, bufsize);
667 * Open a Lustre file.
669 * \param name the name of the file to be opened
670 * \param flags access mode, see flags in open(2)
671 * \param mode permission of the file if it is created, see mode in open(2)
672 * \param param stripe pattern of the newly created file
674 * \retval file descriptor of opened file
675 * \retval negative errno on failure
677 int llapi_file_open_param(const char *name, int flags, mode_t mode,
678 const struct llapi_stripe_param *param)
680 char fsname[MAX_OBD_NAME + 1] = { 0 };
681 struct lov_user_md *lum = NULL;
682 char *pool_name = param->lsp_pool;
686 /* Make sure we are on a Lustre file system */
687 rc = llapi_search_fsname(name, fsname);
689 llapi_error(LLAPI_MSG_ERROR, rc,
690 "'%s' is not on a Lustre filesystem",
695 /* Check if the stripe pattern is sane. */
696 rc = llapi_stripe_param_verify(param, &pool_name, fsname);
700 if (param->lsp_is_specific)
701 lum_size = lov_user_md_size(param->lsp_stripe_count,
702 LOV_USER_MAGIC_SPECIFIC);
704 lum_size = sizeof(struct lov_user_md_v3);
706 lum_size = sizeof(*lum);
708 lum = calloc(1, lum_size);
713 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
715 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
716 flags = O_DIRECTORY | O_RDONLY;
723 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
728 /* Initialize IOCTL striping pattern structure */
729 lum->lmm_magic = LOV_USER_MAGIC_V1;
730 lum->lmm_pattern = param->lsp_stripe_pattern;
731 lum->lmm_stripe_size = param->lsp_stripe_size;
732 lum->lmm_stripe_count = param->lsp_stripe_count;
733 lum->lmm_stripe_offset = param->lsp_stripe_offset;
734 if (pool_name != NULL) {
735 struct lov_user_md_v3 *lumv3 = (void *)lum;
737 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
738 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
740 if (param->lsp_is_specific) {
741 struct lov_user_md_v3 *lumv3 = (void *)lum;
744 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
745 if (pool_name == NULL) {
747 * LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
748 * OST list, therefore if pool is not specified we have
749 * to pack a null pool name for placeholder.
751 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
754 for (i = 0; i < param->lsp_stripe_count; i++)
755 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
758 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
759 char errmsg[512] = "stripe already set";
762 if (errno != EEXIST && errno != EALREADY)
763 strncpy(errmsg, strerror(errno), sizeof(errmsg) - 1);
764 if (rc == -EREMOTEIO)
765 snprintf(errmsg, sizeof(errmsg),
766 "inactive OST among your specified %d OST(s)",
767 param->lsp_stripe_count);
769 llapi_err_noerrno(LLAPI_MSG_ERROR,
770 "setstripe error for '%s': %s", name, errmsg);
781 int llapi_file_is_encrypted(int fd)
786 rc = ioctl(fd, FS_IOC_GETFLAGS, &flags);
790 return !!(flags & LUSTRE_ENCRYPT_FL);
793 int llapi_file_open_pool(const char *name, int flags, int mode,
794 unsigned long long stripe_size, int stripe_offset,
795 int stripe_count, int stripe_pattern, char *pool_name)
797 const struct llapi_stripe_param param = {
798 .lsp_stripe_size = stripe_size,
799 .lsp_stripe_count = stripe_count,
800 .lsp_stripe_pattern = stripe_pattern,
801 .lsp_stripe_offset = stripe_offset,
802 .lsp_pool = pool_name
804 return llapi_file_open_param(name, flags, mode, ¶m);
807 int llapi_file_open(const char *name, int flags, int mode,
808 unsigned long long stripe_size, int stripe_offset,
809 int stripe_count, int stripe_pattern)
811 return llapi_file_open_pool(name, flags, mode, stripe_size,
812 stripe_offset, stripe_count,
813 stripe_pattern, NULL);
816 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
817 __u32 flags, char *foreign_lov)
820 struct lov_foreign_md *lfm;
823 if (foreign_lov == NULL) {
825 llapi_error(LLAPI_MSG_ERROR, rc,
826 "foreign LOV EA content must be provided");
830 len = strlen(foreign_lov);
831 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
834 llapi_error(LLAPI_MSG_ERROR, rc,
835 "foreign LOV EA size %zu (must be 0 < len < %zu)",
836 len, XATTR_SIZE_MAX -
837 offsetof(struct lov_foreign_md, lfm_value));
841 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
844 llapi_error(LLAPI_MSG_ERROR, rc,
845 "failed to allocate lov_foreign_md");
849 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
852 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
856 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
857 lfm->lfm_length = len;
858 lfm->lfm_type = type;
859 lfm->lfm_flags = flags;
860 memcpy(lfm->lfm_value, foreign_lov, len);
862 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
863 char *errmsg = "stripe already set";
867 errmsg = "not on a Lustre filesystem";
868 else if (errno == EEXIST || errno == EALREADY)
869 errmsg = "stripe already set";
871 errmsg = strerror(errno);
873 llapi_err_noerrno(LLAPI_MSG_ERROR,
874 "setstripe error for '%s': %s", name, errmsg);
890 int llapi_file_create(const char *name, unsigned long long stripe_size,
891 int stripe_offset, int stripe_count, int stripe_pattern)
895 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
896 stripe_offset, stripe_count, stripe_pattern,
905 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
906 int stripe_offset, int stripe_count,
907 int stripe_pattern, char *pool_name)
911 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
912 stripe_offset, stripe_count, stripe_pattern,
921 static int verify_dir_param(const char *name,
922 const struct llapi_stripe_param *param)
924 char fsname[MAX_OBD_NAME + 1] = { 0 };
925 char *pool_name = param->lsp_pool;
928 /* Make sure we are on a Lustre file system */
929 rc = llapi_search_fsname(name, fsname);
931 llapi_error(LLAPI_MSG_ERROR, rc,
932 "'%s' is not on a Lustre filesystem",
937 /* Check if the stripe pattern is sane. */
938 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
939 param->lsp_stripe_count,
940 param->lsp_stripe_pattern);
944 /* Make sure we have a good pool */
945 if (pool_name != NULL) {
947 * in case user gives the full pool name <fsname>.<poolname>,
950 char *ptr = strchr(pool_name, '.');
954 if (strcmp(pool_name, fsname) != 0) {
956 llapi_err_noerrno(LLAPI_MSG_ERROR,
957 "Pool '%s' is not on filesystem '%s'",
964 /* Make sure the pool exists and is non-empty */
965 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
967 char *err = rc == 0 ? "has no OSTs" : "does not exist";
969 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
970 fsname, pool_name, err);
975 /* sanity check of target list */
976 if (param->lsp_is_specific) {
977 char mdtname[MAX_OBD_NAME + 64];
981 for (i = 0; i < param->lsp_stripe_count; i++) {
982 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
983 fsname, param->lsp_tgts[i]);
984 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
989 llapi_error(LLAPI_MSG_ERROR, rc,
990 "%s: cannot find MDT %s in %s",
997 /* Make sure stripe offset is in MDT list. */
998 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
1002 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1003 "%s: stripe offset '%d' is not in the target list",
1004 __func__, param->lsp_stripe_offset);
1012 static inline void param2lmu(struct lmv_user_md *lmu,
1013 const struct llapi_stripe_param *param)
1015 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
1017 lmu->lum_stripe_count = param->lsp_stripe_count;
1018 lmu->lum_stripe_offset = param->lsp_stripe_offset;
1019 lmu->lum_hash_type = param->lsp_stripe_pattern;
1020 lmu->lum_max_inherit = param->lsp_max_inherit;
1021 lmu->lum_max_inherit_rr = param->lsp_max_inherit_rr;
1022 if (param->lsp_pool != NULL)
1023 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
1024 if (param->lsp_is_specific) {
1027 for (i = 0; i < param->lsp_stripe_count; i++)
1028 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
1032 int llapi_dir_set_default_lmv(const char *name,
1033 const struct llapi_stripe_param *param)
1035 struct lmv_user_md lmu = { 0 };
1039 rc = verify_dir_param(name, param);
1043 /* TODO: default lmv doesn't support specific targets yet */
1044 if (param->lsp_is_specific)
1047 param2lmu(&lmu, param);
1049 fd = open(name, O_DIRECTORY | O_RDONLY);
1052 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1056 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
1058 char *errmsg = "stripe already set";
1061 if (errno != EEXIST && errno != EALREADY)
1062 errmsg = strerror(errno);
1064 llapi_err_noerrno(LLAPI_MSG_ERROR,
1065 "default dirstripe error on '%s': %s",
1072 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
1073 int stripe_count, int stripe_pattern,
1074 const char *pool_name)
1076 const struct llapi_stripe_param param = {
1077 .lsp_stripe_count = stripe_count,
1078 .lsp_stripe_offset = stripe_offset,
1079 .lsp_stripe_pattern = stripe_pattern,
1080 .lsp_pool = (char *)pool_name
1083 return llapi_dir_set_default_lmv(name, ¶m);
1087 * Create a Lustre directory.
1089 * \param name the name of the directory to be created
1090 * \param mode permission of the file if it is created, see mode in open(2)
1091 * \param param stripe pattern of the newly created directory
1093 * \retval 0 on success
1094 * \retval negative errno on failure
1096 int llapi_dir_create(const char *name, mode_t mode,
1097 const struct llapi_stripe_param *param)
1099 struct lmv_user_md *lmu = NULL;
1101 struct obd_ioctl_data data = { 0 };
1104 char *dirpath = NULL;
1105 char *namepath = NULL;
1110 rc = verify_dir_param(name, param);
1114 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1115 param->lsp_is_specific ?
1116 LMV_USER_MAGIC_SPECIFIC :
1119 lmu = calloc(1, lmu_size);
1123 dirpath = strdup(name);
1129 namepath = strdup(name);
1136 param2lmu(lmu, param);
1138 filename = basename(namepath);
1139 dir = dirname(dirpath);
1141 data.ioc_inlbuf1 = (char *)filename;
1142 data.ioc_inllen1 = strlen(filename) + 1;
1143 data.ioc_inlbuf2 = (char *)lmu;
1144 data.ioc_inllen2 = lmu_size;
1145 data.ioc_type = mode;
1146 if (param->lsp_is_create)
1147 /* borrow obdo1.o_flags to store this flag */
1148 data.ioc_obdo1.o_flags = OBD_FL_OBDMDEXISTS;
1149 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1151 llapi_error(LLAPI_MSG_ERROR, rc,
1152 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1157 fd = open(dir, O_DIRECTORY | O_RDONLY);
1160 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1164 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1165 char *errmsg = "stripe already set";
1168 if (errno != EEXIST && errno != EALREADY)
1169 errmsg = strerror(errno);
1171 llapi_err_noerrno(LLAPI_MSG_ERROR,
1172 "dirstripe error on '%s': %s", name, errmsg);
1183 * Create a foreign directory.
1185 * \param name the name of the directory to be created
1186 * \param mode permission of the file if it is created, see mode in open(2)
1187 * \param type foreign type to be set in LMV EA
1188 * \param flags foreign flags to be set in LMV EA
1189 * \param value foreign pattern to be set in LMV EA
1191 * \retval 0 on success
1192 * \retval negative errno on failure
1194 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1195 __u32 flags, const char *value)
1197 struct lmv_foreign_md *lfm = NULL;
1198 size_t lfm_size, len;
1199 struct obd_ioctl_data data = { 0 };
1202 char *dirpath = NULL;
1203 char *namepath = NULL;
1208 len = strlen(value);
1209 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1212 llapi_error(LLAPI_MSG_ERROR, rc,
1213 "invalid LOV EA length %zu (must be 0 < len < %zu)",
1214 len, XATTR_SIZE_MAX -
1215 offsetof(struct lmv_foreign_md, lfm_value));
1218 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1219 lfm = calloc(1, lfm_size);
1223 dirpath = strdup(name);
1229 namepath = strdup(name);
1236 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1237 lfm->lfm_length = len;
1238 lfm->lfm_type = type;
1239 lfm->lfm_flags = flags;
1240 memcpy(lfm->lfm_value, value, len);
1242 filename = basename(namepath);
1243 dir = dirname(dirpath);
1245 data.ioc_inlbuf1 = (char *)filename;
1246 data.ioc_inllen1 = strlen(filename) + 1;
1247 data.ioc_inlbuf2 = (char *)lfm;
1248 data.ioc_inllen2 = lfm_size;
1249 data.ioc_type = mode;
1250 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1252 llapi_error(LLAPI_MSG_ERROR, rc,
1253 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1258 fd = open(dir, O_DIRECTORY | O_RDONLY);
1261 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1265 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1266 char *errmsg = "stripe already set";
1269 if (errno != EEXIST && errno != EALREADY)
1270 errmsg = strerror(errno);
1272 llapi_err_noerrno(LLAPI_MSG_ERROR,
1273 "dirstripe error on '%s': %s", name, errmsg);
1283 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1284 int stripe_count, int stripe_pattern,
1285 const char *pool_name)
1287 const struct llapi_stripe_param param = {
1288 .lsp_stripe_count = stripe_count,
1289 .lsp_stripe_offset = stripe_offset,
1290 .lsp_stripe_pattern = stripe_pattern,
1291 .lsp_pool = (char *)pool_name
1294 return llapi_dir_create(name, mode, ¶m);
1298 * Find the fsname, the full path, and/or an open fd.
1299 * Either the fsname or path must not be NULL
1301 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1304 char buf[PATH_MAX], mntdir[PATH_MAX];
1305 char *ptr, *ptr_end;
1307 int idx = 0, mntlen = 0, fd;
1309 int fsnamelen, mountlen;
1311 /* get the mount point */
1312 fp = setmntent(PROC_MOUNTS, "r");
1315 llapi_error(LLAPI_MSG_ERROR, rc,
1316 "cannot retrieve filesystem mount point");
1320 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1323 if (!llapi_is_lustre_mnt(&mnt))
1326 if ((want & WANT_INDEX) && (idx++ != index))
1329 mntlen = strlen(mnt.mnt_dir);
1330 ptr = strchr(mnt.mnt_fsname, '/');
1331 while (ptr && *ptr == '/')
1334 * thanks to the call to llapi_is_lustre_mnt() above,
1335 * we are sure that mnt.mnt_fsname contains ":/",
1336 * so ptr should never be NULL
1341 while (*ptr_end != '/' && *ptr_end != '\0')
1344 /* Check the fsname for a match, if given */
1345 mountlen = ptr_end - ptr;
1346 if (!(want & WANT_FSNAME) && fsname != NULL &&
1347 (fsnamelen = strlen(fsname)) > 0 &&
1348 (fsnamelen != mountlen ||
1349 (strncmp(ptr, fsname, mountlen) != 0)))
1352 /* If the path isn't set return the first one we find */
1353 if (path == NULL || strlen(path) == 0) {
1354 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1355 mntdir[sizeof(mntdir) - 1] = '\0';
1356 if ((want & WANT_FSNAME) && fsname != NULL) {
1357 strncpy(fsname, ptr, mountlen);
1358 fsname[mountlen] = '\0';
1362 /* Otherwise find the longest matching path */
1363 } else if ((strlen(path) >= mntlen) &&
1364 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1365 /* check the path format */
1366 if (strlen(path) > mntlen && path[mntlen] != '/')
1368 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1369 mntdir[sizeof(mntdir) - 1] = '\0';
1370 if ((want & WANT_FSNAME) && fsname != NULL) {
1371 strncpy(fsname, ptr, mountlen);
1372 fsname[mountlen] = '\0';
1382 if ((want & WANT_PATH) && path != NULL) {
1383 strncpy(path, mntdir, mntlen);
1384 path[mntlen] = '\0';
1386 if (want & WANT_FD) {
1387 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1390 llapi_error(LLAPI_MSG_ERROR, rc,
1391 "cannot open '%s'", mntdir);
1397 } else if (want & WANT_ERROR)
1398 llapi_err_noerrno(LLAPI_MSG_ERROR,
1399 "'%s' not on a mounted Lustre filesystem",
1400 (want & WANT_PATH) ? fsname : path);
1405 * search lustre mounts
1407 * Calling this function will return to the user the mount point, mntdir, and
1408 * the file system name, fsname, if the user passed a buffer to this routine.
1410 * The user inputs are pathname and index. If the pathname is supplied then
1411 * the value of the index will be ignored. The pathname will return data if
1412 * the pathname is located on a lustre mount. Index is used to pick which
1413 * mount point you want in the case of multiple mounted lustre file systems.
1414 * See function lfs_osts in lfs.c for an example of the index use.
1416 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1419 int want = WANT_PATH, idx = -1;
1421 if (!pathname || pathname[0] == '\0') {
1425 strcpy(mntdir, pathname);
1429 want |= WANT_FSNAME;
1430 return get_root_path(want, fsname, NULL, mntdir, idx);
1433 /* Given a path, find the corresponding Lustre fsname */
1434 int llapi_search_fsname(const char *pathname, char *fsname)
1439 path = realpath(pathname, NULL);
1441 char tmp[PATH_MAX - 1];
1447 if (pathname[0] != '/') {
1449 * Need an absolute path, but realpath() only works for
1450 * pathnames that actually exist. We go through the
1451 * extra hurdle of dirname(getcwd() + pathname) in
1452 * case the relative pathname contains ".." in it.
1454 char realpath[PATH_MAX - 1];
1456 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1458 llapi_error(LLAPI_MSG_ERROR, rc,
1459 "cannot get current working directory");
1463 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1464 if (rc >= sizeof(tmp)) {
1466 llapi_error(LLAPI_MSG_ERROR, rc,
1467 "invalid parent path '%s'",
1473 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1474 if (rc >= sizeof(buf)) {
1476 llapi_error(LLAPI_MSG_ERROR, rc,
1477 "invalid path '%s'", pathname);
1480 path = realpath(buf, NULL);
1482 ptr = strrchr(buf, '/');
1484 llapi_error(LLAPI_MSG_ERROR |
1485 LLAPI_MSG_NO_ERRNO, 0,
1486 "cannot resolve path '%s'",
1491 path = realpath(buf, NULL);
1494 llapi_error(LLAPI_MSG_ERROR, rc,
1495 "cannot resolve path '%s'",
1501 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1506 int llapi_search_rootpath(char *pathname, const char *fsname)
1509 * pathname can be used as an argument by get_root_path(),
1510 * clear it for safety
1513 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1517 * Get the list of pool members.
1518 * \param poolname string of format \<fsname\>.\<poolname\>
1519 * \param members caller-allocated array of char*
1520 * \param list_size size of the members array
1521 * \param buffer caller-allocated buffer for storing OST names
1522 * \param buffer_size size of the buffer
1524 * \return number of members retrieved for this pool
1525 * \retval -error failure
1527 int llapi_get_poolmembers(const char *poolname, char **members,
1528 int list_size, char *buffer, int buffer_size)
1530 char fsname[PATH_MAX];
1539 /* name is FSNAME.POOLNAME */
1540 if (strlen(poolname) >= sizeof(fsname))
1543 snprintf(fsname, sizeof(fsname), "%s", poolname);
1544 pool = strchr(fsname, '.');
1551 rc = poolpath(&pathname, fsname, NULL);
1553 llapi_error(LLAPI_MSG_ERROR, rc,
1554 "Lustre filesystem '%s' not found",
1559 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1560 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1561 cfs_free_param_data(&pathname);
1562 if (rc >= sizeof(buf))
1564 fd = fopen(buf, "r");
1567 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1572 while (fgets(buf, sizeof(buf), fd) != NULL) {
1573 if (nb_entries >= list_size) {
1577 buf[sizeof(buf) - 1] = '\0';
1579 tmp = strchr(buf, '\n');
1582 if (used + strlen(buf) + 1 > buffer_size) {
1587 strcpy(buffer + used, buf);
1588 members[nb_entries] = buffer + used;
1589 used += strlen(buf) + 1;
1599 * Get the list of pools in a filesystem.
1600 * \param name filesystem name or path
1601 * \param poollist caller-allocated array of char*
1602 * \param list_size size of the poollist array
1603 * \param buffer caller-allocated buffer for storing pool names
1604 * \param buffer_size size of the buffer
1606 * \return number of pools retrieved for this filesystem
1607 * \retval -error failure
1609 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1610 char *buffer, int buffer_size)
1616 struct dirent *pool;
1618 unsigned int nb_entries = 0;
1619 unsigned int used = 0;
1622 /* initialize output array */
1623 for (i = 0; i < list_size; i++)
1626 /* is name a pathname ? */
1627 ptr = strchr(name, '/');
1629 char fsname_buf[MAXNAMLEN];
1631 /* We will need fsname for printing later */
1632 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1636 ptr = strrchr(fsname_buf, '-');
1640 fsname = strdup(fsname_buf);
1644 /* name is FSNAME */
1645 fsname = strdup(name);
1650 rc = poolpath(&pathname, fsname, NULL);
1652 llapi_error(LLAPI_MSG_ERROR, rc,
1653 "Lustre filesystem '%s' not found", name);
1657 dir = opendir(pathname.gl_pathv[0]);
1660 llapi_error(LLAPI_MSG_ERROR, rc,
1661 "Could not open pool list for '%s'",
1668 pool = readdir(dir);
1674 /* ignore . and .. */
1675 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1678 /* check output bounds */
1679 if (nb_entries >= list_size) {
1681 goto free_dir_no_msg;
1684 /* +2 for '.' and final '\0' */
1685 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1688 goto free_dir_no_msg;
1691 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1692 poollist[nb_entries] = buffer + used;
1693 used += strlen(pool->d_name) + strlen(fsname) + 2;
1699 llapi_error(LLAPI_MSG_ERROR, rc,
1700 "Error reading pool list for '%s'", name);
1702 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1707 cfs_free_param_data(&pathname);
1710 return rc != 0 ? rc : nb_entries;
1713 /* wrapper for lfs.c and obd.c */
1714 int llapi_poollist(const char *name)
1716 int poolcount, rc, i;
1719 rc = llapi_get_poolbuf(name, &buf, &pools, &poolcount);
1723 for (i = 0; i < poolcount; i++)
1724 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", pools[i]);
1731 * Get buffer that holds uuids and the list of pools in a filesystem.
1733 * \param name filesystem name or path
1734 * \param buf bufffer that has to be freed if function returns 0
1735 * \param pools pointer to the list of pools in buffer
1736 * \param poolcount number of pools
1738 * \return 0 when found at least 1 pool, i.e. poolcount > 0
1739 * \retval -error failure
1741 int llapi_get_poolbuf(const char *name, char **buf,
1742 char ***pools, int *poolcount)
1745 * list of pool names (assume that pool count is smaller
1748 char **list, *buffer = NULL, *fsname = (char *)name;
1749 char *poolname = NULL, *tmp = NULL, data[16];
1750 enum param_filter type = FILTER_BY_PATH;
1751 int obdcount, bufsize, rc, nb;
1756 if (name[0] != '/') {
1757 fsname = strdup(name);
1761 poolname = strchr(fsname, '.');
1764 type = FILTER_BY_FS_NAME;
1767 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1768 data, sizeof(data));
1771 obdcount = atoi(data);
1774 * Allocate space for each fsname-OST0000_UUID, 1 per OST,
1775 * and also an array to store the pointers for all that
1779 bufsize = sizeof(struct obd_uuid) * obdcount;
1780 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1781 if (buffer == NULL) {
1785 list = (char **) (buffer + bufsize);
1788 /* name is a path or fsname */
1789 nb = llapi_get_poollist(name, list, obdcount,
1792 /* name is a pool name (<fsname>.<poolname>) */
1793 nb = llapi_get_poolmembers(name, list, obdcount,
1797 if (nb == -EOVERFLOW) {
1800 goto retry_get_pools;
1803 rc = (nb < 0 ? nb : 0);
1810 /* Don't free buffer, it will be used later */
1813 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1818 typedef int (semantic_func_t)(char *path, int p, int *d,
1819 void *data, struct dirent64 *de);
1821 #define OBD_NOT_FOUND (-1)
1823 static bool lmv_is_foreign(__u32 magic)
1825 return magic == LMV_MAGIC_FOREIGN;
1828 static void find_param_fini(struct find_param *param)
1830 if (param->fp_migrate)
1833 if (param->fp_obd_indexes) {
1834 free(param->fp_obd_indexes);
1835 param->fp_obd_indexes = NULL;
1838 if (param->fp_lmd) {
1839 free(param->fp_lmd);
1840 param->fp_lmd = NULL;
1843 if (param->fp_lmv_md) {
1844 free(param->fp_lmv_md);
1845 param->fp_lmv_md = NULL;
1849 static int common_param_init(struct find_param *param, char *path)
1851 int lum_size = get_mds_md_size(path);
1856 /* migrate has fp_lmv_md initialized outside */
1857 if (param->fp_migrate)
1860 if (lum_size < PATH_MAX + 1)
1861 lum_size = PATH_MAX + 1;
1863 param->fp_lum_size = lum_size;
1864 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1866 if (param->fp_lmd == NULL) {
1867 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1868 "error: allocate %zu bytes for layout failed",
1869 sizeof(lstat_t) + param->fp_lum_size);
1873 param->fp_lmv_stripe_count = 256;
1874 param->fp_lmv_md = calloc(1,
1875 lmv_user_md_size(param->fp_lmv_stripe_count,
1876 LMV_USER_MAGIC_SPECIFIC));
1877 if (param->fp_lmv_md == NULL) {
1878 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1879 "error: allocation of %d bytes for ioctl",
1880 lmv_user_md_size(param->fp_lmv_stripe_count,
1881 LMV_USER_MAGIC_SPECIFIC));
1882 find_param_fini(param);
1886 param->fp_got_uuids = 0;
1887 param->fp_obd_indexes = NULL;
1888 param->fp_obd_index = OBD_NOT_FOUND;
1889 param->fp_mdt_index = OBD_NOT_FOUND;
1893 static int cb_common_fini(char *path, int p, int *dp, void *data,
1894 struct dirent64 *de)
1896 struct find_param *param = data;
1902 /* set errno upon failure */
1903 static int open_parent(const char *path)
1909 path_copy = strdup(path);
1910 if (path_copy == NULL)
1913 parent_path = dirname(path_copy);
1914 parent = open(parent_path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1920 static int cb_get_dirstripe(char *path, int *d, struct find_param *param)
1923 bool did_nofollow = false;
1928 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1929 if (param->fp_get_default_lmv)
1930 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1932 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1934 ret = ioctl(*d, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1936 /* if ENOTTY likely to be a fake symlink, so try again after
1937 * new open() with O_NOFOLLOW, but only once to prevent any
1938 * loop like for the path of a file/dir not on Lustre !!
1940 if (ret < 0 && errno == ENOTTY && !did_nofollow) {
1943 did_nofollow = true;
1944 fd = open(path, O_RDONLY | O_NOFOLLOW);
1946 /* restore original errno */
1951 /* close original fd and set new */
1954 ret2 = ioctl(fd, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1955 if (ret2 < 0 && errno != E2BIG) {
1956 /* restore original errno */
1960 /* LMV is ok or need to handle E2BIG case now */
1964 if (errno == E2BIG && ret != 0) {
1968 /* if foreign LMV case, fake stripes number */
1969 if (lmv_is_foreign(param->fp_lmv_md->lum_magic)) {
1970 struct lmv_foreign_md *lfm;
1972 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1973 if (lfm->lfm_length < XATTR_SIZE_MAX -
1974 offsetof(typeof(*lfm), lfm_value)) {
1975 uint32_t size = lfm->lfm_length +
1976 offsetof(typeof(*lfm), lfm_value);
1978 stripe_count = lmv_foreign_to_md_stripes(size);
1980 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1981 "error: invalid %d foreign size returned from ioctl",
1986 stripe_count = param->fp_lmv_md->lum_stripe_count;
1988 if (stripe_count <= param->fp_lmv_stripe_count)
1991 free(param->fp_lmv_md);
1992 param->fp_lmv_stripe_count = stripe_count;
1993 lmv_size = lmv_user_md_size(stripe_count,
1994 LMV_USER_MAGIC_SPECIFIC);
1995 param->fp_lmv_md = malloc(lmv_size);
1996 if (param->fp_lmv_md == NULL) {
1997 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1998 "error: allocation of %d bytes for ioctl",
1999 lmv_user_md_size(param->fp_lmv_stripe_count,
2000 LMV_USER_MAGIC_SPECIFIC));
2009 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
2012 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
2013 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
2014 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
2015 lmd_v2->lmd_stx.stx_uid = st->st_uid;
2016 lmd_v2->lmd_stx.stx_gid = st->st_gid;
2017 lmd_v2->lmd_stx.stx_mode = st->st_mode;
2018 lmd_v2->lmd_stx.stx_ino = st->st_ino;
2019 lmd_v2->lmd_stx.stx_size = st->st_size;
2020 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
2021 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
2022 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
2023 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
2024 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
2025 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
2026 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
2027 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
2028 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
2030 lmd_v2->lmd_flags = 0;
2032 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
2034 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
2035 if (lmd_v2->lmd_stx.stx_size)
2036 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
2037 if (lmd_v2->lmd_stx.stx_blocks)
2038 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
2040 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
2041 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
2042 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
2047 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
2049 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
2050 struct lov_user_mds_data *lmd_v2 = lmdbuf;
2054 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
2058 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
2061 st = lmd_v1->lmd_st;
2062 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
2063 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
2064 convert_lmd_statx(lmd_v2, &st, false);
2065 lmd_v2->lmd_lmmsize = 0;
2066 lmd_v2->lmd_padding = 0;
2071 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
2072 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
2074 struct lov_user_mds_data *lmd = lmdbuf;
2075 static bool use_old_ioctl;
2079 if (parent_fd < 0 && dir_fd < 0)
2081 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
2086 * LL_IOC_MDC_GETINFO operates on the current directory inode
2087 * and returns struct lov_user_mds_data, while
2088 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
2090 if (type == GET_LMD_INFO)
2091 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_V1 :
2092 LL_IOC_MDC_GETINFO_V2;
2094 cmd = LL_IOC_LOV_GETSTRIPE;
2097 ret = ioctl(dir_fd, cmd, lmdbuf);
2098 if (ret < 0 && errno == ENOTTY &&
2099 cmd == LL_IOC_MDC_GETINFO_V2) {
2100 cmd = LL_IOC_MDC_GETINFO_V1;
2101 use_old_ioctl = true;
2105 if (cmd == LL_IOC_MDC_GETINFO_V1 && !ret)
2106 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2108 if (ret < 0 && errno == ENOTTY && type == GET_LMD_STRIPE) {
2111 /* retry ioctl() after new open() with O_NOFOLLOW
2112 * just in case it could be a fake symlink
2113 * need using a new open() as dir_fd is being closed
2117 dir_fd2 = open(path, O_RDONLY | O_NDELAY | O_NOFOLLOW);
2119 /* return original error */
2122 ret = ioctl(dir_fd2, cmd, lmdbuf);
2123 /* pass new errno or success back to caller */
2129 } else if (parent_fd >= 0) {
2130 const char *fname = strrchr(path, '/');
2133 * IOC_MDC_GETFILEINFO takes as input the filename (relative to
2134 * the parent directory) and returns struct lov_user_mds_data,
2135 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2137 * This avoids opening, locking, and closing each file on the
2138 * client if that is not needed. Multiple of these ioctl() can
2139 * be done on the parent dir with a single open for all
2140 * files in that directory, and it also doesn't pollute the
2141 * client dcache with millions of dentries when traversing
2142 * a large filesystem.
2144 fname = (fname == NULL ? path : fname + 1);
2146 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2149 else if (ret >= lmdlen || ret++ == 0)
2152 if (type == GET_LMD_INFO)
2153 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_V1 :
2154 IOC_MDC_GETFILEINFO_V2;
2156 cmd = IOC_MDC_GETFILESTRIPE;
2159 ret = ioctl(parent_fd, cmd, lmdbuf);
2160 if (ret < 0 && errno == ENOTTY &&
2161 cmd == IOC_MDC_GETFILEINFO_V2) {
2162 cmd = IOC_MDC_GETFILEINFO_V1;
2163 use_old_ioctl = true;
2164 goto retry_getfileinfo;
2167 if (cmd == IOC_MDC_GETFILEINFO_V1 && !ret)
2168 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2172 if (ret && type == GET_LMD_INFO) {
2173 if (errno == ENOTTY) {
2177 * ioctl is not supported, it is not a lustre fs.
2178 * Do the regular lstat(2) instead.
2180 ret = lstat_f(path, &st);
2183 llapi_error(LLAPI_MSG_ERROR, ret,
2184 "error: %s: lstat failed for %s",
2188 convert_lmd_statx(lmd, &st, true);
2190 * It may be wrong to set use_old_ioctl with true as
2191 * the file is not a lustre fs. So reset it with false
2194 use_old_ioctl = false;
2195 } else if (errno == ENOENT) {
2197 llapi_error(LLAPI_MSG_WARN, ret,
2198 "warning: %s does not exist", path);
2199 } else if (errno != EISDIR && errno != ENODATA) {
2201 llapi_error(LLAPI_MSG_ERROR, ret,
2202 "%s ioctl failed for %s.",
2203 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2204 "IOC_MDC_GETFILEINFO", path);
2211 static int llapi_semantic_traverse(char *path, int size, int parent,
2212 semantic_func_t sem_init,
2213 semantic_func_t sem_fini, void *data,
2214 struct dirent64 *de)
2216 struct find_param *param = (struct find_param *)data;
2217 struct dirent64 *dent;
2218 int len, ret, d, p = -1;
2224 d = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
2225 /* if an invalid fake dir symlink, opendir() will return EINVAL
2226 * instead of ENOTDIR. If a valid but dangling faked or real file/dir
2227 * symlink ENOENT will be returned. For a valid/resolved fake or real
2228 * file symlink ENOTDIR will be returned as for a regular file.
2229 * opendir() will be successful for a valid and resolved fake or real
2230 * dir simlink or a regular dir.
2232 if (d == -1 && errno != ENOTDIR && errno != EINVAL && errno != ENOENT) {
2234 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2237 } else if (d == -1) {
2238 if (errno == ENOENT || errno == EINVAL) {
2239 int old_errno = errno;
2241 /* try to open with O_NOFOLLOW this will help
2242 * differentiate fake vs real symlinks
2243 * it is ok to not use O_DIRECTORY with O_RDONLY
2244 * and it will prevent the need to deal with ENOTDIR
2245 * error, instead of ELOOP, being returned by recent
2246 * kernels for real symlinks
2248 d = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2249 /* if a dangling real symlink should return ELOOP, or
2250 * again ENOENT if really non-existing path, or E...??
2251 * So return original error. If success or ENOTDIR, path
2252 * is likely to be a fake dir/file symlink, so continue
2262 if (parent == -1 && d == -1) {
2263 /* Open the parent dir. */
2264 p = open_parent(path);
2270 } else { /* d != -1 */
2273 /* try to reopen dir with O_NOFOLLOW just in case of a foreign
2276 d2 = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2281 /* continue with d */
2287 ret = sem_init(path, (parent != -1) ? parent : p, &d, data, de);
2297 /* ENOTDIR if fake symlink, do not consider it as an error */
2298 if (errno != ENOTDIR)
2299 llapi_error(LLAPI_MSG_ERROR, errno,
2300 "fdopendir() failed");
2307 while ((dent = readdir64(dir)) != NULL) {
2310 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2314 if ((len + dent->d_reclen + 2) > size) {
2315 llapi_err_noerrno(LLAPI_MSG_ERROR,
2316 "error: %s: string buffer too small for %s",
2321 strcat(path, dent->d_name);
2323 if (dent->d_type == DT_UNKNOWN) {
2324 struct lov_user_mds_data *lmd = param->fp_lmd;
2326 rc = get_lmd_info_fd(path, d, -1, param->fp_lmd,
2327 param->fp_lum_size, GET_LMD_INFO);
2329 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2336 switch (dent->d_type) {
2338 llapi_err_noerrno(LLAPI_MSG_ERROR,
2339 "error: %s: '%s' is UNKNOWN type %d",
2340 __func__, dent->d_name, dent->d_type);
2343 rc = llapi_semantic_traverse(path, size, d, sem_init,
2344 sem_fini, data, dent);
2345 if (rc != 0 && ret == 0)
2351 rc = sem_init(path, d, NULL, data, dent);
2352 if (rc < 0 && ret == 0) {
2357 if (sem_fini && rc == 0)
2358 sem_fini(path, d, NULL, data, dent);
2366 sem_fini(path, parent, &d, data, de);
2379 static int param_callback(char *path, semantic_func_t sem_init,
2380 semantic_func_t sem_fini, struct find_param *param)
2382 int ret, len = strlen(path);
2385 if (len > PATH_MAX) {
2387 llapi_error(LLAPI_MSG_ERROR, ret,
2388 "Path name '%s' is too long", path);
2392 buf = (char *)malloc(2 * PATH_MAX);
2396 snprintf(buf, PATH_MAX + 1, "%s", path);
2397 ret = common_param_init(param, buf);
2401 param->fp_depth = 0;
2403 ret = llapi_semantic_traverse(buf, 2 * PATH_MAX, -1, sem_init,
2404 sem_fini, param, NULL);
2406 find_param_fini(param);
2408 return ret < 0 ? ret : 0;
2411 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2415 rc = ioctl(fd, OBD_IOC_GETDTNAME, lov_name);
2416 if (rc && errno == ENOTTY)
2417 rc = ioctl(fd, OBD_IOC_GETNAME_OLD, lov_name);
2420 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2426 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2430 rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2433 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2439 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2443 /* do not follow faked symlinks */
2444 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
2446 /* real symlink should have failed with ELOOP so retry without
2447 * O_NOFOLLOW just in case
2449 fd = open(path, O_RDONLY | O_NONBLOCK);
2452 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'",
2458 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2464 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2468 fd = open(path, O_RDONLY | O_NONBLOCK);
2471 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2475 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2487 * If uuidp is NULL, return the number of available obd uuids.
2488 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2489 * there are more OSTs than allocated to uuidp, then an error is returned with
2490 * the ost_count set to number of available obd uuids.
2492 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2493 int *ost_count, enum tgt_type type)
2495 char buf[PATH_MAX], format[32];
2496 int rc = 0, index = 0;
2497 struct obd_uuid name;
2501 /* Get the lov name */
2502 if (type == LOV_TYPE)
2503 rc = llapi_file_fget_lov_uuid(fd, &name);
2505 rc = llapi_file_fget_lmv_uuid(fd, &name);
2509 /* Now get the ost uuids */
2510 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2511 FILTER_BY_EXACT, "target_obd", ¶m);
2515 fp = fopen(param.gl_pathv[0], "r");
2518 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2523 snprintf(format, sizeof(format),
2524 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2525 while (fgets(buf, sizeof(buf), fp) != NULL) {
2526 if (uuidp && (index < *ost_count)) {
2527 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2535 if (uuidp && (index > *ost_count))
2540 cfs_free_param_data(¶m);
2544 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2546 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2549 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2554 root = open(mnt, O_RDONLY | O_DIRECTORY);
2557 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2562 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2571 * Check if user specified value matches a real uuid. Ignore _UUID,
2572 * -osc-4ba41334, other trailing gunk in comparison.
2573 * @param real_uuid ends in "_UUID"
2574 * @param search_uuid may or may not end in "_UUID"
2576 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2578 int cmplen = strlen(real_uuid);
2579 int searchlen = strlen(search_uuid);
2581 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2583 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2587 * The UUIDs may legitimately be different lengths, if
2588 * the system was upgraded from an older version.
2590 if (cmplen != searchlen)
2593 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2597 * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2598 * returned in param->fp_obd_index
2600 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2602 struct obd_uuid obd_uuid;
2609 if (param->fp_got_uuids)
2612 /* Get the lov/lmv name */
2613 if (param->fp_get_lmv)
2614 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2616 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2618 if (rc != -ENOTTY) {
2619 llapi_error(LLAPI_MSG_ERROR, rc,
2620 "error: can't get %s name: %s",
2621 param->fp_get_lmv ? "lmv" : "lov",
2629 param->fp_got_uuids = 1;
2631 /* Now get the ost uuids */
2632 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2633 obd_uuid.uuid, FILTER_BY_EXACT,
2634 "target_obd", ¶m_data);
2638 fp = fopen(param_data.gl_pathv[0], "r");
2641 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2642 param_data.gl_pathv[0]);
2646 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2647 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2648 param->fp_get_lmv ? "MDTS" : "OBDS");
2650 snprintf(format, sizeof(format),
2651 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2652 while (fgets(buf, sizeof(buf), fp) != NULL) {
2655 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2658 if (param->fp_obd_uuid) {
2659 if (llapi_uuid_match(obd_uuid.uuid,
2660 param->fp_obd_uuid->uuid)) {
2661 param->fp_obd_index = index;
2664 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2665 /* Print everything */
2666 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2669 param->fp_obds_printed = 1;
2673 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2674 llapi_err_noerrno(LLAPI_MSG_ERROR,
2675 "error: %s: unknown obduuid: %s",
2676 __func__, param->fp_obd_uuid->uuid);
2680 cfs_free_param_data(¶m_data);
2685 * In this case, param->fp_obd_uuid will be an array of obduuids and
2686 * obd index for all these obduuids will be returned in
2687 * param->fp_obd_indexes
2689 static int setup_indexes(int d, char *path, struct obd_uuid *obduuids,
2690 int num_obds, int **obdindexes, int *obdindex,
2693 int ret, obdcount, obd_valid = 0, obdnum;
2695 struct obd_uuid *uuids = NULL;
2699 if (type == LOV_TYPE)
2700 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2702 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2706 obdcount = atoi(buf);
2707 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2712 ret = llapi_get_target_uuids(d, uuids, &obdcount, type);
2714 if (ret == -EOVERFLOW) {
2715 struct obd_uuid *uuids_temp;
2717 uuids_temp = realloc(uuids, obdcount *
2718 sizeof(struct obd_uuid));
2719 if (uuids_temp != NULL) {
2721 goto retry_get_uuids;
2726 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2730 indexes = malloc(num_obds * sizeof(*obdindex));
2731 if (indexes == NULL) {
2736 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2739 /* The user may have specified a simple index */
2740 i = strtol(obduuids[obdnum].uuid, &end, 0);
2741 if (end && *end == '\0' && i < obdcount) {
2742 indexes[obdnum] = i;
2745 for (i = 0; i < obdcount; i++) {
2746 if (llapi_uuid_match(uuids[i].uuid,
2747 obduuids[obdnum].uuid)) {
2748 indexes[obdnum] = i;
2754 if (i >= obdcount) {
2755 indexes[obdnum] = OBD_NOT_FOUND;
2756 llapi_err_noerrno(LLAPI_MSG_ERROR,
2757 "invalid obduuid '%s'",
2758 obduuids[obdnum].uuid);
2764 *obdindex = OBD_NOT_FOUND;
2766 *obdindex = obd_valid;
2768 *obdindexes = indexes;
2776 static int setup_target_indexes(int d, char *path, struct find_param *param)
2780 if (param->fp_mdt_uuid) {
2781 ret = setup_indexes(d, path, param->fp_mdt_uuid,
2783 ¶m->fp_mdt_indexes,
2784 ¶m->fp_mdt_index, LMV_TYPE);
2789 if (param->fp_obd_uuid) {
2790 ret = setup_indexes(d, path, param->fp_obd_uuid,
2792 ¶m->fp_obd_indexes,
2793 ¶m->fp_obd_index, LOV_TYPE);
2798 param->fp_got_uuids = 1;
2803 int llapi_ostlist(char *path, struct find_param *param)
2808 fd = open(path, O_RDONLY | O_DIRECTORY);
2812 ret = setup_obd_uuid(fd, path, param);
2819 * Tries to determine the default stripe attributes for a given filesystem. The
2820 * filesystem to check should be specified by fsname, or will be determined
2823 static int sattr_get_defaults(const char *const fsname,
2824 unsigned int *scount,
2825 unsigned int *ssize,
2826 unsigned int *soffset)
2832 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2833 "stripecount", val, sizeof(val));
2836 *scount = atoi(val);
2840 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2841 "stripesize", val, sizeof(val));
2848 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2849 "stripeoffset", val, sizeof(val));
2852 *soffset = atoi(val);
2859 * Tries to gather the default stripe attributes for a given filesystem. If
2860 * the attributes can be determined, they are cached for easy retreival the
2861 * next time they are needed. Only a single filesystem's attributes are
2864 int sattr_cache_get_defaults(const char *const fsname,
2865 const char *const pathname, unsigned int *scount,
2866 unsigned int *ssize, unsigned int *soffset)
2869 char fsname[PATH_MAX + 1];
2870 unsigned int stripecount;
2871 unsigned int stripesize;
2872 unsigned int stripeoffset;
2878 char fsname_buf[PATH_MAX + 1];
2879 unsigned int tmp[3];
2881 if (fsname == NULL) {
2882 rc = llapi_search_fsname(pathname, fsname_buf);
2886 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2889 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2891 * Ensure all 3 sattrs (count, size, and offset) are
2892 * successfully retrieved and stored in tmp before writing to
2895 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2899 cache.stripecount = tmp[0];
2900 cache.stripesize = tmp[1];
2901 cache.stripeoffset = tmp[2];
2902 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2906 *scount = cache.stripecount;
2908 *ssize = cache.stripesize;
2910 *soffset = cache.stripeoffset;
2915 static char *layout2name(__u32 layout_pattern)
2917 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2919 else if (layout_pattern == LOV_PATTERN_MDT)
2921 else if (layout_pattern == LOV_PATTERN_RAID0)
2923 else if (layout_pattern ==
2924 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2925 return "raid0,overstriped";
2930 enum lov_dump_flags {
2931 LDF_IS_DIR = 0x0001,
2932 LDF_IS_RAW = 0x0002,
2933 LDF_INDENT = 0x0004,
2934 LDF_SKIP_OBJS = 0x0008,
2936 LDF_EXTENSION = 0x0020,
2939 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2940 struct lov_user_ost_data_v1 *objects,
2941 enum llapi_layout_verbose verbose,
2942 int depth, char *pool_name,
2943 enum lov_dump_flags flags)
2945 bool is_dir = flags & LDF_IS_DIR;
2946 bool is_raw = flags & LDF_IS_RAW;
2947 bool indent = flags & LDF_INDENT;
2948 bool yaml = flags & LDF_YAML;
2949 bool skip_objs = flags & LDF_SKIP_OBJS;
2950 bool extension = flags & LDF_EXTENSION;
2951 char *prefix = is_dir ? "" : "lmm_";
2952 char *separator = "";
2953 char *space = indent ? " " : "";
2956 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2957 lmm_oi_set_seq(&lum->lmm_oi, 0);
2958 if (!indent && (verbose & VERBOSE_DETAIL))
2959 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2962 if (!yaml && !indent && depth && path &&
2963 ((verbose != VERBOSE_OBJID) || !is_dir))
2964 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2966 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2967 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2968 space, prefix, lum->lmm_magic);
2969 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2971 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2972 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2974 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2977 if (verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) {
2982 if (verbose & ~VERBOSE_DFID)
2983 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2987 struct lu_fid dir_fid;
2989 rc = llapi_path2fid(path, &dir_fid);
2991 llapi_error(LLAPI_MSG_ERROR, rc,
2992 "Cannot determine directory fid.");
2994 seq = dir_fid.f_seq;
2995 oid = dir_fid.f_oid;
2996 ver = dir_fid.f_ver;
2999 * This needs a bit of hand-holding since old 1.x
3000 * lmm_oi have { oi.oi_id = mds_inum, oi.oi_seq = 0 }
3001 * and 2.x lmm_oi have { oi.oi_id = mds_oid,
3002 * oi.oi_seq = mds_seq } instead of a real FID.
3003 * Ideally the 2.x code would have stored this like a
3004 * FID with { oi_id = mds_seq, oi_seq = mds_oid } so
3005 * the ostid union lu_fid { f_seq = mds_seq,
3006 * f_oid = mds_oid } worked properly (especially since
3007 * IGIF FIDs use mds_inum as the FID SEQ), but
3008 * unfortunately that didn't happen.
3010 * Print it to look like an IGIF FID, even though the
3011 * fields are reversed on disk, so that it makes sense
3014 * Don't use ostid_id() and ostid_seq(), since they
3015 * assume the oi_fid fields are in the right order.
3016 * This is why there are separate lmm_oi_seq() and
3017 * lmm_oi_id() routines for this.
3019 * For newer layout types hopefully this will be a
3022 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
3023 lmm_oi_id(&lum->lmm_oi) :
3024 lmm_oi_seq(&lum->lmm_oi);
3025 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
3026 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
3027 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
3031 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
3032 (unsigned long long)seq, oid, ver);
3034 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
3035 (unsigned long long)seq, oid, ver);
3038 if (verbose & VERBOSE_STRIPE_COUNT) {
3039 if (verbose & ~VERBOSE_STRIPE_COUNT)
3040 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
3043 if (!is_raw && lum->lmm_stripe_count == 0 &&
3044 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
3045 unsigned int scount;
3047 rc = sattr_cache_get_defaults(NULL, path,
3051 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3054 llapi_error(LLAPI_MSG_ERROR, rc,
3055 "Cannot determine default stripe count.");
3057 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3059 (__s16)lum->lmm_stripe_count);
3062 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
3064 (__s16)lum->lmm_stripe_count);
3066 if (!yaml && is_dir)
3072 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
3073 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
3074 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3075 if (verbose & ~VERBOSE_EXT_SIZE && extension)
3076 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
3078 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
3079 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
3081 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
3084 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
3087 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
3089 llapi_error(LLAPI_MSG_ERROR, rc,
3090 "Cannot determine default stripe size.");
3092 /* Extension size is in KiB */
3093 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3095 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
3096 (unsigned long long)lum->lmm_stripe_size);
3098 if (!yaml && is_dir)
3104 if ((verbose & VERBOSE_PATTERN)) {
3105 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3106 if (verbose & ~VERBOSE_PATTERN)
3107 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
3109 if (lov_pattern_supported(lum->lmm_pattern))
3110 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3111 layout2name(lum->lmm_pattern));
3113 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
3114 separator = (!yaml && is_dir) ? " " : "\n";
3117 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
3118 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3119 if (verbose & ~VERBOSE_GENERATION)
3120 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
3122 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3123 skip_objs ? 0 : (int)lum->lmm_layout_gen);
3127 if (verbose & VERBOSE_STRIPE_OFFSET) {
3128 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3129 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3130 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
3132 if (is_dir || skip_objs)
3133 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3134 lum->lmm_stripe_offset ==
3135 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
3136 lum->lmm_stripe_offset);
3137 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
3138 llapi_printf(LLAPI_MSG_NORMAL, "0");
3140 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3141 objects[0].l_ost_idx);
3142 if (!yaml && is_dir)
3148 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
3149 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3150 if (verbose & ~VERBOSE_POOL)
3151 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
3153 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
3154 if (!yaml && is_dir)
3160 if (strlen(separator) != 0)
3161 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3164 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
3165 struct lov_user_ost_data_v1 *objects,
3166 char *path, int obdindex, int depth,
3167 enum llapi_layout_verbose verbose,
3168 enum lov_dump_flags flags)
3170 bool is_dir = flags & LDF_IS_DIR;
3171 bool indent = flags & LDF_INDENT;
3172 bool skip_objs = flags & LDF_SKIP_OBJS;
3173 bool yaml = flags & LDF_YAML;
3174 bool obdstripe = obdindex == OBD_NOT_FOUND;
3177 if (!obdstripe && !skip_objs) {
3178 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
3179 if (obdindex == objects[i].l_ost_idx) {
3189 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
3192 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
3193 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
3194 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
3195 char *space = " - ";
3198 llapi_printf(LLAPI_MSG_NORMAL,
3199 "%6slmm_objects:\n", " ");
3201 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
3203 llapi_printf(LLAPI_MSG_NORMAL,
3204 "\tobdidx\t\t objid\t\t objid\t\t group\n");
3206 for (i = 0; i < lum->lmm_stripe_count; i++) {
3207 int idx = objects[i].l_ost_idx;
3208 long long oid = ostid_id(&objects[i].l_ost_oi);
3209 long long gr = ostid_seq(&objects[i].l_ost_oi);
3211 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
3215 struct lu_fid fid = { 0 };
3217 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3218 llapi_printf(LLAPI_MSG_NORMAL,
3219 "%sl_ost_idx: %d\n", space, idx);
3220 llapi_printf(LLAPI_MSG_NORMAL,
3221 "%8sl_fid: "DFID_NOBRACE"\n",
3223 } else if (indent) {
3224 struct lu_fid fid = { 0 };
3226 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3227 llapi_printf(LLAPI_MSG_NORMAL,
3228 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
3229 space, i, idx, PFID(&fid));
3233 sprintf(fmt, "%s%s%s\n",
3234 "\t%6u\t%14llu\t%#13llx\t",
3235 (fid_seq_is_rsvd(gr) ||
3236 fid_seq_is_mdt0(gr)) ?
3237 "%14llu" : "%#14llx", "%s");
3238 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
3240 obdindex == idx ? " *" : "");
3244 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3247 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3248 char *path, int obdindex, int depth,
3249 enum llapi_layout_verbose verbose,
3250 enum lov_dump_flags flags)
3252 struct lmv_user_mds_data *objects = lum->lum_objects;
3253 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3254 char *separator = "";
3255 bool yaml = flags & LDF_YAML;
3256 bool obdstripe = false;
3259 if (obdindex != OBD_NOT_FOUND) {
3260 if (lum->lum_stripe_count == 0) {
3261 if (obdindex == lum->lum_stripe_offset)
3264 for (i = 0; i < lum->lum_stripe_count; i++) {
3265 if (obdindex == objects[i].lum_mds) {
3266 llapi_printf(LLAPI_MSG_NORMAL,
3281 /* show all information default */
3283 if (lum->lum_magic == LMV_USER_MAGIC)
3284 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3285 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3287 verbose = VERBOSE_OBJID;
3290 if (depth && path && ((verbose != VERBOSE_OBJID)))
3291 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3293 if (verbose & VERBOSE_STRIPE_COUNT) {
3294 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3295 if (verbose & ~VERBOSE_STRIPE_COUNT)
3296 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3297 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3298 (int)lum->lum_stripe_count);
3299 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3305 if (verbose & VERBOSE_STRIPE_OFFSET) {
3306 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3307 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3308 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3309 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3310 (int)lum->lum_stripe_offset);
3311 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3317 if (verbose & VERBOSE_HASH_TYPE) {
3318 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3319 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3321 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3322 if (verbose & ~VERBOSE_HASH_TYPE)
3323 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3324 if (type < LMV_HASH_TYPE_MAX)
3325 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3326 mdt_hash_name[type]);
3328 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3330 if (flags & LMV_HASH_FLAG_MIGRATION)
3331 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3332 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3333 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3334 if (flags & LMV_HASH_FLAG_LOST_LMV)
3335 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3336 if (flags & LMV_HASH_FLAG_FIXED)
3337 llapi_printf(LLAPI_MSG_NORMAL, ",fixed");
3338 if (flags & ~LMV_HASH_FLAG_KNOWN)
3339 llapi_printf(LLAPI_MSG_NORMAL, ",unknown_%04x",
3340 flags & ~LMV_HASH_FLAG_KNOWN);
3342 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3348 if ((verbose & VERBOSE_INHERIT) && lum->lum_magic == LMV_USER_MAGIC) {
3349 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3350 if (verbose & ~VERBOSE_INHERIT)
3351 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit: ");
3352 if (lum->lum_max_inherit == LMV_INHERIT_UNLIMITED)
3353 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3354 else if (lum->lum_max_inherit == LMV_INHERIT_NONE)
3355 llapi_printf(LLAPI_MSG_NORMAL, "0");
3357 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3358 lum->lum_max_inherit);
3359 if (verbose & VERBOSE_INHERIT && !yaml)
3365 if ((verbose & VERBOSE_INHERIT_RR) &&
3366 lum->lum_magic == LMV_USER_MAGIC &&
3367 lum->lum_stripe_offset == LMV_OFFSET_DEFAULT) {
3368 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3369 if (verbose & ~VERBOSE_INHERIT_RR)
3370 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit_rr: ");
3371 if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_UNLIMITED)
3372 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3373 else if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_NONE)
3374 llapi_printf(LLAPI_MSG_NORMAL, "0");
3376 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3377 lum->lum_max_inherit_rr);
3378 if (verbose & VERBOSE_INHERIT_RR && !yaml)
3386 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3387 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3388 if (lum->lum_stripe_count > 0)
3389 llapi_printf(LLAPI_MSG_NORMAL,
3390 "mdtidx\t\t FID[seq:oid:ver]\n");
3391 for (i = 0; i < lum->lum_stripe_count; i++) {
3392 int idx = objects[i].lum_mds;
3393 struct lu_fid *fid = &objects[i].lum_fid;
3395 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3396 llapi_printf(LLAPI_MSG_NORMAL,
3397 "%6u\t\t "DFID"\t\t%s\n",
3399 obdindex == idx ? " *" : "");
3403 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3404 pool_name[0] != '\0') {
3405 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3406 if (verbose & ~VERBOSE_POOL)
3407 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3409 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3413 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3414 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3417 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3418 enum lov_dump_flags flags)
3420 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3421 int depth = param->fp_max_depth;
3422 enum llapi_layout_verbose verbose = param->fp_verbose;
3423 bool yaml = flags & LDF_YAML;
3425 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3426 !(flags & LDF_IS_DIR)) && !yaml)
3427 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3429 if (verbose & VERBOSE_DETAIL) {
3430 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3431 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3432 " ", comp_v1->lcm_magic);
3433 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3434 " ", comp_v1->lcm_size);
3435 if (flags & LDF_IS_DIR)
3436 llapi_printf(LLAPI_MSG_NORMAL,
3437 "%2slcm_flags: %s\n", " ",
3438 comp_v1->lcm_mirror_count > 0 ?
3441 llapi_printf(LLAPI_MSG_NORMAL,
3442 "%2slcm_flags: %s\n", " ",
3443 llapi_layout_flags_string(comp_v1->lcm_flags));
3446 if (verbose & VERBOSE_GENERATION) {
3447 if (verbose & ~VERBOSE_GENERATION)
3448 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3450 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3453 if (verbose & VERBOSE_MIRROR_COUNT) {
3454 if (verbose & ~VERBOSE_MIRROR_COUNT)
3455 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3457 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3458 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3459 comp_v1->lcm_mirror_count + 1 : 1);
3462 if (verbose & VERBOSE_COMP_COUNT) {
3463 if (verbose & ~VERBOSE_COMP_COUNT)
3464 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3466 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3467 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3468 comp_v1->lcm_entry_count : 0);
3471 if (verbose & VERBOSE_DETAIL && !yaml)
3472 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3475 static void lcme_flags2str(__u32 comp_flags)
3481 llapi_printf(LLAPI_MSG_NORMAL, "0");
3484 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3485 if (comp_flags & comp_flags_table[i].cfn_flag) {
3487 llapi_printf(LLAPI_MSG_NORMAL, ",");
3488 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3489 comp_flags_table[i].cfn_name);
3490 comp_flags &= ~comp_flags_table[i].cfn_flag;
3496 llapi_printf(LLAPI_MSG_NORMAL, ",");
3497 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3501 static void lov_dump_comp_v1_entry(struct find_param *param,
3502 enum lov_dump_flags flags, int index)
3504 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3505 struct lov_comp_md_entry_v1 *entry;
3506 char *separator = "";
3507 enum llapi_layout_verbose verbose = param->fp_verbose;
3508 bool yaml = flags & LDF_YAML;
3510 entry = &comp_v1->lcm_entries[index];
3513 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3515 if (verbose & VERBOSE_COMP_ID) {
3516 if (verbose & VERBOSE_DETAIL && !yaml)
3517 llapi_printf(LLAPI_MSG_NORMAL,
3518 "%slcme_id: ", " - ");
3519 else if (verbose & ~VERBOSE_COMP_ID)
3520 llapi_printf(LLAPI_MSG_NORMAL,
3521 "%4slcme_id: ", " ");
3522 if (entry->lcme_id != LCME_ID_INVAL)
3523 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3525 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3529 if (verbose & VERBOSE_MIRROR_ID) {
3530 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3531 if (verbose & ~VERBOSE_MIRROR_ID)
3532 llapi_printf(LLAPI_MSG_NORMAL,
3533 "%4slcme_mirror_id: ", " ");
3534 if (entry->lcme_id != LCME_ID_INVAL)
3535 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3536 mirror_id_of(entry->lcme_id));
3538 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3542 if (verbose & VERBOSE_COMP_FLAGS) {
3543 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3544 if (verbose & ~VERBOSE_COMP_FLAGS)
3545 llapi_printf(LLAPI_MSG_NORMAL,
3546 "%4slcme_flags: ", " ");
3547 lcme_flags2str(entry->lcme_flags);
3550 /* print snapshot timestamp if its a nosync comp */
3551 if ((verbose & VERBOSE_COMP_FLAGS) &&
3552 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3553 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3554 if (verbose & ~VERBOSE_COMP_FLAGS)
3555 llapi_printf(LLAPI_MSG_NORMAL,
3556 "%4slcme_timestamp: ", " ");
3558 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3559 (unsigned long long)entry->lcme_timestamp);
3561 time_t stamp = entry->lcme_timestamp;
3562 char *date_str = asctime(localtime(&stamp));
3564 date_str[strlen(date_str) - 1] = '\0';
3565 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3571 if (verbose & VERBOSE_COMP_START) {
3572 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3573 if (verbose & ~VERBOSE_COMP_START)
3574 llapi_printf(LLAPI_MSG_NORMAL,
3575 "%4slcme_extent.e_start: ", " ");
3576 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3577 (unsigned long long)entry->lcme_extent.e_start);
3581 if (verbose & VERBOSE_COMP_END) {
3582 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3583 if (verbose & ~VERBOSE_COMP_END)
3584 llapi_printf(LLAPI_MSG_NORMAL,
3585 "%4slcme_extent.e_end: ", " ");
3586 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3587 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3589 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3590 (unsigned long long)entry->lcme_extent.e_end);
3595 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3596 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3597 } else if (verbose & VERBOSE_DETAIL) {
3598 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3599 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3600 " ", entry->lcme_offset);
3601 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3602 " ", entry->lcme_size);
3603 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3605 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3610 * Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3611 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3613 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3614 * The table below gives the answers for the specified parameters (value and
3615 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3616 * --------------------------------------
3617 * 1 | file > limit; sign > 0 | -1 / -1 |
3618 * 2 | file = limit; sign > 0 | -1 / -1 |
3619 * 3 | file < limit; sign > 0 | ? / 1 |
3620 * 4 | file > limit; sign = 0 | -1 / -1 |
3621 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3622 * 6 | file < limit; sign = 0 | ? / -1 |
3623 * 7 | file > limit; sign < 0 | 1 / 1 |
3624 * 8 | file = limit; sign < 0 | ? / -1 |
3625 * 9 | file < limit; sign < 0 | ? / -1 |
3626 * --------------------------------------
3627 * Note: 5th actually means that the value is within the interval
3628 * (limit - margin, limit].
3630 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3631 int sign, int negopt, unsigned long long margin,
3637 /* Drop the fraction of margin (of days or size). */
3638 if (file + margin <= limit)
3640 } else if (sign == 0) {
3641 if (file <= limit && file + margin > limit)
3643 else if (file + margin <= limit)
3645 } else if (sign < 0) {
3652 return negopt ? ~ret + 1 : ret;
3655 static inline struct lov_user_md *
3656 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3658 return (struct lov_user_md *)((char *)comp_v1 +
3659 comp_v1->lcm_entries[ent_idx].lcme_offset);
3662 static inline struct lov_user_ost_data_v1 *
3663 lov_v1v3_objects(struct lov_user_md *v1)
3665 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3666 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3668 return v1->lmm_objects;
3672 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3674 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3675 snprintf(pool_name, LOV_MAXPOOLNAME + 1, "%s",
3676 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3678 pool_name[0] = '\0';
3682 print_last_init_comp(struct find_param *param)
3684 /* print all component info */
3685 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3688 /* print specific component info */
3689 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3690 param->fp_check_comp_start || param->fp_check_comp_end ||
3691 param->fp_check_mirror_id || param->fp_check_mirror_index)
3697 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3701 if (param->fp_comp_end == LUSTRE_EOF) {
3702 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3703 match = end == LUSTRE_EOF ? 1 : -1;
3704 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3705 match = end == LUSTRE_EOF ? -1 : 1;
3706 else /* at least EOF */
3708 if (param->fp_exclude_comp_end)
3711 unsigned long long margin;
3713 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3714 match = find_value_cmp(end, param->fp_comp_end,
3715 param->fp_comp_end_sign,
3716 param->fp_exclude_comp_end, margin, 0);
3723 * An example of "getstripe -v" for a two components PFL file:
3726 * lcm_magic: 0x0BD60BD0
3730 * lcm_entry_count: 2
3734 * lcme_extent.e_start: 0
3735 * lcme_extent.e_end: 1048576
3739 * lmm_magic: 0x0BD10BD0
3740 * lmm_seq: 0x200000401
3741 * lmm_object_id: 0x1
3742 * lmm_fid: [0x200000401:0x1:0x0]
3743 * lmm_stripe_count: 1
3744 * lmm_stripe_size: 1048576
3745 * lmm_pattern: raid0
3747 * lmm_stripe_offset: 0
3749 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3753 * lcme_extent.e_start: 1048576
3754 * lcme_extent.e_end: EOF
3758 * lmm_magic: 0x0BD10BD0
3759 * lmm_seq: 0x200000401
3760 * lmm_object_id: 0x1
3761 * lmm_fid: [0x200000401:0x1:0x0]
3762 * lmm_stripe_count: 2
3763 * lmm_stripe_size: 1048576
3764 * lmm_pattern: raid0
3766 * lmm_stripe_offset: 1
3768 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3769 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3771 static void lov_dump_comp_v1(struct find_param *param, char *path,
3772 enum lov_dump_flags flags)
3774 struct lov_comp_md_entry_v1 *entry;
3775 struct lov_user_ost_data_v1 *objects;
3776 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3777 struct lov_user_md_v1 *v1;
3778 char pool_name[LOV_MAXPOOLNAME + 1];
3779 int obdindex = param->fp_obd_index;
3780 int i, j, match, ext;
3781 bool obdstripe = false;
3782 __u16 mirror_index = 0;
3783 __u16 mirror_id = 0;
3785 if (obdindex != OBD_NOT_FOUND) {
3786 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3787 i < comp_v1->lcm_entry_count; i++) {
3788 if (!(comp_v1->lcm_entries[i].lcme_flags &
3792 v1 = lov_comp_entry(comp_v1, i);
3793 objects = lov_v1v3_objects(v1);
3795 for (j = 0; j < v1->lmm_stripe_count; j++) {
3796 if (obdindex == objects[j].l_ost_idx) {
3809 lov_dump_comp_v1_header(param, path, flags);
3811 flags |= LDF_INDENT;
3813 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3814 entry = &comp_v1->lcm_entries[i];
3816 if (param->fp_check_comp_flags) {
3817 if (((param->fp_comp_flags & entry->lcme_flags) !=
3818 param->fp_comp_flags) ||
3819 (param->fp_comp_neg_flags & entry->lcme_flags))
3823 if (param->fp_check_comp_id &&
3824 param->fp_comp_id != entry->lcme_id)
3827 if (param->fp_check_comp_start) {
3828 match = find_value_cmp(entry->lcme_extent.e_start,
3829 param->fp_comp_start,
3830 param->fp_comp_start_sign,
3832 param->fp_comp_start_units, 0);
3837 if (param->fp_check_comp_end) {
3838 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3844 if (param->fp_check_mirror_index) {
3845 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3847 mirror_id = mirror_id_of(entry->lcme_id);
3850 match = find_value_cmp(mirror_index,
3851 param->fp_mirror_index,
3852 param->fp_mirror_index_sign,
3853 param->fp_exclude_mirror_index,
3857 } else if (param->fp_check_mirror_id) {
3858 if (mirror_id != mirror_id_of(entry->lcme_id))
3859 mirror_id = mirror_id_of(entry->lcme_id);
3861 match = find_value_cmp(mirror_id,
3862 param->fp_mirror_id,
3863 param->fp_mirror_id_sign,
3864 param->fp_exclude_mirror_id,
3870 if (print_last_init_comp(param)) {
3872 * if part of stripe info is needed, we'd print only
3873 * the last instantiated component info.
3875 if (entry->lcme_flags & LCME_FL_INIT)
3878 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3879 if (entry->lcme_flags & LCME_FL_EXTENSION)
3880 /* moved back below */
3888 if (entry->lcme_flags & LCME_FL_INIT) {
3889 if (obdindex != OBD_NOT_FOUND) {
3890 flags |= LDF_SKIP_OBJS;
3891 v1 = lov_comp_entry(comp_v1, i);
3892 objects = lov_v1v3_objects(v1);
3894 for (j = 0; j < v1->lmm_stripe_count; j++) {
3895 if (obdindex == objects[j].l_ost_idx) {
3896 flags &= ~LDF_SKIP_OBJS;
3901 flags &= ~LDF_SKIP_OBJS;
3904 flags |= LDF_SKIP_OBJS;
3907 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3909 lov_dump_comp_v1_entry(param, flags, i);
3911 v1 = lov_comp_entry(comp_v1, i);
3912 objects = lov_v1v3_objects(v1);
3913 lov_v1v3_pool_name(v1, pool_name);
3915 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3916 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3917 param->fp_max_depth, param->fp_verbose,
3920 if (print_last_init_comp(param)) {
3922 * directory layout contains only layout template, print the
3926 i = comp_v1->lcm_entry_count - 1;
3929 flags &= ~LDF_SKIP_OBJS;
3931 lov_dump_comp_v1_entry(param, flags, i);
3933 v1 = lov_comp_entry(comp_v1, i);
3934 objects = lov_v1v3_objects(v1);
3935 lov_v1v3_pool_name(v1, pool_name);
3937 entry = &comp_v1->lcm_entries[i];
3938 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3939 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3940 param->fp_max_depth, param->fp_verbose,
3945 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3946 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3949 static inline bool has_any_comp_options(struct find_param *param)
3951 enum llapi_layout_verbose verbose = param->fp_verbose;
3953 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3954 param->fp_check_comp_start || param->fp_check_comp_end ||
3955 param->fp_check_comp_flags)
3958 /* show full layout information, not component specific */
3959 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3962 return verbose & VERBOSE_COMP_OPTS;
3965 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3968 struct lov_user_md *lum = &orig->lmd_lmm;
3969 struct lov_user_mds_data *new;
3970 struct lov_comp_md_v1 *comp_v1;
3971 struct lov_comp_md_entry_v1 *ent;
3972 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3973 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3976 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3978 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3982 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3983 + sizeof(new->lmd_lmmsize));
3985 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3986 comp_v1->lcm_magic = lum->lmm_magic;
3987 comp_v1->lcm_size = lum_off + lum_size;
3988 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3989 comp_v1->lcm_flags = 0;
3990 comp_v1->lcm_entry_count = 1;
3992 ent = &comp_v1->lcm_entries[0];
3994 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3995 ent->lcme_extent.e_start = 0;
3996 ent->lcme_extent.e_end = LUSTRE_EOF;
3997 ent->lcme_offset = lum_off;
3998 ent->lcme_size = lum_size;
4000 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
4005 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
4006 enum lov_dump_flags flags)
4008 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
4010 if (has_any_comp_options(param)) {
4011 struct lov_user_mds_data *new_lmd, *orig_lmd;
4013 orig_lmd = param->fp_lmd;
4014 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
4015 if (new_lmd != NULL) {
4016 param->fp_lmd = new_lmd;
4017 lov_dump_comp_v1(param, path, flags);
4018 param->fp_lmd = orig_lmd;
4024 if (magic == LOV_USER_MAGIC_V1) {
4025 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
4026 param->fp_lmd->lmd_lmm.lmm_objects,
4027 path, param->fp_obd_index,
4028 param->fp_max_depth, param->fp_verbose,
4031 char pool_name[LOV_MAXPOOLNAME + 1];
4032 struct lov_user_ost_data_v1 *objects;
4033 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
4035 snprintf(pool_name, sizeof(pool_name), "%s",
4036 lmmv3->lmm_pool_name);
4037 objects = lmmv3->lmm_objects;
4038 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
4039 objects, path, param->fp_obd_index,
4040 param->fp_max_depth, param->fp_verbose,
4045 static uint32_t check_foreign_type(uint32_t foreign_type)
4049 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
4050 if (lu_foreign_types[i].lft_name == NULL)
4052 if (foreign_type == lu_foreign_types[i].lft_type)
4056 return LU_FOREIGN_TYPE_UNKNOWN;
4059 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
4060 enum lov_dump_flags flags)
4062 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
4063 bool yaml = flags & LDF_YAML;
4065 if (!yaml && param->fp_depth && path)
4066 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4068 if (param->fp_verbose & VERBOSE_DETAIL) {
4069 uint32_t type = check_foreign_type(lfm->lfm_type);
4071 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
4073 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
4075 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
4077 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4078 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4079 lu_foreign_types[type].lft_name);
4081 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4083 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
4086 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
4087 lfm->lfm_length, lfm->lfm_value);
4088 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4091 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
4092 enum lov_dump_flags flags)
4094 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
4095 bool yaml = flags & LDF_YAML;
4097 if (!yaml && param->fp_depth && path)
4098 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4100 if (param->fp_verbose & VERBOSE_DETAIL) {
4101 uint32_t type = check_foreign_type(lfm->lfm_type);
4103 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
4105 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
4107 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
4109 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4110 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4111 lu_foreign_types[type].lft_name);
4113 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4115 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
4118 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
4119 lfm->lfm_length, lfm->lfm_value);
4120 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4123 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
4124 enum lov_dump_flags flags)
4128 if (param->fp_get_lmv || param->fp_get_default_lmv)
4129 magic = (__u32)param->fp_lmv_md->lum_magic;
4131 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
4134 flags |= LDF_IS_RAW;
4139 case LOV_USER_MAGIC_V1:
4140 case LOV_USER_MAGIC_V3:
4141 case LOV_USER_MAGIC_SPECIFIC:
4142 lov_dump_plain_user_lmm(param, path, flags);
4144 case LOV_USER_MAGIC_FOREIGN:
4145 lov_dump_foreign_lmm(param, path, flags);
4148 case LMV_USER_MAGIC: {
4149 char pool_name[LOV_MAXPOOLNAME + 1];
4150 struct lmv_user_md *lum;
4152 lum = (struct lmv_user_md *)param->fp_lmv_md;
4153 snprintf(pool_name, sizeof(pool_name), "%s",
4154 lum->lum_pool_name);
4155 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
4156 param->fp_max_depth, param->fp_verbose,
4160 case LOV_USER_MAGIC_COMP_V1:
4161 lov_dump_comp_v1(param, path, flags);
4163 case LMV_MAGIC_FOREIGN:
4164 lmv_dump_foreign_lmm(param, path, flags);
4167 llapi_printf(LLAPI_MSG_NORMAL,
4168 "unknown lmm_magic: %#x (expecting one of %#x %#x %#x %#x)\n",
4169 *(__u32 *)¶m->fp_lmd->lmd_lmm,
4170 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
4171 LMV_USER_MAGIC, LMV_MAGIC_V1);
4176 static int llapi_file_get_stripe1(const char *path, struct lov_user_md *lum)
4182 fname = strrchr(path, '/');
4184 /* It should be a file (or other non-directory) */
4185 if (fname == NULL) {
4186 dname = (char *)malloc(2);
4190 fname = (char *)path;
4192 dname = (char *)malloc(fname - path + 1);
4195 strncpy(dname, path, fname - path);
4196 dname[fname - path] = '\0';
4200 fd = open(dname, O_RDONLY | O_NONBLOCK);
4206 strcpy((char *)lum, fname);
4207 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
4210 if (close(fd) == -1 && rc == 0)
4218 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
4220 char *canon_path = NULL;
4223 rc = llapi_file_get_stripe1(path, lum);
4224 if (!(rc == -ENOTTY || rc == -ENODATA))
4227 /* Handle failure due to symlinks by dereferencing path manually. */
4228 canon_path = canonicalize_file_name(path);
4229 if (canon_path == NULL)
4230 goto out; /* Keep original rc. */
4232 rc2 = llapi_file_get_stripe1(canon_path, lum);
4234 goto out; /* Keep original rc. */
4243 int llapi_file_lookup(int dirfd, const char *name)
4245 struct obd_ioctl_data data = { 0 };
4250 if (dirfd < 0 || name == NULL)
4253 data.ioc_version = OBD_IOCTL_VERSION;
4254 data.ioc_len = sizeof(data);
4255 data.ioc_inlbuf1 = (char *)name;
4256 data.ioc_inllen1 = strlen(name) + 1;
4258 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
4260 llapi_error(LLAPI_MSG_ERROR, rc,
4261 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
4266 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
4273 * Check if the file time matches all the given criteria (e.g. --atime +/-N).
4274 * Return -1 or 1 if file timestamp does not or does match the given criteria
4275 * correspondingly. Return 0 if the MDS time is being checked and there are
4276 * attributes on OSTs and it is not yet clear if the timespamp matches.
4278 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
4279 * updated timestamps.
4281 static int find_time_check(struct find_param *param, int mds)
4283 struct lov_user_mds_data *lmd = param->fp_lmd;
4287 /* Check if file is accepted. */
4288 if (param->fp_atime) {
4289 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4290 param->fp_atime, param->fp_asign,
4291 param->fp_exclude_atime,
4292 param->fp_time_margin, mds);
4298 if (param->fp_mtime) {
4299 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4300 param->fp_mtime, param->fp_msign,
4301 param->fp_exclude_mtime,
4302 param->fp_time_margin, mds);
4307 * If the previous check matches, but this one is not yet clear,
4308 * we should return 0 to do an RPC on OSTs.
4314 if (param->fp_ctime) {
4315 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4316 param->fp_ctime, param->fp_csign,
4317 param->fp_exclude_ctime,
4318 param->fp_time_margin, mds);
4323 * If the previous check matches, but this one is not yet clear,
4324 * we should return 0 to do an RPC on OSTs.
4333 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4335 struct lov_user_mds_data *lmd = param->fp_lmd;
4340 for (i = 0; i < 2; i++) {
4341 /* Check if file is accepted. */
4342 if (param->fp_newery[NEWERXY_ATIME][i]) {
4343 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4344 param->fp_newery[NEWERXY_ATIME][i],
4351 if (param->fp_newery[NEWERXY_MTIME][i]) {
4352 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4353 param->fp_newery[NEWERXY_MTIME][i],
4359 * If the previous check matches, but this one is not
4360 * yet clear, we should return 0 to do an RPC on OSTs.
4366 if (param->fp_newery[NEWERXY_CTIME][i]) {
4367 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4368 param->fp_newery[NEWERXY_CTIME][i],
4374 * If the previous check matches, but this one is not
4375 * yet clear, we should return 0 to do an RPC on OSTs.
4382 * File birth time (btime) can get from MDT directly.
4383 * if @from_mdt is true, it means the input file attributs are
4384 * obtained directly from MDT.
4385 * Thus, if @from_mdt is false, we should skip the following
4391 if (param->fp_newery[NEWERXY_BTIME][i]) {
4392 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4395 rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4396 param->fp_newery[NEWERXY_BTIME][i],
4407 * Check whether the stripes matches the indexes user provided
4411 static int check_obd_match(struct find_param *param)
4413 struct lov_user_ost_data_v1 *objects;
4414 struct lov_comp_md_v1 *comp_v1 = NULL;
4415 struct lov_user_mds_data *lmd = param->fp_lmd;
4416 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4417 int i, j, k, count = 1;
4419 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4422 if (!S_ISREG(lmd->lmd_stx.stx_mode))
4425 /* exclude foreign */
4426 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4427 return param->fp_exclude_obd;
4430 * Only those files should be accepted, which have a
4431 * stripe on the specified OST.
4433 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4434 comp_v1 = (struct lov_comp_md_v1 *)v1;
4435 count = comp_v1->lcm_entry_count;
4438 for (i = 0; i < count; i++) {
4440 v1 = lov_comp_entry(comp_v1, i);
4442 objects = lov_v1v3_objects(v1);
4444 for (j = 0; j < v1->lmm_stripe_count; j++) {
4445 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4448 for (k = 0; k < param->fp_num_obds; k++) {
4449 if (param->fp_obd_indexes[k] ==
4450 objects[j].l_ost_idx)
4451 return !param->fp_exclude_obd;
4456 return param->fp_exclude_obd;
4459 static int check_mdt_match(struct find_param *param)
4463 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4466 /* FIXME: For striped dir, we should get stripe information and check */
4467 for (i = 0; i < param->fp_num_mdts; i++) {
4468 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4469 return !param->fp_exclude_mdt;
4472 if (param->fp_exclude_mdt)
4479 * Check whether the obd is active or not, if it is
4480 * not active, just print the object affected by this
4483 static void print_failed_tgt(struct find_param *param, char *path, int type)
4485 struct obd_statfs stat_buf;
4486 struct obd_uuid uuid_buf;
4487 int tgt_nr, i, *indexes;
4490 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV) {
4491 llapi_error(LLAPI_MSG_NORMAL, ret, "%s: wrong statfs type(%d)",
4496 tgt_nr = (type == LL_STATFS_LOV) ? param->fp_obd_index :
4497 param->fp_mdt_index;
4498 indexes = (type == LL_STATFS_LOV) ? param->fp_obd_indexes :
4499 param->fp_mdt_indexes;
4501 for (i = 0; i < tgt_nr; i++) {
4502 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4503 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4505 ret = llapi_obd_statfs(path, type, indexes[i], &stat_buf,
4508 llapi_error(LLAPI_MSG_NORMAL, ret,
4509 "%s: obd_uuid: %s failed",
4510 __func__, param->fp_obd_uuid->uuid);
4514 static int find_check_stripe_size(struct find_param *param)
4516 struct lov_comp_md_v1 *comp_v1 = NULL;
4517 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4518 __u32 stripe_size = 0;
4519 int ret, i, count = 1;
4521 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4522 return param->fp_exclude_stripe_size ? 1 : -1;
4524 ret = param->fp_exclude_stripe_size ? 1 : -1;
4525 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4526 comp_v1 = (struct lov_comp_md_v1 *)v1;
4527 count = comp_v1->lcm_entry_count;
4530 for (i = 0; i < count; i++) {
4531 struct lov_comp_md_entry_v1 *ent;
4534 v1 = lov_comp_entry(comp_v1, i);
4536 ent = &comp_v1->lcm_entries[i];
4537 if (ent->lcme_flags & LCME_FL_EXTENSION)
4539 if (!(ent->lcme_flags & LCME_FL_INIT))
4542 stripe_size = v1->lmm_stripe_size;
4545 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4546 param->fp_stripe_size_sign,
4547 param->fp_exclude_stripe_size,
4548 param->fp_stripe_size_units, 0);
4553 static int find_check_ext_size(struct find_param *param)
4555 struct lov_comp_md_v1 *comp_v1;
4556 struct lov_user_md_v1 *v1;
4559 ret = param->fp_exclude_ext_size ? 1 : -1;
4560 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4561 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4564 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4565 struct lov_comp_md_entry_v1 *ent;
4567 v1 = lov_comp_entry(comp_v1, i);
4569 ent = &comp_v1->lcm_entries[i];
4570 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4573 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4574 param->fp_ext_size_sign,
4575 param->fp_exclude_ext_size,
4576 param->fp_ext_size_units, 0);
4577 /* If any ext_size matches */
4585 static __u32 find_get_stripe_count(struct find_param *param)
4587 struct lov_comp_md_v1 *comp_v1 = NULL;
4588 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4590 __u32 stripe_count = 0;
4592 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4595 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4596 comp_v1 = (struct lov_comp_md_v1 *)v1;
4597 count = comp_v1->lcm_entry_count;
4600 for (i = 0; i < count; i++) {
4602 struct lov_comp_md_entry_v1 *ent;
4604 v1 = lov_comp_entry(comp_v1, i);
4606 ent = &comp_v1->lcm_entries[i];
4607 if (!(ent->lcme_flags & LCME_FL_INIT))
4610 if (ent->lcme_flags & LCME_FL_EXTENSION)
4613 stripe_count = v1->lmm_stripe_count;
4616 return stripe_count;
4619 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4621 static int find_check_layout(struct find_param *param)
4623 struct lov_comp_md_v1 *comp_v1 = NULL;
4624 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4626 bool found = false, valid = false;
4628 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4629 comp_v1 = (struct lov_comp_md_v1 *)v1;
4630 count = comp_v1->lcm_entry_count;
4633 for (i = 0; i < count; i++) {
4635 v1 = lov_comp_entry(comp_v1, i);
4637 /* foreign file have a special magic but no pattern field */
4638 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4641 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4645 if (v1->lmm_pattern & param->fp_layout) {
4654 if ((found && !param->fp_exclude_layout) ||
4655 (!found && param->fp_exclude_layout))
4662 * if no type specified, check/exclude all foreign
4663 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4665 static int find_check_foreign(struct find_param *param)
4667 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4668 struct lov_foreign_md *lfm;
4670 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4671 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4672 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4673 return param->fp_exclude_foreign ? 1 : -1;
4677 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4678 lfm->lfm_type == param->fp_foreign_type)
4679 return param->fp_exclude_foreign ? -1 : 1;
4680 return param->fp_exclude_foreign ? 1 : -1;
4683 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4684 struct lmv_foreign_md *lfm;
4686 lfm = (void *)param->fp_lmv_md;
4687 if (lmv_is_foreign(lfm->lfm_magic)) {
4688 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4689 return param->fp_exclude_foreign ? 1 : -1;
4693 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4694 lfm->lfm_type == param->fp_foreign_type)
4695 return param->fp_exclude_foreign ? -1 : 1;
4696 return param->fp_exclude_foreign ? 1 : -1;
4701 static int find_check_pool(struct find_param *param)
4703 struct lov_comp_md_v1 *comp_v1 = NULL;
4704 struct lov_user_md_v3 *v3 = (void *)¶m->fp_lmd->lmd_lmm;
4708 if (v3->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4709 comp_v1 = (struct lov_comp_md_v1 *)v3;
4710 count = comp_v1->lcm_entry_count;
4711 /* empty requested pool is taken as no pool search */
4712 if (count == 0 && param->fp_poolname[0] == '\0') {
4718 for (i = 0; i < count; i++) {
4719 if (comp_v1 != NULL) {
4720 if (!(comp_v1->lcm_entries[i].lcme_flags &
4724 v3 = (void *)lov_comp_entry(comp_v1, i);
4727 if (v3->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4730 if (((v3->lmm_magic == LOV_USER_MAGIC_V1) &&
4731 (param->fp_poolname[0] == '\0')) ||
4732 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4733 (strncmp(v3->lmm_pool_name,
4734 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4735 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4736 (strcmp(param->fp_poolname, "*") == 0))) {
4743 if ((found && !param->fp_exclude_pool) ||
4744 (!found && param->fp_exclude_pool))
4750 static int find_check_comp_options(struct find_param *param)
4752 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4753 struct lov_user_mds_data *lmd = param->fp_lmd;
4754 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4755 struct lov_comp_md_entry_v1 *entry;
4758 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4761 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4762 comp_v1 = (struct lov_comp_md_v1 *)v1;
4764 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4765 if (forged_v1 == NULL)
4767 comp_v1 = forged_v1;
4768 comp_v1->lcm_entry_count = 1;
4769 entry = &comp_v1->lcm_entries[0];
4770 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4772 entry->lcme_extent.e_start = 0;
4773 entry->lcme_extent.e_end = LUSTRE_EOF;
4776 /* invalid case, don't match for any kind of search. */
4777 if (comp_v1->lcm_entry_count == 0) {
4782 if (param->fp_check_comp_count) {
4783 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4784 param->fp_comp_count,
4785 param->fp_comp_count_sign,
4786 param->fp_exclude_comp_count, 1, 0);
4792 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4793 entry = &comp_v1->lcm_entries[i];
4795 if (param->fp_check_comp_flags) {
4797 if (((param->fp_comp_flags & entry->lcme_flags) !=
4798 param->fp_comp_flags) ||
4799 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4805 if (param->fp_check_comp_start) {
4806 ret = find_value_cmp(entry->lcme_extent.e_start,
4807 param->fp_comp_start,
4808 param->fp_comp_start_sign,
4809 param->fp_exclude_comp_start,
4810 param->fp_comp_start_units, 0);
4815 if (param->fp_check_comp_end) {
4816 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4822 /* the component matches all criteria */
4831 static int find_check_mirror_options(struct find_param *param)
4833 struct lov_comp_md_v1 *comp_v1;
4834 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4837 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4840 comp_v1 = (struct lov_comp_md_v1 *)v1;
4842 if (param->fp_check_mirror_count) {
4843 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4844 param->fp_mirror_count,
4845 param->fp_mirror_count_sign,
4846 param->fp_exclude_mirror_count, 1, 0);
4851 if (param->fp_check_mirror_state) {
4853 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4855 if ((param->fp_mirror_state != 0 &&
4856 file_state != param->fp_mirror_state) ||
4857 file_state == param->fp_mirror_neg_state)
4864 static bool find_check_lmm_info(struct find_param *param)
4866 return param->fp_check_pool || param->fp_check_stripe_count ||
4867 param->fp_check_stripe_size || param->fp_check_layout ||
4868 param->fp_check_comp_count || param->fp_check_comp_end ||
4869 param->fp_check_comp_start || param->fp_check_comp_flags ||
4870 param->fp_check_mirror_count || param->fp_check_foreign ||
4871 param->fp_check_mirror_state || param->fp_check_ext_size ||
4872 param->fp_check_projid;
4876 * Interpret backslash escape sequences and write output into buffer.
4877 * Anything written to the buffer will be null terminated.
4879 * @param[in] seq String being parsed for escape sequence. The leading
4880 * '\' character is not included in this string (only the
4881 * characters after it)
4882 * @param[out] buffer Location where interpreted escape sequence is written
4883 * @param[in] size Size of the available buffer. (Needs to be large enough
4884 * to handle escape sequence output plus null terminator.)
4885 * @param[out] wrote Number of bytes written to the buffer.
4886 * @return Number of characters from input string processed
4887 * as part of the escape sequence (0 for an unrecognized
4890 int printf_format_escape(char *seq, char *buffer, size_t size, int *wrote)
4893 /* For now, only handle single char escape sequences: \n, \t, \\ */
4916 * Interpret formats for timestamps (%a, %A@, etc)
4918 * @param[in] seq String being parsed for timestamp format. The leading
4919 * '%' character is not included in this string
4920 * @param[out] buffer Location where timestamp info is written
4921 * @param[in] size Size of the available buffer.
4922 * @param[out] wrote Number of bytes written to the buffer.
4923 * @return Number of characters from input string processed
4924 * as part of the format (0 for an unknown format)
4927 int printf_format_timestamp(char *seq, char *buffer, size_t size, int *wrote,
4928 struct find_param *param)
4930 struct statx_timestamp ts = { 0, 0 };
4934 char *fmt = "%c"; /* Print in ctime format by default */
4939 ts = param->fp_lmd->lmd_stx.stx_atime;
4943 if (*(seq + 1) == '@') {
4944 ts = param->fp_lmd->lmd_stx.stx_atime;
4950 ts = param->fp_lmd->lmd_stx.stx_ctime;
4954 if (*(seq + 1) == '@') {
4955 ts = param->fp_lmd->lmd_stx.stx_ctime;
4961 ts = param->fp_lmd->lmd_stx.stx_mtime;
4965 if (*(seq + 1) == '@') {
4966 ts = param->fp_lmd->lmd_stx.stx_mtime;
4972 ts = param->fp_lmd->lmd_stx.stx_btime;
4976 if (*(seq + 1) == '@') {
4977 ts = param->fp_lmd->lmd_stx.stx_btime;
4987 /* Found valid format, print to buffer */
4990 *wrote = strftime(buffer, size, fmt, tm);
4997 * Print all ost indices associated with a file layout using a commma separated
4998 * list. For a file with mutliple components, the list of indices for each
4999 * component will be enclosed in brackets.
5001 * @param[out] buffer Location where OST indices are written
5002 * @param[in] size Size of the available buffer.
5003 * @pararm[in] layout Pointer to layout structure for the file
5004 * @return Number of bytes written to output buffer
5006 static int printf_format_ost_indices(char *buffer, size_t size,
5007 struct llapi_layout *layout)
5009 uint64_t count, idx, i;
5010 int err, bytes, wrote = 0;
5012 /* Make sure to start at the first component */
5013 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
5015 llapi_error(LLAPI_MSG_ERROR, err,
5016 "error: layout component iteration failed\n");
5020 err = llapi_layout_stripe_count_get(layout, &count);
5022 llapi_error(LLAPI_MSG_ERROR, err,
5023 "error: cannot get stripe_count\n");
5027 bytes = snprintf(buffer, (size - wrote), "%s", "[");
5032 for (i = 0; i < count; i++) {
5033 err = llapi_layout_ost_index_get(layout, i, &idx);
5035 llapi_error(LLAPI_MSG_ERROR, err,
5036 "error: cannot get OST index\n");
5037 bytes = snprintf(buffer, (size - wrote),
5040 bytes = snprintf(buffer, (size - wrote),
5048 /* Overwrite last comma with closing bracket */
5049 *(buffer - 1) = ']';
5051 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
5052 if (err == 0) /* next component is found */
5055 llapi_error(LLAPI_MSG_ERROR, err,
5056 "error: layout component iteration failed\n");
5057 /* At this point, either got error or reached last component */
5068 * Parse Lustre-specific format sequences of the form %L{x}.
5070 * @param[in] seq String being parsed for format sequence. The leading
5071 * '%' character is not included in this string
5072 * @param[out] buffer Location where interpreted format info is written
5073 * @param[in] size Size of the available buffer.
5074 * @param[out] wrote Number of bytes written to the buffer.
5075 * @param[in] param The find_param structure associated with the file/dir
5076 * @param[in] path Pathname of the current file/dir being handled
5077 * @param[in] projid Project ID associated with the current file/dir
5078 * @param[in] d File descriptor for the directory (or -1 for a
5079 * non-directory file)
5080 * @return Number of characters from input string processed
5081 * as part of the format (0 for an unknown format)
5083 int printf_format_lustre(char *seq, char *buffer, size_t size, int *wrote,
5084 struct find_param *param, char *path, int projid,
5087 struct lmv_user_md *lum;
5088 struct lmv_user_mds_data *objects;
5089 struct llapi_layout *layout = NULL;
5091 unsigned int hash_type;
5092 uint64_t str_cnt, str_size, idx;
5093 char pool_name[LOV_MAXPOOLNAME + 1] = { '\0' };
5095 int rc = 2; /* all current valid sequences are 2 chars */
5098 /* Sanity check. Formats always look like %L{X} */
5099 if (*seq++ != 'L') {
5105 * Some formats like %LF or %LP are handled the same for both files
5106 * and dirs, so handle all of those here.
5110 err = llapi_path2fid(path, &fid);
5112 llapi_error(LLAPI_MSG_ERROR, err,
5113 "error: cannot get fid\n");
5116 *wrote = snprintf(buffer, size, DFID_NOBRACE, PFID(&fid));
5119 *wrote = snprintf(buffer, size, "%d", projid);
5123 /* Other formats for files/dirs need to be handled differently */
5124 if (d == -1) { /* file */
5125 //layout = llapi_layout_get_by_xattr(¶m->fp_lmd->lmd_lmm,
5126 // param->fp_lum_size, 0);
5127 layout = llapi_layout_get_by_path(path, 0);
5128 if (layout == NULL) {
5129 llapi_error(LLAPI_MSG_ERROR, errno,
5130 "error: cannot get file layout\n");
5135 * Set the layout pointer to the last init component
5136 * since that is the component used for most of these
5137 * formats. (This also works for non-composite files)
5139 err = llapi_layout_get_last_init_comp(layout);
5141 llapi_error(LLAPI_MSG_ERROR, err,
5142 "error: cannot get last initialized compomnent\n");
5147 case 'c': /* stripe count */
5148 err = llapi_layout_stripe_count_get(layout, &str_cnt);
5150 llapi_error(LLAPI_MSG_ERROR, err,
5151 "error: cannot get stripe_count\n");
5154 *wrote = snprintf(buffer, size, "%"PRIu64, str_cnt);
5156 case 'h': /* hash info */
5157 /* Not applicable to files. Skip it. */
5159 case 'i': /* starting index */
5160 err = llapi_layout_ost_index_get(layout, 0, &idx);
5162 llapi_error(LLAPI_MSG_ERROR, err,
5163 "error: cannot get OST index of last initialized component\n");
5166 *wrote = snprintf(buffer, size, "%"PRIu64, idx);
5168 case 'o': /* list of object indices */
5169 *wrote = printf_format_ost_indices(buffer, size, layout);
5171 case 'p': /* pool name */
5172 err = llapi_layout_pool_name_get(layout, pool_name,
5175 llapi_error(LLAPI_MSG_ERROR, rc,
5176 "error: cannot get pool name\n");
5179 *wrote = snprintf(buffer, size, "%s", pool_name);
5181 case 'S': /* stripe size */
5182 err = llapi_layout_stripe_size_get(layout, &str_size);
5184 llapi_error(LLAPI_MSG_ERROR, rc,
5185 "error: cannot get stripe_size\n");
5188 *wrote = snprintf(buffer, size, "%"PRIu64, str_size);
5194 } else { /* directory */
5195 lum = (struct lmv_user_md *)param->fp_lmv_md;
5196 objects = lum->lum_objects;
5199 case 'c': /* stripe count */
5200 *wrote = snprintf(buffer, size, "%d",
5201 (int)lum->lum_stripe_count);
5203 case 'h': /* hash info */
5204 hash_type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
5205 if (hash_type < LMV_HASH_TYPE_MAX)
5206 *wrote = snprintf(buffer, size, "%s",
5207 mdt_hash_name[hash_type]);
5209 *wrote = snprintf(buffer, size, "%#x",
5212 case 'i': /* starting index */
5213 *wrote = snprintf(buffer, size, "%d",
5214 lum->lum_stripe_offset);
5216 case 'o': /* list of object indices */
5217 str_cnt = (int) lum->lum_stripe_count;
5218 *wrote = snprintf(buffer, size, "%s", "[");
5222 for (i = 0; i < str_cnt; i++) {
5223 bytes = snprintf(buffer, (size - *wrote),
5224 "%d,", objects[i].lum_mds);
5231 /* Use lum_offset as the only list entry */
5232 bytes = snprintf(buffer, (size - *wrote),
5233 "%d]", lum->lum_stripe_offset);
5236 /* Overwrite last comma with closing bracket */
5237 *(buffer - 1) = ']';
5240 case 'p': /* pool name */
5241 *wrote = snprintf(buffer, size, "%s",
5242 lum->lum_pool_name);
5244 case 'S': /* stripe size */
5245 /* This has no meaning for directories. Skip it. */
5255 llapi_layout_free(layout);
5258 /* output of snprintf was truncated */
5265 * Interpret format specifiers beginning with '%'.
5267 * @param[in] seq String being parsed for format specifier. The leading
5268 * '%' character is not included in this string
5269 * @param[out] buffer Location where formatted info is written
5270 * @param[in] size Size of the available buffer.
5271 * @param[out] wrote Number of bytes written to the buffer.
5272 * @param[in] param The find_param structure associated with the file/dir
5273 * @param[in] path Pathname of the current file/dir being handled
5274 * @param[in] projid Project ID associated with the current file/dir
5275 * @param[in] d File descriptor for the directory (or -1 for a
5276 * non-directory file)
5277 * @return Number of characters from input string processed
5278 * as part of the format (0 for an unknown format)
5280 int printf_format_directive(char *seq, char *buffer, size_t size, int *wrote,
5281 struct find_param *param, char *path, int projid,
5284 __u16 mode = param->fp_lmd->lmd_stx.stx_mode;
5285 uint64_t blocks = param->fp_lmd->lmd_stx.stx_blocks;
5286 int rc = 1; /* most specifiers are single character */
5294 case 'w': case 'W': /* timestamps */
5295 rc = printf_format_timestamp(seq, buffer, size, wrote, param);
5297 case 'b': /* file size (in 512B blocks) */
5298 *wrote = snprintf(buffer, size, "%"PRIu64, blocks);
5300 case 'G': /* GID of owner */
5301 *wrote = snprintf(buffer, size, "%u",
5302 param->fp_lmd->lmd_stx.stx_gid);
5304 case 'k': /* file size (in 1K blocks) */
5305 *wrote = snprintf(buffer, size, "%"PRIu64, (blocks + 1)/2);
5307 case 'L': /* Lustre-specific formats */
5308 rc = printf_format_lustre(seq, buffer, size, wrote, param,
5311 case 'm': /* file mode in octal */
5312 *wrote = snprintf(buffer, size, "%#o", (mode & (~S_IFMT)));
5314 case 'p': /* Path name of file */
5315 *wrote = snprintf(buffer, size, "%s", path);
5317 case 's': /* file size (in bytes) */
5318 *wrote = snprintf(buffer, size, "%"PRIu64,
5319 (uint64_t) param->fp_lmd->lmd_stx.stx_size);
5321 case 'U': /* UID of owner */
5322 *wrote = snprintf(buffer, size, "%u",
5323 param->fp_lmd->lmd_stx.stx_uid);
5325 case 'y': /* file type */
5328 else if (S_ISDIR(mode))
5330 else if (S_ISLNK(mode))
5332 else if (S_ISBLK(mode))
5334 else if (S_ISCHR(mode))
5336 else if (S_ISFIFO(mode))
5338 else if (S_ISSOCK(mode))
5348 default: /* invalid format specifier */
5354 /* output of snprintf was truncated */
5361 * Parse user-supplied string for the -printf option and interpret any
5362 * '%' format specifiers or '\' escape sequences.
5364 * @param[in] param The find_param struct containing the -printf string
5365 * as well as info about the current file/dir that mathced
5366 * the lfs find search criteria
5367 * @param[in] path Path name for current file/dir
5368 * @param[in] projid Project ID associated with current file/dir
5369 * @param[in] d File descriptor for current directory (or -1 for a
5370 * non-directory file)
5372 void printf_format_string(struct find_param *param, char *path,
5375 char output[FORMATTED_BUF_LEN];
5376 char *fmt_char = param->fp_format_printf_str;
5377 char *buff = output;
5383 buff_size = FORMATTED_BUF_LEN;
5385 /* Always leave one free byte in buffer for trailing NUL */
5386 while (*fmt_char && (buff_size > 1)) {
5389 if (*fmt_char == '%') {
5390 rc = printf_format_directive(fmt_char + 1, buff,
5391 buff_size, &written, param,
5393 } else if (*fmt_char == '\\')
5394 rc = printf_format_escape(fmt_char + 1, buff,
5395 buff_size, &written);
5398 /* Either a '\' escape or '%' format was processed.
5399 * Increment pointers accordingly.
5401 fmt_char += (rc + 1);
5403 buff_size -= written;
5405 /* Regular char or invalid escape/format.
5406 * Either way, copy current character.
5408 *buff++ = *fmt_char++;
5413 /* Terminate output buffer and print */
5415 llapi_printf(LLAPI_MSG_NORMAL, "%s", output);
5419 * Get file/directory project id.
5420 * by the open fd resides on.
5421 * Return 0 and project id on success, or -ve errno.
5423 static int fget_projid(int fd, int *projid)
5428 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
5432 *projid = fsx.fsx_projid;
5437 * Check that the file's permissions in *st matches the one in find_param
5439 static int check_file_permissions(const struct find_param *param,
5446 switch (param->fp_perm_sign) {
5447 case LFS_FIND_PERM_EXACT:
5448 decision = (mode == param->fp_perm);
5450 case LFS_FIND_PERM_ALL:
5451 decision = ((mode & param->fp_perm) == param->fp_perm);
5453 case LFS_FIND_PERM_ANY:
5454 decision = ((mode & param->fp_perm) != 0);
5458 if ((param->fp_exclude_perm && decision)
5459 || (!param->fp_exclude_perm && !decision))
5465 static int cb_find_init(char *path, int p, int *dp,
5466 void *data, struct dirent64 *de)
5468 struct find_param *param = (struct find_param *)data;
5469 struct lov_user_mds_data *lmd = param->fp_lmd;
5470 int d = dp == NULL ? -1 : *dp;
5471 int decision = 1; /* 1 is accepted; -1 is rejected. */
5473 int checked_type = 0;
5475 __u32 stripe_count = 0;
5479 bool gather_all = false;
5481 if (p == -1 && d == -1)
5484 /* Reset this value between invocations */
5485 param->fp_get_lmv = 0;
5487 /* Gather all file/dir info, not just what's needed for search params */
5488 if (param->fp_format_printf_str)
5491 /* If a regular expression is presented, make the initial decision */
5492 if (param->fp_pattern != NULL) {
5493 char *fname = strrchr(path, '/');
5495 fname = (fname == NULL ? path : fname + 1);
5496 ret = fnmatch(param->fp_pattern, fname, 0);
5497 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
5498 (ret == 0 && param->fp_exclude_pattern))
5502 /* See if we can check the file type from the dirent. */
5503 if (de != NULL && de->d_type != DT_UNKNOWN) {
5504 if (param->fp_type != 0) {
5507 if (DTTOIF(de->d_type) == param->fp_type) {
5508 if (param->fp_exclude_type)
5511 if (!param->fp_exclude_type)
5515 if ((param->fp_check_mdt_count || param->fp_hash_type ||
5516 param->fp_check_hash_flag) && de->d_type != DT_DIR)
5523 * Request MDS for the stat info if some of these parameters need
5526 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
5527 param->fp_check_uid || param->fp_check_gid ||
5528 param->fp_newerxy || param->fp_btime ||
5529 param->fp_atime || param->fp_mtime || param->fp_ctime ||
5530 param->fp_check_size || param->fp_check_blocks ||
5531 find_check_lmm_info(param) ||
5532 param->fp_check_mdt_count || param->fp_hash_type ||
5533 param->fp_check_hash_flag || param->fp_perm_sign ||
5537 if (param->fp_type != 0 && checked_type == 0)
5540 if (decision == 0) {
5542 (param->fp_check_mdt_count || param->fp_hash_type ||
5543 param->fp_check_hash_flag || param->fp_check_foreign ||
5545 param->fp_get_lmv = 1;
5546 ret = cb_get_dirstripe(path, &d, param);
5548 if (errno == ENODATA) {
5549 /* Fill in struct for unstriped dir */
5551 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
5552 /* Use 0 until we find actual offset */
5553 param->fp_lmv_md->lum_stripe_offset = 0;
5554 param->fp_lmv_md->lum_stripe_count = 0;
5555 param->fp_lmv_md->lum_hash_type = 0;
5557 if (param->fp_check_foreign) {
5558 if (param->fp_exclude_foreign)
5567 if (param->fp_check_mdt_count) {
5568 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5571 decision = find_value_cmp(param->fp_lmv_md->lum_stripe_count,
5572 param->fp_mdt_count,
5573 param->fp_mdt_count_sign,
5574 param->fp_exclude_mdt_count, 1, 0);
5579 if (param->fp_hash_type) {
5581 __u32 type = param->fp_lmv_md->lum_hash_type &
5584 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5587 found = (1 << type) & param->fp_hash_type;
5588 if ((found && param->fp_exclude_hash_type) ||
5589 (!found && !param->fp_exclude_hash_type))
5593 if (param->fp_check_hash_flag) {
5594 __u32 flags = param->fp_lmv_md->lum_hash_type &
5595 ~LMV_HASH_TYPE_MASK;
5597 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5600 if (!(flags & param->fp_hash_inflags) ||
5601 (flags & param->fp_hash_exflags))
5606 param->fp_lmd->lmd_lmm.lmm_magic = 0;
5607 ret = get_lmd_info_fd(path, p, d, param->fp_lmd,
5608 param->fp_lum_size, GET_LMD_INFO);
5609 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
5610 find_check_lmm_info(param)) {
5611 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
5614 * We need to "fake" the "use the default" values
5615 * since the lmm struct is zeroed out at this point.
5617 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5618 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
5620 ostid_set_seq(&lmm->lmm_oi,
5621 FID_SEQ_LOV_DEFAULT);
5622 lmm->lmm_stripe_size = 0;
5623 lmm->lmm_stripe_count = 0;
5624 lmm->lmm_stripe_offset = -1;
5626 if (ret == 0 && (param->fp_mdt_uuid != NULL || gather_all)) {
5628 ret = llapi_file_fget_mdtidx(d,
5629 ¶m->fp_file_mdt_index);
5631 * Make sure lum_stripe_offset matches
5632 * mdt_index even for unstriped directories.
5634 if (ret == 0 && param->fp_get_lmv)
5635 param->fp_lmv_md->lum_stripe_offset =
5636 param->fp_file_mdt_index;
5637 } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
5639 * FIXME: we could get the MDT index from the
5640 * file's FID in lmd->lmd_lmm.lmm_oi without
5641 * opening the file, once we are sure that
5642 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
5643 * That would still be an ioctl() to map the
5644 * FID to the MDT, but not an open RPC.
5646 fd = open(path, O_RDONLY);
5648 ret = llapi_file_fget_mdtidx(fd,
5649 ¶m->fp_file_mdt_index);
5655 * For a special file, we assume it resides on
5656 * the same MDT as the parent directory.
5658 ret = llapi_file_fget_mdtidx(p,
5659 ¶m->fp_file_mdt_index);
5670 stripe_count = find_get_stripe_count(param);
5674 /* Check the file permissions from the stat info */
5675 if (param->fp_perm_sign) {
5676 decision = check_file_permissions(param, lmd->lmd_stx.stx_mode);
5681 if (param->fp_type && !checked_type) {
5682 if ((param->fp_check_mdt_count || param->fp_check_hash_flag ||
5683 param->fp_hash_type) && !S_ISDIR(lmd->lmd_stx.stx_mode))
5686 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
5687 if (param->fp_exclude_type)
5690 if (!param->fp_exclude_type)
5696 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
5697 if (lustre_fs && param->fp_got_uuids &&
5698 param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
5699 lmd->lmd_stx.stx_dev_minor)) {
5700 /* A lustre/lustre mount point is crossed. */
5701 param->fp_got_uuids = 0;
5702 param->fp_obds_printed = 0;
5703 param->fp_mdt_index = OBD_NOT_FOUND;
5704 param->fp_obd_index = OBD_NOT_FOUND;
5707 if (lustre_fs && !param->fp_got_uuids) {
5708 ret = setup_target_indexes((d != -1) ? d : p, path,
5713 param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
5714 lmd->lmd_stx.stx_dev_minor);
5715 } else if (!lustre_fs && param->fp_got_uuids) {
5716 /* A lustre/non-lustre mount point is crossed. */
5717 param->fp_got_uuids = 0;
5718 param->fp_mdt_index = OBD_NOT_FOUND;
5719 param->fp_obd_index = OBD_NOT_FOUND;
5723 if (param->fp_check_foreign) {
5724 decision = find_check_foreign(param);
5729 if (param->fp_check_stripe_size) {
5730 decision = find_check_stripe_size(param);
5735 if (param->fp_check_ext_size) {
5736 decision = find_check_ext_size(param);
5741 if (param->fp_check_stripe_count) {
5742 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
5743 param->fp_stripe_count_sign,
5744 param->fp_exclude_stripe_count, 1, 0);
5749 if (param->fp_check_layout) {
5750 decision = find_check_layout(param);
5755 /* If an OBD UUID is specified but none matches, skip this file. */
5756 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
5757 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
5761 * If an OST or MDT UUID is given, and some OST matches,
5764 if (param->fp_obd_index != OBD_NOT_FOUND ||
5765 param->fp_mdt_index != OBD_NOT_FOUND) {
5766 if (param->fp_obd_uuid) {
5767 if (check_obd_match(param)) {
5769 * If no mdtuuid is given, we are done.
5770 * Otherwise, fall through to the mdtuuid
5773 if (!param->fp_mdt_uuid)
5780 if (param->fp_mdt_uuid) {
5781 if (check_mdt_match(param))
5788 if (param->fp_check_uid) {
5789 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
5790 if (param->fp_exclude_uid)
5793 if (!param->fp_exclude_uid)
5798 if (param->fp_check_gid) {
5799 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
5800 if (param->fp_exclude_gid)
5803 if (!param->fp_exclude_gid)
5808 if (param->fp_check_projid || gather_all) {
5812 fd = open(path, O_RDONLY);
5815 ret = fget_projid(fd, &projid);
5820 if (projid == param->fp_projid) {
5821 if (param->fp_exclude_projid)
5824 if (!param->fp_exclude_projid)
5829 if (param->fp_check_pool) {
5830 decision = find_check_pool(param);
5835 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
5836 param->fp_check_comp_start || param->fp_check_comp_end) {
5837 decision = find_check_comp_options(param);
5842 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
5843 decision = find_check_mirror_options(param);
5848 /* Check the time on mds. */
5850 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
5853 for_mds = lustre_fs ?
5854 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5855 decision = find_time_check(param, for_mds);
5860 if (param->fp_btime) {
5861 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
5866 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
5867 param->fp_btime, param->fp_bsign,
5868 param->fp_exclude_btime,
5869 param->fp_time_margin, 0);
5874 if (param->fp_newerxy) {
5877 for_mds = lustre_fs ?
5878 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5879 decision = find_newerxy_check(param, for_mds, true);
5888 flags = param->fp_lmd->lmd_flags;
5889 if (param->fp_check_size &&
5890 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5891 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5892 !(flags & OBD_MD_FLSIZE ||
5893 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
5896 if (param->fp_check_blocks &&
5897 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5898 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5899 !(flags & OBD_MD_FLBLOCKS ||
5900 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
5904 * If file still fits the request, ask ost for updated info.
5905 * The regular stat is almost of the same speed as some new
5906 * 'glimpse-size-ioctl'.
5908 if (!decision || gather_all) {
5912 * For regular files with the stripe the decision may have not
5913 * been taken yet if *time or size is to be checked.
5915 if (param->fp_obd_index != OBD_NOT_FOUND)
5916 print_failed_tgt(param, path, LL_STATFS_LOV);
5918 if (param->fp_mdt_index != OBD_NOT_FOUND)
5919 print_failed_tgt(param, path, LL_STATFS_LMV);
5922 ret = fstat_f(d, &st);
5923 else if (de != NULL)
5924 ret = fstatat_f(p, de->d_name, &st,
5925 AT_SYMLINK_NOFOLLOW);
5927 ret = lstat_f(path, &st);
5930 if (errno == ENOENT) {
5931 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
5932 "warning: %s: %s does not exist",
5937 llapi_error(LLAPI_MSG_ERROR, ret,
5938 "%s: stat on %s failed",
5944 convert_lmd_statx(param->fp_lmd, &st, true);
5945 /* Check the time on osc. */
5946 decision = find_time_check(param, 0);
5950 if (param->fp_newerxy) {
5951 decision = find_newerxy_check(param, 0, false);
5961 if (param->fp_check_size) {
5962 decision = find_value_cmp(lmd->lmd_stx.stx_size,
5964 param->fp_size_sign,
5965 param->fp_exclude_size,
5966 param->fp_size_units, 0);
5971 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
5972 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
5974 param->fp_blocks_sign,
5975 param->fp_exclude_blocks,
5976 param->fp_blocks_units, 0);
5982 if (param->fp_format_printf_str)
5983 printf_format_string(param, path, projid, d);
5985 llapi_printf(LLAPI_MSG_NORMAL, "%s%c", path,
5986 param->fp_zero_end ? '\0' : '\n');
5990 /* Do not get down anymore? */
5991 if (param->fp_depth == param->fp_max_depth) {
6002 static int cb_migrate_mdt_init(char *path, int p, int *dp,
6003 void *param_data, struct dirent64 *de)
6005 struct find_param *param = (struct find_param *)param_data;
6006 struct lmv_user_md *lmu = param->fp_lmv_md;
6008 char raw[MAX_IOC_BUFLEN] = {'\0'};
6010 struct obd_ioctl_data data = { 0 };
6016 if (p == -1 && dp == NULL)
6022 if (dp != NULL && *dp != -1)
6026 tmp_p = open_parent(path);
6030 llapi_error(LLAPI_MSG_ERROR, ret,
6031 "can not open %s", path);
6036 path_copy = strdup(path);
6037 filename = basename(path_copy);
6039 data.ioc_inlbuf1 = (char *)filename;
6040 data.ioc_inllen1 = strlen(filename) + 1;
6041 data.ioc_inlbuf2 = (char *)lmu;
6042 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
6045 if (param->fp_depth == param->fp_max_depth)
6046 data.ioc_type = MDS_MIGRATE_NSONLY;
6047 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
6049 llapi_error(LLAPI_MSG_ERROR, ret,
6050 "%s: error packing ioctl data", __func__);
6055 ret = ioctl(tmp_p, LL_IOC_MIGRATE, rawbuf);
6057 if (errno == EBUSY && !retry) {
6059 * because migrate may not be able to lock all involved
6060 * objects in order, for some of them it try lock, while
6061 * there may be conflicting COS locks and cause migrate
6062 * fail with EBUSY, hope a sync() could cause
6063 * transaction commit and release these COS locks.
6068 } else if (errno == EALREADY) {
6069 if (param->fp_verbose & VERBOSE_DETAIL)
6070 llapi_printf(LLAPI_MSG_NORMAL,
6071 "%s migrated to MDT%d already\n",
6072 path, lmu->lum_stripe_offset);
6076 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
6080 } else if (param->fp_verbose & VERBOSE_DETAIL) {
6081 llapi_printf(LLAPI_MSG_NORMAL,
6082 "migrate %s to MDT%d stripe count %d\n",
6083 path, lmu->lum_stripe_offset,
6084 lmu->lum_stripe_count);
6088 /* Do not get down anymore? */
6089 if (param->fp_depth == param->fp_max_depth)
6095 * If the directory is being migration, we need
6096 * close the directory after migration,
6097 * so the old directory cache will be cleanup
6098 * on the client side, and re-open to get the
6099 * new directory handle
6101 *dp = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
6104 llapi_error(LLAPI_MSG_ERROR, ret,
6105 "%s: Failed to open '%s'", __func__, path);
6117 /* dir migration finished, shrink its stripes */
6118 static int cb_migrate_mdt_fini(char *path, int p, int *dp, void *data,
6119 struct dirent64 *de)
6121 struct find_param *param = data;
6122 struct lmv_user_md *lmu = param->fp_lmv_md;
6123 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
6126 if (de && de->d_type != DT_DIR)
6131 * close it before setxattr because the latter may destroy the
6132 * original object, and cause close fail.
6140 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
6141 if (ret == -EALREADY)
6144 cb_common_fini(path, p, dp, data, de);
6148 int llapi_migrate_mdt(char *path, struct find_param *param)
6150 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
6154 int llapi_mv(char *path, struct find_param *param)
6156 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
6157 static bool printed;
6160 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
6161 "%s() is deprecated, use llapi_migrate_mdt() instead",
6166 return llapi_migrate_mdt(path, param);
6170 * Check string for escape sequences and print a message to stdout
6171 * if any invalid escapes are found.
6173 * @param[in] c Pointer to character immediately following the
6174 * '\' character indicating the start of an escape
6176 * @return Number of characters examined in the escape sequence
6177 * (regardless of whether the sequence is valid or not).
6179 int validate_printf_esc(char *c)
6181 char *valid_esc = "nt\\";
6184 /* backslash at end of string */
6185 llapi_err_noerrno(LLAPI_MSG_WARN,
6186 "warning: '\\' at end of -printf format string\n");
6190 if (!strchr(valid_esc, *c))
6191 /* Invalid escape character */
6192 llapi_err_noerrno(LLAPI_MSG_WARN,
6193 "warning: unrecognized escape: '\\%c'\n", *c);
6199 * Check string for format directives and print a message to stdout
6200 * if any invalid directives are found.
6202 * @param[in] c Pointer to character immediately following the
6203 * '%' character indicating the start of a format
6205 * @return Number of characters examined in the format directive
6206 * (regardless of whether the directive is valid or not).
6208 int validate_printf_fmt(char *c)
6210 char *valid_fmt_single = "abcGkmpstUwy%";
6211 char *valid_fmt_double = "ACTW";
6212 char *valid_fmt_lustre = "cFhioPpS";
6213 char curr = *c, next;
6216 llapi_err_noerrno(LLAPI_MSG_WARN,
6217 "warning: '%%' at end of -printf format string\n");
6222 if ((next == '\0') || (next == '%') || (next == '\\'))
6223 /* Treat as single char format directive */
6226 /* Check format directives with multiple characters */
6227 if (strchr(valid_fmt_double, curr)) {
6228 /* For now, only valid formats are followed by '@' char */
6230 llapi_err_noerrno(LLAPI_MSG_WARN,
6231 "warning: unrecognized format directive: '%%%c%c'\n",
6236 /* Lustre formats always start with 'L' */
6238 if (!strchr(valid_fmt_lustre, next))
6239 llapi_err_noerrno(LLAPI_MSG_WARN,
6240 "warning: unrecognized format directive: '%%%c%c'\n",
6247 if (!strchr(valid_fmt_single, curr))
6248 llapi_err_noerrno(LLAPI_MSG_WARN,
6249 "warning: unrecognized format directive: '%%%c'\n", curr);
6254 * Validate the user-supplied string for the -printf option and report
6255 * any invalid backslash escape sequences or format directives.
6257 * @param[in] param Structure containing info about invocation of lfs find
6260 void validate_printf_str(struct find_param *param)
6262 char *c = param->fp_format_printf_str;
6268 ret = validate_printf_fmt(++c);
6272 ret = validate_printf_esc(++c);
6282 int llapi_find(char *path, struct find_param *param)
6284 if (param->fp_format_printf_str)
6285 validate_printf_str(param);
6286 return param_callback(path, cb_find_init, cb_common_fini, param);
6290 * Get MDT number that the file/directory inode referenced
6291 * by the open fd resides on.
6292 * Return 0 and mdtidx on success, or -ve errno.
6294 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
6296 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
6301 static int cb_get_mdt_index(char *path, int p, int *dp, void *data,
6302 struct dirent64 *de)
6304 struct find_param *param = (struct find_param *)data;
6305 int d = dp == NULL ? -1 : *dp;
6309 if (p == -1 && d == -1)
6313 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6314 } else /* if (p != -1) */ {
6317 fd = open(path, O_RDONLY | O_NOCTTY);
6319 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
6327 if (ret == -ENODATA) {
6328 if (!param->fp_obd_uuid)
6329 llapi_printf(LLAPI_MSG_NORMAL,
6330 "'%s' has no stripe info\n", path);
6332 } else if (ret == -ENOENT) {
6333 llapi_error(LLAPI_MSG_WARN, ret,
6334 "warning: %s: '%s' does not exist",
6337 } else if (ret == -ENOTTY) {
6338 llapi_error(LLAPI_MSG_ERROR, ret,
6339 "%s: '%s' not on a Lustre fs",
6342 llapi_error(LLAPI_MSG_ERROR, ret,
6343 "error: %s: '%s' failed get_mdtidx",
6349 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
6350 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
6352 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
6356 /* Do not go down anymore? */
6357 if (param->fp_depth == param->fp_max_depth)
6365 static int cb_getstripe(char *path, int p, int *dp, void *data,
6366 struct dirent64 *de)
6368 struct find_param *param = (struct find_param *)data;
6369 int d = dp == NULL ? -1 : *dp;
6372 if (p == -1 && d == -1)
6375 if (param->fp_obd_uuid) {
6376 param->fp_quiet = 1;
6377 ret = setup_obd_uuid(d != -1 ? d : p, path, param);
6382 if (d != -1 && (param->fp_get_lmv || param->fp_get_default_lmv))
6383 ret = cb_get_dirstripe(path, &d, param);
6385 (p != -1 && !param->fp_get_lmv && !param->fp_get_default_lmv))
6386 ret = get_lmd_info_fd(path, p, d, ¶m->fp_lmd->lmd_lmm,
6387 param->fp_lum_size, GET_LMD_STRIPE);
6388 else if (d == -1 && (param->fp_get_lmv || param->fp_get_default_lmv)) {
6389 /* in case of a dangling or valid faked symlink dir, opendir()
6390 * should have return either EINVAL or ENOENT, so let's try
6391 * to get LMV just in case, and by opening it as a file but
6392 * with O_NOFOLLOW ...
6394 int fd = open(path, O_RDONLY | O_NOFOLLOW);
6398 ret = cb_get_dirstripe(path, &fd, param);
6400 llapi_lov_dump_user_lmm(param, path, LDF_IS_DIR);
6407 if (errno == ENODATA && d != -1) {
6409 * We need to "fake" the "use the default" values
6410 * since the lmm struct is zeroed out at this point.
6411 * The magic needs to be set in order to satisfy
6412 * a check later on in the code path.
6413 * The object_seq needs to be set for the "(Default)"
6414 * prefix to be displayed.
6416 if (param->fp_get_default_lmv) {
6417 struct lmv_user_md *lum = param->fp_lmv_md;
6419 lum->lum_magic = LMV_USER_MAGIC;
6420 lum->lum_stripe_count = 0;
6421 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
6423 } else if (param->fp_get_lmv) {
6424 struct lmv_user_md *lum = param->fp_lmv_md;
6427 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6430 lum->lum_magic = LMV_MAGIC_V1;
6431 lum->lum_stripe_count = 0;
6432 lum->lum_stripe_offset = mdtidx;
6435 struct lov_user_md *lmm =
6436 ¶m->fp_lmd->lmd_lmm;
6438 lmm->lmm_magic = LOV_USER_MAGIC_V1;
6440 ostid_set_seq(&lmm->lmm_oi,
6441 FID_SEQ_LOV_DEFAULT);
6442 lmm->lmm_stripe_count = 0;
6443 lmm->lmm_stripe_size = 0;
6444 lmm->lmm_stripe_offset = -1;
6447 } else if (errno == ENODATA && p != -1) {
6448 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
6449 llapi_printf(LLAPI_MSG_NORMAL,
6450 "%s has no stripe info\n", path);
6452 } else if (errno == ENOENT) {
6453 llapi_error(LLAPI_MSG_WARN, -ENOENT,
6454 "warning: %s: %s does not exist",
6457 } else if (errno == ENOTTY) {
6459 llapi_error(LLAPI_MSG_ERROR, ret,
6460 "%s: '%s' not on a Lustre fs?",
6465 llapi_error(LLAPI_MSG_ERROR, ret,
6466 "error: %s: %s failed for %s",
6468 "LL_IOC_LOV_GETSTRIPE" :
6469 "IOC_MDC_GETFILESTRIPE", path);
6476 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
6477 llapi_lov_dump_user_lmm(param, path, d != -1 ? LDF_IS_DIR : 0);
6480 /* Do not get down anymore? */
6481 if (param->fp_depth == param->fp_max_depth)
6489 int llapi_getstripe(char *path, struct find_param *param)
6491 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
6492 cb_get_mdt_index : cb_getstripe,
6493 cb_common_fini, param);
6496 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
6497 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6499 char raw[MAX_IOC_BUFLEN] = {'\0'};
6501 struct obd_ioctl_data data = { 0 };
6504 data.ioc_inlbuf1 = (char *)&type;
6505 data.ioc_inllen1 = sizeof(__u32);
6506 data.ioc_inlbuf2 = (char *)&index;
6507 data.ioc_inllen2 = sizeof(__u32);
6508 data.ioc_pbuf1 = (char *)stat_buf;
6509 data.ioc_plen1 = sizeof(struct obd_statfs);
6510 data.ioc_pbuf2 = (char *)uuid_buf;
6511 data.ioc_plen2 = sizeof(struct obd_uuid);
6513 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
6515 llapi_error(LLAPI_MSG_ERROR, rc,
6516 "%s: error packing ioctl data", __func__);
6520 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
6522 return rc < 0 ? -errno : 0;
6525 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
6526 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6531 fd = open(path, O_RDONLY);
6534 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
6537 * If we can't even open a file on the filesystem (e.g. with
6538 * -ESHUTDOWN), force caller to exit or it will loop forever.
6543 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
6550 #define MAX_STRING_SIZE 128
6552 int llapi_ping(char *obd_type, char *obd_name)
6554 int flags = O_RDONLY;
6555 char buf[1] = { 0 };
6559 rc = cfs_get_param_paths(&path, "%s/%s/ping",
6560 obd_type, obd_name);
6564 fd = open(path.gl_pathv[0], flags);
6566 if (errno == EACCES && flags == O_RDONLY) {
6571 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
6576 if (flags == O_RDONLY)
6577 rc = read(fd, buf, sizeof(buf));
6579 rc = write(fd, buf, sizeof(buf));
6587 cfs_free_param_data(&path);
6591 int llapi_target_iterate(int type_num, char **obd_type,
6592 void *args, llapi_cb_t cb)
6598 for (i = 0; i < type_num; i++) {
6601 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
6605 for (j = 0; j < param.gl_pathc; j++) {
6606 char obd_uuid[UUID_MAX + 1];
6610 fp = fopen(param.gl_pathv[j], "r");
6613 llapi_error(LLAPI_MSG_ERROR, rc,
6614 "error: opening '%s'",
6619 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
6621 llapi_error(LLAPI_MSG_ERROR, rc,
6622 "error: reading '%s'",
6627 /* Extract the obd_name from the sysfs path.
6628 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
6630 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
6636 /* skip /fs/lustre/'obd_type'/ */
6637 obd_name += strlen(obd_type[i]) + 12;
6638 /* chop off after obd_name */
6639 ptr = strrchr(obd_name, '/');
6643 cb(obd_type[i], obd_name, obd_uuid, args);
6652 cfs_free_param_data(¶m);
6656 static void do_target_check(char *obd_type_name, char *obd_name,
6657 char *obd_uuid, void *args)
6661 rc = llapi_ping(obd_type_name, obd_name);
6663 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
6665 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
6667 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
6670 int llapi_target_check(int type_num, char **obd_type, char *dir)
6672 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
6675 #undef MAX_STRING_SIZE
6677 /* Is this a lustre fs? */
6678 int llapi_is_lustre_mnttype(const char *type)
6680 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
6683 /* Is this a lustre client fs? */
6684 int llapi_is_lustre_mnt(struct mntent *mnt)
6686 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
6687 strstr(mnt->mnt_fsname, ":/") != NULL);
6690 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
6692 char fsname[PATH_MAX + 1];
6696 rc = llapi_search_fsname(mnt, fsname);
6700 root = open(mnt, O_RDONLY | O_DIRECTORY);
6703 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
6707 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
6710 if (rc == -ENOENT && LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd))
6711 llapi_error(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, rc,
6712 "Cannot find pool '%s'", qctl->qc_poolname);
6718 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
6723 root = open(mnt, O_RDONLY | O_DIRECTORY);
6726 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
6730 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
6733 llapi_error(LLAPI_MSG_ERROR, rc,
6734 "ioctl on %s for getting connect flags failed", mnt);
6741 * Flush cached pages from all clients.
6743 * \param fd File descriptor
6747 int llapi_file_flush(int fd)
6751 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);