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,
90 struct lustre_foreign_type lu_foreign_types[] = {
91 {.lft_type = LU_FOREIGN_TYPE_NONE, .lft_name = "none"},
92 {.lft_type = LU_FOREIGN_TYPE_DAOS, .lft_name = "daos"},
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 if (liblustreapi_cmd != NULL)
128 fprintf(stderr, "%s %s: ", program_invocation_short_name,
131 fprintf(stderr, "%s: ", program_invocation_short_name);
132 vfprintf(stderr, fmt, ap);
133 if (level & LLAPI_MSG_NO_ERRNO)
134 fprintf(stderr, "\n");
136 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
139 static void info_callback_default(enum llapi_message_level level, int err,
140 const char *fmt, va_list ap)
143 if (liblustreapi_cmd != NULL) {
144 fprintf(stdout, "%s %s: ",
145 program_invocation_short_name,
148 fprintf(stdout, "%s: ", program_invocation_short_name);
151 vfprintf(stdout, fmt, ap);
154 static llapi_log_callback_t llapi_error_callback = error_callback_default;
155 static llapi_log_callback_t llapi_info_callback = info_callback_default;
158 /* llapi_error will preserve errno */
159 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
162 int tmp_errno = errno;
164 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
168 llapi_error_callback(level, abs(err), fmt, args);
173 /* llapi_printf will preserve errno */
174 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
177 int tmp_errno = errno;
179 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
183 llapi_info_callback(level, 0, fmt, args);
189 * Set a custom error logging function. Passing in NULL will reset the logging
190 * callback to its default value.
192 * This function returns the value of the old callback.
194 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
196 llapi_log_callback_t old = llapi_error_callback;
199 llapi_error_callback = cb;
201 llapi_error_callback = error_callback_default;
207 * Set a custom info logging function. Passing in NULL will reset the logging
208 * callback to its default value.
210 * This function returns the value of the old callback.
212 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
214 llapi_log_callback_t old = llapi_info_callback;
217 llapi_info_callback = cb;
219 llapi_info_callback = info_callback_default;
225 * size_units is to be initialized (or zeroed) by caller.
227 int llapi_parse_size(const char *optarg, unsigned long long *size,
228 unsigned long long *size_units, int bytes_spec)
231 char *argbuf = (char *)optarg;
232 unsigned long long frac = 0, frac_d = 1;
234 if (strncmp(optarg, "-", 1) == 0)
237 if (*size_units == 0)
240 *size = strtoull(argbuf, &end, 0);
241 if (end != NULL && *end == '.') {
245 frac = strtoull(argbuf, &end, 10);
246 /* count decimal places */
247 for (i = 0; i < (end - argbuf); i++)
252 if ((*end == 'b') && *(end + 1) == '\0' &&
253 (*size & (~0ULL << (64 - 9))) == 0 &&
255 *size_units = 1 << 9;
256 } else if ((*end == 'b') &&
257 *(end + 1) == '\0' &&
260 } else if ((*end == 'k' || *end == 'K') &&
261 *(end + 1) == '\0' &&
262 (*size & (~0ULL << (64 - 10))) == 0) {
263 *size_units = 1 << 10;
264 } else if ((*end == 'm' || *end == 'M') &&
265 *(end + 1) == '\0' &&
266 (*size & (~0ULL << (64 - 20))) == 0) {
267 *size_units = 1 << 20;
268 } else if ((*end == 'g' || *end == 'G') &&
269 *(end + 1) == '\0' &&
270 (*size & (~0ULL << (64 - 30))) == 0) {
271 *size_units = 1 << 30;
272 } else if ((*end == 't' || *end == 'T') &&
273 *(end + 1) == '\0' &&
274 (*size & (~0ULL << (64 - 40))) == 0) {
275 *size_units = 1ULL << 40;
276 } else if ((*end == 'p' || *end == 'P') &&
277 *(end + 1) == '\0' &&
278 (*size & (~0ULL << (64 - 50))) == 0) {
279 *size_units = 1ULL << 50;
280 } else if ((*end == 'e' || *end == 'E') &&
281 *(end + 1) == '\0' &&
282 (*size & (~0ULL << (64 - 60))) == 0) {
283 *size_units = 1ULL << 60;
288 *size = *size * *size_units + frac * *size_units / frac_d;
293 int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
295 struct obd_ioctl_data *overlay;
298 data->ioc_len = obd_ioctl_packlen(data);
299 data->ioc_version = OBD_IOCTL_VERSION;
301 if (*pbuf != NULL && data->ioc_len > max_len) {
302 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
303 "pbuf = %p, ioc_len = %u, max_len = %d\n",
304 *pbuf, data->ioc_len, max_len);
309 *pbuf = malloc(data->ioc_len);
314 overlay = (struct obd_ioctl_data *)*pbuf;
315 memcpy(*pbuf, data, sizeof(*data));
317 ptr = overlay->ioc_bulk;
318 if (data->ioc_inlbuf1) {
319 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
320 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
323 if (data->ioc_inlbuf2) {
324 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
325 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
328 if (data->ioc_inlbuf3) {
329 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
330 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
333 if (data->ioc_inlbuf4) {
334 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
335 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
341 int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
343 struct obd_ioctl_data *overlay;
349 overlay = (struct obd_ioctl_data *)pbuf;
351 /* Preserve the caller's buffer pointers */
352 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
353 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
354 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
355 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
357 memcpy(data, pbuf, sizeof(*data));
359 ptr = overlay->ioc_bulk;
360 if (data->ioc_inlbuf1) {
361 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
362 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
365 if (data->ioc_inlbuf2) {
366 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
367 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
370 if (data->ioc_inlbuf3) {
371 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
372 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
375 if (data->ioc_inlbuf4) {
376 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
377 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
383 /* XXX: llapi_xxx() functions return negative values upon failure */
385 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
386 int stripe_count, int stripe_pattern)
388 static int page_size;
391 if (page_size == 0) {
392 /* 64 KB is the largest common page size (on ia64/PPC/ARM),
393 * but check the local page size just in case. The page_size
394 * will not change for the lifetime of this process at least.
396 page_size = LOV_MIN_STRIPE_SIZE;
397 if (getpagesize() > page_size) {
398 page_size = getpagesize();
399 llapi_err_noerrno(LLAPI_MSG_WARN,
400 "warning: page size (%u) larger than expected (%u)",
401 page_size, LOV_MIN_STRIPE_SIZE);
404 if (!llapi_stripe_size_is_aligned(stripe_size)) {
406 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
407 "must be an even multiple of %d bytes",
408 stripe_size, page_size);
411 if (!llapi_stripe_index_is_valid(stripe_offset)) {
413 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
417 if (!llapi_stripe_count_is_valid(stripe_count)) {
419 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
423 if (llapi_stripe_size_is_too_big(stripe_size)) {
425 llapi_error(LLAPI_MSG_ERROR, rc,
426 "error: stripe size '%llu' over 4GB limit",
436 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
441 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
443 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
447 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
449 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
454 if (!llapi_dir_hash_type_is_valid(hash_type)) {
456 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
464 * Trim a trailing newline from a string, if it exists.
466 int llapi_chomp_string(char *buf)
482 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
484 static int get_param_lov(const char *path, const char *param,
485 char *buf, size_t buf_size)
487 struct obd_uuid uuid;
490 rc = llapi_file_get_lov_uuid(path, &uuid);
494 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
499 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
501 static int get_param_lmv(const char *path, const char *param,
502 char *buf, size_t buf_size)
504 struct obd_uuid uuid;
507 rc = llapi_file_get_lmv_uuid(path, &uuid);
511 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
515 static int get_mds_md_size(const char *path)
517 char buf[PATH_MAX], inst[PATH_MAX];
518 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
521 rc = llapi_getname(path, inst, sizeof(inst));
525 /* Get the max ea size from llite parameters. */
526 rc = get_lustre_param_value("llite", inst, FILTER_BY_EXACT,
527 "max_easize", buf, sizeof(buf));
533 return rc > 0 ? rc : md_size;
536 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
538 return get_param_lmv(path, "uuid", buf, bufsize);
542 * Open a Lustre file.
544 * \param name the name of the file to be opened
545 * \param flags access mode, see flags in open(2)
546 * \param mode permission of the file if it is created, see mode in open(2)
547 * \param param stripe pattern of the newly created file
549 * \retval file descriptor of opened file
550 * \retval negative errno on failure
552 int llapi_file_open_param(const char *name, int flags, mode_t mode,
553 const struct llapi_stripe_param *param)
555 char fsname[MAX_OBD_NAME + 1] = { 0 };
556 char *pool_name = param->lsp_pool;
557 struct lov_user_md *lum = NULL;
558 size_t lum_size = sizeof(*lum);
561 /* Make sure we are on a Lustre file system */
562 rc = llapi_search_fsname(name, fsname);
564 llapi_error(LLAPI_MSG_ERROR, rc,
565 "'%s' is not on a Lustre filesystem",
570 /* Check if the stripe pattern is sane. */
571 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
572 param->lsp_stripe_offset,
573 param->lsp_stripe_count,
574 param->lsp_stripe_pattern);
578 /* Make sure we have a good pool */
579 if (pool_name != NULL) {
580 /* in case user gives the full pool name <fsname>.<poolname>,
581 * strip the fsname */
582 char *ptr = strchr(pool_name, '.');
585 if (strcmp(pool_name, fsname) != 0) {
587 llapi_err_noerrno(LLAPI_MSG_ERROR,
588 "Pool '%s' is not on filesystem '%s'",
595 /* Make sure the pool exists and is non-empty */
596 rc = llapi_search_ost(fsname, pool_name, NULL);
598 char *err = rc == 0 ? "has no OSTs" : "does not exist";
600 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
601 fsname, pool_name, err);
605 lum_size = sizeof(struct lov_user_md_v3);
608 /* sanity check of target list */
609 if (param->lsp_is_specific) {
610 char ostname[MAX_OBD_NAME + 64];
614 for (i = 0; i < param->lsp_stripe_count; i++) {
615 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
616 fsname, param->lsp_osts[i]);
617 rc = llapi_search_ost(fsname, pool_name, ostname);
622 llapi_error(LLAPI_MSG_ERROR, rc,
623 "%s: cannot find OST %s in %s",
630 /* Make sure stripe offset is in OST list. */
631 if (param->lsp_osts[i] == param->lsp_stripe_offset)
635 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
636 "%s: stripe offset '%d' is not in the "
638 __func__, param->lsp_stripe_offset);
642 lum_size = lov_user_md_size(param->lsp_stripe_count,
643 LOV_USER_MAGIC_SPECIFIC);
646 lum = calloc(1, lum_size);
651 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
653 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
654 flags = O_DIRECTORY | O_RDONLY;
661 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
666 /* Initialize IOCTL striping pattern structure */
667 lum->lmm_magic = LOV_USER_MAGIC_V1;
668 lum->lmm_pattern = param->lsp_stripe_pattern;
669 lum->lmm_stripe_size = param->lsp_stripe_size;
670 lum->lmm_stripe_count = param->lsp_stripe_count;
671 lum->lmm_stripe_offset = param->lsp_stripe_offset;
672 if (pool_name != NULL) {
673 struct lov_user_md_v3 *lumv3 = (void *)lum;
675 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
676 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
678 if (param->lsp_is_specific) {
679 struct lov_user_md_v3 *lumv3 = (void *)lum;
682 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
683 if (pool_name == NULL) {
684 /* LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
685 * OST list, therefore if pool is not specified we have
686 * to pack a null pool name for placeholder. */
687 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
690 for (i = 0; i < param->lsp_stripe_count; i++)
691 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
694 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
695 char *errmsg = "stripe already set";
698 if (errno != EEXIST && errno != EALREADY)
699 errmsg = strerror(errno);
701 llapi_err_noerrno(LLAPI_MSG_ERROR,
702 "setstripe error for '%s': %s", name, errmsg);
713 int llapi_file_open_pool(const char *name, int flags, int mode,
714 unsigned long long stripe_size, int stripe_offset,
715 int stripe_count, int stripe_pattern, char *pool_name)
717 const struct llapi_stripe_param param = {
718 .lsp_stripe_size = stripe_size,
719 .lsp_stripe_count = stripe_count,
720 .lsp_stripe_pattern = stripe_pattern,
721 .lsp_stripe_offset = stripe_offset,
722 .lsp_pool = pool_name
724 return llapi_file_open_param(name, flags, mode, ¶m);
727 int llapi_file_open(const char *name, int flags, int mode,
728 unsigned long long stripe_size, int stripe_offset,
729 int stripe_count, int stripe_pattern)
731 return llapi_file_open_pool(name, flags, mode, stripe_size,
732 stripe_offset, stripe_count,
733 stripe_pattern, NULL);
736 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
737 __u32 flags, char *foreign_lov)
740 struct lov_foreign_md *lfm;
743 if (foreign_lov == NULL) {
745 llapi_error(LLAPI_MSG_ERROR, rc,
746 "foreign LOV EA content must be provided");
750 len = strlen(foreign_lov);
751 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
754 llapi_error(LLAPI_MSG_ERROR, rc,
755 "foreign LOV EA size %zu (must be 0 < len < %zu)",
756 len, XATTR_SIZE_MAX -
757 offsetof(struct lov_foreign_md, lfm_value));
761 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
764 llapi_error(LLAPI_MSG_ERROR, rc,
765 "failed to allocate lov_foreign_md");
769 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
772 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
776 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
777 lfm->lfm_length = len;
778 lfm->lfm_type = type;
779 lfm->lfm_flags = flags;
780 memcpy(lfm->lfm_value, foreign_lov, len);
782 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
783 char *errmsg = "stripe already set";
787 errmsg = "not on a Lustre filesystem";
788 else if (errno == EEXIST || errno == EALREADY)
789 errmsg = "stripe already set";
791 errmsg = strerror(errno);
793 llapi_err_noerrno(LLAPI_MSG_ERROR,
794 "setstripe error for '%s': %s", name, errmsg);
810 int llapi_file_create(const char *name, unsigned long long stripe_size,
811 int stripe_offset, int stripe_count, int stripe_pattern)
815 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
816 stripe_offset, stripe_count, stripe_pattern,
825 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
826 int stripe_offset, int stripe_count,
827 int stripe_pattern, char *pool_name)
831 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
832 stripe_offset, stripe_count, stripe_pattern,
841 static int verify_dir_param(const char *name,
842 const struct llapi_stripe_param *param)
844 char fsname[MAX_OBD_NAME + 1] = { 0 };
845 char *pool_name = param->lsp_pool;
848 /* Make sure we are on a Lustre file system */
849 rc = llapi_search_fsname(name, fsname);
851 llapi_error(LLAPI_MSG_ERROR, rc,
852 "'%s' is not on a Lustre filesystem",
857 /* Check if the stripe pattern is sane. */
858 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
859 param->lsp_stripe_count,
860 param->lsp_stripe_pattern);
864 /* Make sure we have a good pool */
865 if (pool_name != NULL) {
866 /* in case user gives the full pool name <fsname>.<poolname>,
867 * strip the fsname */
868 char *ptr = strchr(pool_name, '.');
872 if (strcmp(pool_name, fsname) != 0) {
874 llapi_err_noerrno(LLAPI_MSG_ERROR,
875 "Pool '%s' is not on filesystem '%s'",
882 /* Make sure the pool exists and is non-empty */
883 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
885 char *err = rc == 0 ? "has no OSTs" : "does not exist";
887 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
888 fsname, pool_name, err);
893 /* sanity check of target list */
894 if (param->lsp_is_specific) {
895 char mdtname[MAX_OBD_NAME + 64];
899 for (i = 0; i < param->lsp_stripe_count; i++) {
900 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
901 fsname, param->lsp_tgts[i]);
902 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
907 llapi_error(LLAPI_MSG_ERROR, rc,
908 "%s: cannot find MDT %s in %s",
915 /* Make sure stripe offset is in MDT list. */
916 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
920 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
921 "%s: stripe offset '%d' is not in the "
923 __func__, param->lsp_stripe_offset);
931 static inline void param2lmu(struct lmv_user_md *lmu,
932 const struct llapi_stripe_param *param)
934 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
936 lmu->lum_stripe_count = param->lsp_stripe_count;
937 lmu->lum_stripe_offset = param->lsp_stripe_offset;
938 lmu->lum_hash_type = param->lsp_stripe_pattern;
939 if (param->lsp_pool != NULL)
940 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
941 if (param->lsp_is_specific) {
944 for (i = 0; i < param->lsp_stripe_count; i++)
945 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
949 int llapi_dir_set_default_lmv(const char *name,
950 const struct llapi_stripe_param *param)
952 struct lmv_user_md lmu = { 0 };
956 rc = verify_dir_param(name, param);
960 /* TODO: default lmv doesn't support specific targets yet */
961 if (param->lsp_is_specific)
964 param2lmu(&lmu, param);
966 fd = open(name, O_DIRECTORY | O_RDONLY);
969 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
973 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
975 char *errmsg = "stripe already set";
977 if (errno != EEXIST && errno != EALREADY)
978 errmsg = strerror(errno);
980 llapi_err_noerrno(LLAPI_MSG_ERROR,
981 "default dirstripe error on '%s': %s",
988 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
989 int stripe_count, int stripe_pattern,
990 const char *pool_name)
992 const struct llapi_stripe_param param = {
993 .lsp_stripe_count = stripe_count,
994 .lsp_stripe_offset = stripe_offset,
995 .lsp_stripe_pattern = stripe_pattern,
996 .lsp_pool = (char *)pool_name
999 return llapi_dir_set_default_lmv(name, ¶m);
1003 * Create a Lustre directory.
1005 * \param name the name of the directory to be created
1006 * \param mode permission of the file if it is created, see mode in open(2)
1007 * \param param stripe pattern of the newly created directory
1009 * \retval 0 on success
1010 * \retval negative errno on failure
1012 int llapi_dir_create(const char *name, mode_t mode,
1013 const struct llapi_stripe_param *param)
1015 struct lmv_user_md *lmu = NULL;
1017 struct obd_ioctl_data data = { 0 };
1020 char *dirpath = NULL;
1021 char *namepath = NULL;
1026 rc = verify_dir_param(name, param);
1030 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1031 param->lsp_is_specific ?
1032 LMV_USER_MAGIC_SPECIFIC :
1035 lmu = calloc(1, lmu_size);
1039 dirpath = strdup(name);
1045 namepath = strdup(name);
1052 param2lmu(lmu, param);
1054 filename = basename(namepath);
1055 dir = dirname(dirpath);
1057 data.ioc_inlbuf1 = (char *)filename;
1058 data.ioc_inllen1 = strlen(filename) + 1;
1059 data.ioc_inlbuf2 = (char *)lmu;
1060 data.ioc_inllen2 = lmu_size;
1061 data.ioc_type = mode;
1062 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1064 llapi_error(LLAPI_MSG_ERROR, rc,
1065 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1070 fd = open(dir, O_DIRECTORY | O_RDONLY);
1073 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1077 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1078 char *errmsg = "stripe already set";
1081 if (errno != EEXIST && errno != EALREADY)
1082 errmsg = strerror(errno);
1084 llapi_err_noerrno(LLAPI_MSG_ERROR,
1085 "dirstripe error on '%s': %s", name, errmsg);
1096 * Create a foreign directory.
1098 * \param name the name of the directory to be created
1099 * \param mode permission of the file if it is created, see mode in open(2)
1100 * \param type foreign type to be set in LMV EA
1101 * \param flags foreign flags to be set in LMV EA
1102 * \param value foreign pattern to be set in LMV EA
1104 * \retval 0 on success
1105 * \retval negative errno on failure
1107 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1108 __u32 flags, const char *value)
1110 struct lmv_foreign_md *lfm = NULL;
1111 size_t lfm_size, len;
1112 struct obd_ioctl_data data = { 0 };
1115 char *dirpath = NULL;
1116 char *namepath = NULL;
1121 len = strlen(value);
1122 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1125 llapi_error(LLAPI_MSG_ERROR, rc, "invalid LOV EA length %zu "
1126 "(must be 0 < len < %zu)\n", len,
1128 offsetof(struct lmv_foreign_md, lfm_value));
1131 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1132 lfm = calloc(1, lfm_size);
1136 dirpath = strdup(name);
1142 namepath = strdup(name);
1149 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1150 lfm->lfm_length = len;
1151 lfm->lfm_type = type;
1152 lfm->lfm_flags = flags;
1153 memcpy(lfm->lfm_value, value, len);
1155 filename = basename(namepath);
1156 dir = dirname(dirpath);
1158 data.ioc_inlbuf1 = (char *)filename;
1159 data.ioc_inllen1 = strlen(filename) + 1;
1160 data.ioc_inlbuf2 = (char *)lfm;
1161 data.ioc_inllen2 = lfm_size;
1162 data.ioc_type = mode;
1163 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1165 llapi_error(LLAPI_MSG_ERROR, rc,
1166 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1171 fd = open(dir, O_DIRECTORY | O_RDONLY);
1174 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1178 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1179 char *errmsg = "stripe already set";
1182 if (errno != EEXIST && errno != EALREADY)
1183 errmsg = strerror(errno);
1185 llapi_err_noerrno(LLAPI_MSG_ERROR,
1186 "dirstripe error on '%s': %s", name, errmsg);
1196 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1197 int stripe_count, int stripe_pattern,
1198 const char *pool_name)
1200 const struct llapi_stripe_param param = {
1201 .lsp_stripe_count = stripe_count,
1202 .lsp_stripe_offset = stripe_offset,
1203 .lsp_stripe_pattern = stripe_pattern,
1204 .lsp_pool = (char *)pool_name
1207 return llapi_dir_create(name, mode, ¶m);
1210 int llapi_direntry_remove(char *dname)
1212 char *dirpath = NULL;
1213 char *namepath = NULL;
1219 dirpath = strdup(dname);
1220 namepath = strdup(dname);
1221 if (!dirpath || !namepath)
1224 filename = basename(namepath);
1226 dir = dirname(dirpath);
1228 fd = open(dir, O_DIRECTORY | O_RDONLY);
1231 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
1236 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
1237 char *errmsg = strerror(errno);
1238 llapi_err_noerrno(LLAPI_MSG_ERROR,
1239 "error on ioctl %#jx for '%s' (%d): %s",
1240 (uintmax_t)LL_IOC_LMV_SETSTRIPE, filename,
1252 * Find the fsname, the full path, and/or an open fd.
1253 * Either the fsname or path must not be NULL
1255 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1258 char buf[PATH_MAX], mntdir[PATH_MAX];
1259 char *ptr, *ptr_end;
1261 int idx = 0, len = 0, mntlen, fd;
1264 /* get the mount point */
1265 fp = setmntent(PROC_MOUNTS, "r");
1268 llapi_error(LLAPI_MSG_ERROR, rc,
1269 "cannot retrieve filesystem mount point");
1273 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1276 if (!llapi_is_lustre_mnt(&mnt))
1279 if ((want & WANT_INDEX) && (idx++ != index))
1282 mntlen = strlen(mnt.mnt_dir);
1283 ptr = strchr(mnt.mnt_fsname, '/');
1284 while (ptr && *ptr == '/')
1286 /* thanks to the call to llapi_is_lustre_mnt() above,
1287 * we are sure that mnt.mnt_fsname contains ":/",
1288 * so ptr should never be NULL */
1292 while (*ptr_end != '/' && *ptr_end != '\0')
1295 /* Check the fsname for a match, if given */
1296 if (!(want & WANT_FSNAME) && fsname != NULL &&
1297 (strlen(fsname) > 0) &&
1298 (strncmp(ptr, fsname, ptr_end - ptr) != 0))
1301 /* If the path isn't set return the first one we find */
1302 if (path == NULL || strlen(path) == 0) {
1303 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1304 mntdir[sizeof(mntdir) - 1] = '\0';
1305 if ((want & WANT_FSNAME) && fsname != NULL) {
1306 strncpy(fsname, ptr, ptr_end - ptr);
1307 fsname[ptr_end - ptr] = '\0';
1311 /* Otherwise find the longest matching path */
1312 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1313 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1314 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1315 mntdir[sizeof(mntdir) - 1] = '\0';
1317 if ((want & WANT_FSNAME) && fsname != NULL) {
1318 strncpy(fsname, ptr, ptr_end - ptr);
1319 fsname[ptr_end - ptr] = '\0';
1328 if ((want & WANT_PATH) && path != NULL) {
1329 strncpy(path, mntdir, PATH_MAX);
1330 path[strlen(mntdir)] = '\0';
1332 if (want & WANT_FD) {
1333 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1336 llapi_error(LLAPI_MSG_ERROR, rc,
1337 "cannot open '%s': %s", mntdir,
1344 } else if (want & WANT_ERROR)
1345 llapi_err_noerrno(LLAPI_MSG_ERROR,
1346 "'%s' not on a mounted Lustre filesystem",
1347 (want & WANT_PATH) ? fsname : path);
1352 * search lustre mounts
1354 * Calling this function will return to the user the mount point, mntdir, and
1355 * the file system name, fsname, if the user passed a buffer to this routine.
1357 * The user inputs are pathname and index. If the pathname is supplied then
1358 * the value of the index will be ignored. The pathname will return data if
1359 * the pathname is located on a lustre mount. Index is used to pick which
1360 * mount point you want in the case of multiple mounted lustre file systems.
1361 * See function lfs_osts in lfs.c for an example of the index use.
1363 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1366 int want = WANT_PATH, idx = -1;
1368 if (!pathname || pathname[0] == '\0') {
1372 strcpy(mntdir, pathname);
1375 want |= WANT_FSNAME;
1376 return get_root_path(want, fsname, NULL, mntdir, idx);
1379 /* Given a path, find the corresponding Lustre fsname */
1380 int llapi_search_fsname(const char *pathname, char *fsname)
1385 path = realpath(pathname, NULL);
1387 char tmp[PATH_MAX - 1];
1393 if (pathname[0] != '/') {
1394 /* Need an absolute path, but realpath() only works for
1395 * pathnames that actually exist. We go through the
1396 * extra hurdle of dirname(getcwd() + pathname) in
1397 * case the relative pathname contains ".." in it. */
1398 char realpath[PATH_MAX - 1];
1400 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1402 llapi_error(LLAPI_MSG_ERROR, rc,
1403 "cannot get current working directory");
1407 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1408 if (rc >= sizeof(tmp)) {
1410 llapi_error(LLAPI_MSG_ERROR, rc,
1411 "invalid parent path '%s'",
1417 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1418 if (rc >= sizeof(buf)) {
1420 llapi_error(LLAPI_MSG_ERROR, rc,
1421 "invalid path '%s'", pathname);
1424 path = realpath(buf, NULL);
1426 ptr = strrchr(buf, '/');
1428 llapi_error(LLAPI_MSG_ERROR |
1429 LLAPI_MSG_NO_ERRNO, 0,
1430 "cannot resolve path '%s'",
1435 path = realpath(buf, NULL);
1438 llapi_error(LLAPI_MSG_ERROR, rc,
1439 "cannot resolve path '%s'",
1445 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1450 int llapi_search_rootpath(char *pathname, const char *fsname)
1452 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1455 int llapi_getname(const char *path, char *buf, size_t size)
1457 struct obd_uuid uuid_buf;
1458 char *uuid = uuid_buf.uuid;
1460 int rc, len, fsname_len;
1462 memset(&uuid_buf, 0, sizeof(uuid_buf));
1463 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1468 * We want to turn testfs-clilov-ffff88002738bc00 into
1469 * testfs-ffff88002738bc00 in a portable way that doesn't depend
1470 * on what is after "-clilov-" as it may change in the future.
1471 * Unfortunately, the "fsname" part may contain a dash, so we
1472 * can't just skip to the first dash, and the "instance" may be a
1473 * UUID in the future, so we can't necessarily go to the last dash.
1475 cfg_instance = strstr(uuid, "-clilov-");
1479 fsname_len = cfg_instance - uuid;
1480 cfg_instance += strlen("-clilov-");
1481 len = snprintf(buf, size, "%.*s-%s", fsname_len, uuid, cfg_instance);
1490 * Get the list of pool members.
1491 * \param poolname string of format \<fsname\>.\<poolname\>
1492 * \param members caller-allocated array of char*
1493 * \param list_size size of the members array
1494 * \param buffer caller-allocated buffer for storing OST names
1495 * \param buffer_size size of the buffer
1497 * \return number of members retrieved for this pool
1498 * \retval -error failure
1500 int llapi_get_poolmembers(const char *poolname, char **members,
1501 int list_size, char *buffer, int buffer_size)
1503 char fsname[PATH_MAX];
1512 /* name is FSNAME.POOLNAME */
1513 if (strlen(poolname) >= sizeof(fsname))
1516 snprintf(fsname, sizeof(fsname), "%s", poolname);
1517 pool = strchr(fsname, '.');
1524 rc = poolpath(&pathname, fsname, NULL);
1526 llapi_error(LLAPI_MSG_ERROR, rc,
1527 "Lustre filesystem '%s' not found",
1532 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1533 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1534 cfs_free_param_data(&pathname);
1535 if (rc >= sizeof(buf))
1537 fd = fopen(buf, "r");
1540 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1545 while (fgets(buf, sizeof(buf), fd) != NULL) {
1546 if (nb_entries >= list_size) {
1550 buf[sizeof(buf) - 1] = '\0';
1552 tmp = strchr(buf, '\n');
1555 if (used + strlen(buf) + 1 > buffer_size) {
1560 strcpy(buffer + used, buf);
1561 members[nb_entries] = buffer + used;
1562 used += strlen(buf) + 1;
1572 * Get the list of pools in a filesystem.
1573 * \param name filesystem name or path
1574 * \param poollist caller-allocated array of char*
1575 * \param list_size size of the poollist array
1576 * \param buffer caller-allocated buffer for storing pool names
1577 * \param buffer_size size of the buffer
1579 * \return number of pools retrieved for this filesystem
1580 * \retval -error failure
1582 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1583 char *buffer, int buffer_size)
1589 struct dirent *pool;
1591 unsigned int nb_entries = 0;
1592 unsigned int used = 0;
1595 /* initialize output array */
1596 for (i = 0; i < list_size; i++)
1599 /* is name a pathname ? */
1600 ptr = strchr(name, '/');
1602 char fsname_buf[MAXNAMLEN];
1604 /* We will need fsname for printing later */
1605 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1609 ptr = strrchr(fsname_buf, '-');
1613 fsname = strdup(fsname_buf);
1617 /* name is FSNAME */
1618 fsname = strdup(name);
1623 rc = poolpath(&pathname, fsname, NULL);
1625 llapi_error(LLAPI_MSG_ERROR, rc,
1626 "Lustre filesystem '%s' not found", name);
1630 dir = opendir(pathname.gl_pathv[0]);
1633 llapi_error(LLAPI_MSG_ERROR, rc,
1634 "Could not open pool list for '%s'",
1641 pool = readdir(dir);
1647 /* ignore . and .. */
1648 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1651 /* check output bounds */
1652 if (nb_entries >= list_size) {
1654 goto free_dir_no_msg;
1657 /* +2 for '.' and final '\0' */
1658 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1661 goto free_dir_no_msg;
1664 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1665 poollist[nb_entries] = buffer + used;
1666 used += strlen(pool->d_name) + strlen(fsname) + 2;
1672 llapi_error(LLAPI_MSG_ERROR, rc,
1673 "Error reading pool list for '%s'", name);
1675 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1680 cfs_free_param_data(&pathname);
1683 return rc != 0 ? rc : nb_entries;
1686 /* wrapper for lfs.c and obd.c */
1687 int llapi_poollist(const char *name)
1689 /* list of pool names (assume that pool count is smaller
1691 char **list, *buffer = NULL, *fsname = (char *)name;
1692 char *poolname = NULL, *tmp = NULL, data[16];
1693 enum param_filter type = FILTER_BY_PATH;
1694 int obdcount, bufsize, rc, nb, i;
1699 if (name[0] != '/') {
1700 fsname = strdup(name);
1704 poolname = strchr(fsname, '.');
1707 type = FILTER_BY_FS_NAME;
1710 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1711 data, sizeof(data));
1714 obdcount = atoi(data);
1716 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1717 * and also an array to store the pointers for all that
1718 * allocated space. */
1720 bufsize = sizeof(struct obd_uuid) * obdcount;
1721 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1722 if (buffer == NULL) {
1726 list = (char **) (buffer + bufsize);
1729 /* name is a path or fsname */
1730 nb = llapi_get_poollist(name, list, obdcount,
1733 /* name is a pool name (<fsname>.<poolname>) */
1734 nb = llapi_get_poolmembers(name, list, obdcount,
1738 if (nb == -EOVERFLOW) {
1741 goto retry_get_pools;
1744 for (i = 0; i < nb; i++)
1745 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1746 rc = (nb < 0 ? nb : 0);
1750 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1755 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1756 void *data, struct dirent64 *de);
1758 #define OBD_NOT_FOUND (-1)
1760 static void find_param_fini(struct find_param *param)
1762 if (param->fp_migrate)
1765 if (param->fp_obd_indexes) {
1766 free(param->fp_obd_indexes);
1767 param->fp_obd_indexes = NULL;
1770 if (param->fp_lmd) {
1771 free(param->fp_lmd);
1772 param->fp_lmd = NULL;
1775 if (param->fp_lmv_md) {
1776 free(param->fp_lmv_md);
1777 param->fp_lmv_md = NULL;
1781 static int common_param_init(struct find_param *param, char *path)
1783 int lum_size = get_mds_md_size(path);
1788 /* migrate has fp_lmv_md initialized outside */
1789 if (param->fp_migrate)
1792 if (lum_size < PATH_MAX + 1)
1793 lum_size = PATH_MAX + 1;
1795 param->fp_lum_size = lum_size;
1796 param->fp_lmd = calloc(1, sizeof(lstat_t) + lum_size);
1797 if (param->fp_lmd == NULL) {
1798 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1799 "error: allocation of %zu bytes for ioctl",
1800 sizeof(lstat_t) + param->fp_lum_size);
1804 param->fp_lmv_stripe_count = 256;
1805 param->fp_lmv_md = calloc(1,
1806 lmv_user_md_size(param->fp_lmv_stripe_count,
1807 LMV_USER_MAGIC_SPECIFIC));
1808 if (param->fp_lmv_md == NULL) {
1809 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1810 "error: allocation of %d bytes for ioctl",
1811 lmv_user_md_size(param->fp_lmv_stripe_count,
1812 LMV_USER_MAGIC_SPECIFIC));
1813 find_param_fini(param);
1817 param->fp_got_uuids = 0;
1818 param->fp_obd_indexes = NULL;
1819 param->fp_obd_index = OBD_NOT_FOUND;
1820 param->fp_mdt_index = OBD_NOT_FOUND;
1824 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1825 struct dirent64 *de)
1827 struct find_param *param = data;
1833 /* set errno upon failure */
1834 static DIR *opendir_parent(const char *path)
1840 path_copy = strdup(path);
1841 if (path_copy == NULL)
1844 parent_path = dirname(path_copy);
1845 parent = opendir(parent_path);
1851 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1856 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1857 if (param->fp_get_default_lmv)
1858 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1860 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1862 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1863 if (errno == E2BIG && ret != 0) {
1867 /* if foreign LMV case, fake stripes number */
1868 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
1869 struct lmv_foreign_md *lfm;
1871 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1872 if (lfm->lfm_length < XATTR_SIZE_MAX -
1873 offsetof(typeof(*lfm), lfm_value)) {
1874 uint32_t size = lfm->lfm_length +
1875 offsetof(typeof(*lfm), lfm_value);
1877 stripe_count = lmv_foreign_to_md_stripes(size);
1879 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1880 "error: invalid %d foreign size "
1881 "returned from ioctl",
1886 stripe_count = param->fp_lmv_md->lum_stripe_count;
1888 if (stripe_count <= param->fp_lmv_stripe_count)
1891 free(param->fp_lmv_md);
1892 param->fp_lmv_stripe_count = stripe_count;
1893 lmv_size = lmv_user_md_size(stripe_count,
1894 LMV_USER_MAGIC_SPECIFIC);
1895 param->fp_lmv_md = malloc(lmv_size);
1896 if (param->fp_lmv_md == NULL) {
1897 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1898 "error: allocation of %d bytes for ioctl",
1899 lmv_user_md_size(param->fp_lmv_stripe_count,
1900 LMV_USER_MAGIC_SPECIFIC));
1908 int get_lmd_info_fd(char *path, int parent_fd, int dir_fd,
1909 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1911 struct lov_user_mds_data *lmd = lmdbuf;
1912 lstat_t *st = &lmd->lmd_st;
1915 if (parent_fd < 0 && dir_fd < 0)
1917 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1921 /* LL_IOC_MDC_GETINFO operates on the current directory inode
1922 * and returns struct lov_user_mds_data, while
1923 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
1925 ret = ioctl(dir_fd, type == GET_LMD_INFO ? LL_IOC_MDC_GETINFO :
1926 LL_IOC_LOV_GETSTRIPE,
1928 } else if (parent_fd >= 0) {
1929 char *fname = strrchr(path, '/');
1931 /* IOC_MDC_GETFILEINFO takes as input the filename (relative to
1932 * the parent directory) and returns struct lov_user_mds_data,
1933 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
1935 * This avoids opening, locking, and closing each file on the
1936 * client if that is not needed. Multiple of these ioctl() can
1937 * be done on the parent dir with a single open for all
1938 * files in that directory, and it also doesn't pollute the
1939 * client dcache with millions of dentries when traversing
1940 * a large filesystem.
1942 fname = (fname == NULL ? path : fname + 1);
1944 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
1947 else if (ret >= lmdlen || ret++ == 0)
1950 ret = ioctl(parent_fd, type == GET_LMD_INFO ?
1951 IOC_MDC_GETFILEINFO :
1952 IOC_MDC_GETFILESTRIPE, lmdbuf);
1955 if (ret && type == GET_LMD_INFO) {
1956 if (errno == ENOTTY) {
1957 /* ioctl is not supported, it is not a lustre fs.
1958 * Do the regular lstat(2) instead.
1960 ret = lstat_f(path, st);
1963 llapi_error(LLAPI_MSG_ERROR, ret,
1964 "error: %s: lstat failed for %s",
1967 } else if (errno == ENOENT) {
1969 llapi_error(LLAPI_MSG_WARN, ret,
1970 "warning: %s does not exist", path);
1971 } else if (errno != EISDIR && errno != ENODATA) {
1973 llapi_error(LLAPI_MSG_ERROR, ret,
1974 "%s ioctl failed for %s.",
1975 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
1976 "IOC_MDC_GETFILEINFO", path);
1983 static int get_lmd_info(char *path, DIR *parent, DIR *dir, void *lmdbuf,
1984 int lmdlen, enum get_lmd_info_type type)
1990 parent_fd = dirfd(parent);
1992 dir_fd = dirfd(dir);
1994 return get_lmd_info_fd(path, parent_fd, dir_fd, lmdbuf, lmdlen, type);
1997 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1998 semantic_func_t sem_init,
1999 semantic_func_t sem_fini, void *data,
2000 struct dirent64 *de)
2002 struct find_param *param = (struct find_param *)data;
2003 struct dirent64 *dent;
2011 if (!d && errno != ENOTDIR) {
2013 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2016 } else if (!d && !parent) {
2017 /* ENOTDIR. Open the parent dir. */
2018 p = opendir_parent(path);
2025 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
2031 while ((dent = readdir64(d)) != NULL) {
2034 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2038 if ((len + dent->d_reclen + 2) > size) {
2039 llapi_err_noerrno(LLAPI_MSG_ERROR,
2040 "error: %s: string buffer too small",
2045 strcat(path, dent->d_name);
2047 if (dent->d_type == DT_UNKNOWN) {
2048 lstat_t *st = ¶m->fp_lmd->lmd_st;
2050 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
2051 param->fp_lum_size, GET_LMD_INFO);
2053 dent->d_type = IFTODT(st->st_mode);
2060 switch (dent->d_type) {
2062 llapi_err_noerrno(LLAPI_MSG_ERROR,
2063 "error: %s: '%s' is UNKNOWN type %d",
2064 __func__, dent->d_name, dent->d_type);
2067 rc = llapi_semantic_traverse(path, size, d, sem_init,
2068 sem_fini, data, dent);
2069 if (rc != 0 && ret == 0)
2075 rc = sem_init(path, d, NULL, data, dent);
2076 if (rc < 0 && ret == 0) {
2081 if (sem_fini && rc == 0)
2082 sem_fini(path, d, NULL, data, dent);
2090 sem_fini(path, parent, &d, data, de);
2099 static int param_callback(char *path, semantic_func_t sem_init,
2100 semantic_func_t sem_fini, struct find_param *param)
2102 int ret, len = strlen(path);
2105 if (len > PATH_MAX) {
2107 llapi_error(LLAPI_MSG_ERROR, ret,
2108 "Path name '%s' is too long", path);
2112 buf = (char *)malloc(PATH_MAX + 1);
2116 snprintf(buf, PATH_MAX + 1, "%s", path);
2117 ret = common_param_init(param, buf);
2121 param->fp_depth = 0;
2123 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
2124 sem_fini, param, NULL);
2126 find_param_fini(param);
2128 return ret < 0 ? ret : 0;
2131 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2133 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
2137 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2142 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2144 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2147 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2152 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2156 fd = open(path, O_RDONLY | O_NONBLOCK);
2159 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
2163 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2169 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2173 fd = open(path, O_RDONLY | O_NONBLOCK);
2176 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2180 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2192 * If uuidp is NULL, return the number of available obd uuids.
2193 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2194 * there are more OSTs than allocated to uuidp, then an error is returned with
2195 * the ost_count set to number of available obd uuids.
2197 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2198 int *ost_count, enum tgt_type type)
2200 char buf[PATH_MAX], format[32];
2201 int rc = 0, index = 0;
2202 struct obd_uuid name;
2206 /* Get the lov name */
2207 if (type == LOV_TYPE)
2208 rc = llapi_file_fget_lov_uuid(fd, &name);
2210 rc = llapi_file_fget_lmv_uuid(fd, &name);
2214 /* Now get the ost uuids */
2215 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2216 FILTER_BY_EXACT, "target_obd", ¶m);
2220 fp = fopen(param.gl_pathv[0], "r");
2223 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2228 snprintf(format, sizeof(format),
2229 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2230 while (fgets(buf, sizeof(buf), fp) != NULL) {
2231 if (uuidp && (index < *ost_count)) {
2232 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2240 if (uuidp && (index > *ost_count))
2245 cfs_free_param_data(¶m);
2249 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2251 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2254 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2259 root = open(mnt, O_RDONLY | O_DIRECTORY);
2262 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2267 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2275 /* Check if user specified value matches a real uuid. Ignore _UUID,
2276 * -osc-4ba41334, other trailing gunk in comparison.
2277 * @param real_uuid ends in "_UUID"
2278 * @param search_uuid may or may not end in "_UUID"
2280 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2282 int cmplen = strlen(real_uuid);
2283 int searchlen = strlen(search_uuid);
2285 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2287 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2290 /* The UUIDs may legitimately be different lengths, if
2291 * the system was upgraded from an older version. */
2292 if (cmplen != searchlen)
2295 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2298 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2299 * returned in param->fp_obd_index */
2300 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2302 struct obd_uuid obd_uuid;
2309 if (param->fp_got_uuids)
2312 /* Get the lov/lmv name */
2313 if (param->fp_get_lmv)
2314 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2316 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2318 if (rc != -ENOTTY) {
2319 llapi_error(LLAPI_MSG_ERROR, rc,
2320 "error: can't get %s name: %s",
2321 param->fp_get_lmv ? "lmv" : "lov",
2329 param->fp_got_uuids = 1;
2331 /* Now get the ost uuids */
2332 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2333 obd_uuid.uuid, FILTER_BY_EXACT,
2334 "target_obd", ¶m_data);
2338 fp = fopen(param_data.gl_pathv[0], "r");
2341 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2342 param_data.gl_pathv[0]);
2346 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2347 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2348 param->fp_get_lmv ? "MDTS" : "OBDS");
2350 snprintf(format, sizeof(format),
2351 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2352 while (fgets(buf, sizeof(buf), fp) != NULL) {
2355 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2358 if (param->fp_obd_uuid) {
2359 if (llapi_uuid_match(obd_uuid.uuid,
2360 param->fp_obd_uuid->uuid)) {
2361 param->fp_obd_index = index;
2364 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2365 /* Print everything */
2366 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2369 param->fp_obds_printed = 1;
2373 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2374 llapi_err_noerrno(LLAPI_MSG_ERROR,
2375 "error: %s: unknown obduuid: %s",
2376 __func__, param->fp_obd_uuid->uuid);
2380 cfs_free_param_data(¶m_data);
2384 /* In this case, param->fp_obd_uuid will be an array of obduuids and
2385 * obd index for all these obduuids will be returned in
2386 * param->fp_obd_indexes */
2387 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2388 int num_obds, int **obdindexes, int *obdindex,
2391 int ret, obdcount, obd_valid = 0, obdnum;
2393 struct obd_uuid *uuids = NULL;
2397 if (type == LOV_TYPE)
2398 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2400 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2404 obdcount = atoi(buf);
2405 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2410 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2412 if (ret == -EOVERFLOW) {
2413 struct obd_uuid *uuids_temp;
2415 uuids_temp = realloc(uuids, obdcount *
2416 sizeof(struct obd_uuid));
2417 if (uuids_temp != NULL) {
2419 goto retry_get_uuids;
2424 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2428 indexes = malloc(num_obds * sizeof(*obdindex));
2429 if (indexes == NULL) {
2434 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2437 /* The user may have specified a simple index */
2438 i = strtol(obduuids[obdnum].uuid, &end, 0);
2439 if (end && *end == '\0' && i < obdcount) {
2440 indexes[obdnum] = i;
2443 for (i = 0; i < obdcount; i++) {
2444 if (llapi_uuid_match(uuids[i].uuid,
2445 obduuids[obdnum].uuid)) {
2446 indexes[obdnum] = i;
2452 if (i >= obdcount) {
2453 indexes[obdnum] = OBD_NOT_FOUND;
2454 llapi_err_noerrno(LLAPI_MSG_ERROR,
2455 "invalid obduuid '%s'",
2456 obduuids[obdnum].uuid);
2462 *obdindex = OBD_NOT_FOUND;
2464 *obdindex = obd_valid;
2466 *obdindexes = indexes;
2474 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2478 if (param->fp_mdt_uuid) {
2479 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2481 ¶m->fp_mdt_indexes,
2482 ¶m->fp_mdt_index, LMV_TYPE);
2487 if (param->fp_obd_uuid) {
2488 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2490 ¶m->fp_obd_indexes,
2491 ¶m->fp_obd_index, LOV_TYPE);
2496 param->fp_got_uuids = 1;
2501 int llapi_ostlist(char *path, struct find_param *param)
2506 fd = open(path, O_RDONLY | O_DIRECTORY);
2510 ret = setup_obd_uuid(fd, path, param);
2517 * Tries to determine the default stripe attributes for a given filesystem. The
2518 * filesystem to check should be specified by fsname, or will be determined
2521 static int sattr_get_defaults(const char *const fsname,
2522 unsigned int *scount,
2523 unsigned int *ssize,
2524 unsigned int *soffset)
2530 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2531 "stripecount", val, sizeof(val));
2534 *scount = atoi(val);
2538 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2539 "stripesize", val, sizeof(val));
2546 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2547 "stripeoffset", val, sizeof(val));
2550 *soffset = atoi(val);
2557 * Tries to gather the default stripe attributes for a given filesystem. If
2558 * the attributes can be determined, they are cached for easy retreival the
2559 * next time they are needed. Only a single filesystem's attributes are
2562 int sattr_cache_get_defaults(const char *const fsname,
2563 const char *const pathname, unsigned int *scount,
2564 unsigned int *ssize, unsigned int *soffset)
2567 char fsname[PATH_MAX + 1];
2568 unsigned int stripecount;
2569 unsigned int stripesize;
2570 unsigned int stripeoffset;
2576 char fsname_buf[PATH_MAX + 1];
2577 unsigned int tmp[3];
2579 if (fsname == NULL) {
2580 rc = llapi_search_fsname(pathname, fsname_buf);
2584 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2587 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2589 * Ensure all 3 sattrs (count, size, and offset) are
2590 * successfully retrieved and stored in tmp before writing to
2593 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2597 cache.stripecount = tmp[0];
2598 cache.stripesize = tmp[1];
2599 cache.stripeoffset = tmp[2];
2600 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2604 *scount = cache.stripecount;
2606 *ssize = cache.stripesize;
2608 *soffset = cache.stripeoffset;
2613 static char *layout2name(__u32 layout_pattern)
2615 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2617 else if (layout_pattern == LOV_PATTERN_MDT)
2619 else if (layout_pattern == LOV_PATTERN_RAID0)
2621 else if (layout_pattern ==
2622 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2623 return "raid0,overstriped";
2628 enum lov_dump_flags {
2629 LDF_IS_DIR = 0x0001,
2630 LDF_IS_RAW = 0x0002,
2631 LDF_INDENT = 0x0004,
2632 LDF_SKIP_OBJS = 0x0008,
2634 LDF_EXTENSION = 0x0020,
2637 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2638 struct lov_user_ost_data_v1 *objects,
2639 enum llapi_layout_verbose verbose,
2640 int depth, char *pool_name,
2641 enum lov_dump_flags flags)
2643 bool is_dir = flags & LDF_IS_DIR;
2644 bool is_raw = flags & LDF_IS_RAW;
2645 bool indent = flags & LDF_INDENT;
2646 bool yaml = flags & LDF_YAML;
2647 bool skip_objs = flags & LDF_SKIP_OBJS;
2648 bool extension = flags & LDF_EXTENSION;
2649 char *prefix = is_dir ? "" : "lmm_";
2650 char *separator = "";
2651 char *space = indent ? " " : "";
2654 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2655 lmm_oi_set_seq(&lum->lmm_oi, 0);
2656 if (!indent && (verbose & VERBOSE_DETAIL))
2657 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2660 if (!yaml && !indent && depth && path &&
2661 ((verbose != VERBOSE_OBJID) || !is_dir))
2662 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2664 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2665 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2666 space, prefix, lum->lmm_magic);
2667 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2669 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2670 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2672 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2674 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2679 if (verbose & ~VERBOSE_DFID)
2680 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2682 /* This needs a bit of hand-holding since old 1.x lmm_oi
2683 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2684 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2685 * a real FID. Ideally the 2.x code would have stored this
2686 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2687 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2688 * worked properly (especially since IGIF FIDs use mds_inum as
2689 * the FID SEQ), but unfortunately that didn't happen.
2691 * Print it to look like an IGIF FID, even though the fields
2692 * are reversed on disk, so that it makes sense to userspace.
2694 * Don't use ostid_id() and ostid_seq(), since they assume the
2695 * oi_fid fields are in the right order. This is why there are
2696 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2698 * For newer layout types hopefully this will be a real FID. */
2699 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2700 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2701 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2702 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2703 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2705 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2708 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2712 if (verbose & VERBOSE_STRIPE_COUNT) {
2713 if (verbose & ~VERBOSE_STRIPE_COUNT)
2714 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2717 if (!is_raw && lum->lmm_stripe_count == 0 &&
2718 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2719 unsigned int scount;
2720 rc = sattr_cache_get_defaults(NULL, path,
2724 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2727 llapi_error(LLAPI_MSG_ERROR, rc,
2728 "Cannot determine default"
2731 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2733 (__s16)lum->lmm_stripe_count);
2736 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2738 (__s16)lum->lmm_stripe_count);
2740 if (!yaml && is_dir)
2746 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
2747 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
2748 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2749 if (verbose & ~VERBOSE_EXT_SIZE && extension)
2750 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
2752 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
2753 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2755 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2757 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2760 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2762 llapi_error(LLAPI_MSG_ERROR, rc,
2763 "Cannot determine default"
2766 /* Extension size is in KiB */
2767 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2769 lum->lmm_stripe_size * SEL_UNIT_SIZE :
2770 lum->lmm_stripe_size);
2772 if (!yaml && is_dir)
2778 if ((verbose & VERBOSE_PATTERN)) {
2779 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2780 if (verbose & ~VERBOSE_PATTERN)
2781 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2783 if (lov_pattern_supported(lum->lmm_pattern))
2784 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2785 layout2name(lum->lmm_pattern));
2787 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2788 separator = (!yaml && is_dir) ? " " : "\n";
2791 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2792 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2793 if (verbose & ~VERBOSE_GENERATION)
2794 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2796 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2797 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2801 if (verbose & VERBOSE_STRIPE_OFFSET) {
2802 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2803 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2804 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2806 if (is_dir || skip_objs)
2807 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2808 lum->lmm_stripe_offset ==
2809 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2810 lum->lmm_stripe_offset);
2811 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
2812 llapi_printf(LLAPI_MSG_NORMAL, "0");
2814 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2815 objects[0].l_ost_idx);
2816 if (!yaml && is_dir)
2822 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2823 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2824 if (verbose & ~VERBOSE_POOL)
2825 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2827 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2828 if (!yaml && is_dir)
2834 if (strlen(separator) != 0)
2835 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2838 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2839 struct lov_user_ost_data_v1 *objects,
2840 char *path, int obdindex, int depth,
2841 enum llapi_layout_verbose verbose,
2842 enum lov_dump_flags flags)
2844 bool is_dir = flags & LDF_IS_DIR;
2845 bool indent = flags & LDF_INDENT;
2846 bool skip_objs = flags & LDF_SKIP_OBJS;
2847 bool yaml = flags & LDF_YAML;
2848 bool obdstripe = obdindex == OBD_NOT_FOUND;
2851 if (!obdstripe && !skip_objs) {
2852 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2853 if (obdindex == objects[i].l_ost_idx) {
2863 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2866 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
2867 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2868 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
2869 char *space = " - ";
2872 llapi_printf(LLAPI_MSG_NORMAL,
2873 "%6slmm_objects:\n", " ");
2875 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2877 llapi_printf(LLAPI_MSG_NORMAL,
2878 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2880 for (i = 0; i < lum->lmm_stripe_count; i++) {
2881 int idx = objects[i].l_ost_idx;
2882 long long oid = ostid_id(&objects[i].l_ost_oi);
2883 long long gr = ostid_seq(&objects[i].l_ost_oi);
2885 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2889 struct lu_fid fid = { 0 };
2891 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2892 llapi_printf(LLAPI_MSG_NORMAL,
2893 "%sl_ost_idx: %d\n", space, idx);
2894 llapi_printf(LLAPI_MSG_NORMAL,
2895 "%8sl_fid: "DFID_NOBRACE"\n",
2897 } else if (indent) {
2898 struct lu_fid fid = { 0 };
2900 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2901 llapi_printf(LLAPI_MSG_NORMAL,
2902 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
2903 space, i, idx, PFID(&fid));
2906 sprintf(fmt, "%s%s%s\n",
2907 "\t%6u\t%14llu\t%#13llx\t",
2908 (fid_seq_is_rsvd(gr) ||
2909 fid_seq_is_mdt0(gr)) ?
2910 "%14llu" : "%#14llx", "%s");
2911 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2913 obdindex == idx ? " *" : "");
2917 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2920 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2921 char *path, int obdindex, int depth,
2922 enum llapi_layout_verbose verbose,
2923 enum lov_dump_flags flags)
2925 struct lmv_user_mds_data *objects = lum->lum_objects;
2926 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2927 char *separator = "";
2928 bool yaml = flags & LDF_YAML;
2929 bool obdstripe = false;
2932 if (obdindex != OBD_NOT_FOUND) {
2933 if (lum->lum_stripe_count == 0) {
2934 if (obdindex == lum->lum_stripe_offset)
2937 for (i = 0; i < lum->lum_stripe_count; i++) {
2938 if (obdindex == objects[i].lum_mds) {
2939 llapi_printf(LLAPI_MSG_NORMAL,
2954 /* show all information default */
2956 if (lum->lum_magic == LMV_USER_MAGIC)
2957 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
2958 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
2960 verbose = VERBOSE_OBJID;
2963 if (depth && path && ((verbose != VERBOSE_OBJID)))
2964 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2966 if (verbose & VERBOSE_STRIPE_COUNT) {
2967 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2968 if (verbose & ~VERBOSE_STRIPE_COUNT)
2969 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2970 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2971 (int)lum->lum_stripe_count);
2972 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
2978 if (verbose & VERBOSE_STRIPE_OFFSET) {
2979 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2980 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2981 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2982 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2983 (int)lum->lum_stripe_offset);
2984 if (verbose & VERBOSE_HASH_TYPE && !yaml)
2990 if (verbose & VERBOSE_HASH_TYPE) {
2991 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
2992 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
2994 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2995 if (verbose & ~VERBOSE_HASH_TYPE)
2996 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
2997 if (type < LMV_HASH_TYPE_MAX)
2998 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2999 mdt_hash_name[type]);
3001 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3003 if (flags & LMV_HASH_FLAG_SPACE)
3004 llapi_printf(LLAPI_MSG_NORMAL, ",space");
3005 if (flags & LMV_HASH_FLAG_MIGRATION)
3006 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3007 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3008 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3009 if (flags & LMV_HASH_FLAG_LOST_LMV)
3010 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3015 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3016 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3017 if (lum->lum_stripe_count > 0)
3018 llapi_printf(LLAPI_MSG_NORMAL,
3019 "mdtidx\t\t FID[seq:oid:ver]\n");
3020 for (i = 0; i < lum->lum_stripe_count; i++) {
3021 int idx = objects[i].lum_mds;
3022 struct lu_fid *fid = &objects[i].lum_fid;
3023 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3024 llapi_printf(LLAPI_MSG_NORMAL,
3025 "%6u\t\t "DFID"\t\t%s\n",
3027 obdindex == idx ? " *" : "");
3032 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3033 pool_name[0] != '\0') {
3034 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3035 if (verbose & ~VERBOSE_POOL)
3036 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3038 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3042 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3043 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3046 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3047 enum lov_dump_flags flags)
3049 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3050 int depth = param->fp_max_depth;
3051 enum llapi_layout_verbose verbose = param->fp_verbose;
3052 bool yaml = flags & LDF_YAML;
3054 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3055 !(flags & LDF_IS_DIR)) && !yaml)
3056 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3058 if (verbose & VERBOSE_DETAIL) {
3059 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3060 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3061 " ", comp_v1->lcm_magic);
3062 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3063 " ", comp_v1->lcm_size);
3064 if (flags & LDF_IS_DIR)
3065 llapi_printf(LLAPI_MSG_NORMAL,
3066 "%2slcm_flags: %s\n", " ",
3067 comp_v1->lcm_mirror_count > 0 ?
3070 llapi_printf(LLAPI_MSG_NORMAL,
3071 "%2slcm_flags: %s\n", " ",
3072 llapi_layout_flags_string(comp_v1->lcm_flags));
3075 if (verbose & VERBOSE_GENERATION) {
3076 if (verbose & ~VERBOSE_GENERATION)
3077 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3079 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3082 if (verbose & VERBOSE_MIRROR_COUNT) {
3083 if (verbose & ~VERBOSE_MIRROR_COUNT)
3084 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3086 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3087 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3088 comp_v1->lcm_mirror_count + 1 : 1);
3091 if (verbose & VERBOSE_COMP_COUNT) {
3092 if (verbose & ~VERBOSE_COMP_COUNT)
3093 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3095 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3096 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3097 comp_v1->lcm_entry_count : 0);
3100 if (verbose & VERBOSE_DETAIL && !yaml)
3101 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3104 static void lcme_flags2str(__u32 comp_flags)
3110 llapi_printf(LLAPI_MSG_NORMAL, "0");
3113 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3114 if (comp_flags & comp_flags_table[i].cfn_flag) {
3116 llapi_printf(LLAPI_MSG_NORMAL, ",");
3117 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3118 comp_flags_table[i].cfn_name);
3119 comp_flags &= ~comp_flags_table[i].cfn_flag;
3125 llapi_printf(LLAPI_MSG_NORMAL, ",");
3126 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3130 static void lov_dump_comp_v1_entry(struct find_param *param,
3131 enum lov_dump_flags flags, int index)
3133 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3134 struct lov_comp_md_entry_v1 *entry;
3135 char *separator = "";
3136 enum llapi_layout_verbose verbose = param->fp_verbose;
3137 bool yaml = flags & LDF_YAML;
3139 entry = &comp_v1->lcm_entries[index];
3142 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3144 if (verbose & VERBOSE_COMP_ID) {
3145 if (verbose & VERBOSE_DETAIL && !yaml)
3146 llapi_printf(LLAPI_MSG_NORMAL,
3147 "%slcme_id: ", " - ");
3148 else if (verbose & ~VERBOSE_COMP_ID)
3149 llapi_printf(LLAPI_MSG_NORMAL,
3150 "%4slcme_id: ", " ");
3151 if (entry->lcme_id != LCME_ID_INVAL)
3152 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3154 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3158 if (verbose & VERBOSE_MIRROR_ID) {
3159 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3160 if (verbose & ~VERBOSE_MIRROR_ID)
3161 llapi_printf(LLAPI_MSG_NORMAL,
3162 "%4slcme_mirror_id: ", " ");
3163 if (entry->lcme_id != LCME_ID_INVAL)
3164 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3165 mirror_id_of(entry->lcme_id));
3167 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3171 if (verbose & VERBOSE_COMP_FLAGS) {
3172 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3173 if (verbose & ~VERBOSE_COMP_FLAGS)
3174 llapi_printf(LLAPI_MSG_NORMAL,
3175 "%4slcme_flags: ", " ");
3176 lcme_flags2str(entry->lcme_flags);
3179 /* print snapshot timestamp if its a nosync comp */
3180 if ((verbose & VERBOSE_COMP_FLAGS) &&
3181 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3182 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3183 if (verbose & ~VERBOSE_COMP_FLAGS)
3184 llapi_printf(LLAPI_MSG_NORMAL,
3185 "%4slcme_timestamp: ", " ");
3187 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3188 entry->lcme_timestamp);
3190 time_t stamp = entry->lcme_timestamp;
3191 char *date_str = asctime(localtime(&stamp));
3193 date_str[strlen(date_str) - 1] = '\0';
3194 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3200 if (verbose & VERBOSE_COMP_START) {
3201 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3202 if (verbose & ~VERBOSE_COMP_START)
3203 llapi_printf(LLAPI_MSG_NORMAL,
3204 "%4slcme_extent.e_start: ", " ");
3205 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3206 entry->lcme_extent.e_start);
3210 if (verbose & VERBOSE_COMP_END) {
3211 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3212 if (verbose & ~VERBOSE_COMP_END)
3213 llapi_printf(LLAPI_MSG_NORMAL,
3214 "%4slcme_extent.e_end: ", " ");
3215 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3216 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3218 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3219 entry->lcme_extent.e_end);
3224 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3225 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3226 } else if (verbose & VERBOSE_DETAIL) {
3227 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3228 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3229 " ", entry->lcme_offset);
3230 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3231 " ", entry->lcme_size);
3232 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3234 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3238 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3239 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3241 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3242 * The table below gives the answers for the specified parameters (value and
3243 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3244 * --------------------------------------
3245 * 1 | file > limit; sign > 0 | -1 / -1 |
3246 * 2 | file = limit; sign > 0 | -1 / -1 |
3247 * 3 | file < limit; sign > 0 | ? / 1 |
3248 * 4 | file > limit; sign = 0 | -1 / -1 |
3249 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3250 * 6 | file < limit; sign = 0 | ? / -1 |
3251 * 7 | file > limit; sign < 0 | 1 / 1 |
3252 * 8 | file = limit; sign < 0 | ? / -1 |
3253 * 9 | file < limit; sign < 0 | ? / -1 |
3254 * --------------------------------------
3255 * Note: 5th actually means that the value is within the interval
3256 * (limit - margin, limit]. */
3257 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3258 int sign, int negopt, unsigned long long margin,
3264 /* Drop the fraction of margin (of days or size). */
3265 if (file + margin <= limit)
3267 } else if (sign == 0) {
3268 if (file <= limit && file + margin > limit)
3270 else if (file + margin <= limit)
3272 } else if (sign < 0) {
3279 return negopt ? ~ret + 1 : ret;
3282 static inline struct lov_user_md *
3283 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3285 return (struct lov_user_md *)((char *)comp_v1 +
3286 comp_v1->lcm_entries[ent_idx].lcme_offset);
3289 static inline struct lov_user_ost_data_v1 *
3290 lov_v1v3_objects(struct lov_user_md *v1)
3292 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3293 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3295 return v1->lmm_objects;
3299 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3301 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3302 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3303 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3305 pool_name[0] = '\0';
3309 print_last_init_comp(struct find_param *param)
3311 /* print all component info */
3312 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3315 /* print specific component info */
3316 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3317 param->fp_check_comp_start || param->fp_check_comp_end ||
3318 param->fp_check_mirror_id || param->fp_check_mirror_index)
3324 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3328 if (param->fp_comp_end == LUSTRE_EOF) {
3329 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3330 match = end == LUSTRE_EOF ? 1 : -1;
3331 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3332 match = end == LUSTRE_EOF ? -1 : 1;
3333 else /* at least EOF */
3335 if (param->fp_exclude_comp_end)
3338 unsigned long long margin;
3340 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3341 match = find_value_cmp(end, param->fp_comp_end,
3342 param->fp_comp_end_sign,
3343 param->fp_exclude_comp_end, margin, 0);
3350 * An example of "getstripe -v" for a two components PFL file:
3353 * lcm_magic: 0x0BD60BD0
3357 * lcm_entry_count: 2
3361 * lcme_extent.e_start: 0
3362 * lcme_extent.e_end: 1048576
3366 * lmm_magic: 0x0BD10BD0
3367 * lmm_seq: 0x200000401
3368 * lmm_object_id: 0x1
3369 * lmm_fid: [0x200000401:0x1:0x0]
3370 * lmm_stripe_count: 1
3371 * lmm_stripe_size: 1048576
3372 * lmm_pattern: raid0
3374 * lmm_stripe_offset: 0
3376 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3380 * lcme_extent.e_start: 1048576
3381 * lcme_extent.e_end: EOF
3385 * lmm_magic: 0x0BD10BD0
3386 * lmm_seq: 0x200000401
3387 * lmm_object_id: 0x1
3388 * lmm_fid: [0x200000401:0x1:0x0]
3389 * lmm_stripe_count: 2
3390 * lmm_stripe_size: 1048576
3391 * lmm_pattern: raid0
3393 * lmm_stripe_offset: 1
3395 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3396 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3398 static void lov_dump_comp_v1(struct find_param *param, char *path,
3399 enum lov_dump_flags flags)
3401 struct lov_comp_md_entry_v1 *entry;
3402 struct lov_user_ost_data_v1 *objects;
3403 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3404 struct lov_user_md_v1 *v1;
3405 char pool_name[LOV_MAXPOOLNAME + 1];
3406 int obdindex = param->fp_obd_index;
3407 int i, j, match, ext;
3408 bool obdstripe = false;
3409 __u16 mirror_index = 0;
3410 __u16 mirror_id = 0;
3412 if (obdindex != OBD_NOT_FOUND) {
3413 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3414 i < comp_v1->lcm_entry_count; i++) {
3415 if (!(comp_v1->lcm_entries[i].lcme_flags &
3419 v1 = lov_comp_entry(comp_v1, i);
3420 objects = lov_v1v3_objects(v1);
3422 for (j = 0; j < v1->lmm_stripe_count; j++) {
3423 if (obdindex == objects[j].l_ost_idx) {
3436 lov_dump_comp_v1_header(param, path, flags);
3438 flags |= LDF_INDENT;
3440 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3441 entry = &comp_v1->lcm_entries[i];
3443 if (param->fp_check_comp_flags) {
3444 if (((param->fp_comp_flags & entry->lcme_flags) !=
3445 param->fp_comp_flags) ||
3446 (param->fp_comp_neg_flags & entry->lcme_flags))
3450 if (param->fp_check_comp_id &&
3451 param->fp_comp_id != entry->lcme_id)
3454 if (param->fp_check_comp_start) {
3455 match = find_value_cmp(entry->lcme_extent.e_start,
3456 param->fp_comp_start,
3457 param->fp_comp_start_sign,
3459 param->fp_comp_start_units, 0);
3464 if (param->fp_check_comp_end) {
3465 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3471 if (param->fp_check_mirror_index) {
3472 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3474 mirror_id = mirror_id_of(entry->lcme_id);
3477 match = find_value_cmp(mirror_index,
3478 param->fp_mirror_index,
3479 param->fp_mirror_index_sign,
3480 param->fp_exclude_mirror_index,
3484 } else if (param->fp_check_mirror_id) {
3485 if (mirror_id != mirror_id_of(entry->lcme_id))
3486 mirror_id = mirror_id_of(entry->lcme_id);
3488 match = find_value_cmp(mirror_id,
3489 param->fp_mirror_id,
3490 param->fp_mirror_id_sign,
3491 param->fp_exclude_mirror_id,
3497 if (print_last_init_comp(param)) {
3499 * if part of stripe info is needed, we'd print only
3500 * the last instantiated component info.
3502 if (entry->lcme_flags & LCME_FL_INIT)
3505 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3506 if (entry->lcme_flags & LCME_FL_EXTENSION)
3507 /* moved back below */
3515 if (entry->lcme_flags & LCME_FL_INIT) {
3516 if (obdindex != OBD_NOT_FOUND) {
3517 flags |= LDF_SKIP_OBJS;
3518 v1 = lov_comp_entry(comp_v1, i);
3519 objects = lov_v1v3_objects(v1);
3521 for (j = 0; j < v1->lmm_stripe_count; j++) {
3522 if (obdindex == objects[j].l_ost_idx) {
3523 flags &= ~LDF_SKIP_OBJS;
3528 flags &= ~LDF_SKIP_OBJS;
3531 flags |= LDF_SKIP_OBJS;
3534 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3536 lov_dump_comp_v1_entry(param, flags, i);
3538 v1 = lov_comp_entry(comp_v1, i);
3539 objects = lov_v1v3_objects(v1);
3540 lov_v1v3_pool_name(v1, pool_name);
3542 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3543 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3544 param->fp_max_depth, param->fp_verbose,
3547 if (print_last_init_comp(param)) {
3549 * directory layout contains only layout template, print the
3553 i = comp_v1->lcm_entry_count - 1;
3556 flags &= ~LDF_SKIP_OBJS;
3558 lov_dump_comp_v1_entry(param, flags, i);
3560 v1 = lov_comp_entry(comp_v1, i);
3561 objects = lov_v1v3_objects(v1);
3562 lov_v1v3_pool_name(v1, pool_name);
3564 entry = &comp_v1->lcm_entries[i];
3565 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3566 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3567 param->fp_max_depth, param->fp_verbose,
3572 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3573 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3576 static inline bool has_any_comp_options(struct find_param *param)
3578 enum llapi_layout_verbose verbose = param->fp_verbose;
3580 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3581 param->fp_check_comp_start || param->fp_check_comp_end ||
3582 param->fp_check_comp_flags)
3585 /* show full layout information, not component specific */
3586 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3589 return verbose & VERBOSE_COMP_OPTS;
3592 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3595 struct lov_user_md *lum = &orig->lmd_lmm;
3596 struct lov_user_mds_data *new;
3597 struct lov_comp_md_v1 *comp_v1;
3598 struct lov_comp_md_entry_v1 *ent;
3599 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3600 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3603 new = malloc(sizeof(lstat_t) + lum_off + lum_size);
3605 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3609 memcpy(new, orig, sizeof(lstat_t));
3611 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3612 comp_v1->lcm_magic = lum->lmm_magic;
3613 comp_v1->lcm_size = lum_off + lum_size;
3614 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3615 comp_v1->lcm_flags = 0;
3616 comp_v1->lcm_entry_count = 1;
3618 ent = &comp_v1->lcm_entries[0];
3620 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3621 ent->lcme_extent.e_start = 0;
3622 ent->lcme_extent.e_end = LUSTRE_EOF;
3623 ent->lcme_offset = lum_off;
3624 ent->lcme_size = lum_size;
3626 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3631 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3632 enum lov_dump_flags flags)
3634 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3636 if (has_any_comp_options(param)) {
3637 struct lov_user_mds_data *new_lmd, *orig_lmd;
3639 orig_lmd = param->fp_lmd;
3640 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3641 if (new_lmd != NULL) {
3642 param->fp_lmd = new_lmd;
3643 lov_dump_comp_v1(param, path, flags);
3644 param->fp_lmd = orig_lmd;
3650 if (magic == LOV_USER_MAGIC_V1) {
3651 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3652 param->fp_lmd->lmd_lmm.lmm_objects,
3653 path, param->fp_obd_index,
3654 param->fp_max_depth, param->fp_verbose,
3657 char pool_name[LOV_MAXPOOLNAME + 1];
3658 struct lov_user_ost_data_v1 *objects;
3659 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3661 snprintf(pool_name, sizeof(pool_name), "%s",
3662 lmmv3->lmm_pool_name);
3663 objects = lmmv3->lmm_objects;
3664 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3665 objects, path, param->fp_obd_index,
3666 param->fp_max_depth, param->fp_verbose,
3671 static uint32_t check_foreign_type(uint32_t foreign_type)
3675 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3676 if (lu_foreign_types[i].lft_name == NULL)
3678 if (foreign_type == lu_foreign_types[i].lft_type)
3682 return LU_FOREIGN_TYPE_UNKNOWN;
3685 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3686 enum lov_dump_flags flags)
3688 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3689 bool yaml = flags & LDF_YAML;
3691 if (!yaml && param->fp_depth && path)
3692 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3694 if (param->fp_verbose & VERBOSE_DETAIL) {
3695 uint32_t type = check_foreign_type(lfm->lfm_type);
3697 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3699 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3701 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3703 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3704 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3705 lu_foreign_types[type].lft_name);
3707 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3709 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3712 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3713 lfm->lfm_length, lfm->lfm_value);
3714 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3717 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3718 enum lov_dump_flags flags)
3720 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3721 bool yaml = flags & LDF_YAML;
3723 if (!yaml && param->fp_depth && path)
3724 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3726 if (param->fp_verbose & VERBOSE_DETAIL) {
3727 uint32_t type = check_foreign_type(lfm->lfm_type);
3729 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3731 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3733 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3735 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3736 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3737 lu_foreign_types[type].lft_name);
3739 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3741 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3744 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3745 lfm->lfm_length, lfm->lfm_value);
3746 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3749 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3750 enum lov_dump_flags flags)
3754 if (param->fp_get_lmv || param->fp_get_default_lmv)
3755 magic = (__u32)param->fp_lmv_md->lum_magic;
3757 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3760 flags |= LDF_IS_RAW;
3765 case LOV_USER_MAGIC_V1:
3766 case LOV_USER_MAGIC_V3:
3767 case LOV_USER_MAGIC_SPECIFIC:
3768 lov_dump_plain_user_lmm(param, path, flags);
3770 case LOV_USER_MAGIC_FOREIGN:
3771 lov_dump_foreign_lmm(param, path, flags);
3774 case LMV_USER_MAGIC: {
3775 char pool_name[LOV_MAXPOOLNAME + 1];
3776 struct lmv_user_md *lum;
3778 lum = (struct lmv_user_md *)param->fp_lmv_md;
3779 snprintf(pool_name, sizeof(pool_name), "%s",
3780 lum->lum_pool_name);
3781 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3782 param->fp_max_depth, param->fp_verbose,
3786 case LOV_USER_MAGIC_COMP_V1:
3787 lov_dump_comp_v1(param, path, flags);
3789 case LMV_MAGIC_FOREIGN:
3790 lmv_dump_foreign_lmm(param, path, flags);
3793 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
3794 "(expecting one of %#x %#x %#x %#x)\n",
3795 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3796 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3797 LMV_USER_MAGIC, LMV_MAGIC_V1);
3802 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3808 fname = strrchr(path, '/');
3810 /* It should be a file (or other non-directory) */
3811 if (fname == NULL) {
3812 dname = (char *)malloc(2);
3816 fname = (char *)path;
3818 dname = (char *)malloc(fname - path + 1);
3821 strncpy(dname, path, fname - path);
3822 dname[fname - path] = '\0';
3826 fd = open(dname, O_RDONLY | O_NONBLOCK);
3832 strcpy((char *)lum, fname);
3833 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3836 if (close(fd) == -1 && rc == 0)
3844 int llapi_file_lookup(int dirfd, const char *name)
3846 struct obd_ioctl_data data = { 0 };
3851 if (dirfd < 0 || name == NULL)
3854 data.ioc_version = OBD_IOCTL_VERSION;
3855 data.ioc_len = sizeof(data);
3856 data.ioc_inlbuf1 = (char *)name;
3857 data.ioc_inllen1 = strlen(name) + 1;
3859 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3861 llapi_error(LLAPI_MSG_ERROR, rc,
3862 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3867 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
3873 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
3874 * Return -1 or 1 if file timestamp does not or does match the given criteria
3875 * correspondingly. Return 0 if the MDS time is being checked and there are
3876 * attributes on OSTs and it is not yet clear if the timespamp matches.
3878 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
3879 * updated timestamps. */
3880 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
3885 /* Check if file is accepted. */
3886 if (param->fp_atime) {
3887 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
3888 param->fp_asign, param->fp_exclude_atime,
3889 param->fp_time_margin, mds);
3895 if (param->fp_mtime) {
3896 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
3897 param->fp_msign, param->fp_exclude_mtime,
3898 param->fp_time_margin, mds);
3902 /* If the previous check matches, but this one is not yet clear,
3903 * we should return 0 to do an RPC on OSTs. */
3908 if (param->fp_ctime) {
3909 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
3910 param->fp_csign, param->fp_exclude_ctime,
3911 param->fp_time_margin, mds);
3915 /* If the previous check matches, but this one is not yet clear,
3916 * we should return 0 to do an RPC on OSTs. */
3925 * Check whether the stripes matches the indexes user provided
3929 static int check_obd_match(struct find_param *param)
3931 struct lov_user_ost_data_v1 *objects;
3932 struct lov_comp_md_v1 *comp_v1 = NULL;
3933 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3934 lstat_t *st = ¶m->fp_lmd->lmd_st;
3935 int i, j, k, count = 1;
3937 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
3940 if (!S_ISREG(st->st_mode))
3943 /* exclude foreign */
3944 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
3945 return param->fp_exclude_obd;
3947 /* Only those files should be accepted, which have a
3948 * stripe on the specified OST. */
3949 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3950 comp_v1 = (struct lov_comp_md_v1 *)v1;
3951 count = comp_v1->lcm_entry_count;
3954 for (i = 0; i < count; i++) {
3956 v1 = lov_comp_entry(comp_v1, i);
3958 objects = lov_v1v3_objects(v1);
3960 for (j = 0; j < v1->lmm_stripe_count; j++) {
3961 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
3964 for (k = 0; k < param->fp_num_obds; k++) {
3965 if (param->fp_obd_indexes[k] ==
3966 objects[j].l_ost_idx)
3967 return !param->fp_exclude_obd;
3972 return param->fp_exclude_obd;
3975 static int check_mdt_match(struct find_param *param)
3979 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
3982 /* FIXME: For striped dir, we should get stripe information and check */
3983 for (i = 0; i < param->fp_num_mdts; i++) {
3984 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
3985 return !param->fp_exclude_mdt;
3988 if (param->fp_exclude_mdt)
3995 * Check whether the obd is active or not, if it is
3996 * not active, just print the object affected by this
3999 static int print_failed_tgt(struct find_param *param, char *path, int type)
4001 struct obd_statfs stat_buf;
4002 struct obd_uuid uuid_buf;
4005 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV)
4008 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4009 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4010 ret = llapi_obd_statfs(path, type,
4011 param->fp_obd_index, &stat_buf,
4014 llapi_printf(LLAPI_MSG_NORMAL,
4015 "obd_uuid: %s failed %s ",
4016 param->fp_obd_uuid->uuid,
4023 static int find_check_stripe_size(struct find_param *param)
4025 struct lov_comp_md_v1 *comp_v1 = NULL;
4026 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4027 __u32 stripe_size = 0;
4028 int ret, i, count = 1;
4030 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4031 return param->fp_exclude_stripe_size ? 1 : -1;
4033 ret = param->fp_exclude_stripe_size ? 1 : -1;
4034 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4035 comp_v1 = (struct lov_comp_md_v1 *)v1;
4036 count = comp_v1->lcm_entry_count;
4039 for (i = 0; i < count; i++) {
4040 struct lov_comp_md_entry_v1 *ent;
4043 v1 = lov_comp_entry(comp_v1, i);
4045 ent = &comp_v1->lcm_entries[i];
4046 if (ent->lcme_flags & LCME_FL_EXTENSION)
4048 if (!(ent->lcme_flags & LCME_FL_INIT))
4051 stripe_size = v1->lmm_stripe_size;
4054 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4055 param->fp_stripe_size_sign,
4056 param->fp_exclude_stripe_size,
4057 param->fp_stripe_size_units, 0);
4062 static int find_check_ext_size(struct find_param *param)
4064 struct lov_comp_md_v1 *comp_v1;
4065 struct lov_user_md_v1 *v1;
4068 ret = param->fp_exclude_ext_size ? 1 : -1;
4069 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4070 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4073 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4074 struct lov_comp_md_entry_v1 *ent;
4076 v1 = lov_comp_entry(comp_v1, i);
4078 ent = &comp_v1->lcm_entries[i];
4079 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4082 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4083 param->fp_ext_size_sign,
4084 param->fp_exclude_ext_size,
4085 param->fp_ext_size_units, 0);
4086 /* If any ext_size matches */
4094 static __u32 find_get_stripe_count(struct find_param *param)
4096 struct lov_comp_md_v1 *comp_v1 = NULL;
4097 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4099 __u32 stripe_count = 0;
4101 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4104 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4105 comp_v1 = (struct lov_comp_md_v1 *)v1;
4106 count = comp_v1->lcm_entry_count;
4109 for (i = 0; i < count; i++) {
4111 struct lov_comp_md_entry_v1 *ent;
4113 v1 = lov_comp_entry(comp_v1, i);
4115 ent = &comp_v1->lcm_entries[i];
4116 if (!(ent->lcme_flags & LCME_FL_INIT))
4119 if (ent->lcme_flags & LCME_FL_EXTENSION)
4122 stripe_count = v1->lmm_stripe_count;
4125 return stripe_count;
4128 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4130 static int find_check_layout(struct find_param *param)
4132 struct lov_comp_md_v1 *comp_v1 = NULL;
4133 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4135 bool found = false, valid = false;
4137 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4138 comp_v1 = (struct lov_comp_md_v1 *)v1;
4139 count = comp_v1->lcm_entry_count;
4142 for (i = 0; i < count; i++) {
4144 v1 = lov_comp_entry(comp_v1, i);
4146 /* foreign file have a special magic but no pattern field */
4147 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4150 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4154 if (v1->lmm_pattern & param->fp_layout) {
4163 if ((found && !param->fp_exclude_layout) ||
4164 (!found && param->fp_exclude_layout))
4170 /* if no type specified, check/exclude all foreign
4171 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4173 static int find_check_foreign(struct find_param *param)
4175 if (S_ISREG(param->fp_lmd->lmd_st.st_mode)) {
4176 struct lov_foreign_md *lfm;
4178 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4179 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4180 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4181 return param->fp_exclude_foreign ? 1 : -1;
4184 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4185 lfm->lfm_type == param->fp_foreign_type)
4186 return param->fp_exclude_foreign ? -1 : 1;
4187 return param->fp_exclude_foreign ? 1 : -1;
4191 if (S_ISDIR(param->fp_lmd->lmd_st.st_mode)) {
4192 struct lmv_foreign_md *lfm;
4194 lfm = (void *)param->fp_lmv_md;
4195 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
4196 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4197 return param->fp_exclude_foreign ? 1 : -1;
4200 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4201 lfm->lfm_type == param->fp_foreign_type)
4202 return param->fp_exclude_foreign ? -1 : 1;
4203 return param->fp_exclude_foreign ? 1 : -1;
4209 static int find_check_pool(struct find_param *param)
4211 struct lov_comp_md_v1 *comp_v1 = NULL;
4212 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4213 struct lov_user_md_v3 *v3 = (void *)v1;
4217 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4218 comp_v1 = (struct lov_comp_md_v1 *)v1;
4219 count = comp_v1->lcm_entry_count;
4220 /* empty requested pool is taken as no pool search */
4221 if (count == 0 && param->fp_poolname[0] == '\0')
4225 for (i = 0; i < count; i++) {
4226 if (comp_v1 != NULL)
4227 v1 = lov_comp_entry(comp_v1, i);
4229 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4232 if (((v1->lmm_magic == LOV_USER_MAGIC_V1) &&
4233 (param->fp_poolname[0] == '\0')) ||
4234 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4235 (strncmp(v3->lmm_pool_name,
4236 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4237 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4238 (strcmp(param->fp_poolname, "*") == 0))) {
4244 if ((found && !param->fp_exclude_pool) ||
4245 (!found && param->fp_exclude_pool))
4251 static int find_check_comp_options(struct find_param *param)
4253 lstat_t *st = ¶m->fp_lmd->lmd_st;
4254 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4255 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4256 struct lov_comp_md_entry_v1 *entry;
4259 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4262 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4263 comp_v1 = (struct lov_comp_md_v1 *)v1;
4265 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4266 if (forged_v1 == NULL)
4268 comp_v1 = forged_v1;
4269 comp_v1->lcm_entry_count = 1;
4270 entry = &comp_v1->lcm_entries[0];
4271 entry->lcme_flags = S_ISDIR(st->st_mode) ? 0 : LCME_FL_INIT;
4272 entry->lcme_extent.e_start = 0;
4273 entry->lcme_extent.e_end = LUSTRE_EOF;
4276 /* invalid case, don't match for any kind of search. */
4277 if (comp_v1->lcm_entry_count == 0) {
4282 if (param->fp_check_comp_count) {
4283 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4284 param->fp_comp_count,
4285 param->fp_comp_count_sign,
4286 param->fp_exclude_comp_count, 1, 0);
4292 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4293 entry = &comp_v1->lcm_entries[i];
4295 if (param->fp_check_comp_flags) {
4297 if (((param->fp_comp_flags & entry->lcme_flags) !=
4298 param->fp_comp_flags) ||
4299 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4305 if (param->fp_check_comp_start) {
4306 ret = find_value_cmp(entry->lcme_extent.e_start,
4307 param->fp_comp_start,
4308 param->fp_comp_start_sign,
4309 param->fp_exclude_comp_start,
4310 param->fp_comp_start_units, 0);
4315 if (param->fp_check_comp_end) {
4316 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4322 /* the component matches all criteria */
4331 static int find_check_mirror_options(struct find_param *param)
4333 struct lov_comp_md_v1 *comp_v1;
4334 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4337 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4340 comp_v1 = (struct lov_comp_md_v1 *)v1;
4342 if (param->fp_check_mirror_count) {
4343 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4344 param->fp_mirror_count,
4345 param->fp_mirror_count_sign,
4346 param->fp_exclude_mirror_count, 1, 0);
4351 if (param->fp_check_mirror_state) {
4353 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4355 if ((param->fp_mirror_state != 0 &&
4356 file_state != param->fp_mirror_state) ||
4357 file_state == param->fp_mirror_neg_state)
4364 static bool find_check_lmm_info(struct find_param *param)
4366 return param->fp_check_pool || param->fp_check_stripe_count ||
4367 param->fp_check_stripe_size || param->fp_check_layout ||
4368 param->fp_check_comp_count || param->fp_check_comp_end ||
4369 param->fp_check_comp_start || param->fp_check_comp_flags ||
4370 param->fp_check_mirror_count || param->fp_check_foreign ||
4371 param->fp_check_mirror_state || param->fp_check_ext_size ||
4372 param->fp_check_projid;
4376 * Get file/directory project id.
4377 * by the open fd resides on.
4378 * Return 0 and project id on success, or -ve errno.
4380 static int fget_projid(int fd, int *projid)
4385 rc = ioctl(fd, LL_IOC_FSGETXATTR, &fsx);
4389 *projid = fsx.fsx_projid;
4393 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
4394 void *data, struct dirent64 *de)
4396 struct find_param *param = (struct find_param *)data;
4397 DIR *dir = dirp == NULL ? NULL : *dirp;
4398 int decision = 1; /* 1 is accepted; -1 is rejected. */
4399 lstat_t *st = ¶m->fp_lmd->lmd_st;
4401 int checked_type = 0;
4403 __u32 stripe_count = 0;
4406 if (parent == NULL && dir == NULL)
4409 /* If a regular expression is presented, make the initial decision */
4410 if (param->fp_pattern != NULL) {
4411 char *fname = strrchr(path, '/');
4412 fname = (fname == NULL ? path : fname + 1);
4413 ret = fnmatch(param->fp_pattern, fname, 0);
4414 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4415 (ret == 0 && param->fp_exclude_pattern))
4419 /* See if we can check the file type from the dirent. */
4420 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
4423 if (DTTOIF(de->d_type) == param->fp_type) {
4424 if (param->fp_exclude_type)
4427 if (!param->fp_exclude_type)
4434 /* Request MDS for the stat info if some of these parameters need
4435 * to be compared. */
4436 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4437 param->fp_check_uid || param->fp_check_gid ||
4438 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4439 param->fp_check_size || param->fp_check_blocks ||
4440 find_check_lmm_info(param) ||
4441 param->fp_check_mdt_count || param->fp_check_hash_type)
4444 if (param->fp_type != 0 && checked_type == 0)
4447 if (decision == 0) {
4448 if (dir && (param->fp_check_mdt_count ||
4449 param->fp_check_hash_type || param->fp_check_foreign)) {
4450 param->fp_get_lmv = 1;
4451 ret = cb_get_dirstripe(path, dir, param);
4453 /* XXX this works to decide for foreign
4456 if (errno == ENODATA &&
4457 param->fp_check_foreign) {
4458 if (param->fp_exclude_foreign)
4466 param->fp_lmd->lmd_lmm.lmm_magic = 0;
4467 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
4468 param->fp_lum_size, GET_LMD_INFO);
4469 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
4470 find_check_lmm_info(param)) {
4471 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
4473 /* We need to "fake" the "use the default" values
4474 * since the lmm struct is zeroed out at this point. */
4475 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4476 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
4478 ostid_set_seq(&lmm->lmm_oi,
4479 FID_SEQ_LOV_DEFAULT);
4480 lmm->lmm_stripe_size = 0;
4481 lmm->lmm_stripe_count = 0;
4482 lmm->lmm_stripe_offset = -1;
4484 if (ret == 0 && param->fp_mdt_uuid != NULL) {
4486 ret = llapi_file_fget_mdtidx(dirfd(dir),
4487 ¶m->fp_file_mdt_index);
4488 } else if (S_ISREG(st->st_mode)) {
4489 /* FIXME: we could get the MDT index from the
4490 * file's FID in lmd->lmd_lmm.lmm_oi without
4491 * opening the file, once we are sure that
4492 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
4493 * That would still be an ioctl() to map the
4494 * FID to the MDT, but not an open RPC. */
4495 fd = open(path, O_RDONLY);
4497 ret = llapi_file_fget_mdtidx(fd,
4498 ¶m->fp_file_mdt_index);
4503 /* For a special file, we assume it resides on
4504 * the same MDT as the parent directory. */
4505 ret = llapi_file_fget_mdtidx(dirfd(parent),
4506 ¶m->fp_file_mdt_index);
4517 stripe_count = find_get_stripe_count(param);
4521 if (param->fp_type && !checked_type) {
4522 if ((st->st_mode & S_IFMT) == param->fp_type) {
4523 if (param->fp_exclude_type)
4526 if (!param->fp_exclude_type)
4532 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
4533 if (lustre_fs && param->fp_got_uuids &&
4534 param->fp_dev != st->st_dev) {
4535 /* A lustre/lustre mount point is crossed. */
4536 param->fp_got_uuids = 0;
4537 param->fp_obds_printed = 0;
4538 param->fp_mdt_index = OBD_NOT_FOUND;
4539 param->fp_obd_index = OBD_NOT_FOUND;
4542 if (lustre_fs && !param->fp_got_uuids) {
4543 ret = setup_target_indexes(dir ? dir : parent, path,
4548 param->fp_dev = st->st_dev;
4549 } else if (!lustre_fs && param->fp_got_uuids) {
4550 /* A lustre/non-lustre mount point is crossed. */
4551 param->fp_got_uuids = 0;
4552 param->fp_mdt_index = OBD_NOT_FOUND;
4553 param->fp_obd_index = OBD_NOT_FOUND;
4557 if (param->fp_check_foreign) {
4558 decision = find_check_foreign(param);
4563 if (param->fp_check_stripe_size) {
4564 decision = find_check_stripe_size(param);
4569 if (param->fp_check_ext_size) {
4570 decision = find_check_ext_size(param);
4575 if (param->fp_check_stripe_count) {
4576 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
4577 param->fp_stripe_count_sign,
4578 param->fp_exclude_stripe_count, 1, 0);
4583 if (param->fp_check_mdt_count) {
4584 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4589 decision = find_value_cmp(
4590 param->fp_lmv_md->lum_stripe_count,
4591 param->fp_mdt_count,
4592 param->fp_mdt_count_sign,
4593 param->fp_exclude_mdt_count, 1, 0);
4598 if (param->fp_check_layout) {
4599 decision = find_check_layout(param);
4604 if (param->fp_check_hash_type) {
4607 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4612 found = param->fp_lmv_md->lum_hash_type & param->fp_hash_type;
4613 if ((found && param->fp_exclude_hash_type) ||
4614 (!found && !param->fp_exclude_hash_type)) {
4620 /* If an OBD UUID is specified but none matches, skip this file. */
4621 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
4622 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
4625 /* If an OST or MDT UUID is given, and some OST matches,
4627 if (param->fp_obd_index != OBD_NOT_FOUND ||
4628 param->fp_mdt_index != OBD_NOT_FOUND) {
4629 if (param->fp_obd_uuid) {
4630 if (check_obd_match(param)) {
4631 /* If no mdtuuid is given, we are done.
4632 * Otherwise, fall through to the mdtuuid
4634 if (!param->fp_mdt_uuid)
4641 if (param->fp_mdt_uuid) {
4642 if (check_mdt_match(param))
4649 if (param->fp_check_uid) {
4650 if (st->st_uid == param->fp_uid) {
4651 if (param->fp_exclude_uid)
4654 if (!param->fp_exclude_uid)
4659 if (param->fp_check_gid) {
4660 if (st->st_gid == param->fp_gid) {
4661 if (param->fp_exclude_gid)
4664 if (!param->fp_exclude_gid)
4669 if (param->fp_check_projid) {
4673 fd = open(path, O_RDONLY);
4676 ret = fget_projid(fd, &projid);
4681 if (projid == param->fp_projid) {
4682 if (param->fp_exclude_projid)
4685 if (!param->fp_exclude_projid)
4690 if (param->fp_check_pool) {
4691 decision = find_check_pool(param);
4696 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
4697 param->fp_check_comp_start || param->fp_check_comp_end) {
4698 decision = find_check_comp_options(param);
4703 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
4704 decision = find_check_mirror_options(param);
4709 /* Check the time on mds. */
4711 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
4714 for_mds = lustre_fs ?
4715 (S_ISREG(st->st_mode) && stripe_count) : 0;
4716 decision = find_time_check(st, param, for_mds);
4721 /* If file still fits the request, ask ost for updated info.
4722 The regular stat is almost of the same speed as some new
4723 'glimpse-size-ioctl'. */
4725 if ((param->fp_check_size || param->fp_check_blocks) &&
4726 ((S_ISREG(st->st_mode) && stripe_count) || S_ISDIR(st->st_mode)))
4730 /* For regular files with the stripe the decision may have not
4731 * been taken yet if *time or size is to be checked. */
4732 if (param->fp_obd_index != OBD_NOT_FOUND)
4733 print_failed_tgt(param, path, LL_STATFS_LOV);
4735 if (param->fp_mdt_index != OBD_NOT_FOUND)
4736 print_failed_tgt(param, path, LL_STATFS_LMV);
4739 ret = fstat_f(dirfd(dir), st);
4740 else if (de != NULL)
4741 ret = fstatat_f(dirfd(parent), de->d_name, st,
4742 AT_SYMLINK_NOFOLLOW);
4744 ret = lstat_f(path, st);
4747 if (errno == ENOENT) {
4748 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
4749 "warning: %s: %s does not exist",
4754 llapi_error(LLAPI_MSG_ERROR, ret,
4755 "%s: IOC_LOV_GETINFO on %s failed",
4761 /* Check the time on osc. */
4762 decision = find_time_check(st, param, 0);
4767 if (param->fp_check_size) {
4768 decision = find_value_cmp(st->st_size, param->fp_size,
4769 param->fp_size_sign,
4770 param->fp_exclude_size,
4771 param->fp_size_units, 0);
4776 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
4777 decision = find_value_cmp(st->st_blocks * 512, param->fp_blocks,
4778 param->fp_blocks_sign,
4779 param->fp_exclude_blocks,
4780 param->fp_blocks_units, 0);
4786 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
4787 if (param->fp_zero_end)
4788 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
4790 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4794 /* Do not get down anymore? */
4795 if (param->fp_depth == param->fp_max_depth) {
4806 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
4807 void *param_data, struct dirent64 *de)
4809 struct find_param *param = (struct find_param *)param_data;
4810 struct lmv_user_md *lmu = param->fp_lmv_md;
4811 DIR *tmp_parent = parent;
4812 char raw[MAX_IOC_BUFLEN] = {'\0'};
4814 struct obd_ioctl_data data = { 0 };
4821 if (parent == NULL && dirp == NULL)
4830 if (parent == NULL) {
4831 tmp_parent = opendir_parent(path);
4832 if (tmp_parent == NULL) {
4835 llapi_error(LLAPI_MSG_ERROR, ret,
4836 "can not open %s", path);
4841 fd = dirfd(tmp_parent);
4843 path_copy = strdup(path);
4844 filename = basename(path_copy);
4846 data.ioc_inlbuf1 = (char *)filename;
4847 data.ioc_inllen1 = strlen(filename) + 1;
4848 data.ioc_inlbuf2 = (char *)lmu;
4849 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
4851 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
4853 llapi_error(LLAPI_MSG_ERROR, ret,
4854 "llapi_obd_statfs: error packing ioctl data");
4859 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
4861 if (errno == EBUSY && !retry) {
4862 /* because migrate may not be able to lock all involved
4863 * objects in order, for some of them it try lock, while
4864 * there may be conflicting COS locks and cause migrate
4865 * fail with EBUSY, hope a sync() could cause
4866 * transaction commit and release these COS locks. */
4870 } else if (errno == EALREADY) {
4871 if (param->fp_verbose & VERBOSE_DETAIL)
4872 llapi_printf(LLAPI_MSG_NORMAL,
4873 "%s was migrated to MDT%d already\n",
4874 path, lmu->lum_stripe_offset);
4878 llapi_error(LLAPI_MSG_ERROR, ret,
4879 "%s migrate failed: %s (%d)\n",
4880 path, strerror(-ret), ret);
4883 } else if (param->fp_verbose & VERBOSE_DETAIL) {
4884 llapi_printf(LLAPI_MSG_NORMAL,
4885 "migrate %s to MDT%d stripe count %d\n",
4886 path, lmu->lum_stripe_offset,
4887 lmu->lum_stripe_count);
4892 /* If the directory is being migration, we need
4893 * close the directory after migration,
4894 * so the old directory cache will be cleanup
4895 * on the client side, and re-open to get the
4896 * new directory handle */
4897 *dirp = opendir(path);
4898 if (*dirp == NULL) {
4900 llapi_error(LLAPI_MSG_ERROR, ret,
4901 "%s: Failed to open '%s'", __func__, path);
4906 closedir(tmp_parent);
4913 /* dir migration finished, shrink its stripes */
4914 static int cb_migrate_mdt_fini(char *path, DIR *parent, DIR **dirp, void *data,
4915 struct dirent64 *de)
4917 struct find_param *param = data;
4918 struct lmv_user_md *lmu = param->fp_lmv_md;
4919 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
4922 if (de && de->d_type != DT_DIR)
4927 * close it before setxattr because the latter may destroy the
4928 * original object, and cause close fail.
4930 ret = closedir(*dirp);
4936 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
4937 if (ret == -EALREADY)
4940 cb_common_fini(path, parent, dirp, data, de);
4944 int llapi_migrate_mdt(char *path, struct find_param *param)
4946 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
4950 int llapi_mv(char *path, struct find_param *param)
4952 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
4953 static bool printed;
4956 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
4957 "llapi_mv() is deprecated, use llapi_migrate_mdt()\n");
4961 return llapi_migrate_mdt(path, param);
4964 int llapi_find(char *path, struct find_param *param)
4966 return param_callback(path, cb_find_init, cb_common_fini, param);
4970 * Get MDT number that the file/directory inode referenced
4971 * by the open fd resides on.
4972 * Return 0 and mdtidx on success, or -ve errno.
4974 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
4976 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
4981 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
4982 struct dirent64 *de)
4984 struct find_param *param = (struct find_param *)data;
4985 DIR *d = dirp == NULL ? NULL : *dirp;
4989 if (parent == NULL && d == NULL)
4993 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
4994 } else /* if (parent) */ {
4997 fd = open(path, O_RDONLY | O_NOCTTY);
4999 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5007 if (ret == -ENODATA) {
5008 if (!param->fp_obd_uuid)
5009 llapi_printf(LLAPI_MSG_NORMAL,
5010 "'%s' has no stripe info\n", path);
5012 } else if (ret == -ENOENT) {
5013 llapi_error(LLAPI_MSG_WARN, ret,
5014 "warning: %s: '%s' does not exist",
5017 } else if (ret == -ENOTTY) {
5018 llapi_error(LLAPI_MSG_ERROR, ret,
5019 "%s: '%s' not on a Lustre fs",
5022 llapi_error(LLAPI_MSG_ERROR, ret,
5023 "error: %s: '%s' failed get_mdtidx",
5029 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5030 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5032 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5036 /* Do not go down anymore? */
5037 if (param->fp_depth == param->fp_max_depth)
5045 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
5046 struct dirent64 *de)
5048 struct find_param *param = (struct find_param *)data;
5049 DIR *d = dirp == NULL ? NULL : *dirp;
5052 if (parent == NULL && d == NULL)
5055 if (param->fp_obd_uuid) {
5056 param->fp_quiet = 1;
5057 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
5062 if (d && (param->fp_get_lmv || param->fp_get_default_lmv))
5063 ret = cb_get_dirstripe(path, d, param);
5065 (parent && !param->fp_get_lmv && !param->fp_get_default_lmv))
5066 ret = get_lmd_info(path, parent, d, ¶m->fp_lmd->lmd_lmm,
5067 param->fp_lum_size, GET_LMD_STRIPE);
5072 if (errno == ENODATA && d != NULL) {
5073 /* We need to "fake" the "use the default" values
5074 * since the lmm struct is zeroed out at this point.
5075 * The magic needs to be set in order to satisfy
5076 * a check later on in the code path.
5077 * The object_seq needs to be set for the "(Default)"
5078 * prefix to be displayed. */
5079 if (param->fp_get_default_lmv) {
5080 struct lmv_user_md *lum = param->fp_lmv_md;
5082 lum->lum_magic = LMV_USER_MAGIC;
5083 lum->lum_stripe_count = 0;
5084 lum->lum_stripe_offset = -1;
5086 } else if (param->fp_get_lmv) {
5087 struct lmv_user_md *lum = param->fp_lmv_md;
5090 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5093 lum->lum_magic = LMV_MAGIC_V1;
5094 lum->lum_stripe_count = 0;
5095 lum->lum_stripe_offset = mdtidx;
5098 struct lov_user_md *lmm =
5099 ¶m->fp_lmd->lmd_lmm;
5101 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5103 ostid_set_seq(&lmm->lmm_oi,
5104 FID_SEQ_LOV_DEFAULT);
5105 lmm->lmm_stripe_count = 0;
5106 lmm->lmm_stripe_size = 0;
5107 lmm->lmm_stripe_offset = -1;
5110 } else if (errno == ENODATA && parent != NULL) {
5111 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5112 llapi_printf(LLAPI_MSG_NORMAL,
5113 "%s has no stripe info\n", path);
5115 } else if (errno == ENOENT) {
5116 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5117 "warning: %s: %s does not exist",
5120 } else if (errno == ENOTTY) {
5122 llapi_error(LLAPI_MSG_ERROR, ret,
5123 "%s: '%s' not on a Lustre fs?",
5128 llapi_error(LLAPI_MSG_ERROR, ret,
5129 "error: %s: %s failed for %s",
5130 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
5131 "IOC_MDC_GETFILESTRIPE", path);
5138 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5139 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
5142 /* Do not get down anymore? */
5143 if (param->fp_depth == param->fp_max_depth)
5151 int llapi_getstripe(char *path, struct find_param *param)
5153 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5154 cb_get_mdt_index : cb_getstripe,
5155 cb_common_fini, param);
5158 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5159 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5161 char raw[MAX_IOC_BUFLEN] = {'\0'};
5163 struct obd_ioctl_data data = { 0 };
5166 data.ioc_inlbuf1 = (char *)&type;
5167 data.ioc_inllen1 = sizeof(__u32);
5168 data.ioc_inlbuf2 = (char *)&index;
5169 data.ioc_inllen2 = sizeof(__u32);
5170 data.ioc_pbuf1 = (char *)stat_buf;
5171 data.ioc_plen1 = sizeof(struct obd_statfs);
5172 data.ioc_pbuf2 = (char *)uuid_buf;
5173 data.ioc_plen2 = sizeof(struct obd_uuid);
5175 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5177 llapi_error(LLAPI_MSG_ERROR, rc,
5178 "llapi_obd_statfs: error packing ioctl data");
5182 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5184 return rc < 0 ? -errno : 0;
5187 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5188 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5193 fd = open(path, O_RDONLY);
5196 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5198 /* If we can't even open a file on the filesystem (e.g. with
5199 * -ESHUTDOWN), force caller to exit or it will loop forever. */
5203 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5210 #define MAX_STRING_SIZE 128
5212 int llapi_ping(char *obd_type, char *obd_name)
5214 int flags = O_RDONLY;
5215 char buf[1] = { 0 };
5219 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5220 obd_type, obd_name);
5224 fd = open(path.gl_pathv[0], flags);
5226 if (errno == EACCES && flags == O_RDONLY) {
5231 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5236 if (flags == O_RDONLY)
5237 rc = read(fd, buf, sizeof(buf));
5239 rc = write(fd, buf, sizeof(buf));
5247 cfs_free_param_data(&path);
5251 int llapi_target_iterate(int type_num, char **obd_type,
5252 void *args, llapi_cb_t cb)
5258 for (i = 0; i < type_num; i++) {
5261 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5265 for (j = 0; j < param.gl_pathc; j++) {
5266 char obd_uuid[UUID_MAX + 1];
5270 fp = fopen(param.gl_pathv[j], "r");
5273 llapi_error(LLAPI_MSG_ERROR, rc,
5274 "error: opening '%s'",
5279 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5281 llapi_error(LLAPI_MSG_ERROR, rc,
5282 "error: reading '%s'",
5287 /* Extract the obd_name from the sysfs path.
5288 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5290 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5296 /* skip /fs/lustre/'obd_type'/ */
5297 obd_name += strlen(obd_type[i]) + 12;
5298 /* chop off after obd_name */
5299 ptr = strrchr(obd_name, '/');
5303 cb(obd_type[i], obd_name, obd_uuid, args);
5312 cfs_free_param_data(¶m);
5316 static void do_target_check(char *obd_type_name, char *obd_name,
5317 char *obd_uuid, void *args)
5321 rc = llapi_ping(obd_type_name, obd_name);
5322 if (rc == ENOTCONN) {
5323 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5325 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5327 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5331 int llapi_target_check(int type_num, char **obd_type, char *dir)
5333 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5336 #undef MAX_STRING_SIZE
5338 /* Is this a lustre fs? */
5339 int llapi_is_lustre_mnttype(const char *type)
5341 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
5344 /* Is this a lustre client fs? */
5345 int llapi_is_lustre_mnt(struct mntent *mnt)
5347 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5348 strstr(mnt->mnt_fsname, ":/") != NULL);
5351 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5353 char fsname[PATH_MAX + 1];
5357 rc = llapi_search_fsname(mnt, fsname);
5361 root = open(mnt, O_RDONLY | O_DIRECTORY);
5364 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
5368 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
5376 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
5377 * format must have %s%s, buf must be > 16
5378 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
5379 * then buf = "lustre-MDT0000"
5381 static int get_mdtname(char *name, char *format, char *buf)
5383 char suffix[]="-MDT0000";
5384 int len = strlen(name);
5386 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
5387 name[len - 5] = '\0';
5392 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
5395 /* Not enough room to add suffix */
5396 llapi_err_noerrno(LLAPI_MSG_ERROR,
5397 "Invalid MDT name |%s|", name);
5402 return sprintf(buf, format, name, suffix);
5405 /** ioctl on filsystem root, with mdtindex sent as data
5406 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
5407 * \param mdtidxp pointer to integer within data to be filled in with the
5408 * mdt index (0 if no mdt is specified). NULL won't be filled.
5410 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
5418 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
5419 Open root and parse mdt index. */
5420 if (mdtname[0] == '/') {
5422 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
5423 (char *)mdtname, -1);
5425 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
5427 ptr = fsname + strlen(fsname) - 8;
5429 index = strtol(ptr + 4, NULL, 16);
5430 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
5434 llapi_err_noerrno(LLAPI_MSG_ERROR,
5435 "Can't open %s: %d\n", mdtname, rc);
5442 rc = ioctl(fd, opc, data);
5451 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
5452 int buflen, long long *recno, int *linkno)
5454 const char *fidstr_orig = fidstr;
5456 struct getinfo_fid2path *gf;
5461 while (*fidstr == '[')
5464 sscanf(fidstr, SFID, RFID(&fid));
5465 if (!fid_is_sane(&fid)) {
5466 llapi_err_noerrno(LLAPI_MSG_ERROR,
5467 "bad FID format '%s', should be [seq:oid:ver]"
5468 " (e.g. "DFID")\n", fidstr_orig,
5469 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
5473 gf = malloc(sizeof(*gf) + buflen);
5478 gf->gf_recno = *recno;
5479 gf->gf_linkno = *linkno;
5480 gf->gf_pathlen = buflen;
5482 /* Take path or fsname */
5483 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
5488 /* strip out instances of // */
5489 for (a = gf->gf_u.gf_path; *a != '\0'; a++) {
5490 if ((*a == '/') && (*(a + 1) == '/'))
5497 if (buf[0] == '\0') { /* ROOT path */
5502 *recno = gf->gf_recno;
5503 *linkno = gf->gf_linkno;
5510 static int fid_from_lma(const char *path, int fd, struct lu_fid *fid)
5513 struct lustre_mdt_attrs *lma;
5517 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
5519 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
5522 lma = (struct lustre_mdt_attrs *)buf;
5523 fid_le_to_cpu(fid, &lma->lma_self_fid);
5527 int llapi_get_mdt_index_by_fid(int fd, const struct lu_fid *fid,
5532 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
5541 int llapi_fd2fid(int fd, struct lu_fid *fid)
5545 memset(fid, 0, sizeof(*fid));
5547 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
5548 if (rc == -EINVAL || rc == -ENOTTY)
5549 rc = fid_from_lma(NULL, fd, fid);
5554 int llapi_path2fid(const char *path, struct lu_fid *fid)
5558 memset(fid, 0, sizeof(*fid));
5559 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5561 if (errno == ELOOP || errno == ENXIO)
5562 return fid_from_lma(path, -1, fid);
5566 rc = llapi_fd2fid(fd, fid);
5567 if (rc == -EINVAL || rc == -ENOTTY)
5568 rc = fid_from_lma(path, -1, fid);
5574 int llapi_fd2parent(int fd, unsigned int linkno, struct lu_fid *parent_fid,
5575 char *name, size_t name_size)
5577 struct getparent *gp;
5580 gp = malloc(sizeof(*gp) + name_size);
5584 gp->gp_linkno = linkno;
5585 gp->gp_name_size = name_size;
5587 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
5593 *parent_fid = gp->gp_fid;
5595 strncpy(name, gp->gp_name, name_size);
5596 name[name_size - 1] = '\0';
5603 int llapi_path2parent(const char *path, unsigned int linkno,
5604 struct lu_fid *parent_fid, char *name, size_t name_size)
5609 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5613 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
5618 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
5623 root = open(mnt, O_RDONLY | O_DIRECTORY);
5626 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
5630 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
5633 llapi_error(LLAPI_MSG_ERROR, rc,
5634 "ioctl on %s for getting connect flags failed", mnt);
5641 * Get a 64-bit value representing the version of file data pointed by fd.
5643 * Each write or truncate, flushed on OST, will change this value. You can use
5644 * this value to verify if file data was modified. This only checks the file
5645 * data, not metadata.
5647 * \param flags 0: no flush pages, usually used it the process has already
5649 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
5651 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
5652 * pages from clients.
5654 * \retval 0 on success.
5655 * \retval -errno on error.
5657 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
5660 struct ioc_data_version idv;
5662 idv.idv_flags = (__u32)flags;
5664 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5668 *data_version = idv.idv_version;
5674 * Flush cached pages from all clients.
5676 * \param fd File descriptor
5680 int llapi_file_flush(int fd)
5684 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);
5688 * Fetch layout version from OST objects. Layout version on OST objects are
5689 * only set when the file is a mirrored file AND after the file has been
5690 * written at least once.
5692 * It actually fetches the least layout version from the objects.
5694 int llapi_get_ost_layout_version(int fd, __u32 *layout_version)
5697 struct ioc_data_version idv = { 0 };
5699 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5703 *layout_version = idv.idv_layout_version;
5709 * Create a file without any name and open it for read/write
5711 * - file is created as if it were a standard file in the given \a directory
5712 * - file does not appear in \a directory and mtime does not change because
5713 * the filename is handled specially by the Lustre MDS.
5714 * - file is destroyed at final close
5716 * \param[in] directory directory from which to inherit layout/MDT idx
5717 * \param[in] mdt_idx MDT index on which the file is created,
5718 * \a idx == -1 means no specific MDT is requested
5719 * \param[in] mode standard open(2) mode
5720 * \param[in] stripe_param stripe parameters. May be NULL.
5722 * \retval a file descriptor on success.
5723 * \retval -errno on error.
5725 int llapi_create_volatile_param(const char *directory, int mdt_idx,
5726 int open_flags, mode_t mode,
5727 const struct llapi_stripe_param *stripe_param)
5729 char file_path[PATH_MAX];
5730 int saved_errno = errno;
5732 unsigned int rnumber;
5738 rc = snprintf(file_path, sizeof(file_path),
5739 "%s/" LUSTRE_VOLATILE_HDR "::%.4X",
5740 directory, rnumber);
5742 rc = snprintf(file_path, sizeof(file_path),
5743 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X",
5744 directory, mdt_idx, rnumber);
5746 if (rc < 0 || rc >= sizeof(file_path))
5747 return -ENAMETOOLONG;
5750 * Either open O_WRONLY or O_RDWR, creating RDONLY
5751 * is non-sensical here
5753 if ((open_flags & O_ACCMODE) == O_RDONLY)
5754 open_flags = O_RDWR | (open_flags & ~O_ACCMODE);
5756 open_flags |= O_CREAT | O_EXCL | O_NOFOLLOW;
5758 if (stripe_param != NULL) {
5759 fd = llapi_file_open_param(file_path, open_flags,
5760 mode, stripe_param);
5764 fd = open(file_path, open_flags, mode);
5768 } while (fd < 0 && rc == -EEXIST);
5771 llapi_error(LLAPI_MSG_ERROR, rc,
5772 "Cannot create volatile file '%s' in '%s'",
5773 file_path + strlen(directory) + 1 +
5774 LUSTRE_VOLATILE_HDR_LEN,
5780 * Unlink file in case this wasn't a Lustre filesystem and the magic
5781 * volatile filename wasn't handled as intended. The effect is the
5782 * same. If volatile open was supported then we expect unlink() to
5785 (void)unlink(file_path);
5788 * Since we are returning successfully we restore errno (and
5789 * mask out possible EEXIST from open() and ENOENT from unlink().
5791 errno = saved_errno;
5797 * Create a file without any name open it for read/write
5799 * - file is created as if it were a standard file in the given \a directory
5800 * - file does not appear in \a directory and mtime does not change because
5801 * the filename is handled specially by the Lustre MDS.
5802 * - file is removed at final close
5803 * - file modes are rw------- since it doesn't make sense to have a read-only
5804 * or write-only file that cannot be opened again.
5805 * - if user wants another mode it must use fchmod() on the open file, no
5806 * security problems arise because it cannot be opened by another process.
5808 * \param[in] directory directory from which to inherit layout/MDT idx
5809 * \param[in] idx MDT index on which the file is created,
5810 * \a idx == -1 means no specific MDT is requested
5811 * \param[in] open_flags standard open(2) flags
5813 * \retval a file descriptor on success.
5814 * \retval -errno on error.
5816 int llapi_create_volatile_idx(const char *directory, int mdt_idx,
5819 return llapi_create_volatile_param(directory, mdt_idx, open_flags,
5820 S_IRUSR | S_IWUSR, NULL);
5824 * Swap the layouts between 2 file descriptors
5825 * the 2 files must be open for writing
5826 * first fd received the ioctl, second fd is passed as arg
5827 * this is assymetric but avoid use of root path for ioctl
5829 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
5830 int gid, __u64 flags)
5832 struct lustre_swap_layouts lsl;
5837 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5838 rc = fstat(fd1, &st1);
5842 rc = fstat(fd2, &st2);
5847 lsl.sl_flags = flags;
5851 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
5855 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5856 struct timeval tv1[2];
5857 struct timeval tv2[2];
5859 memset(tv1, 0, sizeof(tv1));
5860 memset(tv2, 0, sizeof(tv2));
5862 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
5863 tv1[0].tv_sec = st1.st_atime;
5864 tv2[0].tv_sec = st2.st_atime;
5866 tv1[0].tv_sec = st2.st_atime;
5867 tv2[0].tv_sec = st1.st_atime;
5870 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
5871 tv1[1].tv_sec = st1.st_mtime;
5872 tv2[1].tv_sec = st2.st_mtime;
5874 tv1[1].tv_sec = st2.st_mtime;
5875 tv2[1].tv_sec = st1.st_mtime;
5878 rc = futimes(fd1, tv1);
5882 rc = futimes(fd2, tv2);
5890 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
5897 while (grp_id == 0);
5899 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
5907 * Swap the layouts between 2 files
5908 * the 2 files are open in write
5910 int llapi_swap_layouts(const char *path1, const char *path2,
5911 __u64 dv1, __u64 dv2, __u64 flags)
5915 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
5918 llapi_error(LLAPI_MSG_ERROR, rc,
5919 "error: cannot open '%s' for write", path1);
5923 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
5926 llapi_error(LLAPI_MSG_ERROR, rc,
5927 "error: cannot open '%s' for write", path2);
5931 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
5933 llapi_error(LLAPI_MSG_ERROR, rc,
5934 "error: cannot swap layout between '%s' and '%s'",
5945 * Attempt to open a file with Lustre file identifier \a fid
5946 * and return an open file descriptor.
5948 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
5949 * \param[in] fid Lustre file identifier of file to open
5950 * \param[in] flags open() flags
5952 * \retval non-negative file descriptor on successful open
5953 * \retval -1 if an error occurred
5955 int llapi_open_by_fid(const char *lustre_dir, const struct lu_fid *fid,
5958 char mntdir[PATH_MAX];
5959 char path[PATH_MAX + 64];
5962 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
5966 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
5967 return open(path, flags);
5973 * \param fd File to lock.
5974 * \param gid Group Identifier.
5976 * \retval 0 on success.
5977 * \retval -errno on failure.
5979 int llapi_group_lock(int fd, int gid)
5983 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
5986 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
5994 * \param fd File to unlock.
5995 * \param gid Group Identifier.
5997 * \retval 0 on success.
5998 * \retval -errno on failure.
6000 int llapi_group_unlock(int fd, int gid)
6004 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
6007 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");