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_is_encrypted(int fd)
784 rc = ioctl(fd, FS_IOC_GETFLAGS, &flags);
788 return !!(flags & LUSTRE_ENCRYPT_FL);
791 int llapi_file_open_pool(const char *name, int flags, int mode,
792 unsigned long long stripe_size, int stripe_offset,
793 int stripe_count, int stripe_pattern, char *pool_name)
795 const struct llapi_stripe_param param = {
796 .lsp_stripe_size = stripe_size,
797 .lsp_stripe_count = stripe_count,
798 .lsp_stripe_pattern = stripe_pattern,
799 .lsp_stripe_offset = stripe_offset,
800 .lsp_pool = pool_name
802 return llapi_file_open_param(name, flags, mode, ¶m);
805 int llapi_file_open(const char *name, int flags, int mode,
806 unsigned long long stripe_size, int stripe_offset,
807 int stripe_count, int stripe_pattern)
809 return llapi_file_open_pool(name, flags, mode, stripe_size,
810 stripe_offset, stripe_count,
811 stripe_pattern, NULL);
814 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
815 __u32 flags, char *foreign_lov)
818 struct lov_foreign_md *lfm;
821 if (foreign_lov == NULL) {
823 llapi_error(LLAPI_MSG_ERROR, rc,
824 "foreign LOV EA content must be provided");
828 len = strlen(foreign_lov);
829 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
832 llapi_error(LLAPI_MSG_ERROR, rc,
833 "foreign LOV EA size %zu (must be 0 < len < %zu)",
834 len, XATTR_SIZE_MAX -
835 offsetof(struct lov_foreign_md, lfm_value));
839 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
842 llapi_error(LLAPI_MSG_ERROR, rc,
843 "failed to allocate lov_foreign_md");
847 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
850 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
854 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
855 lfm->lfm_length = len;
856 lfm->lfm_type = type;
857 lfm->lfm_flags = flags;
858 memcpy(lfm->lfm_value, foreign_lov, len);
860 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
861 char *errmsg = "stripe already set";
865 errmsg = "not on a Lustre filesystem";
866 else if (errno == EEXIST || errno == EALREADY)
867 errmsg = "stripe already set";
869 errmsg = strerror(errno);
871 llapi_err_noerrno(LLAPI_MSG_ERROR,
872 "setstripe error for '%s': %s", name, errmsg);
888 int llapi_file_create(const char *name, unsigned long long stripe_size,
889 int stripe_offset, int stripe_count, int stripe_pattern)
893 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
894 stripe_offset, stripe_count, stripe_pattern,
903 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
904 int stripe_offset, int stripe_count,
905 int stripe_pattern, char *pool_name)
909 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
910 stripe_offset, stripe_count, stripe_pattern,
919 static int verify_dir_param(const char *name,
920 const struct llapi_stripe_param *param)
922 char fsname[MAX_OBD_NAME + 1] = { 0 };
923 char *pool_name = param->lsp_pool;
926 /* Make sure we are on a Lustre file system */
927 rc = llapi_search_fsname(name, fsname);
929 llapi_error(LLAPI_MSG_ERROR, rc,
930 "'%s' is not on a Lustre filesystem",
935 /* Check if the stripe pattern is sane. */
936 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
937 param->lsp_stripe_count,
938 param->lsp_stripe_pattern);
942 /* Make sure we have a good pool */
943 if (pool_name != NULL) {
945 * in case user gives the full pool name <fsname>.<poolname>,
948 char *ptr = strchr(pool_name, '.');
952 if (strcmp(pool_name, fsname) != 0) {
954 llapi_err_noerrno(LLAPI_MSG_ERROR,
955 "Pool '%s' is not on filesystem '%s'",
962 /* Make sure the pool exists and is non-empty */
963 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
965 char *err = rc == 0 ? "has no OSTs" : "does not exist";
967 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
968 fsname, pool_name, err);
973 /* sanity check of target list */
974 if (param->lsp_is_specific) {
975 char mdtname[MAX_OBD_NAME + 64];
979 for (i = 0; i < param->lsp_stripe_count; i++) {
980 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
981 fsname, param->lsp_tgts[i]);
982 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
987 llapi_error(LLAPI_MSG_ERROR, rc,
988 "%s: cannot find MDT %s in %s",
995 /* Make sure stripe offset is in MDT list. */
996 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
1000 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1001 "%s: stripe offset '%d' is not in the target list",
1002 __func__, param->lsp_stripe_offset);
1010 static inline void param2lmu(struct lmv_user_md *lmu,
1011 const struct llapi_stripe_param *param)
1013 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
1015 lmu->lum_stripe_count = param->lsp_stripe_count;
1016 lmu->lum_stripe_offset = param->lsp_stripe_offset;
1017 lmu->lum_hash_type = param->lsp_stripe_pattern;
1018 lmu->lum_max_inherit = param->lsp_max_inherit;
1019 lmu->lum_max_inherit_rr = param->lsp_max_inherit_rr;
1020 if (param->lsp_pool != NULL)
1021 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
1022 if (param->lsp_is_specific) {
1025 for (i = 0; i < param->lsp_stripe_count; i++)
1026 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
1030 int llapi_dir_set_default_lmv(const char *name,
1031 const struct llapi_stripe_param *param)
1033 struct lmv_user_md lmu = { 0 };
1037 rc = verify_dir_param(name, param);
1041 /* TODO: default lmv doesn't support specific targets yet */
1042 if (param->lsp_is_specific)
1045 param2lmu(&lmu, param);
1047 fd = open(name, O_DIRECTORY | O_RDONLY);
1050 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1054 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
1056 char *errmsg = "stripe already set";
1059 if (errno != EEXIST && errno != EALREADY)
1060 errmsg = strerror(errno);
1062 llapi_err_noerrno(LLAPI_MSG_ERROR,
1063 "default dirstripe error on '%s': %s",
1070 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
1071 int stripe_count, int stripe_pattern,
1072 const char *pool_name)
1074 const struct llapi_stripe_param param = {
1075 .lsp_stripe_count = stripe_count,
1076 .lsp_stripe_offset = stripe_offset,
1077 .lsp_stripe_pattern = stripe_pattern,
1078 .lsp_pool = (char *)pool_name
1081 return llapi_dir_set_default_lmv(name, ¶m);
1085 * Create a Lustre directory.
1087 * \param name the name of the directory to be created
1088 * \param mode permission of the file if it is created, see mode in open(2)
1089 * \param param stripe pattern of the newly created directory
1091 * \retval 0 on success
1092 * \retval negative errno on failure
1094 int llapi_dir_create(const char *name, mode_t mode,
1095 const struct llapi_stripe_param *param)
1097 struct lmv_user_md *lmu = NULL;
1099 struct obd_ioctl_data data = { 0 };
1102 char *dirpath = NULL;
1103 char *namepath = NULL;
1108 rc = verify_dir_param(name, param);
1112 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1113 param->lsp_is_specific ?
1114 LMV_USER_MAGIC_SPECIFIC :
1117 lmu = calloc(1, lmu_size);
1121 dirpath = strdup(name);
1127 namepath = strdup(name);
1134 param2lmu(lmu, param);
1136 filename = basename(namepath);
1137 dir = dirname(dirpath);
1139 data.ioc_inlbuf1 = (char *)filename;
1140 data.ioc_inllen1 = strlen(filename) + 1;
1141 data.ioc_inlbuf2 = (char *)lmu;
1142 data.ioc_inllen2 = lmu_size;
1143 data.ioc_type = mode;
1144 if (param->lsp_is_create)
1145 /* borrow obdo1.o_flags to store this flag */
1146 data.ioc_obdo1.o_flags = OBD_FL_OBDMDEXISTS;
1147 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1149 llapi_error(LLAPI_MSG_ERROR, rc,
1150 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1155 fd = open(dir, O_DIRECTORY | O_RDONLY);
1158 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1162 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1163 char *errmsg = "stripe already set";
1166 if (errno != EEXIST && errno != EALREADY)
1167 errmsg = strerror(errno);
1169 llapi_err_noerrno(LLAPI_MSG_ERROR,
1170 "dirstripe error on '%s': %s", name, errmsg);
1181 * Create a foreign directory.
1183 * \param name the name of the directory to be created
1184 * \param mode permission of the file if it is created, see mode in open(2)
1185 * \param type foreign type to be set in LMV EA
1186 * \param flags foreign flags to be set in LMV EA
1187 * \param value foreign pattern to be set in LMV EA
1189 * \retval 0 on success
1190 * \retval negative errno on failure
1192 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1193 __u32 flags, const char *value)
1195 struct lmv_foreign_md *lfm = NULL;
1196 size_t lfm_size, len;
1197 struct obd_ioctl_data data = { 0 };
1200 char *dirpath = NULL;
1201 char *namepath = NULL;
1206 len = strlen(value);
1207 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1210 llapi_error(LLAPI_MSG_ERROR, rc,
1211 "invalid LOV EA length %zu (must be 0 < len < %zu)",
1212 len, XATTR_SIZE_MAX -
1213 offsetof(struct lmv_foreign_md, lfm_value));
1216 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1217 lfm = calloc(1, lfm_size);
1221 dirpath = strdup(name);
1227 namepath = strdup(name);
1234 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1235 lfm->lfm_length = len;
1236 lfm->lfm_type = type;
1237 lfm->lfm_flags = flags;
1238 memcpy(lfm->lfm_value, value, len);
1240 filename = basename(namepath);
1241 dir = dirname(dirpath);
1243 data.ioc_inlbuf1 = (char *)filename;
1244 data.ioc_inllen1 = strlen(filename) + 1;
1245 data.ioc_inlbuf2 = (char *)lfm;
1246 data.ioc_inllen2 = lfm_size;
1247 data.ioc_type = mode;
1248 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1250 llapi_error(LLAPI_MSG_ERROR, rc,
1251 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1256 fd = open(dir, O_DIRECTORY | O_RDONLY);
1259 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1263 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1264 char *errmsg = "stripe already set";
1267 if (errno != EEXIST && errno != EALREADY)
1268 errmsg = strerror(errno);
1270 llapi_err_noerrno(LLAPI_MSG_ERROR,
1271 "dirstripe error on '%s': %s", name, errmsg);
1281 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1282 int stripe_count, int stripe_pattern,
1283 const char *pool_name)
1285 const struct llapi_stripe_param param = {
1286 .lsp_stripe_count = stripe_count,
1287 .lsp_stripe_offset = stripe_offset,
1288 .lsp_stripe_pattern = stripe_pattern,
1289 .lsp_pool = (char *)pool_name
1292 return llapi_dir_create(name, mode, ¶m);
1296 * Find the fsname, the full path, and/or an open fd.
1297 * Either the fsname or path must not be NULL
1299 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1302 char buf[PATH_MAX], mntdir[PATH_MAX];
1303 char *ptr, *ptr_end;
1305 int idx = 0, mntlen = 0, fd;
1307 int fsnamelen, mountlen;
1309 /* get the mount point */
1310 fp = setmntent(PROC_MOUNTS, "r");
1313 llapi_error(LLAPI_MSG_ERROR, rc,
1314 "cannot retrieve filesystem mount point");
1318 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1321 if (!llapi_is_lustre_mnt(&mnt))
1324 if ((want & WANT_INDEX) && (idx++ != index))
1327 mntlen = strlen(mnt.mnt_dir);
1328 ptr = strchr(mnt.mnt_fsname, '/');
1329 while (ptr && *ptr == '/')
1332 * thanks to the call to llapi_is_lustre_mnt() above,
1333 * we are sure that mnt.mnt_fsname contains ":/",
1334 * so ptr should never be NULL
1339 while (*ptr_end != '/' && *ptr_end != '\0')
1342 /* Check the fsname for a match, if given */
1343 mountlen = ptr_end - ptr;
1344 if (!(want & WANT_FSNAME) && fsname != NULL &&
1345 (fsnamelen = strlen(fsname)) > 0 &&
1346 (fsnamelen != mountlen ||
1347 (strncmp(ptr, fsname, mountlen) != 0)))
1350 /* If the path isn't set return the first one we find */
1351 if (path == NULL || strlen(path) == 0) {
1352 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1353 mntdir[sizeof(mntdir) - 1] = '\0';
1354 if ((want & WANT_FSNAME) && fsname != NULL) {
1355 strncpy(fsname, ptr, mountlen);
1356 fsname[mountlen] = '\0';
1360 /* Otherwise find the longest matching path */
1361 } else if ((strlen(path) >= mntlen) &&
1362 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1363 /* check the path format */
1364 if (strlen(path) > mntlen && path[mntlen] != '/')
1366 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1367 mntdir[sizeof(mntdir) - 1] = '\0';
1368 if ((want & WANT_FSNAME) && fsname != NULL) {
1369 strncpy(fsname, ptr, mountlen);
1370 fsname[mountlen] = '\0';
1380 if ((want & WANT_PATH) && path != NULL) {
1381 strncpy(path, mntdir, mntlen);
1382 path[mntlen] = '\0';
1384 if (want & WANT_FD) {
1385 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1388 llapi_error(LLAPI_MSG_ERROR, rc,
1389 "cannot open '%s'", mntdir);
1395 } else if (want & WANT_ERROR)
1396 llapi_err_noerrno(LLAPI_MSG_ERROR,
1397 "'%s' not on a mounted Lustre filesystem",
1398 (want & WANT_PATH) ? fsname : path);
1403 * search lustre mounts
1405 * Calling this function will return to the user the mount point, mntdir, and
1406 * the file system name, fsname, if the user passed a buffer to this routine.
1408 * The user inputs are pathname and index. If the pathname is supplied then
1409 * the value of the index will be ignored. The pathname will return data if
1410 * the pathname is located on a lustre mount. Index is used to pick which
1411 * mount point you want in the case of multiple mounted lustre file systems.
1412 * See function lfs_osts in lfs.c for an example of the index use.
1414 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1417 int want = WANT_PATH, idx = -1;
1419 if (!pathname || pathname[0] == '\0') {
1423 strcpy(mntdir, pathname);
1427 want |= WANT_FSNAME;
1428 return get_root_path(want, fsname, NULL, mntdir, idx);
1431 /* Given a path, find the corresponding Lustre fsname */
1432 int llapi_search_fsname(const char *pathname, char *fsname)
1437 path = realpath(pathname, NULL);
1439 char tmp[PATH_MAX - 1];
1445 if (pathname[0] != '/') {
1447 * Need an absolute path, but realpath() only works for
1448 * pathnames that actually exist. We go through the
1449 * extra hurdle of dirname(getcwd() + pathname) in
1450 * case the relative pathname contains ".." in it.
1452 char realpath[PATH_MAX - 1];
1454 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1456 llapi_error(LLAPI_MSG_ERROR, rc,
1457 "cannot get current working directory");
1461 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1462 if (rc >= sizeof(tmp)) {
1464 llapi_error(LLAPI_MSG_ERROR, rc,
1465 "invalid parent path '%s'",
1471 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1472 if (rc >= sizeof(buf)) {
1474 llapi_error(LLAPI_MSG_ERROR, rc,
1475 "invalid path '%s'", pathname);
1478 path = realpath(buf, NULL);
1480 ptr = strrchr(buf, '/');
1482 llapi_error(LLAPI_MSG_ERROR |
1483 LLAPI_MSG_NO_ERRNO, 0,
1484 "cannot resolve path '%s'",
1489 path = realpath(buf, NULL);
1492 llapi_error(LLAPI_MSG_ERROR, rc,
1493 "cannot resolve path '%s'",
1499 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1504 int llapi_search_rootpath(char *pathname, const char *fsname)
1507 * pathname can be used as an argument by get_root_path(),
1508 * clear it for safety
1511 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1515 * Get the list of pool members.
1516 * \param poolname string of format \<fsname\>.\<poolname\>
1517 * \param members caller-allocated array of char*
1518 * \param list_size size of the members array
1519 * \param buffer caller-allocated buffer for storing OST names
1520 * \param buffer_size size of the buffer
1522 * \return number of members retrieved for this pool
1523 * \retval -error failure
1525 int llapi_get_poolmembers(const char *poolname, char **members,
1526 int list_size, char *buffer, int buffer_size)
1528 char fsname[PATH_MAX];
1537 /* name is FSNAME.POOLNAME */
1538 if (strlen(poolname) >= sizeof(fsname))
1541 snprintf(fsname, sizeof(fsname), "%s", poolname);
1542 pool = strchr(fsname, '.');
1549 rc = poolpath(&pathname, fsname, NULL);
1551 llapi_error(LLAPI_MSG_ERROR, rc,
1552 "Lustre filesystem '%s' not found",
1557 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1558 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1559 cfs_free_param_data(&pathname);
1560 if (rc >= sizeof(buf))
1562 fd = fopen(buf, "r");
1565 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1570 while (fgets(buf, sizeof(buf), fd) != NULL) {
1571 if (nb_entries >= list_size) {
1575 buf[sizeof(buf) - 1] = '\0';
1577 tmp = strchr(buf, '\n');
1580 if (used + strlen(buf) + 1 > buffer_size) {
1585 strcpy(buffer + used, buf);
1586 members[nb_entries] = buffer + used;
1587 used += strlen(buf) + 1;
1597 * Get the list of pools in a filesystem.
1598 * \param name filesystem name or path
1599 * \param poollist caller-allocated array of char*
1600 * \param list_size size of the poollist array
1601 * \param buffer caller-allocated buffer for storing pool names
1602 * \param buffer_size size of the buffer
1604 * \return number of pools retrieved for this filesystem
1605 * \retval -error failure
1607 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1608 char *buffer, int buffer_size)
1614 struct dirent *pool;
1616 unsigned int nb_entries = 0;
1617 unsigned int used = 0;
1620 /* initialize output array */
1621 for (i = 0; i < list_size; i++)
1624 /* is name a pathname ? */
1625 ptr = strchr(name, '/');
1627 char fsname_buf[MAXNAMLEN];
1629 /* We will need fsname for printing later */
1630 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1634 ptr = strrchr(fsname_buf, '-');
1638 fsname = strdup(fsname_buf);
1642 /* name is FSNAME */
1643 fsname = strdup(name);
1648 rc = poolpath(&pathname, fsname, NULL);
1650 llapi_error(LLAPI_MSG_ERROR, rc,
1651 "Lustre filesystem '%s' not found", name);
1655 dir = opendir(pathname.gl_pathv[0]);
1658 llapi_error(LLAPI_MSG_ERROR, rc,
1659 "Could not open pool list for '%s'",
1666 pool = readdir(dir);
1672 /* ignore . and .. */
1673 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1676 /* check output bounds */
1677 if (nb_entries >= list_size) {
1679 goto free_dir_no_msg;
1682 /* +2 for '.' and final '\0' */
1683 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1686 goto free_dir_no_msg;
1689 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1690 poollist[nb_entries] = buffer + used;
1691 used += strlen(pool->d_name) + strlen(fsname) + 2;
1697 llapi_error(LLAPI_MSG_ERROR, rc,
1698 "Error reading pool list for '%s'", name);
1700 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1705 cfs_free_param_data(&pathname);
1708 return rc != 0 ? rc : nb_entries;
1711 /* wrapper for lfs.c and obd.c */
1712 int llapi_poollist(const char *name)
1714 int poolcount, rc, i;
1717 rc = llapi_get_poolbuf(name, &buf, &pools, &poolcount);
1721 for (i = 0; i < poolcount; i++)
1722 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", pools[i]);
1729 * Get buffer that holds uuids and the list of pools in a filesystem.
1731 * \param name filesystem name or path
1732 * \param buf bufffer that has to be freed if function returns 0
1733 * \param pools pointer to the list of pools in buffer
1734 * \param poolcount number of pools
1736 * \return 0 when found at least 1 pool, i.e. poolcount > 0
1737 * \retval -error failure
1739 int llapi_get_poolbuf(const char *name, char **buf,
1740 char ***pools, int *poolcount)
1743 * list of pool names (assume that pool count is smaller
1746 char **list, *buffer = NULL, *fsname = (char *)name;
1747 char *poolname = NULL, *tmp = NULL, data[16];
1748 enum param_filter type = FILTER_BY_PATH;
1749 int obdcount, bufsize, rc, nb;
1754 if (name[0] != '/') {
1755 fsname = strdup(name);
1759 poolname = strchr(fsname, '.');
1762 type = FILTER_BY_FS_NAME;
1765 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1766 data, sizeof(data));
1769 obdcount = atoi(data);
1772 * Allocate space for each fsname-OST0000_UUID, 1 per OST,
1773 * and also an array to store the pointers for all that
1777 bufsize = sizeof(struct obd_uuid) * obdcount;
1778 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1779 if (buffer == NULL) {
1783 list = (char **) (buffer + bufsize);
1786 /* name is a path or fsname */
1787 nb = llapi_get_poollist(name, list, obdcount,
1790 /* name is a pool name (<fsname>.<poolname>) */
1791 nb = llapi_get_poolmembers(name, list, obdcount,
1795 if (nb == -EOVERFLOW) {
1798 goto retry_get_pools;
1801 rc = (nb < 0 ? nb : 0);
1808 /* Don't free buffer, it will be used later */
1811 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1816 typedef int (semantic_func_t)(char *path, int p, int *d,
1817 void *data, struct dirent64 *de);
1819 #define OBD_NOT_FOUND (-1)
1821 static bool lmv_is_foreign(__u32 magic)
1823 return magic == LMV_MAGIC_FOREIGN;
1826 static void find_param_fini(struct find_param *param)
1828 if (param->fp_migrate)
1831 if (param->fp_obd_indexes) {
1832 free(param->fp_obd_indexes);
1833 param->fp_obd_indexes = NULL;
1836 if (param->fp_lmd) {
1837 free(param->fp_lmd);
1838 param->fp_lmd = NULL;
1841 if (param->fp_lmv_md) {
1842 free(param->fp_lmv_md);
1843 param->fp_lmv_md = NULL;
1847 static int common_param_init(struct find_param *param, char *path)
1849 int lum_size = get_mds_md_size(path);
1854 /* migrate has fp_lmv_md initialized outside */
1855 if (param->fp_migrate)
1858 if (lum_size < PATH_MAX + 1)
1859 lum_size = PATH_MAX + 1;
1861 param->fp_lum_size = lum_size;
1862 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1864 if (param->fp_lmd == NULL) {
1865 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1866 "error: allocate %zu bytes for layout failed",
1867 sizeof(lstat_t) + param->fp_lum_size);
1871 param->fp_lmv_stripe_count = 256;
1872 param->fp_lmv_md = calloc(1,
1873 lmv_user_md_size(param->fp_lmv_stripe_count,
1874 LMV_USER_MAGIC_SPECIFIC));
1875 if (param->fp_lmv_md == NULL) {
1876 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1877 "error: allocation of %d bytes for ioctl",
1878 lmv_user_md_size(param->fp_lmv_stripe_count,
1879 LMV_USER_MAGIC_SPECIFIC));
1880 find_param_fini(param);
1884 param->fp_got_uuids = 0;
1885 param->fp_obd_indexes = NULL;
1886 param->fp_obd_index = OBD_NOT_FOUND;
1887 param->fp_mdt_index = OBD_NOT_FOUND;
1891 static int cb_common_fini(char *path, int p, int *dp, void *data,
1892 struct dirent64 *de)
1894 struct find_param *param = data;
1900 /* set errno upon failure */
1901 static int open_parent(const char *path)
1907 path_copy = strdup(path);
1908 if (path_copy == NULL)
1911 parent_path = dirname(path_copy);
1912 parent = open(parent_path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1918 static int cb_get_dirstripe(char *path, int *d, struct find_param *param)
1921 bool did_nofollow = false;
1926 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1927 if (param->fp_get_default_lmv)
1928 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1930 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1932 ret = ioctl(*d, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1934 /* if ENOTTY likely to be a fake symlink, so try again after
1935 * new open() with O_NOFOLLOW, but only once to prevent any
1936 * loop like for the path of a file/dir not on Lustre !!
1938 if (ret < 0 && errno == ENOTTY && !did_nofollow) {
1941 did_nofollow = true;
1942 fd = open(path, O_RDONLY | O_NOFOLLOW);
1944 /* restore original errno */
1949 /* close original fd and set new */
1952 ret2 = ioctl(fd, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1953 if (ret2 < 0 && errno != E2BIG) {
1954 /* restore original errno */
1958 /* LMV is ok or need to handle E2BIG case now */
1962 if (errno == E2BIG && ret != 0) {
1966 /* if foreign LMV case, fake stripes number */
1967 if (lmv_is_foreign(param->fp_lmv_md->lum_magic)) {
1968 struct lmv_foreign_md *lfm;
1970 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1971 if (lfm->lfm_length < XATTR_SIZE_MAX -
1972 offsetof(typeof(*lfm), lfm_value)) {
1973 uint32_t size = lfm->lfm_length +
1974 offsetof(typeof(*lfm), lfm_value);
1976 stripe_count = lmv_foreign_to_md_stripes(size);
1978 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1979 "error: invalid %d foreign size returned from ioctl",
1984 stripe_count = param->fp_lmv_md->lum_stripe_count;
1986 if (stripe_count <= param->fp_lmv_stripe_count)
1989 free(param->fp_lmv_md);
1990 param->fp_lmv_stripe_count = stripe_count;
1991 lmv_size = lmv_user_md_size(stripe_count,
1992 LMV_USER_MAGIC_SPECIFIC);
1993 param->fp_lmv_md = malloc(lmv_size);
1994 if (param->fp_lmv_md == NULL) {
1995 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1996 "error: allocation of %d bytes for ioctl",
1997 lmv_user_md_size(param->fp_lmv_stripe_count,
1998 LMV_USER_MAGIC_SPECIFIC));
2007 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
2010 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
2011 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
2012 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
2013 lmd_v2->lmd_stx.stx_uid = st->st_uid;
2014 lmd_v2->lmd_stx.stx_gid = st->st_gid;
2015 lmd_v2->lmd_stx.stx_mode = st->st_mode;
2016 lmd_v2->lmd_stx.stx_ino = st->st_ino;
2017 lmd_v2->lmd_stx.stx_size = st->st_size;
2018 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
2019 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
2020 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
2021 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
2022 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
2023 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
2024 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
2025 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
2026 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
2028 lmd_v2->lmd_flags = 0;
2030 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
2032 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
2033 if (lmd_v2->lmd_stx.stx_size)
2034 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
2035 if (lmd_v2->lmd_stx.stx_blocks)
2036 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
2038 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
2039 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
2040 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
2045 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
2047 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
2048 struct lov_user_mds_data *lmd_v2 = lmdbuf;
2052 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
2056 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
2059 st = lmd_v1->lmd_st;
2060 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
2061 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
2062 convert_lmd_statx(lmd_v2, &st, false);
2063 lmd_v2->lmd_lmmsize = 0;
2064 lmd_v2->lmd_padding = 0;
2069 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
2070 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
2072 struct lov_user_mds_data *lmd = lmdbuf;
2073 static bool use_old_ioctl;
2077 if (parent_fd < 0 && dir_fd < 0)
2079 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
2084 * LL_IOC_MDC_GETINFO operates on the current directory inode
2085 * and returns struct lov_user_mds_data, while
2086 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
2088 if (type == GET_LMD_INFO)
2089 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_V1 :
2090 LL_IOC_MDC_GETINFO_V2;
2092 cmd = LL_IOC_LOV_GETSTRIPE;
2095 ret = ioctl(dir_fd, cmd, lmdbuf);
2096 if (ret < 0 && errno == ENOTTY &&
2097 cmd == LL_IOC_MDC_GETINFO_V2) {
2098 cmd = LL_IOC_MDC_GETINFO_V1;
2099 use_old_ioctl = true;
2103 if (cmd == LL_IOC_MDC_GETINFO_V1 && !ret)
2104 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2106 if (ret < 0 && errno == ENOTTY && type == GET_LMD_STRIPE) {
2109 /* retry ioctl() after new open() with O_NOFOLLOW
2110 * just in case it could be a fake symlink
2111 * need using a new open() as dir_fd is being closed
2115 dir_fd2 = open(path, O_RDONLY | O_NDELAY | O_NOFOLLOW);
2117 /* return original error */
2120 ret = ioctl(dir_fd2, cmd, lmdbuf);
2121 /* pass new errno or success back to caller */
2127 } else if (parent_fd >= 0) {
2128 const char *fname = strrchr(path, '/');
2131 * IOC_MDC_GETFILEINFO takes as input the filename (relative to
2132 * the parent directory) and returns struct lov_user_mds_data,
2133 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2135 * This avoids opening, locking, and closing each file on the
2136 * client if that is not needed. Multiple of these ioctl() can
2137 * be done on the parent dir with a single open for all
2138 * files in that directory, and it also doesn't pollute the
2139 * client dcache with millions of dentries when traversing
2140 * a large filesystem.
2142 fname = (fname == NULL ? path : fname + 1);
2144 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2147 else if (ret >= lmdlen || ret++ == 0)
2150 if (type == GET_LMD_INFO)
2151 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_V1 :
2152 IOC_MDC_GETFILEINFO_V2;
2154 cmd = IOC_MDC_GETFILESTRIPE;
2157 ret = ioctl(parent_fd, cmd, lmdbuf);
2158 if (ret < 0 && errno == ENOTTY &&
2159 cmd == IOC_MDC_GETFILEINFO_V2) {
2160 cmd = IOC_MDC_GETFILEINFO_V1;
2161 use_old_ioctl = true;
2162 goto retry_getfileinfo;
2165 if (cmd == IOC_MDC_GETFILEINFO_V1 && !ret)
2166 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2170 if (ret && type == GET_LMD_INFO) {
2171 if (errno == ENOTTY) {
2175 * ioctl is not supported, it is not a lustre fs.
2176 * Do the regular lstat(2) instead.
2178 ret = lstat_f(path, &st);
2181 llapi_error(LLAPI_MSG_ERROR, ret,
2182 "error: %s: lstat failed for %s",
2186 convert_lmd_statx(lmd, &st, true);
2188 * It may be wrong to set use_old_ioctl with true as
2189 * the file is not a lustre fs. So reset it with false
2192 use_old_ioctl = false;
2193 } else if (errno == ENOENT) {
2195 llapi_error(LLAPI_MSG_WARN, ret,
2196 "warning: %s does not exist", path);
2197 } else if (errno != EISDIR && errno != ENODATA) {
2199 llapi_error(LLAPI_MSG_ERROR, ret,
2200 "%s ioctl failed for %s.",
2201 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2202 "IOC_MDC_GETFILEINFO", path);
2209 static int llapi_semantic_traverse(char *path, int size, int parent,
2210 semantic_func_t sem_init,
2211 semantic_func_t sem_fini, void *data,
2212 struct dirent64 *de)
2214 struct find_param *param = (struct find_param *)data;
2215 struct dirent64 *dent;
2216 int len, ret, d, p = -1;
2222 d = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
2223 /* if an invalid fake dir symlink, opendir() will return EINVAL
2224 * instead of ENOTDIR. If a valid but dangling faked or real file/dir
2225 * symlink ENOENT will be returned. For a valid/resolved fake or real
2226 * file symlink ENOTDIR will be returned as for a regular file.
2227 * opendir() will be successful for a valid and resolved fake or real
2228 * dir simlink or a regular dir.
2230 if (d == -1 && errno != ENOTDIR && errno != EINVAL && errno != ENOENT) {
2232 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2235 } else if (d == -1) {
2236 if (errno == ENOENT || errno == EINVAL) {
2237 int old_errno = errno;
2239 /* try to open with O_NOFOLLOW this will help
2240 * differentiate fake vs real symlinks
2241 * it is ok to not use O_DIRECTORY with O_RDONLY
2242 * and it will prevent the need to deal with ENOTDIR
2243 * error, instead of ELOOP, being returned by recent
2244 * kernels for real symlinks
2246 d = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2247 /* if a dangling real symlink should return ELOOP, or
2248 * again ENOENT if really non-existing path, or E...??
2249 * So return original error. If success or ENOTDIR, path
2250 * is likely to be a fake dir/file symlink, so continue
2260 if (parent == -1 && d == -1) {
2261 /* Open the parent dir. */
2262 p = open_parent(path);
2268 } else { /* d != -1 */
2271 /* try to reopen dir with O_NOFOLLOW just in case of a foreign
2274 d2 = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2279 /* continue with d */
2285 ret = sem_init(path, (parent != -1) ? parent : p, &d, data, de);
2295 /* ENOTDIR if fake symlink, do not consider it as an error */
2296 if (errno != ENOTDIR)
2297 llapi_error(LLAPI_MSG_ERROR, errno,
2298 "fdopendir() failed");
2305 while ((dent = readdir64(dir)) != NULL) {
2308 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2312 if ((len + dent->d_reclen + 2) > size) {
2313 llapi_err_noerrno(LLAPI_MSG_ERROR,
2314 "error: %s: string buffer too small for %s",
2319 strcat(path, dent->d_name);
2321 if (dent->d_type == DT_UNKNOWN) {
2322 struct lov_user_mds_data *lmd = param->fp_lmd;
2324 rc = get_lmd_info_fd(path, d, -1, param->fp_lmd,
2325 param->fp_lum_size, GET_LMD_INFO);
2327 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2334 switch (dent->d_type) {
2336 llapi_err_noerrno(LLAPI_MSG_ERROR,
2337 "error: %s: '%s' is UNKNOWN type %d",
2338 __func__, dent->d_name, dent->d_type);
2341 rc = llapi_semantic_traverse(path, size, d, sem_init,
2342 sem_fini, data, dent);
2343 if (rc != 0 && ret == 0)
2349 rc = sem_init(path, d, NULL, data, dent);
2350 if (rc < 0 && ret == 0) {
2355 if (sem_fini && rc == 0)
2356 sem_fini(path, d, NULL, data, dent);
2364 sem_fini(path, parent, &d, data, de);
2377 static int param_callback(char *path, semantic_func_t sem_init,
2378 semantic_func_t sem_fini, struct find_param *param)
2380 int ret, len = strlen(path);
2383 if (len > PATH_MAX) {
2385 llapi_error(LLAPI_MSG_ERROR, ret,
2386 "Path name '%s' is too long", path);
2390 buf = (char *)malloc(2 * PATH_MAX);
2394 snprintf(buf, PATH_MAX + 1, "%s", path);
2395 ret = common_param_init(param, buf);
2399 param->fp_depth = 0;
2401 ret = llapi_semantic_traverse(buf, 2 * PATH_MAX, -1, sem_init,
2402 sem_fini, param, NULL);
2404 find_param_fini(param);
2406 return ret < 0 ? ret : 0;
2409 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2413 rc = ioctl(fd, OBD_IOC_GETDTNAME, lov_name);
2414 if (rc && errno == ENOTTY)
2415 rc = ioctl(fd, OBD_IOC_GETNAME_OLD, lov_name);
2418 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2424 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2428 rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2431 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2437 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2441 /* do not follow faked symlinks */
2442 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
2444 /* real symlink should have failed with ELOOP so retry without
2445 * O_NOFOLLOW just in case
2447 fd = open(path, O_RDONLY | O_NONBLOCK);
2450 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'",
2456 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2462 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2466 fd = open(path, O_RDONLY | O_NONBLOCK);
2469 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2473 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2485 * If uuidp is NULL, return the number of available obd uuids.
2486 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2487 * there are more OSTs than allocated to uuidp, then an error is returned with
2488 * the ost_count set to number of available obd uuids.
2490 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2491 int *ost_count, enum tgt_type type)
2493 char buf[PATH_MAX], format[32];
2494 int rc = 0, index = 0;
2495 struct obd_uuid name;
2499 /* Get the lov name */
2500 if (type == LOV_TYPE)
2501 rc = llapi_file_fget_lov_uuid(fd, &name);
2503 rc = llapi_file_fget_lmv_uuid(fd, &name);
2507 /* Now get the ost uuids */
2508 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2509 FILTER_BY_EXACT, "target_obd", ¶m);
2513 fp = fopen(param.gl_pathv[0], "r");
2516 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2521 snprintf(format, sizeof(format),
2522 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2523 while (fgets(buf, sizeof(buf), fp) != NULL) {
2524 if (uuidp && (index < *ost_count)) {
2525 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2533 if (uuidp && (index > *ost_count))
2538 cfs_free_param_data(¶m);
2542 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2544 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2547 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2552 root = open(mnt, O_RDONLY | O_DIRECTORY);
2555 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2560 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2569 * Check if user specified value matches a real uuid. Ignore _UUID,
2570 * -osc-4ba41334, other trailing gunk in comparison.
2571 * @param real_uuid ends in "_UUID"
2572 * @param search_uuid may or may not end in "_UUID"
2574 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2576 int cmplen = strlen(real_uuid);
2577 int searchlen = strlen(search_uuid);
2579 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2581 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2585 * The UUIDs may legitimately be different lengths, if
2586 * the system was upgraded from an older version.
2588 if (cmplen != searchlen)
2591 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2595 * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2596 * returned in param->fp_obd_index
2598 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2600 struct obd_uuid obd_uuid;
2607 if (param->fp_got_uuids)
2610 /* Get the lov/lmv name */
2611 if (param->fp_get_lmv)
2612 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2614 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2616 if (rc != -ENOTTY) {
2617 llapi_error(LLAPI_MSG_ERROR, rc,
2618 "error: can't get %s name: %s",
2619 param->fp_get_lmv ? "lmv" : "lov",
2627 param->fp_got_uuids = 1;
2629 /* Now get the ost uuids */
2630 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2631 obd_uuid.uuid, FILTER_BY_EXACT,
2632 "target_obd", ¶m_data);
2636 fp = fopen(param_data.gl_pathv[0], "r");
2639 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2640 param_data.gl_pathv[0]);
2644 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2645 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2646 param->fp_get_lmv ? "MDTS" : "OBDS");
2648 snprintf(format, sizeof(format),
2649 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2650 while (fgets(buf, sizeof(buf), fp) != NULL) {
2653 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2656 if (param->fp_obd_uuid) {
2657 if (llapi_uuid_match(obd_uuid.uuid,
2658 param->fp_obd_uuid->uuid)) {
2659 param->fp_obd_index = index;
2662 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2663 /* Print everything */
2664 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2667 param->fp_obds_printed = 1;
2671 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2672 llapi_err_noerrno(LLAPI_MSG_ERROR,
2673 "error: %s: unknown obduuid: %s",
2674 __func__, param->fp_obd_uuid->uuid);
2678 cfs_free_param_data(¶m_data);
2683 * In this case, param->fp_obd_uuid will be an array of obduuids and
2684 * obd index for all these obduuids will be returned in
2685 * param->fp_obd_indexes
2687 static int setup_indexes(int d, char *path, struct obd_uuid *obduuids,
2688 int num_obds, int **obdindexes, int *obdindex,
2691 int ret, obdcount, obd_valid = 0, obdnum;
2693 struct obd_uuid *uuids = NULL;
2697 if (type == LOV_TYPE)
2698 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2700 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2704 obdcount = atoi(buf);
2705 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2710 ret = llapi_get_target_uuids(d, uuids, &obdcount, type);
2712 if (ret == -EOVERFLOW) {
2713 struct obd_uuid *uuids_temp;
2715 uuids_temp = realloc(uuids, obdcount *
2716 sizeof(struct obd_uuid));
2717 if (uuids_temp != NULL) {
2719 goto retry_get_uuids;
2724 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2728 indexes = malloc(num_obds * sizeof(*obdindex));
2729 if (indexes == NULL) {
2734 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2737 /* The user may have specified a simple index */
2738 i = strtol(obduuids[obdnum].uuid, &end, 0);
2739 if (end && *end == '\0' && i < obdcount) {
2740 indexes[obdnum] = i;
2743 for (i = 0; i < obdcount; i++) {
2744 if (llapi_uuid_match(uuids[i].uuid,
2745 obduuids[obdnum].uuid)) {
2746 indexes[obdnum] = i;
2752 if (i >= obdcount) {
2753 indexes[obdnum] = OBD_NOT_FOUND;
2754 llapi_err_noerrno(LLAPI_MSG_ERROR,
2755 "invalid obduuid '%s'",
2756 obduuids[obdnum].uuid);
2762 *obdindex = OBD_NOT_FOUND;
2764 *obdindex = obd_valid;
2766 *obdindexes = indexes;
2774 static int setup_target_indexes(int d, char *path, struct find_param *param)
2778 if (param->fp_mdt_uuid) {
2779 ret = setup_indexes(d, path, param->fp_mdt_uuid,
2781 ¶m->fp_mdt_indexes,
2782 ¶m->fp_mdt_index, LMV_TYPE);
2787 if (param->fp_obd_uuid) {
2788 ret = setup_indexes(d, path, param->fp_obd_uuid,
2790 ¶m->fp_obd_indexes,
2791 ¶m->fp_obd_index, LOV_TYPE);
2796 param->fp_got_uuids = 1;
2801 int llapi_ostlist(char *path, struct find_param *param)
2806 fd = open(path, O_RDONLY | O_DIRECTORY);
2810 ret = setup_obd_uuid(fd, path, param);
2817 * Tries to determine the default stripe attributes for a given filesystem. The
2818 * filesystem to check should be specified by fsname, or will be determined
2821 static int sattr_get_defaults(const char *const fsname,
2822 unsigned int *scount,
2823 unsigned int *ssize,
2824 unsigned int *soffset)
2830 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2831 "stripecount", val, sizeof(val));
2834 *scount = atoi(val);
2838 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2839 "stripesize", val, sizeof(val));
2846 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2847 "stripeoffset", val, sizeof(val));
2850 *soffset = atoi(val);
2857 * Tries to gather the default stripe attributes for a given filesystem. If
2858 * the attributes can be determined, they are cached for easy retreival the
2859 * next time they are needed. Only a single filesystem's attributes are
2862 int sattr_cache_get_defaults(const char *const fsname,
2863 const char *const pathname, unsigned int *scount,
2864 unsigned int *ssize, unsigned int *soffset)
2867 char fsname[PATH_MAX + 1];
2868 unsigned int stripecount;
2869 unsigned int stripesize;
2870 unsigned int stripeoffset;
2876 char fsname_buf[PATH_MAX + 1];
2877 unsigned int tmp[3];
2879 if (fsname == NULL) {
2880 rc = llapi_search_fsname(pathname, fsname_buf);
2884 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2887 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2889 * Ensure all 3 sattrs (count, size, and offset) are
2890 * successfully retrieved and stored in tmp before writing to
2893 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2897 cache.stripecount = tmp[0];
2898 cache.stripesize = tmp[1];
2899 cache.stripeoffset = tmp[2];
2900 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2904 *scount = cache.stripecount;
2906 *ssize = cache.stripesize;
2908 *soffset = cache.stripeoffset;
2913 static char *layout2name(__u32 layout_pattern)
2915 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2917 else if (layout_pattern == LOV_PATTERN_MDT)
2919 else if (layout_pattern == LOV_PATTERN_RAID0)
2921 else if (layout_pattern ==
2922 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2923 return "raid0,overstriped";
2928 enum lov_dump_flags {
2929 LDF_IS_DIR = 0x0001,
2930 LDF_IS_RAW = 0x0002,
2931 LDF_INDENT = 0x0004,
2932 LDF_SKIP_OBJS = 0x0008,
2934 LDF_EXTENSION = 0x0020,
2937 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2938 struct lov_user_ost_data_v1 *objects,
2939 enum llapi_layout_verbose verbose,
2940 int depth, char *pool_name,
2941 enum lov_dump_flags flags)
2943 bool is_dir = flags & LDF_IS_DIR;
2944 bool is_raw = flags & LDF_IS_RAW;
2945 bool indent = flags & LDF_INDENT;
2946 bool yaml = flags & LDF_YAML;
2947 bool skip_objs = flags & LDF_SKIP_OBJS;
2948 bool extension = flags & LDF_EXTENSION;
2949 char *prefix = is_dir ? "" : "lmm_";
2950 char *separator = "";
2951 char *space = indent ? " " : "";
2954 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2955 lmm_oi_set_seq(&lum->lmm_oi, 0);
2956 if (!indent && (verbose & VERBOSE_DETAIL))
2957 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2960 if (!yaml && !indent && depth && path &&
2961 ((verbose != VERBOSE_OBJID) || !is_dir))
2962 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2964 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2965 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2966 space, prefix, lum->lmm_magic);
2967 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2969 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2970 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2972 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2975 if (verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) {
2980 if (verbose & ~VERBOSE_DFID)
2981 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2985 struct lu_fid dir_fid;
2987 rc = llapi_path2fid(path, &dir_fid);
2989 llapi_error(LLAPI_MSG_ERROR, rc,
2990 "Cannot determine directory fid.");
2992 seq = dir_fid.f_seq;
2993 oid = dir_fid.f_oid;
2994 ver = dir_fid.f_ver;
2997 * This needs a bit of hand-holding since old 1.x
2998 * lmm_oi have { oi.oi_id = mds_inum, oi.oi_seq = 0 }
2999 * and 2.x lmm_oi have { oi.oi_id = mds_oid,
3000 * oi.oi_seq = mds_seq } instead of a real FID.
3001 * Ideally the 2.x code would have stored this like a
3002 * FID with { oi_id = mds_seq, oi_seq = mds_oid } so
3003 * the ostid union lu_fid { f_seq = mds_seq,
3004 * f_oid = mds_oid } worked properly (especially since
3005 * IGIF FIDs use mds_inum as the FID SEQ), but
3006 * unfortunately that didn't happen.
3008 * Print it to look like an IGIF FID, even though the
3009 * fields are reversed on disk, so that it makes sense
3012 * Don't use ostid_id() and ostid_seq(), since they
3013 * assume the oi_fid fields are in the right order.
3014 * This is why there are separate lmm_oi_seq() and
3015 * lmm_oi_id() routines for this.
3017 * For newer layout types hopefully this will be a
3020 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
3021 lmm_oi_id(&lum->lmm_oi) :
3022 lmm_oi_seq(&lum->lmm_oi);
3023 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
3024 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
3025 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
3029 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
3030 (unsigned long long)seq, oid, ver);
3032 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
3033 (unsigned long long)seq, oid, ver);
3036 if (verbose & VERBOSE_STRIPE_COUNT) {
3037 if (verbose & ~VERBOSE_STRIPE_COUNT)
3038 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
3041 if (!is_raw && lum->lmm_stripe_count == 0 &&
3042 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
3043 unsigned int scount;
3045 rc = sattr_cache_get_defaults(NULL, path,
3049 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3052 llapi_error(LLAPI_MSG_ERROR, rc,
3053 "Cannot determine default stripe count.");
3055 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3057 (__s16)lum->lmm_stripe_count);
3060 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
3062 (__s16)lum->lmm_stripe_count);
3064 if (!yaml && is_dir)
3070 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
3071 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
3072 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3073 if (verbose & ~VERBOSE_EXT_SIZE && extension)
3074 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
3076 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
3077 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
3079 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
3082 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
3085 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
3087 llapi_error(LLAPI_MSG_ERROR, rc,
3088 "Cannot determine default stripe size.");
3090 /* Extension size is in KiB */
3091 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3093 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
3094 (unsigned long long)lum->lmm_stripe_size);
3096 if (!yaml && is_dir)
3102 if ((verbose & VERBOSE_PATTERN)) {
3103 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3104 if (verbose & ~VERBOSE_PATTERN)
3105 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
3107 if (lov_pattern_supported(lum->lmm_pattern))
3108 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3109 layout2name(lum->lmm_pattern));
3111 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
3112 separator = (!yaml && is_dir) ? " " : "\n";
3115 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
3116 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3117 if (verbose & ~VERBOSE_GENERATION)
3118 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
3120 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3121 skip_objs ? 0 : (int)lum->lmm_layout_gen);
3125 if (verbose & VERBOSE_STRIPE_OFFSET) {
3126 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3127 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3128 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
3130 if (is_dir || skip_objs)
3131 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3132 lum->lmm_stripe_offset ==
3133 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
3134 lum->lmm_stripe_offset);
3135 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_M