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 #define FORMATTED_BUF_LEN 1024
87 static int llapi_msg_level = LLAPI_MSG_MAX;
88 const char *liblustreapi_cmd;
90 struct lustre_foreign_type lu_foreign_types[] = {
91 {.lft_type = LU_FOREIGN_TYPE_NONE, .lft_name = "none"},
92 {.lft_type = LU_FOREIGN_TYPE_SYMLINK, .lft_name = "symlink"},
93 /* must be the last element */
94 {.lft_type = LU_FOREIGN_TYPE_UNKNOWN, .lft_name = NULL}
95 /* array max dimension must be <= UINT32_MAX */
98 void llapi_msg_set_level(int level)
100 /* ensure level is in the good range */
101 if (level < LLAPI_MSG_OFF)
102 llapi_msg_level = LLAPI_MSG_OFF;
103 else if (level > LLAPI_MSG_MAX)
104 llapi_msg_level = LLAPI_MSG_MAX;
106 llapi_msg_level = level;
109 int llapi_msg_get_level(void)
111 return llapi_msg_level;
114 void llapi_set_command_name(const char *cmd)
116 liblustreapi_cmd = cmd;
119 void llapi_clear_command_name(void)
121 liblustreapi_cmd = NULL;
124 static void error_callback_default(enum llapi_message_level level, int err,
125 const char *fmt, va_list ap)
127 bool has_nl = strchr(fmt, '\n') != NULL;
129 if (liblustreapi_cmd != NULL)
130 fprintf(stderr, "%s %s: ", program_invocation_short_name,
133 fprintf(stderr, "%s: ", program_invocation_short_name);
136 if (level & LLAPI_MSG_NO_ERRNO) {
137 vfprintf(stderr, fmt, ap);
139 fprintf(stderr, "\n");
144 * Remove trailing linefeed so error string can be appended.
145 * @fmt is a const string, so we can't modify it directly.
147 if (has_nl && (newfmt = strdup(fmt)))
148 *strrchr(newfmt, '\n') = '\0';
150 newfmt = (char *)fmt;
152 vfprintf(stderr, newfmt, ap);
155 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
159 static void info_callback_default(enum llapi_message_level level, int err,
160 const char *fmt, va_list ap)
163 if (liblustreapi_cmd != NULL) {
164 fprintf(stdout, "%s %s: ",
165 program_invocation_short_name,
168 fprintf(stdout, "%s: ", program_invocation_short_name);
171 vfprintf(stdout, fmt, ap);
174 static llapi_log_callback_t llapi_error_callback = error_callback_default;
175 static llapi_log_callback_t llapi_info_callback = info_callback_default;
178 /* llapi_error will preserve errno */
179 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
182 int tmp_errno = errno;
184 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
188 llapi_error_callback(level, abs(err), fmt, args);
193 /* llapi_printf will preserve errno */
194 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
197 int tmp_errno = errno;
199 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
203 llapi_info_callback(level, 0, fmt, args);
209 * Set a custom error logging function. Passing in NULL will reset the logging
210 * callback to its default value.
212 * This function returns the value of the old callback.
214 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
216 llapi_log_callback_t old = llapi_error_callback;
219 llapi_error_callback = cb;
221 llapi_error_callback = error_callback_default;
227 * Set a custom info logging function. Passing in NULL will reset the logging
228 * callback to its default value.
230 * This function returns the value of the old callback.
232 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
234 llapi_log_callback_t old = llapi_info_callback;
237 llapi_info_callback = cb;
239 llapi_info_callback = info_callback_default;
245 * size_units is to be initialized (or zeroed) by caller.
247 int llapi_parse_size(const char *optarg, unsigned long long *size,
248 unsigned long long *size_units, int bytes_spec)
251 char *argbuf = (char *)optarg;
252 unsigned long long frac = 0, frac_d = 1;
254 if (strncmp(optarg, "-", 1) == 0)
257 if (*size_units == 0)
260 *size = strtoull(argbuf, &end, 0);
261 if (end != NULL && *end == '.') {
265 frac = strtoull(argbuf, &end, 10);
266 /* count decimal places */
267 for (i = 0; i < (end - argbuf); i++)
272 if ((*end == 'b') && *(end + 1) == '\0' &&
273 (*size & (~0ULL << (64 - 9))) == 0 &&
275 *size_units = 1 << 9;
276 } else if ((*end == 'b') &&
277 *(end + 1) == '\0' &&
280 } else if ((*end == 'k' || *end == 'K') &&
281 *(end + 1) == '\0' &&
282 (*size & (~0ULL << (64 - 10))) == 0) {
283 *size_units = 1 << 10;
284 } else if ((*end == 'm' || *end == 'M') &&
285 *(end + 1) == '\0' &&
286 (*size & (~0ULL << (64 - 20))) == 0) {
287 *size_units = 1 << 20;
288 } else if ((*end == 'g' || *end == 'G') &&
289 *(end + 1) == '\0' &&
290 (*size & (~0ULL << (64 - 30))) == 0) {
291 *size_units = 1 << 30;
292 } else if ((*end == 't' || *end == 'T') &&
293 *(end + 1) == '\0' &&
294 (*size & (~0ULL << (64 - 40))) == 0) {
295 *size_units = 1ULL << 40;
296 } else if ((*end == 'p' || *end == 'P') &&
297 *(end + 1) == '\0' &&
298 (*size & (~0ULL << (64 - 50))) == 0) {
299 *size_units = 1ULL << 50;
300 } else if ((*end == 'e' || *end == 'E') &&
301 *(end + 1) == '\0' &&
302 (*size & (~0ULL << (64 - 60))) == 0) {
303 *size_units = 1ULL << 60;
308 *size = *size * *size_units + frac * *size_units / frac_d;
313 int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
315 struct obd_ioctl_data *overlay;
318 data->ioc_len = obd_ioctl_packlen(data);
319 data->ioc_version = OBD_IOCTL_VERSION;
321 if (*pbuf != NULL && data->ioc_len > max_len) {
322 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
323 "pbuf = %p, ioc_len = %u, max_len = %d",
324 *pbuf, data->ioc_len, max_len);
329 *pbuf = malloc(data->ioc_len);
334 overlay = (struct obd_ioctl_data *)*pbuf;
335 memcpy(*pbuf, data, sizeof(*data));
337 ptr = overlay->ioc_bulk;
338 if (data->ioc_inlbuf1) {
339 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
340 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
343 if (data->ioc_inlbuf2) {
344 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
345 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
348 if (data->ioc_inlbuf3) {
349 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
350 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
353 if (data->ioc_inlbuf4) {
354 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
355 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
361 int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
363 struct obd_ioctl_data *overlay;
369 overlay = (struct obd_ioctl_data *)pbuf;
371 /* Preserve the caller's buffer pointers */
372 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
373 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
374 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
375 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
377 memcpy(data, pbuf, sizeof(*data));
379 ptr = overlay->ioc_bulk;
380 if (data->ioc_inlbuf1) {
381 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
382 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
385 if (data->ioc_inlbuf2) {
386 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
387 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
390 if (data->ioc_inlbuf3) {
391 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
392 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
395 if (data->ioc_inlbuf4) {
396 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
397 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
404 * Verify the setstripe parameters before using.
405 * This is a pair method for comp_args_to_layout()/llapi_layout_sanity_cb()
406 * when just 1 component or a non-PFL layout is given.
408 * \param[in] param stripe parameters
409 * \param[in] pool_name pool name
410 * \param[in] fsname lustre FS name
413 * < 0, error code on failre
415 static int llapi_stripe_param_verify(const struct llapi_stripe_param *param,
416 const char **pool_name)
419 static int page_size;
422 if (page_size == 0) {
424 * 64 KB is the largest common page size (on ia64/PPC/ARM),
425 * but check the local page size just in case. The page_size
426 * will not change for the lifetime of this process at least.
428 page_size = LOV_MIN_STRIPE_SIZE;
429 if (getpagesize() > page_size) {
430 page_size = getpagesize();
431 llapi_err_noerrno(LLAPI_MSG_WARN,
432 "warning: page size (%u) larger than expected (%u)",
433 page_size, LOV_MIN_STRIPE_SIZE);
436 if (!llapi_stripe_size_is_aligned(param->lsp_stripe_size)) {
438 llapi_error(LLAPI_MSG_ERROR, rc,
439 "error: bad stripe_size %llu, must be an even multiple of %d bytes",
440 param->lsp_stripe_size, page_size);
443 if (!llapi_stripe_index_is_valid(param->lsp_stripe_offset)) {
445 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
446 param->lsp_stripe_offset);
449 if (llapi_stripe_size_is_too_big(param->lsp_stripe_size)) {
451 llapi_error(LLAPI_MSG_ERROR, rc,
452 "error: stripe size '%llu' over 4GB limit",
453 param->lsp_stripe_size);
457 count = param->lsp_stripe_count;
458 if (param->lsp_stripe_pattern == LOV_PATTERN_MDT) {
460 llapi_error(LLAPI_MSG_ERROR, rc,
461 "Invalid pattern: '-L mdt', must be specified "
465 if (!llapi_stripe_count_is_valid(count)) {
467 llapi_error(LLAPI_MSG_ERROR, rc,
468 "Invalid stripe count %d\n", count);
473 /* Make sure we have a good pool */
474 if (*pool_name != NULL) {
475 if (!llapi_pool_name_is_valid(pool_name)) {
477 llapi_error(LLAPI_MSG_ERROR, rc,
478 "Invalid Poolname '%s'", *pool_name);
488 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
493 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
495 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
499 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
501 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
506 if (!llapi_dir_hash_type_is_valid(hash_type)) {
508 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
516 * Trim a trailing newline from a string, if it exists.
518 int llapi_chomp_string(char *buf)
534 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
536 static int get_param_lov(const char *path, const char *param,
537 char *buf, size_t buf_size)
539 struct obd_uuid uuid;
542 rc = llapi_file_get_lov_uuid(path, &uuid);
546 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
551 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
553 static int get_param_lmv(const char *path, const char *param,
554 char *buf, size_t buf_size)
556 struct obd_uuid uuid;
559 rc = llapi_file_get_lmv_uuid(path, &uuid);
563 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
567 static int get_mds_md_size(const char *path)
569 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
572 * Rather than open the file and do the ioctl to get the
573 * instance name and close the file and search for the param
574 * file and open the param file and read the param file and
575 * parse the value and close the param file, let's just return
576 * a large enough value. It's 2020, RAM is cheap and this is
580 if (md_size < XATTR_SIZE_MAX)
581 md_size = XATTR_SIZE_MAX;
586 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
588 return get_param_lmv(path, "uuid", buf, bufsize);
592 * Open a Lustre file.
594 * \param name the name of the file to be opened
595 * \param flags access mode, see flags in open(2)
596 * \param mode permission of the file if it is created, see mode in open(2)
597 * \param param stripe pattern of the newly created file
599 * \retval file descriptor of opened file
600 * \retval negative errno on failure
602 int llapi_file_open_param(const char *name, int flags, mode_t mode,
603 const struct llapi_stripe_param *param)
605 struct lov_user_md *lum = NULL;
606 const char *pool_name = param->lsp_pool;
610 /* Check if the stripe pattern is sane. */
611 rc = llapi_stripe_param_verify(param, &pool_name);
615 if (param->lsp_is_specific)
616 lum_size = lov_user_md_size(param->lsp_stripe_count,
617 LOV_USER_MAGIC_SPECIFIC);
619 lum_size = sizeof(struct lov_user_md_v3);
621 lum_size = sizeof(*lum);
623 lum = calloc(1, lum_size);
628 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
630 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
631 flags = O_DIRECTORY | O_RDONLY;
638 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
643 /* Initialize IOCTL striping pattern structure */
644 lum->lmm_magic = LOV_USER_MAGIC_V1;
645 lum->lmm_pattern = param->lsp_stripe_pattern;
646 lum->lmm_stripe_size = param->lsp_stripe_size;
647 lum->lmm_stripe_count = param->lsp_stripe_count;
648 lum->lmm_stripe_offset = param->lsp_stripe_offset;
649 if (pool_name != NULL) {
650 struct lov_user_md_v3 *lumv3 = (void *)lum;
652 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
653 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
655 if (param->lsp_is_specific) {
656 struct lov_user_md_v3 *lumv3 = (void *)lum;
659 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
660 if (pool_name == NULL) {
662 * LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
663 * OST list, therefore if pool is not specified we have
664 * to pack a null pool name for placeholder.
666 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
669 for (i = 0; i < param->lsp_stripe_count; i++)
670 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
673 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
674 char errmsg[512] = "stripe already set";
677 if (errno != EEXIST && errno != EALREADY)
678 strncpy(errmsg, strerror(errno), sizeof(errmsg) - 1);
679 if (rc == -EREMOTEIO)
680 snprintf(errmsg, sizeof(errmsg),
681 "inactive OST among your specified %d OST(s)",
682 param->lsp_stripe_count);
684 llapi_err_noerrno(LLAPI_MSG_ERROR,
685 "setstripe error for '%s': %s", name, errmsg);
696 int llapi_file_is_encrypted(int fd)
701 rc = ioctl(fd, FS_IOC_GETFLAGS, &flags);
705 return !!(flags & LUSTRE_ENCRYPT_FL);
708 int llapi_file_open_pool(const char *name, int flags, int mode,
709 unsigned long long stripe_size, int stripe_offset,
710 int stripe_count, int stripe_pattern, char *pool_name)
712 const struct llapi_stripe_param param = {
713 .lsp_stripe_size = stripe_size,
714 .lsp_stripe_count = stripe_count,
715 .lsp_stripe_pattern = stripe_pattern,
716 .lsp_stripe_offset = stripe_offset,
717 .lsp_pool = pool_name
719 return llapi_file_open_param(name, flags, mode, ¶m);
722 int llapi_file_open(const char *name, int flags, int mode,
723 unsigned long long stripe_size, int stripe_offset,
724 int stripe_count, int stripe_pattern)
726 return llapi_file_open_pool(name, flags, mode, stripe_size,
727 stripe_offset, stripe_count,
728 stripe_pattern, NULL);
731 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
732 __u32 flags, char *foreign_lov)
735 struct lov_foreign_md *lfm;
738 if (foreign_lov == NULL) {
740 llapi_error(LLAPI_MSG_ERROR, rc,
741 "foreign LOV EA content must be provided");
745 len = strlen(foreign_lov);
746 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
749 llapi_error(LLAPI_MSG_ERROR, rc,
750 "foreign LOV EA size %zu (must be 0 < len < %zu)",
751 len, XATTR_SIZE_MAX -
752 offsetof(struct lov_foreign_md, lfm_value));
756 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
759 llapi_error(LLAPI_MSG_ERROR, rc,
760 "failed to allocate lov_foreign_md");
764 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
767 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
771 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
772 lfm->lfm_length = len;
773 lfm->lfm_type = type;
774 lfm->lfm_flags = flags;
775 memcpy(lfm->lfm_value, foreign_lov, len);
777 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
778 char *errmsg = "stripe already set";
782 errmsg = "not on a Lustre filesystem";
783 else if (errno == EEXIST || errno == EALREADY)
784 errmsg = "stripe already set";
786 errmsg = strerror(errno);
788 llapi_err_noerrno(LLAPI_MSG_ERROR,
789 "setstripe error for '%s': %s", name, errmsg);
805 int llapi_file_create(const char *name, unsigned long long stripe_size,
806 int stripe_offset, int stripe_count, int stripe_pattern)
810 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
811 stripe_offset, stripe_count, stripe_pattern,
820 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
821 int stripe_offset, int stripe_count,
822 int stripe_pattern, char *pool_name)
826 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
827 stripe_offset, stripe_count, stripe_pattern,
836 static int verify_dir_param(const char *name,
837 const struct llapi_stripe_param *param)
839 char fsname[MAX_OBD_NAME + 1] = { 0 };
840 char *pool_name = param->lsp_pool;
843 /* Make sure we are on a Lustre file system */
844 rc = llapi_search_fsname(name, fsname);
846 llapi_error(LLAPI_MSG_ERROR, rc,
847 "'%s' is not on a Lustre filesystem",
852 /* Check if the stripe pattern is sane. */
853 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
854 param->lsp_stripe_count,
855 param->lsp_stripe_pattern);
859 /* Make sure we have a good pool */
860 if (pool_name != NULL) {
862 * in case user gives the full pool name <fsname>.<poolname>,
865 char *ptr = strchr(pool_name, '.');
869 if (strcmp(pool_name, fsname) != 0) {
871 llapi_err_noerrno(LLAPI_MSG_ERROR,
872 "Pool '%s' is not on filesystem '%s'",
879 /* Make sure the pool exists and is non-empty */
880 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
882 char *err = rc == 0 ? "has no OSTs" : "does not exist";
884 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
885 fsname, pool_name, err);
890 /* sanity check of target list */
891 if (param->lsp_is_specific) {
892 char mdtname[MAX_OBD_NAME + 64];
896 for (i = 0; i < param->lsp_stripe_count; i++) {
897 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
898 fsname, param->lsp_tgts[i]);
899 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
904 llapi_error(LLAPI_MSG_ERROR, rc,
905 "%s: cannot find MDT %s in %s",
912 /* Make sure stripe offset is in MDT list. */
913 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
917 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
918 "%s: stripe offset '%d' is not in the target list",
919 __func__, param->lsp_stripe_offset);
927 static inline void param2lmu(struct lmv_user_md *lmu,
928 const struct llapi_stripe_param *param)
930 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
932 lmu->lum_stripe_count = param->lsp_stripe_count;
933 lmu->lum_stripe_offset = param->lsp_stripe_offset;
934 lmu->lum_hash_type = param->lsp_stripe_pattern;
935 lmu->lum_max_inherit = param->lsp_max_inherit;
936 lmu->lum_max_inherit_rr = param->lsp_max_inherit_rr;
937 if (param->lsp_pool != NULL)
938 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
939 if (param->lsp_is_specific) {
942 for (i = 0; i < param->lsp_stripe_count; i++)
943 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
947 int llapi_dir_set_default_lmv(const char *name,
948 const struct llapi_stripe_param *param)
950 struct lmv_user_md lmu = { 0 };
954 rc = verify_dir_param(name, param);
958 /* TODO: default lmv doesn't support specific targets yet */
959 if (param->lsp_is_specific)
962 param2lmu(&lmu, param);
964 fd = open(name, O_DIRECTORY | O_RDONLY);
967 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
971 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
973 char *errmsg = "stripe already set";
976 if (errno != EEXIST && errno != EALREADY)
977 errmsg = strerror(errno);
979 llapi_err_noerrno(LLAPI_MSG_ERROR,
980 "default dirstripe error on '%s': %s",
987 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
988 int stripe_count, int stripe_pattern,
989 const char *pool_name)
991 const struct llapi_stripe_param param = {
992 .lsp_stripe_count = stripe_count,
993 .lsp_stripe_offset = stripe_offset,
994 .lsp_stripe_pattern = stripe_pattern,
995 .lsp_pool = (char *)pool_name
998 return llapi_dir_set_default_lmv(name, ¶m);
1002 * Create a Lustre directory.
1004 * \param name the name of the directory to be created
1005 * \param mode permission of the file if it is created, see mode in open(2)
1006 * \param param stripe pattern of the newly created directory
1008 * \retval 0 on success
1009 * \retval negative errno on failure
1011 int llapi_dir_create(const char *name, mode_t mode,
1012 const struct llapi_stripe_param *param)
1014 struct lmv_user_md *lmu = NULL;
1016 struct obd_ioctl_data data = { 0 };
1019 char *dirpath = NULL;
1020 char *namepath = NULL;
1025 rc = verify_dir_param(name, param);
1029 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1030 param->lsp_is_specific ?
1031 LMV_USER_MAGIC_SPECIFIC :
1034 lmu = calloc(1, lmu_size);
1038 dirpath = strdup(name);
1044 namepath = strdup(name);
1051 param2lmu(lmu, param);
1053 filename = basename(namepath);
1054 dir = dirname(dirpath);
1056 data.ioc_inlbuf1 = (char *)filename;
1057 data.ioc_inllen1 = strlen(filename) + 1;
1058 data.ioc_inlbuf2 = (char *)lmu;
1059 data.ioc_inllen2 = lmu_size;
1060 data.ioc_type = mode;
1061 if (param->lsp_is_create)
1062 /* borrow obdo1.o_flags to store this flag */
1063 data.ioc_obdo1.o_flags = OBD_FL_OBDMDEXISTS;
1064 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1066 llapi_error(LLAPI_MSG_ERROR, rc,
1067 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1072 fd = open(dir, O_DIRECTORY | O_RDONLY);
1075 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1079 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1080 char *errmsg = "stripe already set";
1083 if (errno != EEXIST && errno != EALREADY)
1084 errmsg = strerror(errno);
1086 llapi_err_noerrno(LLAPI_MSG_ERROR,
1087 "dirstripe error on '%s': %s", name, errmsg);
1098 * Create a foreign directory.
1100 * \param name the name of the directory to be created
1101 * \param mode permission of the file if it is created, see mode in open(2)
1102 * \param type foreign type to be set in LMV EA
1103 * \param flags foreign flags to be set in LMV EA
1104 * \param value foreign pattern to be set in LMV EA
1106 * \retval 0 on success
1107 * \retval negative errno on failure
1109 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1110 __u32 flags, const char *value)
1112 struct lmv_foreign_md *lfm = NULL;
1113 size_t lfm_size, len;
1114 struct obd_ioctl_data data = { 0 };
1117 char *dirpath = NULL;
1118 char *namepath = NULL;
1123 len = strlen(value);
1124 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1127 llapi_error(LLAPI_MSG_ERROR, rc,
1128 "invalid LOV EA length %zu (must be 0 < len < %zu)",
1129 len, XATTR_SIZE_MAX -
1130 offsetof(struct lmv_foreign_md, lfm_value));
1133 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1134 lfm = calloc(1, lfm_size);
1138 dirpath = strdup(name);
1144 namepath = strdup(name);
1151 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1152 lfm->lfm_length = len;
1153 lfm->lfm_type = type;
1154 lfm->lfm_flags = flags;
1155 memcpy(lfm->lfm_value, value, len);
1157 filename = basename(namepath);
1158 dir = dirname(dirpath);
1160 data.ioc_inlbuf1 = (char *)filename;
1161 data.ioc_inllen1 = strlen(filename) + 1;
1162 data.ioc_inlbuf2 = (char *)lfm;
1163 data.ioc_inllen2 = lfm_size;
1164 data.ioc_type = mode;
1165 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1167 llapi_error(LLAPI_MSG_ERROR, rc,
1168 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1173 fd = open(dir, O_DIRECTORY | O_RDONLY);
1176 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1180 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1181 char *errmsg = "stripe already set";
1184 if (errno != EEXIST && errno != EALREADY)
1185 errmsg = strerror(errno);
1187 llapi_err_noerrno(LLAPI_MSG_ERROR,
1188 "dirstripe error on '%s': %s", name, errmsg);
1198 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1199 int stripe_count, int stripe_pattern,
1200 const char *pool_name)
1202 const struct llapi_stripe_param param = {
1203 .lsp_stripe_count = stripe_count,
1204 .lsp_stripe_offset = stripe_offset,
1205 .lsp_stripe_pattern = stripe_pattern,
1206 .lsp_pool = (char *)pool_name
1209 return llapi_dir_create(name, mode, ¶m);
1213 * Find the fsname, the full path, and/or an open fd.
1214 * Either the fsname or path must not be NULL
1216 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1219 char buf[PATH_MAX], mntdir[PATH_MAX];
1220 char *ptr, *ptr_end;
1222 int idx = 0, mntlen = 0, fd;
1224 int fsnamelen, mountlen;
1226 /* get the mount point */
1227 fp = setmntent(PROC_MOUNTS, "r");
1230 llapi_error(LLAPI_MSG_ERROR, rc,
1231 "cannot retrieve filesystem mount point");
1235 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1238 if (!llapi_is_lustre_mnt(&mnt))
1241 if ((want & WANT_INDEX) && (idx++ != index))
1244 mntlen = strlen(mnt.mnt_dir);
1245 ptr = strchr(mnt.mnt_fsname, '/');
1246 while (ptr && *ptr == '/')
1249 * thanks to the call to llapi_is_lustre_mnt() above,
1250 * we are sure that mnt.mnt_fsname contains ":/",
1251 * so ptr should never be NULL
1256 while (*ptr_end != '/' && *ptr_end != '\0')
1259 /* Check the fsname for a match, if given */
1260 mountlen = ptr_end - ptr;
1261 if (!(want & WANT_FSNAME) && fsname != NULL &&
1262 (fsnamelen = strlen(fsname)) > 0 &&
1263 (fsnamelen != mountlen ||
1264 (strncmp(ptr, fsname, mountlen) != 0)))
1267 /* If the path isn't set return the first one we find */
1268 if (path == NULL || strlen(path) == 0) {
1269 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1270 mntdir[sizeof(mntdir) - 1] = '\0';
1271 if ((want & WANT_FSNAME) && fsname != NULL) {
1272 strncpy(fsname, ptr, mountlen);
1273 fsname[mountlen] = '\0';
1277 /* Otherwise find the longest matching path */
1278 } else if ((strlen(path) >= mntlen) &&
1279 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1280 /* check the path format */
1281 if (strlen(path) > mntlen && path[mntlen] != '/')
1283 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1284 mntdir[sizeof(mntdir) - 1] = '\0';
1285 if ((want & WANT_FSNAME) && fsname != NULL) {
1286 strncpy(fsname, ptr, mountlen);
1287 fsname[mountlen] = '\0';
1297 if ((want & WANT_PATH) && path != NULL) {
1298 strncpy(path, mntdir, mntlen);
1299 path[mntlen] = '\0';
1301 if (want & WANT_FD) {
1302 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1305 llapi_error(LLAPI_MSG_ERROR, rc,
1306 "cannot open '%s'", mntdir);
1312 } else if (want & WANT_ERROR)
1313 llapi_err_noerrno(LLAPI_MSG_ERROR,
1314 "'%s' not on a mounted Lustre filesystem",
1315 (want & WANT_PATH) ? fsname : path);
1320 * search lustre mounts
1322 * Calling this function will return to the user the mount point, mntdir, and
1323 * the file system name, fsname, if the user passed a buffer to this routine.
1325 * The user inputs are pathname and index. If the pathname is supplied then
1326 * the value of the index will be ignored. The pathname will return data if
1327 * the pathname is located on a lustre mount. Index is used to pick which
1328 * mount point you want in the case of multiple mounted lustre file systems.
1329 * See function lfs_osts in lfs.c for an example of the index use.
1331 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1334 int want = WANT_PATH, idx = -1;
1336 if (!pathname || pathname[0] == '\0') {
1340 strcpy(mntdir, pathname);
1344 want |= WANT_FSNAME;
1345 return get_root_path(want, fsname, NULL, mntdir, idx);
1348 /* Given a path, find the corresponding Lustre fsname */
1349 int llapi_search_fsname(const char *pathname, char *fsname)
1354 path = realpath(pathname, NULL);
1356 char tmp[PATH_MAX - 1];
1362 if (pathname[0] != '/') {
1364 * Need an absolute path, but realpath() only works for
1365 * pathnames that actually exist. We go through the
1366 * extra hurdle of dirname(getcwd() + pathname) in
1367 * case the relative pathname contains ".." in it.
1369 char realpath[PATH_MAX - 1];
1371 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1373 llapi_error(LLAPI_MSG_ERROR, rc,
1374 "cannot get current working directory");
1378 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1379 if (rc >= sizeof(tmp)) {
1381 llapi_error(LLAPI_MSG_ERROR, rc,
1382 "invalid parent path '%s'",
1388 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1389 if (rc >= sizeof(buf)) {
1391 llapi_error(LLAPI_MSG_ERROR, rc,
1392 "invalid path '%s'", pathname);
1395 path = realpath(buf, NULL);
1397 ptr = strrchr(buf, '/');
1399 llapi_error(LLAPI_MSG_ERROR |
1400 LLAPI_MSG_NO_ERRNO, 0,
1401 "cannot resolve path '%s'",
1406 path = realpath(buf, NULL);
1409 llapi_error(LLAPI_MSG_ERROR, rc,
1410 "cannot resolve path '%s'",
1416 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1421 int llapi_search_rootpath(char *pathname, const char *fsname)
1424 * pathname can be used as an argument by get_root_path(),
1425 * clear it for safety
1428 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1432 * Get the list of pool members.
1433 * \param poolname string of format \<fsname\>.\<poolname\>
1434 * \param members caller-allocated array of char*
1435 * \param list_size size of the members array
1436 * \param buffer caller-allocated buffer for storing OST names
1437 * \param buffer_size size of the buffer
1439 * \return number of members retrieved for this pool
1440 * \retval -error failure
1442 int llapi_get_poolmembers(const char *poolname, char **members,
1443 int list_size, char *buffer, int buffer_size)
1445 char fsname[PATH_MAX];
1454 /* name is FSNAME.POOLNAME */
1455 if (strlen(poolname) >= sizeof(fsname))
1458 snprintf(fsname, sizeof(fsname), "%s", poolname);
1459 pool = strchr(fsname, '.');
1466 rc = poolpath(&pathname, fsname, NULL);
1468 llapi_error(LLAPI_MSG_ERROR, rc,
1469 "Lustre filesystem '%s' not found",
1474 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1475 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1476 cfs_free_param_data(&pathname);
1477 if (rc >= sizeof(buf))
1479 fd = fopen(buf, "r");
1482 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1487 while (fgets(buf, sizeof(buf), fd) != NULL) {
1488 if (nb_entries >= list_size) {
1492 buf[sizeof(buf) - 1] = '\0';
1494 tmp = strchr(buf, '\n');
1497 if (used + strlen(buf) + 1 > buffer_size) {
1502 strcpy(buffer + used, buf);
1503 members[nb_entries] = buffer + used;
1504 used += strlen(buf) + 1;
1514 * Get the list of pools in a filesystem.
1515 * \param name filesystem name or path
1516 * \param poollist caller-allocated array of char*
1517 * \param list_size size of the poollist array
1518 * \param buffer caller-allocated buffer for storing pool names
1519 * \param buffer_size size of the buffer
1521 * \return number of pools retrieved for this filesystem
1522 * \retval -error failure
1524 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1525 char *buffer, int buffer_size)
1531 struct dirent *pool;
1533 unsigned int nb_entries = 0;
1534 unsigned int used = 0;
1537 /* initialize output array */
1538 for (i = 0; i < list_size; i++)
1541 /* is name a pathname ? */
1542 ptr = strchr(name, '/');
1544 char fsname_buf[MAXNAMLEN];
1546 /* We will need fsname for printing later */
1547 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1551 ptr = strrchr(fsname_buf, '-');
1555 fsname = strdup(fsname_buf);
1559 /* name is FSNAME */
1560 fsname = strdup(name);
1565 rc = poolpath(&pathname, fsname, NULL);
1567 llapi_error(LLAPI_MSG_ERROR, rc,
1568 "Lustre filesystem '%s' not found", name);
1572 dir = opendir(pathname.gl_pathv[0]);
1575 llapi_error(LLAPI_MSG_ERROR, rc,
1576 "Could not open pool list for '%s'",
1583 pool = readdir(dir);
1589 /* ignore . and .. */
1590 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1593 /* check output bounds */
1594 if (nb_entries >= list_size) {
1596 goto free_dir_no_msg;
1599 /* +2 for '.' and final '\0' */
1600 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1603 goto free_dir_no_msg;
1606 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1607 poollist[nb_entries] = buffer + used;
1608 used += strlen(pool->d_name) + strlen(fsname) + 2;
1614 llapi_error(LLAPI_MSG_ERROR, rc,
1615 "Error reading pool list for '%s'", name);
1617 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1622 cfs_free_param_data(&pathname);
1625 return rc != 0 ? rc : nb_entries;
1628 /* wrapper for lfs.c and obd.c */
1629 int llapi_poollist(const char *name)
1631 int poolcount, rc, i;
1634 rc = llapi_get_poolbuf(name, &buf, &pools, &poolcount);
1638 for (i = 0; i < poolcount; i++)
1639 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", pools[i]);
1646 * Get buffer that holds uuids and the list of pools in a filesystem.
1648 * \param name filesystem name or path
1649 * \param buf bufffer that has to be freed if function returns 0
1650 * \param pools pointer to the list of pools in buffer
1651 * \param poolcount number of pools
1653 * \return 0 when found at least 1 pool, i.e. poolcount > 0
1654 * \retval -error failure
1656 int llapi_get_poolbuf(const char *name, char **buf,
1657 char ***pools, int *poolcount)
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;
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 rc = (nb < 0 ? nb : 0);
1725 /* Don't free buffer, it will be used later */
1728 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1733 typedef int (semantic_func_t)(char *path, int p, int *d,
1734 void *data, struct dirent64 *de);
1736 #define OBD_NOT_FOUND (-1)
1738 static bool lmv_is_foreign(__u32 magic)
1740 return magic == LMV_MAGIC_FOREIGN;
1743 static void find_param_fini(struct find_param *param)
1745 if (param->fp_migrate)
1748 if (param->fp_obd_indexes) {
1749 free(param->fp_obd_indexes);
1750 param->fp_obd_indexes = NULL;
1753 if (param->fp_lmd) {
1754 free(param->fp_lmd);
1755 param->fp_lmd = NULL;
1758 if (param->fp_lmv_md) {
1759 free(param->fp_lmv_md);
1760 param->fp_lmv_md = NULL;
1764 static int common_param_init(struct find_param *param, char *path)
1766 int lum_size = get_mds_md_size(path);
1771 /* migrate has fp_lmv_md initialized outside */
1772 if (param->fp_migrate)
1775 if (lum_size < PATH_MAX + 1)
1776 lum_size = PATH_MAX + 1;
1778 param->fp_lum_size = lum_size;
1779 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1781 if (param->fp_lmd == NULL) {
1782 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1783 "error: allocate %zu bytes for layout failed",
1784 sizeof(lstat_t) + param->fp_lum_size);
1788 param->fp_lmv_stripe_count = 256;
1789 param->fp_lmv_md = calloc(1,
1790 lmv_user_md_size(param->fp_lmv_stripe_count,
1791 LMV_USER_MAGIC_SPECIFIC));
1792 if (param->fp_lmv_md == NULL) {
1793 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1794 "error: allocation of %d bytes for ioctl",
1795 lmv_user_md_size(param->fp_lmv_stripe_count,
1796 LMV_USER_MAGIC_SPECIFIC));
1797 find_param_fini(param);
1801 param->fp_got_uuids = 0;
1802 param->fp_obd_indexes = NULL;
1803 param->fp_obd_index = OBD_NOT_FOUND;
1804 param->fp_mdt_index = OBD_NOT_FOUND;
1808 static int cb_common_fini(char *path, int p, int *dp, void *data,
1809 struct dirent64 *de)
1811 struct find_param *param = data;
1817 /* set errno upon failure */
1818 static int open_parent(const char *path)
1824 path_copy = strdup(path);
1825 if (path_copy == NULL)
1828 parent_path = dirname(path_copy);
1829 parent = open(parent_path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1835 static int cb_get_dirstripe(char *path, int *d, struct find_param *param)
1838 bool did_nofollow = false;
1843 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1844 if (param->fp_get_default_lmv) {
1848 /* open() may not fetch LOOKUP lock, statx() to ensure dir depth
1851 statx(*d, "", AT_EMPTY_PATH, STATX_MODE, &stx);
1857 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1859 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1862 ret = ioctl(*d, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1864 /* if ENOTTY likely to be a fake symlink, so try again after
1865 * new open() with O_NOFOLLOW, but only once to prevent any
1866 * loop like for the path of a file/dir not on Lustre !!
1868 if (ret < 0 && errno == ENOTTY && !did_nofollow) {
1871 did_nofollow = true;
1872 fd = open(path, O_RDONLY | O_NOFOLLOW);
1874 /* restore original errno */
1879 /* close original fd and set new */
1882 ret2 = ioctl(fd, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1883 if (ret2 < 0 && errno != E2BIG) {
1884 /* restore original errno */
1888 /* LMV is ok or need to handle E2BIG case now */
1892 if (errno == E2BIG && ret != 0) {
1896 /* if foreign LMV case, fake stripes number */
1897 if (lmv_is_foreign(param->fp_lmv_md->lum_magic)) {
1898 struct lmv_foreign_md *lfm;
1900 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1901 if (lfm->lfm_length < XATTR_SIZE_MAX -
1902 offsetof(typeof(*lfm), lfm_value)) {
1903 uint32_t size = lfm->lfm_length +
1904 offsetof(typeof(*lfm), lfm_value);
1906 stripe_count = lmv_foreign_to_md_stripes(size);
1908 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1909 "error: invalid %d foreign size returned from ioctl",
1914 stripe_count = param->fp_lmv_md->lum_stripe_count;
1916 if (stripe_count <= param->fp_lmv_stripe_count)
1919 free(param->fp_lmv_md);
1920 param->fp_lmv_stripe_count = stripe_count;
1921 lmv_size = lmv_user_md_size(stripe_count,
1922 LMV_USER_MAGIC_SPECIFIC);
1923 param->fp_lmv_md = malloc(lmv_size);
1924 if (param->fp_lmv_md == NULL) {
1925 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1926 "error: allocation of %d bytes for ioctl",
1927 lmv_user_md_size(param->fp_lmv_stripe_count,
1928 LMV_USER_MAGIC_SPECIFIC));
1937 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1940 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
1941 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
1942 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
1943 lmd_v2->lmd_stx.stx_uid = st->st_uid;
1944 lmd_v2->lmd_stx.stx_gid = st->st_gid;
1945 lmd_v2->lmd_stx.stx_mode = st->st_mode;
1946 lmd_v2->lmd_stx.stx_ino = st->st_ino;
1947 lmd_v2->lmd_stx.stx_size = st->st_size;
1948 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
1949 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
1950 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
1951 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
1952 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
1953 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
1954 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
1955 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
1956 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
1958 lmd_v2->lmd_flags = 0;
1960 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1962 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
1963 if (lmd_v2->lmd_stx.stx_size)
1964 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
1965 if (lmd_v2->lmd_stx.stx_blocks)
1966 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
1968 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1969 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
1970 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
1975 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
1977 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
1978 struct lov_user_mds_data *lmd_v2 = lmdbuf;
1982 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
1986 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
1989 st = lmd_v1->lmd_st;
1990 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
1991 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
1992 convert_lmd_statx(lmd_v2, &st, false);
1993 lmd_v2->lmd_lmmsize = 0;
1994 lmd_v2->lmd_padding = 0;
1999 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
2000 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
2002 struct lov_user_mds_data *lmd = lmdbuf;
2003 static bool use_old_ioctl;
2007 if (parent_fd < 0 && dir_fd < 0)
2009 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
2014 * LL_IOC_MDC_GETINFO operates on the current directory inode
2015 * and returns struct lov_user_mds_data, while
2016 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
2018 if (type == GET_LMD_INFO)
2019 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_V1 :
2020 LL_IOC_MDC_GETINFO_V2;
2022 cmd = LL_IOC_LOV_GETSTRIPE;
2025 ret = ioctl(dir_fd, cmd, lmdbuf);
2026 if (ret < 0 && errno == ENOTTY &&
2027 cmd == LL_IOC_MDC_GETINFO_V2) {
2028 cmd = LL_IOC_MDC_GETINFO_V1;
2029 use_old_ioctl = true;
2033 if (cmd == LL_IOC_MDC_GETINFO_V1 && !ret)
2034 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2036 if (ret < 0 && errno == ENOTTY && type == GET_LMD_STRIPE) {
2039 /* retry ioctl() after new open() with O_NOFOLLOW
2040 * just in case it could be a fake symlink
2041 * need using a new open() as dir_fd is being closed
2045 dir_fd2 = open(path, O_RDONLY | O_NDELAY | O_NOFOLLOW);
2047 /* return original error */
2050 ret = ioctl(dir_fd2, cmd, lmdbuf);
2051 /* pass new errno or success back to caller */
2057 } else if (parent_fd >= 0) {
2058 const char *fname = strrchr(path, '/');
2061 * IOC_MDC_GETFILEINFO takes as input the filename (relative to
2062 * the parent directory) and returns struct lov_user_mds_data,
2063 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2065 * This avoids opening, locking, and closing each file on the
2066 * client if that is not needed. Multiple of these ioctl() can
2067 * be done on the parent dir with a single open for all
2068 * files in that directory, and it also doesn't pollute the
2069 * client dcache with millions of dentries when traversing
2070 * a large filesystem.
2072 fname = (fname == NULL ? path : fname + 1);
2074 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2077 else if (ret >= lmdlen || ret++ == 0)
2080 if (type == GET_LMD_INFO)
2081 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_V1 :
2082 IOC_MDC_GETFILEINFO_V2;
2084 cmd = IOC_MDC_GETFILESTRIPE;
2087 ret = ioctl(parent_fd, cmd, lmdbuf);
2088 if (ret < 0 && errno == ENOTTY &&
2089 cmd == IOC_MDC_GETFILEINFO_V2) {
2090 cmd = IOC_MDC_GETFILEINFO_V1;
2091 use_old_ioctl = true;
2092 goto retry_getfileinfo;
2095 if (cmd == IOC_MDC_GETFILEINFO_V1 && !ret)
2096 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2100 if (ret && type == GET_LMD_INFO) {
2101 if (errno == ENOTTY) {
2105 * ioctl is not supported, it is not a lustre fs.
2106 * Do the regular lstat(2) instead.
2108 ret = lstat_f(path, &st);
2111 llapi_error(LLAPI_MSG_ERROR, ret,
2112 "error: %s: lstat failed for %s",
2116 convert_lmd_statx(lmd, &st, true);
2118 * It may be wrong to set use_old_ioctl with true as
2119 * the file is not a lustre fs. So reset it with false
2122 use_old_ioctl = false;
2123 } else if (errno == ENOENT) {
2125 llapi_error(LLAPI_MSG_WARN, ret,
2126 "warning: %s does not exist", path);
2127 } else if (errno != EISDIR && errno != ENODATA) {
2129 llapi_error(LLAPI_MSG_ERROR, ret,
2130 "%s ioctl failed for %s.",
2131 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2132 "IOC_MDC_GETFILEINFO", path);
2139 static int llapi_semantic_traverse(char *path, int size, int parent,
2140 semantic_func_t sem_init,
2141 semantic_func_t sem_fini, void *data,
2142 struct dirent64 *de)
2144 struct find_param *param = (struct find_param *)data;
2145 struct dirent64 *dent;
2146 int len, ret, d, p = -1;
2152 d = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
2153 /* if an invalid fake dir symlink, opendir() will return EINVAL
2154 * instead of ENOTDIR. If a valid but dangling faked or real file/dir
2155 * symlink ENOENT will be returned. For a valid/resolved fake or real
2156 * file symlink ENOTDIR will be returned as for a regular file.
2157 * opendir() will be successful for a valid and resolved fake or real
2158 * dir simlink or a regular dir.
2160 if (d == -1 && errno != ENOTDIR && errno != EINVAL && errno != ENOENT) {
2162 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2165 } else if (d == -1) {
2166 if (errno == ENOENT || errno == EINVAL) {
2167 int old_errno = errno;
2169 /* try to open with O_NOFOLLOW this will help
2170 * differentiate fake vs real symlinks
2171 * it is ok to not use O_DIRECTORY with O_RDONLY
2172 * and it will prevent the need to deal with ENOTDIR
2173 * error, instead of ELOOP, being returned by recent
2174 * kernels for real symlinks
2176 d = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2177 /* if a dangling real symlink should return ELOOP, or
2178 * again ENOENT if really non-existing path, or E...??
2179 * So return original error. If success or ENOTDIR, path
2180 * is likely to be a fake dir/file symlink, so continue
2190 if (parent == -1 && d == -1) {
2191 /* Open the parent dir. */
2192 p = open_parent(path);
2198 } else { /* d != -1 */
2201 /* try to reopen dir with O_NOFOLLOW just in case of a foreign
2204 d2 = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2209 /* continue with d */
2215 ret = sem_init(path, (parent != -1) ? parent : p, &d, data, de);
2225 /* ENOTDIR if fake symlink, do not consider it as an error */
2226 if (errno != ENOTDIR)
2227 llapi_error(LLAPI_MSG_ERROR, errno,
2228 "fdopendir() failed");
2235 while ((dent = readdir64(dir)) != NULL) {
2238 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2242 if ((len + dent->d_reclen + 2) > size) {
2243 llapi_err_noerrno(LLAPI_MSG_ERROR,
2244 "error: %s: string buffer too small for %s",
2249 strcat(path, dent->d_name);
2251 if (dent->d_type == DT_UNKNOWN) {
2252 struct lov_user_mds_data *lmd = param->fp_lmd;
2254 rc = get_lmd_info_fd(path, d, -1, param->fp_lmd,
2255 param->fp_lum_size, GET_LMD_INFO);
2257 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2264 switch (dent->d_type) {
2266 llapi_err_noerrno(LLAPI_MSG_ERROR,
2267 "error: %s: '%s' is UNKNOWN type %d",
2268 __func__, dent->d_name, dent->d_type);
2271 rc = llapi_semantic_traverse(path, size, d, sem_init,
2272 sem_fini, data, dent);
2273 if (rc != 0 && ret == 0)
2275 if (rc < 0 && rc != -EALREADY &&
2276 param->fp_stop_on_error)
2282 rc = sem_init(path, d, NULL, data, dent);
2283 if (rc < 0 && ret == 0) {
2285 if (rc && rc != -EALREADY &&
2286 param->fp_stop_on_error)
2291 if (sem_fini && rc == 0)
2292 sem_fini(path, d, NULL, data, dent);
2300 sem_fini(path, parent, &d, data, de);
2313 static int param_callback(char *path, semantic_func_t sem_init,
2314 semantic_func_t sem_fini, struct find_param *param)
2316 int ret, len = strlen(path);
2319 if (len > PATH_MAX) {
2321 llapi_error(LLAPI_MSG_ERROR, ret,
2322 "Path name '%s' is too long", path);
2326 buf = (char *)malloc(2 * PATH_MAX);
2330 snprintf(buf, PATH_MAX + 1, "%s", path);
2331 ret = common_param_init(param, buf);
2335 param->fp_depth = 0;
2337 ret = llapi_semantic_traverse(buf, 2 * PATH_MAX, -1, sem_init,
2338 sem_fini, param, NULL);
2340 find_param_fini(param);
2342 return ret < 0 ? ret : 0;
2345 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2349 rc = ioctl(fd, OBD_IOC_GETDTNAME, lov_name);
2350 if (rc && errno == ENOTTY)
2351 rc = ioctl(fd, OBD_IOC_GETNAME_OLD, lov_name);
2354 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2360 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2364 rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2367 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2373 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2377 /* do not follow faked symlinks */
2378 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
2380 /* real symlink should have failed with ELOOP so retry without
2381 * O_NOFOLLOW just in case
2383 fd = open(path, O_RDONLY | O_NONBLOCK);
2386 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'",
2392 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2398 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2402 fd = open(path, O_RDONLY | O_NONBLOCK);
2405 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2409 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2421 * If uuidp is NULL, return the number of available obd uuids.
2422 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2423 * there are more OSTs than allocated to uuidp, then an error is returned with
2424 * the ost_count set to number of available obd uuids.
2426 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2427 int *ost_count, enum tgt_type type)
2429 char buf[PATH_MAX], format[32];
2430 int rc = 0, index = 0;
2431 struct obd_uuid name;
2435 /* Get the lov name */
2436 if (type == LOV_TYPE)
2437 rc = llapi_file_fget_lov_uuid(fd, &name);
2439 rc = llapi_file_fget_lmv_uuid(fd, &name);
2443 /* Now get the ost uuids */
2444 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2445 FILTER_BY_EXACT, "target_obd", ¶m);
2449 fp = fopen(param.gl_pathv[0], "r");
2452 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2457 snprintf(format, sizeof(format),
2458 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2459 while (fgets(buf, sizeof(buf), fp) != NULL) {
2460 if (uuidp && (index < *ost_count)) {
2461 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2469 if (uuidp && (index > *ost_count))
2474 cfs_free_param_data(¶m);
2478 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2480 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2483 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2488 root = open(mnt, O_RDONLY | O_DIRECTORY);
2491 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2496 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2505 * Check if user specified value matches a real uuid. Ignore _UUID,
2506 * -osc-4ba41334, other trailing gunk in comparison.
2507 * @param real_uuid ends in "_UUID"
2508 * @param search_uuid may or may not end in "_UUID"
2510 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2512 int cmplen = strlen(real_uuid);
2513 int searchlen = strlen(search_uuid);
2515 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2517 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2521 * The UUIDs may legitimately be different lengths, if
2522 * the system was upgraded from an older version.
2524 if (cmplen != searchlen)
2527 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2531 * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2532 * returned in param->fp_obd_index
2534 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2536 struct obd_uuid obd_uuid;
2543 if (param->fp_got_uuids)
2546 /* Get the lov/lmv name */
2547 if (param->fp_get_lmv)
2548 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2550 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2552 if (rc != -ENOTTY) {
2553 llapi_error(LLAPI_MSG_ERROR, rc,
2554 "error: can't get %s name: %s",
2555 param->fp_get_lmv ? "lmv" : "lov",
2563 param->fp_got_uuids = 1;
2565 /* Now get the ost uuids */
2566 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2567 obd_uuid.uuid, FILTER_BY_EXACT,
2568 "target_obd", ¶m_data);
2572 fp = fopen(param_data.gl_pathv[0], "r");
2575 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2576 param_data.gl_pathv[0]);
2580 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2581 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2582 param->fp_get_lmv ? "MDTS" : "OBDS");
2584 snprintf(format, sizeof(format),
2585 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2586 while (fgets(buf, sizeof(buf), fp) != NULL) {
2589 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2592 if (param->fp_obd_uuid) {
2593 if (llapi_uuid_match(obd_uuid.uuid,
2594 param->fp_obd_uuid->uuid)) {
2595 param->fp_obd_index = index;
2598 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2599 /* Print everything */
2600 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2603 param->fp_obds_printed = 1;
2607 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2608 llapi_err_noerrno(LLAPI_MSG_ERROR,
2609 "error: %s: unknown obduuid: %s",
2610 __func__, param->fp_obd_uuid->uuid);
2614 cfs_free_param_data(¶m_data);
2619 * In this case, param->fp_obd_uuid will be an array of obduuids and
2620 * obd index for all these obduuids will be returned in
2621 * param->fp_obd_indexes
2623 static int setup_indexes(int d, char *path, struct obd_uuid *obduuids,
2624 int num_obds, int **obdindexes, int *obdindex,
2627 int ret, obdcount, obd_valid = 0, obdnum;
2629 struct obd_uuid *uuids = NULL;
2633 if (type == LOV_TYPE)
2634 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2636 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2640 obdcount = atoi(buf);
2641 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2646 ret = llapi_get_target_uuids(d, uuids, &obdcount, type);
2648 if (ret == -EOVERFLOW) {
2649 struct obd_uuid *uuids_temp;
2651 uuids_temp = realloc(uuids, obdcount *
2652 sizeof(struct obd_uuid));
2653 if (uuids_temp != NULL) {
2655 goto retry_get_uuids;
2660 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2664 indexes = malloc(num_obds * sizeof(*obdindex));
2665 if (indexes == NULL) {
2670 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2673 /* The user may have specified a simple index */
2674 i = strtol(obduuids[obdnum].uuid, &end, 0);
2675 if (end && *end == '\0' && i < obdcount) {
2676 indexes[obdnum] = i;
2679 for (i = 0; i < obdcount; i++) {
2680 if (llapi_uuid_match(uuids[i].uuid,
2681 obduuids[obdnum].uuid)) {
2682 indexes[obdnum] = i;
2688 if (i >= obdcount) {
2689 indexes[obdnum] = OBD_NOT_FOUND;
2690 llapi_err_noerrno(LLAPI_MSG_ERROR,
2691 "invalid obduuid '%s'",
2692 obduuids[obdnum].uuid);
2698 *obdindex = OBD_NOT_FOUND;
2700 *obdindex = obd_valid;
2702 *obdindexes = indexes;
2710 static int setup_target_indexes(int d, char *path, struct find_param *param)
2714 if (param->fp_mdt_uuid) {
2715 ret = setup_indexes(d, path, param->fp_mdt_uuid,
2717 ¶m->fp_mdt_indexes,
2718 ¶m->fp_mdt_index, LMV_TYPE);
2723 if (param->fp_obd_uuid) {
2724 ret = setup_indexes(d, path, param->fp_obd_uuid,
2726 ¶m->fp_obd_indexes,
2727 ¶m->fp_obd_index, LOV_TYPE);
2732 param->fp_got_uuids = 1;
2737 int llapi_ostlist(char *path, struct find_param *param)
2742 fd = open(path, O_RDONLY | O_DIRECTORY);
2746 ret = setup_obd_uuid(fd, path, param);
2753 * Tries to determine the default stripe attributes for a given filesystem. The
2754 * filesystem to check should be specified by fsname, or will be determined
2757 static int sattr_get_defaults(const char *const fsname,
2758 unsigned int *scount,
2759 unsigned int *ssize,
2760 unsigned int *soffset)
2766 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2767 "stripecount", val, sizeof(val));
2770 *scount = atoi(val);
2774 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2775 "stripesize", val, sizeof(val));
2782 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2783 "stripeoffset", val, sizeof(val));
2786 *soffset = atoi(val);
2793 * Tries to gather the default stripe attributes for a given filesystem. If
2794 * the attributes can be determined, they are cached for easy retreival the
2795 * next time they are needed. Only a single filesystem's attributes are
2798 int sattr_cache_get_defaults(const char *const fsname,
2799 const char *const pathname, unsigned int *scount,
2800 unsigned int *ssize, unsigned int *soffset)
2803 char fsname[PATH_MAX + 1];
2804 unsigned int stripecount;
2805 unsigned int stripesize;
2806 unsigned int stripeoffset;
2812 char fsname_buf[PATH_MAX + 1];
2813 unsigned int tmp[3];
2815 if (fsname == NULL) {
2816 rc = llapi_search_fsname(pathname, fsname_buf);
2820 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2823 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2825 * Ensure all 3 sattrs (count, size, and offset) are
2826 * successfully retrieved and stored in tmp before writing to
2829 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2833 cache.stripecount = tmp[0];
2834 cache.stripesize = tmp[1];
2835 cache.stripeoffset = tmp[2];
2836 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2840 *scount = cache.stripecount;
2842 *ssize = cache.stripesize;
2844 *soffset = cache.stripeoffset;
2849 static char *layout2name(__u32 layout_pattern)
2851 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2853 else if (layout_pattern == LOV_PATTERN_MDT)
2855 else if (layout_pattern == LOV_PATTERN_RAID0)
2857 else if (layout_pattern ==
2858 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2859 return "raid0,overstriped";
2864 enum lov_dump_flags {
2865 LDF_IS_DIR = 0x0001,
2866 LDF_IS_RAW = 0x0002,
2867 LDF_INDENT = 0x0004,
2868 LDF_SKIP_OBJS = 0x0008,
2870 LDF_EXTENSION = 0x0020,
2873 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2874 struct lov_user_ost_data_v1 *objects,
2875 enum llapi_layout_verbose verbose,
2876 int depth, char *pool_name,
2877 enum lov_dump_flags flags)
2879 bool is_dir = flags & LDF_IS_DIR;
2880 bool is_raw = flags & LDF_IS_RAW;
2881 bool indent = flags & LDF_INDENT;
2882 bool yaml = flags & LDF_YAML;
2883 bool skip_objs = flags & LDF_SKIP_OBJS;
2884 bool extension = flags & LDF_EXTENSION;
2885 char *prefix = is_dir ? "" : "lmm_";
2886 char *separator = "";
2887 char *space = indent ? " " : "";
2890 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2891 lmm_oi_set_seq(&lum->lmm_oi, 0);
2892 if (!indent && (verbose & VERBOSE_DETAIL))
2893 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2896 if (!yaml && !indent && depth && path &&
2897 ((verbose != VERBOSE_OBJID) || !is_dir))
2898 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2900 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2901 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2902 space, prefix, lum->lmm_magic);
2903 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2905 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2906 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2908 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2911 if (verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) {
2916 if (verbose & ~VERBOSE_DFID)
2917 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2921 struct lu_fid dir_fid;
2923 rc = llapi_path2fid(path, &dir_fid);
2925 llapi_error(LLAPI_MSG_ERROR, rc,
2926 "Cannot determine directory fid.");
2928 seq = dir_fid.f_seq;
2929 oid = dir_fid.f_oid;
2930 ver = dir_fid.f_ver;
2933 * This needs a bit of hand-holding since old 1.x
2934 * lmm_oi have { oi.oi_id = mds_inum, oi.oi_seq = 0 }
2935 * and 2.x lmm_oi have { oi.oi_id = mds_oid,
2936 * oi.oi_seq = mds_seq } instead of a real FID.
2937 * Ideally the 2.x code would have stored this like a
2938 * FID with { oi_id = mds_seq, oi_seq = mds_oid } so
2939 * the ostid union lu_fid { f_seq = mds_seq,
2940 * f_oid = mds_oid } worked properly (especially since
2941 * IGIF FIDs use mds_inum as the FID SEQ), but
2942 * unfortunately that didn't happen.
2944 * Print it to look like an IGIF FID, even though the
2945 * fields are reversed on disk, so that it makes sense
2948 * Don't use ostid_id() and ostid_seq(), since they
2949 * assume the oi_fid fields are in the right order.
2950 * This is why there are separate lmm_oi_seq() and
2951 * lmm_oi_id() routines for this.
2953 * For newer layout types hopefully this will be a
2956 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2957 lmm_oi_id(&lum->lmm_oi) :
2958 lmm_oi_seq(&lum->lmm_oi);
2959 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2960 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2961 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2965 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2966 (unsigned long long)seq, oid, ver);
2968 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2969 (unsigned long long)seq, oid, ver);
2972 if (verbose & VERBOSE_STRIPE_COUNT) {
2973 if (verbose & ~VERBOSE_STRIPE_COUNT)
2974 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2977 if (!is_raw && lum->lmm_stripe_count == 0 &&
2978 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2979 unsigned int scount;
2981 rc = sattr_cache_get_defaults(NULL, path,
2985 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2988 llapi_error(LLAPI_MSG_ERROR, rc,
2989 "Cannot determine default stripe count.");
2991 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2993 (__s16)lum->lmm_stripe_count);
2996 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2998 (__s16)lum->lmm_stripe_count);
3000 if (!yaml && is_dir)
3006 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
3007 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
3008 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3009 if (verbose & ~VERBOSE_EXT_SIZE && extension)
3010 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
3012 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
3013 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
3015 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
3018 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
3021 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
3023 llapi_error(LLAPI_MSG_ERROR, rc,
3024 "Cannot determine default stripe size.");
3026 /* Extension size is in KiB */
3027 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3029 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
3030 (unsigned long long)lum->lmm_stripe_size);
3032 if (!yaml && is_dir)
3038 if ((verbose & VERBOSE_PATTERN)) {
3039 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3040 if (verbose & ~VERBOSE_PATTERN)
3041 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
3043 if (lov_pattern_supported(lum->lmm_pattern))
3044 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3045 layout2name(lum->lmm_pattern));
3047 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
3048 separator = (!yaml && is_dir) ? " " : "\n";
3051 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
3052 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3053 if (verbose & ~VERBOSE_GENERATION)
3054 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
3056 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3057 skip_objs ? 0 : (int)lum->lmm_layout_gen);
3061 if (verbose & VERBOSE_STRIPE_OFFSET) {
3062 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3063 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3064 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
3066 if (is_dir || skip_objs)
3067 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3068 lum->lmm_stripe_offset ==
3069 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
3070 lum->lmm_stripe_offset);
3071 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
3072 llapi_printf(LLAPI_MSG_NORMAL, "0");
3074 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3075 objects[0].l_ost_idx);
3076 if (!yaml && is_dir)
3082 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
3083 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3084 if (verbose & ~VERBOSE_POOL)
3085 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
3087 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
3088 if (!yaml && is_dir)
3094 if (strlen(separator) != 0)
3095 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3098 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
3099 struct lov_user_ost_data_v1 *objects,
3100 char *path, int obdindex, int depth,
3101 enum llapi_layout_verbose verbose,
3102 enum lov_dump_flags flags)
3104 bool is_dir = flags & LDF_IS_DIR;
3105 bool indent = flags & LDF_INDENT;
3106 bool skip_objs = flags & LDF_SKIP_OBJS;
3107 bool yaml = flags & LDF_YAML;
3108 bool obdstripe = obdindex == OBD_NOT_FOUND;
3111 if (!obdstripe && !skip_objs) {
3112 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
3113 if (obdindex == objects[i].l_ost_idx) {
3123 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
3126 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
3127 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
3128 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
3129 char *space = " - ";
3132 llapi_printf(LLAPI_MSG_NORMAL,
3133 "%6slmm_objects:\n", " ");
3135 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
3137 llapi_printf(LLAPI_MSG_NORMAL,
3138 "\tobdidx\t\t objid\t\t objid\t\t group\n");
3140 for (i = 0; i < lum->lmm_stripe_count; i++) {
3141 int idx = objects[i].l_ost_idx;
3142 long long oid = ostid_id(&objects[i].l_ost_oi);
3143 long long gr = ostid_seq(&objects[i].l_ost_oi);
3145 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
3149 struct lu_fid fid = { 0 };
3151 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3152 llapi_printf(LLAPI_MSG_NORMAL,
3153 "%sl_ost_idx: %d\n", space, idx);
3154 llapi_printf(LLAPI_MSG_NORMAL,
3155 "%8sl_fid: "DFID_NOBRACE"\n",
3157 } else if (indent) {
3158 struct lu_fid fid = { 0 };
3160 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3161 llapi_printf(LLAPI_MSG_NORMAL,
3162 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
3163 space, i, idx, PFID(&fid));
3167 sprintf(fmt, "%s%s%s\n",
3168 "\t%6u\t%14llu\t%#13llx\t",
3169 (fid_seq_is_rsvd(gr) ||
3170 fid_seq_is_mdt0(gr)) ?
3171 "%14llu" : "%#14llx", "%s");
3172 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
3174 obdindex == idx ? " *" : "");
3178 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3181 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3182 char *path, int obdindex, int depth,
3183 enum llapi_layout_verbose verbose,
3184 enum lov_dump_flags flags)
3186 struct lmv_user_mds_data *objects = lum->lum_objects;
3187 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3188 char *separator = "";
3189 bool yaml = flags & LDF_YAML;
3190 bool obdstripe = false;
3193 if (obdindex != OBD_NOT_FOUND) {
3194 if (lum->lum_stripe_count == 0) {
3195 if (obdindex == lum->lum_stripe_offset)
3198 for (i = 0; i < lum->lum_stripe_count; i++) {
3199 if (obdindex == objects[i].lum_mds) {
3200 llapi_printf(LLAPI_MSG_NORMAL,
3215 /* show all information default */
3217 if (lum->lum_magic == LMV_USER_MAGIC)
3218 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3219 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3221 verbose = VERBOSE_OBJID;
3224 if (depth && path && ((verbose != VERBOSE_OBJID)))
3225 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3227 if (verbose & VERBOSE_STRIPE_COUNT) {
3228 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3229 if (verbose & ~VERBOSE_STRIPE_COUNT)
3230 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3231 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3232 (int)lum->lum_stripe_count);
3233 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3239 if (verbose & VERBOSE_STRIPE_OFFSET) {
3240 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3241 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3242 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3243 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3244 (int)lum->lum_stripe_offset);
3245 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3251 if (verbose & VERBOSE_HASH_TYPE) {
3252 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3253 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3255 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3256 if (verbose & ~VERBOSE_HASH_TYPE)
3257 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3258 if (type < LMV_HASH_TYPE_MAX)
3259 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3260 mdt_hash_name[type]);
3262 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3264 if (flags & LMV_HASH_FLAG_MIGRATION)
3265 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3266 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3267 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3268 if (flags & LMV_HASH_FLAG_LOST_LMV)
3269 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3270 if (flags & LMV_HASH_FLAG_FIXED)
3271 llapi_printf(LLAPI_MSG_NORMAL, ",fixed");
3272 if (flags & ~LMV_HASH_FLAG_KNOWN)
3273 llapi_printf(LLAPI_MSG_NORMAL, ",unknown_%04x",
3274 flags & ~LMV_HASH_FLAG_KNOWN);
3276 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3282 if ((verbose & VERBOSE_INHERIT) && lum->lum_magic == LMV_USER_MAGIC) {
3283 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3284 if (verbose & ~VERBOSE_INHERIT)
3285 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit: ");
3286 if (lum->lum_max_inherit == LMV_INHERIT_UNLIMITED)
3287 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3288 else if (lum->lum_max_inherit == LMV_INHERIT_NONE)
3289 llapi_printf(LLAPI_MSG_NORMAL, "0");
3291 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3292 lum->lum_max_inherit);
3293 if (verbose & VERBOSE_INHERIT && !yaml)
3299 if ((verbose & VERBOSE_INHERIT_RR) &&
3300 lum->lum_magic == LMV_USER_MAGIC &&
3301 lum->lum_stripe_offset == LMV_OFFSET_DEFAULT) {
3302 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3303 if (verbose & ~VERBOSE_INHERIT_RR)
3304 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit_rr: ");
3305 if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_UNLIMITED)
3306 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3307 else if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_NONE)
3308 llapi_printf(LLAPI_MSG_NORMAL, "0");
3310 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3311 lum->lum_max_inherit_rr);
3312 if (verbose & VERBOSE_INHERIT_RR && !yaml)
3320 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3321 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3322 if (lum->lum_stripe_count > 0)
3323 llapi_printf(LLAPI_MSG_NORMAL,
3324 "mdtidx\t\t FID[seq:oid:ver]\n");
3325 for (i = 0; i < lum->lum_stripe_count; i++) {
3326 int idx = objects[i].lum_mds;
3327 struct lu_fid *fid = &objects[i].lum_fid;
3329 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3330 llapi_printf(LLAPI_MSG_NORMAL,
3331 "%6u\t\t "DFID"\t\t%s\n",
3333 obdindex == idx ? " *" : "");
3337 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3338 pool_name[0] != '\0') {
3339 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3340 if (verbose & ~VERBOSE_POOL)
3341 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3343 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3347 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3348 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3351 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3352 enum lov_dump_flags flags)
3354 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3355 int depth = param->fp_max_depth;
3356 enum llapi_layout_verbose verbose = param->fp_verbose;
3357 bool yaml = flags & LDF_YAML;
3359 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3360 !(flags & LDF_IS_DIR)) && !yaml)
3361 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3363 if (verbose & VERBOSE_DETAIL) {
3364 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3365 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3366 " ", comp_v1->lcm_magic);
3367 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3368 " ", comp_v1->lcm_size);
3369 if (flags & LDF_IS_DIR)
3370 llapi_printf(LLAPI_MSG_NORMAL,
3371 "%2slcm_flags: %s\n", " ",
3372 comp_v1->lcm_mirror_count > 0 ?
3375 llapi_printf(LLAPI_MSG_NORMAL,
3376 "%2slcm_flags: %s\n", " ",
3377 llapi_layout_flags_string(comp_v1->lcm_flags));
3380 if (verbose & VERBOSE_GENERATION) {
3381 if (verbose & ~VERBOSE_GENERATION)
3382 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3384 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3387 if (verbose & VERBOSE_MIRROR_COUNT) {
3388 if (verbose & ~VERBOSE_MIRROR_COUNT)
3389 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3391 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3392 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3393 comp_v1->lcm_mirror_count + 1 : 1);
3396 if (verbose & VERBOSE_COMP_COUNT) {
3397 if (verbose & ~VERBOSE_COMP_COUNT)
3398 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3400 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3401 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3402 comp_v1->lcm_entry_count : 0);
3405 if (verbose & VERBOSE_DETAIL && !yaml)
3406 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3409 static void lcme_flags2str(__u32 comp_flags)
3415 llapi_printf(LLAPI_MSG_NORMAL, "0");
3418 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3419 if (comp_flags & comp_flags_table[i].cfn_flag) {
3421 llapi_printf(LLAPI_MSG_NORMAL, ",");
3422 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3423 comp_flags_table[i].cfn_name);
3424 comp_flags &= ~comp_flags_table[i].cfn_flag;
3430 llapi_printf(LLAPI_MSG_NORMAL, ",");
3431 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3435 static void lov_dump_comp_v1_entry(struct find_param *param,
3436 enum lov_dump_flags flags, int index)
3438 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3439 struct lov_comp_md_entry_v1 *entry;
3440 char *separator = "";
3441 enum llapi_layout_verbose verbose = param->fp_verbose;
3442 bool yaml = flags & LDF_YAML;
3444 entry = &comp_v1->lcm_entries[index];
3447 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3449 if (verbose & VERBOSE_COMP_ID) {
3450 if (verbose & VERBOSE_DETAIL && !yaml)
3451 llapi_printf(LLAPI_MSG_NORMAL,
3452 "%slcme_id: ", " - ");
3453 else if (verbose & ~VERBOSE_COMP_ID)
3454 llapi_printf(LLAPI_MSG_NORMAL,
3455 "%4slcme_id: ", " ");
3456 if (entry->lcme_id != LCME_ID_INVAL)
3457 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3459 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3463 if (verbose & VERBOSE_MIRROR_ID) {
3464 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3465 if (verbose & ~VERBOSE_MIRROR_ID)
3466 llapi_printf(LLAPI_MSG_NORMAL,
3467 "%4slcme_mirror_id: ", " ");
3468 if (entry->lcme_id != LCME_ID_INVAL)
3469 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3470 mirror_id_of(entry->lcme_id));
3472 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3476 if (verbose & VERBOSE_COMP_FLAGS) {
3477 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3478 if (verbose & ~VERBOSE_COMP_FLAGS)
3479 llapi_printf(LLAPI_MSG_NORMAL,
3480 "%4slcme_flags: ", " ");
3481 lcme_flags2str(entry->lcme_flags);
3484 /* print snapshot timestamp if its a nosync comp */
3485 if ((verbose & VERBOSE_COMP_FLAGS) &&
3486 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3487 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3488 if (verbose & ~VERBOSE_COMP_FLAGS)
3489 llapi_printf(LLAPI_MSG_NORMAL,
3490 "%4slcme_timestamp: ", " ");
3492 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3493 (unsigned long long)entry->lcme_timestamp);
3495 time_t stamp = entry->lcme_timestamp;
3496 char *date_str = asctime(localtime(&stamp));
3498 date_str[strlen(date_str) - 1] = '\0';
3499 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3505 if (verbose & VERBOSE_COMP_START) {
3506 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3507 if (verbose & ~VERBOSE_COMP_START)
3508 llapi_printf(LLAPI_MSG_NORMAL,
3509 "%4slcme_extent.e_start: ", " ");
3510 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3511 (unsigned long long)entry->lcme_extent.e_start);
3515 if (verbose & VERBOSE_COMP_END) {
3516 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3517 if (verbose & ~VERBOSE_COMP_END)
3518 llapi_printf(LLAPI_MSG_NORMAL,
3519 "%4slcme_extent.e_end: ", " ");
3520 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3521 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3523 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3524 (unsigned long long)entry->lcme_extent.e_end);
3529 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3530 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3531 } else if (verbose & VERBOSE_DETAIL) {
3532 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3533 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3534 " ", entry->lcme_offset);
3535 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3536 " ", entry->lcme_size);
3537 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3539 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3544 * Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3545 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3547 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3548 * The table below gives the answers for the specified parameters (value and
3549 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3550 * --------------------------------------
3551 * 1 | file > limit; sign > 0 | -1 / -1 |
3552 * 2 | file = limit; sign > 0 | -1 / -1 |
3553 * 3 | file < limit; sign > 0 | ? / 1 |
3554 * 4 | file > limit; sign = 0 | -1 / -1 |
3555 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3556 * 6 | file < limit; sign = 0 | ? / -1 |
3557 * 7 | file > limit; sign < 0 | 1 / 1 |
3558 * 8 | file = limit; sign < 0 | ? / -1 |
3559 * 9 | file < limit; sign < 0 | ? / -1 |
3560 * --------------------------------------
3561 * Note: 5th actually means that the value is within the interval
3562 * (limit - margin, limit].
3564 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3565 int sign, int negopt, unsigned long long margin,
3571 /* Drop the fraction of margin (of days or size). */
3572 if (file + margin <= limit)
3574 } else if (sign == 0) {
3575 if (file <= limit && file + margin > limit)
3577 else if (file + margin <= limit)
3579 } else if (sign < 0) {
3586 return negopt ? ~ret + 1 : ret;
3589 static inline struct lov_user_md *
3590 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3592 return (struct lov_user_md *)((char *)comp_v1 +
3593 comp_v1->lcm_entries[ent_idx].lcme_offset);
3596 static inline struct lov_user_ost_data_v1 *
3597 lov_v1v3_objects(struct lov_user_md *v1)
3599 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3600 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3602 return v1->lmm_objects;
3606 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3608 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3609 snprintf(pool_name, LOV_MAXPOOLNAME + 1, "%s",
3610 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3612 pool_name[0] = '\0';
3616 print_last_init_comp(struct find_param *param)
3618 /* print all component info */
3619 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3622 /* print specific component info */
3623 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3624 param->fp_check_comp_start || param->fp_check_comp_end ||
3625 param->fp_check_mirror_id || param->fp_check_mirror_index)
3631 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3635 if (param->fp_comp_end == LUSTRE_EOF) {
3636 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3637 match = end == LUSTRE_EOF ? 1 : -1;
3638 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3639 match = end == LUSTRE_EOF ? -1 : 1;
3640 else /* at least EOF */
3642 if (param->fp_exclude_comp_end)
3645 unsigned long long margin;
3647 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3648 match = find_value_cmp(end, param->fp_comp_end,
3649 param->fp_comp_end_sign,
3650 param->fp_exclude_comp_end, margin, 0);
3657 * An example of "getstripe -v" for a two components PFL file:
3660 * lcm_magic: 0x0BD60BD0
3664 * lcm_entry_count: 2
3668 * lcme_extent.e_start: 0
3669 * lcme_extent.e_end: 1048576
3673 * lmm_magic: 0x0BD10BD0
3674 * lmm_seq: 0x200000401
3675 * lmm_object_id: 0x1
3676 * lmm_fid: [0x200000401:0x1:0x0]
3677 * lmm_stripe_count: 1
3678 * lmm_stripe_size: 1048576
3679 * lmm_pattern: raid0
3681 * lmm_stripe_offset: 0
3683 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3687 * lcme_extent.e_start: 1048576
3688 * lcme_extent.e_end: EOF
3692 * lmm_magic: 0x0BD10BD0
3693 * lmm_seq: 0x200000401
3694 * lmm_object_id: 0x1
3695 * lmm_fid: [0x200000401:0x1:0x0]
3696 * lmm_stripe_count: 2
3697 * lmm_stripe_size: 1048576
3698 * lmm_pattern: raid0
3700 * lmm_stripe_offset: 1
3702 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3703 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3705 static void lov_dump_comp_v1(struct find_param *param, char *path,
3706 enum lov_dump_flags flags)
3708 struct lov_comp_md_entry_v1 *entry;
3709 struct lov_user_ost_data_v1 *objects;
3710 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3711 struct lov_user_md_v1 *v1;
3712 char pool_name[LOV_MAXPOOLNAME + 1];
3713 int obdindex = param->fp_obd_index;
3714 int i, j, match, ext;
3715 bool obdstripe = false;
3716 __u16 mirror_index = 0;
3717 __u16 mirror_id = 0;
3719 if (obdindex != OBD_NOT_FOUND) {
3720 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3721 i < comp_v1->lcm_entry_count; i++) {
3722 if (!(comp_v1->lcm_entries[i].lcme_flags &
3726 v1 = lov_comp_entry(comp_v1, i);
3727 objects = lov_v1v3_objects(v1);
3729 for (j = 0; j < v1->lmm_stripe_count; j++) {
3730 if (obdindex == objects[j].l_ost_idx) {
3743 lov_dump_comp_v1_header(param, path, flags);
3745 flags |= LDF_INDENT;
3747 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3748 entry = &comp_v1->lcm_entries[i];
3750 if (param->fp_check_comp_flags) {
3751 if (((param->fp_comp_flags & entry->lcme_flags) !=
3752 param->fp_comp_flags) ||
3753 (param->fp_comp_neg_flags & entry->lcme_flags))
3757 if (param->fp_check_comp_id &&
3758 param->fp_comp_id != entry->lcme_id)
3761 if (param->fp_check_comp_start) {
3762 match = find_value_cmp(entry->lcme_extent.e_start,
3763 param->fp_comp_start,
3764 param->fp_comp_start_sign,
3766 param->fp_comp_start_units, 0);
3771 if (param->fp_check_comp_end) {
3772 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3778 if (param->fp_check_mirror_index) {
3779 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3781 mirror_id = mirror_id_of(entry->lcme_id);
3784 match = find_value_cmp(mirror_index,
3785 param->fp_mirror_index,
3786 param->fp_mirror_index_sign,
3787 param->fp_exclude_mirror_index,
3791 } else if (param->fp_check_mirror_id) {
3792 if (mirror_id != mirror_id_of(entry->lcme_id))
3793 mirror_id = mirror_id_of(entry->lcme_id);
3795 match = find_value_cmp(mirror_id,
3796 param->fp_mirror_id,
3797 param->fp_mirror_id_sign,
3798 param->fp_exclude_mirror_id,
3804 if (print_last_init_comp(param)) {
3806 * if part of stripe info is needed, we'd print only
3807 * the last instantiated component info.
3809 if (entry->lcme_flags & LCME_FL_INIT)
3812 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3813 if (entry->lcme_flags & LCME_FL_EXTENSION)
3814 /* moved back below */
3822 if (entry->lcme_flags & LCME_FL_INIT) {
3823 if (obdindex != OBD_NOT_FOUND) {
3824 flags |= LDF_SKIP_OBJS;
3825 v1 = lov_comp_entry(comp_v1, i);
3826 objects = lov_v1v3_objects(v1);
3828 for (j = 0; j < v1->lmm_stripe_count; j++) {
3829 if (obdindex == objects[j].l_ost_idx) {
3830 flags &= ~LDF_SKIP_OBJS;
3835 flags &= ~LDF_SKIP_OBJS;
3838 flags |= LDF_SKIP_OBJS;
3841 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3843 lov_dump_comp_v1_entry(param, flags, i);
3845 v1 = lov_comp_entry(comp_v1, i);
3846 objects = lov_v1v3_objects(v1);
3847 lov_v1v3_pool_name(v1, pool_name);
3849 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3850 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3851 param->fp_max_depth, param->fp_verbose,
3854 if (print_last_init_comp(param)) {
3856 * directory layout contains only layout template, print the
3860 i = comp_v1->lcm_entry_count - 1;
3863 flags &= ~LDF_SKIP_OBJS;
3865 lov_dump_comp_v1_entry(param, flags, i);
3867 v1 = lov_comp_entry(comp_v1, i);
3868 objects = lov_v1v3_objects(v1);
3869 lov_v1v3_pool_name(v1, pool_name);
3871 entry = &comp_v1->lcm_entries[i];
3872 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3873 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3874 param->fp_max_depth, param->fp_verbose,
3879 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3880 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3883 static inline bool has_any_comp_options(struct find_param *param)
3885 enum llapi_layout_verbose verbose = param->fp_verbose;
3887 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3888 param->fp_check_comp_start || param->fp_check_comp_end ||
3889 param->fp_check_comp_flags)
3892 /* show full layout information, not component specific */
3893 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3896 return verbose & VERBOSE_COMP_OPTS;
3899 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3902 struct lov_user_md *lum = &orig->lmd_lmm;
3903 struct lov_user_mds_data *new;
3904 struct lov_comp_md_v1 *comp_v1;
3905 struct lov_comp_md_entry_v1 *ent;
3906 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3907 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3910 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3912 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3916 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3917 + sizeof(new->lmd_lmmsize));
3919 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3920 comp_v1->lcm_magic = lum->lmm_magic;
3921 comp_v1->lcm_size = lum_off + lum_size;
3922 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3923 comp_v1->lcm_flags = 0;
3924 comp_v1->lcm_entry_count = 1;
3926 ent = &comp_v1->lcm_entries[0];
3928 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3929 ent->lcme_extent.e_start = 0;
3930 ent->lcme_extent.e_end = LUSTRE_EOF;
3931 ent->lcme_offset = lum_off;
3932 ent->lcme_size = lum_size;
3934 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3939 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3940 enum lov_dump_flags flags)
3942 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3944 if (has_any_comp_options(param)) {
3945 struct lov_user_mds_data *new_lmd, *orig_lmd;
3947 orig_lmd = param->fp_lmd;
3948 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3949 if (new_lmd != NULL) {
3950 param->fp_lmd = new_lmd;
3951 lov_dump_comp_v1(param, path, flags);
3952 param->fp_lmd = orig_lmd;
3958 if (magic == LOV_USER_MAGIC_V1) {
3959 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3960 param->fp_lmd->lmd_lmm.lmm_objects,
3961 path, param->fp_obd_index,
3962 param->fp_max_depth, param->fp_verbose,
3965 char pool_name[LOV_MAXPOOLNAME + 1];
3966 struct lov_user_ost_data_v1 *objects;
3967 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3969 snprintf(pool_name, sizeof(pool_name), "%s",
3970 lmmv3->lmm_pool_name);
3971 objects = lmmv3->lmm_objects;
3972 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3973 objects, path, param->fp_obd_index,
3974 param->fp_max_depth, param->fp_verbose,
3979 static uint32_t check_foreign_type(uint32_t foreign_type)
3983 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3984 if (lu_foreign_types[i].lft_name == NULL)
3986 if (foreign_type == lu_foreign_types[i].lft_type)
3990 return LU_FOREIGN_TYPE_UNKNOWN;
3993 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3994 enum lov_dump_flags flags)
3996 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3997 bool yaml = flags & LDF_YAML;
3999 if (!yaml && param->fp_depth && path)
4000 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4002 if (param->fp_verbose & VERBOSE_DETAIL) {
4003 uint32_t type = check_foreign_type(lfm->lfm_type);
4005 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
4007 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
4009 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
4011 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4012 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4013 lu_foreign_types[type].lft_name);
4015 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4017 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
4020 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
4021 lfm->lfm_length, lfm->lfm_value);
4022 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4025 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
4026 enum lov_dump_flags flags)
4028 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
4029 bool yaml = flags & LDF_YAML;
4031 if (!yaml && param->fp_depth && path)
4032 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4034 if (param->fp_verbose & VERBOSE_DETAIL) {
4035 uint32_t type = check_foreign_type(lfm->lfm_type);
4037 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
4039 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
4041 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
4043 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4044 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4045 lu_foreign_types[type].lft_name);
4047 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4049 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
4052 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
4053 lfm->lfm_length, lfm->lfm_value);
4054 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4057 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
4058 enum lov_dump_flags flags)
4062 if (param->fp_get_lmv || param->fp_get_default_lmv)
4063 magic = (__u32)param->fp_lmv_md->lum_magic;
4065 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
4068 flags |= LDF_IS_RAW;
4073 case LOV_USER_MAGIC_V1:
4074 case LOV_USER_MAGIC_V3:
4075 case LOV_USER_MAGIC_SPECIFIC:
4076 lov_dump_plain_user_lmm(param, path, flags);
4078 case LOV_USER_MAGIC_FOREIGN:
4079 lov_dump_foreign_lmm(param, path, flags);
4082 case LMV_USER_MAGIC: {
4083 char pool_name[LOV_MAXPOOLNAME + 1];
4084 struct lmv_user_md *lum;
4086 lum = (struct lmv_user_md *)param->fp_lmv_md;
4087 snprintf(pool_name, sizeof(pool_name), "%s",
4088 lum->lum_pool_name);
4089 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
4090 param->fp_max_depth, param->fp_verbose,
4094 case LOV_USER_MAGIC_COMP_V1:
4095 lov_dump_comp_v1(param, path, flags);
4097 case LMV_MAGIC_FOREIGN:
4098 lmv_dump_foreign_lmm(param, path, flags);
4101 llapi_printf(LLAPI_MSG_NORMAL,
4102 "unknown lmm_magic: %#x (expecting one of %#x %#x %#x %#x)\n",
4103 *(__u32 *)¶m->fp_lmd->lmd_lmm,
4104 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
4105 LMV_USER_MAGIC, LMV_MAGIC_V1);
4110 static int llapi_file_get_stripe1(const char *path, struct lov_user_md *lum)
4116 fname = strrchr(path, '/');
4118 /* It should be a file (or other non-directory) */
4119 if (fname == NULL) {
4120 dname = (char *)malloc(2);
4124 fname = (char *)path;
4126 dname = (char *)malloc(fname - path + 1);
4129 strncpy(dname, path, fname - path);
4130 dname[fname - path] = '\0';
4134 fd = open(dname, O_RDONLY | O_NONBLOCK);
4140 strcpy((char *)lum, fname);
4141 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
4144 if (close(fd) == -1 && rc == 0)
4152 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
4154 char *canon_path = NULL;
4157 rc = llapi_file_get_stripe1(path, lum);
4158 if (!(rc == -ENOTTY || rc == -ENODATA))
4161 /* Handle failure due to symlinks by dereferencing path manually. */
4162 canon_path = canonicalize_file_name(path);
4163 if (canon_path == NULL)
4164 goto out; /* Keep original rc. */
4166 rc2 = llapi_file_get_stripe1(canon_path, lum);
4168 goto out; /* Keep original rc. */
4177 int llapi_file_lookup(int dirfd, const char *name)
4179 struct obd_ioctl_data data = { 0 };
4184 if (dirfd < 0 || name == NULL)
4187 data.ioc_version = OBD_IOCTL_VERSION;
4188 data.ioc_len = sizeof(data);
4189 data.ioc_inlbuf1 = (char *)name;
4190 data.ioc_inllen1 = strlen(name) + 1;
4192 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
4194 llapi_error(LLAPI_MSG_ERROR, rc,
4195 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
4200 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
4207 * Check if the file time matches all the given criteria (e.g. --atime +/-N).
4208 * Return -1 or 1 if file timestamp does not or does match the given criteria
4209 * correspondingly. Return 0 if the MDS time is being checked and there are
4210 * attributes on OSTs and it is not yet clear if the timespamp matches.
4212 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
4213 * updated timestamps.
4215 static int find_time_check(struct find_param *param, int mds)
4217 struct lov_user_mds_data *lmd = param->fp_lmd;
4221 /* Check if file is accepted. */
4222 if (param->fp_atime) {
4223 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4224 param->fp_atime, param->fp_asign,
4225 param->fp_exclude_atime,
4226 param->fp_time_margin, mds);
4232 if (param->fp_mtime) {
4233 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4234 param->fp_mtime, param->fp_msign,
4235 param->fp_exclude_mtime,
4236 param->fp_time_margin, mds);
4241 * If the previous check matches, but this one is not yet clear,
4242 * we should return 0 to do an RPC on OSTs.
4248 if (param->fp_ctime) {
4249 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4250 param->fp_ctime, param->fp_csign,
4251 param->fp_exclude_ctime,
4252 param->fp_time_margin, mds);
4257 * If the previous check matches, but this one is not yet clear,
4258 * we should return 0 to do an RPC on OSTs.
4267 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4269 struct lov_user_mds_data *lmd = param->fp_lmd;
4274 for (i = 0; i < 2; i++) {
4275 /* Check if file is accepted. */
4276 if (param->fp_newery[NEWERXY_ATIME][i]) {
4277 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4278 param->fp_newery[NEWERXY_ATIME][i],
4285 if (param->fp_newery[NEWERXY_MTIME][i]) {
4286 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4287 param->fp_newery[NEWERXY_MTIME][i],
4293 * If the previous check matches, but this one is not
4294 * yet clear, we should return 0 to do an RPC on OSTs.
4300 if (param->fp_newery[NEWERXY_CTIME][i]) {
4301 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4302 param->fp_newery[NEWERXY_CTIME][i],
4308 * If the previous check matches, but this one is not
4309 * yet clear, we should return 0 to do an RPC on OSTs.
4316 * File birth time (btime) can get from MDT directly.
4317 * if @from_mdt is true, it means the input file attributs are
4318 * obtained directly from MDT.
4319 * Thus, if @from_mdt is false, we should skip the following
4325 if (param->fp_newery[NEWERXY_BTIME][i]) {
4326 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4329 rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4330 param->fp_newery[NEWERXY_BTIME][i],
4341 * Check whether the stripes matches the indexes user provided
4345 static int check_obd_match(struct find_param *param)
4347 struct lov_user_ost_data_v1 *objects;
4348 struct lov_comp_md_v1 *comp_v1 = NULL;
4349 struct lov_user_mds_data *lmd = param->fp_lmd;
4350 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4351 int i, j, k, count = 1;
4353 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4356 if (!S_ISREG(lmd->lmd_stx.stx_mode))
4359 /* exclude foreign */
4360 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4361 return param->fp_exclude_obd;
4364 * Only those files should be accepted, which have a
4365 * stripe on the specified OST.
4367 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4368 comp_v1 = (struct lov_comp_md_v1 *)v1;
4369 count = comp_v1->lcm_entry_count;
4372 for (i = 0; i < count; i++) {
4374 v1 = lov_comp_entry(comp_v1, i);
4376 objects = lov_v1v3_objects(v1);
4378 for (j = 0; j < v1->lmm_stripe_count; j++) {
4379 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4382 for (k = 0; k < param->fp_num_obds; k++) {
4383 if (param->fp_obd_indexes[k] ==
4384 objects[j].l_ost_idx)
4385 return !param->fp_exclude_obd;
4390 return param->fp_exclude_obd;
4393 static int check_mdt_match(struct find_param *param)
4397 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4400 /* FIXME: For striped dir, we should get stripe information and check */
4401 for (i = 0; i < param->fp_num_mdts; i++) {
4402 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4403 return !param->fp_exclude_mdt;
4406 if (param->fp_exclude_mdt)
4413 * Check whether the obd is active or not, if it is
4414 * not active, just print the object affected by this
4417 static void print_failed_tgt(struct find_param *param, char *path, int type)
4419 struct obd_statfs stat_buf;
4420 struct obd_uuid uuid_buf;
4421 int tgt_nr, i, *indexes;
4424 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV) {
4425 llapi_error(LLAPI_MSG_NORMAL, ret, "%s: wrong statfs type(%d)",
4430 tgt_nr = (type == LL_STATFS_LOV) ? param->fp_obd_index :
4431 param->fp_mdt_index;
4432 indexes = (type == LL_STATFS_LOV) ? param->fp_obd_indexes :
4433 param->fp_mdt_indexes;
4435 for (i = 0; i < tgt_nr; i++) {
4436 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4437 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4439 ret = llapi_obd_statfs(path, type, indexes[i], &stat_buf,
4442 llapi_error(LLAPI_MSG_NORMAL, ret,
4443 "%s: obd_uuid: %s failed",
4444 __func__, param->fp_obd_uuid->uuid);
4448 static int find_check_stripe_size(struct find_param *param)
4450 struct lov_comp_md_v1 *comp_v1 = NULL;
4451 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4452 __u32 stripe_size = 0;
4453 int ret, i, count = 1;
4455 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4456 return param->fp_exclude_stripe_size ? 1 : -1;
4458 ret = param->fp_exclude_stripe_size ? 1 : -1;
4459 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4460 comp_v1 = (struct lov_comp_md_v1 *)v1;
4461 count = comp_v1->lcm_entry_count;
4464 for (i = 0; i < count; i++) {
4465 struct lov_comp_md_entry_v1 *ent;
4468 v1 = lov_comp_entry(comp_v1, i);
4470 ent = &comp_v1->lcm_entries[i];
4471 if (ent->lcme_flags & LCME_FL_EXTENSION)
4473 if (!(ent->lcme_flags & LCME_FL_INIT))
4476 stripe_size = v1->lmm_stripe_size;
4479 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4480 param->fp_stripe_size_sign,
4481 param->fp_exclude_stripe_size,
4482 param->fp_stripe_size_units, 0);
4487 static int find_check_ext_size(struct find_param *param)
4489 struct lov_comp_md_v1 *comp_v1;
4490 struct lov_user_md_v1 *v1;
4493 ret = param->fp_exclude_ext_size ? 1 : -1;
4494 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4495 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4498 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4499 struct lov_comp_md_entry_v1 *ent;
4501 v1 = lov_comp_entry(comp_v1, i);
4503 ent = &comp_v1->lcm_entries[i];
4504 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4507 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4508 param->fp_ext_size_sign,
4509 param->fp_exclude_ext_size,
4510 param->fp_ext_size_units, 0);
4511 /* If any ext_size matches */
4519 static __u32 find_get_stripe_count(struct find_param *param)
4521 struct lov_comp_md_v1 *comp_v1 = NULL;
4522 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4524 __u32 stripe_count = 0;
4526 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4529 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4530 comp_v1 = (struct lov_comp_md_v1 *)v1;
4531 count = comp_v1->lcm_entry_count;
4534 for (i = 0; i < count; i++) {
4536 struct lov_comp_md_entry_v1 *ent;
4538 v1 = lov_comp_entry(comp_v1, i);
4540 ent = &comp_v1->lcm_entries[i];
4541 if (!(ent->lcme_flags & LCME_FL_INIT))
4544 if (ent->lcme_flags & LCME_FL_EXTENSION)
4547 stripe_count = v1->lmm_stripe_count;
4550 return stripe_count;
4553 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4555 static int find_check_layout(struct find_param *param)
4557 struct lov_comp_md_v1 *comp_v1 = NULL;
4558 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4560 bool found = false, valid = false;
4562 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4563 comp_v1 = (struct lov_comp_md_v1 *)v1;
4564 count = comp_v1->lcm_entry_count;
4567 for (i = 0; i < count; i++) {
4569 v1 = lov_comp_entry(comp_v1, i);
4571 /* foreign file have a special magic but no pattern field */
4572 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4575 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4579 if (v1->lmm_pattern & param->fp_layout) {
4588 if ((found && !param->fp_exclude_layout) ||
4589 (!found && param->fp_exclude_layout))
4596 * if no type specified, check/exclude all foreign
4597 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4599 static int find_check_foreign(struct find_param *param)
4601 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4602 struct lov_foreign_md *lfm;
4604 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4605 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4606 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4607 return param->fp_exclude_foreign ? 1 : -1;
4611 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4612 lfm->lfm_type == param->fp_foreign_type)
4613 return param->fp_exclude_foreign ? -1 : 1;
4614 return param->fp_exclude_foreign ? 1 : -1;
4617 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4618 struct lmv_foreign_md *lfm;
4620 lfm = (void *)param->fp_lmv_md;
4621 if (lmv_is_foreign(lfm->lfm_magic)) {
4622 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4623 return param->fp_exclude_foreign ? 1 : -1;
4627 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4628 lfm->lfm_type == param->fp_foreign_type)
4629 return param->fp_exclude_foreign ? -1 : 1;
4630 return param->fp_exclude_foreign ? 1 : -1;
4635 static int find_check_pool(struct find_param *param)
4637 struct lov_comp_md_v1 *comp_v1 = NULL;
4638 struct lov_user_md_v3 *v3 = (void *)¶m->fp_lmd->lmd_lmm;
4642 if (v3->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4643 comp_v1 = (struct lov_comp_md_v1 *)v3;
4644 count = comp_v1->lcm_entry_count;
4645 /* empty requested pool is taken as no pool search */
4646 if (count == 0 && param->fp_poolname[0] == '\0') {
4652 for (i = 0; i < count; i++) {
4653 if (comp_v1 != NULL) {
4654 if (!(comp_v1->lcm_entries[i].lcme_flags &
4658 v3 = (void *)lov_comp_entry(comp_v1, i);
4661 if (v3->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4664 if (((v3->lmm_magic == LOV_USER_MAGIC_V1) &&
4665 (param->fp_poolname[0] == '\0')) ||
4666 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4667 (strncmp(v3->lmm_pool_name,
4668 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4669 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4670 (strcmp(param->fp_poolname, "*") == 0))) {
4677 if ((found && !param->fp_exclude_pool) ||
4678 (!found && param->fp_exclude_pool))
4684 static int find_check_comp_options(struct find_param *param)
4686 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4687 struct lov_user_mds_data *lmd = param->fp_lmd;
4688 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4689 struct lov_comp_md_entry_v1 *entry;
4692 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4695 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4696 comp_v1 = (struct lov_comp_md_v1 *)v1;
4698 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4699 if (forged_v1 == NULL)
4701 comp_v1 = forged_v1;
4702 comp_v1->lcm_entry_count = 1;
4703 entry = &comp_v1->lcm_entries[0];
4704 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4706 entry->lcme_extent.e_start = 0;
4707 entry->lcme_extent.e_end = LUSTRE_EOF;
4710 /* invalid case, don't match for any kind of search. */
4711 if (comp_v1->lcm_entry_count == 0) {
4716 if (param->fp_check_comp_count) {
4717 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4718 param->fp_comp_count,
4719 param->fp_comp_count_sign,
4720 param->fp_exclude_comp_count, 1, 0);
4726 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4727 entry = &comp_v1->lcm_entries[i];
4729 if (param->fp_check_comp_flags) {
4731 if (((param->fp_comp_flags & entry->lcme_flags) !=
4732 param->fp_comp_flags) ||
4733 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4739 if (param->fp_check_comp_start) {
4740 ret = find_value_cmp(entry->lcme_extent.e_start,
4741 param->fp_comp_start,
4742 param->fp_comp_start_sign,
4743 param->fp_exclude_comp_start,
4744 param->fp_comp_start_units, 0);
4749 if (param->fp_check_comp_end) {
4750 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4756 /* the component matches all criteria */
4765 static int find_check_mirror_options(struct find_param *param)
4767 struct lov_comp_md_v1 *comp_v1;
4768 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4771 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4774 comp_v1 = (struct lov_comp_md_v1 *)v1;
4776 if (param->fp_check_mirror_count) {
4777 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4778 param->fp_mirror_count,
4779 param->fp_mirror_count_sign,
4780 param->fp_exclude_mirror_count, 1, 0);
4785 if (param->fp_check_mirror_state) {
4787 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4789 if ((param->fp_mirror_state != 0 &&
4790 file_state != param->fp_mirror_state) ||
4791 file_state == param->fp_mirror_neg_state)
4798 static bool find_check_lmm_info(struct find_param *param)
4800 return param->fp_check_pool || param->fp_check_stripe_count ||
4801 param->fp_check_stripe_size || param->fp_check_layout ||
4802 param->fp_check_comp_count || param->fp_check_comp_end ||
4803 param->fp_check_comp_start || param->fp_check_comp_flags ||
4804 param->fp_check_mirror_count || param->fp_check_foreign ||
4805 param->fp_check_mirror_state || param->fp_check_ext_size ||
4806 param->fp_check_projid;
4810 * Interpret backslash escape sequences and write output into buffer.
4811 * Anything written to the buffer will be null terminated.
4813 * @param[in] seq String being parsed for escape sequence. The leading
4814 * '\' character is not included in this string (only the
4815 * characters after it)
4816 * @param[out] buffer Location where interpreted escape sequence is written
4817 * @param[in] size Size of the available buffer. (Needs to be large enough
4818 * to handle escape sequence output plus null terminator.)
4819 * @param[out] wrote Number of bytes written to the buffer.
4820 * @return Number of characters from input string processed
4821 * as part of the escape sequence (0 for an unrecognized
4824 int printf_format_escape(char *seq, char *buffer, size_t size, int *wrote)
4827 /* For now, only handle single char escape sequences: \n, \t, \\ */
4850 * Interpret formats for timestamps (%a, %A@, etc)
4852 * @param[in] seq String being parsed for timestamp format. The leading
4853 * '%' character is not included in this string
4854 * @param[out] buffer Location where timestamp info is written
4855 * @param[in] size Size of the available buffer.
4856 * @param[out] wrote Number of bytes written to the buffer.
4857 * @return Number of characters from input string processed
4858 * as part of the format (0 for an unknown format)
4861 int printf_format_timestamp(char *seq, char *buffer, size_t size, int *wrote,
4862 struct find_param *param)
4864 struct statx_timestamp ts = { 0, 0 };
4868 char *fmt = "%c"; /* Print in ctime format by default */
4873 ts = param->fp_lmd->lmd_stx.stx_atime;
4877 if (*(seq + 1) == '@') {
4878 ts = param->fp_lmd->lmd_stx.stx_atime;
4884 ts = param->fp_lmd->lmd_stx.stx_ctime;
4888 if (*(seq + 1) == '@') {
4889 ts = param->fp_lmd->lmd_stx.stx_ctime;
4895 ts = param->fp_lmd->lmd_stx.stx_mtime;
4899 if (*(seq + 1) == '@') {
4900 ts = param->fp_lmd->lmd_stx.stx_mtime;
4906 ts = param->fp_lmd->lmd_stx.stx_btime;
4910 if (*(seq + 1) == '@') {
4911 ts = param->fp_lmd->lmd_stx.stx_btime;
4921 /* Found valid format, print to buffer */
4924 *wrote = strftime(buffer, size, fmt, tm);
4931 * Print all ost indices associated with a file layout using a commma separated
4932 * list. For a file with mutliple components, the list of indices for each
4933 * component will be enclosed in brackets.
4935 * @param[out] buffer Location where OST indices are written
4936 * @param[in] size Size of the available buffer.
4937 * @pararm[in] layout Pointer to layout structure for the file
4938 * @return Number of bytes written to output buffer
4940 static int printf_format_ost_indices(char *buffer, size_t size,
4941 struct llapi_layout *layout)
4943 uint64_t count, idx, i;
4944 int err, bytes, wrote = 0;
4946 /* Make sure to start at the first component */
4947 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
4949 llapi_error(LLAPI_MSG_ERROR, err,
4950 "error: layout component iteration failed\n");
4954 err = llapi_layout_stripe_count_get(layout, &count);
4956 llapi_error(LLAPI_MSG_ERROR, err,
4957 "error: cannot get stripe_count\n");
4961 bytes = snprintf(buffer, (size - wrote), "%s", "[");
4966 for (i = 0; i < count; i++) {
4967 err = llapi_layout_ost_index_get(layout, i, &idx);
4969 llapi_error(LLAPI_MSG_ERROR, err,
4970 "error: cannot get OST index\n");
4971 bytes = snprintf(buffer, (size - wrote),
4974 bytes = snprintf(buffer, (size - wrote),
4982 /* Overwrite last comma with closing bracket */
4983 *(buffer - 1) = ']';
4985 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
4986 if (err == 0) /* next component is found */
4989 llapi_error(LLAPI_MSG_ERROR, err,
4990 "error: layout component iteration failed\n");
4991 /* At this point, either got error or reached last component */
5002 * Parse Lustre-specific format sequences of the form %L{x}.
5004 * @param[in] seq String being parsed for format sequence. The leading
5005 * '%' character is not included in this string
5006 * @param[out] buffer Location where interpreted format info is written
5007 * @param[in] size Size of the available buffer.
5008 * @param[out] wrote Number of bytes written to the buffer.
5009 * @param[in] param The find_param structure associated with the file/dir
5010 * @param[in] path Pathname of the current file/dir being handled
5011 * @param[in] projid Project ID associated with the current file/dir
5012 * @param[in] d File descriptor for the directory (or -1 for a
5013 * non-directory file)
5014 * @return Number of characters from input string processed
5015 * as part of the format (0 for an unknown format)
5017 int printf_format_lustre(char *seq, char *buffer, size_t size, int *wrote,
5018 struct find_param *param, char *path, int projid,
5021 struct lmv_user_md *lum;
5022 struct lmv_user_mds_data *objects;
5023 struct llapi_layout *layout = NULL;
5025 unsigned int hash_type;
5026 uint64_t str_cnt, str_size, idx;
5027 char pool_name[LOV_MAXPOOLNAME + 1] = { '\0' };
5029 int rc = 2; /* all current valid sequences are 2 chars */
5032 /* Sanity check. Formats always look like %L{X} */
5033 if (*seq++ != 'L') {
5039 * Some formats like %LF or %LP are handled the same for both files
5040 * and dirs, so handle all of those here.
5044 err = llapi_path2fid(path, &fid);
5046 llapi_error(LLAPI_MSG_ERROR, err,
5047 "error: cannot get fid\n");
5050 *wrote = snprintf(buffer, size, DFID_NOBRACE, PFID(&fid));
5053 *wrote = snprintf(buffer, size, "%d", projid);
5057 /* Other formats for files/dirs need to be handled differently */
5058 if (d == -1) { /* file */
5059 //layout = llapi_layout_get_by_xattr(¶m->fp_lmd->lmd_lmm,
5060 // param->fp_lum_size, 0);
5061 layout = llapi_layout_get_by_path(path, 0);
5062 if (layout == NULL) {
5063 llapi_error(LLAPI_MSG_ERROR, errno,
5064 "error: cannot get file layout\n");
5069 * Set the layout pointer to the last init component
5070 * since that is the component used for most of these
5071 * formats. (This also works for non-composite files)
5073 err = llapi_layout_get_last_init_comp(layout);
5075 llapi_error(LLAPI_MSG_ERROR, err,
5076 "error: cannot get last initialized compomnent\n");
5081 case 'c': /* stripe count */
5082 err = llapi_layout_stripe_count_get(layout, &str_cnt);
5084 llapi_error(LLAPI_MSG_ERROR, err,
5085 "error: cannot get stripe_count\n");
5088 *wrote = snprintf(buffer, size, "%"PRIu64, str_cnt);
5090 case 'h': /* hash info */
5091 /* Not applicable to files. Skip it. */
5093 case 'i': /* starting index */
5094 err = llapi_layout_ost_index_get(layout, 0, &idx);
5096 llapi_error(LLAPI_MSG_ERROR, err,
5097 "error: cannot get OST index of last initialized component\n");
5100 *wrote = snprintf(buffer, size, "%"PRIu64, idx);
5102 case 'o': /* list of object indices */
5103 *wrote = printf_format_ost_indices(buffer, size, layout);
5105 case 'p': /* pool name */
5106 err = llapi_layout_pool_name_get(layout, pool_name,
5109 llapi_error(LLAPI_MSG_ERROR, rc,
5110 "error: cannot get pool name\n");
5113 *wrote = snprintf(buffer, size, "%s", pool_name);
5115 case 'S': /* stripe size */
5116 err = llapi_layout_stripe_size_get(layout, &str_size);
5118 llapi_error(LLAPI_MSG_ERROR, rc,
5119 "error: cannot get stripe_size\n");
5122 *wrote = snprintf(buffer, size, "%"PRIu64, str_size);
5128 } else { /* directory */
5129 lum = (struct lmv_user_md *)param->fp_lmv_md;
5130 objects = lum->lum_objects;
5133 case 'c': /* stripe count */
5134 *wrote = snprintf(buffer, size, "%d",
5135 (int)lum->lum_stripe_count);
5137 case 'h': /* hash info */
5138 hash_type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
5139 if (hash_type < LMV_HASH_TYPE_MAX)
5140 *wrote = snprintf(buffer, size, "%s",
5141 mdt_hash_name[hash_type]);
5143 *wrote = snprintf(buffer, size, "%#x",
5146 case 'i': /* starting index */
5147 *wrote = snprintf(buffer, size, "%d",
5148 lum->lum_stripe_offset);
5150 case 'o': /* list of object indices */
5151 str_cnt = (int) lum->lum_stripe_count;
5152 *wrote = snprintf(buffer, size, "%s", "[");
5156 for (i = 0; i < str_cnt; i++) {
5157 bytes = snprintf(buffer, (size - *wrote),
5158 "%d,", objects[i].lum_mds);
5165 /* Use lum_offset as the only list entry */
5166 bytes = snprintf(buffer, (size - *wrote),
5167 "%d]", lum->lum_stripe_offset);
5170 /* Overwrite last comma with closing bracket */
5171 *(buffer - 1) = ']';
5174 case 'p': /* pool name */
5175 *wrote = snprintf(buffer, size, "%s",
5176 lum->lum_pool_name);
5178 case 'S': /* stripe size */
5179 /* This has no meaning for directories. Skip it. */
5189 llapi_layout_free(layout);
5192 /* output of snprintf was truncated */
5199 * Interpret format specifiers beginning with '%'.
5201 * @param[in] seq String being parsed for format specifier. The leading
5202 * '%' character is not included in this string
5203 * @param[out] buffer Location where formatted info is written
5204 * @param[in] size Size of the available buffer.
5205 * @param[out] wrote Number of bytes written to the buffer.
5206 * @param[in] param The find_param structure associated with the file/dir
5207 * @param[in] path Pathname of the current file/dir being handled
5208 * @param[in] projid Project ID associated with the current file/dir
5209 * @param[in] d File descriptor for the directory (or -1 for a
5210 * non-directory file)
5211 * @return Number of characters from input string processed
5212 * as part of the format (0 for an unknown format)
5214 int printf_format_directive(char *seq, char *buffer, size_t size, int *wrote,
5215 struct find_param *param, char *path, int projid,
5218 __u16 mode = param->fp_lmd->lmd_stx.stx_mode;
5219 uint64_t blocks = param->fp_lmd->lmd_stx.stx_blocks;
5220 int rc = 1; /* most specifiers are single character */
5228 case 'w': case 'W': /* timestamps */
5229 rc = printf_format_timestamp(seq, buffer, size, wrote, param);
5231 case 'b': /* file size (in 512B blocks) */
5232 *wrote = snprintf(buffer, size, "%"PRIu64, blocks);
5234 case 'G': /* GID of owner */
5235 *wrote = snprintf(buffer, size, "%u",
5236 param->fp_lmd->lmd_stx.stx_gid);
5238 case 'k': /* file size (in 1K blocks) */
5239 *wrote = snprintf(buffer, size, "%"PRIu64, (blocks + 1)/2);
5241 case 'L': /* Lustre-specific formats */
5242 rc = printf_format_lustre(seq, buffer, size, wrote, param,
5245 case 'm': /* file mode in octal */
5246 *wrote = snprintf(buffer, size, "%#o", (mode & (~S_IFMT)));
5248 case 'p': /* Path name of file */
5249 *wrote = snprintf(buffer, size, "%s", path);
5251 case 's': /* file size (in bytes) */
5252 *wrote = snprintf(buffer, size, "%"PRIu64,
5253 (uint64_t) param->fp_lmd->lmd_stx.stx_size);
5255 case 'U': /* UID of owner */
5256 *wrote = snprintf(buffer, size, "%u",
5257 param->fp_lmd->lmd_stx.stx_uid);
5259 case 'y': /* file type */
5262 else if (S_ISDIR(mode))
5264 else if (S_ISLNK(mode))
5266 else if (S_ISBLK(mode))
5268 else if (S_ISCHR(mode))
5270 else if (S_ISFIFO(mode))
5272 else if (S_ISSOCK(mode))
5282 default: /* invalid format specifier */
5288 /* output of snprintf was truncated */
5295 * Parse user-supplied string for the -printf option and interpret any
5296 * '%' format specifiers or '\' escape sequences.
5298 * @param[in] param The find_param struct containing the -printf string
5299 * as well as info about the current file/dir that mathced
5300 * the lfs find search criteria
5301 * @param[in] path Path name for current file/dir
5302 * @param[in] projid Project ID associated with current file/dir
5303 * @param[in] d File descriptor for current directory (or -1 for a
5304 * non-directory file)
5306 void printf_format_string(struct find_param *param, char *path,
5309 char output[FORMATTED_BUF_LEN];
5310 char *fmt_char = param->fp_format_printf_str;
5311 char *buff = output;
5317 buff_size = FORMATTED_BUF_LEN;
5319 /* Always leave one free byte in buffer for trailing NUL */
5320 while (*fmt_char && (buff_size > 1)) {
5323 if (*fmt_char == '%') {
5324 rc = printf_format_directive(fmt_char + 1, buff,
5325 buff_size, &written, param,
5327 } else if (*fmt_char == '\\')
5328 rc = printf_format_escape(fmt_char + 1, buff,
5329 buff_size, &written);
5332 /* Either a '\' escape or '%' format was processed.
5333 * Increment pointers accordingly.
5335 fmt_char += (rc + 1);
5337 buff_size -= written;
5339 /* Regular char or invalid escape/format.
5340 * Either way, copy current character.
5342 *buff++ = *fmt_char++;
5347 /* Terminate output buffer and print */
5349 llapi_printf(LLAPI_MSG_NORMAL, "%s", output);
5353 * Get file/directory project id.
5354 * by the open fd resides on.
5355 * Return 0 and project id on success, or -ve errno.
5357 static int fget_projid(int fd, int *projid)
5362 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
5366 *projid = fsx.fsx_projid;
5371 * Check that the file's permissions in *st matches the one in find_param
5373 static int check_file_permissions(const struct find_param *param,
5380 switch (param->fp_perm_sign) {
5381 case LFS_FIND_PERM_EXACT:
5382 decision = (mode == param->fp_perm);
5384 case LFS_FIND_PERM_ALL:
5385 decision = ((mode & param->fp_perm) == param->fp_perm);
5387 case LFS_FIND_PERM_ANY:
5388 decision = ((mode & param->fp_perm) != 0);
5392 if ((param->fp_exclude_perm && decision)
5393 || (!param->fp_exclude_perm && !decision))
5399 static int cb_find_init(char *path, int p, int *dp,
5400 void *data, struct dirent64 *de)
5402 struct find_param *param = (struct find_param *)data;
5403 struct lov_user_mds_data *lmd = param->fp_lmd;
5404 int d = dp == NULL ? -1 : *dp;
5405 int decision = 1; /* 1 is accepted; -1 is rejected. */
5407 int checked_type = 0;
5409 __u32 stripe_count = 0;
5413 bool gather_all = false;
5415 if (p == -1 && d == -1)
5418 /* Reset this value between invocations */
5419 param->fp_get_lmv = 0;
5421 /* Gather all file/dir info, not just what's needed for search params */
5422 if (param->fp_format_printf_str)
5425 /* If a regular expression is presented, make the initial decision */
5426 if (param->fp_pattern != NULL) {
5427 char *fname = strrchr(path, '/');
5429 fname = (fname == NULL ? path : fname + 1);
5430 ret = fnmatch(param->fp_pattern, fname, 0);
5431 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
5432 (ret == 0 && param->fp_exclude_pattern))
5436 /* See if we can check the file type from the dirent. */
5437 if (de != NULL && de->d_type != DT_UNKNOWN) {
5438 if (param->fp_type != 0) {
5441 if (DTTOIF(de->d_type) == param->fp_type) {
5442 if (param->fp_exclude_type)
5445 if (!param->fp_exclude_type)
5449 if ((param->fp_check_mdt_count || param->fp_hash_type ||
5450 param->fp_check_hash_flag) && de->d_type != DT_DIR)
5457 * Request MDS for the stat info if some of these parameters need
5460 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
5461 param->fp_check_uid || param->fp_check_gid ||
5462 param->fp_newerxy || param->fp_btime ||
5463 param->fp_atime || param->fp_mtime || param->fp_ctime ||
5464 param->fp_check_size || param->fp_check_blocks ||
5465 find_check_lmm_info(param) ||
5466 param->fp_check_mdt_count || param->fp_hash_type ||
5467 param->fp_check_hash_flag || param->fp_perm_sign ||
5471 if (param->fp_type != 0 && checked_type == 0)
5474 if (decision == 0) {
5476 (param->fp_check_mdt_count || param->fp_hash_type ||
5477 param->fp_check_hash_flag || param->fp_check_foreign ||
5479 param->fp_get_lmv = 1;
5480 ret = cb_get_dirstripe(path, &d, param);
5482 if (errno == ENODATA) {
5483 /* Fill in struct for unstriped dir */
5485 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
5486 /* Use 0 until we find actual offset */
5487 param->fp_lmv_md->lum_stripe_offset = 0;
5488 param->fp_lmv_md->lum_stripe_count = 0;
5489 param->fp_lmv_md->lum_hash_type = 0;
5491 if (param->fp_check_foreign) {
5492 if (param->fp_exclude_foreign)
5501 if (param->fp_check_mdt_count) {
5502 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5505 decision = find_value_cmp(param->fp_lmv_md->lum_stripe_count,
5506 param->fp_mdt_count,
5507 param->fp_mdt_count_sign,
5508 param->fp_exclude_mdt_count, 1, 0);
5513 if (param->fp_hash_type) {
5515 __u32 type = param->fp_lmv_md->lum_hash_type &
5518 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5521 found = (1 << type) & param->fp_hash_type;
5522 if ((found && param->fp_exclude_hash_type) ||
5523 (!found && !param->fp_exclude_hash_type))
5527 if (param->fp_check_hash_flag) {
5528 __u32 flags = param->fp_lmv_md->lum_hash_type &
5529 ~LMV_HASH_TYPE_MASK;
5531 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5534 if (!(flags & param->fp_hash_inflags) ||
5535 (flags & param->fp_hash_exflags))
5540 param->fp_lmd->lmd_lmm.lmm_magic = 0;
5541 ret = get_lmd_info_fd(path, p, d, param->fp_lmd,
5542 param->fp_lum_size, GET_LMD_INFO);
5543 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
5544 find_check_lmm_info(param)) {
5545 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
5548 * We need to "fake" the "use the default" values
5549 * since the lmm struct is zeroed out at this point.
5551 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5552 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
5554 ostid_set_seq(&lmm->lmm_oi,
5555 FID_SEQ_LOV_DEFAULT);
5556 lmm->lmm_stripe_size = 0;
5557 lmm->lmm_stripe_count = 0;
5558 lmm->lmm_stripe_offset = -1;
5560 if (ret == 0 && (param->fp_mdt_uuid != NULL || gather_all)) {
5562 ret = llapi_file_fget_mdtidx(d,
5563 ¶m->fp_file_mdt_index);
5565 * Make sure lum_stripe_offset matches
5566 * mdt_index even for unstriped directories.
5568 if (ret == 0 && param->fp_get_lmv)
5569 param->fp_lmv_md->lum_stripe_offset =
5570 param->fp_file_mdt_index;
5571 } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
5573 * FIXME: we could get the MDT index from the
5574 * file's FID in lmd->lmd_lmm.lmm_oi without
5575 * opening the file, once we are sure that
5576 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
5577 * That would still be an ioctl() to map the
5578 * FID to the MDT, but not an open RPC.
5580 fd = open(path, O_RDONLY);
5582 ret = llapi_file_fget_mdtidx(fd,
5583 ¶m->fp_file_mdt_index);
5589 * For a special file, we assume it resides on
5590 * the same MDT as the parent directory.
5592 ret = llapi_file_fget_mdtidx(p,
5593 ¶m->fp_file_mdt_index);
5604 stripe_count = find_get_stripe_count(param);
5608 /* Check the file permissions from the stat info */
5609 if (param->fp_perm_sign) {
5610 decision = check_file_permissions(param, lmd->lmd_stx.stx_mode);
5615 if (param->fp_type && !checked_type) {
5616 if ((param->fp_check_mdt_count || param->fp_check_hash_flag ||
5617 param->fp_hash_type) && !S_ISDIR(lmd->lmd_stx.stx_mode))
5620 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
5621 if (param->fp_exclude_type)
5624 if (!param->fp_exclude_type)
5630 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
5631 if (lustre_fs && param->fp_got_uuids &&
5632 param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
5633 lmd->lmd_stx.stx_dev_minor)) {
5634 /* A lustre/lustre mount point is crossed. */
5635 param->fp_got_uuids = 0;
5636 param->fp_obds_printed = 0;
5637 param->fp_mdt_index = OBD_NOT_FOUND;
5638 param->fp_obd_index = OBD_NOT_FOUND;
5641 if (lustre_fs && !param->fp_got_uuids) {
5642 ret = setup_target_indexes((d != -1) ? d : p, path,
5647 param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
5648 lmd->lmd_stx.stx_dev_minor);
5649 } else if (!lustre_fs && param->fp_got_uuids) {
5650 /* A lustre/non-lustre mount point is crossed. */
5651 param->fp_got_uuids = 0;
5652 param->fp_mdt_index = OBD_NOT_FOUND;
5653 param->fp_obd_index = OBD_NOT_FOUND;
5657 if (param->fp_check_foreign) {
5658 decision = find_check_foreign(param);
5663 if (param->fp_check_stripe_size) {
5664 decision = find_check_stripe_size(param);
5669 if (param->fp_check_ext_size) {
5670 decision = find_check_ext_size(param);
5675 if (param->fp_check_stripe_count) {
5676 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
5677 param->fp_stripe_count_sign,
5678 param->fp_exclude_stripe_count, 1, 0);
5683 if (param->fp_check_layout) {
5684 decision = find_check_layout(param);
5689 /* If an OBD UUID is specified but none matches, skip this file. */
5690 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
5691 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
5695 * If an OST or MDT UUID is given, and some OST matches,
5698 if (param->fp_obd_index != OBD_NOT_FOUND ||
5699 param->fp_mdt_index != OBD_NOT_FOUND) {
5700 if (param->fp_obd_uuid) {
5701 if (check_obd_match(param)) {
5703 * If no mdtuuid is given, we are done.
5704 * Otherwise, fall through to the mdtuuid
5707 if (!param->fp_mdt_uuid)
5714 if (param->fp_mdt_uuid) {
5715 if (check_mdt_match(param))
5722 if (param->fp_check_uid) {
5723 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
5724 if (param->fp_exclude_uid)
5727 if (!param->fp_exclude_uid)
5732 if (param->fp_check_gid) {
5733 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
5734 if (param->fp_exclude_gid)
5737 if (!param->fp_exclude_gid)
5742 if (param->fp_check_projid || gather_all) {
5746 fd = open(path, O_RDONLY);
5749 ret = fget_projid(fd, &projid);
5754 if (projid == param->fp_projid) {
5755 if (param->fp_exclude_projid)
5758 if (!param->fp_exclude_projid)
5763 if (param->fp_check_pool) {
5764 decision = find_check_pool(param);
5769 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
5770 param->fp_check_comp_start || param->fp_check_comp_end) {
5771 decision = find_check_comp_options(param);
5776 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
5777 decision = find_check_mirror_options(param);
5782 /* Check the time on mds. */
5784 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
5787 for_mds = lustre_fs ?
5788 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5789 decision = find_time_check(param, for_mds);
5794 if (param->fp_btime) {
5795 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
5800 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
5801 param->fp_btime, param->fp_bsign,
5802 param->fp_exclude_btime,
5803 param->fp_time_margin, 0);
5808 if (param->fp_newerxy) {
5811 for_mds = lustre_fs ?
5812 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5813 decision = find_newerxy_check(param, for_mds, true);
5822 flags = param->fp_lmd->lmd_flags;
5823 if (param->fp_check_size &&
5824 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5825 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5826 !(flags & OBD_MD_FLSIZE ||
5827 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
5830 if (param->fp_check_blocks &&
5831 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5832 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5833 !(flags & OBD_MD_FLBLOCKS ||
5834 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
5838 * If file still fits the request, ask ost for updated info.
5839 * The regular stat is almost of the same speed as some new
5840 * 'glimpse-size-ioctl'.
5842 if (!decision || gather_all) {
5846 * For regular files with the stripe the decision may have not
5847 * been taken yet if *time or size is to be checked.
5849 if (param->fp_obd_index != OBD_NOT_FOUND)
5850 print_failed_tgt(param, path, LL_STATFS_LOV);
5852 if (param->fp_mdt_index != OBD_NOT_FOUND)
5853 print_failed_tgt(param, path, LL_STATFS_LMV);
5856 ret = fstat_f(d, &st);
5857 else if (de != NULL)
5858 ret = fstatat_f(p, de->d_name, &st,
5859 AT_SYMLINK_NOFOLLOW);
5861 ret = lstat_f(path, &st);
5864 if (errno == ENOENT) {
5865 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
5866 "warning: %s: %s does not exist",
5871 llapi_error(LLAPI_MSG_ERROR, ret,
5872 "%s: stat on %s failed",
5878 convert_lmd_statx(param->fp_lmd, &st, true);
5879 /* Check the time on osc. */
5880 decision = find_time_check(param, 0);
5884 if (param->fp_newerxy) {
5885 decision = find_newerxy_check(param, 0, false);
5895 if (param->fp_check_size) {
5896 decision = find_value_cmp(lmd->lmd_stx.stx_size,
5898 param->fp_size_sign,
5899 param->fp_exclude_size,
5900 param->fp_size_units, 0);
5905 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
5906 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
5908 param->fp_blocks_sign,
5909 param->fp_exclude_blocks,
5910 param->fp_blocks_units, 0);
5916 if (param->fp_format_printf_str)
5917 printf_format_string(param, path, projid, d);
5919 llapi_printf(LLAPI_MSG_NORMAL, "%s%c", path,
5920 param->fp_zero_end ? '\0' : '\n');
5924 /* Do not get down anymore? */
5925 if (param->fp_depth == param->fp_max_depth) {
5936 static int cb_migrate_mdt_init(char *path, int p, int *dp,
5937 void *param_data, struct dirent64 *de)
5939 struct find_param *param = (struct find_param *)param_data;
5940 struct lmv_user_md *lmu = param->fp_lmv_md;
5942 char raw[MAX_IOC_BUFLEN] = {'\0'};
5944 struct obd_ioctl_data data = { 0 };
5950 if (p == -1 && dp == NULL)
5956 if (dp != NULL && *dp != -1)
5960 tmp_p = open_parent(path);
5964 llapi_error(LLAPI_MSG_ERROR, ret,
5965 "can not open %s", path);
5970 path_copy = strdup(path);
5971 filename = basename(path_copy);
5973 data.ioc_inlbuf1 = (char *)filename;
5974 data.ioc_inllen1 = strlen(filename) + 1;
5975 data.ioc_inlbuf2 = (char *)lmu;
5976 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
5979 if (param->fp_depth == param->fp_max_depth)
5980 data.ioc_type = MDS_MIGRATE_NSONLY;
5981 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5983 llapi_error(LLAPI_MSG_ERROR, ret,
5984 "%s: error packing ioctl data", __func__);
5989 ret = ioctl(tmp_p, LL_IOC_MIGRATE, rawbuf);
5991 if (errno == EBUSY && !retry) {
5993 * because migrate may not be able to lock all involved
5994 * objects in order, for some of them it try lock, while
5995 * there may be conflicting COS locks and cause migrate
5996 * fail with EBUSY, hope a sync() could cause
5997 * transaction commit and release these COS locks.
6002 } else if (errno == EALREADY) {
6003 if (param->fp_verbose & VERBOSE_DETAIL)
6004 llapi_printf(LLAPI_MSG_NORMAL,
6005 "%s migrated to MDT%d already\n",
6006 path, lmu->lum_stripe_offset);
6010 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
6014 } else if (param->fp_verbose & VERBOSE_DETAIL) {
6015 llapi_printf(LLAPI_MSG_NORMAL,
6016 "migrate %s to MDT%d stripe count %d\n",
6017 path, lmu->lum_stripe_offset,
6018 lmu->lum_stripe_count);
6022 /* Do not get down anymore? */
6023 if (param->fp_depth == param->fp_max_depth)
6030 * If the directory is being migration, we need
6031 * close the directory after migration,
6032 * so the old directory cache will be cleanup
6033 * on the client side, and re-open to get the
6034 * new directory handle
6036 *dp = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
6039 llapi_error(LLAPI_MSG_ERROR, ret,
6040 "%s: Failed to open '%s'", __func__, path);
6052 /* dir migration finished, shrink its stripes */
6053 static int cb_migrate_mdt_fini(char *path, int p, int *dp, void *data,
6054 struct dirent64 *de)
6056 struct find_param *param = data;
6057 struct lmv_user_md *lmu = param->fp_lmv_md;
6058 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
6061 if (de && de->d_type != DT_DIR)
6066 * close it before setxattr because the latter may destroy the
6067 * original object, and cause close fail.
6075 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
6076 if (ret == -EALREADY)
6079 cb_common_fini(path, p, dp, data, de);
6083 int llapi_migrate_mdt(char *path, struct find_param *param)
6085 param->fp_stop_on_error = 1;
6086 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
6090 int llapi_mv(char *path, struct find_param *param)
6092 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
6093 static bool printed;
6096 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
6097 "%s() is deprecated, use llapi_migrate_mdt() instead",
6102 return llapi_migrate_mdt(path, param);
6106 * Check string for escape sequences and print a message to stdout
6107 * if any invalid escapes are found.
6109 * @param[in] c Pointer to character immediately following the
6110 * '\' character indicating the start of an escape
6112 * @return Number of characters examined in the escape sequence
6113 * (regardless of whether the sequence is valid or not).
6115 int validate_printf_esc(char *c)
6117 char *valid_esc = "nt\\";
6120 /* backslash at end of string */
6121 llapi_err_noerrno(LLAPI_MSG_WARN,
6122 "warning: '\\' at end of -printf format string\n");
6126 if (!strchr(valid_esc, *c))
6127 /* Invalid escape character */
6128 llapi_err_noerrno(LLAPI_MSG_WARN,
6129 "warning: unrecognized escape: '\\%c'\n", *c);
6135 * Check string for format directives and print a message to stdout
6136 * if any invalid directives are found.
6138 * @param[in] c Pointer to character immediately following the
6139 * '%' character indicating the start of a format
6141 * @return Number of characters examined in the format directive
6142 * (regardless of whether the directive is valid or not).
6144 int validate_printf_fmt(char *c)
6146 char *valid_fmt_single = "abcGkmpstUwy%";
6147 char *valid_fmt_double = "ACTW";
6148 char *valid_fmt_lustre = "cFhioPpS";
6149 char curr = *c, next;
6152 llapi_err_noerrno(LLAPI_MSG_WARN,
6153 "warning: '%%' at end of -printf format string\n");
6158 if ((next == '\0') || (next == '%') || (next == '\\'))
6159 /* Treat as single char format directive */
6162 /* Check format directives with multiple characters */
6163 if (strchr(valid_fmt_double, curr)) {
6164 /* For now, only valid formats are followed by '@' char */
6166 llapi_err_noerrno(LLAPI_MSG_WARN,
6167 "warning: unrecognized format directive: '%%%c%c'\n",
6172 /* Lustre formats always start with 'L' */
6174 if (!strchr(valid_fmt_lustre, next))
6175 llapi_err_noerrno(LLAPI_MSG_WARN,
6176 "warning: unrecognized format directive: '%%%c%c'\n",
6183 if (!strchr(valid_fmt_single, curr))
6184 llapi_err_noerrno(LLAPI_MSG_WARN,
6185 "warning: unrecognized format directive: '%%%c'\n", curr);
6190 * Validate the user-supplied string for the -printf option and report
6191 * any invalid backslash escape sequences or format directives.
6193 * @param[in] param Structure containing info about invocation of lfs find
6196 void validate_printf_str(struct find_param *param)
6198 char *c = param->fp_format_printf_str;
6204 ret = validate_printf_fmt(++c);
6208 ret = validate_printf_esc(++c);
6218 int llapi_find(char *path, struct find_param *param)
6220 if (param->fp_format_printf_str)
6221 validate_printf_str(param);
6222 return param_callback(path, cb_find_init, cb_common_fini, param);
6226 * Get MDT number that the file/directory inode referenced
6227 * by the open fd resides on.
6228 * Return 0 and mdtidx on success, or -ve errno.
6230 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
6232 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
6237 static int cb_get_mdt_index(char *path, int p, int *dp, void *data,
6238 struct dirent64 *de)
6240 struct find_param *param = (struct find_param *)data;
6241 int d = dp == NULL ? -1 : *dp;
6245 if (p == -1 && d == -1)
6249 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6250 } else /* if (p != -1) */ {
6253 fd = open(path, O_RDONLY | O_NOCTTY);
6255 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
6263 if (ret == -ENODATA) {
6264 if (!param->fp_obd_uuid)
6265 llapi_printf(LLAPI_MSG_NORMAL,
6266 "'%s' has no stripe info\n", path);
6268 } else if (ret == -ENOENT) {
6269 llapi_error(LLAPI_MSG_WARN, ret,
6270 "warning: %s: '%s' does not exist",
6273 } else if (ret == -ENOTTY) {
6274 llapi_error(LLAPI_MSG_ERROR, ret,
6275 "%s: '%s' not on a Lustre fs",
6278 llapi_error(LLAPI_MSG_ERROR, ret,
6279 "error: %s: '%s' failed get_mdtidx",
6285 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
6286 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
6288 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
6292 /* Do not go down anymore? */
6293 if (param->fp_depth == param->fp_max_depth)
6301 static int cb_getstripe(char *path, int p, int *dp, void *data,
6302 struct dirent64 *de)
6304 struct find_param *param = (struct find_param *)data;
6305 int d = dp == NULL ? -1 : *dp;
6308 if (p == -1 && d == -1)
6311 if (param->fp_obd_uuid) {
6312 param->fp_quiet = 1;
6313 ret = setup_obd_uuid(d != -1 ? d : p, path, param);
6318 if (d != -1 && (param->fp_get_lmv || param->fp_get_default_lmv))
6319 ret = cb_get_dirstripe(path, &d, param);
6321 (p != -1 && !param->fp_get_lmv && !param->fp_get_default_lmv))
6322 ret = get_lmd_info_fd(path, p, d, ¶m->fp_lmd->lmd_lmm,
6323 param->fp_lum_size, GET_LMD_STRIPE);
6324 else if (d == -1 && (param->fp_get_lmv || param->fp_get_default_lmv)) {
6325 /* in case of a dangling or valid faked symlink dir, opendir()
6326 * should have return either EINVAL or ENOENT, so let's try
6327 * to get LMV just in case, and by opening it as a file but
6328 * with O_NOFOLLOW ...
6330 int fd = open(path, O_RDONLY | O_NOFOLLOW);
6334 ret = cb_get_dirstripe(path, &fd, param);
6336 llapi_lov_dump_user_lmm(param, path, LDF_IS_DIR);
6343 if (errno == ENODATA && d != -1) {
6345 * We need to "fake" the "use the default" values
6346 * since the lmm struct is zeroed out at this point.
6347 * The magic needs to be set in order to satisfy
6348 * a check later on in the code path.
6349 * The object_seq needs to be set for the "(Default)"
6350 * prefix to be displayed.
6352 if (param->fp_get_default_lmv) {
6353 struct lmv_user_md *lum = param->fp_lmv_md;
6355 lum->lum_magic = LMV_USER_MAGIC;
6356 lum->lum_stripe_count = 0;
6357 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
6359 } else if (param->fp_get_lmv) {
6360 struct lmv_user_md *lum = param->fp_lmv_md;
6363 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6366 lum->lum_magic = LMV_MAGIC_V1;
6367 lum->lum_stripe_count = 0;
6368 lum->lum_stripe_offset = mdtidx;
6371 struct lov_user_md *lmm =
6372 ¶m->fp_lmd->lmd_lmm;
6374 lmm->lmm_magic = LOV_USER_MAGIC_V1;
6376 ostid_set_seq(&lmm->lmm_oi,
6377 FID_SEQ_LOV_DEFAULT);
6378 lmm->lmm_stripe_count = 0;
6379 lmm->lmm_stripe_size = 0;
6380 lmm->lmm_stripe_offset = -1;
6383 } else if (errno == ENODATA && p != -1) {
6384 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
6385 llapi_printf(LLAPI_MSG_NORMAL,
6386 "%s has no stripe info\n", path);
6388 } else if (errno == ENOENT) {
6389 llapi_error(LLAPI_MSG_WARN, -ENOENT,
6390 "warning: %s: %s does not exist",
6393 } else if (errno == ENOTTY) {
6395 llapi_error(LLAPI_MSG_ERROR, ret,
6396 "%s: '%s' not on a Lustre fs?",
6401 llapi_error(LLAPI_MSG_ERROR, ret,
6402 "error: %s: %s failed for %s",
6404 "LL_IOC_LOV_GETSTRIPE" :
6405 "IOC_MDC_GETFILESTRIPE", path);
6412 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
6413 llapi_lov_dump_user_lmm(param, path, d != -1 ? LDF_IS_DIR : 0);
6416 /* Do not get down anymore? */
6417 if (param->fp_depth == param->fp_max_depth)
6425 int llapi_getstripe(char *path, struct find_param *param)
6427 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
6428 cb_get_mdt_index : cb_getstripe,
6429 cb_common_fini, param);
6432 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
6433 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6435 char raw[MAX_IOC_BUFLEN] = {'\0'};
6437 struct obd_ioctl_data data = { 0 };
6440 data.ioc_inlbuf1 = (char *)&type;
6441 data.ioc_inllen1 = sizeof(__u32);
6442 data.ioc_inlbuf2 = (char *)&index;
6443 data.ioc_inllen2 = sizeof(__u32);
6444 data.ioc_pbuf1 = (char *)stat_buf;
6445 data.ioc_plen1 = sizeof(struct obd_statfs);
6446 data.ioc_pbuf2 = (char *)uuid_buf;
6447 data.ioc_plen2 = sizeof(struct obd_uuid);
6449 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
6451 llapi_error(LLAPI_MSG_ERROR, rc,
6452 "%s: error packing ioctl data", __func__);
6456 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
6458 return rc < 0 ? -errno : 0;
6461 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
6462 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6467 fd = open(path, O_RDONLY);
6470 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
6473 * If we can't even open a file on the filesystem (e.g. with
6474 * -ESHUTDOWN), force caller to exit or it will loop forever.
6479 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
6486 #define MAX_STRING_SIZE 128
6488 int llapi_ping(char *obd_type, char *obd_name)
6490 int flags = O_RDONLY;
6491 char buf[1] = { 0 };
6495 rc = cfs_get_param_paths(&path, "%s/%s/ping",
6496 obd_type, obd_name);
6500 fd = open(path.gl_pathv[0], flags);
6502 if (errno == EACCES && flags == O_RDONLY) {
6507 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
6512 if (flags == O_RDONLY)
6513 rc = read(fd, buf, sizeof(buf));
6515 rc = write(fd, buf, sizeof(buf));
6523 cfs_free_param_data(&path);
6527 int llapi_target_iterate(int type_num, char **obd_type,
6528 void *args, llapi_cb_t cb)
6534 for (i = 0; i < type_num; i++) {
6537 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
6541 for (j = 0; j < param.gl_pathc; j++) {
6542 char obd_uuid[UUID_MAX + 1];
6546 fp = fopen(param.gl_pathv[j], "r");
6549 llapi_error(LLAPI_MSG_ERROR, rc,
6550 "error: opening '%s'",
6555 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
6557 llapi_error(LLAPI_MSG_ERROR, rc,
6558 "error: reading '%s'",
6563 /* Extract the obd_name from the sysfs path.
6564 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
6566 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
6572 /* skip /fs/lustre/'obd_type'/ */
6573 obd_name += strlen(obd_type[i]) + 12;
6574 /* chop off after obd_name */
6575 ptr = strrchr(obd_name, '/');
6579 cb(obd_type[i], obd_name, obd_uuid, args);
6588 cfs_free_param_data(¶m);
6592 static void do_target_check(char *obd_type_name, char *obd_name,
6593 char *obd_uuid, void *args)
6597 rc = llapi_ping(obd_type_name, obd_name);
6599 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
6601 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
6603 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
6606 int llapi_target_check(int type_num, char **obd_type, char *dir)
6608 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
6611 #undef MAX_STRING_SIZE
6613 /* Is this a lustre fs? */
6614 int llapi_is_lustre_mnttype(const char *type)
6616 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
6619 /* Is this a lustre client fs? */
6620 int llapi_is_lustre_mnt(struct mntent *mnt)
6622 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
6623 strstr(mnt->mnt_fsname, ":/") != NULL);
6626 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
6628 char fsname[PATH_MAX + 1];
6632 rc = llapi_search_fsname(mnt, fsname);
6636 root = open(mnt, O_RDONLY | O_DIRECTORY);
6639 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
6643 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
6646 if (rc == -ENOENT && LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd))
6647 llapi_error(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, rc,
6648 "Cannot find pool '%s'", qctl->qc_poolname);
6654 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
6659 root = open(mnt, O_RDONLY | O_DIRECTORY);
6662 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
6666 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
6669 llapi_error(LLAPI_MSG_ERROR, rc,
6670 "ioctl on %s for getting connect flags failed", mnt);
6677 * Flush cached pages from all clients.
6679 * \param fd File descriptor
6683 int llapi_file_flush(int fd)
6687 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);