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);
1479 * Get the list of pool members.
1480 * \param poolname string of format \<fsname\>.\<poolname\>
1481 * \param members caller-allocated array of char*
1482 * \param list_size size of the members array
1483 * \param buffer caller-allocated buffer for storing OST names
1484 * \param buffer_size size of the buffer
1486 * \return number of members retrieved for this pool
1487 * \retval -error failure
1489 int llapi_get_poolmembers(const char *poolname, char **members,
1490 int list_size, char *buffer, int buffer_size)
1492 char fsname[PATH_MAX];
1501 /* name is FSNAME.POOLNAME */
1502 if (strlen(poolname) >= sizeof(fsname))
1505 snprintf(fsname, sizeof(fsname), "%s", poolname);
1506 pool = strchr(fsname, '.');
1513 rc = poolpath(&pathname, fsname, NULL);
1515 llapi_error(LLAPI_MSG_ERROR, rc,
1516 "Lustre filesystem '%s' not found",
1521 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1522 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1523 cfs_free_param_data(&pathname);
1524 if (rc >= sizeof(buf))
1526 fd = fopen(buf, "r");
1529 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1534 while (fgets(buf, sizeof(buf), fd) != NULL) {
1535 if (nb_entries >= list_size) {
1539 buf[sizeof(buf) - 1] = '\0';
1541 tmp = strchr(buf, '\n');
1544 if (used + strlen(buf) + 1 > buffer_size) {
1549 strcpy(buffer + used, buf);
1550 members[nb_entries] = buffer + used;
1551 used += strlen(buf) + 1;
1561 * Get the list of pools in a filesystem.
1562 * \param name filesystem name or path
1563 * \param poollist caller-allocated array of char*
1564 * \param list_size size of the poollist array
1565 * \param buffer caller-allocated buffer for storing pool names
1566 * \param buffer_size size of the buffer
1568 * \return number of pools retrieved for this filesystem
1569 * \retval -error failure
1571 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1572 char *buffer, int buffer_size)
1578 struct dirent *pool;
1580 unsigned int nb_entries = 0;
1581 unsigned int used = 0;
1584 /* initialize output array */
1585 for (i = 0; i < list_size; i++)
1588 /* is name a pathname ? */
1589 ptr = strchr(name, '/');
1591 char fsname_buf[MAXNAMLEN];
1593 /* We will need fsname for printing later */
1594 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1598 ptr = strrchr(fsname_buf, '-');
1602 fsname = strdup(fsname_buf);
1606 /* name is FSNAME */
1607 fsname = strdup(name);
1612 rc = poolpath(&pathname, fsname, NULL);
1614 llapi_error(LLAPI_MSG_ERROR, rc,
1615 "Lustre filesystem '%s' not found", name);
1619 dir = opendir(pathname.gl_pathv[0]);
1622 llapi_error(LLAPI_MSG_ERROR, rc,
1623 "Could not open pool list for '%s'",
1630 pool = readdir(dir);
1636 /* ignore . and .. */
1637 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1640 /* check output bounds */
1641 if (nb_entries >= list_size) {
1643 goto free_dir_no_msg;
1646 /* +2 for '.' and final '\0' */
1647 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1650 goto free_dir_no_msg;
1653 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1654 poollist[nb_entries] = buffer + used;
1655 used += strlen(pool->d_name) + strlen(fsname) + 2;
1661 llapi_error(LLAPI_MSG_ERROR, rc,
1662 "Error reading pool list for '%s'", name);
1664 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1669 cfs_free_param_data(&pathname);
1672 return rc != 0 ? rc : nb_entries;
1675 /* wrapper for lfs.c and obd.c */
1676 int llapi_poollist(const char *name)
1678 /* list of pool names (assume that pool count is smaller
1680 char **list, *buffer = NULL, *fsname = (char *)name;
1681 char *poolname = NULL, *tmp = NULL, data[16];
1682 enum param_filter type = FILTER_BY_PATH;
1683 int obdcount, bufsize, rc, nb, i;
1688 if (name[0] != '/') {
1689 fsname = strdup(name);
1693 poolname = strchr(fsname, '.');
1696 type = FILTER_BY_FS_NAME;
1699 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1700 data, sizeof(data));
1703 obdcount = atoi(data);
1705 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1706 * and also an array to store the pointers for all that
1707 * allocated space. */
1709 bufsize = sizeof(struct obd_uuid) * obdcount;
1710 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1711 if (buffer == NULL) {
1715 list = (char **) (buffer + bufsize);
1718 /* name is a path or fsname */
1719 nb = llapi_get_poollist(name, list, obdcount,
1722 /* name is a pool name (<fsname>.<poolname>) */
1723 nb = llapi_get_poolmembers(name, list, obdcount,
1727 if (nb == -EOVERFLOW) {
1730 goto retry_get_pools;
1733 for (i = 0; i < nb; i++)
1734 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1735 rc = (nb < 0 ? nb : 0);
1739 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1744 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1745 void *data, struct dirent64 *de);
1747 #define OBD_NOT_FOUND (-1)
1749 static void find_param_fini(struct find_param *param)
1751 if (param->fp_migrate)
1754 if (param->fp_obd_indexes) {
1755 free(param->fp_obd_indexes);
1756 param->fp_obd_indexes = NULL;
1759 if (param->fp_lmd) {
1760 free(param->fp_lmd);
1761 param->fp_lmd = NULL;
1764 if (param->fp_lmv_md) {
1765 free(param->fp_lmv_md);
1766 param->fp_lmv_md = NULL;
1770 static int common_param_init(struct find_param *param, char *path)
1772 int lum_size = get_mds_md_size(path);
1777 /* migrate has fp_lmv_md initialized outside */
1778 if (param->fp_migrate)
1781 if (lum_size < PATH_MAX + 1)
1782 lum_size = PATH_MAX + 1;
1784 param->fp_lum_size = lum_size;
1785 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1787 if (param->fp_lmd == NULL) {
1788 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1789 "error: allocate %zu bytes for layout failed",
1790 sizeof(lstat_t) + param->fp_lum_size);
1794 param->fp_lmv_stripe_count = 256;
1795 param->fp_lmv_md = calloc(1,
1796 lmv_user_md_size(param->fp_lmv_stripe_count,
1797 LMV_USER_MAGIC_SPECIFIC));
1798 if (param->fp_lmv_md == NULL) {
1799 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1800 "error: allocation of %d bytes for ioctl",
1801 lmv_user_md_size(param->fp_lmv_stripe_count,
1802 LMV_USER_MAGIC_SPECIFIC));
1803 find_param_fini(param);
1807 param->fp_got_uuids = 0;
1808 param->fp_obd_indexes = NULL;
1809 param->fp_obd_index = OBD_NOT_FOUND;
1810 param->fp_mdt_index = OBD_NOT_FOUND;
1814 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1815 struct dirent64 *de)
1817 struct find_param *param = data;
1823 /* set errno upon failure */
1824 static DIR *opendir_parent(const char *path)
1830 path_copy = strdup(path);
1831 if (path_copy == NULL)
1834 parent_path = dirname(path_copy);
1835 parent = opendir(parent_path);
1841 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1846 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1847 if (param->fp_get_default_lmv)
1848 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1850 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1852 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1853 if (errno == E2BIG && ret != 0) {
1857 /* if foreign LMV case, fake stripes number */
1858 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
1859 struct lmv_foreign_md *lfm;
1861 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1862 if (lfm->lfm_length < XATTR_SIZE_MAX -
1863 offsetof(typeof(*lfm), lfm_value)) {
1864 uint32_t size = lfm->lfm_length +
1865 offsetof(typeof(*lfm), lfm_value);
1867 stripe_count = lmv_foreign_to_md_stripes(size);
1869 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1870 "error: invalid %d foreign size "
1871 "returned from ioctl",
1876 stripe_count = param->fp_lmv_md->lum_stripe_count;
1878 if (stripe_count <= param->fp_lmv_stripe_count)
1881 free(param->fp_lmv_md);
1882 param->fp_lmv_stripe_count = stripe_count;
1883 lmv_size = lmv_user_md_size(stripe_count,
1884 LMV_USER_MAGIC_SPECIFIC);
1885 param->fp_lmv_md = malloc(lmv_size);
1886 if (param->fp_lmv_md == NULL) {
1887 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1888 "error: allocation of %d bytes for ioctl",
1889 lmv_user_md_size(param->fp_lmv_stripe_count,
1890 LMV_USER_MAGIC_SPECIFIC));
1898 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1901 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
1902 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
1903 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
1904 lmd_v2->lmd_stx.stx_uid = st->st_uid;
1905 lmd_v2->lmd_stx.stx_gid = st->st_gid;
1906 lmd_v2->lmd_stx.stx_mode = st->st_mode;
1907 lmd_v2->lmd_stx.stx_ino = st->st_ino;
1908 lmd_v2->lmd_stx.stx_size = st->st_size;
1909 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
1910 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
1911 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
1912 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
1913 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
1914 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
1915 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
1916 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
1917 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
1919 lmd_v2->lmd_flags = 0;
1921 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1923 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
1924 if (lmd_v2->lmd_stx.stx_size)
1925 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
1926 if (lmd_v2->lmd_stx.stx_blocks)
1927 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
1929 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1930 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
1931 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
1936 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
1938 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
1939 struct lov_user_mds_data *lmd_v2 = lmdbuf;
1943 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
1947 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
1950 st = lmd_v1->lmd_st;
1951 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
1952 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
1953 convert_lmd_statx(lmd_v2, &st, false);
1954 lmd_v2->lmd_lmmsize = 0;
1955 lmd_v2->lmd_padding = 0;
1960 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
1961 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1963 struct lov_user_mds_data *lmd = lmdbuf;
1964 static bool use_old_ioctl;
1968 if (parent_fd < 0 && dir_fd < 0)
1970 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1974 /* LL_IOC_MDC_GETINFO operates on the current directory inode
1975 * and returns struct lov_user_mds_data, while
1976 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
1978 if (type == GET_LMD_INFO)
1979 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_OLD :
1982 cmd = LL_IOC_LOV_GETSTRIPE;
1985 ret = ioctl(dir_fd, cmd, lmdbuf);
1986 if (ret < 0 && errno == ENOTTY && cmd == LL_IOC_MDC_GETINFO) {
1987 cmd = LL_IOC_MDC_GETINFO_OLD;
1988 use_old_ioctl = true;
1992 if (cmd == LL_IOC_MDC_GETINFO_OLD && !ret)
1993 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
1994 } else if (parent_fd >= 0) {
1995 const char *fname = strrchr(path, '/');
1997 /* IOC_MDC_GETFILEINFO takes as input the filename (relative to
1998 * the parent directory) and returns struct lov_user_mds_data,
1999 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2001 * This avoids opening, locking, and closing each file on the
2002 * client if that is not needed. Multiple of these ioctl() can
2003 * be done on the parent dir with a single open for all
2004 * files in that directory, and it also doesn't pollute the
2005 * client dcache with millions of dentries when traversing
2006 * a large filesystem.
2008 fname = (fname == NULL ? path : fname + 1);
2010 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2013 else if (ret >= lmdlen || ret++ == 0)
2016 if (type == GET_LMD_INFO)
2017 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_OLD :
2018 IOC_MDC_GETFILEINFO;
2020 cmd = IOC_MDC_GETFILESTRIPE;
2023 ret = ioctl(parent_fd, cmd, lmdbuf);
2024 if (ret < 0 && errno == ENOTTY &&
2025 cmd == IOC_MDC_GETFILEINFO) {
2026 cmd = IOC_MDC_GETFILEINFO_OLD;
2027 use_old_ioctl = true;
2028 goto retry_getfileinfo;
2031 if (cmd == IOC_MDC_GETFILEINFO_OLD && !ret)
2032 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2036 if (ret && type == GET_LMD_INFO) {
2037 if (errno == ENOTTY) {
2040 /* ioctl is not supported, it is not a lustre fs.
2041 * Do the regular lstat(2) instead.
2043 ret = lstat_f(path, &st);
2046 llapi_error(LLAPI_MSG_ERROR, ret,
2047 "error: %s: lstat failed for %s",
2051 convert_lmd_statx(lmd, &st, true);
2052 /* It may be wrong to set use_old_ioctl with true as
2053 * the file is not a lustre fs. So reset it with false
2056 use_old_ioctl = false;
2057 } else if (errno == ENOENT) {
2059 llapi_error(LLAPI_MSG_WARN, ret,
2060 "warning: %s does not exist", path);
2061 } else if (errno != EISDIR && errno != ENODATA) {
2063 llapi_error(LLAPI_MSG_ERROR, ret,
2064 "%s ioctl failed for %s.",
2065 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2066 "IOC_MDC_GETFILEINFO", path);
2073 static int get_lmd_info(char *path, DIR *parent, DIR *dir, void *lmdbuf,
2074 int lmdlen, enum get_lmd_info_type type)
2080 parent_fd = dirfd(parent);
2082 dir_fd = dirfd(dir);
2084 return get_lmd_info_fd(path, parent_fd, dir_fd, lmdbuf, lmdlen, type);
2087 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
2088 semantic_func_t sem_init,
2089 semantic_func_t sem_fini, void *data,
2090 struct dirent64 *de)
2092 struct find_param *param = (struct find_param *)data;
2093 struct dirent64 *dent;
2101 if (!d && errno != ENOTDIR) {
2103 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2106 } else if (!d && !parent) {
2107 /* ENOTDIR. Open the parent dir. */
2108 p = opendir_parent(path);
2115 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
2121 while ((dent = readdir64(d)) != NULL) {
2124 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2128 if ((len + dent->d_reclen + 2) > size) {
2129 llapi_err_noerrno(LLAPI_MSG_ERROR,
2130 "error: %s: string buffer too small",
2135 strcat(path, dent->d_name);
2137 if (dent->d_type == DT_UNKNOWN) {
2138 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
2140 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
2141 param->fp_lum_size, GET_LMD_INFO);
2143 dent->d_type = IFTODT(stx->stx_mode);
2150 switch (dent->d_type) {
2152 llapi_err_noerrno(LLAPI_MSG_ERROR,
2153 "error: %s: '%s' is UNKNOWN type %d",
2154 __func__, dent->d_name, dent->d_type);
2157 rc = llapi_semantic_traverse(path, size, d, sem_init,
2158 sem_fini, data, dent);
2159 if (rc != 0 && ret == 0)
2165 rc = sem_init(path, d, NULL, data, dent);
2166 if (rc < 0 && ret == 0) {
2171 if (sem_fini && rc == 0)
2172 sem_fini(path, d, NULL, data, dent);
2180 sem_fini(path, parent, &d, data, de);
2189 static int param_callback(char *path, semantic_func_t sem_init,
2190 semantic_func_t sem_fini, struct find_param *param)
2192 int ret, len = strlen(path);
2195 if (len > PATH_MAX) {
2197 llapi_error(LLAPI_MSG_ERROR, ret,
2198 "Path name '%s' is too long", path);
2202 buf = (char *)malloc(PATH_MAX + 1);
2206 snprintf(buf, PATH_MAX + 1, "%s", path);
2207 ret = common_param_init(param, buf);
2211 param->fp_depth = 0;
2213 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
2214 sem_fini, param, NULL);
2216 find_param_fini(param);
2218 return ret < 0 ? ret : 0;
2221 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2223 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
2227 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2232 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2234 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2237 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2242 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2246 fd = open(path, O_RDONLY | O_NONBLOCK);
2249 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
2253 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2259 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2263 fd = open(path, O_RDONLY | O_NONBLOCK);
2266 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2270 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2282 * If uuidp is NULL, return the number of available obd uuids.
2283 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2284 * there are more OSTs than allocated to uuidp, then an error is returned with
2285 * the ost_count set to number of available obd uuids.
2287 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2288 int *ost_count, enum tgt_type type)
2290 char buf[PATH_MAX], format[32];
2291 int rc = 0, index = 0;
2292 struct obd_uuid name;
2296 /* Get the lov name */
2297 if (type == LOV_TYPE)
2298 rc = llapi_file_fget_lov_uuid(fd, &name);
2300 rc = llapi_file_fget_lmv_uuid(fd, &name);
2304 /* Now get the ost uuids */
2305 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2306 FILTER_BY_EXACT, "target_obd", ¶m);
2310 fp = fopen(param.gl_pathv[0], "r");
2313 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2318 snprintf(format, sizeof(format),
2319 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2320 while (fgets(buf, sizeof(buf), fp) != NULL) {
2321 if (uuidp && (index < *ost_count)) {
2322 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2330 if (uuidp && (index > *ost_count))
2335 cfs_free_param_data(¶m);
2339 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2341 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2344 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2349 root = open(mnt, O_RDONLY | O_DIRECTORY);
2352 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2357 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2365 /* Check if user specified value matches a real uuid. Ignore _UUID,
2366 * -osc-4ba41334, other trailing gunk in comparison.
2367 * @param real_uuid ends in "_UUID"
2368 * @param search_uuid may or may not end in "_UUID"
2370 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2372 int cmplen = strlen(real_uuid);
2373 int searchlen = strlen(search_uuid);
2375 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2377 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2380 /* The UUIDs may legitimately be different lengths, if
2381 * the system was upgraded from an older version. */
2382 if (cmplen != searchlen)
2385 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2388 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2389 * returned in param->fp_obd_index */
2390 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2392 struct obd_uuid obd_uuid;
2399 if (param->fp_got_uuids)
2402 /* Get the lov/lmv name */
2403 if (param->fp_get_lmv)
2404 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2406 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2408 if (rc != -ENOTTY) {
2409 llapi_error(LLAPI_MSG_ERROR, rc,
2410 "error: can't get %s name: %s",
2411 param->fp_get_lmv ? "lmv" : "lov",
2419 param->fp_got_uuids = 1;
2421 /* Now get the ost uuids */
2422 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2423 obd_uuid.uuid, FILTER_BY_EXACT,
2424 "target_obd", ¶m_data);
2428 fp = fopen(param_data.gl_pathv[0], "r");
2431 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2432 param_data.gl_pathv[0]);
2436 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2437 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2438 param->fp_get_lmv ? "MDTS" : "OBDS");
2440 snprintf(format, sizeof(format),
2441 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2442 while (fgets(buf, sizeof(buf), fp) != NULL) {
2445 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2448 if (param->fp_obd_uuid) {
2449 if (llapi_uuid_match(obd_uuid.uuid,
2450 param->fp_obd_uuid->uuid)) {
2451 param->fp_obd_index = index;
2454 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2455 /* Print everything */
2456 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2459 param->fp_obds_printed = 1;
2463 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2464 llapi_err_noerrno(LLAPI_MSG_ERROR,
2465 "error: %s: unknown obduuid: %s",
2466 __func__, param->fp_obd_uuid->uuid);
2470 cfs_free_param_data(¶m_data);
2474 /* In this case, param->fp_obd_uuid will be an array of obduuids and
2475 * obd index for all these obduuids will be returned in
2476 * param->fp_obd_indexes */
2477 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2478 int num_obds, int **obdindexes, int *obdindex,
2481 int ret, obdcount, obd_valid = 0, obdnum;
2483 struct obd_uuid *uuids = NULL;
2487 if (type == LOV_TYPE)
2488 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2490 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2494 obdcount = atoi(buf);
2495 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2500 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2502 if (ret == -EOVERFLOW) {
2503 struct obd_uuid *uuids_temp;
2505 uuids_temp = realloc(uuids, obdcount *
2506 sizeof(struct obd_uuid));
2507 if (uuids_temp != NULL) {
2509 goto retry_get_uuids;
2514 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2518 indexes = malloc(num_obds * sizeof(*obdindex));
2519 if (indexes == NULL) {
2524 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2527 /* The user may have specified a simple index */
2528 i = strtol(obduuids[obdnum].uuid, &end, 0);
2529 if (end && *end == '\0' && i < obdcount) {
2530 indexes[obdnum] = i;
2533 for (i = 0; i < obdcount; i++) {
2534 if (llapi_uuid_match(uuids[i].uuid,
2535 obduuids[obdnum].uuid)) {
2536 indexes[obdnum] = i;
2542 if (i >= obdcount) {
2543 indexes[obdnum] = OBD_NOT_FOUND;
2544 llapi_err_noerrno(LLAPI_MSG_ERROR,
2545 "invalid obduuid '%s'",
2546 obduuids[obdnum].uuid);
2552 *obdindex = OBD_NOT_FOUND;
2554 *obdindex = obd_valid;
2556 *obdindexes = indexes;
2564 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2568 if (param->fp_mdt_uuid) {
2569 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2571 ¶m->fp_mdt_indexes,
2572 ¶m->fp_mdt_index, LMV_TYPE);
2577 if (param->fp_obd_uuid) {
2578 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2580 ¶m->fp_obd_indexes,
2581 ¶m->fp_obd_index, LOV_TYPE);
2586 param->fp_got_uuids = 1;
2591 int llapi_ostlist(char *path, struct find_param *param)
2596 fd = open(path, O_RDONLY | O_DIRECTORY);
2600 ret = setup_obd_uuid(fd, path, param);
2607 * Tries to determine the default stripe attributes for a given filesystem. The
2608 * filesystem to check should be specified by fsname, or will be determined
2611 static int sattr_get_defaults(const char *const fsname,
2612 unsigned int *scount,
2613 unsigned int *ssize,
2614 unsigned int *soffset)
2620 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2621 "stripecount", val, sizeof(val));
2624 *scount = atoi(val);
2628 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2629 "stripesize", val, sizeof(val));
2636 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2637 "stripeoffset", val, sizeof(val));
2640 *soffset = atoi(val);
2647 * Tries to gather the default stripe attributes for a given filesystem. If
2648 * the attributes can be determined, they are cached for easy retreival the
2649 * next time they are needed. Only a single filesystem's attributes are
2652 int sattr_cache_get_defaults(const char *const fsname,
2653 const char *const pathname, unsigned int *scount,
2654 unsigned int *ssize, unsigned int *soffset)
2657 char fsname[PATH_MAX + 1];
2658 unsigned int stripecount;
2659 unsigned int stripesize;
2660 unsigned int stripeoffset;
2666 char fsname_buf[PATH_MAX + 1];
2667 unsigned int tmp[3];
2669 if (fsname == NULL) {
2670 rc = llapi_search_fsname(pathname, fsname_buf);
2674 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2677 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2679 * Ensure all 3 sattrs (count, size, and offset) are
2680 * successfully retrieved and stored in tmp before writing to
2683 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2687 cache.stripecount = tmp[0];
2688 cache.stripesize = tmp[1];
2689 cache.stripeoffset = tmp[2];
2690 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2694 *scount = cache.stripecount;
2696 *ssize = cache.stripesize;
2698 *soffset = cache.stripeoffset;
2703 static char *layout2name(__u32 layout_pattern)
2705 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2707 else if (layout_pattern == LOV_PATTERN_MDT)
2709 else if (layout_pattern == LOV_PATTERN_RAID0)
2711 else if (layout_pattern ==
2712 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2713 return "raid0,overstriped";
2718 enum lov_dump_flags {
2719 LDF_IS_DIR = 0x0001,
2720 LDF_IS_RAW = 0x0002,
2721 LDF_INDENT = 0x0004,
2722 LDF_SKIP_OBJS = 0x0008,
2724 LDF_EXTENSION = 0x0020,
2727 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2728 struct lov_user_ost_data_v1 *objects,
2729 enum llapi_layout_verbose verbose,
2730 int depth, char *pool_name,
2731 enum lov_dump_flags flags)
2733 bool is_dir = flags & LDF_IS_DIR;
2734 bool is_raw = flags & LDF_IS_RAW;
2735 bool indent = flags & LDF_INDENT;
2736 bool yaml = flags & LDF_YAML;
2737 bool skip_objs = flags & LDF_SKIP_OBJS;
2738 bool extension = flags & LDF_EXTENSION;
2739 char *prefix = is_dir ? "" : "lmm_";
2740 char *separator = "";
2741 char *space = indent ? " " : "";
2744 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2745 lmm_oi_set_seq(&lum->lmm_oi, 0);
2746 if (!indent && (verbose & VERBOSE_DETAIL))
2747 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2750 if (!yaml && !indent && depth && path &&
2751 ((verbose != VERBOSE_OBJID) || !is_dir))
2752 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2754 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2755 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2756 space, prefix, lum->lmm_magic);
2757 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2759 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2760 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2762 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2764 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2769 if (verbose & ~VERBOSE_DFID)
2770 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2772 /* This needs a bit of hand-holding since old 1.x lmm_oi
2773 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2774 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2775 * a real FID. Ideally the 2.x code would have stored this
2776 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2777 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2778 * worked properly (especially since IGIF FIDs use mds_inum as
2779 * the FID SEQ), but unfortunately that didn't happen.
2781 * Print it to look like an IGIF FID, even though the fields
2782 * are reversed on disk, so that it makes sense to userspace.
2784 * Don't use ostid_id() and ostid_seq(), since they assume the
2785 * oi_fid fields are in the right order. This is why there are
2786 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2788 * For newer layout types hopefully this will be a real FID. */
2789 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2790 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2791 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2792 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2793 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2795 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2796 (unsigned long long)seq, oid, ver);
2798 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2799 (unsigned long long)seq, oid, ver);
2802 if (verbose & VERBOSE_STRIPE_COUNT) {
2803 if (verbose & ~VERBOSE_STRIPE_COUNT)
2804 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2807 if (!is_raw && lum->lmm_stripe_count == 0 &&
2808 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2809 unsigned int scount;
2810 rc = sattr_cache_get_defaults(NULL, path,
2814 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2817 llapi_error(LLAPI_MSG_ERROR, rc,
2818 "Cannot determine default"
2821 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2823 (__s16)lum->lmm_stripe_count);
2826 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2828 (__s16)lum->lmm_stripe_count);
2830 if (!yaml && is_dir)
2836 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
2837 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
2838 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2839 if (verbose & ~VERBOSE_EXT_SIZE && extension)
2840 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
2842 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
2843 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2845 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2847 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2850 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2852 llapi_error(LLAPI_MSG_ERROR, rc,
2853 "Cannot determine default"
2856 /* Extension size is in KiB */
2857 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2859 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
2860 (unsigned long long)lum->lmm_stripe_size);
2862 if (!yaml && is_dir)
2868 if ((verbose & VERBOSE_PATTERN)) {
2869 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2870 if (verbose & ~VERBOSE_PATTERN)
2871 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2873 if (lov_pattern_supported(lum->lmm_pattern))
2874 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2875 layout2name(lum->lmm_pattern));
2877 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2878 separator = (!yaml && is_dir) ? " " : "\n";
2881 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2882 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2883 if (verbose & ~VERBOSE_GENERATION)
2884 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2886 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2887 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2891 if (verbose & VERBOSE_STRIPE_OFFSET) {
2892 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2893 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2894 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2896 if (is_dir || skip_objs)
2897 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2898 lum->lmm_stripe_offset ==
2899 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2900 lum->lmm_stripe_offset);
2901 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
2902 llapi_printf(LLAPI_MSG_NORMAL, "0");
2904 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2905 objects[0].l_ost_idx);
2906 if (!yaml && is_dir)
2912 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2913 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2914 if (verbose & ~VERBOSE_POOL)
2915 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2917 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2918 if (!yaml && is_dir)
2924 if (strlen(separator) != 0)
2925 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2928 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2929 struct lov_user_ost_data_v1 *objects,
2930 char *path, int obdindex, int depth,
2931 enum llapi_layout_verbose verbose,
2932 enum lov_dump_flags flags)
2934 bool is_dir = flags & LDF_IS_DIR;
2935 bool indent = flags & LDF_INDENT;
2936 bool skip_objs = flags & LDF_SKIP_OBJS;
2937 bool yaml = flags & LDF_YAML;
2938 bool obdstripe = obdindex == OBD_NOT_FOUND;
2941 if (!obdstripe && !skip_objs) {
2942 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2943 if (obdindex == objects[i].l_ost_idx) {
2953 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2956 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
2957 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2958 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
2959 char *space = " - ";
2962 llapi_printf(LLAPI_MSG_NORMAL,
2963 "%6slmm_objects:\n", " ");
2965 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2967 llapi_printf(LLAPI_MSG_NORMAL,
2968 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2970 for (i = 0; i < lum->lmm_stripe_count; i++) {
2971 int idx = objects[i].l_ost_idx;
2972 long long oid = ostid_id(&objects[i].l_ost_oi);
2973 long long gr = ostid_seq(&objects[i].l_ost_oi);
2975 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2979 struct lu_fid fid = { 0 };
2981 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2982 llapi_printf(LLAPI_MSG_NORMAL,
2983 "%sl_ost_idx: %d\n", space, idx);
2984 llapi_printf(LLAPI_MSG_NORMAL,
2985 "%8sl_fid: "DFID_NOBRACE"\n",
2987 } else if (indent) {
2988 struct lu_fid fid = { 0 };
2990 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2991 llapi_printf(LLAPI_MSG_NORMAL,
2992 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
2993 space, i, idx, PFID(&fid));
2996 sprintf(fmt, "%s%s%s\n",
2997 "\t%6u\t%14llu\t%#13llx\t",
2998 (fid_seq_is_rsvd(gr) ||
2999 fid_seq_is_mdt0(gr)) ?
3000 "%14llu" : "%#14llx", "%s");
3001 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
3003 obdindex == idx ? " *" : "");
3007 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3010 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3011 char *path, int obdindex, int depth,
3012 enum llapi_layout_verbose verbose,
3013 enum lov_dump_flags flags)
3015 struct lmv_user_mds_data *objects = lum->lum_objects;
3016 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3017 char *separator = "";
3018 bool yaml = flags & LDF_YAML;
3019 bool obdstripe = false;
3022 if (obdindex != OBD_NOT_FOUND) {
3023 if (lum->lum_stripe_count == 0) {
3024 if (obdindex == lum->lum_stripe_offset)
3027 for (i = 0; i < lum->lum_stripe_count; i++) {
3028 if (obdindex == objects[i].lum_mds) {
3029 llapi_printf(LLAPI_MSG_NORMAL,
3044 /* show all information default */
3046 if (lum->lum_magic == LMV_USER_MAGIC)
3047 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3048 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3050 verbose = VERBOSE_OBJID;
3053 if (depth && path && ((verbose != VERBOSE_OBJID)))
3054 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3056 if (verbose & VERBOSE_STRIPE_COUNT) {
3057 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3058 if (verbose & ~VERBOSE_STRIPE_COUNT)
3059 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3060 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3061 (int)lum->lum_stripe_count);
3062 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3068 if (verbose & VERBOSE_STRIPE_OFFSET) {
3069 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3070 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3071 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3072 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3073 (int)lum->lum_stripe_offset);
3074 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3080 if (verbose & VERBOSE_HASH_TYPE) {
3081 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3082 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3084 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3085 if (verbose & ~VERBOSE_HASH_TYPE)
3086 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3087 if (type < LMV_HASH_TYPE_MAX)
3088 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3089 mdt_hash_name[type]);
3091 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3093 if (flags & LMV_HASH_FLAG_MIGRATION)
3094 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3095 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3096 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3097 if (flags & LMV_HASH_FLAG_LOST_LMV)
3098 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3103 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3104 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3105 if (lum->lum_stripe_count > 0)
3106 llapi_printf(LLAPI_MSG_NORMAL,
3107 "mdtidx\t\t FID[seq:oid:ver]\n");
3108 for (i = 0; i < lum->lum_stripe_count; i++) {
3109 int idx = objects[i].lum_mds;
3110 struct lu_fid *fid = &objects[i].lum_fid;
3111 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3112 llapi_printf(LLAPI_MSG_NORMAL,
3113 "%6u\t\t "DFID"\t\t%s\n",
3115 obdindex == idx ? " *" : "");
3120 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3121 pool_name[0] != '\0') {
3122 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3123 if (verbose & ~VERBOSE_POOL)
3124 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3126 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3130 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3131 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3134 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3135 enum lov_dump_flags flags)
3137 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3138 int depth = param->fp_max_depth;
3139 enum llapi_layout_verbose verbose = param->fp_verbose;
3140 bool yaml = flags & LDF_YAML;
3142 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3143 !(flags & LDF_IS_DIR)) && !yaml)
3144 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3146 if (verbose & VERBOSE_DETAIL) {
3147 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3148 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3149 " ", comp_v1->lcm_magic);
3150 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3151 " ", comp_v1->lcm_size);
3152 if (flags & LDF_IS_DIR)
3153 llapi_printf(LLAPI_MSG_NORMAL,
3154 "%2slcm_flags: %s\n", " ",
3155 comp_v1->lcm_mirror_count > 0 ?
3158 llapi_printf(LLAPI_MSG_NORMAL,
3159 "%2slcm_flags: %s\n", " ",
3160 llapi_layout_flags_string(comp_v1->lcm_flags));
3163 if (verbose & VERBOSE_GENERATION) {
3164 if (verbose & ~VERBOSE_GENERATION)
3165 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3167 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3170 if (verbose & VERBOSE_MIRROR_COUNT) {
3171 if (verbose & ~VERBOSE_MIRROR_COUNT)
3172 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3174 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3175 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3176 comp_v1->lcm_mirror_count + 1 : 1);
3179 if (verbose & VERBOSE_COMP_COUNT) {
3180 if (verbose & ~VERBOSE_COMP_COUNT)
3181 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3183 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3184 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3185 comp_v1->lcm_entry_count : 0);
3188 if (verbose & VERBOSE_DETAIL && !yaml)
3189 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3192 static void lcme_flags2str(__u32 comp_flags)
3198 llapi_printf(LLAPI_MSG_NORMAL, "0");
3201 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3202 if (comp_flags & comp_flags_table[i].cfn_flag) {
3204 llapi_printf(LLAPI_MSG_NORMAL, ",");
3205 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3206 comp_flags_table[i].cfn_name);
3207 comp_flags &= ~comp_flags_table[i].cfn_flag;
3213 llapi_printf(LLAPI_MSG_NORMAL, ",");
3214 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3218 static void lov_dump_comp_v1_entry(struct find_param *param,
3219 enum lov_dump_flags flags, int index)
3221 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3222 struct lov_comp_md_entry_v1 *entry;
3223 char *separator = "";
3224 enum llapi_layout_verbose verbose = param->fp_verbose;
3225 bool yaml = flags & LDF_YAML;
3227 entry = &comp_v1->lcm_entries[index];
3230 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3232 if (verbose & VERBOSE_COMP_ID) {
3233 if (verbose & VERBOSE_DETAIL && !yaml)
3234 llapi_printf(LLAPI_MSG_NORMAL,
3235 "%slcme_id: ", " - ");
3236 else if (verbose & ~VERBOSE_COMP_ID)
3237 llapi_printf(LLAPI_MSG_NORMAL,
3238 "%4slcme_id: ", " ");
3239 if (entry->lcme_id != LCME_ID_INVAL)
3240 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3242 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3246 if (verbose & VERBOSE_MIRROR_ID) {
3247 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3248 if (verbose & ~VERBOSE_MIRROR_ID)
3249 llapi_printf(LLAPI_MSG_NORMAL,
3250 "%4slcme_mirror_id: ", " ");
3251 if (entry->lcme_id != LCME_ID_INVAL)
3252 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3253 mirror_id_of(entry->lcme_id));
3255 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3259 if (verbose & VERBOSE_COMP_FLAGS) {
3260 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3261 if (verbose & ~VERBOSE_COMP_FLAGS)
3262 llapi_printf(LLAPI_MSG_NORMAL,
3263 "%4slcme_flags: ", " ");
3264 lcme_flags2str(entry->lcme_flags);
3267 /* print snapshot timestamp if its a nosync comp */
3268 if ((verbose & VERBOSE_COMP_FLAGS) &&
3269 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3270 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3271 if (verbose & ~VERBOSE_COMP_FLAGS)
3272 llapi_printf(LLAPI_MSG_NORMAL,
3273 "%4slcme_timestamp: ", " ");
3275 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3276 (unsigned long long)entry->lcme_timestamp);
3278 time_t stamp = entry->lcme_timestamp;
3279 char *date_str = asctime(localtime(&stamp));
3281 date_str[strlen(date_str) - 1] = '\0';
3282 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3288 if (verbose & VERBOSE_COMP_START) {
3289 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3290 if (verbose & ~VERBOSE_COMP_START)
3291 llapi_printf(LLAPI_MSG_NORMAL,
3292 "%4slcme_extent.e_start: ", " ");
3293 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3294 (unsigned long long)entry->lcme_extent.e_start);
3298 if (verbose & VERBOSE_COMP_END) {
3299 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3300 if (verbose & ~VERBOSE_COMP_END)
3301 llapi_printf(LLAPI_MSG_NORMAL,
3302 "%4slcme_extent.e_end: ", " ");
3303 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3304 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3306 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3307 (unsigned long long)entry->lcme_extent.e_end);
3312 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3313 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3314 } else if (verbose & VERBOSE_DETAIL) {
3315 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3316 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3317 " ", entry->lcme_offset);
3318 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3319 " ", entry->lcme_size);
3320 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3322 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3326 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3327 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3329 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3330 * The table below gives the answers for the specified parameters (value and
3331 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3332 * --------------------------------------
3333 * 1 | file > limit; sign > 0 | -1 / -1 |
3334 * 2 | file = limit; sign > 0 | -1 / -1 |
3335 * 3 | file < limit; sign > 0 | ? / 1 |
3336 * 4 | file > limit; sign = 0 | -1 / -1 |
3337 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3338 * 6 | file < limit; sign = 0 | ? / -1 |
3339 * 7 | file > limit; sign < 0 | 1 / 1 |
3340 * 8 | file = limit; sign < 0 | ? / -1 |
3341 * 9 | file < limit; sign < 0 | ? / -1 |
3342 * --------------------------------------
3343 * Note: 5th actually means that the value is within the interval
3344 * (limit - margin, limit]. */
3345 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3346 int sign, int negopt, unsigned long long margin,
3352 /* Drop the fraction of margin (of days or size). */
3353 if (file + margin <= limit)
3355 } else if (sign == 0) {
3356 if (file <= limit && file + margin > limit)
3358 else if (file + margin <= limit)
3360 } else if (sign < 0) {
3367 return negopt ? ~ret + 1 : ret;
3370 static inline struct lov_user_md *
3371 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3373 return (struct lov_user_md *)((char *)comp_v1 +
3374 comp_v1->lcm_entries[ent_idx].lcme_offset);
3377 static inline struct lov_user_ost_data_v1 *
3378 lov_v1v3_objects(struct lov_user_md *v1)
3380 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3381 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3383 return v1->lmm_objects;
3387 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3389 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3390 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3391 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3393 pool_name[0] = '\0';
3397 print_last_init_comp(struct find_param *param)
3399 /* print all component info */
3400 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3403 /* print specific component info */
3404 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3405 param->fp_check_comp_start || param->fp_check_comp_end ||
3406 param->fp_check_mirror_id || param->fp_check_mirror_index)
3412 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3416 if (param->fp_comp_end == LUSTRE_EOF) {
3417 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3418 match = end == LUSTRE_EOF ? 1 : -1;
3419 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3420 match = end == LUSTRE_EOF ? -1 : 1;
3421 else /* at least EOF */
3423 if (param->fp_exclude_comp_end)
3426 unsigned long long margin;
3428 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3429 match = find_value_cmp(end, param->fp_comp_end,
3430 param->fp_comp_end_sign,
3431 param->fp_exclude_comp_end, margin, 0);
3438 * An example of "getstripe -v" for a two components PFL file:
3441 * lcm_magic: 0x0BD60BD0
3445 * lcm_entry_count: 2
3449 * lcme_extent.e_start: 0
3450 * lcme_extent.e_end: 1048576
3454 * lmm_magic: 0x0BD10BD0
3455 * lmm_seq: 0x200000401
3456 * lmm_object_id: 0x1
3457 * lmm_fid: [0x200000401:0x1:0x0]
3458 * lmm_stripe_count: 1
3459 * lmm_stripe_size: 1048576
3460 * lmm_pattern: raid0
3462 * lmm_stripe_offset: 0
3464 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3468 * lcme_extent.e_start: 1048576
3469 * lcme_extent.e_end: EOF
3473 * lmm_magic: 0x0BD10BD0
3474 * lmm_seq: 0x200000401
3475 * lmm_object_id: 0x1
3476 * lmm_fid: [0x200000401:0x1:0x0]
3477 * lmm_stripe_count: 2
3478 * lmm_stripe_size: 1048576
3479 * lmm_pattern: raid0
3481 * lmm_stripe_offset: 1
3483 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3484 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3486 static void lov_dump_comp_v1(struct find_param *param, char *path,
3487 enum lov_dump_flags flags)
3489 struct lov_comp_md_entry_v1 *entry;
3490 struct lov_user_ost_data_v1 *objects;
3491 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3492 struct lov_user_md_v1 *v1;
3493 char pool_name[LOV_MAXPOOLNAME + 1];
3494 int obdindex = param->fp_obd_index;
3495 int i, j, match, ext;
3496 bool obdstripe = false;
3497 __u16 mirror_index = 0;
3498 __u16 mirror_id = 0;
3500 if (obdindex != OBD_NOT_FOUND) {
3501 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3502 i < comp_v1->lcm_entry_count; i++) {
3503 if (!(comp_v1->lcm_entries[i].lcme_flags &
3507 v1 = lov_comp_entry(comp_v1, i);
3508 objects = lov_v1v3_objects(v1);
3510 for (j = 0; j < v1->lmm_stripe_count; j++) {
3511 if (obdindex == objects[j].l_ost_idx) {
3524 lov_dump_comp_v1_header(param, path, flags);
3526 flags |= LDF_INDENT;
3528 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3529 entry = &comp_v1->lcm_entries[i];
3531 if (param->fp_check_comp_flags) {
3532 if (((param->fp_comp_flags & entry->lcme_flags) !=
3533 param->fp_comp_flags) ||
3534 (param->fp_comp_neg_flags & entry->lcme_flags))
3538 if (param->fp_check_comp_id &&
3539 param->fp_comp_id != entry->lcme_id)
3542 if (param->fp_check_comp_start) {
3543 match = find_value_cmp(entry->lcme_extent.e_start,
3544 param->fp_comp_start,
3545 param->fp_comp_start_sign,
3547 param->fp_comp_start_units, 0);
3552 if (param->fp_check_comp_end) {
3553 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3559 if (param->fp_check_mirror_index) {
3560 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3562 mirror_id = mirror_id_of(entry->lcme_id);
3565 match = find_value_cmp(mirror_index,
3566 param->fp_mirror_index,
3567 param->fp_mirror_index_sign,
3568 param->fp_exclude_mirror_index,
3572 } else if (param->fp_check_mirror_id) {
3573 if (mirror_id != mirror_id_of(entry->lcme_id))
3574 mirror_id = mirror_id_of(entry->lcme_id);
3576 match = find_value_cmp(mirror_id,
3577 param->fp_mirror_id,
3578 param->fp_mirror_id_sign,
3579 param->fp_exclude_mirror_id,
3585 if (print_last_init_comp(param)) {
3587 * if part of stripe info is needed, we'd print only
3588 * the last instantiated component info.
3590 if (entry->lcme_flags & LCME_FL_INIT)
3593 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3594 if (entry->lcme_flags & LCME_FL_EXTENSION)
3595 /* moved back below */
3603 if (entry->lcme_flags & LCME_FL_INIT) {
3604 if (obdindex != OBD_NOT_FOUND) {
3605 flags |= LDF_SKIP_OBJS;
3606 v1 = lov_comp_entry(comp_v1, i);
3607 objects = lov_v1v3_objects(v1);
3609 for (j = 0; j < v1->lmm_stripe_count; j++) {
3610 if (obdindex == objects[j].l_ost_idx) {
3611 flags &= ~LDF_SKIP_OBJS;
3616 flags &= ~LDF_SKIP_OBJS;
3619 flags |= LDF_SKIP_OBJS;
3622 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3624 lov_dump_comp_v1_entry(param, flags, i);
3626 v1 = lov_comp_entry(comp_v1, i);
3627 objects = lov_v1v3_objects(v1);
3628 lov_v1v3_pool_name(v1, pool_name);
3630 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3631 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3632 param->fp_max_depth, param->fp_verbose,
3635 if (print_last_init_comp(param)) {
3637 * directory layout contains only layout template, print the
3641 i = comp_v1->lcm_entry_count - 1;
3644 flags &= ~LDF_SKIP_OBJS;
3646 lov_dump_comp_v1_entry(param, flags, i);
3648 v1 = lov_comp_entry(comp_v1, i);
3649 objects = lov_v1v3_objects(v1);
3650 lov_v1v3_pool_name(v1, pool_name);
3652 entry = &comp_v1->lcm_entries[i];
3653 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3654 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3655 param->fp_max_depth, param->fp_verbose,
3660 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3661 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3664 static inline bool has_any_comp_options(struct find_param *param)
3666 enum llapi_layout_verbose verbose = param->fp_verbose;
3668 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3669 param->fp_check_comp_start || param->fp_check_comp_end ||
3670 param->fp_check_comp_flags)
3673 /* show full layout information, not component specific */
3674 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3677 return verbose & VERBOSE_COMP_OPTS;
3680 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3683 struct lov_user_md *lum = &orig->lmd_lmm;
3684 struct lov_user_mds_data *new;
3685 struct lov_comp_md_v1 *comp_v1;
3686 struct lov_comp_md_entry_v1 *ent;
3687 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3688 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3691 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3693 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3697 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3698 + sizeof(new->lmd_lmmsize));
3700 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3701 comp_v1->lcm_magic = lum->lmm_magic;
3702 comp_v1->lcm_size = lum_off + lum_size;
3703 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3704 comp_v1->lcm_flags = 0;
3705 comp_v1->lcm_entry_count = 1;
3707 ent = &comp_v1->lcm_entries[0];
3709 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3710 ent->lcme_extent.e_start = 0;
3711 ent->lcme_extent.e_end = LUSTRE_EOF;
3712 ent->lcme_offset = lum_off;
3713 ent->lcme_size = lum_size;
3715 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3720 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3721 enum lov_dump_flags flags)
3723 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3725 if (has_any_comp_options(param)) {
3726 struct lov_user_mds_data *new_lmd, *orig_lmd;
3728 orig_lmd = param->fp_lmd;
3729 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3730 if (new_lmd != NULL) {
3731 param->fp_lmd = new_lmd;
3732 lov_dump_comp_v1(param, path, flags);
3733 param->fp_lmd = orig_lmd;
3739 if (magic == LOV_USER_MAGIC_V1) {
3740 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3741 param->fp_lmd->lmd_lmm.lmm_objects,
3742 path, param->fp_obd_index,
3743 param->fp_max_depth, param->fp_verbose,
3746 char pool_name[LOV_MAXPOOLNAME + 1];
3747 struct lov_user_ost_data_v1 *objects;
3748 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3750 snprintf(pool_name, sizeof(pool_name), "%s",
3751 lmmv3->lmm_pool_name);
3752 objects = lmmv3->lmm_objects;
3753 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3754 objects, path, param->fp_obd_index,
3755 param->fp_max_depth, param->fp_verbose,
3760 static uint32_t check_foreign_type(uint32_t foreign_type)
3764 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3765 if (lu_foreign_types[i].lft_name == NULL)
3767 if (foreign_type == lu_foreign_types[i].lft_type)
3771 return LU_FOREIGN_TYPE_UNKNOWN;
3774 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3775 enum lov_dump_flags flags)
3777 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3778 bool yaml = flags & LDF_YAML;
3780 if (!yaml && param->fp_depth && path)
3781 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3783 if (param->fp_verbose & VERBOSE_DETAIL) {
3784 uint32_t type = check_foreign_type(lfm->lfm_type);
3786 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3788 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3790 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3792 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3793 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3794 lu_foreign_types[type].lft_name);
3796 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3798 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3801 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3802 lfm->lfm_length, lfm->lfm_value);
3803 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3806 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3807 enum lov_dump_flags flags)
3809 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3810 bool yaml = flags & LDF_YAML;
3812 if (!yaml && param->fp_depth && path)
3813 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3815 if (param->fp_verbose & VERBOSE_DETAIL) {
3816 uint32_t type = check_foreign_type(lfm->lfm_type);
3818 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3820 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3822 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3824 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3825 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3826 lu_foreign_types[type].lft_name);
3828 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3830 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3833 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3834 lfm->lfm_length, lfm->lfm_value);
3835 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3838 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3839 enum lov_dump_flags flags)
3843 if (param->fp_get_lmv || param->fp_get_default_lmv)
3844 magic = (__u32)param->fp_lmv_md->lum_magic;
3846 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3849 flags |= LDF_IS_RAW;
3854 case LOV_USER_MAGIC_V1:
3855 case LOV_USER_MAGIC_V3:
3856 case LOV_USER_MAGIC_SPECIFIC:
3857 lov_dump_plain_user_lmm(param, path, flags);
3859 case LOV_USER_MAGIC_FOREIGN:
3860 lov_dump_foreign_lmm(param, path, flags);
3863 case LMV_USER_MAGIC: {
3864 char pool_name[LOV_MAXPOOLNAME + 1];
3865 struct lmv_user_md *lum;
3867 lum = (struct lmv_user_md *)param->fp_lmv_md;
3868 snprintf(pool_name, sizeof(pool_name), "%s",
3869 lum->lum_pool_name);
3870 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3871 param->fp_max_depth, param->fp_verbose,
3875 case LOV_USER_MAGIC_COMP_V1:
3876 lov_dump_comp_v1(param, path, flags);
3878 case LMV_MAGIC_FOREIGN:
3879 lmv_dump_foreign_lmm(param, path, flags);
3882 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
3883 "(expecting one of %#x %#x %#x %#x)\n",
3884 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3885 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3886 LMV_USER_MAGIC, LMV_MAGIC_V1);
3891 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3897 fname = strrchr(path, '/');
3899 /* It should be a file (or other non-directory) */
3900 if (fname == NULL) {
3901 dname = (char *)malloc(2);
3905 fname = (char *)path;
3907 dname = (char *)malloc(fname - path + 1);
3910 strncpy(dname, path, fname - path);
3911 dname[fname - path] = '\0';
3915 fd = open(dname, O_RDONLY | O_NONBLOCK);
3921 strcpy((char *)lum, fname);
3922 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3925 if (close(fd) == -1 && rc == 0)
3933 int llapi_file_lookup(int dirfd, const char *name)
3935 struct obd_ioctl_data data = { 0 };
3940 if (dirfd < 0 || name == NULL)
3943 data.ioc_version = OBD_IOCTL_VERSION;
3944 data.ioc_len = sizeof(data);
3945 data.ioc_inlbuf1 = (char *)name;
3946 data.ioc_inllen1 = strlen(name) + 1;
3948 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3950 llapi_error(LLAPI_MSG_ERROR, rc,
3951 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3956 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
3962 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
3963 * Return -1 or 1 if file timestamp does not or does match the given criteria
3964 * correspondingly. Return 0 if the MDS time is being checked and there are
3965 * attributes on OSTs and it is not yet clear if the timespamp matches.
3967 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
3968 * updated timestamps. */
3969 static int find_time_check(lstatx_t *stx, struct find_param *param, int mds)
3974 /* Check if file is accepted. */
3975 if (param->fp_atime) {
3976 rc2 = find_value_cmp(stx->stx_atime.tv_sec, param->fp_atime,
3977 param->fp_asign, param->fp_exclude_atime,
3978 param->fp_time_margin, mds);
3984 if (param->fp_mtime) {
3985 rc2 = find_value_cmp(stx->stx_mtime.tv_sec, param->fp_mtime,
3986 param->fp_msign, param->fp_exclude_mtime,
3987 param->fp_time_margin, mds);
3991 /* If the previous check matches, but this one is not yet clear,
3992 * we should return 0 to do an RPC on OSTs. */
3997 if (param->fp_ctime) {
3998 rc2 = find_value_cmp(stx->stx_ctime.tv_sec, param->fp_ctime,
3999 param->fp_csign, param->fp_exclude_ctime,
4000 param->fp_time_margin, mds);
4004 /* If the previous check matches, but this one is not yet clear,
4005 * we should return 0 to do an RPC on OSTs. */
4013 static int find_newerxy_check(lstatx_t *stx, struct find_param *param, int mds)
4019 for (i = 0; i < 2; i++) {
4020 /* Check if file is accepted. */
4021 if (param->fp_newery[NEWERXY_ATIME][i]) {
4022 rc2 = find_value_cmp(stx->stx_atime.tv_sec,
4023 param->fp_newery[NEWERXY_ATIME][i],
4030 if (param->fp_newery[NEWERXY_MTIME][i]) {
4031 rc2 = find_value_cmp(stx->stx_mtime.tv_sec,
4032 param->fp_newery[NEWERXY_MTIME][i],
4038 * If the previous check matches, but this one is not
4039 * yet clear, we should return 0 to do an RPC on OSTs.
4045 if (param->fp_newery[NEWERXY_CTIME][i]) {
4046 rc2 = find_value_cmp(stx->stx_ctime.tv_sec,
4047 param->fp_newery[NEWERXY_CTIME][i],
4053 * If the previous check matches, but this one is not
4054 * yet clear, we should return 0 to do an RPC on OSTs.
4065 * Check whether the stripes matches the indexes user provided
4069 static int check_obd_match(struct find_param *param)
4071 struct lov_user_ost_data_v1 *objects;
4072 struct lov_comp_md_v1 *comp_v1 = NULL;
4073 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4074 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
4075 int i, j, k, count = 1;
4077 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4080 if (!S_ISREG(stx->stx_mode))
4083 /* exclude foreign */
4084 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4085 return param->fp_exclude_obd;
4087 /* Only those files should be accepted, which have a
4088 * stripe on the specified OST. */
4089 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4090 comp_v1 = (struct lov_comp_md_v1 *)v1;
4091 count = comp_v1->lcm_entry_count;
4094 for (i = 0; i < count; i++) {
4096 v1 = lov_comp_entry(comp_v1, i);
4098 objects = lov_v1v3_objects(v1);
4100 for (j = 0; j < v1->lmm_stripe_count; j++) {
4101 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4104 for (k = 0; k < param->fp_num_obds; k++) {
4105 if (param->fp_obd_indexes[k] ==
4106 objects[j].l_ost_idx)
4107 return !param->fp_exclude_obd;
4112 return param->fp_exclude_obd;
4115 static int check_mdt_match(struct find_param *param)
4119 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4122 /* FIXME: For striped dir, we should get stripe information and check */
4123 for (i = 0; i < param->fp_num_mdts; i++) {
4124 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4125 return !param->fp_exclude_mdt;
4128 if (param->fp_exclude_mdt)
4135 * Check whether the obd is active or not, if it is
4136 * not active, just print the object affected by this
4139 static int print_failed_tgt(struct find_param *param, char *path, int type)
4141 struct obd_statfs stat_buf;
4142 struct obd_uuid uuid_buf;
4145 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV)
4148 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4149 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4150 ret = llapi_obd_statfs(path, type,
4151 param->fp_obd_index, &stat_buf,
4154 llapi_error(LLAPI_MSG_NORMAL, ret, "obd_uuid: %s failed",
4155 param->fp_obd_uuid->uuid);
4160 static int find_check_stripe_size(struct find_param *param)
4162 struct lov_comp_md_v1 *comp_v1 = NULL;
4163 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4164 __u32 stripe_size = 0;
4165 int ret, i, count = 1;
4167 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4168 return param->fp_exclude_stripe_size ? 1 : -1;
4170 ret = param->fp_exclude_stripe_size ? 1 : -1;
4171 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4172 comp_v1 = (struct lov_comp_md_v1 *)v1;
4173 count = comp_v1->lcm_entry_count;
4176 for (i = 0; i < count; i++) {
4177 struct lov_comp_md_entry_v1 *ent;
4180 v1 = lov_comp_entry(comp_v1, i);
4182 ent = &comp_v1->lcm_entries[i];
4183 if (ent->lcme_flags & LCME_FL_EXTENSION)
4185 if (!(ent->lcme_flags & LCME_FL_INIT))
4188 stripe_size = v1->lmm_stripe_size;
4191 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4192 param->fp_stripe_size_sign,
4193 param->fp_exclude_stripe_size,
4194 param->fp_stripe_size_units, 0);
4199 static int find_check_ext_size(struct find_param *param)
4201 struct lov_comp_md_v1 *comp_v1;
4202 struct lov_user_md_v1 *v1;
4205 ret = param->fp_exclude_ext_size ? 1 : -1;
4206 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4207 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4210 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4211 struct lov_comp_md_entry_v1 *ent;
4213 v1 = lov_comp_entry(comp_v1, i);
4215 ent = &comp_v1->lcm_entries[i];
4216 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4219 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4220 param->fp_ext_size_sign,
4221 param->fp_exclude_ext_size,
4222 param->fp_ext_size_units, 0);
4223 /* If any ext_size matches */
4231 static __u32 find_get_stripe_count(struct find_param *param)
4233 struct lov_comp_md_v1 *comp_v1 = NULL;
4234 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4236 __u32 stripe_count = 0;
4238 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4241 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4242 comp_v1 = (struct lov_comp_md_v1 *)v1;
4243 count = comp_v1->lcm_entry_count;
4246 for (i = 0; i < count; i++) {
4248 struct lov_comp_md_entry_v1 *ent;
4250 v1 = lov_comp_entry(comp_v1, i);
4252 ent = &comp_v1->lcm_entries[i];
4253 if (!(ent->lcme_flags & LCME_FL_INIT))
4256 if (ent->lcme_flags & LCME_FL_EXTENSION)
4259 stripe_count = v1->lmm_stripe_count;
4262 return stripe_count;
4265 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4267 static int find_check_layout(struct find_param *param)
4269 struct lov_comp_md_v1 *comp_v1 = NULL;
4270 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4272 bool found = false, valid = false;
4274 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4275 comp_v1 = (struct lov_comp_md_v1 *)v1;
4276 count = comp_v1->lcm_entry_count;
4279 for (i = 0; i < count; i++) {
4281 v1 = lov_comp_entry(comp_v1, i);
4283 /* foreign file have a special magic but no pattern field */
4284 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4287 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4291 if (v1->lmm_pattern & param->fp_layout) {
4300 if ((found && !param->fp_exclude_layout) ||
4301 (!found && param->fp_exclude_layout))
4307 /* if no type specified, check/exclude all foreign
4308 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4310 static int find_check_foreign(struct find_param *param)
4312 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4313 struct lov_foreign_md *lfm;
4315 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4316 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4317 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4318 return param->fp_exclude_foreign ? 1 : -1;
4321 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4322 lfm->lfm_type == param->fp_foreign_type)
4323 return param->fp_exclude_foreign ? -1 : 1;
4324 return param->fp_exclude_foreign ? 1 : -1;
4328 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4329 struct lmv_foreign_md *lfm;
4331 lfm = (void *)param->fp_lmv_md;
4332 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
4333 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4334 return param->fp_exclude_foreign ? 1 : -1;
4337 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4338 lfm->lfm_type == param->fp_foreign_type)
4339 return param->fp_exclude_foreign ? -1 : 1;
4340 return param->fp_exclude_foreign ? 1 : -1;
4346 static int find_check_pool(struct find_param *param)
4348 struct lov_comp_md_v1 *comp_v1 = NULL;
4349 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4350 struct lov_user_md_v3 *v3 = (void *)v1;
4354 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4355 comp_v1 = (struct lov_comp_md_v1 *)v1;
4356 count = comp_v1->lcm_entry_count;
4357 /* empty requested pool is taken as no pool search */
4358 if (count == 0 && param->fp_poolname[0] == '\0')
4362 for (i = 0; i < count; i++) {
4363 if (comp_v1 != NULL)
4364 v1 = lov_comp_entry(comp_v1, i);
4366 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4369 if (((v1->lmm_magic == LOV_USER_MAGIC_V1) &&
4370 (param->fp_poolname[0] == '\0')) ||
4371 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4372 (strncmp(v3->lmm_pool_name,
4373 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4374 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4375 (strcmp(param->fp_poolname, "*") == 0))) {
4381 if ((found && !param->fp_exclude_pool) ||
4382 (!found && param->fp_exclude_pool))
4388 static int find_check_comp_options(struct find_param *param)
4390 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
4391 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4392 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4393 struct lov_comp_md_entry_v1 *entry;
4396 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4399 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4400 comp_v1 = (struct lov_comp_md_v1 *)v1;
4402 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4403 if (forged_v1 == NULL)
4405 comp_v1 = forged_v1;
4406 comp_v1->lcm_entry_count = 1;
4407 entry = &comp_v1->lcm_entries[0];
4408 entry->lcme_flags = S_ISDIR(stx->stx_mode) ? 0 : LCME_FL_INIT;
4409 entry->lcme_extent.e_start = 0;
4410 entry->lcme_extent.e_end = LUSTRE_EOF;
4413 /* invalid case, don't match for any kind of search. */
4414 if (comp_v1->lcm_entry_count == 0) {
4419 if (param->fp_check_comp_count) {
4420 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4421 param->fp_comp_count,
4422 param->fp_comp_count_sign,
4423 param->fp_exclude_comp_count, 1, 0);
4429 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4430 entry = &comp_v1->lcm_entries[i];
4432 if (param->fp_check_comp_flags) {
4434 if (((param->fp_comp_flags & entry->lcme_flags) !=
4435 param->fp_comp_flags) ||
4436 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4442 if (param->fp_check_comp_start) {
4443 ret = find_value_cmp(entry->lcme_extent.e_start,
4444 param->fp_comp_start,
4445 param->fp_comp_start_sign,
4446 param->fp_exclude_comp_start,
4447 param->fp_comp_start_units, 0);
4452 if (param->fp_check_comp_end) {
4453 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4459 /* the component matches all criteria */
4468 static int find_check_mirror_options(struct find_param *param)
4470 struct lov_comp_md_v1 *comp_v1;
4471 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4474 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4477 comp_v1 = (struct lov_comp_md_v1 *)v1;
4479 if (param->fp_check_mirror_count) {
4480 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4481 param->fp_mirror_count,
4482 param->fp_mirror_count_sign,
4483 param->fp_exclude_mirror_count, 1, 0);
4488 if (param->fp_check_mirror_state) {
4490 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4492 if ((param->fp_mirror_state != 0 &&
4493 file_state != param->fp_mirror_state) ||
4494 file_state == param->fp_mirror_neg_state)
4501 static bool find_check_lmm_info(struct find_param *param)
4503 return param->fp_check_pool || param->fp_check_stripe_count ||
4504 param->fp_check_stripe_size || param->fp_check_layout ||
4505 param->fp_check_comp_count || param->fp_check_comp_end ||
4506 param->fp_check_comp_start || param->fp_check_comp_flags ||
4507 param->fp_check_mirror_count || param->fp_check_foreign ||
4508 param->fp_check_mirror_state || param->fp_check_ext_size ||
4509 param->fp_check_projid;
4513 * Get file/directory project id.
4514 * by the open fd resides on.
4515 * Return 0 and project id on success, or -ve errno.
4517 static int fget_projid(int fd, int *projid)
4522 rc = ioctl(fd, LL_IOC_FSGETXATTR, &fsx);
4526 *projid = fsx.fsx_projid;
4530 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
4531 void *data, struct dirent64 *de)
4533 struct find_param *param = (struct find_param *)data;
4534 DIR *dir = dirp == NULL ? NULL : *dirp;
4535 int decision = 1; /* 1 is accepted; -1 is rejected. */
4536 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
4538 int checked_type = 0;
4540 __u32 stripe_count = 0;
4544 if (parent == NULL && dir == NULL)
4547 /* If a regular expression is presented, make the initial decision */
4548 if (param->fp_pattern != NULL) {
4549 char *fname = strrchr(path, '/');
4550 fname = (fname == NULL ? path : fname + 1);
4551 ret = fnmatch(param->fp_pattern, fname, 0);
4552 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4553 (ret == 0 && param->fp_exclude_pattern))
4557 /* See if we can check the file type from the dirent. */
4558 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
4561 if (DTTOIF(de->d_type) == param->fp_type) {
4562 if (param->fp_exclude_type)
4565 if (!param->fp_exclude_type)
4572 /* Request MDS for the stat info if some of these parameters need
4573 * to be compared. */
4574 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4575 param->fp_check_uid || param->fp_check_gid || param->fp_newerxy ||
4576 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4577 param->fp_check_size || param->fp_check_blocks ||
4578 find_check_lmm_info(param) ||
4579 param->fp_check_mdt_count || param->fp_check_hash_type)
4582 if (param->fp_type != 0 && checked_type == 0)
4585 if (decision == 0) {
4586 if (dir && (param->fp_check_mdt_count ||
4587 param->fp_check_hash_type || param->fp_check_foreign)) {
4588 param->fp_get_lmv = 1;
4589 ret = cb_get_dirstripe(path, dir, param);
4591 /* XXX this works to decide for foreign
4594 if (errno == ENODATA &&
4595 param->fp_check_foreign) {
4596 if (param->fp_exclude_foreign)
4604 param->fp_lmd->lmd_lmm.lmm_magic = 0;
4605 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
4606 param->fp_lum_size, GET_LMD_INFO);
4607 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
4608 find_check_lmm_info(param)) {
4609 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
4611 /* We need to "fake" the "use the default" values
4612 * since the lmm struct is zeroed out at this point. */
4613 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4614 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
4616 ostid_set_seq(&lmm->lmm_oi,
4617 FID_SEQ_LOV_DEFAULT);
4618 lmm->lmm_stripe_size = 0;
4619 lmm->lmm_stripe_count = 0;
4620 lmm->lmm_stripe_offset = -1;
4622 if (ret == 0 && param->fp_mdt_uuid != NULL) {
4624 ret = llapi_file_fget_mdtidx(dirfd(dir),
4625 ¶m->fp_file_mdt_index);
4626 } else if (S_ISREG(stx->stx_mode)) {
4627 /* FIXME: we could get the MDT index from the
4628 * file's FID in lmd->lmd_lmm.lmm_oi without
4629 * opening the file, once we are sure that
4630 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
4631 * That would still be an ioctl() to map the
4632 * FID to the MDT, but not an open RPC. */
4633 fd = open(path, O_RDONLY);
4635 ret = llapi_file_fget_mdtidx(fd,
4636 ¶m->fp_file_mdt_index);
4641 /* For a special file, we assume it resides on
4642 * the same MDT as the parent directory. */
4643 ret = llapi_file_fget_mdtidx(dirfd(parent),
4644 ¶m->fp_file_mdt_index);
4655 stripe_count = find_get_stripe_count(param);
4659 if (param->fp_type && !checked_type) {
4660 if ((stx->stx_mode & S_IFMT) == param->fp_type) {
4661 if (param->fp_exclude_type)
4664 if (!param->fp_exclude_type)
4670 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
4671 if (lustre_fs && param->fp_got_uuids &&
4672 param->fp_dev != makedev(stx->stx_dev_major,
4673 stx->stx_dev_minor)) {
4674 /* A lustre/lustre mount point is crossed. */
4675 param->fp_got_uuids = 0;
4676 param->fp_obds_printed = 0;
4677 param->fp_mdt_index = OBD_NOT_FOUND;
4678 param->fp_obd_index = OBD_NOT_FOUND;
4681 if (lustre_fs && !param->fp_got_uuids) {
4682 ret = setup_target_indexes(dir ? dir : parent, path,
4687 param->fp_dev = makedev(stx->stx_dev_major,
4688 stx->stx_dev_minor);
4689 } else if (!lustre_fs && param->fp_got_uuids) {
4690 /* A lustre/non-lustre mount point is crossed. */
4691 param->fp_got_uuids = 0;
4692 param->fp_mdt_index = OBD_NOT_FOUND;
4693 param->fp_obd_index = OBD_NOT_FOUND;
4697 if (param->fp_check_foreign) {
4698 decision = find_check_foreign(param);
4703 if (param->fp_check_stripe_size) {
4704 decision = find_check_stripe_size(param);
4709 if (param->fp_check_ext_size) {
4710 decision = find_check_ext_size(param);
4715 if (param->fp_check_stripe_count) {
4716 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
4717 param->fp_stripe_count_sign,
4718 param->fp_exclude_stripe_count, 1, 0);
4723 if (param->fp_check_mdt_count) {
4724 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4729 decision = find_value_cmp(
4730 param->fp_lmv_md->lum_stripe_count,
4731 param->fp_mdt_count,
4732 param->fp_mdt_count_sign,
4733 param->fp_exclude_mdt_count, 1, 0);
4738 if (param->fp_check_layout) {
4739 decision = find_check_layout(param);
4744 if (param->fp_check_hash_type) {
4747 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4752 found = param->fp_lmv_md->lum_hash_type & param->fp_hash_type;
4753 if ((found && param->fp_exclude_hash_type) ||
4754 (!found && !param->fp_exclude_hash_type)) {
4760 /* If an OBD UUID is specified but none matches, skip this file. */
4761 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
4762 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
4765 /* If an OST or MDT UUID is given, and some OST matches,
4767 if (param->fp_obd_index != OBD_NOT_FOUND ||
4768 param->fp_mdt_index != OBD_NOT_FOUND) {
4769 if (param->fp_obd_uuid) {
4770 if (check_obd_match(param)) {
4771 /* If no mdtuuid is given, we are done.
4772 * Otherwise, fall through to the mdtuuid
4774 if (!param->fp_mdt_uuid)
4781 if (param->fp_mdt_uuid) {
4782 if (check_mdt_match(param))
4789 if (param->fp_check_uid) {
4790 if (stx->stx_uid == param->fp_uid) {
4791 if (param->fp_exclude_uid)
4794 if (!param->fp_exclude_uid)
4799 if (param->fp_check_gid) {
4800 if (stx->stx_gid == param->fp_gid) {
4801 if (param->fp_exclude_gid)
4804 if (!param->fp_exclude_gid)
4809 if (param->fp_check_projid) {
4813 fd = open(path, O_RDONLY);
4816 ret = fget_projid(fd, &projid);
4821 if (projid == param->fp_projid) {
4822 if (param->fp_exclude_projid)
4825 if (!param->fp_exclude_projid)
4830 if (param->fp_check_pool) {
4831 decision = find_check_pool(param);
4836 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
4837 param->fp_check_comp_start || param->fp_check_comp_end) {
4838 decision = find_check_comp_options(param);
4843 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
4844 decision = find_check_mirror_options(param);
4849 /* Check the time on mds. */
4851 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
4854 for_mds = lustre_fs ?
4855 (S_ISREG(stx->stx_mode) && stripe_count) : 0;
4856 decision = find_time_check(stx, param, for_mds);
4861 if (param->fp_newerxy) {
4864 for_mds = lustre_fs ?
4865 (S_ISREG(stx->stx_mode) && stripe_count) : 0;
4866 decision = find_newerxy_check(stx, param, for_mds);
4871 flags = param->fp_lmd->lmd_flags;
4872 if (param->fp_check_size &&
4873 ((S_ISREG(stx->stx_mode) && stripe_count) ||
4874 S_ISDIR(stx->stx_mode)) &&
4875 !(flags & OBD_MD_FLSIZE ||
4876 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
4879 if (param->fp_check_blocks &&
4880 ((S_ISREG(stx->stx_mode) && stripe_count) ||
4881 S_ISDIR(stx->stx_mode)) &&
4882 !(flags & OBD_MD_FLBLOCKS ||
4883 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
4886 /* If file still fits the request, ask ost for updated info.
4887 * The regular stat is almost of the same speed as some new
4888 * 'glimpse-size-ioctl'.
4893 /* For regular files with the stripe the decision may have not
4894 * been taken yet if *time or size is to be checked. */
4895 if (param->fp_obd_index != OBD_NOT_FOUND)
4896 print_failed_tgt(param, path, LL_STATFS_LOV);
4898 if (param->fp_mdt_index != OBD_NOT_FOUND)
4899 print_failed_tgt(param, path, LL_STATFS_LMV);
4902 ret = fstat_f(dirfd(dir), &st);
4903 else if (de != NULL)
4904 ret = fstatat_f(dirfd(parent), de->d_name, &st,
4905 AT_SYMLINK_NOFOLLOW);
4907 ret = lstat_f(path, &st);
4910 if (errno == ENOENT) {
4911 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
4912 "warning: %s: %s does not exist",
4917 llapi_error(LLAPI_MSG_ERROR, ret,
4918 "%s: stat on %s failed",
4924 convert_lmd_statx(param->fp_lmd, &st, true);
4925 /* Check the time on osc. */
4926 decision = find_time_check(stx, param, 0);
4930 if (param->fp_newerxy) {
4931 decision = find_newerxy_check(stx, param, 0);
4938 if (param->fp_check_size) {
4939 decision = find_value_cmp(stx->stx_size, param->fp_size,
4940 param->fp_size_sign,
4941 param->fp_exclude_size,
4942 param->fp_size_units, 0);
4947 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
4948 decision = find_value_cmp(stx->stx_blocks * 512,
4950 param->fp_blocks_sign,
4951 param->fp_exclude_blocks,
4952 param->fp_blocks_units, 0);
4958 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
4959 if (param->fp_zero_end)
4960 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
4962 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4966 /* Do not get down anymore? */
4967 if (param->fp_depth == param->fp_max_depth) {
4978 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
4979 void *param_data, struct dirent64 *de)
4981 struct find_param *param = (struct find_param *)param_data;
4982 struct lmv_user_md *lmu = param->fp_lmv_md;
4983 DIR *tmp_parent = parent;
4984 char raw[MAX_IOC_BUFLEN] = {'\0'};
4986 struct obd_ioctl_data data = { 0 };
4993 if (parent == NULL && dirp == NULL)
5002 if (parent == NULL) {
5003 tmp_parent = opendir_parent(path);
5004 if (tmp_parent == NULL) {
5007 llapi_error(LLAPI_MSG_ERROR, ret,
5008 "can not open %s", path);
5013 fd = dirfd(tmp_parent);
5015 path_copy = strdup(path);
5016 filename = basename(path_copy);
5018 data.ioc_inlbuf1 = (char *)filename;
5019 data.ioc_inllen1 = strlen(filename) + 1;
5020 data.ioc_inlbuf2 = (char *)lmu;
5021 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
5023 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5025 llapi_error(LLAPI_MSG_ERROR, ret,
5026 "llapi_obd_statfs: error packing ioctl data");
5031 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
5033 if (errno == EBUSY && !retry) {
5034 /* because migrate may not be able to lock all involved
5035 * objects in order, for some of them it try lock, while
5036 * there may be conflicting COS locks and cause migrate
5037 * fail with EBUSY, hope a sync() could cause
5038 * transaction commit and release these COS locks. */
5042 } else if (errno == EALREADY) {
5043 if (param->fp_verbose & VERBOSE_DETAIL)
5044 llapi_printf(LLAPI_MSG_NORMAL,
5045 "%s migrated to MDT%d already\n",
5046 path, lmu->lum_stripe_offset);
5050 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
5054 } else if (param->fp_verbose & VERBOSE_DETAIL) {
5055 llapi_printf(LLAPI_MSG_NORMAL,
5056 "migrate %s to MDT%d stripe count %d\n",
5057 path, lmu->lum_stripe_offset,
5058 lmu->lum_stripe_count);
5063 /* If the directory is being migration, we need
5064 * close the directory after migration,
5065 * so the old directory cache will be cleanup
5066 * on the client side, and re-open to get the
5067 * new directory handle */
5068 *dirp = opendir(path);
5069 if (*dirp == NULL) {
5071 llapi_error(LLAPI_MSG_ERROR, ret,
5072 "%s: Failed to open '%s'", __func__, path);
5077 closedir(tmp_parent);
5084 /* dir migration finished, shrink its stripes */
5085 static int cb_migrate_mdt_fini(char *path, DIR *parent, DIR **dirp, void *data,
5086 struct dirent64 *de)
5088 struct find_param *param = data;
5089 struct lmv_user_md *lmu = param->fp_lmv_md;
5090 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
5093 if (de && de->d_type != DT_DIR)
5098 * close it before setxattr because the latter may destroy the
5099 * original object, and cause close fail.
5101 ret = closedir(*dirp);
5107 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
5108 if (ret == -EALREADY)
5111 cb_common_fini(path, parent, dirp, data, de);
5115 int llapi_migrate_mdt(char *path, struct find_param *param)
5117 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
5121 int llapi_mv(char *path, struct find_param *param)
5123 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
5124 static bool printed;
5127 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
5128 "%s() is deprecated, use llapi_migrate_mdt() instead",
5133 return llapi_migrate_mdt(path, param);
5136 int llapi_find(char *path, struct find_param *param)
5138 return param_callback(path, cb_find_init, cb_common_fini, param);
5142 * Get MDT number that the file/directory inode referenced
5143 * by the open fd resides on.
5144 * Return 0 and mdtidx on success, or -ve errno.
5146 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
5148 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
5153 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
5154 struct dirent64 *de)
5156 struct find_param *param = (struct find_param *)data;
5157 DIR *d = dirp == NULL ? NULL : *dirp;
5161 if (parent == NULL && d == NULL)
5165 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5166 } else /* if (parent) */ {
5169 fd = open(path, O_RDONLY | O_NOCTTY);
5171 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5179 if (ret == -ENODATA) {
5180 if (!param->fp_obd_uuid)
5181 llapi_printf(LLAPI_MSG_NORMAL,
5182 "'%s' has no stripe info\n", path);
5184 } else if (ret == -ENOENT) {
5185 llapi_error(LLAPI_MSG_WARN, ret,
5186 "warning: %s: '%s' does not exist",
5189 } else if (ret == -ENOTTY) {
5190 llapi_error(LLAPI_MSG_ERROR, ret,
5191 "%s: '%s' not on a Lustre fs",
5194 llapi_error(LLAPI_MSG_ERROR, ret,
5195 "error: %s: '%s' failed get_mdtidx",
5201 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5202 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5204 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5208 /* Do not go down anymore? */
5209 if (param->fp_depth == param->fp_max_depth)
5217 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
5218 struct dirent64 *de)
5220 struct find_param *param = (struct find_param *)data;
5221 DIR *d = dirp == NULL ? NULL : *dirp;
5224 if (parent == NULL && d == NULL)
5227 if (param->fp_obd_uuid) {
5228 param->fp_quiet = 1;
5229 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
5234 if (d && (param->fp_get_lmv || param->fp_get_default_lmv))
5235 ret = cb_get_dirstripe(path, d, param);
5237 (parent && !param->fp_get_lmv && !param->fp_get_default_lmv))
5238 ret = get_lmd_info(path, parent, d, ¶m->fp_lmd->lmd_lmm,
5239 param->fp_lum_size, GET_LMD_STRIPE);
5244 if (errno == ENODATA && d != NULL) {
5245 /* We need to "fake" the "use the default" values
5246 * since the lmm struct is zeroed out at this point.
5247 * The magic needs to be set in order to satisfy
5248 * a check later on in the code path.
5249 * The object_seq needs to be set for the "(Default)"
5250 * prefix to be displayed. */
5251 if (param->fp_get_default_lmv) {
5252 struct lmv_user_md *lum = param->fp_lmv_md;
5254 lum->lum_magic = LMV_USER_MAGIC;
5255 lum->lum_stripe_count = 0;
5256 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
5258 } else if (param->fp_get_lmv) {
5259 struct lmv_user_md *lum = param->fp_lmv_md;
5262 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5265 lum->lum_magic = LMV_MAGIC_V1;
5266 lum->lum_stripe_count = 0;
5267 lum->lum_stripe_offset = mdtidx;
5270 struct lov_user_md *lmm =
5271 ¶m->fp_lmd->lmd_lmm;
5273 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5275 ostid_set_seq(&lmm->lmm_oi,
5276 FID_SEQ_LOV_DEFAULT);
5277 lmm->lmm_stripe_count = 0;
5278 lmm->lmm_stripe_size = 0;
5279 lmm->lmm_stripe_offset = -1;
5282 } else if (errno == ENODATA && parent != NULL) {
5283 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5284 llapi_printf(LLAPI_MSG_NORMAL,
5285 "%s has no stripe info\n", path);
5287 } else if (errno == ENOENT) {
5288 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5289 "warning: %s: %s does not exist",
5292 } else if (errno == ENOTTY) {
5294 llapi_error(LLAPI_MSG_ERROR, ret,
5295 "%s: '%s' not on a Lustre fs?",
5300 llapi_error(LLAPI_MSG_ERROR, ret,
5301 "error: %s: %s failed for %s",
5302 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
5303 "IOC_MDC_GETFILESTRIPE", path);
5310 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5311 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
5314 /* Do not get down anymore? */
5315 if (param->fp_depth == param->fp_max_depth)
5323 int llapi_getstripe(char *path, struct find_param *param)
5325 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5326 cb_get_mdt_index : cb_getstripe,
5327 cb_common_fini, param);
5330 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5331 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5333 char raw[MAX_IOC_BUFLEN] = {'\0'};
5335 struct obd_ioctl_data data = { 0 };
5338 data.ioc_inlbuf1 = (char *)&type;
5339 data.ioc_inllen1 = sizeof(__u32);
5340 data.ioc_inlbuf2 = (char *)&index;
5341 data.ioc_inllen2 = sizeof(__u32);
5342 data.ioc_pbuf1 = (char *)stat_buf;
5343 data.ioc_plen1 = sizeof(struct obd_statfs);
5344 data.ioc_pbuf2 = (char *)uuid_buf;
5345 data.ioc_plen2 = sizeof(struct obd_uuid);
5347 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5349 llapi_error(LLAPI_MSG_ERROR, rc,
5350 "llapi_obd_statfs: error packing ioctl data");
5354 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5356 return rc < 0 ? -errno : 0;
5359 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5360 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5365 fd = open(path, O_RDONLY);
5368 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5370 /* If we can't even open a file on the filesystem (e.g. with
5371 * -ESHUTDOWN), force caller to exit or it will loop forever. */
5375 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5382 #define MAX_STRING_SIZE 128
5384 int llapi_ping(char *obd_type, char *obd_name)
5386 int flags = O_RDONLY;
5387 char buf[1] = { 0 };
5391 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5392 obd_type, obd_name);
5396 fd = open(path.gl_pathv[0], flags);
5398 if (errno == EACCES && flags == O_RDONLY) {
5403 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5408 if (flags == O_RDONLY)
5409 rc = read(fd, buf, sizeof(buf));
5411 rc = write(fd, buf, sizeof(buf));
5419 cfs_free_param_data(&path);
5423 int llapi_target_iterate(int type_num, char **obd_type,
5424 void *args, llapi_cb_t cb)
5430 for (i = 0; i < type_num; i++) {
5433 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5437 for (j = 0; j < param.gl_pathc; j++) {
5438 char obd_uuid[UUID_MAX + 1];
5442 fp = fopen(param.gl_pathv[j], "r");
5445 llapi_error(LLAPI_MSG_ERROR, rc,
5446 "error: opening '%s'",
5451 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5453 llapi_error(LLAPI_MSG_ERROR, rc,
5454 "error: reading '%s'",
5459 /* Extract the obd_name from the sysfs path.
5460 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5462 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5468 /* skip /fs/lustre/'obd_type'/ */
5469 obd_name += strlen(obd_type[i]) + 12;
5470 /* chop off after obd_name */
5471 ptr = strrchr(obd_name, '/');
5475 cb(obd_type[i], obd_name, obd_uuid, args);
5484 cfs_free_param_data(¶m);
5488 static void do_target_check(char *obd_type_name, char *obd_name,
5489 char *obd_uuid, void *args)
5493 rc = llapi_ping(obd_type_name, obd_name);
5494 if (rc == ENOTCONN) {
5495 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5497 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5499 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5503 int llapi_target_check(int type_num, char **obd_type, char *dir)
5505 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5508 #undef MAX_STRING_SIZE
5510 /* Is this a lustre fs? */
5511 int llapi_is_lustre_mnttype(const char *type)
5513 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
5516 /* Is this a lustre client fs? */
5517 int llapi_is_lustre_mnt(struct mntent *mnt)
5519 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5520 strstr(mnt->mnt_fsname, ":/") != NULL);
5523 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5525 char fsname[PATH_MAX + 1];
5529 rc = llapi_search_fsname(mnt, fsname);
5533 root = open(mnt, O_RDONLY | O_DIRECTORY);
5536 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
5540 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
5548 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
5553 root = open(mnt, O_RDONLY | O_DIRECTORY);
5556 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
5560 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
5563 llapi_error(LLAPI_MSG_ERROR, rc,
5564 "ioctl on %s for getting connect flags failed", mnt);
5571 * Get a 64-bit value representing the version of file data pointed by fd.
5573 * Each write or truncate, flushed on OST, will change this value. You can use
5574 * this value to verify if file data was modified. This only checks the file
5575 * data, not metadata.
5577 * \param flags 0: no flush pages, usually used it the process has already
5579 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
5581 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
5582 * pages from clients.
5584 * \retval 0 on success.
5585 * \retval -errno on error.
5587 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
5590 struct ioc_data_version idv;
5592 idv.idv_flags = (__u32)flags;
5594 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5598 *data_version = idv.idv_version;
5604 * Flush cached pages from all clients.
5606 * \param fd File descriptor
5610 int llapi_file_flush(int fd)
5614 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);
5618 * Fetch layout version from OST objects. Layout version on OST objects are
5619 * only set when the file is a mirrored file AND after the file has been
5620 * written at least once.
5622 * It actually fetches the least layout version from the objects.
5624 int llapi_get_ost_layout_version(int fd, __u32 *layout_version)
5627 struct ioc_data_version idv = { 0 };
5629 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5633 *layout_version = idv.idv_layout_version;
5639 * Create a file without any name and open it for read/write
5641 * - file is created as if it were a standard file in the given \a directory
5642 * - file does not appear in \a directory and mtime does not change because
5643 * the filename is handled specially by the Lustre MDS.
5644 * - file is destroyed at final close
5646 * \param[in] directory directory from which to inherit layout/MDT idx
5647 * \param[in] mdt_idx MDT index on which the file is created,
5648 * \a idx == -1 means no specific MDT is requested
5649 * \param[in] mode standard open(2) mode
5650 * \param[in] stripe_param stripe parameters. May be NULL.
5652 * \retval a file descriptor on success.
5653 * \retval -errno on error.
5655 int llapi_create_volatile_param(const char *directory, int mdt_idx,
5656 int open_flags, mode_t mode,
5657 const struct llapi_stripe_param *stripe_param)
5659 char file_path[PATH_MAX];
5660 int saved_errno = errno;
5662 unsigned int rnumber;
5668 rc = snprintf(file_path, sizeof(file_path),
5669 "%s/" LUSTRE_VOLATILE_HDR "::%.4X",
5670 directory, rnumber);
5672 rc = snprintf(file_path, sizeof(file_path),
5673 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X",
5674 directory, mdt_idx, rnumber);
5676 if (rc < 0 || rc >= sizeof(file_path))
5677 return -ENAMETOOLONG;
5680 * Either open O_WRONLY or O_RDWR, creating RDONLY
5681 * is non-sensical here
5683 if ((open_flags & O_ACCMODE) == O_RDONLY)
5684 open_flags = O_RDWR | (open_flags & ~O_ACCMODE);
5686 open_flags |= O_CREAT | O_EXCL | O_NOFOLLOW;
5688 if (stripe_param != NULL) {
5689 fd = llapi_file_open_param(file_path, open_flags,
5690 mode, stripe_param);
5694 fd = open(file_path, open_flags, mode);
5698 } while (fd < 0 && rc == -EEXIST);
5701 llapi_error(LLAPI_MSG_ERROR, rc,
5702 "Cannot create volatile file '%s' in '%s'",
5703 file_path + strlen(directory) + 1 +
5704 LUSTRE_VOLATILE_HDR_LEN,
5710 * Unlink file in case this wasn't a Lustre filesystem and the magic
5711 * volatile filename wasn't handled as intended. The effect is the
5712 * same. If volatile open was supported then we expect unlink() to
5715 (void)unlink(file_path);
5718 * Since we are returning successfully we restore errno (and
5719 * mask out possible EEXIST from open() and ENOENT from unlink().
5721 errno = saved_errno;
5727 * Create a file without any name open it for read/write
5729 * - file is created as if it were a standard file in the given \a directory
5730 * - file does not appear in \a directory and mtime does not change because
5731 * the filename is handled specially by the Lustre MDS.
5732 * - file is removed at final close
5733 * - file modes are rw------- since it doesn't make sense to have a read-only
5734 * or write-only file that cannot be opened again.
5735 * - if user wants another mode it must use fchmod() on the open file, no
5736 * security problems arise because it cannot be opened by another process.
5738 * \param[in] directory directory from which to inherit layout/MDT idx
5739 * \param[in] idx MDT index on which the file is created,
5740 * \a idx == -1 means no specific MDT is requested
5741 * \param[in] open_flags standard open(2) flags
5743 * \retval a file descriptor on success.
5744 * \retval -errno on error.
5746 int llapi_create_volatile_idx(const char *directory, int mdt_idx,
5749 return llapi_create_volatile_param(directory, mdt_idx, open_flags,
5750 S_IRUSR | S_IWUSR, NULL);
5754 * Swap the layouts between 2 file descriptors
5755 * the 2 files must be open for writing
5756 * first fd received the ioctl, second fd is passed as arg
5757 * this is assymetric but avoid use of root path for ioctl
5759 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
5760 int gid, __u64 flags)
5762 struct lustre_swap_layouts lsl;
5767 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5768 rc = fstat(fd1, &st1);
5772 rc = fstat(fd2, &st2);
5777 lsl.sl_flags = flags;
5781 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
5785 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5786 struct timeval tv1[2];
5787 struct timeval tv2[2];
5789 memset(tv1, 0, sizeof(tv1));
5790 memset(tv2, 0, sizeof(tv2));
5792 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
5793 tv1[0].tv_sec = st1.st_atime;
5794 tv2[0].tv_sec = st2.st_atime;
5796 tv1[0].tv_sec = st2.st_atime;
5797 tv2[0].tv_sec = st1.st_atime;
5800 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
5801 tv1[1].tv_sec = st1.st_mtime;
5802 tv2[1].tv_sec = st2.st_mtime;
5804 tv1[1].tv_sec = st2.st_mtime;
5805 tv2[1].tv_sec = st1.st_mtime;
5808 rc = futimes(fd1, tv1);
5812 rc = futimes(fd2, tv2);
5820 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
5827 while (grp_id == 0);
5829 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
5837 * Swap the layouts between 2 files
5838 * the 2 files are open in write
5840 int llapi_swap_layouts(const char *path1, const char *path2,
5841 __u64 dv1, __u64 dv2, __u64 flags)
5845 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
5848 llapi_error(LLAPI_MSG_ERROR, rc,
5849 "error: cannot open '%s' for write", path1);
5853 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
5856 llapi_error(LLAPI_MSG_ERROR, rc,
5857 "error: cannot open '%s' for write", path2);
5861 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
5863 llapi_error(LLAPI_MSG_ERROR, rc,
5864 "error: cannot swap layout between '%s' and '%s'",
5877 * \param fd File to lock.
5878 * \param gid Group Identifier.
5880 * \retval 0 on success.
5881 * \retval -errno on failure.
5883 int llapi_group_lock(int fd, int gid)
5887 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
5890 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
5898 * \param fd File to unlock.
5899 * \param gid Group Identifier.
5901 * \retval 0 on success.
5902 * \retval -errno on failure.
5904 int llapi_group_unlock(int fd, int gid)
5908 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
5911 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");