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)
389 static int page_size;
392 if (page_size == 0) {
393 /* 64 KB is the largest common page size (on ia64/PPC/ARM),
394 * but check the local page size just in case. The page_size
395 * will not change for the lifetime of this process at least.
397 page_size = LOV_MIN_STRIPE_SIZE;
398 if (getpagesize() > page_size) {
399 page_size = getpagesize();
400 llapi_err_noerrno(LLAPI_MSG_WARN,
401 "warning: page size (%u) larger than expected (%u)",
402 page_size, LOV_MIN_STRIPE_SIZE);
405 if (!llapi_stripe_size_is_aligned(stripe_size)) {
407 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
408 "must be an even multiple of %d bytes",
409 stripe_size, page_size);
412 if (!llapi_stripe_index_is_valid(stripe_offset)) {
414 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
418 if (!llapi_stripe_count_is_valid(stripe_count)) {
420 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
424 if (llapi_stripe_size_is_too_big(stripe_size)) {
426 llapi_error(LLAPI_MSG_ERROR, rc,
427 "error: stripe size '%llu' over 4GB limit",
437 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
442 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
444 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
448 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
450 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
455 if (!llapi_dir_hash_type_is_valid(hash_type)) {
457 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
465 * Trim a trailing newline from a string, if it exists.
467 int llapi_chomp_string(char *buf)
483 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
485 static int get_param_lov(const char *path, const char *param,
486 char *buf, size_t buf_size)
488 struct obd_uuid uuid;
491 rc = llapi_file_get_lov_uuid(path, &uuid);
495 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
500 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
502 static int get_param_lmv(const char *path, const char *param,
503 char *buf, size_t buf_size)
505 struct obd_uuid uuid;
508 rc = llapi_file_get_lmv_uuid(path, &uuid);
512 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
516 static int get_mds_md_size(const char *path)
518 char buf[PATH_MAX], inst[PATH_MAX];
519 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
522 rc = llapi_getname(path, inst, sizeof(inst));
526 /* Get the max ea size from llite parameters. */
527 rc = get_lustre_param_value("llite", inst, FILTER_BY_EXACT,
528 "max_easize", buf, sizeof(buf));
534 return rc > 0 ? rc : md_size;
537 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
539 return get_param_lmv(path, "uuid", buf, bufsize);
543 * if pool is NULL, search tgtname in target_obd
544 * if pool is not NULL:
545 * if pool not found returns errno < 0
546 * if tgtname is NULL, returns 1 if pool is not empty and 0 if pool empty
547 * if tgtname is not NULL, returns 1 if OST is in pool and 0 if not
549 int llapi_search_tgt(char *fsname, char *poolname, char *tgtname, bool is_mdt)
551 char buffer[PATH_MAX];
557 /* You need one or the other */
558 if (poolname == NULL && fsname == NULL)
562 len = strlen(tgtname);
564 if (poolname == NULL && len == 0)
567 /* Search by poolname and fsname if is not NULL */
568 if (poolname != NULL) {
569 rc = poolpath(¶m, fsname, NULL);
571 snprintf(buffer, sizeof(buffer), "%s/%s",
572 param.gl_pathv[0], poolname);
574 } else if (fsname != NULL) {
575 rc = get_lustre_param_path(is_mdt ? "lmv" : "lov", fsname,
577 "target_obd", ¶m);
579 strncpy(buffer, param.gl_pathv[0],
585 cfs_free_param_data(¶m);
589 fd = fopen(buffer, "r");
593 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
594 if (poolname == NULL) {
596 /* Search for an tgtname in the list of targets
597 * Line format is IDX: fsname-OST/MDTxxxx_UUID STATUS */
598 ptr = strchr(buffer, ' ');
600 (strncmp(ptr + 1, tgtname, len) == 0)) {
605 /* Search for an tgtname in a pool,
606 * (or an existing non-empty pool if no tgtname) */
607 if ((tgtname == NULL) ||
608 (strncmp(buffer, tgtname, len) == 0)) {
618 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
620 return llapi_search_tgt(fsname, poolname, ostname, false);
624 * Open a Lustre file.
626 * \param name the name of the file to be opened
627 * \param flags access mode, see flags in open(2)
628 * \param mode permission of the file if it is created, see mode in open(2)
629 * \param param stripe pattern of the newly created file
631 * \retval file descriptor of opened file
632 * \retval negative errno on failure
634 int llapi_file_open_param(const char *name, int flags, mode_t mode,
635 const struct llapi_stripe_param *param)
637 char fsname[MAX_OBD_NAME + 1] = { 0 };
638 char *pool_name = param->lsp_pool;
639 struct lov_user_md *lum = NULL;
640 size_t lum_size = sizeof(*lum);
643 /* Make sure we are on a Lustre file system */
644 rc = llapi_search_fsname(name, fsname);
646 llapi_error(LLAPI_MSG_ERROR, rc,
647 "'%s' is not on a Lustre filesystem",
652 /* Check if the stripe pattern is sane. */
653 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
654 param->lsp_stripe_offset,
655 param->lsp_stripe_count,
656 param->lsp_stripe_pattern);
660 /* Make sure we have a good pool */
661 if (pool_name != NULL) {
662 /* in case user gives the full pool name <fsname>.<poolname>,
663 * strip the fsname */
664 char *ptr = strchr(pool_name, '.');
667 if (strcmp(pool_name, fsname) != 0) {
669 llapi_err_noerrno(LLAPI_MSG_ERROR,
670 "Pool '%s' is not on filesystem '%s'",
677 /* Make sure the pool exists and is non-empty */
678 rc = llapi_search_ost(fsname, pool_name, NULL);
680 char *err = rc == 0 ? "has no OSTs" : "does not exist";
682 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
683 fsname, pool_name, err);
687 lum_size = sizeof(struct lov_user_md_v3);
690 /* sanity check of target list */
691 if (param->lsp_is_specific) {
692 char ostname[MAX_OBD_NAME + 64];
696 for (i = 0; i < param->lsp_stripe_count; i++) {
697 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
698 fsname, param->lsp_osts[i]);
699 rc = llapi_search_ost(fsname, pool_name, ostname);
704 llapi_error(LLAPI_MSG_ERROR, rc,
705 "%s: cannot find OST %s in %s",
712 /* Make sure stripe offset is in OST list. */
713 if (param->lsp_osts[i] == param->lsp_stripe_offset)
717 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
718 "%s: stripe offset '%d' is not in the "
720 __func__, param->lsp_stripe_offset);
724 lum_size = lov_user_md_size(param->lsp_stripe_count,
725 LOV_USER_MAGIC_SPECIFIC);
728 lum = calloc(1, lum_size);
733 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
735 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
736 flags = O_DIRECTORY | O_RDONLY;
743 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
748 /* Initialize IOCTL striping pattern structure */
749 lum->lmm_magic = LOV_USER_MAGIC_V1;
750 lum->lmm_pattern = param->lsp_stripe_pattern;
751 lum->lmm_stripe_size = param->lsp_stripe_size;
752 lum->lmm_stripe_count = param->lsp_stripe_count;
753 lum->lmm_stripe_offset = param->lsp_stripe_offset;
754 if (pool_name != NULL) {
755 struct lov_user_md_v3 *lumv3 = (void *)lum;
757 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
758 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
760 if (param->lsp_is_specific) {
761 struct lov_user_md_v3 *lumv3 = (void *)lum;
764 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
765 if (pool_name == NULL) {
766 /* LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
767 * OST list, therefore if pool is not specified we have
768 * to pack a null pool name for placeholder. */
769 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
772 for (i = 0; i < param->lsp_stripe_count; i++)
773 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
776 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
777 char *errmsg = "stripe already set";
780 if (errno != EEXIST && errno != EALREADY)
781 errmsg = strerror(errno);
783 llapi_err_noerrno(LLAPI_MSG_ERROR,
784 "setstripe error for '%s': %s", name, errmsg);
795 int llapi_file_open_pool(const char *name, int flags, int mode,
796 unsigned long long stripe_size, int stripe_offset,
797 int stripe_count, int stripe_pattern, char *pool_name)
799 const struct llapi_stripe_param param = {
800 .lsp_stripe_size = stripe_size,
801 .lsp_stripe_count = stripe_count,
802 .lsp_stripe_pattern = stripe_pattern,
803 .lsp_stripe_offset = stripe_offset,
804 .lsp_pool = pool_name
806 return llapi_file_open_param(name, flags, mode, ¶m);
809 int llapi_file_open(const char *name, int flags, int mode,
810 unsigned long long stripe_size, int stripe_offset,
811 int stripe_count, int stripe_pattern)
813 return llapi_file_open_pool(name, flags, mode, stripe_size,
814 stripe_offset, stripe_count,
815 stripe_pattern, NULL);
818 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
819 __u32 flags, char *foreign_lov)
822 struct lov_foreign_md *lfm;
825 if (foreign_lov == NULL) {
826 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
827 "foreign LOV EA content must be provided");
831 len = strlen(foreign_lov);
832 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
834 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
835 "foreign LOV EA size %zu (must be 0 < len < %zu)",
836 len, XATTR_SIZE_MAX -
837 offsetof(struct lov_foreign_md, lfm_value));
841 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
843 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
844 "failed to allocate lov_foreign_md");
848 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
855 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
856 lfm->lfm_length = len;
857 lfm->lfm_type = type;
858 lfm->lfm_flags = flags;
859 memcpy(lfm->lfm_value, foreign_lov, len);
861 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
862 char *errmsg = "stripe already set";
863 char fsname[MAX_OBD_NAME + 1] = { 0 };
866 if (errno != EEXIST && errno != EALREADY)
867 errmsg = strerror(errno);
869 llapi_err_noerrno(LLAPI_MSG_ERROR,
870 "setstripe error for '%s': %s", name, errmsg);
872 /* Make sure we are on a Lustre file system */
873 if (rc == -ENOTTY && llapi_search_fsname(name, fsname))
874 llapi_error(LLAPI_MSG_ERROR, rc,
875 "'%s' is not on a Lustre filesystem",
888 int llapi_file_create(const char *name, unsigned long long stripe_size,
889 int stripe_offset, int stripe_count, int stripe_pattern)
893 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
894 stripe_offset, stripe_count, stripe_pattern,
903 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
904 int stripe_offset, int stripe_count,
905 int stripe_pattern, char *pool_name)
909 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
910 stripe_offset, stripe_count, stripe_pattern,
919 static int verify_dir_param(const char *name,
920 const struct llapi_stripe_param *param)
922 char fsname[MAX_OBD_NAME + 1] = { 0 };
923 char *pool_name = param->lsp_pool;
926 /* Make sure we are on a Lustre file system */
927 rc = llapi_search_fsname(name, fsname);
929 llapi_error(LLAPI_MSG_ERROR, rc,
930 "'%s' is not on a Lustre filesystem",
935 /* Check if the stripe pattern is sane. */
936 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
937 param->lsp_stripe_count,
938 param->lsp_stripe_pattern);
942 /* Make sure we have a good pool */
943 if (pool_name != NULL) {
944 /* in case user gives the full pool name <fsname>.<poolname>,
945 * strip the fsname */
946 char *ptr = strchr(pool_name, '.');
950 if (strcmp(pool_name, fsname) != 0) {
952 llapi_err_noerrno(LLAPI_MSG_ERROR,
953 "Pool '%s' is not on filesystem '%s'",
960 /* Make sure the pool exists and is non-empty */
961 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
963 char *err = rc == 0 ? "has no OSTs" : "does not exist";
965 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
966 fsname, pool_name, err);
971 /* sanity check of target list */
972 if (param->lsp_is_specific) {
973 char mdtname[MAX_OBD_NAME + 64];
977 for (i = 0; i < param->lsp_stripe_count; i++) {
978 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
979 fsname, param->lsp_tgts[i]);
980 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
985 llapi_error(LLAPI_MSG_ERROR, rc,
986 "%s: cannot find MDT %s in %s",
993 /* Make sure stripe offset is in MDT list. */
994 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
998 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
999 "%s: stripe offset '%d' is not in the "
1001 __func__, param->lsp_stripe_offset);
1009 static inline void param2lmu(struct lmv_user_md *lmu,
1010 const struct llapi_stripe_param *param)
1012 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
1014 lmu->lum_stripe_count = param->lsp_stripe_count;
1015 lmu->lum_stripe_offset = param->lsp_stripe_offset;
1016 lmu->lum_hash_type = param->lsp_stripe_pattern;
1017 if (param->lsp_pool != NULL)
1018 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
1019 if (param->lsp_is_specific) {
1022 for (i = 0; i < param->lsp_stripe_count; i++)
1023 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
1027 int llapi_dir_set_default_lmv(const char *name,
1028 const struct llapi_stripe_param *param)
1030 struct lmv_user_md lmu = { 0 };
1034 rc = verify_dir_param(name, param);
1038 /* TODO: default lmv doesn't support specific targets yet */
1039 if (param->lsp_is_specific)
1042 param2lmu(&lmu, param);
1044 fd = open(name, O_DIRECTORY | O_RDONLY);
1047 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1051 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
1053 char *errmsg = "stripe already set";
1055 if (errno != EEXIST && errno != EALREADY)
1056 errmsg = strerror(errno);
1058 llapi_err_noerrno(LLAPI_MSG_ERROR,
1059 "default dirstripe error on '%s': %s",
1066 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
1067 int stripe_count, int stripe_pattern,
1068 const char *pool_name)
1070 const struct llapi_stripe_param param = {
1071 .lsp_stripe_count = stripe_count,
1072 .lsp_stripe_offset = stripe_offset,
1073 .lsp_stripe_pattern = stripe_pattern,
1074 .lsp_pool = (char *)pool_name
1077 return llapi_dir_set_default_lmv(name, ¶m);
1081 * Create a Lustre directory.
1083 * \param name the name of the directory to be created
1084 * \param mode permission of the file if it is created, see mode in open(2)
1085 * \param param stripe pattern of the newly created directory
1087 * \retval 0 on success
1088 * \retval negative errno on failure
1090 int llapi_dir_create(const char *name, mode_t mode,
1091 const struct llapi_stripe_param *param)
1093 struct lmv_user_md *lmu = NULL;
1095 struct obd_ioctl_data data = { 0 };
1098 char *dirpath = NULL;
1099 char *namepath = NULL;
1104 rc = verify_dir_param(name, param);
1108 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1109 param->lsp_is_specific ?
1110 LMV_USER_MAGIC_SPECIFIC :
1113 lmu = calloc(1, lmu_size);
1117 dirpath = strdup(name);
1123 namepath = strdup(name);
1130 param2lmu(lmu, param);
1132 filename = basename(namepath);
1133 dir = dirname(dirpath);
1135 data.ioc_inlbuf1 = (char *)filename;
1136 data.ioc_inllen1 = strlen(filename) + 1;
1137 data.ioc_inlbuf2 = (char *)lmu;
1138 data.ioc_inllen2 = lmu_size;
1139 data.ioc_type = mode;
1140 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1142 llapi_error(LLAPI_MSG_ERROR, rc,
1143 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1148 fd = open(dir, O_DIRECTORY | O_RDONLY);
1151 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1155 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1156 char *errmsg = "stripe already set";
1159 if (errno != EEXIST && errno != EALREADY)
1160 errmsg = strerror(errno);
1162 llapi_err_noerrno(LLAPI_MSG_ERROR,
1163 "dirstripe error on '%s': %s", name, errmsg);
1174 * Create a foreign directory.
1176 * \param name the name of the directory to be created
1177 * \param mode permission of the file if it is created, see mode in open(2)
1178 * \param type foreign type to be set in LMV EA
1179 * \param flags foreign flags to be set in LMV EA
1180 * \param value foreign pattern to be set in LMV EA
1182 * \retval 0 on success
1183 * \retval negative errno on failure
1185 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1186 __u32 flags, const char *value)
1188 struct lmv_foreign_md *lfm = NULL;
1189 size_t lfm_size, len;
1190 struct obd_ioctl_data data = { 0 };
1193 char *dirpath = NULL;
1194 char *namepath = NULL;
1199 len = strlen(value);
1200 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1203 llapi_error(LLAPI_MSG_ERROR, rc, "invalid LOV EA length %zu "
1204 "(must be 0 < len < %zu)\n", len,
1206 offsetof(struct lmv_foreign_md, lfm_value));
1209 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1210 lfm = calloc(1, lfm_size);
1214 dirpath = strdup(name);
1220 namepath = strdup(name);
1227 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1228 lfm->lfm_length = len;
1229 lfm->lfm_type = type;
1230 lfm->lfm_flags = flags;
1231 memcpy(lfm->lfm_value, value, len);
1233 filename = basename(namepath);
1234 dir = dirname(dirpath);
1236 data.ioc_inlbuf1 = (char *)filename;
1237 data.ioc_inllen1 = strlen(filename) + 1;
1238 data.ioc_inlbuf2 = (char *)lfm;
1239 data.ioc_inllen2 = lfm_size;
1240 data.ioc_type = mode;
1241 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1243 llapi_error(LLAPI_MSG_ERROR, rc,
1244 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1249 fd = open(dir, O_DIRECTORY | O_RDONLY);
1252 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1256 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1257 char *errmsg = "stripe already set";
1260 if (errno != EEXIST && errno != EALREADY)
1261 errmsg = strerror(errno);
1263 llapi_err_noerrno(LLAPI_MSG_ERROR,
1264 "dirstripe error on '%s': %s", name, errmsg);
1274 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1275 int stripe_count, int stripe_pattern,
1276 const char *pool_name)
1278 const struct llapi_stripe_param param = {
1279 .lsp_stripe_count = stripe_count,
1280 .lsp_stripe_offset = stripe_offset,
1281 .lsp_stripe_pattern = stripe_pattern,
1282 .lsp_pool = (char *)pool_name
1285 return llapi_dir_create(name, mode, ¶m);
1288 int llapi_direntry_remove(char *dname)
1290 char *dirpath = NULL;
1291 char *namepath = NULL;
1297 dirpath = strdup(dname);
1298 namepath = strdup(dname);
1299 if (!dirpath || !namepath)
1302 filename = basename(namepath);
1304 dir = dirname(dirpath);
1306 fd = open(dir, O_DIRECTORY | O_RDONLY);
1309 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
1314 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
1315 char *errmsg = strerror(errno);
1316 llapi_err_noerrno(LLAPI_MSG_ERROR,
1317 "error on ioctl %#jx for '%s' (%d): %s",
1318 (uintmax_t)LL_IOC_LMV_SETSTRIPE, filename,
1330 * Find the fsname, the full path, and/or an open fd.
1331 * Either the fsname or path must not be NULL
1333 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1336 char buf[PATH_MAX], mntdir[PATH_MAX];
1337 char *ptr, *ptr_end;
1339 int idx = 0, len = 0, mntlen, fd;
1342 /* get the mount point */
1343 fp = setmntent(PROC_MOUNTS, "r");
1346 llapi_error(LLAPI_MSG_ERROR, rc,
1347 "cannot retrieve filesystem mount point");
1351 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1354 if (!llapi_is_lustre_mnt(&mnt))
1357 if ((want & WANT_INDEX) && (idx++ != index))
1360 mntlen = strlen(mnt.mnt_dir);
1361 ptr = strchr(mnt.mnt_fsname, '/');
1362 while (ptr && *ptr == '/')
1364 /* thanks to the call to llapi_is_lustre_mnt() above,
1365 * we are sure that mnt.mnt_fsname contains ":/",
1366 * so ptr should never be NULL */
1370 while (*ptr_end != '/' && *ptr_end != '\0')
1373 /* Check the fsname for a match, if given */
1374 if (!(want & WANT_FSNAME) && fsname != NULL &&
1375 (strlen(fsname) > 0) &&
1376 (strncmp(ptr, fsname, ptr_end - ptr) != 0))
1379 /* If the path isn't set return the first one we find */
1380 if (path == NULL || strlen(path) == 0) {
1381 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1382 mntdir[strlen(mnt.mnt_dir)] = '\0';
1383 if ((want & WANT_FSNAME) && fsname != NULL) {
1384 strncpy(fsname, ptr, ptr_end - ptr);
1385 fsname[ptr_end - ptr] = '\0';
1389 /* Otherwise find the longest matching path */
1390 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1391 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1392 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir));
1393 mntdir[strlen(mnt.mnt_dir)] = '\0';
1395 if ((want & WANT_FSNAME) && fsname != NULL) {
1396 strncpy(fsname, ptr, ptr_end - ptr);
1397 fsname[ptr_end - ptr] = '\0';
1406 if ((want & WANT_PATH) && path != NULL) {
1407 strncpy(path, mntdir, PATH_MAX);
1408 path[strlen(mntdir)] = '\0';
1410 if (want & WANT_FD) {
1411 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1414 llapi_error(LLAPI_MSG_ERROR, rc,
1415 "cannot open '%s': %s", mntdir,
1422 } else if (want & WANT_ERROR)
1423 llapi_err_noerrno(LLAPI_MSG_ERROR,
1424 "'%s' not on a mounted Lustre filesystem",
1425 (want & WANT_PATH) ? fsname : path);
1430 * search lustre mounts
1432 * Calling this function will return to the user the mount point, mntdir, and
1433 * the file system name, fsname, if the user passed a buffer to this routine.
1435 * The user inputs are pathname and index. If the pathname is supplied then
1436 * the value of the index will be ignored. The pathname will return data if
1437 * the pathname is located on a lustre mount. Index is used to pick which
1438 * mount point you want in the case of multiple mounted lustre file systems.
1439 * See function lfs_osts in lfs.c for an example of the index use.
1441 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1444 int want = WANT_PATH, idx = -1;
1446 if (!pathname || pathname[0] == '\0') {
1450 strcpy(mntdir, pathname);
1453 want |= WANT_FSNAME;
1454 return get_root_path(want, fsname, NULL, mntdir, idx);
1457 /* Given a path, find the corresponding Lustre fsname */
1458 int llapi_search_fsname(const char *pathname, char *fsname)
1463 path = realpath(pathname, NULL);
1465 char tmp[PATH_MAX - 1];
1471 if (pathname[0] != '/') {
1472 /* Need an absolute path, but realpath() only works for
1473 * pathnames that actually exist. We go through the
1474 * extra hurdle of dirname(getcwd() + pathname) in
1475 * case the relative pathname contains ".." in it. */
1476 char realpath[PATH_MAX - 1];
1478 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1480 llapi_error(LLAPI_MSG_ERROR, rc,
1481 "cannot get current working directory");
1485 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1486 if (rc >= sizeof(tmp)) {
1488 llapi_error(LLAPI_MSG_ERROR, rc,
1489 "invalid parent path '%s'",
1495 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1496 if (rc >= sizeof(buf)) {
1498 llapi_error(LLAPI_MSG_ERROR, rc,
1499 "invalid path '%s'", pathname);
1502 path = realpath(buf, NULL);
1504 ptr = strrchr(buf, '/');
1506 llapi_error(LLAPI_MSG_ERROR |
1507 LLAPI_MSG_NO_ERRNO, 0,
1508 "cannot resolve path '%s'",
1513 path = realpath(buf, NULL);
1516 llapi_error(LLAPI_MSG_ERROR, rc,
1517 "cannot resolve path '%s'",
1523 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1528 int llapi_search_rootpath(char *pathname, const char *fsname)
1530 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1533 int llapi_getname(const char *path, char *buf, size_t size)
1535 struct obd_uuid uuid_buf;
1536 char *uuid = uuid_buf.uuid;
1538 int rc, len, fsname_len;
1540 memset(&uuid_buf, 0, sizeof(uuid_buf));
1541 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1546 * We want to turn testfs-clilov-ffff88002738bc00 into
1547 * testfs-ffff88002738bc00 in a portable way that doesn't depend
1548 * on what is after "-clilov-" as it may change in the future.
1549 * Unfortunately, the "fsname" part may contain a dash, so we
1550 * can't just skip to the first dash, and the "instance" may be a
1551 * UUID in the future, so we can't necessarily go to the last dash.
1553 cfg_instance = strstr(uuid, "-clilov-");
1557 fsname_len = cfg_instance - uuid;
1558 cfg_instance += strlen("-clilov-");
1559 len = snprintf(buf, size, "%.*s-%s", fsname_len, uuid, cfg_instance);
1568 * Get the list of pool members.
1569 * \param poolname string of format \<fsname\>.\<poolname\>
1570 * \param members caller-allocated array of char*
1571 * \param list_size size of the members array
1572 * \param buffer caller-allocated buffer for storing OST names
1573 * \param buffer_size size of the buffer
1575 * \return number of members retrieved for this pool
1576 * \retval -error failure
1578 int llapi_get_poolmembers(const char *poolname, char **members,
1579 int list_size, char *buffer, int buffer_size)
1581 char fsname[PATH_MAX];
1590 /* name is FSNAME.POOLNAME */
1591 if (strlen(poolname) >= sizeof(fsname))
1594 snprintf(fsname, sizeof(fsname), "%s", poolname);
1595 pool = strchr(fsname, '.');
1602 rc = poolpath(&pathname, fsname, NULL);
1604 llapi_error(LLAPI_MSG_ERROR, rc,
1605 "Lustre filesystem '%s' not found",
1610 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1611 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1612 cfs_free_param_data(&pathname);
1613 if (rc >= sizeof(buf))
1615 fd = fopen(buf, "r");
1618 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1623 while (fgets(buf, sizeof(buf), fd) != NULL) {
1624 if (nb_entries >= list_size) {
1628 buf[sizeof(buf) - 1] = '\0';
1630 tmp = strchr(buf, '\n');
1633 if (used + strlen(buf) + 1 > buffer_size) {
1638 strcpy(buffer + used, buf);
1639 members[nb_entries] = buffer + used;
1640 used += strlen(buf) + 1;
1650 * Get the list of pools in a filesystem.
1651 * \param name filesystem name or path
1652 * \param poollist caller-allocated array of char*
1653 * \param list_size size of the poollist array
1654 * \param buffer caller-allocated buffer for storing pool names
1655 * \param buffer_size size of the buffer
1657 * \return number of pools retrieved for this filesystem
1658 * \retval -error failure
1660 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1661 char *buffer, int buffer_size)
1663 char rname[PATH_MAX];
1668 struct dirent *pool;
1670 unsigned int nb_entries = 0;
1671 unsigned int used = 0;
1674 /* initialize output array */
1675 for (i = 0; i < list_size; i++)
1678 /* is name a pathname ? */
1679 ptr = strchr(name, '/');
1681 /* only absolute pathname is supported */
1685 if (!realpath(name, rname)) {
1687 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1692 fsname = strdup(rname);
1696 rc = poolpath(&pathname, NULL, rname);
1698 /* name is FSNAME */
1699 fsname = strdup(name);
1702 rc = poolpath(&pathname, fsname, NULL);
1705 llapi_error(LLAPI_MSG_ERROR, rc,
1706 "Lustre filesystem '%s' not found", name);
1710 dir = opendir(pathname.gl_pathv[0]);
1713 llapi_error(LLAPI_MSG_ERROR, rc,
1714 "Could not open pool list for '%s'",
1721 pool = readdir(dir);
1727 /* ignore . and .. */
1728 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1731 /* check output bounds */
1732 if (nb_entries >= list_size) {
1734 goto free_dir_no_msg;
1737 /* +2 for '.' and final '\0' */
1738 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1741 goto free_dir_no_msg;
1744 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1745 poollist[nb_entries] = buffer + used;
1746 used += strlen(pool->d_name) + strlen(fsname) + 2;
1752 llapi_error(LLAPI_MSG_ERROR, rc,
1753 "Error reading pool list for '%s'", name);
1755 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1760 cfs_free_param_data(&pathname);
1763 return rc != 0 ? rc : nb_entries;
1766 /* wrapper for lfs.c and obd.c */
1767 int llapi_poollist(const char *name)
1769 /* list of pool names (assume that pool count is smaller
1771 char **list, *buffer = NULL, *fsname = (char *)name;
1772 char *poolname = NULL, *tmp = NULL, data[16];
1773 enum param_filter type = FILTER_BY_PATH;
1774 int obdcount, bufsize, rc, nb, i;
1779 if (name[0] != '/') {
1780 fsname = strdup(name);
1784 poolname = strchr(fsname, '.');
1787 type = FILTER_BY_FS_NAME;
1790 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1791 data, sizeof(data));
1794 obdcount = atoi(data);
1796 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1797 * and also an array to store the pointers for all that
1798 * allocated space. */
1800 bufsize = sizeof(struct obd_uuid) * obdcount;
1801 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1802 if (buffer == NULL) {
1806 list = (char **) (buffer + bufsize);
1809 /* name is a path or fsname */
1810 nb = llapi_get_poollist(name, list, obdcount,
1813 /* name is a pool name (<fsname>.<poolname>) */
1814 nb = llapi_get_poolmembers(name, list, obdcount,
1818 if (nb == -EOVERFLOW) {
1821 goto retry_get_pools;
1824 for (i = 0; i < nb; i++)
1825 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1826 rc = (nb < 0 ? nb : 0);
1830 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1835 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1836 void *data, struct dirent64 *de);
1838 #define OBD_NOT_FOUND (-1)
1840 static void find_param_fini(struct find_param *param)
1842 if (param->fp_migrate)
1845 if (param->fp_obd_indexes) {
1846 free(param->fp_obd_indexes);
1847 param->fp_obd_indexes = NULL;
1850 if (param->fp_lmd) {
1851 free(param->fp_lmd);
1852 param->fp_lmd = NULL;
1855 if (param->fp_lmv_md) {
1856 free(param->fp_lmv_md);
1857 param->fp_lmv_md = NULL;
1861 static int common_param_init(struct find_param *param, char *path)
1863 int lum_size = get_mds_md_size(path);
1868 /* migrate has fp_lmv_md initialized outside */
1869 if (param->fp_migrate)
1872 if (lum_size < PATH_MAX + 1)
1873 lum_size = PATH_MAX + 1;
1875 param->fp_lum_size = lum_size;
1876 param->fp_lmd = calloc(1, sizeof(lstat_t) + lum_size);
1877 if (param->fp_lmd == NULL) {
1878 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1879 "error: allocation of %zu bytes for ioctl",
1880 sizeof(lstat_t) + param->fp_lum_size);
1884 param->fp_lmv_stripe_count = 256;
1885 param->fp_lmv_md = calloc(1,
1886 lmv_user_md_size(param->fp_lmv_stripe_count,
1887 LMV_USER_MAGIC_SPECIFIC));
1888 if (param->fp_lmv_md == NULL) {
1889 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1890 "error: allocation of %d bytes for ioctl",
1891 lmv_user_md_size(param->fp_lmv_stripe_count,
1892 LMV_USER_MAGIC_SPECIFIC));
1893 find_param_fini(param);
1897 param->fp_got_uuids = 0;
1898 param->fp_obd_indexes = NULL;
1899 param->fp_obd_index = OBD_NOT_FOUND;
1900 param->fp_mdt_index = OBD_NOT_FOUND;
1904 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1905 struct dirent64 *de)
1907 struct find_param *param = data;
1913 /* set errno upon failure */
1914 static DIR *opendir_parent(const char *path)
1920 path_copy = strdup(path);
1921 if (path_copy == NULL)
1924 parent_path = dirname(path_copy);
1925 parent = opendir(parent_path);
1931 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1936 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1937 if (param->fp_get_default_lmv)
1938 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1940 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1942 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1943 if (errno == E2BIG && ret != 0) {
1947 /* if foreign LMV case, fake stripes number */
1948 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
1949 struct lmv_foreign_md *lfm;
1951 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1952 if (lfm->lfm_length < XATTR_SIZE_MAX -
1953 offsetof(typeof(*lfm), lfm_value)) {
1954 uint32_t size = lfm->lfm_length +
1955 offsetof(typeof(*lfm), lfm_value);
1957 stripe_count = lmv_foreign_to_md_stripes(size);
1959 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1960 "error: invalid %d foreign size "
1961 "returned from ioctl",
1966 stripe_count = param->fp_lmv_md->lum_stripe_count;
1968 if (stripe_count <= param->fp_lmv_stripe_count)
1971 free(param->fp_lmv_md);
1972 param->fp_lmv_stripe_count = stripe_count;
1973 lmv_size = lmv_user_md_size(stripe_count,
1974 LMV_USER_MAGIC_SPECIFIC);
1975 param->fp_lmv_md = malloc(lmv_size);
1976 if (param->fp_lmv_md == NULL) {
1977 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1978 "error: allocation of %d bytes for ioctl",
1979 lmv_user_md_size(param->fp_lmv_stripe_count,
1980 LMV_USER_MAGIC_SPECIFIC));
1988 int get_lmd_info_fd(char *path, int parent_fd, int dir_fd,
1989 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1991 struct lov_user_mds_data *lmd = lmdbuf;
1992 lstat_t *st = &lmd->lmd_st;
1995 if (parent_fd < 0 && dir_fd < 0)
1997 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
2001 /* LL_IOC_MDC_GETINFO operates on the current directory inode
2002 * and returns struct lov_user_mds_data, while
2003 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
2005 ret = ioctl(dir_fd, type == GET_LMD_INFO ? LL_IOC_MDC_GETINFO :
2006 LL_IOC_LOV_GETSTRIPE,
2008 } else if (parent_fd >= 0) {
2009 char *fname = strrchr(path, '/');
2011 /* IOC_MDC_GETFILEINFO takes as input the filename (relative to
2012 * the parent directory) and returns struct lov_user_mds_data,
2013 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2015 * This avoids opening, locking, and closing each file on the
2016 * client if that is not needed. Multiple of these ioctl() can
2017 * be done on the parent dir with a single open for all
2018 * files in that directory, and it also doesn't pollute the
2019 * client dcache with millions of dentries when traversing
2020 * a large filesystem.
2022 fname = (fname == NULL ? path : fname + 1);
2024 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2027 else if (ret >= lmdlen || ret++ == 0)
2030 ret = ioctl(parent_fd, type == GET_LMD_INFO ?
2031 IOC_MDC_GETFILEINFO :
2032 IOC_MDC_GETFILESTRIPE, lmdbuf);
2035 if (ret && type == GET_LMD_INFO) {
2036 if (errno == ENOTTY) {
2037 /* ioctl is not supported, it is not a lustre fs.
2038 * Do the regular lstat(2) instead.
2040 ret = lstat_f(path, st);
2043 llapi_error(LLAPI_MSG_ERROR, ret,
2044 "error: %s: lstat failed for %s",
2047 } else if (errno == ENOENT) {
2049 llapi_error(LLAPI_MSG_WARN, ret,
2050 "warning: %s does not exist", path);
2051 } else if (errno != EISDIR && errno != ENODATA) {
2053 llapi_error(LLAPI_MSG_ERROR, ret,
2054 "%s ioctl failed for %s.",
2055 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2056 "IOC_MDC_GETFILEINFO", path);
2063 static int get_lmd_info(char *path, DIR *parent, DIR *dir, void *lmdbuf,
2064 int lmdlen, enum get_lmd_info_type type)
2070 parent_fd = dirfd(parent);
2072 dir_fd = dirfd(dir);
2074 return get_lmd_info_fd(path, parent_fd, dir_fd, lmdbuf, lmdlen, type);
2077 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
2078 semantic_func_t sem_init,
2079 semantic_func_t sem_fini, void *data,
2080 struct dirent64 *de)
2082 struct find_param *param = (struct find_param *)data;
2083 struct dirent64 *dent;
2091 if (!d && errno != ENOTDIR) {
2093 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2096 } else if (!d && !parent) {
2097 /* ENOTDIR. Open the parent dir. */
2098 p = opendir_parent(path);
2105 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
2111 while ((dent = readdir64(d)) != NULL) {
2114 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2118 if ((len + dent->d_reclen + 2) > size) {
2119 llapi_err_noerrno(LLAPI_MSG_ERROR,
2120 "error: %s: string buffer too small",
2125 strcat(path, dent->d_name);
2127 if (dent->d_type == DT_UNKNOWN) {
2128 lstat_t *st = ¶m->fp_lmd->lmd_st;
2130 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
2131 param->fp_lum_size, GET_LMD_INFO);
2133 dent->d_type = IFTODT(st->st_mode);
2140 switch (dent->d_type) {
2142 llapi_err_noerrno(LLAPI_MSG_ERROR,
2143 "error: %s: '%s' is UNKNOWN type %d",
2144 __func__, dent->d_name, dent->d_type);
2147 rc = llapi_semantic_traverse(path, size, d, sem_init,
2148 sem_fini, data, dent);
2149 if (rc != 0 && ret == 0)
2155 rc = sem_init(path, d, NULL, data, dent);
2156 if (rc < 0 && ret == 0) {
2161 if (sem_fini && rc == 0)
2162 sem_fini(path, d, NULL, data, dent);
2170 sem_fini(path, parent, &d, data, de);
2179 static int param_callback(char *path, semantic_func_t sem_init,
2180 semantic_func_t sem_fini, struct find_param *param)
2182 int ret, len = strlen(path);
2185 if (len > PATH_MAX) {
2187 llapi_error(LLAPI_MSG_ERROR, ret,
2188 "Path name '%s' is too long", path);
2192 buf = (char *)malloc(PATH_MAX + 1);
2196 snprintf(buf, PATH_MAX + 1, "%s", path);
2197 ret = common_param_init(param, buf);
2201 param->fp_depth = 0;
2203 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
2204 sem_fini, param, NULL);
2206 find_param_fini(param);
2208 return ret < 0 ? ret : 0;
2211 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2213 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
2217 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2222 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2224 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2227 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2232 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2236 fd = open(path, O_RDONLY | O_NONBLOCK);
2239 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
2243 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2249 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2253 fd = open(path, O_RDONLY | O_NONBLOCK);
2256 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2260 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2272 * If uuidp is NULL, return the number of available obd uuids.
2273 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2274 * there are more OSTs than allocated to uuidp, then an error is returned with
2275 * the ost_count set to number of available obd uuids.
2277 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2278 int *ost_count, enum tgt_type type)
2280 char buf[PATH_MAX], format[32];
2281 int rc = 0, index = 0;
2282 struct obd_uuid name;
2286 /* Get the lov name */
2287 if (type == LOV_TYPE)
2288 rc = llapi_file_fget_lov_uuid(fd, &name);
2290 rc = llapi_file_fget_lmv_uuid(fd, &name);
2294 /* Now get the ost uuids */
2295 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2296 FILTER_BY_EXACT, "target_obd", ¶m);
2300 fp = fopen(param.gl_pathv[0], "r");
2303 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2308 snprintf(format, sizeof(format),
2309 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2310 while (fgets(buf, sizeof(buf), fp) != NULL) {
2311 if (uuidp && (index < *ost_count)) {
2312 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2320 if (uuidp && (index > *ost_count))
2325 cfs_free_param_data(¶m);
2329 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2331 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2334 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2339 root = open(mnt, O_RDONLY | O_DIRECTORY);
2342 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2347 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2355 /* Check if user specified value matches a real uuid. Ignore _UUID,
2356 * -osc-4ba41334, other trailing gunk in comparison.
2357 * @param real_uuid ends in "_UUID"
2358 * @param search_uuid may or may not end in "_UUID"
2360 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2362 int cmplen = strlen(real_uuid);
2363 int searchlen = strlen(search_uuid);
2365 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2367 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2370 /* The UUIDs may legitimately be different lengths, if
2371 * the system was upgraded from an older version. */
2372 if (cmplen != searchlen)
2375 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2378 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2379 * returned in param->fp_obd_index */
2380 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2382 struct obd_uuid obd_uuid;
2389 if (param->fp_got_uuids)
2392 /* Get the lov/lmv name */
2393 if (param->fp_get_lmv)
2394 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2396 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2398 if (rc != -ENOTTY) {
2399 llapi_error(LLAPI_MSG_ERROR, rc,
2400 "error: can't get %s name: %s",
2401 param->fp_get_lmv ? "lmv" : "lov",
2409 param->fp_got_uuids = 1;
2411 /* Now get the ost uuids */
2412 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2413 obd_uuid.uuid, FILTER_BY_EXACT,
2414 "target_obd", ¶m_data);
2418 fp = fopen(param_data.gl_pathv[0], "r");
2421 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2422 param_data.gl_pathv[0]);
2426 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2427 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2428 param->fp_get_lmv ? "MDTS" : "OBDS");
2430 snprintf(format, sizeof(format),
2431 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2432 while (fgets(buf, sizeof(buf), fp) != NULL) {
2435 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2438 if (param->fp_obd_uuid) {
2439 if (llapi_uuid_match(obd_uuid.uuid,
2440 param->fp_obd_uuid->uuid)) {
2441 param->fp_obd_index = index;
2444 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2445 /* Print everything */
2446 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2449 param->fp_obds_printed = 1;
2453 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2454 llapi_err_noerrno(LLAPI_MSG_ERROR,
2455 "error: %s: unknown obduuid: %s",
2456 __func__, param->fp_obd_uuid->uuid);
2460 cfs_free_param_data(¶m_data);
2464 /* In this case, param->fp_obd_uuid will be an array of obduuids and
2465 * obd index for all these obduuids will be returned in
2466 * param->fp_obd_indexes */
2467 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2468 int num_obds, int **obdindexes, int *obdindex,
2471 int ret, obdcount, obd_valid = 0, obdnum;
2473 struct obd_uuid *uuids = NULL;
2477 if (type == LOV_TYPE)
2478 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2480 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2484 obdcount = atoi(buf);
2485 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2490 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2492 if (ret == -EOVERFLOW) {
2493 struct obd_uuid *uuids_temp;
2495 uuids_temp = realloc(uuids, obdcount *
2496 sizeof(struct obd_uuid));
2497 if (uuids_temp != NULL) {
2499 goto retry_get_uuids;
2504 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2508 indexes = malloc(num_obds * sizeof(*obdindex));
2509 if (indexes == NULL) {
2514 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2517 /* The user may have specified a simple index */
2518 i = strtol(obduuids[obdnum].uuid, &end, 0);
2519 if (end && *end == '\0' && i < obdcount) {
2520 indexes[obdnum] = i;
2523 for (i = 0; i < obdcount; i++) {
2524 if (llapi_uuid_match(uuids[i].uuid,
2525 obduuids[obdnum].uuid)) {
2526 indexes[obdnum] = i;
2532 if (i >= obdcount) {
2533 indexes[obdnum] = OBD_NOT_FOUND;
2534 llapi_err_noerrno(LLAPI_MSG_ERROR,
2535 "invalid obduuid '%s'",
2536 obduuids[obdnum].uuid);
2542 *obdindex = OBD_NOT_FOUND;
2544 *obdindex = obd_valid;
2546 *obdindexes = indexes;
2554 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2558 if (param->fp_mdt_uuid) {
2559 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2561 ¶m->fp_mdt_indexes,
2562 ¶m->fp_mdt_index, LMV_TYPE);
2567 if (param->fp_obd_uuid) {
2568 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2570 ¶m->fp_obd_indexes,
2571 ¶m->fp_obd_index, LOV_TYPE);
2576 param->fp_got_uuids = 1;
2581 int llapi_ostlist(char *path, struct find_param *param)
2586 fd = open(path, O_RDONLY | O_DIRECTORY);
2590 ret = setup_obd_uuid(fd, path, param);
2597 * Tries to determine the default stripe attributes for a given filesystem. The
2598 * filesystem to check should be specified by fsname, or will be determined
2601 static int sattr_get_defaults(const char *const fsname,
2602 unsigned int *scount,
2603 unsigned int *ssize,
2604 unsigned int *soffset)
2610 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2611 "stripecount", val, sizeof(val));
2614 *scount = atoi(val);
2618 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2619 "stripesize", val, sizeof(val));
2626 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2627 "stripeoffset", val, sizeof(val));
2630 *soffset = atoi(val);
2637 * Tries to gather the default stripe attributes for a given filesystem. If
2638 * the attributes can be determined, they are cached for easy retreival the
2639 * next time they are needed. Only a single filesystem's attributes are
2642 int sattr_cache_get_defaults(const char *const fsname,
2643 const char *const pathname, unsigned int *scount,
2644 unsigned int *ssize, unsigned int *soffset)
2647 char fsname[PATH_MAX + 1];
2648 unsigned int stripecount;
2649 unsigned int stripesize;
2650 unsigned int stripeoffset;
2656 char fsname_buf[PATH_MAX + 1];
2657 unsigned int tmp[3];
2659 if (fsname == NULL) {
2660 rc = llapi_search_fsname(pathname, fsname_buf);
2664 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2667 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2669 * Ensure all 3 sattrs (count, size, and offset) are
2670 * successfully retrieved and stored in tmp before writing to
2673 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2677 cache.stripecount = tmp[0];
2678 cache.stripesize = tmp[1];
2679 cache.stripeoffset = tmp[2];
2680 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2684 *scount = cache.stripecount;
2686 *ssize = cache.stripesize;
2688 *soffset = cache.stripeoffset;
2693 static char *layout2name(__u32 layout_pattern)
2695 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2697 else if (layout_pattern == LOV_PATTERN_MDT)
2699 else if (layout_pattern == LOV_PATTERN_RAID0)
2701 else if (layout_pattern ==
2702 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2703 return "raid0,overstriped";
2708 enum lov_dump_flags {
2709 LDF_IS_DIR = 0x0001,
2710 LDF_IS_RAW = 0x0002,
2711 LDF_INDENT = 0x0004,
2712 LDF_SKIP_OBJS = 0x0008,
2716 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2717 struct lov_user_ost_data_v1 *objects,
2718 enum llapi_layout_verbose verbose,
2719 int depth, char *pool_name,
2720 enum lov_dump_flags flags)
2722 bool is_dir = flags & LDF_IS_DIR;
2723 bool is_raw = flags & LDF_IS_RAW;
2724 bool indent = flags & LDF_INDENT;
2725 bool yaml = flags & LDF_YAML;
2726 bool skip_objs = flags & LDF_SKIP_OBJS;
2727 char *prefix = is_dir ? "" : "lmm_";
2728 char *separator = "";
2729 char *space = indent ? " " : "";
2732 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2733 lmm_oi_set_seq(&lum->lmm_oi, 0);
2734 if (!indent && (verbose & VERBOSE_DETAIL))
2735 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2738 if (!yaml && !indent && depth && path &&
2739 ((verbose != VERBOSE_OBJID) || !is_dir))
2740 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2742 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2743 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2744 space, prefix, lum->lmm_magic);
2745 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2747 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2748 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2750 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2752 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2757 if (verbose & ~VERBOSE_DFID)
2758 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2760 /* This needs a bit of hand-holding since old 1.x lmm_oi
2761 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2762 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2763 * a real FID. Ideally the 2.x code would have stored this
2764 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2765 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2766 * worked properly (especially since IGIF FIDs use mds_inum as
2767 * the FID SEQ), but unfortunately that didn't happen.
2769 * Print it to look like an IGIF FID, even though the fields
2770 * are reversed on disk, so that it makes sense to userspace.
2772 * Don't use ostid_id() and ostid_seq(), since they assume the
2773 * oi_fid fields are in the right order. This is why there are
2774 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2776 * For newer layout types hopefully this will be a real FID. */
2777 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2778 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2779 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2780 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2781 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2783 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2786 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2790 if (verbose & VERBOSE_STRIPE_COUNT) {
2791 if (verbose & ~VERBOSE_STRIPE_COUNT)
2792 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2795 if (!is_raw && lum->lmm_stripe_count == 0 &&
2796 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2797 unsigned int scount;
2798 rc = sattr_cache_get_defaults(NULL, path,
2802 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2805 llapi_error(LLAPI_MSG_ERROR, rc,
2806 "Cannot determine default"
2809 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2810 lum->lmm_stripe_count ==
2811 (typeof(lum->lmm_stripe_count))(-1)
2812 ? -1 : lum->lmm_stripe_count);
2815 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2816 (__s16)lum->lmm_stripe_count);
2818 if (!yaml && is_dir)
2824 if (verbose & VERBOSE_STRIPE_SIZE) {
2825 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2826 if (verbose & ~VERBOSE_STRIPE_SIZE)
2827 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2829 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2831 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2834 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2836 llapi_error(LLAPI_MSG_ERROR, rc,
2837 "Cannot determine default"
2840 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2841 lum->lmm_stripe_size);
2843 if (!yaml && is_dir)
2849 if ((verbose & VERBOSE_PATTERN)) {
2850 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2851 if (verbose & ~VERBOSE_PATTERN)
2852 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2854 if (lov_pattern_supported(lum->lmm_pattern))
2855 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2856 layout2name(lum->lmm_pattern));
2858 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2859 separator = (!yaml && is_dir) ? " " : "\n";
2862 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2863 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2864 if (verbose & ~VERBOSE_GENERATION)
2865 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2867 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2868 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2872 if (verbose & VERBOSE_STRIPE_OFFSET) {
2873 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2874 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2875 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2877 if (is_dir || skip_objs)
2878 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2879 lum->lmm_stripe_offset ==
2880 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2881 lum->lmm_stripe_offset);
2882 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
2883 llapi_printf(LLAPI_MSG_NORMAL, "0");
2885 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2886 objects[0].l_ost_idx);
2887 if (!yaml && is_dir)
2893 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2894 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2895 if (verbose & ~VERBOSE_POOL)
2896 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2898 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2899 if (!yaml && is_dir)
2905 if (strlen(separator) != 0)
2906 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2909 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2910 struct lov_user_ost_data_v1 *objects,
2911 char *path, int obdindex, int depth,
2912 enum llapi_layout_verbose verbose,
2913 enum lov_dump_flags flags)
2915 bool is_dir = flags & LDF_IS_DIR;
2916 bool indent = flags & LDF_INDENT;
2917 bool skip_objs = flags & LDF_SKIP_OBJS;
2918 bool yaml = flags & LDF_YAML;
2919 bool obdstripe = obdindex == OBD_NOT_FOUND;
2922 if (!obdstripe && !skip_objs) {
2923 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2924 if (obdindex == objects[i].l_ost_idx) {
2934 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2937 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
2938 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2939 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
2940 char *space = " - ";
2943 llapi_printf(LLAPI_MSG_NORMAL,
2944 "%6slmm_objects:\n", " ");
2946 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2948 llapi_printf(LLAPI_MSG_NORMAL,
2949 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2951 for (i = 0; i < lum->lmm_stripe_count; i++) {
2952 int idx = objects[i].l_ost_idx;
2953 long long oid = ostid_id(&objects[i].l_ost_oi);
2954 long long gr = ostid_seq(&objects[i].l_ost_oi);
2956 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2960 struct lu_fid fid = { 0 };
2962 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2963 llapi_printf(LLAPI_MSG_NORMAL,
2964 "%sl_ost_idx: %d\n", space, idx);
2965 llapi_printf(LLAPI_MSG_NORMAL,
2966 "%8sl_fid: "DFID_NOBRACE"\n",
2968 } else if (indent) {
2969 struct lu_fid fid = { 0 };
2971 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2972 llapi_printf(LLAPI_MSG_NORMAL,
2973 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
2974 space, i, idx, PFID(&fid));
2977 sprintf(fmt, "%s%s%s\n",
2978 "\t%6u\t%14llu\t%#13llx\t",
2979 (fid_seq_is_rsvd(gr) ||
2980 fid_seq_is_mdt0(gr)) ?
2981 "%14llu" : "%#14llx", "%s");
2982 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2984 obdindex == idx ? " *" : "");
2988 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2991 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2992 char *path, int obdindex, int depth,
2993 enum llapi_layout_verbose verbose,
2994 enum lov_dump_flags flags)
2996 struct lmv_user_mds_data *objects = lum->lum_objects;
2997 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2998 char *separator = "";
2999 bool yaml = flags & LDF_YAML;
3000 bool obdstripe = false;
3003 if (obdindex != OBD_NOT_FOUND) {
3004 if (lum->lum_stripe_count == 0) {
3005 if (obdindex == lum->lum_stripe_offset)
3008 for (i = 0; i < lum->lum_stripe_count; i++) {
3009 if (obdindex == objects[i].lum_mds) {
3010 llapi_printf(LLAPI_MSG_NORMAL,
3025 /* show all information default */
3027 if (lum->lum_magic == LMV_USER_MAGIC)
3028 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3029 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3031 verbose = VERBOSE_OBJID;
3034 if (depth && path && ((verbose != VERBOSE_OBJID)))
3035 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3037 if (verbose & VERBOSE_STRIPE_COUNT) {
3038 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3039 if (verbose & ~VERBOSE_STRIPE_COUNT)
3040 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3041 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3042 (int)lum->lum_stripe_count);
3043 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3049 if (verbose & VERBOSE_STRIPE_OFFSET) {
3050 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3051 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3052 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3053 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3054 (int)lum->lum_stripe_offset);
3055 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3061 if (verbose & VERBOSE_HASH_TYPE) {
3062 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3063 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3065 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3066 if (verbose & ~VERBOSE_HASH_TYPE)
3067 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3068 if (type < LMV_HASH_TYPE_MAX)
3069 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3070 mdt_hash_name[type]);
3072 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3074 if (flags & LMV_HASH_FLAG_MIGRATION)
3075 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3076 if (flags & LMV_HASH_FLAG_DEAD)
3077 llapi_printf(LLAPI_MSG_NORMAL, ",dead");
3078 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3079 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3080 if (flags & LMV_HASH_FLAG_LOST_LMV)
3081 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3086 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3087 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3088 if (lum->lum_stripe_count > 0)
3089 llapi_printf(LLAPI_MSG_NORMAL,
3090 "mdtidx\t\t FID[seq:oid:ver]\n");
3091 for (i = 0; i < lum->lum_stripe_count; i++) {
3092 int idx = objects[i].lum_mds;
3093 struct lu_fid *fid = &objects[i].lum_fid;
3094 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3095 llapi_printf(LLAPI_MSG_NORMAL,
3096 "%6u\t\t "DFID"\t\t%s\n",
3098 obdindex == idx ? " *" : "");
3103 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3104 pool_name[0] != '\0') {
3105 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3106 if (verbose & ~VERBOSE_POOL)
3107 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3109 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3113 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3114 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3117 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3118 enum lov_dump_flags flags)
3120 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3121 int depth = param->fp_max_depth;
3122 enum llapi_layout_verbose verbose = param->fp_verbose;
3123 bool yaml = flags & LDF_YAML;
3125 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3126 !(flags & LDF_IS_DIR)) && !yaml)
3127 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3129 if (verbose & VERBOSE_DETAIL) {
3130 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3131 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3132 " ", comp_v1->lcm_magic);
3133 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3134 " ", comp_v1->lcm_size);
3135 if (flags & LDF_IS_DIR)
3136 llapi_printf(LLAPI_MSG_NORMAL,
3137 "%2slcm_flags: %s\n", " ",
3138 comp_v1->lcm_mirror_count > 0 ?
3141 llapi_printf(LLAPI_MSG_NORMAL,
3142 "%2slcm_flags: %s\n", " ",
3143 llapi_layout_flags_string(comp_v1->lcm_flags));
3146 if (verbose & VERBOSE_GENERATION) {
3147 if (verbose & ~VERBOSE_GENERATION)
3148 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3150 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3153 if (verbose & VERBOSE_MIRROR_COUNT) {
3154 if (verbose & ~VERBOSE_MIRROR_COUNT)
3155 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3157 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3158 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3159 comp_v1->lcm_mirror_count + 1 : 1);
3162 if (verbose & VERBOSE_COMP_COUNT) {
3163 if (verbose & ~VERBOSE_COMP_COUNT)
3164 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3166 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3167 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3168 comp_v1->lcm_entry_count : 0);
3171 if (verbose & VERBOSE_DETAIL && !yaml)
3172 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3175 static void lcme_flags2str(__u32 comp_flags)
3181 llapi_printf(LLAPI_MSG_NORMAL, "0");
3184 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3185 if (comp_flags & comp_flags_table[i].cfn_flag) {
3187 llapi_printf(LLAPI_MSG_NORMAL, ",");
3188 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3189 comp_flags_table[i].cfn_name);
3190 comp_flags &= ~comp_flags_table[i].cfn_flag;
3196 llapi_printf(LLAPI_MSG_NORMAL, ",");
3197 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3201 static void lov_dump_comp_v1_entry(struct find_param *param,
3202 enum lov_dump_flags flags, int index)
3204 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3205 struct lov_comp_md_entry_v1 *entry;
3206 char *separator = "";
3207 enum llapi_layout_verbose verbose = param->fp_verbose;
3208 bool yaml = flags & LDF_YAML;
3210 entry = &comp_v1->lcm_entries[index];
3213 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3215 if (verbose & VERBOSE_COMP_ID) {
3216 if (verbose & VERBOSE_DETAIL && !yaml)
3217 llapi_printf(LLAPI_MSG_NORMAL,
3218 "%slcme_id: ", " - ");
3219 else if (verbose & ~VERBOSE_COMP_ID)
3220 llapi_printf(LLAPI_MSG_NORMAL,
3221 "%4slcme_id: ", " ");
3222 if (entry->lcme_id != LCME_ID_INVAL)
3223 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3225 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3229 if (verbose & VERBOSE_MIRROR_ID) {
3230 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3231 if (verbose & ~VERBOSE_MIRROR_ID)
3232 llapi_printf(LLAPI_MSG_NORMAL,
3233 "%4slcme_mirror_id: ", " ");
3234 if (entry->lcme_id != LCME_ID_INVAL)
3235 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3236 mirror_id_of(entry->lcme_id));
3238 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3242 if (verbose & VERBOSE_COMP_FLAGS) {
3243 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3244 if (verbose & ~VERBOSE_COMP_FLAGS)
3245 llapi_printf(LLAPI_MSG_NORMAL,
3246 "%4slcme_flags: ", " ");
3247 lcme_flags2str(entry->lcme_flags);
3250 /* print snapshot timestamp if its a nosync comp */
3251 if ((verbose & VERBOSE_COMP_FLAGS) &&
3252 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3253 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3254 if (verbose & ~VERBOSE_COMP_FLAGS)
3255 llapi_printf(LLAPI_MSG_NORMAL,
3256 "%4slcme_timestamp: ", " ");
3258 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3259 entry->lcme_timestamp);
3261 time_t stamp = entry->lcme_timestamp;
3262 char *date_str = asctime(localtime(&stamp));
3264 date_str[strlen(date_str) - 1] = '\0';
3265 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3271 if (verbose & VERBOSE_COMP_START) {
3272 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3273 if (verbose & ~VERBOSE_COMP_START)
3274 llapi_printf(LLAPI_MSG_NORMAL,
3275 "%4slcme_extent.e_start: ", " ");
3276 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3277 entry->lcme_extent.e_start);
3281 if (verbose & VERBOSE_COMP_END) {
3282 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3283 if (verbose & ~VERBOSE_COMP_END)
3284 llapi_printf(LLAPI_MSG_NORMAL,
3285 "%4slcme_extent.e_end: ", " ");
3286 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3287 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3289 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3290 entry->lcme_extent.e_end);
3295 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3296 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3297 } else if (verbose & VERBOSE_DETAIL) {
3298 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3299 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3300 " ", entry->lcme_offset);
3301 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3302 " ", entry->lcme_size);
3303 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3305 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3309 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3310 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3312 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3313 * The table below gives the answers for the specified parameters (value and
3314 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3315 * --------------------------------------
3316 * 1 | file > limit; sign > 0 | -1 / -1 |
3317 * 2 | file = limit; sign > 0 | -1 / -1 |
3318 * 3 | file < limit; sign > 0 | ? / 1 |
3319 * 4 | file > limit; sign = 0 | -1 / -1 |
3320 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3321 * 6 | file < limit; sign = 0 | ? / -1 |
3322 * 7 | file > limit; sign < 0 | 1 / 1 |
3323 * 8 | file = limit; sign < 0 | ? / -1 |
3324 * 9 | file < limit; sign < 0 | ? / -1 |
3325 * --------------------------------------
3326 * Note: 5th actually means that the value is within the interval
3327 * (limit - margin, limit]. */
3328 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3329 int sign, int negopt, unsigned long long margin,
3335 /* Drop the fraction of margin (of days or size). */
3336 if (file + margin <= limit)
3338 } else if (sign == 0) {
3339 if (file <= limit && file + margin > limit)
3341 else if (file + margin <= limit)
3343 } else if (sign < 0) {
3350 return negopt ? ~ret + 1 : ret;
3353 static inline struct lov_user_md *
3354 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3356 return (struct lov_user_md *)((char *)comp_v1 +
3357 comp_v1->lcm_entries[ent_idx].lcme_offset);
3360 static inline struct lov_user_ost_data_v1 *
3361 lov_v1v3_objects(struct lov_user_md *v1)
3363 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3364 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3366 return v1->lmm_objects;
3370 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3372 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3373 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3374 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3376 pool_name[0] = '\0';
3380 print_last_init_comp(struct find_param *param)
3382 /* print all component info */
3383 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3386 /* print specific component info */
3387 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3388 param->fp_check_comp_start || param->fp_check_comp_end ||
3389 param->fp_check_mirror_id || param->fp_check_mirror_index)
3395 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3399 if (param->fp_comp_end == LUSTRE_EOF) {
3400 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3401 match = end == LUSTRE_EOF ? 1 : -1;
3402 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3403 match = end == LUSTRE_EOF ? -1 : 1;
3404 else /* at least EOF */
3406 if (param->fp_exclude_comp_end)
3409 unsigned long long margin;
3411 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3412 match = find_value_cmp(end, param->fp_comp_end,
3413 param->fp_comp_end_sign,
3414 param->fp_exclude_comp_end, margin, 0);
3421 * An example of "getstripe -v" for a two components PFL file:
3424 * lcm_magic: 0x0BD60BD0
3428 * lcm_entry_count: 2
3432 * lcme_extent.e_start: 0
3433 * lcme_extent.e_end: 1048576
3437 * lmm_magic: 0x0BD10BD0
3438 * lmm_seq: 0x200000401
3439 * lmm_object_id: 0x1
3440 * lmm_fid: [0x200000401:0x1:0x0]
3441 * lmm_stripe_count: 1
3442 * lmm_stripe_size: 1048576
3443 * lmm_pattern: raid0
3445 * lmm_stripe_offset: 0
3447 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3451 * lcme_extent.e_start: 1048576
3452 * lcme_extent.e_end: EOF
3456 * lmm_magic: 0x0BD10BD0
3457 * lmm_seq: 0x200000401
3458 * lmm_object_id: 0x1
3459 * lmm_fid: [0x200000401:0x1:0x0]
3460 * lmm_stripe_count: 2
3461 * lmm_stripe_size: 1048576
3462 * lmm_pattern: raid0
3464 * lmm_stripe_offset: 1
3466 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3467 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3469 static void lov_dump_comp_v1(struct find_param *param, char *path,
3470 enum lov_dump_flags flags)
3472 struct lov_comp_md_entry_v1 *entry;
3473 struct lov_user_ost_data_v1 *objects;
3474 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3475 struct lov_user_md_v1 *v1;
3476 char pool_name[LOV_MAXPOOLNAME + 1];
3477 int obdindex = param->fp_obd_index;
3479 bool obdstripe = false;
3480 __u16 mirror_index = 0;
3481 __u16 mirror_id = 0;
3483 if (obdindex != OBD_NOT_FOUND) {
3484 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3485 i < comp_v1->lcm_entry_count; i++) {
3486 if (!(comp_v1->lcm_entries[i].lcme_flags &
3490 v1 = lov_comp_entry(comp_v1, i);
3491 objects = lov_v1v3_objects(v1);
3493 for (j = 0; j < v1->lmm_stripe_count; j++) {
3494 if (obdindex == objects[j].l_ost_idx) {
3507 lov_dump_comp_v1_header(param, path, flags);
3509 flags |= LDF_INDENT;
3511 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3512 entry = &comp_v1->lcm_entries[i];
3514 if (param->fp_check_comp_flags) {
3515 if (((param->fp_comp_flags & entry->lcme_flags) !=
3516 param->fp_comp_flags) ||
3517 (param->fp_comp_neg_flags & entry->lcme_flags))
3521 if (param->fp_check_comp_id &&
3522 param->fp_comp_id != entry->lcme_id)
3525 if (param->fp_check_comp_start) {
3526 match = find_value_cmp(entry->lcme_extent.e_start,
3527 param->fp_comp_start,
3528 param->fp_comp_start_sign,
3530 param->fp_comp_start_units, 0);
3535 if (param->fp_check_comp_end) {
3536 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3542 if (param->fp_check_mirror_index) {
3543 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3545 mirror_id = mirror_id_of(entry->lcme_id);
3548 match = find_value_cmp(mirror_index,
3549 param->fp_mirror_index,
3550 param->fp_mirror_index_sign,
3551 param->fp_exclude_mirror_index,
3555 } else if (param->fp_check_mirror_id) {
3556 if (mirror_id != mirror_id_of(entry->lcme_id))
3557 mirror_id = mirror_id_of(entry->lcme_id);
3559 match = find_value_cmp(mirror_id,
3560 param->fp_mirror_id,
3561 param->fp_mirror_id_sign,
3562 param->fp_exclude_mirror_id,
3568 if (print_last_init_comp(param)) {
3570 * if part of stripe info is needed, we'd print only
3571 * the last instantiated component info.
3573 if (entry->lcme_flags & LCME_FL_INIT)
3579 if (entry->lcme_flags & LCME_FL_INIT) {
3580 if (obdindex != OBD_NOT_FOUND) {
3581 flags |= LDF_SKIP_OBJS;
3582 v1 = lov_comp_entry(comp_v1, i);
3583 objects = lov_v1v3_objects(v1);
3585 for (j = 0; j < v1->lmm_stripe_count; j++) {
3586 if (obdindex == objects[j].l_ost_idx) {
3587 flags &= ~LDF_SKIP_OBJS;
3592 flags &= ~LDF_SKIP_OBJS;
3595 flags |= LDF_SKIP_OBJS;
3598 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3600 lov_dump_comp_v1_entry(param, flags, i);
3602 v1 = lov_comp_entry(comp_v1, i);
3603 objects = lov_v1v3_objects(v1);
3604 lov_v1v3_pool_name(v1, pool_name);
3606 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3607 param->fp_max_depth, param->fp_verbose,
3610 if (print_last_init_comp(param)) {
3612 * directory layout contains only layout template, print the
3616 i = comp_v1->lcm_entry_count - 1;
3619 flags &= ~LDF_SKIP_OBJS;
3621 lov_dump_comp_v1_entry(param, flags, i);
3623 v1 = lov_comp_entry(comp_v1, i);
3624 objects = lov_v1v3_objects(v1);
3625 lov_v1v3_pool_name(v1, pool_name);
3627 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3628 param->fp_max_depth, param->fp_verbose,
3633 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3634 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3637 static inline bool has_any_comp_options(struct find_param *param)
3639 enum llapi_layout_verbose verbose = param->fp_verbose;
3641 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3642 param->fp_check_comp_start || param->fp_check_comp_end ||
3643 param->fp_check_comp_flags)
3646 /* show full layout information, not component specific */
3647 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3650 return verbose & VERBOSE_COMP_OPTS;
3653 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3656 struct lov_user_md *lum = &orig->lmd_lmm;
3657 struct lov_user_mds_data *new;
3658 struct lov_comp_md_v1 *comp_v1;
3659 struct lov_comp_md_entry_v1 *ent;
3660 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3661 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3664 new = malloc(sizeof(lstat_t) + lum_off + lum_size);
3666 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3670 memcpy(new, orig, sizeof(lstat_t));
3672 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3673 comp_v1->lcm_magic = lum->lmm_magic;
3674 comp_v1->lcm_size = lum_off + lum_size;
3675 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3676 comp_v1->lcm_flags = 0;
3677 comp_v1->lcm_entry_count = 1;
3679 ent = &comp_v1->lcm_entries[0];
3681 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3682 ent->lcme_extent.e_start = 0;
3683 ent->lcme_extent.e_end = LUSTRE_EOF;
3684 ent->lcme_offset = lum_off;
3685 ent->lcme_size = lum_size;
3687 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3692 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3693 enum lov_dump_flags flags)
3695 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3697 if (has_any_comp_options(param)) {
3698 struct lov_user_mds_data *new_lmd, *orig_lmd;
3700 orig_lmd = param->fp_lmd;
3701 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3702 if (new_lmd != NULL) {
3703 param->fp_lmd = new_lmd;
3704 lov_dump_comp_v1(param, path, flags);
3705 param->fp_lmd = orig_lmd;
3711 if (magic == LOV_USER_MAGIC_V1) {
3712 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3713 param->fp_lmd->lmd_lmm.lmm_objects,
3714 path, param->fp_obd_index,
3715 param->fp_max_depth, param->fp_verbose,
3718 char pool_name[LOV_MAXPOOLNAME + 1];
3719 struct lov_user_ost_data_v1 *objects;
3720 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3722 snprintf(pool_name, sizeof(pool_name), "%s",
3723 lmmv3->lmm_pool_name);
3724 objects = lmmv3->lmm_objects;
3725 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3726 objects, path, param->fp_obd_index,
3727 param->fp_max_depth, param->fp_verbose,
3732 static uint32_t check_foreign_type(uint32_t foreign_type)
3736 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3737 if (lu_foreign_types[i].lft_name == NULL)
3739 if (foreign_type == lu_foreign_types[i].lft_type)
3743 return LU_FOREIGN_TYPE_UNKNOWN;
3746 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3747 enum lov_dump_flags flags)
3749 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3750 bool yaml = flags & LDF_YAML;
3752 if (!yaml && param->fp_depth && path)
3753 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3755 if (param->fp_verbose & VERBOSE_DETAIL) {
3756 uint32_t type = check_foreign_type(lfm->lfm_type);
3758 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3760 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3762 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3764 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3765 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3766 lu_foreign_types[type].lft_name);
3768 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3770 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3773 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3774 lfm->lfm_length, lfm->lfm_value);
3775 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3778 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3779 enum lov_dump_flags flags)
3781 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3782 bool yaml = flags & LDF_YAML;
3784 if (!yaml && param->fp_depth && path)
3785 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3787 if (param->fp_verbose & VERBOSE_DETAIL) {
3788 uint32_t type = check_foreign_type(lfm->lfm_type);
3790 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3792 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3794 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3796 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3797 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3798 lu_foreign_types[type].lft_name);
3800 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3802 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3805 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3806 lfm->lfm_length, lfm->lfm_value);
3807 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3810 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3811 enum lov_dump_flags flags)
3815 if (param->fp_get_lmv || param->fp_get_default_lmv)
3816 magic = (__u32)param->fp_lmv_md->lum_magic;
3818 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3821 flags |= LDF_IS_RAW;
3826 case LOV_USER_MAGIC_V1:
3827 case LOV_USER_MAGIC_V3:
3828 case LOV_USER_MAGIC_SPECIFIC:
3829 lov_dump_plain_user_lmm(param, path, flags);
3831 case LOV_USER_MAGIC_FOREIGN:
3832 lov_dump_foreign_lmm(param, path, flags);
3835 case LMV_USER_MAGIC: {
3836 char pool_name[LOV_MAXPOOLNAME + 1];
3837 struct lmv_user_md *lum;
3839 lum = (struct lmv_user_md *)param->fp_lmv_md;
3840 snprintf(pool_name, sizeof(pool_name), "%s",
3841 lum->lum_pool_name);
3842 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3843 param->fp_max_depth, param->fp_verbose,
3847 case LOV_USER_MAGIC_COMP_V1:
3848 lov_dump_comp_v1(param, path, flags);
3850 case LMV_MAGIC_FOREIGN:
3851 lmv_dump_foreign_lmm(param, path, flags);
3854 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
3855 "(expecting one of %#x %#x %#x %#x)\n",
3856 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3857 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3858 LMV_USER_MAGIC, LMV_MAGIC_V1);
3863 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3869 fname = strrchr(path, '/');
3871 /* It should be a file (or other non-directory) */
3872 if (fname == NULL) {
3873 dname = (char *)malloc(2);
3877 fname = (char *)path;
3879 dname = (char *)malloc(fname - path + 1);
3882 strncpy(dname, path, fname - path);
3883 dname[fname - path] = '\0';
3887 fd = open(dname, O_RDONLY | O_NONBLOCK);
3893 strcpy((char *)lum, fname);
3894 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3897 if (close(fd) == -1 && rc == 0)
3905 int llapi_file_lookup(int dirfd, const char *name)
3907 struct obd_ioctl_data data = { 0 };
3912 if (dirfd < 0 || name == NULL)
3915 data.ioc_version = OBD_IOCTL_VERSION;
3916 data.ioc_len = sizeof(data);
3917 data.ioc_inlbuf1 = (char *)name;
3918 data.ioc_inllen1 = strlen(name) + 1;
3920 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3922 llapi_error(LLAPI_MSG_ERROR, rc,
3923 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3928 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
3934 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
3935 * Return -1 or 1 if file timestamp does not or does match the given criteria
3936 * correspondingly. Return 0 if the MDS time is being checked and there are
3937 * attributes on OSTs and it is not yet clear if the timespamp matches.
3939 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
3940 * updated timestamps. */
3941 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
3946 /* Check if file is accepted. */
3947 if (param->fp_atime) {
3948 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
3949 param->fp_asign, param->fp_exclude_atime,
3950 param->fp_time_margin, mds);
3956 if (param->fp_mtime) {
3957 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
3958 param->fp_msign, param->fp_exclude_mtime,
3959 param->fp_time_margin, mds);
3963 /* If the previous check matches, but this one is not yet clear,
3964 * we should return 0 to do an RPC on OSTs. */
3969 if (param->fp_ctime) {
3970 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
3971 param->fp_csign, param->fp_exclude_ctime,
3972 param->fp_time_margin, mds);
3976 /* If the previous check matches, but this one is not yet clear,
3977 * we should return 0 to do an RPC on OSTs. */
3986 * Check whether the stripes matches the indexes user provided
3990 static int check_obd_match(struct find_param *param)
3992 struct lov_user_ost_data_v1 *objects;
3993 struct lov_comp_md_v1 *comp_v1 = NULL;
3994 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3995 lstat_t *st = ¶m->fp_lmd->lmd_st;
3996 int i, j, k, count = 1;
3998 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4001 if (!S_ISREG(st->st_mode))
4004 /* exclude foreign */
4005 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4006 return param->fp_exclude_obd;
4008 /* Only those files should be accepted, which have a
4009 * stripe on the specified OST. */
4010 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4011 comp_v1 = (struct lov_comp_md_v1 *)v1;
4012 count = comp_v1->lcm_entry_count;
4015 for (i = 0; i < count; i++) {
4017 v1 = lov_comp_entry(comp_v1, i);
4019 objects = lov_v1v3_objects(v1);
4021 for (j = 0; j < v1->lmm_stripe_count; j++) {
4022 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4025 for (k = 0; k < param->fp_num_obds; k++) {
4026 if (param->fp_obd_indexes[k] ==
4027 objects[j].l_ost_idx)
4028 return !param->fp_exclude_obd;
4033 return param->fp_exclude_obd;
4036 static int check_mdt_match(struct find_param *param)
4040 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4043 /* FIXME: For striped dir, we should get stripe information and check */
4044 for (i = 0; i < param->fp_num_mdts; i++) {
4045 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4046 return !param->fp_exclude_mdt;
4049 if (param->fp_exclude_mdt)
4056 * Check whether the obd is active or not, if it is
4057 * not active, just print the object affected by this
4060 static int print_failed_tgt(struct find_param *param, char *path, int type)
4062 struct obd_statfs stat_buf;
4063 struct obd_uuid uuid_buf;
4066 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV)
4069 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4070 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4071 ret = llapi_obd_statfs(path, type,
4072 param->fp_obd_index, &stat_buf,
4075 llapi_printf(LLAPI_MSG_NORMAL,
4076 "obd_uuid: %s failed %s ",
4077 param->fp_obd_uuid->uuid,
4084 static int find_check_stripe_size(struct find_param *param)
4086 struct lov_comp_md_v1 *comp_v1 = NULL;
4087 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4088 int ret, i, count = 1;
4090 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4091 return param->fp_exclude_stripe_size ? 1 : -1;
4093 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4094 comp_v1 = (struct lov_comp_md_v1 *)v1;
4095 count = comp_v1->lcm_entry_count;
4096 ret = param->fp_exclude_stripe_size ? 1 : -1;
4099 for (i = 0; i < count; i++) {
4101 v1 = lov_comp_entry(comp_v1, i);
4103 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_stripe_size,
4104 param->fp_stripe_size_sign,
4105 param->fp_exclude_stripe_size,
4106 param->fp_stripe_size_units, 0);
4107 /* If any stripe_size matches */
4115 static __u32 find_get_stripe_count(struct find_param *param)
4117 struct lov_comp_md_v1 *comp_v1 = NULL;
4118 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4120 __u32 stripe_count = 0;
4122 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4125 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4126 comp_v1 = (struct lov_comp_md_v1 *)v1;
4127 count = comp_v1->lcm_entry_count;
4130 for (i = 0; i < count; i++) {
4132 v1 = lov_comp_entry(comp_v1, i);
4133 stripe_count += v1->lmm_stripe_count;
4136 return stripe_count;
4139 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4141 static int find_check_layout(struct find_param *param)
4143 struct lov_comp_md_v1 *comp_v1 = NULL;
4144 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4146 bool found = false, valid = false;
4148 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4149 comp_v1 = (struct lov_comp_md_v1 *)v1;
4150 count = comp_v1->lcm_entry_count;
4153 for (i = 0; i < count; i++) {
4155 v1 = lov_comp_entry(comp_v1, i);
4157 /* foreign file have a special magic but no pattern field */
4158 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4161 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4165 if (v1->lmm_pattern & param->fp_layout) {
4174 if ((found && !param->fp_exclude_layout) ||
4175 (!found && param->fp_exclude_layout))
4181 /* if no type specified, check/exclude all foreign
4182 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4184 static int find_check_foreign(struct find_param *param)
4186 if (S_ISREG(param->fp_lmd->lmd_st.st_mode)) {
4187 struct lov_foreign_md *lfm;
4189 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4190 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4191 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4192 return param->fp_exclude_foreign ? 1 : -1;
4195 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4196 lfm->lfm_type == param->fp_foreign_type)
4197 return param->fp_exclude_foreign ? -1 : 1;
4198 return param->fp_exclude_foreign ? 1 : -1;
4202 if (S_ISDIR(param->fp_lmd->lmd_st.st_mode)) {
4203 struct lmv_foreign_md *lfm;
4205 lfm = (void *)param->fp_lmv_md;
4206 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
4207 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4208 return param->fp_exclude_foreign ? 1 : -1;
4211 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4212 lfm->lfm_type == param->fp_foreign_type)
4213 return param->fp_exclude_foreign ? -1 : 1;
4214 return param->fp_exclude_foreign ? 1 : -1;
4220 static int find_check_pool(struct find_param *param)
4222 struct lov_comp_md_v1 *comp_v1 = NULL;
4223 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4224 struct lov_user_md_v3 *v3 = (void *)v1;
4228 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4229 comp_v1 = (struct lov_comp_md_v1 *)v1;
4230 count = comp_v1->lcm_entry_count;
4231 /* empty requested pool is taken as no pool search */
4232 if (count == 0 && param->fp_poolname[0] == '\0')
4236 for (i = 0; i < count; i++) {
4237 if (comp_v1 != NULL)
4238 v1 = lov_comp_entry(comp_v1, i);
4240 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4243 if (((v1->lmm_magic == LOV_USER_MAGIC_V1) &&
4244 (param->fp_poolname[0] == '\0')) ||
4245 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4246 (strncmp(v3->lmm_pool_name,
4247 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4248 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4249 (strcmp(param->fp_poolname, "*") == 0))) {
4255 if ((found && !param->fp_exclude_pool) ||
4256 (!found && param->fp_exclude_pool))
4262 static int find_check_comp_options(struct find_param *param)
4264 lstat_t *st = ¶m->fp_lmd->lmd_st;
4265 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4266 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4267 struct lov_comp_md_entry_v1 *entry;
4270 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4273 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4274 comp_v1 = (struct lov_comp_md_v1 *)v1;
4276 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4277 if (forged_v1 == NULL)
4279 comp_v1 = forged_v1;
4280 comp_v1->lcm_entry_count = 1;
4281 entry = &comp_v1->lcm_entries[0];
4282 entry->lcme_flags = S_ISDIR(st->st_mode) ? 0 : LCME_FL_INIT;
4283 entry->lcme_extent.e_start = 0;
4284 entry->lcme_extent.e_end = LUSTRE_EOF;
4287 /* invalid case, don't match for any kind of search. */
4288 if (comp_v1->lcm_entry_count == 0) {
4293 if (param->fp_check_comp_count) {
4294 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4295 param->fp_comp_count,
4296 param->fp_comp_count_sign,
4297 param->fp_exclude_comp_count, 1, 0);
4303 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4304 entry = &comp_v1->lcm_entries[i];
4306 if (param->fp_check_comp_flags) {
4308 if (((param->fp_comp_flags & entry->lcme_flags) !=
4309 param->fp_comp_flags) ||
4310 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4316 if (param->fp_check_comp_start) {
4317 ret = find_value_cmp(entry->lcme_extent.e_start,
4318 param->fp_comp_start,
4319 param->fp_comp_start_sign,
4320 param->fp_exclude_comp_start,
4321 param->fp_comp_start_units, 0);
4326 if (param->fp_check_comp_end) {
4327 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4333 /* the component matches all criteria */
4342 static int find_check_mirror_options(struct find_param *param)
4344 struct lov_comp_md_v1 *comp_v1;
4345 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4348 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4351 comp_v1 = (struct lov_comp_md_v1 *)v1;
4353 if (param->fp_check_mirror_count) {
4354 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4355 param->fp_mirror_count,
4356 param->fp_mirror_count_sign,
4357 param->fp_exclude_mirror_count, 1, 0);
4362 if (param->fp_check_mirror_state) {
4364 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4366 if ((param->fp_mirror_state != 0 &&
4367 file_state != param->fp_mirror_state) ||
4368 file_state == param->fp_mirror_neg_state)
4375 static bool find_check_lmm_info(struct find_param *param)
4377 return param->fp_check_pool || param->fp_check_stripe_count ||
4378 param->fp_check_stripe_size || param->fp_check_layout ||
4379 param->fp_check_comp_count || param->fp_check_comp_end ||
4380 param->fp_check_comp_start || param->fp_check_comp_flags ||
4381 param->fp_check_mirror_count || param->fp_check_foreign ||
4382 param->fp_check_mirror_state ||
4383 param->fp_check_projid;
4387 * Get file/directory project id.
4388 * by the open fd resides on.
4389 * Return 0 and project id on success, or -ve errno.
4391 static int fget_projid(int fd, int *projid)
4396 rc = ioctl(fd, LL_IOC_FSGETXATTR, &fsx);
4400 *projid = fsx.fsx_projid;
4404 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
4405 void *data, struct dirent64 *de)
4407 struct find_param *param = (struct find_param *)data;
4408 DIR *dir = dirp == NULL ? NULL : *dirp;
4409 int decision = 1; /* 1 is accepted; -1 is rejected. */
4410 lstat_t *st = ¶m->fp_lmd->lmd_st;
4412 int checked_type = 0;
4414 __u32 stripe_count = 0;
4417 if (parent == NULL && dir == NULL)
4420 /* If a regular expression is presented, make the initial decision */
4421 if (param->fp_pattern != NULL) {
4422 char *fname = strrchr(path, '/');
4423 fname = (fname == NULL ? path : fname + 1);
4424 ret = fnmatch(param->fp_pattern, fname, 0);
4425 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4426 (ret == 0 && param->fp_exclude_pattern))
4430 /* See if we can check the file type from the dirent. */
4431 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
4434 if (DTTOIF(de->d_type) == param->fp_type) {
4435 if (param->fp_exclude_type)
4438 if (!param->fp_exclude_type)
4445 /* Request MDS for the stat info if some of these parameters need
4446 * to be compared. */
4447 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4448 param->fp_check_uid || param->fp_check_gid ||
4449 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4450 param->fp_check_size || param->fp_check_blocks ||
4451 find_check_lmm_info(param) ||
4452 param->fp_check_mdt_count || param->fp_check_hash_type)
4455 if (param->fp_type != 0 && checked_type == 0)
4458 if (decision == 0) {
4459 if (dir && (param->fp_check_mdt_count ||
4460 param->fp_check_hash_type || param->fp_check_foreign)) {
4461 param->fp_get_lmv = 1;
4462 ret = cb_get_dirstripe(path, dir, param);
4464 /* XXX this works to decide for foreign
4467 if (errno == ENODATA &&
4468 param->fp_check_foreign) {
4469 if (param->fp_exclude_foreign)
4477 param->fp_lmd->lmd_lmm.lmm_magic = 0;
4478 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
4479 param->fp_lum_size, GET_LMD_INFO);
4480 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
4481 find_check_lmm_info(param)) {
4482 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
4484 /* We need to "fake" the "use the default" values
4485 * since the lmm struct is zeroed out at this point. */
4486 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4487 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
4489 ostid_set_seq(&lmm->lmm_oi,
4490 FID_SEQ_LOV_DEFAULT);
4491 lmm->lmm_stripe_size = 0;
4492 lmm->lmm_stripe_count = 0;
4493 lmm->lmm_stripe_offset = -1;
4495 if (ret == 0 && param->fp_mdt_uuid != NULL) {
4497 ret = llapi_file_fget_mdtidx(dirfd(dir),
4498 ¶m->fp_file_mdt_index);
4499 } else if (S_ISREG(st->st_mode)) {
4500 /* FIXME: we could get the MDT index from the
4501 * file's FID in lmd->lmd_lmm.lmm_oi without
4502 * opening the file, once we are sure that
4503 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
4504 * That would still be an ioctl() to map the
4505 * FID to the MDT, but not an open RPC. */
4506 fd = open(path, O_RDONLY);
4508 ret = llapi_file_fget_mdtidx(fd,
4509 ¶m->fp_file_mdt_index);
4514 /* For a special file, we assume it resides on
4515 * the same MDT as the parent directory. */
4516 ret = llapi_file_fget_mdtidx(dirfd(parent),
4517 ¶m->fp_file_mdt_index);
4528 stripe_count = find_get_stripe_count(param);
4532 if (param->fp_type && !checked_type) {
4533 if ((st->st_mode & S_IFMT) == param->fp_type) {
4534 if (param->fp_exclude_type)
4537 if (!param->fp_exclude_type)
4543 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
4544 if (lustre_fs && param->fp_got_uuids &&
4545 param->fp_dev != st->st_dev) {
4546 /* A lustre/lustre mount point is crossed. */
4547 param->fp_got_uuids = 0;
4548 param->fp_obds_printed = 0;
4549 param->fp_mdt_index = OBD_NOT_FOUND;
4550 param->fp_obd_index = OBD_NOT_FOUND;
4553 if (lustre_fs && !param->fp_got_uuids) {
4554 ret = setup_target_indexes(dir ? dir : parent, path,
4559 param->fp_dev = st->st_dev;
4560 } else if (!lustre_fs && param->fp_got_uuids) {
4561 /* A lustre/non-lustre mount point is crossed. */
4562 param->fp_got_uuids = 0;
4563 param->fp_mdt_index = OBD_NOT_FOUND;
4564 param->fp_obd_index = OBD_NOT_FOUND;
4568 if (param->fp_check_foreign) {
4569 decision = find_check_foreign(param);
4574 if (param->fp_check_stripe_size) {
4575 decision = find_check_stripe_size(param);
4580 if (param->fp_check_stripe_count) {
4581 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
4582 param->fp_stripe_count_sign,
4583 param->fp_exclude_stripe_count, 1, 0);
4588 if (param->fp_check_mdt_count) {
4589 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4594 decision = find_value_cmp(
4595 param->fp_lmv_md->lum_stripe_count,
4596 param->fp_mdt_count,
4597 param->fp_mdt_count_sign,
4598 param->fp_exclude_mdt_count, 1, 0);
4603 if (param->fp_check_layout) {
4604 decision = find_check_layout(param);
4609 if (param->fp_check_hash_type) {
4612 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4617 found = param->fp_lmv_md->lum_hash_type & param->fp_hash_type;
4618 if ((found && param->fp_exclude_hash_type) ||
4619 (!found && !param->fp_exclude_hash_type)) {
4625 /* If an OBD UUID is specified but none matches, skip this file. */
4626 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
4627 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
4630 /* If an OST or MDT UUID is given, and some OST matches,
4632 if (param->fp_obd_index != OBD_NOT_FOUND ||
4633 param->fp_mdt_index != OBD_NOT_FOUND) {
4634 if (param->fp_obd_uuid) {
4635 if (check_obd_match(param)) {
4636 /* If no mdtuuid is given, we are done.
4637 * Otherwise, fall through to the mdtuuid
4639 if (!param->fp_mdt_uuid)
4646 if (param->fp_mdt_uuid) {
4647 if (check_mdt_match(param))
4654 if (param->fp_check_uid) {
4655 if (st->st_uid == param->fp_uid) {
4656 if (param->fp_exclude_uid)
4659 if (!param->fp_exclude_uid)
4664 if (param->fp_check_gid) {
4665 if (st->st_gid == param->fp_gid) {
4666 if (param->fp_exclude_gid)
4669 if (!param->fp_exclude_gid)
4674 if (param->fp_check_projid) {
4678 fd = open(path, O_RDONLY);
4681 ret = fget_projid(fd, &projid);
4686 if (projid == param->fp_projid) {
4687 if (param->fp_exclude_projid)
4690 if (!param->fp_exclude_projid)
4695 if (param->fp_check_pool) {
4696 decision = find_check_pool(param);
4701 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
4702 param->fp_check_comp_start || param->fp_check_comp_end) {
4703 decision = find_check_comp_options(param);
4708 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
4709 decision = find_check_mirror_options(param);
4714 /* Check the time on mds. */
4716 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
4719 for_mds = lustre_fs ?
4720 (S_ISREG(st->st_mode) && stripe_count) : 0;
4721 decision = find_time_check(st, param, for_mds);
4726 /* If file still fits the request, ask ost for updated info.
4727 The regular stat is almost of the same speed as some new
4728 'glimpse-size-ioctl'. */
4730 if ((param->fp_check_size || param->fp_check_blocks) &&
4731 ((S_ISREG(st->st_mode) && stripe_count) || S_ISDIR(st->st_mode)))
4735 /* For regular files with the stripe the decision may have not
4736 * been taken yet if *time or size is to be checked. */
4737 if (param->fp_obd_index != OBD_NOT_FOUND)
4738 print_failed_tgt(param, path, LL_STATFS_LOV);
4740 if (param->fp_mdt_index != OBD_NOT_FOUND)
4741 print_failed_tgt(param, path, LL_STATFS_LMV);
4744 ret = fstat_f(dirfd(dir), st);
4745 else if (de != NULL)
4746 ret = fstatat_f(dirfd(parent), de->d_name, st,
4747 AT_SYMLINK_NOFOLLOW);
4749 ret = lstat_f(path, st);
4752 if (errno == ENOENT) {
4753 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
4754 "warning: %s: %s does not exist",
4759 llapi_error(LLAPI_MSG_ERROR, ret,
4760 "%s: IOC_LOV_GETINFO on %s failed",
4766 /* Check the time on osc. */
4767 decision = find_time_check(st, param, 0);
4772 if (param->fp_check_size) {
4773 decision = find_value_cmp(st->st_size, param->fp_size,
4774 param->fp_size_sign,
4775 param->fp_exclude_size,
4776 param->fp_size_units, 0);
4781 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
4782 decision = find_value_cmp(st->st_blocks * 512, param->fp_blocks,
4783 param->fp_blocks_sign,
4784 param->fp_exclude_blocks,
4785 param->fp_blocks_units, 0);
4791 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
4792 if (param->fp_zero_end)
4793 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
4795 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4799 /* Do not get down anymore? */
4800 if (param->fp_depth == param->fp_max_depth) {
4811 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
4812 void *param_data, struct dirent64 *de)
4814 struct find_param *param = (struct find_param *)param_data;
4815 struct lmv_user_md *lmu = param->fp_lmv_md;
4816 DIR *tmp_parent = parent;
4817 char raw[MAX_IOC_BUFLEN] = {'\0'};
4819 struct obd_ioctl_data data = { 0 };
4826 if (parent == NULL && dirp == NULL)
4835 if (parent == NULL) {
4836 tmp_parent = opendir_parent(path);
4837 if (tmp_parent == NULL) {
4840 llapi_error(LLAPI_MSG_ERROR, ret,
4841 "can not open %s", path);
4846 fd = dirfd(tmp_parent);
4848 path_copy = strdup(path);
4849 filename = basename(path_copy);
4851 data.ioc_inlbuf1 = (char *)filename;
4852 data.ioc_inllen1 = strlen(filename) + 1;
4853 data.ioc_inlbuf2 = (char *)lmu;
4854 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
4856 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
4858 llapi_error(LLAPI_MSG_ERROR, ret,
4859 "llapi_obd_statfs: error packing ioctl data");
4864 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
4866 if (errno == EBUSY && !retry) {
4867 /* because migrate may not be able to lock all involved
4868 * objects in order, for some of them it try lock, while
4869 * there may be conflicting COS locks and cause migrate
4870 * fail with EBUSY, hope a sync() could cause
4871 * transaction commit and release these COS locks. */
4875 } else if (errno == EALREADY) {
4876 if (param->fp_verbose & VERBOSE_DETAIL)
4877 llapi_printf(LLAPI_MSG_NORMAL,
4878 "%s was migrated to MDT%d already\n",
4879 path, lmu->lum_stripe_offset);
4883 llapi_error(LLAPI_MSG_ERROR, ret,
4884 "%s migrate failed: %s (%d)\n",
4885 path, strerror(-ret), ret);
4888 } else if (param->fp_verbose & VERBOSE_DETAIL) {
4889 llapi_printf(LLAPI_MSG_NORMAL,
4890 "migrate %s to MDT%d stripe count %d\n",
4891 path, lmu->lum_stripe_offset,
4892 lmu->lum_stripe_count);
4897 /* If the directory is being migration, we need
4898 * close the directory after migration,
4899 * so the old directory cache will be cleanup
4900 * on the client side, and re-open to get the
4901 * new directory handle */
4902 *dirp = opendir(path);
4903 if (*dirp == NULL) {
4905 llapi_error(LLAPI_MSG_ERROR, ret,
4906 "%s: Failed to open '%s'", __func__, path);
4911 closedir(tmp_parent);
4918 /* dir migration finished, shrink its stripes */
4919 static int cb_migrate_mdt_fini(char *path, DIR *parent, DIR **dirp, void *data,
4920 struct dirent64 *de)
4922 struct find_param *param = data;
4923 struct lmv_user_md *lmu = param->fp_lmv_md;
4924 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
4927 if (de && de->d_type != DT_DIR)
4932 * close it before setxattr because the latter may destroy the
4933 * original object, and cause close fail.
4935 ret = closedir(*dirp);
4941 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
4942 if (ret == -EALREADY)
4945 cb_common_fini(path, parent, dirp, data, de);
4949 int llapi_migrate_mdt(char *path, struct find_param *param)
4951 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
4955 int llapi_mv(char *path, struct find_param *param)
4957 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
4958 static bool printed;
4961 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
4962 "llapi_mv() is deprecated, use llapi_migrate_mdt()\n");
4966 return llapi_migrate_mdt(path, param);
4969 int llapi_find(char *path, struct find_param *param)
4971 return param_callback(path, cb_find_init, cb_common_fini, param);
4975 * Get MDT number that the file/directory inode referenced
4976 * by the open fd resides on.
4977 * Return 0 and mdtidx on success, or -ve errno.
4979 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
4981 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
4986 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
4987 struct dirent64 *de)
4989 struct find_param *param = (struct find_param *)data;
4990 DIR *d = dirp == NULL ? NULL : *dirp;
4994 if (parent == NULL && d == NULL)
4998 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
4999 } else /* if (parent) */ {
5002 fd = open(path, O_RDONLY | O_NOCTTY);
5004 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5012 if (ret == -ENODATA) {
5013 if (!param->fp_obd_uuid)
5014 llapi_printf(LLAPI_MSG_NORMAL,
5015 "'%s' has no stripe info\n", path);
5017 } else if (ret == -ENOENT) {
5018 llapi_error(LLAPI_MSG_WARN, ret,
5019 "warning: %s: '%s' does not exist",
5022 } else if (ret == -ENOTTY) {
5023 llapi_error(LLAPI_MSG_ERROR, ret,
5024 "%s: '%s' not on a Lustre fs",
5027 llapi_error(LLAPI_MSG_ERROR, ret,
5028 "error: %s: '%s' failed get_mdtidx",
5034 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5035 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5037 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5041 /* Do not go down anymore? */
5042 if (param->fp_depth == param->fp_max_depth)
5050 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
5051 struct dirent64 *de)
5053 struct find_param *param = (struct find_param *)data;
5054 DIR *d = dirp == NULL ? NULL : *dirp;
5057 if (parent == NULL && d == NULL)
5060 if (param->fp_obd_uuid) {
5061 param->fp_quiet = 1;
5062 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
5067 if (d && (param->fp_get_lmv || param->fp_get_default_lmv))
5068 ret = cb_get_dirstripe(path, d, param);
5070 (parent && !param->fp_get_lmv && !param->fp_get_default_lmv))
5071 ret = get_lmd_info(path, parent, d, ¶m->fp_lmd->lmd_lmm,
5072 param->fp_lum_size, GET_LMD_STRIPE);
5077 if (errno == ENODATA && d != NULL) {
5078 /* We need to "fake" the "use the default" values
5079 * since the lmm struct is zeroed out at this point.
5080 * The magic needs to be set in order to satisfy
5081 * a check later on in the code path.
5082 * The object_seq needs to be set for the "(Default)"
5083 * prefix to be displayed. */
5084 if (param->fp_get_default_lmv) {
5085 struct lmv_user_md *lum = param->fp_lmv_md;
5087 lum->lum_magic = LMV_USER_MAGIC;
5088 lum->lum_stripe_count = 0;
5089 lum->lum_stripe_offset = -1;
5091 } else if (param->fp_get_lmv) {
5092 struct lmv_user_md *lum = param->fp_lmv_md;
5095 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5098 lum->lum_magic = LMV_MAGIC_V1;
5099 lum->lum_stripe_count = 0;
5100 lum->lum_stripe_offset = mdtidx;
5103 struct lov_user_md *lmm =
5104 ¶m->fp_lmd->lmd_lmm;
5106 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5108 ostid_set_seq(&lmm->lmm_oi,
5109 FID_SEQ_LOV_DEFAULT);
5110 lmm->lmm_stripe_count = 0;
5111 lmm->lmm_stripe_size = 0;
5112 lmm->lmm_stripe_offset = -1;
5115 } else if (errno == ENODATA && parent != NULL) {
5116 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5117 llapi_printf(LLAPI_MSG_NORMAL,
5118 "%s has no stripe info\n", path);
5120 } else if (errno == ENOENT) {
5121 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5122 "warning: %s: %s does not exist",
5125 } else if (errno == ENOTTY) {
5127 llapi_error(LLAPI_MSG_ERROR, ret,
5128 "%s: '%s' not on a Lustre fs?",
5133 llapi_error(LLAPI_MSG_ERROR, ret,
5134 "error: %s: %s failed for %s",
5135 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
5136 "IOC_MDC_GETFILESTRIPE", path);
5143 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5144 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
5147 /* Do not get down anymore? */
5148 if (param->fp_depth == param->fp_max_depth)
5156 int llapi_getstripe(char *path, struct find_param *param)
5158 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5159 cb_get_mdt_index : cb_getstripe,
5160 cb_common_fini, param);
5163 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5164 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5166 char raw[MAX_IOC_BUFLEN] = {'\0'};
5168 struct obd_ioctl_data data = { 0 };
5171 data.ioc_inlbuf1 = (char *)&type;
5172 data.ioc_inllen1 = sizeof(__u32);
5173 data.ioc_inlbuf2 = (char *)&index;
5174 data.ioc_inllen2 = sizeof(__u32);
5175 data.ioc_pbuf1 = (char *)stat_buf;
5176 data.ioc_plen1 = sizeof(struct obd_statfs);
5177 data.ioc_pbuf2 = (char *)uuid_buf;
5178 data.ioc_plen2 = sizeof(struct obd_uuid);
5180 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5182 llapi_error(LLAPI_MSG_ERROR, rc,
5183 "llapi_obd_statfs: error packing ioctl data");
5187 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5189 return rc < 0 ? -errno : 0;
5192 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5193 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5198 fd = open(path, O_RDONLY);
5201 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5203 /* If we can't even open a file on the filesystem (e.g. with
5204 * -ESHUTDOWN), force caller to exit or it will loop forever. */
5208 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5215 #define MAX_STRING_SIZE 128
5217 int llapi_ping(char *obd_type, char *obd_name)
5219 int flags = O_RDONLY;
5220 char buf[1] = { 0 };
5224 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5225 obd_type, obd_name);
5229 fd = open(path.gl_pathv[0], flags);
5231 if (errno == EACCES && flags == O_RDONLY) {
5236 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5241 if (flags == O_RDONLY)
5242 rc = read(fd, buf, sizeof(buf));
5244 rc = write(fd, buf, sizeof(buf));
5252 cfs_free_param_data(&path);
5256 int llapi_target_iterate(int type_num, char **obd_type,
5257 void *args, llapi_cb_t cb)
5263 for (i = 0; i < type_num; i++) {
5266 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5270 for (j = 0; j < param.gl_pathc; j++) {
5271 char obd_uuid[UUID_MAX + 1];
5275 fp = fopen(param.gl_pathv[j], "r");
5278 llapi_error(LLAPI_MSG_ERROR, rc,
5279 "error: opening '%s'",
5284 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5286 llapi_error(LLAPI_MSG_ERROR, rc,
5287 "error: reading '%s'",
5292 /* Extract the obd_name from the sysfs path.
5293 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5295 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5301 /* skip /fs/lustre/'obd_type'/ */
5302 obd_name += strlen(obd_type[i]) + 12;
5303 /* chop off after obd_name */
5304 ptr = strrchr(obd_name, '/');
5308 cb(obd_type[i], obd_name, obd_uuid, args);
5317 cfs_free_param_data(¶m);
5321 static void do_target_check(char *obd_type_name, char *obd_name,
5322 char *obd_uuid, void *args)
5326 rc = llapi_ping(obd_type_name, obd_name);
5327 if (rc == ENOTCONN) {
5328 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5330 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5332 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5336 int llapi_target_check(int type_num, char **obd_type, char *dir)
5338 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5341 #undef MAX_STRING_SIZE
5343 /* Is this a lustre fs? */
5344 int llapi_is_lustre_mnttype(const char *type)
5346 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
5349 /* Is this a lustre client fs? */
5350 int llapi_is_lustre_mnt(struct mntent *mnt)
5352 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5353 strstr(mnt->mnt_fsname, ":/") != NULL);
5356 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5358 char fsname[PATH_MAX + 1];
5362 rc = llapi_search_fsname(mnt, fsname);
5366 root = open(mnt, O_RDONLY | O_DIRECTORY);
5369 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
5373 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
5381 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
5382 * format must have %s%s, buf must be > 16
5383 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
5384 * then buf = "lustre-MDT0000"
5386 static int get_mdtname(char *name, char *format, char *buf)
5388 char suffix[]="-MDT0000";
5389 int len = strlen(name);
5391 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
5392 name[len - 5] = '\0';
5397 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
5400 /* Not enough room to add suffix */
5401 llapi_err_noerrno(LLAPI_MSG_ERROR,
5402 "Invalid MDT name |%s|", name);
5407 return sprintf(buf, format, name, suffix);
5410 /** ioctl on filsystem root, with mdtindex sent as data
5411 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
5412 * \param mdtidxp pointer to integer within data to be filled in with the
5413 * mdt index (0 if no mdt is specified). NULL won't be filled.
5415 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
5423 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
5424 Open root and parse mdt index. */
5425 if (mdtname[0] == '/') {
5427 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
5428 (char *)mdtname, -1);
5430 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
5432 ptr = fsname + strlen(fsname) - 8;
5434 index = strtol(ptr + 4, NULL, 16);
5435 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
5439 llapi_err_noerrno(LLAPI_MSG_ERROR,
5440 "Can't open %s: %d\n", mdtname, rc);
5447 rc = ioctl(fd, opc, data);
5456 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
5457 int buflen, long long *recno, int *linkno)
5459 const char *fidstr_orig = fidstr;
5461 struct getinfo_fid2path *gf;
5466 while (*fidstr == '[')
5469 sscanf(fidstr, SFID, RFID(&fid));
5470 if (!fid_is_sane(&fid)) {
5471 llapi_err_noerrno(LLAPI_MSG_ERROR,
5472 "bad FID format '%s', should be [seq:oid:ver]"
5473 " (e.g. "DFID")\n", fidstr_orig,
5474 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
5478 gf = malloc(sizeof(*gf) + buflen);
5483 gf->gf_recno = *recno;
5484 gf->gf_linkno = *linkno;
5485 gf->gf_pathlen = buflen;
5487 /* Take path or fsname */
5488 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
5493 /* strip out instances of // */
5494 for (a = gf->gf_u.gf_path; *a != '\0'; a++) {
5495 if ((*a == '/') && (*(a + 1) == '/'))
5502 if (buf[0] == '\0') { /* ROOT path */
5507 *recno = gf->gf_recno;
5508 *linkno = gf->gf_linkno;
5515 static int fid_from_lma(const char *path, int fd, struct lu_fid *fid)
5518 struct lustre_mdt_attrs *lma;
5522 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
5524 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
5527 lma = (struct lustre_mdt_attrs *)buf;
5528 fid_le_to_cpu(fid, &lma->lma_self_fid);
5532 int llapi_get_mdt_index_by_fid(int fd, const struct lu_fid *fid,
5537 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
5546 int llapi_fd2fid(int fd, struct lu_fid *fid)
5550 memset(fid, 0, sizeof(*fid));
5552 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
5553 if (rc == -EINVAL || rc == -ENOTTY)
5554 rc = fid_from_lma(NULL, fd, fid);
5559 int llapi_path2fid(const char *path, struct lu_fid *fid)
5563 memset(fid, 0, sizeof(*fid));
5564 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5566 if (errno == ELOOP || errno == ENXIO)
5567 return fid_from_lma(path, -1, fid);
5571 rc = llapi_fd2fid(fd, fid);
5572 if (rc == -EINVAL || rc == -ENOTTY)
5573 rc = fid_from_lma(path, -1, fid);
5579 int llapi_fd2parent(int fd, unsigned int linkno, struct lu_fid *parent_fid,
5580 char *name, size_t name_size)
5582 struct getparent *gp;
5585 gp = malloc(sizeof(*gp) + name_size);
5589 gp->gp_linkno = linkno;
5590 gp->gp_name_size = name_size;
5592 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
5598 *parent_fid = gp->gp_fid;
5600 strncpy(name, gp->gp_name, name_size);
5601 name[name_size - 1] = '\0';
5608 int llapi_path2parent(const char *path, unsigned int linkno,
5609 struct lu_fid *parent_fid, char *name, size_t name_size)
5614 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5618 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
5623 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
5628 root = open(mnt, O_RDONLY | O_DIRECTORY);
5631 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
5635 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
5638 llapi_error(LLAPI_MSG_ERROR, rc,
5639 "ioctl on %s for getting connect flags failed", mnt);
5646 * Get a 64-bit value representing the version of file data pointed by fd.
5648 * Each write or truncate, flushed on OST, will change this value. You can use
5649 * this value to verify if file data was modified. This only checks the file
5650 * data, not metadata.
5652 * \param flags 0: no flush pages, usually used it the process has already
5654 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
5656 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
5657 * pages from clients.
5659 * \retval 0 on success.
5660 * \retval -errno on error.
5662 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
5665 struct ioc_data_version idv;
5667 idv.idv_flags = (__u32)flags;
5669 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5673 *data_version = idv.idv_version;
5679 * Flush cached pages from all clients.
5681 * \param fd File descriptor
5685 int llapi_file_flush(int fd)
5689 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);
5693 * Fetch layout version from OST objects. Layout version on OST objects are
5694 * only set when the file is a mirrored file AND after the file has been
5695 * written at least once.
5697 * It actually fetches the least layout version from the objects.
5699 int llapi_get_ost_layout_version(int fd, __u32 *layout_version)
5702 struct ioc_data_version idv = { 0 };
5704 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5708 *layout_version = idv.idv_layout_version;
5714 * Create a file without any name and open it for read/write
5716 * - file is created as if it were a standard file in the given \a directory
5717 * - file does not appear in \a directory and mtime does not change because
5718 * the filename is handled specially by the Lustre MDS.
5719 * - file is destroyed at final close
5721 * \param[in] directory directory from which to inherit layout/MDT idx
5722 * \param[in] mdt_idx MDT index on which the file is created,
5723 * \a idx == -1 means no specific MDT is requested
5724 * \param[in] mode standard open(2) mode
5725 * \param[in] stripe_param stripe parameters. May be NULL.
5727 * \retval a file descriptor on success.
5728 * \retval -errno on error.
5730 int llapi_create_volatile_param(const char *directory, int mdt_idx,
5731 int open_flags, mode_t mode,
5732 const struct llapi_stripe_param *stripe_param)
5734 char file_path[PATH_MAX];
5735 int saved_errno = errno;
5737 unsigned int rnumber;
5743 rc = snprintf(file_path, sizeof(file_path),
5744 "%s/" LUSTRE_VOLATILE_HDR "::%.4X",
5745 directory, rnumber);
5747 rc = snprintf(file_path, sizeof(file_path),
5748 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X",
5749 directory, mdt_idx, rnumber);
5751 if (rc < 0 || rc >= sizeof(file_path))
5752 return -ENAMETOOLONG;
5755 * Either open O_WRONLY or O_RDWR, creating RDONLY
5756 * is non-sensical here
5758 if ((open_flags & O_ACCMODE) == O_RDONLY)
5759 open_flags = O_RDWR | (open_flags & ~O_ACCMODE);
5761 open_flags |= O_CREAT | O_EXCL | O_NOFOLLOW;
5763 if (stripe_param != NULL) {
5764 fd = llapi_file_open_param(file_path, open_flags,
5765 mode, stripe_param);
5769 fd = open(file_path, open_flags, mode);
5773 } while (fd < 0 && rc == -EEXIST);
5776 llapi_error(LLAPI_MSG_ERROR, rc,
5777 "Cannot create volatile file '%s' in '%s'",
5778 file_path + strlen(directory) + 1 +
5779 LUSTRE_VOLATILE_HDR_LEN,
5785 * Unlink file in case this wasn't a Lustre filesystem and the magic
5786 * volatile filename wasn't handled as intended. The effect is the
5787 * same. If volatile open was supported then we expect unlink() to
5790 (void)unlink(file_path);
5793 * Since we are returning successfully we restore errno (and
5794 * mask out possible EEXIST from open() and ENOENT from unlink().
5796 errno = saved_errno;
5802 * Create a file without any name open it for read/write
5804 * - file is created as if it were a standard file in the given \a directory
5805 * - file does not appear in \a directory and mtime does not change because
5806 * the filename is handled specially by the Lustre MDS.
5807 * - file is removed at final close
5808 * - file modes are rw------- since it doesn't make sense to have a read-only
5809 * or write-only file that cannot be opened again.
5810 * - if user wants another mode it must use fchmod() on the open file, no
5811 * security problems arise because it cannot be opened by another process.
5813 * \param[in] directory directory from which to inherit layout/MDT idx
5814 * \param[in] idx MDT index on which the file is created,
5815 * \a idx == -1 means no specific MDT is requested
5816 * \param[in] open_flags standard open(2) flags
5818 * \retval a file descriptor on success.
5819 * \retval -errno on error.
5821 int llapi_create_volatile_idx(const char *directory, int mdt_idx,
5824 return llapi_create_volatile_param(directory, mdt_idx, open_flags,
5825 S_IRUSR | S_IWUSR, NULL);
5829 * Swap the layouts between 2 file descriptors
5830 * the 2 files must be open for writing
5831 * first fd received the ioctl, second fd is passed as arg
5832 * this is assymetric but avoid use of root path for ioctl
5834 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
5835 int gid, __u64 flags)
5837 struct lustre_swap_layouts lsl;
5842 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5843 rc = fstat(fd1, &st1);
5847 rc = fstat(fd2, &st2);
5852 lsl.sl_flags = flags;
5856 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
5860 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5861 struct timeval tv1[2];
5862 struct timeval tv2[2];
5864 memset(tv1, 0, sizeof(tv1));
5865 memset(tv2, 0, sizeof(tv2));
5867 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
5868 tv1[0].tv_sec = st1.st_atime;
5869 tv2[0].tv_sec = st2.st_atime;
5871 tv1[0].tv_sec = st2.st_atime;
5872 tv2[0].tv_sec = st1.st_atime;
5875 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
5876 tv1[1].tv_sec = st1.st_mtime;
5877 tv2[1].tv_sec = st2.st_mtime;
5879 tv1[1].tv_sec = st2.st_mtime;
5880 tv2[1].tv_sec = st1.st_mtime;
5883 rc = futimes(fd1, tv1);
5887 rc = futimes(fd2, tv2);
5895 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
5902 while (grp_id == 0);
5904 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
5912 * Swap the layouts between 2 files
5913 * the 2 files are open in write
5915 int llapi_swap_layouts(const char *path1, const char *path2,
5916 __u64 dv1, __u64 dv2, __u64 flags)
5920 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
5923 llapi_error(LLAPI_MSG_ERROR, rc,
5924 "error: cannot open '%s' for write", path1);
5928 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
5931 llapi_error(LLAPI_MSG_ERROR, rc,
5932 "error: cannot open '%s' for write", path2);
5936 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
5938 llapi_error(LLAPI_MSG_ERROR, rc,
5939 "error: cannot swap layout between '%s' and '%s'",
5950 * Attempt to open a file with Lustre file identifier \a fid
5951 * and return an open file descriptor.
5953 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
5954 * \param[in] fid Lustre file identifier of file to open
5955 * \param[in] flags open() flags
5957 * \retval non-negative file descriptor on successful open
5958 * \retval -1 if an error occurred
5960 int llapi_open_by_fid(const char *lustre_dir, const struct lu_fid *fid,
5963 char mntdir[PATH_MAX];
5964 char path[PATH_MAX + 64];
5967 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
5971 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
5972 return open(path, flags);
5978 * \param fd File to lock.
5979 * \param gid Group Identifier.
5981 * \retval 0 on success.
5982 * \retval -errno on failure.
5984 int llapi_group_lock(int fd, int gid)
5988 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
5991 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
5999 * \param fd File to unlock.
6000 * \param gid Group Identifier.
6002 * \retval 0 on success.
6003 * \retval -errno on failure.
6005 int llapi_group_unlock(int fd, int gid)
6009 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
6012 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");