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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/utils/liblustreapi.c
34 * Author: Peter J. Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Robert Read <rread@clusterfs.com>
50 #include <sys/ioctl.h>
57 #include <sys/statfs.h>
58 #include <sys/syscall.h>
60 #include <sys/types.h>
61 #include <sys/xattr.h>
64 #include <libgen.h> /* for dirname() */
65 #ifdef HAVE_LINUX_UNISTD_H
66 #include <linux/unistd.h>
73 #include <libcfs/util/ioctl.h>
74 #include <libcfs/util/param.h>
75 #include <libcfs/util/string.h>
76 #include <linux/lnet/lnetctl.h>
77 #include <lustre/lustreapi.h>
78 #include <linux/lustre/lustre_ostid.h>
79 #include <linux/lustre/lustre_ioctl.h>
80 #include "lustreapi_internal.h"
82 static int llapi_msg_level = LLAPI_MSG_MAX;
83 const char *liblustreapi_cmd;
85 char *mdt_hash_name[] = { "none",
86 LMV_HASH_NAME_ALL_CHARS,
87 LMV_HASH_NAME_FNV_1A_64 };
89 void llapi_msg_set_level(int level)
91 /* ensure level is in the good range */
92 if (level < LLAPI_MSG_OFF)
93 llapi_msg_level = LLAPI_MSG_OFF;
94 else if (level > LLAPI_MSG_MAX)
95 llapi_msg_level = LLAPI_MSG_MAX;
97 llapi_msg_level = level;
100 int llapi_msg_get_level(void)
102 return llapi_msg_level;
105 void llapi_set_command_name(const char *cmd)
107 liblustreapi_cmd = cmd;
110 void llapi_clear_command_name(void)
112 liblustreapi_cmd = NULL;
115 static void error_callback_default(enum llapi_message_level level, int err,
116 const char *fmt, va_list ap)
118 if (liblustreapi_cmd != NULL)
119 fprintf(stderr, "%s %s: ", program_invocation_short_name,
122 fprintf(stderr, "%s: ", program_invocation_short_name);
123 vfprintf(stderr, fmt, ap);
124 if (level & LLAPI_MSG_NO_ERRNO)
125 fprintf(stderr, "\n");
127 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
130 static void info_callback_default(enum llapi_message_level level, int err,
131 const char *fmt, va_list ap)
134 if (liblustreapi_cmd != NULL) {
135 fprintf(stdout, "%s %s: ",
136 program_invocation_short_name,
139 fprintf(stdout, "%s: ", program_invocation_short_name);
142 vfprintf(stdout, fmt, ap);
145 static llapi_log_callback_t llapi_error_callback = error_callback_default;
146 static llapi_log_callback_t llapi_info_callback = info_callback_default;
149 /* llapi_error will preserve errno */
150 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
153 int tmp_errno = errno;
155 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
159 llapi_error_callback(level, abs(err), fmt, args);
164 /* llapi_printf will preserve errno */
165 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
168 int tmp_errno = errno;
170 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
174 llapi_info_callback(level, 0, fmt, args);
180 * Set a custom error logging function. Passing in NULL will reset the logging
181 * callback to its default value.
183 * This function returns the value of the old callback.
185 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
187 llapi_log_callback_t old = llapi_error_callback;
190 llapi_error_callback = cb;
192 llapi_error_callback = error_callback_default;
198 * Set a custom info logging function. Passing in NULL will reset the logging
199 * callback to its default value.
201 * This function returns the value of the old callback.
203 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
205 llapi_log_callback_t old = llapi_info_callback;
208 llapi_info_callback = cb;
210 llapi_info_callback = info_callback_default;
216 * size_units is to be initialized (or zeroed) by caller.
218 int llapi_parse_size(const char *optarg, unsigned long long *size,
219 unsigned long long *size_units, int bytes_spec)
222 char *argbuf = (char *)optarg;
223 unsigned long long frac = 0, frac_d = 1;
225 if (strncmp(optarg, "-", 1) == 0)
228 if (*size_units == 0)
231 *size = strtoull(argbuf, &end, 0);
232 if (end != NULL && *end == '.') {
236 frac = strtoull(argbuf, &end, 10);
237 /* count decimal places */
238 for (i = 0; i < (end - argbuf); i++)
243 if ((*end == 'b') && *(end + 1) == '\0' &&
244 (*size & (~0ULL << (64 - 9))) == 0 &&
246 *size_units = 1 << 9;
247 } else if ((*end == 'b') &&
248 *(end + 1) == '\0' &&
251 } else if ((*end == 'k' || *end == 'K') &&
252 *(end + 1) == '\0' &&
253 (*size & (~0ULL << (64 - 10))) == 0) {
254 *size_units = 1 << 10;
255 } else if ((*end == 'm' || *end == 'M') &&
256 *(end + 1) == '\0' &&
257 (*size & (~0ULL << (64 - 20))) == 0) {
258 *size_units = 1 << 20;
259 } else if ((*end == 'g' || *end == 'G') &&
260 *(end + 1) == '\0' &&
261 (*size & (~0ULL << (64 - 30))) == 0) {
262 *size_units = 1 << 30;
263 } else if ((*end == 't' || *end == 'T') &&
264 *(end + 1) == '\0' &&
265 (*size & (~0ULL << (64 - 40))) == 0) {
266 *size_units = 1ULL << 40;
267 } else if ((*end == 'p' || *end == 'P') &&
268 *(end + 1) == '\0' &&
269 (*size & (~0ULL << (64 - 50))) == 0) {
270 *size_units = 1ULL << 50;
271 } else if ((*end == 'e' || *end == 'E') &&
272 *(end + 1) == '\0' &&
273 (*size & (~0ULL << (64 - 60))) == 0) {
274 *size_units = 1ULL << 60;
279 *size = *size * *size_units + frac * *size_units / frac_d;
284 int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
286 struct obd_ioctl_data *overlay;
289 data->ioc_len = obd_ioctl_packlen(data);
290 data->ioc_version = OBD_IOCTL_VERSION;
292 if (*pbuf != NULL && data->ioc_len > max_len) {
293 fprintf(stderr, "pbuf = %p, ioc_len = %u, max_len = %d\n",
294 *pbuf, data->ioc_len, max_len);
299 *pbuf = malloc(data->ioc_len);
304 overlay = (struct obd_ioctl_data *)*pbuf;
305 memcpy(*pbuf, data, sizeof(*data));
307 ptr = overlay->ioc_bulk;
308 if (data->ioc_inlbuf1) {
309 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
310 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
313 if (data->ioc_inlbuf2) {
314 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
315 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
318 if (data->ioc_inlbuf3) {
319 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
320 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
323 if (data->ioc_inlbuf4) {
324 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
325 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
331 int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
333 struct obd_ioctl_data *overlay;
339 overlay = (struct obd_ioctl_data *)pbuf;
341 /* Preserve the caller's buffer pointers */
342 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
343 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
344 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
345 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
347 memcpy(data, pbuf, sizeof(*data));
349 ptr = overlay->ioc_bulk;
350 if (data->ioc_inlbuf1) {
351 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
352 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
355 if (data->ioc_inlbuf2) {
356 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
357 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
360 if (data->ioc_inlbuf3) {
361 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
362 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
365 if (data->ioc_inlbuf4) {
366 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
367 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
373 /* XXX: llapi_xxx() functions return negative values upon failure */
375 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
376 int stripe_count, int stripe_pattern)
380 /* 64 KB is the largest common page size I'm aware of (on ia64), but
381 * check the local page size just in case. */
382 page_size = LOV_MIN_STRIPE_SIZE;
383 if (getpagesize() > page_size) {
384 page_size = getpagesize();
385 llapi_err_noerrno(LLAPI_MSG_WARN,
386 "warning: your page size (%u) is "
387 "larger than expected (%u)", page_size,
388 LOV_MIN_STRIPE_SIZE);
390 if (!llapi_stripe_size_is_aligned(stripe_size)) {
392 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
393 "must be an even multiple of %d bytes",
394 stripe_size, page_size);
397 if (!llapi_stripe_index_is_valid(stripe_offset)) {
399 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
403 if (!llapi_stripe_count_is_valid(stripe_count)) {
405 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
409 if (llapi_stripe_size_is_too_big(stripe_size)) {
411 llapi_error(LLAPI_MSG_ERROR, rc,
412 "warning: stripe size 4G or larger "
413 "is not currently supported and would wrap");
419 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
424 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
426 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
430 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
432 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
437 if (!llapi_dir_hash_type_is_valid(hash_type)) {
439 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
447 * Trim a trailing newline from a string, if it exists.
449 int llapi_chomp_string(char *buf)
465 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
467 static int get_param_lov(const char *path, const char *param,
468 char *buf, size_t buf_size)
470 struct obd_uuid uuid;
473 rc = llapi_file_get_lov_uuid(path, &uuid);
477 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
482 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
484 static int get_param_lmv(const char *path, const char *param,
485 char *buf, size_t buf_size)
487 struct obd_uuid uuid;
490 rc = llapi_file_get_lmv_uuid(path, &uuid);
494 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
498 static int get_mds_md_size(const char *path)
500 char buf[PATH_MAX], inst[PATH_MAX];
501 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
504 rc = llapi_getname(path, inst, sizeof(inst));
508 /* Get the max ea size from llite parameters. */
509 rc = get_lustre_param_value("llite", inst, FILTER_BY_EXACT,
510 "max_easize", buf, sizeof(buf));
516 return rc > 0 ? rc : md_size;
519 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
521 return get_param_lmv(path, "uuid", buf, bufsize);
525 * if pool is NULL, search tgtname in target_obd
526 * if pool is not NULL:
527 * if pool not found returns errno < 0
528 * if tgtname is NULL, returns 1 if pool is not empty and 0 if pool empty
529 * if tgtname is not NULL, returns 1 if OST is in pool and 0 if not
531 int llapi_search_tgt(char *fsname, char *poolname, char *tgtname, bool is_mdt)
533 char buffer[PATH_MAX];
539 /* You need one or the other */
540 if (poolname == NULL && fsname == NULL)
544 len = strlen(tgtname);
546 if (poolname == NULL && len == 0)
549 /* Search by poolname and fsname if is not NULL */
550 if (poolname != NULL) {
551 rc = poolpath(¶m, fsname, NULL);
553 snprintf(buffer, sizeof(buffer), "%s/%s",
554 param.gl_pathv[0], poolname);
556 } else if (fsname != NULL) {
557 rc = get_lustre_param_path(is_mdt ? "lmv" : "lov", fsname,
559 "target_obd", ¶m);
561 strncpy(buffer, param.gl_pathv[0],
567 cfs_free_param_data(¶m);
571 fd = fopen(buffer, "r");
575 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
576 if (poolname == NULL) {
578 /* Search for an tgtname in the list of targets
579 * Line format is IDX: fsname-OST/MDTxxxx_UUID STATUS */
580 ptr = strchr(buffer, ' ');
582 (strncmp(ptr + 1, tgtname, len) == 0)) {
587 /* Search for an tgtname in a pool,
588 * (or an existing non-empty pool if no tgtname) */
589 if ((tgtname == NULL) ||
590 (strncmp(buffer, tgtname, len) == 0)) {
600 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
602 return llapi_search_tgt(fsname, poolname, ostname, false);
606 * Open a Lustre file.
608 * \param name the name of the file to be opened
609 * \param flags access mode, see flags in open(2)
610 * \param mode permission of the file if it is created, see mode in open(2)
611 * \param param stripe pattern of the newly created file
613 * \retval file descriptor of opened file
614 * \retval negative errno on failure
616 int llapi_file_open_param(const char *name, int flags, mode_t mode,
617 const struct llapi_stripe_param *param)
619 char fsname[MAX_OBD_NAME + 1] = { 0 };
620 char *pool_name = param->lsp_pool;
621 struct lov_user_md *lum = NULL;
622 size_t lum_size = sizeof(*lum);
625 /* Make sure we are on a Lustre file system */
626 rc = llapi_search_fsname(name, fsname);
628 llapi_error(LLAPI_MSG_ERROR, rc,
629 "'%s' is not on a Lustre filesystem",
634 /* Check if the stripe pattern is sane. */
635 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
636 param->lsp_stripe_offset,
637 param->lsp_stripe_count,
638 param->lsp_stripe_pattern);
642 /* Make sure we have a good pool */
643 if (pool_name != NULL) {
644 /* in case user gives the full pool name <fsname>.<poolname>,
645 * strip the fsname */
646 char *ptr = strchr(pool_name, '.');
649 if (strcmp(pool_name, fsname) != 0) {
651 llapi_err_noerrno(LLAPI_MSG_ERROR,
652 "Pool '%s' is not on filesystem '%s'",
659 /* Make sure the pool exists and is non-empty */
660 rc = llapi_search_ost(fsname, pool_name, NULL);
662 char *err = rc == 0 ? "has no OSTs" : "does not exist";
664 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
665 fsname, pool_name, err);
669 lum_size = sizeof(struct lov_user_md_v3);
672 /* sanity check of target list */
673 if (param->lsp_is_specific) {
674 char ostname[MAX_OBD_NAME + 1];
678 for (i = 0; i < param->lsp_stripe_count; i++) {
679 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
680 fsname, param->lsp_osts[i]);
681 rc = llapi_search_ost(fsname, pool_name, ostname);
686 llapi_error(LLAPI_MSG_ERROR, rc,
687 "%s: cannot find OST %s in %s",
694 /* Make sure stripe offset is in OST list. */
695 if (param->lsp_osts[i] == param->lsp_stripe_offset)
699 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
700 "%s: stripe offset '%d' is not in the "
702 __func__, param->lsp_stripe_offset);
706 lum_size = lov_user_md_size(param->lsp_stripe_count,
707 LOV_USER_MAGIC_SPECIFIC);
710 lum = calloc(1, lum_size);
715 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
717 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
718 flags = O_DIRECTORY | O_RDONLY;
725 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
730 /* Initialize IOCTL striping pattern structure */
731 lum->lmm_magic = LOV_USER_MAGIC_V1;
732 lum->lmm_pattern = param->lsp_stripe_pattern;
733 lum->lmm_stripe_size = param->lsp_stripe_size;
734 lum->lmm_stripe_count = param->lsp_stripe_count;
735 lum->lmm_stripe_offset = param->lsp_stripe_offset;
736 if (pool_name != NULL) {
737 struct lov_user_md_v3 *lumv3 = (void *)lum;
739 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
740 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
742 if (param->lsp_is_specific) {
743 struct lov_user_md_v3 *lumv3 = (void *)lum;
746 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
747 if (pool_name == NULL) {
748 /* LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
749 * OST list, therefore if pool is not specified we have
750 * 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 = "stripe already set";
762 if (errno != EEXIST && errno != EALREADY)
763 errmsg = strerror(errno);
765 llapi_err_noerrno(LLAPI_MSG_ERROR,
766 "setstripe error for '%s': %s", name, errmsg);
777 int llapi_file_open_pool(const char *name, int flags, int mode,
778 unsigned long long stripe_size, int stripe_offset,
779 int stripe_count, int stripe_pattern, char *pool_name)
781 const struct llapi_stripe_param param = {
782 .lsp_stripe_size = stripe_size,
783 .lsp_stripe_count = stripe_count,
784 .lsp_stripe_pattern = stripe_pattern,
785 .lsp_stripe_offset = stripe_offset,
786 .lsp_pool = pool_name
788 return llapi_file_open_param(name, flags, mode, ¶m);
791 int llapi_file_open(const char *name, int flags, int mode,
792 unsigned long long stripe_size, int stripe_offset,
793 int stripe_count, int stripe_pattern)
795 return llapi_file_open_pool(name, flags, mode, stripe_size,
796 stripe_offset, stripe_count,
797 stripe_pattern, NULL);
800 int llapi_file_create(const char *name, unsigned long long stripe_size,
801 int stripe_offset, int stripe_count, int stripe_pattern)
805 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
806 stripe_offset, stripe_count, stripe_pattern,
815 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
816 int stripe_offset, int stripe_count,
817 int stripe_pattern, char *pool_name)
821 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
822 stripe_offset, stripe_count, stripe_pattern,
831 static int verify_dir_param(const char *name,
832 const struct llapi_stripe_param *param)
834 char fsname[MAX_OBD_NAME + 1] = { 0 };
835 char *pool_name = param->lsp_pool;
838 /* Make sure we are on a Lustre file system */
839 rc = llapi_search_fsname(name, fsname);
841 llapi_error(LLAPI_MSG_ERROR, rc,
842 "'%s' is not on a Lustre filesystem",
847 /* Check if the stripe pattern is sane. */
848 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
849 param->lsp_stripe_count,
850 param->lsp_stripe_pattern);
854 /* Make sure we have a good pool */
855 if (pool_name != NULL) {
856 /* in case user gives the full pool name <fsname>.<poolname>,
857 * strip the fsname */
858 char *ptr = strchr(pool_name, '.');
862 if (strcmp(pool_name, fsname) != 0) {
864 llapi_err_noerrno(LLAPI_MSG_ERROR,
865 "Pool '%s' is not on filesystem '%s'",
872 /* Make sure the pool exists and is non-empty */
873 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
875 char *err = rc == 0 ? "has no OSTs" : "does not exist";
877 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
878 fsname, pool_name, err);
883 /* sanity check of target list */
884 if (param->lsp_is_specific) {
885 char mdtname[MAX_OBD_NAME + 1];
889 for (i = 0; i < param->lsp_stripe_count; i++) {
890 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
891 fsname, param->lsp_tgts[i]);
892 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
897 llapi_error(LLAPI_MSG_ERROR, rc,
898 "%s: cannot find MDT %s in %s",
905 /* Make sure stripe offset is in MDT list. */
906 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
910 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
911 "%s: stripe offset '%d' is not in the "
913 __func__, param->lsp_stripe_offset);
921 static inline void param2lmu(struct lmv_user_md *lmu,
922 const struct llapi_stripe_param *param)
924 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
926 lmu->lum_stripe_count = param->lsp_stripe_count;
927 lmu->lum_stripe_offset = param->lsp_stripe_offset;
928 lmu->lum_hash_type = param->lsp_stripe_pattern;
929 if (param->lsp_pool != NULL)
930 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
931 if (param->lsp_is_specific) {
934 for (i = 0; i < param->lsp_stripe_count; i++)
935 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
939 int llapi_dir_set_default_lmv(const char *name,
940 const struct llapi_stripe_param *param)
942 struct lmv_user_md lmu = { 0 };
946 rc = verify_dir_param(name, param);
950 /* TODO: default lmv doesn't support specific targets yet */
951 if (param->lsp_is_specific)
954 param2lmu(&lmu, param);
956 fd = open(name, O_DIRECTORY | O_RDONLY);
959 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
963 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
965 char *errmsg = "stripe already set";
967 if (errno != EEXIST && errno != EALREADY)
968 errmsg = strerror(errno);
970 llapi_err_noerrno(LLAPI_MSG_ERROR,
971 "default dirstripe error on '%s': %s",
978 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
979 int stripe_count, int stripe_pattern,
980 const char *pool_name)
982 const struct llapi_stripe_param param = {
983 .lsp_stripe_count = stripe_count,
984 .lsp_stripe_offset = stripe_offset,
985 .lsp_stripe_pattern = stripe_pattern,
986 .lsp_pool = (char *)pool_name
989 return llapi_dir_set_default_lmv(name, ¶m);
993 * Create a Lustre directory.
995 * \param name the name of the directory to be created
996 * \param mode permission of the file if it is created, see mode in open(2)
997 * \param param stripe pattern of the newly created directory
999 * \retval 0 on success
1000 * \retval negative errno on failure
1002 int llapi_dir_create(const char *name, mode_t mode,
1003 const struct llapi_stripe_param *param)
1005 struct lmv_user_md *lmu = NULL;
1007 struct obd_ioctl_data data = { 0 };
1010 char *dirpath = NULL;
1011 char *namepath = NULL;
1016 rc = verify_dir_param(name, param);
1020 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1021 param->lsp_is_specific ?
1022 LMV_USER_MAGIC_SPECIFIC :
1025 lmu = calloc(1, lmu_size);
1029 dirpath = strdup(name);
1035 namepath = strdup(name);
1042 param2lmu(lmu, param);
1044 filename = basename(namepath);
1045 dir = dirname(dirpath);
1047 data.ioc_inlbuf1 = (char *)filename;
1048 data.ioc_inllen1 = strlen(filename) + 1;
1049 data.ioc_inlbuf2 = (char *)lmu;
1050 data.ioc_inllen2 = lmu_size;
1051 data.ioc_type = mode;
1052 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1054 llapi_error(LLAPI_MSG_ERROR, rc,
1055 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1060 fd = open(dir, O_DIRECTORY | O_RDONLY);
1063 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1067 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1068 char *errmsg = "stripe already set";
1071 if (errno != EEXIST && errno != EALREADY)
1072 errmsg = strerror(errno);
1074 llapi_err_noerrno(LLAPI_MSG_ERROR,
1075 "dirstripe error on '%s': %s", name, errmsg);
1086 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1087 int stripe_count, int stripe_pattern,
1088 const char *pool_name)
1090 const struct llapi_stripe_param param = {
1091 .lsp_stripe_count = stripe_count,
1092 .lsp_stripe_offset = stripe_offset,
1093 .lsp_stripe_pattern = stripe_pattern,
1094 .lsp_pool = (char *)pool_name
1097 return llapi_dir_create(name, mode, ¶m);
1100 int llapi_direntry_remove(char *dname)
1102 char *dirpath = NULL;
1103 char *namepath = NULL;
1109 dirpath = strdup(dname);
1110 namepath = strdup(dname);
1111 if (!dirpath || !namepath)
1114 filename = basename(namepath);
1116 dir = dirname(dirpath);
1118 fd = open(dir, O_DIRECTORY | O_RDONLY);
1121 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
1126 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
1127 char *errmsg = strerror(errno);
1128 llapi_err_noerrno(LLAPI_MSG_ERROR,
1129 "error on ioctl %#jx for '%s' (%d): %s",
1130 (uintmax_t)LL_IOC_LMV_SETSTRIPE, filename,
1142 * Find the fsname, the full path, and/or an open fd.
1143 * Either the fsname or path must not be NULL
1145 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1148 char buf[PATH_MAX], mntdir[PATH_MAX];
1149 char *ptr, *ptr_end;
1151 int idx = 0, len = 0, mntlen, fd;
1154 /* get the mount point */
1155 fp = setmntent(PROC_MOUNTS, "r");
1158 llapi_error(LLAPI_MSG_ERROR, rc,
1159 "cannot retrieve filesystem mount point");
1163 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1166 if (!llapi_is_lustre_mnt(&mnt))
1169 if ((want & WANT_INDEX) && (idx++ != index))
1172 mntlen = strlen(mnt.mnt_dir);
1173 ptr = strchr(mnt.mnt_fsname, '/');
1174 while (ptr && *ptr == '/')
1176 /* thanks to the call to llapi_is_lustre_mnt() above,
1177 * we are sure that mnt.mnt_fsname contains ":/",
1178 * so ptr should never be NULL */
1182 while (*ptr_end != '/' && *ptr_end != '\0')
1185 /* Check the fsname for a match, if given */
1186 if (!(want & WANT_FSNAME) && fsname != NULL &&
1187 (strlen(fsname) > 0) &&
1188 (strncmp(ptr, fsname, ptr_end - ptr) != 0))
1191 /* If the path isn't set return the first one we find */
1192 if (path == NULL || strlen(path) == 0) {
1193 strncpy(mntdir, mnt.mnt_dir, strlen(mnt.mnt_dir));
1194 mntdir[strlen(mnt.mnt_dir)] = '\0';
1195 if ((want & WANT_FSNAME) && fsname != NULL) {
1196 strncpy(fsname, ptr, ptr_end - ptr);
1197 fsname[ptr_end - ptr] = '\0';
1201 /* Otherwise find the longest matching path */
1202 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1203 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1204 strncpy(mntdir, mnt.mnt_dir, strlen(mnt.mnt_dir));
1205 mntdir[strlen(mnt.mnt_dir)] = '\0';
1207 if ((want & WANT_FSNAME) && fsname != NULL) {
1208 strncpy(fsname, ptr, ptr_end - ptr);
1209 fsname[ptr_end - ptr] = '\0';
1218 if ((want & WANT_PATH) && path != NULL) {
1219 strncpy(path, mntdir, strlen(mntdir));
1220 path[strlen(mntdir)] = '\0';
1222 if (want & WANT_FD) {
1223 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1226 llapi_error(LLAPI_MSG_ERROR, rc,
1227 "cannot open '%s': %s", mntdir,
1234 } else if (want & WANT_ERROR)
1235 llapi_err_noerrno(LLAPI_MSG_ERROR,
1236 "'%s' not on a mounted Lustre filesystem",
1237 (want & WANT_PATH) ? fsname : path);
1242 * search lustre mounts
1244 * Calling this function will return to the user the mount point, mntdir, and
1245 * the file system name, fsname, if the user passed a buffer to this routine.
1247 * The user inputs are pathname and index. If the pathname is supplied then
1248 * the value of the index will be ignored. The pathname will return data if
1249 * the pathname is located on a lustre mount. Index is used to pick which
1250 * mount point you want in the case of multiple mounted lustre file systems.
1251 * See function lfs_osts in lfs.c for an example of the index use.
1253 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1256 int want = WANT_PATH, idx = -1;
1258 if (!pathname || pathname[0] == '\0') {
1262 strcpy(mntdir, pathname);
1265 want |= WANT_FSNAME;
1266 return get_root_path(want, fsname, NULL, mntdir, idx);
1269 /* Given a path, find the corresponding Lustre fsname */
1270 int llapi_search_fsname(const char *pathname, char *fsname)
1275 path = realpath(pathname, NULL);
1277 char tmp[PATH_MAX - 1];
1283 if (pathname[0] != '/') {
1284 /* Need an absolute path, but realpath() only works for
1285 * pathnames that actually exist. We go through the
1286 * extra hurdle of dirname(getcwd() + pathname) in
1287 * case the relative pathname contains ".." in it. */
1288 char realpath[PATH_MAX - 1];
1290 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1292 llapi_error(LLAPI_MSG_ERROR, rc,
1293 "cannot get current working directory");
1297 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1298 if (rc >= sizeof(tmp)) {
1300 llapi_error(LLAPI_MSG_ERROR, rc,
1301 "invalid parent path '%s'",
1307 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1308 if (rc >= sizeof(buf)) {
1310 llapi_error(LLAPI_MSG_ERROR, rc,
1311 "invalid path '%s'", pathname);
1314 path = realpath(buf, NULL);
1316 ptr = strrchr(buf, '/');
1318 llapi_error(LLAPI_MSG_ERROR |
1319 LLAPI_MSG_NO_ERRNO, 0,
1320 "cannot resolve path '%s'",
1325 path = realpath(buf, NULL);
1328 llapi_error(LLAPI_MSG_ERROR, rc,
1329 "cannot resolve path '%s'",
1335 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1340 int llapi_search_rootpath(char *pathname, const char *fsname)
1342 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1345 int llapi_getname(const char *path, char *buf, size_t size)
1347 struct obd_uuid uuid_buf;
1348 char *uuid = uuid_buf.uuid;
1350 int rc, len, fsname_len;
1352 memset(&uuid_buf, 0, sizeof(uuid_buf));
1353 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1358 * We want to turn testfs-clilov-ffff88002738bc00 into
1359 * testfs-ffff88002738bc00 in a portable way that doesn't depend
1360 * on what is after "-clilov-" as it may change in the future.
1361 * Unfortunately, the "fsname" part may contain a dash, so we
1362 * can't just skip to the first dash, and the "instance" may be a
1363 * UUID in the future, so we can't necessarily go to the last dash.
1365 cfg_instance = strstr(uuid, "-clilov-");
1369 fsname_len = cfg_instance - uuid;
1370 cfg_instance += strlen("-clilov-");
1371 len = snprintf(buf, size, "%.*s-%s", fsname_len, uuid, cfg_instance);
1380 * Get the list of pool members.
1381 * \param poolname string of format \<fsname\>.\<poolname\>
1382 * \param members caller-allocated array of char*
1383 * \param list_size size of the members array
1384 * \param buffer caller-allocated buffer for storing OST names
1385 * \param buffer_size size of the buffer
1387 * \return number of members retrieved for this pool
1388 * \retval -error failure
1390 int llapi_get_poolmembers(const char *poolname, char **members,
1391 int list_size, char *buffer, int buffer_size)
1393 char fsname[PATH_MAX];
1402 /* name is FSNAME.POOLNAME */
1403 if (strlen(poolname) >= sizeof(fsname))
1406 snprintf(fsname, sizeof(fsname), "%s", poolname);
1407 pool = strchr(fsname, '.');
1414 rc = poolpath(&pathname, fsname, NULL);
1416 llapi_error(LLAPI_MSG_ERROR, rc,
1417 "Lustre filesystem '%s' not found",
1422 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1423 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1424 cfs_free_param_data(&pathname);
1425 if (rc >= sizeof(buf))
1427 fd = fopen(buf, "r");
1430 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1435 while (fgets(buf, sizeof(buf), fd) != NULL) {
1436 if (nb_entries >= list_size) {
1440 buf[sizeof(buf) - 1] = '\0';
1442 tmp = strchr(buf, '\n');
1445 if (used + strlen(buf) + 1 > buffer_size) {
1450 strcpy(buffer + used, buf);
1451 members[nb_entries] = buffer + used;
1452 used += strlen(buf) + 1;
1462 * Get the list of pools in a filesystem.
1463 * \param name filesystem name or path
1464 * \param poollist caller-allocated array of char*
1465 * \param list_size size of the poollist array
1466 * \param buffer caller-allocated buffer for storing pool names
1467 * \param buffer_size size of the buffer
1469 * \return number of pools retrieved for this filesystem
1470 * \retval -error failure
1472 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1473 char *buffer, int buffer_size)
1475 char rname[PATH_MAX];
1480 struct dirent *pool;
1482 unsigned int nb_entries = 0;
1483 unsigned int used = 0;
1486 /* initialize output array */
1487 for (i = 0; i < list_size; i++)
1490 /* is name a pathname ? */
1491 ptr = strchr(name, '/');
1493 /* only absolute pathname is supported */
1497 if (!realpath(name, rname)) {
1499 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1504 fsname = strdup(rname);
1508 rc = poolpath(&pathname, NULL, rname);
1510 /* name is FSNAME */
1511 fsname = strdup(name);
1514 rc = poolpath(&pathname, fsname, NULL);
1517 llapi_error(LLAPI_MSG_ERROR, rc,
1518 "Lustre filesystem '%s' not found", name);
1522 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1523 dir = opendir(pathname.gl_pathv[0]);
1526 llapi_error(LLAPI_MSG_ERROR, rc,
1527 "Could not open pool list for '%s'",
1534 pool = readdir(dir);
1540 /* ignore . and .. */
1541 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1544 /* check output bounds */
1545 if (nb_entries >= list_size) {
1550 /* +2 for '.' and final '\0' */
1551 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1557 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1558 poollist[nb_entries] = buffer + used;
1559 used += strlen(pool->d_name) + strlen(fsname) + 2;
1565 llapi_error(LLAPI_MSG_ERROR, rc,
1566 "Error reading pool list for '%s'", name);
1569 cfs_free_param_data(&pathname);
1572 return rc != 0 ? rc : nb_entries;
1575 /* wrapper for lfs.c and obd.c */
1576 int llapi_poollist(const char *name)
1578 /* list of pool names (assume that pool count is smaller
1580 char **list, *buffer = NULL, *fsname = (char *)name;
1581 char *poolname = NULL, *tmp = NULL, data[16];
1582 enum param_filter type = FILTER_BY_PATH;
1583 int obdcount, bufsize, rc, nb, i;
1588 if (name[0] != '/') {
1589 fsname = strdup(name);
1593 poolname = strchr(fsname, '.');
1596 type = FILTER_BY_FS_NAME;
1599 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1600 data, sizeof(data));
1603 obdcount = atoi(data);
1605 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1606 * and also an array to store the pointers for all that
1607 * allocated space. */
1609 bufsize = sizeof(struct obd_uuid) * obdcount;
1610 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1611 if (buffer == NULL) {
1615 list = (char **) (buffer + bufsize);
1618 /* name is a path or fsname */
1619 nb = llapi_get_poollist(name, list, obdcount,
1622 /* name is a pool name (<fsname>.<poolname>) */
1623 nb = llapi_get_poolmembers(name, list, obdcount,
1627 if (nb == -EOVERFLOW) {
1630 goto retry_get_pools;
1633 for (i = 0; i < nb; i++)
1634 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1635 rc = (nb < 0 ? nb : 0);
1639 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1644 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1645 void *data, struct dirent64 *de);
1647 #define OBD_NOT_FOUND (-1)
1649 static int common_param_init(struct find_param *param, char *path)
1651 int lum_size = get_mds_md_size(path);
1656 /* migrate has fp_lmv_md initialized outside */
1657 if (param->fp_migrate)
1660 if (lum_size < PATH_MAX + 1)
1661 lum_size = PATH_MAX + 1;
1663 param->fp_lum_size = lum_size;
1664 param->fp_lmd = calloc(1, sizeof(lstat_t) + lum_size);
1665 if (param->fp_lmd == NULL) {
1666 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1667 "error: allocation of %zu bytes for ioctl",
1668 sizeof(lstat_t) + param->fp_lum_size);
1672 param->fp_lmv_stripe_count = 256;
1673 param->fp_lmv_md = calloc(1,
1674 lmv_user_md_size(param->fp_lmv_stripe_count,
1675 LMV_USER_MAGIC_SPECIFIC));
1676 if (param->fp_lmv_md == NULL) {
1677 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1678 "error: allocation of %d bytes for ioctl",
1679 lmv_user_md_size(param->fp_lmv_stripe_count,
1680 LMV_USER_MAGIC_SPECIFIC));
1684 param->fp_got_uuids = 0;
1685 param->fp_obd_indexes = NULL;
1686 param->fp_obd_index = OBD_NOT_FOUND;
1687 param->fp_mdt_index = OBD_NOT_FOUND;
1691 static void find_param_fini(struct find_param *param)
1693 if (param->fp_migrate)
1696 if (param->fp_obd_indexes)
1697 free(param->fp_obd_indexes);
1700 free(param->fp_lmd);
1702 if (param->fp_lmv_md)
1703 free(param->fp_lmv_md);
1706 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1707 struct dirent64 *de)
1709 struct find_param *param = data;
1715 /* set errno upon failure */
1716 static DIR *opendir_parent(const char *path)
1722 path_copy = strdup(path);
1723 if (path_copy == NULL)
1726 parent_path = dirname(path_copy);
1727 parent = opendir(parent_path);
1733 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1738 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1739 if (param->fp_get_default_lmv)
1740 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1742 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1744 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1745 if (errno == E2BIG && ret != 0) {
1749 stripe_count = (__u32)param->fp_lmv_md->lum_stripe_count;
1750 if (stripe_count <= param->fp_lmv_stripe_count)
1753 free(param->fp_lmv_md);
1754 param->fp_lmv_stripe_count = stripe_count;
1755 lmv_size = lmv_user_md_size(stripe_count,
1756 LMV_USER_MAGIC_SPECIFIC);
1757 param->fp_lmv_md = malloc(lmv_size);
1758 if (param->fp_lmv_md == NULL) {
1759 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1760 "error: allocation of %d bytes for ioctl",
1761 lmv_user_md_size(param->fp_lmv_stripe_count,
1762 LMV_USER_MAGIC_SPECIFIC));
1770 int get_lmd_info_fd(char *path, int parent_fd, int dir_fd,
1771 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1773 struct lov_user_mds_data *lmd = lmdbuf;
1774 lstat_t *st = &lmd->lmd_st;
1777 if (parent_fd < 0 && dir_fd < 0)
1779 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1783 /* LL_IOC_MDC_GETINFO operates on the current directory inode
1784 * and returns struct lov_user_mds_data, while
1785 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
1787 ret = ioctl(dir_fd, type == GET_LMD_INFO ? LL_IOC_MDC_GETINFO :
1788 LL_IOC_LOV_GETSTRIPE,
1790 } else if (parent_fd >= 0) {
1791 char *fname = strrchr(path, '/');
1793 /* IOC_MDC_GETFILEINFO takes as input the filename (relative to
1794 * the parent directory) and returns struct lov_user_mds_data,
1795 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
1797 * This avoids opening, locking, and closing each file on the
1798 * client if that is not needed. Multiple of these ioctl() can
1799 * be done on the parent dir with a single open for all
1800 * files in that directory, and it also doesn't pollute the
1801 * client dcache with millions of dentries when traversing
1802 * a large filesystem.
1804 fname = (fname == NULL ? path : fname + 1);
1806 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
1809 else if (ret >= lmdlen || ret++ == 0)
1812 ret = ioctl(parent_fd, type == GET_LMD_INFO ?
1813 IOC_MDC_GETFILEINFO :
1814 IOC_MDC_GETFILESTRIPE, lmdbuf);
1817 if (ret && type == GET_LMD_INFO) {
1818 if (errno == ENOTTY) {
1819 /* ioctl is not supported, it is not a lustre fs.
1820 * Do the regular lstat(2) instead.
1822 ret = lstat_f(path, st);
1825 llapi_error(LLAPI_MSG_ERROR, ret,
1826 "error: %s: lstat failed for %s",
1829 } else if (errno == ENOENT) {
1831 llapi_error(LLAPI_MSG_WARN, ret,
1832 "warning: %s does not exist", path);
1833 } else if (errno != EISDIR && errno != ENODATA) {
1835 llapi_error(LLAPI_MSG_ERROR, ret,
1836 "%s ioctl failed for %s.",
1837 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
1838 "IOC_MDC_GETFILEINFO", path);
1845 static int get_lmd_info(char *path, DIR *parent, DIR *dir, void *lmdbuf,
1846 int lmdlen, enum get_lmd_info_type type)
1852 parent_fd = dirfd(parent);
1854 dir_fd = dirfd(dir);
1856 return get_lmd_info_fd(path, parent_fd, dir_fd, lmdbuf, lmdlen, type);
1859 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1860 semantic_func_t sem_init,
1861 semantic_func_t sem_fini, void *data,
1862 struct dirent64 *de)
1864 struct find_param *param = (struct find_param *)data;
1865 struct dirent64 *dent;
1873 if (!d && errno != ENOTDIR) {
1875 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1878 } else if (!d && !parent) {
1879 /* ENOTDIR. Open the parent dir. */
1880 p = opendir_parent(path);
1887 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
1893 while ((dent = readdir64(d)) != NULL) {
1896 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1900 if ((len + dent->d_reclen + 2) > size) {
1901 llapi_err_noerrno(LLAPI_MSG_ERROR,
1902 "error: %s: string buffer too small",
1907 strcat(path, dent->d_name);
1909 if (dent->d_type == DT_UNKNOWN) {
1910 lstat_t *st = ¶m->fp_lmd->lmd_st;
1912 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
1913 param->fp_lum_size, GET_LMD_INFO);
1915 dent->d_type = IFTODT(st->st_mode);
1922 switch (dent->d_type) {
1924 llapi_err_noerrno(LLAPI_MSG_ERROR,
1925 "error: %s: '%s' is UNKNOWN type %d",
1926 __func__, dent->d_name, dent->d_type);
1929 rc = llapi_semantic_traverse(path, size, d, sem_init,
1930 sem_fini, data, dent);
1931 if (rc != 0 && ret == 0)
1937 rc = sem_init(path, d, NULL, data, dent);
1938 if (rc < 0 && ret == 0) {
1943 if (sem_fini && rc == 0)
1944 sem_fini(path, d, NULL, data, dent);
1952 sem_fini(path, parent, &d, data, de);
1961 static int param_callback(char *path, semantic_func_t sem_init,
1962 semantic_func_t sem_fini, struct find_param *param)
1964 int ret, len = strlen(path);
1967 if (len > PATH_MAX) {
1969 llapi_error(LLAPI_MSG_ERROR, ret,
1970 "Path name '%s' is too long", path);
1974 buf = (char *)malloc(PATH_MAX + 1);
1978 snprintf(buf, PATH_MAX + 1, "%s", path);
1979 ret = common_param_init(param, buf);
1983 param->fp_depth = 0;
1985 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1986 sem_fini, param, NULL);
1988 find_param_fini(param);
1990 return ret < 0 ? ret : 0;
1993 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1995 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1999 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2004 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2006 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2009 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2014 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2018 fd = open(path, O_RDONLY | O_NONBLOCK);
2021 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
2025 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2031 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2035 fd = open(path, O_RDONLY | O_NONBLOCK);
2038 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2042 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2054 * If uuidp is NULL, return the number of available obd uuids.
2055 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2056 * there are more OSTs than allocated to uuidp, then an error is returned with
2057 * the ost_count set to number of available obd uuids.
2059 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2060 int *ost_count, enum tgt_type type)
2062 char buf[PATH_MAX], format[32];
2063 int rc = 0, index = 0;
2064 struct obd_uuid name;
2068 /* Get the lov name */
2069 if (type == LOV_TYPE)
2070 rc = llapi_file_fget_lov_uuid(fd, &name);
2072 rc = llapi_file_fget_lmv_uuid(fd, &name);
2076 /* Now get the ost uuids */
2077 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2078 FILTER_BY_EXACT, "target_obd", ¶m);
2082 fp = fopen(param.gl_pathv[0], "r");
2085 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2090 snprintf(format, sizeof(format),
2091 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2092 while (fgets(buf, sizeof(buf), fp) != NULL) {
2093 if (uuidp && (index < *ost_count)) {
2094 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2102 if (uuidp && (index > *ost_count))
2107 cfs_free_param_data(¶m);
2111 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2113 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2116 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2121 root = open(mnt, O_RDONLY | O_DIRECTORY);
2124 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2129 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2137 /* Check if user specified value matches a real uuid. Ignore _UUID,
2138 * -osc-4ba41334, other trailing gunk in comparison.
2139 * @param real_uuid ends in "_UUID"
2140 * @param search_uuid may or may not end in "_UUID"
2142 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2144 int cmplen = strlen(real_uuid);
2145 int searchlen = strlen(search_uuid);
2147 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2149 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2152 /* The UUIDs may legitimately be different lengths, if
2153 * the system was upgraded from an older version. */
2154 if (cmplen != searchlen)
2157 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2160 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2161 * returned in param->fp_obd_index */
2162 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2164 struct obd_uuid obd_uuid;
2171 if (param->fp_got_uuids)
2174 /* Get the lov/lmv name */
2175 if (param->fp_get_lmv)
2176 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2178 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2180 if (rc != -ENOTTY) {
2181 llapi_error(LLAPI_MSG_ERROR, rc,
2182 "error: can't get %s name: %s",
2183 param->fp_get_lmv ? "lmv" : "lov",
2191 param->fp_got_uuids = 1;
2193 /* Now get the ost uuids */
2194 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2195 obd_uuid.uuid, FILTER_BY_EXACT,
2196 "target_obd", ¶m_data);
2200 fp = fopen(param_data.gl_pathv[0], "r");
2203 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2204 param_data.gl_pathv[0]);
2208 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2209 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2210 param->fp_get_lmv ? "MDTS" : "OBDS");
2212 snprintf(format, sizeof(format),
2213 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2214 while (fgets(buf, sizeof(buf), fp) != NULL) {
2217 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2220 if (param->fp_obd_uuid) {
2221 if (llapi_uuid_match(obd_uuid.uuid,
2222 param->fp_obd_uuid->uuid)) {
2223 param->fp_obd_index = index;
2226 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2227 /* Print everything */
2228 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2231 param->fp_obds_printed = 1;
2235 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2236 llapi_err_noerrno(LLAPI_MSG_ERROR,
2237 "error: %s: unknown obduuid: %s",
2238 __func__, param->fp_obd_uuid->uuid);
2242 cfs_free_param_data(¶m_data);
2246 /* In this case, param->fp_obd_uuid will be an array of obduuids and
2247 * obd index for all these obduuids will be returned in
2248 * param->fp_obd_indexes */
2249 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2250 int num_obds, int **obdindexes, int *obdindex,
2253 int ret, obdcount, obd_valid = 0, obdnum;
2255 struct obd_uuid *uuids = NULL;
2259 if (type == LOV_TYPE)
2260 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2262 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2266 obdcount = atoi(buf);
2267 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2272 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2274 if (ret == -EOVERFLOW) {
2275 struct obd_uuid *uuids_temp;
2277 uuids_temp = realloc(uuids, obdcount *
2278 sizeof(struct obd_uuid));
2279 if (uuids_temp != NULL) {
2281 goto retry_get_uuids;
2286 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2290 indexes = malloc(num_obds * sizeof(*obdindex));
2291 if (indexes == NULL) {
2296 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2299 /* The user may have specified a simple index */
2300 i = strtol(obduuids[obdnum].uuid, &end, 0);
2301 if (end && *end == '\0' && i < obdcount) {
2302 indexes[obdnum] = i;
2305 for (i = 0; i < obdcount; i++) {
2306 if (llapi_uuid_match(uuids[i].uuid,
2307 obduuids[obdnum].uuid)) {
2308 indexes[obdnum] = i;
2314 if (i >= obdcount) {
2315 indexes[obdnum] = OBD_NOT_FOUND;
2316 llapi_err_noerrno(LLAPI_MSG_ERROR,
2317 "invalid obduuid '%s'",
2318 obduuids[obdnum].uuid);
2324 *obdindex = OBD_NOT_FOUND;
2326 *obdindex = obd_valid;
2328 *obdindexes = indexes;
2336 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2340 if (param->fp_mdt_uuid) {
2341 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2343 ¶m->fp_mdt_indexes,
2344 ¶m->fp_mdt_index, LMV_TYPE);
2349 if (param->fp_obd_uuid) {
2350 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2352 ¶m->fp_obd_indexes,
2353 ¶m->fp_obd_index, LOV_TYPE);
2358 param->fp_got_uuids = 1;
2363 int llapi_ostlist(char *path, struct find_param *param)
2368 fd = open(path, O_RDONLY | O_DIRECTORY);
2372 ret = setup_obd_uuid(fd, path, param);
2379 * Tries to determine the default stripe attributes for a given filesystem. The
2380 * filesystem to check should be specified by fsname, or will be determined
2383 static int sattr_get_defaults(const char *const fsname,
2384 unsigned int *scount,
2385 unsigned int *ssize,
2386 unsigned int *soffset)
2392 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2393 "stripecount", val, sizeof(val));
2396 *scount = atoi(val);
2400 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2401 "stripesize", val, sizeof(val));
2408 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2409 "stripeoffset", val, sizeof(val));
2412 *soffset = atoi(val);
2419 * Tries to gather the default stripe attributes for a given filesystem. If
2420 * the attributes can be determined, they are cached for easy retreival the
2421 * next time they are needed. Only a single filesystem's attributes are
2424 int sattr_cache_get_defaults(const char *const fsname,
2425 const char *const pathname, unsigned int *scount,
2426 unsigned int *ssize, unsigned int *soffset)
2429 char fsname[PATH_MAX + 1];
2430 unsigned int stripecount;
2431 unsigned int stripesize;
2432 unsigned int stripeoffset;
2438 char fsname_buf[PATH_MAX + 1];
2439 unsigned int tmp[3];
2441 if (fsname == NULL) {
2442 rc = llapi_search_fsname(pathname, fsname_buf);
2446 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2449 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2451 * Ensure all 3 sattrs (count, size, and offset) are
2452 * successfully retrieved and stored in tmp before writing to
2455 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2459 cache.stripecount = tmp[0];
2460 cache.stripesize = tmp[1];
2461 cache.stripeoffset = tmp[2];
2462 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2466 *scount = cache.stripecount;
2468 *ssize = cache.stripesize;
2470 *soffset = cache.stripeoffset;
2475 static char *layout2name(__u32 layout_pattern)
2477 if (layout_pattern == LOV_PATTERN_MDT)
2479 else if (layout_pattern == LOV_PATTERN_RAID0)
2481 else if (layout_pattern == (LOV_PATTERN_RAID0 | LOV_PATTERN_F_RELEASED))
2487 enum lov_dump_flags {
2488 LDF_IS_DIR = 0x0001,
2489 LDF_IS_RAW = 0x0002,
2490 LDF_INDENT = 0x0004,
2491 LDF_SKIP_OBJS = 0x0008,
2495 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2496 struct lov_user_ost_data_v1 *objects,
2497 enum llapi_layout_verbose verbose,
2498 int depth, char *pool_name,
2499 enum lov_dump_flags flags)
2501 bool is_dir = flags & LDF_IS_DIR;
2502 bool is_raw = flags & LDF_IS_RAW;
2503 bool indent = flags & LDF_INDENT;
2504 bool yaml = flags & LDF_YAML;
2505 bool skip_objs = flags & LDF_SKIP_OBJS;
2506 char *prefix = is_dir ? "" : "lmm_";
2507 char *separator = "";
2508 char *space = indent ? " " : "";
2511 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2512 lmm_oi_set_seq(&lum->lmm_oi, 0);
2513 if (!indent && (verbose & VERBOSE_DETAIL))
2514 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2517 if (!yaml && !indent && depth && path &&
2518 ((verbose != VERBOSE_OBJID) || !is_dir))
2519 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2521 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2522 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2523 space, prefix, lum->lmm_magic);
2524 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2526 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2527 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2529 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2531 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2536 if (verbose & ~VERBOSE_DFID)
2537 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2539 /* This needs a bit of hand-holding since old 1.x lmm_oi
2540 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2541 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2542 * a real FID. Ideally the 2.x code would have stored this
2543 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2544 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2545 * worked properly (especially since IGIF FIDs use mds_inum as
2546 * the FID SEQ), but unfortunately that didn't happen.
2548 * Print it to look like an IGIF FID, even though the fields
2549 * are reversed on disk, so that it makes sense to userspace.
2551 * Don't use ostid_id() and ostid_seq(), since they assume the
2552 * oi_fid fields are in the right order. This is why there are
2553 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2555 * For newer layout types hopefully this will be a real FID. */
2556 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2557 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2558 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2559 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2560 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2562 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2565 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2569 if (verbose & VERBOSE_STRIPE_COUNT) {
2570 if (verbose & ~VERBOSE_STRIPE_COUNT)
2571 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2574 if (!is_raw && lum->lmm_stripe_count == 0 &&
2575 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2576 unsigned int scount;
2577 rc = sattr_cache_get_defaults(NULL, path,
2581 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2584 llapi_error(LLAPI_MSG_ERROR, rc,
2585 "Cannot determine default"
2588 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2589 lum->lmm_stripe_count ==
2590 (typeof(lum->lmm_stripe_count))(-1)
2591 ? -1 : lum->lmm_stripe_count);
2594 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2595 (__s16)lum->lmm_stripe_count);
2597 if (!yaml && is_dir)
2603 if (verbose & VERBOSE_STRIPE_SIZE) {
2604 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2605 if (verbose & ~VERBOSE_STRIPE_SIZE)
2606 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2608 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2610 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2613 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2615 llapi_error(LLAPI_MSG_ERROR, rc,
2616 "Cannot determine default"
2619 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2620 lum->lmm_stripe_size);
2622 if (!yaml && is_dir)
2628 if ((verbose & VERBOSE_PATTERN)) {
2629 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2630 if (verbose & ~VERBOSE_PATTERN)
2631 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2633 if (lov_pattern_supported(lum->lmm_pattern))
2634 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2635 layout2name(lum->lmm_pattern));
2637 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2638 separator = (!yaml && is_dir) ? " " : "\n";
2641 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2642 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2643 if (verbose & ~VERBOSE_GENERATION)
2644 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2646 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2647 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2651 if (verbose & VERBOSE_STRIPE_OFFSET) {
2652 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2653 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2654 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2656 if (is_dir || skip_objs)
2657 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2658 lum->lmm_stripe_offset ==
2659 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2660 lum->lmm_stripe_offset);
2661 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
2662 llapi_printf(LLAPI_MSG_NORMAL, "0");
2664 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2665 objects[0].l_ost_idx);
2666 if (!yaml && is_dir)
2672 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2673 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2674 if (verbose & ~VERBOSE_POOL)
2675 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2677 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2678 if (!yaml && is_dir)
2684 if (strlen(separator) != 0)
2685 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2688 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2689 struct lov_user_ost_data_v1 *objects,
2690 char *path, int obdindex, int depth,
2691 enum llapi_layout_verbose verbose,
2692 enum lov_dump_flags flags)
2694 bool is_dir = flags & LDF_IS_DIR;
2695 bool indent = flags & LDF_INDENT;
2696 bool skip_objs = flags & LDF_SKIP_OBJS;
2697 bool yaml = flags & LDF_YAML;
2698 bool obdstripe = obdindex == OBD_NOT_FOUND;
2701 if (!obdstripe && !skip_objs) {
2702 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2703 if (obdindex == objects[i].l_ost_idx) {
2713 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2716 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
2717 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2718 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
2719 char *space = " - ";
2722 llapi_printf(LLAPI_MSG_NORMAL,
2723 "%6slmm_objects:\n", " ");
2725 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2727 llapi_printf(LLAPI_MSG_NORMAL,
2728 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2730 for (i = 0; i < lum->lmm_stripe_count; i++) {
2731 int idx = objects[i].l_ost_idx;
2732 long long oid = ostid_id(&objects[i].l_ost_oi);
2733 long long gr = ostid_seq(&objects[i].l_ost_oi);
2735 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2739 struct lu_fid fid = { 0 };
2741 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2742 llapi_printf(LLAPI_MSG_NORMAL,
2743 "%sl_ost_idx: %d\n", space, idx);
2744 llapi_printf(LLAPI_MSG_NORMAL,
2745 "%8sl_fid: "DFID_NOBRACE"\n",
2747 } else if (indent) {
2748 struct lu_fid fid = { 0 };
2750 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2751 llapi_printf(LLAPI_MSG_NORMAL,
2752 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
2753 space, i, idx, PFID(&fid));
2756 sprintf(fmt, "%s%s%s\n",
2757 "\t%6u\t%14llu\t%#13llx\t",
2758 (fid_seq_is_rsvd(gr) ||
2759 fid_seq_is_mdt0(gr)) ?
2760 "%14llu" : "%#14llx", "%s");
2761 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2763 obdindex == idx ? " *" : "");
2767 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2770 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2771 char *path, int obdindex, int depth,
2772 enum llapi_layout_verbose verbose,
2773 enum lov_dump_flags flags)
2775 struct lmv_user_mds_data *objects = lum->lum_objects;
2776 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2777 char *separator = "";
2778 bool yaml = flags & LDF_YAML;
2779 bool obdstripe = false;
2782 if (obdindex != OBD_NOT_FOUND) {
2783 if (lum->lum_stripe_count == 0) {
2784 if (obdindex == lum->lum_stripe_offset)
2787 for (i = 0; i < lum->lum_stripe_count; i++) {
2788 if (obdindex == objects[i].lum_mds) {
2789 llapi_printf(LLAPI_MSG_NORMAL,
2804 /* show all information default */
2806 if (lum->lum_magic == LMV_USER_MAGIC)
2807 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
2808 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
2810 verbose = VERBOSE_OBJID;
2813 if (depth && path && ((verbose != VERBOSE_OBJID)))
2814 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2816 if (verbose & VERBOSE_STRIPE_COUNT) {
2817 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2818 if (verbose & ~VERBOSE_STRIPE_COUNT)
2819 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2820 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2821 (int)lum->lum_stripe_count);
2822 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
2828 if (verbose & VERBOSE_STRIPE_OFFSET) {
2829 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2830 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2831 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2832 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2833 (int)lum->lum_stripe_offset);
2834 if (verbose & VERBOSE_HASH_TYPE && !yaml)
2840 if (verbose & VERBOSE_HASH_TYPE) {
2841 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
2842 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
2844 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2845 if (verbose & ~VERBOSE_HASH_TYPE)
2846 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
2847 if (type < LMV_HASH_TYPE_MAX)
2848 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2849 mdt_hash_name[type]);
2851 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
2853 if (flags & LMV_HASH_FLAG_MIGRATION)
2854 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
2855 if (flags & LMV_HASH_FLAG_DEAD)
2856 llapi_printf(LLAPI_MSG_NORMAL, ",dead");
2857 if (flags & LMV_HASH_FLAG_BAD_TYPE)
2858 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
2859 if (flags & LMV_HASH_FLAG_LOST_LMV)
2860 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
2865 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
2866 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2867 if (lum->lum_stripe_count > 0)
2868 llapi_printf(LLAPI_MSG_NORMAL,
2869 "mdtidx\t\t FID[seq:oid:ver]\n");
2870 for (i = 0; i < lum->lum_stripe_count; i++) {
2871 int idx = objects[i].lum_mds;
2872 struct lu_fid *fid = &objects[i].lum_fid;
2873 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
2874 llapi_printf(LLAPI_MSG_NORMAL,
2875 "%6u\t\t "DFID"\t\t%s\n",
2877 obdindex == idx ? " *" : "");
2882 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
2883 pool_name[0] != '\0') {
2884 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2885 if (verbose & ~VERBOSE_POOL)
2886 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
2888 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
2892 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
2893 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2896 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
2897 enum lov_dump_flags flags)
2899 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
2900 int depth = param->fp_max_depth;
2901 enum llapi_layout_verbose verbose = param->fp_verbose;
2902 bool yaml = flags & LDF_YAML;
2904 if (depth && path && ((verbose != VERBOSE_OBJID) ||
2905 !(flags & LDF_IS_DIR)) && !yaml)
2906 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2908 if (verbose & VERBOSE_DETAIL) {
2909 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
2910 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
2911 " ", comp_v1->lcm_magic);
2912 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
2913 " ", comp_v1->lcm_size);
2914 if (flags & LDF_IS_DIR)
2915 llapi_printf(LLAPI_MSG_NORMAL,
2916 "%2slcm_flags: %s\n", " ",
2917 comp_v1->lcm_mirror_count > 0 ?
2920 llapi_printf(LLAPI_MSG_NORMAL,
2921 "%2slcm_flags: %s\n", " ",
2922 llapi_layout_flags_string(comp_v1->lcm_flags));
2925 if (verbose & VERBOSE_GENERATION) {
2926 if (verbose & ~VERBOSE_GENERATION)
2927 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
2929 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
2932 if (verbose & VERBOSE_MIRROR_COUNT) {
2933 if (verbose & ~VERBOSE_MIRROR_COUNT)
2934 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
2936 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
2937 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
2938 comp_v1->lcm_mirror_count + 1 : 1);
2941 if (verbose & VERBOSE_COMP_COUNT) {
2942 if (verbose & ~VERBOSE_COMP_COUNT)
2943 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
2945 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
2946 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
2947 comp_v1->lcm_entry_count : 0);
2950 if (verbose & VERBOSE_DETAIL && !yaml)
2951 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
2954 static void lcme_flags2str(__u32 comp_flags)
2960 llapi_printf(LLAPI_MSG_NORMAL, "0");
2963 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
2964 if (comp_flags & comp_flags_table[i].cfn_flag) {
2966 llapi_printf(LLAPI_MSG_NORMAL, ",");
2967 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2968 comp_flags_table[i].cfn_name);
2969 comp_flags &= ~comp_flags_table[i].cfn_flag;
2975 llapi_printf(LLAPI_MSG_NORMAL, ",");
2976 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
2980 static void lov_dump_comp_v1_entry(struct find_param *param,
2981 enum lov_dump_flags flags, int index)
2983 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
2984 struct lov_comp_md_entry_v1 *entry;
2985 char *separator = "";
2986 enum llapi_layout_verbose verbose = param->fp_verbose;
2987 bool yaml = flags & LDF_YAML;
2989 entry = &comp_v1->lcm_entries[index];
2992 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
2994 if (verbose & VERBOSE_COMP_ID) {
2995 if (verbose & VERBOSE_DETAIL && !yaml)
2996 llapi_printf(LLAPI_MSG_NORMAL,
2997 "%slcme_id: ", " - ");
2998 else if (verbose & ~VERBOSE_COMP_ID)
2999 llapi_printf(LLAPI_MSG_NORMAL,
3000 "%4slcme_id: ", " ");
3001 if (entry->lcme_id != LCME_ID_INVAL)
3002 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3004 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3008 if (verbose & VERBOSE_MIRROR_ID) {
3009 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3010 if (verbose & ~VERBOSE_MIRROR_ID)
3011 llapi_printf(LLAPI_MSG_NORMAL,
3012 "%4slcme_mirror_id: ", " ");
3013 if (entry->lcme_id != LCME_ID_INVAL)
3014 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3015 mirror_id_of(entry->lcme_id));
3017 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3021 if (verbose & VERBOSE_COMP_FLAGS) {
3022 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3023 if (verbose & ~VERBOSE_COMP_FLAGS)
3024 llapi_printf(LLAPI_MSG_NORMAL,
3025 "%4slcme_flags: ", " ");
3026 lcme_flags2str(entry->lcme_flags);
3029 /* print snapshot timestamp if its a nosync comp */
3030 if ((verbose & VERBOSE_COMP_FLAGS) &&
3031 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3032 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3033 if (verbose & ~VERBOSE_COMP_FLAGS)
3034 llapi_printf(LLAPI_MSG_NORMAL,
3035 "%4slcme_timestamp: ", " ");
3037 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3038 entry->lcme_timestamp);
3040 time_t stamp = entry->lcme_timestamp;
3041 char *date_str = asctime(localtime(&stamp));
3043 date_str[strlen(date_str) - 1] = '\0';
3044 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3050 if (verbose & VERBOSE_COMP_START) {
3051 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3052 if (verbose & ~VERBOSE_COMP_START)
3053 llapi_printf(LLAPI_MSG_NORMAL,
3054 "%4slcme_extent.e_start: ", " ");
3055 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3056 entry->lcme_extent.e_start);
3060 if (verbose & VERBOSE_COMP_END) {
3061 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3062 if (verbose & ~VERBOSE_COMP_END)
3063 llapi_printf(LLAPI_MSG_NORMAL,
3064 "%4slcme_extent.e_end: ", " ");
3065 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3066 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3068 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3069 entry->lcme_extent.e_end);
3074 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3075 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3076 } else if (verbose & VERBOSE_DETAIL) {
3077 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3078 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3079 " ", entry->lcme_offset);
3080 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3081 " ", entry->lcme_size);
3082 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3084 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3088 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3089 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3091 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3092 * The table below gives the answers for the specified parameters (value and
3093 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3094 * --------------------------------------