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) {
1933 /* open() may not fetch LOOKUP lock, statx() to ensure dir depth
1936 statx(*d, "", AT_EMPTY_PATH, STATX_MODE, &stx);
1942 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1944 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1947 ret = ioctl(*d, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1949 /* if ENOTTY likely to be a fake symlink, so try again after
1950 * new open() with O_NOFOLLOW, but only once to prevent any
1951 * loop like for the path of a file/dir not on Lustre !!
1953 if (ret < 0 && errno == ENOTTY && !did_nofollow) {
1956 did_nofollow = true;
1957 fd = open(path, O_RDONLY | O_NOFOLLOW);
1959 /* restore original errno */
1964 /* close original fd and set new */
1967 ret2 = ioctl(fd, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1968 if (ret2 < 0 && errno != E2BIG) {
1969 /* restore original errno */
1973 /* LMV is ok or need to handle E2BIG case now */
1977 if (errno == E2BIG && ret != 0) {
1981 /* if foreign LMV case, fake stripes number */
1982 if (lmv_is_foreign(param->fp_lmv_md->lum_magic)) {
1983 struct lmv_foreign_md *lfm;
1985 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1986 if (lfm->lfm_length < XATTR_SIZE_MAX -
1987 offsetof(typeof(*lfm), lfm_value)) {
1988 uint32_t size = lfm->lfm_length +
1989 offsetof(typeof(*lfm), lfm_value);
1991 stripe_count = lmv_foreign_to_md_stripes(size);
1993 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1994 "error: invalid %d foreign size returned from ioctl",
1999 stripe_count = param->fp_lmv_md->lum_stripe_count;
2001 if (stripe_count <= param->fp_lmv_stripe_count)
2004 free(param->fp_lmv_md);
2005 param->fp_lmv_stripe_count = stripe_count;
2006 lmv_size = lmv_user_md_size(stripe_count,
2007 LMV_USER_MAGIC_SPECIFIC);
2008 param->fp_lmv_md = malloc(lmv_size);
2009 if (param->fp_lmv_md == NULL) {
2010 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
2011 "error: allocation of %d bytes for ioctl",
2012 lmv_user_md_size(param->fp_lmv_stripe_count,
2013 LMV_USER_MAGIC_SPECIFIC));
2022 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
2025 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
2026 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
2027 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
2028 lmd_v2->lmd_stx.stx_uid = st->st_uid;
2029 lmd_v2->lmd_stx.stx_gid = st->st_gid;
2030 lmd_v2->lmd_stx.stx_mode = st->st_mode;
2031 lmd_v2->lmd_stx.stx_ino = st->st_ino;
2032 lmd_v2->lmd_stx.stx_size = st->st_size;
2033 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
2034 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
2035 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
2036 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
2037 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
2038 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
2039 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
2040 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
2041 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
2043 lmd_v2->lmd_flags = 0;
2045 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
2047 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
2048 if (lmd_v2->lmd_stx.stx_size)
2049 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
2050 if (lmd_v2->lmd_stx.stx_blocks)
2051 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
2053 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
2054 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
2055 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
2060 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
2062 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
2063 struct lov_user_mds_data *lmd_v2 = lmdbuf;
2067 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
2071 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
2074 st = lmd_v1->lmd_st;
2075 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
2076 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
2077 convert_lmd_statx(lmd_v2, &st, false);
2078 lmd_v2->lmd_lmmsize = 0;
2079 lmd_v2->lmd_padding = 0;
2084 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
2085 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
2087 struct lov_user_mds_data *lmd = lmdbuf;
2088 static bool use_old_ioctl;
2092 if (parent_fd < 0 && dir_fd < 0)
2094 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
2099 * LL_IOC_MDC_GETINFO operates on the current directory inode
2100 * and returns struct lov_user_mds_data, while
2101 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
2103 if (type == GET_LMD_INFO)
2104 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_V1 :
2105 LL_IOC_MDC_GETINFO_V2;
2107 cmd = LL_IOC_LOV_GETSTRIPE;
2110 ret = ioctl(dir_fd, cmd, lmdbuf);
2111 if (ret < 0 && errno == ENOTTY &&
2112 cmd == LL_IOC_MDC_GETINFO_V2) {
2113 cmd = LL_IOC_MDC_GETINFO_V1;
2114 use_old_ioctl = true;
2118 if (cmd == LL_IOC_MDC_GETINFO_V1 && !ret)
2119 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2121 if (ret < 0 && errno == ENOTTY && type == GET_LMD_STRIPE) {
2124 /* retry ioctl() after new open() with O_NOFOLLOW
2125 * just in case it could be a fake symlink
2126 * need using a new open() as dir_fd is being closed
2130 dir_fd2 = open(path, O_RDONLY | O_NDELAY | O_NOFOLLOW);
2132 /* return original error */
2135 ret = ioctl(dir_fd2, cmd, lmdbuf);
2136 /* pass new errno or success back to caller */
2142 } else if (parent_fd >= 0) {
2143 const char *fname = strrchr(path, '/');
2146 * IOC_MDC_GETFILEINFO takes as input the filename (relative to
2147 * the parent directory) and returns struct lov_user_mds_data,
2148 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2150 * This avoids opening, locking, and closing each file on the
2151 * client if that is not needed. Multiple of these ioctl() can
2152 * be done on the parent dir with a single open for all
2153 * files in that directory, and it also doesn't pollute the
2154 * client dcache with millions of dentries when traversing
2155 * a large filesystem.
2157 fname = (fname == NULL ? path : fname + 1);
2159 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2162 else if (ret >= lmdlen || ret++ == 0)
2165 if (type == GET_LMD_INFO)
2166 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_V1 :
2167 IOC_MDC_GETFILEINFO_V2;
2169 cmd = IOC_MDC_GETFILESTRIPE;
2172 ret = ioctl(parent_fd, cmd, lmdbuf);
2173 if (ret < 0 && errno == ENOTTY &&
2174 cmd == IOC_MDC_GETFILEINFO_V2) {
2175 cmd = IOC_MDC_GETFILEINFO_V1;
2176 use_old_ioctl = true;
2177 goto retry_getfileinfo;
2180 if (cmd == IOC_MDC_GETFILEINFO_V1 && !ret)
2181 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2185 if (ret && type == GET_LMD_INFO) {
2186 if (errno == ENOTTY) {
2190 * ioctl is not supported, it is not a lustre fs.
2191 * Do the regular lstat(2) instead.
2193 ret = lstat_f(path, &st);
2196 llapi_error(LLAPI_MSG_ERROR, ret,
2197 "error: %s: lstat failed for %s",
2201 convert_lmd_statx(lmd, &st, true);
2203 * It may be wrong to set use_old_ioctl with true as
2204 * the file is not a lustre fs. So reset it with false
2207 use_old_ioctl = false;
2208 } else if (errno == ENOENT) {
2210 llapi_error(LLAPI_MSG_WARN, ret,
2211 "warning: %s does not exist", path);
2212 } else if (errno != EISDIR && errno != ENODATA) {
2214 llapi_error(LLAPI_MSG_ERROR, ret,
2215 "%s ioctl failed for %s.",
2216 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2217 "IOC_MDC_GETFILEINFO", path);
2224 static int llapi_semantic_traverse(char *path, int size, int parent,
2225 semantic_func_t sem_init,
2226 semantic_func_t sem_fini, void *data,
2227 struct dirent64 *de)
2229 struct find_param *param = (struct find_param *)data;
2230 struct dirent64 *dent;
2231 int len, ret, d, p = -1;
2237 d = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
2238 /* if an invalid fake dir symlink, opendir() will return EINVAL
2239 * instead of ENOTDIR. If a valid but dangling faked or real file/dir
2240 * symlink ENOENT will be returned. For a valid/resolved fake or real
2241 * file symlink ENOTDIR will be returned as for a regular file.
2242 * opendir() will be successful for a valid and resolved fake or real
2243 * dir simlink or a regular dir.
2245 if (d == -1 && errno != ENOTDIR && errno != EINVAL && errno != ENOENT) {
2247 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2250 } else if (d == -1) {
2251 if (errno == ENOENT || errno == EINVAL) {
2252 int old_errno = errno;
2254 /* try to open with O_NOFOLLOW this will help
2255 * differentiate fake vs real symlinks
2256 * it is ok to not use O_DIRECTORY with O_RDONLY
2257 * and it will prevent the need to deal with ENOTDIR
2258 * error, instead of ELOOP, being returned by recent
2259 * kernels for real symlinks
2261 d = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2262 /* if a dangling real symlink should return ELOOP, or
2263 * again ENOENT if really non-existing path, or E...??
2264 * So return original error. If success or ENOTDIR, path
2265 * is likely to be a fake dir/file symlink, so continue
2275 if (parent == -1 && d == -1) {
2276 /* Open the parent dir. */
2277 p = open_parent(path);
2283 } else { /* d != -1 */
2286 /* try to reopen dir with O_NOFOLLOW just in case of a foreign
2289 d2 = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2294 /* continue with d */
2300 ret = sem_init(path, (parent != -1) ? parent : p, &d, data, de);
2310 /* ENOTDIR if fake symlink, do not consider it as an error */
2311 if (errno != ENOTDIR)
2312 llapi_error(LLAPI_MSG_ERROR, errno,
2313 "fdopendir() failed");
2320 while ((dent = readdir64(dir)) != NULL) {
2323 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2327 if ((len + dent->d_reclen + 2) > size) {
2328 llapi_err_noerrno(LLAPI_MSG_ERROR,
2329 "error: %s: string buffer too small for %s",
2334 strcat(path, dent->d_name);
2336 if (dent->d_type == DT_UNKNOWN) {
2337 struct lov_user_mds_data *lmd = param->fp_lmd;
2339 rc = get_lmd_info_fd(path, d, -1, param->fp_lmd,
2340 param->fp_lum_size, GET_LMD_INFO);
2342 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2349 switch (dent->d_type) {
2351 llapi_err_noerrno(LLAPI_MSG_ERROR,
2352 "error: %s: '%s' is UNKNOWN type %d",
2353 __func__, dent->d_name, dent->d_type);
2356 rc = llapi_semantic_traverse(path, size, d, sem_init,
2357 sem_fini, data, dent);
2358 if (rc != 0 && ret == 0)
2364 rc = sem_init(path, d, NULL, data, dent);
2365 if (rc < 0 && ret == 0) {
2370 if (sem_fini && rc == 0)
2371 sem_fini(path, d, NULL, data, dent);
2379 sem_fini(path, parent, &d, data, de);
2392 static int param_callback(char *path, semantic_func_t sem_init,
2393 semantic_func_t sem_fini, struct find_param *param)
2395 int ret, len = strlen(path);
2398 if (len > PATH_MAX) {
2400 llapi_error(LLAPI_MSG_ERROR, ret,
2401 "Path name '%s' is too long", path);
2405 buf = (char *)malloc(2 * PATH_MAX);
2409 snprintf(buf, PATH_MAX + 1, "%s", path);
2410 ret = common_param_init(param, buf);
2414 param->fp_depth = 0;
2416 ret = llapi_semantic_traverse(buf, 2 * PATH_MAX, -1, sem_init,
2417 sem_fini, param, NULL);
2419 find_param_fini(param);
2421 return ret < 0 ? ret : 0;
2424 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2428 rc = ioctl(fd, OBD_IOC_GETDTNAME, lov_name);
2429 if (rc && errno == ENOTTY)
2430 rc = ioctl(fd, OBD_IOC_GETNAME_OLD, lov_name);
2433 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2439 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2443 rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2446 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2452 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2456 /* do not follow faked symlinks */
2457 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
2459 /* real symlink should have failed with ELOOP so retry without
2460 * O_NOFOLLOW just in case
2462 fd = open(path, O_RDONLY | O_NONBLOCK);
2465 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'",
2471 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2477 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2481 fd = open(path, O_RDONLY | O_NONBLOCK);
2484 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2488 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2500 * If uuidp is NULL, return the number of available obd uuids.
2501 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2502 * there are more OSTs than allocated to uuidp, then an error is returned with
2503 * the ost_count set to number of available obd uuids.
2505 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2506 int *ost_count, enum tgt_type type)
2508 char buf[PATH_MAX], format[32];
2509 int rc = 0, index = 0;
2510 struct obd_uuid name;
2514 /* Get the lov name */
2515 if (type == LOV_TYPE)
2516 rc = llapi_file_fget_lov_uuid(fd, &name);
2518 rc = llapi_file_fget_lmv_uuid(fd, &name);
2522 /* Now get the ost uuids */
2523 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2524 FILTER_BY_EXACT, "target_obd", ¶m);
2528 fp = fopen(param.gl_pathv[0], "r");
2531 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2536 snprintf(format, sizeof(format),
2537 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2538 while (fgets(buf, sizeof(buf), fp) != NULL) {
2539 if (uuidp && (index < *ost_count)) {
2540 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2548 if (uuidp && (index > *ost_count))
2553 cfs_free_param_data(¶m);
2557 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2559 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2562 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2567 root = open(mnt, O_RDONLY | O_DIRECTORY);
2570 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2575 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2584 * Check if user specified value matches a real uuid. Ignore _UUID,
2585 * -osc-4ba41334, other trailing gunk in comparison.
2586 * @param real_uuid ends in "_UUID"
2587 * @param search_uuid may or may not end in "_UUID"
2589 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2591 int cmplen = strlen(real_uuid);
2592 int searchlen = strlen(search_uuid);
2594 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2596 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2600 * The UUIDs may legitimately be different lengths, if
2601 * the system was upgraded from an older version.
2603 if (cmplen != searchlen)
2606 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2610 * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2611 * returned in param->fp_obd_index
2613 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2615 struct obd_uuid obd_uuid;
2622 if (param->fp_got_uuids)
2625 /* Get the lov/lmv name */
2626 if (param->fp_get_lmv)
2627 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2629 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2631 if (rc != -ENOTTY) {
2632 llapi_error(LLAPI_MSG_ERROR, rc,
2633 "error: can't get %s name: %s",
2634 param->fp_get_lmv ? "lmv" : "lov",
2642 param->fp_got_uuids = 1;
2644 /* Now get the ost uuids */
2645 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2646 obd_uuid.uuid, FILTER_BY_EXACT,
2647 "target_obd", ¶m_data);
2651 fp = fopen(param_data.gl_pathv[0], "r");
2654 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2655 param_data.gl_pathv[0]);
2659 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2660 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2661 param->fp_get_lmv ? "MDTS" : "OBDS");
2663 snprintf(format, sizeof(format),
2664 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2665 while (fgets(buf, sizeof(buf), fp) != NULL) {
2668 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2671 if (param->fp_obd_uuid) {
2672 if (llapi_uuid_match(obd_uuid.uuid,
2673 param->fp_obd_uuid->uuid)) {
2674 param->fp_obd_index = index;
2677 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2678 /* Print everything */
2679 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2682 param->fp_obds_printed = 1;
2686 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2687 llapi_err_noerrno(LLAPI_MSG_ERROR,
2688 "error: %s: unknown obduuid: %s",
2689 __func__, param->fp_obd_uuid->uuid);
2693 cfs_free_param_data(¶m_data);
2698 * In this case, param->fp_obd_uuid will be an array of obduuids and
2699 * obd index for all these obduuids will be returned in
2700 * param->fp_obd_indexes
2702 static int setup_indexes(int d, char *path, struct obd_uuid *obduuids,
2703 int num_obds, int **obdindexes, int *obdindex,
2706 int ret, obdcount, obd_valid = 0, obdnum;
2708 struct obd_uuid *uuids = NULL;
2712 if (type == LOV_TYPE)
2713 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2715 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2719 obdcount = atoi(buf);
2720 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2725 ret = llapi_get_target_uuids(d, uuids, &obdcount, type);
2727 if (ret == -EOVERFLOW) {
2728 struct obd_uuid *uuids_temp;
2730 uuids_temp = realloc(uuids, obdcount *
2731 sizeof(struct obd_uuid));
2732 if (uuids_temp != NULL) {
2734 goto retry_get_uuids;
2739 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2743 indexes = malloc(num_obds * sizeof(*obdindex));
2744 if (indexes == NULL) {
2749 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2752 /* The user may have specified a simple index */
2753 i = strtol(obduuids[obdnum].uuid, &end, 0);
2754 if (end && *end == '\0' && i < obdcount) {
2755 indexes[obdnum] = i;
2758 for (i = 0; i < obdcount; i++) {
2759 if (llapi_uuid_match(uuids[i].uuid,
2760 obduuids[obdnum].uuid)) {
2761 indexes[obdnum] = i;
2767 if (i >= obdcount) {
2768 indexes[obdnum] = OBD_NOT_FOUND;
2769 llapi_err_noerrno(LLAPI_MSG_ERROR,
2770 "invalid obduuid '%s'",
2771 obduuids[obdnum].uuid);
2777 *obdindex = OBD_NOT_FOUND;
2779 *obdindex = obd_valid;
2781 *obdindexes = indexes;
2789 static int setup_target_indexes(int d, char *path, struct find_param *param)
2793 if (param->fp_mdt_uuid) {
2794 ret = setup_indexes(d, path, param->fp_mdt_uuid,
2796 ¶m->fp_mdt_indexes,
2797 ¶m->fp_mdt_index, LMV_TYPE);
2802 if (param->fp_obd_uuid) {
2803 ret = setup_indexes(d, path, param->fp_obd_uuid,
2805 ¶m->fp_obd_indexes,
2806 ¶m->fp_obd_index, LOV_TYPE);
2811 param->fp_got_uuids = 1;
2816 int llapi_ostlist(char *path, struct find_param *param)
2821 fd = open(path, O_RDONLY | O_DIRECTORY);
2825 ret = setup_obd_uuid(fd, path, param);
2832 * Tries to determine the default stripe attributes for a given filesystem. The
2833 * filesystem to check should be specified by fsname, or will be determined
2836 static int sattr_get_defaults(const char *const fsname,
2837 unsigned int *scount,
2838 unsigned int *ssize,
2839 unsigned int *soffset)
2845 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2846 "stripecount", val, sizeof(val));
2849 *scount = atoi(val);
2853 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2854 "stripesize", val, sizeof(val));
2861 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2862 "stripeoffset", val, sizeof(val));
2865 *soffset = atoi(val);
2872 * Tries to gather the default stripe attributes for a given filesystem. If
2873 * the attributes can be determined, they are cached for easy retreival the
2874 * next time they are needed. Only a single filesystem's attributes are
2877 int sattr_cache_get_defaults(const char *const fsname,
2878 const char *const pathname, unsigned int *scount,
2879 unsigned int *ssize, unsigned int *soffset)
2882 char fsname[PATH_MAX + 1];
2883 unsigned int stripecount;
2884 unsigned int stripesize;
2885 unsigned int stripeoffset;
2891 char fsname_buf[PATH_MAX + 1];
2892 unsigned int tmp[3];
2894 if (fsname == NULL) {
2895 rc = llapi_search_fsname(pathname, fsname_buf);
2899 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2902 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2904 * Ensure all 3 sattrs (count, size, and offset) are
2905 * successfully retrieved and stored in tmp before writing to
2908 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2912 cache.stripecount = tmp[0];
2913 cache.stripesize = tmp[1];
2914 cache.stripeoffset = tmp[2];
2915 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2919 *scount = cache.stripecount;
2921 *ssize = cache.stripesize;
2923 *soffset = cache.stripeoffset;
2928 static char *layout2name(__u32 layout_pattern)
2930 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2932 else if (layout_pattern == LOV_PATTERN_MDT)
2934 else if (layout_pattern == LOV_PATTERN_RAID0)
2936 else if (layout_pattern ==
2937 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2938 return "raid0,overstriped";
2943 enum lov_dump_flags {
2944 LDF_IS_DIR = 0x0001,
2945 LDF_IS_RAW = 0x0002,
2946 LDF_INDENT = 0x0004,
2947 LDF_SKIP_OBJS = 0x0008,
2949 LDF_EXTENSION = 0x0020,
2952 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2953 struct lov_user_ost_data_v1 *objects,
2954 enum llapi_layout_verbose verbose,
2955 int depth, char *pool_name,
2956 enum lov_dump_flags flags)
2958 bool is_dir = flags & LDF_IS_DIR;
2959 bool is_raw = flags & LDF_IS_RAW;
2960 bool indent = flags & LDF_INDENT;
2961 bool yaml = flags & LDF_YAML;
2962 bool skip_objs = flags & LDF_SKIP_OBJS;
2963 bool extension = flags & LDF_EXTENSION;
2964 char *prefix = is_dir ? "" : "lmm_";
2965 char *separator = "";
2966 char *space = indent ? " " : "";
2969 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2970 lmm_oi_set_seq(&lum->lmm_oi, 0);
2971 if (!indent && (verbose & VERBOSE_DETAIL))
2972 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2975 if (!yaml && !indent && depth && path &&
2976 ((verbose != VERBOSE_OBJID) || !is_dir))
2977 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2979 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2980 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2981 space, prefix, lum->lmm_magic);
2982 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2984 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2985 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2987 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2990 if (verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) {
2995 if (verbose & ~VERBOSE_DFID)
2996 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
3000 struct lu_fid dir_fid;
3002 rc = llapi_path2fid(path, &dir_fid);
3004 llapi_error(LLAPI_MSG_ERROR, rc,
3005 "Cannot determine directory fid.");
3007 seq = dir_fid.f_seq;
3008 oid = dir_fid.f_oid;
3009 ver = dir_fid.f_ver;
3012 * This needs a bit of hand-holding since old 1.x
3013 * lmm_oi have { oi.oi_id = mds_inum, oi.oi_seq = 0 }
3014 * and 2.x lmm_oi have { oi.oi_id = mds_oid,
3015 * oi.oi_seq = mds_seq } instead of a real FID.
3016 * Ideally the 2.x code would have stored this like a
3017 * FID with { oi_id = mds_seq, oi_seq = mds_oid } so
3018 * the ostid union lu_fid { f_seq = mds_seq,
3019 * f_oid = mds_oid } worked properly (especially since
3020 * IGIF FIDs use mds_inum as the FID SEQ), but
3021 * unfortunately that didn't happen.
3023 * Print it to look like an IGIF FID, even though the
3024 * fields are reversed on disk, so that it makes sense
3027 * Don't use ostid_id() and ostid_seq(), since they
3028 * assume the oi_fid fields are in the right order.
3029 * This is why there are separate lmm_oi_seq() and
3030 * lmm_oi_id() routines for this.
3032 * For newer layout types hopefully this will be a
3035 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
3036 lmm_oi_id(&lum->lmm_oi) :
3037 lmm_oi_seq(&lum->lmm_oi);
3038 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
3039 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
3040 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
3044 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
3045 (unsigned long long)seq, oid, ver);
3047 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
3048 (unsigned long long)seq, oid, ver);
3051 if (verbose & VERBOSE_STRIPE_COUNT) {
3052 if (verbose & ~VERBOSE_STRIPE_COUNT)
3053 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
3056 if (!is_raw && lum->lmm_stripe_count == 0 &&
3057 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
3058 unsigned int scount;
3060 rc = sattr_cache_get_defaults(NULL, path,
3064 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3067 llapi_error(LLAPI_MSG_ERROR, rc,
3068 "Cannot determine default stripe count.");
3070 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3072 (__s16)lum->lmm_stripe_count);
3075 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
3077 (__s16)lum->lmm_stripe_count);
3079 if (!yaml && is_dir)
3085 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
3086 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
3087 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3088 if (verbose & ~VERBOSE_EXT_SIZE && extension)
3089 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
3091 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
3092 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
3094 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
3097 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
3100 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
3102 llapi_error(LLAPI_MSG_ERROR, rc,
3103 "Cannot determine default stripe size.");
3105 /* Extension size is in KiB */
3106 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3108 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
3109 (unsigned long long)lum->lmm_stripe_size);
3111 if (!yaml && is_dir)
3117 if ((verbose & VERBOSE_PATTERN)) {
3118 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3119 if (verbose & ~VERBOSE_PATTERN)
3120 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
3122 if (lov_pattern_supported(lum->lmm_pattern))
3123 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3124 layout2name(lum->lmm_pattern));
3126 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
3127 separator = (!yaml && is_dir) ? " " : "\n";
3130 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
3131 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3132 if (verbose & ~VERBOSE_GENERATION)
3133 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
3135 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3136 skip_objs ? 0 : (int)lum->lmm_layout_gen);
3140 if (verbose & VERBOSE_STRIPE_OFFSET) {
3141 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3142 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3143 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
3145 if (is_dir || skip_objs)
3146 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3147 lum->lmm_stripe_offset ==
3148 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
3149 lum->lmm_stripe_offset);
3150 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
3151 llapi_printf(LLAPI_MSG_NORMAL, "0");
3153 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3154 objects[0].l_ost_idx);
3155 if (!yaml && is_dir)
3161 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
3162 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3163 if (verbose & ~VERBOSE_POOL)
3164 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
3166 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
3167 if (!yaml && is_dir)
3173 if (strlen(separator) != 0)
3174 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3177 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
3178 struct lov_user_ost_data_v1 *objects,
3179 char *path, int obdindex, int depth,
3180 enum llapi_layout_verbose verbose,
3181 enum lov_dump_flags flags)
3183 bool is_dir = flags & LDF_IS_DIR;
3184 bool indent = flags & LDF_INDENT;
3185 bool skip_objs = flags & LDF_SKIP_OBJS;
3186 bool yaml = flags & LDF_YAML;
3187 bool obdstripe = obdindex == OBD_NOT_FOUND;
3190 if (!obdstripe && !skip_objs) {
3191 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
3192 if (obdindex == objects[i].l_ost_idx) {
3202 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
3205 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
3206 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
3207 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
3208 char *space = " - ";
3211 llapi_printf(LLAPI_MSG_NORMAL,
3212 "%6slmm_objects:\n", " ");
3214 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
3216 llapi_printf(LLAPI_MSG_NORMAL,
3217 "\tobdidx\t\t objid\t\t objid\t\t group\n");
3219 for (i = 0; i < lum->lmm_stripe_count; i++) {
3220 int idx = objects[i].l_ost_idx;
3221 long long oid = ostid_id(&objects[i].l_ost_oi);
3222 long long gr = ostid_seq(&objects[i].l_ost_oi);
3224 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
3228 struct lu_fid fid = { 0 };
3230 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3231 llapi_printf(LLAPI_MSG_NORMAL,
3232 "%sl_ost_idx: %d\n", space, idx);
3233 llapi_printf(LLAPI_MSG_NORMAL,
3234 "%8sl_fid: "DFID_NOBRACE"\n",
3236 } else if (indent) {
3237 struct lu_fid fid = { 0 };
3239 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3240 llapi_printf(LLAPI_MSG_NORMAL,
3241 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
3242 space, i, idx, PFID(&fid));
3246 sprintf(fmt, "%s%s%s\n",
3247 "\t%6u\t%14llu\t%#13llx\t",
3248 (fid_seq_is_rsvd(gr) ||
3249 fid_seq_is_mdt0(gr)) ?
3250 "%14llu" : "%#14llx", "%s");
3251 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
3253 obdindex == idx ? " *" : "");
3257 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3260 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3261 char *path, int obdindex, int depth,
3262 enum llapi_layout_verbose verbose,
3263 enum lov_dump_flags flags)
3265 struct lmv_user_mds_data *objects = lum->lum_objects;
3266 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3267 char *separator = "";
3268 bool yaml = flags & LDF_YAML;
3269 bool obdstripe = false;
3272 if (obdindex != OBD_NOT_FOUND) {
3273 if (lum->lum_stripe_count == 0) {
3274 if (obdindex == lum->lum_stripe_offset)
3277 for (i = 0; i < lum->lum_stripe_count; i++) {
3278 if (obdindex == objects[i].lum_mds) {
3279 llapi_printf(LLAPI_MSG_NORMAL,
3294 /* show all information default */
3296 if (lum->lum_magic == LMV_USER_MAGIC)
3297 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3298 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3300 verbose = VERBOSE_OBJID;
3303 if (depth && path && ((verbose != VERBOSE_OBJID)))
3304 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3306 if (verbose & VERBOSE_STRIPE_COUNT) {
3307 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3308 if (verbose & ~VERBOSE_STRIPE_COUNT)
3309 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3310 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3311 (int)lum->lum_stripe_count);
3312 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3318 if (verbose & VERBOSE_STRIPE_OFFSET) {
3319 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3320 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3321 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3322 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3323 (int)lum->lum_stripe_offset);
3324 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3330 if (verbose & VERBOSE_HASH_TYPE) {
3331 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3332 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3334 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3335 if (verbose & ~VERBOSE_HASH_TYPE)
3336 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3337 if (type < LMV_HASH_TYPE_MAX)
3338 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3339 mdt_hash_name[type]);
3341 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3343 if (flags & LMV_HASH_FLAG_MIGRATION)
3344 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3345 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3346 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3347 if (flags & LMV_HASH_FLAG_LOST_LMV)
3348 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3349 if (flags & LMV_HASH_FLAG_FIXED)
3350 llapi_printf(LLAPI_MSG_NORMAL, ",fixed");
3351 if (flags & ~LMV_HASH_FLAG_KNOWN)
3352 llapi_printf(LLAPI_MSG_NORMAL, ",unknown_%04x",
3353 flags & ~LMV_HASH_FLAG_KNOWN);
3355 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3361 if ((verbose & VERBOSE_INHERIT) && lum->lum_magic == LMV_USER_MAGIC) {
3362 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3363 if (verbose & ~VERBOSE_INHERIT)
3364 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit: ");
3365 if (lum->lum_max_inherit == LMV_INHERIT_UNLIMITED)
3366 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3367 else if (lum->lum_max_inherit == LMV_INHERIT_NONE)
3368 llapi_printf(LLAPI_MSG_NORMAL, "0");
3370 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3371 lum->lum_max_inherit);
3372 if (verbose & VERBOSE_INHERIT && !yaml)
3378 if ((verbose & VERBOSE_INHERIT_RR) &&
3379 lum->lum_magic == LMV_USER_MAGIC &&
3380 lum->lum_stripe_offset == LMV_OFFSET_DEFAULT) {
3381 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3382 if (verbose & ~VERBOSE_INHERIT_RR)
3383 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit_rr: ");
3384 if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_UNLIMITED)
3385 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3386 else if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_NONE)
3387 llapi_printf(LLAPI_MSG_NORMAL, "0");
3389 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3390 lum->lum_max_inherit_rr);
3391 if (verbose & VERBOSE_INHERIT_RR && !yaml)
3399 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3400 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3401 if (lum->lum_stripe_count > 0)
3402 llapi_printf(LLAPI_MSG_NORMAL,
3403 "mdtidx\t\t FID[seq:oid:ver]\n");
3404 for (i = 0; i < lum->lum_stripe_count; i++) {
3405 int idx = objects[i].lum_mds;
3406 struct lu_fid *fid = &objects[i].lum_fid;
3408 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3409 llapi_printf(LLAPI_MSG_NORMAL,
3410 "%6u\t\t "DFID"\t\t%s\n",
3412 obdindex == idx ? " *" : "");
3416 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3417 pool_name[0] != '\0') {
3418 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3419 if (verbose & ~VERBOSE_POOL)
3420 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3422 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3426 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3427 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3430 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3431 enum lov_dump_flags flags)
3433 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3434 int depth = param->fp_max_depth;
3435 enum llapi_layout_verbose verbose = param->fp_verbose;
3436 bool yaml = flags & LDF_YAML;
3438 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3439 !(flags & LDF_IS_DIR)) && !yaml)
3440 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3442 if (verbose & VERBOSE_DETAIL) {
3443 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3444 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3445 " ", comp_v1->lcm_magic);
3446 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3447 " ", comp_v1->lcm_size);
3448 if (flags & LDF_IS_DIR)
3449 llapi_printf(LLAPI_MSG_NORMAL,
3450 "%2slcm_flags: %s\n", " ",
3451 comp_v1->lcm_mirror_count > 0 ?
3454 llapi_printf(LLAPI_MSG_NORMAL,
3455 "%2slcm_flags: %s\n", " ",
3456 llapi_layout_flags_string(comp_v1->lcm_flags));
3459 if (verbose & VERBOSE_GENERATION) {
3460 if (verbose & ~VERBOSE_GENERATION)
3461 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3463 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3466 if (verbose & VERBOSE_MIRROR_COUNT) {
3467 if (verbose & ~VERBOSE_MIRROR_COUNT)
3468 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3470 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3471 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3472 comp_v1->lcm_mirror_count + 1 : 1);
3475 if (verbose & VERBOSE_COMP_COUNT) {
3476 if (verbose & ~VERBOSE_COMP_COUNT)
3477 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3479 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3480 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3481 comp_v1->lcm_entry_count : 0);
3484 if (verbose & VERBOSE_DETAIL && !yaml)
3485 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3488 static void lcme_flags2str(__u32 comp_flags)
3494 llapi_printf(LLAPI_MSG_NORMAL, "0");
3497 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3498 if (comp_flags & comp_flags_table[i].cfn_flag) {
3500 llapi_printf(LLAPI_MSG_NORMAL, ",");
3501 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3502 comp_flags_table[i].cfn_name);
3503 comp_flags &= ~comp_flags_table[i].cfn_flag;
3509 llapi_printf(LLAPI_MSG_NORMAL, ",");
3510 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3514 static void lov_dump_comp_v1_entry(struct find_param *param,
3515 enum lov_dump_flags flags, int index)
3517 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3518 struct lov_comp_md_entry_v1 *entry;
3519 char *separator = "";
3520 enum llapi_layout_verbose verbose = param->fp_verbose;
3521 bool yaml = flags & LDF_YAML;
3523 entry = &comp_v1->lcm_entries[index];
3526 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3528 if (verbose & VERBOSE_COMP_ID) {
3529 if (verbose & VERBOSE_DETAIL && !yaml)
3530 llapi_printf(LLAPI_MSG_NORMAL,
3531 "%slcme_id: ", " - ");
3532 else if (verbose & ~VERBOSE_COMP_ID)
3533 llapi_printf(LLAPI_MSG_NORMAL,
3534 "%4slcme_id: ", " ");
3535 if (entry->lcme_id != LCME_ID_INVAL)
3536 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3538 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3542 if (verbose & VERBOSE_MIRROR_ID) {
3543 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3544 if (verbose & ~VERBOSE_MIRROR_ID)
3545 llapi_printf(LLAPI_MSG_NORMAL,
3546 "%4slcme_mirror_id: ", " ");
3547 if (entry->lcme_id != LCME_ID_INVAL)
3548 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3549 mirror_id_of(entry->lcme_id));
3551 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3555 if (verbose & VERBOSE_COMP_FLAGS) {
3556 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3557 if (verbose & ~VERBOSE_COMP_FLAGS)
3558 llapi_printf(LLAPI_MSG_NORMAL,
3559 "%4slcme_flags: ", " ");
3560 lcme_flags2str(entry->lcme_flags);
3563 /* print snapshot timestamp if its a nosync comp */
3564 if ((verbose & VERBOSE_COMP_FLAGS) &&
3565 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3566 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3567 if (verbose & ~VERBOSE_COMP_FLAGS)
3568 llapi_printf(LLAPI_MSG_NORMAL,
3569 "%4slcme_timestamp: ", " ");
3571 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3572 (unsigned long long)entry->lcme_timestamp);
3574 time_t stamp = entry->lcme_timestamp;
3575 char *date_str = asctime(localtime(&stamp));
3577 date_str[strlen(date_str) - 1] = '\0';
3578 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3584 if (verbose & VERBOSE_COMP_START) {
3585 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3586 if (verbose & ~VERBOSE_COMP_START)
3587 llapi_printf(LLAPI_MSG_NORMAL,
3588 "%4slcme_extent.e_start: ", " ");
3589 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3590 (unsigned long long)entry->lcme_extent.e_start);
3594 if (verbose & VERBOSE_COMP_END) {
3595 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3596 if (verbose & ~VERBOSE_COMP_END)
3597 llapi_printf(LLAPI_MSG_NORMAL,
3598 "%4slcme_extent.e_end: ", " ");
3599 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3600 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3602 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3603 (unsigned long long)entry->lcme_extent.e_end);
3608 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3609 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3610 } else if (verbose & VERBOSE_DETAIL) {
3611 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3612 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3613 " ", entry->lcme_offset);
3614 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3615 " ", entry->lcme_size);
3616 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3618 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3623 * Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3624 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3626 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3627 * The table below gives the answers for the specified parameters (value and
3628 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3629 * --------------------------------------
3630 * 1 | file > limit; sign > 0 | -1 / -1 |
3631 * 2 | file = limit; sign > 0 | -1 / -1 |
3632 * 3 | file < limit; sign > 0 | ? / 1 |
3633 * 4 | file > limit; sign = 0 | -1 / -1 |
3634 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3635 * 6 | file < limit; sign = 0 | ? / -1 |
3636 * 7 | file > limit; sign < 0 | 1 / 1 |
3637 * 8 | file = limit; sign < 0 | ? / -1 |
3638 * 9 | file < limit; sign < 0 | ? / -1 |
3639 * --------------------------------------
3640 * Note: 5th actually means that the value is within the interval
3641 * (limit - margin, limit].
3643 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3644 int sign, int negopt, unsigned long long margin,
3650 /* Drop the fraction of margin (of days or size). */
3651 if (file + margin <= limit)
3653 } else if (sign == 0) {
3654 if (file <= limit && file + margin > limit)
3656 else if (file + margin <= limit)
3658 } else if (sign < 0) {
3665 return negopt ? ~ret + 1 : ret;
3668 static inline struct lov_user_md *
3669 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3671 return (struct lov_user_md *)((char *)comp_v1 +
3672 comp_v1->lcm_entries[ent_idx].lcme_offset);
3675 static inline struct lov_user_ost_data_v1 *
3676 lov_v1v3_objects(struct lov_user_md *v1)
3678 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3679 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3681 return v1->lmm_objects;
3685 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3687 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3688 snprintf(pool_name, LOV_MAXPOOLNAME + 1, "%s",
3689 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3691 pool_name[0] = '\0';
3695 print_last_init_comp(struct find_param *param)
3697 /* print all component info */
3698 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3701 /* print specific component info */
3702 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3703 param->fp_check_comp_start || param->fp_check_comp_end ||
3704 param->fp_check_mirror_id || param->fp_check_mirror_index)
3710 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3714 if (param->fp_comp_end == LUSTRE_EOF) {
3715 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3716 match = end == LUSTRE_EOF ? 1 : -1;
3717 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3718 match = end == LUSTRE_EOF ? -1 : 1;
3719 else /* at least EOF */
3721 if (param->fp_exclude_comp_end)
3724 unsigned long long margin;
3726 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3727 match = find_value_cmp(end, param->fp_comp_end,
3728 param->fp_comp_end_sign,
3729 param->fp_exclude_comp_end, margin, 0);
3736 * An example of "getstripe -v" for a two components PFL file:
3739 * lcm_magic: 0x0BD60BD0
3743 * lcm_entry_count: 2
3747 * lcme_extent.e_start: 0
3748 * lcme_extent.e_end: 1048576
3752 * lmm_magic: 0x0BD10BD0
3753 * lmm_seq: 0x200000401
3754 * lmm_object_id: 0x1
3755 * lmm_fid: [0x200000401:0x1:0x0]
3756 * lmm_stripe_count: 1
3757 * lmm_stripe_size: 1048576
3758 * lmm_pattern: raid0
3760 * lmm_stripe_offset: 0
3762 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3766 * lcme_extent.e_start: 1048576
3767 * lcme_extent.e_end: EOF
3771 * lmm_magic: 0x0BD10BD0
3772 * lmm_seq: 0x200000401
3773 * lmm_object_id: 0x1
3774 * lmm_fid: [0x200000401:0x1:0x0]
3775 * lmm_stripe_count: 2
3776 * lmm_stripe_size: 1048576
3777 * lmm_pattern: raid0
3779 * lmm_stripe_offset: 1
3781 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3782 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3784 static void lov_dump_comp_v1(struct find_param *param, char *path,
3785 enum lov_dump_flags flags)
3787 struct lov_comp_md_entry_v1 *entry;
3788 struct lov_user_ost_data_v1 *objects;
3789 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3790 struct lov_user_md_v1 *v1;
3791 char pool_name[LOV_MAXPOOLNAME + 1];
3792 int obdindex = param->fp_obd_index;
3793 int i, j, match, ext;
3794 bool obdstripe = false;
3795 __u16 mirror_index = 0;
3796 __u16 mirror_id = 0;
3798 if (obdindex != OBD_NOT_FOUND) {
3799 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3800 i < comp_v1->lcm_entry_count; i++) {
3801 if (!(comp_v1->lcm_entries[i].lcme_flags &
3805 v1 = lov_comp_entry(comp_v1, i);
3806 objects = lov_v1v3_objects(v1);
3808 for (j = 0; j < v1->lmm_stripe_count; j++) {
3809 if (obdindex == objects[j].l_ost_idx) {
3822 lov_dump_comp_v1_header(param, path, flags);
3824 flags |= LDF_INDENT;
3826 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3827 entry = &comp_v1->lcm_entries[i];
3829 if (param->fp_check_comp_flags) {
3830 if (((param->fp_comp_flags & entry->lcme_flags) !=
3831 param->fp_comp_flags) ||
3832 (param->fp_comp_neg_flags & entry->lcme_flags))
3836 if (param->fp_check_comp_id &&
3837 param->fp_comp_id != entry->lcme_id)
3840 if (param->fp_check_comp_start) {
3841 match = find_value_cmp(entry->lcme_extent.e_start,
3842 param->fp_comp_start,
3843 param->fp_comp_start_sign,
3845 param->fp_comp_start_units, 0);
3850 if (param->fp_check_comp_end) {
3851 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3857 if (param->fp_check_mirror_index) {
3858 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3860 mirror_id = mirror_id_of(entry->lcme_id);
3863 match = find_value_cmp(mirror_index,
3864 param->fp_mirror_index,
3865 param->fp_mirror_index_sign,
3866 param->fp_exclude_mirror_index,
3870 } else if (param->fp_check_mirror_id) {
3871 if (mirror_id != mirror_id_of(entry->lcme_id))
3872 mirror_id = mirror_id_of(entry->lcme_id);
3874 match = find_value_cmp(mirror_id,
3875 param->fp_mirror_id,
3876 param->fp_mirror_id_sign,
3877 param->fp_exclude_mirror_id,
3883 if (print_last_init_comp(param)) {
3885 * if part of stripe info is needed, we'd print only
3886 * the last instantiated component info.
3888 if (entry->lcme_flags & LCME_FL_INIT)
3891 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3892 if (entry->lcme_flags & LCME_FL_EXTENSION)
3893 /* moved back below */
3901 if (entry->lcme_flags & LCME_FL_INIT) {
3902 if (obdindex != OBD_NOT_FOUND) {
3903 flags |= LDF_SKIP_OBJS;
3904 v1 = lov_comp_entry(comp_v1, i);
3905 objects = lov_v1v3_objects(v1);
3907 for (j = 0; j < v1->lmm_stripe_count; j++) {
3908 if (obdindex == objects[j].l_ost_idx) {
3909 flags &= ~LDF_SKIP_OBJS;
3914 flags &= ~LDF_SKIP_OBJS;
3917 flags |= LDF_SKIP_OBJS;
3920 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3922 lov_dump_comp_v1_entry(param, flags, i);
3924 v1 = lov_comp_entry(comp_v1, i);
3925 objects = lov_v1v3_objects(v1);
3926 lov_v1v3_pool_name(v1, pool_name);
3928 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3929 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3930 param->fp_max_depth, param->fp_verbose,
3933 if (print_last_init_comp(param)) {
3935 * directory layout contains only layout template, print the
3939 i = comp_v1->lcm_entry_count - 1;
3942 flags &= ~LDF_SKIP_OBJS;
3944 lov_dump_comp_v1_entry(param, flags, i);
3946 v1 = lov_comp_entry(comp_v1, i);
3947 objects = lov_v1v3_objects(v1);
3948 lov_v1v3_pool_name(v1, pool_name);
3950 entry = &comp_v1->lcm_entries[i];
3951 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3952 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3953 param->fp_max_depth, param->fp_verbose,
3958 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3959 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3962 static inline bool has_any_comp_options(struct find_param *param)
3964 enum llapi_layout_verbose verbose = param->fp_verbose;
3966 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3967 param->fp_check_comp_start || param->fp_check_comp_end ||
3968 param->fp_check_comp_flags)
3971 /* show full layout information, not component specific */
3972 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3975 return verbose & VERBOSE_COMP_OPTS;
3978 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3981 struct lov_user_md *lum = &orig->lmd_lmm;
3982 struct lov_user_mds_data *new;
3983 struct lov_comp_md_v1 *comp_v1;
3984 struct lov_comp_md_entry_v1 *ent;
3985 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3986 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3989 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3991 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3995 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3996 + sizeof(new->lmd_lmmsize));
3998 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3999 comp_v1->lcm_magic = lum->lmm_magic;
4000 comp_v1->lcm_size = lum_off + lum_size;
4001 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
4002 comp_v1->lcm_flags = 0;
4003 comp_v1->lcm_entry_count = 1;
4005 ent = &comp_v1->lcm_entries[0];
4007 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
4008 ent->lcme_extent.e_start = 0;
4009 ent->lcme_extent.e_end = LUSTRE_EOF;
4010 ent->lcme_offset = lum_off;
4011 ent->lcme_size = lum_size;
4013 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
4018 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
4019 enum lov_dump_flags flags)
4021 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
4023 if (has_any_comp_options(param)) {
4024 struct lov_user_mds_data *new_lmd, *orig_lmd;
4026 orig_lmd = param->fp_lmd;
4027 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
4028 if (new_lmd != NULL) {
4029 param->fp_lmd = new_lmd;
4030 lov_dump_comp_v1(param, path, flags);
4031 param->fp_lmd = orig_lmd;
4037 if (magic == LOV_USER_MAGIC_V1) {
4038 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
4039 param->fp_lmd->lmd_lmm.lmm_objects,
4040 path, param->fp_obd_index,
4041 param->fp_max_depth, param->fp_verbose,
4044 char pool_name[LOV_MAXPOOLNAME + 1];
4045 struct lov_user_ost_data_v1 *objects;
4046 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
4048 snprintf(pool_name, sizeof(pool_name), "%s",
4049 lmmv3->lmm_pool_name);
4050 objects = lmmv3->lmm_objects;
4051 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
4052 objects, path, param->fp_obd_index,
4053 param->fp_max_depth, param->fp_verbose,
4058 static uint32_t check_foreign_type(uint32_t foreign_type)
4062 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
4063 if (lu_foreign_types[i].lft_name == NULL)
4065 if (foreign_type == lu_foreign_types[i].lft_type)
4069 return LU_FOREIGN_TYPE_UNKNOWN;
4072 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
4073 enum lov_dump_flags flags)
4075 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
4076 bool yaml = flags & LDF_YAML;
4078 if (!yaml && param->fp_depth && path)
4079 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4081 if (param->fp_verbose & VERBOSE_DETAIL) {
4082 uint32_t type = check_foreign_type(lfm->lfm_type);
4084 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
4086 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
4088 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
4090 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4091 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4092 lu_foreign_types[type].lft_name);
4094 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4096 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
4099 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
4100 lfm->lfm_length, lfm->lfm_value);
4101 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4104 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
4105 enum lov_dump_flags flags)
4107 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
4108 bool yaml = flags & LDF_YAML;
4110 if (!yaml && param->fp_depth && path)
4111 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4113 if (param->fp_verbose & VERBOSE_DETAIL) {
4114 uint32_t type = check_foreign_type(lfm->lfm_type);
4116 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
4118 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
4120 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
4122 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4123 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4124 lu_foreign_types[type].lft_name);
4126 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4128 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
4131 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
4132 lfm->lfm_length, lfm->lfm_value);
4133 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4136 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
4137 enum lov_dump_flags flags)
4141 if (param->fp_get_lmv || param->fp_get_default_lmv)
4142 magic = (__u32)param->fp_lmv_md->lum_magic;
4144 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
4147 flags |= LDF_IS_RAW;
4152 case LOV_USER_MAGIC_V1:
4153 case LOV_USER_MAGIC_V3:
4154 case LOV_USER_MAGIC_SPECIFIC:
4155 lov_dump_plain_user_lmm(param, path, flags);
4157 case LOV_USER_MAGIC_FOREIGN:
4158 lov_dump_foreign_lmm(param, path, flags);
4161 case LMV_USER_MAGIC: {
4162 char pool_name[LOV_MAXPOOLNAME + 1];
4163 struct lmv_user_md *lum;
4165 lum = (struct lmv_user_md *)param->fp_lmv_md;
4166 snprintf(pool_name, sizeof(pool_name), "%s",
4167 lum->lum_pool_name);
4168 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
4169 param->fp_max_depth, param->fp_verbose,
4173 case LOV_USER_MAGIC_COMP_V1:
4174 lov_dump_comp_v1(param, path, flags);
4176 case LMV_MAGIC_FOREIGN:
4177 lmv_dump_foreign_lmm(param, path, flags);
4180 llapi_printf(LLAPI_MSG_NORMAL,
4181 "unknown lmm_magic: %#x (expecting one of %#x %#x %#x %#x)\n",
4182 *(__u32 *)¶m->fp_lmd->lmd_lmm,
4183 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
4184 LMV_USER_MAGIC, LMV_MAGIC_V1);
4189 static int llapi_file_get_stripe1(const char *path, struct lov_user_md *lum)
4195 fname = strrchr(path, '/');
4197 /* It should be a file (or other non-directory) */
4198 if (fname == NULL) {
4199 dname = (char *)malloc(2);
4203 fname = (char *)path;
4205 dname = (char *)malloc(fname - path + 1);
4208 strncpy(dname, path, fname - path);
4209 dname[fname - path] = '\0';
4213 fd = open(dname, O_RDONLY | O_NONBLOCK);
4219 strcpy((char *)lum, fname);
4220 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
4223 if (close(fd) == -1 && rc == 0)
4231 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
4233 char *canon_path = NULL;
4236 rc = llapi_file_get_stripe1(path, lum);
4237 if (!(rc == -ENOTTY || rc == -ENODATA))
4240 /* Handle failure due to symlinks by dereferencing path manually. */
4241 canon_path = canonicalize_file_name(path);
4242 if (canon_path == NULL)
4243 goto out; /* Keep original rc. */
4245 rc2 = llapi_file_get_stripe1(canon_path, lum);
4247 goto out; /* Keep original rc. */
4256 int llapi_file_lookup(int dirfd, const char *name)
4258 struct obd_ioctl_data data = { 0 };
4263 if (dirfd < 0 || name == NULL)
4266 data.ioc_version = OBD_IOCTL_VERSION;
4267 data.ioc_len = sizeof(data);
4268 data.ioc_inlbuf1 = (char *)name;
4269 data.ioc_inllen1 = strlen(name) + 1;
4271 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
4273 llapi_error(LLAPI_MSG_ERROR, rc,
4274 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
4279 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
4286 * Check if the file time matches all the given criteria (e.g. --atime +/-N).
4287 * Return -1 or 1 if file timestamp does not or does match the given criteria
4288 * correspondingly. Return 0 if the MDS time is being checked and there are
4289 * attributes on OSTs and it is not yet clear if the timespamp matches.
4291 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
4292 * updated timestamps.
4294 static int find_time_check(struct find_param *param, int mds)
4296 struct lov_user_mds_data *lmd = param->fp_lmd;
4300 /* Check if file is accepted. */
4301 if (param->fp_atime) {
4302 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4303 param->fp_atime, param->fp_asign,
4304 param->fp_exclude_atime,
4305 param->fp_time_margin, mds);
4311 if (param->fp_mtime) {
4312 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4313 param->fp_mtime, param->fp_msign,
4314 param->fp_exclude_mtime,
4315 param->fp_time_margin, mds);
4320 * If the previous check matches, but this one is not yet clear,
4321 * we should return 0 to do an RPC on OSTs.
4327 if (param->fp_ctime) {
4328 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4329 param->fp_ctime, param->fp_csign,
4330 param->fp_exclude_ctime,
4331 param->fp_time_margin, mds);
4336 * If the previous check matches, but this one is not yet clear,
4337 * we should return 0 to do an RPC on OSTs.
4346 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4348 struct lov_user_mds_data *lmd = param->fp_lmd;
4353 for (i = 0; i < 2; i++) {
4354 /* Check if file is accepted. */
4355 if (param->fp_newery[NEWERXY_ATIME][i]) {
4356 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4357 param->fp_newery[NEWERXY_ATIME][i],
4364 if (param->fp_newery[NEWERXY_MTIME][i]) {
4365 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4366 param->fp_newery[NEWERXY_MTIME][i],
4372 * If the previous check matches, but this one is not
4373 * yet clear, we should return 0 to do an RPC on OSTs.
4379 if (param->fp_newery[NEWERXY_CTIME][i]) {
4380 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4381 param->fp_newery[NEWERXY_CTIME][i],
4387 * If the previous check matches, but this one is not
4388 * yet clear, we should return 0 to do an RPC on OSTs.
4395 * File birth time (btime) can get from MDT directly.
4396 * if @from_mdt is true, it means the input file attributs are
4397 * obtained directly from MDT.
4398 * Thus, if @from_mdt is false, we should skip the following
4404 if (param->fp_newery[NEWERXY_BTIME][i]) {
4405 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4408 rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4409 param->fp_newery[NEWERXY_BTIME][i],
4420 * Check whether the stripes matches the indexes user provided
4424 static int check_obd_match(struct find_param *param)
4426 struct lov_user_ost_data_v1 *objects;
4427 struct lov_comp_md_v1 *comp_v1 = NULL;
4428 struct lov_user_mds_data *lmd = param->fp_lmd;
4429 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4430 int i, j, k, count = 1;
4432 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4435 if (!S_ISREG(lmd->lmd_stx.stx_mode))
4438 /* exclude foreign */
4439 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4440 return param->fp_exclude_obd;
4443 * Only those files should be accepted, which have a
4444 * stripe on the specified OST.
4446 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4447 comp_v1 = (struct lov_comp_md_v1 *)v1;
4448 count = comp_v1->lcm_entry_count;
4451 for (i = 0; i < count; i++) {
4453 v1 = lov_comp_entry(comp_v1, i);
4455 objects = lov_v1v3_objects(v1);
4457 for (j = 0; j < v1->lmm_stripe_count; j++) {
4458 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4461 for (k = 0; k < param->fp_num_obds; k++) {
4462 if (param->fp_obd_indexes[k] ==
4463 objects[j].l_ost_idx)
4464 return !param->fp_exclude_obd;
4469 return param->fp_exclude_obd;
4472 static int check_mdt_match(struct find_param *param)
4476 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4479 /* FIXME: For striped dir, we should get stripe information and check */
4480 for (i = 0; i < param->fp_num_mdts; i++) {
4481 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4482 return !param->fp_exclude_mdt;
4485 if (param->fp_exclude_mdt)
4492 * Check whether the obd is active or not, if it is
4493 * not active, just print the object affected by this
4496 static void print_failed_tgt(struct find_param *param, char *path, int type)
4498 struct obd_statfs stat_buf;
4499 struct obd_uuid uuid_buf;
4500 int tgt_nr, i, *indexes;
4503 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV) {
4504 llapi_error(LLAPI_MSG_NORMAL, ret, "%s: wrong statfs type(%d)",
4509 tgt_nr = (type == LL_STATFS_LOV) ? param->fp_obd_index :
4510 param->fp_mdt_index;
4511 indexes = (type == LL_STATFS_LOV) ? param->fp_obd_indexes :
4512 param->fp_mdt_indexes;
4514 for (i = 0; i < tgt_nr; i++) {
4515 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4516 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4518 ret = llapi_obd_statfs(path, type, indexes[i], &stat_buf,
4521 llapi_error(LLAPI_MSG_NORMAL, ret,
4522 "%s: obd_uuid: %s failed",
4523 __func__, param->fp_obd_uuid->uuid);
4527 static int find_check_stripe_size(struct find_param *param)
4529 struct lov_comp_md_v1 *comp_v1 = NULL;
4530 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4531 __u32 stripe_size = 0;
4532 int ret, i, count = 1;
4534 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4535 return param->fp_exclude_stripe_size ? 1 : -1;
4537 ret = param->fp_exclude_stripe_size ? 1 : -1;
4538 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4539 comp_v1 = (struct lov_comp_md_v1 *)v1;
4540 count = comp_v1->lcm_entry_count;
4543 for (i = 0; i < count; i++) {
4544 struct lov_comp_md_entry_v1 *ent;
4547 v1 = lov_comp_entry(comp_v1, i);
4549 ent = &comp_v1->lcm_entries[i];
4550 if (ent->lcme_flags & LCME_FL_EXTENSION)
4552 if (!(ent->lcme_flags & LCME_FL_INIT))
4555 stripe_size = v1->lmm_stripe_size;
4558 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4559 param->fp_stripe_size_sign,
4560 param->fp_exclude_stripe_size,
4561 param->fp_stripe_size_units, 0);
4566 static int find_check_ext_size(struct find_param *param)
4568 struct lov_comp_md_v1 *comp_v1;
4569 struct lov_user_md_v1 *v1;
4572 ret = param->fp_exclude_ext_size ? 1 : -1;
4573 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4574 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4577 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4578 struct lov_comp_md_entry_v1 *ent;
4580 v1 = lov_comp_entry(comp_v1, i);
4582 ent = &comp_v1->lcm_entries[i];
4583 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4586 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4587 param->fp_ext_size_sign,
4588 param->fp_exclude_ext_size,
4589 param->fp_ext_size_units, 0);
4590 /* If any ext_size matches */
4598 static __u32 find_get_stripe_count(struct find_param *param)
4600 struct lov_comp_md_v1 *comp_v1 = NULL;
4601 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4603 __u32 stripe_count = 0;
4605 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4608 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4609 comp_v1 = (struct lov_comp_md_v1 *)v1;
4610 count = comp_v1->lcm_entry_count;
4613 for (i = 0; i < count; i++) {
4615 struct lov_comp_md_entry_v1 *ent;
4617 v1 = lov_comp_entry(comp_v1, i);
4619 ent = &comp_v1->lcm_entries[i];
4620 if (!(ent->lcme_flags & LCME_FL_INIT))
4623 if (ent->lcme_flags & LCME_FL_EXTENSION)
4626 stripe_count = v1->lmm_stripe_count;
4629 return stripe_count;
4632 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4634 static int find_check_layout(struct find_param *param)
4636 struct lov_comp_md_v1 *comp_v1 = NULL;
4637 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4639 bool found = false, valid = false;
4641 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4642 comp_v1 = (struct lov_comp_md_v1 *)v1;
4643 count = comp_v1->lcm_entry_count;
4646 for (i = 0; i < count; i++) {
4648 v1 = lov_comp_entry(comp_v1, i);
4650 /* foreign file have a special magic but no pattern field */
4651 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4654 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4658 if (v1->lmm_pattern & param->fp_layout) {
4667 if ((found && !param->fp_exclude_layout) ||
4668 (!found && param->fp_exclude_layout))
4675 * if no type specified, check/exclude all foreign
4676 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4678 static int find_check_foreign(struct find_param *param)
4680 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4681 struct lov_foreign_md *lfm;
4683 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4684 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4685 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4686 return param->fp_exclude_foreign ? 1 : -1;
4690 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4691 lfm->lfm_type == param->fp_foreign_type)
4692 return param->fp_exclude_foreign ? -1 : 1;
4693 return param->fp_exclude_foreign ? 1 : -1;
4696 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4697 struct lmv_foreign_md *lfm;
4699 lfm = (void *)param->fp_lmv_md;
4700 if (lmv_is_foreign(lfm->lfm_magic)) {
4701 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4702 return param->fp_exclude_foreign ? 1 : -1;
4706 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4707 lfm->lfm_type == param->fp_foreign_type)
4708 return param->fp_exclude_foreign ? -1 : 1;
4709 return param->fp_exclude_foreign ? 1 : -1;
4714 static int find_check_pool(struct find_param *param)
4716 struct lov_comp_md_v1 *comp_v1 = NULL;
4717 struct lov_user_md_v3 *v3 = (void *)¶m->fp_lmd->lmd_lmm;
4721 if (v3->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4722 comp_v1 = (struct lov_comp_md_v1 *)v3;
4723 count = comp_v1->lcm_entry_count;
4724 /* empty requested pool is taken as no pool search */
4725 if (count == 0 && param->fp_poolname[0] == '\0') {
4731 for (i = 0; i < count; i++) {
4732 if (comp_v1 != NULL) {
4733 if (!(comp_v1->lcm_entries[i].lcme_flags &
4737 v3 = (void *)lov_comp_entry(comp_v1, i);
4740 if (v3->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4743 if (((v3->lmm_magic == LOV_USER_MAGIC_V1) &&
4744 (param->fp_poolname[0] == '\0')) ||
4745 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4746 (strncmp(v3->lmm_pool_name,
4747 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4748 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4749 (strcmp(param->fp_poolname, "*") == 0))) {
4756 if ((found && !param->fp_exclude_pool) ||
4757 (!found && param->fp_exclude_pool))
4763 static int find_check_comp_options(struct find_param *param)
4765 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4766 struct lov_user_mds_data *lmd = param->fp_lmd;
4767 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4768 struct lov_comp_md_entry_v1 *entry;
4771 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4774 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4775 comp_v1 = (struct lov_comp_md_v1 *)v1;
4777 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4778 if (forged_v1 == NULL)
4780 comp_v1 = forged_v1;
4781 comp_v1->lcm_entry_count = 1;
4782 entry = &comp_v1->lcm_entries[0];
4783 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4785 entry->lcme_extent.e_start = 0;
4786 entry->lcme_extent.e_end = LUSTRE_EOF;
4789 /* invalid case, don't match for any kind of search. */
4790 if (comp_v1->lcm_entry_count == 0) {
4795 if (param->fp_check_comp_count) {
4796 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4797 param->fp_comp_count,
4798 param->fp_comp_count_sign,
4799 param->fp_exclude_comp_count, 1, 0);
4805 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4806 entry = &comp_v1->lcm_entries[i];
4808 if (param->fp_check_comp_flags) {
4810 if (((param->fp_comp_flags & entry->lcme_flags) !=
4811 param->fp_comp_flags) ||
4812 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4818 if (param->fp_check_comp_start) {
4819 ret = find_value_cmp(entry->lcme_extent.e_start,
4820 param->fp_comp_start,
4821 param->fp_comp_start_sign,
4822 param->fp_exclude_comp_start,
4823 param->fp_comp_start_units, 0);
4828 if (param->fp_check_comp_end) {
4829 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4835 /* the component matches all criteria */
4844 static int find_check_mirror_options(struct find_param *param)
4846 struct lov_comp_md_v1 *comp_v1;
4847 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4850 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4853 comp_v1 = (struct lov_comp_md_v1 *)v1;
4855 if (param->fp_check_mirror_count) {
4856 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4857 param->fp_mirror_count,
4858 param->fp_mirror_count_sign,
4859 param->fp_exclude_mirror_count, 1, 0);
4864 if (param->fp_check_mirror_state) {
4866 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4868 if ((param->fp_mirror_state != 0 &&
4869 file_state != param->fp_mirror_state) ||
4870 file_state == param->fp_mirror_neg_state)
4877 static bool find_check_lmm_info(struct find_param *param)
4879 return param->fp_check_pool || param->fp_check_stripe_count ||
4880 param->fp_check_stripe_size || param->fp_check_layout ||
4881 param->fp_check_comp_count || param->fp_check_comp_end ||
4882 param->fp_check_comp_start || param->fp_check_comp_flags ||
4883 param->fp_check_mirror_count || param->fp_check_foreign ||
4884 param->fp_check_mirror_state || param->fp_check_ext_size ||
4885 param->fp_check_projid;
4889 * Interpret backslash escape sequences and write output into buffer.
4890 * Anything written to the buffer will be null terminated.
4892 * @param[in] seq String being parsed for escape sequence. The leading
4893 * '\' character is not included in this string (only the
4894 * characters after it)
4895 * @param[out] buffer Location where interpreted escape sequence is written
4896 * @param[in] size Size of the available buffer. (Needs to be large enough
4897 * to handle escape sequence output plus null terminator.)
4898 * @param[out] wrote Number of bytes written to the buffer.
4899 * @return Number of characters from input string processed
4900 * as part of the escape sequence (0 for an unrecognized
4903 int printf_format_escape(char *seq, char *buffer, size_t size, int *wrote)
4906 /* For now, only handle single char escape sequences: \n, \t, \\ */
4929 * Interpret formats for timestamps (%a, %A@, etc)
4931 * @param[in] seq String being parsed for timestamp format. The leading
4932 * '%' character is not included in this string
4933 * @param[out] buffer Location where timestamp info is written
4934 * @param[in] size Size of the available buffer.
4935 * @param[out] wrote Number of bytes written to the buffer.
4936 * @return Number of characters from input string processed
4937 * as part of the format (0 for an unknown format)
4940 int printf_format_timestamp(char *seq, char *buffer, size_t size, int *wrote,
4941 struct find_param *param)
4943 struct statx_timestamp ts = { 0, 0 };
4947 char *fmt = "%c"; /* Print in ctime format by default */
4952 ts = param->fp_lmd->lmd_stx.stx_atime;
4956 if (*(seq + 1) == '@') {
4957 ts = param->fp_lmd->lmd_stx.stx_atime;
4963 ts = param->fp_lmd->lmd_stx.stx_ctime;
4967 if (*(seq + 1) == '@') {
4968 ts = param->fp_lmd->lmd_stx.stx_ctime;
4974 ts = param->fp_lmd->lmd_stx.stx_mtime;
4978 if (*(seq + 1) == '@') {
4979 ts = param->fp_lmd->lmd_stx.stx_mtime;
4985 ts = param->fp_lmd->lmd_stx.stx_btime;
4989 if (*(seq + 1) == '@') {
4990 ts = param->fp_lmd->lmd_stx.stx_btime;
5000 /* Found valid format, print to buffer */
5003 *wrote = strftime(buffer, size, fmt, tm);
5010 * Print all ost indices associated with a file layout using a commma separated
5011 * list. For a file with mutliple components, the list of indices for each
5012 * component will be enclosed in brackets.
5014 * @param[out] buffer Location where OST indices are written
5015 * @param[in] size Size of the available buffer.
5016 * @pararm[in] layout Pointer to layout structure for the file
5017 * @return Number of bytes written to output buffer
5019 static int printf_format_ost_indices(char *buffer, size_t size,
5020 struct llapi_layout *layout)
5022 uint64_t count, idx, i;
5023 int err, bytes, wrote = 0;
5025 /* Make sure to start at the first component */
5026 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
5028 llapi_error(LLAPI_MSG_ERROR, err,
5029 "error: layout component iteration failed\n");
5033 err = llapi_layout_stripe_count_get(layout, &count);
5035 llapi_error(LLAPI_MSG_ERROR, err,
5036 "error: cannot get stripe_count\n");
5040 bytes = snprintf(buffer, (size - wrote), "%s", "[");
5045 for (i = 0; i < count; i++) {
5046 err = llapi_layout_ost_index_get(layout, i, &idx);
5048 llapi_error(LLAPI_MSG_ERROR, err,
5049 "error: cannot get OST index\n");
5050 bytes = snprintf(buffer, (size - wrote),
5053 bytes = snprintf(buffer, (size - wrote),
5061 /* Overwrite last comma with closing bracket */
5062 *(buffer - 1) = ']';
5064 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
5065 if (err == 0) /* next component is found */
5068 llapi_error(LLAPI_MSG_ERROR, err,
5069 "error: layout component iteration failed\n");
5070 /* At this point, either got error or reached last component */
5081 * Parse Lustre-specific format sequences of the form %L{x}.
5083 * @param[in] seq String being parsed for format sequence. The leading
5084 * '%' character is not included in this string
5085 * @param[out] buffer Location where interpreted format info is written
5086 * @param[in] size Size of the available buffer.
5087 * @param[out] wrote Number of bytes written to the buffer.
5088 * @param[in] param The find_param structure associated with the file/dir
5089 * @param[in] path Pathname of the current file/dir being handled
5090 * @param[in] projid Project ID associated with the current file/dir
5091 * @param[in] d File descriptor for the directory (or -1 for a
5092 * non-directory file)
5093 * @return Number of characters from input string processed
5094 * as part of the format (0 for an unknown format)
5096 int printf_format_lustre(char *seq, char *buffer, size_t size, int *wrote,
5097 struct find_param *param, char *path, int projid,
5100 struct lmv_user_md *lum;
5101 struct lmv_user_mds_data *objects;
5102 struct llapi_layout *layout = NULL;
5104 unsigned int hash_type;
5105 uint64_t str_cnt, str_size, idx;
5106 char pool_name[LOV_MAXPOOLNAME + 1] = { '\0' };
5108 int rc = 2; /* all current valid sequences are 2 chars */
5111 /* Sanity check. Formats always look like %L{X} */
5112 if (*seq++ != 'L') {
5118 * Some formats like %LF or %LP are handled the same for both files
5119 * and dirs, so handle all of those here.
5123 err = llapi_path2fid(path, &fid);
5125 llapi_error(LLAPI_MSG_ERROR, err,
5126 "error: cannot get fid\n");
5129 *wrote = snprintf(buffer, size, DFID_NOBRACE, PFID(&fid));
5132 *wrote = snprintf(buffer, size, "%d", projid);
5136 /* Other formats for files/dirs need to be handled differently */
5137 if (d == -1) { /* file */
5138 //layout = llapi_layout_get_by_xattr(¶m->fp_lmd->lmd_lmm,
5139 // param->fp_lum_size, 0);
5140 layout = llapi_layout_get_by_path(path, 0);
5141 if (layout == NULL) {
5142 llapi_error(LLAPI_MSG_ERROR, errno,
5143 "error: cannot get file layout\n");
5148 * Set the layout pointer to the last init component
5149 * since that is the component used for most of these
5150 * formats. (This also works for non-composite files)
5152 err = llapi_layout_get_last_init_comp(layout);
5154 llapi_error(LLAPI_MSG_ERROR, err,
5155 "error: cannot get last initialized compomnent\n");
5160 case 'c': /* stripe count */
5161 err = llapi_layout_stripe_count_get(layout, &str_cnt);
5163 llapi_error(LLAPI_MSG_ERROR, err,
5164 "error: cannot get stripe_count\n");
5167 *wrote = snprintf(buffer, size, "%"PRIu64, str_cnt);
5169 case 'h': /* hash info */
5170 /* Not applicable to files. Skip it. */
5172 case 'i': /* starting index */
5173 err = llapi_layout_ost_index_get(layout, 0, &idx);
5175 llapi_error(LLAPI_MSG_ERROR, err,
5176 "error: cannot get OST index of last initialized component\n");
5179 *wrote = snprintf(buffer, size, "%"PRIu64, idx);
5181 case 'o': /* list of object indices */
5182 *wrote = printf_format_ost_indices(buffer, size, layout);
5184 case 'p': /* pool name */
5185 err = llapi_layout_pool_name_get(layout, pool_name,
5188 llapi_error(LLAPI_MSG_ERROR, rc,
5189 "error: cannot get pool name\n");
5192 *wrote = snprintf(buffer, size, "%s", pool_name);
5194 case 'S': /* stripe size */
5195 err = llapi_layout_stripe_size_get(layout, &str_size);
5197 llapi_error(LLAPI_MSG_ERROR, rc,
5198 "error: cannot get stripe_size\n");
5201 *wrote = snprintf(buffer, size, "%"PRIu64, str_size);
5207 } else { /* directory */
5208 lum = (struct lmv_user_md *)param->fp_lmv_md;
5209 objects = lum->lum_objects;
5212 case 'c': /* stripe count */
5213 *wrote = snprintf(buffer, size, "%d",
5214 (int)lum->lum_stripe_count);
5216 case 'h': /* hash info */
5217 hash_type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
5218 if (hash_type < LMV_HASH_TYPE_MAX)
5219 *wrote = snprintf(buffer, size, "%s",
5220 mdt_hash_name[hash_type]);
5222 *wrote = snprintf(buffer, size, "%#x",
5225 case 'i': /* starting index */
5226 *wrote = snprintf(buffer, size, "%d",
5227 lum->lum_stripe_offset);
5229 case 'o': /* list of object indices */
5230 str_cnt = (int) lum->lum_stripe_count;
5231 *wrote = snprintf(buffer, size, "%s", "[");
5235 for (i = 0; i < str_cnt; i++) {
5236 bytes = snprintf(buffer, (size - *wrote),
5237 "%d,", objects[i].lum_mds);
5244 /* Use lum_offset as the only list entry */
5245 bytes = snprintf(buffer, (size - *wrote),
5246 "%d]", lum->lum_stripe_offset);
5249 /* Overwrite last comma with closing bracket */
5250 *(buffer - 1) = ']';
5253 case 'p': /* pool name */
5254 *wrote = snprintf(buffer, size, "%s",
5255 lum->lum_pool_name);
5257 case 'S': /* stripe size */
5258 /* This has no meaning for directories. Skip it. */
5268 llapi_layout_free(layout);
5271 /* output of snprintf was truncated */
5278 * Interpret format specifiers beginning with '%'.
5280 * @param[in] seq String being parsed for format specifier. The leading
5281 * '%' character is not included in this string
5282 * @param[out] buffer Location where formatted info is written
5283 * @param[in] size Size of the available buffer.
5284 * @param[out] wrote Number of bytes written to the buffer.
5285 * @param[in] param The find_param structure associated with the file/dir
5286 * @param[in] path Pathname of the current file/dir being handled
5287 * @param[in] projid Project ID associated with the current file/dir
5288 * @param[in] d File descriptor for the directory (or -1 for a
5289 * non-directory file)
5290 * @return Number of characters from input string processed
5291 * as part of the format (0 for an unknown format)
5293 int printf_format_directive(char *seq, char *buffer, size_t size, int *wrote,
5294 struct find_param *param, char *path, int projid,
5297 __u16 mode = param->fp_lmd->lmd_stx.stx_mode;
5298 uint64_t blocks = param->fp_lmd->lmd_stx.stx_blocks;
5299 int rc = 1; /* most specifiers are single character */
5307 case 'w': case 'W': /* timestamps */
5308 rc = printf_format_timestamp(seq, buffer, size, wrote, param);
5310 case 'b': /* file size (in 512B blocks) */
5311 *wrote = snprintf(buffer, size, "%"PRIu64, blocks);
5313 case 'G': /* GID of owner */
5314 *wrote = snprintf(buffer, size, "%u",
5315 param->fp_lmd->lmd_stx.stx_gid);
5317 case 'k': /* file size (in 1K blocks) */
5318 *wrote = snprintf(buffer, size, "%"PRIu64, (blocks + 1)/2);
5320 case 'L': /* Lustre-specific formats */
5321 rc = printf_format_lustre(seq, buffer, size, wrote, param,
5324 case 'm': /* file mode in octal */
5325 *wrote = snprintf(buffer, size, "%#o", (mode & (~S_IFMT)));
5327 case 'p': /* Path name of file */
5328 *wrote = snprintf(buffer, size, "%s", path);
5330 case 's': /* file size (in bytes) */
5331 *wrote = snprintf(buffer, size, "%"PRIu64,
5332 (uint64_t) param->fp_lmd->lmd_stx.stx_size);
5334 case 'U': /* UID of owner */
5335 *wrote = snprintf(buffer, size, "%u",
5336 param->fp_lmd->lmd_stx.stx_uid);
5338 case 'y': /* file type */
5341 else if (S_ISDIR(mode))
5343 else if (S_ISLNK(mode))
5345 else if (S_ISBLK(mode))
5347 else if (S_ISCHR(mode))
5349 else if (S_ISFIFO(mode))
5351 else if (S_ISSOCK(mode))
5361 default: /* invalid format specifier */
5367 /* output of snprintf was truncated */
5374 * Parse user-supplied string for the -printf option and interpret any
5375 * '%' format specifiers or '\' escape sequences.
5377 * @param[in] param The find_param struct containing the -printf string
5378 * as well as info about the current file/dir that mathced
5379 * the lfs find search criteria
5380 * @param[in] path Path name for current file/dir
5381 * @param[in] projid Project ID associated with current file/dir
5382 * @param[in] d File descriptor for current directory (or -1 for a
5383 * non-directory file)
5385 void printf_format_string(struct find_param *param, char *path,
5388 char output[FORMATTED_BUF_LEN];
5389 char *fmt_char = param->fp_format_printf_str;
5390 char *buff = output;
5396 buff_size = FORMATTED_BUF_LEN;
5398 /* Always leave one free byte in buffer for trailing NUL */
5399 while (*fmt_char && (buff_size > 1)) {
5402 if (*fmt_char == '%') {
5403 rc = printf_format_directive(fmt_char + 1, buff,
5404 buff_size, &written, param,
5406 } else if (*fmt_char == '\\')
5407 rc = printf_format_escape(fmt_char + 1, buff,
5408 buff_size, &written);
5411 /* Either a '\' escape or '%' format was processed.
5412 * Increment pointers accordingly.
5414 fmt_char += (rc + 1);
5416 buff_size -= written;
5418 /* Regular char or invalid escape/format.
5419 * Either way, copy current character.
5421 *buff++ = *fmt_char++;
5426 /* Terminate output buffer and print */
5428 llapi_printf(LLAPI_MSG_NORMAL, "%s", output);
5432 * Get file/directory project id.
5433 * by the open fd resides on.
5434 * Return 0 and project id on success, or -ve errno.
5436 static int fget_projid(int fd, int *projid)
5441 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
5445 *projid = fsx.fsx_projid;
5450 * Check that the file's permissions in *st matches the one in find_param
5452 static int check_file_permissions(const struct find_param *param,
5459 switch (param->fp_perm_sign) {
5460 case LFS_FIND_PERM_EXACT:
5461 decision = (mode == param->fp_perm);
5463 case LFS_FIND_PERM_ALL:
5464 decision = ((mode & param->fp_perm) == param->fp_perm);
5466 case LFS_FIND_PERM_ANY:
5467 decision = ((mode & param->fp_perm) != 0);
5471 if ((param->fp_exclude_perm && decision)
5472 || (!param->fp_exclude_perm && !decision))
5478 static int cb_find_init(char *path, int p, int *dp,
5479 void *data, struct dirent64 *de)
5481 struct find_param *param = (struct find_param *)data;
5482 struct lov_user_mds_data *lmd = param->fp_lmd;
5483 int d = dp == NULL ? -1 : *dp;
5484 int decision = 1; /* 1 is accepted; -1 is rejected. */
5486 int checked_type = 0;
5488 __u32 stripe_count = 0;
5492 bool gather_all = false;
5494 if (p == -1 && d == -1)
5497 /* Reset this value between invocations */
5498 param->fp_get_lmv = 0;
5500 /* Gather all file/dir info, not just what's needed for search params */
5501 if (param->fp_format_printf_str)
5504 /* If a regular expression is presented, make the initial decision */
5505 if (param->fp_pattern != NULL) {
5506 char *fname = strrchr(path, '/');
5508 fname = (fname == NULL ? path : fname + 1);
5509 ret = fnmatch(param->fp_pattern, fname, 0);
5510 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
5511 (ret == 0 && param->fp_exclude_pattern))
5515 /* See if we can check the file type from the dirent. */
5516 if (de != NULL && de->d_type != DT_UNKNOWN) {
5517 if (param->fp_type != 0) {
5520 if (DTTOIF(de->d_type) == param->fp_type) {
5521 if (param->fp_exclude_type)
5524 if (!param->fp_exclude_type)
5528 if ((param->fp_check_mdt_count || param->fp_hash_type ||
5529 param->fp_check_hash_flag) && de->d_type != DT_DIR)
5536 * Request MDS for the stat info if some of these parameters need
5539 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
5540 param->fp_check_uid || param->fp_check_gid ||
5541 param->fp_newerxy || param->fp_btime ||
5542 param->fp_atime || param->fp_mtime || param->fp_ctime ||
5543 param->fp_check_size || param->fp_check_blocks ||
5544 find_check_lmm_info(param) ||
5545 param->fp_check_mdt_count || param->fp_hash_type ||
5546 param->fp_check_hash_flag || param->fp_perm_sign ||
5550 if (param->fp_type != 0 && checked_type == 0)
5553 if (decision == 0) {
5555 (param->fp_check_mdt_count || param->fp_hash_type ||
5556 param->fp_check_hash_flag || param->fp_check_foreign ||
5558 param->fp_get_lmv = 1;
5559 ret = cb_get_dirstripe(path, &d, param);
5561 if (errno == ENODATA) {
5562 /* Fill in struct for unstriped dir */
5564 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
5565 /* Use 0 until we find actual offset */
5566 param->fp_lmv_md->lum_stripe_offset = 0;
5567 param->fp_lmv_md->lum_stripe_count = 0;
5568 param->fp_lmv_md->lum_hash_type = 0;
5570 if (param->fp_check_foreign) {
5571 if (param->fp_exclude_foreign)
5580 if (param->fp_check_mdt_count) {
5581 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5584 decision = find_value_cmp(param->fp_lmv_md->lum_stripe_count,
5585 param->fp_mdt_count,
5586 param->fp_mdt_count_sign,
5587 param->fp_exclude_mdt_count, 1, 0);
5592 if (param->fp_hash_type) {
5594 __u32 type = param->fp_lmv_md->lum_hash_type &
5597 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5600 found = (1 << type) & param->fp_hash_type;
5601 if ((found && param->fp_exclude_hash_type) ||
5602 (!found && !param->fp_exclude_hash_type))
5606 if (param->fp_check_hash_flag) {
5607 __u32 flags = param->fp_lmv_md->lum_hash_type &
5608 ~LMV_HASH_TYPE_MASK;
5610 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5613 if (!(flags & param->fp_hash_inflags) ||
5614 (flags & param->fp_hash_exflags))
5619 param->fp_lmd->lmd_lmm.lmm_magic = 0;
5620 ret = get_lmd_info_fd(path, p, d, param->fp_lmd,
5621 param->fp_lum_size, GET_LMD_INFO);
5622 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
5623 find_check_lmm_info(param)) {
5624 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
5627 * We need to "fake" the "use the default" values
5628 * since the lmm struct is zeroed out at this point.
5630 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5631 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
5633 ostid_set_seq(&lmm->lmm_oi,
5634 FID_SEQ_LOV_DEFAULT);
5635 lmm->lmm_stripe_size = 0;
5636 lmm->lmm_stripe_count = 0;
5637 lmm->lmm_stripe_offset = -1;
5639 if (ret == 0 && (param->fp_mdt_uuid != NULL || gather_all)) {
5641 ret = llapi_file_fget_mdtidx(d,
5642 ¶m->fp_file_mdt_index);
5644 * Make sure lum_stripe_offset matches
5645 * mdt_index even for unstriped directories.
5647 if (ret == 0 && param->fp_get_lmv)
5648 param->fp_lmv_md->lum_stripe_offset =
5649 param->fp_file_mdt_index;
5650 } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
5652 * FIXME: we could get the MDT index from the
5653 * file's FID in lmd->lmd_lmm.lmm_oi without
5654 * opening the file, once we are sure that
5655 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
5656 * That would still be an ioctl() to map the
5657 * FID to the MDT, but not an open RPC.
5659 fd = open(path, O_RDONLY);
5661 ret = llapi_file_fget_mdtidx(fd,
5662 ¶m->fp_file_mdt_index);
5668 * For a special file, we assume it resides on
5669 * the same MDT as the parent directory.
5671 ret = llapi_file_fget_mdtidx(p,
5672 ¶m->fp_file_mdt_index);
5683 stripe_count = find_get_stripe_count(param);
5687 /* Check the file permissions from the stat info */
5688 if (param->fp_perm_sign) {
5689 decision = check_file_permissions(param, lmd->lmd_stx.stx_mode);
5694 if (param->fp_type && !checked_type) {
5695 if ((param->fp_check_mdt_count || param->fp_check_hash_flag ||
5696 param->fp_hash_type) && !S_ISDIR(lmd->lmd_stx.stx_mode))
5699 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
5700 if (param->fp_exclude_type)
5703 if (!param->fp_exclude_type)
5709 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
5710 if (lustre_fs && param->fp_got_uuids &&
5711 param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
5712 lmd->lmd_stx.stx_dev_minor)) {
5713 /* A lustre/lustre mount point is crossed. */
5714 param->fp_got_uuids = 0;
5715 param->fp_obds_printed = 0;
5716 param->fp_mdt_index = OBD_NOT_FOUND;
5717 param->fp_obd_index = OBD_NOT_FOUND;
5720 if (lustre_fs && !param->fp_got_uuids) {
5721 ret = setup_target_indexes((d != -1) ? d : p, path,
5726 param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
5727 lmd->lmd_stx.stx_dev_minor);
5728 } else if (!lustre_fs && param->fp_got_uuids) {
5729 /* A lustre/non-lustre mount point is crossed. */
5730 param->fp_got_uuids = 0;
5731 param->fp_mdt_index = OBD_NOT_FOUND;
5732 param->fp_obd_index = OBD_NOT_FOUND;
5736 if (param->fp_check_foreign) {
5737 decision = find_check_foreign(param);
5742 if (param->fp_check_stripe_size) {
5743 decision = find_check_stripe_size(param);
5748 if (param->fp_check_ext_size) {
5749 decision = find_check_ext_size(param);
5754 if (param->fp_check_stripe_count) {
5755 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
5756 param->fp_stripe_count_sign,
5757 param->fp_exclude_stripe_count, 1, 0);
5762 if (param->fp_check_layout) {
5763 decision = find_check_layout(param);
5768 /* If an OBD UUID is specified but none matches, skip this file. */
5769 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
5770 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
5774 * If an OST or MDT UUID is given, and some OST matches,
5777 if (param->fp_obd_index != OBD_NOT_FOUND ||
5778 param->fp_mdt_index != OBD_NOT_FOUND) {
5779 if (param->fp_obd_uuid) {
5780 if (check_obd_match(param)) {
5782 * If no mdtuuid is given, we are done.
5783 * Otherwise, fall through to the mdtuuid
5786 if (!param->fp_mdt_uuid)
5793 if (param->fp_mdt_uuid) {
5794 if (check_mdt_match(param))
5801 if (param->fp_check_uid) {
5802 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
5803 if (param->fp_exclude_uid)
5806 if (!param->fp_exclude_uid)
5811 if (param->fp_check_gid) {
5812 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
5813 if (param->fp_exclude_gid)
5816 if (!param->fp_exclude_gid)
5821 if (param->fp_check_projid || gather_all) {
5825 fd = open(path, O_RDONLY);
5828 ret = fget_projid(fd, &projid);
5833 if (projid == param->fp_projid) {
5834 if (param->fp_exclude_projid)
5837 if (!param->fp_exclude_projid)
5842 if (param->fp_check_pool) {
5843 decision = find_check_pool(param);
5848 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
5849 param->fp_check_comp_start || param->fp_check_comp_end) {
5850 decision = find_check_comp_options(param);
5855 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
5856 decision = find_check_mirror_options(param);
5861 /* Check the time on mds. */
5863 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
5866 for_mds = lustre_fs ?
5867 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5868 decision = find_time_check(param, for_mds);
5873 if (param->fp_btime) {
5874 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
5879 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
5880 param->fp_btime, param->fp_bsign,
5881 param->fp_exclude_btime,
5882 param->fp_time_margin, 0);
5887 if (param->fp_newerxy) {
5890 for_mds = lustre_fs ?
5891 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5892 decision = find_newerxy_check(param, for_mds, true);
5901 flags = param->fp_lmd->lmd_flags;
5902 if (param->fp_check_size &&
5903 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5904 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5905 !(flags & OBD_MD_FLSIZE ||
5906 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
5909 if (param->fp_check_blocks &&
5910 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5911 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5912 !(flags & OBD_MD_FLBLOCKS ||
5913 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
5917 * If file still fits the request, ask ost for updated info.
5918 * The regular stat is almost of the same speed as some new
5919 * 'glimpse-size-ioctl'.
5921 if (!decision || gather_all) {
5925 * For regular files with the stripe the decision may have not
5926 * been taken yet if *time or size is to be checked.
5928 if (param->fp_obd_index != OBD_NOT_FOUND)
5929 print_failed_tgt(param, path, LL_STATFS_LOV);
5931 if (param->fp_mdt_index != OBD_NOT_FOUND)
5932 print_failed_tgt(param, path, LL_STATFS_LMV);
5935 ret = fstat_f(d, &st);
5936 else if (de != NULL)
5937 ret = fstatat_f(p, de->d_name, &st,
5938 AT_SYMLINK_NOFOLLOW);
5940 ret = lstat_f(path, &st);
5943 if (errno == ENOENT) {
5944 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
5945 "warning: %s: %s does not exist",
5950 llapi_error(LLAPI_MSG_ERROR, ret,
5951 "%s: stat on %s failed",
5957 convert_lmd_statx(param->fp_lmd, &st, true);
5958 /* Check the time on osc. */
5959 decision = find_time_check(param, 0);
5963 if (param->fp_newerxy) {
5964 decision = find_newerxy_check(param, 0, false);
5974 if (param->fp_check_size) {
5975 decision = find_value_cmp(lmd->lmd_stx.stx_size,
5977 param->fp_size_sign,
5978 param->fp_exclude_size,
5979 param->fp_size_units, 0);
5984 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
5985 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
5987 param->fp_blocks_sign,
5988 param->fp_exclude_blocks,
5989 param->fp_blocks_units, 0);
5995 if (param->fp_format_printf_str)
5996 printf_format_string(param, path, projid, d);
5998 llapi_printf(LLAPI_MSG_NORMAL, "%s%c", path,
5999 param->fp_zero_end ? '\0' : '\n');
6003 /* Do not get down anymore? */
6004 if (param->fp_depth == param->fp_max_depth) {
6015 static int cb_migrate_mdt_init(char *path, int p, int *dp,
6016 void *param_data, struct dirent64 *de)
6018 struct find_param *param = (struct find_param *)param_data;
6019 struct lmv_user_md *lmu = param->fp_lmv_md;
6021 char raw[MAX_IOC_BUFLEN] = {'\0'};
6023 struct obd_ioctl_data data = { 0 };
6029 if (p == -1 && dp == NULL)
6035 if (dp != NULL && *dp != -1)
6039 tmp_p = open_parent(path);
6043 llapi_error(LLAPI_MSG_ERROR, ret,
6044 "can not open %s", path);
6049 path_copy = strdup(path);
6050 filename = basename(path_copy);
6052 data.ioc_inlbuf1 = (char *)filename;
6053 data.ioc_inllen1 = strlen(filename) + 1;
6054 data.ioc_inlbuf2 = (char *)lmu;
6055 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
6058 if (param->fp_depth == param->fp_max_depth)
6059 data.ioc_type = MDS_MIGRATE_NSONLY;
6060 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
6062 llapi_error(LLAPI_MSG_ERROR, ret,
6063 "%s: error packing ioctl data", __func__);
6068 ret = ioctl(tmp_p, LL_IOC_MIGRATE, rawbuf);
6070 if (errno == EBUSY && !retry) {
6072 * because migrate may not be able to lock all involved
6073 * objects in order, for some of them it try lock, while
6074 * there may be conflicting COS locks and cause migrate
6075 * fail with EBUSY, hope a sync() could cause
6076 * transaction commit and release these COS locks.
6081 } else if (errno == EALREADY) {
6082 if (param->fp_verbose & VERBOSE_DETAIL)
6083 llapi_printf(LLAPI_MSG_NORMAL,
6084 "%s migrated to MDT%d already\n",
6085 path, lmu->lum_stripe_offset);
6089 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
6093 } else if (param->fp_verbose & VERBOSE_DETAIL) {
6094 llapi_printf(LLAPI_MSG_NORMAL,
6095 "migrate %s to MDT%d stripe count %d\n",
6096 path, lmu->lum_stripe_offset,
6097 lmu->lum_stripe_count);
6101 /* Do not get down anymore? */
6102 if (param->fp_depth == param->fp_max_depth)
6108 * If the directory is being migration, we need
6109 * close the directory after migration,
6110 * so the old directory cache will be cleanup
6111 * on the client side, and re-open to get the
6112 * new directory handle
6114 *dp = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
6117 llapi_error(LLAPI_MSG_ERROR, ret,
6118 "%s: Failed to open '%s'", __func__, path);
6130 /* dir migration finished, shrink its stripes */
6131 static int cb_migrate_mdt_fini(char *path, int p, int *dp, void *data,
6132 struct dirent64 *de)
6134 struct find_param *param = data;
6135 struct lmv_user_md *lmu = param->fp_lmv_md;
6136 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
6139 if (de && de->d_type != DT_DIR)
6144 * close it before setxattr because the latter may destroy the
6145 * original object, and cause close fail.
6153 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
6154 if (ret == -EALREADY)
6157 cb_common_fini(path, p, dp, data, de);
6161 int llapi_migrate_mdt(char *path, struct find_param *param)
6163 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
6167 int llapi_mv(char *path, struct find_param *param)
6169 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
6170 static bool printed;
6173 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
6174 "%s() is deprecated, use llapi_migrate_mdt() instead",
6179 return llapi_migrate_mdt(path, param);
6183 * Check string for escape sequences and print a message to stdout
6184 * if any invalid escapes are found.
6186 * @param[in] c Pointer to character immediately following the
6187 * '\' character indicating the start of an escape
6189 * @return Number of characters examined in the escape sequence
6190 * (regardless of whether the sequence is valid or not).
6192 int validate_printf_esc(char *c)
6194 char *valid_esc = "nt\\";
6197 /* backslash at end of string */
6198 llapi_err_noerrno(LLAPI_MSG_WARN,
6199 "warning: '\\' at end of -printf format string\n");
6203 if (!strchr(valid_esc, *c))
6204 /* Invalid escape character */
6205 llapi_err_noerrno(LLAPI_MSG_WARN,
6206 "warning: unrecognized escape: '\\%c'\n", *c);
6212 * Check string for format directives and print a message to stdout
6213 * if any invalid directives are found.
6215 * @param[in] c Pointer to character immediately following the
6216 * '%' character indicating the start of a format
6218 * @return Number of characters examined in the format directive
6219 * (regardless of whether the directive is valid or not).
6221 int validate_printf_fmt(char *c)
6223 char *valid_fmt_single = "abcGkmpstUwy%";
6224 char *valid_fmt_double = "ACTW";
6225 char *valid_fmt_lustre = "cFhioPpS";
6226 char curr = *c, next;
6229 llapi_err_noerrno(LLAPI_MSG_WARN,
6230 "warning: '%%' at end of -printf format string\n");
6235 if ((next == '\0') || (next == '%') || (next == '\\'))
6236 /* Treat as single char format directive */
6239 /* Check format directives with multiple characters */
6240 if (strchr(valid_fmt_double, curr)) {
6241 /* For now, only valid formats are followed by '@' char */
6243 llapi_err_noerrno(LLAPI_MSG_WARN,
6244 "warning: unrecognized format directive: '%%%c%c'\n",
6249 /* Lustre formats always start with 'L' */
6251 if (!strchr(valid_fmt_lustre, next))
6252 llapi_err_noerrno(LLAPI_MSG_WARN,
6253 "warning: unrecognized format directive: '%%%c%c'\n",
6260 if (!strchr(valid_fmt_single, curr))
6261 llapi_err_noerrno(LLAPI_MSG_WARN,
6262 "warning: unrecognized format directive: '%%%c'\n", curr);
6267 * Validate the user-supplied string for the -printf option and report
6268 * any invalid backslash escape sequences or format directives.
6270 * @param[in] param Structure containing info about invocation of lfs find
6273 void validate_printf_str(struct find_param *param)
6275 char *c = param->fp_format_printf_str;
6281 ret = validate_printf_fmt(++c);
6285 ret = validate_printf_esc(++c);
6295 int llapi_find(char *path, struct find_param *param)
6297 if (param->fp_format_printf_str)
6298 validate_printf_str(param);
6299 return param_callback(path, cb_find_init, cb_common_fini, param);
6303 * Get MDT number that the file/directory inode referenced
6304 * by the open fd resides on.
6305 * Return 0 and mdtidx on success, or -ve errno.
6307 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
6309 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
6314 static int cb_get_mdt_index(char *path, int p, int *dp, void *data,
6315 struct dirent64 *de)
6317 struct find_param *param = (struct find_param *)data;
6318 int d = dp == NULL ? -1 : *dp;
6322 if (p == -1 && d == -1)
6326 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6327 } else /* if (p != -1) */ {
6330 fd = open(path, O_RDONLY | O_NOCTTY);
6332 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
6340 if (ret == -ENODATA) {
6341 if (!param->fp_obd_uuid)
6342 llapi_printf(LLAPI_MSG_NORMAL,
6343 "'%s' has no stripe info\n", path);
6345 } else if (ret == -ENOENT) {
6346 llapi_error(LLAPI_MSG_WARN, ret,
6347 "warning: %s: '%s' does not exist",
6350 } else if (ret == -ENOTTY) {
6351 llapi_error(LLAPI_MSG_ERROR, ret,
6352 "%s: '%s' not on a Lustre fs",
6355 llapi_error(LLAPI_MSG_ERROR, ret,
6356 "error: %s: '%s' failed get_mdtidx",
6362 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
6363 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
6365 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
6369 /* Do not go down anymore? */
6370 if (param->fp_depth == param->fp_max_depth)
6378 static int cb_getstripe(char *path, int p, int *dp, void *data,
6379 struct dirent64 *de)
6381 struct find_param *param = (struct find_param *)data;
6382 int d = dp == NULL ? -1 : *dp;
6385 if (p == -1 && d == -1)
6388 if (param->fp_obd_uuid) {
6389 param->fp_quiet = 1;
6390 ret = setup_obd_uuid(d != -1 ? d : p, path, param);
6395 if (d != -1 && (param->fp_get_lmv || param->fp_get_default_lmv))
6396 ret = cb_get_dirstripe(path, &d, param);
6398 (p != -1 && !param->fp_get_lmv && !param->fp_get_default_lmv))
6399 ret = get_lmd_info_fd(path, p, d, ¶m->fp_lmd->lmd_lmm,
6400 param->fp_lum_size, GET_LMD_STRIPE);
6401 else if (d == -1 && (param->fp_get_lmv || param->fp_get_default_lmv)) {
6402 /* in case of a dangling or valid faked symlink dir, opendir()
6403 * should have return either EINVAL or ENOENT, so let's try
6404 * to get LMV just in case, and by opening it as a file but
6405 * with O_NOFOLLOW ...
6407 int fd = open(path, O_RDONLY | O_NOFOLLOW);
6411 ret = cb_get_dirstripe(path, &fd, param);
6413 llapi_lov_dump_user_lmm(param, path, LDF_IS_DIR);
6420 if (errno == ENODATA && d != -1) {
6422 * We need to "fake" the "use the default" values
6423 * since the lmm struct is zeroed out at this point.
6424 * The magic needs to be set in order to satisfy
6425 * a check later on in the code path.
6426 * The object_seq needs to be set for the "(Default)"
6427 * prefix to be displayed.
6429 if (param->fp_get_default_lmv) {
6430 struct lmv_user_md *lum = param->fp_lmv_md;
6432 lum->lum_magic = LMV_USER_MAGIC;
6433 lum->lum_stripe_count = 0;
6434 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
6436 } else if (param->fp_get_lmv) {
6437 struct lmv_user_md *lum = param->fp_lmv_md;
6440 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6443 lum->lum_magic = LMV_MAGIC_V1;
6444 lum->lum_stripe_count = 0;
6445 lum->lum_stripe_offset = mdtidx;
6448 struct lov_user_md *lmm =
6449 ¶m->fp_lmd->lmd_lmm;
6451 lmm->lmm_magic = LOV_USER_MAGIC_V1;
6453 ostid_set_seq(&lmm->lmm_oi,
6454 FID_SEQ_LOV_DEFAULT);
6455 lmm->lmm_stripe_count = 0;
6456 lmm->lmm_stripe_size = 0;
6457 lmm->lmm_stripe_offset = -1;
6460 } else if (errno == ENODATA && p != -1) {
6461 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
6462 llapi_printf(LLAPI_MSG_NORMAL,
6463 "%s has no stripe info\n", path);
6465 } else if (errno == ENOENT) {
6466 llapi_error(LLAPI_MSG_WARN, -ENOENT,
6467 "warning: %s: %s does not exist",
6470 } else if (errno == ENOTTY) {
6472 llapi_error(LLAPI_MSG_ERROR, ret,
6473 "%s: '%s' not on a Lustre fs?",
6478 llapi_error(LLAPI_MSG_ERROR, ret,
6479 "error: %s: %s failed for %s",
6481 "LL_IOC_LOV_GETSTRIPE" :
6482 "IOC_MDC_GETFILESTRIPE", path);
6489 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
6490 llapi_lov_dump_user_lmm(param, path, d != -1 ? LDF_IS_DIR : 0);
6493 /* Do not get down anymore? */
6494 if (param->fp_depth == param->fp_max_depth)
6502 int llapi_getstripe(char *path, struct find_param *param)
6504 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
6505 cb_get_mdt_index : cb_getstripe,
6506 cb_common_fini, param);
6509 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
6510 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6512 char raw[MAX_IOC_BUFLEN] = {'\0'};
6514 struct obd_ioctl_data data = { 0 };
6517 data.ioc_inlbuf1 = (char *)&type;
6518 data.ioc_inllen1 = sizeof(__u32);
6519 data.ioc_inlbuf2 = (char *)&index;
6520 data.ioc_inllen2 = sizeof(__u32);
6521 data.ioc_pbuf1 = (char *)stat_buf;
6522 data.ioc_plen1 = sizeof(struct obd_statfs);
6523 data.ioc_pbuf2 = (char *)uuid_buf;
6524 data.ioc_plen2 = sizeof(struct obd_uuid);
6526 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
6528 llapi_error(LLAPI_MSG_ERROR, rc,
6529 "%s: error packing ioctl data", __func__);
6533 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
6535 return rc < 0 ? -errno : 0;
6538 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
6539 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6544 fd = open(path, O_RDONLY);
6547 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
6550 * If we can't even open a file on the filesystem (e.g. with
6551 * -ESHUTDOWN), force caller to exit or it will loop forever.
6556 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
6563 #define MAX_STRING_SIZE 128
6565 int llapi_ping(char *obd_type, char *obd_name)
6567 int flags = O_RDONLY;
6568 char buf[1] = { 0 };
6572 rc = cfs_get_param_paths(&path, "%s/%s/ping",
6573 obd_type, obd_name);
6577 fd = open(path.gl_pathv[0], flags);
6579 if (errno == EACCES && flags == O_RDONLY) {
6584 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
6589 if (flags == O_RDONLY)
6590 rc = read(fd, buf, sizeof(buf));
6592 rc = write(fd, buf, sizeof(buf));
6600 cfs_free_param_data(&path);
6604 int llapi_target_iterate(int type_num, char **obd_type,
6605 void *args, llapi_cb_t cb)
6611 for (i = 0; i < type_num; i++) {
6614 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
6618 for (j = 0; j < param.gl_pathc; j++) {
6619 char obd_uuid[UUID_MAX + 1];
6623 fp = fopen(param.gl_pathv[j], "r");
6626 llapi_error(LLAPI_MSG_ERROR, rc,
6627 "error: opening '%s'",
6632 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
6634 llapi_error(LLAPI_MSG_ERROR, rc,
6635 "error: reading '%s'",
6640 /* Extract the obd_name from the sysfs path.
6641 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
6643 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
6649 /* skip /fs/lustre/'obd_type'/ */
6650 obd_name += strlen(obd_type[i]) + 12;
6651 /* chop off after obd_name */
6652 ptr = strrchr(obd_name, '/');
6656 cb(obd_type[i], obd_name, obd_uuid, args);
6665 cfs_free_param_data(¶m);
6669 static void do_target_check(char *obd_type_name, char *obd_name,
6670 char *obd_uuid, void *args)
6674 rc = llapi_ping(obd_type_name, obd_name);
6676 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
6678 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
6680 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
6683 int llapi_target_check(int type_num, char **obd_type, char *dir)
6685 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
6688 #undef MAX_STRING_SIZE
6690 /* Is this a lustre fs? */
6691 int llapi_is_lustre_mnttype(const char *type)
6693 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
6696 /* Is this a lustre client fs? */
6697 int llapi_is_lustre_mnt(struct mntent *mnt)
6699 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
6700 strstr(mnt->mnt_fsname, ":/") != NULL);
6703 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
6705 char fsname[PATH_MAX + 1];
6709 rc = llapi_search_fsname(mnt, fsname);
6713 root = open(mnt, O_RDONLY | O_DIRECTORY);
6716 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
6720 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
6723 if (rc == -ENOENT && LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd))
6724 llapi_error(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, rc,
6725 "Cannot find pool '%s'", qctl->qc_poolname);
6731 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
6736 root = open(mnt, O_RDONLY | O_DIRECTORY);
6739 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
6743 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
6746 llapi_error(LLAPI_MSG_ERROR, rc,
6747 "ioctl on %s for getting connect flags failed", mnt);
6754 * Flush cached pages from all clients.
6756 * \param fd File descriptor
6760 int llapi_file_flush(int fd)
6764 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);