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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/utils/liblustreapi.c
34 * Author: Peter J. Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Robert Read <rread@clusterfs.com>
50 #include <sys/ioctl.h>
57 #include <sys/statfs.h>
58 #include <sys/syscall.h>
60 #include <sys/types.h>
61 #include <sys/xattr.h>
64 #include <libgen.h> /* for dirname() */
65 #ifdef HAVE_LINUX_UNISTD_H
66 #include <linux/unistd.h>
73 #include <libcfs/util/ioctl.h>
74 #include <libcfs/util/param.h>
75 #include <libcfs/util/string.h>
76 #include <linux/lnet/lnetctl.h>
77 #include <lustre/lustreapi.h>
78 #include <linux/lustre/lustre_ostid.h>
79 #include <linux/lustre/lustre_ioctl.h>
80 #include "lustreapi_internal.h"
82 static int llapi_msg_level = LLAPI_MSG_MAX;
83 const char *liblustreapi_cmd;
85 char *mdt_hash_name[] = { "none",
86 LMV_HASH_NAME_ALL_CHARS,
87 LMV_HASH_NAME_FNV_1A_64,
91 struct lustre_foreign_type lu_foreign_types[] = {
92 {.lft_type = LU_FOREIGN_TYPE_NONE, .lft_name = "none"},
93 {.lft_type = LU_FOREIGN_TYPE_DAOS, .lft_name = "daos"},
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 if (liblustreapi_cmd != NULL)
129 fprintf(stderr, "%s %s: ", program_invocation_short_name,
132 fprintf(stderr, "%s: ", program_invocation_short_name);
133 vfprintf(stderr, fmt, ap);
134 if (level & LLAPI_MSG_NO_ERRNO)
135 fprintf(stderr, "\n");
137 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
140 static void info_callback_default(enum llapi_message_level level, int err,
141 const char *fmt, va_list ap)
144 if (liblustreapi_cmd != NULL) {
145 fprintf(stdout, "%s %s: ",
146 program_invocation_short_name,
149 fprintf(stdout, "%s: ", program_invocation_short_name);
152 vfprintf(stdout, fmt, ap);
155 static llapi_log_callback_t llapi_error_callback = error_callback_default;
156 static llapi_log_callback_t llapi_info_callback = info_callback_default;
159 /* llapi_error will preserve errno */
160 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
163 int tmp_errno = errno;
165 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
169 llapi_error_callback(level, abs(err), fmt, args);
174 /* llapi_printf will preserve errno */
175 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
178 int tmp_errno = errno;
180 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
184 llapi_info_callback(level, 0, fmt, args);
190 * Set a custom error logging function. Passing in NULL will reset the logging
191 * callback to its default value.
193 * This function returns the value of the old callback.
195 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
197 llapi_log_callback_t old = llapi_error_callback;
200 llapi_error_callback = cb;
202 llapi_error_callback = error_callback_default;
208 * Set a custom info logging function. Passing in NULL will reset the logging
209 * callback to its default value.
211 * This function returns the value of the old callback.
213 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
215 llapi_log_callback_t old = llapi_info_callback;
218 llapi_info_callback = cb;
220 llapi_info_callback = info_callback_default;
226 * size_units is to be initialized (or zeroed) by caller.
228 int llapi_parse_size(const char *optarg, unsigned long long *size,
229 unsigned long long *size_units, int bytes_spec)
232 char *argbuf = (char *)optarg;
233 unsigned long long frac = 0, frac_d = 1;
235 if (strncmp(optarg, "-", 1) == 0)
238 if (*size_units == 0)
241 *size = strtoull(argbuf, &end, 0);
242 if (end != NULL && *end == '.') {
246 frac = strtoull(argbuf, &end, 10);
247 /* count decimal places */
248 for (i = 0; i < (end - argbuf); i++)
253 if ((*end == 'b') && *(end + 1) == '\0' &&
254 (*size & (~0ULL << (64 - 9))) == 0 &&
256 *size_units = 1 << 9;
257 } else if ((*end == 'b') &&
258 *(end + 1) == '\0' &&
261 } else if ((*end == 'k' || *end == 'K') &&
262 *(end + 1) == '\0' &&
263 (*size & (~0ULL << (64 - 10))) == 0) {
264 *size_units = 1 << 10;
265 } else if ((*end == 'm' || *end == 'M') &&
266 *(end + 1) == '\0' &&
267 (*size & (~0ULL << (64 - 20))) == 0) {
268 *size_units = 1 << 20;
269 } else if ((*end == 'g' || *end == 'G') &&
270 *(end + 1) == '\0' &&
271 (*size & (~0ULL << (64 - 30))) == 0) {
272 *size_units = 1 << 30;
273 } else if ((*end == 't' || *end == 'T') &&
274 *(end + 1) == '\0' &&
275 (*size & (~0ULL << (64 - 40))) == 0) {
276 *size_units = 1ULL << 40;
277 } else if ((*end == 'p' || *end == 'P') &&
278 *(end + 1) == '\0' &&
279 (*size & (~0ULL << (64 - 50))) == 0) {
280 *size_units = 1ULL << 50;
281 } else if ((*end == 'e' || *end == 'E') &&
282 *(end + 1) == '\0' &&
283 (*size & (~0ULL << (64 - 60))) == 0) {
284 *size_units = 1ULL << 60;
289 *size = *size * *size_units + frac * *size_units / frac_d;
294 int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
296 struct obd_ioctl_data *overlay;
299 data->ioc_len = obd_ioctl_packlen(data);
300 data->ioc_version = OBD_IOCTL_VERSION;
302 if (*pbuf != NULL && data->ioc_len > max_len) {
303 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
304 "pbuf = %p, ioc_len = %u, max_len = %d\n",
305 *pbuf, data->ioc_len, max_len);
310 *pbuf = malloc(data->ioc_len);
315 overlay = (struct obd_ioctl_data *)*pbuf;
316 memcpy(*pbuf, data, sizeof(*data));
318 ptr = overlay->ioc_bulk;
319 if (data->ioc_inlbuf1) {
320 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
321 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
324 if (data->ioc_inlbuf2) {
325 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
326 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
329 if (data->ioc_inlbuf3) {
330 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
331 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
334 if (data->ioc_inlbuf4) {
335 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
336 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
342 int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
344 struct obd_ioctl_data *overlay;
350 overlay = (struct obd_ioctl_data *)pbuf;
352 /* Preserve the caller's buffer pointers */
353 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
354 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
355 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
356 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
358 memcpy(data, pbuf, sizeof(*data));
360 ptr = overlay->ioc_bulk;
361 if (data->ioc_inlbuf1) {
362 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
363 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
366 if (data->ioc_inlbuf2) {
367 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
368 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
371 if (data->ioc_inlbuf3) {
372 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
373 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
376 if (data->ioc_inlbuf4) {
377 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
378 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
384 /* XXX: llapi_xxx() functions return negative values upon failure */
386 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
387 int stripe_count, int stripe_pattern)
391 /* 64 KB is the largest common page size I'm aware of (on ia64), but
392 * check the local page size just in case. */
393 page_size = LOV_MIN_STRIPE_SIZE;
394 if (getpagesize() > page_size) {
395 page_size = getpagesize();
396 llapi_err_noerrno(LLAPI_MSG_WARN,
397 "warning: your page size (%u) is "
398 "larger than expected (%u)", page_size,
399 LOV_MIN_STRIPE_SIZE);
401 if (!llapi_stripe_size_is_aligned(stripe_size)) {
403 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
404 "must be an even multiple of %d bytes",
405 stripe_size, page_size);
408 if (!llapi_stripe_index_is_valid(stripe_offset)) {
410 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
414 if (!llapi_stripe_count_is_valid(stripe_count)) {
416 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
420 if (llapi_stripe_size_is_too_big(stripe_size)) {
422 llapi_error(LLAPI_MSG_ERROR, rc,
423 "warning: stripe size 4G or larger "
424 "is not currently supported and would wrap");
430 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
435 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
437 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
441 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
443 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
448 if (!llapi_dir_hash_type_is_valid(hash_type)) {
450 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
458 * Trim a trailing newline from a string, if it exists.
460 int llapi_chomp_string(char *buf)
476 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
478 static int get_param_lov(const char *path, const char *param,
479 char *buf, size_t buf_size)
481 struct obd_uuid uuid;
484 rc = llapi_file_get_lov_uuid(path, &uuid);
488 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
493 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
495 static int get_param_lmv(const char *path, const char *param,
496 char *buf, size_t buf_size)
498 struct obd_uuid uuid;
501 rc = llapi_file_get_lmv_uuid(path, &uuid);
505 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
509 static int get_mds_md_size(const char *path)
511 char buf[PATH_MAX], inst[PATH_MAX];
512 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
515 rc = llapi_getname(path, inst, sizeof(inst));
519 /* Get the max ea size from llite parameters. */
520 rc = get_lustre_param_value("llite", inst, FILTER_BY_EXACT,
521 "max_easize", buf, sizeof(buf));
527 return rc > 0 ? rc : md_size;
530 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
532 return get_param_lmv(path, "uuid", buf, bufsize);
536 * if pool is NULL, search tgtname in target_obd
537 * if pool is not NULL:
538 * if pool not found returns errno < 0
539 * if tgtname is NULL, returns 1 if pool is not empty and 0 if pool empty
540 * if tgtname is not NULL, returns 1 if OST is in pool and 0 if not
542 int llapi_search_tgt(char *fsname, char *poolname, char *tgtname, bool is_mdt)
544 char buffer[PATH_MAX];
550 /* You need one or the other */
551 if (poolname == NULL && fsname == NULL)
555 len = strlen(tgtname);
557 if (poolname == NULL && len == 0)
560 /* Search by poolname and fsname if is not NULL */
561 if (poolname != NULL) {
562 rc = poolpath(¶m, fsname, NULL);
564 snprintf(buffer, sizeof(buffer), "%s/%s",
565 param.gl_pathv[0], poolname);
567 } else if (fsname != NULL) {
568 rc = get_lustre_param_path(is_mdt ? "lmv" : "lov", fsname,
570 "target_obd", ¶m);
572 strncpy(buffer, param.gl_pathv[0],
578 cfs_free_param_data(¶m);
582 fd = fopen(buffer, "r");
586 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
587 if (poolname == NULL) {
589 /* Search for an tgtname in the list of targets
590 * Line format is IDX: fsname-OST/MDTxxxx_UUID STATUS */
591 ptr = strchr(buffer, ' ');
593 (strncmp(ptr + 1, tgtname, len) == 0)) {
598 /* Search for an tgtname in a pool,
599 * (or an existing non-empty pool if no tgtname) */
600 if ((tgtname == NULL) ||
601 (strncmp(buffer, tgtname, len) == 0)) {
611 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
613 return llapi_search_tgt(fsname, poolname, ostname, false);
617 * Open a Lustre file.
619 * \param name the name of the file to be opened
620 * \param flags access mode, see flags in open(2)
621 * \param mode permission of the file if it is created, see mode in open(2)
622 * \param param stripe pattern of the newly created file
624 * \retval file descriptor of opened file
625 * \retval negative errno on failure
627 int llapi_file_open_param(const char *name, int flags, mode_t mode,
628 const struct llapi_stripe_param *param)
630 char fsname[MAX_OBD_NAME + 1] = { 0 };
631 char *pool_name = param->lsp_pool;
632 struct lov_user_md *lum = NULL;
633 size_t lum_size = sizeof(*lum);
636 /* Make sure we are on a Lustre file system */
637 rc = llapi_search_fsname(name, fsname);
639 llapi_error(LLAPI_MSG_ERROR, rc,
640 "'%s' is not on a Lustre filesystem",
645 /* Check if the stripe pattern is sane. */
646 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
647 param->lsp_stripe_offset,
648 param->lsp_stripe_count,
649 param->lsp_stripe_pattern);
653 /* Make sure we have a good pool */
654 if (pool_name != NULL) {
655 /* in case user gives the full pool name <fsname>.<poolname>,
656 * strip the fsname */
657 char *ptr = strchr(pool_name, '.');
660 if (strcmp(pool_name, fsname) != 0) {
662 llapi_err_noerrno(LLAPI_MSG_ERROR,
663 "Pool '%s' is not on filesystem '%s'",
670 /* Make sure the pool exists and is non-empty */
671 rc = llapi_search_ost(fsname, pool_name, NULL);
673 char *err = rc == 0 ? "has no OSTs" : "does not exist";
675 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
676 fsname, pool_name, err);
680 lum_size = sizeof(struct lov_user_md_v3);
683 /* sanity check of target list */
684 if (param->lsp_is_specific) {
685 char ostname[MAX_OBD_NAME + 64];
689 for (i = 0; i < param->lsp_stripe_count; i++) {
690 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
691 fsname, param->lsp_osts[i]);
692 rc = llapi_search_ost(fsname, pool_name, ostname);
697 llapi_error(LLAPI_MSG_ERROR, rc,
698 "%s: cannot find OST %s in %s",
705 /* Make sure stripe offset is in OST list. */
706 if (param->lsp_osts[i] == param->lsp_stripe_offset)
710 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
711 "%s: stripe offset '%d' is not in the "
713 __func__, param->lsp_stripe_offset);
717 lum_size = lov_user_md_size(param->lsp_stripe_count,
718 LOV_USER_MAGIC_SPECIFIC);
721 lum = calloc(1, lum_size);
726 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
728 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
729 flags = O_DIRECTORY | O_RDONLY;
736 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
741 /* Initialize IOCTL striping pattern structure */
742 lum->lmm_magic = LOV_USER_MAGIC_V1;
743 lum->lmm_pattern = param->lsp_stripe_pattern;
744 lum->lmm_stripe_size = param->lsp_stripe_size;
745 lum->lmm_stripe_count = param->lsp_stripe_count;
746 lum->lmm_stripe_offset = param->lsp_stripe_offset;
747 if (pool_name != NULL) {
748 struct lov_user_md_v3 *lumv3 = (void *)lum;
750 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
751 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
753 if (param->lsp_is_specific) {
754 struct lov_user_md_v3 *lumv3 = (void *)lum;
757 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
758 if (pool_name == NULL) {
759 /* LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
760 * OST list, therefore if pool is not specified we have
761 * to pack a null pool name for placeholder. */
762 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
765 for (i = 0; i < param->lsp_stripe_count; i++)
766 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
769 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
770 char *errmsg = "stripe already set";
773 if (errno != EEXIST && errno != EALREADY)
774 errmsg = strerror(errno);
776 llapi_err_noerrno(LLAPI_MSG_ERROR,
777 "setstripe error for '%s': %s", name, errmsg);
788 int llapi_file_open_pool(const char *name, int flags, int mode,
789 unsigned long long stripe_size, int stripe_offset,
790 int stripe_count, int stripe_pattern, char *pool_name)
792 const struct llapi_stripe_param param = {
793 .lsp_stripe_size = stripe_size,
794 .lsp_stripe_count = stripe_count,
795 .lsp_stripe_pattern = stripe_pattern,
796 .lsp_stripe_offset = stripe_offset,
797 .lsp_pool = pool_name
799 return llapi_file_open_param(name, flags, mode, ¶m);
802 int llapi_file_open(const char *name, int flags, int mode,
803 unsigned long long stripe_size, int stripe_offset,
804 int stripe_count, int stripe_pattern)
806 return llapi_file_open_pool(name, flags, mode, stripe_size,
807 stripe_offset, stripe_count,
808 stripe_pattern, NULL);
811 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
812 __u32 flags, char *foreign_lov)
815 struct lov_foreign_md *lfm;
818 if (foreign_lov == NULL) {
819 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
820 "foreign LOV EA content must be provided");
824 len = strlen(foreign_lov);
825 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
827 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
828 "foreign LOV EA size %zu (must be 0 < len < %zu)",
829 len, XATTR_SIZE_MAX -
830 offsetof(struct lov_foreign_md, lfm_value));
834 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
836 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
837 "failed to allocate lov_foreign_md");
841 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
848 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
849 lfm->lfm_length = len;
850 lfm->lfm_type = type;
851 lfm->lfm_flags = flags;
852 memcpy(lfm->lfm_value, foreign_lov, len);
854 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
855 char *errmsg = "stripe already set";
856 char fsname[MAX_OBD_NAME + 1] = { 0 };
859 if (errno != EEXIST && errno != EALREADY)
860 errmsg = strerror(errno);
862 llapi_err_noerrno(LLAPI_MSG_ERROR,
863 "setstripe error for '%s': %s", name, errmsg);
865 /* Make sure we are on a Lustre file system */
866 if (rc == -ENOTTY && llapi_search_fsname(name, fsname))
867 llapi_error(LLAPI_MSG_ERROR, rc,
868 "'%s' is not on a Lustre filesystem",
881 int llapi_file_create(const char *name, unsigned long long stripe_size,
882 int stripe_offset, int stripe_count, int stripe_pattern)
886 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
887 stripe_offset, stripe_count, stripe_pattern,
896 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
897 int stripe_offset, int stripe_count,
898 int stripe_pattern, char *pool_name)
902 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
903 stripe_offset, stripe_count, stripe_pattern,
912 static int verify_dir_param(const char *name,
913 const struct llapi_stripe_param *param)
915 char fsname[MAX_OBD_NAME + 1] = { 0 };
916 char *pool_name = param->lsp_pool;
919 /* Make sure we are on a Lustre file system */
920 rc = llapi_search_fsname(name, fsname);
922 llapi_error(LLAPI_MSG_ERROR, rc,
923 "'%s' is not on a Lustre filesystem",
928 /* Check if the stripe pattern is sane. */
929 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
930 param->lsp_stripe_count,
931 param->lsp_stripe_pattern);
935 /* Make sure we have a good pool */
936 if (pool_name != NULL) {
937 /* in case user gives the full pool name <fsname>.<poolname>,
938 * strip the fsname */
939 char *ptr = strchr(pool_name, '.');
943 if (strcmp(pool_name, fsname) != 0) {
945 llapi_err_noerrno(LLAPI_MSG_ERROR,
946 "Pool '%s' is not on filesystem '%s'",
953 /* Make sure the pool exists and is non-empty */
954 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
956 char *err = rc == 0 ? "has no OSTs" : "does not exist";
958 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
959 fsname, pool_name, err);
964 /* sanity check of target list */
965 if (param->lsp_is_specific) {
966 char mdtname[MAX_OBD_NAME + 64];
970 for (i = 0; i < param->lsp_stripe_count; i++) {
971 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
972 fsname, param->lsp_tgts[i]);
973 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
978 llapi_error(LLAPI_MSG_ERROR, rc,
979 "%s: cannot find MDT %s in %s",
986 /* Make sure stripe offset is in MDT list. */
987 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
991 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
992 "%s: stripe offset '%d' is not in the "
994 __func__, param->lsp_stripe_offset);
1002 static inline void param2lmu(struct lmv_user_md *lmu,
1003 const struct llapi_stripe_param *param)
1005 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
1007 lmu->lum_stripe_count = param->lsp_stripe_count;
1008 lmu->lum_stripe_offset = param->lsp_stripe_offset;
1009 lmu->lum_hash_type = param->lsp_stripe_pattern;
1010 if (param->lsp_pool != NULL)
1011 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
1012 if (param->lsp_is_specific) {
1015 for (i = 0; i < param->lsp_stripe_count; i++)
1016 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
1020 int llapi_dir_set_default_lmv(const char *name,
1021 const struct llapi_stripe_param *param)
1023 struct lmv_user_md lmu = { 0 };
1027 rc = verify_dir_param(name, param);
1031 /* TODO: default lmv doesn't support specific targets yet */
1032 if (param->lsp_is_specific)
1035 param2lmu(&lmu, param);
1037 fd = open(name, O_DIRECTORY | O_RDONLY);
1040 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1044 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
1046 char *errmsg = "stripe already set";
1048 if (errno != EEXIST && errno != EALREADY)
1049 errmsg = strerror(errno);
1051 llapi_err_noerrno(LLAPI_MSG_ERROR,
1052 "default dirstripe error on '%s': %s",
1059 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
1060 int stripe_count, int stripe_pattern,
1061 const char *pool_name)
1063 const struct llapi_stripe_param param = {
1064 .lsp_stripe_count = stripe_count,
1065 .lsp_stripe_offset = stripe_offset,
1066 .lsp_stripe_pattern = stripe_pattern,
1067 .lsp_pool = (char *)pool_name
1070 return llapi_dir_set_default_lmv(name, ¶m);
1074 * Create a Lustre directory.
1076 * \param name the name of the directory to be created
1077 * \param mode permission of the file if it is created, see mode in open(2)
1078 * \param param stripe pattern of the newly created directory
1080 * \retval 0 on success
1081 * \retval negative errno on failure
1083 int llapi_dir_create(const char *name, mode_t mode,
1084 const struct llapi_stripe_param *param)
1086 struct lmv_user_md *lmu = NULL;
1088 struct obd_ioctl_data data = { 0 };
1091 char *dirpath = NULL;
1092 char *namepath = NULL;
1097 rc = verify_dir_param(name, param);
1101 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1102 param->lsp_is_specific ?
1103 LMV_USER_MAGIC_SPECIFIC :
1106 lmu = calloc(1, lmu_size);
1110 dirpath = strdup(name);
1116 namepath = strdup(name);
1123 param2lmu(lmu, param);
1125 filename = basename(namepath);
1126 dir = dirname(dirpath);
1128 data.ioc_inlbuf1 = (char *)filename;
1129 data.ioc_inllen1 = strlen(filename) + 1;
1130 data.ioc_inlbuf2 = (char *)lmu;
1131 data.ioc_inllen2 = lmu_size;
1132 data.ioc_type = mode;
1133 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1135 llapi_error(LLAPI_MSG_ERROR, rc,
1136 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1141 fd = open(dir, O_DIRECTORY | O_RDONLY);
1144 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1148 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1149 char *errmsg = "stripe already set";
1152 if (errno != EEXIST && errno != EALREADY)
1153 errmsg = strerror(errno);
1155 llapi_err_noerrno(LLAPI_MSG_ERROR,
1156 "dirstripe error on '%s': %s", name, errmsg);
1167 * Create a foreign directory.
1169 * \param name the name of the directory to be created
1170 * \param mode permission of the file if it is created, see mode in open(2)
1171 * \param type foreign type to be set in LMV EA
1172 * \param flags foreign flags to be set in LMV EA
1173 * \param value foreign pattern to be set in LMV EA
1175 * \retval 0 on success
1176 * \retval negative errno on failure
1178 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1179 __u32 flags, const char *value)
1181 struct lmv_foreign_md *lfm = NULL;
1182 size_t lfm_size, len;
1183 struct obd_ioctl_data data = { 0 };
1186 char *dirpath = NULL;
1187 char *namepath = NULL;
1192 len = strlen(value);
1193 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1196 llapi_error(LLAPI_MSG_ERROR, rc, "invalid LOV EA length %zu "
1197 "(must be 0 < len < %zu)\n", len,
1199 offsetof(struct lmv_foreign_md, lfm_value));
1202 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1203 lfm = calloc(1, lfm_size);
1207 dirpath = strdup(name);
1213 namepath = strdup(name);
1220 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1221 lfm->lfm_length = len;
1222 lfm->lfm_type = type;
1223 lfm->lfm_flags = flags;
1224 memcpy(lfm->lfm_value, value, len);
1226 filename = basename(namepath);
1227 dir = dirname(dirpath);
1229 data.ioc_inlbuf1 = (char *)filename;
1230 data.ioc_inllen1 = strlen(filename) + 1;
1231 data.ioc_inlbuf2 = (char *)lfm;
1232 data.ioc_inllen2 = lfm_size;
1233 data.ioc_type = mode;
1234 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1236 llapi_error(LLAPI_MSG_ERROR, rc,
1237 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1242 fd = open(dir, O_DIRECTORY | O_RDONLY);
1245 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1249 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1250 char *errmsg = "stripe already set";
1253 if (errno != EEXIST && errno != EALREADY)
1254 errmsg = strerror(errno);
1256 llapi_err_noerrno(LLAPI_MSG_ERROR,
1257 "dirstripe error on '%s': %s", name, errmsg);
1267 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1268 int stripe_count, int stripe_pattern,
1269 const char *pool_name)
1271 const struct llapi_stripe_param param = {
1272 .lsp_stripe_count = stripe_count,
1273 .lsp_stripe_offset = stripe_offset,
1274 .lsp_stripe_pattern = stripe_pattern,
1275 .lsp_pool = (char *)pool_name
1278 return llapi_dir_create(name, mode, ¶m);
1281 int llapi_direntry_remove(char *dname)
1283 char *dirpath = NULL;
1284 char *namepath = NULL;
1290 dirpath = strdup(dname);
1291 namepath = strdup(dname);
1292 if (!dirpath || !namepath)
1295 filename = basename(namepath);
1297 dir = dirname(dirpath);
1299 fd = open(dir, O_DIRECTORY | O_RDONLY);
1302 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
1307 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
1308 char *errmsg = strerror(errno);
1309 llapi_err_noerrno(LLAPI_MSG_ERROR,
1310 "error on ioctl %#jx for '%s' (%d): %s",
1311 (uintmax_t)LL_IOC_LMV_SETSTRIPE, filename,
1323 * Find the fsname, the full path, and/or an open fd.
1324 * Either the fsname or path must not be NULL
1326 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1329 char buf[PATH_MAX], mntdir[PATH_MAX];
1330 char *ptr, *ptr_end;
1332 int idx = 0, len = 0, mntlen, fd;
1335 /* get the mount point */
1336 fp = setmntent(PROC_MOUNTS, "r");
1339 llapi_error(LLAPI_MSG_ERROR, rc,
1340 "cannot retrieve filesystem mount point");
1344 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1347 if (!llapi_is_lustre_mnt(&mnt))
1350 if ((want & WANT_INDEX) && (idx++ != index))
1353 mntlen = strlen(mnt.mnt_dir);
1354 ptr = strchr(mnt.mnt_fsname, '/');
1355 while (ptr && *ptr == '/')
1357 /* thanks to the call to llapi_is_lustre_mnt() above,
1358 * we are sure that mnt.mnt_fsname contains ":/",
1359 * so ptr should never be NULL */
1363 while (*ptr_end != '/' && *ptr_end != '\0')
1366 /* Check the fsname for a match, if given */
1367 if (!(want & WANT_FSNAME) && fsname != NULL &&
1368 (strlen(fsname) > 0) &&
1369 (strncmp(ptr, fsname, ptr_end - ptr) != 0))
1372 /* If the path isn't set return the first one we find */
1373 if (path == NULL || strlen(path) == 0) {
1374 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1375 mntdir[strlen(mnt.mnt_dir)] = '\0';
1376 if ((want & WANT_FSNAME) && fsname != NULL) {
1377 strncpy(fsname, ptr, ptr_end - ptr);
1378 fsname[ptr_end - ptr] = '\0';
1382 /* Otherwise find the longest matching path */
1383 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1384 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1385 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir));
1386 mntdir[strlen(mnt.mnt_dir)] = '\0';
1388 if ((want & WANT_FSNAME) && fsname != NULL) {
1389 strncpy(fsname, ptr, ptr_end - ptr);
1390 fsname[ptr_end - ptr] = '\0';
1399 if ((want & WANT_PATH) && path != NULL) {
1400 strncpy(path, mntdir, PATH_MAX);
1401 path[strlen(mntdir)] = '\0';
1403 if (want & WANT_FD) {
1404 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1407 llapi_error(LLAPI_MSG_ERROR, rc,
1408 "cannot open '%s': %s", mntdir,
1415 } else if (want & WANT_ERROR)
1416 llapi_err_noerrno(LLAPI_MSG_ERROR,
1417 "'%s' not on a mounted Lustre filesystem",
1418 (want & WANT_PATH) ? fsname : path);
1423 * search lustre mounts
1425 * Calling this function will return to the user the mount point, mntdir, and
1426 * the file system name, fsname, if the user passed a buffer to this routine.
1428 * The user inputs are pathname and index. If the pathname is supplied then
1429 * the value of the index will be ignored. The pathname will return data if
1430 * the pathname is located on a lustre mount. Index is used to pick which
1431 * mount point you want in the case of multiple mounted lustre file systems.
1432 * See function lfs_osts in lfs.c for an example of the index use.
1434 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1437 int want = WANT_PATH, idx = -1;
1439 if (!pathname || pathname[0] == '\0') {
1443 strcpy(mntdir, pathname);
1446 want |= WANT_FSNAME;
1447 return get_root_path(want, fsname, NULL, mntdir, idx);
1450 /* Given a path, find the corresponding Lustre fsname */
1451 int llapi_search_fsname(const char *pathname, char *fsname)
1456 path = realpath(pathname, NULL);
1458 char tmp[PATH_MAX - 1];
1464 if (pathname[0] != '/') {
1465 /* Need an absolute path, but realpath() only works for
1466 * pathnames that actually exist. We go through the
1467 * extra hurdle of dirname(getcwd() + pathname) in
1468 * case the relative pathname contains ".." in it. */
1469 char realpath[PATH_MAX - 1];
1471 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1473 llapi_error(LLAPI_MSG_ERROR, rc,
1474 "cannot get current working directory");
1478 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1479 if (rc >= sizeof(tmp)) {
1481 llapi_error(LLAPI_MSG_ERROR, rc,
1482 "invalid parent path '%s'",
1488 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1489 if (rc >= sizeof(buf)) {
1491 llapi_error(LLAPI_MSG_ERROR, rc,
1492 "invalid path '%s'", pathname);
1495 path = realpath(buf, NULL);
1497 ptr = strrchr(buf, '/');
1499 llapi_error(LLAPI_MSG_ERROR |
1500 LLAPI_MSG_NO_ERRNO, 0,
1501 "cannot resolve path '%s'",
1506 path = realpath(buf, NULL);
1509 llapi_error(LLAPI_MSG_ERROR, rc,
1510 "cannot resolve path '%s'",
1516 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1521 int llapi_search_rootpath(char *pathname, const char *fsname)
1523 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1526 int llapi_getname(const char *path, char *buf, size_t size)
1528 struct obd_uuid uuid_buf;
1529 char *uuid = uuid_buf.uuid;
1531 int rc, len, fsname_len;
1533 memset(&uuid_buf, 0, sizeof(uuid_buf));
1534 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1539 * We want to turn testfs-clilov-ffff88002738bc00 into
1540 * testfs-ffff88002738bc00 in a portable way that doesn't depend
1541 * on what is after "-clilov-" as it may change in the future.
1542 * Unfortunately, the "fsname" part may contain a dash, so we
1543 * can't just skip to the first dash, and the "instance" may be a
1544 * UUID in the future, so we can't necessarily go to the last dash.
1546 cfg_instance = strstr(uuid, "-clilov-");
1550 fsname_len = cfg_instance - uuid;
1551 cfg_instance += strlen("-clilov-");
1552 len = snprintf(buf, size, "%.*s-%s", fsname_len, uuid, cfg_instance);
1561 * Get the list of pool members.
1562 * \param poolname string of format \<fsname\>.\<poolname\>
1563 * \param members caller-allocated array of char*
1564 * \param list_size size of the members array
1565 * \param buffer caller-allocated buffer for storing OST names
1566 * \param buffer_size size of the buffer
1568 * \return number of members retrieved for this pool
1569 * \retval -error failure
1571 int llapi_get_poolmembers(const char *poolname, char **members,
1572 int list_size, char *buffer, int buffer_size)
1574 char fsname[PATH_MAX];
1583 /* name is FSNAME.POOLNAME */
1584 if (strlen(poolname) >= sizeof(fsname))
1587 snprintf(fsname, sizeof(fsname), "%s", poolname);
1588 pool = strchr(fsname, '.');
1595 rc = poolpath(&pathname, fsname, NULL);
1597 llapi_error(LLAPI_MSG_ERROR, rc,
1598 "Lustre filesystem '%s' not found",
1603 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1604 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1605 cfs_free_param_data(&pathname);
1606 if (rc >= sizeof(buf))
1608 fd = fopen(buf, "r");
1611 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1616 while (fgets(buf, sizeof(buf), fd) != NULL) {
1617 if (nb_entries >= list_size) {
1621 buf[sizeof(buf) - 1] = '\0';
1623 tmp = strchr(buf, '\n');
1626 if (used + strlen(buf) + 1 > buffer_size) {
1631 strcpy(buffer + used, buf);
1632 members[nb_entries] = buffer + used;
1633 used += strlen(buf) + 1;
1643 * Get the list of pools in a filesystem.
1644 * \param name filesystem name or path
1645 * \param poollist caller-allocated array of char*
1646 * \param list_size size of the poollist array
1647 * \param buffer caller-allocated buffer for storing pool names
1648 * \param buffer_size size of the buffer
1650 * \return number of pools retrieved for this filesystem
1651 * \retval -error failure
1653 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1654 char *buffer, int buffer_size)
1656 char rname[PATH_MAX];
1661 struct dirent *pool;
1663 unsigned int nb_entries = 0;
1664 unsigned int used = 0;
1667 /* initialize output array */
1668 for (i = 0; i < list_size; i++)
1671 /* is name a pathname ? */
1672 ptr = strchr(name, '/');
1674 /* only absolute pathname is supported */
1678 if (!realpath(name, rname)) {
1680 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1685 fsname = strdup(rname);
1689 rc = poolpath(&pathname, NULL, rname);
1691 /* name is FSNAME */
1692 fsname = strdup(name);
1695 rc = poolpath(&pathname, fsname, NULL);
1698 llapi_error(LLAPI_MSG_ERROR, rc,
1699 "Lustre filesystem '%s' not found", name);
1703 dir = opendir(pathname.gl_pathv[0]);
1706 llapi_error(LLAPI_MSG_ERROR, rc,
1707 "Could not open pool list for '%s'",
1714 pool = readdir(dir);
1720 /* ignore . and .. */
1721 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1724 /* check output bounds */
1725 if (nb_entries >= list_size) {
1727 goto free_dir_no_msg;
1730 /* +2 for '.' and final '\0' */
1731 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1734 goto free_dir_no_msg;
1737 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1738 poollist[nb_entries] = buffer + used;
1739 used += strlen(pool->d_name) + strlen(fsname) + 2;
1745 llapi_error(LLAPI_MSG_ERROR, rc,
1746 "Error reading pool list for '%s'", name);
1748 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1753 cfs_free_param_data(&pathname);
1756 return rc != 0 ? rc : nb_entries;
1759 /* wrapper for lfs.c and obd.c */
1760 int llapi_poollist(const char *name)
1762 /* list of pool names (assume that pool count is smaller
1764 char **list, *buffer = NULL, *fsname = (char *)name;
1765 char *poolname = NULL, *tmp = NULL, data[16];
1766 enum param_filter type = FILTER_BY_PATH;
1767 int obdcount, bufsize, rc, nb, i;
1772 if (name[0] != '/') {
1773 fsname = strdup(name);
1777 poolname = strchr(fsname, '.');
1780 type = FILTER_BY_FS_NAME;
1783 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1784 data, sizeof(data));
1787 obdcount = atoi(data);
1789 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1790 * and also an array to store the pointers for all that
1791 * allocated space. */
1793 bufsize = sizeof(struct obd_uuid) * obdcount;
1794 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1795 if (buffer == NULL) {
1799 list = (char **) (buffer + bufsize);
1802 /* name is a path or fsname */
1803 nb = llapi_get_poollist(name, list, obdcount,
1806 /* name is a pool name (<fsname>.<poolname>) */
1807 nb = llapi_get_poolmembers(name, list, obdcount,
1811 if (nb == -EOVERFLOW) {
1814 goto retry_get_pools;
1817 for (i = 0; i < nb; i++)
1818 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1819 rc = (nb < 0 ? nb : 0);
1823 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1828 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1829 void *data, struct dirent64 *de);
1831 #define OBD_NOT_FOUND (-1)
1833 static void find_param_fini(struct find_param *param)
1835 if (param->fp_migrate)
1838 if (param->fp_obd_indexes) {
1839 free(param->fp_obd_indexes);
1840 param->fp_obd_indexes = NULL;
1843 if (param->fp_lmd) {
1844 free(param->fp_lmd);
1845 param->fp_lmd = NULL;
1848 if (param->fp_lmv_md) {
1849 free(param->fp_lmv_md);
1850 param->fp_lmv_md = NULL;
1854 static int common_param_init(struct find_param *param, char *path)
1856 int lum_size = get_mds_md_size(path);
1861 /* migrate has fp_lmv_md initialized outside */
1862 if (param->fp_migrate)
1865 if (lum_size < PATH_MAX + 1)
1866 lum_size = PATH_MAX + 1;
1868 param->fp_lum_size = lum_size;
1869 param->fp_lmd = calloc(1, sizeof(lstat_t) + lum_size);
1870 if (param->fp_lmd == NULL) {
1871 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1872 "error: allocation of %zu bytes for ioctl",
1873 sizeof(lstat_t) + param->fp_lum_size);
1877 param->fp_lmv_stripe_count = 256;
1878 param->fp_lmv_md = calloc(1,
1879 lmv_user_md_size(param->fp_lmv_stripe_count,
1880 LMV_USER_MAGIC_SPECIFIC));
1881 if (param->fp_lmv_md == NULL) {
1882 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1883 "error: allocation of %d bytes for ioctl",
1884 lmv_user_md_size(param->fp_lmv_stripe_count,
1885 LMV_USER_MAGIC_SPECIFIC));
1886 find_param_fini(param);
1890 param->fp_got_uuids = 0;
1891 param->fp_obd_indexes = NULL;
1892 param->fp_obd_index = OBD_NOT_FOUND;
1893 param->fp_mdt_index = OBD_NOT_FOUND;
1897 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1898 struct dirent64 *de)
1900 struct find_param *param = data;
1906 /* set errno upon failure */
1907 static DIR *opendir_parent(const char *path)
1913 path_copy = strdup(path);
1914 if (path_copy == NULL)
1917 parent_path = dirname(path_copy);
1918 parent = opendir(parent_path);
1924 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1929 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1930 if (param->fp_get_default_lmv)
1931 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1933 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1935 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1936 if (errno == E2BIG && ret != 0) {
1940 /* if foreign LMV case, fake stripes number */
1941 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
1942 struct lmv_foreign_md *lfm;
1944 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1945 if (lfm->lfm_length < XATTR_SIZE_MAX -
1946 offsetof(typeof(*lfm), lfm_value)) {
1947 uint32_t size = lfm->lfm_length +
1948 offsetof(typeof(*lfm), lfm_value);
1950 stripe_count = lmv_foreign_to_md_stripes(size);
1952 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1953 "error: invalid %d foreign size "
1954 "returned from ioctl",
1959 stripe_count = param->fp_lmv_md->lum_stripe_count;
1961 if (stripe_count <= param->fp_lmv_stripe_count)
1964 free(param->fp_lmv_md);
1965 param->fp_lmv_stripe_count = stripe_count;
1966 lmv_size = lmv_user_md_size(stripe_count,
1967 LMV_USER_MAGIC_SPECIFIC);
1968 param->fp_lmv_md = malloc(lmv_size);
1969 if (param->fp_lmv_md == NULL) {
1970 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1971 "error: allocation of %d bytes for ioctl",
1972 lmv_user_md_size(param->fp_lmv_stripe_count,
1973 LMV_USER_MAGIC_SPECIFIC));
1981 int get_lmd_info_fd(char *path, int parent_fd, int dir_fd,
1982 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1984 struct lov_user_mds_data *lmd = lmdbuf;
1985 lstat_t *st = &lmd->lmd_st;
1988 if (parent_fd < 0 && dir_fd < 0)
1990 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1994 /* LL_IOC_MDC_GETINFO operates on the current directory inode
1995 * and returns struct lov_user_mds_data, while
1996 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
1998 ret = ioctl(dir_fd, type == GET_LMD_INFO ? LL_IOC_MDC_GETINFO :
1999 LL_IOC_LOV_GETSTRIPE,
2001 } else if (parent_fd >= 0) {
2002 char *fname = strrchr(path, '/');
2004 /* IOC_MDC_GETFILEINFO takes as input the filename (relative to
2005 * the parent directory) and returns struct lov_user_mds_data,
2006 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2008 * This avoids opening, locking, and closing each file on the
2009 * client if that is not needed. Multiple of these ioctl() can
2010 * be done on the parent dir with a single open for all
2011 * files in that directory, and it also doesn't pollute the
2012 * client dcache with millions of dentries when traversing
2013 * a large filesystem.
2015 fname = (fname == NULL ? path : fname + 1);
2017 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2020 else if (ret >= lmdlen || ret++ == 0)
2023 ret = ioctl(parent_fd, type == GET_LMD_INFO ?
2024 IOC_MDC_GETFILEINFO :
2025 IOC_MDC_GETFILESTRIPE, lmdbuf);
2028 if (ret && type == GET_LMD_INFO) {
2029 if (errno == ENOTTY) {
2030 /* ioctl is not supported, it is not a lustre fs.
2031 * Do the regular lstat(2) instead.
2033 ret = lstat_f(path, st);
2036 llapi_error(LLAPI_MSG_ERROR, ret,
2037 "error: %s: lstat failed for %s",
2040 } else if (errno == ENOENT) {
2042 llapi_error(LLAPI_MSG_WARN, ret,
2043 "warning: %s does not exist", path);
2044 } else if (errno != EISDIR && errno != ENODATA) {
2046 llapi_error(LLAPI_MSG_ERROR, ret,
2047 "%s ioctl failed for %s.",
2048 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2049 "IOC_MDC_GETFILEINFO", path);
2056 static int get_lmd_info(char *path, DIR *parent, DIR *dir, void *lmdbuf,
2057 int lmdlen, enum get_lmd_info_type type)
2063 parent_fd = dirfd(parent);
2065 dir_fd = dirfd(dir);
2067 return get_lmd_info_fd(path, parent_fd, dir_fd, lmdbuf, lmdlen, type);
2070 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
2071 semantic_func_t sem_init,
2072 semantic_func_t sem_fini, void *data,
2073 struct dirent64 *de)
2075 struct find_param *param = (struct find_param *)data;
2076 struct dirent64 *dent;
2084 if (!d && errno != ENOTDIR) {
2086 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2089 } else if (!d && !parent) {
2090 /* ENOTDIR. Open the parent dir. */
2091 p = opendir_parent(path);
2098 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
2104 while ((dent = readdir64(d)) != NULL) {
2107 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2111 if ((len + dent->d_reclen + 2) > size) {
2112 llapi_err_noerrno(LLAPI_MSG_ERROR,
2113 "error: %s: string buffer too small",
2118 strcat(path, dent->d_name);
2120 if (dent->d_type == DT_UNKNOWN) {
2121 lstat_t *st = ¶m->fp_lmd->lmd_st;
2123 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
2124 param->fp_lum_size, GET_LMD_INFO);
2126 dent->d_type = IFTODT(st->st_mode);
2133 switch (dent->d_type) {
2135 llapi_err_noerrno(LLAPI_MSG_ERROR,
2136 "error: %s: '%s' is UNKNOWN type %d",
2137 __func__, dent->d_name, dent->d_type);
2140 rc = llapi_semantic_traverse(path, size, d, sem_init,
2141 sem_fini, data, dent);
2142 if (rc != 0 && ret == 0)
2148 rc = sem_init(path, d, NULL, data, dent);
2149 if (rc < 0 && ret == 0) {
2154 if (sem_fini && rc == 0)
2155 sem_fini(path, d, NULL, data, dent);
2163 sem_fini(path, parent, &d, data, de);
2172 static int param_callback(char *path, semantic_func_t sem_init,
2173 semantic_func_t sem_fini, struct find_param *param)
2175 int ret, len = strlen(path);
2178 if (len > PATH_MAX) {
2180 llapi_error(LLAPI_MSG_ERROR, ret,
2181 "Path name '%s' is too long", path);
2185 buf = (char *)malloc(PATH_MAX + 1);
2189 snprintf(buf, PATH_MAX + 1, "%s", path);
2190 ret = common_param_init(param, buf);
2194 param->fp_depth = 0;
2196 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
2197 sem_fini, param, NULL);
2199 find_param_fini(param);
2201 return ret < 0 ? ret : 0;
2204 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2206 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
2210 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2215 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2217 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2220 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2225 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2229 fd = open(path, O_RDONLY | O_NONBLOCK);
2232 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
2236 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2242 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2246 fd = open(path, O_RDONLY | O_NONBLOCK);
2249 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2253 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2265 * If uuidp is NULL, return the number of available obd uuids.
2266 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2267 * there are more OSTs than allocated to uuidp, then an error is returned with
2268 * the ost_count set to number of available obd uuids.
2270 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2271 int *ost_count, enum tgt_type type)
2273 char buf[PATH_MAX], format[32];
2274 int rc = 0, index = 0;
2275 struct obd_uuid name;
2279 /* Get the lov name */
2280 if (type == LOV_TYPE)
2281 rc = llapi_file_fget_lov_uuid(fd, &name);
2283 rc = llapi_file_fget_lmv_uuid(fd, &name);
2287 /* Now get the ost uuids */
2288 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2289 FILTER_BY_EXACT, "target_obd", ¶m);
2293 fp = fopen(param.gl_pathv[0], "r");
2296 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2301 snprintf(format, sizeof(format),
2302 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2303 while (fgets(buf, sizeof(buf), fp) != NULL) {
2304 if (uuidp && (index < *ost_count)) {
2305 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2313 if (uuidp && (index > *ost_count))
2318 cfs_free_param_data(¶m);
2322 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2324 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2327 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2332 root = open(mnt, O_RDONLY | O_DIRECTORY);
2335 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2340 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2348 /* Check if user specified value matches a real uuid. Ignore _UUID,
2349 * -osc-4ba41334, other trailing gunk in comparison.
2350 * @param real_uuid ends in "_UUID"
2351 * @param search_uuid may or may not end in "_UUID"
2353 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2355 int cmplen = strlen(real_uuid);
2356 int searchlen = strlen(search_uuid);
2358 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2360 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2363 /* The UUIDs may legitimately be different lengths, if
2364 * the system was upgraded from an older version. */
2365 if (cmplen != searchlen)
2368 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2371 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2372 * returned in param->fp_obd_index */
2373 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2375 struct obd_uuid obd_uuid;
2382 if (param->fp_got_uuids)
2385 /* Get the lov/lmv name */
2386 if (param->fp_get_lmv)
2387 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2389 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2391 if (rc != -ENOTTY) {
2392 llapi_error(LLAPI_MSG_ERROR, rc,
2393 "error: can't get %s name: %s",
2394 param->fp_get_lmv ? "lmv" : "lov",
2402 param->fp_got_uuids = 1;
2404 /* Now get the ost uuids */
2405 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2406 obd_uuid.uuid, FILTER_BY_EXACT,
2407 "target_obd", ¶m_data);
2411 fp = fopen(param_data.gl_pathv[0], "r");
2414 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2415 param_data.gl_pathv[0]);
2419 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2420 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2421 param->fp_get_lmv ? "MDTS" : "OBDS");
2423 snprintf(format, sizeof(format),
2424 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2425 while (fgets(buf, sizeof(buf), fp) != NULL) {
2428 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2431 if (param->fp_obd_uuid) {
2432 if (llapi_uuid_match(obd_uuid.uuid,
2433 param->fp_obd_uuid->uuid)) {
2434 param->fp_obd_index = index;
2437 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2438 /* Print everything */
2439 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2442 param->fp_obds_printed = 1;
2446 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2447 llapi_err_noerrno(LLAPI_MSG_ERROR,
2448 "error: %s: unknown obduuid: %s",
2449 __func__, param->fp_obd_uuid->uuid);
2453 cfs_free_param_data(¶m_data);
2457 /* In this case, param->fp_obd_uuid will be an array of obduuids and
2458 * obd index for all these obduuids will be returned in
2459 * param->fp_obd_indexes */
2460 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2461 int num_obds, int **obdindexes, int *obdindex,
2464 int ret, obdcount, obd_valid = 0, obdnum;
2466 struct obd_uuid *uuids = NULL;
2470 if (type == LOV_TYPE)
2471 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2473 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2477 obdcount = atoi(buf);
2478 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2483 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2485 if (ret == -EOVERFLOW) {
2486 struct obd_uuid *uuids_temp;
2488 uuids_temp = realloc(uuids, obdcount *
2489 sizeof(struct obd_uuid));
2490 if (uuids_temp != NULL) {
2492 goto retry_get_uuids;
2497 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2501 indexes = malloc(num_obds * sizeof(*obdindex));
2502 if (indexes == NULL) {
2507 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2510 /* The user may have specified a simple index */
2511 i = strtol(obduuids[obdnum].uuid, &end, 0);
2512 if (end && *end == '\0' && i < obdcount) {
2513 indexes[obdnum] = i;
2516 for (i = 0; i < obdcount; i++) {
2517 if (llapi_uuid_match(uuids[i].uuid,
2518 obduuids[obdnum].uuid)) {
2519 indexes[obdnum] = i;
2525 if (i >= obdcount) {
2526 indexes[obdnum] = OBD_NOT_FOUND;
2527 llapi_err_noerrno(LLAPI_MSG_ERROR,
2528 "invalid obduuid '%s'",
2529 obduuids[obdnum].uuid);
2535 *obdindex = OBD_NOT_FOUND;
2537 *obdindex = obd_valid;
2539 *obdindexes = indexes;
2547 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2551 if (param->fp_mdt_uuid) {
2552 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2554 ¶m->fp_mdt_indexes,
2555 ¶m->fp_mdt_index, LMV_TYPE);
2560 if (param->fp_obd_uuid) {
2561 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2563 ¶m->fp_obd_indexes,
2564 ¶m->fp_obd_index, LOV_TYPE);
2569 param->fp_got_uuids = 1;
2574 int llapi_ostlist(char *path, struct find_param *param)
2579 fd = open(path, O_RDONLY | O_DIRECTORY);
2583 ret = setup_obd_uuid(fd, path, param);
2590 * Tries to determine the default stripe attributes for a given filesystem. The
2591 * filesystem to check should be specified by fsname, or will be determined
2594 static int sattr_get_defaults(const char *const fsname,
2595 unsigned int *scount,
2596 unsigned int *ssize,
2597 unsigned int *soffset)
2603 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2604 "stripecount", val, sizeof(val));
2607 *scount = atoi(val);
2611 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2612 "stripesize", val, sizeof(val));
2619 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2620 "stripeoffset", val, sizeof(val));
2623 *soffset = atoi(val);
2630 * Tries to gather the default stripe attributes for a given filesystem. If
2631 * the attributes can be determined, they are cached for easy retreival the
2632 * next time they are needed. Only a single filesystem's attributes are
2635 int sattr_cache_get_defaults(const char *const fsname,
2636 const char *const pathname, unsigned int *scount,
2637 unsigned int *ssize, unsigned int *soffset)
2640 char fsname[PATH_MAX + 1];
2641 unsigned int stripecount;
2642 unsigned int stripesize;
2643 unsigned int stripeoffset;
2649 char fsname_buf[PATH_MAX + 1];
2650 unsigned int tmp[3];
2652 if (fsname == NULL) {
2653 rc = llapi_search_fsname(pathname, fsname_buf);
2657 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2660 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2662 * Ensure all 3 sattrs (count, size, and offset) are
2663 * successfully retrieved and stored in tmp before writing to
2666 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2670 cache.stripecount = tmp[0];
2671 cache.stripesize = tmp[1];
2672 cache.stripeoffset = tmp[2];
2673 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2677 *scount = cache.stripecount;
2679 *ssize = cache.stripesize;
2681 *soffset = cache.stripeoffset;
2686 static char *layout2name(__u32 layout_pattern)
2688 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2690 else if (layout_pattern == LOV_PATTERN_MDT)
2692 else if (layout_pattern == LOV_PATTERN_RAID0)
2694 else if (layout_pattern ==
2695 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2696 return "raid0,overstriped";
2701 enum lov_dump_flags {
2702 LDF_IS_DIR = 0x0001,
2703 LDF_IS_RAW = 0x0002,
2704 LDF_INDENT = 0x0004,
2705 LDF_SKIP_OBJS = 0x0008,
2709 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2710 struct lov_user_ost_data_v1 *objects,
2711 enum llapi_layout_verbose verbose,
2712 int depth, char *pool_name,
2713 enum lov_dump_flags flags)
2715 bool is_dir = flags & LDF_IS_DIR;
2716 bool is_raw = flags & LDF_IS_RAW;
2717 bool indent = flags & LDF_INDENT;
2718 bool yaml = flags & LDF_YAML;
2719 bool skip_objs = flags & LDF_SKIP_OBJS;
2720 char *prefix = is_dir ? "" : "lmm_";
2721 char *separator = "";
2722 char *space = indent ? " " : "";
2725 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2726 lmm_oi_set_seq(&lum->lmm_oi, 0);
2727 if (!indent && (verbose & VERBOSE_DETAIL))
2728 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2731 if (!yaml && !indent && depth && path &&
2732 ((verbose != VERBOSE_OBJID) || !is_dir))
2733 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2735 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2736 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2737 space, prefix, lum->lmm_magic);
2738 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2740 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2741 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2743 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2745 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2750 if (verbose & ~VERBOSE_DFID)
2751 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2753 /* This needs a bit of hand-holding since old 1.x lmm_oi
2754 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2755 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2756 * a real FID. Ideally the 2.x code would have stored this
2757 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2758 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2759 * worked properly (especially since IGIF FIDs use mds_inum as
2760 * the FID SEQ), but unfortunately that didn't happen.
2762 * Print it to look like an IGIF FID, even though the fields
2763 * are reversed on disk, so that it makes sense to userspace.
2765 * Don't use ostid_id() and ostid_seq(), since they assume the
2766 * oi_fid fields are in the right order. This is why there are
2767 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2769 * For newer layout types hopefully this will be a real FID. */
2770 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2771 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2772 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2773 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2774 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2776 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2779 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2783 if (verbose & VERBOSE_STRIPE_COUNT) {
2784 if (verbose & ~VERBOSE_STRIPE_COUNT)
2785 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2788 if (!is_raw && lum->lmm_stripe_count == 0 &&
2789 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2790 unsigned int scount;
2791 rc = sattr_cache_get_defaults(NULL, path,
2795 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2798 llapi_error(LLAPI_MSG_ERROR, rc,
2799 "Cannot determine default"
2802 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2803 lum->lmm_stripe_count ==
2804 (typeof(lum->lmm_stripe_count))(-1)
2805 ? -1 : lum->lmm_stripe_count);
2808 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2809 (__s16)lum->lmm_stripe_count);
2811 if (!yaml && is_dir)
2817 if (verbose & VERBOSE_STRIPE_SIZE) {
2818 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2819 if (verbose & ~VERBOSE_STRIPE_SIZE)
2820 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2822 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2824 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2827 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2829 llapi_error(LLAPI_MSG_ERROR, rc,
2830 "Cannot determine default"
2833 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2834 lum->lmm_stripe_size);
2836 if (!yaml && is_dir)
2842 if ((verbose & VERBOSE_PATTERN)) {
2843 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2844 if (verbose & ~VERBOSE_PATTERN)
2845 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2847 if (lov_pattern_supported(lum->lmm_pattern))
2848 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2849 layout2name(lum->lmm_pattern));
2851 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2852 separator = (!yaml && is_dir) ? " " : "\n";
2855 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2856 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2857 if (verbose & ~VERBOSE_GENERATION)
2858 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2860 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2861 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2865 if (verbose & VERBOSE_STRIPE_OFFSET) {
2866 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2867 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2868 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2870 if (is_dir || skip_objs)
2871 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2872 lum->lmm_stripe_offset ==
2873 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2874 lum->lmm_stripe_offset);
2875 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
2876 llapi_printf(LLAPI_MSG_NORMAL, "0");
2878 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2879 objects[0].l_ost_idx);
2880 if (!yaml && is_dir)
2886 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2887 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2888 if (verbose & ~VERBOSE_POOL)
2889 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2891 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2892 if (!yaml && is_dir)
2898 if (strlen(separator) != 0)
2899 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2902 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2903 struct lov_user_ost_data_v1 *objects,
2904 char *path, int obdindex, int depth,
2905 enum llapi_layout_verbose verbose,
2906 enum lov_dump_flags flags)
2908 bool is_dir = flags & LDF_IS_DIR;
2909 bool indent = flags & LDF_INDENT;
2910 bool skip_objs = flags & LDF_SKIP_OBJS;
2911 bool yaml = flags & LDF_YAML;
2912 bool obdstripe = obdindex == OBD_NOT_FOUND;
2915 if (!obdstripe && !skip_objs) {
2916 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2917 if (obdindex == objects[i].l_ost_idx) {
2927 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2930 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
2931 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2932 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
2933 char *space = " - ";
2936 llapi_printf(LLAPI_MSG_NORMAL,
2937 "%6slmm_objects:\n", " ");
2939 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2941 llapi_printf(LLAPI_MSG_NORMAL,
2942 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2944 for (i = 0; i < lum->lmm_stripe_count; i++) {
2945 int idx = objects[i].l_ost_idx;
2946 long long oid = ostid_id(&objects[i].l_ost_oi);
2947 long long gr = ostid_seq(&objects[i].l_ost_oi);
2949 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2953 struct lu_fid fid = { 0 };
2955 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2956 llapi_printf(LLAPI_MSG_NORMAL,
2957 "%sl_ost_idx: %d\n", space, idx);
2958 llapi_printf(LLAPI_MSG_NORMAL,
2959 "%8sl_fid: "DFID_NOBRACE"\n",
2961 } else if (indent) {
2962 struct lu_fid fid = { 0 };
2964 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2965 llapi_printf(LLAPI_MSG_NORMAL,
2966 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
2967 space, i, idx, PFID(&fid));
2970 sprintf(fmt, "%s%s%s\n",
2971 "\t%6u\t%14llu\t%#13llx\t",
2972 (fid_seq_is_rsvd(gr) ||
2973 fid_seq_is_mdt0(gr)) ?
2974 "%14llu" : "%#14llx", "%s");
2975 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2977 obdindex == idx ? " *" : "");
2981 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2984 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2985 char *path, int obdindex, int depth,
2986 enum llapi_layout_verbose verbose,
2987 enum lov_dump_flags flags)
2989 struct lmv_user_mds_data *objects = lum->lum_objects;
2990 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2991 char *separator = "";
2992 bool yaml = flags & LDF_YAML;
2993 bool obdstripe = false;
2996 if (obdindex != OBD_NOT_FOUND) {
2997 if (lum->lum_stripe_count == 0) {
2998 if (obdindex == lum->lum_stripe_offset)
3001 for (i = 0; i < lum->lum_stripe_count; i++) {
3002 if (obdindex == objects[i].lum_mds) {
3003 llapi_printf(LLAPI_MSG_NORMAL,
3018 /* show all information default */
3020 if (lum->lum_magic == LMV_USER_MAGIC)
3021 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3022 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3024 verbose = VERBOSE_OBJID;
3027 if (depth && path && ((verbose != VERBOSE_OBJID)))
3028 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3030 if (verbose & VERBOSE_STRIPE_COUNT) {
3031 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3032 if (verbose & ~VERBOSE_STRIPE_COUNT)
3033 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3034 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3035 (int)lum->lum_stripe_count);
3036 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3042 if (verbose & VERBOSE_STRIPE_OFFSET) {
3043 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3044 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3045 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3046 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3047 (int)lum->lum_stripe_offset);
3048 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3054 if (verbose & VERBOSE_HASH_TYPE) {
3055 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3056 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3058 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3059 if (verbose & ~VERBOSE_HASH_TYPE)
3060 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3061 if (type < LMV_HASH_TYPE_MAX)
3062 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3063 mdt_hash_name[type]);
3065 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3067 if (flags & LMV_HASH_FLAG_MIGRATION)
3068 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3069 if (flags & LMV_HASH_FLAG_DEAD)
3070 llapi_printf(LLAPI_MSG_NORMAL, ",dead");
3071 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3072 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3073 if (flags & LMV_HASH_FLAG_LOST_LMV)
3074 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3079 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3080 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3081 if (lum->lum_stripe_count > 0)
3082 llapi_printf(LLAPI_MSG_NORMAL,
3083 "mdtidx\t\t FID[seq:oid:ver]\n");
3084 for (i = 0; i < lum->lum_stripe_count; i++) {
3085 int idx = objects[i].lum_mds;
3086 struct lu_fid *fid = &objects[i].lum_fid;
3087 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3088 llapi_printf(LLAPI_MSG_NORMAL,
3089 "%6u\t\t "DFID"\t\t%s\n",
3091 obdindex == idx ? " *" : "");
3096 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3097 pool_name[0] != '\0') {
3098 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3099 if (verbose & ~VERBOSE_POOL)
3100 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3102 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3106 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3107 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3110 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3111 enum lov_dump_flags flags)
3113 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3114 int depth = param->fp_max_depth;
3115 enum llapi_layout_verbose verbose = param->fp_verbose;
3116 bool yaml = flags & LDF_YAML;
3118 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3119 !(flags & LDF_IS_DIR)) && !yaml)
3120 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3122 if (verbose & VERBOSE_DETAIL) {
3123 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3124 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3125 " ", comp_v1->lcm_magic);
3126 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3127 " ", comp_v1->lcm_size);
3128 if (flags & LDF_IS_DIR)
3129 llapi_printf(LLAPI_MSG_NORMAL,
3130 "%2slcm_flags: %s\n", " ",
3131 comp_v1->lcm_mirror_count > 0 ?
3134 llapi_printf(LLAPI_MSG_NORMAL,
3135 "%2slcm_flags: %s\n", " ",
3136 llapi_layout_flags_string(comp_v1->lcm_flags));
3139 if (verbose & VERBOSE_GENERATION) {
3140 if (verbose & ~VERBOSE_GENERATION)
3141 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3143 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3146 if (verbose & VERBOSE_MIRROR_COUNT) {
3147 if (verbose & ~VERBOSE_MIRROR_COUNT)
3148 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3150 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3151 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3152 comp_v1->lcm_mirror_count + 1 : 1);
3155 if (verbose & VERBOSE_COMP_COUNT) {
3156 if (verbose & ~VERBOSE_COMP_COUNT)
3157 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3159 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3160 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3161 comp_v1->lcm_entry_count : 0);
3164 if (verbose & VERBOSE_DETAIL && !yaml)
3165 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3168 static void lcme_flags2str(__u32 comp_flags)
3174 llapi_printf(LLAPI_MSG_NORMAL, "0");
3177 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3178 if (comp_flags & comp_flags_table[i].cfn_flag) {
3180 llapi_printf(LLAPI_MSG_NORMAL, ",");
3181 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3182 comp_flags_table[i].cfn_name);
3183 comp_flags &= ~comp_flags_table[i].cfn_flag;
3189 llapi_printf(LLAPI_MSG_NORMAL, ",");
3190 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3194 static void lov_dump_comp_v1_entry(struct find_param *param,
3195 enum lov_dump_flags flags, int index)
3197 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3198 struct lov_comp_md_entry_v1 *entry;
3199 char *separator = "";
3200 enum llapi_layout_verbose verbose = param->fp_verbose;
3201 bool yaml = flags & LDF_YAML;
3203 entry = &comp_v1->lcm_entries[index];
3206 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3208 if (verbose & VERBOSE_COMP_ID) {
3209 if (verbose & VERBOSE_DETAIL && !yaml)
3210 llapi_printf(LLAPI_MSG_NORMAL,
3211 "%slcme_id: ", " - ");
3212 else if (verbose & ~VERBOSE_COMP_ID)
3213 llapi_printf(LLAPI_MSG_NORMAL,
3214 "%4slcme_id: ", " ");
3215 if (entry->lcme_id != LCME_ID_INVAL)
3216 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3218 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3222 if (verbose & VERBOSE_MIRROR_ID) {
3223 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3224 if (verbose & ~VERBOSE_MIRROR_ID)
3225 llapi_printf(LLAPI_MSG_NORMAL,
3226 "%4slcme_mirror_id: ", " ");
3227 if (entry->lcme_id != LCME_ID_INVAL)
3228 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3229 mirror_id_of(entry->lcme_id));
3231 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3235 if (verbose & VERBOSE_COMP_FLAGS) {
3236 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3237 if (verbose & ~VERBOSE_COMP_FLAGS)
3238 llapi_printf(LLAPI_MSG_NORMAL,
3239 "%4slcme_flags: ", " ");
3240 lcme_flags2str(entry->lcme_flags);
3243 /* print snapshot timestamp if its a nosync comp */
3244 if ((verbose & VERBOSE_COMP_FLAGS) &&
3245 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3246 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3247 if (verbose & ~VERBOSE_COMP_FLAGS)
3248 llapi_printf(LLAPI_MSG_NORMAL,
3249 "%4slcme_timestamp: ", " ");
3251 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3252 entry->lcme_timestamp);
3254 time_t stamp = entry->lcme_timestamp;
3255 char *date_str = asctime(localtime(&stamp));
3257 date_str[strlen(date_str) - 1] = '\0';
3258 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3264 if (verbose & VERBOSE_COMP_START) {
3265 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3266 if (verbose & ~VERBOSE_COMP_START)
3267 llapi_printf(LLAPI_MSG_NORMAL,
3268 "%4slcme_extent.e_start: ", " ");
3269 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3270 entry->lcme_extent.e_start);
3274 if (verbose & VERBOSE_COMP_END) {
3275 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3276 if (verbose & ~VERBOSE_COMP_END)
3277 llapi_printf(LLAPI_MSG_NORMAL,
3278 "%4slcme_extent.e_end: ", " ");
3279 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3280 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3282 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3283 entry->lcme_extent.e_end);
3288 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3289 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3290 } else if (verbose & VERBOSE_DETAIL) {
3291 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3292 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3293 " ", entry->lcme_offset);
3294 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3295 " ", entry->lcme_size);
3296 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3298 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3302 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3303 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3305 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3306 * The table below gives the answers for the specified parameters (value and
3307 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3308 * --------------------------------------
3309 * 1 | file > limit; sign > 0 | -1 / -1 |
3310 * 2 | file = limit; sign > 0 | -1 / -1 |
3311 * 3 | file < limit; sign > 0 | ? / 1 |
3312 * 4 | file > limit; sign = 0 | -1 / -1 |
3313 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3314 * 6 | file < limit; sign = 0 | ? / -1 |
3315 * 7 | file > limit; sign < 0 | 1 / 1 |
3316 * 8 | file = limit; sign < 0 | ? / -1 |
3317 * 9 | file < limit; sign < 0 | ? / -1 |
3318 * --------------------------------------
3319 * Note: 5th actually means that the value is within the interval
3320 * (limit - margin, limit]. */
3321 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3322 int sign, int negopt, unsigned long long margin,
3328 /* Drop the fraction of margin (of days or size). */
3329 if (file + margin <= limit)
3331 } else if (sign == 0) {
3332 if (file <= limit && file + margin > limit)
3334 else if (file + margin <= limit)
3336 } else if (sign < 0) {
3343 return negopt ? ~ret + 1 : ret;
3346 static inline struct lov_user_md *
3347 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3349 return (struct lov_user_md *)((char *)comp_v1 +
3350 comp_v1->lcm_entries[ent_idx].lcme_offset);
3353 static inline struct lov_user_ost_data_v1 *
3354 lov_v1v3_objects(struct lov_user_md *v1)
3356 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3357 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3359 return v1->lmm_objects;
3363 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3365 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3366 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3367 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3369 pool_name[0] = '\0';
3373 print_last_init_comp(struct find_param *param)
3375 /* print all component info */
3376 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3379 /* print specific component info */
3380 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3381 param->fp_check_comp_start || param->fp_check_comp_end ||
3382 param->fp_check_mirror_id || param->fp_check_mirror_index)
3388 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3392 if (param->fp_comp_end == LUSTRE_EOF) {
3393 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3394 match = end == LUSTRE_EOF ? 1 : -1;
3395 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3396 match = end == LUSTRE_EOF ? -1 : 1;
3397 else /* at least EOF */
3399 if (param->fp_exclude_comp_end)
3402 unsigned long long margin;
3404 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3405 match = find_value_cmp(end, param->fp_comp_end,
3406 param->fp_comp_end_sign,
3407 param->fp_exclude_comp_end, margin, 0);
3414 * An example of "getstripe -v" for a two components PFL file:
3417 * lcm_magic: 0x0BD60BD0
3421 * lcm_entry_count: 2
3425 * lcme_extent.e_start: 0
3426 * lcme_extent.e_end: 1048576
3430 * lmm_magic: 0x0BD10BD0
3431 * lmm_seq: 0x200000401
3432 * lmm_object_id: 0x1
3433 * lmm_fid: [0x200000401:0x1:0x0]
3434 * lmm_stripe_count: 1
3435 * lmm_stripe_size: 1048576
3436 * lmm_pattern: raid0
3438 * lmm_stripe_offset: 0
3440 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3444 * lcme_extent.e_start: 1048576
3445 * lcme_extent.e_end: EOF
3449 * lmm_magic: 0x0BD10BD0
3450 * lmm_seq: 0x200000401
3451 * lmm_object_id: 0x1
3452 * lmm_fid: [0x200000401:0x1:0x0]
3453 * lmm_stripe_count: 2
3454 * lmm_stripe_size: 1048576
3455 * lmm_pattern: raid0
3457 * lmm_stripe_offset: 1
3459 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3460 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3462 static void lov_dump_comp_v1(struct find_param *param, char *path,
3463 enum lov_dump_flags flags)
3465 struct lov_comp_md_entry_v1 *entry;
3466 struct lov_user_ost_data_v1 *objects;
3467 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3468 struct lov_user_md_v1 *v1;
3469 char pool_name[LOV_MAXPOOLNAME + 1];
3470 int obdindex = param->fp_obd_index;
3472 bool obdstripe = false;
3473 __u16 mirror_index = 0;
3474 __u16 mirror_id = 0;
3476 if (obdindex != OBD_NOT_FOUND) {
3477 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3478 i < comp_v1->lcm_entry_count; i++) {
3479 if (!(comp_v1->lcm_entries[i].lcme_flags &
3483 v1 = lov_comp_entry(comp_v1, i);
3484 objects = lov_v1v3_objects(v1);
3486 for (j = 0; j < v1->lmm_stripe_count; j++) {
3487 if (obdindex == objects[j].l_ost_idx) {
3500 lov_dump_comp_v1_header(param, path, flags);
3502 flags |= LDF_INDENT;
3504 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3505 entry = &comp_v1->lcm_entries[i];
3507 if (param->fp_check_comp_flags) {
3508 if (((param->fp_comp_flags & entry->lcme_flags) !=
3509 param->fp_comp_flags) ||
3510 (param->fp_comp_neg_flags & entry->lcme_flags))
3514 if (param->fp_check_comp_id &&
3515 param->fp_comp_id != entry->lcme_id)
3518 if (param->fp_check_comp_start) {
3519 match = find_value_cmp(entry->lcme_extent.e_start,
3520 param->fp_comp_start,
3521 param->fp_comp_start_sign,
3523 param->fp_comp_start_units, 0);
3528 if (param->fp_check_comp_end) {
3529 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3535 if (param->fp_check_mirror_index) {
3536 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3538 mirror_id = mirror_id_of(entry->lcme_id);
3541 match = find_value_cmp(mirror_index,
3542 param->fp_mirror_index,
3543 param->fp_mirror_index_sign,
3544 param->fp_exclude_mirror_index,
3548 } else if (param->fp_check_mirror_id) {
3549 if (mirror_id != mirror_id_of(entry->lcme_id))
3550 mirror_id = mirror_id_of(entry->lcme_id);
3552 match = find_value_cmp(mirror_id,
3553 param->fp_mirror_id,
3554 param->fp_mirror_id_sign,
3555 param->fp_exclude_mirror_id,
3561 if (print_last_init_comp(param)) {
3563 * if part of stripe info is needed, we'd print only
3564 * the last instantiated component info.
3566 if (entry->lcme_flags & LCME_FL_INIT)
3572 if (entry->lcme_flags & LCME_FL_INIT) {
3573 if (obdindex != OBD_NOT_FOUND) {
3574 flags |= LDF_SKIP_OBJS;
3575 v1 = lov_comp_entry(comp_v1, i);
3576 objects = lov_v1v3_objects(v1);
3578 for (j = 0; j < v1->lmm_stripe_count; j++) {
3579 if (obdindex == objects[j].l_ost_idx) {
3580 flags &= ~LDF_SKIP_OBJS;
3585 flags &= ~LDF_SKIP_OBJS;
3588 flags |= LDF_SKIP_OBJS;
3591 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3593 lov_dump_comp_v1_entry(param, flags, i);
3595 v1 = lov_comp_entry(comp_v1, i);
3596 objects = lov_v1v3_objects(v1);
3597 lov_v1v3_pool_name(v1, pool_name);
3599 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3600 param->fp_max_depth, param->fp_verbose,
3603 if (print_last_init_comp(param)) {
3605 * directory layout contains only layout template, print the
3609 i = comp_v1->lcm_entry_count - 1;
3612 flags &= ~LDF_SKIP_OBJS;
3614 lov_dump_comp_v1_entry(param, flags, i);
3616 v1 = lov_comp_entry(comp_v1, i);
3617 objects = lov_v1v3_objects(v1);
3618 lov_v1v3_pool_name(v1, pool_name);
3620 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3621 param->fp_max_depth, param->fp_verbose,
3626 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3627 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3630 static inline bool has_any_comp_options(struct find_param *param)
3632 enum llapi_layout_verbose verbose = param->fp_verbose;
3634 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3635 param->fp_check_comp_start || param->fp_check_comp_end ||
3636 param->fp_check_comp_flags)
3639 /* show full layout information, not component specific */
3640 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3643 return verbose & VERBOSE_COMP_OPTS;
3646 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3649 struct lov_user_md *lum = &orig->lmd_lmm;
3650 struct lov_user_mds_data *new;
3651 struct lov_comp_md_v1 *comp_v1;
3652 struct lov_comp_md_entry_v1 *ent;
3653 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3654 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3657 new = malloc(sizeof(lstat_t) + lum_off + lum_size);
3659 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3663 memcpy(new, orig, sizeof(lstat_t));
3665 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3666 comp_v1->lcm_magic = lum->lmm_magic;
3667 comp_v1->lcm_size = lum_off + lum_size;
3668 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3669 comp_v1->lcm_flags = 0;
3670 comp_v1->lcm_entry_count = 1;
3672 ent = &comp_v1->lcm_entries[0];
3674 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3675 ent->lcme_extent.e_start = 0;
3676 ent->lcme_extent.e_end = LUSTRE_EOF;
3677 ent->lcme_offset = lum_off;
3678 ent->lcme_size = lum_size;
3680 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3685 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3686 enum lov_dump_flags flags)
3688 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3690 if (has_any_comp_options(param)) {
3691 struct lov_user_mds_data *new_lmd, *orig_lmd;
3693 orig_lmd = param->fp_lmd;
3694 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3695 if (new_lmd != NULL) {
3696 param->fp_lmd = new_lmd;
3697 lov_dump_comp_v1(param, path, flags);
3698 param->fp_lmd = orig_lmd;
3704 if (magic == LOV_USER_MAGIC_V1) {
3705 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3706 param->fp_lmd->lmd_lmm.lmm_objects,
3707 path, param->fp_obd_index,
3708 param->fp_max_depth, param->fp_verbose,
3711 char pool_name[LOV_MAXPOOLNAME + 1];
3712 struct lov_user_ost_data_v1 *objects;
3713 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3715 snprintf(pool_name, sizeof(pool_name), "%s",
3716 lmmv3->lmm_pool_name);
3717 objects = lmmv3->lmm_objects;
3718 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3719 objects, path, param->fp_obd_index,
3720 param->fp_max_depth, param->fp_verbose,
3725 static uint32_t check_foreign_type(uint32_t foreign_type)
3729 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3730 if (lu_foreign_types[i].lft_name == NULL)
3732 if (foreign_type == lu_foreign_types[i].lft_type)
3736 return LU_FOREIGN_TYPE_UNKNOWN;
3739 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3740 enum lov_dump_flags flags)
3742 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3743 bool yaml = flags & LDF_YAML;
3745 if (!yaml && param->fp_depth && path)
3746 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3748 if (param->fp_verbose & VERBOSE_DETAIL) {
3749 uint32_t type = check_foreign_type(lfm->lfm_type);
3751 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3753 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3755 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3757 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3758 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3759 lu_foreign_types[type].lft_name);
3761 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3763 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3766 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3767 lfm->lfm_length, lfm->lfm_value);
3768 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3771 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3772 enum lov_dump_flags flags)
3774 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3775 bool yaml = flags & LDF_YAML;
3777 if (!yaml && param->fp_depth && path)
3778 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3780 if (param->fp_verbose & VERBOSE_DETAIL) {
3781 uint32_t type = check_foreign_type(lfm->lfm_type);
3783 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3785 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3787 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3789 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3790 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3791 lu_foreign_types[type].lft_name);
3793 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3795 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3798 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3799 lfm->lfm_length, lfm->lfm_value);
3800 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3803 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3804 enum lov_dump_flags flags)
3808 if (param->fp_get_lmv || param->fp_get_default_lmv)
3809 magic = (__u32)param->fp_lmv_md->lum_magic;
3811 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3814 flags |= LDF_IS_RAW;
3819 case LOV_USER_MAGIC_V1:
3820 case LOV_USER_MAGIC_V3:
3821 case LOV_USER_MAGIC_SPECIFIC:
3822 lov_dump_plain_user_lmm(param, path, flags);
3824 case LOV_USER_MAGIC_FOREIGN:
3825 lov_dump_foreign_lmm(param, path, flags);
3828 case LMV_USER_MAGIC: {
3829 char pool_name[LOV_MAXPOOLNAME + 1];
3830 struct lmv_user_md *lum;
3832 lum = (struct lmv_user_md *)param->fp_lmv_md;
3833 snprintf(pool_name, sizeof(pool_name), "%s",
3834 lum->lum_pool_name);
3835 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3836 param->fp_max_depth, param->fp_verbose,
3840 case LOV_USER_MAGIC_COMP_V1:
3841 lov_dump_comp_v1(param, path, flags);
3843 case LMV_MAGIC_FOREIGN:
3844 lmv_dump_foreign_lmm(param, path, flags);
3847 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
3848 "(expecting one of %#x %#x %#x %#x)\n",
3849 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3850 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3851 LMV_USER_MAGIC, LMV_MAGIC_V1);
3856 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3862 fname = strrchr(path, '/');
3864 /* It should be a file (or other non-directory) */
3865 if (fname == NULL) {
3866 dname = (char *)malloc(2);
3870 fname = (char *)path;
3872 dname = (char *)malloc(fname - path + 1);
3875 strncpy(dname, path, fname - path);
3876 dname[fname - path] = '\0';
3880 fd = open(dname, O_RDONLY | O_NONBLOCK);
3886 strcpy((char *)lum, fname);
3887 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3890 if (close(fd) == -1 && rc == 0)
3898 int llapi_file_lookup(int dirfd, const char *name)
3900 struct obd_ioctl_data data = { 0 };
3905 if (dirfd < 0 || name == NULL)
3908 data.ioc_version = OBD_IOCTL_VERSION;
3909 data.ioc_len = sizeof(data);
3910 data.ioc_inlbuf1 = (char *)name;
3911 data.ioc_inllen1 = strlen(name) + 1;
3913 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3915 llapi_error(LLAPI_MSG_ERROR, rc,
3916 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3921 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
3927 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
3928 * Return -1 or 1 if file timestamp does not or does match the given criteria
3929 * correspondingly. Return 0 if the MDS time is being checked and there are
3930 * attributes on OSTs and it is not yet clear if the timespamp matches.
3932 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
3933 * updated timestamps. */
3934 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
3939 /* Check if file is accepted. */
3940 if (param->fp_atime) {
3941 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
3942 param->fp_asign, param->fp_exclude_atime,
3943 param->fp_time_margin, mds);
3949 if (param->fp_mtime) {
3950 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
3951 param->fp_msign, param->fp_exclude_mtime,
3952 param->fp_time_margin, mds);
3956 /* If the previous check matches, but this one is not yet clear,
3957 * we should return 0 to do an RPC on OSTs. */
3962 if (param->fp_ctime) {
3963 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
3964 param->fp_csign, param->fp_exclude_ctime,
3965 param->fp_time_margin, mds);
3969 /* If the previous check matches, but this one is not yet clear,
3970 * we should return 0 to do an RPC on OSTs. */
3979 * Check whether the stripes matches the indexes user provided
3983 static int check_obd_match(struct find_param *param)
3985 struct lov_user_ost_data_v1 *objects;
3986 struct lov_comp_md_v1 *comp_v1 = NULL;
3987 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3988 lstat_t *st = ¶m->fp_lmd->lmd_st;
3989 int i, j, k, count = 1;
3991 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
3994 if (!S_ISREG(st->st_mode))
3997 /* exclude foreign */
3998 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
3999 return param->fp_exclude_obd;
4001 /* Only those files should be accepted, which have a
4002 * stripe on the specified OST. */
4003 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4004 comp_v1 = (struct lov_comp_md_v1 *)v1;
4005 count = comp_v1->lcm_entry_count;
4008 for (i = 0; i < count; i++) {
4010 v1 = lov_comp_entry(comp_v1, i);
4012 objects = lov_v1v3_objects(v1);
4014 for (j = 0; j < v1->lmm_stripe_count; j++) {
4015 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4018 for (k = 0; k < param->fp_num_obds; k++) {
4019 if (param->fp_obd_indexes[k] ==
4020 objects[j].l_ost_idx)
4021 return !param->fp_exclude_obd;
4026 return param->fp_exclude_obd;
4029 static int check_mdt_match(struct find_param *param)
4033 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4036 /* FIXME: For striped dir, we should get stripe information and check */
4037 for (i = 0; i < param->fp_num_mdts; i++) {
4038 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4039 return !param->fp_exclude_mdt;
4042 if (param->fp_exclude_mdt)
4049 * Check whether the obd is active or not, if it is
4050 * not active, just print the object affected by this
4053 static int print_failed_tgt(struct find_param *param, char *path, int type)
4055 struct obd_statfs stat_buf;
4056 struct obd_uuid uuid_buf;
4059 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV)
4062 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4063 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4064 ret = llapi_obd_statfs(path, type,
4065 param->fp_obd_index, &stat_buf,
4068 llapi_printf(LLAPI_MSG_NORMAL,
4069 "obd_uuid: %s failed %s ",
4070 param->fp_obd_uuid->uuid,
4077 static int find_check_stripe_size(struct find_param *param)
4079 struct lov_comp_md_v1 *comp_v1 = NULL;
4080 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4081 int ret, i, count = 1;
4083 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4084 return param->fp_exclude_stripe_size ? 1 : -1;
4086 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4087 comp_v1 = (struct lov_comp_md_v1 *)v1;
4088 count = comp_v1->lcm_entry_count;
4089 ret = param->fp_exclude_stripe_size ? 1 : -1;
4092 for (i = 0; i < count; i++) {
4094 v1 = lov_comp_entry(comp_v1, i);
4096 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_stripe_size,
4097 param->fp_stripe_size_sign,
4098 param->fp_exclude_stripe_size,
4099 param->fp_stripe_size_units, 0);
4100 /* If any stripe_size matches */
4108 static __u32 find_get_stripe_count(struct find_param *param)
4110 struct lov_comp_md_v1 *comp_v1 = NULL;
4111 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4113 __u32 stripe_count = 0;
4115 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4118 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4119 comp_v1 = (struct lov_comp_md_v1 *)v1;
4120 count = comp_v1->lcm_entry_count;
4123 for (i = 0; i < count; i++) {
4125 v1 = lov_comp_entry(comp_v1, i);
4126 stripe_count += v1->lmm_stripe_count;
4129 return stripe_count;
4132 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4134 static int find_check_layout(struct find_param *param)
4136 struct lov_comp_md_v1 *comp_v1 = NULL;
4137 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4139 bool found = false, valid = false;
4141 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4142 comp_v1 = (struct lov_comp_md_v1 *)v1;
4143 count = comp_v1->lcm_entry_count;
4146 for (i = 0; i < count; i++) {
4148 v1 = lov_comp_entry(comp_v1, i);
4150 /* foreign file have a special magic but no pattern field */
4151 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4154 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4158 if (v1->lmm_pattern & param->fp_layout) {
4167 if ((found && !param->fp_exclude_layout) ||
4168 (!found && param->fp_exclude_layout))
4174 /* if no type specified, check/exclude all foreign
4175 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4177 static int find_check_foreign(struct find_param *param)
4179 if (S_ISREG(param->fp_lmd->lmd_st.st_mode)) {
4180 struct lov_foreign_md *lfm;
4182 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4183 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4184 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4185 return param->fp_exclude_foreign ? 1 : -1;
4188 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4189 lfm->lfm_type == param->fp_foreign_type)
4190 return param->fp_exclude_foreign ? -1 : 1;
4191 return param->fp_exclude_foreign ? 1 : -1;
4195 if (S_ISDIR(param->fp_lmd->lmd_st.st_mode)) {
4196 struct lmv_foreign_md *lfm;
4198 lfm = (void *)param->fp_lmv_md;
4199 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
4200 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4201 return param->fp_exclude_foreign ? 1 : -1;
4204 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4205 lfm->lfm_type == param->fp_foreign_type)
4206 return param->fp_exclude_foreign ? -1 : 1;
4207 return param->fp_exclude_foreign ? 1 : -1;
4213 static int find_check_pool(struct find_param *param)
4215 struct lov_comp_md_v1 *comp_v1 = NULL;
4216 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4217 struct lov_user_md_v3 *v3 = (void *)v1;
4221 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4222 comp_v1 = (struct lov_comp_md_v1 *)v1;
4223 count = comp_v1->lcm_entry_count;
4224 /* empty requested pool is taken as no pool search */
4225 if (count == 0 && param->fp_poolname[0] == '\0')
4229 for (i = 0; i < count; i++) {
4230 if (comp_v1 != NULL)
4231 v1 = lov_comp_entry(comp_v1, i);
4233 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4236 if (((v1->lmm_magic == LOV_USER_MAGIC_V1) &&
4237 (param->fp_poolname[0] == '\0')) ||
4238 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4239 (strncmp(v3->lmm_pool_name,
4240 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4241 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4242 (strcmp(param->fp_poolname, "*") == 0))) {
4248 if ((found && !param->fp_exclude_pool) ||
4249 (!found && param->fp_exclude_pool))
4255 static int find_check_comp_options(struct find_param *param)
4257 lstat_t *st = ¶m->fp_lmd->lmd_st;
4258 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4259 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4260 struct lov_comp_md_entry_v1 *entry;
4263 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4266 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4267 comp_v1 = (struct lov_comp_md_v1 *)v1;
4269 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4270 if (forged_v1 == NULL)
4272 comp_v1 = forged_v1;
4273 comp_v1->lcm_entry_count = 1;
4274 entry = &comp_v1->lcm_entries[0];
4275 entry->lcme_flags = S_ISDIR(st->st_mode) ? 0 : LCME_FL_INIT;
4276 entry->lcme_extent.e_start = 0;
4277 entry->lcme_extent.e_end = LUSTRE_EOF;
4280 /* invalid case, don't match for any kind of search. */
4281 if (comp_v1->lcm_entry_count == 0) {
4286 if (param->fp_check_comp_count) {
4287 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4288 param->fp_comp_count,
4289 param->fp_comp_count_sign,
4290 param->fp_exclude_comp_count, 1, 0);
4296 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4297 entry = &comp_v1->lcm_entries[i];
4299 if (param->fp_check_comp_flags) {
4301 if (((param->fp_comp_flags & entry->lcme_flags) !=
4302 param->fp_comp_flags) ||
4303 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4309 if (param->fp_check_comp_start) {
4310 ret = find_value_cmp(entry->lcme_extent.e_start,
4311 param->fp_comp_start,
4312 param->fp_comp_start_sign,
4313 param->fp_exclude_comp_start,
4314 param->fp_comp_start_units, 0);
4319 if (param->fp_check_comp_end) {
4320 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4326 /* the component matches all criteria */
4335 static int find_check_mirror_options(struct find_param *param)
4337 struct lov_comp_md_v1 *comp_v1;
4338 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4341 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4344 comp_v1 = (struct lov_comp_md_v1 *)v1;
4346 if (param->fp_check_mirror_count) {
4347 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4348 param->fp_mirror_count,
4349 param->fp_mirror_count_sign,
4350 param->fp_exclude_mirror_count, 1, 0);
4355 if (param->fp_check_mirror_state) {
4357 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4359 if ((param->fp_mirror_state != 0 &&
4360 file_state != param->fp_mirror_state) ||
4361 file_state == param->fp_mirror_neg_state)
4368 static bool find_check_lmm_info(struct find_param *param)
4370 return param->fp_check_pool || param->fp_check_stripe_count ||
4371 param->fp_check_stripe_size || param->fp_check_layout ||
4372 param->fp_check_comp_count || param->fp_check_comp_end ||
4373 param->fp_check_comp_start || param->fp_check_comp_flags ||
4374 param->fp_check_mirror_count || param->fp_check_foreign ||
4375 param->fp_check_mirror_state ||
4376 param->fp_check_projid;
4380 * Get file/directory project id.
4381 * by the open fd resides on.
4382 * Return 0 and project id on success, or -ve errno.
4384 static int fget_projid(int fd, int *projid)
4389 rc = ioctl(fd, LL_IOC_FSGETXATTR, &fsx);
4393 *projid = fsx.fsx_projid;
4397 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
4398 void *data, struct dirent64 *de)
4400 struct find_param *param = (struct find_param *)data;
4401 DIR *dir = dirp == NULL ? NULL : *dirp;
4402 int decision = 1; /* 1 is accepted; -1 is rejected. */
4403 lstat_t *st = ¶m->fp_lmd->lmd_st;
4405 int checked_type = 0;
4407 __u32 stripe_count = 0;
4410 if (parent == NULL && dir == NULL)
4413 /* If a regular expression is presented, make the initial decision */
4414 if (param->fp_pattern != NULL) {
4415 char *fname = strrchr(path, '/');
4416 fname = (fname == NULL ? path : fname + 1);
4417 ret = fnmatch(param->fp_pattern, fname, 0);
4418 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4419 (ret == 0 && param->fp_exclude_pattern))
4423 /* See if we can check the file type from the dirent. */
4424 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
4427 if (DTTOIF(de->d_type) == param->fp_type) {
4428 if (param->fp_exclude_type)
4431 if (!param->fp_exclude_type)
4438 /* Request MDS for the stat info if some of these parameters need
4439 * to be compared. */
4440 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4441 param->fp_check_uid || param->fp_check_gid ||
4442 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4443 param->fp_check_size || param->fp_check_blocks ||
4444 find_check_lmm_info(param) ||
4445 param->fp_check_mdt_count || param->fp_check_hash_type)
4448 if (param->fp_type != 0 && checked_type == 0)
4451 if (decision == 0) {
4452 if (dir && (param->fp_check_mdt_count ||
4453 param->fp_check_hash_type || param->fp_check_foreign)) {
4454 param->fp_get_lmv = 1;
4455 ret = cb_get_dirstripe(path, dir, param);
4457 /* XXX this works to decide for foreign
4460 if (errno == ENODATA &&
4461 param->fp_check_foreign) {
4462 if (param->fp_exclude_foreign)
4470 param->fp_lmd->lmd_lmm.lmm_magic = 0;
4471 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
4472 param->fp_lum_size, GET_LMD_INFO);
4473 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
4474 find_check_lmm_info(param)) {
4475 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
4477 /* We need to "fake" the "use the default" values
4478 * since the lmm struct is zeroed out at this point. */
4479 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4480 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
4482 ostid_set_seq(&lmm->lmm_oi,
4483 FID_SEQ_LOV_DEFAULT);
4484 lmm->lmm_stripe_size = 0;
4485 lmm->lmm_stripe_count = 0;
4486 lmm->lmm_stripe_offset = -1;
4488 if (ret == 0 && param->fp_mdt_uuid != NULL) {
4490 ret = llapi_file_fget_mdtidx(dirfd(dir),
4491 ¶m->fp_file_mdt_index);
4492 } else if (S_ISREG(st->st_mode)) {
4493 /* FIXME: we could get the MDT index from the
4494 * file's FID in lmd->lmd_lmm.lmm_oi without
4495 * opening the file, once we are sure that
4496 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
4497 * That would still be an ioctl() to map the
4498 * FID to the MDT, but not an open RPC. */
4499 fd = open(path, O_RDONLY);
4501 ret = llapi_file_fget_mdtidx(fd,
4502 ¶m->fp_file_mdt_index);
4507 /* For a special file, we assume it resides on
4508 * the same MDT as the parent directory. */
4509 ret = llapi_file_fget_mdtidx(dirfd(parent),
4510 ¶m->fp_file_mdt_index);
4521 stripe_count = find_get_stripe_count(param);
4525 if (param->fp_type && !checked_type) {
4526 if ((st->st_mode & S_IFMT) == param->fp_type) {
4527 if (param->fp_exclude_type)
4530 if (!param->fp_exclude_type)
4536 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
4537 if (lustre_fs && param->fp_got_uuids &&
4538 param->fp_dev != st->st_dev) {
4539 /* A lustre/lustre mount point is crossed. */
4540 param->fp_got_uuids = 0;
4541 param->fp_obds_printed = 0;
4542 param->fp_mdt_index = OBD_NOT_FOUND;
4543 param->fp_obd_index = OBD_NOT_FOUND;
4546 if (lustre_fs && !param->fp_got_uuids) {
4547 ret = setup_target_indexes(dir ? dir : parent, path,
4552 param->fp_dev = st->st_dev;
4553 } else if (!lustre_fs && param->fp_got_uuids) {
4554 /* A lustre/non-lustre mount point is crossed. */
4555 param->fp_got_uuids = 0;
4556 param->fp_mdt_index = OBD_NOT_FOUND;
4557 param->fp_obd_index = OBD_NOT_FOUND;
4561 if (param->fp_check_foreign) {
4562 decision = find_check_foreign(param);
4567 if (param->fp_check_stripe_size) {
4568 decision = find_check_stripe_size(param);
4573 if (param->fp_check_stripe_count) {
4574 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
4575 param->fp_stripe_count_sign,
4576 param->fp_exclude_stripe_count, 1, 0);
4581 if (param->fp_check_mdt_count) {
4582 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4587 decision = find_value_cmp(
4588 param->fp_lmv_md->lum_stripe_count,
4589 param->fp_mdt_count,
4590 param->fp_mdt_count_sign,
4591 param->fp_exclude_mdt_count, 1, 0);
4596 if (param->fp_check_layout) {
4597 decision = find_check_layout(param);
4602 if (param->fp_check_hash_type) {
4605 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4610 found = param->fp_lmv_md->lum_hash_type & param->fp_hash_type;
4611 if ((found && param->fp_exclude_hash_type) ||
4612 (!found && !param->fp_exclude_hash_type)) {
4618 /* If an OBD UUID is specified but none matches, skip this file. */
4619 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
4620 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
4623 /* If an OST or MDT UUID is given, and some OST matches,
4625 if (param->fp_obd_index != OBD_NOT_FOUND ||
4626 param->fp_mdt_index != OBD_NOT_FOUND) {
4627 if (param->fp_obd_uuid) {
4628 if (check_obd_match(param)) {
4629 /* If no mdtuuid is given, we are done.
4630 * Otherwise, fall through to the mdtuuid
4632 if (!param->fp_mdt_uuid)
4639 if (param->fp_mdt_uuid) {
4640 if (check_mdt_match(param))
4647 if (param->fp_check_uid) {
4648 if (st->st_uid == param->fp_uid) {
4649 if (param->fp_exclude_uid)
4652 if (!param->fp_exclude_uid)
4657 if (param->fp_check_gid) {
4658 if (st->st_gid == param->fp_gid) {
4659 if (param->fp_exclude_gid)
4662 if (!param->fp_exclude_gid)
4667 if (param->fp_check_projid) {
4671 fd = open(path, O_RDONLY);
4674 ret = fget_projid(fd, &projid);
4679 if (projid == param->fp_projid) {
4680 if (param->fp_exclude_projid)
4683 if (!param->fp_exclude_projid)
4688 if (param->fp_check_pool) {
4689 decision = find_check_pool(param);
4694 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
4695 param->fp_check_comp_start || param->fp_check_comp_end) {
4696 decision = find_check_comp_options(param);
4701 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
4702 decision = find_check_mirror_options(param);
4707 /* Check the time on mds. */
4709 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
4712 for_mds = lustre_fs ?
4713 (S_ISREG(st->st_mode) && stripe_count) : 0;
4714 decision = find_time_check(st, param, for_mds);
4719 /* If file still fits the request, ask ost for updated info.
4720 The regular stat is almost of the same speed as some new
4721 'glimpse-size-ioctl'. */
4723 if ((param->fp_check_size || param->fp_check_blocks) &&
4724 ((S_ISREG(st->st_mode) && stripe_count) || S_ISDIR(st->st_mode)))
4728 /* For regular files with the stripe the decision may have not
4729 * been taken yet if *time or size is to be checked. */
4730 if (param->fp_obd_index != OBD_NOT_FOUND)
4731 print_failed_tgt(param, path, LL_STATFS_LOV);
4733 if (param->fp_mdt_index != OBD_NOT_FOUND)
4734 print_failed_tgt(param, path, LL_STATFS_LMV);
4737 ret = fstat_f(dirfd(dir), st);
4738 else if (de != NULL)
4739 ret = fstatat_f(dirfd(parent), de->d_name, st,
4740 AT_SYMLINK_NOFOLLOW);
4742 ret = lstat_f(path, st);
4745 if (errno == ENOENT) {
4746 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
4747 "warning: %s: %s does not exist",
4752 llapi_error(LLAPI_MSG_ERROR, ret,
4753 "%s: IOC_LOV_GETINFO on %s failed",
4759 /* Check the time on osc. */
4760 decision = find_time_check(st, param, 0);
4765 if (param->fp_check_size) {
4766 decision = find_value_cmp(st->st_size, param->fp_size,
4767 param->fp_size_sign,
4768 param->fp_exclude_size,
4769 param->fp_size_units, 0);
4774 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
4775 decision = find_value_cmp(st->st_blocks * 512, param->fp_blocks,
4776 param->fp_blocks_sign,
4777 param->fp_exclude_blocks,
4778 param->fp_blocks_units, 0);
4784 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
4785 if (param->fp_zero_end)
4786 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
4788 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4792 /* Do not get down anymore? */
4793 if (param->fp_depth == param->fp_max_depth) {
4804 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
4805 void *param_data, struct dirent64 *de)
4807 struct find_param *param = (struct find_param *)param_data;
4808 struct lmv_user_md *lmu = param->fp_lmv_md;
4809 DIR *tmp_parent = parent;
4810 char raw[MAX_IOC_BUFLEN] = {'\0'};
4812 struct obd_ioctl_data data = { 0 };
4819 if (parent == NULL && dirp == NULL)
4828 if (parent == NULL) {
4829 tmp_parent = opendir_parent(path);
4830 if (tmp_parent == NULL) {
4833 llapi_error(LLAPI_MSG_ERROR, ret,
4834 "can not open %s", path);
4839 fd = dirfd(tmp_parent);
4841 path_copy = strdup(path);
4842 filename = basename(path_copy);
4844 data.ioc_inlbuf1 = (char *)filename;
4845 data.ioc_inllen1 = strlen(filename) + 1;
4846 data.ioc_inlbuf2 = (char *)lmu;
4847 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
4849 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
4851 llapi_error(LLAPI_MSG_ERROR, ret,
4852 "llapi_obd_statfs: error packing ioctl data");
4857 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
4859 if (errno == EBUSY && !retry) {
4860 /* because migrate may not be able to lock all involved
4861 * objects in order, for some of them it try lock, while
4862 * there may be conflicting COS locks and cause migrate
4863 * fail with EBUSY, hope a sync() could cause
4864 * transaction commit and release these COS locks. */
4868 } else if (errno == EALREADY) {
4869 if (param->fp_verbose & VERBOSE_DETAIL)
4870 llapi_printf(LLAPI_MSG_NORMAL,
4871 "%s was migrated to MDT%d already\n",
4872 path, lmu->lum_stripe_offset);
4876 llapi_error(LLAPI_MSG_ERROR, ret,
4877 "%s migrate failed: %s (%d)\n",
4878 path, strerror(-ret), ret);
4881 } else if (param->fp_verbose & VERBOSE_DETAIL) {
4882 llapi_printf(LLAPI_MSG_NORMAL,
4883 "migrate %s to MDT%d stripe count %d\n",
4884 path, lmu->lum_stripe_offset,
4885 lmu->lum_stripe_count);
4890 /* If the directory is being migration, we need
4891 * close the directory after migration,
4892 * so the old directory cache will be cleanup
4893 * on the client side, and re-open to get the
4894 * new directory handle */
4895 *dirp = opendir(path);
4896 if (*dirp == NULL) {
4898 llapi_error(LLAPI_MSG_ERROR, ret,
4899 "%s: Failed to open '%s'", __func__, path);
4904 closedir(tmp_parent);
4911 /* dir migration finished, shrink its stripes */
4912 static int cb_migrate_mdt_fini(char *path, DIR *parent, DIR **dirp, void *data,
4913 struct dirent64 *de)
4915 struct find_param *param = data;
4916 struct lmv_user_md *lmu = param->fp_lmv_md;
4917 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
4920 if (de && de->d_type != DT_DIR)
4925 * close it before setxattr because the latter may destroy the
4926 * original object, and cause close fail.
4928 ret = closedir(*dirp);
4934 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
4935 if (ret == -EALREADY)
4938 cb_common_fini(path, parent, dirp, data, de);
4942 int llapi_migrate_mdt(char *path, struct find_param *param)
4944 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
4948 int llapi_mv(char *path, struct find_param *param)
4950 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
4951 static bool printed;
4954 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
4955 "llapi_mv() is deprecated, use llapi_migrate_mdt()\n");
4959 return llapi_migrate_mdt(path, param);
4962 int llapi_find(char *path, struct find_param *param)
4964 return param_callback(path, cb_find_init, cb_common_fini, param);
4968 * Get MDT number that the file/directory inode referenced
4969 * by the open fd resides on.
4970 * Return 0 and mdtidx on success, or -ve errno.
4972 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
4974 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
4979 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
4980 struct dirent64 *de)
4982 struct find_param *param = (struct find_param *)data;
4983 DIR *d = dirp == NULL ? NULL : *dirp;
4987 if (parent == NULL && d == NULL)
4991 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
4992 } else /* if (parent) */ {
4995 fd = open(path, O_RDONLY | O_NOCTTY);
4997 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5005 if (ret == -ENODATA) {
5006 if (!param->fp_obd_uuid)
5007 llapi_printf(LLAPI_MSG_NORMAL,
5008 "'%s' has no stripe info\n", path);
5010 } else if (ret == -ENOENT) {
5011 llapi_error(LLAPI_MSG_WARN, ret,
5012 "warning: %s: '%s' does not exist",
5015 } else if (ret == -ENOTTY) {
5016 llapi_error(LLAPI_MSG_ERROR, ret,
5017 "%s: '%s' not on a Lustre fs",
5020 llapi_error(LLAPI_MSG_ERROR, ret,
5021 "error: %s: '%s' failed get_mdtidx",
5027 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5028 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5030 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5034 /* Do not go down anymore? */
5035 if (param->fp_depth == param->fp_max_depth)
5043 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
5044 struct dirent64 *de)
5046 struct find_param *param = (struct find_param *)data;
5047 DIR *d = dirp == NULL ? NULL : *dirp;
5050 if (parent == NULL && d == NULL)
5053 if (param->fp_obd_uuid) {
5054 param->fp_quiet = 1;
5055 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
5060 if (d && (param->fp_get_lmv || param->fp_get_default_lmv))
5061 ret = cb_get_dirstripe(path, d, param);
5063 (parent && !param->fp_get_lmv && !param->fp_get_default_lmv))
5064 ret = get_lmd_info(path, parent, d, ¶m->fp_lmd->lmd_lmm,
5065 param->fp_lum_size, GET_LMD_STRIPE);
5070 if (errno == ENODATA && d != NULL) {
5071 /* We need to "fake" the "use the default" values
5072 * since the lmm struct is zeroed out at this point.
5073 * The magic needs to be set in order to satisfy
5074 * a check later on in the code path.
5075 * The object_seq needs to be set for the "(Default)"
5076 * prefix to be displayed. */
5077 if (param->fp_get_default_lmv) {
5078 struct lmv_user_md *lum = param->fp_lmv_md;
5080 lum->lum_magic = LMV_USER_MAGIC;
5081 lum->lum_stripe_count = 0;
5082 lum->lum_stripe_offset = -1;
5084 } else if (param->fp_get_lmv) {
5085 struct lmv_user_md *lum = param->fp_lmv_md;
5088 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5091 lum->lum_magic = LMV_MAGIC_V1;
5092 lum->lum_stripe_count = 0;
5093 lum->lum_stripe_offset = mdtidx;
5096 struct lov_user_md *lmm =
5097 ¶m->fp_lmd->lmd_lmm;
5099 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5101 ostid_set_seq(&lmm->lmm_oi,
5102 FID_SEQ_LOV_DEFAULT);
5103 lmm->lmm_stripe_count = 0;
5104 lmm->lmm_stripe_size = 0;
5105 lmm->lmm_stripe_offset = -1;
5108 } else if (errno == ENODATA && parent != NULL) {
5109 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5110 llapi_printf(LLAPI_MSG_NORMAL,
5111 "%s has no stripe info\n", path);
5113 } else if (errno == ENOENT) {
5114 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5115 "warning: %s: %s does not exist",
5118 } else if (errno == ENOTTY) {
5120 llapi_error(LLAPI_MSG_ERROR, ret,
5121 "%s: '%s' not on a Lustre fs?",
5126 llapi_error(LLAPI_MSG_ERROR, ret,
5127 "error: %s: %s failed for %s",
5128 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
5129 "IOC_MDC_GETFILESTRIPE", path);
5136 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5137 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
5140 /* Do not get down anymore? */
5141 if (param->fp_depth == param->fp_max_depth)
5149 int llapi_getstripe(char *path, struct find_param *param)
5151 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5152 cb_get_mdt_index : cb_getstripe,
5153 cb_common_fini, param);
5156 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5157 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5159 char raw[MAX_IOC_BUFLEN] = {'\0'};
5161 struct obd_ioctl_data data = { 0 };
5164 data.ioc_inlbuf1 = (char *)&type;
5165 data.ioc_inllen1 = sizeof(__u32);
5166 data.ioc_inlbuf2 = (char *)&index;
5167 data.ioc_inllen2 = sizeof(__u32);
5168 data.ioc_pbuf1 = (char *)stat_buf;
5169 data.ioc_plen1 = sizeof(struct obd_statfs);
5170 data.ioc_pbuf2 = (char *)uuid_buf;
5171 data.ioc_plen2 = sizeof(struct obd_uuid);
5173 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5175 llapi_error(LLAPI_MSG_ERROR, rc,
5176 "llapi_obd_statfs: error packing ioctl data");
5180 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5182 return rc < 0 ? -errno : 0;
5185 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5186 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5191 fd = open(path, O_RDONLY);
5194 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5196 /* If we can't even open a file on the filesystem (e.g. with
5197 * -ESHUTDOWN), force caller to exit or it will loop forever. */
5201 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5208 #define MAX_STRING_SIZE 128
5210 int llapi_ping(char *obd_type, char *obd_name)
5212 int flags = O_RDONLY;
5213 char buf[1] = { 0 };
5217 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5218 obd_type, obd_name);
5222 fd = open(path.gl_pathv[0], flags);
5224 if (errno == EACCES && flags == O_RDONLY) {
5229 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5234 if (flags == O_RDONLY)
5235 rc = read(fd, buf, sizeof(buf));
5237 rc = write(fd, buf, sizeof(buf));
5245 cfs_free_param_data(&path);
5249 int llapi_target_iterate(int type_num, char **obd_type,
5250 void *args, llapi_cb_t cb)
5256 for (i = 0; i < type_num; i++) {
5259 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5263 for (j = 0; j < param.gl_pathc; j++) {
5264 char obd_uuid[UUID_MAX + 1];
5268 fp = fopen(param.gl_pathv[j], "r");
5271 llapi_error(LLAPI_MSG_ERROR, rc,
5272 "error: opening '%s'",
5277 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5279 llapi_error(LLAPI_MSG_ERROR, rc,
5280 "error: reading '%s'",
5285 /* Extract the obd_name from the sysfs path.
5286 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5288 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5294 /* skip /fs/lustre/'obd_type'/ */
5295 obd_name += strlen(obd_type[i]) + 12;
5296 /* chop off after obd_name */
5297 ptr = strrchr(obd_name, '/');
5301 cb(obd_type[i], obd_name, obd_uuid, args);
5310 cfs_free_param_data(¶m);
5314 static void do_target_check(char *obd_type_name, char *obd_name,
5315 char *obd_uuid, void *args)
5319 rc = llapi_ping(obd_type_name, obd_name);
5320 if (rc == ENOTCONN) {
5321 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5323 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5325 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5329 int llapi_target_check(int type_num, char **obd_type, char *dir)
5331 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5334 #undef MAX_STRING_SIZE
5336 /* Is this a lustre fs? */
5337 int llapi_is_lustre_mnttype(const char *type)
5339 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
5342 /* Is this a lustre client fs? */
5343 int llapi_is_lustre_mnt(struct mntent *mnt)
5345 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5346 strstr(mnt->mnt_fsname, ":/") != NULL);
5349 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5351 char fsname[PATH_MAX + 1];
5355 rc = llapi_search_fsname(mnt, fsname);
5359 root = open(mnt, O_RDONLY | O_DIRECTORY);
5362 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
5366 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
5374 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
5375 * format must have %s%s, buf must be > 16
5376 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
5377 * then buf = "lustre-MDT0000"
5379 static int get_mdtname(char *name, char *format, char *buf)
5381 char suffix[]="-MDT0000";
5382 int len = strlen(name);
5384 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
5385 name[len - 5] = '\0';
5390 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
5393 /* Not enough room to add suffix */
5394 llapi_err_noerrno(LLAPI_MSG_ERROR,
5395 "Invalid MDT name |%s|", name);
5400 return sprintf(buf, format, name, suffix);
5403 /** ioctl on filsystem root, with mdtindex sent as data
5404 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
5405 * \param mdtidxp pointer to integer within data to be filled in with the
5406 * mdt index (0 if no mdt is specified). NULL won't be filled.
5408 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
5416 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
5417 Open root and parse mdt index. */
5418 if (mdtname[0] == '/') {
5420 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
5421 (char *)mdtname, -1);
5423 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
5425 ptr = fsname + strlen(fsname) - 8;
5427 index = strtol(ptr + 4, NULL, 16);
5428 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
5432 llapi_err_noerrno(LLAPI_MSG_ERROR,
5433 "Can't open %s: %d\n", mdtname, rc);
5440 rc = ioctl(fd, opc, data);
5449 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
5450 int buflen, long long *recno, int *linkno)
5452 const char *fidstr_orig = fidstr;
5454 struct getinfo_fid2path *gf;
5459 while (*fidstr == '[')
5462 sscanf(fidstr, SFID, RFID(&fid));
5463 if (!fid_is_sane(&fid)) {
5464 llapi_err_noerrno(LLAPI_MSG_ERROR,
5465 "bad FID format '%s', should be [seq:oid:ver]"
5466 " (e.g. "DFID")\n", fidstr_orig,
5467 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
5471 gf = malloc(sizeof(*gf) + buflen);
5476 gf->gf_recno = *recno;
5477 gf->gf_linkno = *linkno;
5478 gf->gf_pathlen = buflen;
5480 /* Take path or fsname */
5481 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
5486 /* strip out instances of // */
5487 for (a = gf->gf_u.gf_path; *a != '\0'; a++) {
5488 if ((*a == '/') && (*(a + 1) == '/'))
5495 if (buf[0] == '\0') { /* ROOT path */
5500 *recno = gf->gf_recno;
5501 *linkno = gf->gf_linkno;
5508 static int fid_from_lma(const char *path, int fd, struct lu_fid *fid)
5511 struct lustre_mdt_attrs *lma;
5515 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
5517 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
5520 lma = (struct lustre_mdt_attrs *)buf;
5521 fid_le_to_cpu(fid, &lma->lma_self_fid);
5525 int llapi_get_mdt_index_by_fid(int fd, const struct lu_fid *fid,
5530 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
5539 int llapi_fd2fid(int fd, struct lu_fid *fid)
5543 memset(fid, 0, sizeof(*fid));
5545 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
5546 if (rc == -EINVAL || rc == -ENOTTY)
5547 rc = fid_from_lma(NULL, fd, fid);
5552 int llapi_path2fid(const char *path, struct lu_fid *fid)
5556 memset(fid, 0, sizeof(*fid));
5557 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5559 if (errno == ELOOP || errno == ENXIO)
5560 return fid_from_lma(path, -1, fid);
5564 rc = llapi_fd2fid(fd, fid);
5565 if (rc == -EINVAL || rc == -ENOTTY)
5566 rc = fid_from_lma(path, -1, fid);
5572 int llapi_fd2parent(int fd, unsigned int linkno, struct lu_fid *parent_fid,
5573 char *name, size_t name_size)
5575 struct getparent *gp;
5578 gp = malloc(sizeof(*gp) + name_size);
5582 gp->gp_linkno = linkno;
5583 gp->gp_name_size = name_size;
5585 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
5591 *parent_fid = gp->gp_fid;
5593 strncpy(name, gp->gp_name, name_size);
5594 name[name_size - 1] = '\0';
5601 int llapi_path2parent(const char *path, unsigned int linkno,
5602 struct lu_fid *parent_fid, char *name, size_t name_size)
5607 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5611 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
5616 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
5621 root = open(mnt, O_RDONLY | O_DIRECTORY);
5624 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
5628 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
5631 llapi_error(LLAPI_MSG_ERROR, rc,
5632 "ioctl on %s for getting connect flags failed", mnt);
5639 * Get a 64-bit value representing the version of file data pointed by fd.
5641 * Each write or truncate, flushed on OST, will change this value. You can use
5642 * this value to verify if file data was modified. This only checks the file
5643 * data, not metadata.
5645 * \param flags 0: no flush pages, usually used it the process has already
5647 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
5649 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
5650 * pages from clients.
5652 * \retval 0 on success.
5653 * \retval -errno on error.
5655 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
5658 struct ioc_data_version idv;
5660 idv.idv_flags = (__u32)flags;
5662 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5666 *data_version = idv.idv_version;
5672 * Flush cached pages from all clients.
5674 * \param fd File descriptor
5678 int llapi_file_flush(int fd)
5682 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);
5686 * Fetch layout version from OST objects. Layout version on OST objects are
5687 * only set when the file is a mirrored file AND after the file has been
5688 * written at least once.
5690 * It actually fetches the least layout version from the objects.
5692 int llapi_get_ost_layout_version(int fd, __u32 *layout_version)
5695 struct ioc_data_version idv = { 0 };
5697 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5701 *layout_version = idv.idv_layout_version;
5707 * Create a file without any name and open it for read/write
5709 * - file is created as if it were a standard file in the given \a directory
5710 * - file does not appear in \a directory and mtime does not change because
5711 * the filename is handled specially by the Lustre MDS.
5712 * - file is destroyed at final close
5714 * \param[in] directory directory from which to inherit layout/MDT idx
5715 * \param[in] mdt_idx MDT index on which the file is created,
5716 * \a idx == -1 means no specific MDT is requested
5717 * \param[in] mode standard open(2) mode
5718 * \param[in] stripe_param stripe parameters. May be NULL.
5720 * \retval a file descriptor on success.
5721 * \retval -errno on error.
5723 int llapi_create_volatile_param(const char *directory, int mdt_idx,
5724 int open_flags, mode_t mode,
5725 const struct llapi_stripe_param *stripe_param)
5727 char file_path[PATH_MAX];
5728 int saved_errno = errno;
5730 unsigned int rnumber;
5736 rc = snprintf(file_path, sizeof(file_path),
5737 "%s/" LUSTRE_VOLATILE_HDR "::%.4X",
5738 directory, rnumber);
5740 rc = snprintf(file_path, sizeof(file_path),
5741 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X",
5742 directory, mdt_idx, rnumber);
5744 if (rc < 0 || rc >= sizeof(file_path))
5745 return -ENAMETOOLONG;
5748 * Either open O_WRONLY or O_RDWR, creating RDONLY
5749 * is non-sensical here
5751 if ((open_flags & O_ACCMODE) == O_RDONLY)
5752 open_flags = O_RDWR | (open_flags & ~O_ACCMODE);
5754 open_flags |= O_CREAT | O_EXCL | O_NOFOLLOW;
5756 if (stripe_param != NULL) {
5757 fd = llapi_file_open_param(file_path, open_flags,
5758 mode, stripe_param);
5762 fd = open(file_path, open_flags, mode);
5766 } while (fd < 0 && rc == -EEXIST);
5769 llapi_error(LLAPI_MSG_ERROR, rc,
5770 "Cannot create volatile file '%s' in '%s'",
5771 file_path + strlen(directory) + 1 +
5772 LUSTRE_VOLATILE_HDR_LEN,
5778 * Unlink file in case this wasn't a Lustre filesystem and the magic
5779 * volatile filename wasn't handled as intended. The effect is the
5780 * same. If volatile open was supported then we expect unlink() to
5783 (void)unlink(file_path);
5786 * Since we are returning successfully we restore errno (and
5787 * mask out possible EEXIST from open() and ENOENT from unlink().
5789 errno = saved_errno;
5795 * Create a file without any name open it for read/write
5797 * - file is created as if it were a standard file in the given \a directory
5798 * - file does not appear in \a directory and mtime does not change because
5799 * the filename is handled specially by the Lustre MDS.
5800 * - file is removed at final close
5801 * - file modes are rw------- since it doesn't make sense to have a read-only
5802 * or write-only file that cannot be opened again.
5803 * - if user wants another mode it must use fchmod() on the open file, no
5804 * security problems arise because it cannot be opened by another process.
5806 * \param[in] directory directory from which to inherit layout/MDT idx
5807 * \param[in] idx MDT index on which the file is created,
5808 * \a idx == -1 means no specific MDT is requested
5809 * \param[in] open_flags standard open(2) flags
5811 * \retval a file descriptor on success.
5812 * \retval -errno on error.
5814 int llapi_create_volatile_idx(const char *directory, int mdt_idx,
5817 return llapi_create_volatile_param(directory, mdt_idx, open_flags,
5818 S_IRUSR | S_IWUSR, NULL);
5822 * Swap the layouts between 2 file descriptors
5823 * the 2 files must be open for writing
5824 * first fd received the ioctl, second fd is passed as arg
5825 * this is assymetric but avoid use of root path for ioctl
5827 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
5828 int gid, __u64 flags)
5830 struct lustre_swap_layouts lsl;
5835 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5836 rc = fstat(fd1, &st1);
5840 rc = fstat(fd2, &st2);
5845 lsl.sl_flags = flags;
5849 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
5853 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5854 struct timeval tv1[2];
5855 struct timeval tv2[2];
5857 memset(tv1, 0, sizeof(tv1));
5858 memset(tv2, 0, sizeof(tv2));
5860 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
5861 tv1[0].tv_sec = st1.st_atime;
5862 tv2[0].tv_sec = st2.st_atime;
5864 tv1[0].tv_sec = st2.st_atime;
5865 tv2[0].tv_sec = st1.st_atime;
5868 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
5869 tv1[1].tv_sec = st1.st_mtime;
5870 tv2[1].tv_sec = st2.st_mtime;
5872 tv1[1].tv_sec = st2.st_mtime;
5873 tv2[1].tv_sec = st1.st_mtime;
5876 rc = futimes(fd1, tv1);
5880 rc = futimes(fd2, tv2);
5888 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
5895 while (grp_id == 0);
5897 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
5905 * Swap the layouts between 2 files
5906 * the 2 files are open in write
5908 int llapi_swap_layouts(const char *path1, const char *path2,
5909 __u64 dv1, __u64 dv2, __u64 flags)
5913 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
5916 llapi_error(LLAPI_MSG_ERROR, rc,
5917 "error: cannot open '%s' for write", path1);
5921 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
5924 llapi_error(LLAPI_MSG_ERROR, rc,
5925 "error: cannot open '%s' for write", path2);
5929 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
5931 llapi_error(LLAPI_MSG_ERROR, rc,
5932 "error: cannot swap layout between '%s' and '%s'",
5943 * Attempt to open a file with Lustre file identifier \a fid
5944 * and return an open file descriptor.
5946 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
5947 * \param[in] fid Lustre file identifier of file to open
5948 * \param[in] flags open() flags
5950 * \retval non-negative file descriptor on successful open
5951 * \retval -1 if an error occurred
5953 int llapi_open_by_fid(const char *lustre_dir, const struct lu_fid *fid,
5956 char mntdir[PATH_MAX];
5957 char path[PATH_MAX + 64];
5960 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
5964 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
5965 return open(path, flags);
5971 * \param fd File to lock.
5972 * \param gid Group Identifier.
5974 * \retval 0 on success.
5975 * \retval -errno on failure.
5977 int llapi_group_lock(int fd, int gid)
5981 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
5984 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
5992 * \param fd File to unlock.
5993 * \param gid Group Identifier.
5995 * \retval 0 on success.
5996 * \retval -errno on failure.
5998 int llapi_group_unlock(int fd, int gid)
6002 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
6005 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");