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/
31 * lustre/utils/liblustreapi.c
33 * Author: Peter J. Braam <braam@clusterfs.com>
34 * Author: Phil Schwan <phil@clusterfs.com>
35 * Author: Robert Read <rread@clusterfs.com>
49 #include <sys/ioctl.h>
56 #include <sys/statfs.h>
57 #include <sys/syscall.h>
59 #include <sys/types.h>
60 #include <sys/xattr.h>
61 #include <sys/sysmacros.h>
64 #include <libgen.h> /* for dirname() */
65 #include <linux/limits.h>
66 #ifdef HAVE_LINUX_UNISTD_H
67 #include <linux/unistd.h>
75 #include <libcfs/util/ioctl.h>
76 #include <libcfs/util/param.h>
77 #include <libcfs/util/string.h>
78 #include <linux/lnet/lnetctl.h>
79 #include <lustre/lustreapi.h>
80 #include <linux/lustre/lustre_ostid.h>
81 #include <linux/lustre/lustre_ioctl.h>
82 #include "lustreapi_internal.h"
85 static int llapi_msg_level = LLAPI_MSG_MAX;
86 const char *liblustreapi_cmd;
88 struct lustre_foreign_type lu_foreign_types[] = {
89 {.lft_type = LU_FOREIGN_TYPE_NONE, .lft_name = "none"},
90 {.lft_type = LU_FOREIGN_TYPE_SYMLINK, .lft_name = "symlink"},
91 /* must be the last element */
92 {.lft_type = LU_FOREIGN_TYPE_UNKNOWN, .lft_name = NULL}
93 /* array max dimension must be <= UINT32_MAX */
96 void llapi_msg_set_level(int level)
98 /* ensure level is in the good range */
99 if (level < LLAPI_MSG_OFF)
100 llapi_msg_level = LLAPI_MSG_OFF;
101 else if (level > LLAPI_MSG_MAX)
102 llapi_msg_level = LLAPI_MSG_MAX;
104 llapi_msg_level = level;
107 int llapi_msg_get_level(void)
109 return llapi_msg_level;
112 void llapi_set_command_name(const char *cmd)
114 liblustreapi_cmd = cmd;
117 void llapi_clear_command_name(void)
119 liblustreapi_cmd = NULL;
122 static void error_callback_default(enum llapi_message_level level, int err,
123 const char *fmt, va_list ap)
125 bool has_nl = strchr(fmt, '\n') != NULL;
127 if (liblustreapi_cmd != NULL)
128 fprintf(stderr, "%s %s: ", program_invocation_short_name,
131 fprintf(stderr, "%s: ", program_invocation_short_name);
134 if (level & LLAPI_MSG_NO_ERRNO) {
135 vfprintf(stderr, fmt, ap);
137 fprintf(stderr, "\n");
142 * Remove trailing linefeed so error string can be appended.
143 * @fmt is a const string, so we can't modify it directly.
145 if (has_nl && (newfmt = strdup(fmt)))
146 *strrchr(newfmt, '\n') = '\0';
148 newfmt = (char *)fmt;
150 vfprintf(stderr, newfmt, ap);
153 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
157 static void info_callback_default(enum llapi_message_level level, int err,
158 const char *fmt, va_list ap)
161 if (liblustreapi_cmd != NULL) {
162 fprintf(stdout, "%s %s: ",
163 program_invocation_short_name,
166 fprintf(stdout, "%s: ", program_invocation_short_name);
169 vfprintf(stdout, fmt, ap);
172 static llapi_log_callback_t llapi_error_callback = error_callback_default;
173 static llapi_log_callback_t llapi_info_callback = info_callback_default;
176 /* llapi_error will preserve errno */
177 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
180 int tmp_errno = errno;
182 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
186 llapi_error_callback(level, abs(err), fmt, args);
191 /* llapi_printf will preserve errno */
192 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
195 int tmp_errno = errno;
197 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
201 llapi_info_callback(level, 0, fmt, args);
207 * Set a custom error logging function. Passing in NULL will reset the logging
208 * callback to its default value.
210 * This function returns the value of the old callback.
212 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
214 llapi_log_callback_t old = llapi_error_callback;
217 llapi_error_callback = cb;
219 llapi_error_callback = error_callback_default;
225 * Set a custom info logging function. Passing in NULL will reset the logging
226 * callback to its default value.
228 * This function returns the value of the old callback.
230 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
232 llapi_log_callback_t old = llapi_info_callback;
235 llapi_info_callback = cb;
237 llapi_info_callback = info_callback_default;
243 * size_units is to be initialized (or zeroed) by caller.
245 int llapi_parse_size(const char *optarg, unsigned long long *size,
246 unsigned long long *size_units, int bytes_spec)
249 char *argbuf = (char *)optarg;
250 unsigned long long frac = 0, frac_d = 1;
252 if (strncmp(optarg, "-", 1) == 0)
255 if (*size_units == 0)
258 *size = strtoull(argbuf, &end, 0);
259 if (end != NULL && *end == '.') {
263 frac = strtoull(argbuf, &end, 10);
264 /* count decimal places */
265 for (i = 0; i < (end - argbuf); i++)
270 if ((*end == 'b') && *(end + 1) == '\0' &&
271 (*size & (~0ULL << (64 - 9))) == 0 &&
273 *size_units = 1 << 9;
274 } else if ((*end == 'b') &&
275 *(end + 1) == '\0' &&
278 } else if ((*end == 'k' || *end == 'K') &&
279 *(end + 1) == '\0' &&
280 (*size & (~0ULL << (64 - 10))) == 0) {
281 *size_units = 1 << 10;
282 } else if ((*end == 'm' || *end == 'M') &&
283 *(end + 1) == '\0' &&
284 (*size & (~0ULL << (64 - 20))) == 0) {
285 *size_units = 1 << 20;
286 } else if ((*end == 'g' || *end == 'G') &&
287 *(end + 1) == '\0' &&
288 (*size & (~0ULL << (64 - 30))) == 0) {
289 *size_units = 1 << 30;
290 } else if ((*end == 't' || *end == 'T') &&
291 *(end + 1) == '\0' &&
292 (*size & (~0ULL << (64 - 40))) == 0) {
293 *size_units = 1ULL << 40;
294 } else if ((*end == 'p' || *end == 'P') &&
295 *(end + 1) == '\0' &&
296 (*size & (~0ULL << (64 - 50))) == 0) {
297 *size_units = 1ULL << 50;
298 } else if ((*end == 'e' || *end == 'E') &&
299 *(end + 1) == '\0' &&
300 (*size & (~0ULL << (64 - 60))) == 0) {
301 *size_units = 1ULL << 60;
306 *size = *size * *size_units + frac * *size_units / frac_d;
311 int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
313 struct obd_ioctl_data *overlay;
316 data->ioc_len = obd_ioctl_packlen(data);
317 data->ioc_version = OBD_IOCTL_VERSION;
319 if (*pbuf != NULL && data->ioc_len > max_len) {
320 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
321 "pbuf = %p, ioc_len = %u, max_len = %d",
322 *pbuf, data->ioc_len, max_len);
327 *pbuf = malloc(data->ioc_len);
332 overlay = (struct obd_ioctl_data *)*pbuf;
333 memcpy(*pbuf, data, sizeof(*data));
335 ptr = overlay->ioc_bulk;
336 if (data->ioc_inlbuf1) {
337 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
338 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
341 if (data->ioc_inlbuf2) {
342 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
343 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
346 if (data->ioc_inlbuf3) {
347 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
348 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
351 if (data->ioc_inlbuf4) {
352 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
353 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
359 int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
361 struct obd_ioctl_data *overlay;
367 overlay = (struct obd_ioctl_data *)pbuf;
369 /* Preserve the caller's buffer pointers */
370 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
371 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
372 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
373 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
375 memcpy(data, pbuf, sizeof(*data));
377 ptr = overlay->ioc_bulk;
378 if (data->ioc_inlbuf1) {
379 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
380 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
383 if (data->ioc_inlbuf2) {
384 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
385 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
388 if (data->ioc_inlbuf3) {
389 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
390 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
393 if (data->ioc_inlbuf4) {
394 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
395 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
401 /* XXX: llapi_xxx() functions return negative values upon failure */
403 int llapi_layout_search_ost(__u32 ost, char *pname, char *fsname)
405 char ostname[MAX_OBD_NAME + 64];
406 char *pool_name = pname;
410 * The current policy is that the pool does not have to exist at the
411 * setstripe time, see sanity-pfl/-flr tests.
412 * If this logic will change, re-enable it.
414 * if (pname && strlen(pname) == 0)
418 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
420 rc = llapi_search_ost(fsname, pool_name, ostname);
425 llapi_error(LLAPI_MSG_ERROR, rc,
426 "%s: cannot find OST %s in %s", __func__, ostname,
427 pool_name != NULL ? "pool" : "system");
435 * Verify the setstripe parameters before using.
436 * This is a pair method for comp_args_to_layout()/llapi_layout_sanity_cb()
437 * when just 1 component or a non-PFL layout is given.
439 * \param[in] param stripe parameters
440 * \param[in] pool_name pool name
441 * \param[in] fsname lustre FS name
444 * < 0, error code on failre
446 static int llapi_stripe_param_verify(const struct llapi_stripe_param *param,
451 static int page_size;
454 if (page_size == 0) {
456 * 64 KB is the largest common page size (on ia64/PPC/ARM),
457 * but check the local page size just in case. The page_size
458 * will not change for the lifetime of this process at least.
460 page_size = LOV_MIN_STRIPE_SIZE;
461 if (getpagesize() > page_size) {
462 page_size = getpagesize();
463 llapi_err_noerrno(LLAPI_MSG_WARN,
464 "warning: page size (%u) larger than expected (%u)",
465 page_size, LOV_MIN_STRIPE_SIZE);
468 if (!llapi_stripe_size_is_aligned(param->lsp_stripe_size)) {
470 llapi_error(LLAPI_MSG_ERROR, rc,
471 "error: bad stripe_size %llu, must be an even multiple of %d bytes",
472 param->lsp_stripe_size, page_size);
475 if (!llapi_stripe_index_is_valid(param->lsp_stripe_offset)) {
477 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
478 param->lsp_stripe_offset);
481 if (llapi_stripe_size_is_too_big(param->lsp_stripe_size)) {
483 llapi_error(LLAPI_MSG_ERROR, rc,
484 "error: stripe size '%llu' over 4GB limit",
485 param->lsp_stripe_size);
489 count = param->lsp_stripe_count;
490 if (param->lsp_stripe_pattern == LOV_PATTERN_MDT) {
492 llapi_error(LLAPI_MSG_ERROR, rc,
493 "Invalid pattern: %d, must be specified with -E\n",
494 param->lsp_stripe_pattern);
497 if (!llapi_stripe_count_is_valid(count)) {
499 llapi_error(LLAPI_MSG_ERROR, rc,
500 "Invalid stripe count %d\n", count);
505 /* Make sure we have a good pool */
506 if (*pool_name != NULL) {
507 if (!llapi_pool_name_is_valid(pool_name, fsname)) {
509 llapi_error(LLAPI_MSG_ERROR, rc,
510 "Pool '%s' is not on filesystem '%s'",
515 /* Make sure the pool exists and is non-empty */
516 rc = llapi_search_ost(fsname, *pool_name, NULL);
518 char *err = rc == 0 ? "has no OSTs" : "does not exist";
521 llapi_error(LLAPI_MSG_ERROR, rc, "pool '%s.%s' %s",
522 fsname, *pool_name, err);
528 /* sanity check of target list */
529 if (param->lsp_is_specific) {
533 for (i = 0; i < count; i++) {
534 rc = llapi_layout_search_ost(param->lsp_osts[i],
539 /* Make sure stripe offset is in OST list. */
540 if (param->lsp_osts[i] == param->lsp_stripe_offset)
545 llapi_error(LLAPI_MSG_ERROR, rc,
546 "%s: stripe offset '%d' is not in the target list",
547 __func__, param->lsp_stripe_offset);
550 } else if (param->lsp_stripe_offset != -1) {
551 rc = llapi_layout_search_ost(param->lsp_stripe_offset,
561 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
566 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
568 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
572 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
574 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
579 if (!llapi_dir_hash_type_is_valid(hash_type)) {
581 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
589 * Trim a trailing newline from a string, if it exists.
591 int llapi_chomp_string(char *buf)
607 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
609 static int get_param_lov(const char *path, const char *param,
610 char *buf, size_t buf_size)
612 struct obd_uuid uuid;
615 rc = llapi_file_get_lov_uuid(path, &uuid);
619 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
624 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
626 static int get_param_lmv(const char *path, const char *param,
627 char *buf, size_t buf_size)
629 struct obd_uuid uuid;
632 rc = llapi_file_get_lmv_uuid(path, &uuid);
636 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
640 static int get_mds_md_size(const char *path)
642 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
645 * Rather than open the file and do the ioctl to get the
646 * instance name and close the file and search for the param
647 * file and open the param file and read the param file and
648 * parse the value and close the param file, let's just return
649 * a large enough value. It's 2020, RAM is cheap and this is
653 if (md_size < XATTR_SIZE_MAX)
654 md_size = XATTR_SIZE_MAX;
659 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
661 return get_param_lmv(path, "uuid", buf, bufsize);
665 * Open a Lustre file.
667 * \param name the name of the file to be opened
668 * \param flags access mode, see flags in open(2)
669 * \param mode permission of the file if it is created, see mode in open(2)
670 * \param param stripe pattern of the newly created file
672 * \retval file descriptor of opened file
673 * \retval negative errno on failure
675 int llapi_file_open_param(const char *name, int flags, mode_t mode,
676 const struct llapi_stripe_param *param)
678 char fsname[MAX_OBD_NAME + 1] = { 0 };
679 struct lov_user_md *lum = NULL;
680 char *pool_name = param->lsp_pool;
684 /* Make sure we are on a Lustre file system */
685 rc = llapi_search_fsname(name, fsname);
687 llapi_error(LLAPI_MSG_ERROR, rc,
688 "'%s' is not on a Lustre filesystem",
693 /* Check if the stripe pattern is sane. */
694 rc = llapi_stripe_param_verify(param, &pool_name, fsname);
698 if (param->lsp_is_specific)
699 lum_size = lov_user_md_size(param->lsp_stripe_count,
700 LOV_USER_MAGIC_SPECIFIC);
702 lum_size = sizeof(struct lov_user_md_v3);
704 lum_size = sizeof(*lum);
706 lum = calloc(1, lum_size);
711 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
713 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
714 flags = O_DIRECTORY | O_RDONLY;
721 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
726 /* Initialize IOCTL striping pattern structure */
727 lum->lmm_magic = LOV_USER_MAGIC_V1;
728 lum->lmm_pattern = param->lsp_stripe_pattern;
729 lum->lmm_stripe_size = param->lsp_stripe_size;
730 lum->lmm_stripe_count = param->lsp_stripe_count;
731 lum->lmm_stripe_offset = param->lsp_stripe_offset;
732 if (pool_name != NULL) {
733 struct lov_user_md_v3 *lumv3 = (void *)lum;
735 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
736 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
738 if (param->lsp_is_specific) {
739 struct lov_user_md_v3 *lumv3 = (void *)lum;
742 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
743 if (pool_name == NULL) {
745 * LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
746 * OST list, therefore if pool is not specified we have
747 * to pack a null pool name for placeholder.
749 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
752 for (i = 0; i < param->lsp_stripe_count; i++)
753 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
756 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
757 char errmsg[512] = "stripe already set";
760 if (errno != EEXIST && errno != EALREADY)
761 strncpy(errmsg, strerror(errno), sizeof(errmsg) - 1);
762 if (rc == -EREMOTEIO)
763 snprintf(errmsg, sizeof(errmsg),
764 "inactive OST among your specified %d OST(s)",
765 param->lsp_stripe_count);
767 llapi_err_noerrno(LLAPI_MSG_ERROR,
768 "setstripe error for '%s': %s", name, errmsg);
779 int llapi_file_open_pool(const char *name, int flags, int mode,
780 unsigned long long stripe_size, int stripe_offset,
781 int stripe_count, int stripe_pattern, char *pool_name)
783 const struct llapi_stripe_param param = {
784 .lsp_stripe_size = stripe_size,
785 .lsp_stripe_count = stripe_count,
786 .lsp_stripe_pattern = stripe_pattern,
787 .lsp_stripe_offset = stripe_offset,
788 .lsp_pool = pool_name
790 return llapi_file_open_param(name, flags, mode, ¶m);
793 int llapi_file_open(const char *name, int flags, int mode,
794 unsigned long long stripe_size, int stripe_offset,
795 int stripe_count, int stripe_pattern)
797 return llapi_file_open_pool(name, flags, mode, stripe_size,
798 stripe_offset, stripe_count,
799 stripe_pattern, NULL);
802 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
803 __u32 flags, char *foreign_lov)
806 struct lov_foreign_md *lfm;
809 if (foreign_lov == NULL) {
811 llapi_error(LLAPI_MSG_ERROR, rc,
812 "foreign LOV EA content must be provided");
816 len = strlen(foreign_lov);
817 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
820 llapi_error(LLAPI_MSG_ERROR, rc,
821 "foreign LOV EA size %zu (must be 0 < len < %zu)",
822 len, XATTR_SIZE_MAX -
823 offsetof(struct lov_foreign_md, lfm_value));
827 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
830 llapi_error(LLAPI_MSG_ERROR, rc,
831 "failed to allocate lov_foreign_md");
835 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
838 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
842 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
843 lfm->lfm_length = len;
844 lfm->lfm_type = type;
845 lfm->lfm_flags = flags;
846 memcpy(lfm->lfm_value, foreign_lov, len);
848 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
849 char *errmsg = "stripe already set";
853 errmsg = "not on a Lustre filesystem";
854 else if (errno == EEXIST || errno == EALREADY)
855 errmsg = "stripe already set";
857 errmsg = strerror(errno);
859 llapi_err_noerrno(LLAPI_MSG_ERROR,
860 "setstripe error for '%s': %s", name, errmsg);
876 int llapi_file_create(const char *name, unsigned long long stripe_size,
877 int stripe_offset, int stripe_count, int stripe_pattern)
881 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
882 stripe_offset, stripe_count, stripe_pattern,
891 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
892 int stripe_offset, int stripe_count,
893 int stripe_pattern, char *pool_name)
897 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
898 stripe_offset, stripe_count, stripe_pattern,
907 static int verify_dir_param(const char *name,
908 const struct llapi_stripe_param *param)
910 char fsname[MAX_OBD_NAME + 1] = { 0 };
911 char *pool_name = param->lsp_pool;
914 /* Make sure we are on a Lustre file system */
915 rc = llapi_search_fsname(name, fsname);
917 llapi_error(LLAPI_MSG_ERROR, rc,
918 "'%s' is not on a Lustre filesystem",
923 /* Check if the stripe pattern is sane. */
924 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
925 param->lsp_stripe_count,
926 param->lsp_stripe_pattern);
930 /* Make sure we have a good pool */
931 if (pool_name != NULL) {
933 * in case user gives the full pool name <fsname>.<poolname>,
936 char *ptr = strchr(pool_name, '.');
940 if (strcmp(pool_name, fsname) != 0) {
942 llapi_err_noerrno(LLAPI_MSG_ERROR,
943 "Pool '%s' is not on filesystem '%s'",
950 /* Make sure the pool exists and is non-empty */
951 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
953 char *err = rc == 0 ? "has no OSTs" : "does not exist";
955 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
956 fsname, pool_name, err);
961 /* sanity check of target list */
962 if (param->lsp_is_specific) {
963 char mdtname[MAX_OBD_NAME + 64];
967 for (i = 0; i < param->lsp_stripe_count; i++) {
968 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
969 fsname, param->lsp_tgts[i]);
970 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
975 llapi_error(LLAPI_MSG_ERROR, rc,
976 "%s: cannot find MDT %s in %s",
983 /* Make sure stripe offset is in MDT list. */
984 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
988 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
989 "%s: stripe offset '%d' is not in the target list",
990 __func__, param->lsp_stripe_offset);
998 static inline void param2lmu(struct lmv_user_md *lmu,
999 const struct llapi_stripe_param *param)
1001 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
1003 lmu->lum_stripe_count = param->lsp_stripe_count;
1004 lmu->lum_stripe_offset = param->lsp_stripe_offset;
1005 lmu->lum_hash_type = param->lsp_stripe_pattern;
1006 lmu->lum_max_inherit = param->lsp_max_inherit;
1007 lmu->lum_max_inherit_rr = param->lsp_max_inherit_rr;
1008 if (param->lsp_pool != NULL)
1009 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
1010 if (param->lsp_is_specific) {
1013 for (i = 0; i < param->lsp_stripe_count; i++)
1014 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
1018 int llapi_dir_set_default_lmv(const char *name,
1019 const struct llapi_stripe_param *param)
1021 struct lmv_user_md lmu = { 0 };
1025 rc = verify_dir_param(name, param);
1029 /* TODO: default lmv doesn't support specific targets yet */
1030 if (param->lsp_is_specific)
1033 param2lmu(&lmu, param);
1035 fd = open(name, O_DIRECTORY | O_RDONLY);
1038 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1042 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
1044 char *errmsg = "stripe already set";
1047 if (errno != EEXIST && errno != EALREADY)
1048 errmsg = strerror(errno);
1050 llapi_err_noerrno(LLAPI_MSG_ERROR,
1051 "default dirstripe error on '%s': %s",
1058 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
1059 int stripe_count, int stripe_pattern,
1060 const char *pool_name)
1062 const struct llapi_stripe_param param = {
1063 .lsp_stripe_count = stripe_count,
1064 .lsp_stripe_offset = stripe_offset,
1065 .lsp_stripe_pattern = stripe_pattern,
1066 .lsp_pool = (char *)pool_name
1069 return llapi_dir_set_default_lmv(name, ¶m);
1073 * Create a Lustre directory.
1075 * \param name the name of the directory to be created
1076 * \param mode permission of the file if it is created, see mode in open(2)
1077 * \param param stripe pattern of the newly created directory
1079 * \retval 0 on success
1080 * \retval negative errno on failure
1082 int llapi_dir_create(const char *name, mode_t mode,
1083 const struct llapi_stripe_param *param)
1085 struct lmv_user_md *lmu = NULL;
1087 struct obd_ioctl_data data = { 0 };
1090 char *dirpath = NULL;
1091 char *namepath = NULL;
1096 rc = verify_dir_param(name, param);
1100 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1101 param->lsp_is_specific ?
1102 LMV_USER_MAGIC_SPECIFIC :
1105 lmu = calloc(1, lmu_size);
1109 dirpath = strdup(name);
1115 namepath = strdup(name);
1122 param2lmu(lmu, param);
1124 filename = basename(namepath);
1125 dir = dirname(dirpath);
1127 data.ioc_inlbuf1 = (char *)filename;
1128 data.ioc_inllen1 = strlen(filename) + 1;
1129 data.ioc_inlbuf2 = (char *)lmu;
1130 data.ioc_inllen2 = lmu_size;
1131 data.ioc_type = mode;
1132 if (param->lsp_is_create)
1133 /* borrow obdo1.o_flags to store this flag */
1134 data.ioc_obdo1.o_flags = OBD_FL_OBDMDEXISTS;
1135 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1137 llapi_error(LLAPI_MSG_ERROR, rc,
1138 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1143 fd = open(dir, O_DIRECTORY | O_RDONLY);
1146 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1150 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1151 char *errmsg = "stripe already set";
1154 if (errno != EEXIST && errno != EALREADY)
1155 errmsg = strerror(errno);
1157 llapi_err_noerrno(LLAPI_MSG_ERROR,
1158 "dirstripe error on '%s': %s", name, errmsg);
1169 * Create a foreign directory.
1171 * \param name the name of the directory to be created
1172 * \param mode permission of the file if it is created, see mode in open(2)
1173 * \param type foreign type to be set in LMV EA
1174 * \param flags foreign flags to be set in LMV EA
1175 * \param value foreign pattern to be set in LMV EA
1177 * \retval 0 on success
1178 * \retval negative errno on failure
1180 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1181 __u32 flags, const char *value)
1183 struct lmv_foreign_md *lfm = NULL;
1184 size_t lfm_size, len;
1185 struct obd_ioctl_data data = { 0 };
1188 char *dirpath = NULL;
1189 char *namepath = NULL;
1194 len = strlen(value);
1195 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1198 llapi_error(LLAPI_MSG_ERROR, rc,
1199 "invalid LOV EA length %zu (must be 0 < len < %zu)",
1200 len, XATTR_SIZE_MAX -
1201 offsetof(struct lmv_foreign_md, lfm_value));
1204 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1205 lfm = calloc(1, lfm_size);
1209 dirpath = strdup(name);
1215 namepath = strdup(name);
1222 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1223 lfm->lfm_length = len;
1224 lfm->lfm_type = type;
1225 lfm->lfm_flags = flags;
1226 memcpy(lfm->lfm_value, value, len);
1228 filename = basename(namepath);
1229 dir = dirname(dirpath);
1231 data.ioc_inlbuf1 = (char *)filename;
1232 data.ioc_inllen1 = strlen(filename) + 1;
1233 data.ioc_inlbuf2 = (char *)lfm;
1234 data.ioc_inllen2 = lfm_size;
1235 data.ioc_type = mode;
1236 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1238 llapi_error(LLAPI_MSG_ERROR, rc,
1239 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1244 fd = open(dir, O_DIRECTORY | O_RDONLY);
1247 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1251 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1252 char *errmsg = "stripe already set";
1255 if (errno != EEXIST && errno != EALREADY)
1256 errmsg = strerror(errno);
1258 llapi_err_noerrno(LLAPI_MSG_ERROR,
1259 "dirstripe error on '%s': %s", name, errmsg);
1269 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1270 int stripe_count, int stripe_pattern,
1271 const char *pool_name)
1273 const struct llapi_stripe_param param = {
1274 .lsp_stripe_count = stripe_count,
1275 .lsp_stripe_offset = stripe_offset,
1276 .lsp_stripe_pattern = stripe_pattern,
1277 .lsp_pool = (char *)pool_name
1280 return llapi_dir_create(name, mode, ¶m);
1284 * Find the fsname, the full path, and/or an open fd.
1285 * Either the fsname or path must not be NULL
1287 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1290 char buf[PATH_MAX], mntdir[PATH_MAX];
1291 char *ptr, *ptr_end;
1293 int idx = 0, mntlen = 0, fd;
1295 int fsnamelen, mountlen;
1297 /* get the mount point */
1298 fp = setmntent(PROC_MOUNTS, "r");
1301 llapi_error(LLAPI_MSG_ERROR, rc,
1302 "cannot retrieve filesystem mount point");
1306 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1309 if (!llapi_is_lustre_mnt(&mnt))
1312 if ((want & WANT_INDEX) && (idx++ != index))
1315 mntlen = strlen(mnt.mnt_dir);
1316 ptr = strchr(mnt.mnt_fsname, '/');
1317 while (ptr && *ptr == '/')
1320 * thanks to the call to llapi_is_lustre_mnt() above,
1321 * we are sure that mnt.mnt_fsname contains ":/",
1322 * so ptr should never be NULL
1327 while (*ptr_end != '/' && *ptr_end != '\0')
1330 /* Check the fsname for a match, if given */
1331 mountlen = ptr_end - ptr;
1332 if (!(want & WANT_FSNAME) && fsname != NULL &&
1333 (fsnamelen = strlen(fsname)) > 0 &&
1334 (fsnamelen != mountlen ||
1335 (strncmp(ptr, fsname, mountlen) != 0)))
1338 /* If the path isn't set return the first one we find */
1339 if (path == NULL || strlen(path) == 0) {
1340 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1341 mntdir[sizeof(mntdir) - 1] = '\0';
1342 if ((want & WANT_FSNAME) && fsname != NULL) {
1343 strncpy(fsname, ptr, mountlen);
1344 fsname[mountlen] = '\0';
1348 /* Otherwise find the longest matching path */
1349 } else if ((strlen(path) >= mntlen) &&
1350 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1351 /* check the path format */
1352 if (strlen(path) > mntlen && path[mntlen] != '/')
1354 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1355 mntdir[sizeof(mntdir) - 1] = '\0';
1356 if ((want & WANT_FSNAME) && fsname != NULL) {
1357 strncpy(fsname, ptr, mountlen);
1358 fsname[mountlen] = '\0';
1368 if ((want & WANT_PATH) && path != NULL) {
1369 strncpy(path, mntdir, mntlen);
1370 path[mntlen] = '\0';
1372 if (want & WANT_FD) {
1373 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1376 llapi_error(LLAPI_MSG_ERROR, rc,
1377 "cannot open '%s'", mntdir);
1383 } else if (want & WANT_ERROR)
1384 llapi_err_noerrno(LLAPI_MSG_ERROR,
1385 "'%s' not on a mounted Lustre filesystem",
1386 (want & WANT_PATH) ? fsname : path);
1391 * search lustre mounts
1393 * Calling this function will return to the user the mount point, mntdir, and
1394 * the file system name, fsname, if the user passed a buffer to this routine.
1396 * The user inputs are pathname and index. If the pathname is supplied then
1397 * the value of the index will be ignored. The pathname will return data if
1398 * the pathname is located on a lustre mount. Index is used to pick which
1399 * mount point you want in the case of multiple mounted lustre file systems.
1400 * See function lfs_osts in lfs.c for an example of the index use.
1402 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1405 int want = WANT_PATH, idx = -1;
1407 if (!pathname || pathname[0] == '\0') {
1411 strcpy(mntdir, pathname);
1415 want |= WANT_FSNAME;
1416 return get_root_path(want, fsname, NULL, mntdir, idx);
1419 /* Given a path, find the corresponding Lustre fsname */
1420 int llapi_search_fsname(const char *pathname, char *fsname)
1425 path = realpath(pathname, NULL);
1427 char tmp[PATH_MAX - 1];
1433 if (pathname[0] != '/') {
1435 * Need an absolute path, but realpath() only works for
1436 * pathnames that actually exist. We go through the
1437 * extra hurdle of dirname(getcwd() + pathname) in
1438 * case the relative pathname contains ".." in it.
1440 char realpath[PATH_MAX - 1];
1442 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1444 llapi_error(LLAPI_MSG_ERROR, rc,
1445 "cannot get current working directory");
1449 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1450 if (rc >= sizeof(tmp)) {
1452 llapi_error(LLAPI_MSG_ERROR, rc,
1453 "invalid parent path '%s'",
1459 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1460 if (rc >= sizeof(buf)) {
1462 llapi_error(LLAPI_MSG_ERROR, rc,
1463 "invalid path '%s'", pathname);
1466 path = realpath(buf, NULL);
1468 ptr = strrchr(buf, '/');
1470 llapi_error(LLAPI_MSG_ERROR |
1471 LLAPI_MSG_NO_ERRNO, 0,
1472 "cannot resolve path '%s'",
1477 path = realpath(buf, NULL);
1480 llapi_error(LLAPI_MSG_ERROR, rc,
1481 "cannot resolve path '%s'",
1487 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1492 int llapi_search_rootpath(char *pathname, const char *fsname)
1495 * pathname can be used as an argument by get_root_path(),
1496 * clear it for safety
1499 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1503 * Get the list of pool members.
1504 * \param poolname string of format \<fsname\>.\<poolname\>
1505 * \param members caller-allocated array of char*
1506 * \param list_size size of the members array
1507 * \param buffer caller-allocated buffer for storing OST names
1508 * \param buffer_size size of the buffer
1510 * \return number of members retrieved for this pool
1511 * \retval -error failure
1513 int llapi_get_poolmembers(const char *poolname, char **members,
1514 int list_size, char *buffer, int buffer_size)
1516 char fsname[PATH_MAX];
1525 /* name is FSNAME.POOLNAME */
1526 if (strlen(poolname) >= sizeof(fsname))
1529 snprintf(fsname, sizeof(fsname), "%s", poolname);
1530 pool = strchr(fsname, '.');
1537 rc = poolpath(&pathname, fsname, NULL);
1539 llapi_error(LLAPI_MSG_ERROR, rc,
1540 "Lustre filesystem '%s' not found",
1545 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1546 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1547 cfs_free_param_data(&pathname);
1548 if (rc >= sizeof(buf))
1550 fd = fopen(buf, "r");
1553 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1558 while (fgets(buf, sizeof(buf), fd) != NULL) {
1559 if (nb_entries >= list_size) {
1563 buf[sizeof(buf) - 1] = '\0';
1565 tmp = strchr(buf, '\n');
1568 if (used + strlen(buf) + 1 > buffer_size) {
1573 strcpy(buffer + used, buf);
1574 members[nb_entries] = buffer + used;
1575 used += strlen(buf) + 1;
1585 * Get the list of pools in a filesystem.
1586 * \param name filesystem name or path
1587 * \param poollist caller-allocated array of char*
1588 * \param list_size size of the poollist array
1589 * \param buffer caller-allocated buffer for storing pool names
1590 * \param buffer_size size of the buffer
1592 * \return number of pools retrieved for this filesystem
1593 * \retval -error failure
1595 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1596 char *buffer, int buffer_size)
1602 struct dirent *pool;
1604 unsigned int nb_entries = 0;
1605 unsigned int used = 0;
1608 /* initialize output array */
1609 for (i = 0; i < list_size; i++)
1612 /* is name a pathname ? */
1613 ptr = strchr(name, '/');
1615 char fsname_buf[MAXNAMLEN];
1617 /* We will need fsname for printing later */
1618 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1622 ptr = strrchr(fsname_buf, '-');
1626 fsname = strdup(fsname_buf);
1630 /* name is FSNAME */
1631 fsname = strdup(name);
1636 rc = poolpath(&pathname, fsname, NULL);
1638 llapi_error(LLAPI_MSG_ERROR, rc,
1639 "Lustre filesystem '%s' not found", name);
1643 dir = opendir(pathname.gl_pathv[0]);
1646 llapi_error(LLAPI_MSG_ERROR, rc,
1647 "Could not open pool list for '%s'",
1654 pool = readdir(dir);
1660 /* ignore . and .. */
1661 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1664 /* check output bounds */
1665 if (nb_entries >= list_size) {
1667 goto free_dir_no_msg;
1670 /* +2 for '.' and final '\0' */
1671 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1674 goto free_dir_no_msg;
1677 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1678 poollist[nb_entries] = buffer + used;
1679 used += strlen(pool->d_name) + strlen(fsname) + 2;
1685 llapi_error(LLAPI_MSG_ERROR, rc,
1686 "Error reading pool list for '%s'", name);
1688 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1693 cfs_free_param_data(&pathname);
1696 return rc != 0 ? rc : nb_entries;
1699 /* wrapper for lfs.c and obd.c */
1700 int llapi_poollist(const char *name)
1702 int poolcount, rc, i;
1705 rc = llapi_get_poolbuf(name, &buf, &pools, &poolcount);
1709 for (i = 0; i < poolcount; i++)
1710 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", pools[i]);
1717 * Get buffer that holds uuids and the list of pools in a filesystem.
1719 * \param name filesystem name or path
1720 * \param buf bufffer that has to be freed if function returns 0
1721 * \param pools pointer to the list of pools in buffer
1722 * \param poolcount number of pools
1724 * \return 0 when found at least 1 pool, i.e. poolcount > 0
1725 * \retval -error failure
1727 int llapi_get_poolbuf(const char *name, char **buf,
1728 char ***pools, int *poolcount)
1731 * list of pool names (assume that pool count is smaller
1734 char **list, *buffer = NULL, *fsname = (char *)name;
1735 char *poolname = NULL, *tmp = NULL, data[16];
1736 enum param_filter type = FILTER_BY_PATH;
1737 int obdcount, bufsize, rc, nb;
1742 if (name[0] != '/') {
1743 fsname = strdup(name);
1747 poolname = strchr(fsname, '.');
1750 type = FILTER_BY_FS_NAME;
1753 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1754 data, sizeof(data));
1757 obdcount = atoi(data);
1760 * Allocate space for each fsname-OST0000_UUID, 1 per OST,
1761 * and also an array to store the pointers for all that
1765 bufsize = sizeof(struct obd_uuid) * obdcount;
1766 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1767 if (buffer == NULL) {
1771 list = (char **) (buffer + bufsize);
1774 /* name is a path or fsname */
1775 nb = llapi_get_poollist(name, list, obdcount,
1778 /* name is a pool name (<fsname>.<poolname>) */
1779 nb = llapi_get_poolmembers(name, list, obdcount,
1783 if (nb == -EOVERFLOW) {
1786 goto retry_get_pools;
1789 rc = (nb < 0 ? nb : 0);
1796 /* Don't free buffer, it will be used later */
1799 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1804 typedef int (semantic_func_t)(char *path, int p, int *d,
1805 void *data, struct dirent64 *de);
1807 #define OBD_NOT_FOUND (-1)
1809 static void find_param_fini(struct find_param *param)
1811 if (param->fp_migrate)
1814 if (param->fp_obd_indexes) {
1815 free(param->fp_obd_indexes);
1816 param->fp_obd_indexes = NULL;
1819 if (param->fp_lmd) {
1820 free(param->fp_lmd);
1821 param->fp_lmd = NULL;
1824 if (param->fp_lmv_md) {
1825 free(param->fp_lmv_md);
1826 param->fp_lmv_md = NULL;
1830 static int common_param_init(struct find_param *param, char *path)
1832 int lum_size = get_mds_md_size(path);
1837 /* migrate has fp_lmv_md initialized outside */
1838 if (param->fp_migrate)
1841 if (lum_size < PATH_MAX + 1)
1842 lum_size = PATH_MAX + 1;
1844 param->fp_lum_size = lum_size;
1845 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1847 if (param->fp_lmd == NULL) {
1848 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1849 "error: allocate %zu bytes for layout failed",
1850 sizeof(lstat_t) + param->fp_lum_size);
1854 param->fp_lmv_stripe_count = 256;
1855 param->fp_lmv_md = calloc(1,
1856 lmv_user_md_size(param->fp_lmv_stripe_count,
1857 LMV_USER_MAGIC_SPECIFIC));
1858 if (param->fp_lmv_md == NULL) {
1859 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1860 "error: allocation of %d bytes for ioctl",
1861 lmv_user_md_size(param->fp_lmv_stripe_count,
1862 LMV_USER_MAGIC_SPECIFIC));
1863 find_param_fini(param);
1867 param->fp_got_uuids = 0;
1868 param->fp_obd_indexes = NULL;
1869 param->fp_obd_index = OBD_NOT_FOUND;
1870 param->fp_mdt_index = OBD_NOT_FOUND;
1874 static int cb_common_fini(char *path, int p, int *dp, void *data,
1875 struct dirent64 *de)
1877 struct find_param *param = data;
1883 /* set errno upon failure */
1884 static int open_parent(const char *path)
1890 path_copy = strdup(path);
1891 if (path_copy == NULL)
1894 parent_path = dirname(path_copy);
1895 parent = open(parent_path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1901 static int cb_get_dirstripe(char *path, int *d, struct find_param *param)
1904 bool did_nofollow = false;
1907 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1908 if (param->fp_get_default_lmv)
1909 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1911 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1913 ret = ioctl(*d, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1915 /* if ENOTTY likely to be a fake symlink, so try again after
1916 * new open() with O_NOFOLLOW, but only once to prevent any
1917 * loop like for the path of a file/dir not on Lustre !!
1919 if (ret < 0 && errno == ENOTTY && !did_nofollow) {
1922 did_nofollow = true;
1923 fd = open(path, O_RDONLY | O_NOFOLLOW);
1925 /* restore original errno */
1930 /* close original fd and set new */
1933 ret2 = ioctl(fd, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1934 if (ret2 < 0 && errno != E2BIG) {
1935 /* restore original errno */
1939 /* LMV is ok or need to handle E2BIG case now */
1943 if (errno == E2BIG && ret != 0) {
1947 /* if foreign LMV case, fake stripes number */
1948 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
1949 struct lmv_foreign_md *lfm;
1951 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1952 if (lfm->lfm_length < XATTR_SIZE_MAX -
1953 offsetof(typeof(*lfm), lfm_value)) {
1954 uint32_t size = lfm->lfm_length +
1955 offsetof(typeof(*lfm), lfm_value);
1957 stripe_count = lmv_foreign_to_md_stripes(size);
1959 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1960 "error: invalid %d foreign size returned from ioctl",
1965 stripe_count = param->fp_lmv_md->lum_stripe_count;
1967 if (stripe_count <= param->fp_lmv_stripe_count)
1970 free(param->fp_lmv_md);
1971 param->fp_lmv_stripe_count = stripe_count;
1972 lmv_size = lmv_user_md_size(stripe_count,
1973 LMV_USER_MAGIC_SPECIFIC);
1974 param->fp_lmv_md = malloc(lmv_size);
1975 if (param->fp_lmv_md == NULL) {
1976 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1977 "error: allocation of %d bytes for ioctl",
1978 lmv_user_md_size(param->fp_lmv_stripe_count,
1979 LMV_USER_MAGIC_SPECIFIC));
1987 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1990 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
1991 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
1992 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
1993 lmd_v2->lmd_stx.stx_uid = st->st_uid;
1994 lmd_v2->lmd_stx.stx_gid = st->st_gid;
1995 lmd_v2->lmd_stx.stx_mode = st->st_mode;
1996 lmd_v2->lmd_stx.stx_ino = st->st_ino;
1997 lmd_v2->lmd_stx.stx_size = st->st_size;
1998 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
1999 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
2000 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
2001 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
2002 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
2003 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
2004 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
2005 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
2006 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
2008 lmd_v2->lmd_flags = 0;
2010 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
2012 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
2013 if (lmd_v2->lmd_stx.stx_size)
2014 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
2015 if (lmd_v2->lmd_stx.stx_blocks)
2016 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
2018 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
2019 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
2020 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
2025 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
2027 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
2028 struct lov_user_mds_data *lmd_v2 = lmdbuf;
2032 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
2036 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
2039 st = lmd_v1->lmd_st;
2040 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
2041 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
2042 convert_lmd_statx(lmd_v2, &st, false);
2043 lmd_v2->lmd_lmmsize = 0;
2044 lmd_v2->lmd_padding = 0;
2049 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
2050 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
2052 struct lov_user_mds_data *lmd = lmdbuf;
2053 static bool use_old_ioctl;
2057 if (parent_fd < 0 && dir_fd < 0)
2059 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
2064 * LL_IOC_MDC_GETINFO operates on the current directory inode
2065 * and returns struct lov_user_mds_data, while
2066 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
2068 if (type == GET_LMD_INFO)
2069 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_V1 :
2070 LL_IOC_MDC_GETINFO_V2;
2072 cmd = LL_IOC_LOV_GETSTRIPE;
2075 ret = ioctl(dir_fd, cmd, lmdbuf);
2076 if (ret < 0 && errno == ENOTTY &&
2077 cmd == LL_IOC_MDC_GETINFO_V2) {
2078 cmd = LL_IOC_MDC_GETINFO_V1;
2079 use_old_ioctl = true;
2083 if (cmd == LL_IOC_MDC_GETINFO_V1 && !ret)
2084 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2086 if (ret < 0 && errno == ENOTTY && type == GET_LMD_STRIPE) {
2089 /* retry ioctl() after new open() with O_NOFOLLOW
2090 * just in case it could be a fake symlink
2091 * need using a new open() as dir_fd is being closed
2095 dir_fd2 = open(path, O_RDONLY | O_NDELAY | O_NOFOLLOW);
2097 /* return original error */
2100 ret = ioctl(dir_fd2, cmd, lmdbuf);
2101 /* pass new errno or success back to caller */
2107 } else if (parent_fd >= 0) {
2108 const char *fname = strrchr(path, '/');
2111 * IOC_MDC_GETFILEINFO takes as input the filename (relative to
2112 * the parent directory) and returns struct lov_user_mds_data,
2113 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2115 * This avoids opening, locking, and closing each file on the
2116 * client if that is not needed. Multiple of these ioctl() can
2117 * be done on the parent dir with a single open for all
2118 * files in that directory, and it also doesn't pollute the
2119 * client dcache with millions of dentries when traversing
2120 * a large filesystem.
2122 fname = (fname == NULL ? path : fname + 1);
2124 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2127 else if (ret >= lmdlen || ret++ == 0)
2130 if (type == GET_LMD_INFO)
2131 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_V1 :
2132 IOC_MDC_GETFILEINFO_V2;
2134 cmd = IOC_MDC_GETFILESTRIPE;
2137 ret = ioctl(parent_fd, cmd, lmdbuf);
2138 if (ret < 0 && errno == ENOTTY &&
2139 cmd == IOC_MDC_GETFILEINFO_V2) {
2140 cmd = IOC_MDC_GETFILEINFO_V1;
2141 use_old_ioctl = true;
2142 goto retry_getfileinfo;
2145 if (cmd == IOC_MDC_GETFILEINFO_V1 && !ret)
2146 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2150 if (ret && type == GET_LMD_INFO) {
2151 if (errno == ENOTTY) {
2155 * ioctl is not supported, it is not a lustre fs.
2156 * Do the regular lstat(2) instead.
2158 ret = lstat_f(path, &st);
2161 llapi_error(LLAPI_MSG_ERROR, ret,
2162 "error: %s: lstat failed for %s",
2166 convert_lmd_statx(lmd, &st, true);
2168 * It may be wrong to set use_old_ioctl with true as
2169 * the file is not a lustre fs. So reset it with false
2172 use_old_ioctl = false;
2173 } else if (errno == ENOENT) {
2175 llapi_error(LLAPI_MSG_WARN, ret,
2176 "warning: %s does not exist", path);
2177 } else if (errno != EISDIR && errno != ENODATA) {
2179 llapi_error(LLAPI_MSG_ERROR, ret,
2180 "%s ioctl failed for %s.",
2181 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2182 "IOC_MDC_GETFILEINFO", path);
2189 static int llapi_semantic_traverse(char *path, int size, int parent,
2190 semantic_func_t sem_init,
2191 semantic_func_t sem_fini, void *data,
2192 struct dirent64 *de)
2194 struct find_param *param = (struct find_param *)data;
2195 struct dirent64 *dent;
2196 int len, ret, d, p = -1;
2202 d = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
2203 /* if an invalid fake dir symlink, opendir() will return EINVAL
2204 * instead of ENOTDIR. If a valid but dangling faked or real file/dir
2205 * symlink ENOENT will be returned. For a valid/resolved fake or real
2206 * file symlink ENOTDIR will be returned as for a regular file.
2207 * opendir() will be successful for a valid and resolved fake or real
2208 * dir simlink or a regular dir.
2210 if (d == -1 && errno != ENOTDIR && errno != EINVAL && errno != ENOENT) {
2212 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2215 } else if (d == -1) {
2216 if (errno == ENOENT || errno == EINVAL) {
2217 int old_errno = errno;
2219 /* try to open with O_NOFOLLOW this will help
2220 * differentiate fake vs real symlinks
2221 * it is ok to not use O_DIRECTORY with O_RDONLY
2222 * and it will prevent the need to deal with ENOTDIR
2223 * error, instead of ELOOP, being returned by recent
2224 * kernels for real symlinks
2226 d = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2227 /* if a dangling real symlink should return ELOOP, or
2228 * again ENOENT if really non-existing path, or E...??
2229 * So return original error. If success or ENOTDIR, path
2230 * is likely to be a fake dir/file symlink, so continue
2240 if (parent == -1 && d == -1) {
2241 /* Open the parent dir. */
2242 p = open_parent(path);
2248 } else { /* d != -1 */
2251 /* try to reopen dir with O_NOFOLLOW just in case of a foreign
2254 d2 = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2259 /* continue with d */
2265 ret = sem_init(path, (parent != -1) ? parent : p, &d, data, de);
2275 /* ENOTDIR if fake symlink, do not consider it as an error */
2276 if (errno != ENOTDIR)
2277 llapi_error(LLAPI_MSG_ERROR, errno,
2278 "fdopendir() failed");
2285 while ((dent = readdir64(dir)) != NULL) {
2288 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2292 if ((len + dent->d_reclen + 2) > size) {
2293 llapi_err_noerrno(LLAPI_MSG_ERROR,
2294 "error: %s: string buffer too small for %s",
2299 strcat(path, dent->d_name);
2301 if (dent->d_type == DT_UNKNOWN) {
2302 struct lov_user_mds_data *lmd = param->fp_lmd;
2304 rc = get_lmd_info_fd(path, d, -1, param->fp_lmd,
2305 param->fp_lum_size, GET_LMD_INFO);
2307 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2314 switch (dent->d_type) {
2316 llapi_err_noerrno(LLAPI_MSG_ERROR,
2317 "error: %s: '%s' is UNKNOWN type %d",
2318 __func__, dent->d_name, dent->d_type);
2321 rc = llapi_semantic_traverse(path, size, d, sem_init,
2322 sem_fini, data, dent);
2323 if (rc != 0 && ret == 0)
2329 rc = sem_init(path, d, NULL, data, dent);
2330 if (rc < 0 && ret == 0) {
2335 if (sem_fini && rc == 0)
2336 sem_fini(path, d, NULL, data, dent);
2344 sem_fini(path, parent, &d, data, de);
2357 static int param_callback(char *path, semantic_func_t sem_init,
2358 semantic_func_t sem_fini, struct find_param *param)
2360 int ret, len = strlen(path);
2363 if (len > PATH_MAX) {
2365 llapi_error(LLAPI_MSG_ERROR, ret,
2366 "Path name '%s' is too long", path);
2370 buf = (char *)malloc(2 * PATH_MAX);
2374 snprintf(buf, PATH_MAX + 1, "%s", path);
2375 ret = common_param_init(param, buf);
2379 param->fp_depth = 0;
2381 ret = llapi_semantic_traverse(buf, 2 * PATH_MAX, -1, sem_init,
2382 sem_fini, param, NULL);
2384 find_param_fini(param);
2386 return ret < 0 ? ret : 0;
2389 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2393 rc = ioctl(fd, OBD_IOC_GETDTNAME, lov_name);
2394 if (rc && errno == ENOTTY)
2395 rc = ioctl(fd, OBD_IOC_GETNAME_OLD, lov_name);
2398 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2404 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2408 rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2411 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2417 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2421 /* do not follow faked symlinks */
2422 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
2424 /* real symlink should have failed with ELOOP so retry without
2425 * O_NOFOLLOW just in case
2427 fd = open(path, O_RDONLY | O_NONBLOCK);
2430 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'",
2436 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2442 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2446 fd = open(path, O_RDONLY | O_NONBLOCK);
2449 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2453 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2465 * If uuidp is NULL, return the number of available obd uuids.
2466 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2467 * there are more OSTs than allocated to uuidp, then an error is returned with
2468 * the ost_count set to number of available obd uuids.
2470 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2471 int *ost_count, enum tgt_type type)
2473 char buf[PATH_MAX], format[32];
2474 int rc = 0, index = 0;
2475 struct obd_uuid name;
2479 /* Get the lov name */
2480 if (type == LOV_TYPE)
2481 rc = llapi_file_fget_lov_uuid(fd, &name);
2483 rc = llapi_file_fget_lmv_uuid(fd, &name);
2487 /* Now get the ost uuids */
2488 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2489 FILTER_BY_EXACT, "target_obd", ¶m);
2493 fp = fopen(param.gl_pathv[0], "r");
2496 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2501 snprintf(format, sizeof(format),
2502 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2503 while (fgets(buf, sizeof(buf), fp) != NULL) {
2504 if (uuidp && (index < *ost_count)) {
2505 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2513 if (uuidp && (index > *ost_count))
2518 cfs_free_param_data(¶m);
2522 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2524 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2527 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2532 root = open(mnt, O_RDONLY | O_DIRECTORY);
2535 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2540 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2549 * Check if user specified value matches a real uuid. Ignore _UUID,
2550 * -osc-4ba41334, other trailing gunk in comparison.
2551 * @param real_uuid ends in "_UUID"
2552 * @param search_uuid may or may not end in "_UUID"
2554 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2556 int cmplen = strlen(real_uuid);
2557 int searchlen = strlen(search_uuid);
2559 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2561 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2565 * The UUIDs may legitimately be different lengths, if
2566 * the system was upgraded from an older version.
2568 if (cmplen != searchlen)
2571 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2575 * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2576 * returned in param->fp_obd_index
2578 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2580 struct obd_uuid obd_uuid;
2587 if (param->fp_got_uuids)
2590 /* Get the lov/lmv name */
2591 if (param->fp_get_lmv)
2592 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2594 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2596 if (rc != -ENOTTY) {
2597 llapi_error(LLAPI_MSG_ERROR, rc,
2598 "error: can't get %s name: %s",
2599 param->fp_get_lmv ? "lmv" : "lov",
2607 param->fp_got_uuids = 1;
2609 /* Now get the ost uuids */
2610 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2611 obd_uuid.uuid, FILTER_BY_EXACT,
2612 "target_obd", ¶m_data);
2616 fp = fopen(param_data.gl_pathv[0], "r");
2619 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2620 param_data.gl_pathv[0]);
2624 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2625 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2626 param->fp_get_lmv ? "MDTS" : "OBDS");
2628 snprintf(format, sizeof(format),
2629 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2630 while (fgets(buf, sizeof(buf), fp) != NULL) {
2633 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2636 if (param->fp_obd_uuid) {
2637 if (llapi_uuid_match(obd_uuid.uuid,
2638 param->fp_obd_uuid->uuid)) {
2639 param->fp_obd_index = index;
2642 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2643 /* Print everything */
2644 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2647 param->fp_obds_printed = 1;
2651 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2652 llapi_err_noerrno(LLAPI_MSG_ERROR,
2653 "error: %s: unknown obduuid: %s",
2654 __func__, param->fp_obd_uuid->uuid);
2658 cfs_free_param_data(¶m_data);
2663 * In this case, param->fp_obd_uuid will be an array of obduuids and
2664 * obd index for all these obduuids will be returned in
2665 * param->fp_obd_indexes
2667 static int setup_indexes(int d, char *path, struct obd_uuid *obduuids,
2668 int num_obds, int **obdindexes, int *obdindex,
2671 int ret, obdcount, obd_valid = 0, obdnum;
2673 struct obd_uuid *uuids = NULL;
2677 if (type == LOV_TYPE)
2678 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2680 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2684 obdcount = atoi(buf);
2685 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2690 ret = llapi_get_target_uuids(d, uuids, &obdcount, type);
2692 if (ret == -EOVERFLOW) {
2693 struct obd_uuid *uuids_temp;
2695 uuids_temp = realloc(uuids, obdcount *
2696 sizeof(struct obd_uuid));
2697 if (uuids_temp != NULL) {
2699 goto retry_get_uuids;
2704 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2708 indexes = malloc(num_obds * sizeof(*obdindex));
2709 if (indexes == NULL) {
2714 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2717 /* The user may have specified a simple index */
2718 i = strtol(obduuids[obdnum].uuid, &end, 0);
2719 if (end && *end == '\0' && i < obdcount) {
2720 indexes[obdnum] = i;
2723 for (i = 0; i < obdcount; i++) {
2724 if (llapi_uuid_match(uuids[i].uuid,
2725 obduuids[obdnum].uuid)) {
2726 indexes[obdnum] = i;
2732 if (i >= obdcount) {
2733 indexes[obdnum] = OBD_NOT_FOUND;
2734 llapi_err_noerrno(LLAPI_MSG_ERROR,
2735 "invalid obduuid '%s'",
2736 obduuids[obdnum].uuid);
2742 *obdindex = OBD_NOT_FOUND;
2744 *obdindex = obd_valid;
2746 *obdindexes = indexes;
2754 static int setup_target_indexes(int d, char *path, struct find_param *param)
2758 if (param->fp_mdt_uuid) {
2759 ret = setup_indexes(d, path, param->fp_mdt_uuid,
2761 ¶m->fp_mdt_indexes,
2762 ¶m->fp_mdt_index, LMV_TYPE);
2767 if (param->fp_obd_uuid) {
2768 ret = setup_indexes(d, path, param->fp_obd_uuid,
2770 ¶m->fp_obd_indexes,
2771 ¶m->fp_obd_index, LOV_TYPE);
2776 param->fp_got_uuids = 1;
2781 int llapi_ostlist(char *path, struct find_param *param)
2786 fd = open(path, O_RDONLY | O_DIRECTORY);
2790 ret = setup_obd_uuid(fd, path, param);
2797 * Tries to determine the default stripe attributes for a given filesystem. The
2798 * filesystem to check should be specified by fsname, or will be determined
2801 static int sattr_get_defaults(const char *const fsname,
2802 unsigned int *scount,
2803 unsigned int *ssize,
2804 unsigned int *soffset)
2810 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2811 "stripecount", val, sizeof(val));
2814 *scount = atoi(val);
2818 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2819 "stripesize", val, sizeof(val));
2826 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2827 "stripeoffset", val, sizeof(val));
2830 *soffset = atoi(val);
2837 * Tries to gather the default stripe attributes for a given filesystem. If
2838 * the attributes can be determined, they are cached for easy retreival the
2839 * next time they are needed. Only a single filesystem's attributes are
2842 int sattr_cache_get_defaults(const char *const fsname,
2843 const char *const pathname, unsigned int *scount,
2844 unsigned int *ssize, unsigned int *soffset)
2847 char fsname[PATH_MAX + 1];
2848 unsigned int stripecount;
2849 unsigned int stripesize;
2850 unsigned int stripeoffset;
2856 char fsname_buf[PATH_MAX + 1];
2857 unsigned int tmp[3];
2859 if (fsname == NULL) {
2860 rc = llapi_search_fsname(pathname, fsname_buf);
2864 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2867 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2869 * Ensure all 3 sattrs (count, size, and offset) are
2870 * successfully retrieved and stored in tmp before writing to
2873 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2877 cache.stripecount = tmp[0];
2878 cache.stripesize = tmp[1];
2879 cache.stripeoffset = tmp[2];
2880 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2884 *scount = cache.stripecount;
2886 *ssize = cache.stripesize;
2888 *soffset = cache.stripeoffset;
2893 static char *layout2name(__u32 layout_pattern)
2895 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2897 else if (layout_pattern == LOV_PATTERN_MDT)
2899 else if (layout_pattern == LOV_PATTERN_RAID0)
2901 else if (layout_pattern ==
2902 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2903 return "raid0,overstriped";
2908 enum lov_dump_flags {
2909 LDF_IS_DIR = 0x0001,
2910 LDF_IS_RAW = 0x0002,
2911 LDF_INDENT = 0x0004,
2912 LDF_SKIP_OBJS = 0x0008,
2914 LDF_EXTENSION = 0x0020,
2917 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2918 struct lov_user_ost_data_v1 *objects,
2919 enum llapi_layout_verbose verbose,
2920 int depth, char *pool_name,
2921 enum lov_dump_flags flags)
2923 bool is_dir = flags & LDF_IS_DIR;
2924 bool is_raw = flags & LDF_IS_RAW;
2925 bool indent = flags & LDF_INDENT;
2926 bool yaml = flags & LDF_YAML;
2927 bool skip_objs = flags & LDF_SKIP_OBJS;
2928 bool extension = flags & LDF_EXTENSION;
2929 char *prefix = is_dir ? "" : "lmm_";
2930 char *separator = "";
2931 char *space = indent ? " " : "";
2934 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2935 lmm_oi_set_seq(&lum->lmm_oi, 0);
2936 if (!indent && (verbose & VERBOSE_DETAIL))
2937 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2940 if (!yaml && !indent && depth && path &&
2941 ((verbose != VERBOSE_OBJID) || !is_dir))
2942 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2944 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2945 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2946 space, prefix, lum->lmm_magic);
2947 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2949 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2950 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2952 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2955 if (verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) {
2960 if (verbose & ~VERBOSE_DFID)
2961 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2965 struct lu_fid dir_fid;
2967 rc = llapi_path2fid(path, &dir_fid);
2969 llapi_error(LLAPI_MSG_ERROR, rc,
2970 "Cannot determine directory fid.");
2972 seq = dir_fid.f_seq;
2973 oid = dir_fid.f_oid;
2974 ver = dir_fid.f_ver;
2977 * This needs a bit of hand-holding since old 1.x
2978 * lmm_oi have { oi.oi_id = mds_inum, oi.oi_seq = 0 }
2979 * and 2.x lmm_oi have { oi.oi_id = mds_oid,
2980 * oi.oi_seq = mds_seq } instead of a real FID.
2981 * Ideally the 2.x code would have stored this like a
2982 * FID with { oi_id = mds_seq, oi_seq = mds_oid } so
2983 * the ostid union lu_fid { f_seq = mds_seq,
2984 * f_oid = mds_oid } worked properly (especially since
2985 * IGIF FIDs use mds_inum as the FID SEQ), but
2986 * unfortunately that didn't happen.
2988 * Print it to look like an IGIF FID, even though the
2989 * fields are reversed on disk, so that it makes sense
2992 * Don't use ostid_id() and ostid_seq(), since they
2993 * assume the oi_fid fields are in the right order.
2994 * This is why there are separate lmm_oi_seq() and
2995 * lmm_oi_id() routines for this.
2997 * For newer layout types hopefully this will be a
3000 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
3001 lmm_oi_id(&lum->lmm_oi) :
3002 lmm_oi_seq(&lum->lmm_oi);
3003 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
3004 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
3005 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
3009 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
3010 (unsigned long long)seq, oid, ver);
3012 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
3013 (unsigned long long)seq, oid, ver);
3016 if (verbose & VERBOSE_STRIPE_COUNT) {
3017 if (verbose & ~VERBOSE_STRIPE_COUNT)
3018 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
3021 if (!is_raw && lum->lmm_stripe_count == 0 &&
3022 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
3023 unsigned int scount;
3025 rc = sattr_cache_get_defaults(NULL, path,
3029 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3032 llapi_error(LLAPI_MSG_ERROR, rc,
3033 "Cannot determine default stripe count.");
3035 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3037 (__s16)lum->lmm_stripe_count);
3040 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
3042 (__s16)lum->lmm_stripe_count);
3044 if (!yaml && is_dir)
3050 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
3051 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
3052 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3053 if (verbose & ~VERBOSE_EXT_SIZE && extension)
3054 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
3056 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
3057 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
3059 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
3062 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
3065 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
3067 llapi_error(LLAPI_MSG_ERROR, rc,
3068 "Cannot determine default stripe size.");
3070 /* Extension size is in KiB */
3071 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3073 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
3074 (unsigned long long)lum->lmm_stripe_size);
3076 if (!yaml && is_dir)
3082 if ((verbose & VERBOSE_PATTERN)) {
3083 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3084 if (verbose & ~VERBOSE_PATTERN)
3085 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
3087 if (lov_pattern_supported(lum->lmm_pattern))
3088 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3089 layout2name(lum->lmm_pattern));
3091 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
3092 separator = (!yaml && is_dir) ? " " : "\n";
3095 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
3096 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3097 if (verbose & ~VERBOSE_GENERATION)
3098 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
3100 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3101 skip_objs ? 0 : (int)lum->lmm_layout_gen);
3105 if (verbose & VERBOSE_STRIPE_OFFSET) {
3106 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3107 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3108 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
3110 if (is_dir || skip_objs)
3111 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3112 lum->lmm_stripe_offset ==
3113 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
3114 lum->lmm_stripe_offset);
3115 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
3116 llapi_printf(LLAPI_MSG_NORMAL, "0");
3118 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3119 objects[0].l_ost_idx);
3120 if (!yaml && is_dir)
3126 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
3127 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3128 if (verbose & ~VERBOSE_POOL)
3129 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
3131 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
3132 if (!yaml && is_dir)
3138 if (strlen(separator) != 0)
3139 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3142 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
3143 struct lov_user_ost_data_v1 *objects,
3144 char *path, int obdindex, int depth,
3145 enum llapi_layout_verbose verbose,
3146 enum lov_dump_flags flags)
3148 bool is_dir = flags & LDF_IS_DIR;
3149 bool indent = flags & LDF_INDENT;
3150 bool skip_objs = flags & LDF_SKIP_OBJS;
3151 bool yaml = flags & LDF_YAML;
3152 bool obdstripe = obdindex == OBD_NOT_FOUND;
3155 if (!obdstripe && !skip_objs) {
3156 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
3157 if (obdindex == objects[i].l_ost_idx) {
3167 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
3170 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
3171 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
3172 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
3173 char *space = " - ";
3176 llapi_printf(LLAPI_MSG_NORMAL,
3177 "%6slmm_objects:\n", " ");
3179 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
3181 llapi_printf(LLAPI_MSG_NORMAL,
3182 "\tobdidx\t\t objid\t\t objid\t\t group\n");
3184 for (i = 0; i < lum->lmm_stripe_count; i++) {
3185 int idx = objects[i].l_ost_idx;
3186 long long oid = ostid_id(&objects[i].l_ost_oi);
3187 long long gr = ostid_seq(&objects[i].l_ost_oi);
3189 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
3193 struct lu_fid fid = { 0 };
3195 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3196 llapi_printf(LLAPI_MSG_NORMAL,
3197 "%sl_ost_idx: %d\n", space, idx);
3198 llapi_printf(LLAPI_MSG_NORMAL,
3199 "%8sl_fid: "DFID_NOBRACE"\n",
3201 } else if (indent) {
3202 struct lu_fid fid = { 0 };
3204 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3205 llapi_printf(LLAPI_MSG_NORMAL,
3206 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
3207 space, i, idx, PFID(&fid));
3211 sprintf(fmt, "%s%s%s\n",
3212 "\t%6u\t%14llu\t%#13llx\t",
3213 (fid_seq_is_rsvd(gr) ||
3214 fid_seq_is_mdt0(gr)) ?
3215 "%14llu" : "%#14llx", "%s");
3216 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
3218 obdindex == idx ? " *" : "");
3222 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3225 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3226 char *path, int obdindex, int depth,
3227 enum llapi_layout_verbose verbose,
3228 enum lov_dump_flags flags)
3230 struct lmv_user_mds_data *objects = lum->lum_objects;
3231 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3232 char *separator = "";
3233 bool yaml = flags & LDF_YAML;
3234 bool obdstripe = false;
3237 if (obdindex != OBD_NOT_FOUND) {
3238 if (lum->lum_stripe_count == 0) {
3239 if (obdindex == lum->lum_stripe_offset)
3242 for (i = 0; i < lum->lum_stripe_count; i++) {
3243 if (obdindex == objects[i].lum_mds) {
3244 llapi_printf(LLAPI_MSG_NORMAL,
3259 /* show all information default */
3261 if (lum->lum_magic == LMV_USER_MAGIC)
3262 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3263 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3265 verbose = VERBOSE_OBJID;
3268 if (depth && path && ((verbose != VERBOSE_OBJID)))
3269 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3271 if (verbose & VERBOSE_STRIPE_COUNT) {
3272 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3273 if (verbose & ~VERBOSE_STRIPE_COUNT)
3274 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3275 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3276 (int)lum->lum_stripe_count);
3277 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3283 if (verbose & VERBOSE_STRIPE_OFFSET) {
3284 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3285 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3286 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3287 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3288 (int)lum->lum_stripe_offset);
3289 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3295 if (verbose & VERBOSE_HASH_TYPE) {
3296 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3297 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3299 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3300 if (verbose & ~VERBOSE_HASH_TYPE)
3301 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3302 if (type < LMV_HASH_TYPE_MAX)
3303 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3304 mdt_hash_name[type]);
3306 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3308 if (flags & LMV_HASH_FLAG_MIGRATION)
3309 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3310 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3311 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3312 if (flags & LMV_HASH_FLAG_LOST_LMV)
3313 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3315 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3321 if ((verbose & VERBOSE_INHERIT) && lum->lum_magic == LMV_USER_MAGIC) {
3322 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3323 if (verbose & ~VERBOSE_INHERIT)
3324 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit: ");
3325 if (lum->lum_max_inherit == LMV_INHERIT_UNLIMITED)
3326 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3327 else if (lum->lum_max_inherit == LMV_INHERIT_NONE)
3328 llapi_printf(LLAPI_MSG_NORMAL, "0");
3330 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3331 lum->lum_max_inherit);
3332 if (verbose & VERBOSE_INHERIT && !yaml)
3338 if ((verbose & VERBOSE_INHERIT_RR) &&
3339 lum->lum_magic == LMV_USER_MAGIC &&
3340 lum->lum_stripe_offset == LMV_OFFSET_DEFAULT) {
3341 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3342 if (verbose & ~VERBOSE_INHERIT_RR)
3343 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit_rr: ");
3344 if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_UNLIMITED)
3345 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3346 else if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_NONE)
3347 llapi_printf(LLAPI_MSG_NORMAL, "0");
3349 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3350 lum->lum_max_inherit_rr);
3351 if (verbose & VERBOSE_INHERIT_RR && !yaml)
3359 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3360 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3361 if (lum->lum_stripe_count > 0)
3362 llapi_printf(LLAPI_MSG_NORMAL,
3363 "mdtidx\t\t FID[seq:oid:ver]\n");
3364 for (i = 0; i < lum->lum_stripe_count; i++) {
3365 int idx = objects[i].lum_mds;
3366 struct lu_fid *fid = &objects[i].lum_fid;
3368 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3369 llapi_printf(LLAPI_MSG_NORMAL,
3370 "%6u\t\t "DFID"\t\t%s\n",
3372 obdindex == idx ? " *" : "");
3376 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3377 pool_name[0] != '\0') {
3378 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3379 if (verbose & ~VERBOSE_POOL)
3380 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3382 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3386 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3387 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3390 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3391 enum lov_dump_flags flags)
3393 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3394 int depth = param->fp_max_depth;
3395 enum llapi_layout_verbose verbose = param->fp_verbose;
3396 bool yaml = flags & LDF_YAML;
3398 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3399 !(flags & LDF_IS_DIR)) && !yaml)
3400 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3402 if (verbose & VERBOSE_DETAIL) {
3403 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3404 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3405 " ", comp_v1->lcm_magic);
3406 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3407 " ", comp_v1->lcm_size);
3408 if (flags & LDF_IS_DIR)
3409 llapi_printf(LLAPI_MSG_NORMAL,
3410 "%2slcm_flags: %s\n", " ",
3411 comp_v1->lcm_mirror_count > 0 ?
3414 llapi_printf(LLAPI_MSG_NORMAL,
3415 "%2slcm_flags: %s\n", " ",
3416 llapi_layout_flags_string(comp_v1->lcm_flags));
3419 if (verbose & VERBOSE_GENERATION) {
3420 if (verbose & ~VERBOSE_GENERATION)
3421 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3423 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3426 if (verbose & VERBOSE_MIRROR_COUNT) {
3427 if (verbose & ~VERBOSE_MIRROR_COUNT)
3428 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3430 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3431 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3432 comp_v1->lcm_mirror_count + 1 : 1);
3435 if (verbose & VERBOSE_COMP_COUNT) {
3436 if (verbose & ~VERBOSE_COMP_COUNT)
3437 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3439 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3440 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3441 comp_v1->lcm_entry_count : 0);
3444 if (verbose & VERBOSE_DETAIL && !yaml)
3445 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3448 static void lcme_flags2str(__u32 comp_flags)
3454 llapi_printf(LLAPI_MSG_NORMAL, "0");
3457 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3458 if (comp_flags & comp_flags_table[i].cfn_flag) {
3460 llapi_printf(LLAPI_MSG_NORMAL, ",");
3461 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3462 comp_flags_table[i].cfn_name);
3463 comp_flags &= ~comp_flags_table[i].cfn_flag;
3469 llapi_printf(LLAPI_MSG_NORMAL, ",");
3470 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3474 static void lov_dump_comp_v1_entry(struct find_param *param,
3475 enum lov_dump_flags flags, int index)
3477 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3478 struct lov_comp_md_entry_v1 *entry;
3479 char *separator = "";
3480 enum llapi_layout_verbose verbose = param->fp_verbose;
3481 bool yaml = flags & LDF_YAML;
3483 entry = &comp_v1->lcm_entries[index];
3486 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3488 if (verbose & VERBOSE_COMP_ID) {
3489 if (verbose & VERBOSE_DETAIL && !yaml)
3490 llapi_printf(LLAPI_MSG_NORMAL,
3491 "%slcme_id: ", " - ");
3492 else if (verbose & ~VERBOSE_COMP_ID)
3493 llapi_printf(LLAPI_MSG_NORMAL,
3494 "%4slcme_id: ", " ");
3495 if (entry->lcme_id != LCME_ID_INVAL)
3496 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3498 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3502 if (verbose & VERBOSE_MIRROR_ID) {
3503 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3504 if (verbose & ~VERBOSE_MIRROR_ID)
3505 llapi_printf(LLAPI_MSG_NORMAL,
3506 "%4slcme_mirror_id: ", " ");
3507 if (entry->lcme_id != LCME_ID_INVAL)
3508 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3509 mirror_id_of(entry->lcme_id));
3511 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3515 if (verbose & VERBOSE_COMP_FLAGS) {
3516 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3517 if (verbose & ~VERBOSE_COMP_FLAGS)
3518 llapi_printf(LLAPI_MSG_NORMAL,
3519 "%4slcme_flags: ", " ");
3520 lcme_flags2str(entry->lcme_flags);
3523 /* print snapshot timestamp if its a nosync comp */
3524 if ((verbose & VERBOSE_COMP_FLAGS) &&
3525 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3526 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3527 if (verbose & ~VERBOSE_COMP_FLAGS)
3528 llapi_printf(LLAPI_MSG_NORMAL,
3529 "%4slcme_timestamp: ", " ");
3531 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3532 (unsigned long long)entry->lcme_timestamp);
3534 time_t stamp = entry->lcme_timestamp;
3535 char *date_str = asctime(localtime(&stamp));
3537 date_str[strlen(date_str) - 1] = '\0';
3538 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3544 if (verbose & VERBOSE_COMP_START) {
3545 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3546 if (verbose & ~VERBOSE_COMP_START)
3547 llapi_printf(LLAPI_MSG_NORMAL,
3548 "%4slcme_extent.e_start: ", " ");
3549 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3550 (unsigned long long)entry->lcme_extent.e_start);
3554 if (verbose & VERBOSE_COMP_END) {
3555 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3556 if (verbose & ~VERBOSE_COMP_END)
3557 llapi_printf(LLAPI_MSG_NORMAL,
3558 "%4slcme_extent.e_end: ", " ");
3559 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3560 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3562 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3563 (unsigned long long)entry->lcme_extent.e_end);
3568 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3569 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3570 } else if (verbose & VERBOSE_DETAIL) {
3571 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3572 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3573 " ", entry->lcme_offset);
3574 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3575 " ", entry->lcme_size);
3576 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3578 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3583 * Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3584 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3586 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3587 * The table below gives the answers for the specified parameters (value and
3588 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3589 * --------------------------------------
3590 * 1 | file > limit; sign > 0 | -1 / -1 |
3591 * 2 | file = limit; sign > 0 | -1 / -1 |
3592 * 3 | file < limit; sign > 0 | ? / 1 |
3593 * 4 | file > limit; sign = 0 | -1 / -1 |
3594 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3595 * 6 | file < limit; sign = 0 | ? / -1 |
3596 * 7 | file > limit; sign < 0 | 1 / 1 |
3597 * 8 | file = limit; sign < 0 | ? / -1 |
3598 * 9 | file < limit; sign < 0 | ? / -1 |
3599 * --------------------------------------
3600 * Note: 5th actually means that the value is within the interval
3601 * (limit - margin, limit].
3603 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3604 int sign, int negopt, unsigned long long margin,
3610 /* Drop the fraction of margin (of days or size). */
3611 if (file + margin <= limit)
3613 } else if (sign == 0) {
3614 if (file <= limit && file + margin > limit)
3616 else if (file + margin <= limit)
3618 } else if (sign < 0) {
3625 return negopt ? ~ret + 1 : ret;
3628 static inline struct lov_user_md *
3629 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3631 return (struct lov_user_md *)((char *)comp_v1 +
3632 comp_v1->lcm_entries[ent_idx].lcme_offset);
3635 static inline struct lov_user_ost_data_v1 *
3636 lov_v1v3_objects(struct lov_user_md *v1)
3638 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3639 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3641 return v1->lmm_objects;
3645 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3647 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3648 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3649 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3651 pool_name[0] = '\0';
3655 print_last_init_comp(struct find_param *param)
3657 /* print all component info */
3658 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3661 /* print specific component info */
3662 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3663 param->fp_check_comp_start || param->fp_check_comp_end ||
3664 param->fp_check_mirror_id || param->fp_check_mirror_index)
3670 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3674 if (param->fp_comp_end == LUSTRE_EOF) {
3675 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3676 match = end == LUSTRE_EOF ? 1 : -1;
3677 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3678 match = end == LUSTRE_EOF ? -1 : 1;
3679 else /* at least EOF */
3681 if (param->fp_exclude_comp_end)
3684 unsigned long long margin;
3686 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3687 match = find_value_cmp(end, param->fp_comp_end,
3688 param->fp_comp_end_sign,
3689 param->fp_exclude_comp_end, margin, 0);
3696 * An example of "getstripe -v" for a two components PFL file:
3699 * lcm_magic: 0x0BD60BD0
3703 * lcm_entry_count: 2
3707 * lcme_extent.e_start: 0
3708 * lcme_extent.e_end: 1048576
3712 * lmm_magic: 0x0BD10BD0
3713 * lmm_seq: 0x200000401
3714 * lmm_object_id: 0x1
3715 * lmm_fid: [0x200000401:0x1:0x0]
3716 * lmm_stripe_count: 1
3717 * lmm_stripe_size: 1048576
3718 * lmm_pattern: raid0
3720 * lmm_stripe_offset: 0
3722 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3726 * lcme_extent.e_start: 1048576
3727 * lcme_extent.e_end: EOF
3731 * lmm_magic: 0x0BD10BD0
3732 * lmm_seq: 0x200000401
3733 * lmm_object_id: 0x1
3734 * lmm_fid: [0x200000401:0x1:0x0]
3735 * lmm_stripe_count: 2
3736 * lmm_stripe_size: 1048576
3737 * lmm_pattern: raid0
3739 * lmm_stripe_offset: 1
3741 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3742 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3744 static void lov_dump_comp_v1(struct find_param *param, char *path,
3745 enum lov_dump_flags flags)
3747 struct lov_comp_md_entry_v1 *entry;
3748 struct lov_user_ost_data_v1 *objects;
3749 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3750 struct lov_user_md_v1 *v1;
3751 char pool_name[LOV_MAXPOOLNAME + 1];
3752 int obdindex = param->fp_obd_index;
3753 int i, j, match, ext;
3754 bool obdstripe = false;
3755 __u16 mirror_index = 0;
3756 __u16 mirror_id = 0;
3758 if (obdindex != OBD_NOT_FOUND) {
3759 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3760 i < comp_v1->lcm_entry_count; i++) {
3761 if (!(comp_v1->lcm_entries[i].lcme_flags &
3765 v1 = lov_comp_entry(comp_v1, i);
3766 objects = lov_v1v3_objects(v1);
3768 for (j = 0; j < v1->lmm_stripe_count; j++) {
3769 if (obdindex == objects[j].l_ost_idx) {
3782 lov_dump_comp_v1_header(param, path, flags);
3784 flags |= LDF_INDENT;
3786 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3787 entry = &comp_v1->lcm_entries[i];
3789 if (param->fp_check_comp_flags) {
3790 if (((param->fp_comp_flags & entry->lcme_flags) !=
3791 param->fp_comp_flags) ||
3792 (param->fp_comp_neg_flags & entry->lcme_flags))
3796 if (param->fp_check_comp_id &&
3797 param->fp_comp_id != entry->lcme_id)
3800 if (param->fp_check_comp_start) {
3801 match = find_value_cmp(entry->lcme_extent.e_start,
3802 param->fp_comp_start,
3803 param->fp_comp_start_sign,
3805 param->fp_comp_start_units, 0);
3810 if (param->fp_check_comp_end) {
3811 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3817 if (param->fp_check_mirror_index) {
3818 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3820 mirror_id = mirror_id_of(entry->lcme_id);
3823 match = find_value_cmp(mirror_index,
3824 param->fp_mirror_index,
3825 param->fp_mirror_index_sign,
3826 param->fp_exclude_mirror_index,
3830 } else if (param->fp_check_mirror_id) {
3831 if (mirror_id != mirror_id_of(entry->lcme_id))
3832 mirror_id = mirror_id_of(entry->lcme_id);
3834 match = find_value_cmp(mirror_id,
3835 param->fp_mirror_id,
3836 param->fp_mirror_id_sign,
3837 param->fp_exclude_mirror_id,
3843 if (print_last_init_comp(param)) {
3845 * if part of stripe info is needed, we'd print only
3846 * the last instantiated component info.
3848 if (entry->lcme_flags & LCME_FL_INIT)
3851 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3852 if (entry->lcme_flags & LCME_FL_EXTENSION)
3853 /* moved back below */
3861 if (entry->lcme_flags & LCME_FL_INIT) {
3862 if (obdindex != OBD_NOT_FOUND) {
3863 flags |= LDF_SKIP_OBJS;
3864 v1 = lov_comp_entry(comp_v1, i);
3865 objects = lov_v1v3_objects(v1);
3867 for (j = 0; j < v1->lmm_stripe_count; j++) {
3868 if (obdindex == objects[j].l_ost_idx) {
3869 flags &= ~LDF_SKIP_OBJS;
3874 flags &= ~LDF_SKIP_OBJS;
3877 flags |= LDF_SKIP_OBJS;
3880 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3882 lov_dump_comp_v1_entry(param, flags, i);
3884 v1 = lov_comp_entry(comp_v1, i);
3885 objects = lov_v1v3_objects(v1);
3886 lov_v1v3_pool_name(v1, pool_name);
3888 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3889 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3890 param->fp_max_depth, param->fp_verbose,
3893 if (print_last_init_comp(param)) {
3895 * directory layout contains only layout template, print the
3899 i = comp_v1->lcm_entry_count - 1;
3902 flags &= ~LDF_SKIP_OBJS;
3904 lov_dump_comp_v1_entry(param, flags, i);
3906 v1 = lov_comp_entry(comp_v1, i);
3907 objects = lov_v1v3_objects(v1);
3908 lov_v1v3_pool_name(v1, pool_name);
3910 entry = &comp_v1->lcm_entries[i];
3911 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3912 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3913 param->fp_max_depth, param->fp_verbose,
3918 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3919 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3922 static inline bool has_any_comp_options(struct find_param *param)
3924 enum llapi_layout_verbose verbose = param->fp_verbose;
3926 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3927 param->fp_check_comp_start || param->fp_check_comp_end ||
3928 param->fp_check_comp_flags)
3931 /* show full layout information, not component specific */
3932 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3935 return verbose & VERBOSE_COMP_OPTS;
3938 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3941 struct lov_user_md *lum = &orig->lmd_lmm;
3942 struct lov_user_mds_data *new;
3943 struct lov_comp_md_v1 *comp_v1;
3944 struct lov_comp_md_entry_v1 *ent;
3945 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3946 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3949 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3951 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3955 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3956 + sizeof(new->lmd_lmmsize));
3958 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3959 comp_v1->lcm_magic = lum->lmm_magic;
3960 comp_v1->lcm_size = lum_off + lum_size;
3961 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3962 comp_v1->lcm_flags = 0;
3963 comp_v1->lcm_entry_count = 1;
3965 ent = &comp_v1->lcm_entries[0];
3967 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3968 ent->lcme_extent.e_start = 0;
3969 ent->lcme_extent.e_end = LUSTRE_EOF;
3970 ent->lcme_offset = lum_off;
3971 ent->lcme_size = lum_size;
3973 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3978 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3979 enum lov_dump_flags flags)
3981 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3983 if (has_any_comp_options(param)) {
3984 struct lov_user_mds_data *new_lmd, *orig_lmd;
3986 orig_lmd = param->fp_lmd;
3987 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3988 if (new_lmd != NULL) {
3989 param->fp_lmd = new_lmd;
3990 lov_dump_comp_v1(param, path, flags);
3991 param->fp_lmd = orig_lmd;
3997 if (magic == LOV_USER_MAGIC_V1) {
3998 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3999 param->fp_lmd->lmd_lmm.lmm_objects,
4000 path, param->fp_obd_index,
4001 param->fp_max_depth, param->fp_verbose,
4004 char pool_name[LOV_MAXPOOLNAME + 1];
4005 struct lov_user_ost_data_v1 *objects;
4006 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
4008 snprintf(pool_name, sizeof(pool_name), "%s",
4009 lmmv3->lmm_pool_name);
4010 objects = lmmv3->lmm_objects;
4011 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
4012 objects, path, param->fp_obd_index,
4013 param->fp_max_depth, param->fp_verbose,
4018 static uint32_t check_foreign_type(uint32_t foreign_type)
4022 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
4023 if (lu_foreign_types[i].lft_name == NULL)
4025 if (foreign_type == lu_foreign_types[i].lft_type)
4029 return LU_FOREIGN_TYPE_UNKNOWN;
4032 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
4033 enum lov_dump_flags flags)
4035 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
4036 bool yaml = flags & LDF_YAML;
4038 if (!yaml && param->fp_depth && path)
4039 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4041 if (param->fp_verbose & VERBOSE_DETAIL) {
4042 uint32_t type = check_foreign_type(lfm->lfm_type);
4044 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
4046 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
4048 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
4050 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4051 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4052 lu_foreign_types[type].lft_name);
4054 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4056 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
4059 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
4060 lfm->lfm_length, lfm->lfm_value);
4061 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4064 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
4065 enum lov_dump_flags flags)
4067 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
4068 bool yaml = flags & LDF_YAML;
4070 if (!yaml && param->fp_depth && path)
4071 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4073 if (param->fp_verbose & VERBOSE_DETAIL) {
4074 uint32_t type = check_foreign_type(lfm->lfm_type);
4076 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
4078 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
4080 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
4082 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4083 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4084 lu_foreign_types[type].lft_name);
4086 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4088 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
4091 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
4092 lfm->lfm_length, lfm->lfm_value);
4093 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4096 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
4097 enum lov_dump_flags flags)
4101 if (param->fp_get_lmv || param->fp_get_default_lmv)
4102 magic = (__u32)param->fp_lmv_md->lum_magic;
4104 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
4107 flags |= LDF_IS_RAW;
4112 case LOV_USER_MAGIC_V1:
4113 case LOV_USER_MAGIC_V3:
4114 case LOV_USER_MAGIC_SPECIFIC:
4115 lov_dump_plain_user_lmm(param, path, flags);
4117 case LOV_USER_MAGIC_FOREIGN:
4118 lov_dump_foreign_lmm(param, path, flags);
4121 case LMV_USER_MAGIC: {
4122 char pool_name[LOV_MAXPOOLNAME + 1];
4123 struct lmv_user_md *lum;
4125 lum = (struct lmv_user_md *)param->fp_lmv_md;
4126 snprintf(pool_name, sizeof(pool_name), "%s",
4127 lum->lum_pool_name);
4128 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
4129 param->fp_max_depth, param->fp_verbose,
4133 case LOV_USER_MAGIC_COMP_V1:
4134 lov_dump_comp_v1(param, path, flags);
4136 case LMV_MAGIC_FOREIGN:
4137 lmv_dump_foreign_lmm(param, path, flags);
4140 llapi_printf(LLAPI_MSG_NORMAL,
4141 "unknown lmm_magic: %#x (expecting one of %#x %#x %#x %#x)\n",
4142 *(__u32 *)¶m->fp_lmd->lmd_lmm,
4143 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
4144 LMV_USER_MAGIC, LMV_MAGIC_V1);
4149 static int llapi_file_get_stripe1(const char *path, struct lov_user_md *lum)
4155 fname = strrchr(path, '/');
4157 /* It should be a file (or other non-directory) */
4158 if (fname == NULL) {
4159 dname = (char *)malloc(2);
4163 fname = (char *)path;
4165 dname = (char *)malloc(fname - path + 1);
4168 strncpy(dname, path, fname - path);
4169 dname[fname - path] = '\0';
4173 fd = open(dname, O_RDONLY | O_NONBLOCK);
4179 strcpy((char *)lum, fname);
4180 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
4183 if (close(fd) == -1 && rc == 0)
4191 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
4193 char *canon_path = NULL;
4196 rc = llapi_file_get_stripe1(path, lum);
4197 if (!(rc == -ENOTTY || rc == -ENODATA))
4200 /* Handle failure due to symlinks by dereferencing path manually. */
4201 canon_path = canonicalize_file_name(path);
4202 if (canon_path == NULL)
4203 goto out; /* Keep original rc. */
4205 rc2 = llapi_file_get_stripe1(canon_path, lum);
4207 goto out; /* Keep original rc. */
4216 int llapi_file_lookup(int dirfd, const char *name)
4218 struct obd_ioctl_data data = { 0 };
4223 if (dirfd < 0 || name == NULL)
4226 data.ioc_version = OBD_IOCTL_VERSION;
4227 data.ioc_len = sizeof(data);
4228 data.ioc_inlbuf1 = (char *)name;
4229 data.ioc_inllen1 = strlen(name) + 1;
4231 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
4233 llapi_error(LLAPI_MSG_ERROR, rc,
4234 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
4239 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
4246 * Check if the file time matches all the given criteria (e.g. --atime +/-N).
4247 * Return -1 or 1 if file timestamp does not or does match the given criteria
4248 * correspondingly. Return 0 if the MDS time is being checked and there are
4249 * attributes on OSTs and it is not yet clear if the timespamp matches.
4251 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
4252 * updated timestamps.
4254 static int find_time_check(struct find_param *param, int mds)
4256 struct lov_user_mds_data *lmd = param->fp_lmd;
4260 /* Check if file is accepted. */
4261 if (param->fp_atime) {
4262 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4263 param->fp_atime, param->fp_asign,
4264 param->fp_exclude_atime,
4265 param->fp_time_margin, mds);
4271 if (param->fp_mtime) {
4272 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4273 param->fp_mtime, param->fp_msign,
4274 param->fp_exclude_mtime,
4275 param->fp_time_margin, mds);
4280 * If the previous check matches, but this one is not yet clear,
4281 * we should return 0 to do an RPC on OSTs.
4287 if (param->fp_ctime) {
4288 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4289 param->fp_ctime, param->fp_csign,
4290 param->fp_exclude_ctime,
4291 param->fp_time_margin, mds);
4296 * If the previous check matches, but this one is not yet clear,
4297 * we should return 0 to do an RPC on OSTs.
4306 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4308 struct lov_user_mds_data *lmd = param->fp_lmd;
4313 for (i = 0; i < 2; i++) {
4314 /* Check if file is accepted. */
4315 if (param->fp_newery[NEWERXY_ATIME][i]) {
4316 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4317 param->fp_newery[NEWERXY_ATIME][i],
4324 if (param->fp_newery[NEWERXY_MTIME][i]) {
4325 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4326 param->fp_newery[NEWERXY_MTIME][i],
4332 * If the previous check matches, but this one is not
4333 * yet clear, we should return 0 to do an RPC on OSTs.
4339 if (param->fp_newery[NEWERXY_CTIME][i]) {
4340 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4341 param->fp_newery[NEWERXY_CTIME][i],
4347 * If the previous check matches, but this one is not
4348 * yet clear, we should return 0 to do an RPC on OSTs.
4355 * File birth time (btime) can get from MDT directly.
4356 * if @from_mdt is true, it means the input file attributs are
4357 * obtained directly from MDT.
4358 * Thus, if @from_mdt is false, we should skip the following
4364 if (param->fp_newery[NEWERXY_BTIME][i]) {
4365 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4368 rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4369 param->fp_newery[NEWERXY_BTIME][i],
4380 * Check whether the stripes matches the indexes user provided
4384 static int check_obd_match(struct find_param *param)
4386 struct lov_user_ost_data_v1 *objects;
4387 struct lov_comp_md_v1 *comp_v1 = NULL;
4388 struct lov_user_mds_data *lmd = param->fp_lmd;
4389 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4390 int i, j, k, count = 1;
4392 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4395 if (!S_ISREG(lmd->lmd_stx.stx_mode))
4398 /* exclude foreign */
4399 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4400 return param->fp_exclude_obd;
4403 * Only those files should be accepted, which have a
4404 * stripe on the specified OST.
4406 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4407 comp_v1 = (struct lov_comp_md_v1 *)v1;
4408 count = comp_v1->lcm_entry_count;
4411 for (i = 0; i < count; i++) {
4413 v1 = lov_comp_entry(comp_v1, i);
4415 objects = lov_v1v3_objects(v1);
4417 for (j = 0; j < v1->lmm_stripe_count; j++) {
4418 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4421 for (k = 0; k < param->fp_num_obds; k++) {
4422 if (param->fp_obd_indexes[k] ==
4423 objects[j].l_ost_idx)
4424 return !param->fp_exclude_obd;
4429 return param->fp_exclude_obd;
4432 static int check_mdt_match(struct find_param *param)
4436 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4439 /* FIXME: For striped dir, we should get stripe information and check */
4440 for (i = 0; i < param->fp_num_mdts; i++) {
4441 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4442 return !param->fp_exclude_mdt;
4445 if (param->fp_exclude_mdt)
4452 * Check whether the obd is active or not, if it is
4453 * not active, just print the object affected by this
4456 static void print_failed_tgt(struct find_param *param, char *path, int type)
4458 struct obd_statfs stat_buf;
4459 struct obd_uuid uuid_buf;
4460 int tgt_nr, i, *indexes;
4463 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV) {
4464 llapi_error(LLAPI_MSG_NORMAL, ret, "%s: wrong statfs type(%d)",
4469 tgt_nr = (type == LL_STATFS_LOV) ? param->fp_obd_index :
4470 param->fp_mdt_index;
4471 indexes = (type == LL_STATFS_LOV) ? param->fp_obd_indexes :
4472 param->fp_mdt_indexes;
4474 for (i = 0; i < tgt_nr; i++) {
4475 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4476 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4478 ret = llapi_obd_statfs(path, type, indexes[i], &stat_buf,
4481 llapi_error(LLAPI_MSG_NORMAL, ret,
4482 "%s: obd_uuid: %s failed",
4483 __func__, param->fp_obd_uuid->uuid);
4487 static int find_check_stripe_size(struct find_param *param)
4489 struct lov_comp_md_v1 *comp_v1 = NULL;
4490 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4491 __u32 stripe_size = 0;
4492 int ret, i, count = 1;
4494 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4495 return param->fp_exclude_stripe_size ? 1 : -1;
4497 ret = param->fp_exclude_stripe_size ? 1 : -1;
4498 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4499 comp_v1 = (struct lov_comp_md_v1 *)v1;
4500 count = comp_v1->lcm_entry_count;
4503 for (i = 0; i < count; i++) {
4504 struct lov_comp_md_entry_v1 *ent;
4507 v1 = lov_comp_entry(comp_v1, i);
4509 ent = &comp_v1->lcm_entries[i];
4510 if (ent->lcme_flags & LCME_FL_EXTENSION)
4512 if (!(ent->lcme_flags & LCME_FL_INIT))
4515 stripe_size = v1->lmm_stripe_size;
4518 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4519 param->fp_stripe_size_sign,
4520 param->fp_exclude_stripe_size,
4521 param->fp_stripe_size_units, 0);
4526 static int find_check_ext_size(struct find_param *param)
4528 struct lov_comp_md_v1 *comp_v1;
4529 struct lov_user_md_v1 *v1;
4532 ret = param->fp_exclude_ext_size ? 1 : -1;
4533 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4534 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4537 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4538 struct lov_comp_md_entry_v1 *ent;
4540 v1 = lov_comp_entry(comp_v1, i);
4542 ent = &comp_v1->lcm_entries[i];
4543 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4546 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4547 param->fp_ext_size_sign,
4548 param->fp_exclude_ext_size,
4549 param->fp_ext_size_units, 0);
4550 /* If any ext_size matches */
4558 static __u32 find_get_stripe_count(struct find_param *param)
4560 struct lov_comp_md_v1 *comp_v1 = NULL;
4561 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4563 __u32 stripe_count = 0;
4565 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4568 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4569 comp_v1 = (struct lov_comp_md_v1 *)v1;
4570 count = comp_v1->lcm_entry_count;
4573 for (i = 0; i < count; i++) {
4575 struct lov_comp_md_entry_v1 *ent;
4577 v1 = lov_comp_entry(comp_v1, i);
4579 ent = &comp_v1->lcm_entries[i];
4580 if (!(ent->lcme_flags & LCME_FL_INIT))
4583 if (ent->lcme_flags & LCME_FL_EXTENSION)
4586 stripe_count = v1->lmm_stripe_count;
4589 return stripe_count;
4592 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4594 static int find_check_layout(struct find_param *param)
4596 struct lov_comp_md_v1 *comp_v1 = NULL;
4597 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4599 bool found = false, valid = false;
4601 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4602 comp_v1 = (struct lov_comp_md_v1 *)v1;
4603 count = comp_v1->lcm_entry_count;
4606 for (i = 0; i < count; i++) {
4608 v1 = lov_comp_entry(comp_v1, i);
4610 /* foreign file have a special magic but no pattern field */
4611 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4614 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4618 if (v1->lmm_pattern & param->fp_layout) {
4627 if ((found && !param->fp_exclude_layout) ||
4628 (!found && param->fp_exclude_layout))
4635 * if no type specified, check/exclude all foreign
4636 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4638 static int find_check_foreign(struct find_param *param)
4640 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4641 struct lov_foreign_md *lfm;
4643 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4644 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4645 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4646 return param->fp_exclude_foreign ? 1 : -1;
4650 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4651 lfm->lfm_type == param->fp_foreign_type)
4652 return param->fp_exclude_foreign ? -1 : 1;
4653 return param->fp_exclude_foreign ? 1 : -1;
4656 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4657 struct lmv_foreign_md *lfm;
4659 lfm = (void *)param->fp_lmv_md;
4660 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
4661 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4662 return param->fp_exclude_foreign ? 1 : -1;
4666 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4667 lfm->lfm_type == param->fp_foreign_type)
4668 return param->fp_exclude_foreign ? -1 : 1;
4669 return param->fp_exclude_foreign ? 1 : -1;
4674 static int find_check_pool(struct find_param *param)
4676 struct lov_comp_md_v1 *comp_v1 = NULL;
4677 struct lov_user_md_v3 *v3 = (void *)¶m->fp_lmd->lmd_lmm;
4681 if (v3->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4682 comp_v1 = (struct lov_comp_md_v1 *)v3;
4683 count = comp_v1->lcm_entry_count;
4684 /* empty requested pool is taken as no pool search */
4685 if (count == 0 && param->fp_poolname[0] == '\0') {
4691 for (i = 0; i < count; i++) {
4692 if (comp_v1 != NULL) {
4693 if (!(comp_v1->lcm_entries[i].lcme_flags &
4697 v3 = (void *)lov_comp_entry(comp_v1, i);
4700 if (v3->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4703 if (((v3->lmm_magic == LOV_USER_MAGIC_V1) &&
4704 (param->fp_poolname[0] == '\0')) ||
4705 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4706 (strncmp(v3->lmm_pool_name,
4707 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4708 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4709 (strcmp(param->fp_poolname, "*") == 0))) {
4716 if ((found && !param->fp_exclude_pool) ||
4717 (!found && param->fp_exclude_pool))
4723 static int find_check_comp_options(struct find_param *param)
4725 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4726 struct lov_user_mds_data *lmd = param->fp_lmd;
4727 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4728 struct lov_comp_md_entry_v1 *entry;
4731 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4734 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4735 comp_v1 = (struct lov_comp_md_v1 *)v1;
4737 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4738 if (forged_v1 == NULL)
4740 comp_v1 = forged_v1;
4741 comp_v1->lcm_entry_count = 1;
4742 entry = &comp_v1->lcm_entries[0];
4743 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4745 entry->lcme_extent.e_start = 0;
4746 entry->lcme_extent.e_end = LUSTRE_EOF;
4749 /* invalid case, don't match for any kind of search. */
4750 if (comp_v1->lcm_entry_count == 0) {
4755 if (param->fp_check_comp_count) {
4756 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4757 param->fp_comp_count,
4758 param->fp_comp_count_sign,
4759 param->fp_exclude_comp_count, 1, 0);
4765 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4766 entry = &comp_v1->lcm_entries[i];
4768 if (param->fp_check_comp_flags) {
4770 if (((param->fp_comp_flags & entry->lcme_flags) !=
4771 param->fp_comp_flags) ||
4772 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4778 if (param->fp_check_comp_start) {
4779 ret = find_value_cmp(entry->lcme_extent.e_start,
4780 param->fp_comp_start,
4781 param->fp_comp_start_sign,
4782 param->fp_exclude_comp_start,
4783 param->fp_comp_start_units, 0);
4788 if (param->fp_check_comp_end) {
4789 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4795 /* the component matches all criteria */
4804 static int find_check_mirror_options(struct find_param *param)
4806 struct lov_comp_md_v1 *comp_v1;
4807 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4810 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4813 comp_v1 = (struct lov_comp_md_v1 *)v1;
4815 if (param->fp_check_mirror_count) {
4816 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4817 param->fp_mirror_count,
4818 param->fp_mirror_count_sign,
4819 param->fp_exclude_mirror_count, 1, 0);
4824 if (param->fp_check_mirror_state) {
4826 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4828 if ((param->fp_mirror_state != 0 &&
4829 file_state != param->fp_mirror_state) ||
4830 file_state == param->fp_mirror_neg_state)
4837 static bool find_check_lmm_info(struct find_param *param)
4839 return param->fp_check_pool || param->fp_check_stripe_count ||
4840 param->fp_check_stripe_size || param->fp_check_layout ||
4841 param->fp_check_comp_count || param->fp_check_comp_end ||
4842 param->fp_check_comp_start || param->fp_check_comp_flags ||
4843 param->fp_check_mirror_count || param->fp_check_foreign ||
4844 param->fp_check_mirror_state || param->fp_check_ext_size ||
4845 param->fp_check_projid;
4849 * Get file/directory project id.
4850 * by the open fd resides on.
4851 * Return 0 and project id on success, or -ve errno.
4853 static int fget_projid(int fd, int *projid)
4858 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
4862 *projid = fsx.fsx_projid;
4867 * Check that the file's permissions in *st matches the one in find_param
4869 static int check_file_permissions(const struct find_param *param,
4872 const mode_t st_mode = st->st_mode & 07777;
4875 switch (param->fp_perm_sign) {
4876 case LFS_FIND_PERM_EXACT:
4877 decision = (st_mode == param->fp_perm);
4879 case LFS_FIND_PERM_ALL:
4880 decision = ((st_mode & param->fp_perm) == param->fp_perm);
4882 case LFS_FIND_PERM_ANY:
4883 decision = ((st_mode & param->fp_perm) != 0);
4887 if ((param->fp_exclude_perm && decision)
4888 || (!param->fp_exclude_perm && !decision))
4894 static int cb_find_init(char *path, int p, int *dp,
4895 void *data, struct dirent64 *de)
4897 struct find_param *param = (struct find_param *)data;
4898 struct lov_user_mds_data *lmd = param->fp_lmd;
4899 int d = dp == NULL ? -1 : *dp;
4900 int decision = 1; /* 1 is accepted; -1 is rejected. */
4902 int checked_type = 0;
4904 __u32 stripe_count = 0;
4908 if (p == -1 && d == -1)
4911 /* If a regular expression is presented, make the initial decision */
4912 if (param->fp_pattern != NULL) {
4913 char *fname = strrchr(path, '/');
4915 fname = (fname == NULL ? path : fname + 1);
4916 ret = fnmatch(param->fp_pattern, fname, 0);
4917 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4918 (ret == 0 && param->fp_exclude_pattern))
4922 /* See if we can check the file type from the dirent. */
4923 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
4926 if (DTTOIF(de->d_type) == param->fp_type) {
4927 if (param->fp_exclude_type)
4930 if (!param->fp_exclude_type)
4938 * Request MDS for the stat info if some of these parameters need
4941 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4942 param->fp_check_uid || param->fp_check_gid ||
4943 param->fp_newerxy || param->fp_btime ||
4944 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4945 param->fp_check_size || param->fp_check_blocks ||
4946 find_check_lmm_info(param) ||
4947 param->fp_check_mdt_count || param->fp_hash_type ||
4948 param->fp_check_hash_flag || param->fp_perm_sign)
4951 if (param->fp_type != 0 && checked_type == 0)
4954 if (decision == 0) {
4955 if (d != -1 && (param->fp_check_mdt_count ||
4956 param->fp_hash_type || param->fp_check_foreign ||
4957 param->fp_check_hash_flag)) {
4958 param->fp_get_lmv = 1;
4959 ret = cb_get_dirstripe(path, &d, param);
4962 * XXX this works to decide for foreign
4965 if (errno == ENODATA &&
4966 param->fp_check_foreign) {
4967 if (param->fp_exclude_foreign)
4975 param->fp_lmd->lmd_lmm.lmm_magic = 0;
4976 ret = get_lmd_info_fd(path, p, d, param->fp_lmd,
4977 param->fp_lum_size, GET_LMD_INFO);
4978 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
4979 find_check_lmm_info(param)) {
4980 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
4983 * We need to "fake" the "use the default" values
4984 * since the lmm struct is zeroed out at this point.
4986 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4987 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
4989 ostid_set_seq(&lmm->lmm_oi,
4990 FID_SEQ_LOV_DEFAULT);
4991 lmm->lmm_stripe_size = 0;
4992 lmm->lmm_stripe_count = 0;
4993 lmm->lmm_stripe_offset = -1;
4995 if (ret == 0 && param->fp_mdt_uuid != NULL) {
4997 ret = llapi_file_fget_mdtidx(d,
4998 ¶m->fp_file_mdt_index);
4999 } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
5001 * FIXME: we could get the MDT index from the
5002 * file's FID in lmd->lmd_lmm.lmm_oi without
5003 * opening the file, once we are sure that
5004 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
5005 * That would still be an ioctl() to map the
5006 * FID to the MDT, but not an open RPC.
5008 fd = open(path, O_RDONLY);
5010 ret = llapi_file_fget_mdtidx(fd,
5011 ¶m->fp_file_mdt_index);
5017 * For a special file, we assume it resides on
5018 * the same MDT as the parent directory.
5020 ret = llapi_file_fget_mdtidx(p,
5021 ¶m->fp_file_mdt_index);
5032 stripe_count = find_get_stripe_count(param);
5036 if (param->fp_type && !checked_type) {
5037 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
5038 if (param->fp_exclude_type)
5041 if (!param->fp_exclude_type)
5047 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
5048 if (lustre_fs && param->fp_got_uuids &&
5049 param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
5050 lmd->lmd_stx.stx_dev_minor)) {
5051 /* A lustre/lustre mount point is crossed. */
5052 param->fp_got_uuids = 0;
5053 param->fp_obds_printed = 0;
5054 param->fp_mdt_index = OBD_NOT_FOUND;
5055 param->fp_obd_index = OBD_NOT_FOUND;
5058 if (lustre_fs && !param->fp_got_uuids) {
5059 ret = setup_target_indexes((d != -1) ? d : p, path,
5064 param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
5065 lmd->lmd_stx.stx_dev_minor);
5066 } else if (!lustre_fs && param->fp_got_uuids) {
5067 /* A lustre/non-lustre mount point is crossed. */
5068 param->fp_got_uuids = 0;
5069 param->fp_mdt_index = OBD_NOT_FOUND;
5070 param->fp_obd_index = OBD_NOT_FOUND;
5074 if (param->fp_check_foreign) {
5075 decision = find_check_foreign(param);
5080 if (param->fp_check_stripe_size) {
5081 decision = find_check_stripe_size(param);
5086 if (param->fp_check_ext_size) {
5087 decision = find_check_ext_size(param);
5092 if (param->fp_check_stripe_count) {
5093 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
5094 param->fp_stripe_count_sign,
5095 param->fp_exclude_stripe_count, 1, 0);
5100 if (param->fp_check_mdt_count) {
5101 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
5106 decision = find_value_cmp(
5107 param->fp_lmv_md->lum_stripe_count,
5108 param->fp_mdt_count,
5109 param->fp_mdt_count_sign,
5110 param->fp_exclude_mdt_count, 1, 0);
5115 if (param->fp_check_layout) {
5116 decision = find_check_layout(param);
5121 if (param->fp_hash_type) {
5123 __u32 type = param->fp_lmv_md->lum_hash_type &
5126 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
5131 found = (1 << type) & param->fp_hash_type;
5132 if ((found && param->fp_exclude_hash_type) ||
5133 (!found && !param->fp_exclude_hash_type)) {
5139 if (param->fp_check_hash_flag) {
5140 __u32 flags = param->fp_lmv_md->lum_hash_type &
5141 ~LMV_HASH_TYPE_MASK;
5143 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
5148 if (!(flags & param->fp_hash_inflags) ||
5149 (flags & param->fp_hash_exflags)) {
5155 /* If an OBD UUID is specified but none matches, skip this file. */
5156 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
5157 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
5161 * If an OST or MDT UUID is given, and some OST matches,
5164 if (param->fp_obd_index != OBD_NOT_FOUND ||
5165 param->fp_mdt_index != OBD_NOT_FOUND) {
5166 if (param->fp_obd_uuid) {
5167 if (check_obd_match(param)) {
5169 * If no mdtuuid is given, we are done.
5170 * Otherwise, fall through to the mdtuuid
5173 if (!param->fp_mdt_uuid)
5180 if (param->fp_mdt_uuid) {
5181 if (check_mdt_match(param))
5188 if (param->fp_check_uid) {
5189 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
5190 if (param->fp_exclude_uid)
5193 if (!param->fp_exclude_uid)
5198 if (param->fp_check_gid) {
5199 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
5200 if (param->fp_exclude_gid)
5203 if (!param->fp_exclude_gid)
5208 if (param->fp_check_projid) {
5212 fd = open(path, O_RDONLY);
5215 ret = fget_projid(fd, &projid);
5220 if (projid == param->fp_projid) {
5221 if (param->fp_exclude_projid)
5224 if (!param->fp_exclude_projid)
5229 if (param->fp_check_pool) {
5230 decision = find_check_pool(param);
5235 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
5236 param->fp_check_comp_start || param->fp_check_comp_end) {
5237 decision = find_check_comp_options(param);
5242 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
5243 decision = find_check_mirror_options(param);
5248 /* Check the time on mds. */
5250 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
5253 for_mds = lustre_fs ?
5254 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5255 decision = find_time_check(param, for_mds);
5260 if (param->fp_btime) {
5261 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
5266 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
5267 param->fp_btime, param->fp_bsign,
5268 param->fp_exclude_btime,
5269 param->fp_time_margin, 0);
5274 if (param->fp_newerxy) {
5277 for_mds = lustre_fs ?
5278 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5279 decision = find_newerxy_check(param, for_mds, true);
5288 flags = param->fp_lmd->lmd_flags;
5289 if (param->fp_check_size &&
5290 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5291 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5292 !(flags & OBD_MD_FLSIZE ||
5293 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
5296 if (param->fp_check_blocks &&
5297 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5298 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5299 !(flags & OBD_MD_FLBLOCKS ||
5300 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
5303 if (param->fp_perm_sign)
5307 * If file still fits the request, ask ost for updated info.
5308 * The regular stat is almost of the same speed as some new
5309 * 'glimpse-size-ioctl'.
5315 * For regular files with the stripe the decision may have not
5316 * been taken yet if *time or size is to be checked.
5318 if (param->fp_obd_index != OBD_NOT_FOUND)
5319 print_failed_tgt(param, path, LL_STATFS_LOV);
5321 if (param->fp_mdt_index != OBD_NOT_FOUND)
5322 print_failed_tgt(param, path, LL_STATFS_LMV);
5325 ret = fstat_f(d, &st);
5326 else if (de != NULL)
5327 ret = fstatat_f(p, de->d_name, &st,
5328 AT_SYMLINK_NOFOLLOW);
5330 ret = lstat_f(path, &st);
5333 if (errno == ENOENT) {
5334 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
5335 "warning: %s: %s does not exist",
5340 llapi_error(LLAPI_MSG_ERROR, ret,
5341 "%s: stat on %s failed",
5347 convert_lmd_statx(param->fp_lmd, &st, true);
5348 /* Check the time on osc. */
5349 decision = find_time_check(param, 0);
5353 if (param->fp_newerxy) {
5354 decision = find_newerxy_check(param, 0, false);
5363 /* Check the file permissions from the stat info */
5364 if (param->fp_perm_sign) {
5365 decision = check_file_permissions(param, &st);
5371 if (param->fp_check_size) {
5372 decision = find_value_cmp(lmd->lmd_stx.stx_size,
5374 param->fp_size_sign,
5375 param->fp_exclude_size,
5376 param->fp_size_units, 0);
5381 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
5382 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
5384 param->fp_blocks_sign,
5385 param->fp_exclude_blocks,
5386 param->fp_blocks_units, 0);
5392 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
5393 if (param->fp_zero_end)
5394 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
5396 llapi_printf(LLAPI_MSG_NORMAL, "\n");
5400 /* Do not get down anymore? */
5401 if (param->fp_depth == param->fp_max_depth) {
5412 static int cb_migrate_mdt_init(char *path, int p, int *dp,
5413 void *param_data, struct dirent64 *de)
5415 struct find_param *param = (struct find_param *)param_data;
5416 struct lmv_user_md *lmu = param->fp_lmv_md;
5418 char raw[MAX_IOC_BUFLEN] = {'\0'};
5420 struct obd_ioctl_data data = { 0 };
5426 if (p == -1 && dp == NULL)
5432 if (dp != NULL && *dp != -1)
5436 tmp_p = open_parent(path);
5440 llapi_error(LLAPI_MSG_ERROR, ret,
5441 "can not open %s", path);
5446 path_copy = strdup(path);
5447 filename = basename(path_copy);
5449 data.ioc_inlbuf1 = (char *)filename;
5450 data.ioc_inllen1 = strlen(filename) + 1;
5451 data.ioc_inlbuf2 = (char *)lmu;
5452 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
5454 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5456 llapi_error(LLAPI_MSG_ERROR, ret,
5457 "%s: error packing ioctl data", __func__);
5462 ret = ioctl(tmp_p, LL_IOC_MIGRATE, rawbuf);
5464 if (errno == EBUSY && !retry) {
5466 * because migrate may not be able to lock all involved
5467 * objects in order, for some of them it try lock, while
5468 * there may be conflicting COS locks and cause migrate
5469 * fail with EBUSY, hope a sync() could cause
5470 * transaction commit and release these COS locks.
5475 } else if (errno == EALREADY) {
5476 if (param->fp_verbose & VERBOSE_DETAIL)
5477 llapi_printf(LLAPI_MSG_NORMAL,
5478 "%s migrated to MDT%d already\n",
5479 path, lmu->lum_stripe_offset);
5483 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
5487 } else if (param->fp_verbose & VERBOSE_DETAIL) {
5488 llapi_printf(LLAPI_MSG_NORMAL,
5489 "migrate %s to MDT%d stripe count %d\n",
5490 path, lmu->lum_stripe_offset,
5491 lmu->lum_stripe_count);
5497 * If the directory is being migration, we need
5498 * close the directory after migration,
5499 * so the old directory cache will be cleanup
5500 * on the client side, and re-open to get the
5501 * new directory handle
5503 *dp = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
5506 llapi_error(LLAPI_MSG_ERROR, ret,
5507 "%s: Failed to open '%s'", __func__, path);
5519 /* dir migration finished, shrink its stripes */
5520 static int cb_migrate_mdt_fini(char *path, int p, int *dp, void *data,
5521 struct dirent64 *de)
5523 struct find_param *param = data;
5524 struct lmv_user_md *lmu = param->fp_lmv_md;
5525 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
5528 if (de && de->d_type != DT_DIR)
5533 * close it before setxattr because the latter may destroy the
5534 * original object, and cause close fail.
5542 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
5543 if (ret == -EALREADY)
5546 cb_common_fini(path, p, dp, data, de);
5550 int llapi_migrate_mdt(char *path, struct find_param *param)
5552 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
5556 int llapi_mv(char *path, struct find_param *param)
5558 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
5559 static bool printed;
5562 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
5563 "%s() is deprecated, use llapi_migrate_mdt() instead",
5568 return llapi_migrate_mdt(path, param);
5571 int llapi_find(char *path, struct find_param *param)
5573 return param_callback(path, cb_find_init, cb_common_fini, param);
5577 * Get MDT number that the file/directory inode referenced
5578 * by the open fd resides on.
5579 * Return 0 and mdtidx on success, or -ve errno.
5581 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
5583 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
5588 static int cb_get_mdt_index(char *path, int p, int *dp, void *data,
5589 struct dirent64 *de)
5591 struct find_param *param = (struct find_param *)data;
5592 int d = dp == NULL ? -1 : *dp;
5596 if (p == -1 && d == -1)
5600 ret = llapi_file_fget_mdtidx(d, &mdtidx);
5601 } else /* if (p != -1) */ {
5604 fd = open(path, O_RDONLY | O_NOCTTY);
5606 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5614 if (ret == -ENODATA) {
5615 if (!param->fp_obd_uuid)
5616 llapi_printf(LLAPI_MSG_NORMAL,
5617 "'%s' has no stripe info\n", path);
5619 } else if (ret == -ENOENT) {
5620 llapi_error(LLAPI_MSG_WARN, ret,
5621 "warning: %s: '%s' does not exist",
5624 } else if (ret == -ENOTTY) {
5625 llapi_error(LLAPI_MSG_ERROR, ret,
5626 "%s: '%s' not on a Lustre fs",
5629 llapi_error(LLAPI_MSG_ERROR, ret,
5630 "error: %s: '%s' failed get_mdtidx",
5636 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5637 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5639 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5643 /* Do not go down anymore? */
5644 if (param->fp_depth == param->fp_max_depth)
5652 static int cb_getstripe(char *path, int p, int *dp, void *data,
5653 struct dirent64 *de)
5655 struct find_param *param = (struct find_param *)data;
5656 int d = dp == NULL ? -1 : *dp;
5659 if (p == -1 && d == -1)
5662 if (param->fp_obd_uuid) {
5663 param->fp_quiet = 1;
5664 ret = setup_obd_uuid(d != -1 ? d : p, path, param);
5669 if (d != -1 && (param->fp_get_lmv || param->fp_get_default_lmv))
5670 ret = cb_get_dirstripe(path, &d, param);
5672 (p != -1 && !param->fp_get_lmv && !param->fp_get_default_lmv))
5673 ret = get_lmd_info_fd(path, p, d, ¶m->fp_lmd->lmd_lmm,
5674 param->fp_lum_size, GET_LMD_STRIPE);
5675 else if (d == -1 && (param->fp_get_lmv || param->fp_get_default_lmv)) {
5676 /* in case of a dangling or valid faked symlink dir, opendir()
5677 * should have return either EINVAL or ENOENT, so let's try
5678 * to get LMV just in case, and by opening it as a file but
5679 * with O_NOFOLLOW ...
5681 int fd = open(path, O_RDONLY | O_NOFOLLOW);
5685 ret = cb_get_dirstripe(path, &fd, param);
5687 llapi_lov_dump_user_lmm(param, path, LDF_IS_DIR);
5694 if (errno == ENODATA && d != -1) {
5696 * We need to "fake" the "use the default" values
5697 * since the lmm struct is zeroed out at this point.
5698 * The magic needs to be set in order to satisfy
5699 * a check later on in the code path.
5700 * The object_seq needs to be set for the "(Default)"
5701 * prefix to be displayed.
5703 if (param->fp_get_default_lmv) {
5704 struct lmv_user_md *lum = param->fp_lmv_md;
5706 lum->lum_magic = LMV_USER_MAGIC;
5707 lum->lum_stripe_count = 0;
5708 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
5710 } else if (param->fp_get_lmv) {
5711 struct lmv_user_md *lum = param->fp_lmv_md;
5714 ret = llapi_file_fget_mdtidx(d, &mdtidx);
5717 lum->lum_magic = LMV_MAGIC_V1;
5718 lum->lum_stripe_count = 0;
5719 lum->lum_stripe_offset = mdtidx;
5722 struct lov_user_md *lmm =
5723 ¶m->fp_lmd->lmd_lmm;
5725 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5727 ostid_set_seq(&lmm->lmm_oi,
5728 FID_SEQ_LOV_DEFAULT);
5729 lmm->lmm_stripe_count = 0;
5730 lmm->lmm_stripe_size = 0;
5731 lmm->lmm_stripe_offset = -1;
5734 } else if (errno == ENODATA && p != -1) {
5735 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5736 llapi_printf(LLAPI_MSG_NORMAL,
5737 "%s has no stripe info\n", path);
5739 } else if (errno == ENOENT) {
5740 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5741 "warning: %s: %s does not exist",
5744 } else if (errno == ENOTTY) {
5746 llapi_error(LLAPI_MSG_ERROR, ret,
5747 "%s: '%s' not on a Lustre fs?",
5752 llapi_error(LLAPI_MSG_ERROR, ret,
5753 "error: %s: %s failed for %s",
5755 "LL_IOC_LOV_GETSTRIPE" :
5756 "IOC_MDC_GETFILESTRIPE", path);
5763 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5764 llapi_lov_dump_user_lmm(param, path, d != -1 ? LDF_IS_DIR : 0);
5767 /* Do not get down anymore? */
5768 if (param->fp_depth == param->fp_max_depth)
5776 int llapi_getstripe(char *path, struct find_param *param)
5778 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5779 cb_get_mdt_index : cb_getstripe,
5780 cb_common_fini, param);
5783 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5784 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5786 char raw[MAX_IOC_BUFLEN] = {'\0'};
5788 struct obd_ioctl_data data = { 0 };
5791 data.ioc_inlbuf1 = (char *)&type;
5792 data.ioc_inllen1 = sizeof(__u32);
5793 data.ioc_inlbuf2 = (char *)&index;
5794 data.ioc_inllen2 = sizeof(__u32);
5795 data.ioc_pbuf1 = (char *)stat_buf;
5796 data.ioc_plen1 = sizeof(struct obd_statfs);
5797 data.ioc_pbuf2 = (char *)uuid_buf;
5798 data.ioc_plen2 = sizeof(struct obd_uuid);
5800 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5802 llapi_error(LLAPI_MSG_ERROR, rc,
5803 "%s: error packing ioctl data", __func__);
5807 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5809 return rc < 0 ? -errno : 0;
5812 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5813 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5818 fd = open(path, O_RDONLY);
5821 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5824 * If we can't even open a file on the filesystem (e.g. with
5825 * -ESHUTDOWN), force caller to exit or it will loop forever.
5830 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5837 #define MAX_STRING_SIZE 128
5839 int llapi_ping(char *obd_type, char *obd_name)
5841 int flags = O_RDONLY;
5842 char buf[1] = { 0 };
5846 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5847 obd_type, obd_name);
5851 fd = open(path.gl_pathv[0], flags);
5853 if (errno == EACCES && flags == O_RDONLY) {
5858 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5863 if (flags == O_RDONLY)
5864 rc = read(fd, buf, sizeof(buf));
5866 rc = write(fd, buf, sizeof(buf));
5874 cfs_free_param_data(&path);
5878 int llapi_target_iterate(int type_num, char **obd_type,
5879 void *args, llapi_cb_t cb)
5885 for (i = 0; i < type_num; i++) {
5888 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5892 for (j = 0; j < param.gl_pathc; j++) {
5893 char obd_uuid[UUID_MAX + 1];
5897 fp = fopen(param.gl_pathv[j], "r");
5900 llapi_error(LLAPI_MSG_ERROR, rc,
5901 "error: opening '%s'",
5906 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5908 llapi_error(LLAPI_MSG_ERROR, rc,
5909 "error: reading '%s'",
5914 /* Extract the obd_name from the sysfs path.
5915 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5917 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5923 /* skip /fs/lustre/'obd_type'/ */
5924 obd_name += strlen(obd_type[i]) + 12;
5925 /* chop off after obd_name */
5926 ptr = strrchr(obd_name, '/');
5930 cb(obd_type[i], obd_name, obd_uuid, args);
5939 cfs_free_param_data(¶m);
5943 static void do_target_check(char *obd_type_name, char *obd_name,
5944 char *obd_uuid, void *args)
5948 rc = llapi_ping(obd_type_name, obd_name);
5950 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5952 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5954 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5957 int llapi_target_check(int type_num, char **obd_type, char *dir)
5959 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5962 #undef MAX_STRING_SIZE
5964 /* Is this a lustre fs? */
5965 int llapi_is_lustre_mnttype(const char *type)
5967 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
5970 /* Is this a lustre client fs? */
5971 int llapi_is_lustre_mnt(struct mntent *mnt)
5973 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5974 strstr(mnt->mnt_fsname, ":/") != NULL);
5977 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5979 char fsname[PATH_MAX + 1];
5983 rc = llapi_search_fsname(mnt, fsname);
5987 root = open(mnt, O_RDONLY | O_DIRECTORY);
5990 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
5994 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
5997 if (rc == -ENOENT && LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd))
5998 llapi_error(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, rc,
5999 "Cannot find pool '%s'", qctl->qc_poolname);
6005 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
6010 root = open(mnt, O_RDONLY | O_DIRECTORY);
6013 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
6017 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
6020 llapi_error(LLAPI_MSG_ERROR, rc,
6021 "ioctl on %s for getting connect flags failed", mnt);
6028 * Flush cached pages from all clients.
6030 * \param fd File descriptor
6034 int llapi_file_flush(int fd)
6038 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);