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>
76 #include <libcfs/util/ioctl.h>
77 #include <libcfs/util/param.h>
78 #include <libcfs/util/string.h>
79 #include <linux/lnet/lnetctl.h>
80 #include <lustre/lustreapi.h>
81 #include <linux/lustre/lustre_ostid.h>
82 #include <linux/lustre/lustre_ioctl.h>
83 #include "lustreapi_internal.h"
86 #define FORMATTED_BUF_LEN 1024
88 static int llapi_msg_level = LLAPI_MSG_MAX;
89 const char *liblustreapi_cmd;
91 struct lustre_foreign_type lu_foreign_types[] = {
92 {.lft_type = LU_FOREIGN_TYPE_NONE, .lft_name = "none"},
93 {.lft_type = LU_FOREIGN_TYPE_SYMLINK, .lft_name = "symlink"},
94 /* must be the last element */
95 {.lft_type = LU_FOREIGN_TYPE_UNKNOWN, .lft_name = NULL}
96 /* array max dimension must be <= UINT32_MAX */
99 void llapi_msg_set_level(int level)
101 /* ensure level is in the good range */
102 if (level < LLAPI_MSG_OFF)
103 llapi_msg_level = LLAPI_MSG_OFF;
104 else if (level > LLAPI_MSG_MAX)
105 llapi_msg_level = LLAPI_MSG_MAX;
107 llapi_msg_level = level;
110 int llapi_msg_get_level(void)
112 return llapi_msg_level;
115 void llapi_set_command_name(const char *cmd)
117 liblustreapi_cmd = cmd;
120 void llapi_clear_command_name(void)
122 liblustreapi_cmd = NULL;
125 static void error_callback_default(enum llapi_message_level level, int err,
126 const char *fmt, va_list ap)
128 bool has_nl = strchr(fmt, '\n') != NULL;
130 if (liblustreapi_cmd != NULL)
131 fprintf(stderr, "%s %s: ", program_invocation_short_name,
134 fprintf(stderr, "%s: ", program_invocation_short_name);
137 if (level & LLAPI_MSG_NO_ERRNO) {
138 vfprintf(stderr, fmt, ap);
140 fprintf(stderr, "\n");
145 * Remove trailing linefeed so error string can be appended.
146 * @fmt is a const string, so we can't modify it directly.
148 if (has_nl && (newfmt = strdup(fmt)))
149 *strrchr(newfmt, '\n') = '\0';
151 newfmt = (char *)fmt;
153 vfprintf(stderr, newfmt, ap);
156 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
160 static void info_callback_default(enum llapi_message_level level, int err,
161 const char *fmt, va_list ap)
164 if (liblustreapi_cmd != NULL) {
165 fprintf(stdout, "%s %s: ",
166 program_invocation_short_name,
169 fprintf(stdout, "%s: ", program_invocation_short_name);
172 vfprintf(stdout, fmt, ap);
175 static llapi_log_callback_t llapi_error_callback = error_callback_default;
176 static llapi_log_callback_t llapi_info_callback = info_callback_default;
179 /* llapi_error will preserve errno */
180 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
183 int tmp_errno = errno;
185 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
189 llapi_error_callback(level, abs(err), fmt, args);
194 /* llapi_printf will preserve errno */
195 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
198 int tmp_errno = errno;
200 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
204 llapi_info_callback(level, 0, fmt, args);
210 * Set a custom error logging function. Passing in NULL will reset the logging
211 * callback to its default value.
213 * This function returns the value of the old callback.
215 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
217 llapi_log_callback_t old = llapi_error_callback;
220 llapi_error_callback = cb;
222 llapi_error_callback = error_callback_default;
228 * Set a custom info logging function. Passing in NULL will reset the logging
229 * callback to its default value.
231 * This function returns the value of the old callback.
233 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
235 llapi_log_callback_t old = llapi_info_callback;
238 llapi_info_callback = cb;
240 llapi_info_callback = info_callback_default;
246 * size_units is to be initialized (or zeroed) by caller.
248 int llapi_parse_size(const char *optarg, unsigned long long *size,
249 unsigned long long *size_units, int bytes_spec)
252 char *argbuf = (char *)optarg;
253 unsigned long long frac = 0, frac_d = 1;
255 if (strncmp(optarg, "-", 1) == 0)
258 if (*size_units == 0)
261 *size = strtoull(argbuf, &end, 0);
262 if (end != NULL && *end == '.') {
266 frac = strtoull(argbuf, &end, 10);
267 /* count decimal places */
268 for (i = 0; i < (end - argbuf); i++)
273 if ((*end == 'b') && *(end + 1) == '\0' &&
274 (*size & (~0ULL << (64 - 9))) == 0 &&
276 *size_units = 1 << 9;
277 } else if ((*end == 'b') &&
278 *(end + 1) == '\0' &&
281 } else if ((*end == 'k' || *end == 'K') &&
282 *(end + 1) == '\0' &&
283 (*size & (~0ULL << (64 - 10))) == 0) {
284 *size_units = 1 << 10;
285 } else if ((*end == 'm' || *end == 'M') &&
286 *(end + 1) == '\0' &&
287 (*size & (~0ULL << (64 - 20))) == 0) {
288 *size_units = 1 << 20;
289 } else if ((*end == 'g' || *end == 'G') &&
290 *(end + 1) == '\0' &&
291 (*size & (~0ULL << (64 - 30))) == 0) {
292 *size_units = 1 << 30;
293 } else if ((*end == 't' || *end == 'T') &&
294 *(end + 1) == '\0' &&
295 (*size & (~0ULL << (64 - 40))) == 0) {
296 *size_units = 1ULL << 40;
297 } else if ((*end == 'p' || *end == 'P') &&
298 *(end + 1) == '\0' &&
299 (*size & (~0ULL << (64 - 50))) == 0) {
300 *size_units = 1ULL << 50;
301 } else if ((*end == 'e' || *end == 'E') &&
302 *(end + 1) == '\0' &&
303 (*size & (~0ULL << (64 - 60))) == 0) {
304 *size_units = 1ULL << 60;
309 *size = *size * *size_units + frac * *size_units / frac_d;
314 int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
316 struct obd_ioctl_data *overlay;
319 data->ioc_len = obd_ioctl_packlen(data);
320 data->ioc_version = OBD_IOCTL_VERSION;
322 if (*pbuf != NULL && data->ioc_len > max_len) {
323 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
324 "pbuf = %p, ioc_len = %u, max_len = %d",
325 *pbuf, data->ioc_len, max_len);
330 *pbuf = malloc(data->ioc_len);
335 overlay = (struct obd_ioctl_data *)*pbuf;
336 memcpy(*pbuf, data, sizeof(*data));
338 ptr = overlay->ioc_bulk;
339 if (data->ioc_inlbuf1) {
340 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
341 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
344 if (data->ioc_inlbuf2) {
345 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
346 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
349 if (data->ioc_inlbuf3) {
350 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
351 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
354 if (data->ioc_inlbuf4) {
355 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
356 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
362 int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
364 struct obd_ioctl_data *overlay;
370 overlay = (struct obd_ioctl_data *)pbuf;
372 /* Preserve the caller's buffer pointers */
373 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
374 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
375 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
376 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
378 memcpy(data, pbuf, sizeof(*data));
380 ptr = overlay->ioc_bulk;
381 if (data->ioc_inlbuf1) {
382 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
383 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
386 if (data->ioc_inlbuf2) {
387 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
388 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
391 if (data->ioc_inlbuf3) {
392 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
393 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
396 if (data->ioc_inlbuf4) {
397 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
398 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
405 * Verify the setstripe parameters before using.
406 * This is a pair method for comp_args_to_layout()/llapi_layout_sanity_cb()
407 * when just 1 component or a non-PFL layout is given.
409 * \param[in] param stripe parameters
410 * \param[in] pool_name pool name
411 * \param[in] fsname lustre FS name
414 * < 0, error code on failre
416 static int llapi_stripe_param_verify(const struct llapi_stripe_param *param,
417 const char **pool_name)
420 static int page_size;
423 if (page_size == 0) {
425 * 64 KB is the largest common page size (on ia64/PPC/ARM),
426 * but check the local page size just in case. The page_size
427 * will not change for the lifetime of this process at least.
429 page_size = LOV_MIN_STRIPE_SIZE;
430 if (getpagesize() > page_size) {
431 page_size = getpagesize();
432 llapi_err_noerrno(LLAPI_MSG_WARN,
433 "warning: page size (%u) larger than expected (%u)",
434 page_size, LOV_MIN_STRIPE_SIZE);
437 if (!llapi_stripe_size_is_aligned(param->lsp_stripe_size)) {
439 llapi_error(LLAPI_MSG_ERROR, rc,
440 "error: bad stripe_size %llu, must be an even multiple of %d bytes",
441 param->lsp_stripe_size, page_size);
444 if (!llapi_stripe_index_is_valid(param->lsp_stripe_offset)) {
446 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
447 param->lsp_stripe_offset);
450 if (llapi_stripe_size_is_too_big(param->lsp_stripe_size)) {
452 llapi_error(LLAPI_MSG_ERROR, rc,
453 "error: stripe size '%llu' over 4GB limit",
454 param->lsp_stripe_size);
458 count = param->lsp_stripe_count;
459 if (param->lsp_stripe_pattern == LOV_PATTERN_MDT) {
461 llapi_error(LLAPI_MSG_ERROR, rc,
462 "Invalid pattern: '-L mdt', must be specified "
466 if (!llapi_stripe_count_is_valid(count)) {
468 llapi_error(LLAPI_MSG_ERROR, rc,
469 "Invalid stripe count %d\n", count);
474 /* Make sure we have a good pool */
475 if (*pool_name != NULL) {
476 if (!llapi_pool_name_is_valid(pool_name)) {
478 llapi_error(LLAPI_MSG_ERROR, rc,
479 "Invalid Poolname '%s'", *pool_name);
489 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
494 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
496 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
500 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
502 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
507 if (!llapi_dir_hash_type_is_valid(hash_type)) {
509 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
517 * Trim a trailing newline from a string, if it exists.
519 int llapi_chomp_string(char *buf)
535 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
537 static int get_param_lov(const char *path, const char *param,
538 char *buf, size_t buf_size)
540 struct obd_uuid uuid;
543 rc = llapi_file_get_lov_uuid(path, &uuid);
547 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
552 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
554 static int get_param_lmv(const char *path, const char *param,
555 char *buf, size_t buf_size)
557 struct obd_uuid uuid;
560 rc = llapi_file_get_lmv_uuid(path, &uuid);
564 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
568 static int get_mds_md_size(const char *path)
570 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
573 * Rather than open the file and do the ioctl to get the
574 * instance name and close the file and search for the param
575 * file and open the param file and read the param file and
576 * parse the value and close the param file, let's just return
577 * a large enough value. It's 2020, RAM is cheap and this is
581 if (md_size < XATTR_SIZE_MAX)
582 md_size = XATTR_SIZE_MAX;
587 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
589 return get_param_lmv(path, "uuid", buf, bufsize);
593 * Open a Lustre file.
595 * \param name the name of the file to be opened
596 * \param flags access mode, see flags in open(2)
597 * \param mode permission of the file if it is created, see mode in open(2)
598 * \param param stripe pattern of the newly created file
600 * \retval file descriptor of opened file
601 * \retval negative errno on failure
603 int llapi_file_open_param(const char *name, int flags, mode_t mode,
604 const struct llapi_stripe_param *param)
606 struct lov_user_md *lum = NULL;
607 const char *pool_name = param->lsp_pool;
611 /* Check if the stripe pattern is sane. */
612 rc = llapi_stripe_param_verify(param, &pool_name);
616 if (param->lsp_is_specific)
617 lum_size = lov_user_md_size(param->lsp_stripe_count,
618 LOV_USER_MAGIC_SPECIFIC);
620 lum_size = sizeof(struct lov_user_md_v3);
622 lum_size = sizeof(*lum);
624 lum = calloc(1, lum_size);
629 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
631 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
632 flags = O_DIRECTORY | O_RDONLY;
639 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
644 /* Initialize IOCTL striping pattern structure */
645 lum->lmm_magic = LOV_USER_MAGIC_V1;
646 lum->lmm_pattern = param->lsp_stripe_pattern;
647 lum->lmm_stripe_size = param->lsp_stripe_size;
648 lum->lmm_stripe_count = param->lsp_stripe_count;
649 lum->lmm_stripe_offset = param->lsp_stripe_offset;
650 if (pool_name != NULL) {
651 struct lov_user_md_v3 *lumv3 = (void *)lum;
653 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
654 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
656 if (param->lsp_is_specific) {
657 struct lov_user_md_v3 *lumv3 = (void *)lum;
660 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
661 if (pool_name == NULL) {
663 * LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
664 * OST list, therefore if pool is not specified we have
665 * to pack a null pool name for placeholder.
667 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
670 for (i = 0; i < param->lsp_stripe_count; i++)
671 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
674 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
675 char errmsg[512] = "stripe already set";
678 if (errno != EEXIST && errno != EALREADY)
679 strncpy(errmsg, strerror(errno), sizeof(errmsg) - 1);
680 if (rc == -EREMOTEIO)
681 snprintf(errmsg, sizeof(errmsg),
682 "inactive OST among your specified %d OST(s)",
683 param->lsp_stripe_count);
685 llapi_err_noerrno(LLAPI_MSG_ERROR,
686 "setstripe error for '%s': %s", name, errmsg);
697 int llapi_file_is_encrypted(int fd)
702 rc = ioctl(fd, FS_IOC_GETFLAGS, &flags);
706 return !!(flags & LUSTRE_ENCRYPT_FL);
709 int llapi_file_open_pool(const char *name, int flags, int mode,
710 unsigned long long stripe_size, int stripe_offset,
711 int stripe_count, int stripe_pattern, char *pool_name)
713 const struct llapi_stripe_param param = {
714 .lsp_stripe_size = stripe_size,
715 .lsp_stripe_count = stripe_count,
716 .lsp_stripe_pattern = stripe_pattern,
717 .lsp_stripe_offset = stripe_offset,
718 .lsp_pool = pool_name
720 return llapi_file_open_param(name, flags, mode, ¶m);
723 int llapi_file_open(const char *name, int flags, int mode,
724 unsigned long long stripe_size, int stripe_offset,
725 int stripe_count, int stripe_pattern)
727 return llapi_file_open_pool(name, flags, mode, stripe_size,
728 stripe_offset, stripe_count,
729 stripe_pattern, NULL);
732 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
733 __u32 flags, char *foreign_lov)
736 struct lov_foreign_md *lfm;
739 if (foreign_lov == NULL) {
741 llapi_error(LLAPI_MSG_ERROR, rc,
742 "foreign LOV EA content must be provided");
746 len = strlen(foreign_lov);
747 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
750 llapi_error(LLAPI_MSG_ERROR, rc,
751 "foreign LOV EA size %zu (must be 0 < len < %zu)",
752 len, XATTR_SIZE_MAX -
753 offsetof(struct lov_foreign_md, lfm_value));
757 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
760 llapi_error(LLAPI_MSG_ERROR, rc,
761 "failed to allocate lov_foreign_md");
765 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
768 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
772 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
773 lfm->lfm_length = len;
774 lfm->lfm_type = type;
775 lfm->lfm_flags = flags;
776 memcpy(lfm->lfm_value, foreign_lov, len);
778 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
779 char *errmsg = "stripe already set";
783 errmsg = "not on a Lustre filesystem";
784 else if (errno == EEXIST || errno == EALREADY)
785 errmsg = "stripe already set";
787 errmsg = strerror(errno);
789 llapi_err_noerrno(LLAPI_MSG_ERROR,
790 "setstripe error for '%s': %s", name, errmsg);
806 int llapi_file_create(const char *name, unsigned long long stripe_size,
807 int stripe_offset, int stripe_count, int stripe_pattern)
811 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
812 stripe_offset, stripe_count, stripe_pattern,
821 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
822 int stripe_offset, int stripe_count,
823 int stripe_pattern, char *pool_name)
827 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
828 stripe_offset, stripe_count, stripe_pattern,
837 static int verify_dir_param(const char *name,
838 const struct llapi_stripe_param *param)
840 char fsname[MAX_OBD_NAME + 1] = { 0 };
841 char *pool_name = param->lsp_pool;
844 /* Make sure we are on a Lustre file system */
845 rc = llapi_search_fsname(name, fsname);
847 llapi_error(LLAPI_MSG_ERROR, rc,
848 "'%s' is not on a Lustre filesystem",
853 /* Check if the stripe pattern is sane. */
854 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
855 param->lsp_stripe_count,
856 param->lsp_stripe_pattern);
860 /* Make sure we have a good pool */
861 if (pool_name != NULL) {
863 * in case user gives the full pool name <fsname>.<poolname>,
866 char *ptr = strchr(pool_name, '.');
870 if (strcmp(pool_name, fsname) != 0) {
872 llapi_err_noerrno(LLAPI_MSG_ERROR,
873 "Pool '%s' is not on filesystem '%s'",
880 /* Make sure the pool exists and is non-empty */
881 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
883 char *err = rc == 0 ? "has no OSTs" : "does not exist";
885 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
886 fsname, pool_name, err);
891 /* sanity check of target list */
892 if (param->lsp_is_specific) {
893 char mdtname[MAX_OBD_NAME + 64];
897 for (i = 0; i < param->lsp_stripe_count; i++) {
898 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
899 fsname, param->lsp_tgts[i]);
900 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
905 llapi_error(LLAPI_MSG_ERROR, rc,
906 "%s: cannot find MDT %s in %s",
913 /* Make sure stripe offset is in MDT list. */
914 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
918 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
919 "%s: stripe offset '%d' is not in the target list",
920 __func__, param->lsp_stripe_offset);
928 static inline void param2lmu(struct lmv_user_md *lmu,
929 const struct llapi_stripe_param *param)
931 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
933 lmu->lum_stripe_count = param->lsp_stripe_count;
934 lmu->lum_stripe_offset = param->lsp_stripe_offset;
935 lmu->lum_hash_type = param->lsp_stripe_pattern;
936 lmu->lum_max_inherit = param->lsp_max_inherit;
937 lmu->lum_max_inherit_rr = param->lsp_max_inherit_rr;
938 if (param->lsp_pool != NULL)
939 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
940 if (param->lsp_is_specific) {
943 for (i = 0; i < param->lsp_stripe_count; i++)
944 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
948 int llapi_dir_set_default_lmv(const char *name,
949 const struct llapi_stripe_param *param)
951 struct lmv_user_md lmu = { 0 };
955 rc = verify_dir_param(name, param);
959 /* TODO: default lmv doesn't support specific targets yet */
960 if (param->lsp_is_specific)
963 param2lmu(&lmu, param);
965 fd = open(name, O_DIRECTORY | O_RDONLY);
968 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
972 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
974 char *errmsg = "stripe already set";
977 if (errno != EEXIST && errno != EALREADY)
978 errmsg = strerror(errno);
980 llapi_err_noerrno(LLAPI_MSG_ERROR,
981 "default dirstripe error on '%s': %s",
988 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
989 int stripe_count, int stripe_pattern,
990 const char *pool_name)
992 const struct llapi_stripe_param param = {
993 .lsp_stripe_count = stripe_count,
994 .lsp_stripe_offset = stripe_offset,
995 .lsp_stripe_pattern = stripe_pattern,
996 .lsp_pool = (char *)pool_name
999 return llapi_dir_set_default_lmv(name, ¶m);
1003 * Create a Lustre directory.
1005 * \param name the name of the directory to be created
1006 * \param mode permission of the file if it is created, see mode in open(2)
1007 * \param param stripe pattern of the newly created directory
1009 * \retval 0 on success
1010 * \retval negative errno on failure
1012 int llapi_dir_create(const char *name, mode_t mode,
1013 const struct llapi_stripe_param *param)
1015 struct lmv_user_md *lmu = NULL;
1017 struct obd_ioctl_data data = { 0 };
1020 char *dirpath = NULL;
1021 char *namepath = NULL;
1026 rc = verify_dir_param(name, param);
1030 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1031 param->lsp_is_specific ?
1032 LMV_USER_MAGIC_SPECIFIC :
1035 lmu = calloc(1, lmu_size);
1039 dirpath = strdup(name);
1045 namepath = strdup(name);
1052 param2lmu(lmu, param);
1054 filename = basename(namepath);
1055 dir = dirname(dirpath);
1057 data.ioc_inlbuf1 = (char *)filename;
1058 data.ioc_inllen1 = strlen(filename) + 1;
1059 data.ioc_inlbuf2 = (char *)lmu;
1060 data.ioc_inllen2 = lmu_size;
1061 data.ioc_type = mode;
1062 if (param->lsp_is_create)
1063 /* borrow obdo1.o_flags to store this flag */
1064 data.ioc_obdo1.o_flags = OBD_FL_OBDMDEXISTS;
1065 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1067 llapi_error(LLAPI_MSG_ERROR, rc,
1068 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1073 fd = open(dir, O_DIRECTORY | O_RDONLY);
1076 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1080 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1081 char *errmsg = "stripe already set";
1084 if (errno != EEXIST && errno != EALREADY)
1085 errmsg = strerror(errno);
1087 llapi_err_noerrno(LLAPI_MSG_ERROR,
1088 "dirstripe error on '%s': %s", name, errmsg);
1099 * Create a foreign directory.
1101 * \param name the name of the directory to be created
1102 * \param mode permission of the file if it is created, see mode in open(2)
1103 * \param type foreign type to be set in LMV EA
1104 * \param flags foreign flags to be set in LMV EA
1105 * \param value foreign pattern to be set in LMV EA
1107 * \retval 0 on success
1108 * \retval negative errno on failure
1110 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1111 __u32 flags, const char *value)
1113 struct lmv_foreign_md *lfm = NULL;
1114 size_t lfm_size, len;
1115 struct obd_ioctl_data data = { 0 };
1118 char *dirpath = NULL;
1119 char *namepath = NULL;
1124 len = strlen(value);
1125 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1128 llapi_error(LLAPI_MSG_ERROR, rc,
1129 "invalid LOV EA length %zu (must be 0 < len < %zu)",
1130 len, XATTR_SIZE_MAX -
1131 offsetof(struct lmv_foreign_md, lfm_value));
1134 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1135 lfm = calloc(1, lfm_size);
1139 dirpath = strdup(name);
1145 namepath = strdup(name);
1152 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1153 lfm->lfm_length = len;
1154 lfm->lfm_type = type;
1155 lfm->lfm_flags = flags;
1156 memcpy(lfm->lfm_value, value, len);
1158 filename = basename(namepath);
1159 dir = dirname(dirpath);
1161 data.ioc_inlbuf1 = (char *)filename;
1162 data.ioc_inllen1 = strlen(filename) + 1;
1163 data.ioc_inlbuf2 = (char *)lfm;
1164 data.ioc_inllen2 = lfm_size;
1165 data.ioc_type = mode;
1166 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1168 llapi_error(LLAPI_MSG_ERROR, rc,
1169 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1174 fd = open(dir, O_DIRECTORY | O_RDONLY);
1177 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1181 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1182 char *errmsg = "stripe already set";
1185 if (errno != EEXIST && errno != EALREADY)
1186 errmsg = strerror(errno);
1188 llapi_err_noerrno(LLAPI_MSG_ERROR,
1189 "dirstripe error on '%s': %s", name, errmsg);
1199 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1200 int stripe_count, int stripe_pattern,
1201 const char *pool_name)
1203 const struct llapi_stripe_param param = {
1204 .lsp_stripe_count = stripe_count,
1205 .lsp_stripe_offset = stripe_offset,
1206 .lsp_stripe_pattern = stripe_pattern,
1207 .lsp_pool = (char *)pool_name
1210 return llapi_dir_create(name, mode, ¶m);
1214 * Get the list of pool members.
1215 * \param poolname string of format \<fsname\>.\<poolname\>
1216 * \param members caller-allocated array of char*
1217 * \param list_size size of the members array
1218 * \param buffer caller-allocated buffer for storing OST names
1219 * \param buffer_size size of the buffer
1221 * \return number of members retrieved for this pool
1222 * \retval -error failure
1224 int llapi_get_poolmembers(const char *poolname, char **members,
1225 int list_size, char *buffer, int buffer_size)
1227 char fsname[PATH_MAX];
1236 /* name is FSNAME.POOLNAME */
1237 if (strlen(poolname) >= sizeof(fsname))
1240 snprintf(fsname, sizeof(fsname), "%s", poolname);
1241 pool = strchr(fsname, '.');
1248 rc = poolpath(&pathname, fsname, NULL);
1250 llapi_error(LLAPI_MSG_ERROR, rc,
1251 "Lustre filesystem '%s' not found",
1256 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1257 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1258 cfs_free_param_data(&pathname);
1259 if (rc >= sizeof(buf))
1261 fd = fopen(buf, "r");
1264 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1269 while (fgets(buf, sizeof(buf), fd) != NULL) {
1270 if (nb_entries >= list_size) {
1274 buf[sizeof(buf) - 1] = '\0';
1276 tmp = strchr(buf, '\n');
1279 if (used + strlen(buf) + 1 > buffer_size) {
1284 strcpy(buffer + used, buf);
1285 members[nb_entries] = buffer + used;
1286 used += strlen(buf) + 1;
1296 * Get the list of pools in a filesystem.
1297 * \param name filesystem name or path
1298 * \param poollist caller-allocated array of char*
1299 * \param list_size size of the poollist array
1300 * \param buffer caller-allocated buffer for storing pool names
1301 * \param buffer_size size of the buffer
1303 * \return number of pools retrieved for this filesystem
1304 * \retval -error failure
1306 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1307 char *buffer, int buffer_size)
1313 struct dirent *pool;
1315 unsigned int nb_entries = 0;
1316 unsigned int used = 0;
1319 /* initialize output array */
1320 for (i = 0; i < list_size; i++)
1323 /* is name a pathname ? */
1324 ptr = strchr(name, '/');
1326 char fsname_buf[MAXNAMLEN];
1328 /* We will need fsname for printing later */
1329 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1333 ptr = strrchr(fsname_buf, '-');
1337 fsname = strdup(fsname_buf);
1341 /* name is FSNAME */
1342 fsname = strdup(name);
1347 rc = poolpath(&pathname, fsname, NULL);
1349 llapi_error(LLAPI_MSG_ERROR, rc,
1350 "Lustre filesystem '%s' not found", name);
1354 dir = opendir(pathname.gl_pathv[0]);
1357 llapi_error(LLAPI_MSG_ERROR, rc,
1358 "Could not open pool list for '%s'",
1365 pool = readdir(dir);
1371 /* ignore . and .. */
1372 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1375 /* check output bounds */
1376 if (nb_entries >= list_size) {
1378 goto free_dir_no_msg;
1381 /* +2 for '.' and final '\0' */
1382 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1385 goto free_dir_no_msg;
1388 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1389 poollist[nb_entries] = buffer + used;
1390 used += strlen(pool->d_name) + strlen(fsname) + 2;
1396 llapi_error(LLAPI_MSG_ERROR, rc,
1397 "Error reading pool list for '%s'", name);
1399 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1404 cfs_free_param_data(&pathname);
1407 return rc != 0 ? rc : nb_entries;
1410 /* wrapper for lfs.c and obd.c */
1411 int llapi_poollist(const char *name)
1413 int poolcount, rc, i;
1416 rc = llapi_get_poolbuf(name, &buf, &pools, &poolcount);
1420 for (i = 0; i < poolcount; i++)
1421 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", pools[i]);
1428 * Get buffer that holds uuids and the list of pools in a filesystem.
1430 * \param name filesystem name or path
1431 * \param buf bufffer that has to be freed if function returns 0
1432 * \param pools pointer to the list of pools in buffer
1433 * \param poolcount number of pools
1435 * \return 0 when found at least 1 pool, i.e. poolcount > 0
1436 * \retval -error failure
1438 int llapi_get_poolbuf(const char *name, char **buf,
1439 char ***pools, int *poolcount)
1442 * list of pool names (assume that pool count is smaller
1445 char **list, *buffer = NULL, *fsname = (char *)name;
1446 char *poolname = NULL, *tmp = NULL, data[16];
1447 enum param_filter type = FILTER_BY_PATH;
1448 int obdcount, bufsize, rc, nb;
1453 if (name[0] != '/') {
1454 fsname = strdup(name);
1458 poolname = strchr(fsname, '.');
1461 type = FILTER_BY_FS_NAME;
1464 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1465 data, sizeof(data));
1468 obdcount = atoi(data);
1471 * Allocate space for each fsname-OST0000_UUID, 1 per OST,
1472 * and also an array to store the pointers for all that
1476 bufsize = sizeof(struct obd_uuid) * obdcount;
1477 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1478 if (buffer == NULL) {
1482 list = (char **) (buffer + bufsize);
1485 /* name is a path or fsname */
1486 nb = llapi_get_poollist(name, list, obdcount,
1489 /* name is a pool name (<fsname>.<poolname>) */
1490 nb = llapi_get_poolmembers(name, list, obdcount,
1494 if (nb == -EOVERFLOW) {
1497 goto retry_get_pools;
1500 rc = (nb < 0 ? nb : 0);
1507 /* Don't free buffer, it will be used later */
1510 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1515 typedef int (semantic_func_t)(char *path, int p, int *d,
1516 void *data, struct dirent64 *de);
1518 #define OBD_NOT_FOUND (-1)
1520 static bool lmv_is_foreign(__u32 magic)
1522 return magic == LMV_MAGIC_FOREIGN;
1525 static void find_param_fini(struct find_param *param)
1527 if (param->fp_migrate)
1530 if (param->fp_obd_indexes) {
1531 free(param->fp_obd_indexes);
1532 param->fp_obd_indexes = NULL;
1535 if (param->fp_lmd) {
1536 free(param->fp_lmd);
1537 param->fp_lmd = NULL;
1540 if (param->fp_lmv_md) {
1541 free(param->fp_lmv_md);
1542 param->fp_lmv_md = NULL;
1546 static int common_param_init(struct find_param *param, char *path)
1548 int lum_size = get_mds_md_size(path);
1553 /* migrate has fp_lmv_md initialized outside */
1554 if (param->fp_migrate)
1557 if (lum_size < PATH_MAX + 1)
1558 lum_size = PATH_MAX + 1;
1560 param->fp_lum_size = lum_size;
1561 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1563 if (param->fp_lmd == NULL) {
1564 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1565 "error: allocate %zu bytes for layout failed",
1566 sizeof(lstat_t) + param->fp_lum_size);
1570 param->fp_lmv_stripe_count = 256;
1571 param->fp_lmv_md = calloc(1,
1572 lmv_user_md_size(param->fp_lmv_stripe_count,
1573 LMV_USER_MAGIC_SPECIFIC));
1574 if (param->fp_lmv_md == NULL) {
1575 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1576 "error: allocation of %d bytes for ioctl",
1577 lmv_user_md_size(param->fp_lmv_stripe_count,
1578 LMV_USER_MAGIC_SPECIFIC));
1579 find_param_fini(param);
1583 param->fp_got_uuids = 0;
1584 param->fp_obd_indexes = NULL;
1585 param->fp_obd_index = OBD_NOT_FOUND;
1586 param->fp_mdt_index = OBD_NOT_FOUND;
1590 static int cb_common_fini(char *path, int p, int *dp, void *data,
1591 struct dirent64 *de)
1593 struct find_param *param = data;
1599 /* set errno upon failure */
1600 static int open_parent(const char *path)
1606 path_copy = strdup(path);
1607 if (path_copy == NULL)
1610 parent_path = dirname(path_copy);
1611 parent = open(parent_path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1617 static int cb_get_dirstripe(char *path, int *d, struct find_param *param)
1620 bool did_nofollow = false;
1625 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1626 if (param->fp_get_default_lmv) {
1630 /* open() may not fetch LOOKUP lock, statx() to ensure dir depth
1633 statx(*d, "", AT_EMPTY_PATH, STATX_MODE, &stx);
1639 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1641 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1644 ret = ioctl(*d, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1646 /* if ENOTTY likely to be a fake symlink, so try again after
1647 * new open() with O_NOFOLLOW, but only once to prevent any
1648 * loop like for the path of a file/dir not on Lustre !!
1650 if (ret < 0 && errno == ENOTTY && !did_nofollow) {
1653 did_nofollow = true;
1654 fd = open(path, O_RDONLY | O_NOFOLLOW);
1656 /* restore original errno */
1661 /* close original fd and set new */
1664 ret2 = ioctl(fd, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1665 if (ret2 < 0 && errno != E2BIG) {
1666 /* restore original errno */
1670 /* LMV is ok or need to handle E2BIG case now */
1674 if (errno == E2BIG && ret != 0) {
1678 /* if foreign LMV case, fake stripes number */
1679 if (lmv_is_foreign(param->fp_lmv_md->lum_magic)) {
1680 struct lmv_foreign_md *lfm;
1682 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1683 if (lfm->lfm_length < XATTR_SIZE_MAX -
1684 offsetof(typeof(*lfm), lfm_value)) {
1685 uint32_t size = lfm->lfm_length +
1686 offsetof(typeof(*lfm), lfm_value);
1688 stripe_count = lmv_foreign_to_md_stripes(size);
1690 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1691 "error: invalid %d foreign size returned from ioctl",
1696 stripe_count = param->fp_lmv_md->lum_stripe_count;
1698 if (stripe_count <= param->fp_lmv_stripe_count)
1701 free(param->fp_lmv_md);
1702 param->fp_lmv_stripe_count = stripe_count;
1703 lmv_size = lmv_user_md_size(stripe_count,
1704 LMV_USER_MAGIC_SPECIFIC);
1705 param->fp_lmv_md = malloc(lmv_size);
1706 if (param->fp_lmv_md == NULL) {
1707 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1708 "error: allocation of %d bytes for ioctl",
1709 lmv_user_md_size(param->fp_lmv_stripe_count,
1710 LMV_USER_MAGIC_SPECIFIC));
1719 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1722 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
1723 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
1724 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
1725 lmd_v2->lmd_stx.stx_uid = st->st_uid;
1726 lmd_v2->lmd_stx.stx_gid = st->st_gid;
1727 lmd_v2->lmd_stx.stx_mode = st->st_mode;
1728 lmd_v2->lmd_stx.stx_ino = st->st_ino;
1729 lmd_v2->lmd_stx.stx_size = st->st_size;
1730 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
1731 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
1732 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
1733 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
1734 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
1735 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
1736 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
1737 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
1738 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
1740 lmd_v2->lmd_flags = 0;
1742 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1744 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
1745 if (lmd_v2->lmd_stx.stx_size)
1746 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
1747 if (lmd_v2->lmd_stx.stx_blocks)
1748 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
1750 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1751 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
1752 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
1757 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
1759 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
1760 struct lov_user_mds_data *lmd_v2 = lmdbuf;
1764 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
1768 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
1771 st = lmd_v1->lmd_st;
1772 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
1773 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
1774 convert_lmd_statx(lmd_v2, &st, false);
1775 lmd_v2->lmd_lmmsize = 0;
1776 lmd_v2->lmd_padding = 0;
1781 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
1782 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1784 struct lov_user_mds_data *lmd = lmdbuf;
1785 static bool use_old_ioctl;
1789 if (parent_fd < 0 && dir_fd < 0)
1791 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1796 * LL_IOC_MDC_GETINFO operates on the current directory inode
1797 * and returns struct lov_user_mds_data, while
1798 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
1800 if (type == GET_LMD_INFO)
1801 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_V1 :
1802 LL_IOC_MDC_GETINFO_V2;
1804 cmd = LL_IOC_LOV_GETSTRIPE;
1807 ret = ioctl(dir_fd, cmd, lmdbuf);
1808 if (ret < 0 && errno == ENOTTY &&
1809 cmd == LL_IOC_MDC_GETINFO_V2) {
1810 cmd = LL_IOC_MDC_GETINFO_V1;
1811 use_old_ioctl = true;
1815 if (cmd == LL_IOC_MDC_GETINFO_V1 && !ret)
1816 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
1818 if (ret < 0 && errno == ENOTTY && type == GET_LMD_STRIPE) {
1821 /* retry ioctl() after new open() with O_NOFOLLOW
1822 * just in case it could be a fake symlink
1823 * need using a new open() as dir_fd is being closed
1827 dir_fd2 = open(path, O_RDONLY | O_NDELAY | O_NOFOLLOW);
1829 /* return original error */
1832 ret = ioctl(dir_fd2, cmd, lmdbuf);
1833 /* pass new errno or success back to caller */
1839 } else if (parent_fd >= 0) {
1840 const char *fname = strrchr(path, '/');
1843 * IOC_MDC_GETFILEINFO takes as input the filename (relative to
1844 * the parent directory) and returns struct lov_user_mds_data,
1845 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
1847 * This avoids opening, locking, and closing each file on the
1848 * client if that is not needed. Multiple of these ioctl() can
1849 * be done on the parent dir with a single open for all
1850 * files in that directory, and it also doesn't pollute the
1851 * client dcache with millions of dentries when traversing
1852 * a large filesystem.
1854 fname = (fname == NULL ? path : fname + 1);
1856 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
1859 else if (ret >= lmdlen || ret++ == 0)
1862 if (type == GET_LMD_INFO)
1863 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_V1 :
1864 IOC_MDC_GETFILEINFO_V2;
1866 cmd = IOC_MDC_GETFILESTRIPE;
1869 ret = ioctl(parent_fd, cmd, lmdbuf);
1870 if (ret < 0 && errno == ENOTTY &&
1871 cmd == IOC_MDC_GETFILEINFO_V2) {
1872 cmd = IOC_MDC_GETFILEINFO_V1;
1873 use_old_ioctl = true;
1874 goto retry_getfileinfo;
1877 if (cmd == IOC_MDC_GETFILEINFO_V1 && !ret)
1878 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
1882 if (ret && type == GET_LMD_INFO) {
1883 if (errno == ENOTTY) {
1887 * ioctl is not supported, it is not a lustre fs.
1888 * Do the regular lstat(2) instead.
1890 ret = lstat_f(path, &st);
1893 llapi_error(LLAPI_MSG_ERROR, ret,
1894 "error: %s: lstat failed for %s",
1898 convert_lmd_statx(lmd, &st, true);
1900 * It may be wrong to set use_old_ioctl with true as
1901 * the file is not a lustre fs. So reset it with false
1904 use_old_ioctl = false;
1905 } else if (errno == ENOENT) {
1907 llapi_error(LLAPI_MSG_WARN, ret,
1908 "warning: %s does not exist", path);
1909 } else if (errno != EISDIR && errno != ENODATA) {
1911 llapi_error(LLAPI_MSG_ERROR, ret,
1912 "%s ioctl failed for %s.",
1913 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
1914 "IOC_MDC_GETFILEINFO", path);
1921 static int llapi_semantic_traverse(char *path, int size, int parent,
1922 semantic_func_t sem_init,
1923 semantic_func_t sem_fini, void *data,
1924 struct dirent64 *de)
1926 struct find_param *param = (struct find_param *)data;
1927 struct dirent64 *dent;
1928 int len, ret, d, p = -1;
1934 d = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1935 /* if an invalid fake dir symlink, opendir() will return EINVAL
1936 * instead of ENOTDIR. If a valid but dangling faked or real file/dir
1937 * symlink ENOENT will be returned. For a valid/resolved fake or real
1938 * file symlink ENOTDIR will be returned as for a regular file.
1939 * opendir() will be successful for a valid and resolved fake or real
1940 * dir simlink or a regular dir.
1942 if (d == -1 && errno != ENOTDIR && errno != EINVAL && errno != ENOENT) {
1944 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1947 } else if (d == -1) {
1948 if (errno == ENOENT || errno == EINVAL) {
1949 int old_errno = errno;
1951 /* try to open with O_NOFOLLOW this will help
1952 * differentiate fake vs real symlinks
1953 * it is ok to not use O_DIRECTORY with O_RDONLY
1954 * and it will prevent the need to deal with ENOTDIR
1955 * error, instead of ELOOP, being returned by recent
1956 * kernels for real symlinks
1958 d = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
1959 /* if a dangling real symlink should return ELOOP, or
1960 * again ENOENT if really non-existing path, or E...??
1961 * So return original error. If success or ENOTDIR, path
1962 * is likely to be a fake dir/file symlink, so continue
1972 if (parent == -1 && d == -1) {
1973 /* Open the parent dir. */
1974 p = open_parent(path);
1980 } else { /* d != -1 */
1983 /* try to reopen dir with O_NOFOLLOW just in case of a foreign
1986 d2 = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
1991 /* continue with d */
1997 ret = sem_init(path, (parent != -1) ? parent : p, &d, data, de);
2007 /* ENOTDIR if fake symlink, do not consider it as an error */
2008 if (errno != ENOTDIR)
2009 llapi_error(LLAPI_MSG_ERROR, errno,
2010 "fdopendir() failed");
2017 while ((dent = readdir64(dir)) != NULL) {
2020 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2024 if ((len + dent->d_reclen + 2) > size) {
2025 llapi_err_noerrno(LLAPI_MSG_ERROR,
2026 "error: %s: string buffer too small for %s",
2031 strcat(path, dent->d_name);
2033 if (dent->d_type == DT_UNKNOWN) {
2034 struct lov_user_mds_data *lmd = param->fp_lmd;
2036 rc = get_lmd_info_fd(path, d, -1, param->fp_lmd,
2037 param->fp_lum_size, GET_LMD_INFO);
2039 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2046 switch (dent->d_type) {
2048 llapi_err_noerrno(LLAPI_MSG_ERROR,
2049 "error: %s: '%s' is UNKNOWN type %d",
2050 __func__, dent->d_name, dent->d_type);
2053 rc = llapi_semantic_traverse(path, size, d, sem_init,
2054 sem_fini, data, dent);
2055 if (rc != 0 && ret == 0)
2057 if (rc < 0 && rc != -EALREADY &&
2058 param->fp_stop_on_error)
2064 rc = sem_init(path, d, NULL, data, dent);
2065 if (rc < 0 && ret == 0) {
2067 if (rc && rc != -EALREADY &&
2068 param->fp_stop_on_error)
2073 if (sem_fini && rc == 0)
2074 sem_fini(path, d, NULL, data, dent);
2082 sem_fini(path, parent, &d, data, de);
2095 static int param_callback(char *path, semantic_func_t sem_init,
2096 semantic_func_t sem_fini, struct find_param *param)
2098 int ret, len = strlen(path);
2101 if (len > PATH_MAX) {
2103 llapi_error(LLAPI_MSG_ERROR, ret,
2104 "Path name '%s' is too long", path);
2108 buf = (char *)malloc(2 * PATH_MAX);
2112 snprintf(buf, PATH_MAX + 1, "%s", path);
2113 ret = common_param_init(param, buf);
2117 param->fp_depth = 0;
2119 ret = llapi_semantic_traverse(buf, 2 * PATH_MAX, -1, sem_init,
2120 sem_fini, param, NULL);
2122 find_param_fini(param);
2124 return ret < 0 ? ret : 0;
2127 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2131 rc = ioctl(fd, OBD_IOC_GETDTNAME, lov_name);
2132 if (rc && errno == ENOTTY)
2133 rc = ioctl(fd, OBD_IOC_GETNAME_OLD, lov_name);
2136 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2142 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2146 rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2149 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2155 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2159 /* do not follow faked symlinks */
2160 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
2162 /* real symlink should have failed with ELOOP so retry without
2163 * O_NOFOLLOW just in case
2165 fd = open(path, O_RDONLY | O_NONBLOCK);
2168 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'",
2174 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2180 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2184 fd = open(path, O_RDONLY | O_NONBLOCK);
2187 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2191 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2203 * If uuidp is NULL, return the number of available obd uuids.
2204 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2205 * there are more OSTs than allocated to uuidp, then an error is returned with
2206 * the ost_count set to number of available obd uuids.
2208 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2209 int *ost_count, enum tgt_type type)
2211 char buf[PATH_MAX], format[32];
2212 int rc = 0, index = 0;
2213 struct obd_uuid name;
2217 /* Get the lov name */
2218 if (type == LOV_TYPE)
2219 rc = llapi_file_fget_lov_uuid(fd, &name);
2221 rc = llapi_file_fget_lmv_uuid(fd, &name);
2225 /* Now get the ost uuids */
2226 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2227 FILTER_BY_EXACT, "target_obd", ¶m);
2231 fp = fopen(param.gl_pathv[0], "r");
2234 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2239 snprintf(format, sizeof(format),
2240 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2241 while (fgets(buf, sizeof(buf), fp) != NULL) {
2242 if (uuidp && (index < *ost_count)) {
2243 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2251 if (uuidp && (index > *ost_count))
2256 cfs_free_param_data(¶m);
2260 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2262 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2265 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2270 root = open(mnt, O_RDONLY | O_DIRECTORY);
2273 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2278 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2287 * Check if user specified value matches a real uuid. Ignore _UUID,
2288 * -osc-4ba41334, other trailing gunk in comparison.
2289 * @param real_uuid ends in "_UUID"
2290 * @param search_uuid may or may not end in "_UUID"
2292 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2294 int cmplen = strlen(real_uuid);
2295 int searchlen = strlen(search_uuid);
2297 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2299 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2303 * The UUIDs may legitimately be different lengths, if
2304 * the system was upgraded from an older version.
2306 if (cmplen != searchlen)
2309 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2313 * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2314 * returned in param->fp_obd_index
2316 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2318 struct obd_uuid obd_uuid;
2325 if (param->fp_got_uuids)
2328 /* Get the lov/lmv name */
2329 if (param->fp_get_lmv)
2330 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2332 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2334 if (rc != -ENOTTY) {
2335 llapi_error(LLAPI_MSG_ERROR, rc,
2336 "error: can't get %s name: %s",
2337 param->fp_get_lmv ? "lmv" : "lov",
2345 param->fp_got_uuids = 1;
2347 /* Now get the ost uuids */
2348 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2349 obd_uuid.uuid, FILTER_BY_EXACT,
2350 "target_obd", ¶m_data);
2354 fp = fopen(param_data.gl_pathv[0], "r");
2357 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2358 param_data.gl_pathv[0]);
2362 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2363 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2364 param->fp_get_lmv ? "MDTS" : "OBDS");
2366 snprintf(format, sizeof(format),
2367 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2368 while (fgets(buf, sizeof(buf), fp) != NULL) {
2371 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2374 if (param->fp_obd_uuid) {
2375 if (llapi_uuid_match(obd_uuid.uuid,
2376 param->fp_obd_uuid->uuid)) {
2377 param->fp_obd_index = index;
2380 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2381 /* Print everything */
2382 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2385 param->fp_obds_printed = 1;
2389 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2390 llapi_err_noerrno(LLAPI_MSG_ERROR,
2391 "error: %s: unknown obduuid: %s",
2392 __func__, param->fp_obd_uuid->uuid);
2396 cfs_free_param_data(¶m_data);
2401 * In this case, param->fp_obd_uuid will be an array of obduuids and
2402 * obd index for all these obduuids will be returned in
2403 * param->fp_obd_indexes
2405 static int setup_indexes(int d, char *path, struct obd_uuid *obduuids,
2406 int num_obds, int **obdindexes, int *obdindex,
2409 int ret, obdcount, obd_valid = 0, obdnum;
2411 struct obd_uuid *uuids = NULL;
2415 if (type == LOV_TYPE)
2416 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2418 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2422 obdcount = atoi(buf);
2423 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2428 ret = llapi_get_target_uuids(d, uuids, &obdcount, type);
2430 if (ret == -EOVERFLOW) {
2431 struct obd_uuid *uuids_temp;
2433 uuids_temp = realloc(uuids, obdcount *
2434 sizeof(struct obd_uuid));
2435 if (uuids_temp != NULL) {
2437 goto retry_get_uuids;
2442 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2446 indexes = malloc(num_obds * sizeof(*obdindex));
2447 if (indexes == NULL) {
2452 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2455 /* The user may have specified a simple index */
2456 i = strtol(obduuids[obdnum].uuid, &end, 0);
2457 if (end && *end == '\0' && i < obdcount) {
2458 indexes[obdnum] = i;
2461 for (i = 0; i < obdcount; i++) {
2462 if (llapi_uuid_match(uuids[i].uuid,
2463 obduuids[obdnum].uuid)) {
2464 indexes[obdnum] = i;
2470 if (i >= obdcount) {
2471 indexes[obdnum] = OBD_NOT_FOUND;
2472 llapi_err_noerrno(LLAPI_MSG_ERROR,
2473 "invalid obduuid '%s'",
2474 obduuids[obdnum].uuid);
2480 *obdindex = OBD_NOT_FOUND;
2482 *obdindex = obd_valid;
2484 *obdindexes = indexes;
2492 static int setup_target_indexes(int d, char *path, struct find_param *param)
2496 if (param->fp_mdt_uuid) {
2497 ret = setup_indexes(d, path, param->fp_mdt_uuid,
2499 ¶m->fp_mdt_indexes,
2500 ¶m->fp_mdt_index, LMV_TYPE);
2505 if (param->fp_obd_uuid) {
2506 ret = setup_indexes(d, path, param->fp_obd_uuid,
2508 ¶m->fp_obd_indexes,
2509 ¶m->fp_obd_index, LOV_TYPE);
2514 param->fp_got_uuids = 1;
2519 int llapi_ostlist(char *path, struct find_param *param)
2524 fd = open(path, O_RDONLY | O_DIRECTORY);
2528 ret = setup_obd_uuid(fd, path, param);
2535 * Tries to determine the default stripe attributes for a given filesystem. The
2536 * filesystem to check should be specified by fsname, or will be determined
2539 static int sattr_get_defaults(const char *const fsname,
2540 unsigned int *scount,
2541 unsigned int *ssize,
2542 unsigned int *soffset)
2548 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2549 "stripecount", val, sizeof(val));
2552 *scount = atoi(val);
2556 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2557 "stripesize", val, sizeof(val));
2564 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2565 "stripeoffset", val, sizeof(val));
2568 *soffset = atoi(val);
2575 * Tries to gather the default stripe attributes for a given filesystem. If
2576 * the attributes can be determined, they are cached for easy retreival the
2577 * next time they are needed. Only a single filesystem's attributes are
2580 int sattr_cache_get_defaults(const char *const fsname,
2581 const char *const pathname, unsigned int *scount,
2582 unsigned int *ssize, unsigned int *soffset)
2585 char fsname[PATH_MAX + 1];
2586 unsigned int stripecount;
2587 unsigned int stripesize;
2588 unsigned int stripeoffset;
2594 char fsname_buf[PATH_MAX + 1];
2595 unsigned int tmp[3];
2597 if (fsname == NULL) {
2598 rc = llapi_search_fsname(pathname, fsname_buf);
2602 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2605 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2607 * Ensure all 3 sattrs (count, size, and offset) are
2608 * successfully retrieved and stored in tmp before writing to
2611 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2615 cache.stripecount = tmp[0];
2616 cache.stripesize = tmp[1];
2617 cache.stripeoffset = tmp[2];
2618 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2622 *scount = cache.stripecount;
2624 *ssize = cache.stripesize;
2626 *soffset = cache.stripeoffset;
2631 static char *layout2name(__u32 layout_pattern)
2633 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2635 else if (layout_pattern == LOV_PATTERN_MDT)
2637 else if (layout_pattern == LOV_PATTERN_RAID0)
2639 else if (layout_pattern ==
2640 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2641 return "raid0,overstriped";
2646 enum lov_dump_flags {
2647 LDF_IS_DIR = 0x0001,
2648 LDF_IS_RAW = 0x0002,
2649 LDF_INDENT = 0x0004,
2650 LDF_SKIP_OBJS = 0x0008,
2652 LDF_EXTENSION = 0x0020,
2655 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2656 struct lov_user_ost_data_v1 *objects,
2657 enum llapi_layout_verbose verbose,
2658 int depth, char *pool_name,
2659 enum lov_dump_flags flags)
2661 bool is_dir = flags & LDF_IS_DIR;
2662 bool is_raw = flags & LDF_IS_RAW;
2663 bool indent = flags & LDF_INDENT;
2664 bool yaml = flags & LDF_YAML;
2665 bool skip_objs = flags & LDF_SKIP_OBJS;
2666 bool extension = flags & LDF_EXTENSION;
2667 char *prefix = is_dir ? "" : "lmm_";
2668 char *separator = "";
2669 char *space = indent ? " " : "";
2672 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2673 lmm_oi_set_seq(&lum->lmm_oi, 0);
2674 if (!indent && (verbose & VERBOSE_DETAIL))
2675 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2678 if (!yaml && !indent && depth && path &&
2679 ((verbose != VERBOSE_OBJID) || !is_dir))
2680 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2682 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2683 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2684 space, prefix, lum->lmm_magic);
2685 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2687 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2688 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2690 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2693 if (verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) {
2698 if (verbose & ~VERBOSE_DFID)
2699 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2703 struct lu_fid dir_fid;
2705 rc = llapi_path2fid(path, &dir_fid);
2707 llapi_error(LLAPI_MSG_ERROR, rc,
2708 "Cannot determine directory fid.");
2710 seq = dir_fid.f_seq;
2711 oid = dir_fid.f_oid;
2712 ver = dir_fid.f_ver;
2715 * This needs a bit of hand-holding since old 1.x
2716 * lmm_oi have { oi.oi_id = mds_inum, oi.oi_seq = 0 }
2717 * and 2.x lmm_oi have { oi.oi_id = mds_oid,
2718 * oi.oi_seq = mds_seq } instead of a real FID.
2719 * Ideally the 2.x code would have stored this like a
2720 * FID with { oi_id = mds_seq, oi_seq = mds_oid } so
2721 * the ostid union lu_fid { f_seq = mds_seq,
2722 * f_oid = mds_oid } worked properly (especially since
2723 * IGIF FIDs use mds_inum as the FID SEQ), but
2724 * unfortunately that didn't happen.
2726 * Print it to look like an IGIF FID, even though the
2727 * fields are reversed on disk, so that it makes sense
2730 * Don't use ostid_id() and ostid_seq(), since they
2731 * assume the oi_fid fields are in the right order.
2732 * This is why there are separate lmm_oi_seq() and
2733 * lmm_oi_id() routines for this.
2735 * For newer layout types hopefully this will be a
2738 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2739 lmm_oi_id(&lum->lmm_oi) :
2740 lmm_oi_seq(&lum->lmm_oi);
2741 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2742 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2743 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2747 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2748 (unsigned long long)seq, oid, ver);
2750 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2751 (unsigned long long)seq, oid, ver);
2754 if (verbose & VERBOSE_STRIPE_COUNT) {
2755 if (verbose & ~VERBOSE_STRIPE_COUNT)
2756 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2759 if (!is_raw && lum->lmm_stripe_count == 0 &&
2760 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2761 unsigned int scount;
2763 rc = sattr_cache_get_defaults(NULL, path,
2767 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2770 llapi_error(LLAPI_MSG_ERROR, rc,
2771 "Cannot determine default stripe count.");
2773 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2775 (__s16)lum->lmm_stripe_count);
2778 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2780 (__s16)lum->lmm_stripe_count);
2782 if (!yaml && is_dir)
2788 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
2789 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
2790 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2791 if (verbose & ~VERBOSE_EXT_SIZE && extension)
2792 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
2794 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
2795 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2797 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2800 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2803 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2805 llapi_error(LLAPI_MSG_ERROR, rc,
2806 "Cannot determine default stripe size.");
2808 /* Extension size is in KiB */
2809 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2811 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
2812 (unsigned long long)lum->lmm_stripe_size);
2814 if (!yaml && is_dir)
2820 if ((verbose & VERBOSE_PATTERN)) {
2821 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2822 if (verbose & ~VERBOSE_PATTERN)
2823 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2825 if (lov_pattern_supported(lum->lmm_pattern))
2826 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2827 layout2name(lum->lmm_pattern));
2829 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2830 separator = (!yaml && is_dir) ? " " : "\n";
2833 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2834 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2835 if (verbose & ~VERBOSE_GENERATION)
2836 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2838 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2839 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2843 if (verbose & VERBOSE_STRIPE_OFFSET) {
2844 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2845 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2846 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2848 if (is_dir || skip_objs)
2849 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2850 lum->lmm_stripe_offset ==
2851 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2852 lum->lmm_stripe_offset);
2853 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
2854 llapi_printf(LLAPI_MSG_NORMAL, "0");
2856 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2857 objects[0].l_ost_idx);
2858 if (!yaml && is_dir)
2864 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2865 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2866 if (verbose & ~VERBOSE_POOL)
2867 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2869 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2870 if (!yaml && is_dir)
2876 if (strlen(separator) != 0)
2877 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2880 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2881 struct lov_user_ost_data_v1 *objects,
2882 char *path, int obdindex, int depth,
2883 enum llapi_layout_verbose verbose,
2884 enum lov_dump_flags flags)
2886 bool is_dir = flags & LDF_IS_DIR;
2887 bool indent = flags & LDF_INDENT;
2888 bool skip_objs = flags & LDF_SKIP_OBJS;
2889 bool yaml = flags & LDF_YAML;
2890 bool obdstripe = obdindex == OBD_NOT_FOUND;
2893 if (!obdstripe && !skip_objs) {
2894 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2895 if (obdindex == objects[i].l_ost_idx) {
2905 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2908 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
2909 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2910 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
2911 char *space = " - ";
2914 llapi_printf(LLAPI_MSG_NORMAL,
2915 "%6slmm_objects:\n", " ");
2917 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2919 llapi_printf(LLAPI_MSG_NORMAL,
2920 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2922 for (i = 0; i < lum->lmm_stripe_count; i++) {
2923 int idx = objects[i].l_ost_idx;
2924 long long oid = ostid_id(&objects[i].l_ost_oi);
2925 long long gr = ostid_seq(&objects[i].l_ost_oi);
2927 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2931 struct lu_fid fid = { 0 };
2933 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2934 llapi_printf(LLAPI_MSG_NORMAL,
2935 "%sl_ost_idx: %d\n", space, idx);
2936 llapi_printf(LLAPI_MSG_NORMAL,
2937 "%8sl_fid: "DFID_NOBRACE"\n",
2939 } else if (indent) {
2940 struct lu_fid fid = { 0 };
2942 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2943 llapi_printf(LLAPI_MSG_NORMAL,
2944 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
2945 space, i, idx, PFID(&fid));
2949 sprintf(fmt, "%s%s%s\n",
2950 "\t%6u\t%14llu\t%#13llx\t",
2951 (fid_seq_is_rsvd(gr) ||
2952 fid_seq_is_mdt0(gr)) ?
2953 "%14llu" : "%#14llx", "%s");
2954 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2956 obdindex == idx ? " *" : "");
2960 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2963 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2964 char *path, int obdindex, int depth,
2965 enum llapi_layout_verbose verbose,
2966 enum lov_dump_flags flags)
2968 struct lmv_user_mds_data *objects = lum->lum_objects;
2969 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2970 char *separator = "";
2971 bool yaml = flags & LDF_YAML;
2972 bool obdstripe = false;
2975 if (obdindex != OBD_NOT_FOUND) {
2976 if (lum->lum_stripe_count == 0) {
2977 if (obdindex == lum->lum_stripe_offset)
2980 for (i = 0; i < lum->lum_stripe_count; i++) {
2981 if (obdindex == objects[i].lum_mds) {
2982 llapi_printf(LLAPI_MSG_NORMAL,
2997 /* show all information default */
2999 if (lum->lum_magic == LMV_USER_MAGIC)
3000 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3001 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3003 verbose = VERBOSE_OBJID;
3006 if (depth && path && ((verbose != VERBOSE_OBJID)))
3007 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3009 if (verbose & VERBOSE_STRIPE_COUNT) {
3010 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3011 if (verbose & ~VERBOSE_STRIPE_COUNT)
3012 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3013 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3014 (int)lum->lum_stripe_count);
3015 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3021 if (verbose & VERBOSE_STRIPE_OFFSET) {
3022 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3023 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3024 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3025 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3026 (int)lum->lum_stripe_offset);
3027 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3033 if (verbose & VERBOSE_HASH_TYPE) {
3034 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3035 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3037 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3038 if (verbose & ~VERBOSE_HASH_TYPE)
3039 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3040 if (type < LMV_HASH_TYPE_MAX)
3041 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3042 mdt_hash_name[type]);
3044 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3046 if (flags & LMV_HASH_FLAG_MIGRATION)
3047 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3048 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3049 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3050 if (flags & LMV_HASH_FLAG_LOST_LMV)
3051 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3052 if (flags & LMV_HASH_FLAG_FIXED)
3053 llapi_printf(LLAPI_MSG_NORMAL, ",fixed");
3054 if (flags & ~LMV_HASH_FLAG_KNOWN)
3055 llapi_printf(LLAPI_MSG_NORMAL, ",unknown_%04x",
3056 flags & ~LMV_HASH_FLAG_KNOWN);
3058 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3064 if ((verbose & VERBOSE_INHERIT) && lum->lum_magic == LMV_USER_MAGIC) {
3065 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3066 if (verbose & ~VERBOSE_INHERIT)
3067 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit: ");
3068 if (lum->lum_max_inherit == LMV_INHERIT_UNLIMITED)
3069 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3070 else if (lum->lum_max_inherit == LMV_INHERIT_NONE)
3071 llapi_printf(LLAPI_MSG_NORMAL, "0");
3073 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3074 lum->lum_max_inherit);
3075 if (verbose & VERBOSE_INHERIT && !yaml)
3081 if ((verbose & VERBOSE_INHERIT_RR) &&
3082 lum->lum_magic == LMV_USER_MAGIC &&
3083 lum->lum_stripe_offset == LMV_OFFSET_DEFAULT) {
3084 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3085 if (verbose & ~VERBOSE_INHERIT_RR)
3086 llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit_rr: ");
3087 if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_UNLIMITED)
3088 llapi_printf(LLAPI_MSG_NORMAL, "-1");
3089 else if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_NONE)
3090 llapi_printf(LLAPI_MSG_NORMAL, "0");
3092 llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3093 lum->lum_max_inherit_rr);
3094 if (verbose & VERBOSE_INHERIT_RR && !yaml)
3102 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3103 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3104 if (lum->lum_stripe_count > 0)
3105 llapi_printf(LLAPI_MSG_NORMAL,
3106 "mdtidx\t\t FID[seq:oid:ver]\n");
3107 for (i = 0; i < lum->lum_stripe_count; i++) {
3108 int idx = objects[i].lum_mds;
3109 struct lu_fid *fid = &objects[i].lum_fid;
3111 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3112 llapi_printf(LLAPI_MSG_NORMAL,
3113 "%6u\t\t "DFID"\t\t%s\n",
3115 obdindex == idx ? " *" : "");
3119 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3120 pool_name[0] != '\0') {
3121 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3122 if (verbose & ~VERBOSE_POOL)
3123 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3125 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3129 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3130 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3133 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3134 enum lov_dump_flags flags)
3136 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3137 int depth = param->fp_max_depth;
3138 enum llapi_layout_verbose verbose = param->fp_verbose;
3139 bool yaml = flags & LDF_YAML;
3141 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3142 !(flags & LDF_IS_DIR)) && !yaml)
3143 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3145 if (verbose & VERBOSE_DETAIL) {
3146 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3147 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3148 " ", comp_v1->lcm_magic);
3149 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3150 " ", comp_v1->lcm_size);
3151 if (flags & LDF_IS_DIR)
3152 llapi_printf(LLAPI_MSG_NORMAL,
3153 "%2slcm_flags: %s\n", " ",
3154 comp_v1->lcm_mirror_count > 0 ?
3157 llapi_printf(LLAPI_MSG_NORMAL,
3158 "%2slcm_flags: %s\n", " ",
3159 llapi_layout_flags_string(comp_v1->lcm_flags));
3162 if (verbose & VERBOSE_GENERATION) {
3163 if (verbose & ~VERBOSE_GENERATION)
3164 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3166 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3169 if (verbose & VERBOSE_MIRROR_COUNT) {
3170 if (verbose & ~VERBOSE_MIRROR_COUNT)
3171 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3173 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3174 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3175 comp_v1->lcm_mirror_count + 1 : 1);
3178 if (verbose & VERBOSE_COMP_COUNT) {
3179 if (verbose & ~VERBOSE_COMP_COUNT)
3180 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3182 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3183 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3184 comp_v1->lcm_entry_count : 0);
3187 if (verbose & VERBOSE_DETAIL && !yaml)
3188 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3191 static void lcme_flags2str(__u32 comp_flags)
3197 llapi_printf(LLAPI_MSG_NORMAL, "0");
3200 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3201 if (comp_flags & comp_flags_table[i].cfn_flag) {
3203 llapi_printf(LLAPI_MSG_NORMAL, ",");
3204 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3205 comp_flags_table[i].cfn_name);
3206 comp_flags &= ~comp_flags_table[i].cfn_flag;
3212 llapi_printf(LLAPI_MSG_NORMAL, ",");
3213 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3217 static void lov_dump_comp_v1_entry(struct find_param *param,
3218 enum lov_dump_flags flags, int index)
3220 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3221 struct lov_comp_md_entry_v1 *entry;
3222 char *separator = "";
3223 enum llapi_layout_verbose verbose = param->fp_verbose;
3224 bool yaml = flags & LDF_YAML;
3226 entry = &comp_v1->lcm_entries[index];
3229 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3231 if (verbose & VERBOSE_COMP_ID) {
3232 if (verbose & VERBOSE_DETAIL && !yaml)
3233 llapi_printf(LLAPI_MSG_NORMAL,
3234 "%slcme_id: ", " - ");
3235 else if (verbose & ~VERBOSE_COMP_ID)
3236 llapi_printf(LLAPI_MSG_NORMAL,
3237 "%4slcme_id: ", " ");
3238 if (entry->lcme_id != LCME_ID_INVAL)
3239 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3241 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3245 if (verbose & VERBOSE_MIRROR_ID) {
3246 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3247 if (verbose & ~VERBOSE_MIRROR_ID)
3248 llapi_printf(LLAPI_MSG_NORMAL,
3249 "%4slcme_mirror_id: ", " ");
3250 if (entry->lcme_id != LCME_ID_INVAL)
3251 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3252 mirror_id_of(entry->lcme_id));
3254 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3258 if (verbose & VERBOSE_COMP_FLAGS) {
3259 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3260 if (verbose & ~VERBOSE_COMP_FLAGS)
3261 llapi_printf(LLAPI_MSG_NORMAL,
3262 "%4slcme_flags: ", " ");
3263 lcme_flags2str(entry->lcme_flags);
3266 /* print snapshot timestamp if its a nosync comp */
3267 if ((verbose & VERBOSE_COMP_FLAGS) &&
3268 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3269 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3270 if (verbose & ~VERBOSE_COMP_FLAGS)
3271 llapi_printf(LLAPI_MSG_NORMAL,
3272 "%4slcme_timestamp: ", " ");
3274 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3275 (unsigned long long)entry->lcme_timestamp);
3277 time_t stamp = entry->lcme_timestamp;
3278 char *date_str = asctime(localtime(&stamp));
3280 date_str[strlen(date_str) - 1] = '\0';
3281 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3287 if (verbose & VERBOSE_COMP_START) {
3288 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3289 if (verbose & ~VERBOSE_COMP_START)
3290 llapi_printf(LLAPI_MSG_NORMAL,
3291 "%4slcme_extent.e_start: ", " ");
3292 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3293 (unsigned long long)entry->lcme_extent.e_start);
3297 if (verbose & VERBOSE_COMP_END) {
3298 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3299 if (verbose & ~VERBOSE_COMP_END)
3300 llapi_printf(LLAPI_MSG_NORMAL,
3301 "%4slcme_extent.e_end: ", " ");
3302 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3303 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3305 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3306 (unsigned long long)entry->lcme_extent.e_end);
3311 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3312 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3313 } else if (verbose & VERBOSE_DETAIL) {
3314 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3315 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3316 " ", entry->lcme_offset);
3317 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3318 " ", entry->lcme_size);
3319 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3321 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3326 * Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3327 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3329 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3330 * The table below gives the answers for the specified parameters (value and
3331 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3332 * --------------------------------------
3333 * 1 | file > limit; sign > 0 | -1 / -1 |
3334 * 2 | file = limit; sign > 0 | -1 / -1 |
3335 * 3 | file < limit; sign > 0 | ? / 1 |
3336 * 4 | file > limit; sign = 0 | -1 / -1 |
3337 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3338 * 6 | file < limit; sign = 0 | ? / -1 |
3339 * 7 | file > limit; sign < 0 | 1 / 1 |
3340 * 8 | file = limit; sign < 0 | ? / -1 |
3341 * 9 | file < limit; sign < 0 | ? / -1 |
3342 * --------------------------------------
3343 * Note: 5th actually means that the value is within the interval
3344 * (limit - margin, limit].
3346 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3347 int sign, int negopt, unsigned long long margin,
3353 /* Drop the fraction of margin (of days or size). */
3354 if (file + margin <= limit)
3356 } else if (sign == 0) {
3357 if (file <= limit && file + margin > limit)
3359 else if (file + margin <= limit)
3361 } else if (sign < 0) {
3368 return negopt ? ~ret + 1 : ret;
3371 static inline struct lov_user_md *
3372 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3374 return (struct lov_user_md *)((char *)comp_v1 +
3375 comp_v1->lcm_entries[ent_idx].lcme_offset);
3378 static inline struct lov_user_ost_data_v1 *
3379 lov_v1v3_objects(struct lov_user_md *v1)
3381 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3382 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3384 return v1->lmm_objects;
3388 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3390 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3391 snprintf(pool_name, LOV_MAXPOOLNAME + 1, "%s",
3392 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3394 pool_name[0] = '\0';
3398 print_last_init_comp(struct find_param *param)
3400 /* print all component info */
3401 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3404 /* print specific component info */
3405 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3406 param->fp_check_comp_start || param->fp_check_comp_end ||
3407 param->fp_check_mirror_id || param->fp_check_mirror_index)
3413 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3417 if (param->fp_comp_end == LUSTRE_EOF) {
3418 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3419 match = end == LUSTRE_EOF ? 1 : -1;
3420 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3421 match = end == LUSTRE_EOF ? -1 : 1;
3422 else /* at least EOF */
3424 if (param->fp_exclude_comp_end)
3427 unsigned long long margin;
3429 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3430 match = find_value_cmp(end, param->fp_comp_end,
3431 param->fp_comp_end_sign,
3432 param->fp_exclude_comp_end, margin, 0);
3439 * An example of "getstripe -v" for a two components PFL file:
3442 * lcm_magic: 0x0BD60BD0
3446 * lcm_entry_count: 2
3450 * lcme_extent.e_start: 0
3451 * lcme_extent.e_end: 1048576
3455 * lmm_magic: 0x0BD10BD0
3456 * lmm_seq: 0x200000401
3457 * lmm_object_id: 0x1
3458 * lmm_fid: [0x200000401:0x1:0x0]
3459 * lmm_stripe_count: 1
3460 * lmm_stripe_size: 1048576
3461 * lmm_pattern: raid0
3463 * lmm_stripe_offset: 0
3465 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3469 * lcme_extent.e_start: 1048576
3470 * lcme_extent.e_end: EOF
3474 * lmm_magic: 0x0BD10BD0
3475 * lmm_seq: 0x200000401
3476 * lmm_object_id: 0x1
3477 * lmm_fid: [0x200000401:0x1:0x0]
3478 * lmm_stripe_count: 2
3479 * lmm_stripe_size: 1048576
3480 * lmm_pattern: raid0
3482 * lmm_stripe_offset: 1
3484 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3485 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3487 static void lov_dump_comp_v1(struct find_param *param, char *path,
3488 enum lov_dump_flags flags)
3490 struct lov_comp_md_entry_v1 *entry;
3491 struct lov_user_ost_data_v1 *objects;
3492 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3493 struct lov_user_md_v1 *v1;
3494 char pool_name[LOV_MAXPOOLNAME + 1];
3495 int obdindex = param->fp_obd_index;
3496 int i, j, match, ext;
3497 bool obdstripe = false;
3498 __u16 mirror_index = 0;
3499 __u16 mirror_id = 0;
3501 if (obdindex != OBD_NOT_FOUND) {
3502 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3503 i < comp_v1->lcm_entry_count; i++) {
3504 if (!(comp_v1->lcm_entries[i].lcme_flags &
3508 v1 = lov_comp_entry(comp_v1, i);
3509 objects = lov_v1v3_objects(v1);
3511 for (j = 0; j < v1->lmm_stripe_count; j++) {
3512 if (obdindex == objects[j].l_ost_idx) {
3525 lov_dump_comp_v1_header(param, path, flags);
3527 flags |= LDF_INDENT;
3529 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3530 entry = &comp_v1->lcm_entries[i];
3532 if (param->fp_check_comp_flags) {
3533 if (((param->fp_comp_flags & entry->lcme_flags) !=
3534 param->fp_comp_flags) ||
3535 (param->fp_comp_neg_flags & entry->lcme_flags))
3539 if (param->fp_check_comp_id &&
3540 param->fp_comp_id != entry->lcme_id)
3543 if (param->fp_check_comp_start) {
3544 match = find_value_cmp(entry->lcme_extent.e_start,
3545 param->fp_comp_start,
3546 param->fp_comp_start_sign,
3548 param->fp_comp_start_units, 0);
3553 if (param->fp_check_comp_end) {
3554 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3560 if (param->fp_check_mirror_index) {
3561 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3563 mirror_id = mirror_id_of(entry->lcme_id);
3566 match = find_value_cmp(mirror_index,
3567 param->fp_mirror_index,
3568 param->fp_mirror_index_sign,
3569 param->fp_exclude_mirror_index,
3573 } else if (param->fp_check_mirror_id) {
3574 if (mirror_id != mirror_id_of(entry->lcme_id))
3575 mirror_id = mirror_id_of(entry->lcme_id);
3577 match = find_value_cmp(mirror_id,
3578 param->fp_mirror_id,
3579 param->fp_mirror_id_sign,
3580 param->fp_exclude_mirror_id,
3586 if (print_last_init_comp(param)) {
3588 * if part of stripe info is needed, we'd print only
3589 * the last instantiated component info.
3591 if (entry->lcme_flags & LCME_FL_INIT)
3594 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3595 if (entry->lcme_flags & LCME_FL_EXTENSION)
3596 /* moved back below */
3604 if (entry->lcme_flags & LCME_FL_INIT) {
3605 if (obdindex != OBD_NOT_FOUND) {
3606 flags |= LDF_SKIP_OBJS;
3607 v1 = lov_comp_entry(comp_v1, i);
3608 objects = lov_v1v3_objects(v1);
3610 for (j = 0; j < v1->lmm_stripe_count; j++) {
3611 if (obdindex == objects[j].l_ost_idx) {
3612 flags &= ~LDF_SKIP_OBJS;
3617 flags &= ~LDF_SKIP_OBJS;
3620 flags |= LDF_SKIP_OBJS;
3623 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3625 lov_dump_comp_v1_entry(param, flags, i);
3627 v1 = lov_comp_entry(comp_v1, i);
3628 objects = lov_v1v3_objects(v1);
3629 lov_v1v3_pool_name(v1, pool_name);
3631 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3632 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3633 param->fp_max_depth, param->fp_verbose,
3636 if (print_last_init_comp(param)) {
3638 * directory layout contains only layout template, print the
3642 i = comp_v1->lcm_entry_count - 1;
3645 flags &= ~LDF_SKIP_OBJS;
3647 lov_dump_comp_v1_entry(param, flags, i);
3649 v1 = lov_comp_entry(comp_v1, i);
3650 objects = lov_v1v3_objects(v1);
3651 lov_v1v3_pool_name(v1, pool_name);
3653 entry = &comp_v1->lcm_entries[i];
3654 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3655 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3656 param->fp_max_depth, param->fp_verbose,
3661 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3662 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3665 static inline bool has_any_comp_options(struct find_param *param)
3667 enum llapi_layout_verbose verbose = param->fp_verbose;
3669 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3670 param->fp_check_comp_start || param->fp_check_comp_end ||
3671 param->fp_check_comp_flags)
3674 /* show full layout information, not component specific */
3675 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3678 return verbose & VERBOSE_COMP_OPTS;
3681 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3684 struct lov_user_md *lum = &orig->lmd_lmm;
3685 struct lov_user_mds_data *new;
3686 struct lov_comp_md_v1 *comp_v1;
3687 struct lov_comp_md_entry_v1 *ent;
3688 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3689 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3692 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3694 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3698 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3699 + sizeof(new->lmd_lmmsize));
3701 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3702 comp_v1->lcm_magic = lum->lmm_magic;
3703 comp_v1->lcm_size = lum_off + lum_size;
3704 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3705 comp_v1->lcm_flags = 0;
3706 comp_v1->lcm_entry_count = 1;
3708 ent = &comp_v1->lcm_entries[0];
3710 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3711 ent->lcme_extent.e_start = 0;
3712 ent->lcme_extent.e_end = LUSTRE_EOF;
3713 ent->lcme_offset = lum_off;
3714 ent->lcme_size = lum_size;
3716 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3721 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3722 enum lov_dump_flags flags)
3724 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3726 if (has_any_comp_options(param)) {
3727 struct lov_user_mds_data *new_lmd, *orig_lmd;
3729 orig_lmd = param->fp_lmd;
3730 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3731 if (new_lmd != NULL) {
3732 param->fp_lmd = new_lmd;
3733 lov_dump_comp_v1(param, path, flags);
3734 param->fp_lmd = orig_lmd;
3740 if (magic == LOV_USER_MAGIC_V1) {
3741 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3742 param->fp_lmd->lmd_lmm.lmm_objects,
3743 path, param->fp_obd_index,
3744 param->fp_max_depth, param->fp_verbose,
3747 char pool_name[LOV_MAXPOOLNAME + 1];
3748 struct lov_user_ost_data_v1 *objects;
3749 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3751 snprintf(pool_name, sizeof(pool_name), "%s",
3752 lmmv3->lmm_pool_name);
3753 objects = lmmv3->lmm_objects;
3754 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3755 objects, path, param->fp_obd_index,
3756 param->fp_max_depth, param->fp_verbose,
3761 static uint32_t check_foreign_type(uint32_t foreign_type)
3765 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3766 if (lu_foreign_types[i].lft_name == NULL)
3768 if (foreign_type == lu_foreign_types[i].lft_type)
3772 return LU_FOREIGN_TYPE_UNKNOWN;
3775 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3776 enum lov_dump_flags flags)
3778 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3779 bool yaml = flags & LDF_YAML;
3781 if (!yaml && param->fp_depth && path)
3782 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3784 if (param->fp_verbose & VERBOSE_DETAIL) {
3785 uint32_t type = check_foreign_type(lfm->lfm_type);
3787 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3789 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3791 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3793 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3794 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3795 lu_foreign_types[type].lft_name);
3797 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3799 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3802 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3803 lfm->lfm_length, lfm->lfm_value);
3804 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3807 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3808 enum lov_dump_flags flags)
3810 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3811 bool yaml = flags & LDF_YAML;
3813 if (!yaml && param->fp_depth && path)
3814 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3816 if (param->fp_verbose & VERBOSE_DETAIL) {
3817 uint32_t type = check_foreign_type(lfm->lfm_type);
3819 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3821 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3823 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3825 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3826 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3827 lu_foreign_types[type].lft_name);
3829 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3831 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3834 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3835 lfm->lfm_length, lfm->lfm_value);
3836 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3839 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3840 enum lov_dump_flags flags)
3844 if (param->fp_get_lmv || param->fp_get_default_lmv)
3845 magic = (__u32)param->fp_lmv_md->lum_magic;
3847 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3850 flags |= LDF_IS_RAW;
3855 case LOV_USER_MAGIC_V1:
3856 case LOV_USER_MAGIC_V3:
3857 case LOV_USER_MAGIC_SPECIFIC:
3858 lov_dump_plain_user_lmm(param, path, flags);
3860 case LOV_USER_MAGIC_FOREIGN:
3861 lov_dump_foreign_lmm(param, path, flags);
3864 case LMV_USER_MAGIC: {
3865 char pool_name[LOV_MAXPOOLNAME + 1];
3866 struct lmv_user_md *lum;
3868 lum = (struct lmv_user_md *)param->fp_lmv_md;
3869 snprintf(pool_name, sizeof(pool_name), "%s",
3870 lum->lum_pool_name);
3871 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3872 param->fp_max_depth, param->fp_verbose,
3876 case LOV_USER_MAGIC_COMP_V1:
3877 lov_dump_comp_v1(param, path, flags);
3879 case LMV_MAGIC_FOREIGN:
3880 lmv_dump_foreign_lmm(param, path, flags);
3883 llapi_printf(LLAPI_MSG_NORMAL,
3884 "unknown lmm_magic: %#x (expecting one of %#x %#x %#x %#x)\n",
3885 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3886 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3887 LMV_USER_MAGIC, LMV_MAGIC_V1);
3892 static int llapi_file_get_stripe1(const char *path, struct lov_user_md *lum)
3898 fname = strrchr(path, '/');
3900 /* It should be a file (or other non-directory) */
3901 if (fname == NULL) {
3902 dname = (char *)malloc(2);
3906 fname = (char *)path;
3908 dname = (char *)malloc(fname - path + 1);
3911 strncpy(dname, path, fname - path);
3912 dname[fname - path] = '\0';
3916 fd = open(dname, O_RDONLY | O_NONBLOCK);
3922 strcpy((char *)lum, fname);
3923 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3926 if (close(fd) == -1 && rc == 0)
3934 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3936 char *canon_path = NULL;
3939 rc = llapi_file_get_stripe1(path, lum);
3940 if (!(rc == -ENOTTY || rc == -ENODATA))
3943 /* Handle failure due to symlinks by dereferencing path manually. */
3944 canon_path = canonicalize_file_name(path);
3945 if (canon_path == NULL)
3946 goto out; /* Keep original rc. */
3948 rc2 = llapi_file_get_stripe1(canon_path, lum);
3950 goto out; /* Keep original rc. */
3959 int llapi_file_lookup(int dirfd, const char *name)
3961 struct obd_ioctl_data data = { 0 };
3966 if (dirfd < 0 || name == NULL)
3969 data.ioc_version = OBD_IOCTL_VERSION;
3970 data.ioc_len = sizeof(data);
3971 data.ioc_inlbuf1 = (char *)name;
3972 data.ioc_inllen1 = strlen(name) + 1;
3974 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3976 llapi_error(LLAPI_MSG_ERROR, rc,
3977 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3982 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
3989 * Check if the file time matches all the given criteria (e.g. --atime +/-N).
3990 * Return -1 or 1 if file timestamp does not or does match the given criteria
3991 * correspondingly. Return 0 if the MDS time is being checked and there are
3992 * attributes on OSTs and it is not yet clear if the timespamp matches.
3994 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
3995 * updated timestamps.
3997 static int find_time_check(struct find_param *param, int mds)
3999 struct lov_user_mds_data *lmd = param->fp_lmd;
4003 /* Check if file is accepted. */
4004 if (param->fp_atime) {
4005 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4006 param->fp_atime, param->fp_asign,
4007 param->fp_exclude_atime,
4008 param->fp_time_margin, mds);
4014 if (param->fp_mtime) {
4015 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4016 param->fp_mtime, param->fp_msign,
4017 param->fp_exclude_mtime,
4018 param->fp_time_margin, mds);
4023 * If the previous check matches, but this one is not yet clear,
4024 * we should return 0 to do an RPC on OSTs.
4030 if (param->fp_ctime) {
4031 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4032 param->fp_ctime, param->fp_csign,
4033 param->fp_exclude_ctime,
4034 param->fp_time_margin, mds);
4039 * If the previous check matches, but this one is not yet clear,
4040 * we should return 0 to do an RPC on OSTs.
4049 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4051 struct lov_user_mds_data *lmd = param->fp_lmd;
4056 for (i = 0; i < 2; i++) {
4057 /* Check if file is accepted. */
4058 if (param->fp_newery[NEWERXY_ATIME][i]) {
4059 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4060 param->fp_newery[NEWERXY_ATIME][i],
4067 if (param->fp_newery[NEWERXY_MTIME][i]) {
4068 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4069 param->fp_newery[NEWERXY_MTIME][i],
4075 * If the previous check matches, but this one is not
4076 * yet clear, we should return 0 to do an RPC on OSTs.
4082 if (param->fp_newery[NEWERXY_CTIME][i]) {
4083 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4084 param->fp_newery[NEWERXY_CTIME][i],
4090 * If the previous check matches, but this one is not
4091 * yet clear, we should return 0 to do an RPC on OSTs.
4098 * File birth time (btime) can get from MDT directly.
4099 * if @from_mdt is true, it means the input file attributs are
4100 * obtained directly from MDT.
4101 * Thus, if @from_mdt is false, we should skip the following
4107 if (param->fp_newery[NEWERXY_BTIME][i]) {
4108 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4111 rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4112 param->fp_newery[NEWERXY_BTIME][i],
4123 * Check whether the stripes matches the indexes user provided
4127 static int check_obd_match(struct find_param *param)
4129 struct lov_user_ost_data_v1 *objects;
4130 struct lov_comp_md_v1 *comp_v1 = NULL;
4131 struct lov_user_mds_data *lmd = param->fp_lmd;
4132 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4133 int i, j, k, count = 1;
4135 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4138 if (!S_ISREG(lmd->lmd_stx.stx_mode))
4141 /* exclude foreign */
4142 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4143 return param->fp_exclude_obd;
4146 * Only those files should be accepted, which have a
4147 * stripe on the specified OST.
4149 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4150 comp_v1 = (struct lov_comp_md_v1 *)v1;
4151 count = comp_v1->lcm_entry_count;
4154 for (i = 0; i < count; i++) {
4156 v1 = lov_comp_entry(comp_v1, i);
4158 objects = lov_v1v3_objects(v1);
4160 for (j = 0; j < v1->lmm_stripe_count; j++) {
4161 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4164 for (k = 0; k < param->fp_num_obds; k++) {
4165 if (param->fp_obd_indexes[k] ==
4166 objects[j].l_ost_idx)
4167 return !param->fp_exclude_obd;
4172 return param->fp_exclude_obd;
4175 static int check_mdt_match(struct find_param *param)
4179 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4182 /* FIXME: For striped dir, we should get stripe information and check */
4183 for (i = 0; i < param->fp_num_mdts; i++) {
4184 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4185 return !param->fp_exclude_mdt;
4188 if (param->fp_exclude_mdt)
4195 * Check whether the obd is active or not, if it is
4196 * not active, just print the object affected by this
4199 static void print_failed_tgt(struct find_param *param, char *path, int type)
4201 struct obd_statfs stat_buf;
4202 struct obd_uuid uuid_buf;
4203 int tgt_nr, i, *indexes;
4206 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV) {
4207 llapi_error(LLAPI_MSG_NORMAL, ret, "%s: wrong statfs type(%d)",
4212 tgt_nr = (type == LL_STATFS_LOV) ? param->fp_obd_index :
4213 param->fp_mdt_index;
4214 indexes = (type == LL_STATFS_LOV) ? param->fp_obd_indexes :
4215 param->fp_mdt_indexes;
4217 for (i = 0; i < tgt_nr; i++) {
4218 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4219 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4221 ret = llapi_obd_statfs(path, type, indexes[i], &stat_buf,
4224 llapi_error(LLAPI_MSG_NORMAL, ret,
4225 "%s: obd_uuid: %s failed",
4226 __func__, param->fp_obd_uuid->uuid);
4230 static int find_check_stripe_size(struct find_param *param)
4232 struct lov_comp_md_v1 *comp_v1 = NULL;
4233 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4234 __u32 stripe_size = 0;
4235 int ret, i, count = 1;
4237 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4238 return param->fp_exclude_stripe_size ? 1 : -1;
4240 ret = param->fp_exclude_stripe_size ? 1 : -1;
4241 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4242 comp_v1 = (struct lov_comp_md_v1 *)v1;
4243 count = comp_v1->lcm_entry_count;
4246 for (i = 0; i < count; i++) {
4247 struct lov_comp_md_entry_v1 *ent;
4250 v1 = lov_comp_entry(comp_v1, i);
4252 ent = &comp_v1->lcm_entries[i];
4253 if (ent->lcme_flags & LCME_FL_EXTENSION)
4255 if (!(ent->lcme_flags & LCME_FL_INIT))
4258 stripe_size = v1->lmm_stripe_size;
4261 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4262 param->fp_stripe_size_sign,
4263 param->fp_exclude_stripe_size,
4264 param->fp_stripe_size_units, 0);
4269 static int find_check_ext_size(struct find_param *param)
4271 struct lov_comp_md_v1 *comp_v1;
4272 struct lov_user_md_v1 *v1;
4275 ret = param->fp_exclude_ext_size ? 1 : -1;
4276 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4277 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4280 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4281 struct lov_comp_md_entry_v1 *ent;
4283 v1 = lov_comp_entry(comp_v1, i);
4285 ent = &comp_v1->lcm_entries[i];
4286 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4289 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4290 param->fp_ext_size_sign,
4291 param->fp_exclude_ext_size,
4292 param->fp_ext_size_units, 0);
4293 /* If any ext_size matches */
4301 static __u32 find_get_stripe_count(struct find_param *param)
4303 struct lov_comp_md_v1 *comp_v1 = NULL;
4304 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4306 __u32 stripe_count = 0;
4308 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4311 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4312 comp_v1 = (struct lov_comp_md_v1 *)v1;
4313 count = comp_v1->lcm_entry_count;
4316 for (i = 0; i < count; i++) {
4318 struct lov_comp_md_entry_v1 *ent;
4320 v1 = lov_comp_entry(comp_v1, i);
4322 ent = &comp_v1->lcm_entries[i];
4323 if (!(ent->lcme_flags & LCME_FL_INIT))
4326 if (ent->lcme_flags & LCME_FL_EXTENSION)
4329 stripe_count = v1->lmm_stripe_count;
4332 return stripe_count;
4335 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4337 static int find_check_layout(struct find_param *param)
4339 struct lov_comp_md_v1 *comp_v1 = NULL;
4340 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4342 bool found = false, valid = false;
4344 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4345 comp_v1 = (struct lov_comp_md_v1 *)v1;
4346 count = comp_v1->lcm_entry_count;
4349 for (i = 0; i < count; i++) {
4351 v1 = lov_comp_entry(comp_v1, i);
4353 /* foreign file have a special magic but no pattern field */
4354 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4357 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4361 if (v1->lmm_pattern & param->fp_layout) {
4370 if ((found && !param->fp_exclude_layout) ||
4371 (!found && param->fp_exclude_layout))
4378 * if no type specified, check/exclude all foreign
4379 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4381 static int find_check_foreign(struct find_param *param)
4383 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4384 struct lov_foreign_md *lfm;
4386 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4387 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4388 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4389 return param->fp_exclude_foreign ? 1 : -1;
4393 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4394 lfm->lfm_type == param->fp_foreign_type)
4395 return param->fp_exclude_foreign ? -1 : 1;
4396 return param->fp_exclude_foreign ? 1 : -1;
4399 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4400 struct lmv_foreign_md *lfm;
4402 lfm = (void *)param->fp_lmv_md;
4403 if (lmv_is_foreign(lfm->lfm_magic)) {
4404 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4405 return param->fp_exclude_foreign ? 1 : -1;
4409 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4410 lfm->lfm_type == param->fp_foreign_type)
4411 return param->fp_exclude_foreign ? -1 : 1;
4412 return param->fp_exclude_foreign ? 1 : -1;
4417 static int find_check_pool(struct find_param *param)
4419 struct lov_comp_md_v1 *comp_v1 = NULL;
4420 struct lov_user_md_v3 *v3 = (void *)¶m->fp_lmd->lmd_lmm;
4424 if (v3->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4425 comp_v1 = (struct lov_comp_md_v1 *)v3;
4426 count = comp_v1->lcm_entry_count;
4427 /* empty requested pool is taken as no pool search */
4428 if (count == 0 && param->fp_poolname[0] == '\0') {
4434 for (i = 0; i < count; i++) {
4435 if (comp_v1 != NULL) {
4436 if (!(comp_v1->lcm_entries[i].lcme_flags &
4440 v3 = (void *)lov_comp_entry(comp_v1, i);
4443 if (v3->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4446 if (((v3->lmm_magic == LOV_USER_MAGIC_V1) &&
4447 (param->fp_poolname[0] == '\0')) ||
4448 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4449 (strncmp(v3->lmm_pool_name,
4450 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4451 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4452 (strcmp(param->fp_poolname, "*") == 0))) {
4459 if ((found && !param->fp_exclude_pool) ||
4460 (!found && param->fp_exclude_pool))
4466 static int find_check_comp_options(struct find_param *param)
4468 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4469 struct lov_user_mds_data *lmd = param->fp_lmd;
4470 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4471 struct lov_comp_md_entry_v1 *entry;
4474 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4477 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4478 comp_v1 = (struct lov_comp_md_v1 *)v1;
4480 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4481 if (forged_v1 == NULL)
4483 comp_v1 = forged_v1;
4484 comp_v1->lcm_entry_count = 1;
4485 entry = &comp_v1->lcm_entries[0];
4486 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4488 entry->lcme_extent.e_start = 0;
4489 entry->lcme_extent.e_end = LUSTRE_EOF;
4492 /* invalid case, don't match for any kind of search. */
4493 if (comp_v1->lcm_entry_count == 0) {
4498 if (param->fp_check_comp_count) {
4499 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4500 param->fp_comp_count,
4501 param->fp_comp_count_sign,
4502 param->fp_exclude_comp_count, 1, 0);
4508 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4509 entry = &comp_v1->lcm_entries[i];
4511 if (param->fp_check_comp_flags) {
4513 if (((param->fp_comp_flags & entry->lcme_flags) !=
4514 param->fp_comp_flags) ||
4515 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4521 if (param->fp_check_comp_start) {
4522 ret = find_value_cmp(entry->lcme_extent.e_start,
4523 param->fp_comp_start,
4524 param->fp_comp_start_sign,
4525 param->fp_exclude_comp_start,
4526 param->fp_comp_start_units, 0);
4531 if (param->fp_check_comp_end) {
4532 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4538 /* the component matches all criteria */
4547 static int find_check_mirror_options(struct find_param *param)
4549 struct lov_comp_md_v1 *comp_v1;
4550 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4553 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4556 comp_v1 = (struct lov_comp_md_v1 *)v1;
4558 if (param->fp_check_mirror_count) {
4559 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4560 param->fp_mirror_count,
4561 param->fp_mirror_count_sign,
4562 param->fp_exclude_mirror_count, 1, 0);
4567 if (param->fp_check_mirror_state) {
4569 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4571 if ((param->fp_mirror_state != 0 &&
4572 file_state != param->fp_mirror_state) ||
4573 file_state == param->fp_mirror_neg_state)
4580 static bool find_check_lmm_info(struct find_param *param)
4582 return param->fp_check_pool || param->fp_check_stripe_count ||
4583 param->fp_check_stripe_size || param->fp_check_layout ||
4584 param->fp_check_comp_count || param->fp_check_comp_end ||
4585 param->fp_check_comp_start || param->fp_check_comp_flags ||
4586 param->fp_check_mirror_count || param->fp_check_foreign ||
4587 param->fp_check_mirror_state || param->fp_check_ext_size ||
4588 param->fp_check_projid;
4592 * Interpret backslash escape sequences and write output into buffer.
4593 * Anything written to the buffer will be null terminated.
4595 * @param[in] seq String being parsed for escape sequence. The leading
4596 * '\' character is not included in this string (only the
4597 * characters after it)
4598 * @param[out] buffer Location where interpreted escape sequence is written
4599 * @param[in] size Size of the available buffer. (Needs to be large enough
4600 * to handle escape sequence output plus null terminator.)
4601 * @param[out] wrote Number of bytes written to the buffer.
4602 * @return Number of characters from input string processed
4603 * as part of the escape sequence (0 for an unrecognized
4606 int printf_format_escape(char *seq, char *buffer, size_t size, int *wrote)
4609 /* For now, only handle single char escape sequences: \n, \t, \\ */
4632 * Interpret formats for timestamps (%a, %A@, etc)
4634 * @param[in] seq String being parsed for timestamp format. The leading
4635 * '%' character is not included in this string
4636 * @param[out] buffer Location where timestamp info is written
4637 * @param[in] size Size of the available buffer.
4638 * @param[out] wrote Number of bytes written to the buffer.
4639 * @return Number of characters from input string processed
4640 * as part of the format (0 for an unknown format)
4643 int printf_format_timestamp(char *seq, char *buffer, size_t size, int *wrote,
4644 struct find_param *param)
4646 struct statx_timestamp ts = { 0, 0 };
4650 char *fmt = "%c"; /* Print in ctime format by default */
4655 ts = param->fp_lmd->lmd_stx.stx_atime;
4659 if (*(seq + 1) == '@') {
4660 ts = param->fp_lmd->lmd_stx.stx_atime;
4666 ts = param->fp_lmd->lmd_stx.stx_ctime;
4670 if (*(seq + 1) == '@') {
4671 ts = param->fp_lmd->lmd_stx.stx_ctime;
4677 ts = param->fp_lmd->lmd_stx.stx_mtime;
4681 if (*(seq + 1) == '@') {
4682 ts = param->fp_lmd->lmd_stx.stx_mtime;
4688 ts = param->fp_lmd->lmd_stx.stx_btime;
4692 if (*(seq + 1) == '@') {
4693 ts = param->fp_lmd->lmd_stx.stx_btime;
4703 /* Found valid format, print to buffer */
4706 *wrote = strftime(buffer, size, fmt, tm);
4713 * Print all ost indices associated with a file layout using a commma separated
4714 * list. For a file with mutliple components, the list of indices for each
4715 * component will be enclosed in brackets.
4717 * @param[out] buffer Location where OST indices are written
4718 * @param[in] size Size of the available buffer.
4719 * @pararm[in] layout Pointer to layout structure for the file
4720 * @return Number of bytes written to output buffer
4722 static int printf_format_ost_indices(char *buffer, size_t size,
4723 struct llapi_layout *layout)
4725 uint64_t count, idx, i;
4726 int err, bytes, wrote = 0;
4728 /* Make sure to start at the first component */
4729 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
4731 llapi_error(LLAPI_MSG_ERROR, err,
4732 "error: layout component iteration failed\n");
4736 err = llapi_layout_stripe_count_get(layout, &count);
4738 llapi_error(LLAPI_MSG_ERROR, err,
4739 "error: cannot get stripe_count\n");
4743 bytes = snprintf(buffer, (size - wrote), "%s", "[");
4748 for (i = 0; i < count; i++) {
4749 err = llapi_layout_ost_index_get(layout, i, &idx);
4751 llapi_error(LLAPI_MSG_ERROR, err,
4752 "error: cannot get OST index\n");
4753 bytes = snprintf(buffer, (size - wrote),
4756 bytes = snprintf(buffer, (size - wrote),
4764 /* Overwrite last comma with closing bracket */
4765 *(buffer - 1) = ']';
4767 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
4768 if (err == 0) /* next component is found */
4771 llapi_error(LLAPI_MSG_ERROR, err,
4772 "error: layout component iteration failed\n");
4773 /* At this point, either got error or reached last component */
4784 * Parse Lustre-specific format sequences of the form %L{x}.
4786 * @param[in] seq String being parsed for format sequence. The leading
4787 * '%' character is not included in this string
4788 * @param[out] buffer Location where interpreted format info is written
4789 * @param[in] size Size of the available buffer.
4790 * @param[out] wrote Number of bytes written to the buffer.
4791 * @param[in] param The find_param structure associated with the file/dir
4792 * @param[in] path Pathname of the current file/dir being handled
4793 * @param[in] projid Project ID associated with the current file/dir
4794 * @param[in] d File descriptor for the directory (or -1 for a
4795 * non-directory file)
4796 * @return Number of characters from input string processed
4797 * as part of the format (0 for an unknown format)
4799 int printf_format_lustre(char *seq, char *buffer, size_t size, int *wrote,
4800 struct find_param *param, char *path, int projid,
4803 struct lmv_user_md *lum;
4804 struct lmv_user_mds_data *objects;
4805 struct llapi_layout *layout = NULL;
4807 unsigned int hash_type;
4808 uint64_t str_cnt, str_size, idx;
4809 char pool_name[LOV_MAXPOOLNAME + 1] = { '\0' };
4811 int rc = 2; /* all current valid sequences are 2 chars */
4814 /* Sanity check. Formats always look like %L{X} */
4815 if (*seq++ != 'L') {
4821 * Some formats like %LF or %LP are handled the same for both files
4822 * and dirs, so handle all of those here.
4826 err = llapi_path2fid(path, &fid);
4828 llapi_error(LLAPI_MSG_ERROR, err,
4829 "error: cannot get fid\n");
4832 *wrote = snprintf(buffer, size, DFID_NOBRACE, PFID(&fid));
4835 *wrote = snprintf(buffer, size, "%d", projid);
4839 /* Other formats for files/dirs need to be handled differently */
4840 if (d == -1) { /* file */
4841 //layout = llapi_layout_get_by_xattr(¶m->fp_lmd->lmd_lmm,
4842 // param->fp_lum_size, 0);
4843 layout = llapi_layout_get_by_path(path, 0);
4844 if (layout == NULL) {
4845 llapi_error(LLAPI_MSG_ERROR, errno,
4846 "error: cannot get file layout\n");
4851 * Set the layout pointer to the last init component
4852 * since that is the component used for most of these
4853 * formats. (This also works for non-composite files)
4855 err = llapi_layout_get_last_init_comp(layout);
4857 llapi_error(LLAPI_MSG_ERROR, err,
4858 "error: cannot get last initialized compomnent\n");
4863 case 'c': /* stripe count */
4864 err = llapi_layout_stripe_count_get(layout, &str_cnt);
4866 llapi_error(LLAPI_MSG_ERROR, err,
4867 "error: cannot get stripe_count\n");
4870 *wrote = snprintf(buffer, size, "%"PRIu64, str_cnt);
4872 case 'h': /* hash info */
4873 /* Not applicable to files. Skip it. */
4875 case 'i': /* starting index */
4876 err = llapi_layout_ost_index_get(layout, 0, &idx);
4878 llapi_error(LLAPI_MSG_ERROR, err,
4879 "error: cannot get OST index of last initialized component\n");
4882 *wrote = snprintf(buffer, size, "%"PRIu64, idx);
4884 case 'o': /* list of object indices */
4885 *wrote = printf_format_ost_indices(buffer, size, layout);
4887 case 'p': /* pool name */
4888 err = llapi_layout_pool_name_get(layout, pool_name,
4891 llapi_error(LLAPI_MSG_ERROR, rc,
4892 "error: cannot get pool name\n");
4895 *wrote = snprintf(buffer, size, "%s", pool_name);
4897 case 'S': /* stripe size */
4898 err = llapi_layout_stripe_size_get(layout, &str_size);
4900 llapi_error(LLAPI_MSG_ERROR, rc,
4901 "error: cannot get stripe_size\n");
4904 *wrote = snprintf(buffer, size, "%"PRIu64, str_size);
4910 } else { /* directory */
4911 lum = (struct lmv_user_md *)param->fp_lmv_md;
4912 objects = lum->lum_objects;
4915 case 'c': /* stripe count */
4916 *wrote = snprintf(buffer, size, "%d",
4917 (int)lum->lum_stripe_count);
4919 case 'h': /* hash info */
4920 hash_type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
4921 if (hash_type < LMV_HASH_TYPE_MAX)
4922 *wrote = snprintf(buffer, size, "%s",
4923 mdt_hash_name[hash_type]);
4925 *wrote = snprintf(buffer, size, "%#x",
4928 case 'i': /* starting index */
4929 *wrote = snprintf(buffer, size, "%d",
4930 lum->lum_stripe_offset);
4932 case 'o': /* list of object indices */
4933 str_cnt = (int) lum->lum_stripe_count;
4934 *wrote = snprintf(buffer, size, "%s", "[");
4938 for (i = 0; i < str_cnt; i++) {
4939 bytes = snprintf(buffer, (size - *wrote),
4940 "%d,", objects[i].lum_mds);
4947 /* Use lum_offset as the only list entry */
4948 bytes = snprintf(buffer, (size - *wrote),
4949 "%d]", lum->lum_stripe_offset);
4952 /* Overwrite last comma with closing bracket */
4953 *(buffer - 1) = ']';
4956 case 'p': /* pool name */
4957 *wrote = snprintf(buffer, size, "%s",
4958 lum->lum_pool_name);
4960 case 'S': /* stripe size */
4961 /* This has no meaning for directories. Skip it. */
4971 llapi_layout_free(layout);
4974 /* output of snprintf was truncated */
4981 * Interpret format specifiers beginning with '%'.
4983 * @param[in] seq String being parsed for format specifier. The leading
4984 * '%' character is not included in this string
4985 * @param[out] buffer Location where formatted info is written
4986 * @param[in] size Size of the available buffer.
4987 * @param[out] wrote Number of bytes written to the buffer.
4988 * @param[in] param The find_param structure associated with the file/dir
4989 * @param[in] path Pathname of the current file/dir being handled
4990 * @param[in] projid Project ID associated with the current file/dir
4991 * @param[in] d File descriptor for the directory (or -1 for a
4992 * non-directory file)
4993 * @return Number of characters from input string processed
4994 * as part of the format (0 for an unknown format)
4996 int printf_format_directive(char *seq, char *buffer, size_t size, int *wrote,
4997 struct find_param *param, char *path, int projid,
5000 __u16 mode = param->fp_lmd->lmd_stx.stx_mode;
5001 uint64_t blocks = param->fp_lmd->lmd_stx.stx_blocks;
5002 int rc = 1; /* most specifiers are single character */
5010 case 'w': case 'W': /* timestamps */
5011 rc = printf_format_timestamp(seq, buffer, size, wrote, param);
5013 case 'b': /* file size (in 512B blocks) */
5014 *wrote = snprintf(buffer, size, "%"PRIu64, blocks);
5016 case 'G': /* GID of owner */
5017 *wrote = snprintf(buffer, size, "%u",
5018 param->fp_lmd->lmd_stx.stx_gid);
5020 case 'k': /* file size (in 1K blocks) */
5021 *wrote = snprintf(buffer, size, "%"PRIu64, (blocks + 1)/2);
5023 case 'L': /* Lustre-specific formats */
5024 rc = printf_format_lustre(seq, buffer, size, wrote, param,
5027 case 'm': /* file mode in octal */
5028 *wrote = snprintf(buffer, size, "%#o", (mode & (~S_IFMT)));
5030 case 'p': /* Path name of file */
5031 *wrote = snprintf(buffer, size, "%s", path);
5033 case 's': /* file size (in bytes) */
5034 *wrote = snprintf(buffer, size, "%"PRIu64,
5035 (uint64_t) param->fp_lmd->lmd_stx.stx_size);
5037 case 'U': /* UID of owner */
5038 *wrote = snprintf(buffer, size, "%u",
5039 param->fp_lmd->lmd_stx.stx_uid);
5041 case 'y': /* file type */
5044 else if (S_ISDIR(mode))
5046 else if (S_ISLNK(mode))
5048 else if (S_ISBLK(mode))
5050 else if (S_ISCHR(mode))
5052 else if (S_ISFIFO(mode))
5054 else if (S_ISSOCK(mode))
5064 default: /* invalid format specifier */
5070 /* output of snprintf was truncated */
5077 * Parse user-supplied string for the -printf option and interpret any
5078 * '%' format specifiers or '\' escape sequences.
5080 * @param[in] param The find_param struct containing the -printf string
5081 * as well as info about the current file/dir that mathced
5082 * the lfs find search criteria
5083 * @param[in] path Path name for current file/dir
5084 * @param[in] projid Project ID associated with current file/dir
5085 * @param[in] d File descriptor for current directory (or -1 for a
5086 * non-directory file)
5088 void printf_format_string(struct find_param *param, char *path,
5091 char output[FORMATTED_BUF_LEN];
5092 char *fmt_char = param->fp_format_printf_str;
5093 char *buff = output;
5099 buff_size = FORMATTED_BUF_LEN;
5101 /* Always leave one free byte in buffer for trailing NUL */
5102 while (*fmt_char && (buff_size > 1)) {
5105 if (*fmt_char == '%') {
5106 rc = printf_format_directive(fmt_char + 1, buff,
5107 buff_size, &written, param,
5109 } else if (*fmt_char == '\\')
5110 rc = printf_format_escape(fmt_char + 1, buff,
5111 buff_size, &written);
5114 /* Either a '\' escape or '%' format was processed.
5115 * Increment pointers accordingly.
5117 fmt_char += (rc + 1);
5119 buff_size -= written;
5121 /* Regular char or invalid escape/format.
5122 * Either way, copy current character.
5124 *buff++ = *fmt_char++;
5129 /* Terminate output buffer and print */
5131 llapi_printf(LLAPI_MSG_NORMAL, "%s", output);
5135 * Get file/directory project id.
5136 * by the open fd resides on.
5137 * Return 0 and project id on success, or -ve errno.
5139 static int fget_projid(int fd, int *projid)
5144 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
5148 *projid = fsx.fsx_projid;
5153 * Check that the file's permissions in *st matches the one in find_param
5155 static int check_file_permissions(const struct find_param *param,
5162 switch (param->fp_perm_sign) {
5163 case LFS_FIND_PERM_EXACT:
5164 decision = (mode == param->fp_perm);
5166 case LFS_FIND_PERM_ALL:
5167 decision = ((mode & param->fp_perm) == param->fp_perm);
5169 case LFS_FIND_PERM_ANY:
5170 decision = ((mode & param->fp_perm) != 0);
5174 if ((param->fp_exclude_perm && decision)
5175 || (!param->fp_exclude_perm && !decision))
5181 static int cb_find_init(char *path, int p, int *dp,
5182 void *data, struct dirent64 *de)
5184 struct find_param *param = (struct find_param *)data;
5185 struct lov_user_mds_data *lmd = param->fp_lmd;
5186 int d = dp == NULL ? -1 : *dp;
5187 int decision = 1; /* 1 is accepted; -1 is rejected. */
5189 int checked_type = 0;
5191 __u32 stripe_count = 0;
5195 bool gather_all = false;
5197 if (p == -1 && d == -1)
5200 /* Reset this value between invocations */
5201 param->fp_get_lmv = 0;
5203 /* Gather all file/dir info, not just what's needed for search params */
5204 if (param->fp_format_printf_str)
5207 /* If a regular expression is presented, make the initial decision */
5208 if (param->fp_pattern != NULL) {
5209 char *fname = strrchr(path, '/');
5211 fname = (fname == NULL ? path : fname + 1);
5212 ret = fnmatch(param->fp_pattern, fname, 0);
5213 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
5214 (ret == 0 && param->fp_exclude_pattern))
5218 /* See if we can check the file type from the dirent. */
5219 if (de != NULL && de->d_type != DT_UNKNOWN) {
5220 if (param->fp_type != 0) {
5223 if (DTTOIF(de->d_type) == param->fp_type) {
5224 if (param->fp_exclude_type)
5227 if (!param->fp_exclude_type)
5231 if ((param->fp_check_mdt_count || param->fp_hash_type ||
5232 param->fp_check_hash_flag) && de->d_type != DT_DIR)
5239 * Request MDS for the stat info if some of these parameters need
5242 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
5243 param->fp_check_uid || param->fp_check_gid ||
5244 param->fp_newerxy || param->fp_btime ||
5245 param->fp_atime || param->fp_mtime || param->fp_ctime ||
5246 param->fp_check_size || param->fp_check_blocks ||
5247 find_check_lmm_info(param) ||
5248 param->fp_check_mdt_count || param->fp_hash_type ||
5249 param->fp_check_hash_flag || param->fp_perm_sign ||
5253 if (param->fp_type != 0 && checked_type == 0)
5256 if (decision == 0) {
5258 (param->fp_check_mdt_count || param->fp_hash_type ||
5259 param->fp_check_hash_flag || param->fp_check_foreign ||
5261 param->fp_get_lmv = 1;
5262 ret = cb_get_dirstripe(path, &d, param);
5264 if (errno == ENODATA) {
5265 /* Fill in struct for unstriped dir */
5267 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
5268 /* Use 0 until we find actual offset */
5269 param->fp_lmv_md->lum_stripe_offset = 0;
5270 param->fp_lmv_md->lum_stripe_count = 0;
5271 param->fp_lmv_md->lum_hash_type = 0;
5273 if (param->fp_check_foreign) {
5274 if (param->fp_exclude_foreign)
5283 if (param->fp_check_mdt_count) {
5284 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5287 decision = find_value_cmp(param->fp_lmv_md->lum_stripe_count,
5288 param->fp_mdt_count,
5289 param->fp_mdt_count_sign,
5290 param->fp_exclude_mdt_count, 1, 0);
5295 if (param->fp_hash_type) {
5297 __u32 type = param->fp_lmv_md->lum_hash_type &
5300 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5303 found = (1 << type) & param->fp_hash_type;
5304 if ((found && param->fp_exclude_hash_type) ||
5305 (!found && !param->fp_exclude_hash_type))
5309 if (param->fp_check_hash_flag) {
5310 __u32 flags = param->fp_lmv_md->lum_hash_type &
5311 ~LMV_HASH_TYPE_MASK;
5313 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5316 if (!(flags & param->fp_hash_inflags) ||
5317 (flags & param->fp_hash_exflags))
5322 param->fp_lmd->lmd_lmm.lmm_magic = 0;
5323 ret = get_lmd_info_fd(path, p, d, param->fp_lmd,
5324 param->fp_lum_size, GET_LMD_INFO);
5325 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
5326 find_check_lmm_info(param)) {
5327 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
5330 * We need to "fake" the "use the default" values
5331 * since the lmm struct is zeroed out at this point.
5333 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5334 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
5336 ostid_set_seq(&lmm->lmm_oi,
5337 FID_SEQ_LOV_DEFAULT);
5338 lmm->lmm_stripe_size = 0;
5339 lmm->lmm_stripe_count = 0;
5340 lmm->lmm_stripe_offset = -1;
5342 if (ret == 0 && (param->fp_mdt_uuid != NULL || gather_all)) {
5344 ret = llapi_file_fget_mdtidx(d,
5345 ¶m->fp_file_mdt_index);
5347 * Make sure lum_stripe_offset matches
5348 * mdt_index even for unstriped directories.
5350 if (ret == 0 && param->fp_get_lmv)
5351 param->fp_lmv_md->lum_stripe_offset =
5352 param->fp_file_mdt_index;
5353 } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
5355 * FIXME: we could get the MDT index from the
5356 * file's FID in lmd->lmd_lmm.lmm_oi without
5357 * opening the file, once we are sure that
5358 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
5359 * That would still be an ioctl() to map the
5360 * FID to the MDT, but not an open RPC.
5362 fd = open(path, O_RDONLY);
5364 ret = llapi_file_fget_mdtidx(fd,
5365 ¶m->fp_file_mdt_index);
5371 * For a special file, we assume it resides on
5372 * the same MDT as the parent directory.
5374 ret = llapi_file_fget_mdtidx(p,
5375 ¶m->fp_file_mdt_index);
5386 stripe_count = find_get_stripe_count(param);
5390 /* Check the file permissions from the stat info */
5391 if (param->fp_perm_sign) {
5392 decision = check_file_permissions(param, lmd->lmd_stx.stx_mode);
5397 if (param->fp_type && !checked_type) {
5398 if ((param->fp_check_mdt_count || param->fp_check_hash_flag ||
5399 param->fp_hash_type) && !S_ISDIR(lmd->lmd_stx.stx_mode))
5402 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
5403 if (param->fp_exclude_type)
5406 if (!param->fp_exclude_type)
5412 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
5413 if (lustre_fs && param->fp_got_uuids &&
5414 param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
5415 lmd->lmd_stx.stx_dev_minor)) {
5416 /* A lustre/lustre mount point is crossed. */
5417 param->fp_got_uuids = 0;
5418 param->fp_obds_printed = 0;
5419 param->fp_mdt_index = OBD_NOT_FOUND;
5420 param->fp_obd_index = OBD_NOT_FOUND;
5423 if (lustre_fs && !param->fp_got_uuids) {
5424 ret = setup_target_indexes((d != -1) ? d : p, path,
5429 param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
5430 lmd->lmd_stx.stx_dev_minor);
5431 } else if (!lustre_fs && param->fp_got_uuids) {
5432 /* A lustre/non-lustre mount point is crossed. */
5433 param->fp_got_uuids = 0;
5434 param->fp_mdt_index = OBD_NOT_FOUND;
5435 param->fp_obd_index = OBD_NOT_FOUND;
5439 if (param->fp_check_foreign) {
5440 decision = find_check_foreign(param);
5445 if (param->fp_check_stripe_size) {
5446 decision = find_check_stripe_size(param);
5451 if (param->fp_check_ext_size) {
5452 decision = find_check_ext_size(param);
5457 if (param->fp_check_stripe_count) {
5458 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
5459 param->fp_stripe_count_sign,
5460 param->fp_exclude_stripe_count, 1, 0);
5465 if (param->fp_check_layout) {
5466 decision = find_check_layout(param);
5471 /* If an OBD UUID is specified but none matches, skip this file. */
5472 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
5473 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
5477 * If an OST or MDT UUID is given, and some OST matches,
5480 if (param->fp_obd_index != OBD_NOT_FOUND ||
5481 param->fp_mdt_index != OBD_NOT_FOUND) {
5482 if (param->fp_obd_uuid) {
5483 if (check_obd_match(param)) {
5485 * If no mdtuuid is given, we are done.
5486 * Otherwise, fall through to the mdtuuid
5489 if (!param->fp_mdt_uuid)
5496 if (param->fp_mdt_uuid) {
5497 if (check_mdt_match(param))
5504 if (param->fp_check_uid) {
5505 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
5506 if (param->fp_exclude_uid)
5509 if (!param->fp_exclude_uid)
5514 if (param->fp_check_gid) {
5515 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
5516 if (param->fp_exclude_gid)
5519 if (!param->fp_exclude_gid)
5524 if (param->fp_check_projid || gather_all) {
5528 fd = open(path, O_RDONLY);
5531 ret = fget_projid(fd, &projid);
5536 if (projid == param->fp_projid) {
5537 if (param->fp_exclude_projid)
5540 if (!param->fp_exclude_projid)
5545 if (param->fp_check_pool) {
5546 decision = find_check_pool(param);
5551 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
5552 param->fp_check_comp_start || param->fp_check_comp_end) {
5553 decision = find_check_comp_options(param);
5558 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
5559 decision = find_check_mirror_options(param);
5564 /* Check the time on mds. */
5566 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
5569 for_mds = lustre_fs ?
5570 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5571 decision = find_time_check(param, for_mds);
5576 if (param->fp_btime) {
5577 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
5582 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
5583 param->fp_btime, param->fp_bsign,
5584 param->fp_exclude_btime,
5585 param->fp_time_margin, 0);
5590 if (param->fp_newerxy) {
5593 for_mds = lustre_fs ?
5594 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5595 decision = find_newerxy_check(param, for_mds, true);
5604 flags = param->fp_lmd->lmd_flags;
5605 if (param->fp_check_size &&
5606 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5607 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5608 !(flags & OBD_MD_FLSIZE ||
5609 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
5612 if (param->fp_check_blocks &&
5613 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5614 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5615 !(flags & OBD_MD_FLBLOCKS ||
5616 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
5620 * If file still fits the request, ask ost for updated info.
5621 * The regular stat is almost of the same speed as some new
5622 * 'glimpse-size-ioctl'.
5624 if (!decision || gather_all) {
5628 * For regular files with the stripe the decision may have not
5629 * been taken yet if *time or size is to be checked.
5631 if (param->fp_obd_index != OBD_NOT_FOUND)
5632 print_failed_tgt(param, path, LL_STATFS_LOV);
5634 if (param->fp_mdt_index != OBD_NOT_FOUND)
5635 print_failed_tgt(param, path, LL_STATFS_LMV);
5638 ret = fstat_f(d, &st);
5639 else if (de != NULL)
5640 ret = fstatat_f(p, de->d_name, &st,
5641 AT_SYMLINK_NOFOLLOW);
5643 ret = lstat_f(path, &st);
5646 if (errno == ENOENT) {
5647 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
5648 "warning: %s: %s does not exist",
5653 llapi_error(LLAPI_MSG_ERROR, ret,
5654 "%s: stat on %s failed",
5660 convert_lmd_statx(param->fp_lmd, &st, true);
5661 /* Check the time on osc. */
5662 decision = find_time_check(param, 0);
5666 if (param->fp_newerxy) {
5667 decision = find_newerxy_check(param, 0, false);
5677 if (param->fp_check_size) {
5678 decision = find_value_cmp(lmd->lmd_stx.stx_size,
5680 param->fp_size_sign,
5681 param->fp_exclude_size,
5682 param->fp_size_units, 0);
5687 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
5688 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
5690 param->fp_blocks_sign,
5691 param->fp_exclude_blocks,
5692 param->fp_blocks_units, 0);
5698 if (param->fp_format_printf_str)
5699 printf_format_string(param, path, projid, d);
5701 llapi_printf(LLAPI_MSG_NORMAL, "%s%c", path,
5702 param->fp_zero_end ? '\0' : '\n');
5706 /* Do not get down anymore? */
5707 if (param->fp_depth == param->fp_max_depth) {
5718 static int cb_migrate_mdt_init(char *path, int p, int *dp,
5719 void *param_data, struct dirent64 *de)
5721 struct find_param *param = (struct find_param *)param_data;
5722 struct lmv_user_md *lmu = param->fp_lmv_md;
5724 char raw[MAX_IOC_BUFLEN] = {'\0'};
5726 struct obd_ioctl_data data = { 0 };
5732 if (p == -1 && dp == NULL)
5738 if (dp != NULL && *dp != -1)
5742 tmp_p = open_parent(path);
5746 llapi_error(LLAPI_MSG_ERROR, ret,
5747 "can not open %s", path);
5752 path_copy = strdup(path);
5753 filename = basename(path_copy);
5755 data.ioc_inlbuf1 = (char *)filename;
5756 data.ioc_inllen1 = strlen(filename) + 1;
5757 data.ioc_inlbuf2 = (char *)lmu;
5758 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
5761 if (param->fp_depth == param->fp_max_depth)
5762 data.ioc_type = MDS_MIGRATE_NSONLY;
5763 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5765 llapi_error(LLAPI_MSG_ERROR, ret,
5766 "%s: error packing ioctl data", __func__);
5771 ret = ioctl(tmp_p, LL_IOC_MIGRATE, rawbuf);
5773 if (errno == EBUSY && !retry) {
5775 * because migrate may not be able to lock all involved
5776 * objects in order, for some of them it try lock, while
5777 * there may be conflicting COS locks and cause migrate
5778 * fail with EBUSY, hope a sync() could cause
5779 * transaction commit and release these COS locks.
5784 } else if (errno == EALREADY) {
5785 if (param->fp_verbose & VERBOSE_DETAIL)
5786 llapi_printf(LLAPI_MSG_NORMAL,
5787 "%s migrated to MDT%d already\n",
5788 path, lmu->lum_stripe_offset);
5792 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
5796 } else if (param->fp_verbose & VERBOSE_DETAIL) {
5797 llapi_printf(LLAPI_MSG_NORMAL,
5798 "migrate %s to MDT%d stripe count %d\n",
5799 path, lmu->lum_stripe_offset,
5800 lmu->lum_stripe_count);
5804 /* Do not get down anymore? */
5805 if (param->fp_depth == param->fp_max_depth)
5812 * If the directory is being migration, we need
5813 * close the directory after migration,
5814 * so the old directory cache will be cleanup
5815 * on the client side, and re-open to get the
5816 * new directory handle
5818 *dp = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
5821 llapi_error(LLAPI_MSG_ERROR, ret,
5822 "%s: Failed to open '%s'", __func__, path);
5834 /* dir migration finished, shrink its stripes */
5835 static int cb_migrate_mdt_fini(char *path, int p, int *dp, void *data,
5836 struct dirent64 *de)
5838 struct find_param *param = data;
5839 struct lmv_user_md *lmu = param->fp_lmv_md;
5840 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
5843 if (de && de->d_type != DT_DIR)
5848 * close it before setxattr because the latter may destroy the
5849 * original object, and cause close fail.
5857 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
5858 if (ret == -EALREADY)
5861 cb_common_fini(path, p, dp, data, de);
5865 int llapi_migrate_mdt(char *path, struct find_param *param)
5867 param->fp_stop_on_error = 1;
5868 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
5872 int llapi_mv(char *path, struct find_param *param)
5874 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
5875 static bool printed;
5878 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
5879 "%s() is deprecated, use llapi_migrate_mdt() instead",
5884 return llapi_migrate_mdt(path, param);
5888 * Check string for escape sequences and print a message to stdout
5889 * if any invalid escapes are found.
5891 * @param[in] c Pointer to character immediately following the
5892 * '\' character indicating the start of an escape
5894 * @return Number of characters examined in the escape sequence
5895 * (regardless of whether the sequence is valid or not).
5897 int validate_printf_esc(char *c)
5899 char *valid_esc = "nt\\";
5902 /* backslash at end of string */
5903 llapi_err_noerrno(LLAPI_MSG_WARN,
5904 "warning: '\\' at end of -printf format string\n");
5908 if (!strchr(valid_esc, *c))
5909 /* Invalid escape character */
5910 llapi_err_noerrno(LLAPI_MSG_WARN,
5911 "warning: unrecognized escape: '\\%c'\n", *c);
5917 * Check string for format directives and print a message to stdout
5918 * if any invalid directives are found.
5920 * @param[in] c Pointer to character immediately following the
5921 * '%' character indicating the start of a format
5923 * @return Number of characters examined in the format directive
5924 * (regardless of whether the directive is valid or not).
5926 int validate_printf_fmt(char *c)
5928 char *valid_fmt_single = "abcGkmpstUwy%";
5929 char *valid_fmt_double = "ACTW";
5930 char *valid_fmt_lustre = "cFhioPpS";
5931 char curr = *c, next;
5934 llapi_err_noerrno(LLAPI_MSG_WARN,
5935 "warning: '%%' at end of -printf format string\n");
5940 if ((next == '\0') || (next == '%') || (next == '\\'))
5941 /* Treat as single char format directive */
5944 /* Check format directives with multiple characters */
5945 if (strchr(valid_fmt_double, curr)) {
5946 /* For now, only valid formats are followed by '@' char */
5948 llapi_err_noerrno(LLAPI_MSG_WARN,
5949 "warning: unrecognized format directive: '%%%c%c'\n",
5954 /* Lustre formats always start with 'L' */
5956 if (!strchr(valid_fmt_lustre, next))
5957 llapi_err_noerrno(LLAPI_MSG_WARN,
5958 "warning: unrecognized format directive: '%%%c%c'\n",
5965 if (!strchr(valid_fmt_single, curr))
5966 llapi_err_noerrno(LLAPI_MSG_WARN,
5967 "warning: unrecognized format directive: '%%%c'\n", curr);
5972 * Validate the user-supplied string for the -printf option and report
5973 * any invalid backslash escape sequences or format directives.
5975 * @param[in] param Structure containing info about invocation of lfs find
5978 void validate_printf_str(struct find_param *param)
5980 char *c = param->fp_format_printf_str;
5986 ret = validate_printf_fmt(++c);
5990 ret = validate_printf_esc(++c);
6000 int llapi_find(char *path, struct find_param *param)
6002 if (param->fp_format_printf_str)
6003 validate_printf_str(param);
6004 return param_callback(path, cb_find_init, cb_common_fini, param);
6008 * Get MDT number that the file/directory inode referenced
6009 * by the open fd resides on.
6010 * Return 0 and mdtidx on success, or -ve errno.
6012 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
6014 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
6019 static int cb_get_mdt_index(char *path, int p, int *dp, void *data,
6020 struct dirent64 *de)
6022 struct find_param *param = (struct find_param *)data;
6023 int d = dp == NULL ? -1 : *dp;
6027 if (p == -1 && d == -1)
6031 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6032 } else /* if (p != -1) */ {
6035 fd = open(path, O_RDONLY | O_NOCTTY);
6037 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
6045 if (ret == -ENODATA) {
6046 if (!param->fp_obd_uuid)
6047 llapi_printf(LLAPI_MSG_NORMAL,
6048 "'%s' has no stripe info\n", path);
6050 } else if (ret == -ENOENT) {
6051 llapi_error(LLAPI_MSG_WARN, ret,
6052 "warning: %s: '%s' does not exist",
6055 } else if (ret == -ENOTTY) {
6056 llapi_error(LLAPI_MSG_ERROR, ret,
6057 "%s: '%s' not on a Lustre fs",
6060 llapi_error(LLAPI_MSG_ERROR, ret,
6061 "error: %s: '%s' failed get_mdtidx",
6067 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
6068 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
6070 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
6074 /* Do not go down anymore? */
6075 if (param->fp_depth == param->fp_max_depth)
6083 static int cb_getstripe(char *path, int p, int *dp, void *data,
6084 struct dirent64 *de)
6086 struct find_param *param = (struct find_param *)data;
6087 int d = dp == NULL ? -1 : *dp;
6090 if (p == -1 && d == -1)
6093 if (param->fp_obd_uuid) {
6094 param->fp_quiet = 1;
6095 ret = setup_obd_uuid(d != -1 ? d : p, path, param);
6100 if (d != -1 && (param->fp_get_lmv || param->fp_get_default_lmv))
6101 ret = cb_get_dirstripe(path, &d, param);
6103 (p != -1 && !param->fp_get_lmv && !param->fp_get_default_lmv))
6104 ret = get_lmd_info_fd(path, p, d, ¶m->fp_lmd->lmd_lmm,
6105 param->fp_lum_size, GET_LMD_STRIPE);
6106 else if (d == -1 && (param->fp_get_lmv || param->fp_get_default_lmv)) {
6107 /* in case of a dangling or valid faked symlink dir, opendir()
6108 * should have return either EINVAL or ENOENT, so let's try
6109 * to get LMV just in case, and by opening it as a file but
6110 * with O_NOFOLLOW ...
6112 int fd = open(path, O_RDONLY | O_NOFOLLOW);
6116 ret = cb_get_dirstripe(path, &fd, param);
6118 llapi_lov_dump_user_lmm(param, path, LDF_IS_DIR);
6125 if (errno == ENODATA && d != -1) {
6127 * We need to "fake" the "use the default" values
6128 * since the lmm struct is zeroed out at this point.
6129 * The magic needs to be set in order to satisfy
6130 * a check later on in the code path.
6131 * The object_seq needs to be set for the "(Default)"
6132 * prefix to be displayed.
6134 if (param->fp_get_default_lmv) {
6135 struct lmv_user_md *lum = param->fp_lmv_md;
6137 lum->lum_magic = LMV_USER_MAGIC;
6138 lum->lum_stripe_count = 0;
6139 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
6141 } else if (param->fp_get_lmv) {
6142 struct lmv_user_md *lum = param->fp_lmv_md;
6145 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6148 lum->lum_magic = LMV_MAGIC_V1;
6149 lum->lum_stripe_count = 0;
6150 lum->lum_stripe_offset = mdtidx;
6153 struct lov_user_md *lmm =
6154 ¶m->fp_lmd->lmd_lmm;
6156 lmm->lmm_magic = LOV_USER_MAGIC_V1;
6158 ostid_set_seq(&lmm->lmm_oi,
6159 FID_SEQ_LOV_DEFAULT);
6160 lmm->lmm_stripe_count = 0;
6161 lmm->lmm_stripe_size = 0;
6162 lmm->lmm_stripe_offset = -1;
6165 } else if (errno == ENODATA && p != -1) {
6166 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
6167 llapi_printf(LLAPI_MSG_NORMAL,
6168 "%s has no stripe info\n", path);
6170 } else if (errno == ENOENT) {
6171 llapi_error(LLAPI_MSG_WARN, -ENOENT,
6172 "warning: %s: %s does not exist",
6175 } else if (errno == ENOTTY) {
6177 llapi_error(LLAPI_MSG_ERROR, ret,
6178 "%s: '%s' not on a Lustre fs?",
6183 llapi_error(LLAPI_MSG_ERROR, ret,
6184 "error: %s: %s failed for %s",
6186 "LL_IOC_LOV_GETSTRIPE" :
6187 "IOC_MDC_GETFILESTRIPE", path);
6194 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
6195 llapi_lov_dump_user_lmm(param, path, d != -1 ? LDF_IS_DIR : 0);
6198 /* Do not get down anymore? */
6199 if (param->fp_depth == param->fp_max_depth)
6207 int llapi_getstripe(char *path, struct find_param *param)
6209 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
6210 cb_get_mdt_index : cb_getstripe,
6211 cb_common_fini, param);
6214 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
6215 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6217 char raw[MAX_IOC_BUFLEN] = {'\0'};
6219 struct obd_ioctl_data data = { 0 };
6222 data.ioc_inlbuf1 = (char *)&type;
6223 data.ioc_inllen1 = sizeof(__u32);
6224 data.ioc_inlbuf2 = (char *)&index;
6225 data.ioc_inllen2 = sizeof(__u32);
6226 data.ioc_pbuf1 = (char *)stat_buf;
6227 data.ioc_plen1 = sizeof(struct obd_statfs);
6228 data.ioc_pbuf2 = (char *)uuid_buf;
6229 data.ioc_plen2 = sizeof(struct obd_uuid);
6231 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
6233 llapi_error(LLAPI_MSG_ERROR, rc,
6234 "%s: error packing ioctl data", __func__);
6238 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
6240 return rc < 0 ? -errno : 0;
6243 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
6244 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6249 fd = open(path, O_RDONLY);
6252 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
6255 * If we can't even open a file on the filesystem (e.g. with
6256 * -ESHUTDOWN), force caller to exit or it will loop forever.
6261 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
6268 #define MAX_STRING_SIZE 128
6270 int llapi_ping(char *obd_type, char *obd_name)
6272 int flags = O_RDONLY;
6273 char buf[1] = { 0 };
6277 rc = cfs_get_param_paths(&path, "%s/%s/ping",
6278 obd_type, obd_name);
6282 fd = open(path.gl_pathv[0], flags);
6284 if (errno == EACCES && flags == O_RDONLY) {
6289 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
6294 if (flags == O_RDONLY)
6295 rc = read(fd, buf, sizeof(buf));
6297 rc = write(fd, buf, sizeof(buf));
6305 cfs_free_param_data(&path);
6309 int llapi_target_iterate(int type_num, char **obd_type,
6310 void *args, llapi_cb_t cb)
6316 for (i = 0; i < type_num; i++) {
6319 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
6323 for (j = 0; j < param.gl_pathc; j++) {
6324 char obd_uuid[UUID_MAX + 1];
6328 fp = fopen(param.gl_pathv[j], "r");
6331 llapi_error(LLAPI_MSG_ERROR, rc,
6332 "error: opening '%s'",
6337 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
6339 llapi_error(LLAPI_MSG_ERROR, rc,
6340 "error: reading '%s'",
6345 /* Extract the obd_name from the sysfs path.
6346 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
6348 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
6354 /* skip /fs/lustre/'obd_type'/ */
6355 obd_name += strlen(obd_type[i]) + 12;
6356 /* chop off after obd_name */
6357 ptr = strrchr(obd_name, '/');
6361 cb(obd_type[i], obd_name, obd_uuid, args);
6370 cfs_free_param_data(¶m);
6374 struct check_target_filter {
6379 static void do_target_check(char *obd_type_name, char *obd_name,
6380 char *obd_uuid, void *args)
6383 struct check_target_filter *filter = args;
6385 if (filter != NULL) {
6386 /* check nid if obd type is mgc */
6387 if (strcmp(obd_type_name, "mgc") == 0) {
6388 if (strcmp(obd_name + 3, filter->nid) != 0)
6391 /* check instance for other types of device (osc/mdc) */
6392 else if (strstr(obd_name, filter->instance) == NULL)
6396 rc = llapi_ping(obd_type_name, obd_name);
6398 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
6400 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
6402 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
6405 int llapi_target_check(int type_num, char **obd_type, char *dir)
6407 char nid[MAX_LINE_LEN], instance[MAX_INSTANCE_LEN];
6408 struct check_target_filter filter = {NULL, NULL};
6411 if (dir == NULL || dir[0] == '\0')
6412 return llapi_target_iterate(type_num, obd_type, NULL,
6415 rc = get_root_path(WANT_NID | WANT_ERROR, NULL, NULL, dir, -1, NULL,
6418 llapi_error(LLAPI_MSG_ERROR, rc,
6419 "cannot get nid of path '%s'", dir);
6424 rc = llapi_get_instance(dir, instance, ARRAY_SIZE(instance));
6427 filter.instance = instance;
6429 return llapi_target_iterate(type_num, obd_type, &filter,
6433 #undef MAX_STRING_SIZE
6435 /* Is this a lustre fs? */
6436 int llapi_is_lustre_mnttype(const char *type)
6438 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
6441 /* Is this a lustre client fs? */
6442 int llapi_is_lustre_mnt(struct mntent *mnt)
6444 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
6445 strstr(mnt->mnt_fsname, ":/") != NULL);
6448 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
6450 char fsname[PATH_MAX + 1];
6454 rc = llapi_search_fsname(mnt, fsname);
6458 root = open(mnt, O_RDONLY | O_DIRECTORY);
6461 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
6465 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
6468 if (rc == -ENOENT && LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd))
6469 llapi_error(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, rc,
6470 "Cannot find pool '%s'", qctl->qc_poolname);
6476 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
6481 root = open(mnt, O_RDONLY | O_DIRECTORY);
6484 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
6488 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
6491 llapi_error(LLAPI_MSG_ERROR, rc,
6492 "ioctl on %s for getting connect flags failed", mnt);
6499 * Flush cached pages from all clients.
6501 * \param fd File descriptor
6505 int llapi_file_flush(int fd)
6509 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);