4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/utils/liblustreapi.c
34 * Author: Peter J. Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Robert Read <rread@clusterfs.com>
50 #include <sys/ioctl.h>
57 #include <sys/statfs.h>
58 #include <sys/syscall.h>
60 #include <sys/types.h>
61 #include <sys/xattr.h>
64 #include <libgen.h> /* for dirname() */
65 #ifdef HAVE_LINUX_UNISTD_H
66 #include <linux/unistd.h>
73 #include <libcfs/util/ioctl.h>
74 #include <libcfs/util/param.h>
75 #include <libcfs/util/string.h>
76 #include <linux/lnet/lnetctl.h>
77 #include <lustre/lustreapi.h>
78 #include <linux/lustre/lustre_ostid.h>
79 #include <linux/lustre/lustre_ioctl.h>
80 #include "lustreapi_internal.h"
82 static int llapi_msg_level = LLAPI_MSG_MAX;
83 const char *liblustreapi_cmd;
85 char *mdt_hash_name[] = { "none",
86 LMV_HASH_NAME_ALL_CHARS,
87 LMV_HASH_NAME_FNV_1A_64,
91 struct lustre_foreign_type lu_foreign_types[] = {
92 {.lft_type = LU_FOREIGN_TYPE_NONE, .lft_name = "none"},
93 {.lft_type = LU_FOREIGN_TYPE_DAOS, .lft_name = "daos"},
94 /* must be the last element */
95 {.lft_type = LU_FOREIGN_TYPE_UNKNOWN, .lft_name = NULL}
96 /* array max dimension must be <= UINT32_MAX */
99 void llapi_msg_set_level(int level)
101 /* ensure level is in the good range */
102 if (level < LLAPI_MSG_OFF)
103 llapi_msg_level = LLAPI_MSG_OFF;
104 else if (level > LLAPI_MSG_MAX)
105 llapi_msg_level = LLAPI_MSG_MAX;
107 llapi_msg_level = level;
110 int llapi_msg_get_level(void)
112 return llapi_msg_level;
115 void llapi_set_command_name(const char *cmd)
117 liblustreapi_cmd = cmd;
120 void llapi_clear_command_name(void)
122 liblustreapi_cmd = NULL;
125 static void error_callback_default(enum llapi_message_level level, int err,
126 const char *fmt, va_list ap)
128 if (liblustreapi_cmd != NULL)
129 fprintf(stderr, "%s %s: ", program_invocation_short_name,
132 fprintf(stderr, "%s: ", program_invocation_short_name);
133 vfprintf(stderr, fmt, ap);
134 if (level & LLAPI_MSG_NO_ERRNO)
135 fprintf(stderr, "\n");
137 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
140 static void info_callback_default(enum llapi_message_level level, int err,
141 const char *fmt, va_list ap)
144 if (liblustreapi_cmd != NULL) {
145 fprintf(stdout, "%s %s: ",
146 program_invocation_short_name,
149 fprintf(stdout, "%s: ", program_invocation_short_name);
152 vfprintf(stdout, fmt, ap);
155 static llapi_log_callback_t llapi_error_callback = error_callback_default;
156 static llapi_log_callback_t llapi_info_callback = info_callback_default;
159 /* llapi_error will preserve errno */
160 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
163 int tmp_errno = errno;
165 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
169 llapi_error_callback(level, abs(err), fmt, args);
174 /* llapi_printf will preserve errno */
175 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
178 int tmp_errno = errno;
180 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
184 llapi_info_callback(level, 0, fmt, args);
190 * Set a custom error logging function. Passing in NULL will reset the logging
191 * callback to its default value.
193 * This function returns the value of the old callback.
195 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
197 llapi_log_callback_t old = llapi_error_callback;
200 llapi_error_callback = cb;
202 llapi_error_callback = error_callback_default;
208 * Set a custom info logging function. Passing in NULL will reset the logging
209 * callback to its default value.
211 * This function returns the value of the old callback.
213 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
215 llapi_log_callback_t old = llapi_info_callback;
218 llapi_info_callback = cb;
220 llapi_info_callback = info_callback_default;
226 * size_units is to be initialized (or zeroed) by caller.
228 int llapi_parse_size(const char *optarg, unsigned long long *size,
229 unsigned long long *size_units, int bytes_spec)
232 char *argbuf = (char *)optarg;
233 unsigned long long frac = 0, frac_d = 1;
235 if (strncmp(optarg, "-", 1) == 0)
238 if (*size_units == 0)
241 *size = strtoull(argbuf, &end, 0);
242 if (end != NULL && *end == '.') {
246 frac = strtoull(argbuf, &end, 10);
247 /* count decimal places */
248 for (i = 0; i < (end - argbuf); i++)
253 if ((*end == 'b') && *(end + 1) == '\0' &&
254 (*size & (~0ULL << (64 - 9))) == 0 &&
256 *size_units = 1 << 9;
257 } else if ((*end == 'b') &&
258 *(end + 1) == '\0' &&
261 } else if ((*end == 'k' || *end == 'K') &&
262 *(end + 1) == '\0' &&
263 (*size & (~0ULL << (64 - 10))) == 0) {
264 *size_units = 1 << 10;
265 } else if ((*end == 'm' || *end == 'M') &&
266 *(end + 1) == '\0' &&
267 (*size & (~0ULL << (64 - 20))) == 0) {
268 *size_units = 1 << 20;
269 } else if ((*end == 'g' || *end == 'G') &&
270 *(end + 1) == '\0' &&
271 (*size & (~0ULL << (64 - 30))) == 0) {
272 *size_units = 1 << 30;
273 } else if ((*end == 't' || *end == 'T') &&
274 *(end + 1) == '\0' &&
275 (*size & (~0ULL << (64 - 40))) == 0) {
276 *size_units = 1ULL << 40;
277 } else if ((*end == 'p' || *end == 'P') &&
278 *(end + 1) == '\0' &&
279 (*size & (~0ULL << (64 - 50))) == 0) {
280 *size_units = 1ULL << 50;
281 } else if ((*end == 'e' || *end == 'E') &&
282 *(end + 1) == '\0' &&
283 (*size & (~0ULL << (64 - 60))) == 0) {
284 *size_units = 1ULL << 60;
289 *size = *size * *size_units + frac * *size_units / frac_d;
294 int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
296 struct obd_ioctl_data *overlay;
299 data->ioc_len = obd_ioctl_packlen(data);
300 data->ioc_version = OBD_IOCTL_VERSION;
302 if (*pbuf != NULL && data->ioc_len > max_len) {
303 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
304 "pbuf = %p, ioc_len = %u, max_len = %d\n",
305 *pbuf, data->ioc_len, max_len);
310 *pbuf = malloc(data->ioc_len);
315 overlay = (struct obd_ioctl_data *)*pbuf;
316 memcpy(*pbuf, data, sizeof(*data));
318 ptr = overlay->ioc_bulk;
319 if (data->ioc_inlbuf1) {
320 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
321 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
324 if (data->ioc_inlbuf2) {
325 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
326 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
329 if (data->ioc_inlbuf3) {
330 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
331 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
334 if (data->ioc_inlbuf4) {
335 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
336 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
342 int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
344 struct obd_ioctl_data *overlay;
350 overlay = (struct obd_ioctl_data *)pbuf;
352 /* Preserve the caller's buffer pointers */
353 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
354 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
355 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
356 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
358 memcpy(data, pbuf, sizeof(*data));
360 ptr = overlay->ioc_bulk;
361 if (data->ioc_inlbuf1) {
362 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
363 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
366 if (data->ioc_inlbuf2) {
367 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
368 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
371 if (data->ioc_inlbuf3) {
372 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
373 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
376 if (data->ioc_inlbuf4) {
377 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
378 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
384 /* XXX: llapi_xxx() functions return negative values upon failure */
386 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
387 int stripe_count, int stripe_pattern)
389 static int page_size;
392 if (page_size == 0) {
393 /* 64 KB is the largest common page size (on ia64/PPC/ARM),
394 * but check the local page size just in case. The page_size
395 * will not change for the lifetime of this process at least.
397 page_size = LOV_MIN_STRIPE_SIZE;
398 if (getpagesize() > page_size) {
399 page_size = getpagesize();
400 llapi_err_noerrno(LLAPI_MSG_WARN,
401 "warning: page size (%u) larger than expected (%u)",
402 page_size, LOV_MIN_STRIPE_SIZE);
405 if (!llapi_stripe_size_is_aligned(stripe_size)) {
407 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
408 "must be an even multiple of %d bytes",
409 stripe_size, page_size);
412 if (!llapi_stripe_index_is_valid(stripe_offset)) {
414 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
418 if (!llapi_stripe_count_is_valid(stripe_count)) {
420 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
424 if (llapi_stripe_size_is_too_big(stripe_size)) {
426 llapi_error(LLAPI_MSG_ERROR, rc,
427 "error: stripe size '%llu' over 4GB limit",
437 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
442 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
444 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
448 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
450 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
455 if (!llapi_dir_hash_type_is_valid(hash_type)) {
457 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
465 * Trim a trailing newline from a string, if it exists.
467 int llapi_chomp_string(char *buf)
483 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
485 static int get_param_lov(const char *path, const char *param,
486 char *buf, size_t buf_size)
488 struct obd_uuid uuid;
491 rc = llapi_file_get_lov_uuid(path, &uuid);
495 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
500 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
502 static int get_param_lmv(const char *path, const char *param,
503 char *buf, size_t buf_size)
505 struct obd_uuid uuid;
508 rc = llapi_file_get_lmv_uuid(path, &uuid);
512 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
516 static int get_mds_md_size(const char *path)
518 char buf[PATH_MAX], inst[PATH_MAX];
519 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
522 rc = llapi_getname(path, inst, sizeof(inst));
526 /* Get the max ea size from llite parameters. */
527 rc = get_lustre_param_value("llite", inst, FILTER_BY_EXACT,
528 "max_easize", buf, sizeof(buf));
534 return rc > 0 ? rc : md_size;
537 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
539 return get_param_lmv(path, "uuid", buf, bufsize);
543 * Open a Lustre file.
545 * \param name the name of the file to be opened
546 * \param flags access mode, see flags in open(2)
547 * \param mode permission of the file if it is created, see mode in open(2)
548 * \param param stripe pattern of the newly created file
550 * \retval file descriptor of opened file
551 * \retval negative errno on failure
553 int llapi_file_open_param(const char *name, int flags, mode_t mode,
554 const struct llapi_stripe_param *param)
556 char fsname[MAX_OBD_NAME + 1] = { 0 };
557 char *pool_name = param->lsp_pool;
558 struct lov_user_md *lum = NULL;
559 size_t lum_size = sizeof(*lum);
562 /* Make sure we are on a Lustre file system */
563 rc = llapi_search_fsname(name, fsname);
565 llapi_error(LLAPI_MSG_ERROR, rc,
566 "'%s' is not on a Lustre filesystem",
571 /* Check if the stripe pattern is sane. */
572 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
573 param->lsp_stripe_offset,
574 param->lsp_stripe_count,
575 param->lsp_stripe_pattern);
579 /* Make sure we have a good pool */
580 if (pool_name != NULL) {
581 /* in case user gives the full pool name <fsname>.<poolname>,
582 * strip the fsname */
583 char *ptr = strchr(pool_name, '.');
586 if (strcmp(pool_name, fsname) != 0) {
588 llapi_err_noerrno(LLAPI_MSG_ERROR,
589 "Pool '%s' is not on filesystem '%s'",
596 /* Make sure the pool exists and is non-empty */
597 rc = llapi_search_ost(fsname, pool_name, NULL);
599 char *err = rc == 0 ? "has no OSTs" : "does not exist";
601 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
602 fsname, pool_name, err);
606 lum_size = sizeof(struct lov_user_md_v3);
609 /* sanity check of target list */
610 if (param->lsp_is_specific) {
611 char ostname[MAX_OBD_NAME + 64];
615 for (i = 0; i < param->lsp_stripe_count; i++) {
616 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
617 fsname, param->lsp_osts[i]);
618 rc = llapi_search_ost(fsname, pool_name, ostname);
623 llapi_error(LLAPI_MSG_ERROR, rc,
624 "%s: cannot find OST %s in %s",
631 /* Make sure stripe offset is in OST list. */
632 if (param->lsp_osts[i] == param->lsp_stripe_offset)
636 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
637 "%s: stripe offset '%d' is not in the "
639 __func__, param->lsp_stripe_offset);
643 lum_size = lov_user_md_size(param->lsp_stripe_count,
644 LOV_USER_MAGIC_SPECIFIC);
647 lum = calloc(1, lum_size);
652 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
654 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
655 flags = O_DIRECTORY | O_RDONLY;
662 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
667 /* Initialize IOCTL striping pattern structure */
668 lum->lmm_magic = LOV_USER_MAGIC_V1;
669 lum->lmm_pattern = param->lsp_stripe_pattern;
670 lum->lmm_stripe_size = param->lsp_stripe_size;
671 lum->lmm_stripe_count = param->lsp_stripe_count;
672 lum->lmm_stripe_offset = param->lsp_stripe_offset;
673 if (pool_name != NULL) {
674 struct lov_user_md_v3 *lumv3 = (void *)lum;
676 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
677 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
679 if (param->lsp_is_specific) {
680 struct lov_user_md_v3 *lumv3 = (void *)lum;
683 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
684 if (pool_name == NULL) {
685 /* LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
686 * OST list, therefore if pool is not specified we have
687 * to pack a null pool name for placeholder. */
688 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
691 for (i = 0; i < param->lsp_stripe_count; i++)
692 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
695 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
696 char *errmsg = "stripe already set";
699 if (errno != EEXIST && errno != EALREADY)
700 errmsg = strerror(errno);
702 llapi_err_noerrno(LLAPI_MSG_ERROR,
703 "setstripe error for '%s': %s", name, errmsg);
714 int llapi_file_open_pool(const char *name, int flags, int mode,
715 unsigned long long stripe_size, int stripe_offset,
716 int stripe_count, int stripe_pattern, char *pool_name)
718 const struct llapi_stripe_param param = {
719 .lsp_stripe_size = stripe_size,
720 .lsp_stripe_count = stripe_count,
721 .lsp_stripe_pattern = stripe_pattern,
722 .lsp_stripe_offset = stripe_offset,
723 .lsp_pool = pool_name
725 return llapi_file_open_param(name, flags, mode, ¶m);
728 int llapi_file_open(const char *name, int flags, int mode,
729 unsigned long long stripe_size, int stripe_offset,
730 int stripe_count, int stripe_pattern)
732 return llapi_file_open_pool(name, flags, mode, stripe_size,
733 stripe_offset, stripe_count,
734 stripe_pattern, NULL);
737 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
738 __u32 flags, char *foreign_lov)
741 struct lov_foreign_md *lfm;
744 if (foreign_lov == NULL) {
745 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
746 "foreign LOV EA content must be provided");
750 len = strlen(foreign_lov);
751 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
753 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
754 "foreign LOV EA size %zu (must be 0 < len < %zu)",
755 len, XATTR_SIZE_MAX -
756 offsetof(struct lov_foreign_md, lfm_value));
760 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
762 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
763 "failed to allocate lov_foreign_md");
767 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
774 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
775 lfm->lfm_length = len;
776 lfm->lfm_type = type;
777 lfm->lfm_flags = flags;
778 memcpy(lfm->lfm_value, foreign_lov, len);
780 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
781 char *errmsg = "stripe already set";
782 char fsname[MAX_OBD_NAME + 1] = { 0 };
785 if (errno != EEXIST && errno != EALREADY)
786 errmsg = strerror(errno);
788 llapi_err_noerrno(LLAPI_MSG_ERROR,
789 "setstripe error for '%s': %s", name, errmsg);
791 /* Make sure we are on a Lustre file system */
792 if (rc == -ENOTTY && llapi_search_fsname(name, fsname))
793 llapi_error(LLAPI_MSG_ERROR, rc,
794 "'%s' is not on a Lustre filesystem",
807 int llapi_file_create(const char *name, unsigned long long stripe_size,
808 int stripe_offset, int stripe_count, int stripe_pattern)
812 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
813 stripe_offset, stripe_count, stripe_pattern,
822 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
823 int stripe_offset, int stripe_count,
824 int stripe_pattern, char *pool_name)
828 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
829 stripe_offset, stripe_count, stripe_pattern,
838 static int verify_dir_param(const char *name,
839 const struct llapi_stripe_param *param)
841 char fsname[MAX_OBD_NAME + 1] = { 0 };
842 char *pool_name = param->lsp_pool;
845 /* Make sure we are on a Lustre file system */
846 rc = llapi_search_fsname(name, fsname);
848 llapi_error(LLAPI_MSG_ERROR, rc,
849 "'%s' is not on a Lustre filesystem",
854 /* Check if the stripe pattern is sane. */
855 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
856 param->lsp_stripe_count,
857 param->lsp_stripe_pattern);
861 /* Make sure we have a good pool */
862 if (pool_name != NULL) {
863 /* in case user gives the full pool name <fsname>.<poolname>,
864 * strip the fsname */
865 char *ptr = strchr(pool_name, '.');
869 if (strcmp(pool_name, fsname) != 0) {
871 llapi_err_noerrno(LLAPI_MSG_ERROR,
872 "Pool '%s' is not on filesystem '%s'",
879 /* Make sure the pool exists and is non-empty */
880 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
882 char *err = rc == 0 ? "has no OSTs" : "does not exist";
884 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
885 fsname, pool_name, err);
890 /* sanity check of target list */
891 if (param->lsp_is_specific) {
892 char mdtname[MAX_OBD_NAME + 64];
896 for (i = 0; i < param->lsp_stripe_count; i++) {
897 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
898 fsname, param->lsp_tgts[i]);
899 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
904 llapi_error(LLAPI_MSG_ERROR, rc,
905 "%s: cannot find MDT %s in %s",
912 /* Make sure stripe offset is in MDT list. */
913 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
917 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
918 "%s: stripe offset '%d' is not in the "
920 __func__, param->lsp_stripe_offset);
928 static inline void param2lmu(struct lmv_user_md *lmu,
929 const struct llapi_stripe_param *param)
931 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
933 lmu->lum_stripe_count = param->lsp_stripe_count;
934 lmu->lum_stripe_offset = param->lsp_stripe_offset;
935 lmu->lum_hash_type = param->lsp_stripe_pattern;
936 if (param->lsp_pool != NULL)
937 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
938 if (param->lsp_is_specific) {
941 for (i = 0; i < param->lsp_stripe_count; i++)
942 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
946 int llapi_dir_set_default_lmv(const char *name,
947 const struct llapi_stripe_param *param)
949 struct lmv_user_md lmu = { 0 };
953 rc = verify_dir_param(name, param);
957 /* TODO: default lmv doesn't support specific targets yet */
958 if (param->lsp_is_specific)
961 param2lmu(&lmu, param);
963 fd = open(name, O_DIRECTORY | O_RDONLY);
966 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
970 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
972 char *errmsg = "stripe already set";
974 if (errno != EEXIST && errno != EALREADY)
975 errmsg = strerror(errno);
977 llapi_err_noerrno(LLAPI_MSG_ERROR,
978 "default dirstripe error on '%s': %s",
985 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
986 int stripe_count, int stripe_pattern,
987 const char *pool_name)
989 const struct llapi_stripe_param param = {
990 .lsp_stripe_count = stripe_count,
991 .lsp_stripe_offset = stripe_offset,
992 .lsp_stripe_pattern = stripe_pattern,
993 .lsp_pool = (char *)pool_name
996 return llapi_dir_set_default_lmv(name, ¶m);
1000 * Create a Lustre directory.
1002 * \param name the name of the directory to be created
1003 * \param mode permission of the file if it is created, see mode in open(2)
1004 * \param param stripe pattern of the newly created directory
1006 * \retval 0 on success
1007 * \retval negative errno on failure
1009 int llapi_dir_create(const char *name, mode_t mode,
1010 const struct llapi_stripe_param *param)
1012 struct lmv_user_md *lmu = NULL;
1014 struct obd_ioctl_data data = { 0 };
1017 char *dirpath = NULL;
1018 char *namepath = NULL;
1023 rc = verify_dir_param(name, param);
1027 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1028 param->lsp_is_specific ?
1029 LMV_USER_MAGIC_SPECIFIC :
1032 lmu = calloc(1, lmu_size);
1036 dirpath = strdup(name);
1042 namepath = strdup(name);
1049 param2lmu(lmu, param);
1051 filename = basename(namepath);
1052 dir = dirname(dirpath);
1054 data.ioc_inlbuf1 = (char *)filename;
1055 data.ioc_inllen1 = strlen(filename) + 1;
1056 data.ioc_inlbuf2 = (char *)lmu;
1057 data.ioc_inllen2 = lmu_size;
1058 data.ioc_type = mode;
1059 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1061 llapi_error(LLAPI_MSG_ERROR, rc,
1062 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1067 fd = open(dir, O_DIRECTORY | O_RDONLY);
1070 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1074 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1075 char *errmsg = "stripe already set";
1078 if (errno != EEXIST && errno != EALREADY)
1079 errmsg = strerror(errno);
1081 llapi_err_noerrno(LLAPI_MSG_ERROR,
1082 "dirstripe error on '%s': %s", name, errmsg);
1093 * Create a foreign directory.
1095 * \param name the name of the directory to be created
1096 * \param mode permission of the file if it is created, see mode in open(2)
1097 * \param type foreign type to be set in LMV EA
1098 * \param flags foreign flags to be set in LMV EA
1099 * \param value foreign pattern to be set in LMV EA
1101 * \retval 0 on success
1102 * \retval negative errno on failure
1104 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1105 __u32 flags, const char *value)
1107 struct lmv_foreign_md *lfm = NULL;
1108 size_t lfm_size, len;
1109 struct obd_ioctl_data data = { 0 };
1112 char *dirpath = NULL;
1113 char *namepath = NULL;
1118 len = strlen(value);
1119 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1122 llapi_error(LLAPI_MSG_ERROR, rc, "invalid LOV EA length %zu "
1123 "(must be 0 < len < %zu)\n", len,
1125 offsetof(struct lmv_foreign_md, lfm_value));
1128 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1129 lfm = calloc(1, lfm_size);
1133 dirpath = strdup(name);
1139 namepath = strdup(name);
1146 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1147 lfm->lfm_length = len;
1148 lfm->lfm_type = type;
1149 lfm->lfm_flags = flags;
1150 memcpy(lfm->lfm_value, value, len);
1152 filename = basename(namepath);
1153 dir = dirname(dirpath);
1155 data.ioc_inlbuf1 = (char *)filename;
1156 data.ioc_inllen1 = strlen(filename) + 1;
1157 data.ioc_inlbuf2 = (char *)lfm;
1158 data.ioc_inllen2 = lfm_size;
1159 data.ioc_type = mode;
1160 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1162 llapi_error(LLAPI_MSG_ERROR, rc,
1163 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1168 fd = open(dir, O_DIRECTORY | O_RDONLY);
1171 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1175 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1176 char *errmsg = "stripe already set";
1179 if (errno != EEXIST && errno != EALREADY)
1180 errmsg = strerror(errno);
1182 llapi_err_noerrno(LLAPI_MSG_ERROR,
1183 "dirstripe error on '%s': %s", name, errmsg);
1193 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1194 int stripe_count, int stripe_pattern,
1195 const char *pool_name)
1197 const struct llapi_stripe_param param = {
1198 .lsp_stripe_count = stripe_count,
1199 .lsp_stripe_offset = stripe_offset,
1200 .lsp_stripe_pattern = stripe_pattern,
1201 .lsp_pool = (char *)pool_name
1204 return llapi_dir_create(name, mode, ¶m);
1207 int llapi_direntry_remove(char *dname)
1209 char *dirpath = NULL;
1210 char *namepath = NULL;
1216 dirpath = strdup(dname);
1217 namepath = strdup(dname);
1218 if (!dirpath || !namepath)
1221 filename = basename(namepath);
1223 dir = dirname(dirpath);
1225 fd = open(dir, O_DIRECTORY | O_RDONLY);
1228 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
1233 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
1234 char *errmsg = strerror(errno);
1235 llapi_err_noerrno(LLAPI_MSG_ERROR,
1236 "error on ioctl %#jx for '%s' (%d): %s",
1237 (uintmax_t)LL_IOC_LMV_SETSTRIPE, filename,
1249 * Find the fsname, the full path, and/or an open fd.
1250 * Either the fsname or path must not be NULL
1252 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1255 char buf[PATH_MAX], mntdir[PATH_MAX];
1256 char *ptr, *ptr_end;
1258 int idx = 0, len = 0, mntlen, fd;
1261 /* get the mount point */
1262 fp = setmntent(PROC_MOUNTS, "r");
1265 llapi_error(LLAPI_MSG_ERROR, rc,
1266 "cannot retrieve filesystem mount point");
1270 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1273 if (!llapi_is_lustre_mnt(&mnt))
1276 if ((want & WANT_INDEX) && (idx++ != index))
1279 mntlen = strlen(mnt.mnt_dir);
1280 ptr = strchr(mnt.mnt_fsname, '/');
1281 while (ptr && *ptr == '/')
1283 /* thanks to the call to llapi_is_lustre_mnt() above,
1284 * we are sure that mnt.mnt_fsname contains ":/",
1285 * so ptr should never be NULL */
1289 while (*ptr_end != '/' && *ptr_end != '\0')
1292 /* Check the fsname for a match, if given */
1293 if (!(want & WANT_FSNAME) && fsname != NULL &&
1294 (strlen(fsname) > 0) &&
1295 (strncmp(ptr, fsname, ptr_end - ptr) != 0))
1298 /* If the path isn't set return the first one we find */
1299 if (path == NULL || strlen(path) == 0) {
1300 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1301 mntdir[strlen(mnt.mnt_dir)] = '\0';
1302 if ((want & WANT_FSNAME) && fsname != NULL) {
1303 strncpy(fsname, ptr, ptr_end - ptr);
1304 fsname[ptr_end - ptr] = '\0';
1308 /* Otherwise find the longest matching path */
1309 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1310 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1311 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir));
1312 mntdir[strlen(mnt.mnt_dir)] = '\0';
1314 if ((want & WANT_FSNAME) && fsname != NULL) {
1315 strncpy(fsname, ptr, ptr_end - ptr);
1316 fsname[ptr_end - ptr] = '\0';
1325 if ((want & WANT_PATH) && path != NULL) {
1326 strncpy(path, mntdir, PATH_MAX);
1327 path[strlen(mntdir)] = '\0';
1329 if (want & WANT_FD) {
1330 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1333 llapi_error(LLAPI_MSG_ERROR, rc,
1334 "cannot open '%s': %s", mntdir,
1341 } else if (want & WANT_ERROR)
1342 llapi_err_noerrno(LLAPI_MSG_ERROR,
1343 "'%s' not on a mounted Lustre filesystem",
1344 (want & WANT_PATH) ? fsname : path);
1349 * search lustre mounts
1351 * Calling this function will return to the user the mount point, mntdir, and
1352 * the file system name, fsname, if the user passed a buffer to this routine.
1354 * The user inputs are pathname and index. If the pathname is supplied then
1355 * the value of the index will be ignored. The pathname will return data if
1356 * the pathname is located on a lustre mount. Index is used to pick which
1357 * mount point you want in the case of multiple mounted lustre file systems.
1358 * See function lfs_osts in lfs.c for an example of the index use.
1360 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1363 int want = WANT_PATH, idx = -1;
1365 if (!pathname || pathname[0] == '\0') {
1369 strcpy(mntdir, pathname);
1372 want |= WANT_FSNAME;
1373 return get_root_path(want, fsname, NULL, mntdir, idx);
1376 /* Given a path, find the corresponding Lustre fsname */
1377 int llapi_search_fsname(const char *pathname, char *fsname)
1382 path = realpath(pathname, NULL);
1384 char tmp[PATH_MAX - 1];
1390 if (pathname[0] != '/') {
1391 /* Need an absolute path, but realpath() only works for
1392 * pathnames that actually exist. We go through the
1393 * extra hurdle of dirname(getcwd() + pathname) in
1394 * case the relative pathname contains ".." in it. */
1395 char realpath[PATH_MAX - 1];
1397 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1399 llapi_error(LLAPI_MSG_ERROR, rc,
1400 "cannot get current working directory");
1404 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1405 if (rc >= sizeof(tmp)) {
1407 llapi_error(LLAPI_MSG_ERROR, rc,
1408 "invalid parent path '%s'",
1414 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1415 if (rc >= sizeof(buf)) {
1417 llapi_error(LLAPI_MSG_ERROR, rc,
1418 "invalid path '%s'", pathname);
1421 path = realpath(buf, NULL);
1423 ptr = strrchr(buf, '/');
1425 llapi_error(LLAPI_MSG_ERROR |
1426 LLAPI_MSG_NO_ERRNO, 0,
1427 "cannot resolve path '%s'",
1432 path = realpath(buf, NULL);
1435 llapi_error(LLAPI_MSG_ERROR, rc,
1436 "cannot resolve path '%s'",
1442 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1447 int llapi_search_rootpath(char *pathname, const char *fsname)
1449 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1452 int llapi_getname(const char *path, char *buf, size_t size)
1454 struct obd_uuid uuid_buf;
1455 char *uuid = uuid_buf.uuid;
1457 int rc, len, fsname_len;
1459 memset(&uuid_buf, 0, sizeof(uuid_buf));
1460 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1465 * We want to turn testfs-clilov-ffff88002738bc00 into
1466 * testfs-ffff88002738bc00 in a portable way that doesn't depend
1467 * on what is after "-clilov-" as it may change in the future.
1468 * Unfortunately, the "fsname" part may contain a dash, so we
1469 * can't just skip to the first dash, and the "instance" may be a
1470 * UUID in the future, so we can't necessarily go to the last dash.
1472 cfg_instance = strstr(uuid, "-clilov-");
1476 fsname_len = cfg_instance - uuid;
1477 cfg_instance += strlen("-clilov-");
1478 len = snprintf(buf, size, "%.*s-%s", fsname_len, uuid, cfg_instance);
1487 * Get the list of pool members.
1488 * \param poolname string of format \<fsname\>.\<poolname\>
1489 * \param members caller-allocated array of char*
1490 * \param list_size size of the members array
1491 * \param buffer caller-allocated buffer for storing OST names
1492 * \param buffer_size size of the buffer
1494 * \return number of members retrieved for this pool
1495 * \retval -error failure
1497 int llapi_get_poolmembers(const char *poolname, char **members,
1498 int list_size, char *buffer, int buffer_size)
1500 char fsname[PATH_MAX];
1509 /* name is FSNAME.POOLNAME */
1510 if (strlen(poolname) >= sizeof(fsname))
1513 snprintf(fsname, sizeof(fsname), "%s", poolname);
1514 pool = strchr(fsname, '.');
1521 rc = poolpath(&pathname, fsname, NULL);
1523 llapi_error(LLAPI_MSG_ERROR, rc,
1524 "Lustre filesystem '%s' not found",
1529 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1530 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1531 cfs_free_param_data(&pathname);
1532 if (rc >= sizeof(buf))
1534 fd = fopen(buf, "r");
1537 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1542 while (fgets(buf, sizeof(buf), fd) != NULL) {
1543 if (nb_entries >= list_size) {
1547 buf[sizeof(buf) - 1] = '\0';
1549 tmp = strchr(buf, '\n');
1552 if (used + strlen(buf) + 1 > buffer_size) {
1557 strcpy(buffer + used, buf);
1558 members[nb_entries] = buffer + used;
1559 used += strlen(buf) + 1;
1569 * Get the list of pools in a filesystem.
1570 * \param name filesystem name or path
1571 * \param poollist caller-allocated array of char*
1572 * \param list_size size of the poollist array
1573 * \param buffer caller-allocated buffer for storing pool names
1574 * \param buffer_size size of the buffer
1576 * \return number of pools retrieved for this filesystem
1577 * \retval -error failure
1579 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1580 char *buffer, int buffer_size)
1582 char rname[PATH_MAX];
1587 struct dirent *pool;
1589 unsigned int nb_entries = 0;
1590 unsigned int used = 0;
1593 /* initialize output array */
1594 for (i = 0; i < list_size; i++)
1597 /* is name a pathname ? */
1598 ptr = strchr(name, '/');
1600 /* only absolute pathname is supported */
1604 if (!realpath(name, rname)) {
1606 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1611 fsname = strdup(rname);
1615 rc = poolpath(&pathname, NULL, rname);
1617 /* name is FSNAME */
1618 fsname = strdup(name);
1621 rc = poolpath(&pathname, fsname, NULL);
1624 llapi_error(LLAPI_MSG_ERROR, rc,
1625 "Lustre filesystem '%s' not found", name);
1629 dir = opendir(pathname.gl_pathv[0]);
1632 llapi_error(LLAPI_MSG_ERROR, rc,
1633 "Could not open pool list for '%s'",
1640 pool = readdir(dir);
1646 /* ignore . and .. */
1647 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1650 /* check output bounds */
1651 if (nb_entries >= list_size) {
1653 goto free_dir_no_msg;
1656 /* +2 for '.' and final '\0' */
1657 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1660 goto free_dir_no_msg;
1663 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1664 poollist[nb_entries] = buffer + used;
1665 used += strlen(pool->d_name) + strlen(fsname) + 2;
1671 llapi_error(LLAPI_MSG_ERROR, rc,
1672 "Error reading pool list for '%s'", name);
1674 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1679 cfs_free_param_data(&pathname);
1682 return rc != 0 ? rc : nb_entries;
1685 /* wrapper for lfs.c and obd.c */
1686 int llapi_poollist(const char *name)
1688 /* list of pool names (assume that pool count is smaller
1690 char **list, *buffer = NULL, *fsname = (char *)name;
1691 char *poolname = NULL, *tmp = NULL, data[16];
1692 enum param_filter type = FILTER_BY_PATH;
1693 int obdcount, bufsize, rc, nb, i;
1698 if (name[0] != '/') {
1699 fsname = strdup(name);
1703 poolname = strchr(fsname, '.');
1706 type = FILTER_BY_FS_NAME;
1709 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1710 data, sizeof(data));
1713 obdcount = atoi(data);
1715 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1716 * and also an array to store the pointers for all that
1717 * allocated space. */
1719 bufsize = sizeof(struct obd_uuid) * obdcount;
1720 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1721 if (buffer == NULL) {
1725 list = (char **) (buffer + bufsize);
1728 /* name is a path or fsname */
1729 nb = llapi_get_poollist(name, list, obdcount,
1732 /* name is a pool name (<fsname>.<poolname>) */
1733 nb = llapi_get_poolmembers(name, list, obdcount,
1737 if (nb == -EOVERFLOW) {
1740 goto retry_get_pools;
1743 for (i = 0; i < nb; i++)
1744 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1745 rc = (nb < 0 ? nb : 0);
1749 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1754 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1755 void *data, struct dirent64 *de);
1757 #define OBD_NOT_FOUND (-1)
1759 static void find_param_fini(struct find_param *param)
1761 if (param->fp_migrate)
1764 if (param->fp_obd_indexes) {
1765 free(param->fp_obd_indexes);
1766 param->fp_obd_indexes = NULL;
1769 if (param->fp_lmd) {
1770 free(param->fp_lmd);
1771 param->fp_lmd = NULL;
1774 if (param->fp_lmv_md) {
1775 free(param->fp_lmv_md);
1776 param->fp_lmv_md = NULL;
1780 static int common_param_init(struct find_param *param, char *path)
1782 int lum_size = get_mds_md_size(path);
1787 /* migrate has fp_lmv_md initialized outside */
1788 if (param->fp_migrate)
1791 if (lum_size < PATH_MAX + 1)
1792 lum_size = PATH_MAX + 1;
1794 param->fp_lum_size = lum_size;
1795 param->fp_lmd = calloc(1, sizeof(lstat_t) + lum_size);
1796 if (param->fp_lmd == NULL) {
1797 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1798 "error: allocation of %zu bytes for ioctl",
1799 sizeof(lstat_t) + param->fp_lum_size);
1803 param->fp_lmv_stripe_count = 256;
1804 param->fp_lmv_md = calloc(1,
1805 lmv_user_md_size(param->fp_lmv_stripe_count,
1806 LMV_USER_MAGIC_SPECIFIC));
1807 if (param->fp_lmv_md == NULL) {
1808 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1809 "error: allocation of %d bytes for ioctl",
1810 lmv_user_md_size(param->fp_lmv_stripe_count,
1811 LMV_USER_MAGIC_SPECIFIC));
1812 find_param_fini(param);
1816 param->fp_got_uuids = 0;
1817 param->fp_obd_indexes = NULL;
1818 param->fp_obd_index = OBD_NOT_FOUND;
1819 param->fp_mdt_index = OBD_NOT_FOUND;
1823 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1824 struct dirent64 *de)
1826 struct find_param *param = data;
1832 /* set errno upon failure */
1833 static DIR *opendir_parent(const char *path)
1839 path_copy = strdup(path);
1840 if (path_copy == NULL)
1843 parent_path = dirname(path_copy);
1844 parent = opendir(parent_path);
1850 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1855 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1856 if (param->fp_get_default_lmv)
1857 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1859 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1861 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1862 if (errno == E2BIG && ret != 0) {
1866 /* if foreign LMV case, fake stripes number */
1867 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
1868 struct lmv_foreign_md *lfm;
1870 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1871 if (lfm->lfm_length < XATTR_SIZE_MAX -
1872 offsetof(typeof(*lfm), lfm_value)) {
1873 uint32_t size = lfm->lfm_length +
1874 offsetof(typeof(*lfm), lfm_value);
1876 stripe_count = lmv_foreign_to_md_stripes(size);
1878 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1879 "error: invalid %d foreign size "
1880 "returned from ioctl",
1885 stripe_count = param->fp_lmv_md->lum_stripe_count;
1887 if (stripe_count <= param->fp_lmv_stripe_count)
1890 free(param->fp_lmv_md);
1891 param->fp_lmv_stripe_count = stripe_count;
1892 lmv_size = lmv_user_md_size(stripe_count,
1893 LMV_USER_MAGIC_SPECIFIC);
1894 param->fp_lmv_md = malloc(lmv_size);
1895 if (param->fp_lmv_md == NULL) {
1896 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1897 "error: allocation of %d bytes for ioctl",
1898 lmv_user_md_size(param->fp_lmv_stripe_count,
1899 LMV_USER_MAGIC_SPECIFIC));
1907 int get_lmd_info_fd(char *path, int parent_fd, int dir_fd,
1908 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1910 struct lov_user_mds_data *lmd = lmdbuf;
1911 lstat_t *st = &lmd->lmd_st;
1914 if (parent_fd < 0 && dir_fd < 0)
1916 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1920 /* LL_IOC_MDC_GETINFO operates on the current directory inode
1921 * and returns struct lov_user_mds_data, while
1922 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
1924 ret = ioctl(dir_fd, type == GET_LMD_INFO ? LL_IOC_MDC_GETINFO :
1925 LL_IOC_LOV_GETSTRIPE,
1927 } else if (parent_fd >= 0) {
1928 char *fname = strrchr(path, '/');
1930 /* IOC_MDC_GETFILEINFO takes as input the filename (relative to
1931 * the parent directory) and returns struct lov_user_mds_data,
1932 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
1934 * This avoids opening, locking, and closing each file on the
1935 * client if that is not needed. Multiple of these ioctl() can
1936 * be done on the parent dir with a single open for all
1937 * files in that directory, and it also doesn't pollute the
1938 * client dcache with millions of dentries when traversing
1939 * a large filesystem.
1941 fname = (fname == NULL ? path : fname + 1);
1943 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
1946 else if (ret >= lmdlen || ret++ == 0)
1949 ret = ioctl(parent_fd, type == GET_LMD_INFO ?
1950 IOC_MDC_GETFILEINFO :
1951 IOC_MDC_GETFILESTRIPE, lmdbuf);
1954 if (ret && type == GET_LMD_INFO) {
1955 if (errno == ENOTTY) {
1956 /* ioctl is not supported, it is not a lustre fs.
1957 * Do the regular lstat(2) instead.
1959 ret = lstat_f(path, st);
1962 llapi_error(LLAPI_MSG_ERROR, ret,
1963 "error: %s: lstat failed for %s",
1966 } else if (errno == ENOENT) {
1968 llapi_error(LLAPI_MSG_WARN, ret,
1969 "warning: %s does not exist", path);
1970 } else if (errno != EISDIR && errno != ENODATA) {
1972 llapi_error(LLAPI_MSG_ERROR, ret,
1973 "%s ioctl failed for %s.",
1974 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
1975 "IOC_MDC_GETFILEINFO", path);
1982 static int get_lmd_info(char *path, DIR *parent, DIR *dir, void *lmdbuf,
1983 int lmdlen, enum get_lmd_info_type type)
1989 parent_fd = dirfd(parent);
1991 dir_fd = dirfd(dir);
1993 return get_lmd_info_fd(path, parent_fd, dir_fd, lmdbuf, lmdlen, type);
1996 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1997 semantic_func_t sem_init,
1998 semantic_func_t sem_fini, void *data,
1999 struct dirent64 *de)
2001 struct find_param *param = (struct find_param *)data;
2002 struct dirent64 *dent;
2010 if (!d && errno != ENOTDIR) {
2012 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2015 } else if (!d && !parent) {
2016 /* ENOTDIR. Open the parent dir. */
2017 p = opendir_parent(path);
2024 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
2030 while ((dent = readdir64(d)) != NULL) {
2033 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2037 if ((len + dent->d_reclen + 2) > size) {
2038 llapi_err_noerrno(LLAPI_MSG_ERROR,
2039 "error: %s: string buffer too small",
2044 strcat(path, dent->d_name);
2046 if (dent->d_type == DT_UNKNOWN) {
2047 lstat_t *st = ¶m->fp_lmd->lmd_st;
2049 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
2050 param->fp_lum_size, GET_LMD_INFO);
2052 dent->d_type = IFTODT(st->st_mode);
2059 switch (dent->d_type) {
2061 llapi_err_noerrno(LLAPI_MSG_ERROR,
2062 "error: %s: '%s' is UNKNOWN type %d",
2063 __func__, dent->d_name, dent->d_type);
2066 rc = llapi_semantic_traverse(path, size, d, sem_init,
2067 sem_fini, data, dent);
2068 if (rc != 0 && ret == 0)
2074 rc = sem_init(path, d, NULL, data, dent);
2075 if (rc < 0 && ret == 0) {
2080 if (sem_fini && rc == 0)
2081 sem_fini(path, d, NULL, data, dent);
2089 sem_fini(path, parent, &d, data, de);
2098 static int param_callback(char *path, semantic_func_t sem_init,
2099 semantic_func_t sem_fini, struct find_param *param)
2101 int ret, len = strlen(path);
2104 if (len > PATH_MAX) {
2106 llapi_error(LLAPI_MSG_ERROR, ret,
2107 "Path name '%s' is too long", path);
2111 buf = (char *)malloc(PATH_MAX + 1);
2115 snprintf(buf, PATH_MAX + 1, "%s", path);
2116 ret = common_param_init(param, buf);
2120 param->fp_depth = 0;
2122 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
2123 sem_fini, param, NULL);
2125 find_param_fini(param);
2127 return ret < 0 ? ret : 0;
2130 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2132 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
2136 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2141 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2143 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2146 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2151 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2155 fd = open(path, O_RDONLY | O_NONBLOCK);
2158 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
2162 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2168 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2172 fd = open(path, O_RDONLY | O_NONBLOCK);
2175 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2179 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2191 * If uuidp is NULL, return the number of available obd uuids.
2192 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2193 * there are more OSTs than allocated to uuidp, then an error is returned with
2194 * the ost_count set to number of available obd uuids.
2196 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2197 int *ost_count, enum tgt_type type)
2199 char buf[PATH_MAX], format[32];
2200 int rc = 0, index = 0;
2201 struct obd_uuid name;
2205 /* Get the lov name */
2206 if (type == LOV_TYPE)
2207 rc = llapi_file_fget_lov_uuid(fd, &name);
2209 rc = llapi_file_fget_lmv_uuid(fd, &name);
2213 /* Now get the ost uuids */
2214 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2215 FILTER_BY_EXACT, "target_obd", ¶m);
2219 fp = fopen(param.gl_pathv[0], "r");
2222 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2227 snprintf(format, sizeof(format),
2228 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2229 while (fgets(buf, sizeof(buf), fp) != NULL) {
2230 if (uuidp && (index < *ost_count)) {
2231 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2239 if (uuidp && (index > *ost_count))
2244 cfs_free_param_data(¶m);
2248 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2250 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2253 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2258 root = open(mnt, O_RDONLY | O_DIRECTORY);
2261 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2266 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2274 /* Check if user specified value matches a real uuid. Ignore _UUID,
2275 * -osc-4ba41334, other trailing gunk in comparison.
2276 * @param real_uuid ends in "_UUID"
2277 * @param search_uuid may or may not end in "_UUID"
2279 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2281 int cmplen = strlen(real_uuid);
2282 int searchlen = strlen(search_uuid);
2284 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2286 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2289 /* The UUIDs may legitimately be different lengths, if
2290 * the system was upgraded from an older version. */
2291 if (cmplen != searchlen)
2294 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2297 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2298 * returned in param->fp_obd_index */
2299 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2301 struct obd_uuid obd_uuid;
2308 if (param->fp_got_uuids)
2311 /* Get the lov/lmv name */
2312 if (param->fp_get_lmv)
2313 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2315 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2317 if (rc != -ENOTTY) {
2318 llapi_error(LLAPI_MSG_ERROR, rc,
2319 "error: can't get %s name: %s",
2320 param->fp_get_lmv ? "lmv" : "lov",
2328 param->fp_got_uuids = 1;
2330 /* Now get the ost uuids */
2331 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2332 obd_uuid.uuid, FILTER_BY_EXACT,
2333 "target_obd", ¶m_data);
2337 fp = fopen(param_data.gl_pathv[0], "r");
2340 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2341 param_data.gl_pathv[0]);
2345 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2346 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2347 param->fp_get_lmv ? "MDTS" : "OBDS");
2349 snprintf(format, sizeof(format),
2350 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2351 while (fgets(buf, sizeof(buf), fp) != NULL) {
2354 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2357 if (param->fp_obd_uuid) {
2358 if (llapi_uuid_match(obd_uuid.uuid,
2359 param->fp_obd_uuid->uuid)) {
2360 param->fp_obd_index = index;
2363 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2364 /* Print everything */
2365 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2368 param->fp_obds_printed = 1;
2372 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2373 llapi_err_noerrno(LLAPI_MSG_ERROR,
2374 "error: %s: unknown obduuid: %s",
2375 __func__, param->fp_obd_uuid->uuid);
2379 cfs_free_param_data(¶m_data);
2383 /* In this case, param->fp_obd_uuid will be an array of obduuids and
2384 * obd index for all these obduuids will be returned in
2385 * param->fp_obd_indexes */
2386 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2387 int num_obds, int **obdindexes, int *obdindex,
2390 int ret, obdcount, obd_valid = 0, obdnum;
2392 struct obd_uuid *uuids = NULL;
2396 if (type == LOV_TYPE)
2397 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2399 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2403 obdcount = atoi(buf);
2404 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2409 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2411 if (ret == -EOVERFLOW) {
2412 struct obd_uuid *uuids_temp;
2414 uuids_temp = realloc(uuids, obdcount *
2415 sizeof(struct obd_uuid));
2416 if (uuids_temp != NULL) {
2418 goto retry_get_uuids;
2423 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2427 indexes = malloc(num_obds * sizeof(*obdindex));
2428 if (indexes == NULL) {
2433 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2436 /* The user may have specified a simple index */
2437 i = strtol(obduuids[obdnum].uuid, &end, 0);
2438 if (end && *end == '\0' && i < obdcount) {
2439 indexes[obdnum] = i;
2442 for (i = 0; i < obdcount; i++) {
2443 if (llapi_uuid_match(uuids[i].uuid,
2444 obduuids[obdnum].uuid)) {
2445 indexes[obdnum] = i;
2451 if (i >= obdcount) {
2452 indexes[obdnum] = OBD_NOT_FOUND;
2453 llapi_err_noerrno(LLAPI_MSG_ERROR,
2454 "invalid obduuid '%s'",
2455 obduuids[obdnum].uuid);
2461 *obdindex = OBD_NOT_FOUND;
2463 *obdindex = obd_valid;
2465 *obdindexes = indexes;
2473 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2477 if (param->fp_mdt_uuid) {
2478 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2480 ¶m->fp_mdt_indexes,
2481 ¶m->fp_mdt_index, LMV_TYPE);
2486 if (param->fp_obd_uuid) {
2487 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2489 ¶m->fp_obd_indexes,
2490 ¶m->fp_obd_index, LOV_TYPE);
2495 param->fp_got_uuids = 1;
2500 int llapi_ostlist(char *path, struct find_param *param)
2505 fd = open(path, O_RDONLY | O_DIRECTORY);
2509 ret = setup_obd_uuid(fd, path, param);
2516 * Tries to determine the default stripe attributes for a given filesystem. The
2517 * filesystem to check should be specified by fsname, or will be determined
2520 static int sattr_get_defaults(const char *const fsname,
2521 unsigned int *scount,
2522 unsigned int *ssize,
2523 unsigned int *soffset)
2529 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2530 "stripecount", val, sizeof(val));
2533 *scount = atoi(val);
2537 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2538 "stripesize", val, sizeof(val));
2545 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2546 "stripeoffset", val, sizeof(val));
2549 *soffset = atoi(val);
2556 * Tries to gather the default stripe attributes for a given filesystem. If
2557 * the attributes can be determined, they are cached for easy retreival the
2558 * next time they are needed. Only a single filesystem's attributes are
2561 int sattr_cache_get_defaults(const char *const fsname,
2562 const char *const pathname, unsigned int *scount,
2563 unsigned int *ssize, unsigned int *soffset)
2566 char fsname[PATH_MAX + 1];
2567 unsigned int stripecount;
2568 unsigned int stripesize;
2569 unsigned int stripeoffset;
2575 char fsname_buf[PATH_MAX + 1];
2576 unsigned int tmp[3];
2578 if (fsname == NULL) {
2579 rc = llapi_search_fsname(pathname, fsname_buf);
2583 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2586 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2588 * Ensure all 3 sattrs (count, size, and offset) are
2589 * successfully retrieved and stored in tmp before writing to
2592 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2596 cache.stripecount = tmp[0];
2597 cache.stripesize = tmp[1];
2598 cache.stripeoffset = tmp[2];
2599 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2603 *scount = cache.stripecount;
2605 *ssize = cache.stripesize;
2607 *soffset = cache.stripeoffset;
2612 static char *layout2name(__u32 layout_pattern)
2614 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2616 else if (layout_pattern == LOV_PATTERN_MDT)
2618 else if (layout_pattern == LOV_PATTERN_RAID0)
2620 else if (layout_pattern ==
2621 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2622 return "raid0,overstriped";
2627 enum lov_dump_flags {
2628 LDF_IS_DIR = 0x0001,
2629 LDF_IS_RAW = 0x0002,
2630 LDF_INDENT = 0x0004,
2631 LDF_SKIP_OBJS = 0x0008,
2635 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2636 struct lov_user_ost_data_v1 *objects,
2637 enum llapi_layout_verbose verbose,
2638 int depth, char *pool_name,
2639 enum lov_dump_flags flags)
2641 bool is_dir = flags & LDF_IS_DIR;
2642 bool is_raw = flags & LDF_IS_RAW;
2643 bool indent = flags & LDF_INDENT;
2644 bool yaml = flags & LDF_YAML;
2645 bool skip_objs = flags & LDF_SKIP_OBJS;
2646 char *prefix = is_dir ? "" : "lmm_";
2647 char *separator = "";
2648 char *space = indent ? " " : "";
2651 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2652 lmm_oi_set_seq(&lum->lmm_oi, 0);
2653 if (!indent && (verbose & VERBOSE_DETAIL))
2654 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2657 if (!yaml && !indent && depth && path &&
2658 ((verbose != VERBOSE_OBJID) || !is_dir))
2659 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2661 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2662 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2663 space, prefix, lum->lmm_magic);
2664 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2666 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2667 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2669 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2671 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2676 if (verbose & ~VERBOSE_DFID)
2677 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2679 /* This needs a bit of hand-holding since old 1.x lmm_oi
2680 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2681 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2682 * a real FID. Ideally the 2.x code would have stored this
2683 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2684 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2685 * worked properly (especially since IGIF FIDs use mds_inum as
2686 * the FID SEQ), but unfortunately that didn't happen.
2688 * Print it to look like an IGIF FID, even though the fields
2689 * are reversed on disk, so that it makes sense to userspace.
2691 * Don't use ostid_id() and ostid_seq(), since they assume the
2692 * oi_fid fields are in the right order. This is why there are
2693 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2695 * For newer layout types hopefully this will be a real FID. */
2696 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2697 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2698 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2699 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2700 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2702 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2705 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2709 if (verbose & VERBOSE_STRIPE_COUNT) {
2710 if (verbose & ~VERBOSE_STRIPE_COUNT)
2711 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2714 if (!is_raw && lum->lmm_stripe_count == 0 &&
2715 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2716 unsigned int scount;
2717 rc = sattr_cache_get_defaults(NULL, path,
2721 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2724 llapi_error(LLAPI_MSG_ERROR, rc,
2725 "Cannot determine default"
2728 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2729 lum->lmm_stripe_count ==
2730 (typeof(lum->lmm_stripe_count))(-1)
2731 ? -1 : lum->lmm_stripe_count);
2734 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2735 (__s16)lum->lmm_stripe_count);
2737 if (!yaml && is_dir)
2743 if (verbose & VERBOSE_STRIPE_SIZE) {
2744 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2745 if (verbose & ~VERBOSE_STRIPE_SIZE)
2746 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2748 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2750 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2753 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2755 llapi_error(LLAPI_MSG_ERROR, rc,
2756 "Cannot determine default"
2759 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2760 lum->lmm_stripe_size);
2762 if (!yaml && is_dir)
2768 if ((verbose & VERBOSE_PATTERN)) {
2769 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2770 if (verbose & ~VERBOSE_PATTERN)
2771 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2773 if (lov_pattern_supported(lum->lmm_pattern))
2774 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2775 layout2name(lum->lmm_pattern));
2777 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2778 separator = (!yaml && is_dir) ? " " : "\n";
2781 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2782 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2783 if (verbose & ~VERBOSE_GENERATION)
2784 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2786 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2787 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2791 if (verbose & VERBOSE_STRIPE_OFFSET) {
2792 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2793 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2794 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2796 if (is_dir || skip_objs)
2797 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2798 lum->lmm_stripe_offset ==
2799 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2800 lum->lmm_stripe_offset);
2801 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
2802 llapi_printf(LLAPI_MSG_NORMAL, "0");
2804 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2805 objects[0].l_ost_idx);
2806 if (!yaml && is_dir)
2812 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2813 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2814 if (verbose & ~VERBOSE_POOL)
2815 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2817 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2818 if (!yaml && is_dir)
2824 if (strlen(separator) != 0)
2825 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2828 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2829 struct lov_user_ost_data_v1 *objects,
2830 char *path, int obdindex, int depth,
2831 enum llapi_layout_verbose verbose,
2832 enum lov_dump_flags flags)
2834 bool is_dir = flags & LDF_IS_DIR;
2835 bool indent = flags & LDF_INDENT;
2836 bool skip_objs = flags & LDF_SKIP_OBJS;
2837 bool yaml = flags & LDF_YAML;
2838 bool obdstripe = obdindex == OBD_NOT_FOUND;
2841 if (!obdstripe && !skip_objs) {
2842 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2843 if (obdindex == objects[i].l_ost_idx) {
2853 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2856 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
2857 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2858 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
2859 char *space = " - ";
2862 llapi_printf(LLAPI_MSG_NORMAL,
2863 "%6slmm_objects:\n", " ");
2865 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2867 llapi_printf(LLAPI_MSG_NORMAL,
2868 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2870 for (i = 0; i < lum->lmm_stripe_count; i++) {
2871 int idx = objects[i].l_ost_idx;
2872 long long oid = ostid_id(&objects[i].l_ost_oi);
2873 long long gr = ostid_seq(&objects[i].l_ost_oi);
2875 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2879 struct lu_fid fid = { 0 };
2881 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2882 llapi_printf(LLAPI_MSG_NORMAL,
2883 "%sl_ost_idx: %d\n", space, idx);
2884 llapi_printf(LLAPI_MSG_NORMAL,
2885 "%8sl_fid: "DFID_NOBRACE"\n",
2887 } else if (indent) {
2888 struct lu_fid fid = { 0 };
2890 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2891 llapi_printf(LLAPI_MSG_NORMAL,
2892 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
2893 space, i, idx, PFID(&fid));
2896 sprintf(fmt, "%s%s%s\n",
2897 "\t%6u\t%14llu\t%#13llx\t",
2898 (fid_seq_is_rsvd(gr) ||
2899 fid_seq_is_mdt0(gr)) ?
2900 "%14llu" : "%#14llx", "%s");
2901 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2903 obdindex == idx ? " *" : "");
2907 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2910 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2911 char *path, int obdindex, int depth,
2912 enum llapi_layout_verbose verbose,
2913 enum lov_dump_flags flags)
2915 struct lmv_user_mds_data *objects = lum->lum_objects;
2916 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2917 char *separator = "";
2918 bool yaml = flags & LDF_YAML;
2919 bool obdstripe = false;
2922 if (obdindex != OBD_NOT_FOUND) {
2923 if (lum->lum_stripe_count == 0) {
2924 if (obdindex == lum->lum_stripe_offset)
2927 for (i = 0; i < lum->lum_stripe_count; i++) {
2928 if (obdindex == objects[i].lum_mds) {
2929 llapi_printf(LLAPI_MSG_NORMAL,
2944 /* show all information default */
2946 if (lum->lum_magic == LMV_USER_MAGIC)
2947 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
2948 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
2950 verbose = VERBOSE_OBJID;
2953 if (depth && path && ((verbose != VERBOSE_OBJID)))
2954 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2956 if (verbose & VERBOSE_STRIPE_COUNT) {
2957 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2958 if (verbose & ~VERBOSE_STRIPE_COUNT)
2959 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2960 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2961 (int)lum->lum_stripe_count);
2962 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
2968 if (verbose & VERBOSE_STRIPE_OFFSET) {
2969 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2970 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2971 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2972 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2973 (int)lum->lum_stripe_offset);
2974 if (verbose & VERBOSE_HASH_TYPE && !yaml)
2980 if (verbose & VERBOSE_HASH_TYPE) {
2981 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
2982 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
2984 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2985 if (verbose & ~VERBOSE_HASH_TYPE)
2986 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
2987 if (type < LMV_HASH_TYPE_MAX)
2988 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2989 mdt_hash_name[type]);
2991 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
2993 if (flags & LMV_HASH_FLAG_MIGRATION)
2994 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
2995 if (flags & LMV_HASH_FLAG_DEAD)
2996 llapi_printf(LLAPI_MSG_NORMAL, ",dead");
2997 if (flags & LMV_HASH_FLAG_BAD_TYPE)
2998 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
2999 if (flags & LMV_HASH_FLAG_LOST_LMV)
3000 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3005 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3006 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3007 if (lum->lum_stripe_count > 0)
3008 llapi_printf(LLAPI_MSG_NORMAL,
3009 "mdtidx\t\t FID[seq:oid:ver]\n");
3010 for (i = 0; i < lum->lum_stripe_count; i++) {
3011 int idx = objects[i].lum_mds;
3012 struct lu_fid *fid = &objects[i].lum_fid;
3013 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3014 llapi_printf(LLAPI_MSG_NORMAL,
3015 "%6u\t\t "DFID"\t\t%s\n",
3017 obdindex == idx ? " *" : "");
3022 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3023 pool_name[0] != '\0') {
3024 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3025 if (verbose & ~VERBOSE_POOL)
3026 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3028 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3032 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3033 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3036 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3037 enum lov_dump_flags flags)
3039 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3040 int depth = param->fp_max_depth;
3041 enum llapi_layout_verbose verbose = param->fp_verbose;
3042 bool yaml = flags & LDF_YAML;
3044 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3045 !(flags & LDF_IS_DIR)) && !yaml)
3046 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3048 if (verbose & VERBOSE_DETAIL) {
3049 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3050 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3051 " ", comp_v1->lcm_magic);
3052 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3053 " ", comp_v1->lcm_size);
3054 if (flags & LDF_IS_DIR)
3055 llapi_printf(LLAPI_MSG_NORMAL,
3056 "%2slcm_flags: %s\n", " ",
3057 comp_v1->lcm_mirror_count > 0 ?
3060 llapi_printf(LLAPI_MSG_NORMAL,
3061 "%2slcm_flags: %s\n", " ",
3062 llapi_layout_flags_string(comp_v1->lcm_flags));
3065 if (verbose & VERBOSE_GENERATION) {
3066 if (verbose & ~VERBOSE_GENERATION)
3067 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3069 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3072 if (verbose & VERBOSE_MIRROR_COUNT) {
3073 if (verbose & ~VERBOSE_MIRROR_COUNT)
3074 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3076 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3077 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3078 comp_v1->lcm_mirror_count + 1 : 1);
3081 if (verbose & VERBOSE_COMP_COUNT) {
3082 if (verbose & ~VERBOSE_COMP_COUNT)
3083 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3085 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3086 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3087 comp_v1->lcm_entry_count : 0);
3090 if (verbose & VERBOSE_DETAIL && !yaml)
3091 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3094 static void lcme_flags2str(__u32 comp_flags)
3100 llapi_printf(LLAPI_MSG_NORMAL, "0");
3103 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3104 if (comp_flags & comp_flags_table[i].cfn_flag) {
3106 llapi_printf(LLAPI_MSG_NORMAL, ",");
3107 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3108 comp_flags_table[i].cfn_name);
3109 comp_flags &= ~comp_flags_table[i].cfn_flag;
3115 llapi_printf(LLAPI_MSG_NORMAL, ",");
3116 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3120 static void lov_dump_comp_v1_entry(struct find_param *param,
3121 enum lov_dump_flags flags, int index)
3123 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3124 struct lov_comp_md_entry_v1 *entry;
3125 char *separator = "";
3126 enum llapi_layout_verbose verbose = param->fp_verbose;
3127 bool yaml = flags & LDF_YAML;
3129 entry = &comp_v1->lcm_entries[index];
3132 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3134 if (verbose & VERBOSE_COMP_ID) {
3135 if (verbose & VERBOSE_DETAIL && !yaml)
3136 llapi_printf(LLAPI_MSG_NORMAL,
3137 "%slcme_id: ", " - ");
3138 else if (verbose & ~VERBOSE_COMP_ID)
3139 llapi_printf(LLAPI_MSG_NORMAL,
3140 "%4slcme_id: ", " ");
3141 if (entry->lcme_id != LCME_ID_INVAL)
3142 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3144 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3148 if (verbose & VERBOSE_MIRROR_ID) {
3149 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3150 if (verbose & ~VERBOSE_MIRROR_ID)
3151 llapi_printf(LLAPI_MSG_NORMAL,
3152 "%4slcme_mirror_id: ", " ");
3153 if (entry->lcme_id != LCME_ID_INVAL)
3154 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3155 mirror_id_of(entry->lcme_id));
3157 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3161 if (verbose & VERBOSE_COMP_FLAGS) {
3162 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3163 if (verbose & ~VERBOSE_COMP_FLAGS)
3164 llapi_printf(LLAPI_MSG_NORMAL,
3165 "%4slcme_flags: ", " ");
3166 lcme_flags2str(entry->lcme_flags);
3169 /* print snapshot timestamp if its a nosync comp */
3170 if ((verbose & VERBOSE_COMP_FLAGS) &&
3171 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3172 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3173 if (verbose & ~VERBOSE_COMP_FLAGS)
3174 llapi_printf(LLAPI_MSG_NORMAL,
3175 "%4slcme_timestamp: ", " ");
3177 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3178 entry->lcme_timestamp);
3180 time_t stamp = entry->lcme_timestamp;
3181 char *date_str = asctime(localtime(&stamp));
3183 date_str[strlen(date_str) - 1] = '\0';
3184 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3190 if (verbose & VERBOSE_COMP_START) {
3191 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3192 if (verbose & ~VERBOSE_COMP_START)
3193 llapi_printf(LLAPI_MSG_NORMAL,
3194 "%4slcme_extent.e_start: ", " ");
3195 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3196 entry->lcme_extent.e_start);
3200 if (verbose & VERBOSE_COMP_END) {
3201 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3202 if (verbose & ~VERBOSE_COMP_END)
3203 llapi_printf(LLAPI_MSG_NORMAL,
3204 "%4slcme_extent.e_end: ", " ");
3205 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3206 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3208 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3209 entry->lcme_extent.e_end);
3214 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3215 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3216 } else if (verbose & VERBOSE_DETAIL) {
3217 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3218 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3219 " ", entry->lcme_offset);
3220 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3221 " ", entry->lcme_size);
3222 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3224 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3228 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3229 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3231 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3232 * The table below gives the answers for the specified parameters (value and
3233 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3234 * --------------------------------------
3235 * 1 | file > limit; sign > 0 | -1 / -1 |
3236 * 2 | file = limit; sign > 0 | -1 / -1 |
3237 * 3 | file < limit; sign > 0 | ? / 1 |
3238 * 4 | file > limit; sign = 0 | -1 / -1 |
3239 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3240 * 6 | file < limit; sign = 0 | ? / -1 |
3241 * 7 | file > limit; sign < 0 | 1 / 1 |
3242 * 8 | file = limit; sign < 0 | ? / -1 |
3243 * 9 | file < limit; sign < 0 | ? / -1 |
3244 * --------------------------------------
3245 * Note: 5th actually means that the value is within the interval
3246 * (limit - margin, limit]. */
3247 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3248 int sign, int negopt, unsigned long long margin,
3254 /* Drop the fraction of margin (of days or size). */
3255 if (file + margin <= limit)
3257 } else if (sign == 0) {
3258 if (file <= limit && file + margin > limit)
3260 else if (file + margin <= limit)
3262 } else if (sign < 0) {
3269 return negopt ? ~ret + 1 : ret;
3272 static inline struct lov_user_md *
3273 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3275 return (struct lov_user_md *)((char *)comp_v1 +
3276 comp_v1->lcm_entries[ent_idx].lcme_offset);
3279 static inline struct lov_user_ost_data_v1 *
3280 lov_v1v3_objects(struct lov_user_md *v1)
3282 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3283 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3285 return v1->lmm_objects;
3289 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3291 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3292 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3293 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3295 pool_name[0] = '\0';
3299 print_last_init_comp(struct find_param *param)
3301 /* print all component info */
3302 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3305 /* print specific component info */
3306 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3307 param->fp_check_comp_start || param->fp_check_comp_end ||
3308 param->fp_check_mirror_id || param->fp_check_mirror_index)
3314 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3318 if (param->fp_comp_end == LUSTRE_EOF) {
3319 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3320 match = end == LUSTRE_EOF ? 1 : -1;
3321 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3322 match = end == LUSTRE_EOF ? -1 : 1;
3323 else /* at least EOF */
3325 if (param->fp_exclude_comp_end)
3328 unsigned long long margin;
3330 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3331 match = find_value_cmp(end, param->fp_comp_end,
3332 param->fp_comp_end_sign,
3333 param->fp_exclude_comp_end, margin, 0);
3340 * An example of "getstripe -v" for a two components PFL file:
3343 * lcm_magic: 0x0BD60BD0
3347 * lcm_entry_count: 2
3351 * lcme_extent.e_start: 0
3352 * lcme_extent.e_end: 1048576
3356 * lmm_magic: 0x0BD10BD0
3357 * lmm_seq: 0x200000401
3358 * lmm_object_id: 0x1
3359 * lmm_fid: [0x200000401:0x1:0x0]
3360 * lmm_stripe_count: 1
3361 * lmm_stripe_size: 1048576
3362 * lmm_pattern: raid0
3364 * lmm_stripe_offset: 0
3366 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3370 * lcme_extent.e_start: 1048576
3371 * lcme_extent.e_end: EOF
3375 * lmm_magic: 0x0BD10BD0
3376 * lmm_seq: 0x200000401
3377 * lmm_object_id: 0x1
3378 * lmm_fid: [0x200000401:0x1:0x0]
3379 * lmm_stripe_count: 2
3380 * lmm_stripe_size: 1048576
3381 * lmm_pattern: raid0
3383 * lmm_stripe_offset: 1
3385 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3386 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3388 static void lov_dump_comp_v1(struct find_param *param, char *path,
3389 enum lov_dump_flags flags)
3391 struct lov_comp_md_entry_v1 *entry;
3392 struct lov_user_ost_data_v1 *objects;
3393 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3394 struct lov_user_md_v1 *v1;
3395 char pool_name[LOV_MAXPOOLNAME + 1];
3396 int obdindex = param->fp_obd_index;
3398 bool obdstripe = false;
3399 __u16 mirror_index = 0;
3400 __u16 mirror_id = 0;
3402 if (obdindex != OBD_NOT_FOUND) {
3403 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3404 i < comp_v1->lcm_entry_count; i++) {
3405 if (!(comp_v1->lcm_entries[i].lcme_flags &
3409 v1 = lov_comp_entry(comp_v1, i);
3410 objects = lov_v1v3_objects(v1);
3412 for (j = 0; j < v1->lmm_stripe_count; j++) {
3413 if (obdindex == objects[j].l_ost_idx) {
3426 lov_dump_comp_v1_header(param, path, flags);
3428 flags |= LDF_INDENT;
3430 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3431 entry = &comp_v1->lcm_entries[i];
3433 if (param->fp_check_comp_flags) {
3434 if (((param->fp_comp_flags & entry->lcme_flags) !=
3435 param->fp_comp_flags) ||
3436 (param->fp_comp_neg_flags & entry->lcme_flags))
3440 if (param->fp_check_comp_id &&
3441 param->fp_comp_id != entry->lcme_id)
3444 if (param->fp_check_comp_start) {
3445 match = find_value_cmp(entry->lcme_extent.e_start,
3446 param->fp_comp_start,
3447 param->fp_comp_start_sign,
3449 param->fp_comp_start_units, 0);
3454 if (param->fp_check_comp_end) {
3455 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3461 if (param->fp_check_mirror_index) {
3462 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3464 mirror_id = mirror_id_of(entry->lcme_id);
3467 match = find_value_cmp(mirror_index,
3468 param->fp_mirror_index,
3469 param->fp_mirror_index_sign,
3470 param->fp_exclude_mirror_index,
3474 } else if (param->fp_check_mirror_id) {
3475 if (mirror_id != mirror_id_of(entry->lcme_id))
3476 mirror_id = mirror_id_of(entry->lcme_id);
3478 match = find_value_cmp(mirror_id,
3479 param->fp_mirror_id,
3480 param->fp_mirror_id_sign,
3481 param->fp_exclude_mirror_id,
3487 if (print_last_init_comp(param)) {
3489 * if part of stripe info is needed, we'd print only
3490 * the last instantiated component info.
3492 if (entry->lcme_flags & LCME_FL_INIT)
3498 if (entry->lcme_flags & LCME_FL_INIT) {
3499 if (obdindex != OBD_NOT_FOUND) {
3500 flags |= LDF_SKIP_OBJS;
3501 v1 = lov_comp_entry(comp_v1, i);
3502 objects = lov_v1v3_objects(v1);
3504 for (j = 0; j < v1->lmm_stripe_count; j++) {
3505 if (obdindex == objects[j].l_ost_idx) {
3506 flags &= ~LDF_SKIP_OBJS;
3511 flags &= ~LDF_SKIP_OBJS;
3514 flags |= LDF_SKIP_OBJS;
3517 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3519 lov_dump_comp_v1_entry(param, flags, i);
3521 v1 = lov_comp_entry(comp_v1, i);
3522 objects = lov_v1v3_objects(v1);
3523 lov_v1v3_pool_name(v1, pool_name);
3525 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3526 param->fp_max_depth, param->fp_verbose,
3529 if (print_last_init_comp(param)) {
3531 * directory layout contains only layout template, print the
3535 i = comp_v1->lcm_entry_count - 1;
3538 flags &= ~LDF_SKIP_OBJS;
3540 lov_dump_comp_v1_entry(param, flags, i);
3542 v1 = lov_comp_entry(comp_v1, i);
3543 objects = lov_v1v3_objects(v1);
3544 lov_v1v3_pool_name(v1, pool_name);
3546 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3547 param->fp_max_depth, param->fp_verbose,
3552 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3553 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3556 static inline bool has_any_comp_options(struct find_param *param)
3558 enum llapi_layout_verbose verbose = param->fp_verbose;
3560 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3561 param->fp_check_comp_start || param->fp_check_comp_end ||
3562 param->fp_check_comp_flags)
3565 /* show full layout information, not component specific */
3566 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3569 return verbose & VERBOSE_COMP_OPTS;
3572 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3575 struct lov_user_md *lum = &orig->lmd_lmm;
3576 struct lov_user_mds_data *new;
3577 struct lov_comp_md_v1 *comp_v1;
3578 struct lov_comp_md_entry_v1 *ent;
3579 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3580 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3583 new = malloc(sizeof(lstat_t) + lum_off + lum_size);
3585 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3589 memcpy(new, orig, sizeof(lstat_t));
3591 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3592 comp_v1->lcm_magic = lum->lmm_magic;
3593 comp_v1->lcm_size = lum_off + lum_size;
3594 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3595 comp_v1->lcm_flags = 0;
3596 comp_v1->lcm_entry_count = 1;
3598 ent = &comp_v1->lcm_entries[0];
3600 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3601 ent->lcme_extent.e_start = 0;
3602 ent->lcme_extent.e_end = LUSTRE_EOF;
3603 ent->lcme_offset = lum_off;
3604 ent->lcme_size = lum_size;
3606 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3611 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3612 enum lov_dump_flags flags)
3614 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3616 if (has_any_comp_options(param)) {
3617 struct lov_user_mds_data *new_lmd, *orig_lmd;
3619 orig_lmd = param->fp_lmd;
3620 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3621 if (new_lmd != NULL) {
3622 param->fp_lmd = new_lmd;
3623 lov_dump_comp_v1(param, path, flags);
3624 param->fp_lmd = orig_lmd;
3630 if (magic == LOV_USER_MAGIC_V1) {
3631 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3632 param->fp_lmd->lmd_lmm.lmm_objects,
3633 path, param->fp_obd_index,
3634 param->fp_max_depth, param->fp_verbose,
3637 char pool_name[LOV_MAXPOOLNAME + 1];
3638 struct lov_user_ost_data_v1 *objects;
3639 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3641 snprintf(pool_name, sizeof(pool_name), "%s",
3642 lmmv3->lmm_pool_name);
3643 objects = lmmv3->lmm_objects;
3644 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3645 objects, path, param->fp_obd_index,
3646 param->fp_max_depth, param->fp_verbose,
3651 static uint32_t check_foreign_type(uint32_t foreign_type)
3655 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3656 if (lu_foreign_types[i].lft_name == NULL)
3658 if (foreign_type == lu_foreign_types[i].lft_type)
3662 return LU_FOREIGN_TYPE_UNKNOWN;
3665 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3666 enum lov_dump_flags flags)
3668 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3669 bool yaml = flags & LDF_YAML;
3671 if (!yaml && param->fp_depth && path)
3672 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3674 if (param->fp_verbose & VERBOSE_DETAIL) {
3675 uint32_t type = check_foreign_type(lfm->lfm_type);
3677 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3679 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3681 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3683 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3684 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3685 lu_foreign_types[type].lft_name);
3687 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3689 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3692 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3693 lfm->lfm_length, lfm->lfm_value);
3694 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3697 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3698 enum lov_dump_flags flags)
3700 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3701 bool yaml = flags & LDF_YAML;
3703 if (!yaml && param->fp_depth && path)
3704 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3706 if (param->fp_verbose & VERBOSE_DETAIL) {
3707 uint32_t type = check_foreign_type(lfm->lfm_type);
3709 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3711 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3713 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3715 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3716 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3717 lu_foreign_types[type].lft_name);
3719 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3721 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3724 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3725 lfm->lfm_length, lfm->lfm_value);
3726 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3729 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3730 enum lov_dump_flags flags)
3734 if (param->fp_get_lmv || param->fp_get_default_lmv)
3735 magic = (__u32)param->fp_lmv_md->lum_magic;
3737 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3740 flags |= LDF_IS_RAW;
3745 case LOV_USER_MAGIC_V1:
3746 case LOV_USER_MAGIC_V3:
3747 case LOV_USER_MAGIC_SPECIFIC:
3748 lov_dump_plain_user_lmm(param, path, flags);
3750 case LOV_USER_MAGIC_FOREIGN:
3751 lov_dump_foreign_lmm(param, path, flags);
3754 case LMV_USER_MAGIC: {
3755 char pool_name[LOV_MAXPOOLNAME + 1];
3756 struct lmv_user_md *lum;
3758 lum = (struct lmv_user_md *)param->fp_lmv_md;
3759 snprintf(pool_name, sizeof(pool_name), "%s",
3760 lum->lum_pool_name);
3761 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3762 param->fp_max_depth, param->fp_verbose,
3766 case LOV_USER_MAGIC_COMP_V1:
3767 lov_dump_comp_v1(param, path, flags);
3769 case LMV_MAGIC_FOREIGN:
3770 lmv_dump_foreign_lmm(param, path, flags);
3773 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
3774 "(expecting one of %#x %#x %#x %#x)\n",
3775 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3776 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3777 LMV_USER_MAGIC, LMV_MAGIC_V1);
3782 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3788 fname = strrchr(path, '/');
3790 /* It should be a file (or other non-directory) */
3791 if (fname == NULL) {
3792 dname = (char *)malloc(2);
3796 fname = (char *)path;
3798 dname = (char *)malloc(fname - path + 1);
3801 strncpy(dname, path, fname - path);
3802 dname[fname - path] = '\0';
3806 fd = open(dname, O_RDONLY | O_NONBLOCK);
3812 strcpy((char *)lum, fname);
3813 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3816 if (close(fd) == -1 && rc == 0)
3824 int llapi_file_lookup(int dirfd, const char *name)
3826 struct obd_ioctl_data data = { 0 };
3831 if (dirfd < 0 || name == NULL)
3834 data.ioc_version = OBD_IOCTL_VERSION;
3835 data.ioc_len = sizeof(data);
3836 data.ioc_inlbuf1 = (char *)name;
3837 data.ioc_inllen1 = strlen(name) + 1;
3839 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3841 llapi_error(LLAPI_MSG_ERROR, rc,
3842 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3847 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
3853 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
3854 * Return -1 or 1 if file timestamp does not or does match the given criteria
3855 * correspondingly. Return 0 if the MDS time is being checked and there are
3856 * attributes on OSTs and it is not yet clear if the timespamp matches.
3858 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
3859 * updated timestamps. */
3860 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
3865 /* Check if file is accepted. */
3866 if (param->fp_atime) {
3867 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
3868 param->fp_asign, param->fp_exclude_atime,
3869 param->fp_time_margin, mds);
3875 if (param->fp_mtime) {
3876 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
3877 param->fp_msign, param->fp_exclude_mtime,
3878 param->fp_time_margin, mds);
3882 /* If the previous check matches, but this one is not yet clear,
3883 * we should return 0 to do an RPC on OSTs. */
3888 if (param->fp_ctime) {
3889 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
3890 param->fp_csign, param->fp_exclude_ctime,
3891 param->fp_time_margin, mds);
3895 /* If the previous check matches, but this one is not yet clear,
3896 * we should return 0 to do an RPC on OSTs. */
3905 * Check whether the stripes matches the indexes user provided
3909 static int check_obd_match(struct find_param *param)
3911 struct lov_user_ost_data_v1 *objects;
3912 struct lov_comp_md_v1 *comp_v1 = NULL;
3913 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3914 lstat_t *st = ¶m->fp_lmd->lmd_st;
3915 int i, j, k, count = 1;
3917 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
3920 if (!S_ISREG(st->st_mode))
3923 /* exclude foreign */
3924 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
3925 return param->fp_exclude_obd;
3927 /* Only those files should be accepted, which have a
3928 * stripe on the specified OST. */
3929 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3930 comp_v1 = (struct lov_comp_md_v1 *)v1;
3931 count = comp_v1->lcm_entry_count;
3934 for (i = 0; i < count; i++) {
3936 v1 = lov_comp_entry(comp_v1, i);
3938 objects = lov_v1v3_objects(v1);
3940 for (j = 0; j < v1->lmm_stripe_count; j++) {
3941 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
3944 for (k = 0; k < param->fp_num_obds; k++) {
3945 if (param->fp_obd_indexes[k] ==
3946 objects[j].l_ost_idx)
3947 return !param->fp_exclude_obd;
3952 return param->fp_exclude_obd;
3955 static int check_mdt_match(struct find_param *param)
3959 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
3962 /* FIXME: For striped dir, we should get stripe information and check */
3963 for (i = 0; i < param->fp_num_mdts; i++) {
3964 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
3965 return !param->fp_exclude_mdt;
3968 if (param->fp_exclude_mdt)
3975 * Check whether the obd is active or not, if it is
3976 * not active, just print the object affected by this
3979 static int print_failed_tgt(struct find_param *param, char *path, int type)
3981 struct obd_statfs stat_buf;
3982 struct obd_uuid uuid_buf;
3985 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV)
3988 memset(&stat_buf, 0, sizeof(struct obd_statfs));
3989 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
3990 ret = llapi_obd_statfs(path, type,
3991 param->fp_obd_index, &stat_buf,
3994 llapi_printf(LLAPI_MSG_NORMAL,
3995 "obd_uuid: %s failed %s ",
3996 param->fp_obd_uuid->uuid,
4003 static int find_check_stripe_size(struct find_param *param)
4005 struct lov_comp_md_v1 *comp_v1 = NULL;
4006 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4007 int ret, i, count = 1;
4009 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4010 return param->fp_exclude_stripe_size ? 1 : -1;
4012 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4013 comp_v1 = (struct lov_comp_md_v1 *)v1;
4014 count = comp_v1->lcm_entry_count;
4015 ret = param->fp_exclude_stripe_size ? 1 : -1;
4018 for (i = 0; i < count; i++) {
4020 v1 = lov_comp_entry(comp_v1, i);
4022 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_stripe_size,
4023 param->fp_stripe_size_sign,
4024 param->fp_exclude_stripe_size,
4025 param->fp_stripe_size_units, 0);
4026 /* If any stripe_size matches */
4034 static __u32 find_get_stripe_count(struct find_param *param)
4036 struct lov_comp_md_v1 *comp_v1 = NULL;
4037 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4039 __u32 stripe_count = 0;
4041 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4044 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4045 comp_v1 = (struct lov_comp_md_v1 *)v1;
4046 count = comp_v1->lcm_entry_count;
4049 for (i = 0; i < count; i++) {
4051 v1 = lov_comp_entry(comp_v1, i);
4052 stripe_count += v1->lmm_stripe_count;
4055 return stripe_count;
4058 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4060 static int find_check_layout(struct find_param *param)
4062 struct lov_comp_md_v1 *comp_v1 = NULL;
4063 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4065 bool found = false, valid = false;
4067 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4068 comp_v1 = (struct lov_comp_md_v1 *)v1;
4069 count = comp_v1->lcm_entry_count;
4072 for (i = 0; i < count; i++) {
4074 v1 = lov_comp_entry(comp_v1, i);
4076 /* foreign file have a special magic but no pattern field */
4077 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4080 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4084 if (v1->lmm_pattern & param->fp_layout) {
4093 if ((found && !param->fp_exclude_layout) ||
4094 (!found && param->fp_exclude_layout))
4100 /* if no type specified, check/exclude all foreign
4101 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4103 static int find_check_foreign(struct find_param *param)
4105 if (S_ISREG(param->fp_lmd->lmd_st.st_mode)) {
4106 struct lov_foreign_md *lfm;
4108 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4109 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4110 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4111 return param->fp_exclude_foreign ? 1 : -1;
4114 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4115 lfm->lfm_type == param->fp_foreign_type)
4116 return param->fp_exclude_foreign ? -1 : 1;
4117 return param->fp_exclude_foreign ? 1 : -1;
4121 if (S_ISDIR(param->fp_lmd->lmd_st.st_mode)) {
4122 struct lmv_foreign_md *lfm;
4124 lfm = (void *)param->fp_lmv_md;
4125 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
4126 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4127 return param->fp_exclude_foreign ? 1 : -1;
4130 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4131 lfm->lfm_type == param->fp_foreign_type)
4132 return param->fp_exclude_foreign ? -1 : 1;
4133 return param->fp_exclude_foreign ? 1 : -1;
4139 static int find_check_pool(struct find_param *param)
4141 struct lov_comp_md_v1 *comp_v1 = NULL;
4142 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4143 struct lov_user_md_v3 *v3 = (void *)v1;
4147 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4148 comp_v1 = (struct lov_comp_md_v1 *)v1;
4149 count = comp_v1->lcm_entry_count;
4150 /* empty requested pool is taken as no pool search */
4151 if (count == 0 && param->fp_poolname[0] == '\0')
4155 for (i = 0; i < count; i++) {
4156 if (comp_v1 != NULL)
4157 v1 = lov_comp_entry(comp_v1, i);
4159 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4162 if (((v1->lmm_magic == LOV_USER_MAGIC_V1) &&
4163 (param->fp_poolname[0] == '\0')) ||
4164 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4165 (strncmp(v3->lmm_pool_name,
4166 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4167 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4168 (strcmp(param->fp_poolname, "*") == 0))) {
4174 if ((found && !param->fp_exclude_pool) ||
4175 (!found && param->fp_exclude_pool))
4181 static int find_check_comp_options(struct find_param *param)
4183 lstat_t *st = ¶m->fp_lmd->lmd_st;
4184 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4185 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4186 struct lov_comp_md_entry_v1 *entry;
4189 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4192 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4193 comp_v1 = (struct lov_comp_md_v1 *)v1;
4195 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4196 if (forged_v1 == NULL)
4198 comp_v1 = forged_v1;
4199 comp_v1->lcm_entry_count = 1;
4200 entry = &comp_v1->lcm_entries[0];
4201 entry->lcme_flags = S_ISDIR(st->st_mode) ? 0 : LCME_FL_INIT;
4202 entry->lcme_extent.e_start = 0;
4203 entry->lcme_extent.e_end = LUSTRE_EOF;
4206 /* invalid case, don't match for any kind of search. */
4207 if (comp_v1->lcm_entry_count == 0) {
4212 if (param->fp_check_comp_count) {
4213 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4214 param->fp_comp_count,
4215 param->fp_comp_count_sign,
4216 param->fp_exclude_comp_count, 1, 0);
4222 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4223 entry = &comp_v1->lcm_entries[i];
4225 if (param->fp_check_comp_flags) {
4227 if (((param->fp_comp_flags & entry->lcme_flags) !=
4228 param->fp_comp_flags) ||
4229 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4235 if (param->fp_check_comp_start) {
4236 ret = find_value_cmp(entry->lcme_extent.e_start,
4237 param->fp_comp_start,
4238 param->fp_comp_start_sign,
4239 param->fp_exclude_comp_start,
4240 param->fp_comp_start_units, 0);
4245 if (param->fp_check_comp_end) {
4246 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4252 /* the component matches all criteria */
4261 static int find_check_mirror_options(struct find_param *param)
4263 struct lov_comp_md_v1 *comp_v1;
4264 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4267 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4270 comp_v1 = (struct lov_comp_md_v1 *)v1;
4272 if (param->fp_check_mirror_count) {
4273 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4274 param->fp_mirror_count,
4275 param->fp_mirror_count_sign,
4276 param->fp_exclude_mirror_count, 1, 0);
4281 if (param->fp_check_mirror_state) {
4283 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4285 if ((param->fp_mirror_state != 0 &&
4286 file_state != param->fp_mirror_state) ||
4287 file_state == param->fp_mirror_neg_state)
4294 static bool find_check_lmm_info(struct find_param *param)
4296 return param->fp_check_pool || param->fp_check_stripe_count ||
4297 param->fp_check_stripe_size || param->fp_check_layout ||
4298 param->fp_check_comp_count || param->fp_check_comp_end ||
4299 param->fp_check_comp_start || param->fp_check_comp_flags ||
4300 param->fp_check_mirror_count || param->fp_check_foreign ||
4301 param->fp_check_mirror_state ||
4302 param->fp_check_projid;
4306 * Get file/directory project id.
4307 * by the open fd resides on.
4308 * Return 0 and project id on success, or -ve errno.
4310 static int fget_projid(int fd, int *projid)
4315 rc = ioctl(fd, LL_IOC_FSGETXATTR, &fsx);
4319 *projid = fsx.fsx_projid;
4323 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
4324 void *data, struct dirent64 *de)
4326 struct find_param *param = (struct find_param *)data;
4327 DIR *dir = dirp == NULL ? NULL : *dirp;
4328 int decision = 1; /* 1 is accepted; -1 is rejected. */
4329 lstat_t *st = ¶m->fp_lmd->lmd_st;
4331 int checked_type = 0;
4333 __u32 stripe_count = 0;
4336 if (parent == NULL && dir == NULL)
4339 /* If a regular expression is presented, make the initial decision */
4340 if (param->fp_pattern != NULL) {
4341 char *fname = strrchr(path, '/');
4342 fname = (fname == NULL ? path : fname + 1);
4343 ret = fnmatch(param->fp_pattern, fname, 0);
4344 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4345 (ret == 0 && param->fp_exclude_pattern))
4349 /* See if we can check the file type from the dirent. */
4350 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
4353 if (DTTOIF(de->d_type) == param->fp_type) {
4354 if (param->fp_exclude_type)
4357 if (!param->fp_exclude_type)
4364 /* Request MDS for the stat info if some of these parameters need
4365 * to be compared. */
4366 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4367 param->fp_check_uid || param->fp_check_gid ||
4368 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4369 param->fp_check_size || param->fp_check_blocks ||
4370 find_check_lmm_info(param) ||
4371 param->fp_check_mdt_count || param->fp_check_hash_type)
4374 if (param->fp_type != 0 && checked_type == 0)
4377 if (decision == 0) {
4378 if (dir && (param->fp_check_mdt_count ||
4379 param->fp_check_hash_type || param->fp_check_foreign)) {
4380 param->fp_get_lmv = 1;
4381 ret = cb_get_dirstripe(path, dir, param);
4383 /* XXX this works to decide for foreign
4386 if (errno == ENODATA &&
4387 param->fp_check_foreign) {
4388 if (param->fp_exclude_foreign)
4396 param->fp_lmd->lmd_lmm.lmm_magic = 0;
4397 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
4398 param->fp_lum_size, GET_LMD_INFO);
4399 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
4400 find_check_lmm_info(param)) {
4401 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
4403 /* We need to "fake" the "use the default" values
4404 * since the lmm struct is zeroed out at this point. */
4405 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4406 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
4408 ostid_set_seq(&lmm->lmm_oi,
4409 FID_SEQ_LOV_DEFAULT);
4410 lmm->lmm_stripe_size = 0;
4411 lmm->lmm_stripe_count = 0;
4412 lmm->lmm_stripe_offset = -1;
4414 if (ret == 0 && param->fp_mdt_uuid != NULL) {
4416 ret = llapi_file_fget_mdtidx(dirfd(dir),
4417 ¶m->fp_file_mdt_index);
4418 } else if (S_ISREG(st->st_mode)) {
4419 /* FIXME: we could get the MDT index from the
4420 * file's FID in lmd->lmd_lmm.lmm_oi without
4421 * opening the file, once we are sure that
4422 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
4423 * That would still be an ioctl() to map the
4424 * FID to the MDT, but not an open RPC. */
4425 fd = open(path, O_RDONLY);
4427 ret = llapi_file_fget_mdtidx(fd,
4428 ¶m->fp_file_mdt_index);
4433 /* For a special file, we assume it resides on
4434 * the same MDT as the parent directory. */
4435 ret = llapi_file_fget_mdtidx(dirfd(parent),
4436 ¶m->fp_file_mdt_index);
4447 stripe_count = find_get_stripe_count(param);
4451 if (param->fp_type && !checked_type) {
4452 if ((st->st_mode & S_IFMT) == param->fp_type) {
4453 if (param->fp_exclude_type)
4456 if (!param->fp_exclude_type)
4462 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
4463 if (lustre_fs && param->fp_got_uuids &&
4464 param->fp_dev != st->st_dev) {
4465 /* A lustre/lustre mount point is crossed. */
4466 param->fp_got_uuids = 0;
4467 param->fp_obds_printed = 0;
4468 param->fp_mdt_index = OBD_NOT_FOUND;
4469 param->fp_obd_index = OBD_NOT_FOUND;
4472 if (lustre_fs && !param->fp_got_uuids) {
4473 ret = setup_target_indexes(dir ? dir : parent, path,
4478 param->fp_dev = st->st_dev;
4479 } else if (!lustre_fs && param->fp_got_uuids) {
4480 /* A lustre/non-lustre mount point is crossed. */
4481 param->fp_got_uuids = 0;
4482 param->fp_mdt_index = OBD_NOT_FOUND;
4483 param->fp_obd_index = OBD_NOT_FOUND;
4487 if (param->fp_check_foreign) {
4488 decision = find_check_foreign(param);
4493 if (param->fp_check_stripe_size) {
4494 decision = find_check_stripe_size(param);
4499 if (param->fp_check_stripe_count) {
4500 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
4501 param->fp_stripe_count_sign,
4502 param->fp_exclude_stripe_count, 1, 0);
4507 if (param->fp_check_mdt_count) {
4508 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4513 decision = find_value_cmp(
4514 param->fp_lmv_md->lum_stripe_count,
4515 param->fp_mdt_count,
4516 param->fp_mdt_count_sign,
4517 param->fp_exclude_mdt_count, 1, 0);
4522 if (param->fp_check_layout) {
4523 decision = find_check_layout(param);
4528 if (param->fp_check_hash_type) {
4531 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4536 found = param->fp_lmv_md->lum_hash_type & param->fp_hash_type;
4537 if ((found && param->fp_exclude_hash_type) ||
4538 (!found && !param->fp_exclude_hash_type)) {
4544 /* If an OBD UUID is specified but none matches, skip this file. */
4545 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
4546 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
4549 /* If an OST or MDT UUID is given, and some OST matches,
4551 if (param->fp_obd_index != OBD_NOT_FOUND ||
4552 param->fp_mdt_index != OBD_NOT_FOUND) {
4553 if (param->fp_obd_uuid) {
4554 if (check_obd_match(param)) {
4555 /* If no mdtuuid is given, we are done.
4556 * Otherwise, fall through to the mdtuuid
4558 if (!param->fp_mdt_uuid)
4565 if (param->fp_mdt_uuid) {
4566 if (check_mdt_match(param))
4573 if (param->fp_check_uid) {
4574 if (st->st_uid == param->fp_uid) {
4575 if (param->fp_exclude_uid)
4578 if (!param->fp_exclude_uid)
4583 if (param->fp_check_gid) {
4584 if (st->st_gid == param->fp_gid) {
4585 if (param->fp_exclude_gid)
4588 if (!param->fp_exclude_gid)
4593 if (param->fp_check_projid) {
4597 fd = open(path, O_RDONLY);
4600 ret = fget_projid(fd, &projid);
4605 if (projid == param->fp_projid) {
4606 if (param->fp_exclude_projid)
4609 if (!param->fp_exclude_projid)
4614 if (param->fp_check_pool) {
4615 decision = find_check_pool(param);
4620 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
4621 param->fp_check_comp_start || param->fp_check_comp_end) {
4622 decision = find_check_comp_options(param);
4627 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
4628 decision = find_check_mirror_options(param);
4633 /* Check the time on mds. */
4635 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
4638 for_mds = lustre_fs ?
4639 (S_ISREG(st->st_mode) && stripe_count) : 0;
4640 decision = find_time_check(st, param, for_mds);
4645 /* If file still fits the request, ask ost for updated info.
4646 The regular stat is almost of the same speed as some new
4647 'glimpse-size-ioctl'. */
4649 if ((param->fp_check_size || param->fp_check_blocks) &&
4650 ((S_ISREG(st->st_mode) && stripe_count) || S_ISDIR(st->st_mode)))
4654 /* For regular files with the stripe the decision may have not
4655 * been taken yet if *time or size is to be checked. */
4656 if (param->fp_obd_index != OBD_NOT_FOUND)
4657 print_failed_tgt(param, path, LL_STATFS_LOV);
4659 if (param->fp_mdt_index != OBD_NOT_FOUND)
4660 print_failed_tgt(param, path, LL_STATFS_LMV);
4663 ret = fstat_f(dirfd(dir), st);
4664 else if (de != NULL)
4665 ret = fstatat_f(dirfd(parent), de->d_name, st,
4666 AT_SYMLINK_NOFOLLOW);
4668 ret = lstat_f(path, st);
4671 if (errno == ENOENT) {
4672 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
4673 "warning: %s: %s does not exist",
4678 llapi_error(LLAPI_MSG_ERROR, ret,
4679 "%s: IOC_LOV_GETINFO on %s failed",
4685 /* Check the time on osc. */
4686 decision = find_time_check(st, param, 0);
4691 if (param->fp_check_size) {
4692 decision = find_value_cmp(st->st_size, param->fp_size,
4693 param->fp_size_sign,
4694 param->fp_exclude_size,
4695 param->fp_size_units, 0);
4700 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
4701 decision = find_value_cmp(st->st_blocks * 512, param->fp_blocks,
4702 param->fp_blocks_sign,
4703 param->fp_exclude_blocks,
4704 param->fp_blocks_units, 0);
4710 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
4711 if (param->fp_zero_end)
4712 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
4714 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4718 /* Do not get down anymore? */
4719 if (param->fp_depth == param->fp_max_depth) {
4730 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
4731 void *param_data, struct dirent64 *de)
4733 struct find_param *param = (struct find_param *)param_data;
4734 struct lmv_user_md *lmu = param->fp_lmv_md;
4735 DIR *tmp_parent = parent;
4736 char raw[MAX_IOC_BUFLEN] = {'\0'};
4738 struct obd_ioctl_data data = { 0 };
4745 if (parent == NULL && dirp == NULL)
4754 if (parent == NULL) {
4755 tmp_parent = opendir_parent(path);
4756 if (tmp_parent == NULL) {
4759 llapi_error(LLAPI_MSG_ERROR, ret,
4760 "can not open %s", path);
4765 fd = dirfd(tmp_parent);
4767 path_copy = strdup(path);
4768 filename = basename(path_copy);
4770 data.ioc_inlbuf1 = (char *)filename;
4771 data.ioc_inllen1 = strlen(filename) + 1;
4772 data.ioc_inlbuf2 = (char *)lmu;
4773 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
4775 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
4777 llapi_error(LLAPI_MSG_ERROR, ret,
4778 "llapi_obd_statfs: error packing ioctl data");
4783 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
4785 if (errno == EBUSY && !retry) {
4786 /* because migrate may not be able to lock all involved
4787 * objects in order, for some of them it try lock, while
4788 * there may be conflicting COS locks and cause migrate
4789 * fail with EBUSY, hope a sync() could cause
4790 * transaction commit and release these COS locks. */
4794 } else if (errno == EALREADY) {
4795 if (param->fp_verbose & VERBOSE_DETAIL)
4796 llapi_printf(LLAPI_MSG_NORMAL,
4797 "%s was migrated to MDT%d already\n",
4798 path, lmu->lum_stripe_offset);
4802 llapi_error(LLAPI_MSG_ERROR, ret,
4803 "%s migrate failed: %s (%d)\n",
4804 path, strerror(-ret), ret);
4807 } else if (param->fp_verbose & VERBOSE_DETAIL) {
4808 llapi_printf(LLAPI_MSG_NORMAL,
4809 "migrate %s to MDT%d stripe count %d\n",
4810 path, lmu->lum_stripe_offset,
4811 lmu->lum_stripe_count);
4816 /* If the directory is being migration, we need
4817 * close the directory after migration,
4818 * so the old directory cache will be cleanup
4819 * on the client side, and re-open to get the
4820 * new directory handle */
4821 *dirp = opendir(path);
4822 if (*dirp == NULL) {
4824 llapi_error(LLAPI_MSG_ERROR, ret,
4825 "%s: Failed to open '%s'", __func__, path);
4830 closedir(tmp_parent);
4837 /* dir migration finished, shrink its stripes */
4838 static int cb_migrate_mdt_fini(char *path, DIR *parent, DIR **dirp, void *data,
4839 struct dirent64 *de)
4841 struct find_param *param = data;
4842 struct lmv_user_md *lmu = param->fp_lmv_md;
4843 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
4846 if (de && de->d_type != DT_DIR)
4851 * close it before setxattr because the latter may destroy the
4852 * original object, and cause close fail.
4854 ret = closedir(*dirp);
4860 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
4861 if (ret == -EALREADY)
4864 cb_common_fini(path, parent, dirp, data, de);
4868 int llapi_migrate_mdt(char *path, struct find_param *param)
4870 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
4874 int llapi_mv(char *path, struct find_param *param)
4876 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
4877 static bool printed;
4880 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
4881 "llapi_mv() is deprecated, use llapi_migrate_mdt()\n");
4885 return llapi_migrate_mdt(path, param);
4888 int llapi_find(char *path, struct find_param *param)
4890 return param_callback(path, cb_find_init, cb_common_fini, param);
4894 * Get MDT number that the file/directory inode referenced
4895 * by the open fd resides on.
4896 * Return 0 and mdtidx on success, or -ve errno.
4898 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
4900 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
4905 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
4906 struct dirent64 *de)
4908 struct find_param *param = (struct find_param *)data;
4909 DIR *d = dirp == NULL ? NULL : *dirp;
4913 if (parent == NULL && d == NULL)
4917 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
4918 } else /* if (parent) */ {
4921 fd = open(path, O_RDONLY | O_NOCTTY);
4923 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
4931 if (ret == -ENODATA) {
4932 if (!param->fp_obd_uuid)
4933 llapi_printf(LLAPI_MSG_NORMAL,
4934 "'%s' has no stripe info\n", path);
4936 } else if (ret == -ENOENT) {
4937 llapi_error(LLAPI_MSG_WARN, ret,
4938 "warning: %s: '%s' does not exist",
4941 } else if (ret == -ENOTTY) {
4942 llapi_error(LLAPI_MSG_ERROR, ret,
4943 "%s: '%s' not on a Lustre fs",
4946 llapi_error(LLAPI_MSG_ERROR, ret,
4947 "error: %s: '%s' failed get_mdtidx",
4953 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
4954 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
4956 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
4960 /* Do not go down anymore? */
4961 if (param->fp_depth == param->fp_max_depth)
4969 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
4970 struct dirent64 *de)
4972 struct find_param *param = (struct find_param *)data;
4973 DIR *d = dirp == NULL ? NULL : *dirp;
4976 if (parent == NULL && d == NULL)
4979 if (param->fp_obd_uuid) {
4980 param->fp_quiet = 1;
4981 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
4986 if (d && (param->fp_get_lmv || param->fp_get_default_lmv))
4987 ret = cb_get_dirstripe(path, d, param);
4989 (parent && !param->fp_get_lmv && !param->fp_get_default_lmv))
4990 ret = get_lmd_info(path, parent, d, ¶m->fp_lmd->lmd_lmm,
4991 param->fp_lum_size, GET_LMD_STRIPE);
4996 if (errno == ENODATA && d != NULL) {
4997 /* We need to "fake" the "use the default" values
4998 * since the lmm struct is zeroed out at this point.
4999 * The magic needs to be set in order to satisfy
5000 * a check later on in the code path.
5001 * The object_seq needs to be set for the "(Default)"
5002 * prefix to be displayed. */
5003 if (param->fp_get_default_lmv) {
5004 struct lmv_user_md *lum = param->fp_lmv_md;
5006 lum->lum_magic = LMV_USER_MAGIC;
5007 lum->lum_stripe_count = 0;
5008 lum->lum_stripe_offset = -1;
5010 } else if (param->fp_get_lmv) {
5011 struct lmv_user_md *lum = param->fp_lmv_md;
5014 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5017 lum->lum_magic = LMV_MAGIC_V1;
5018 lum->lum_stripe_count = 0;
5019 lum->lum_stripe_offset = mdtidx;
5022 struct lov_user_md *lmm =
5023 ¶m->fp_lmd->lmd_lmm;
5025 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5027 ostid_set_seq(&lmm->lmm_oi,
5028 FID_SEQ_LOV_DEFAULT);
5029 lmm->lmm_stripe_count = 0;
5030 lmm->lmm_stripe_size = 0;
5031 lmm->lmm_stripe_offset = -1;
5034 } else if (errno == ENODATA && parent != NULL) {
5035 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5036 llapi_printf(LLAPI_MSG_NORMAL,
5037 "%s has no stripe info\n", path);
5039 } else if (errno == ENOENT) {
5040 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5041 "warning: %s: %s does not exist",
5044 } else if (errno == ENOTTY) {
5046 llapi_error(LLAPI_MSG_ERROR, ret,
5047 "%s: '%s' not on a Lustre fs?",
5052 llapi_error(LLAPI_MSG_ERROR, ret,
5053 "error: %s: %s failed for %s",
5054 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
5055 "IOC_MDC_GETFILESTRIPE", path);
5062 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5063 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
5066 /* Do not get down anymore? */
5067 if (param->fp_depth == param->fp_max_depth)
5075 int llapi_getstripe(char *path, struct find_param *param)
5077 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5078 cb_get_mdt_index : cb_getstripe,
5079 cb_common_fini, param);
5082 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5083 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5085 char raw[MAX_IOC_BUFLEN] = {'\0'};
5087 struct obd_ioctl_data data = { 0 };
5090 data.ioc_inlbuf1 = (char *)&type;
5091 data.ioc_inllen1 = sizeof(__u32);
5092 data.ioc_inlbuf2 = (char *)&index;
5093 data.ioc_inllen2 = sizeof(__u32);
5094 data.ioc_pbuf1 = (char *)stat_buf;
5095 data.ioc_plen1 = sizeof(struct obd_statfs);
5096 data.ioc_pbuf2 = (char *)uuid_buf;
5097 data.ioc_plen2 = sizeof(struct obd_uuid);
5099 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5101 llapi_error(LLAPI_MSG_ERROR, rc,
5102 "llapi_obd_statfs: error packing ioctl data");
5106 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5108 return rc < 0 ? -errno : 0;
5111 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5112 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5117 fd = open(path, O_RDONLY);
5120 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5122 /* If we can't even open a file on the filesystem (e.g. with
5123 * -ESHUTDOWN), force caller to exit or it will loop forever. */
5127 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5134 #define MAX_STRING_SIZE 128
5136 int llapi_ping(char *obd_type, char *obd_name)
5138 int flags = O_RDONLY;
5139 char buf[1] = { 0 };
5143 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5144 obd_type, obd_name);
5148 fd = open(path.gl_pathv[0], flags);
5150 if (errno == EACCES && flags == O_RDONLY) {
5155 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5160 if (flags == O_RDONLY)
5161 rc = read(fd, buf, sizeof(buf));
5163 rc = write(fd, buf, sizeof(buf));
5171 cfs_free_param_data(&path);
5175 int llapi_target_iterate(int type_num, char **obd_type,
5176 void *args, llapi_cb_t cb)
5182 for (i = 0; i < type_num; i++) {
5185 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5189 for (j = 0; j < param.gl_pathc; j++) {
5190 char obd_uuid[UUID_MAX + 1];
5194 fp = fopen(param.gl_pathv[j], "r");
5197 llapi_error(LLAPI_MSG_ERROR, rc,
5198 "error: opening '%s'",
5203 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5205 llapi_error(LLAPI_MSG_ERROR, rc,
5206 "error: reading '%s'",
5211 /* Extract the obd_name from the sysfs path.
5212 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5214 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5220 /* skip /fs/lustre/'obd_type'/ */
5221 obd_name += strlen(obd_type[i]) + 12;
5222 /* chop off after obd_name */
5223 ptr = strrchr(obd_name, '/');
5227 cb(obd_type[i], obd_name, obd_uuid, args);
5236 cfs_free_param_data(¶m);
5240 static void do_target_check(char *obd_type_name, char *obd_name,
5241 char *obd_uuid, void *args)
5245 rc = llapi_ping(obd_type_name, obd_name);
5246 if (rc == ENOTCONN) {
5247 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5249 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5251 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5255 int llapi_target_check(int type_num, char **obd_type, char *dir)
5257 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5260 #undef MAX_STRING_SIZE
5262 /* Is this a lustre fs? */
5263 int llapi_is_lustre_mnttype(const char *type)
5265 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
5268 /* Is this a lustre client fs? */
5269 int llapi_is_lustre_mnt(struct mntent *mnt)
5271 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5272 strstr(mnt->mnt_fsname, ":/") != NULL);
5275 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5277 char fsname[PATH_MAX + 1];
5281 rc = llapi_search_fsname(mnt, fsname);
5285 root = open(mnt, O_RDONLY | O_DIRECTORY);
5288 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
5292 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
5300 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
5301 * format must have %s%s, buf must be > 16
5302 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
5303 * then buf = "lustre-MDT0000"
5305 static int get_mdtname(char *name, char *format, char *buf)
5307 char suffix[]="-MDT0000";
5308 int len = strlen(name);
5310 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
5311 name[len - 5] = '\0';
5316 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
5319 /* Not enough room to add suffix */
5320 llapi_err_noerrno(LLAPI_MSG_ERROR,
5321 "Invalid MDT name |%s|", name);
5326 return sprintf(buf, format, name, suffix);
5329 /** ioctl on filsystem root, with mdtindex sent as data
5330 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
5331 * \param mdtidxp pointer to integer within data to be filled in with the
5332 * mdt index (0 if no mdt is specified). NULL won't be filled.
5334 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
5342 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
5343 Open root and parse mdt index. */
5344 if (mdtname[0] == '/') {
5346 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
5347 (char *)mdtname, -1);
5349 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
5351 ptr = fsname + strlen(fsname) - 8;
5353 index = strtol(ptr + 4, NULL, 16);
5354 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
5358 llapi_err_noerrno(LLAPI_MSG_ERROR,
5359 "Can't open %s: %d\n", mdtname, rc);
5366 rc = ioctl(fd, opc, data);
5375 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
5376 int buflen, long long *recno, int *linkno)
5378 const char *fidstr_orig = fidstr;
5380 struct getinfo_fid2path *gf;
5385 while (*fidstr == '[')
5388 sscanf(fidstr, SFID, RFID(&fid));
5389 if (!fid_is_sane(&fid)) {
5390 llapi_err_noerrno(LLAPI_MSG_ERROR,
5391 "bad FID format '%s', should be [seq:oid:ver]"
5392 " (e.g. "DFID")\n", fidstr_orig,
5393 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
5397 gf = malloc(sizeof(*gf) + buflen);
5402 gf->gf_recno = *recno;
5403 gf->gf_linkno = *linkno;
5404 gf->gf_pathlen = buflen;
5406 /* Take path or fsname */
5407 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
5412 /* strip out instances of // */
5413 for (a = gf->gf_u.gf_path; *a != '\0'; a++) {
5414 if ((*a == '/') && (*(a + 1) == '/'))
5421 if (buf[0] == '\0') { /* ROOT path */
5426 *recno = gf->gf_recno;
5427 *linkno = gf->gf_linkno;
5434 static int fid_from_lma(const char *path, int fd, struct lu_fid *fid)
5437 struct lustre_mdt_attrs *lma;
5441 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
5443 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
5446 lma = (struct lustre_mdt_attrs *)buf;
5447 fid_le_to_cpu(fid, &lma->lma_self_fid);
5451 int llapi_get_mdt_index_by_fid(int fd, const struct lu_fid *fid,
5456 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
5465 int llapi_fd2fid(int fd, struct lu_fid *fid)
5469 memset(fid, 0, sizeof(*fid));
5471 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
5472 if (rc == -EINVAL || rc == -ENOTTY)
5473 rc = fid_from_lma(NULL, fd, fid);
5478 int llapi_path2fid(const char *path, struct lu_fid *fid)
5482 memset(fid, 0, sizeof(*fid));
5483 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5485 if (errno == ELOOP || errno == ENXIO)
5486 return fid_from_lma(path, -1, fid);
5490 rc = llapi_fd2fid(fd, fid);
5491 if (rc == -EINVAL || rc == -ENOTTY)
5492 rc = fid_from_lma(path, -1, fid);
5498 int llapi_fd2parent(int fd, unsigned int linkno, struct lu_fid *parent_fid,
5499 char *name, size_t name_size)
5501 struct getparent *gp;
5504 gp = malloc(sizeof(*gp) + name_size);
5508 gp->gp_linkno = linkno;
5509 gp->gp_name_size = name_size;
5511 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
5517 *parent_fid = gp->gp_fid;
5519 strncpy(name, gp->gp_name, name_size);
5520 name[name_size - 1] = '\0';
5527 int llapi_path2parent(const char *path, unsigned int linkno,
5528 struct lu_fid *parent_fid, char *name, size_t name_size)
5533 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5537 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
5542 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
5547 root = open(mnt, O_RDONLY | O_DIRECTORY);
5550 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
5554 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
5557 llapi_error(LLAPI_MSG_ERROR, rc,
5558 "ioctl on %s for getting connect flags failed", mnt);
5565 * Get a 64-bit value representing the version of file data pointed by fd.
5567 * Each write or truncate, flushed on OST, will change this value. You can use
5568 * this value to verify if file data was modified. This only checks the file
5569 * data, not metadata.
5571 * \param flags 0: no flush pages, usually used it the process has already
5573 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
5575 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
5576 * pages from clients.
5578 * \retval 0 on success.
5579 * \retval -errno on error.
5581 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
5584 struct ioc_data_version idv;
5586 idv.idv_flags = (__u32)flags;
5588 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5592 *data_version = idv.idv_version;
5598 * Flush cached pages from all clients.
5600 * \param fd File descriptor
5604 int llapi_file_flush(int fd)
5608 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);
5612 * Fetch layout version from OST objects. Layout version on OST objects are
5613 * only set when the file is a mirrored file AND after the file has been
5614 * written at least once.
5616 * It actually fetches the least layout version from the objects.
5618 int llapi_get_ost_layout_version(int fd, __u32 *layout_version)
5621 struct ioc_data_version idv = { 0 };
5623 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5627 *layout_version = idv.idv_layout_version;
5633 * Create a file without any name and open it for read/write
5635 * - file is created as if it were a standard file in the given \a directory
5636 * - file does not appear in \a directory and mtime does not change because
5637 * the filename is handled specially by the Lustre MDS.
5638 * - file is destroyed at final close
5640 * \param[in] directory directory from which to inherit layout/MDT idx
5641 * \param[in] mdt_idx MDT index on which the file is created,
5642 * \a idx == -1 means no specific MDT is requested
5643 * \param[in] mode standard open(2) mode
5644 * \param[in] stripe_param stripe parameters. May be NULL.
5646 * \retval a file descriptor on success.
5647 * \retval -errno on error.
5649 int llapi_create_volatile_param(const char *directory, int mdt_idx,
5650 int open_flags, mode_t mode,
5651 const struct llapi_stripe_param *stripe_param)
5653 char file_path[PATH_MAX];
5654 int saved_errno = errno;
5656 unsigned int rnumber;
5662 rc = snprintf(file_path, sizeof(file_path),
5663 "%s/" LUSTRE_VOLATILE_HDR "::%.4X",
5664 directory, rnumber);
5666 rc = snprintf(file_path, sizeof(file_path),
5667 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X",
5668 directory, mdt_idx, rnumber);
5670 if (rc < 0 || rc >= sizeof(file_path))
5671 return -ENAMETOOLONG;
5674 * Either open O_WRONLY or O_RDWR, creating RDONLY
5675 * is non-sensical here
5677 if ((open_flags & O_ACCMODE) == O_RDONLY)
5678 open_flags = O_RDWR | (open_flags & ~O_ACCMODE);
5680 open_flags |= O_CREAT | O_EXCL | O_NOFOLLOW;
5682 if (stripe_param != NULL) {
5683 fd = llapi_file_open_param(file_path, open_flags,
5684 mode, stripe_param);
5688 fd = open(file_path, open_flags, mode);
5692 } while (fd < 0 && rc == -EEXIST);
5695 llapi_error(LLAPI_MSG_ERROR, rc,
5696 "Cannot create volatile file '%s' in '%s'",
5697 file_path + strlen(directory) + 1 +
5698 LUSTRE_VOLATILE_HDR_LEN,
5704 * Unlink file in case this wasn't a Lustre filesystem and the magic
5705 * volatile filename wasn't handled as intended. The effect is the
5706 * same. If volatile open was supported then we expect unlink() to
5709 (void)unlink(file_path);
5712 * Since we are returning successfully we restore errno (and
5713 * mask out possible EEXIST from open() and ENOENT from unlink().
5715 errno = saved_errno;
5721 * Create a file without any name open it for read/write
5723 * - file is created as if it were a standard file in the given \a directory
5724 * - file does not appear in \a directory and mtime does not change because
5725 * the filename is handled specially by the Lustre MDS.
5726 * - file is removed at final close
5727 * - file modes are rw------- since it doesn't make sense to have a read-only
5728 * or write-only file that cannot be opened again.
5729 * - if user wants another mode it must use fchmod() on the open file, no
5730 * security problems arise because it cannot be opened by another process.
5732 * \param[in] directory directory from which to inherit layout/MDT idx
5733 * \param[in] idx MDT index on which the file is created,
5734 * \a idx == -1 means no specific MDT is requested
5735 * \param[in] open_flags standard open(2) flags
5737 * \retval a file descriptor on success.
5738 * \retval -errno on error.
5740 int llapi_create_volatile_idx(const char *directory, int mdt_idx,
5743 return llapi_create_volatile_param(directory, mdt_idx, open_flags,
5744 S_IRUSR | S_IWUSR, NULL);
5748 * Swap the layouts between 2 file descriptors
5749 * the 2 files must be open for writing
5750 * first fd received the ioctl, second fd is passed as arg
5751 * this is assymetric but avoid use of root path for ioctl
5753 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
5754 int gid, __u64 flags)
5756 struct lustre_swap_layouts lsl;
5761 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5762 rc = fstat(fd1, &st1);
5766 rc = fstat(fd2, &st2);
5771 lsl.sl_flags = flags;
5775 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
5779 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5780 struct timeval tv1[2];
5781 struct timeval tv2[2];
5783 memset(tv1, 0, sizeof(tv1));
5784 memset(tv2, 0, sizeof(tv2));
5786 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
5787 tv1[0].tv_sec = st1.st_atime;
5788 tv2[0].tv_sec = st2.st_atime;
5790 tv1[0].tv_sec = st2.st_atime;
5791 tv2[0].tv_sec = st1.st_atime;
5794 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
5795 tv1[1].tv_sec = st1.st_mtime;
5796 tv2[1].tv_sec = st2.st_mtime;
5798 tv1[1].tv_sec = st2.st_mtime;
5799 tv2[1].tv_sec = st1.st_mtime;
5802 rc = futimes(fd1, tv1);
5806 rc = futimes(fd2, tv2);
5814 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
5821 while (grp_id == 0);
5823 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
5831 * Swap the layouts between 2 files
5832 * the 2 files are open in write
5834 int llapi_swap_layouts(const char *path1, const char *path2,
5835 __u64 dv1, __u64 dv2, __u64 flags)
5839 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
5842 llapi_error(LLAPI_MSG_ERROR, rc,
5843 "error: cannot open '%s' for write", path1);
5847 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
5850 llapi_error(LLAPI_MSG_ERROR, rc,
5851 "error: cannot open '%s' for write", path2);
5855 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
5857 llapi_error(LLAPI_MSG_ERROR, rc,
5858 "error: cannot swap layout between '%s' and '%s'",
5869 * Attempt to open a file with Lustre file identifier \a fid
5870 * and return an open file descriptor.
5872 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
5873 * \param[in] fid Lustre file identifier of file to open
5874 * \param[in] flags open() flags
5876 * \retval non-negative file descriptor on successful open
5877 * \retval -1 if an error occurred
5879 int llapi_open_by_fid(const char *lustre_dir, const struct lu_fid *fid,
5882 char mntdir[PATH_MAX];
5883 char path[PATH_MAX + 64];
5886 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
5890 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
5891 return open(path, flags);
5897 * \param fd File to lock.
5898 * \param gid Group Identifier.
5900 * \retval 0 on success.
5901 * \retval -errno on failure.
5903 int llapi_group_lock(int fd, int gid)
5907 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
5910 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
5918 * \param fd File to unlock.
5919 * \param gid Group Identifier.
5921 * \retval 0 on success.
5922 * \retval -errno on failure.
5924 int llapi_group_unlock(int fd, int gid)
5928 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
5931 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");