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)
1703 * list of pool names (assume that pool count is smaller
1706 char **list, *buffer = NULL, *fsname = (char *)name;
1707 char *poolname = NULL, *tmp = NULL, data[16];
1708 enum param_filter type = FILTER_BY_PATH;
1709 int obdcount, bufsize, rc, nb, i;
1714 if (name[0] != '/') {
1715 fsname = strdup(name);
1719 poolname = strchr(fsname, '.');
1722 type = FILTER_BY_FS_NAME;
1725 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1726 data, sizeof(data));
1729 obdcount = atoi(data);
1732 * Allocate space for each fsname-OST0000_UUID, 1 per OST,
1733 * and also an array to store the pointers for all that
1737 bufsize = sizeof(struct obd_uuid) * obdcount;
1738 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1739 if (buffer == NULL) {
1743 list = (char **) (buffer + bufsize);
1746 /* name is a path or fsname */
1747 nb = llapi_get_poollist(name, list, obdcount,
1750 /* name is a pool name (<fsname>.<poolname>) */
1751 nb = llapi_get_poolmembers(name, list, obdcount,
1755 if (nb == -EOVERFLOW) {
1758 goto retry_get_pools;
1761 for (i = 0; i < nb; i++)
1762 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1763 rc = (nb < 0 ? nb : 0);
1767 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1772 typedef int (semantic_func_t)(char *path, int p, int *d,
1773 void *data, struct dirent64 *de);
1775 #define OBD_NOT_FOUND (-1)
1777 static void find_param_fini(struct find_param *param)
1779 if (param->fp_migrate)
1782 if (param->fp_obd_indexes) {
1783 free(param->fp_obd_indexes);
1784 param->fp_obd_indexes = NULL;
1787 if (param->fp_lmd) {
1788 free(param->fp_lmd);
1789 param->fp_lmd = NULL;
1792 if (param->fp_lmv_md) {
1793 free(param->fp_lmv_md);
1794 param->fp_lmv_md = NULL;
1798 static int common_param_init(struct find_param *param, char *path)
1800 int lum_size = get_mds_md_size(path);
1805 /* migrate has fp_lmv_md initialized outside */
1806 if (param->fp_migrate)
1809 if (lum_size < PATH_MAX + 1)
1810 lum_size = PATH_MAX + 1;
1812 param->fp_lum_size = lum_size;
1813 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1815 if (param->fp_lmd == NULL) {
1816 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1817 "error: allocate %zu bytes for layout failed",
1818 sizeof(lstat_t) + param->fp_lum_size);
1822 param->fp_lmv_stripe_count = 256;
1823 param->fp_lmv_md = calloc(1,
1824 lmv_user_md_size(param->fp_lmv_stripe_count,
1825 LMV_USER_MAGIC_SPECIFIC));
1826 if (param->fp_lmv_md == NULL) {
1827 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1828 "error: allocation of %d bytes for ioctl",
1829 lmv_user_md_size(param->fp_lmv_stripe_count,
1830 LMV_USER_MAGIC_SPECIFIC));
1831 find_param_fini(param);
1835 param->fp_got_uuids = 0;
1836 param->fp_obd_indexes = NULL;
1837 param->fp_obd_index = OBD_NOT_FOUND;
1838 param->fp_mdt_index = OBD_NOT_FOUND;
1842 static int cb_common_fini(char *path, int p, int *dp, void *data,
1843 struct dirent64 *de)
1845 struct find_param *param = data;
1851 /* set errno upon failure */
1852 static int open_parent(const char *path)
1858 path_copy = strdup(path);
1859 if (path_copy == NULL)
1862 parent_path = dirname(path_copy);
1863 parent = open(parent_path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1869 static int cb_get_dirstripe(char *path, int *d, struct find_param *param)
1872 bool did_nofollow = false;
1875 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1876 if (param->fp_get_default_lmv)
1877 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1879 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1881 ret = ioctl(*d, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1883 /* if ENOTTY likely to be a fake symlink, so try again after
1884 * new open() with O_NOFOLLOW, but only once to prevent any
1885 * loop like for the path of a file/dir not on Lustre !!
1887 if (ret < 0 && errno == ENOTTY && !did_nofollow) {
1890 did_nofollow = true;
1891 fd = open(path, O_RDONLY | O_NOFOLLOW);
1893 /* restore original errno */
1898 /* close original fd and set new */
1901 ret2 = ioctl(fd, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1902 if (ret2 < 0 && errno != E2BIG) {
1903 /* restore original errno */
1907 /* LMV is ok or need to handle E2BIG case now */
1911 if (errno == E2BIG && ret != 0) {
1915 /* if foreign LMV case, fake stripes number */
1916 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
1917 struct lmv_foreign_md *lfm;
1919 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1920 if (lfm->lfm_length < XATTR_SIZE_MAX -
1921 offsetof(typeof(*lfm), lfm_value)) {
1922 uint32_t size = lfm->lfm_length +
1923 offsetof(typeof(*lfm), lfm_value);
1925 stripe_count = lmv_foreign_to_md_stripes(size);
1927 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1928 "error: invalid %d foreign size returned from ioctl",
1933 stripe_count = param->fp_lmv_md->lum_stripe_count;
1935 if (stripe_count <= param->fp_lmv_stripe_count)
1938 free(param->fp_lmv_md);
1939 param->fp_lmv_stripe_count = stripe_count;
1940 lmv_size = lmv_user_md_size(stripe_count,
1941 LMV_USER_MAGIC_SPECIFIC);
1942 param->fp_lmv_md = malloc(lmv_size);
1943 if (param->fp_lmv_md == NULL) {
1944 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1945 "error: allocation of %d bytes for ioctl",
1946 lmv_user_md_size(param->fp_lmv_stripe_count,
1947 LMV_USER_MAGIC_SPECIFIC));
1955 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1958 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
1959 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
1960 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
1961 lmd_v2->lmd_stx.stx_uid = st->st_uid;
1962 lmd_v2->lmd_stx.stx_gid = st->st_gid;
1963 lmd_v2->lmd_stx.stx_mode = st->st_mode;
1964 lmd_v2->lmd_stx.stx_ino = st->st_ino;
1965 lmd_v2->lmd_stx.stx_size = st->st_size;
1966 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
1967 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
1968 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
1969 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
1970 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
1971 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
1972 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
1973 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
1974 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
1976 lmd_v2->lmd_flags = 0;
1978 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1980 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
1981 if (lmd_v2->lmd_stx.stx_size)
1982 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
1983 if (lmd_v2->lmd_stx.stx_blocks)
1984 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
1986 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1987 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
1988 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
1993 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
1995 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
1996 struct lov_user_mds_data *lmd_v2 = lmdbuf;
2000 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
2004 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
2007 st = lmd_v1->lmd_st;
2008 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
2009 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
2010 convert_lmd_statx(lmd_v2, &st, false);
2011 lmd_v2->lmd_lmmsize = 0;
2012 lmd_v2->lmd_padding = 0;
2017 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
2018 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
2020 struct lov_user_mds_data *lmd = lmdbuf;
2021 static bool use_old_ioctl;
2025 if (parent_fd < 0 && dir_fd < 0)
2027 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
2032 * LL_IOC_MDC_GETINFO operates on the current directory inode
2033 * and returns struct lov_user_mds_data, while
2034 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
2036 if (type == GET_LMD_INFO)
2037 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_V1 :
2038 LL_IOC_MDC_GETINFO_V2;
2040 cmd = LL_IOC_LOV_GETSTRIPE;
2043 ret = ioctl(dir_fd, cmd, lmdbuf);
2044 if (ret < 0 && errno == ENOTTY &&
2045 cmd == LL_IOC_MDC_GETINFO_V2) {
2046 cmd = LL_IOC_MDC_GETINFO_V1;
2047 use_old_ioctl = true;
2051 if (cmd == LL_IOC_MDC_GETINFO_V1 && !ret)
2052 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2054 if (ret < 0 && errno == ENOTTY && type == GET_LMD_STRIPE) {
2057 /* retry ioctl() after new open() with O_NOFOLLOW
2058 * just in case it could be a fake symlink
2059 * need using a new open() as dir_fd is being closed
2063 dir_fd2 = open(path, O_RDONLY | O_NDELAY | O_NOFOLLOW);
2065 /* return original error */
2068 ret = ioctl(dir_fd2, cmd, lmdbuf);
2069 /* pass new errno or success back to caller */
2075 } else if (parent_fd >= 0) {
2076 const char *fname = strrchr(path, '/');
2079 * IOC_MDC_GETFILEINFO takes as input the filename (relative to
2080 * the parent directory) and returns struct lov_user_mds_data,
2081 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2083 * This avoids opening, locking, and closing each file on the
2084 * client if that is not needed. Multiple of these ioctl() can
2085 * be done on the parent dir with a single open for all
2086 * files in that directory, and it also doesn't pollute the
2087 * client dcache with millions of dentries when traversing
2088 * a large filesystem.
2090 fname = (fname == NULL ? path : fname + 1);
2092 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2095 else if (ret >= lmdlen || ret++ == 0)
2098 if (type == GET_LMD_INFO)
2099 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_V1 :
2100 IOC_MDC_GETFILEINFO_V2;
2102 cmd = IOC_MDC_GETFILESTRIPE;
2105 ret = ioctl(parent_fd, cmd, lmdbuf);
2106 if (ret < 0 && errno == ENOTTY &&
2107 cmd == IOC_MDC_GETFILEINFO_V2) {
2108 cmd = IOC_MDC_GETFILEINFO_V1;
2109 use_old_ioctl = true;
2110 goto retry_getfileinfo;
2113 if (cmd == IOC_MDC_GETFILEINFO_V1 && !ret)
2114 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2118 if (ret && type == GET_LMD_INFO) {
2119 if (errno == ENOTTY) {
2123 * ioctl is not supported, it is not a lustre fs.
2124 * Do the regular lstat(2) instead.
2126 ret = lstat_f(path, &st);
2129 llapi_error(LLAPI_MSG_ERROR, ret,
2130 "error: %s: lstat failed for %s",
2134 convert_lmd_statx(lmd, &st, true);
2136 * It may be wrong to set use_old_ioctl with true as
2137 * the file is not a lustre fs. So reset it with false
2140 use_old_ioctl = false;
2141 } else if (errno == ENOENT) {
2143 llapi_error(LLAPI_MSG_WARN, ret,
2144 "warning: %s does not exist", path);
2145 } else if (errno != EISDIR && errno != ENODATA) {
2147 llapi_error(LLAPI_MSG_ERROR, ret,
2148 "%s ioctl failed for %s.",
2149 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2150 "IOC_MDC_GETFILEINFO", path);
2157 static int llapi_semantic_traverse(char *path, int size, int parent,
2158 semantic_func_t sem_init,
2159 semantic_func_t sem_fini, void *data,
2160 struct dirent64 *de)
2162 struct find_param *param = (struct find_param *)data;
2163 struct dirent64 *dent;
2164 int len, ret, d, p = -1;
2170 d = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
2171 /* if an invalid fake dir symlink, opendir() will return EINVAL
2172 * instead of ENOTDIR. If a valid but dangling faked or real file/dir
2173 * symlink ENOENT will be returned. For a valid/resolved fake or real
2174 * file symlink ENOTDIR will be returned as for a regular file.
2175 * opendir() will be successful for a valid and resolved fake or real
2176 * dir simlink or a regular dir.
2178 if (d == -1 && errno != ENOTDIR && errno != EINVAL && errno != ENOENT) {
2180 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2183 } else if (d == -1) {
2184 if (errno == ENOENT || errno == EINVAL) {
2185 int old_errno = errno;
2187 /* try to open with O_NOFOLLOW this will help
2188 * differentiate fake vs real symlinks
2189 * it is ok to not use O_DIRECTORY with O_RDONLY
2190 * and it will prevent the need to deal with ENOTDIR
2191 * error, instead of ELOOP, being returned by recent
2192 * kernels for real symlinks
2194 d = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2195 /* if a dangling real symlink should return ELOOP, or
2196 * again ENOENT if really non-existing path, or E...??
2197 * So return original error. If success or ENOTDIR, path
2198 * is likely to be a fake dir/file symlink, so continue
2208 if (parent == -1 && d == -1) {
2209 /* Open the parent dir. */
2210 p = open_parent(path);
2216 } else { /* d != -1 */
2219 /* try to reopen dir with O_NOFOLLOW just in case of a foreign
2222 d2 = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2227 /* continue with d */
2233 ret = sem_init(path, (parent != -1) ? parent : p, &d, data, de);
2243 /* ENOTDIR if fake symlink, do not consider it as an error */
2244 if (errno != ENOTDIR)
2245 llapi_error(LLAPI_MSG_ERROR, errno,
2246 "fdopendir() failed");
2253 while ((dent = readdir64(dir)) != NULL) {
2256 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2260 if ((len + dent->d_reclen + 2) > size) {
2261 llapi_err_noerrno(LLAPI_MSG_ERROR,
2262 "error: %s: string buffer too small for %s",
2267 strcat(path, dent->d_name);
2269 if (dent->d_type == DT_UNKNOWN) {
2270 struct lov_user_mds_data *lmd = param->fp_lmd;
2272 rc = get_lmd_info_fd(path, d, -1, param->fp_lmd,
2273 param->fp_lum_size, GET_LMD_INFO);
2275 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2282 switch (dent->d_type) {
2284 llapi_err_noerrno(LLAPI_MSG_ERROR,
2285 "error: %s: '%s' is UNKNOWN type %d",
2286 __func__, dent->d_name, dent->d_type);
2289 rc = llapi_semantic_traverse(path, size, d, sem_init,
2290 sem_fini, data, dent);
2291 if (rc != 0 && ret == 0)
2297 rc = sem_init(path, d, NULL, data, dent);
2298 if (rc < 0 && ret == 0) {
2303 if (sem_fini && rc == 0)
2304 sem_fini(path, d, NULL, data, dent);
2312 sem_fini(path, parent, &d, data, de);
2325 static int param_callback(char *path, semantic_func_t sem_init,
2326 semantic_func_t sem_fini, struct find_param *param)
2328 int ret, len = strlen(path);
2331 if (len > PATH_MAX) {
2333 llapi_error(LLAPI_MSG_ERROR, ret,
2334 "Path name '%s' is too long", path);
2338 buf = (char *)malloc(2 * PATH_MAX);
2342 snprintf(buf, PATH_MAX + 1, "%s", path);
2343 ret = common_param_init(param, buf);
2347 param->fp_depth = 0;
2349 ret = llapi_semantic_traverse(buf, 2 * PATH_MAX, -1, sem_init,
2350 sem_fini, param, NULL);
2352 find_param_fini(param);
2354 return ret < 0 ? ret : 0;
2357 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2361 rc = ioctl(fd, OBD_IOC_GETDTNAME, lov_name);
2362 if (rc && errno == ENOTTY)
2363 rc = ioctl(fd, OBD_IOC_GETNAME_OLD, lov_name);
2366 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2372 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2376 rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2379 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2385 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2389 /* do not follow faked symlinks */
2390 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
2392 /* real symlink should have failed with ELOOP so retry without
2393 * O_NOFOLLOW just in case
2395 fd = open(path, O_RDONLY | O_NONBLOCK);
2398 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'",
2404 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2410 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2414 fd = open(path, O_RDONLY | O_NONBLOCK);
2417 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2421 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2433 * If uuidp is NULL, return the number of available obd uuids.
2434 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2435 * there are more OSTs than allocated to uuidp, then an error is returned with
2436 * the ost_count set to number of available obd uuids.
2438 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2439 int *ost_count, enum tgt_type type)
2441 char buf[PATH_MAX], format[32];
2442 int rc = 0, index = 0;
2443 struct obd_uuid name;
2447 /* Get the lov name */
2448 if (type == LOV_TYPE)
2449 rc = llapi_file_fget_lov_uuid(fd, &name);
2451 rc = llapi_file_fget_lmv_uuid(fd, &name);
2455 /* Now get the ost uuids */
2456 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2457 FILTER_BY_EXACT, "target_obd", ¶m);
2461 fp = fopen(param.gl_pathv[0], "r");
2464 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2469 snprintf(format, sizeof(format),
2470 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2471 while (fgets(buf, sizeof(buf), fp) != NULL) {
2472 if (uuidp && (index < *ost_count)) {
2473 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2481 if (uuidp && (index > *ost_count))
2486 cfs_free_param_data(¶m);
2490 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2492 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2495 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2500 root = open(mnt, O_RDONLY | O_DIRECTORY);
2503 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2508 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2517 * Check if user specified value matches a real uuid. Ignore _UUID,
2518 * -osc-4ba41334, other trailing gunk in comparison.
2519 * @param real_uuid ends in "_UUID"
2520 * @param search_uuid may or may not end in "_UUID"
2522 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2524 int cmplen = strlen(real_uuid);
2525 int searchlen = strlen(search_uuid);
2527 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2529 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2533 * The UUIDs may legitimately be different lengths, if
2534 * the system was upgraded from an older version.
2536 if (cmplen != searchlen)
2539 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2543 * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2544 * returned in param->fp_obd_index
2546 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2548 struct obd_uuid obd_uuid;
2555 if (param->fp_got_uuids)
2558 /* Get the lov/lmv name */
2559 if (param->fp_get_lmv)
2560 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2562 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2564 if (rc != -ENOTTY) {
2565 llapi_error(LLAPI_MSG_ERROR, rc,
2566 "error: can't get %s name: %s",
2567 param->fp_get_lmv ? "lmv" : "lov",
2575 param->fp_got_uuids = 1;
2577 /* Now get the ost uuids */
2578 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2579 obd_uuid.uuid, FILTER_BY_EXACT,
2580 "target_obd", ¶m_data);
2584 fp = fopen(param_data.gl_pathv[0], "r");
2587 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2588 param_data.gl_pathv[0]);
2592 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2593 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2594 param->fp_get_lmv ? "MDTS" : "OBDS");
2596 snprintf(format, sizeof(format),
2597 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2598 while (fgets(buf, sizeof(buf), fp) != NULL) {
2601 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2604 if (param->fp_obd_uuid) {
2605 if (llapi_uuid_match(obd_uuid.uuid,
2606 param->fp_obd_uuid->uuid)) {
2607 param->fp_obd_index = index;
2610 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2611 /* Print everything */
2612 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2615 param->fp_obds_printed = 1;
2619 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2620 llapi_err_noerrno(LLAPI_MSG_ERROR,
2621 "error: %s: unknown obduuid: %s",
2622 __func__, param->fp_obd_uuid->uuid);
2626 cfs_free_param_data(¶m_data);
2631 * In this case, param->fp_obd_uuid will be an array of obduuids and
2632 * obd index for all these obduuids will be returned in
2633 * param->fp_obd_indexes
2635 static int setup_indexes(int d, char *path, struct obd_uuid *obduuids,
2636 int num_obds, int **obdindexes, int *obdindex,
2639 int ret, obdcount, obd_valid = 0, obdnum;
2641 struct obd_uuid *uuids = NULL;
2645 if (type == LOV_TYPE)
2646 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2648 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2652 obdcount = atoi(buf);
2653 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2658 ret = llapi_get_target_uuids(d, uuids, &obdcount, type);
2660 if (ret == -EOVERFLOW) {
2661 struct obd_uuid *uuids_temp;
2663 uuids_temp = realloc(uuids, obdcount *
2664 sizeof(struct obd_uuid));
2665 if (uuids_temp != NULL) {
2667 goto retry_get_uuids;
2672 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2676 indexes = malloc(num_obds * sizeof(*obdindex));
2677 if (indexes == NULL) {
2682 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2685 /* The user may have specified a simple index */
2686 i = strtol(obduuids[obdnum].uuid, &end, 0);
2687 if (end && *end == '\0' && i < obdcount) {
2688 indexes[obdnum] = i;
2691 for (i = 0; i < obdcount; i++) {
2692 if (llapi_uuid_match(uuids[i].uuid,
2693 obduuids[obdnum].uuid)) {
2694 indexes[obdnum] = i;
2700 if (i >= obdcount) {
2701 indexes[obdnum] = OBD_NOT_FOUND;
2702 llapi_err_noerrno(LLAPI_MSG_ERROR,
2703 "invalid obduuid '%s'",
2704 obduuids[obdnum].uuid);
2710 *obdindex = OBD_NOT_FOUND;
2712 *obdindex = obd_valid;
2714 *obdindexes = indexes;
2722 static int setup_target_indexes(int d, char *path, struct find_param *param)
2726 if (param->fp_mdt_uuid) {
2727 ret = setup_indexes(d, path, param->fp_mdt_uuid,
2729 ¶m->fp_mdt_indexes,
2730 ¶m->fp_mdt_index, LMV_TYPE);
2735 if (param->fp_obd_uuid) {
2736 ret = setup_indexes(d, path, param->fp_obd_uuid,
2738 ¶m->fp_obd_indexes,
2739 ¶m->fp_obd_index, LOV_TYPE);
2744 param->fp_got_uuids = 1;
2749 int llapi_ostlist(char *path, struct find_param *param)
2754 fd = open(path, O_RDONLY | O_DIRECTORY);
2758 ret = setup_obd_uuid(fd, path, param);
2765 * Tries to determine the default stripe attributes for a given filesystem. The
2766 * filesystem to check should be specified by fsname, or will be determined
2769 static int sattr_get_defaults(const char *const fsname,
2770 unsigned int *scount,
2771 unsigned int *ssize,
2772 unsigned int *soffset)
2778 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2779 "stripecount", val, sizeof(val));
2782 *scount = atoi(val);
2786 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2787 "stripesize", val, sizeof(val));
2794 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2795 "stripeoffset", val, sizeof(val));
2798 *soffset = atoi(val);
2805 * Tries to gather the default stripe attributes for a given filesystem. If
2806 * the attributes can be determined, they are cached for easy retreival the
2807 * next time they are needed. Only a single filesystem's attributes are
2810 int sattr_cache_get_defaults(const char *const fsname,
2811 const char *const pathname, unsigned int *scount,
2812 unsigned int *ssize, unsigned int *soffset)
2815 char fsname[PATH_MAX + 1];
2816 unsigned int stripecount;
2817 unsigned int stripesize;
2818 unsigned int stripeoffset;
2824 char fsname_buf[PATH_MAX + 1];
2825 unsigned int tmp[3];
2827 if (fsname == NULL) {
2828 rc = llapi_search_fsname(pathname, fsname_buf);
2832 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2835 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2837 * Ensure all 3 sattrs (count, size, and offset) are
2838 * successfully retrieved and stored in tmp before writing to
2841 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2845 cache.stripecount = tmp[0];
2846 cache.stripesize = tmp[1];
2847 cache.stripeoffset = tmp[2];
2848 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2852 *scount = cache.stripecount;
2854 *ssize = cache.stripesize;
2856 *soffset = cache.stripeoffset;
2861 static char *layout2name(__u32 layout_pattern)
2863 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2865 else if (layout_pattern == LOV_PATTERN_MDT)
2867 else if (layout_pattern == LOV_PATTERN_RAID0)
2869 else if (layout_pattern ==
2870 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2871 return "raid0,overstriped";
2876 enum lov_dump_flags {
2877 LDF_IS_DIR = 0x0001,
2878 LDF_IS_RAW = 0x0002,
2879 LDF_INDENT = 0x0004,
2880 LDF_SKIP_OBJS = 0x0008,
2882 LDF_EXTENSION = 0x0020,
2885 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2886 struct lov_user_ost_data_v1 *objects,
2887 enum llapi_layout_verbose verbose,
2888 int depth, char *pool_name,
2889 enum lov_dump_flags flags)
2891 bool is_dir = flags & LDF_IS_DIR;
2892 bool is_raw = flags & LDF_IS_RAW;
2893 bool indent = flags & LDF_INDENT;
2894 bool yaml = flags & LDF_YAML;
2895 bool skip_objs = flags & LDF_SKIP_OBJS;
2896 bool extension = flags & LDF_EXTENSION;
2897 char *prefix = is_dir ? "" : "lmm_";
2898 char *separator = "";
2899 char *space = indent ? " " : "";
2902 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2903 lmm_oi_set_seq(&lum->lmm_oi, 0);
2904 if (!indent && (verbose & VERBOSE_DETAIL))
2905 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2908 if (!yaml && !indent && depth && path &&
2909 ((verbose != VERBOSE_OBJID) || !is_dir))
2910 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2912 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2913 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2914 space, prefix, lum->lmm_magic);
2915 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2917 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2918 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2920 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2922 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2927 if (verbose & ~VERBOSE_DFID)
2928 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2931 * This needs a bit of hand-holding since old 1.x lmm_oi
2932 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2933 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2934 * a real FID. Ideally the 2.x code would have stored this
2935 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2936 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2937 * worked properly (especially since IGIF FIDs use mds_inum as
2938 * the FID SEQ), but unfortunately that didn't happen.
2940 * Print it to look like an IGIF FID, even though the fields
2941 * are reversed on disk, so that it makes sense to userspace.
2943 * Don't use ostid_id() and ostid_seq(), since they assume the
2944 * oi_fid fields are in the right order. This is why there are
2945 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2947 * For newer layout types hopefully this will be a real FID.
2949 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2950 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2951 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2952 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2953 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2955 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2956 (unsigned long long)seq, oid, ver);
2958 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2959 (unsigned long long)seq, oid, ver);
2962 if (verbose & VERBOSE_STRIPE_COUNT) {
2963 if (verbose & ~VERBOSE_STRIPE_COUNT)
2964 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2967 if (!is_raw && lum->lmm_stripe_count == 0 &&
2968 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2969 unsigned int scount;
2971 rc = sattr_cache_get_defaults(NULL, path,
2975 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2978 llapi_error(LLAPI_MSG_ERROR, rc,
2979 "Cannot determine default stripe count.");
2981 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2983 (__s16)lum->lmm_stripe_count);
2986 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2988 (__s16)lum->lmm_stripe_count);
2990 if (!yaml && is_dir)
2996 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
2997 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
2998 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2999 if (verbose & ~VERBOSE_EXT_SIZE && extension)
3000 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
3002 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
3003 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
3005 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
3008 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
3011 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
3013 llapi_error(LLAPI_MSG_ERROR, rc,
3014 "Cannot determine default stripe size.");
3016 /* Extension size is in KiB */
3017 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3019 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
3020 (unsigned long long)lum->lmm_stripe_size);
3022 if (!yaml && is_dir)
3028 if ((verbose & VERBOSE_PATTERN)) {
3029 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3030 if (verbose & ~VERBOSE_PATTERN)
3031 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
3033 if (lov_pattern_supported(lum->lmm_pattern))
3034 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3035 layout2name(lum->lmm_pattern));
3037 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
3038 separator = (!yaml && is_dir) ? " " : "\n";
3041 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
3042 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3043 if (verbose & ~VERBOSE_GENERATION)
3044 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
3046 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3047 skip_objs ? 0 : (int)lum->lmm_layout_gen);
3051 if (verbose & VERBOSE_STRIPE_OFFSET) {
3052 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3053 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3054 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
3056 if (is_dir || skip_objs)
3057 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3058 lum->lmm_stripe_offset ==
3059 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
3060 lum->lmm_stripe_offset);
3061 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
3062 llapi_printf(LLAPI_MSG_NORMAL, "0");
3064 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3065 objects[0].l_ost_idx);
3066 if (!yaml && is_dir)
3072 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
3073 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3074 if (verbose & ~VERBOSE_POOL)
3075 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
3077 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
3078 if (!yaml && is_dir)
3084 if (strlen(separator) != 0)
3085 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3088 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
3089 struct lov_user_ost_data_v1 *objects,
3090 char *path, int obdindex, int depth,
3091 enum llapi_layout_verbose verbose,
3092 enum lov_dump_flags flags)
3094 bool is_dir = flags & LDF_IS_DIR;
3095 bool indent = flags & LDF_INDENT;
3096 bool skip_objs = flags & LDF_SKIP_OBJS;
3097 bool yaml = flags & LDF_YAML;
3098 bool obdstripe = obdindex == OBD_NOT_FOUND;
3101 if (!obdstripe && !skip_objs) {
3102 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
3103 if (obdindex == objects[i].l_ost_idx) {
3113 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
3116 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
3117 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
3118 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
3119 char *space = " - ";
3122 llapi_printf(LLAPI_MSG_NORMAL,
3123 "%6slmm_objects:\n", " ");
3125 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
3127 llapi_printf(LLAPI_MSG_NORMAL,
3128 "\tobdidx\t\t objid\t\t objid\t\t group\n");
3130 for (i = 0; i < lum->lmm_stripe_count; i++) {
3131 int idx = objects[i].l_ost_idx;
3132 long long oid = ostid_id(&objects[i].l_ost_oi);
3133 long long gr = ostid_seq(&objects[i].l_ost_oi);
3135 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
3139 struct lu_fid fid = { 0 };
3141 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3142 llapi_printf(LLAPI_MSG_NORMAL,
3143 "%sl_ost_idx: %d\n", space, idx);
3144 llapi_printf(LLAPI_MSG_NORMAL,
3145 "%8sl_fid: "DFID_NOBRACE"\n",
3147 } else if (indent) {
3148 struct lu_fid fid = { 0 };
3150 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3151 llapi_printf(LLAPI_MSG_NORMAL,
3152 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
3153 space, i, idx, PFID(&fid));
3157 sprintf(fmt, "%s%s%s\n",
3158 "\t%6u\t%14llu\t%#13llx\t",
3159 (fid_seq_is_rsvd(gr) ||
3160 fid_seq_is_mdt0(gr)) ?
3161 "%14llu" : "%#14llx", "%s");
3162 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
3164 obdindex == idx ? " *" : "");
3168 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3171 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3172 char *path, int obdindex, int depth,
3173 enum llapi_layout_verbose verbose,
3174 enum lov_dump_flags flags)
3176 struct lmv_user_mds_data *objects = lum->lum_objects;
3177 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3178 char *separator = "";
3179 bool yaml = flags & LDF_YAML;
3180 bool obdstripe = false;
3183 if (obdindex != OBD_NOT_FOUND) {
3184 if (lum->lum_stripe_count == 0) {
3185 if (obdindex == lum->lum_stripe_offset)
3188 for (i = 0; i < lum->lum_stripe_count; i++) {
3189 if (obdindex == objects[i].lum_mds) {
3190 llapi_printf(LLAPI_MSG_NORMAL,
3205 /* show all information default */
3207 if (lum->lum_magic == LMV_USER_MAGIC)
3208 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3209 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3211 verbose = VERBOSE_OBJID;
3214 if (depth && path && ((verbose != VERBOSE_OBJID)))
3215 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3217 if (verbose & VERBOSE_STRIPE_COUNT) {
3218 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3219 if (verbose & ~VERBOSE_STRIPE_COUNT)
3220 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3221 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3222 (int)lum->lum_stripe_count);
3223 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3229 if (verbose & VERBOSE_STRIPE_OFFSET) {
3230 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3231 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3232 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3233 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3234 (int)lum->lum_stripe_offset);
3235 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3241 if (verbose & VERBOSE_HASH_TYPE) {
3242 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3243 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3245 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3246 if (verbose & ~VERBOSE_HASH_TYPE)
3247 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3248 if (type < LMV_HASH_TYPE_MAX)
3249 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3250 mdt_hash_name[type]);
3252 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3254 if (flags & LMV_HASH_FLAG_MIGRATION)
3255 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3256 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3257 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3258 if (flags & LMV_HASH_FLAG_LOST_LMV)
3259 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3261 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3267 if ((verbose & VERBOSE_INHERIT) && lum->lum_magic == LMV_USER_MAGIC) {
3268 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3269 if (verbose & ~VERBOSE_INHERIT)
3270 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit: ");
3271 if (lum->lum_max_inherit == LMV_INHERIT_UNLIMITED)
3272 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3273 else if (lum->lum_max_inherit == LMV_INHERIT_NONE)
3274 llapi_printf(LLAPI_MSG_NORMAL, "0");
3276 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3277 lum->lum_max_inherit);
3278 if (verbose & VERBOSE_INHERIT && !yaml)
3284 if ((verbose & VERBOSE_INHERIT_RR) &&
3285 lum->lum_magic == LMV_USER_MAGIC &&
3286 lum->lum_stripe_offset == LMV_OFFSET_DEFAULT) {
3287 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3288 if (verbose & ~VERBOSE_INHERIT_RR)
3289 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit_rr: ");
3290 if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_UNLIMITED)
3291 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3292 else if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_NONE)
3293 llapi_printf(LLAPI_MSG_NORMAL, "0");
3295 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3296 lum->lum_max_inherit_rr);
3297 if (verbose & VERBOSE_INHERIT_RR && !yaml)
3305 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3306 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3307 if (lum->lum_stripe_count > 0)
3308 llapi_printf(LLAPI_MSG_NORMAL,
3309 "mdtidx\t\t FID[seq:oid:ver]\n");
3310 for (i = 0; i < lum->lum_stripe_count; i++) {
3311 int idx = objects[i].lum_mds;
3312 struct lu_fid *fid = &objects[i].lum_fid;
3314 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3315 llapi_printf(LLAPI_MSG_NORMAL,
3316 "%6u\t\t "DFID"\t\t%s\n",
3318 obdindex == idx ? " *" : "");
3322 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3323 pool_name[0] != '\0') {
3324 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3325 if (verbose & ~VERBOSE_POOL)
3326 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3328 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3332 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3333 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3336 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3337 enum lov_dump_flags flags)
3339 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3340 int depth = param->fp_max_depth;
3341 enum llapi_layout_verbose verbose = param->fp_verbose;
3342 bool yaml = flags & LDF_YAML;
3344 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3345 !(flags & LDF_IS_DIR)) && !yaml)
3346 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3348 if (verbose & VERBOSE_DETAIL) {
3349 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3350 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3351 " ", comp_v1->lcm_magic);
3352 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3353 " ", comp_v1->lcm_size);
3354 if (flags & LDF_IS_DIR)
3355 llapi_printf(LLAPI_MSG_NORMAL,
3356 "%2slcm_flags: %s\n", " ",
3357 comp_v1->lcm_mirror_count > 0 ?
3360 llapi_printf(LLAPI_MSG_NORMAL,
3361 "%2slcm_flags: %s\n", " ",
3362 llapi_layout_flags_string(comp_v1->lcm_flags));
3365 if (verbose & VERBOSE_GENERATION) {
3366 if (verbose & ~VERBOSE_GENERATION)
3367 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3369 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3372 if (verbose & VERBOSE_MIRROR_COUNT) {
3373 if (verbose & ~VERBOSE_MIRROR_COUNT)
3374 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3376 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3377 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3378 comp_v1->lcm_mirror_count + 1 : 1);
3381 if (verbose & VERBOSE_COMP_COUNT) {
3382 if (verbose & ~VERBOSE_COMP_COUNT)
3383 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3385 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3386 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3387 comp_v1->lcm_entry_count : 0);
3390 if (verbose & VERBOSE_DETAIL && !yaml)
3391 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3394 static void lcme_flags2str(__u32 comp_flags)
3400 llapi_printf(LLAPI_MSG_NORMAL, "0");
3403 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3404 if (comp_flags & comp_flags_table[i].cfn_flag) {
3406 llapi_printf(LLAPI_MSG_NORMAL, ",");
3407 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3408 comp_flags_table[i].cfn_name);
3409 comp_flags &= ~comp_flags_table[i].cfn_flag;
3415 llapi_printf(LLAPI_MSG_NORMAL, ",");
3416 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3420 static void lov_dump_comp_v1_entry(struct find_param *param,
3421 enum lov_dump_flags flags, int index)
3423 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3424 struct lov_comp_md_entry_v1 *entry;
3425 char *separator = "";
3426 enum llapi_layout_verbose verbose = param->fp_verbose;
3427 bool yaml = flags & LDF_YAML;
3429 entry = &comp_v1->lcm_entries[index];
3432 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3434 if (verbose & VERBOSE_COMP_ID) {
3435 if (verbose & VERBOSE_DETAIL && !yaml)
3436 llapi_printf(LLAPI_MSG_NORMAL,
3437 "%slcme_id: ", " - ");
3438 else if (verbose & ~VERBOSE_COMP_ID)
3439 llapi_printf(LLAPI_MSG_NORMAL,
3440 "%4slcme_id: ", " ");
3441 if (entry->lcme_id != LCME_ID_INVAL)
3442 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3444 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3448 if (verbose & VERBOSE_MIRROR_ID) {
3449 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3450 if (verbose & ~VERBOSE_MIRROR_ID)
3451 llapi_printf(LLAPI_MSG_NORMAL,
3452 "%4slcme_mirror_id: ", " ");
3453 if (entry->lcme_id != LCME_ID_INVAL)
3454 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3455 mirror_id_of(entry->lcme_id));
3457 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3461 if (verbose & VERBOSE_COMP_FLAGS) {
3462 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3463 if (verbose & ~VERBOSE_COMP_FLAGS)
3464 llapi_printf(LLAPI_MSG_NORMAL,
3465 "%4slcme_flags: ", " ");
3466 lcme_flags2str(entry->lcme_flags);
3469 /* print snapshot timestamp if its a nosync comp */
3470 if ((verbose & VERBOSE_COMP_FLAGS) &&
3471 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3472 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3473 if (verbose & ~VERBOSE_COMP_FLAGS)
3474 llapi_printf(LLAPI_MSG_NORMAL,
3475 "%4slcme_timestamp: ", " ");
3477 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3478 (unsigned long long)entry->lcme_timestamp);
3480 time_t stamp = entry->lcme_timestamp;
3481 char *date_str = asctime(localtime(&stamp));
3483 date_str[strlen(date_str) - 1] = '\0';
3484 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3490 if (verbose & VERBOSE_COMP_START) {
3491 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3492 if (verbose & ~VERBOSE_COMP_START)
3493 llapi_printf(LLAPI_MSG_NORMAL,
3494 "%4slcme_extent.e_start: ", " ");
3495 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3496 (unsigned long long)entry->lcme_extent.e_start);
3500 if (verbose & VERBOSE_COMP_END) {
3501 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3502 if (verbose & ~VERBOSE_COMP_END)
3503 llapi_printf(LLAPI_MSG_NORMAL,
3504 "%4slcme_extent.e_end: ", " ");
3505 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3506 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3508 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3509 (unsigned long long)entry->lcme_extent.e_end);
3514 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3515 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3516 } else if (verbose & VERBOSE_DETAIL) {
3517 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3518 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3519 " ", entry->lcme_offset);
3520 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3521 " ", entry->lcme_size);
3522 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3524 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3529 * Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3530 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3532 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3533 * The table below gives the answers for the specified parameters (value and
3534 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3535 * --------------------------------------
3536 * 1 | file > limit; sign > 0 | -1 / -1 |
3537 * 2 | file = limit; sign > 0 | -1 / -1 |
3538 * 3 | file < limit; sign > 0 | ? / 1 |
3539 * 4 | file > limit; sign = 0 | -1 / -1 |
3540 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3541 * 6 | file < limit; sign = 0 | ? / -1 |
3542 * 7 | file > limit; sign < 0 | 1 / 1 |
3543 * 8 | file = limit; sign < 0 | ? / -1 |
3544 * 9 | file < limit; sign < 0 | ? / -1 |
3545 * --------------------------------------
3546 * Note: 5th actually means that the value is within the interval
3547 * (limit - margin, limit].
3549 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3550 int sign, int negopt, unsigned long long margin,
3556 /* Drop the fraction of margin (of days or size). */
3557 if (file + margin <= limit)
3559 } else if (sign == 0) {
3560 if (file <= limit && file + margin > limit)
3562 else if (file + margin <= limit)
3564 } else if (sign < 0) {
3571 return negopt ? ~ret + 1 : ret;
3574 static inline struct lov_user_md *
3575 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3577 return (struct lov_user_md *)((char *)comp_v1 +
3578 comp_v1->lcm_entries[ent_idx].lcme_offset);
3581 static inline struct lov_user_ost_data_v1 *
3582 lov_v1v3_objects(struct lov_user_md *v1)
3584 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3585 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3587 return v1->lmm_objects;
3591 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3593 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3594 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3595 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3597 pool_name[0] = '\0';
3601 print_last_init_comp(struct find_param *param)
3603 /* print all component info */
3604 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3607 /* print specific component info */
3608 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3609 param->fp_check_comp_start || param->fp_check_comp_end ||
3610 param->fp_check_mirror_id || param->fp_check_mirror_index)
3616 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3620 if (param->fp_comp_end == LUSTRE_EOF) {
3621 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3622 match = end == LUSTRE_EOF ? 1 : -1;
3623 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3624 match = end == LUSTRE_EOF ? -1 : 1;
3625 else /* at least EOF */
3627 if (param->fp_exclude_comp_end)
3630 unsigned long long margin;
3632 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3633 match = find_value_cmp(end, param->fp_comp_end,
3634 param->fp_comp_end_sign,
3635 param->fp_exclude_comp_end, margin, 0);
3642 * An example of "getstripe -v" for a two components PFL file:
3645 * lcm_magic: 0x0BD60BD0
3649 * lcm_entry_count: 2
3653 * lcme_extent.e_start: 0
3654 * lcme_extent.e_end: 1048576
3658 * lmm_magic: 0x0BD10BD0
3659 * lmm_seq: 0x200000401
3660 * lmm_object_id: 0x1
3661 * lmm_fid: [0x200000401:0x1:0x0]
3662 * lmm_stripe_count: 1
3663 * lmm_stripe_size: 1048576
3664 * lmm_pattern: raid0
3666 * lmm_stripe_offset: 0
3668 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3672 * lcme_extent.e_start: 1048576
3673 * lcme_extent.e_end: EOF
3677 * lmm_magic: 0x0BD10BD0
3678 * lmm_seq: 0x200000401
3679 * lmm_object_id: 0x1
3680 * lmm_fid: [0x200000401:0x1:0x0]
3681 * lmm_stripe_count: 2
3682 * lmm_stripe_size: 1048576
3683 * lmm_pattern: raid0
3685 * lmm_stripe_offset: 1
3687 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3688 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3690 static void lov_dump_comp_v1(struct find_param *param, char *path,
3691 enum lov_dump_flags flags)
3693 struct lov_comp_md_entry_v1 *entry;
3694 struct lov_user_ost_data_v1 *objects;
3695 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3696 struct lov_user_md_v1 *v1;
3697 char pool_name[LOV_MAXPOOLNAME + 1];
3698 int obdindex = param->fp_obd_index;
3699 int i, j, match, ext;
3700 bool obdstripe = false;
3701 __u16 mirror_index = 0;
3702 __u16 mirror_id = 0;
3704 if (obdindex != OBD_NOT_FOUND) {
3705 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3706 i < comp_v1->lcm_entry_count; i++) {
3707 if (!(comp_v1->lcm_entries[i].lcme_flags &
3711 v1 = lov_comp_entry(comp_v1, i);
3712 objects = lov_v1v3_objects(v1);
3714 for (j = 0; j < v1->lmm_stripe_count; j++) {
3715 if (obdindex == objects[j].l_ost_idx) {
3728 lov_dump_comp_v1_header(param, path, flags);
3730 flags |= LDF_INDENT;
3732 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3733 entry = &comp_v1->lcm_entries[i];
3735 if (param->fp_check_comp_flags) {
3736 if (((param->fp_comp_flags & entry->lcme_flags) !=
3737 param->fp_comp_flags) ||
3738 (param->fp_comp_neg_flags & entry->lcme_flags))
3742 if (param->fp_check_comp_id &&
3743 param->fp_comp_id != entry->lcme_id)
3746 if (param->fp_check_comp_start) {
3747 match = find_value_cmp(entry->lcme_extent.e_start,
3748 param->fp_comp_start,
3749 param->fp_comp_start_sign,
3751 param->fp_comp_start_units, 0);
3756 if (param->fp_check_comp_end) {
3757 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3763 if (param->fp_check_mirror_index) {
3764 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3766 mirror_id = mirror_id_of(entry->lcme_id);
3769 match = find_value_cmp(mirror_index,
3770 param->fp_mirror_index,
3771 param->fp_mirror_index_sign,
3772 param->fp_exclude_mirror_index,
3776 } else if (param->fp_check_mirror_id) {
3777 if (mirror_id != mirror_id_of(entry->lcme_id))
3778 mirror_id = mirror_id_of(entry->lcme_id);
3780 match = find_value_cmp(mirror_id,
3781 param->fp_mirror_id,
3782 param->fp_mirror_id_sign,
3783 param->fp_exclude_mirror_id,
3789 if (print_last_init_comp(param)) {
3791 * if part of stripe info is needed, we'd print only
3792 * the last instantiated component info.
3794 if (entry->lcme_flags & LCME_FL_INIT)
3797 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3798 if (entry->lcme_flags & LCME_FL_EXTENSION)
3799 /* moved back below */
3807 if (entry->lcme_flags & LCME_FL_INIT) {
3808 if (obdindex != OBD_NOT_FOUND) {
3809 flags |= LDF_SKIP_OBJS;
3810 v1 = lov_comp_entry(comp_v1, i);
3811 objects = lov_v1v3_objects(v1);
3813 for (j = 0; j < v1->lmm_stripe_count; j++) {
3814 if (obdindex == objects[j].l_ost_idx) {
3815 flags &= ~LDF_SKIP_OBJS;
3820 flags &= ~LDF_SKIP_OBJS;
3823 flags |= LDF_SKIP_OBJS;
3826 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3828 lov_dump_comp_v1_entry(param, flags, i);
3830 v1 = lov_comp_entry(comp_v1, i);
3831 objects = lov_v1v3_objects(v1);
3832 lov_v1v3_pool_name(v1, pool_name);
3834 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3835 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3836 param->fp_max_depth, param->fp_verbose,
3839 if (print_last_init_comp(param)) {
3841 * directory layout contains only layout template, print the
3845 i = comp_v1->lcm_entry_count - 1;
3848 flags &= ~LDF_SKIP_OBJS;
3850 lov_dump_comp_v1_entry(param, flags, i);
3852 v1 = lov_comp_entry(comp_v1, i);
3853 objects = lov_v1v3_objects(v1);
3854 lov_v1v3_pool_name(v1, pool_name);
3856 entry = &comp_v1->lcm_entries[i];
3857 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3858 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3859 param->fp_max_depth, param->fp_verbose,
3864 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3865 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3868 static inline bool has_any_comp_options(struct find_param *param)
3870 enum llapi_layout_verbose verbose = param->fp_verbose;
3872 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3873 param->fp_check_comp_start || param->fp_check_comp_end ||
3874 param->fp_check_comp_flags)
3877 /* show full layout information, not component specific */
3878 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3881 return verbose & VERBOSE_COMP_OPTS;
3884 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3887 struct lov_user_md *lum = &orig->lmd_lmm;
3888 struct lov_user_mds_data *new;
3889 struct lov_comp_md_v1 *comp_v1;
3890 struct lov_comp_md_entry_v1 *ent;
3891 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3892 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3895 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3897 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3901 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3902 + sizeof(new->lmd_lmmsize));
3904 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3905 comp_v1->lcm_magic = lum->lmm_magic;
3906 comp_v1->lcm_size = lum_off + lum_size;
3907 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3908 comp_v1->lcm_flags = 0;
3909 comp_v1->lcm_entry_count = 1;
3911 ent = &comp_v1->lcm_entries[0];
3913 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3914 ent->lcme_extent.e_start = 0;
3915 ent->lcme_extent.e_end = LUSTRE_EOF;
3916 ent->lcme_offset = lum_off;
3917 ent->lcme_size = lum_size;
3919 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3924 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3925 enum lov_dump_flags flags)
3927 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3929 if (has_any_comp_options(param)) {
3930 struct lov_user_mds_data *new_lmd, *orig_lmd;
3932 orig_lmd = param->fp_lmd;
3933 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3934 if (new_lmd != NULL) {
3935 param->fp_lmd = new_lmd;
3936 lov_dump_comp_v1(param, path, flags);
3937 param->fp_lmd = orig_lmd;
3943 if (magic == LOV_USER_MAGIC_V1) {
3944 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3945 param->fp_lmd->lmd_lmm.lmm_objects,
3946 path, param->fp_obd_index,
3947 param->fp_max_depth, param->fp_verbose,
3950 char pool_name[LOV_MAXPOOLNAME + 1];
3951 struct lov_user_ost_data_v1 *objects;
3952 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3954 snprintf(pool_name, sizeof(pool_name), "%s",
3955 lmmv3->lmm_pool_name);
3956 objects = lmmv3->lmm_objects;
3957 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3958 objects, path, param->fp_obd_index,
3959 param->fp_max_depth, param->fp_verbose,
3964 static uint32_t check_foreign_type(uint32_t foreign_type)
3968 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3969 if (lu_foreign_types[i].lft_name == NULL)
3971 if (foreign_type == lu_foreign_types[i].lft_type)
3975 return LU_FOREIGN_TYPE_UNKNOWN;
3978 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3979 enum lov_dump_flags flags)
3981 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3982 bool yaml = flags & LDF_YAML;
3984 if (!yaml && param->fp_depth && path)
3985 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3987 if (param->fp_verbose & VERBOSE_DETAIL) {
3988 uint32_t type = check_foreign_type(lfm->lfm_type);
3990 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3992 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3994 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3996 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3997 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3998 lu_foreign_types[type].lft_name);
4000 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4002 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
4005 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
4006 lfm->lfm_length, lfm->lfm_value);
4007 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4010 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
4011 enum lov_dump_flags flags)
4013 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
4014 bool yaml = flags & LDF_YAML;
4016 if (!yaml && param->fp_depth && path)
4017 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4019 if (param->fp_verbose & VERBOSE_DETAIL) {
4020 uint32_t type = check_foreign_type(lfm->lfm_type);
4022 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
4024 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
4026 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
4028 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4029 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4030 lu_foreign_types[type].lft_name);
4032 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4034 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
4037 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
4038 lfm->lfm_length, lfm->lfm_value);
4039 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4042 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
4043 enum lov_dump_flags flags)
4047 if (param->fp_get_lmv || param->fp_get_default_lmv)
4048 magic = (__u32)param->fp_lmv_md->lum_magic;
4050 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
4053 flags |= LDF_IS_RAW;
4058 case LOV_USER_MAGIC_V1:
4059 case LOV_USER_MAGIC_V3:
4060 case LOV_USER_MAGIC_SPECIFIC:
4061 lov_dump_plain_user_lmm(param, path, flags);
4063 case LOV_USER_MAGIC_FOREIGN:
4064 lov_dump_foreign_lmm(param, path, flags);
4067 case LMV_USER_MAGIC: {
4068 char pool_name[LOV_MAXPOOLNAME + 1];
4069 struct lmv_user_md *lum;
4071 lum = (struct lmv_user_md *)param->fp_lmv_md;
4072 snprintf(pool_name, sizeof(pool_name), "%s",
4073 lum->lum_pool_name);
4074 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
4075 param->fp_max_depth, param->fp_verbose,
4079 case LOV_USER_MAGIC_COMP_V1:
4080 lov_dump_comp_v1(param, path, flags);
4082 case LMV_MAGIC_FOREIGN:
4083 lmv_dump_foreign_lmm(param, path, flags);
4086 llapi_printf(LLAPI_MSG_NORMAL,
4087 "unknown lmm_magic: %#x (expecting one of %#x %#x %#x %#x)\n",
4088 *(__u32 *)¶m->fp_lmd->lmd_lmm,
4089 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
4090 LMV_USER_MAGIC, LMV_MAGIC_V1);
4095 static int llapi_file_get_stripe1(const char *path, struct lov_user_md *lum)
4101 fname = strrchr(path, '/');
4103 /* It should be a file (or other non-directory) */
4104 if (fname == NULL) {
4105 dname = (char *)malloc(2);
4109 fname = (char *)path;
4111 dname = (char *)malloc(fname - path + 1);
4114 strncpy(dname, path, fname - path);
4115 dname[fname - path] = '\0';
4119 fd = open(dname, O_RDONLY | O_NONBLOCK);
4125 strcpy((char *)lum, fname);
4126 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
4129 if (close(fd) == -1 && rc == 0)
4137 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
4139 char *canon_path = NULL;
4142 rc = llapi_file_get_stripe1(path, lum);
4143 if (!(rc == -ENOTTY || rc == -ENODATA))
4146 /* Handle failure due to symlinks by dereferencing path manually. */
4147 canon_path = canonicalize_file_name(path);
4148 if (canon_path == NULL)
4149 goto out; /* Keep original rc. */
4151 rc2 = llapi_file_get_stripe1(canon_path, lum);
4153 goto out; /* Keep original rc. */
4162 int llapi_file_lookup(int dirfd, const char *name)
4164 struct obd_ioctl_data data = { 0 };
4169 if (dirfd < 0 || name == NULL)
4172 data.ioc_version = OBD_IOCTL_VERSION;
4173 data.ioc_len = sizeof(data);
4174 data.ioc_inlbuf1 = (char *)name;
4175 data.ioc_inllen1 = strlen(name) + 1;
4177 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
4179 llapi_error(LLAPI_MSG_ERROR, rc,
4180 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
4185 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
4192 * Check if the file time matches all the given criteria (e.g. --atime +/-N).
4193 * Return -1 or 1 if file timestamp does not or does match the given criteria
4194 * correspondingly. Return 0 if the MDS time is being checked and there are
4195 * attributes on OSTs and it is not yet clear if the timespamp matches.
4197 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
4198 * updated timestamps.
4200 static int find_time_check(struct find_param *param, int mds)
4202 struct lov_user_mds_data *lmd = param->fp_lmd;
4206 /* Check if file is accepted. */
4207 if (param->fp_atime) {
4208 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4209 param->fp_atime, param->fp_asign,
4210 param->fp_exclude_atime,
4211 param->fp_time_margin, mds);
4217 if (param->fp_mtime) {
4218 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4219 param->fp_mtime, param->fp_msign,
4220 param->fp_exclude_mtime,
4221 param->fp_time_margin, mds);
4226 * If the previous check matches, but this one is not yet clear,
4227 * we should return 0 to do an RPC on OSTs.
4233 if (param->fp_ctime) {
4234 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4235 param->fp_ctime, param->fp_csign,
4236 param->fp_exclude_ctime,
4237 param->fp_time_margin, mds);
4242 * If the previous check matches, but this one is not yet clear,
4243 * we should return 0 to do an RPC on OSTs.
4252 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4254 struct lov_user_mds_data *lmd = param->fp_lmd;
4259 for (i = 0; i < 2; i++) {
4260 /* Check if file is accepted. */
4261 if (param->fp_newery[NEWERXY_ATIME][i]) {
4262 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4263 param->fp_newery[NEWERXY_ATIME][i],
4270 if (param->fp_newery[NEWERXY_MTIME][i]) {
4271 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4272 param->fp_newery[NEWERXY_MTIME][i],
4278 * If the previous check matches, but this one is not
4279 * yet clear, we should return 0 to do an RPC on OSTs.
4285 if (param->fp_newery[NEWERXY_CTIME][i]) {
4286 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4287 param->fp_newery[NEWERXY_CTIME][i],
4293 * If the previous check matches, but this one is not
4294 * yet clear, we should return 0 to do an RPC on OSTs.
4301 * File birth time (btime) can get from MDT directly.
4302 * if @from_mdt is true, it means the input file attributs are
4303 * obtained directly from MDT.
4304 * Thus, if @from_mdt is false, we should skip the following
4310 if (param->fp_newery[NEWERXY_BTIME][i]) {
4311 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4314 rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4315 param->fp_newery[NEWERXY_BTIME][i],
4326 * Check whether the stripes matches the indexes user provided
4330 static int check_obd_match(struct find_param *param)
4332 struct lov_user_ost_data_v1 *objects;
4333 struct lov_comp_md_v1 *comp_v1 = NULL;
4334 struct lov_user_mds_data *lmd = param->fp_lmd;
4335 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4336 int i, j, k, count = 1;
4338 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4341 if (!S_ISREG(lmd->lmd_stx.stx_mode))
4344 /* exclude foreign */
4345 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4346 return param->fp_exclude_obd;
4349 * Only those files should be accepted, which have a
4350 * stripe on the specified OST.
4352 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4353 comp_v1 = (struct lov_comp_md_v1 *)v1;
4354 count = comp_v1->lcm_entry_count;
4357 for (i = 0; i < count; i++) {
4359 v1 = lov_comp_entry(comp_v1, i);
4361 objects = lov_v1v3_objects(v1);
4363 for (j = 0; j < v1->lmm_stripe_count; j++) {
4364 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4367 for (k = 0; k < param->fp_num_obds; k++) {
4368 if (param->fp_obd_indexes[k] ==
4369 objects[j].l_ost_idx)
4370 return !param->fp_exclude_obd;
4375 return param->fp_exclude_obd;
4378 static int check_mdt_match(struct find_param *param)
4382 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4385 /* FIXME: For striped dir, we should get stripe information and check */
4386 for (i = 0; i < param->fp_num_mdts; i++) {
4387 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4388 return !param->fp_exclude_mdt;
4391 if (param->fp_exclude_mdt)
4398 * Check whether the obd is active or not, if it is
4399 * not active, just print the object affected by this
4402 static void print_failed_tgt(struct find_param *param, char *path, int type)
4404 struct obd_statfs stat_buf;
4405 struct obd_uuid uuid_buf;
4406 int tgt_nr, i, *indexes;
4409 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV) {
4410 llapi_error(LLAPI_MSG_NORMAL, ret, "%s: wrong statfs type(%d)",
4415 tgt_nr = (type == LL_STATFS_LOV) ? param->fp_obd_index :
4416 param->fp_mdt_index;
4417 indexes = (type == LL_STATFS_LOV) ? param->fp_obd_indexes :
4418 param->fp_mdt_indexes;
4420 for (i = 0; i < tgt_nr; i++) {
4421 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4422 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4424 ret = llapi_obd_statfs(path, type, indexes[i], &stat_buf,
4427 llapi_error(LLAPI_MSG_NORMAL, ret,
4428 "%s: obd_uuid: %s failed",
4429 __func__, param->fp_obd_uuid->uuid);
4433 static int find_check_stripe_size(struct find_param *param)
4435 struct lov_comp_md_v1 *comp_v1 = NULL;
4436 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4437 __u32 stripe_size = 0;
4438 int ret, i, count = 1;
4440 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4441 return param->fp_exclude_stripe_size ? 1 : -1;
4443 ret = param->fp_exclude_stripe_size ? 1 : -1;
4444 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4445 comp_v1 = (struct lov_comp_md_v1 *)v1;
4446 count = comp_v1->lcm_entry_count;
4449 for (i = 0; i < count; i++) {
4450 struct lov_comp_md_entry_v1 *ent;
4453 v1 = lov_comp_entry(comp_v1, i);
4455 ent = &comp_v1->lcm_entries[i];
4456 if (ent->lcme_flags & LCME_FL_EXTENSION)
4458 if (!(ent->lcme_flags & LCME_FL_INIT))
4461 stripe_size = v1->lmm_stripe_size;
4464 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4465 param->fp_stripe_size_sign,
4466 param->fp_exclude_stripe_size,
4467 param->fp_stripe_size_units, 0);
4472 static int find_check_ext_size(struct find_param *param)
4474 struct lov_comp_md_v1 *comp_v1;
4475 struct lov_user_md_v1 *v1;
4478 ret = param->fp_exclude_ext_size ? 1 : -1;
4479 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4480 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4483 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4484 struct lov_comp_md_entry_v1 *ent;
4486 v1 = lov_comp_entry(comp_v1, i);
4488 ent = &comp_v1->lcm_entries[i];
4489 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4492 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4493 param->fp_ext_size_sign,
4494 param->fp_exclude_ext_size,
4495 param->fp_ext_size_units, 0);
4496 /* If any ext_size matches */
4504 static __u32 find_get_stripe_count(struct find_param *param)
4506 struct lov_comp_md_v1 *comp_v1 = NULL;
4507 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4509 __u32 stripe_count = 0;
4511 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4514 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4515 comp_v1 = (struct lov_comp_md_v1 *)v1;
4516 count = comp_v1->lcm_entry_count;
4519 for (i = 0; i < count; i++) {
4521 struct lov_comp_md_entry_v1 *ent;
4523 v1 = lov_comp_entry(comp_v1, i);
4525 ent = &comp_v1->lcm_entries[i];
4526 if (!(ent->lcme_flags & LCME_FL_INIT))
4529 if (ent->lcme_flags & LCME_FL_EXTENSION)
4532 stripe_count = v1->lmm_stripe_count;
4535 return stripe_count;
4538 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4540 static int find_check_layout(struct find_param *param)
4542 struct lov_comp_md_v1 *comp_v1 = NULL;
4543 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4545 bool found = false, valid = false;
4547 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4548 comp_v1 = (struct lov_comp_md_v1 *)v1;
4549 count = comp_v1->lcm_entry_count;
4552 for (i = 0; i < count; i++) {
4554 v1 = lov_comp_entry(comp_v1, i);
4556 /* foreign file have a special magic but no pattern field */
4557 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4560 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4564 if (v1->lmm_pattern & param->fp_layout) {
4573 if ((found && !param->fp_exclude_layout) ||
4574 (!found && param->fp_exclude_layout))
4581 * if no type specified, check/exclude all foreign
4582 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4584 static int find_check_foreign(struct find_param *param)
4586 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4587 struct lov_foreign_md *lfm;
4589 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4590 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4591 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4592 return param->fp_exclude_foreign ? 1 : -1;
4596 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4597 lfm->lfm_type == param->fp_foreign_type)
4598 return param->fp_exclude_foreign ? -1 : 1;
4599 return param->fp_exclude_foreign ? 1 : -1;
4602 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4603 struct lmv_foreign_md *lfm;
4605 lfm = (void *)param->fp_lmv_md;
4606 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
4607 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4608 return param->fp_exclude_foreign ? 1 : -1;
4612 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4613 lfm->lfm_type == param->fp_foreign_type)
4614 return param->fp_exclude_foreign ? -1 : 1;
4615 return param->fp_exclude_foreign ? 1 : -1;
4620 static int find_check_pool(struct find_param *param)
4622 struct lov_comp_md_v1 *comp_v1 = NULL;
4623 struct lov_user_md_v3 *v3 = (void *)¶m->fp_lmd->lmd_lmm;
4627 if (v3->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4628 comp_v1 = (struct lov_comp_md_v1 *)v3;
4629 count = comp_v1->lcm_entry_count;
4630 /* empty requested pool is taken as no pool search */
4631 if (count == 0 && param->fp_poolname[0] == '\0') {
4637 for (i = 0; i < count; i++) {
4638 if (comp_v1 != NULL) {
4639 if (!(comp_v1->lcm_entries[i].lcme_flags &
4643 v3 = (void *)lov_comp_entry(comp_v1, i);
4646 if (v3->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4649 if (((v3->lmm_magic == LOV_USER_MAGIC_V1) &&
4650 (param->fp_poolname[0] == '\0')) ||
4651 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4652 (strncmp(v3->lmm_pool_name,
4653 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4654 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4655 (strcmp(param->fp_poolname, "*") == 0))) {
4662 if ((found && !param->fp_exclude_pool) ||
4663 (!found && param->fp_exclude_pool))
4669 static int find_check_comp_options(struct find_param *param)
4671 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4672 struct lov_user_mds_data *lmd = param->fp_lmd;
4673 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4674 struct lov_comp_md_entry_v1 *entry;
4677 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4680 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4681 comp_v1 = (struct lov_comp_md_v1 *)v1;
4683 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4684 if (forged_v1 == NULL)
4686 comp_v1 = forged_v1;
4687 comp_v1->lcm_entry_count = 1;
4688 entry = &comp_v1->lcm_entries[0];
4689 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4691 entry->lcme_extent.e_start = 0;
4692 entry->lcme_extent.e_end = LUSTRE_EOF;
4695 /* invalid case, don't match for any kind of search. */
4696 if (comp_v1->lcm_entry_count == 0) {
4701 if (param->fp_check_comp_count) {
4702 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4703 param->fp_comp_count,
4704 param->fp_comp_count_sign,
4705 param->fp_exclude_comp_count, 1, 0);
4711 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4712 entry = &comp_v1->lcm_entries[i];
4714 if (param->fp_check_comp_flags) {
4716 if (((param->fp_comp_flags & entry->lcme_flags) !=
4717 param->fp_comp_flags) ||
4718 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4724 if (param->fp_check_comp_start) {
4725 ret = find_value_cmp(entry->lcme_extent.e_start,
4726 param->fp_comp_start,
4727 param->fp_comp_start_sign,
4728 param->fp_exclude_comp_start,
4729 param->fp_comp_start_units, 0);
4734 if (param->fp_check_comp_end) {
4735 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4741 /* the component matches all criteria */
4750 static int find_check_mirror_options(struct find_param *param)
4752 struct lov_comp_md_v1 *comp_v1;
4753 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4756 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4759 comp_v1 = (struct lov_comp_md_v1 *)v1;
4761 if (param->fp_check_mirror_count) {
4762 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4763 param->fp_mirror_count,
4764 param->fp_mirror_count_sign,
4765 param->fp_exclude_mirror_count, 1, 0);
4770 if (param->fp_check_mirror_state) {
4772 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4774 if ((param->fp_mirror_state != 0 &&
4775 file_state != param->fp_mirror_state) ||
4776 file_state == param->fp_mirror_neg_state)
4783 static bool find_check_lmm_info(struct find_param *param)
4785 return param->fp_check_pool || param->fp_check_stripe_count ||
4786 param->fp_check_stripe_size || param->fp_check_layout ||
4787 param->fp_check_comp_count || param->fp_check_comp_end ||
4788 param->fp_check_comp_start || param->fp_check_comp_flags ||
4789 param->fp_check_mirror_count || param->fp_check_foreign ||
4790 param->fp_check_mirror_state || param->fp_check_ext_size ||
4791 param->fp_check_projid;
4795 * Get file/directory project id.
4796 * by the open fd resides on.
4797 * Return 0 and project id on success, or -ve errno.
4799 static int fget_projid(int fd, int *projid)
4804 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
4808 *projid = fsx.fsx_projid;
4812 static int cb_find_init(char *path, int p, int *dp,
4813 void *data, struct dirent64 *de)
4815 struct find_param *param = (struct find_param *)data;
4816 struct lov_user_mds_data *lmd = param->fp_lmd;
4817 int d = dp == NULL ? -1 : *dp;
4818 int decision = 1; /* 1 is accepted; -1 is rejected. */
4820 int checked_type = 0;
4822 __u32 stripe_count = 0;
4826 if (p == -1 && d == -1)
4829 /* If a regular expression is presented, make the initial decision */
4830 if (param->fp_pattern != NULL) {
4831 char *fname = strrchr(path, '/');
4833 fname = (fname == NULL ? path : fname + 1);
4834 ret = fnmatch(param->fp_pattern, fname, 0);
4835 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4836 (ret == 0 && param->fp_exclude_pattern))
4840 /* See if we can check the file type from the dirent. */
4841 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
4844 if (DTTOIF(de->d_type) == param->fp_type) {
4845 if (param->fp_exclude_type)
4848 if (!param->fp_exclude_type)
4856 * Request MDS for the stat info if some of these parameters need
4859 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4860 param->fp_check_uid || param->fp_check_gid ||
4861 param->fp_newerxy || param->fp_btime ||
4862 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4863 param->fp_check_size || param->fp_check_blocks ||
4864 find_check_lmm_info(param) ||
4865 param->fp_check_mdt_count || param->fp_hash_type ||
4866 param->fp_check_hash_flag)
4869 if (param->fp_type != 0 && checked_type == 0)
4872 if (decision == 0) {
4873 if (d != -1 && (param->fp_check_mdt_count ||
4874 param->fp_hash_type || param->fp_check_foreign ||
4875 param->fp_check_hash_flag)) {
4876 param->fp_get_lmv = 1;
4877 ret = cb_get_dirstripe(path, &d, param);
4880 * XXX this works to decide for foreign
4883 if (errno == ENODATA &&
4884 param->fp_check_foreign) {
4885 if (param->fp_exclude_foreign)
4893 param->fp_lmd->lmd_lmm.lmm_magic = 0;
4894 ret = get_lmd_info_fd(path, p, d, param->fp_lmd,
4895 param->fp_lum_size, GET_LMD_INFO);
4896 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
4897 find_check_lmm_info(param)) {
4898 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
4901 * We need to "fake" the "use the default" values
4902 * since the lmm struct is zeroed out at this point.
4904 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4905 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
4907 ostid_set_seq(&lmm->lmm_oi,
4908 FID_SEQ_LOV_DEFAULT);
4909 lmm->lmm_stripe_size = 0;
4910 lmm->lmm_stripe_count = 0;
4911 lmm->lmm_stripe_offset = -1;
4913 if (ret == 0 && param->fp_mdt_uuid != NULL) {
4915 ret = llapi_file_fget_mdtidx(d,
4916 ¶m->fp_file_mdt_index);
4917 } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
4919 * FIXME: we could get the MDT index from the
4920 * file's FID in lmd->lmd_lmm.lmm_oi without
4921 * opening the file, once we are sure that
4922 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
4923 * That would still be an ioctl() to map the
4924 * FID to the MDT, but not an open RPC.
4926 fd = open(path, O_RDONLY);
4928 ret = llapi_file_fget_mdtidx(fd,
4929 ¶m->fp_file_mdt_index);
4935 * For a special file, we assume it resides on
4936 * the same MDT as the parent directory.
4938 ret = llapi_file_fget_mdtidx(p,
4939 ¶m->fp_file_mdt_index);
4950 stripe_count = find_get_stripe_count(param);
4954 if (param->fp_type && !checked_type) {
4955 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
4956 if (param->fp_exclude_type)
4959 if (!param->fp_exclude_type)
4965 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
4966 if (lustre_fs && param->fp_got_uuids &&
4967 param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
4968 lmd->lmd_stx.stx_dev_minor)) {
4969 /* A lustre/lustre mount point is crossed. */
4970 param->fp_got_uuids = 0;
4971 param->fp_obds_printed = 0;
4972 param->fp_mdt_index = OBD_NOT_FOUND;
4973 param->fp_obd_index = OBD_NOT_FOUND;
4976 if (lustre_fs && !param->fp_got_uuids) {
4977 ret = setup_target_indexes((d != -1) ? d : p, path,
4982 param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
4983 lmd->lmd_stx.stx_dev_minor);
4984 } else if (!lustre_fs && param->fp_got_uuids) {
4985 /* A lustre/non-lustre mount point is crossed. */
4986 param->fp_got_uuids = 0;
4987 param->fp_mdt_index = OBD_NOT_FOUND;
4988 param->fp_obd_index = OBD_NOT_FOUND;
4992 if (param->fp_check_foreign) {
4993 decision = find_check_foreign(param);
4998 if (param->fp_check_stripe_size) {
4999 decision = find_check_stripe_size(param);
5004 if (param->fp_check_ext_size) {
5005 decision = find_check_ext_size(param);
5010 if (param->fp_check_stripe_count) {
5011 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
5012 param->fp_stripe_count_sign,
5013 param->fp_exclude_stripe_count, 1, 0);
5018 if (param->fp_check_mdt_count) {
5019 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
5024 decision = find_value_cmp(
5025 param->fp_lmv_md->lum_stripe_count,
5026 param->fp_mdt_count,
5027 param->fp_mdt_count_sign,
5028 param->fp_exclude_mdt_count, 1, 0);
5033 if (param->fp_check_layout) {
5034 decision = find_check_layout(param);
5039 if (param->fp_hash_type) {
5041 __u32 type = param->fp_lmv_md->lum_hash_type &
5044 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
5049 found = (1 << type) & param->fp_hash_type;
5050 if ((found && param->fp_exclude_hash_type) ||
5051 (!found && !param->fp_exclude_hash_type)) {
5057 if (param->fp_check_hash_flag) {
5058 __u32 flags = param->fp_lmv_md->lum_hash_type &
5059 ~LMV_HASH_TYPE_MASK;
5061 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
5066 if (!(flags & param->fp_hash_inflags) ||
5067 (flags & param->fp_hash_exflags)) {
5073 /* If an OBD UUID is specified but none matches, skip this file. */
5074 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
5075 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
5079 * If an OST or MDT UUID is given, and some OST matches,
5082 if (param->fp_obd_index != OBD_NOT_FOUND ||
5083 param->fp_mdt_index != OBD_NOT_FOUND) {
5084 if (param->fp_obd_uuid) {
5085 if (check_obd_match(param)) {
5087 * If no mdtuuid is given, we are done.
5088 * Otherwise, fall through to the mdtuuid
5091 if (!param->fp_mdt_uuid)
5098 if (param->fp_mdt_uuid) {
5099 if (check_mdt_match(param))
5106 if (param->fp_check_uid) {
5107 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
5108 if (param->fp_exclude_uid)
5111 if (!param->fp_exclude_uid)
5116 if (param->fp_check_gid) {
5117 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
5118 if (param->fp_exclude_gid)
5121 if (!param->fp_exclude_gid)
5126 if (param->fp_check_projid) {
5130 fd = open(path, O_RDONLY);
5133 ret = fget_projid(fd, &projid);
5138 if (projid == param->fp_projid) {
5139 if (param->fp_exclude_projid)
5142 if (!param->fp_exclude_projid)
5147 if (param->fp_check_pool) {
5148 decision = find_check_pool(param);
5153 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
5154 param->fp_check_comp_start || param->fp_check_comp_end) {
5155 decision = find_check_comp_options(param);
5160 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
5161 decision = find_check_mirror_options(param);
5166 /* Check the time on mds. */
5168 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
5171 for_mds = lustre_fs ?
5172 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5173 decision = find_time_check(param, for_mds);
5178 if (param->fp_btime) {
5179 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
5184 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
5185 param->fp_btime, param->fp_bsign,
5186 param->fp_exclude_btime,
5187 param->fp_time_margin, 0);
5192 if (param->fp_newerxy) {
5195 for_mds = lustre_fs ?
5196 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5197 decision = find_newerxy_check(param, for_mds, true);
5206 flags = param->fp_lmd->lmd_flags;
5207 if (param->fp_check_size &&
5208 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5209 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5210 !(flags & OBD_MD_FLSIZE ||
5211 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
5214 if (param->fp_check_blocks &&
5215 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5216 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5217 !(flags & OBD_MD_FLBLOCKS ||
5218 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
5222 * If file still fits the request, ask ost for updated info.
5223 * The regular stat is almost of the same speed as some new
5224 * 'glimpse-size-ioctl'.
5230 * For regular files with the stripe the decision may have not
5231 * been taken yet if *time or size is to be checked.
5233 if (param->fp_obd_index != OBD_NOT_FOUND)
5234 print_failed_tgt(param, path, LL_STATFS_LOV);
5236 if (param->fp_mdt_index != OBD_NOT_FOUND)
5237 print_failed_tgt(param, path, LL_STATFS_LMV);
5240 ret = fstat_f(d, &st);
5241 else if (de != NULL)
5242 ret = fstatat_f(p, de->d_name, &st,
5243 AT_SYMLINK_NOFOLLOW);
5245 ret = lstat_f(path, &st);
5248 if (errno == ENOENT) {
5249 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
5250 "warning: %s: %s does not exist",
5255 llapi_error(LLAPI_MSG_ERROR, ret,
5256 "%s: stat on %s failed",
5262 convert_lmd_statx(param->fp_lmd, &st, true);
5263 /* Check the time on osc. */
5264 decision = find_time_check(param, 0);
5268 if (param->fp_newerxy) {
5269 decision = find_newerxy_check(param, 0, false);
5279 if (param->fp_check_size) {
5280 decision = find_value_cmp(lmd->lmd_stx.stx_size,
5282 param->fp_size_sign,
5283 param->fp_exclude_size,
5284 param->fp_size_units, 0);
5289 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
5290 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
5292 param->fp_blocks_sign,
5293 param->fp_exclude_blocks,
5294 param->fp_blocks_units, 0);
5300 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
5301 if (param->fp_zero_end)
5302 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
5304 llapi_printf(LLAPI_MSG_NORMAL, "\n");
5308 /* Do not get down anymore? */
5309 if (param->fp_depth == param->fp_max_depth) {
5320 static int cb_migrate_mdt_init(char *path, int p, int *dp,
5321 void *param_data, struct dirent64 *de)
5323 struct find_param *param = (struct find_param *)param_data;
5324 struct lmv_user_md *lmu = param->fp_lmv_md;
5326 char raw[MAX_IOC_BUFLEN] = {'\0'};
5328 struct obd_ioctl_data data = { 0 };
5334 if (p == -1 && dp == NULL)
5340 if (dp != NULL && *dp != -1)
5344 tmp_p = open_parent(path);
5348 llapi_error(LLAPI_MSG_ERROR, ret,
5349 "can not open %s", path);
5354 path_copy = strdup(path);
5355 filename = basename(path_copy);
5357 data.ioc_inlbuf1 = (char *)filename;
5358 data.ioc_inllen1 = strlen(filename) + 1;
5359 data.ioc_inlbuf2 = (char *)lmu;
5360 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
5362 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5364 llapi_error(LLAPI_MSG_ERROR, ret,
5365 "%s: error packing ioctl data", __func__);
5370 ret = ioctl(tmp_p, LL_IOC_MIGRATE, rawbuf);
5372 if (errno == EBUSY && !retry) {
5374 * because migrate may not be able to lock all involved
5375 * objects in order, for some of them it try lock, while
5376 * there may be conflicting COS locks and cause migrate
5377 * fail with EBUSY, hope a sync() could cause
5378 * transaction commit and release these COS locks.
5383 } else if (errno == EALREADY) {
5384 if (param->fp_verbose & VERBOSE_DETAIL)
5385 llapi_printf(LLAPI_MSG_NORMAL,
5386 "%s migrated to MDT%d already\n",
5387 path, lmu->lum_stripe_offset);
5391 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
5395 } else if (param->fp_verbose & VERBOSE_DETAIL) {
5396 llapi_printf(LLAPI_MSG_NORMAL,
5397 "migrate %s to MDT%d stripe count %d\n",
5398 path, lmu->lum_stripe_offset,
5399 lmu->lum_stripe_count);
5405 * If the directory is being migration, we need
5406 * close the directory after migration,
5407 * so the old directory cache will be cleanup
5408 * on the client side, and re-open to get the
5409 * new directory handle
5411 *dp = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
5414 llapi_error(LLAPI_MSG_ERROR, ret,
5415 "%s: Failed to open '%s'", __func__, path);
5427 /* dir migration finished, shrink its stripes */
5428 static int cb_migrate_mdt_fini(char *path, int p, int *dp, void *data,
5429 struct dirent64 *de)
5431 struct find_param *param = data;
5432 struct lmv_user_md *lmu = param->fp_lmv_md;
5433 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
5436 if (de && de->d_type != DT_DIR)
5441 * close it before setxattr because the latter may destroy the
5442 * original object, and cause close fail.
5450 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
5451 if (ret == -EALREADY)
5454 cb_common_fini(path, p, dp, data, de);
5458 int llapi_migrate_mdt(char *path, struct find_param *param)
5460 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
5464 int llapi_mv(char *path, struct find_param *param)
5466 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
5467 static bool printed;
5470 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
5471 "%s() is deprecated, use llapi_migrate_mdt() instead",
5476 return llapi_migrate_mdt(path, param);
5479 int llapi_find(char *path, struct find_param *param)
5481 return param_callback(path, cb_find_init, cb_common_fini, param);
5485 * Get MDT number that the file/directory inode referenced
5486 * by the open fd resides on.
5487 * Return 0 and mdtidx on success, or -ve errno.
5489 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
5491 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
5496 static int cb_get_mdt_index(char *path, int p, int *dp, void *data,
5497 struct dirent64 *de)
5499 struct find_param *param = (struct find_param *)data;
5500 int d = dp == NULL ? -1 : *dp;
5504 if (p == -1 && d == -1)
5508 ret = llapi_file_fget_mdtidx(d, &mdtidx);
5509 } else /* if (p != -1) */ {
5512 fd = open(path, O_RDONLY | O_NOCTTY);
5514 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5522 if (ret == -ENODATA) {
5523 if (!param->fp_obd_uuid)
5524 llapi_printf(LLAPI_MSG_NORMAL,
5525 "'%s' has no stripe info\n", path);
5527 } else if (ret == -ENOENT) {
5528 llapi_error(LLAPI_MSG_WARN, ret,
5529 "warning: %s: '%s' does not exist",
5532 } else if (ret == -ENOTTY) {
5533 llapi_error(LLAPI_MSG_ERROR, ret,
5534 "%s: '%s' not on a Lustre fs",
5537 llapi_error(LLAPI_MSG_ERROR, ret,
5538 "error: %s: '%s' failed get_mdtidx",
5544 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5545 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5547 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5551 /* Do not go down anymore? */
5552 if (param->fp_depth == param->fp_max_depth)
5560 static int cb_getstripe(char *path, int p, int *dp, void *data,
5561 struct dirent64 *de)
5563 struct find_param *param = (struct find_param *)data;
5564 int d = dp == NULL ? -1 : *dp;
5567 if (p == -1 && d == -1)
5570 if (param->fp_obd_uuid) {
5571 param->fp_quiet = 1;
5572 ret = setup_obd_uuid(d != -1 ? d : p, path, param);
5577 if (d != -1 && (param->fp_get_lmv || param->fp_get_default_lmv))
5578 ret = cb_get_dirstripe(path, &d, param);
5580 (p != -1 && !param->fp_get_lmv && !param->fp_get_default_lmv))
5581 ret = get_lmd_info_fd(path, p, d, ¶m->fp_lmd->lmd_lmm,
5582 param->fp_lum_size, GET_LMD_STRIPE);
5583 else if (d == -1 && (param->fp_get_lmv || param->fp_get_default_lmv)) {
5584 /* in case of a dangling or valid faked symlink dir, opendir()
5585 * should have return either EINVAL or ENOENT, so let's try
5586 * to get LMV just in case, and by opening it as a file but
5587 * with O_NOFOLLOW ...
5589 int fd = open(path, O_RDONLY | O_NOFOLLOW);
5593 ret = cb_get_dirstripe(path, &fd, param);
5595 llapi_lov_dump_user_lmm(param, path, LDF_IS_DIR);
5602 if (errno == ENODATA && d != -1) {
5604 * We need to "fake" the "use the default" values
5605 * since the lmm struct is zeroed out at this point.
5606 * The magic needs to be set in order to satisfy
5607 * a check later on in the code path.
5608 * The object_seq needs to be set for the "(Default)"
5609 * prefix to be displayed.
5611 if (param->fp_get_default_lmv) {
5612 struct lmv_user_md *lum = param->fp_lmv_md;
5614 lum->lum_magic = LMV_USER_MAGIC;
5615 lum->lum_stripe_count = 0;
5616 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
5618 } else if (param->fp_get_lmv) {
5619 struct lmv_user_md *lum = param->fp_lmv_md;
5622 ret = llapi_file_fget_mdtidx(d, &mdtidx);
5625 lum->lum_magic = LMV_MAGIC_V1;
5626 lum->lum_stripe_count = 0;
5627 lum->lum_stripe_offset = mdtidx;
5630 struct lov_user_md *lmm =
5631 ¶m->fp_lmd->lmd_lmm;
5633 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5635 ostid_set_seq(&lmm->lmm_oi,
5636 FID_SEQ_LOV_DEFAULT);
5637 lmm->lmm_stripe_count = 0;
5638 lmm->lmm_stripe_size = 0;
5639 lmm->lmm_stripe_offset = -1;
5642 } else if (errno == ENODATA && p != -1) {
5643 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5644 llapi_printf(LLAPI_MSG_NORMAL,
5645 "%s has no stripe info\n", path);
5647 } else if (errno == ENOENT) {
5648 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5649 "warning: %s: %s does not exist",
5652 } else if (errno == ENOTTY) {
5654 llapi_error(LLAPI_MSG_ERROR, ret,
5655 "%s: '%s' not on a Lustre fs?",
5660 llapi_error(LLAPI_MSG_ERROR, ret,
5661 "error: %s: %s failed for %s",
5663 "LL_IOC_LOV_GETSTRIPE" :
5664 "IOC_MDC_GETFILESTRIPE", path);
5671 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5672 llapi_lov_dump_user_lmm(param, path, d != -1 ? LDF_IS_DIR : 0);
5675 /* Do not get down anymore? */
5676 if (param->fp_depth == param->fp_max_depth)
5684 int llapi_getstripe(char *path, struct find_param *param)
5686 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5687 cb_get_mdt_index : cb_getstripe,
5688 cb_common_fini, param);
5691 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5692 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5694 char raw[MAX_IOC_BUFLEN] = {'\0'};
5696 struct obd_ioctl_data data = { 0 };
5699 data.ioc_inlbuf1 = (char *)&type;
5700 data.ioc_inllen1 = sizeof(__u32);
5701 data.ioc_inlbuf2 = (char *)&index;
5702 data.ioc_inllen2 = sizeof(__u32);
5703 data.ioc_pbuf1 = (char *)stat_buf;
5704 data.ioc_plen1 = sizeof(struct obd_statfs);
5705 data.ioc_pbuf2 = (char *)uuid_buf;
5706 data.ioc_plen2 = sizeof(struct obd_uuid);
5708 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5710 llapi_error(LLAPI_MSG_ERROR, rc,
5711 "%s: error packing ioctl data", __func__);
5715 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5717 return rc < 0 ? -errno : 0;
5720 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5721 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5726 fd = open(path, O_RDONLY);
5729 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5732 * If we can't even open a file on the filesystem (e.g. with
5733 * -ESHUTDOWN), force caller to exit or it will loop forever.
5738 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5745 #define MAX_STRING_SIZE 128
5747 int llapi_ping(char *obd_type, char *obd_name)
5749 int flags = O_RDONLY;
5750 char buf[1] = { 0 };
5754 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5755 obd_type, obd_name);
5759 fd = open(path.gl_pathv[0], flags);
5761 if (errno == EACCES && flags == O_RDONLY) {
5766 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5771 if (flags == O_RDONLY)
5772 rc = read(fd, buf, sizeof(buf));
5774 rc = write(fd, buf, sizeof(buf));
5782 cfs_free_param_data(&path);
5786 int llapi_target_iterate(int type_num, char **obd_type,
5787 void *args, llapi_cb_t cb)
5793 for (i = 0; i < type_num; i++) {
5796 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5800 for (j = 0; j < param.gl_pathc; j++) {
5801 char obd_uuid[UUID_MAX + 1];
5805 fp = fopen(param.gl_pathv[j], "r");
5808 llapi_error(LLAPI_MSG_ERROR, rc,
5809 "error: opening '%s'",
5814 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5816 llapi_error(LLAPI_MSG_ERROR, rc,
5817 "error: reading '%s'",
5822 /* Extract the obd_name from the sysfs path.
5823 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5825 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5831 /* skip /fs/lustre/'obd_type'/ */
5832 obd_name += strlen(obd_type[i]) + 12;
5833 /* chop off after obd_name */
5834 ptr = strrchr(obd_name, '/');
5838 cb(obd_type[i], obd_name, obd_uuid, args);
5847 cfs_free_param_data(¶m);
5851 static void do_target_check(char *obd_type_name, char *obd_name,
5852 char *obd_uuid, void *args)
5856 rc = llapi_ping(obd_type_name, obd_name);
5858 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5860 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5862 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5865 int llapi_target_check(int type_num, char **obd_type, char *dir)
5867 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5870 #undef MAX_STRING_SIZE
5872 /* Is this a lustre fs? */
5873 int llapi_is_lustre_mnttype(const char *type)
5875 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
5878 /* Is this a lustre client fs? */
5879 int llapi_is_lustre_mnt(struct mntent *mnt)
5881 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5882 strstr(mnt->mnt_fsname, ":/") != NULL);
5885 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5887 char fsname[PATH_MAX + 1];
5891 rc = llapi_search_fsname(mnt, fsname);
5895 root = open(mnt, O_RDONLY | O_DIRECTORY);
5898 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
5902 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
5905 if (rc == -ENOENT && LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd))
5906 llapi_error(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, rc,
5907 "Cannot find pool '%s'", qctl->qc_poolname);
5913 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
5918 root = open(mnt, O_RDONLY | O_DIRECTORY);
5921 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
5925 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
5928 llapi_error(LLAPI_MSG_ERROR, rc,
5929 "ioctl on %s for getting connect flags failed", mnt);
5936 * Flush cached pages from all clients.
5938 * \param fd File descriptor
5942 int llapi_file_flush(int fd)
5946 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);