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_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
404 int stripe_count, int stripe_pattern)
406 static int page_size;
409 if (page_size == 0) {
411 * 64 KB is the largest common page size (on ia64/PPC/ARM),
412 * but check the local page size just in case. The page_size
413 * will not change for the lifetime of this process at least.
415 page_size = LOV_MIN_STRIPE_SIZE;
416 if (getpagesize() > page_size) {
417 page_size = getpagesize();
418 llapi_err_noerrno(LLAPI_MSG_WARN,
419 "warning: page size (%u) larger than expected (%u)",
420 page_size, LOV_MIN_STRIPE_SIZE);
423 if (!llapi_stripe_size_is_aligned(stripe_size)) {
425 llapi_error(LLAPI_MSG_ERROR, rc,
426 "error: bad stripe_size %llu, must be an even multiple of %d bytes",
427 (unsigned long long)stripe_size, page_size);
430 if (!llapi_stripe_index_is_valid(stripe_offset)) {
432 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
436 if (!llapi_stripe_count_is_valid(stripe_count)) {
438 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
442 if (llapi_stripe_size_is_too_big(stripe_size)) {
444 llapi_error(LLAPI_MSG_ERROR, rc,
445 "error: stripe size '%llu' over 4GB limit",
446 (unsigned long long)stripe_size);
455 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
460 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
462 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
466 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
468 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
473 if (!llapi_dir_hash_type_is_valid(hash_type)) {
475 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
483 * Trim a trailing newline from a string, if it exists.
485 int llapi_chomp_string(char *buf)
501 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
503 static int get_param_lov(const char *path, const char *param,
504 char *buf, size_t buf_size)
506 struct obd_uuid uuid;
509 rc = llapi_file_get_lov_uuid(path, &uuid);
513 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
518 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
520 static int get_param_lmv(const char *path, const char *param,
521 char *buf, size_t buf_size)
523 struct obd_uuid uuid;
526 rc = llapi_file_get_lmv_uuid(path, &uuid);
530 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
534 static int get_mds_md_size(const char *path)
536 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
539 * Rather than open the file and do the ioctl to get the
540 * instance name and close the file and search for the param
541 * file and open the param file and read the param file and
542 * parse the value and close the param file, let's just return
543 * a large enough value. It's 2020, RAM is cheap and this is
547 if (md_size < XATTR_SIZE_MAX)
548 md_size = XATTR_SIZE_MAX;
553 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
555 return get_param_lmv(path, "uuid", buf, bufsize);
559 * Open a Lustre file.
561 * \param name the name of the file to be opened
562 * \param flags access mode, see flags in open(2)
563 * \param mode permission of the file if it is created, see mode in open(2)
564 * \param param stripe pattern of the newly created file
566 * \retval file descriptor of opened file
567 * \retval negative errno on failure
569 int llapi_file_open_param(const char *name, int flags, mode_t mode,
570 const struct llapi_stripe_param *param)
572 char fsname[MAX_OBD_NAME + 1] = { 0 };
573 char *pool_name = param->lsp_pool;
574 struct lov_user_md *lum = NULL;
575 size_t lum_size = sizeof(*lum);
578 /* Make sure we are on a Lustre file system */
579 rc = llapi_search_fsname(name, fsname);
581 llapi_error(LLAPI_MSG_ERROR, rc,
582 "'%s' is not on a Lustre filesystem",
587 /* Check if the stripe pattern is sane. */
588 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
589 param->lsp_stripe_offset,
590 param->lsp_stripe_count,
591 param->lsp_stripe_pattern);
595 /* Make sure we have a good pool */
596 if (pool_name != NULL) {
598 * in case user gives the full pool name <fsname>.<poolname>,
601 char *ptr = strchr(pool_name, '.');
605 if (strcmp(pool_name, fsname) != 0) {
607 llapi_err_noerrno(LLAPI_MSG_ERROR,
608 "Pool '%s' is not on filesystem '%s'",
615 /* Make sure the pool exists and is non-empty */
616 rc = llapi_search_ost(fsname, pool_name, NULL);
618 char *err = rc == 0 ? "has no OSTs" : "does not exist";
620 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
621 fsname, pool_name, err);
625 lum_size = sizeof(struct lov_user_md_v3);
628 /* sanity check of target list */
629 if (param->lsp_is_specific) {
630 char ostname[MAX_OBD_NAME + 64];
634 for (i = 0; i < param->lsp_stripe_count; i++) {
635 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
636 fsname, param->lsp_osts[i]);
637 rc = llapi_search_ost(fsname, pool_name, ostname);
642 llapi_error(LLAPI_MSG_ERROR, rc,
643 "%s: cannot find OST %s in %s",
650 /* Make sure stripe offset is in OST list. */
651 if (param->lsp_osts[i] == param->lsp_stripe_offset)
655 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
656 "%s: stripe offset '%d' is not in the target list",
657 __func__, param->lsp_stripe_offset);
661 lum_size = lov_user_md_size(param->lsp_stripe_count,
662 LOV_USER_MAGIC_SPECIFIC);
665 lum = calloc(1, lum_size);
670 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
672 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
673 flags = O_DIRECTORY | O_RDONLY;
680 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
685 /* Initialize IOCTL striping pattern structure */
686 lum->lmm_magic = LOV_USER_MAGIC_V1;
687 lum->lmm_pattern = param->lsp_stripe_pattern;
688 lum->lmm_stripe_size = param->lsp_stripe_size;
689 lum->lmm_stripe_count = param->lsp_stripe_count;
690 lum->lmm_stripe_offset = param->lsp_stripe_offset;
691 if (pool_name != NULL) {
692 struct lov_user_md_v3 *lumv3 = (void *)lum;
694 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
695 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
697 if (param->lsp_is_specific) {
698 struct lov_user_md_v3 *lumv3 = (void *)lum;
701 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
702 if (pool_name == NULL) {
704 * LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
705 * OST list, therefore if pool is not specified we have
706 * to pack a null pool name for placeholder.
708 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
711 for (i = 0; i < param->lsp_stripe_count; i++)
712 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
715 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
716 char errmsg[512] = "stripe already set";
719 if (errno != EEXIST && errno != EALREADY)
720 strncpy(errmsg, strerror(errno), sizeof(errmsg) - 1);
721 if (rc == -EREMOTEIO)
722 snprintf(errmsg, sizeof(errmsg),
723 "inactive OST among your specified %d OST(s)",
724 param->lsp_stripe_count);
726 llapi_err_noerrno(LLAPI_MSG_ERROR,
727 "setstripe error for '%s': %s", name, errmsg);
738 int llapi_file_open_pool(const char *name, int flags, int mode,
739 unsigned long long stripe_size, int stripe_offset,
740 int stripe_count, int stripe_pattern, char *pool_name)
742 const struct llapi_stripe_param param = {
743 .lsp_stripe_size = stripe_size,
744 .lsp_stripe_count = stripe_count,
745 .lsp_stripe_pattern = stripe_pattern,
746 .lsp_stripe_offset = stripe_offset,
747 .lsp_pool = pool_name
749 return llapi_file_open_param(name, flags, mode, ¶m);
752 int llapi_file_open(const char *name, int flags, int mode,
753 unsigned long long stripe_size, int stripe_offset,
754 int stripe_count, int stripe_pattern)
756 return llapi_file_open_pool(name, flags, mode, stripe_size,
757 stripe_offset, stripe_count,
758 stripe_pattern, NULL);
761 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
762 __u32 flags, char *foreign_lov)
765 struct lov_foreign_md *lfm;
768 if (foreign_lov == NULL) {
770 llapi_error(LLAPI_MSG_ERROR, rc,
771 "foreign LOV EA content must be provided");
775 len = strlen(foreign_lov);
776 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
779 llapi_error(LLAPI_MSG_ERROR, rc,
780 "foreign LOV EA size %zu (must be 0 < len < %zu)",
781 len, XATTR_SIZE_MAX -
782 offsetof(struct lov_foreign_md, lfm_value));
786 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
789 llapi_error(LLAPI_MSG_ERROR, rc,
790 "failed to allocate lov_foreign_md");
794 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
797 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
801 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
802 lfm->lfm_length = len;
803 lfm->lfm_type = type;
804 lfm->lfm_flags = flags;
805 memcpy(lfm->lfm_value, foreign_lov, len);
807 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
808 char *errmsg = "stripe already set";
812 errmsg = "not on a Lustre filesystem";
813 else if (errno == EEXIST || errno == EALREADY)
814 errmsg = "stripe already set";
816 errmsg = strerror(errno);
818 llapi_err_noerrno(LLAPI_MSG_ERROR,
819 "setstripe error for '%s': %s", name, errmsg);
835 int llapi_file_create(const char *name, unsigned long long stripe_size,
836 int stripe_offset, int stripe_count, int stripe_pattern)
840 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
841 stripe_offset, stripe_count, stripe_pattern,
850 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
851 int stripe_offset, int stripe_count,
852 int stripe_pattern, char *pool_name)
856 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
857 stripe_offset, stripe_count, stripe_pattern,
866 static int verify_dir_param(const char *name,
867 const struct llapi_stripe_param *param)
869 char fsname[MAX_OBD_NAME + 1] = { 0 };
870 char *pool_name = param->lsp_pool;
873 /* Make sure we are on a Lustre file system */
874 rc = llapi_search_fsname(name, fsname);
876 llapi_error(LLAPI_MSG_ERROR, rc,
877 "'%s' is not on a Lustre filesystem",
882 /* Check if the stripe pattern is sane. */
883 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
884 param->lsp_stripe_count,
885 param->lsp_stripe_pattern);
889 /* Make sure we have a good pool */
890 if (pool_name != NULL) {
892 * in case user gives the full pool name <fsname>.<poolname>,
895 char *ptr = strchr(pool_name, '.');
899 if (strcmp(pool_name, fsname) != 0) {
901 llapi_err_noerrno(LLAPI_MSG_ERROR,
902 "Pool '%s' is not on filesystem '%s'",
909 /* Make sure the pool exists and is non-empty */
910 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
912 char *err = rc == 0 ? "has no OSTs" : "does not exist";
914 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
915 fsname, pool_name, err);
920 /* sanity check of target list */
921 if (param->lsp_is_specific) {
922 char mdtname[MAX_OBD_NAME + 64];
926 for (i = 0; i < param->lsp_stripe_count; i++) {
927 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
928 fsname, param->lsp_tgts[i]);
929 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
934 llapi_error(LLAPI_MSG_ERROR, rc,
935 "%s: cannot find MDT %s in %s",
942 /* Make sure stripe offset is in MDT list. */
943 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
947 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
948 "%s: stripe offset '%d' is not in the target list",
949 __func__, param->lsp_stripe_offset);
957 static inline void param2lmu(struct lmv_user_md *lmu,
958 const struct llapi_stripe_param *param)
960 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
962 lmu->lum_stripe_count = param->lsp_stripe_count;
963 lmu->lum_stripe_offset = param->lsp_stripe_offset;
964 lmu->lum_hash_type = param->lsp_stripe_pattern;
965 if (param->lsp_pool != NULL)
966 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
967 if (param->lsp_is_specific) {
970 for (i = 0; i < param->lsp_stripe_count; i++)
971 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
975 int llapi_dir_set_default_lmv(const char *name,
976 const struct llapi_stripe_param *param)
978 struct lmv_user_md lmu = { 0 };
982 rc = verify_dir_param(name, param);
986 /* TODO: default lmv doesn't support specific targets yet */
987 if (param->lsp_is_specific)
990 param2lmu(&lmu, param);
992 fd = open(name, O_DIRECTORY | O_RDONLY);
995 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
999 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
1001 char *errmsg = "stripe already set";
1004 if (errno != EEXIST && errno != EALREADY)
1005 errmsg = strerror(errno);
1007 llapi_err_noerrno(LLAPI_MSG_ERROR,
1008 "default dirstripe error on '%s': %s",
1015 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
1016 int stripe_count, int stripe_pattern,
1017 const char *pool_name)
1019 const struct llapi_stripe_param param = {
1020 .lsp_stripe_count = stripe_count,
1021 .lsp_stripe_offset = stripe_offset,
1022 .lsp_stripe_pattern = stripe_pattern,
1023 .lsp_pool = (char *)pool_name
1026 return llapi_dir_set_default_lmv(name, ¶m);
1030 * Create a Lustre directory.
1032 * \param name the name of the directory to be created
1033 * \param mode permission of the file if it is created, see mode in open(2)
1034 * \param param stripe pattern of the newly created directory
1036 * \retval 0 on success
1037 * \retval negative errno on failure
1039 int llapi_dir_create(const char *name, mode_t mode,
1040 const struct llapi_stripe_param *param)
1042 struct lmv_user_md *lmu = NULL;
1044 struct obd_ioctl_data data = { 0 };
1047 char *dirpath = NULL;
1048 char *namepath = NULL;
1053 rc = verify_dir_param(name, param);
1057 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1058 param->lsp_is_specific ?
1059 LMV_USER_MAGIC_SPECIFIC :
1062 lmu = calloc(1, lmu_size);
1066 dirpath = strdup(name);
1072 namepath = strdup(name);
1079 param2lmu(lmu, param);
1081 filename = basename(namepath);
1082 dir = dirname(dirpath);
1084 data.ioc_inlbuf1 = (char *)filename;
1085 data.ioc_inllen1 = strlen(filename) + 1;
1086 data.ioc_inlbuf2 = (char *)lmu;
1087 data.ioc_inllen2 = lmu_size;
1088 data.ioc_type = mode;
1089 if (param->lsp_is_create)
1090 /* borrow obdo1.o_flags to store this flag */
1091 data.ioc_obdo1.o_flags = OBD_FL_OBDMDEXISTS;
1092 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1094 llapi_error(LLAPI_MSG_ERROR, rc,
1095 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1100 fd = open(dir, O_DIRECTORY | O_RDONLY);
1103 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1107 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1108 char *errmsg = "stripe already set";
1111 if (errno != EEXIST && errno != EALREADY)
1112 errmsg = strerror(errno);
1114 llapi_err_noerrno(LLAPI_MSG_ERROR,
1115 "dirstripe error on '%s': %s", name, errmsg);
1126 * Create a foreign directory.
1128 * \param name the name of the directory to be created
1129 * \param mode permission of the file if it is created, see mode in open(2)
1130 * \param type foreign type to be set in LMV EA
1131 * \param flags foreign flags to be set in LMV EA
1132 * \param value foreign pattern to be set in LMV EA
1134 * \retval 0 on success
1135 * \retval negative errno on failure
1137 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1138 __u32 flags, const char *value)
1140 struct lmv_foreign_md *lfm = NULL;
1141 size_t lfm_size, len;
1142 struct obd_ioctl_data data = { 0 };
1145 char *dirpath = NULL;
1146 char *namepath = NULL;
1151 len = strlen(value);
1152 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1155 llapi_error(LLAPI_MSG_ERROR, rc,
1156 "invalid LOV EA length %zu (must be 0 < len < %zu)",
1157 len, XATTR_SIZE_MAX -
1158 offsetof(struct lmv_foreign_md, lfm_value));
1161 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1162 lfm = calloc(1, lfm_size);
1166 dirpath = strdup(name);
1172 namepath = strdup(name);
1179 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1180 lfm->lfm_length = len;
1181 lfm->lfm_type = type;
1182 lfm->lfm_flags = flags;
1183 memcpy(lfm->lfm_value, value, len);
1185 filename = basename(namepath);
1186 dir = dirname(dirpath);
1188 data.ioc_inlbuf1 = (char *)filename;
1189 data.ioc_inllen1 = strlen(filename) + 1;
1190 data.ioc_inlbuf2 = (char *)lfm;
1191 data.ioc_inllen2 = lfm_size;
1192 data.ioc_type = mode;
1193 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1195 llapi_error(LLAPI_MSG_ERROR, rc,
1196 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1201 fd = open(dir, O_DIRECTORY | O_RDONLY);
1204 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1208 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1209 char *errmsg = "stripe already set";
1212 if (errno != EEXIST && errno != EALREADY)
1213 errmsg = strerror(errno);
1215 llapi_err_noerrno(LLAPI_MSG_ERROR,
1216 "dirstripe error on '%s': %s", name, errmsg);
1226 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1227 int stripe_count, int stripe_pattern,
1228 const char *pool_name)
1230 const struct llapi_stripe_param param = {
1231 .lsp_stripe_count = stripe_count,
1232 .lsp_stripe_offset = stripe_offset,
1233 .lsp_stripe_pattern = stripe_pattern,
1234 .lsp_pool = (char *)pool_name
1237 return llapi_dir_create(name, mode, ¶m);
1241 * Find the fsname, the full path, and/or an open fd.
1242 * Either the fsname or path must not be NULL
1244 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1247 char buf[PATH_MAX], mntdir[PATH_MAX];
1248 char *ptr, *ptr_end;
1250 int idx = 0, mntlen = 0, fd;
1252 int fsnamelen, mountlen;
1254 /* get the mount point */
1255 fp = setmntent(PROC_MOUNTS, "r");
1258 llapi_error(LLAPI_MSG_ERROR, rc,
1259 "cannot retrieve filesystem mount point");
1263 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1266 if (!llapi_is_lustre_mnt(&mnt))
1269 if ((want & WANT_INDEX) && (idx++ != index))
1272 mntlen = strlen(mnt.mnt_dir);
1273 ptr = strchr(mnt.mnt_fsname, '/');
1274 while (ptr && *ptr == '/')
1277 * thanks to the call to llapi_is_lustre_mnt() above,
1278 * we are sure that mnt.mnt_fsname contains ":/",
1279 * so ptr should never be NULL
1284 while (*ptr_end != '/' && *ptr_end != '\0')
1287 /* Check the fsname for a match, if given */
1288 mountlen = ptr_end - ptr;
1289 if (!(want & WANT_FSNAME) && fsname != NULL &&
1290 (fsnamelen = strlen(fsname)) > 0 &&
1291 (fsnamelen != mountlen ||
1292 (strncmp(ptr, fsname, mountlen) != 0)))
1295 /* If the path isn't set return the first one we find */
1296 if (path == NULL || strlen(path) == 0) {
1297 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1298 mntdir[sizeof(mntdir) - 1] = '\0';
1299 if ((want & WANT_FSNAME) && fsname != NULL) {
1300 strncpy(fsname, ptr, mountlen);
1301 fsname[mountlen] = '\0';
1305 /* Otherwise find the longest matching path */
1306 } else if ((strlen(path) >= mntlen) &&
1307 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1308 /* check the path format */
1309 if (strlen(path) > mntlen && path[mntlen] != '/')
1311 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1312 mntdir[sizeof(mntdir) - 1] = '\0';
1313 if ((want & WANT_FSNAME) && fsname != NULL) {
1314 strncpy(fsname, ptr, mountlen);
1315 fsname[mountlen] = '\0';
1325 if ((want & WANT_PATH) && path != NULL) {
1326 strncpy(path, mntdir, mntlen);
1327 path[mntlen] = '\0';
1329 if (want & WANT_FD) {
1330 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1333 llapi_error(LLAPI_MSG_ERROR, rc,
1334 "cannot open '%s'", mntdir);
1340 } else if (want & WANT_ERROR)
1341 llapi_err_noerrno(LLAPI_MSG_ERROR,
1342 "'%s' not on a mounted Lustre filesystem",
1343 (want & WANT_PATH) ? fsname : path);
1348 * search lustre mounts
1350 * Calling this function will return to the user the mount point, mntdir, and
1351 * the file system name, fsname, if the user passed a buffer to this routine.
1353 * The user inputs are pathname and index. If the pathname is supplied then
1354 * the value of the index will be ignored. The pathname will return data if
1355 * the pathname is located on a lustre mount. Index is used to pick which
1356 * mount point you want in the case of multiple mounted lustre file systems.
1357 * See function lfs_osts in lfs.c for an example of the index use.
1359 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1362 int want = WANT_PATH, idx = -1;
1364 if (!pathname || pathname[0] == '\0') {
1368 strcpy(mntdir, pathname);
1372 want |= WANT_FSNAME;
1373 return get_root_path(want, fsname, NULL, mntdir, idx);
1376 /* Given a path, find the corresponding Lustre fsname */
1377 int llapi_search_fsname(const char *pathname, char *fsname)
1382 path = realpath(pathname, NULL);
1384 char tmp[PATH_MAX - 1];
1390 if (pathname[0] != '/') {
1392 * Need an absolute path, but realpath() only works for
1393 * pathnames that actually exist. We go through the
1394 * extra hurdle of dirname(getcwd() + pathname) in
1395 * case the relative pathname contains ".." in it.
1397 char realpath[PATH_MAX - 1];
1399 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1401 llapi_error(LLAPI_MSG_ERROR, rc,
1402 "cannot get current working directory");
1406 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1407 if (rc >= sizeof(tmp)) {
1409 llapi_error(LLAPI_MSG_ERROR, rc,
1410 "invalid parent path '%s'",
1416 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1417 if (rc >= sizeof(buf)) {
1419 llapi_error(LLAPI_MSG_ERROR, rc,
1420 "invalid path '%s'", pathname);
1423 path = realpath(buf, NULL);
1425 ptr = strrchr(buf, '/');
1427 llapi_error(LLAPI_MSG_ERROR |
1428 LLAPI_MSG_NO_ERRNO, 0,
1429 "cannot resolve path '%s'",
1434 path = realpath(buf, NULL);
1437 llapi_error(LLAPI_MSG_ERROR, rc,
1438 "cannot resolve path '%s'",
1444 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1449 int llapi_search_rootpath(char *pathname, const char *fsname)
1452 * pathname can be used as an argument by get_root_path(),
1453 * clear it for safety
1456 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1460 * Get the list of pool members.
1461 * \param poolname string of format \<fsname\>.\<poolname\>
1462 * \param members caller-allocated array of char*
1463 * \param list_size size of the members array
1464 * \param buffer caller-allocated buffer for storing OST names
1465 * \param buffer_size size of the buffer
1467 * \return number of members retrieved for this pool
1468 * \retval -error failure
1470 int llapi_get_poolmembers(const char *poolname, char **members,
1471 int list_size, char *buffer, int buffer_size)
1473 char fsname[PATH_MAX];
1482 /* name is FSNAME.POOLNAME */
1483 if (strlen(poolname) >= sizeof(fsname))
1486 snprintf(fsname, sizeof(fsname), "%s", poolname);
1487 pool = strchr(fsname, '.');
1494 rc = poolpath(&pathname, fsname, NULL);
1496 llapi_error(LLAPI_MSG_ERROR, rc,
1497 "Lustre filesystem '%s' not found",
1502 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1503 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1504 cfs_free_param_data(&pathname);
1505 if (rc >= sizeof(buf))
1507 fd = fopen(buf, "r");
1510 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1515 while (fgets(buf, sizeof(buf), fd) != NULL) {
1516 if (nb_entries >= list_size) {
1520 buf[sizeof(buf) - 1] = '\0';
1522 tmp = strchr(buf, '\n');
1525 if (used + strlen(buf) + 1 > buffer_size) {
1530 strcpy(buffer + used, buf);
1531 members[nb_entries] = buffer + used;
1532 used += strlen(buf) + 1;
1542 * Get the list of pools in a filesystem.
1543 * \param name filesystem name or path
1544 * \param poollist caller-allocated array of char*
1545 * \param list_size size of the poollist array
1546 * \param buffer caller-allocated buffer for storing pool names
1547 * \param buffer_size size of the buffer
1549 * \return number of pools retrieved for this filesystem
1550 * \retval -error failure
1552 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1553 char *buffer, int buffer_size)
1559 struct dirent *pool;
1561 unsigned int nb_entries = 0;
1562 unsigned int used = 0;
1565 /* initialize output array */
1566 for (i = 0; i < list_size; i++)
1569 /* is name a pathname ? */
1570 ptr = strchr(name, '/');
1572 char fsname_buf[MAXNAMLEN];
1574 /* We will need fsname for printing later */
1575 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1579 ptr = strrchr(fsname_buf, '-');
1583 fsname = strdup(fsname_buf);
1587 /* name is FSNAME */
1588 fsname = strdup(name);
1593 rc = poolpath(&pathname, fsname, NULL);
1595 llapi_error(LLAPI_MSG_ERROR, rc,
1596 "Lustre filesystem '%s' not found", name);
1600 dir = opendir(pathname.gl_pathv[0]);
1603 llapi_error(LLAPI_MSG_ERROR, rc,
1604 "Could not open pool list for '%s'",
1611 pool = readdir(dir);
1617 /* ignore . and .. */
1618 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1621 /* check output bounds */
1622 if (nb_entries >= list_size) {
1624 goto free_dir_no_msg;
1627 /* +2 for '.' and final '\0' */
1628 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1631 goto free_dir_no_msg;
1634 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1635 poollist[nb_entries] = buffer + used;
1636 used += strlen(pool->d_name) + strlen(fsname) + 2;
1642 llapi_error(LLAPI_MSG_ERROR, rc,
1643 "Error reading pool list for '%s'", name);
1645 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1650 cfs_free_param_data(&pathname);
1653 return rc != 0 ? rc : nb_entries;
1656 /* wrapper for lfs.c and obd.c */
1657 int llapi_poollist(const char *name)
1660 * list of pool names (assume that pool count is smaller
1663 char **list, *buffer = NULL, *fsname = (char *)name;
1664 char *poolname = NULL, *tmp = NULL, data[16];
1665 enum param_filter type = FILTER_BY_PATH;
1666 int obdcount, bufsize, rc, nb, i;
1671 if (name[0] != '/') {
1672 fsname = strdup(name);
1676 poolname = strchr(fsname, '.');
1679 type = FILTER_BY_FS_NAME;
1682 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1683 data, sizeof(data));
1686 obdcount = atoi(data);
1689 * Allocate space for each fsname-OST0000_UUID, 1 per OST,
1690 * and also an array to store the pointers for all that
1694 bufsize = sizeof(struct obd_uuid) * obdcount;
1695 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1696 if (buffer == NULL) {
1700 list = (char **) (buffer + bufsize);
1703 /* name is a path or fsname */
1704 nb = llapi_get_poollist(name, list, obdcount,
1707 /* name is a pool name (<fsname>.<poolname>) */
1708 nb = llapi_get_poolmembers(name, list, obdcount,
1712 if (nb == -EOVERFLOW) {
1715 goto retry_get_pools;
1718 for (i = 0; i < nb; i++)
1719 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1720 rc = (nb < 0 ? nb : 0);
1724 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1729 typedef int (semantic_func_t)(char *path, int p, int *d,
1730 void *data, struct dirent64 *de);
1732 #define OBD_NOT_FOUND (-1)
1734 static void find_param_fini(struct find_param *param)
1736 if (param->fp_migrate)
1739 if (param->fp_obd_indexes) {
1740 free(param->fp_obd_indexes);
1741 param->fp_obd_indexes = NULL;
1744 if (param->fp_lmd) {
1745 free(param->fp_lmd);
1746 param->fp_lmd = NULL;
1749 if (param->fp_lmv_md) {
1750 free(param->fp_lmv_md);
1751 param->fp_lmv_md = NULL;
1755 static int common_param_init(struct find_param *param, char *path)
1757 int lum_size = get_mds_md_size(path);
1762 /* migrate has fp_lmv_md initialized outside */
1763 if (param->fp_migrate)
1766 if (lum_size < PATH_MAX + 1)
1767 lum_size = PATH_MAX + 1;
1769 param->fp_lum_size = lum_size;
1770 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1772 if (param->fp_lmd == NULL) {
1773 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1774 "error: allocate %zu bytes for layout failed",
1775 sizeof(lstat_t) + param->fp_lum_size);
1779 param->fp_lmv_stripe_count = 256;
1780 param->fp_lmv_md = calloc(1,
1781 lmv_user_md_size(param->fp_lmv_stripe_count,
1782 LMV_USER_MAGIC_SPECIFIC));
1783 if (param->fp_lmv_md == NULL) {
1784 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1785 "error: allocation of %d bytes for ioctl",
1786 lmv_user_md_size(param->fp_lmv_stripe_count,
1787 LMV_USER_MAGIC_SPECIFIC));
1788 find_param_fini(param);
1792 param->fp_got_uuids = 0;
1793 param->fp_obd_indexes = NULL;
1794 param->fp_obd_index = OBD_NOT_FOUND;
1795 param->fp_mdt_index = OBD_NOT_FOUND;
1799 static int cb_common_fini(char *path, int p, int *dp, void *data,
1800 struct dirent64 *de)
1802 struct find_param *param = data;
1808 /* set errno upon failure */
1809 static int open_parent(const char *path)
1815 path_copy = strdup(path);
1816 if (path_copy == NULL)
1819 parent_path = dirname(path_copy);
1820 parent = open(parent_path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1826 static int cb_get_dirstripe(char *path, int *d, struct find_param *param)
1829 bool did_nofollow = false;
1832 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1833 if (param->fp_get_default_lmv)
1834 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1836 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1838 ret = ioctl(*d, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1840 /* if ENOTTY likely to be a fake symlink, so try again after
1841 * new open() with O_NOFOLLOW, but only once to prevent any
1842 * loop like for the path of a file/dir not on Lustre !!
1844 if (ret < 0 && errno == ENOTTY && !did_nofollow) {
1847 did_nofollow = true;
1848 fd = open(path, O_RDONLY | O_NOFOLLOW);
1850 /* restore original errno */
1855 /* close original fd and set new */
1858 ret2 = ioctl(fd, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1859 if (ret2 < 0 && errno != E2BIG) {
1860 /* restore original errno */
1864 /* LMV is ok or need to handle E2BIG case now */
1868 if (errno == E2BIG && ret != 0) {
1872 /* if foreign LMV case, fake stripes number */
1873 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
1874 struct lmv_foreign_md *lfm;
1876 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1877 if (lfm->lfm_length < XATTR_SIZE_MAX -
1878 offsetof(typeof(*lfm), lfm_value)) {
1879 uint32_t size = lfm->lfm_length +
1880 offsetof(typeof(*lfm), lfm_value);
1882 stripe_count = lmv_foreign_to_md_stripes(size);
1884 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1885 "error: invalid %d foreign size returned from ioctl",
1890 stripe_count = param->fp_lmv_md->lum_stripe_count;
1892 if (stripe_count <= param->fp_lmv_stripe_count)
1895 free(param->fp_lmv_md);
1896 param->fp_lmv_stripe_count = stripe_count;
1897 lmv_size = lmv_user_md_size(stripe_count,
1898 LMV_USER_MAGIC_SPECIFIC);
1899 param->fp_lmv_md = malloc(lmv_size);
1900 if (param->fp_lmv_md == NULL) {
1901 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1902 "error: allocation of %d bytes for ioctl",
1903 lmv_user_md_size(param->fp_lmv_stripe_count,
1904 LMV_USER_MAGIC_SPECIFIC));
1912 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1915 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
1916 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
1917 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
1918 lmd_v2->lmd_stx.stx_uid = st->st_uid;
1919 lmd_v2->lmd_stx.stx_gid = st->st_gid;
1920 lmd_v2->lmd_stx.stx_mode = st->st_mode;
1921 lmd_v2->lmd_stx.stx_ino = st->st_ino;
1922 lmd_v2->lmd_stx.stx_size = st->st_size;
1923 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
1924 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
1925 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
1926 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
1927 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
1928 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
1929 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
1930 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
1931 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
1933 lmd_v2->lmd_flags = 0;
1935 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1937 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
1938 if (lmd_v2->lmd_stx.stx_size)
1939 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
1940 if (lmd_v2->lmd_stx.stx_blocks)
1941 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
1943 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1944 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
1945 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
1950 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
1952 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
1953 struct lov_user_mds_data *lmd_v2 = lmdbuf;
1957 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
1961 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
1964 st = lmd_v1->lmd_st;
1965 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
1966 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
1967 convert_lmd_statx(lmd_v2, &st, false);
1968 lmd_v2->lmd_lmmsize = 0;
1969 lmd_v2->lmd_padding = 0;
1974 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
1975 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1977 struct lov_user_mds_data *lmd = lmdbuf;
1978 static bool use_old_ioctl;
1982 if (parent_fd < 0 && dir_fd < 0)
1984 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1989 * LL_IOC_MDC_GETINFO operates on the current directory inode
1990 * and returns struct lov_user_mds_data, while
1991 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
1993 if (type == GET_LMD_INFO)
1994 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_V1 :
1995 LL_IOC_MDC_GETINFO_V2;
1997 cmd = LL_IOC_LOV_GETSTRIPE;
2000 ret = ioctl(dir_fd, cmd, lmdbuf);
2001 if (ret < 0 && errno == ENOTTY &&
2002 cmd == LL_IOC_MDC_GETINFO_V2) {
2003 cmd = LL_IOC_MDC_GETINFO_V1;
2004 use_old_ioctl = true;
2008 if (cmd == LL_IOC_MDC_GETINFO_V1 && !ret)
2009 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2011 if (ret < 0 && errno == ENOTTY && type == GET_LMD_STRIPE) {
2014 /* retry ioctl() after new open() with O_NOFOLLOW
2015 * just in case it could be a fake symlink
2016 * need using a new open() as dir_fd is being closed
2020 dir_fd2 = open(path, O_RDONLY | O_NDELAY | O_NOFOLLOW);
2022 /* return original error */
2025 ret = ioctl(dir_fd2, cmd, lmdbuf);
2026 /* pass new errno or success back to caller */
2032 } else if (parent_fd >= 0) {
2033 const char *fname = strrchr(path, '/');
2036 * IOC_MDC_GETFILEINFO takes as input the filename (relative to
2037 * the parent directory) and returns struct lov_user_mds_data,
2038 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2040 * This avoids opening, locking, and closing each file on the
2041 * client if that is not needed. Multiple of these ioctl() can
2042 * be done on the parent dir with a single open for all
2043 * files in that directory, and it also doesn't pollute the
2044 * client dcache with millions of dentries when traversing
2045 * a large filesystem.
2047 fname = (fname == NULL ? path : fname + 1);
2049 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2052 else if (ret >= lmdlen || ret++ == 0)
2055 if (type == GET_LMD_INFO)
2056 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_V1 :
2057 IOC_MDC_GETFILEINFO_V2;
2059 cmd = IOC_MDC_GETFILESTRIPE;
2062 ret = ioctl(parent_fd, cmd, lmdbuf);
2063 if (ret < 0 && errno == ENOTTY &&
2064 cmd == IOC_MDC_GETFILEINFO_V2) {
2065 cmd = IOC_MDC_GETFILEINFO_V1;
2066 use_old_ioctl = true;
2067 goto retry_getfileinfo;
2070 if (cmd == IOC_MDC_GETFILEINFO_V1 && !ret)
2071 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2075 if (ret && type == GET_LMD_INFO) {
2076 if (errno == ENOTTY) {
2080 * ioctl is not supported, it is not a lustre fs.
2081 * Do the regular lstat(2) instead.
2083 ret = lstat_f(path, &st);
2086 llapi_error(LLAPI_MSG_ERROR, ret,
2087 "error: %s: lstat failed for %s",
2091 convert_lmd_statx(lmd, &st, true);
2093 * It may be wrong to set use_old_ioctl with true as
2094 * the file is not a lustre fs. So reset it with false
2097 use_old_ioctl = false;
2098 } else if (errno == ENOENT) {
2100 llapi_error(LLAPI_MSG_WARN, ret,
2101 "warning: %s does not exist", path);
2102 } else if (errno != EISDIR && errno != ENODATA) {
2104 llapi_error(LLAPI_MSG_ERROR, ret,
2105 "%s ioctl failed for %s.",
2106 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2107 "IOC_MDC_GETFILEINFO", path);
2114 static int llapi_semantic_traverse(char *path, int size, int parent,
2115 semantic_func_t sem_init,
2116 semantic_func_t sem_fini, void *data,
2117 struct dirent64 *de)
2119 struct find_param *param = (struct find_param *)data;
2120 struct dirent64 *dent;
2121 int len, ret, d, p = -1;
2127 d = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
2128 /* if an invalid fake dir symlink, opendir() will return EINVAL
2129 * instead of ENOTDIR. If a valid but dangling faked or real file/dir
2130 * symlink ENOENT will be returned. For a valid/resolved fake or real
2131 * file symlink ENOTDIR will be returned as for a regular file.
2132 * opendir() will be successful for a valid and resolved fake or real
2133 * dir simlink or a regular dir.
2135 if (d == -1 && errno != ENOTDIR && errno != EINVAL && errno != ENOENT) {
2137 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2140 } else if (d == -1) {
2141 if (errno == ENOENT || errno == EINVAL) {
2142 int old_errno = errno;
2144 /* try to open with O_NOFOLLOW this will help
2145 * differentiate fake vs real symlinks
2146 * it is ok to not use O_DIRECTORY with O_RDONLY
2147 * and it will prevent the need to deal with ENOTDIR
2148 * error, instead of ELOOP, being returned by recent
2149 * kernels for real symlinks
2151 d = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2152 /* if a dangling real symlink should return ELOOP, or
2153 * again ENOENT if really non-existing path, or E...??
2154 * So return original error. If success or ENOTDIR, path
2155 * is likely to be a fake dir/file symlink, so continue
2165 if (parent == -1 && d == -1) {
2166 /* Open the parent dir. */
2167 p = open_parent(path);
2173 } else { /* d != -1 */
2176 /* try to reopen dir with O_NOFOLLOW just in case of a foreign
2179 d2 = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2184 /* continue with d */
2190 ret = sem_init(path, (parent != -1) ? parent : p, &d, data, de);
2200 /* ENOTDIR if fake symlink, do not consider it as an error */
2201 if (errno != ENOTDIR)
2202 llapi_error(LLAPI_MSG_ERROR, errno,
2203 "fdopendir() failed");
2210 while ((dent = readdir64(dir)) != NULL) {
2213 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2217 if ((len + dent->d_reclen + 2) > size) {
2218 llapi_err_noerrno(LLAPI_MSG_ERROR,
2219 "error: %s: string buffer too small for %s",
2224 strcat(path, dent->d_name);
2226 if (dent->d_type == DT_UNKNOWN) {
2227 struct lov_user_mds_data *lmd = param->fp_lmd;
2229 rc = get_lmd_info_fd(path, d, -1, param->fp_lmd,
2230 param->fp_lum_size, GET_LMD_INFO);
2232 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2239 switch (dent->d_type) {
2241 llapi_err_noerrno(LLAPI_MSG_ERROR,
2242 "error: %s: '%s' is UNKNOWN type %d",
2243 __func__, dent->d_name, dent->d_type);
2246 rc = llapi_semantic_traverse(path, size, d, sem_init,
2247 sem_fini, data, dent);
2248 if (rc != 0 && ret == 0)
2254 rc = sem_init(path, d, NULL, data, dent);
2255 if (rc < 0 && ret == 0) {
2260 if (sem_fini && rc == 0)
2261 sem_fini(path, d, NULL, data, dent);
2269 sem_fini(path, parent, &d, data, de);
2282 static int param_callback(char *path, semantic_func_t sem_init,
2283 semantic_func_t sem_fini, struct find_param *param)
2285 int ret, len = strlen(path);
2288 if (len > PATH_MAX) {
2290 llapi_error(LLAPI_MSG_ERROR, ret,
2291 "Path name '%s' is too long", path);
2295 buf = (char *)malloc(2 * PATH_MAX);
2299 snprintf(buf, PATH_MAX + 1, "%s", path);
2300 ret = common_param_init(param, buf);
2304 param->fp_depth = 0;
2306 ret = llapi_semantic_traverse(buf, 2 * PATH_MAX, -1, sem_init,
2307 sem_fini, param, NULL);
2309 find_param_fini(param);
2311 return ret < 0 ? ret : 0;
2314 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2318 rc = ioctl(fd, OBD_IOC_GETDTNAME, lov_name);
2319 if (rc && errno == ENOTTY)
2320 rc = ioctl(fd, OBD_IOC_GETNAME_OLD, lov_name);
2323 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2329 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2333 rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2336 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2342 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2346 /* do not follow faked symlinks */
2347 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
2349 /* real symlink should have failed with ELOOP so retry without
2350 * O_NOFOLLOW just in case
2352 fd = open(path, O_RDONLY | O_NONBLOCK);
2355 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'",
2361 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2367 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2371 fd = open(path, O_RDONLY | O_NONBLOCK);
2374 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2378 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2390 * If uuidp is NULL, return the number of available obd uuids.
2391 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2392 * there are more OSTs than allocated to uuidp, then an error is returned with
2393 * the ost_count set to number of available obd uuids.
2395 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2396 int *ost_count, enum tgt_type type)
2398 char buf[PATH_MAX], format[32];
2399 int rc = 0, index = 0;
2400 struct obd_uuid name;
2404 /* Get the lov name */
2405 if (type == LOV_TYPE)
2406 rc = llapi_file_fget_lov_uuid(fd, &name);
2408 rc = llapi_file_fget_lmv_uuid(fd, &name);
2412 /* Now get the ost uuids */
2413 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2414 FILTER_BY_EXACT, "target_obd", ¶m);
2418 fp = fopen(param.gl_pathv[0], "r");
2421 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2426 snprintf(format, sizeof(format),
2427 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2428 while (fgets(buf, sizeof(buf), fp) != NULL) {
2429 if (uuidp && (index < *ost_count)) {
2430 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2438 if (uuidp && (index > *ost_count))
2443 cfs_free_param_data(¶m);
2447 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2449 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2452 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2457 root = open(mnt, O_RDONLY | O_DIRECTORY);
2460 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2465 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2474 * Check if user specified value matches a real uuid. Ignore _UUID,
2475 * -osc-4ba41334, other trailing gunk in comparison.
2476 * @param real_uuid ends in "_UUID"
2477 * @param search_uuid may or may not end in "_UUID"
2479 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2481 int cmplen = strlen(real_uuid);
2482 int searchlen = strlen(search_uuid);
2484 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2486 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2490 * The UUIDs may legitimately be different lengths, if
2491 * the system was upgraded from an older version.
2493 if (cmplen != searchlen)
2496 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2500 * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2501 * returned in param->fp_obd_index
2503 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2505 struct obd_uuid obd_uuid;
2512 if (param->fp_got_uuids)
2515 /* Get the lov/lmv name */
2516 if (param->fp_get_lmv)
2517 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2519 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2521 if (rc != -ENOTTY) {
2522 llapi_error(LLAPI_MSG_ERROR, rc,
2523 "error: can't get %s name: %s",
2524 param->fp_get_lmv ? "lmv" : "lov",
2532 param->fp_got_uuids = 1;
2534 /* Now get the ost uuids */
2535 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2536 obd_uuid.uuid, FILTER_BY_EXACT,
2537 "target_obd", ¶m_data);
2541 fp = fopen(param_data.gl_pathv[0], "r");
2544 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2545 param_data.gl_pathv[0]);
2549 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2550 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2551 param->fp_get_lmv ? "MDTS" : "OBDS");
2553 snprintf(format, sizeof(format),
2554 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2555 while (fgets(buf, sizeof(buf), fp) != NULL) {
2558 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2561 if (param->fp_obd_uuid) {
2562 if (llapi_uuid_match(obd_uuid.uuid,
2563 param->fp_obd_uuid->uuid)) {
2564 param->fp_obd_index = index;
2567 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2568 /* Print everything */
2569 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2572 param->fp_obds_printed = 1;
2576 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2577 llapi_err_noerrno(LLAPI_MSG_ERROR,
2578 "error: %s: unknown obduuid: %s",
2579 __func__, param->fp_obd_uuid->uuid);
2583 cfs_free_param_data(¶m_data);
2588 * In this case, param->fp_obd_uuid will be an array of obduuids and
2589 * obd index for all these obduuids will be returned in
2590 * param->fp_obd_indexes
2592 static int setup_indexes(int d, char *path, struct obd_uuid *obduuids,
2593 int num_obds, int **obdindexes, int *obdindex,
2596 int ret, obdcount, obd_valid = 0, obdnum;
2598 struct obd_uuid *uuids = NULL;
2602 if (type == LOV_TYPE)
2603 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2605 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2609 obdcount = atoi(buf);
2610 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2615 ret = llapi_get_target_uuids(d, uuids, &obdcount, type);
2617 if (ret == -EOVERFLOW) {
2618 struct obd_uuid *uuids_temp;
2620 uuids_temp = realloc(uuids, obdcount *
2621 sizeof(struct obd_uuid));
2622 if (uuids_temp != NULL) {
2624 goto retry_get_uuids;
2629 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2633 indexes = malloc(num_obds * sizeof(*obdindex));
2634 if (indexes == NULL) {
2639 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2642 /* The user may have specified a simple index */
2643 i = strtol(obduuids[obdnum].uuid, &end, 0);
2644 if (end && *end == '\0' && i < obdcount) {
2645 indexes[obdnum] = i;
2648 for (i = 0; i < obdcount; i++) {
2649 if (llapi_uuid_match(uuids[i].uuid,
2650 obduuids[obdnum].uuid)) {
2651 indexes[obdnum] = i;
2657 if (i >= obdcount) {
2658 indexes[obdnum] = OBD_NOT_FOUND;
2659 llapi_err_noerrno(LLAPI_MSG_ERROR,
2660 "invalid obduuid '%s'",
2661 obduuids[obdnum].uuid);
2667 *obdindex = OBD_NOT_FOUND;
2669 *obdindex = obd_valid;
2671 *obdindexes = indexes;
2679 static int setup_target_indexes(int d, char *path, struct find_param *param)
2683 if (param->fp_mdt_uuid) {
2684 ret = setup_indexes(d, path, param->fp_mdt_uuid,
2686 ¶m->fp_mdt_indexes,
2687 ¶m->fp_mdt_index, LMV_TYPE);
2692 if (param->fp_obd_uuid) {
2693 ret = setup_indexes(d, path, param->fp_obd_uuid,
2695 ¶m->fp_obd_indexes,
2696 ¶m->fp_obd_index, LOV_TYPE);
2701 param->fp_got_uuids = 1;
2706 int llapi_ostlist(char *path, struct find_param *param)
2711 fd = open(path, O_RDONLY | O_DIRECTORY);
2715 ret = setup_obd_uuid(fd, path, param);
2722 * Tries to determine the default stripe attributes for a given filesystem. The
2723 * filesystem to check should be specified by fsname, or will be determined
2726 static int sattr_get_defaults(const char *const fsname,
2727 unsigned int *scount,
2728 unsigned int *ssize,
2729 unsigned int *soffset)
2735 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2736 "stripecount", val, sizeof(val));
2739 *scount = atoi(val);
2743 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2744 "stripesize", val, sizeof(val));
2751 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2752 "stripeoffset", val, sizeof(val));
2755 *soffset = atoi(val);
2762 * Tries to gather the default stripe attributes for a given filesystem. If
2763 * the attributes can be determined, they are cached for easy retreival the
2764 * next time they are needed. Only a single filesystem's attributes are
2767 int sattr_cache_get_defaults(const char *const fsname,
2768 const char *const pathname, unsigned int *scount,
2769 unsigned int *ssize, unsigned int *soffset)
2772 char fsname[PATH_MAX + 1];
2773 unsigned int stripecount;
2774 unsigned int stripesize;
2775 unsigned int stripeoffset;
2781 char fsname_buf[PATH_MAX + 1];
2782 unsigned int tmp[3];
2784 if (fsname == NULL) {
2785 rc = llapi_search_fsname(pathname, fsname_buf);
2789 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2792 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2794 * Ensure all 3 sattrs (count, size, and offset) are
2795 * successfully retrieved and stored in tmp before writing to
2798 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2802 cache.stripecount = tmp[0];
2803 cache.stripesize = tmp[1];
2804 cache.stripeoffset = tmp[2];
2805 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2809 *scount = cache.stripecount;
2811 *ssize = cache.stripesize;
2813 *soffset = cache.stripeoffset;
2818 static char *layout2name(__u32 layout_pattern)
2820 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2822 else if (layout_pattern == LOV_PATTERN_MDT)
2824 else if (layout_pattern == LOV_PATTERN_RAID0)
2826 else if (layout_pattern ==
2827 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2828 return "raid0,overstriped";
2833 enum lov_dump_flags {
2834 LDF_IS_DIR = 0x0001,
2835 LDF_IS_RAW = 0x0002,
2836 LDF_INDENT = 0x0004,
2837 LDF_SKIP_OBJS = 0x0008,
2839 LDF_EXTENSION = 0x0020,
2842 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2843 struct lov_user_ost_data_v1 *objects,
2844 enum llapi_layout_verbose verbose,
2845 int depth, char *pool_name,
2846 enum lov_dump_flags flags)
2848 bool is_dir = flags & LDF_IS_DIR;
2849 bool is_raw = flags & LDF_IS_RAW;
2850 bool indent = flags & LDF_INDENT;
2851 bool yaml = flags & LDF_YAML;
2852 bool skip_objs = flags & LDF_SKIP_OBJS;
2853 bool extension = flags & LDF_EXTENSION;
2854 char *prefix = is_dir ? "" : "lmm_";
2855 char *separator = "";
2856 char *space = indent ? " " : "";
2859 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2860 lmm_oi_set_seq(&lum->lmm_oi, 0);
2861 if (!indent && (verbose & VERBOSE_DETAIL))
2862 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2865 if (!yaml && !indent && depth && path &&
2866 ((verbose != VERBOSE_OBJID) || !is_dir))
2867 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2869 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2870 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2871 space, prefix, lum->lmm_magic);
2872 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2874 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2875 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2877 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2879 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2884 if (verbose & ~VERBOSE_DFID)
2885 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2888 * This needs a bit of hand-holding since old 1.x lmm_oi
2889 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2890 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2891 * a real FID. Ideally the 2.x code would have stored this
2892 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2893 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2894 * worked properly (especially since IGIF FIDs use mds_inum as
2895 * the FID SEQ), but unfortunately that didn't happen.
2897 * Print it to look like an IGIF FID, even though the fields
2898 * are reversed on disk, so that it makes sense to userspace.
2900 * Don't use ostid_id() and ostid_seq(), since they assume the
2901 * oi_fid fields are in the right order. This is why there are
2902 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2904 * For newer layout types hopefully this will be a real FID.
2906 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2907 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2908 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2909 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2910 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2912 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2913 (unsigned long long)seq, oid, ver);
2915 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2916 (unsigned long long)seq, oid, ver);
2919 if (verbose & VERBOSE_STRIPE_COUNT) {
2920 if (verbose & ~VERBOSE_STRIPE_COUNT)
2921 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2924 if (!is_raw && lum->lmm_stripe_count == 0 &&
2925 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2926 unsigned int scount;
2928 rc = sattr_cache_get_defaults(NULL, path,
2932 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2935 llapi_error(LLAPI_MSG_ERROR, rc,
2936 "Cannot determine default stripe count.");
2938 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2940 (__s16)lum->lmm_stripe_count);
2943 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2945 (__s16)lum->lmm_stripe_count);
2947 if (!yaml && is_dir)
2953 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
2954 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
2955 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2956 if (verbose & ~VERBOSE_EXT_SIZE && extension)
2957 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
2959 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
2960 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2962 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2965 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2968 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2970 llapi_error(LLAPI_MSG_ERROR, rc,
2971 "Cannot determine default stripe size.");
2973 /* Extension size is in KiB */
2974 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2976 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
2977 (unsigned long long)lum->lmm_stripe_size);
2979 if (!yaml && is_dir)
2985 if ((verbose & VERBOSE_PATTERN)) {
2986 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2987 if (verbose & ~VERBOSE_PATTERN)
2988 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2990 if (lov_pattern_supported(lum->lmm_pattern))
2991 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2992 layout2name(lum->lmm_pattern));
2994 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2995 separator = (!yaml && is_dir) ? " " : "\n";
2998 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2999 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3000 if (verbose & ~VERBOSE_GENERATION)
3001 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
3003 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3004 skip_objs ? 0 : (int)lum->lmm_layout_gen);
3008 if (verbose & VERBOSE_STRIPE_OFFSET) {
3009 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3010 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3011 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
3013 if (is_dir || skip_objs)
3014 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3015 lum->lmm_stripe_offset ==
3016 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
3017 lum->lmm_stripe_offset);
3018 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
3019 llapi_printf(LLAPI_MSG_NORMAL, "0");
3021 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3022 objects[0].l_ost_idx);
3023 if (!yaml && is_dir)
3029 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
3030 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3031 if (verbose & ~VERBOSE_POOL)
3032 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
3034 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
3035 if (!yaml && is_dir)
3041 if (strlen(separator) != 0)
3042 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3045 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
3046 struct lov_user_ost_data_v1 *objects,
3047 char *path, int obdindex, int depth,
3048 enum llapi_layout_verbose verbose,
3049 enum lov_dump_flags flags)
3051 bool is_dir = flags & LDF_IS_DIR;
3052 bool indent = flags & LDF_INDENT;
3053 bool skip_objs = flags & LDF_SKIP_OBJS;
3054 bool yaml = flags & LDF_YAML;
3055 bool obdstripe = obdindex == OBD_NOT_FOUND;
3058 if (!obdstripe && !skip_objs) {
3059 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
3060 if (obdindex == objects[i].l_ost_idx) {
3070 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
3073 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
3074 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
3075 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
3076 char *space = " - ";
3079 llapi_printf(LLAPI_MSG_NORMAL,
3080 "%6slmm_objects:\n", " ");
3082 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
3084 llapi_printf(LLAPI_MSG_NORMAL,
3085 "\tobdidx\t\t objid\t\t objid\t\t group\n");
3087 for (i = 0; i < lum->lmm_stripe_count; i++) {
3088 int idx = objects[i].l_ost_idx;
3089 long long oid = ostid_id(&objects[i].l_ost_oi);
3090 long long gr = ostid_seq(&objects[i].l_ost_oi);
3092 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
3096 struct lu_fid fid = { 0 };
3098 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3099 llapi_printf(LLAPI_MSG_NORMAL,
3100 "%sl_ost_idx: %d\n", space, idx);
3101 llapi_printf(LLAPI_MSG_NORMAL,
3102 "%8sl_fid: "DFID_NOBRACE"\n",
3104 } else if (indent) {
3105 struct lu_fid fid = { 0 };
3107 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3108 llapi_printf(LLAPI_MSG_NORMAL,
3109 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
3110 space, i, idx, PFID(&fid));
3114 sprintf(fmt, "%s%s%s\n",
3115 "\t%6u\t%14llu\t%#13llx\t",