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>
62 #include <sys/sysmacros.h>
65 #include <libgen.h> /* for dirname() */
66 #ifdef HAVE_LINUX_UNISTD_H
67 #include <linux/unistd.h>
75 #include <libcfs/util/ioctl.h>
76 #include <libcfs/util/param.h>
77 #include <libcfs/util/string.h>
78 #include <linux/lnet/lnetctl.h>
79 #include <lustre/lustreapi.h>
80 #include <linux/lustre/lustre_ostid.h>
81 #include <linux/lustre/lustre_ioctl.h>
82 #include "lustreapi_internal.h"
84 static int llapi_msg_level = LLAPI_MSG_MAX;
85 const char *liblustreapi_cmd;
87 char *mdt_hash_name[] = { "none",
88 LMV_HASH_NAME_ALL_CHARS,
89 LMV_HASH_NAME_FNV_1A_64,
92 struct lustre_foreign_type lu_foreign_types[] = {
93 {.lft_type = LU_FOREIGN_TYPE_NONE, .lft_name = "none"},
94 {.lft_type = LU_FOREIGN_TYPE_DAOS, .lft_name = "daos"},
95 /* must be the last element */
96 {.lft_type = LU_FOREIGN_TYPE_UNKNOWN, .lft_name = NULL}
97 /* array max dimension must be <= UINT32_MAX */
100 void llapi_msg_set_level(int level)
102 /* ensure level is in the good range */
103 if (level < LLAPI_MSG_OFF)
104 llapi_msg_level = LLAPI_MSG_OFF;
105 else if (level > LLAPI_MSG_MAX)
106 llapi_msg_level = LLAPI_MSG_MAX;
108 llapi_msg_level = level;
111 int llapi_msg_get_level(void)
113 return llapi_msg_level;
116 void llapi_set_command_name(const char *cmd)
118 liblustreapi_cmd = cmd;
121 void llapi_clear_command_name(void)
123 liblustreapi_cmd = NULL;
126 static void error_callback_default(enum llapi_message_level level, int err,
127 const char *fmt, va_list ap)
129 bool has_nl = strchr(fmt, '\n') != NULL;
131 if (liblustreapi_cmd != NULL)
132 fprintf(stderr, "%s %s: ", program_invocation_short_name,
135 fprintf(stderr, "%s: ", program_invocation_short_name);
138 if (level & LLAPI_MSG_NO_ERRNO) {
139 vfprintf(stderr, fmt, ap);
141 fprintf(stderr, "\n");
145 /* Remove trailing linefeed so error string can be appended.
146 * @fmt is a const string, so we can't modify it directly.
148 if (has_nl && (newfmt = strdup(fmt)))
149 *strrchr(newfmt, '\n') = '\0';
151 newfmt = (char *)fmt;
153 vfprintf(stderr, newfmt, ap);
156 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
160 static void info_callback_default(enum llapi_message_level level, int err,
161 const char *fmt, va_list ap)
164 if (liblustreapi_cmd != NULL) {
165 fprintf(stdout, "%s %s: ",
166 program_invocation_short_name,
169 fprintf(stdout, "%s: ", program_invocation_short_name);
172 vfprintf(stdout, fmt, ap);
175 static llapi_log_callback_t llapi_error_callback = error_callback_default;
176 static llapi_log_callback_t llapi_info_callback = info_callback_default;
179 /* llapi_error will preserve errno */
180 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
183 int tmp_errno = errno;
185 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
189 llapi_error_callback(level, abs(err), fmt, args);
194 /* llapi_printf will preserve errno */
195 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
198 int tmp_errno = errno;
200 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
204 llapi_info_callback(level, 0, fmt, args);
210 * Set a custom error logging function. Passing in NULL will reset the logging
211 * callback to its default value.
213 * This function returns the value of the old callback.
215 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
217 llapi_log_callback_t old = llapi_error_callback;
220 llapi_error_callback = cb;
222 llapi_error_callback = error_callback_default;
228 * Set a custom info logging function. Passing in NULL will reset the logging
229 * callback to its default value.
231 * This function returns the value of the old callback.
233 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
235 llapi_log_callback_t old = llapi_info_callback;
238 llapi_info_callback = cb;
240 llapi_info_callback = info_callback_default;
246 * size_units is to be initialized (or zeroed) by caller.
248 int llapi_parse_size(const char *optarg, unsigned long long *size,
249 unsigned long long *size_units, int bytes_spec)
252 char *argbuf = (char *)optarg;
253 unsigned long long frac = 0, frac_d = 1;
255 if (strncmp(optarg, "-", 1) == 0)
258 if (*size_units == 0)
261 *size = strtoull(argbuf, &end, 0);
262 if (end != NULL && *end == '.') {
266 frac = strtoull(argbuf, &end, 10);
267 /* count decimal places */
268 for (i = 0; i < (end - argbuf); i++)
273 if ((*end == 'b') && *(end + 1) == '\0' &&
274 (*size & (~0ULL << (64 - 9))) == 0 &&
276 *size_units = 1 << 9;
277 } else if ((*end == 'b') &&
278 *(end + 1) == '\0' &&
281 } else if ((*end == 'k' || *end == 'K') &&
282 *(end + 1) == '\0' &&
283 (*size & (~0ULL << (64 - 10))) == 0) {
284 *size_units = 1 << 10;
285 } else if ((*end == 'm' || *end == 'M') &&
286 *(end + 1) == '\0' &&
287 (*size & (~0ULL << (64 - 20))) == 0) {
288 *size_units = 1 << 20;
289 } else if ((*end == 'g' || *end == 'G') &&
290 *(end + 1) == '\0' &&
291 (*size & (~0ULL << (64 - 30))) == 0) {
292 *size_units = 1 << 30;
293 } else if ((*end == 't' || *end == 'T') &&
294 *(end + 1) == '\0' &&
295 (*size & (~0ULL << (64 - 40))) == 0) {
296 *size_units = 1ULL << 40;
297 } else if ((*end == 'p' || *end == 'P') &&
298 *(end + 1) == '\0' &&
299 (*size & (~0ULL << (64 - 50))) == 0) {
300 *size_units = 1ULL << 50;
301 } else if ((*end == 'e' || *end == 'E') &&
302 *(end + 1) == '\0' &&
303 (*size & (~0ULL << (64 - 60))) == 0) {
304 *size_units = 1ULL << 60;
309 *size = *size * *size_units + frac * *size_units / frac_d;
314 int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
316 struct obd_ioctl_data *overlay;
319 data->ioc_len = obd_ioctl_packlen(data);
320 data->ioc_version = OBD_IOCTL_VERSION;
322 if (*pbuf != NULL && data->ioc_len > max_len) {
323 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
324 "pbuf = %p, ioc_len = %u, max_len = %d",
325 *pbuf, data->ioc_len, max_len);
330 *pbuf = malloc(data->ioc_len);
335 overlay = (struct obd_ioctl_data *)*pbuf;
336 memcpy(*pbuf, data, sizeof(*data));
338 ptr = overlay->ioc_bulk;
339 if (data->ioc_inlbuf1) {
340 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
341 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
344 if (data->ioc_inlbuf2) {
345 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
346 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
349 if (data->ioc_inlbuf3) {
350 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
351 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
354 if (data->ioc_inlbuf4) {
355 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
356 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
362 int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
364 struct obd_ioctl_data *overlay;
370 overlay = (struct obd_ioctl_data *)pbuf;
372 /* Preserve the caller's buffer pointers */
373 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
374 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
375 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
376 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
378 memcpy(data, pbuf, sizeof(*data));
380 ptr = overlay->ioc_bulk;
381 if (data->ioc_inlbuf1) {
382 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
383 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
386 if (data->ioc_inlbuf2) {
387 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
388 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
391 if (data->ioc_inlbuf3) {
392 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
393 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
396 if (data->ioc_inlbuf4) {
397 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
398 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
404 /* XXX: llapi_xxx() functions return negative values upon failure */
406 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
407 int stripe_count, int stripe_pattern)
409 static int page_size;
412 if (page_size == 0) {
413 /* 64 KB is the largest common page size (on ia64/PPC/ARM),
414 * but check the local page size just in case. The page_size
415 * will not change for the lifetime of this process at least.
417 page_size = LOV_MIN_STRIPE_SIZE;
418 if (getpagesize() > page_size) {
419 page_size = getpagesize();
420 llapi_err_noerrno(LLAPI_MSG_WARN,
421 "warning: page size (%u) larger than expected (%u)",
422 page_size, LOV_MIN_STRIPE_SIZE);
425 if (!llapi_stripe_size_is_aligned(stripe_size)) {
427 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
428 "must be an even multiple of %d bytes",
429 (unsigned long long)stripe_size, page_size);
432 if (!llapi_stripe_index_is_valid(stripe_offset)) {
434 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
438 if (!llapi_stripe_count_is_valid(stripe_count)) {
440 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
444 if (llapi_stripe_size_is_too_big(stripe_size)) {
446 llapi_error(LLAPI_MSG_ERROR, rc,
447 "error: stripe size '%llu' over 4GB limit",
448 (unsigned long long)stripe_size);
457 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
462 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
464 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
468 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
470 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
475 if (!llapi_dir_hash_type_is_valid(hash_type)) {
477 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
485 * Trim a trailing newline from a string, if it exists.
487 int llapi_chomp_string(char *buf)
503 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
505 static int get_param_lov(const char *path, const char *param,
506 char *buf, size_t buf_size)
508 struct obd_uuid uuid;
511 rc = llapi_file_get_lov_uuid(path, &uuid);
515 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
520 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
522 static int get_param_lmv(const char *path, const char *param,
523 char *buf, size_t buf_size)
525 struct obd_uuid uuid;
528 rc = llapi_file_get_lmv_uuid(path, &uuid);
532 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
536 static int get_mds_md_size(const char *path)
538 char buf[PATH_MAX], inst[PATH_MAX];
539 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
542 rc = llapi_getname(path, inst, sizeof(inst));
546 /* Get the max ea size from llite parameters. */
547 rc = get_lustre_param_value("llite", inst, FILTER_BY_EXACT,
548 "max_easize", buf, sizeof(buf));
554 return rc > 0 ? rc : md_size;
557 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
559 return get_param_lmv(path, "uuid", buf, bufsize);
563 * Open a Lustre file.
565 * \param name the name of the file to be opened
566 * \param flags access mode, see flags in open(2)
567 * \param mode permission of the file if it is created, see mode in open(2)
568 * \param param stripe pattern of the newly created file
570 * \retval file descriptor of opened file
571 * \retval negative errno on failure
573 int llapi_file_open_param(const char *name, int flags, mode_t mode,
574 const struct llapi_stripe_param *param)
576 char fsname[MAX_OBD_NAME + 1] = { 0 };
577 char *pool_name = param->lsp_pool;
578 struct lov_user_md *lum = NULL;
579 size_t lum_size = sizeof(*lum);
582 /* Make sure we are on a Lustre file system */
583 rc = llapi_search_fsname(name, fsname);
585 llapi_error(LLAPI_MSG_ERROR, rc,
586 "'%s' is not on a Lustre filesystem",
591 /* Check if the stripe pattern is sane. */
592 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
593 param->lsp_stripe_offset,
594 param->lsp_stripe_count,
595 param->lsp_stripe_pattern);
599 /* Make sure we have a good pool */
600 if (pool_name != NULL) {
601 /* in case user gives the full pool name <fsname>.<poolname>,
602 * strip the fsname */
603 char *ptr = strchr(pool_name, '.');
606 if (strcmp(pool_name, fsname) != 0) {
608 llapi_err_noerrno(LLAPI_MSG_ERROR,
609 "Pool '%s' is not on filesystem '%s'",
616 /* Make sure the pool exists and is non-empty */
617 rc = llapi_search_ost(fsname, pool_name, NULL);
619 char *err = rc == 0 ? "has no OSTs" : "does not exist";
621 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
622 fsname, pool_name, err);
626 lum_size = sizeof(struct lov_user_md_v3);
629 /* sanity check of target list */
630 if (param->lsp_is_specific) {
631 char ostname[MAX_OBD_NAME + 64];
635 for (i = 0; i < param->lsp_stripe_count; i++) {
636 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
637 fsname, param->lsp_osts[i]);
638 rc = llapi_search_ost(fsname, pool_name, ostname);
643 llapi_error(LLAPI_MSG_ERROR, rc,
644 "%s: cannot find OST %s in %s",
651 /* Make sure stripe offset is in OST list. */
652 if (param->lsp_osts[i] == param->lsp_stripe_offset)
656 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
657 "%s: stripe offset '%d' is not in the "
659 __func__, param->lsp_stripe_offset);
663 lum_size = lov_user_md_size(param->lsp_stripe_count,
664 LOV_USER_MAGIC_SPECIFIC);
667 lum = calloc(1, lum_size);
672 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
674 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
675 flags = O_DIRECTORY | O_RDONLY;
682 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
687 /* Initialize IOCTL striping pattern structure */
688 lum->lmm_magic = LOV_USER_MAGIC_V1;
689 lum->lmm_pattern = param->lsp_stripe_pattern;
690 lum->lmm_stripe_size = param->lsp_stripe_size;
691 lum->lmm_stripe_count = param->lsp_stripe_count;
692 lum->lmm_stripe_offset = param->lsp_stripe_offset;
693 if (pool_name != NULL) {
694 struct lov_user_md_v3 *lumv3 = (void *)lum;
696 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
697 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
699 if (param->lsp_is_specific) {
700 struct lov_user_md_v3 *lumv3 = (void *)lum;
703 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
704 if (pool_name == NULL) {
705 /* LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
706 * OST list, therefore if pool is not specified we have
707 * to pack a null pool name for placeholder. */
708 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
711 for (i = 0; i < param->lsp_stripe_count; i++)
712 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
715 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
716 char *errmsg = "stripe already set";
719 if (errno != EEXIST && errno != EALREADY)
720 errmsg = strerror(errno);
722 llapi_err_noerrno(LLAPI_MSG_ERROR,
723 "setstripe error for '%s': %s", name, errmsg);
734 int llapi_file_open_pool(const char *name, int flags, int mode,
735 unsigned long long stripe_size, int stripe_offset,
736 int stripe_count, int stripe_pattern, char *pool_name)
738 const struct llapi_stripe_param param = {
739 .lsp_stripe_size = stripe_size,
740 .lsp_stripe_count = stripe_count,
741 .lsp_stripe_pattern = stripe_pattern,
742 .lsp_stripe_offset = stripe_offset,
743 .lsp_pool = pool_name
745 return llapi_file_open_param(name, flags, mode, ¶m);
748 int llapi_file_open(const char *name, int flags, int mode,
749 unsigned long long stripe_size, int stripe_offset,
750 int stripe_count, int stripe_pattern)
752 return llapi_file_open_pool(name, flags, mode, stripe_size,
753 stripe_offset, stripe_count,
754 stripe_pattern, NULL);
757 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
758 __u32 flags, char *foreign_lov)
761 struct lov_foreign_md *lfm;
764 if (foreign_lov == NULL) {
766 llapi_error(LLAPI_MSG_ERROR, rc,
767 "foreign LOV EA content must be provided");
771 len = strlen(foreign_lov);
772 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
775 llapi_error(LLAPI_MSG_ERROR, rc,
776 "foreign LOV EA size %zu (must be 0 < len < %zu)",
777 len, XATTR_SIZE_MAX -
778 offsetof(struct lov_foreign_md, lfm_value));
782 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
785 llapi_error(LLAPI_MSG_ERROR, rc,
786 "failed to allocate lov_foreign_md");
790 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
793 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
797 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
798 lfm->lfm_length = len;
799 lfm->lfm_type = type;
800 lfm->lfm_flags = flags;
801 memcpy(lfm->lfm_value, foreign_lov, len);
803 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
804 char *errmsg = "stripe already set";
808 errmsg = "not on a Lustre filesystem";
809 else if (errno == EEXIST || errno == EALREADY)
810 errmsg = "stripe already set";
812 errmsg = strerror(errno);
814 llapi_err_noerrno(LLAPI_MSG_ERROR,
815 "setstripe error for '%s': %s", name, errmsg);
831 int llapi_file_create(const char *name, unsigned long long stripe_size,
832 int stripe_offset, int stripe_count, int stripe_pattern)
836 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
837 stripe_offset, stripe_count, stripe_pattern,
846 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
847 int stripe_offset, int stripe_count,
848 int stripe_pattern, char *pool_name)
852 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
853 stripe_offset, stripe_count, stripe_pattern,
862 static int verify_dir_param(const char *name,
863 const struct llapi_stripe_param *param)
865 char fsname[MAX_OBD_NAME + 1] = { 0 };
866 char *pool_name = param->lsp_pool;
869 /* Make sure we are on a Lustre file system */
870 rc = llapi_search_fsname(name, fsname);
872 llapi_error(LLAPI_MSG_ERROR, rc,
873 "'%s' is not on a Lustre filesystem",
878 /* Check if the stripe pattern is sane. */
879 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
880 param->lsp_stripe_count,
881 param->lsp_stripe_pattern);
885 /* Make sure we have a good pool */
886 if (pool_name != NULL) {
887 /* in case user gives the full pool name <fsname>.<poolname>,
888 * strip the fsname */
889 char *ptr = strchr(pool_name, '.');
893 if (strcmp(pool_name, fsname) != 0) {
895 llapi_err_noerrno(LLAPI_MSG_ERROR,
896 "Pool '%s' is not on filesystem '%s'",
903 /* Make sure the pool exists and is non-empty */
904 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
906 char *err = rc == 0 ? "has no OSTs" : "does not exist";
908 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
909 fsname, pool_name, err);
914 /* sanity check of target list */
915 if (param->lsp_is_specific) {
916 char mdtname[MAX_OBD_NAME + 64];
920 for (i = 0; i < param->lsp_stripe_count; i++) {
921 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
922 fsname, param->lsp_tgts[i]);
923 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
928 llapi_error(LLAPI_MSG_ERROR, rc,
929 "%s: cannot find MDT %s in %s",
936 /* Make sure stripe offset is in MDT list. */
937 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
941 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
942 "%s: stripe offset '%d' is not in the "
944 __func__, param->lsp_stripe_offset);
952 static inline void param2lmu(struct lmv_user_md *lmu,
953 const struct llapi_stripe_param *param)
955 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
957 lmu->lum_stripe_count = param->lsp_stripe_count;
958 lmu->lum_stripe_offset = param->lsp_stripe_offset;
959 lmu->lum_hash_type = param->lsp_stripe_pattern;
960 if (param->lsp_pool != NULL)
961 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
962 if (param->lsp_is_specific) {
965 for (i = 0; i < param->lsp_stripe_count; i++)
966 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
970 int llapi_dir_set_default_lmv(const char *name,
971 const struct llapi_stripe_param *param)
973 struct lmv_user_md lmu = { 0 };
977 rc = verify_dir_param(name, param);
981 /* TODO: default lmv doesn't support specific targets yet */
982 if (param->lsp_is_specific)
985 param2lmu(&lmu, param);
987 fd = open(name, O_DIRECTORY | O_RDONLY);
990 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
994 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
996 char *errmsg = "stripe already set";
998 if (errno != EEXIST && errno != EALREADY)
999 errmsg = strerror(errno);
1001 llapi_err_noerrno(LLAPI_MSG_ERROR,
1002 "default dirstripe error on '%s': %s",
1009 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
1010 int stripe_count, int stripe_pattern,
1011 const char *pool_name)
1013 const struct llapi_stripe_param param = {
1014 .lsp_stripe_count = stripe_count,
1015 .lsp_stripe_offset = stripe_offset,
1016 .lsp_stripe_pattern = stripe_pattern,
1017 .lsp_pool = (char *)pool_name
1020 return llapi_dir_set_default_lmv(name, ¶m);
1024 * Create a Lustre directory.
1026 * \param name the name of the directory to be created
1027 * \param mode permission of the file if it is created, see mode in open(2)
1028 * \param param stripe pattern of the newly created directory
1030 * \retval 0 on success
1031 * \retval negative errno on failure
1033 int llapi_dir_create(const char *name, mode_t mode,
1034 const struct llapi_stripe_param *param)
1036 struct lmv_user_md *lmu = NULL;
1038 struct obd_ioctl_data data = { 0 };
1041 char *dirpath = NULL;
1042 char *namepath = NULL;
1047 rc = verify_dir_param(name, param);
1051 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1052 param->lsp_is_specific ?
1053 LMV_USER_MAGIC_SPECIFIC :
1056 lmu = calloc(1, lmu_size);
1060 dirpath = strdup(name);
1066 namepath = strdup(name);
1073 param2lmu(lmu, param);
1075 filename = basename(namepath);
1076 dir = dirname(dirpath);
1078 data.ioc_inlbuf1 = (char *)filename;
1079 data.ioc_inllen1 = strlen(filename) + 1;
1080 data.ioc_inlbuf2 = (char *)lmu;
1081 data.ioc_inllen2 = lmu_size;
1082 data.ioc_type = mode;
1083 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1085 llapi_error(LLAPI_MSG_ERROR, rc,
1086 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1091 fd = open(dir, O_DIRECTORY | O_RDONLY);
1094 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1098 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1099 char *errmsg = "stripe already set";
1102 if (errno != EEXIST && errno != EALREADY)
1103 errmsg = strerror(errno);
1105 llapi_err_noerrno(LLAPI_MSG_ERROR,
1106 "dirstripe error on '%s': %s", name, errmsg);
1117 * Create a foreign directory.
1119 * \param name the name of the directory to be created
1120 * \param mode permission of the file if it is created, see mode in open(2)
1121 * \param type foreign type to be set in LMV EA
1122 * \param flags foreign flags to be set in LMV EA
1123 * \param value foreign pattern to be set in LMV EA
1125 * \retval 0 on success
1126 * \retval negative errno on failure
1128 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1129 __u32 flags, const char *value)
1131 struct lmv_foreign_md *lfm = NULL;
1132 size_t lfm_size, len;
1133 struct obd_ioctl_data data = { 0 };
1136 char *dirpath = NULL;
1137 char *namepath = NULL;
1142 len = strlen(value);
1143 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1146 llapi_error(LLAPI_MSG_ERROR, rc,
1147 "invalid LOV EA length %zu (must be 0 < len < %zu)",
1148 len, XATTR_SIZE_MAX -
1149 offsetof(struct lmv_foreign_md, lfm_value));
1152 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1153 lfm = calloc(1, lfm_size);
1157 dirpath = strdup(name);
1163 namepath = strdup(name);
1170 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1171 lfm->lfm_length = len;
1172 lfm->lfm_type = type;
1173 lfm->lfm_flags = flags;
1174 memcpy(lfm->lfm_value, value, len);
1176 filename = basename(namepath);
1177 dir = dirname(dirpath);
1179 data.ioc_inlbuf1 = (char *)filename;
1180 data.ioc_inllen1 = strlen(filename) + 1;
1181 data.ioc_inlbuf2 = (char *)lfm;
1182 data.ioc_inllen2 = lfm_size;
1183 data.ioc_type = mode;
1184 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1186 llapi_error(LLAPI_MSG_ERROR, rc,
1187 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1192 fd = open(dir, O_DIRECTORY | O_RDONLY);
1195 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1199 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1200 char *errmsg = "stripe already set";
1203 if (errno != EEXIST && errno != EALREADY)
1204 errmsg = strerror(errno);
1206 llapi_err_noerrno(LLAPI_MSG_ERROR,
1207 "dirstripe error on '%s': %s", name, errmsg);
1217 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1218 int stripe_count, int stripe_pattern,
1219 const char *pool_name)
1221 const struct llapi_stripe_param param = {
1222 .lsp_stripe_count = stripe_count,
1223 .lsp_stripe_offset = stripe_offset,
1224 .lsp_stripe_pattern = stripe_pattern,
1225 .lsp_pool = (char *)pool_name
1228 return llapi_dir_create(name, mode, ¶m);
1231 int llapi_direntry_remove(char *dname)
1233 char *dirpath = NULL;
1234 char *namepath = NULL;
1240 dirpath = strdup(dname);
1241 namepath = strdup(dname);
1242 if (!dirpath || !namepath)
1245 filename = basename(namepath);
1247 dir = dirname(dirpath);
1249 fd = open(dir, O_DIRECTORY | O_RDONLY);
1252 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
1257 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename))
1258 llapi_error(LLAPI_MSG_ERROR, errno,
1259 "error on ioctl %#lx for '%s' (%d)",
1260 (long)LL_IOC_LMV_SETSTRIPE, filename, fd);
1270 * Find the fsname, the full path, and/or an open fd.
1271 * Either the fsname or path must not be NULL
1273 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1276 char buf[PATH_MAX], mntdir[PATH_MAX];
1277 char *ptr, *ptr_end;
1279 int idx = 0, len = 0, mntlen, fd;
1281 int fsnamelen, mountlen;
1283 /* get the mount point */
1284 fp = setmntent(PROC_MOUNTS, "r");
1287 llapi_error(LLAPI_MSG_ERROR, rc,
1288 "cannot retrieve filesystem mount point");
1292 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1295 if (!llapi_is_lustre_mnt(&mnt))
1298 if ((want & WANT_INDEX) && (idx++ != index))
1301 mntlen = strlen(mnt.mnt_dir);
1302 ptr = strchr(mnt.mnt_fsname, '/');
1303 while (ptr && *ptr == '/')
1305 /* thanks to the call to llapi_is_lustre_mnt() above,
1306 * we are sure that mnt.mnt_fsname contains ":/",
1307 * so ptr should never be NULL */
1311 while (*ptr_end != '/' && *ptr_end != '\0')
1314 /* Check the fsname for a match, if given */
1315 mountlen = ptr_end - ptr;
1316 if (!(want & WANT_FSNAME) && fsname != NULL &&
1317 (fsnamelen = strlen(fsname)) > 0 &&
1318 (fsnamelen != mountlen ||
1319 (strncmp(ptr, fsname, mountlen) != 0)))
1322 /* If the path isn't set return the first one we find */
1323 if (path == NULL || strlen(path) == 0) {
1324 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1325 mntdir[sizeof(mntdir) - 1] = '\0';
1326 if ((want & WANT_FSNAME) && fsname != NULL) {
1327 strncpy(fsname, ptr, mountlen);
1328 fsname[mountlen] = '\0';
1332 /* Otherwise find the longest matching path */
1333 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1334 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1335 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1336 mntdir[sizeof(mntdir) - 1] = '\0';
1338 if ((want & WANT_FSNAME) && fsname != NULL) {
1339 strncpy(fsname, ptr, mountlen);
1340 fsname[mountlen] = '\0';
1349 if ((want & WANT_PATH) && path != NULL) {
1350 strncpy(path, mntdir, PATH_MAX);
1351 path[strlen(mntdir)] = '\0';
1353 if (want & WANT_FD) {
1354 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1357 llapi_error(LLAPI_MSG_ERROR, rc,
1358 "cannot open '%s'", mntdir);
1364 } else if (want & WANT_ERROR)
1365 llapi_err_noerrno(LLAPI_MSG_ERROR,
1366 "'%s' not on a mounted Lustre filesystem",
1367 (want & WANT_PATH) ? fsname : path);
1372 * search lustre mounts
1374 * Calling this function will return to the user the mount point, mntdir, and
1375 * the file system name, fsname, if the user passed a buffer to this routine.
1377 * The user inputs are pathname and index. If the pathname is supplied then
1378 * the value of the index will be ignored. The pathname will return data if
1379 * the pathname is located on a lustre mount. Index is used to pick which
1380 * mount point you want in the case of multiple mounted lustre file systems.
1381 * See function lfs_osts in lfs.c for an example of the index use.
1383 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1386 int want = WANT_PATH, idx = -1;
1388 if (!pathname || pathname[0] == '\0') {
1392 strcpy(mntdir, pathname);
1395 want |= WANT_FSNAME;
1396 return get_root_path(want, fsname, NULL, mntdir, idx);
1399 /* Given a path, find the corresponding Lustre fsname */
1400 int llapi_search_fsname(const char *pathname, char *fsname)
1405 path = realpath(pathname, NULL);
1407 char tmp[PATH_MAX - 1];
1413 if (pathname[0] != '/') {
1414 /* Need an absolute path, but realpath() only works for
1415 * pathnames that actually exist. We go through the
1416 * extra hurdle of dirname(getcwd() + pathname) in
1417 * case the relative pathname contains ".." in it. */
1418 char realpath[PATH_MAX - 1];
1420 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1422 llapi_error(LLAPI_MSG_ERROR, rc,
1423 "cannot get current working directory");
1427 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1428 if (rc >= sizeof(tmp)) {
1430 llapi_error(LLAPI_MSG_ERROR, rc,
1431 "invalid parent path '%s'",
1437 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1438 if (rc >= sizeof(buf)) {
1440 llapi_error(LLAPI_MSG_ERROR, rc,
1441 "invalid path '%s'", pathname);
1444 path = realpath(buf, NULL);
1446 ptr = strrchr(buf, '/');
1448 llapi_error(LLAPI_MSG_ERROR |
1449 LLAPI_MSG_NO_ERRNO, 0,
1450 "cannot resolve path '%s'",
1455 path = realpath(buf, NULL);
1458 llapi_error(LLAPI_MSG_ERROR, rc,
1459 "cannot resolve path '%s'",
1465 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1470 int llapi_search_rootpath(char *pathname, const char *fsname)
1472 /* pathname can be used as an argument by get_root_path(),
1473 * clear it for safety */
1475 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1478 int llapi_getname(const char *path, char *buf, size_t size)
1480 struct obd_uuid uuid_buf;
1481 char *uuid = uuid_buf.uuid;
1483 int rc, len, fsname_len;
1485 memset(&uuid_buf, 0, sizeof(uuid_buf));
1486 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1491 * We want to turn testfs-clilov-ffff88002738bc00 into
1492 * testfs-ffff88002738bc00 in a portable way that doesn't depend
1493 * on what is after "-clilov-" as it may change in the future.
1494 * Unfortunately, the "fsname" part may contain a dash, so we
1495 * can't just skip to the first dash, and the "instance" may be a
1496 * UUID in the future, so we can't necessarily go to the last dash.
1498 cfg_instance = strstr(uuid, "-clilov-");
1502 fsname_len = cfg_instance - uuid;
1503 cfg_instance += strlen("-clilov-");
1504 len = snprintf(buf, size, "%.*s-%s", fsname_len, uuid, cfg_instance);
1513 * Get the list of pool members.
1514 * \param poolname string of format \<fsname\>.\<poolname\>
1515 * \param members caller-allocated array of char*
1516 * \param list_size size of the members array
1517 * \param buffer caller-allocated buffer for storing OST names
1518 * \param buffer_size size of the buffer
1520 * \return number of members retrieved for this pool
1521 * \retval -error failure
1523 int llapi_get_poolmembers(const char *poolname, char **members,
1524 int list_size, char *buffer, int buffer_size)
1526 char fsname[PATH_MAX];
1535 /* name is FSNAME.POOLNAME */
1536 if (strlen(poolname) >= sizeof(fsname))
1539 snprintf(fsname, sizeof(fsname), "%s", poolname);
1540 pool = strchr(fsname, '.');
1547 rc = poolpath(&pathname, fsname, NULL);
1549 llapi_error(LLAPI_MSG_ERROR, rc,
1550 "Lustre filesystem '%s' not found",
1555 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1556 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1557 cfs_free_param_data(&pathname);
1558 if (rc >= sizeof(buf))
1560 fd = fopen(buf, "r");
1563 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1568 while (fgets(buf, sizeof(buf), fd) != NULL) {
1569 if (nb_entries >= list_size) {
1573 buf[sizeof(buf) - 1] = '\0';
1575 tmp = strchr(buf, '\n');
1578 if (used + strlen(buf) + 1 > buffer_size) {
1583 strcpy(buffer + used, buf);
1584 members[nb_entries] = buffer + used;
1585 used += strlen(buf) + 1;
1595 * Get the list of pools in a filesystem.
1596 * \param name filesystem name or path
1597 * \param poollist caller-allocated array of char*
1598 * \param list_size size of the poollist array
1599 * \param buffer caller-allocated buffer for storing pool names
1600 * \param buffer_size size of the buffer
1602 * \return number of pools retrieved for this filesystem
1603 * \retval -error failure
1605 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1606 char *buffer, int buffer_size)
1612 struct dirent *pool;
1614 unsigned int nb_entries = 0;
1615 unsigned int used = 0;
1618 /* initialize output array */
1619 for (i = 0; i < list_size; i++)
1622 /* is name a pathname ? */
1623 ptr = strchr(name, '/');
1625 char fsname_buf[MAXNAMLEN];
1627 /* We will need fsname for printing later */
1628 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1632 ptr = strrchr(fsname_buf, '-');
1636 fsname = strdup(fsname_buf);
1640 /* name is FSNAME */
1641 fsname = strdup(name);
1646 rc = poolpath(&pathname, fsname, NULL);
1648 llapi_error(LLAPI_MSG_ERROR, rc,
1649 "Lustre filesystem '%s' not found", name);
1653 dir = opendir(pathname.gl_pathv[0]);
1656 llapi_error(LLAPI_MSG_ERROR, rc,
1657 "Could not open pool list for '%s'",
1664 pool = readdir(dir);
1670 /* ignore . and .. */
1671 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1674 /* check output bounds */
1675 if (nb_entries >= list_size) {
1677 goto free_dir_no_msg;
1680 /* +2 for '.' and final '\0' */
1681 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1684 goto free_dir_no_msg;
1687 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1688 poollist[nb_entries] = buffer + used;
1689 used += strlen(pool->d_name) + strlen(fsname) + 2;
1695 llapi_error(LLAPI_MSG_ERROR, rc,
1696 "Error reading pool list for '%s'", name);
1698 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1703 cfs_free_param_data(&pathname);
1706 return rc != 0 ? rc : nb_entries;
1709 /* wrapper for lfs.c and obd.c */
1710 int llapi_poollist(const char *name)
1712 /* list of pool names (assume that pool count is smaller
1714 char **list, *buffer = NULL, *fsname = (char *)name;
1715 char *poolname = NULL, *tmp = NULL, data[16];
1716 enum param_filter type = FILTER_BY_PATH;
1717 int obdcount, bufsize, rc, nb, i;
1722 if (name[0] != '/') {
1723 fsname = strdup(name);
1727 poolname = strchr(fsname, '.');
1730 type = FILTER_BY_FS_NAME;
1733 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1734 data, sizeof(data));
1737 obdcount = atoi(data);
1739 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1740 * and also an array to store the pointers for all that
1741 * allocated space. */
1743 bufsize = sizeof(struct obd_uuid) * obdcount;
1744 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1745 if (buffer == NULL) {
1749 list = (char **) (buffer + bufsize);
1752 /* name is a path or fsname */
1753 nb = llapi_get_poollist(name, list, obdcount,
1756 /* name is a pool name (<fsname>.<poolname>) */
1757 nb = llapi_get_poolmembers(name, list, obdcount,
1761 if (nb == -EOVERFLOW) {
1764 goto retry_get_pools;
1767 for (i = 0; i < nb; i++)
1768 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1769 rc = (nb < 0 ? nb : 0);
1773 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1778 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1779 void *data, struct dirent64 *de);
1781 #define OBD_NOT_FOUND (-1)
1783 static void find_param_fini(struct find_param *param)
1785 if (param->fp_migrate)
1788 if (param->fp_obd_indexes) {
1789 free(param->fp_obd_indexes);
1790 param->fp_obd_indexes = NULL;
1793 if (param->fp_lmd) {
1794 free(param->fp_lmd);
1795 param->fp_lmd = NULL;
1798 if (param->fp_lmv_md) {
1799 free(param->fp_lmv_md);
1800 param->fp_lmv_md = NULL;
1804 static int common_param_init(struct find_param *param, char *path)
1806 int lum_size = get_mds_md_size(path);
1811 /* migrate has fp_lmv_md initialized outside */
1812 if (param->fp_migrate)
1815 if (lum_size < PATH_MAX + 1)
1816 lum_size = PATH_MAX + 1;
1818 param->fp_lum_size = lum_size;
1819 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1821 if (param->fp_lmd == NULL) {
1822 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1823 "error: allocate %zu bytes for layout failed",
1824 sizeof(lstat_t) + param->fp_lum_size);
1828 param->fp_lmv_stripe_count = 256;
1829 param->fp_lmv_md = calloc(1,
1830 lmv_user_md_size(param->fp_lmv_stripe_count,
1831 LMV_USER_MAGIC_SPECIFIC));
1832 if (param->fp_lmv_md == NULL) {
1833 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1834 "error: allocation of %d bytes for ioctl",
1835 lmv_user_md_size(param->fp_lmv_stripe_count,
1836 LMV_USER_MAGIC_SPECIFIC));
1837 find_param_fini(param);
1841 param->fp_got_uuids = 0;
1842 param->fp_obd_indexes = NULL;
1843 param->fp_obd_index = OBD_NOT_FOUND;
1844 param->fp_mdt_index = OBD_NOT_FOUND;
1848 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1849 struct dirent64 *de)
1851 struct find_param *param = data;
1857 /* set errno upon failure */
1858 static DIR *opendir_parent(const char *path)
1864 path_copy = strdup(path);
1865 if (path_copy == NULL)
1868 parent_path = dirname(path_copy);
1869 parent = opendir(parent_path);
1875 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1880 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1881 if (param->fp_get_default_lmv)
1882 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1884 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1886 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1887 if (errno == E2BIG && ret != 0) {
1891 /* if foreign LMV case, fake stripes number */
1892 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
1893 struct lmv_foreign_md *lfm;
1895 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1896 if (lfm->lfm_length < XATTR_SIZE_MAX -
1897 offsetof(typeof(*lfm), lfm_value)) {
1898 uint32_t size = lfm->lfm_length +
1899 offsetof(typeof(*lfm), lfm_value);
1901 stripe_count = lmv_foreign_to_md_stripes(size);
1903 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1904 "error: invalid %d foreign size "
1905 "returned from ioctl",
1910 stripe_count = param->fp_lmv_md->lum_stripe_count;
1912 if (stripe_count <= param->fp_lmv_stripe_count)
1915 free(param->fp_lmv_md);
1916 param->fp_lmv_stripe_count = stripe_count;
1917 lmv_size = lmv_user_md_size(stripe_count,
1918 LMV_USER_MAGIC_SPECIFIC);
1919 param->fp_lmv_md = malloc(lmv_size);
1920 if (param->fp_lmv_md == NULL) {
1921 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1922 "error: allocation of %d bytes for ioctl",
1923 lmv_user_md_size(param->fp_lmv_stripe_count,
1924 LMV_USER_MAGIC_SPECIFIC));
1932 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1935 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
1936 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
1937 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
1938 lmd_v2->lmd_stx.stx_uid = st->st_uid;
1939 lmd_v2->lmd_stx.stx_gid = st->st_gid;
1940 lmd_v2->lmd_stx.stx_mode = st->st_mode;
1941 lmd_v2->lmd_stx.stx_ino = st->st_ino;
1942 lmd_v2->lmd_stx.stx_size = st->st_size;
1943 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
1944 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
1945 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
1946 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
1947 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
1948 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
1949 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
1950 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
1951 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
1953 lmd_v2->lmd_flags = 0;
1955 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1957 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
1958 if (lmd_v2->lmd_stx.stx_size)
1959 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
1960 if (lmd_v2->lmd_stx.stx_blocks)
1961 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
1963 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1964 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
1965 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
1970 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
1972 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
1973 struct lov_user_mds_data *lmd_v2 = lmdbuf;
1977 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
1981 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
1984 st = lmd_v1->lmd_st;
1985 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
1986 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
1987 convert_lmd_statx(lmd_v2, &st, false);
1988 lmd_v2->lmd_lmmsize = 0;
1989 lmd_v2->lmd_padding = 0;
1994 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
1995 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1997 struct lov_user_mds_data *lmd = lmdbuf;
1998 static bool use_old_ioctl;
2002 if (parent_fd < 0 && dir_fd < 0)
2004 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
2008 /* LL_IOC_MDC_GETINFO operates on the current directory inode
2009 * and returns struct lov_user_mds_data, while
2010 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
2012 if (type == GET_LMD_INFO)
2013 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_OLD :
2016 cmd = LL_IOC_LOV_GETSTRIPE;
2019 ret = ioctl(dir_fd, cmd, lmdbuf);
2020 if (ret < 0 && errno == ENOTTY && cmd == LL_IOC_MDC_GETINFO) {
2021 cmd = LL_IOC_MDC_GETINFO_OLD;
2022 use_old_ioctl = true;
2026 if (cmd == LL_IOC_MDC_GETINFO_OLD && !ret)
2027 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2028 } else if (parent_fd >= 0) {
2029 const char *fname = strrchr(path, '/');
2031 /* IOC_MDC_GETFILEINFO takes as input the filename (relative to
2032 * the parent directory) and returns struct lov_user_mds_data,
2033 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2035 * This avoids opening, locking, and closing each file on the
2036 * client if that is not needed. Multiple of these ioctl() can
2037 * be done on the parent dir with a single open for all
2038 * files in that directory, and it also doesn't pollute the
2039 * client dcache with millions of dentries when traversing
2040 * a large filesystem.
2042 fname = (fname == NULL ? path : fname + 1);
2044 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2047 else if (ret >= lmdlen || ret++ == 0)
2050 if (type == GET_LMD_INFO)
2051 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_OLD :
2052 IOC_MDC_GETFILEINFO;
2054 cmd = IOC_MDC_GETFILESTRIPE;
2057 ret = ioctl(parent_fd, cmd, lmdbuf);
2058 if (ret < 0 && errno == ENOTTY &&
2059 cmd == IOC_MDC_GETFILEINFO) {
2060 cmd = IOC_MDC_GETFILEINFO_OLD;
2061 use_old_ioctl = true;
2062 goto retry_getfileinfo;
2065 if (cmd == IOC_MDC_GETFILEINFO_OLD && !ret)
2066 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2070 if (ret && type == GET_LMD_INFO) {
2071 if (errno == ENOTTY) {
2074 /* ioctl is not supported, it is not a lustre fs.
2075 * Do the regular lstat(2) instead.
2077 ret = lstat_f(path, &st);
2080 llapi_error(LLAPI_MSG_ERROR, ret,
2081 "error: %s: lstat failed for %s",
2085 convert_lmd_statx(lmd, &st, true);
2086 /* It may be wrong to set use_old_ioctl with true as
2087 * the file is not a lustre fs. So reset it with false
2090 use_old_ioctl = false;
2091 } else if (errno == ENOENT) {
2093 llapi_error(LLAPI_MSG_WARN, ret,
2094 "warning: %s does not exist", path);
2095 } else if (errno != EISDIR && errno != ENODATA) {
2097 llapi_error(LLAPI_MSG_ERROR, ret,
2098 "%s ioctl failed for %s.",
2099 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2100 "IOC_MDC_GETFILEINFO", path);
2107 static int get_lmd_info(char *path, DIR *parent, DIR *dir, void *lmdbuf,
2108 int lmdlen, enum get_lmd_info_type type)
2114 parent_fd = dirfd(parent);
2116 dir_fd = dirfd(dir);
2118 return get_lmd_info_fd(path, parent_fd, dir_fd, lmdbuf, lmdlen, type);
2121 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
2122 semantic_func_t sem_init,
2123 semantic_func_t sem_fini, void *data,
2124 struct dirent64 *de)
2126 struct find_param *param = (struct find_param *)data;
2127 struct dirent64 *dent;
2135 if (!d && errno != ENOTDIR) {
2137 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2140 } else if (!d && !parent) {
2141 /* ENOTDIR. Open the parent dir. */
2142 p = opendir_parent(path);
2149 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
2155 while ((dent = readdir64(d)) != NULL) {
2158 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2162 if ((len + dent->d_reclen + 2) > size) {
2163 llapi_err_noerrno(LLAPI_MSG_ERROR,
2164 "error: %s: string buffer too small",
2169 strcat(path, dent->d_name);
2171 if (dent->d_type == DT_UNKNOWN) {
2172 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
2174 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
2175 param->fp_lum_size, GET_LMD_INFO);
2177 dent->d_type = IFTODT(stx->stx_mode);
2184 switch (dent->d_type) {
2186 llapi_err_noerrno(LLAPI_MSG_ERROR,
2187 "error: %s: '%s' is UNKNOWN type %d",
2188 __func__, dent->d_name, dent->d_type);
2191 rc = llapi_semantic_traverse(path, size, d, sem_init,
2192 sem_fini, data, dent);
2193 if (rc != 0 && ret == 0)
2199 rc = sem_init(path, d, NULL, data, dent);
2200 if (rc < 0 && ret == 0) {
2205 if (sem_fini && rc == 0)
2206 sem_fini(path, d, NULL, data, dent);
2214 sem_fini(path, parent, &d, data, de);
2223 static int param_callback(char *path, semantic_func_t sem_init,
2224 semantic_func_t sem_fini, struct find_param *param)
2226 int ret, len = strlen(path);
2229 if (len > PATH_MAX) {
2231 llapi_error(LLAPI_MSG_ERROR, ret,
2232 "Path name '%s' is too long", path);
2236 buf = (char *)malloc(PATH_MAX + 1);
2240 snprintf(buf, PATH_MAX + 1, "%s", path);
2241 ret = common_param_init(param, buf);
2245 param->fp_depth = 0;
2247 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
2248 sem_fini, param, NULL);
2250 find_param_fini(param);
2252 return ret < 0 ? ret : 0;
2255 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2257 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
2261 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2266 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2268 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2271 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2276 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2280 fd = open(path, O_RDONLY | O_NONBLOCK);
2283 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
2287 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2293 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2297 fd = open(path, O_RDONLY | O_NONBLOCK);
2300 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2304 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2316 * If uuidp is NULL, return the number of available obd uuids.
2317 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2318 * there are more OSTs than allocated to uuidp, then an error is returned with
2319 * the ost_count set to number of available obd uuids.
2321 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2322 int *ost_count, enum tgt_type type)
2324 char buf[PATH_MAX], format[32];
2325 int rc = 0, index = 0;
2326 struct obd_uuid name;
2330 /* Get the lov name */
2331 if (type == LOV_TYPE)
2332 rc = llapi_file_fget_lov_uuid(fd, &name);
2334 rc = llapi_file_fget_lmv_uuid(fd, &name);
2338 /* Now get the ost uuids */
2339 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2340 FILTER_BY_EXACT, "target_obd", ¶m);
2344 fp = fopen(param.gl_pathv[0], "r");
2347 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2352 snprintf(format, sizeof(format),
2353 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2354 while (fgets(buf, sizeof(buf), fp) != NULL) {
2355 if (uuidp && (index < *ost_count)) {
2356 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2364 if (uuidp && (index > *ost_count))
2369 cfs_free_param_data(¶m);
2373 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2375 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2378 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2383 root = open(mnt, O_RDONLY | O_DIRECTORY);
2386 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2391 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2399 /* Check if user specified value matches a real uuid. Ignore _UUID,
2400 * -osc-4ba41334, other trailing gunk in comparison.
2401 * @param real_uuid ends in "_UUID"
2402 * @param search_uuid may or may not end in "_UUID"
2404 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2406 int cmplen = strlen(real_uuid);
2407 int searchlen = strlen(search_uuid);
2409 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2411 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2414 /* The UUIDs may legitimately be different lengths, if
2415 * the system was upgraded from an older version. */
2416 if (cmplen != searchlen)
2419 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2422 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2423 * returned in param->fp_obd_index */
2424 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2426 struct obd_uuid obd_uuid;
2433 if (param->fp_got_uuids)
2436 /* Get the lov/lmv name */
2437 if (param->fp_get_lmv)
2438 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2440 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2442 if (rc != -ENOTTY) {
2443 llapi_error(LLAPI_MSG_ERROR, rc,
2444 "error: can't get %s name: %s",
2445 param->fp_get_lmv ? "lmv" : "lov",
2453 param->fp_got_uuids = 1;
2455 /* Now get the ost uuids */
2456 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2457 obd_uuid.uuid, FILTER_BY_EXACT,
2458 "target_obd", ¶m_data);
2462 fp = fopen(param_data.gl_pathv[0], "r");
2465 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2466 param_data.gl_pathv[0]);
2470 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2471 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2472 param->fp_get_lmv ? "MDTS" : "OBDS");
2474 snprintf(format, sizeof(format),
2475 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2476 while (fgets(buf, sizeof(buf), fp) != NULL) {
2479 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2482 if (param->fp_obd_uuid) {
2483 if (llapi_uuid_match(obd_uuid.uuid,
2484 param->fp_obd_uuid->uuid)) {
2485 param->fp_obd_index = index;
2488 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2489 /* Print everything */
2490 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2493 param->fp_obds_printed = 1;
2497 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2498 llapi_err_noerrno(LLAPI_MSG_ERROR,
2499 "error: %s: unknown obduuid: %s",
2500 __func__, param->fp_obd_uuid->uuid);
2504 cfs_free_param_data(¶m_data);
2508 /* In this case, param->fp_obd_uuid will be an array of obduuids and
2509 * obd index for all these obduuids will be returned in
2510 * param->fp_obd_indexes */
2511 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2512 int num_obds, int **obdindexes, int *obdindex,
2515 int ret, obdcount, obd_valid = 0, obdnum;
2517 struct obd_uuid *uuids = NULL;
2521 if (type == LOV_TYPE)
2522 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2524 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2528 obdcount = atoi(buf);
2529 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2534 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2536 if (ret == -EOVERFLOW) {
2537 struct obd_uuid *uuids_temp;
2539 uuids_temp = realloc(uuids, obdcount *
2540 sizeof(struct obd_uuid));
2541 if (uuids_temp != NULL) {
2543 goto retry_get_uuids;
2548 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2552 indexes = malloc(num_obds * sizeof(*obdindex));
2553 if (indexes == NULL) {
2558 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2561 /* The user may have specified a simple index */
2562 i = strtol(obduuids[obdnum].uuid, &end, 0);
2563 if (end && *end == '\0' && i < obdcount) {
2564 indexes[obdnum] = i;
2567 for (i = 0; i < obdcount; i++) {
2568 if (llapi_uuid_match(uuids[i].uuid,
2569 obduuids[obdnum].uuid)) {
2570 indexes[obdnum] = i;
2576 if (i >= obdcount) {
2577 indexes[obdnum] = OBD_NOT_FOUND;
2578 llapi_err_noerrno(LLAPI_MSG_ERROR,
2579 "invalid obduuid '%s'",
2580 obduuids[obdnum].uuid);
2586 *obdindex = OBD_NOT_FOUND;
2588 *obdindex = obd_valid;
2590 *obdindexes = indexes;
2598 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2602 if (param->fp_mdt_uuid) {
2603 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2605 ¶m->fp_mdt_indexes,
2606 ¶m->fp_mdt_index, LMV_TYPE);
2611 if (param->fp_obd_uuid) {
2612 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2614 ¶m->fp_obd_indexes,
2615 ¶m->fp_obd_index, LOV_TYPE);
2620 param->fp_got_uuids = 1;
2625 int llapi_ostlist(char *path, struct find_param *param)
2630 fd = open(path, O_RDONLY | O_DIRECTORY);
2634 ret = setup_obd_uuid(fd, path, param);
2641 * Tries to determine the default stripe attributes for a given filesystem. The
2642 * filesystem to check should be specified by fsname, or will be determined
2645 static int sattr_get_defaults(const char *const fsname,
2646 unsigned int *scount,
2647 unsigned int *ssize,
2648 unsigned int *soffset)
2654 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2655 "stripecount", val, sizeof(val));
2658 *scount = atoi(val);
2662 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2663 "stripesize", val, sizeof(val));
2670 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2671 "stripeoffset", val, sizeof(val));
2674 *soffset = atoi(val);
2681 * Tries to gather the default stripe attributes for a given filesystem. If
2682 * the attributes can be determined, they are cached for easy retreival the
2683 * next time they are needed. Only a single filesystem's attributes are
2686 int sattr_cache_get_defaults(const char *const fsname,
2687 const char *const pathname, unsigned int *scount,
2688 unsigned int *ssize, unsigned int *soffset)
2691 char fsname[PATH_MAX + 1];
2692 unsigned int stripecount;
2693 unsigned int stripesize;
2694 unsigned int stripeoffset;
2700 char fsname_buf[PATH_MAX + 1];
2701 unsigned int tmp[3];
2703 if (fsname == NULL) {
2704 rc = llapi_search_fsname(pathname, fsname_buf);
2708 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2711 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2713 * Ensure all 3 sattrs (count, size, and offset) are
2714 * successfully retrieved and stored in tmp before writing to
2717 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2721 cache.stripecount = tmp[0];
2722 cache.stripesize = tmp[1];
2723 cache.stripeoffset = tmp[2];
2724 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2728 *scount = cache.stripecount;
2730 *ssize = cache.stripesize;
2732 *soffset = cache.stripeoffset;
2737 static char *layout2name(__u32 layout_pattern)
2739 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2741 else if (layout_pattern == LOV_PATTERN_MDT)
2743 else if (layout_pattern == LOV_PATTERN_RAID0)
2745 else if (layout_pattern ==
2746 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2747 return "raid0,overstriped";
2752 enum lov_dump_flags {
2753 LDF_IS_DIR = 0x0001,
2754 LDF_IS_RAW = 0x0002,
2755 LDF_INDENT = 0x0004,
2756 LDF_SKIP_OBJS = 0x0008,
2758 LDF_EXTENSION = 0x0020,
2761 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2762 struct lov_user_ost_data_v1 *objects,
2763 enum llapi_layout_verbose verbose,
2764 int depth, char *pool_name,
2765 enum lov_dump_flags flags)
2767 bool is_dir = flags & LDF_IS_DIR;
2768 bool is_raw = flags & LDF_IS_RAW;
2769 bool indent = flags & LDF_INDENT;
2770 bool yaml = flags & LDF_YAML;
2771 bool skip_objs = flags & LDF_SKIP_OBJS;
2772 bool extension = flags & LDF_EXTENSION;
2773 char *prefix = is_dir ? "" : "lmm_";
2774 char *separator = "";
2775 char *space = indent ? " " : "";
2778 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2779 lmm_oi_set_seq(&lum->lmm_oi, 0);
2780 if (!indent && (verbose & VERBOSE_DETAIL))
2781 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2784 if (!yaml && !indent && depth && path &&
2785 ((verbose != VERBOSE_OBJID) || !is_dir))
2786 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2788 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2789 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2790 space, prefix, lum->lmm_magic);
2791 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2793 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2794 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2796 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2798 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2803 if (verbose & ~VERBOSE_DFID)
2804 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2806 /* This needs a bit of hand-holding since old 1.x lmm_oi
2807 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2808 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2809 * a real FID. Ideally the 2.x code would have stored this
2810 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2811 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2812 * worked properly (especially since IGIF FIDs use mds_inum as
2813 * the FID SEQ), but unfortunately that didn't happen.
2815 * Print it to look like an IGIF FID, even though the fields
2816 * are reversed on disk, so that it makes sense to userspace.
2818 * Don't use ostid_id() and ostid_seq(), since they assume the
2819 * oi_fid fields are in the right order. This is why there are
2820 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2822 * For newer layout types hopefully this will be a real FID. */
2823 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2824 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2825 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2826 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2827 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2829 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2830 (unsigned long long)seq, oid, ver);
2832 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2833 (unsigned long long)seq, oid, ver);
2836 if (verbose & VERBOSE_STRIPE_COUNT) {
2837 if (verbose & ~VERBOSE_STRIPE_COUNT)
2838 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2841 if (!is_raw && lum->lmm_stripe_count == 0 &&
2842 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2843 unsigned int scount;
2844 rc = sattr_cache_get_defaults(NULL, path,
2848 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2851 llapi_error(LLAPI_MSG_ERROR, rc,
2852 "Cannot determine default"
2855 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2857 (__s16)lum->lmm_stripe_count);
2860 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2862 (__s16)lum->lmm_stripe_count);
2864 if (!yaml && is_dir)
2870 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
2871 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
2872 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2873 if (verbose & ~VERBOSE_EXT_SIZE && extension)
2874 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
2876 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
2877 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2879 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2881 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2884 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2886 llapi_error(LLAPI_MSG_ERROR, rc,
2887 "Cannot determine default"
2890 /* Extension size is in KiB */
2891 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2893 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
2894 (unsigned long long)lum->lmm_stripe_size);
2896 if (!yaml && is_dir)
2902 if ((verbose & VERBOSE_PATTERN)) {
2903 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2904 if (verbose & ~VERBOSE_PATTERN)
2905 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2907 if (lov_pattern_supported(lum->lmm_pattern))
2908 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2909 layout2name(lum->lmm_pattern));
2911 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2912 separator = (!yaml && is_dir) ? " " : "\n";
2915 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2916 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2917 if (verbose & ~VERBOSE_GENERATION)
2918 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2920 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2921 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2925 if (verbose & VERBOSE_STRIPE_OFFSET) {
2926 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2927 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2928 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2930 if (is_dir || skip_objs)
2931 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2932 lum->lmm_stripe_offset ==
2933 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2934 lum->lmm_stripe_offset);
2935 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
2936 llapi_printf(LLAPI_MSG_NORMAL, "0");
2938 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2939 objects[0].l_ost_idx);
2940 if (!yaml && is_dir)
2946 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2947 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2948 if (verbose & ~VERBOSE_POOL)
2949 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2951 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2952 if (!yaml && is_dir)
2958 if (strlen(separator) != 0)
2959 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2962 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2963 struct lov_user_ost_data_v1 *objects,
2964 char *path, int obdindex, int depth,
2965 enum llapi_layout_verbose verbose,
2966 enum lov_dump_flags flags)
2968 bool is_dir = flags & LDF_IS_DIR;
2969 bool indent = flags & LDF_INDENT;
2970 bool skip_objs = flags & LDF_SKIP_OBJS;
2971 bool yaml = flags & LDF_YAML;
2972 bool obdstripe = obdindex == OBD_NOT_FOUND;
2975 if (!obdstripe && !skip_objs) {
2976 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2977 if (obdindex == objects[i].l_ost_idx) {
2987 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2990 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
2991 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2992 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
2993 char *space = " - ";
2996 llapi_printf(LLAPI_MSG_NORMAL,
2997 "%6slmm_objects:\n", " ");
2999 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
3001 llapi_printf(LLAPI_MSG_NORMAL,
3002 "\tobdidx\t\t objid\t\t objid\t\t group\n");
3004 for (i = 0; i < lum->lmm_stripe_count; i++) {
3005 int idx = objects[i].l_ost_idx;
3006 long long oid = ostid_id(&objects[i].l_ost_oi);
3007 long long gr = ostid_seq(&objects[i].l_ost_oi);
3009 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
3013 struct lu_fid fid = { 0 };
3015 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3016 llapi_printf(LLAPI_MSG_NORMAL,
3017 "%sl_ost_idx: %d\n", space, idx);
3018 llapi_printf(LLAPI_MSG_NORMAL,
3019 "%8sl_fid: "DFID_NOBRACE"\n",
3021 } else if (indent) {
3022 struct lu_fid fid = { 0 };
3024 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3025 llapi_printf(LLAPI_MSG_NORMAL,
3026 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
3027 space, i, idx, PFID(&fid));
3030 sprintf(fmt, "%s%s%s\n",
3031 "\t%6u\t%14llu\t%#13llx\t",
3032 (fid_seq_is_rsvd(gr) ||
3033 fid_seq_is_mdt0(gr)) ?
3034 "%14llu" : "%#14llx", "%s");
3035 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
3037 obdindex == idx ? " *" : "");
3041 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3044 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3045 char *path, int obdindex, int depth,
3046 enum llapi_layout_verbose verbose,
3047 enum lov_dump_flags flags)
3049 struct lmv_user_mds_data *objects = lum->lum_objects;
3050 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3051 char *separator = "";
3052 bool yaml = flags & LDF_YAML;
3053 bool obdstripe = false;
3056 if (obdindex != OBD_NOT_FOUND) {
3057 if (lum->lum_stripe_count == 0) {
3058 if (obdindex == lum->lum_stripe_offset)
3061 for (i = 0; i < lum->lum_stripe_count; i++) {
3062 if (obdindex == objects[i].lum_mds) {
3063 llapi_printf(LLAPI_MSG_NORMAL,
3078 /* show all information default */
3080 if (lum->lum_magic == LMV_USER_MAGIC)
3081 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3082 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3084 verbose = VERBOSE_OBJID;
3087 if (depth && path && ((verbose != VERBOSE_OBJID)))
3088 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3090 if (verbose & VERBOSE_STRIPE_COUNT) {
3091 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3092 if (verbose & ~VERBOSE_STRIPE_COUNT)
3093 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3094 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3095 (int)lum->lum_stripe_count);
3096 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3102 if (verbose & VERBOSE_STRIPE_OFFSET) {
3103 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3104 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3105 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3106 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3107 (int)lum->lum_stripe_offset);
3108 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3114 if (verbose & VERBOSE_HASH_TYPE) {
3115 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3116 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3118 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3119 if (verbose & ~VERBOSE_HASH_TYPE)
3120 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3121 if (type < LMV_HASH_TYPE_MAX)
3122 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3123 mdt_hash_name[type]);
3125 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3127 if (flags & LMV_HASH_FLAG_MIGRATION)
3128 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3129 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3130 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3131 if (flags & LMV_HASH_FLAG_LOST_LMV)
3132 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3137 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3138 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3139 if (lum->lum_stripe_count > 0)
3140 llapi_printf(LLAPI_MSG_NORMAL,
3141 "mdtidx\t\t FID[seq:oid:ver]\n");
3142 for (i = 0; i < lum->lum_stripe_count; i++) {
3143 int idx = objects[i].lum_mds;
3144 struct lu_fid *fid = &objects[i].lum_fid;
3145 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3146 llapi_printf(LLAPI_MSG_NORMAL,
3147 "%6u\t\t "DFID"\t\t%s\n",
3149 obdindex == idx ? " *" : "");
3154 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3155 pool_name[0] != '\0') {
3156 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3157 if (verbose & ~VERBOSE_POOL)
3158 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3160 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3164 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3165 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3168 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3169 enum lov_dump_flags flags)
3171 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3172 int depth = param->fp_max_depth;
3173 enum llapi_layout_verbose verbose = param->fp_verbose;
3174 bool yaml = flags & LDF_YAML;
3176 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3177 !(flags & LDF_IS_DIR)) && !yaml)
3178 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3180 if (verbose & VERBOSE_DETAIL) {
3181 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3182 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3183 " ", comp_v1->lcm_magic);
3184 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3185 " ", comp_v1->lcm_size);
3186 if (flags & LDF_IS_DIR)
3187 llapi_printf(LLAPI_MSG_NORMAL,
3188 "%2slcm_flags: %s\n", " ",
3189 comp_v1->lcm_mirror_count > 0 ?
3192 llapi_printf(LLAPI_MSG_NORMAL,
3193 "%2slcm_flags: %s\n", " ",
3194 llapi_layout_flags_string(comp_v1->lcm_flags));
3197 if (verbose & VERBOSE_GENERATION) {
3198 if (verbose & ~VERBOSE_GENERATION)
3199 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3201 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3204 if (verbose & VERBOSE_MIRROR_COUNT) {
3205 if (verbose & ~VERBOSE_MIRROR_COUNT)
3206 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3208 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3209 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3210 comp_v1->lcm_mirror_count + 1 : 1);
3213 if (verbose & VERBOSE_COMP_COUNT) {
3214 if (verbose & ~VERBOSE_COMP_COUNT)
3215 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3217 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3218 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3219 comp_v1->lcm_entry_count : 0);
3222 if (verbose & VERBOSE_DETAIL && !yaml)
3223 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3226 static void lcme_flags2str(__u32 comp_flags)
3232 llapi_printf(LLAPI_MSG_NORMAL, "0");
3235 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3236 if (comp_flags & comp_flags_table[i].cfn_flag) {
3238 llapi_printf(LLAPI_MSG_NORMAL, ",");
3239 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3240 comp_flags_table[i].cfn_name);
3241 comp_flags &= ~comp_flags_table[i].cfn_flag;
3247 llapi_printf(LLAPI_MSG_NORMAL, ",");
3248 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3252 static void lov_dump_comp_v1_entry(struct find_param *param,
3253 enum lov_dump_flags flags, int index)
3255 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3256 struct lov_comp_md_entry_v1 *entry;
3257 char *separator = "";
3258 enum llapi_layout_verbose verbose = param->fp_verbose;
3259 bool yaml = flags & LDF_YAML;
3261 entry = &comp_v1->lcm_entries[index];
3264 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3266 if (verbose & VERBOSE_COMP_ID) {
3267 if (verbose & VERBOSE_DETAIL && !yaml)
3268 llapi_printf(LLAPI_MSG_NORMAL,
3269 "%slcme_id: ", " - ");
3270 else if (verbose & ~VERBOSE_COMP_ID)
3271 llapi_printf(LLAPI_MSG_NORMAL,
3272 "%4slcme_id: ", " ");
3273 if (entry->lcme_id != LCME_ID_INVAL)
3274 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3276 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3280 if (verbose & VERBOSE_MIRROR_ID) {
3281 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3282 if (verbose & ~VERBOSE_MIRROR_ID)
3283 llapi_printf(LLAPI_MSG_NORMAL,
3284 "%4slcme_mirror_id: ", " ");
3285 if (entry->lcme_id != LCME_ID_INVAL)
3286 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3287 mirror_id_of(entry->lcme_id));
3289 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3293 if (verbose & VERBOSE_COMP_FLAGS) {
3294 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3295 if (verbose & ~VERBOSE_COMP_FLAGS)
3296 llapi_printf(LLAPI_MSG_NORMAL,
3297 "%4slcme_flags: ", " ");
3298 lcme_flags2str(entry->lcme_flags);
3301 /* print snapshot timestamp if its a nosync comp */
3302 if ((verbose & VERBOSE_COMP_FLAGS) &&
3303 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3304 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3305 if (verbose & ~VERBOSE_COMP_FLAGS)
3306 llapi_printf(LLAPI_MSG_NORMAL,
3307 "%4slcme_timestamp: ", " ");
3309 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3310 (unsigned long long)entry->lcme_timestamp);
3312 time_t stamp = entry->lcme_timestamp;
3313 char *date_str = asctime(localtime(&stamp));
3315 date_str[strlen(date_str) - 1] = '\0';
3316 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3322 if (verbose & VERBOSE_COMP_START) {
3323 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3324 if (verbose & ~VERBOSE_COMP_START)
3325 llapi_printf(LLAPI_MSG_NORMAL,
3326 "%4slcme_extent.e_start: ", " ");
3327 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3328 (unsigned long long)entry->lcme_extent.e_start);
3332 if (verbose & VERBOSE_COMP_END) {
3333 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3334 if (verbose & ~VERBOSE_COMP_END)
3335 llapi_printf(LLAPI_MSG_NORMAL,
3336 "%4slcme_extent.e_end: ", " ");
3337 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3338 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3340 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3341 (unsigned long long)entry->lcme_extent.e_end);
3346 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3347 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3348 } else if (verbose & VERBOSE_DETAIL) {
3349 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3350 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3351 " ", entry->lcme_offset);
3352 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3353 " ", entry->lcme_size);
3354 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3356 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3360 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3361 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3363 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3364 * The table below gives the answers for the specified parameters (value and
3365 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3366 * --------------------------------------
3367 * 1 | file > limit; sign > 0 | -1 / -1 |
3368 * 2 | file = limit; sign > 0 | -1 / -1 |
3369 * 3 | file < limit; sign > 0 | ? / 1 |
3370 * 4 | file > limit; sign = 0 | -1 / -1 |
3371 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3372 * 6 | file < limit; sign = 0 | ? / -1 |
3373 * 7 | file > limit; sign < 0 | 1 / 1 |
3374 * 8 | file = limit; sign < 0 | ? / -1 |
3375 * 9 | file < limit; sign < 0 | ? / -1 |
3376 * --------------------------------------
3377 * Note: 5th actually means that the value is within the interval
3378 * (limit - margin, limit]. */
3379 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3380 int sign, int negopt, unsigned long long margin,
3386 /* Drop the fraction of margin (of days or size). */
3387 if (file + margin <= limit)
3389 } else if (sign == 0) {
3390 if (file <= limit && file + margin > limit)
3392 else if (file + margin <= limit)
3394 } else if (sign < 0) {
3401 return negopt ? ~ret + 1 : ret;
3404 static inline struct lov_user_md *
3405 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3407 return (struct lov_user_md *)((char *)comp_v1 +
3408 comp_v1->lcm_entries[ent_idx].lcme_offset);
3411 static inline struct lov_user_ost_data_v1 *
3412 lov_v1v3_objects(struct lov_user_md *v1)
3414 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3415 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3417 return v1->lmm_objects;
3421 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3423 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3424 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3425 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3427 pool_name[0] = '\0';
3431 print_last_init_comp(struct find_param *param)
3433 /* print all component info */
3434 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3437 /* print specific component info */
3438 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3439 param->fp_check_comp_start || param->fp_check_comp_end ||
3440 param->fp_check_mirror_id || param->fp_check_mirror_index)
3446 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3450 if (param->fp_comp_end == LUSTRE_EOF) {
3451 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3452 match = end == LUSTRE_EOF ? 1 : -1;
3453 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3454 match = end == LUSTRE_EOF ? -1 : 1;
3455 else /* at least EOF */
3457 if (param->fp_exclude_comp_end)
3460 unsigned long long margin;
3462 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3463 match = find_value_cmp(end, param->fp_comp_end,
3464 param->fp_comp_end_sign,
3465 param->fp_exclude_comp_end, margin, 0);
3472 * An example of "getstripe -v" for a two components PFL file:
3475 * lcm_magic: 0x0BD60BD0
3479 * lcm_entry_count: 2
3483 * lcme_extent.e_start: 0
3484 * lcme_extent.e_end: 1048576
3488 * lmm_magic: 0x0BD10BD0
3489 * lmm_seq: 0x200000401
3490 * lmm_object_id: 0x1
3491 * lmm_fid: [0x200000401:0x1:0x0]
3492 * lmm_stripe_count: 1
3493 * lmm_stripe_size: 1048576
3494 * lmm_pattern: raid0
3496 * lmm_stripe_offset: 0
3498 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3502 * lcme_extent.e_start: 1048576
3503 * lcme_extent.e_end: EOF
3507 * lmm_magic: 0x0BD10BD0
3508 * lmm_seq: 0x200000401
3509 * lmm_object_id: 0x1
3510 * lmm_fid: [0x200000401:0x1:0x0]
3511 * lmm_stripe_count: 2
3512 * lmm_stripe_size: 1048576
3513 * lmm_pattern: raid0
3515 * lmm_stripe_offset: 1
3517 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3518 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3520 static void lov_dump_comp_v1(struct find_param *param, char *path,
3521 enum lov_dump_flags flags)
3523 struct lov_comp_md_entry_v1 *entry;
3524 struct lov_user_ost_data_v1 *objects;
3525 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3526 struct lov_user_md_v1 *v1;
3527 char pool_name[LOV_MAXPOOLNAME + 1];
3528 int obdindex = param->fp_obd_index;
3529 int i, j, match, ext;
3530 bool obdstripe = false;
3531 __u16 mirror_index = 0;
3532 __u16 mirror_id = 0;
3534 if (obdindex != OBD_NOT_FOUND) {
3535 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3536 i < comp_v1->lcm_entry_count; i++) {
3537 if (!(comp_v1->lcm_entries[i].lcme_flags &
3541 v1 = lov_comp_entry(comp_v1, i);
3542 objects = lov_v1v3_objects(v1);
3544 for (j = 0; j < v1->lmm_stripe_count; j++) {
3545 if (obdindex == objects[j].l_ost_idx) {
3558 lov_dump_comp_v1_header(param, path, flags);
3560 flags |= LDF_INDENT;
3562 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3563 entry = &comp_v1->lcm_entries[i];
3565 if (param->fp_check_comp_flags) {
3566 if (((param->fp_comp_flags & entry->lcme_flags) !=
3567 param->fp_comp_flags) ||
3568 (param->fp_comp_neg_flags & entry->lcme_flags))
3572 if (param->fp_check_comp_id &&
3573 param->fp_comp_id != entry->lcme_id)
3576 if (param->fp_check_comp_start) {
3577 match = find_value_cmp(entry->lcme_extent.e_start,
3578 param->fp_comp_start,
3579 param->fp_comp_start_sign,
3581 param->fp_comp_start_units, 0);
3586 if (param->fp_check_comp_end) {
3587 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3593 if (param->fp_check_mirror_index) {
3594 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3596 mirror_id = mirror_id_of(entry->lcme_id);
3599 match = find_value_cmp(mirror_index,
3600 param->fp_mirror_index,
3601 param->fp_mirror_index_sign,
3602 param->fp_exclude_mirror_index,
3606 } else if (param->fp_check_mirror_id) {
3607 if (mirror_id != mirror_id_of(entry->lcme_id))
3608 mirror_id = mirror_id_of(entry->lcme_id);
3610 match = find_value_cmp(mirror_id,
3611 param->fp_mirror_id,
3612 param->fp_mirror_id_sign,
3613 param->fp_exclude_mirror_id,
3619 if (print_last_init_comp(param)) {
3621 * if part of stripe info is needed, we'd print only
3622 * the last instantiated component info.
3624 if (entry->lcme_flags & LCME_FL_INIT)
3627 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3628 if (entry->lcme_flags & LCME_FL_EXTENSION)
3629 /* moved back below */
3637 if (entry->lcme_flags & LCME_FL_INIT) {
3638 if (obdindex != OBD_NOT_FOUND) {
3639 flags |= LDF_SKIP_OBJS;
3640 v1 = lov_comp_entry(comp_v1, i);
3641 objects = lov_v1v3_objects(v1);
3643 for (j = 0; j < v1->lmm_stripe_count; j++) {
3644 if (obdindex == objects[j].l_ost_idx) {
3645 flags &= ~LDF_SKIP_OBJS;
3650 flags &= ~LDF_SKIP_OBJS;
3653 flags |= LDF_SKIP_OBJS;
3656 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3658 lov_dump_comp_v1_entry(param, flags, i);
3660 v1 = lov_comp_entry(comp_v1, i);
3661 objects = lov_v1v3_objects(v1);
3662 lov_v1v3_pool_name(v1, pool_name);
3664 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3665 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3666 param->fp_max_depth, param->fp_verbose,
3669 if (print_last_init_comp(param)) {
3671 * directory layout contains only layout template, print the
3675 i = comp_v1->lcm_entry_count - 1;
3678 flags &= ~LDF_SKIP_OBJS;
3680 lov_dump_comp_v1_entry(param, flags, i);
3682 v1 = lov_comp_entry(comp_v1, i);
3683 objects = lov_v1v3_objects(v1);
3684 lov_v1v3_pool_name(v1, pool_name);
3686 entry = &comp_v1->lcm_entries[i];
3687 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3688 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3689 param->fp_max_depth, param->fp_verbose,
3694 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3695 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3698 static inline bool has_any_comp_options(struct find_param *param)
3700 enum llapi_layout_verbose verbose = param->fp_verbose;
3702 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3703 param->fp_check_comp_start || param->fp_check_comp_end ||
3704 param->fp_check_comp_flags)
3707 /* show full layout information, not component specific */
3708 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3711 return verbose & VERBOSE_COMP_OPTS;
3714 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3717 struct lov_user_md *lum = &orig->lmd_lmm;
3718 struct lov_user_mds_data *new;
3719 struct lov_comp_md_v1 *comp_v1;
3720 struct lov_comp_md_entry_v1 *ent;
3721 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3722 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3725 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3727 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3731 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3732 + sizeof(new->lmd_lmmsize));
3734 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3735 comp_v1->lcm_magic = lum->lmm_magic;
3736 comp_v1->lcm_size = lum_off + lum_size;
3737 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3738 comp_v1->lcm_flags = 0;
3739 comp_v1->lcm_entry_count = 1;
3741 ent = &comp_v1->lcm_entries[0];
3743 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3744 ent->lcme_extent.e_start = 0;
3745 ent->lcme_extent.e_end = LUSTRE_EOF;
3746 ent->lcme_offset = lum_off;
3747 ent->lcme_size = lum_size;
3749 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3754 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3755 enum lov_dump_flags flags)
3757 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3759 if (has_any_comp_options(param)) {
3760 struct lov_user_mds_data *new_lmd, *orig_lmd;
3762 orig_lmd = param->fp_lmd;
3763 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3764 if (new_lmd != NULL) {
3765 param->fp_lmd = new_lmd;
3766 lov_dump_comp_v1(param, path, flags);
3767 param->fp_lmd = orig_lmd;
3773 if (magic == LOV_USER_MAGIC_V1) {
3774 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3775 param->fp_lmd->lmd_lmm.lmm_objects,
3776 path, param->fp_obd_index,
3777 param->fp_max_depth, param->fp_verbose,
3780 char pool_name[LOV_MAXPOOLNAME + 1];
3781 struct lov_user_ost_data_v1 *objects;
3782 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3784 snprintf(pool_name, sizeof(pool_name), "%s",
3785 lmmv3->lmm_pool_name);
3786 objects = lmmv3->lmm_objects;
3787 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3788 objects, path, param->fp_obd_index,
3789 param->fp_max_depth, param->fp_verbose,
3794 static uint32_t check_foreign_type(uint32_t foreign_type)
3798 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3799 if (lu_foreign_types[i].lft_name == NULL)
3801 if (foreign_type == lu_foreign_types[i].lft_type)
3805 return LU_FOREIGN_TYPE_UNKNOWN;
3808 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3809 enum lov_dump_flags flags)
3811 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3812 bool yaml = flags & LDF_YAML;
3814 if (!yaml && param->fp_depth && path)
3815 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3817 if (param->fp_verbose & VERBOSE_DETAIL) {
3818 uint32_t type = check_foreign_type(lfm->lfm_type);
3820 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3822 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3824 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3826 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3827 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3828 lu_foreign_types[type].lft_name);
3830 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3832 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3835 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3836 lfm->lfm_length, lfm->lfm_value);
3837 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3840 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3841 enum lov_dump_flags flags)
3843 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3844 bool yaml = flags & LDF_YAML;
3846 if (!yaml && param->fp_depth && path)
3847 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3849 if (param->fp_verbose & VERBOSE_DETAIL) {
3850 uint32_t type = check_foreign_type(lfm->lfm_type);
3852 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3854 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3856 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3858 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3859 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3860 lu_foreign_types[type].lft_name);
3862 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3864 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3867 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3868 lfm->lfm_length, lfm->lfm_value);
3869 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3872 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3873 enum lov_dump_flags flags)
3877 if (param->fp_get_lmv || param->fp_get_default_lmv)
3878 magic = (__u32)param->fp_lmv_md->lum_magic;
3880 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3883 flags |= LDF_IS_RAW;
3888 case LOV_USER_MAGIC_V1:
3889 case LOV_USER_MAGIC_V3:
3890 case LOV_USER_MAGIC_SPECIFIC:
3891 lov_dump_plain_user_lmm(param, path, flags);
3893 case LOV_USER_MAGIC_FOREIGN:
3894 lov_dump_foreign_lmm(param, path, flags);
3897 case LMV_USER_MAGIC: {
3898 char pool_name[LOV_MAXPOOLNAME + 1];
3899 struct lmv_user_md *lum;
3901 lum = (struct lmv_user_md *)param->fp_lmv_md;
3902 snprintf(pool_name, sizeof(pool_name), "%s",
3903 lum->lum_pool_name);
3904 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3905 param->fp_max_depth, param->fp_verbose,
3909 case LOV_USER_MAGIC_COMP_V1:
3910 lov_dump_comp_v1(param, path, flags);
3912 case LMV_MAGIC_FOREIGN:
3913 lmv_dump_foreign_lmm(param, path, flags);
3916 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
3917 "(expecting one of %#x %#x %#x %#x)\n",
3918 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3919 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3920 LMV_USER_MAGIC, LMV_MAGIC_V1);
3925 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3931 fname = strrchr(path, '/');
3933 /* It should be a file (or other non-directory) */
3934 if (fname == NULL) {
3935 dname = (char *)malloc(2);
3939 fname = (char *)path;
3941 dname = (char *)malloc(fname - path + 1);
3944 strncpy(dname, path, fname - path);
3945 dname[fname - path] = '\0';
3949 fd = open(dname, O_RDONLY | O_NONBLOCK);
3955 strcpy((char *)lum, fname);
3956 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3959 if (close(fd) == -1 && rc == 0)
3967 int llapi_file_lookup(int dirfd, const char *name)
3969 struct obd_ioctl_data data = { 0 };
3974 if (dirfd < 0 || name == NULL)
3977 data.ioc_version = OBD_IOCTL_VERSION;
3978 data.ioc_len = sizeof(data);
3979 data.ioc_inlbuf1 = (char *)name;
3980 data.ioc_inllen1 = strlen(name) + 1;
3982 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3984 llapi_error(LLAPI_MSG_ERROR, rc,
3985 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3990 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
3996 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
3997 * Return -1 or 1 if file timestamp does not or does match the given criteria
3998 * correspondingly. Return 0 if the MDS time is being checked and there are
3999 * attributes on OSTs and it is not yet clear if the timespamp matches.
4001 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
4002 * updated timestamps. */
4003 static int find_time_check(lstatx_t *stx, struct find_param *param, int mds)
4008 /* Check if file is accepted. */
4009 if (param->fp_atime) {
4010 rc2 = find_value_cmp(stx->stx_atime.tv_sec, param->fp_atime,
4011 param->fp_asign, param->fp_exclude_atime,
4012 param->fp_time_margin, mds);
4018 if (param->fp_mtime) {
4019 rc2 = find_value_cmp(stx->stx_mtime.tv_sec, param->fp_mtime,
4020 param->fp_msign, param->fp_exclude_mtime,
4021 param->fp_time_margin, mds);
4025 /* If the previous check matches, but this one is not yet clear,
4026 * we should return 0 to do an RPC on OSTs. */
4031 if (param->fp_ctime) {
4032 rc2 = find_value_cmp(stx->stx_ctime.tv_sec, param->fp_ctime,
4033 param->fp_csign, param->fp_exclude_ctime,
4034 param->fp_time_margin, mds);
4038 /* If the previous check matches, but this one is not yet clear,
4039 * we should return 0 to do an RPC on OSTs. */
4048 * Check whether the stripes matches the indexes user provided
4052 static int check_obd_match(struct find_param *param)
4054 struct lov_user_ost_data_v1 *objects;
4055 struct lov_comp_md_v1 *comp_v1 = NULL;
4056 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4057 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
4058 int i, j, k, count = 1;
4060 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4063 if (!S_ISREG(stx->stx_mode))
4066 /* exclude foreign */
4067 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4068 return param->fp_exclude_obd;
4070 /* Only those files should be accepted, which have a
4071 * stripe on the specified OST. */
4072 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4073 comp_v1 = (struct lov_comp_md_v1 *)v1;
4074 count = comp_v1->lcm_entry_count;
4077 for (i = 0; i < count; i++) {
4079 v1 = lov_comp_entry(comp_v1, i);
4081 objects = lov_v1v3_objects(v1);
4083 for (j = 0; j < v1->lmm_stripe_count; j++) {
4084 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4087 for (k = 0; k < param->fp_num_obds; k++) {
4088 if (param->fp_obd_indexes[k] ==
4089 objects[j].l_ost_idx)
4090 return !param->fp_exclude_obd;
4095 return param->fp_exclude_obd;
4098 static int check_mdt_match(struct find_param *param)
4102 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4105 /* FIXME: For striped dir, we should get stripe information and check */
4106 for (i = 0; i < param->fp_num_mdts; i++) {
4107 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4108 return !param->fp_exclude_mdt;
4111 if (param->fp_exclude_mdt)
4118 * Check whether the obd is active or not, if it is
4119 * not active, just print the object affected by this
4122 static int print_failed_tgt(struct find_param *param, char *path, int type)
4124 struct obd_statfs stat_buf;
4125 struct obd_uuid uuid_buf;
4128 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV)
4131 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4132 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4133 ret = llapi_obd_statfs(path, type,
4134 param->fp_obd_index, &stat_buf,
4137 llapi_error(LLAPI_MSG_NORMAL, ret, "obd_uuid: %s failed",
4138 param->fp_obd_uuid->uuid);
4143 static int find_check_stripe_size(struct find_param *param)
4145 struct lov_comp_md_v1 *comp_v1 = NULL;
4146 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4147 __u32 stripe_size = 0;
4148 int ret, i, count = 1;
4150 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4151 return param->fp_exclude_stripe_size ? 1 : -1;
4153 ret = param->fp_exclude_stripe_size ? 1 : -1;
4154 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4155 comp_v1 = (struct lov_comp_md_v1 *)v1;
4156 count = comp_v1->lcm_entry_count;
4159 for (i = 0; i < count; i++) {
4160 struct lov_comp_md_entry_v1 *ent;
4163 v1 = lov_comp_entry(comp_v1, i);
4165 ent = &comp_v1->lcm_entries[i];
4166 if (ent->lcme_flags & LCME_FL_EXTENSION)
4168 if (!(ent->lcme_flags & LCME_FL_INIT))
4171 stripe_size = v1->lmm_stripe_size;
4174 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4175 param->fp_stripe_size_sign,
4176 param->fp_exclude_stripe_size,
4177 param->fp_stripe_size_units, 0);
4182 static int find_check_ext_size(struct find_param *param)
4184 struct lov_comp_md_v1 *comp_v1;
4185 struct lov_user_md_v1 *v1;
4188 ret = param->fp_exclude_ext_size ? 1 : -1;
4189 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4190 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4193 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4194 struct lov_comp_md_entry_v1 *ent;
4196 v1 = lov_comp_entry(comp_v1, i);
4198 ent = &comp_v1->lcm_entries[i];
4199 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4202 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4203 param->fp_ext_size_sign,
4204 param->fp_exclude_ext_size,
4205 param->fp_ext_size_units, 0);
4206 /* If any ext_size matches */
4214 static __u32 find_get_stripe_count(struct find_param *param)
4216 struct lov_comp_md_v1 *comp_v1 = NULL;
4217 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4219 __u32 stripe_count = 0;
4221 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4224 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4225 comp_v1 = (struct lov_comp_md_v1 *)v1;
4226 count = comp_v1->lcm_entry_count;
4229 for (i = 0; i < count; i++) {
4231 struct lov_comp_md_entry_v1 *ent;
4233 v1 = lov_comp_entry(comp_v1, i);
4235 ent = &comp_v1->lcm_entries[i];
4236 if (!(ent->lcme_flags & LCME_FL_INIT))
4239 if (ent->lcme_flags & LCME_FL_EXTENSION)
4242 stripe_count = v1->lmm_stripe_count;
4245 return stripe_count;
4248 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4250 static int find_check_layout(struct find_param *param)
4252 struct lov_comp_md_v1 *comp_v1 = NULL;
4253 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4255 bool found = false, valid = false;
4257 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4258 comp_v1 = (struct lov_comp_md_v1 *)v1;
4259 count = comp_v1->lcm_entry_count;
4262 for (i = 0; i < count; i++) {
4264 v1 = lov_comp_entry(comp_v1, i);
4266 /* foreign file have a special magic but no pattern field */
4267 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4270 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4274 if (v1->lmm_pattern & param->fp_layout) {
4283 if ((found && !param->fp_exclude_layout) ||
4284 (!found && param->fp_exclude_layout))
4290 /* if no type specified, check/exclude all foreign
4291 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4293 static int find_check_foreign(struct find_param *param)
4295 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4296 struct lov_foreign_md *lfm;
4298 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4299 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4300 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4301 return param->fp_exclude_foreign ? 1 : -1;
4304 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4305 lfm->lfm_type == param->fp_foreign_type)
4306 return param->fp_exclude_foreign ? -1 : 1;
4307 return param->fp_exclude_foreign ? 1 : -1;
4311 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4312 struct lmv_foreign_md *lfm;
4314 lfm = (void *)param->fp_lmv_md;
4315 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
4316 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4317 return param->fp_exclude_foreign ? 1 : -1;
4320 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4321 lfm->lfm_type == param->fp_foreign_type)
4322 return param->fp_exclude_foreign ? -1 : 1;
4323 return param->fp_exclude_foreign ? 1 : -1;
4329 static int find_check_pool(struct find_param *param)
4331 struct lov_comp_md_v1 *comp_v1 = NULL;
4332 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4333 struct lov_user_md_v3 *v3 = (void *)v1;
4337 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4338 comp_v1 = (struct lov_comp_md_v1 *)v1;
4339 count = comp_v1->lcm_entry_count;
4340 /* empty requested pool is taken as no pool search */
4341 if (count == 0 && param->fp_poolname[0] == '\0')
4345 for (i = 0; i < count; i++) {
4346 if (comp_v1 != NULL)
4347 v1 = lov_comp_entry(comp_v1, i);
4349 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4352 if (((v1->lmm_magic == LOV_USER_MAGIC_V1) &&
4353 (param->fp_poolname[0] == '\0')) ||
4354 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4355 (strncmp(v3->lmm_pool_name,
4356 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4357 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4358 (strcmp(param->fp_poolname, "*") == 0))) {
4364 if ((found && !param->fp_exclude_pool) ||
4365 (!found && param->fp_exclude_pool))
4371 static int find_check_comp_options(struct find_param *param)
4373 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
4374 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4375 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4376 struct lov_comp_md_entry_v1 *entry;
4379 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4382 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4383 comp_v1 = (struct lov_comp_md_v1 *)v1;
4385 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4386 if (forged_v1 == NULL)
4388 comp_v1 = forged_v1;
4389 comp_v1->lcm_entry_count = 1;
4390 entry = &comp_v1->lcm_entries[0];
4391 entry->lcme_flags = S_ISDIR(stx->stx_mode) ? 0 : LCME_FL_INIT;
4392 entry->lcme_extent.e_start = 0;
4393 entry->lcme_extent.e_end = LUSTRE_EOF;
4396 /* invalid case, don't match for any kind of search. */
4397 if (comp_v1->lcm_entry_count == 0) {
4402 if (param->fp_check_comp_count) {
4403 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4404 param->fp_comp_count,
4405 param->fp_comp_count_sign,
4406 param->fp_exclude_comp_count, 1, 0);
4412 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4413 entry = &comp_v1->lcm_entries[i];
4415 if (param->fp_check_comp_flags) {
4417 if (((param->fp_comp_flags & entry->lcme_flags) !=
4418 param->fp_comp_flags) ||
4419 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4425 if (param->fp_check_comp_start) {
4426 ret = find_value_cmp(entry->lcme_extent.e_start,
4427 param->fp_comp_start,
4428 param->fp_comp_start_sign,
4429 param->fp_exclude_comp_start,
4430 param->fp_comp_start_units, 0);
4435 if (param->fp_check_comp_end) {
4436 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4442 /* the component matches all criteria */
4451 static int find_check_mirror_options(struct find_param *param)
4453 struct lov_comp_md_v1 *comp_v1;
4454 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4457 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4460 comp_v1 = (struct lov_comp_md_v1 *)v1;
4462 if (param->fp_check_mirror_count) {
4463 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4464 param->fp_mirror_count,
4465 param->fp_mirror_count_sign,
4466 param->fp_exclude_mirror_count, 1, 0);
4471 if (param->fp_check_mirror_state) {
4473 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4475 if ((param->fp_mirror_state != 0 &&
4476 file_state != param->fp_mirror_state) ||
4477 file_state == param->fp_mirror_neg_state)
4484 static bool find_check_lmm_info(struct find_param *param)
4486 return param->fp_check_pool || param->fp_check_stripe_count ||
4487 param->fp_check_stripe_size || param->fp_check_layout ||
4488 param->fp_check_comp_count || param->fp_check_comp_end ||
4489 param->fp_check_comp_start || param->fp_check_comp_flags ||
4490 param->fp_check_mirror_count || param->fp_check_foreign ||
4491 param->fp_check_mirror_state || param->fp_check_ext_size ||
4492 param->fp_check_projid;
4496 * Get file/directory project id.
4497 * by the open fd resides on.
4498 * Return 0 and project id on success, or -ve errno.
4500 static int fget_projid(int fd, int *projid)
4505 rc = ioctl(fd, LL_IOC_FSGETXATTR, &fsx);
4509 *projid = fsx.fsx_projid;
4513 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
4514 void *data, struct dirent64 *de)
4516 struct find_param *param = (struct find_param *)data;
4517 DIR *dir = dirp == NULL ? NULL : *dirp;
4518 int decision = 1; /* 1 is accepted; -1 is rejected. */
4519 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
4521 int checked_type = 0;
4523 __u32 stripe_count = 0;
4527 if (parent == NULL && dir == NULL)
4530 /* If a regular expression is presented, make the initial decision */
4531 if (param->fp_pattern != NULL) {
4532 char *fname = strrchr(path, '/');
4533 fname = (fname == NULL ? path : fname + 1);
4534 ret = fnmatch(param->fp_pattern, fname, 0);
4535 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4536 (ret == 0 && param->fp_exclude_pattern))
4540 /* See if we can check the file type from the dirent. */
4541 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
4544 if (DTTOIF(de->d_type) == param->fp_type) {
4545 if (param->fp_exclude_type)
4548 if (!param->fp_exclude_type)
4555 /* Request MDS for the stat info if some of these parameters need
4556 * to be compared. */
4557 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4558 param->fp_check_uid || param->fp_check_gid ||
4559 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4560 param->fp_check_size || param->fp_check_blocks ||
4561 find_check_lmm_info(param) ||
4562 param->fp_check_mdt_count || param->fp_check_hash_type)
4565 if (param->fp_type != 0 && checked_type == 0)
4568 if (decision == 0) {
4569 if (dir && (param->fp_check_mdt_count ||
4570 param->fp_check_hash_type || param->fp_check_foreign)) {
4571 param->fp_get_lmv = 1;
4572 ret = cb_get_dirstripe(path, dir, param);
4574 /* XXX this works to decide for foreign
4577 if (errno == ENODATA &&
4578 param->fp_check_foreign) {
4579 if (param->fp_exclude_foreign)
4587 param->fp_lmd->lmd_lmm.lmm_magic = 0;
4588 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
4589 param->fp_lum_size, GET_LMD_INFO);
4590 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
4591 find_check_lmm_info(param)) {
4592 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
4594 /* We need to "fake" the "use the default" values
4595 * since the lmm struct is zeroed out at this point. */
4596 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4597 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
4599 ostid_set_seq(&lmm->lmm_oi,
4600 FID_SEQ_LOV_DEFAULT);
4601 lmm->lmm_stripe_size = 0;
4602 lmm->lmm_stripe_count = 0;
4603 lmm->lmm_stripe_offset = -1;
4605 if (ret == 0 && param->fp_mdt_uuid != NULL) {
4607 ret = llapi_file_fget_mdtidx(dirfd(dir),
4608 ¶m->fp_file_mdt_index);
4609 } else if (S_ISREG(stx->stx_mode)) {
4610 /* FIXME: we could get the MDT index from the
4611 * file's FID in lmd->lmd_lmm.lmm_oi without
4612 * opening the file, once we are sure that
4613 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
4614 * That would still be an ioctl() to map the
4615 * FID to the MDT, but not an open RPC. */
4616 fd = open(path, O_RDONLY);
4618 ret = llapi_file_fget_mdtidx(fd,
4619 ¶m->fp_file_mdt_index);
4624 /* For a special file, we assume it resides on
4625 * the same MDT as the parent directory. */
4626 ret = llapi_file_fget_mdtidx(dirfd(parent),
4627 ¶m->fp_file_mdt_index);
4638 stripe_count = find_get_stripe_count(param);
4642 if (param->fp_type && !checked_type) {
4643 if ((stx->stx_mode & S_IFMT) == param->fp_type) {
4644 if (param->fp_exclude_type)
4647 if (!param->fp_exclude_type)
4653 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
4654 if (lustre_fs && param->fp_got_uuids &&
4655 param->fp_dev != makedev(stx->stx_dev_major,
4656 stx->stx_dev_minor)) {
4657 /* A lustre/lustre mount point is crossed. */
4658 param->fp_got_uuids = 0;
4659 param->fp_obds_printed = 0;
4660 param->fp_mdt_index = OBD_NOT_FOUND;
4661 param->fp_obd_index = OBD_NOT_FOUND;
4664 if (lustre_fs && !param->fp_got_uuids) {
4665 ret = setup_target_indexes(dir ? dir : parent, path,
4670 param->fp_dev = makedev(stx->stx_dev_major,
4671 stx->stx_dev_minor);
4672 } else if (!lustre_fs && param->fp_got_uuids) {
4673 /* A lustre/non-lustre mount point is crossed. */
4674 param->fp_got_uuids = 0;
4675 param->fp_mdt_index = OBD_NOT_FOUND;
4676 param->fp_obd_index = OBD_NOT_FOUND;
4680 if (param->fp_check_foreign) {
4681 decision = find_check_foreign(param);
4686 if (param->fp_check_stripe_size) {
4687 decision = find_check_stripe_size(param);
4692 if (param->fp_check_ext_size) {
4693 decision = find_check_ext_size(param);
4698 if (param->fp_check_stripe_count) {
4699 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
4700 param->fp_stripe_count_sign,
4701 param->fp_exclude_stripe_count, 1, 0);
4706 if (param->fp_check_mdt_count) {
4707 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4712 decision = find_value_cmp(
4713 param->fp_lmv_md->lum_stripe_count,
4714 param->fp_mdt_count,
4715 param->fp_mdt_count_sign,
4716 param->fp_exclude_mdt_count, 1, 0);
4721 if (param->fp_check_layout) {
4722 decision = find_check_layout(param);
4727 if (param->fp_check_hash_type) {
4730 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4735 found = param->fp_lmv_md->lum_hash_type & param->fp_hash_type;
4736 if ((found && param->fp_exclude_hash_type) ||
4737 (!found && !param->fp_exclude_hash_type)) {
4743 /* If an OBD UUID is specified but none matches, skip this file. */
4744 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
4745 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
4748 /* If an OST or MDT UUID is given, and some OST matches,
4750 if (param->fp_obd_index != OBD_NOT_FOUND ||
4751 param->fp_mdt_index != OBD_NOT_FOUND) {
4752 if (param->fp_obd_uuid) {
4753 if (check_obd_match(param)) {
4754 /* If no mdtuuid is given, we are done.
4755 * Otherwise, fall through to the mdtuuid
4757 if (!param->fp_mdt_uuid)
4764 if (param->fp_mdt_uuid) {
4765 if (check_mdt_match(param))
4772 if (param->fp_check_uid) {
4773 if (stx->stx_uid == param->fp_uid) {
4774 if (param->fp_exclude_uid)
4777 if (!param->fp_exclude_uid)
4782 if (param->fp_check_gid) {
4783 if (stx->stx_gid == param->fp_gid) {
4784 if (param->fp_exclude_gid)
4787 if (!param->fp_exclude_gid)
4792 if (param->fp_check_projid) {
4796 fd = open(path, O_RDONLY);
4799 ret = fget_projid(fd, &projid);
4804 if (projid == param->fp_projid) {
4805 if (param->fp_exclude_projid)
4808 if (!param->fp_exclude_projid)
4813 if (param->fp_check_pool) {
4814 decision = find_check_pool(param);
4819 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
4820 param->fp_check_comp_start || param->fp_check_comp_end) {
4821 decision = find_check_comp_options(param);
4826 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
4827 decision = find_check_mirror_options(param);
4832 /* Check the time on mds. */
4834 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
4837 for_mds = lustre_fs ?
4838 (S_ISREG(stx->stx_mode) && stripe_count) : 0;
4839 decision = find_time_check(stx, param, for_mds);
4844 flags = param->fp_lmd->lmd_flags;
4845 if (param->fp_check_size &&
4846 ((S_ISREG(stx->stx_mode) && stripe_count) ||
4847 S_ISDIR(stx->stx_mode)) &&
4848 !(flags & OBD_MD_FLSIZE ||
4849 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
4852 if (param->fp_check_blocks &&
4853 ((S_ISREG(stx->stx_mode) && stripe_count) ||
4854 S_ISDIR(stx->stx_mode)) &&
4855 !(flags & OBD_MD_FLBLOCKS ||
4856 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
4859 /* If file still fits the request, ask ost for updated info.
4860 * The regular stat is almost of the same speed as some new
4861 * 'glimpse-size-ioctl'.
4866 /* For regular files with the stripe the decision may have not
4867 * been taken yet if *time or size is to be checked. */
4868 if (param->fp_obd_index != OBD_NOT_FOUND)
4869 print_failed_tgt(param, path, LL_STATFS_LOV);
4871 if (param->fp_mdt_index != OBD_NOT_FOUND)
4872 print_failed_tgt(param, path, LL_STATFS_LMV);
4875 ret = fstat_f(dirfd(dir), &st);
4876 else if (de != NULL)
4877 ret = fstatat_f(dirfd(parent), de->d_name, &st,
4878 AT_SYMLINK_NOFOLLOW);
4880 ret = lstat_f(path, &st);
4883 if (errno == ENOENT) {
4884 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
4885 "warning: %s: %s does not exist",
4890 llapi_error(LLAPI_MSG_ERROR, ret,
4891 "%s: stat on %s failed",
4897 convert_lmd_statx(param->fp_lmd, &st, true);
4898 /* Check the time on osc. */
4899 decision = find_time_check(stx, param, 0);
4904 if (param->fp_check_size) {
4905 decision = find_value_cmp(stx->stx_size, param->fp_size,
4906 param->fp_size_sign,
4907 param->fp_exclude_size,
4908 param->fp_size_units, 0);
4913 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
4914 decision = find_value_cmp(stx->stx_blocks * 512,
4916 param->fp_blocks_sign,
4917 param->fp_exclude_blocks,
4918 param->fp_blocks_units, 0);
4924 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
4925 if (param->fp_zero_end)
4926 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
4928 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4932 /* Do not get down anymore? */
4933 if (param->fp_depth == param->fp_max_depth) {
4944 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
4945 void *param_data, struct dirent64 *de)
4947 struct find_param *param = (struct find_param *)param_data;
4948 struct lmv_user_md *lmu = param->fp_lmv_md;
4949 DIR *tmp_parent = parent;
4950 char raw[MAX_IOC_BUFLEN] = {'\0'};
4952 struct obd_ioctl_data data = { 0 };
4959 if (parent == NULL && dirp == NULL)
4968 if (parent == NULL) {
4969 tmp_parent = opendir_parent(path);
4970 if (tmp_parent == NULL) {
4973 llapi_error(LLAPI_MSG_ERROR, ret,
4974 "can not open %s", path);
4979 fd = dirfd(tmp_parent);
4981 path_copy = strdup(path);
4982 filename = basename(path_copy);
4984 data.ioc_inlbuf1 = (char *)filename;
4985 data.ioc_inllen1 = strlen(filename) + 1;
4986 data.ioc_inlbuf2 = (char *)lmu;
4987 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
4989 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
4991 llapi_error(LLAPI_MSG_ERROR, ret,
4992 "llapi_obd_statfs: error packing ioctl data");
4997 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
4999 if (errno == EBUSY && !retry) {
5000 /* because migrate may not be able to lock all involved
5001 * objects in order, for some of them it try lock, while
5002 * there may be conflicting COS locks and cause migrate
5003 * fail with EBUSY, hope a sync() could cause
5004 * transaction commit and release these COS locks. */
5008 } else if (errno == EALREADY) {
5009 if (param->fp_verbose & VERBOSE_DETAIL)
5010 llapi_printf(LLAPI_MSG_NORMAL,
5011 "%s migrated to MDT%d already\n",
5012 path, lmu->lum_stripe_offset);
5016 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
5020 } else if (param->fp_verbose & VERBOSE_DETAIL) {
5021 llapi_printf(LLAPI_MSG_NORMAL,
5022 "migrate %s to MDT%d stripe count %d\n",
5023 path, lmu->lum_stripe_offset,
5024 lmu->lum_stripe_count);
5029 /* If the directory is being migration, we need
5030 * close the directory after migration,
5031 * so the old directory cache will be cleanup
5032 * on the client side, and re-open to get the
5033 * new directory handle */
5034 *dirp = opendir(path);
5035 if (*dirp == NULL) {
5037 llapi_error(LLAPI_MSG_ERROR, ret,
5038 "%s: Failed to open '%s'", __func__, path);
5043 closedir(tmp_parent);
5050 /* dir migration finished, shrink its stripes */
5051 static int cb_migrate_mdt_fini(char *path, DIR *parent, DIR **dirp, void *data,
5052 struct dirent64 *de)
5054 struct find_param *param = data;
5055 struct lmv_user_md *lmu = param->fp_lmv_md;
5056 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
5059 if (de && de->d_type != DT_DIR)
5064 * close it before setxattr because the latter may destroy the
5065 * original object, and cause close fail.
5067 ret = closedir(*dirp);
5073 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
5074 if (ret == -EALREADY)
5077 cb_common_fini(path, parent, dirp, data, de);
5081 int llapi_migrate_mdt(char *path, struct find_param *param)
5083 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
5087 int llapi_mv(char *path, struct find_param *param)
5089 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
5090 static bool printed;
5093 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
5094 "%s() is deprecated, use llapi_migrate_mdt() instead",
5099 return llapi_migrate_mdt(path, param);
5102 int llapi_find(char *path, struct find_param *param)
5104 return param_callback(path, cb_find_init, cb_common_fini, param);
5108 * Get MDT number that the file/directory inode referenced
5109 * by the open fd resides on.
5110 * Return 0 and mdtidx on success, or -ve errno.
5112 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
5114 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
5119 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
5120 struct dirent64 *de)
5122 struct find_param *param = (struct find_param *)data;
5123 DIR *d = dirp == NULL ? NULL : *dirp;
5127 if (parent == NULL && d == NULL)
5131 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5132 } else /* if (parent) */ {
5135 fd = open(path, O_RDONLY | O_NOCTTY);
5137 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5145 if (ret == -ENODATA) {
5146 if (!param->fp_obd_uuid)
5147 llapi_printf(LLAPI_MSG_NORMAL,
5148 "'%s' has no stripe info\n", path);
5150 } else if (ret == -ENOENT) {
5151 llapi_error(LLAPI_MSG_WARN, ret,
5152 "warning: %s: '%s' does not exist",
5155 } else if (ret == -ENOTTY) {
5156 llapi_error(LLAPI_MSG_ERROR, ret,
5157 "%s: '%s' not on a Lustre fs",
5160 llapi_error(LLAPI_MSG_ERROR, ret,
5161 "error: %s: '%s' failed get_mdtidx",
5167 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5168 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5170 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5174 /* Do not go down anymore? */
5175 if (param->fp_depth == param->fp_max_depth)
5183 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
5184 struct dirent64 *de)
5186 struct find_param *param = (struct find_param *)data;
5187 DIR *d = dirp == NULL ? NULL : *dirp;
5190 if (parent == NULL && d == NULL)
5193 if (param->fp_obd_uuid) {
5194 param->fp_quiet = 1;
5195 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
5200 if (d && (param->fp_get_lmv || param->fp_get_default_lmv))
5201 ret = cb_get_dirstripe(path, d, param);
5203 (parent && !param->fp_get_lmv && !param->fp_get_default_lmv))
5204 ret = get_lmd_info(path, parent, d, ¶m->fp_lmd->lmd_lmm,
5205 param->fp_lum_size, GET_LMD_STRIPE);
5210 if (errno == ENODATA && d != NULL) {
5211 /* We need to "fake" the "use the default" values
5212 * since the lmm struct is zeroed out at this point.
5213 * The magic needs to be set in order to satisfy
5214 * a check later on in the code path.
5215 * The object_seq needs to be set for the "(Default)"
5216 * prefix to be displayed. */
5217 if (param->fp_get_default_lmv) {
5218 struct lmv_user_md *lum = param->fp_lmv_md;
5220 lum->lum_magic = LMV_USER_MAGIC;
5221 lum->lum_stripe_count = 0;
5222 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
5224 } else if (param->fp_get_lmv) {
5225 struct lmv_user_md *lum = param->fp_lmv_md;
5228 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5231 lum->lum_magic = LMV_MAGIC_V1;
5232 lum->lum_stripe_count = 0;
5233 lum->lum_stripe_offset = mdtidx;
5236 struct lov_user_md *lmm =
5237 ¶m->fp_lmd->lmd_lmm;
5239 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5241 ostid_set_seq(&lmm->lmm_oi,
5242 FID_SEQ_LOV_DEFAULT);
5243 lmm->lmm_stripe_count = 0;
5244 lmm->lmm_stripe_size = 0;
5245 lmm->lmm_stripe_offset = -1;
5248 } else if (errno == ENODATA && parent != NULL) {
5249 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5250 llapi_printf(LLAPI_MSG_NORMAL,
5251 "%s has no stripe info\n", path);
5253 } else if (errno == ENOENT) {
5254 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5255 "warning: %s: %s does not exist",
5258 } else if (errno == ENOTTY) {
5260 llapi_error(LLAPI_MSG_ERROR, ret,
5261 "%s: '%s' not on a Lustre fs?",
5266 llapi_error(LLAPI_MSG_ERROR, ret,
5267 "error: %s: %s failed for %s",
5268 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
5269 "IOC_MDC_GETFILESTRIPE", path);
5276 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5277 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
5280 /* Do not get down anymore? */
5281 if (param->fp_depth == param->fp_max_depth)
5289 int llapi_getstripe(char *path, struct find_param *param)
5291 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5292 cb_get_mdt_index : cb_getstripe,
5293 cb_common_fini, param);
5296 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5297 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5299 char raw[MAX_IOC_BUFLEN] = {'\0'};
5301 struct obd_ioctl_data data = { 0 };
5304 data.ioc_inlbuf1 = (char *)&type;
5305 data.ioc_inllen1 = sizeof(__u32);
5306 data.ioc_inlbuf2 = (char *)&index;
5307 data.ioc_inllen2 = sizeof(__u32);
5308 data.ioc_pbuf1 = (char *)stat_buf;
5309 data.ioc_plen1 = sizeof(struct obd_statfs);
5310 data.ioc_pbuf2 = (char *)uuid_buf;
5311 data.ioc_plen2 = sizeof(struct obd_uuid);
5313 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5315 llapi_error(LLAPI_MSG_ERROR, rc,
5316 "llapi_obd_statfs: error packing ioctl data");
5320 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5322 return rc < 0 ? -errno : 0;
5325 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5326 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5331 fd = open(path, O_RDONLY);
5334 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5336 /* If we can't even open a file on the filesystem (e.g. with
5337 * -ESHUTDOWN), force caller to exit or it will loop forever. */
5341 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5348 #define MAX_STRING_SIZE 128
5350 int llapi_ping(char *obd_type, char *obd_name)
5352 int flags = O_RDONLY;
5353 char buf[1] = { 0 };
5357 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5358 obd_type, obd_name);
5362 fd = open(path.gl_pathv[0], flags);
5364 if (errno == EACCES && flags == O_RDONLY) {
5369 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5374 if (flags == O_RDONLY)
5375 rc = read(fd, buf, sizeof(buf));
5377 rc = write(fd, buf, sizeof(buf));
5385 cfs_free_param_data(&path);
5389 int llapi_target_iterate(int type_num, char **obd_type,
5390 void *args, llapi_cb_t cb)
5396 for (i = 0; i < type_num; i++) {
5399 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5403 for (j = 0; j < param.gl_pathc; j++) {
5404 char obd_uuid[UUID_MAX + 1];
5408 fp = fopen(param.gl_pathv[j], "r");
5411 llapi_error(LLAPI_MSG_ERROR, rc,
5412 "error: opening '%s'",
5417 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5419 llapi_error(LLAPI_MSG_ERROR, rc,
5420 "error: reading '%s'",
5425 /* Extract the obd_name from the sysfs path.
5426 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5428 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5434 /* skip /fs/lustre/'obd_type'/ */
5435 obd_name += strlen(obd_type[i]) + 12;
5436 /* chop off after obd_name */
5437 ptr = strrchr(obd_name, '/');
5441 cb(obd_type[i], obd_name, obd_uuid, args);
5450 cfs_free_param_data(¶m);
5454 static void do_target_check(char *obd_type_name, char *obd_name,
5455 char *obd_uuid, void *args)
5459 rc = llapi_ping(obd_type_name, obd_name);
5460 if (rc == ENOTCONN) {
5461 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5463 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5465 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5469 int llapi_target_check(int type_num, char **obd_type, char *dir)
5471 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5474 #undef MAX_STRING_SIZE
5476 /* Is this a lustre fs? */
5477 int llapi_is_lustre_mnttype(const char *type)
5479 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
5482 /* Is this a lustre client fs? */
5483 int llapi_is_lustre_mnt(struct mntent *mnt)
5485 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5486 strstr(mnt->mnt_fsname, ":/") != NULL);
5489 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5491 char fsname[PATH_MAX + 1];
5495 rc = llapi_search_fsname(mnt, fsname);
5499 root = open(mnt, O_RDONLY | O_DIRECTORY);
5502 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
5506 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
5514 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
5519 root = open(mnt, O_RDONLY | O_DIRECTORY);
5522 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
5526 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
5529 llapi_error(LLAPI_MSG_ERROR, rc,
5530 "ioctl on %s for getting connect flags failed", mnt);
5537 * Get a 64-bit value representing the version of file data pointed by fd.
5539 * Each write or truncate, flushed on OST, will change this value. You can use
5540 * this value to verify if file data was modified. This only checks the file
5541 * data, not metadata.
5543 * \param flags 0: no flush pages, usually used it the process has already
5545 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
5547 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
5548 * pages from clients.
5550 * \retval 0 on success.
5551 * \retval -errno on error.
5553 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
5556 struct ioc_data_version idv;
5558 idv.idv_flags = (__u32)flags;
5560 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5564 *data_version = idv.idv_version;
5570 * Flush cached pages from all clients.
5572 * \param fd File descriptor
5576 int llapi_file_flush(int fd)
5580 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);
5584 * Fetch layout version from OST objects. Layout version on OST objects are
5585 * only set when the file is a mirrored file AND after the file has been
5586 * written at least once.
5588 * It actually fetches the least layout version from the objects.
5590 int llapi_get_ost_layout_version(int fd, __u32 *layout_version)
5593 struct ioc_data_version idv = { 0 };
5595 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5599 *layout_version = idv.idv_layout_version;
5605 * Create a file without any name and open it for read/write
5607 * - file is created as if it were a standard file in the given \a directory
5608 * - file does not appear in \a directory and mtime does not change because
5609 * the filename is handled specially by the Lustre MDS.
5610 * - file is destroyed at final close
5612 * \param[in] directory directory from which to inherit layout/MDT idx
5613 * \param[in] mdt_idx MDT index on which the file is created,
5614 * \a idx == -1 means no specific MDT is requested
5615 * \param[in] mode standard open(2) mode
5616 * \param[in] stripe_param stripe parameters. May be NULL.
5618 * \retval a file descriptor on success.
5619 * \retval -errno on error.
5621 int llapi_create_volatile_param(const char *directory, int mdt_idx,
5622 int open_flags, mode_t mode,
5623 const struct llapi_stripe_param *stripe_param)
5625 char file_path[PATH_MAX];
5626 int saved_errno = errno;
5628 unsigned int rnumber;
5634 rc = snprintf(file_path, sizeof(file_path),
5635 "%s/" LUSTRE_VOLATILE_HDR "::%.4X",
5636 directory, rnumber);
5638 rc = snprintf(file_path, sizeof(file_path),
5639 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X",
5640 directory, mdt_idx, rnumber);
5642 if (rc < 0 || rc >= sizeof(file_path))
5643 return -ENAMETOOLONG;
5646 * Either open O_WRONLY or O_RDWR, creating RDONLY
5647 * is non-sensical here
5649 if ((open_flags & O_ACCMODE) == O_RDONLY)
5650 open_flags = O_RDWR | (open_flags & ~O_ACCMODE);
5652 open_flags |= O_CREAT | O_EXCL | O_NOFOLLOW;
5654 if (stripe_param != NULL) {
5655 fd = llapi_file_open_param(file_path, open_flags,
5656 mode, stripe_param);
5660 fd = open(file_path, open_flags, mode);
5664 } while (fd < 0 && rc == -EEXIST);
5667 llapi_error(LLAPI_MSG_ERROR, rc,
5668 "Cannot create volatile file '%s' in '%s'",
5669 file_path + strlen(directory) + 1 +
5670 LUSTRE_VOLATILE_HDR_LEN,
5676 * Unlink file in case this wasn't a Lustre filesystem and the magic
5677 * volatile filename wasn't handled as intended. The effect is the
5678 * same. If volatile open was supported then we expect unlink() to
5681 (void)unlink(file_path);
5684 * Since we are returning successfully we restore errno (and
5685 * mask out possible EEXIST from open() and ENOENT from unlink().
5687 errno = saved_errno;
5693 * Create a file without any name open it for read/write
5695 * - file is created as if it were a standard file in the given \a directory
5696 * - file does not appear in \a directory and mtime does not change because
5697 * the filename is handled specially by the Lustre MDS.
5698 * - file is removed at final close
5699 * - file modes are rw------- since it doesn't make sense to have a read-only
5700 * or write-only file that cannot be opened again.
5701 * - if user wants another mode it must use fchmod() on the open file, no
5702 * security problems arise because it cannot be opened by another process.
5704 * \param[in] directory directory from which to inherit layout/MDT idx
5705 * \param[in] idx MDT index on which the file is created,
5706 * \a idx == -1 means no specific MDT is requested
5707 * \param[in] open_flags standard open(2) flags
5709 * \retval a file descriptor on success.
5710 * \retval -errno on error.
5712 int llapi_create_volatile_idx(const char *directory, int mdt_idx,
5715 return llapi_create_volatile_param(directory, mdt_idx, open_flags,
5716 S_IRUSR | S_IWUSR, NULL);
5720 * Swap the layouts between 2 file descriptors
5721 * the 2 files must be open for writing
5722 * first fd received the ioctl, second fd is passed as arg
5723 * this is assymetric but avoid use of root path for ioctl
5725 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
5726 int gid, __u64 flags)
5728 struct lustre_swap_layouts lsl;
5733 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5734 rc = fstat(fd1, &st1);
5738 rc = fstat(fd2, &st2);
5743 lsl.sl_flags = flags;
5747 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
5751 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5752 struct timeval tv1[2];
5753 struct timeval tv2[2];
5755 memset(tv1, 0, sizeof(tv1));
5756 memset(tv2, 0, sizeof(tv2));
5758 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
5759 tv1[0].tv_sec = st1.st_atime;
5760 tv2[0].tv_sec = st2.st_atime;
5762 tv1[0].tv_sec = st2.st_atime;
5763 tv2[0].tv_sec = st1.st_atime;
5766 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
5767 tv1[1].tv_sec = st1.st_mtime;
5768 tv2[1].tv_sec = st2.st_mtime;
5770 tv1[1].tv_sec = st2.st_mtime;
5771 tv2[1].tv_sec = st1.st_mtime;
5774 rc = futimes(fd1, tv1);
5778 rc = futimes(fd2, tv2);
5786 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
5793 while (grp_id == 0);
5795 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
5803 * Swap the layouts between 2 files
5804 * the 2 files are open in write
5806 int llapi_swap_layouts(const char *path1, const char *path2,
5807 __u64 dv1, __u64 dv2, __u64 flags)
5811 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
5814 llapi_error(LLAPI_MSG_ERROR, rc,
5815 "error: cannot open '%s' for write", path1);
5819 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
5822 llapi_error(LLAPI_MSG_ERROR, rc,
5823 "error: cannot open '%s' for write", path2);
5827 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
5829 llapi_error(LLAPI_MSG_ERROR, rc,
5830 "error: cannot swap layout between '%s' and '%s'",
5843 * \param fd File to lock.
5844 * \param gid Group Identifier.
5846 * \retval 0 on success.
5847 * \retval -errno on failure.
5849 int llapi_group_lock(int fd, int gid)
5853 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
5856 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
5864 * \param fd File to unlock.
5865 * \param gid Group Identifier.
5867 * \retval 0 on success.
5868 * \retval -errno on failure.
5870 int llapi_group_unlock(int fd, int gid)
5874 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
5877 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");