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>
74 #include <libcfs/util/ioctl.h>
75 #include <libcfs/util/param.h>
76 #include <libcfs/util/string.h>
77 #include <linux/lnet/lnetctl.h>
78 #include <lustre/lustreapi.h>
79 #include <linux/lustre/lustre_ostid.h>
80 #include <linux/lustre/lustre_ioctl.h>
81 #include "lustreapi_internal.h"
83 static int llapi_msg_level = LLAPI_MSG_MAX;
84 const char *liblustreapi_cmd;
86 char *mdt_hash_name[] = { "none",
87 LMV_HASH_NAME_ALL_CHARS,
88 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 (unsigned long long)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",
428 (unsigned long long)stripe_size);
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 * Open a Lustre file.
545 * \param name the name of the file to be opened
546 * \param flags access mode, see flags in open(2)
547 * \param mode permission of the file if it is created, see mode in open(2)
548 * \param param stripe pattern of the newly created file
550 * \retval file descriptor of opened file
551 * \retval negative errno on failure
553 int llapi_file_open_param(const char *name, int flags, mode_t mode,
554 const struct llapi_stripe_param *param)
556 char fsname[MAX_OBD_NAME + 1] = { 0 };
557 char *pool_name = param->lsp_pool;
558 struct lov_user_md *lum = NULL;
559 size_t lum_size = sizeof(*lum);
562 /* Make sure we are on a Lustre file system */
563 rc = llapi_search_fsname(name, fsname);
565 llapi_error(LLAPI_MSG_ERROR, rc,
566 "'%s' is not on a Lustre filesystem",
571 /* Check if the stripe pattern is sane. */
572 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
573 param->lsp_stripe_offset,
574 param->lsp_stripe_count,
575 param->lsp_stripe_pattern);
579 /* Make sure we have a good pool */
580 if (pool_name != NULL) {
581 /* in case user gives the full pool name <fsname>.<poolname>,
582 * strip the fsname */
583 char *ptr = strchr(pool_name, '.');
586 if (strcmp(pool_name, fsname) != 0) {
588 llapi_err_noerrno(LLAPI_MSG_ERROR,
589 "Pool '%s' is not on filesystem '%s'",
596 /* Make sure the pool exists and is non-empty */
597 rc = llapi_search_ost(fsname, pool_name, NULL);
599 char *err = rc == 0 ? "has no OSTs" : "does not exist";
601 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
602 fsname, pool_name, err);
606 lum_size = sizeof(struct lov_user_md_v3);
609 /* sanity check of target list */
610 if (param->lsp_is_specific) {
611 char ostname[MAX_OBD_NAME + 64];
615 for (i = 0; i < param->lsp_stripe_count; i++) {
616 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
617 fsname, param->lsp_osts[i]);
618 rc = llapi_search_ost(fsname, pool_name, ostname);
623 llapi_error(LLAPI_MSG_ERROR, rc,
624 "%s: cannot find OST %s in %s",
631 /* Make sure stripe offset is in OST list. */
632 if (param->lsp_osts[i] == param->lsp_stripe_offset)
636 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
637 "%s: stripe offset '%d' is not in the "
639 __func__, param->lsp_stripe_offset);
643 lum_size = lov_user_md_size(param->lsp_stripe_count,
644 LOV_USER_MAGIC_SPECIFIC);
647 lum = calloc(1, lum_size);
652 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
654 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
655 flags = O_DIRECTORY | O_RDONLY;
662 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
667 /* Initialize IOCTL striping pattern structure */
668 lum->lmm_magic = LOV_USER_MAGIC_V1;
669 lum->lmm_pattern = param->lsp_stripe_pattern;
670 lum->lmm_stripe_size = param->lsp_stripe_size;
671 lum->lmm_stripe_count = param->lsp_stripe_count;
672 lum->lmm_stripe_offset = param->lsp_stripe_offset;
673 if (pool_name != NULL) {
674 struct lov_user_md_v3 *lumv3 = (void *)lum;
676 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
677 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
679 if (param->lsp_is_specific) {
680 struct lov_user_md_v3 *lumv3 = (void *)lum;
683 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
684 if (pool_name == NULL) {
685 /* LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
686 * OST list, therefore if pool is not specified we have
687 * to pack a null pool name for placeholder. */
688 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
691 for (i = 0; i < param->lsp_stripe_count; i++)
692 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
695 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
696 char *errmsg = "stripe already set";
699 if (errno != EEXIST && errno != EALREADY)
700 errmsg = strerror(errno);
702 llapi_err_noerrno(LLAPI_MSG_ERROR,
703 "setstripe error for '%s': %s", name, errmsg);
714 int llapi_file_open_pool(const char *name, int flags, int mode,
715 unsigned long long stripe_size, int stripe_offset,
716 int stripe_count, int stripe_pattern, char *pool_name)
718 const struct llapi_stripe_param param = {
719 .lsp_stripe_size = stripe_size,
720 .lsp_stripe_count = stripe_count,
721 .lsp_stripe_pattern = stripe_pattern,
722 .lsp_stripe_offset = stripe_offset,
723 .lsp_pool = pool_name
725 return llapi_file_open_param(name, flags, mode, ¶m);
728 int llapi_file_open(const char *name, int flags, int mode,
729 unsigned long long stripe_size, int stripe_offset,
730 int stripe_count, int stripe_pattern)
732 return llapi_file_open_pool(name, flags, mode, stripe_size,
733 stripe_offset, stripe_count,
734 stripe_pattern, NULL);
737 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
738 __u32 flags, char *foreign_lov)
741 struct lov_foreign_md *lfm;
744 if (foreign_lov == NULL) {
746 llapi_error(LLAPI_MSG_ERROR, rc,
747 "foreign LOV EA content must be provided");
751 len = strlen(foreign_lov);
752 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
755 llapi_error(LLAPI_MSG_ERROR, rc,
756 "foreign LOV EA size %zu (must be 0 < len < %zu)",
757 len, XATTR_SIZE_MAX -
758 offsetof(struct lov_foreign_md, lfm_value));
762 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
765 llapi_error(LLAPI_MSG_ERROR, rc,
766 "failed to allocate lov_foreign_md");
770 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
773 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
777 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
778 lfm->lfm_length = len;
779 lfm->lfm_type = type;
780 lfm->lfm_flags = flags;
781 memcpy(lfm->lfm_value, foreign_lov, len);
783 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
784 char *errmsg = "stripe already set";
788 errmsg = "not on a Lustre filesystem";
789 else if (errno == EEXIST || errno == EALREADY)
790 errmsg = "stripe already set";
792 errmsg = strerror(errno);
794 llapi_err_noerrno(LLAPI_MSG_ERROR,
795 "setstripe error for '%s': %s", name, errmsg);
811 int llapi_file_create(const char *name, unsigned long long stripe_size,
812 int stripe_offset, int stripe_count, int stripe_pattern)
816 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
817 stripe_offset, stripe_count, stripe_pattern,
826 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
827 int stripe_offset, int stripe_count,
828 int stripe_pattern, char *pool_name)
832 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
833 stripe_offset, stripe_count, stripe_pattern,
842 static int verify_dir_param(const char *name,
843 const struct llapi_stripe_param *param)
845 char fsname[MAX_OBD_NAME + 1] = { 0 };
846 char *pool_name = param->lsp_pool;
849 /* Make sure we are on a Lustre file system */
850 rc = llapi_search_fsname(name, fsname);
852 llapi_error(LLAPI_MSG_ERROR, rc,
853 "'%s' is not on a Lustre filesystem",
858 /* Check if the stripe pattern is sane. */
859 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
860 param->lsp_stripe_count,
861 param->lsp_stripe_pattern);
865 /* Make sure we have a good pool */
866 if (pool_name != NULL) {
867 /* in case user gives the full pool name <fsname>.<poolname>,
868 * strip the fsname */
869 char *ptr = strchr(pool_name, '.');
873 if (strcmp(pool_name, fsname) != 0) {
875 llapi_err_noerrno(LLAPI_MSG_ERROR,
876 "Pool '%s' is not on filesystem '%s'",
883 /* Make sure the pool exists and is non-empty */
884 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
886 char *err = rc == 0 ? "has no OSTs" : "does not exist";
888 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
889 fsname, pool_name, err);
894 /* sanity check of target list */
895 if (param->lsp_is_specific) {
896 char mdtname[MAX_OBD_NAME + 64];
900 for (i = 0; i < param->lsp_stripe_count; i++) {
901 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
902 fsname, param->lsp_tgts[i]);
903 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
908 llapi_error(LLAPI_MSG_ERROR, rc,
909 "%s: cannot find MDT %s in %s",
916 /* Make sure stripe offset is in MDT list. */
917 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
921 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
922 "%s: stripe offset '%d' is not in the "
924 __func__, param->lsp_stripe_offset);
932 static inline void param2lmu(struct lmv_user_md *lmu,
933 const struct llapi_stripe_param *param)
935 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
937 lmu->lum_stripe_count = param->lsp_stripe_count;
938 lmu->lum_stripe_offset = param->lsp_stripe_offset;
939 lmu->lum_hash_type = param->lsp_stripe_pattern;
940 if (param->lsp_pool != NULL)
941 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
942 if (param->lsp_is_specific) {
945 for (i = 0; i < param->lsp_stripe_count; i++)
946 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
950 int llapi_dir_set_default_lmv(const char *name,
951 const struct llapi_stripe_param *param)
953 struct lmv_user_md lmu = { 0 };
957 rc = verify_dir_param(name, param);
961 /* TODO: default lmv doesn't support specific targets yet */
962 if (param->lsp_is_specific)
965 param2lmu(&lmu, param);
967 fd = open(name, O_DIRECTORY | O_RDONLY);
970 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
974 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
976 char *errmsg = "stripe already set";
978 if (errno != EEXIST && errno != EALREADY)
979 errmsg = strerror(errno);
981 llapi_err_noerrno(LLAPI_MSG_ERROR,
982 "default dirstripe error on '%s': %s",
989 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
990 int stripe_count, int stripe_pattern,
991 const char *pool_name)
993 const struct llapi_stripe_param param = {
994 .lsp_stripe_count = stripe_count,
995 .lsp_stripe_offset = stripe_offset,
996 .lsp_stripe_pattern = stripe_pattern,
997 .lsp_pool = (char *)pool_name
1000 return llapi_dir_set_default_lmv(name, ¶m);
1004 * Create a Lustre directory.
1006 * \param name the name of the directory to be created
1007 * \param mode permission of the file if it is created, see mode in open(2)
1008 * \param param stripe pattern of the newly created directory
1010 * \retval 0 on success
1011 * \retval negative errno on failure
1013 int llapi_dir_create(const char *name, mode_t mode,
1014 const struct llapi_stripe_param *param)
1016 struct lmv_user_md *lmu = NULL;
1018 struct obd_ioctl_data data = { 0 };
1021 char *dirpath = NULL;
1022 char *namepath = NULL;
1027 rc = verify_dir_param(name, param);
1031 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1032 param->lsp_is_specific ?
1033 LMV_USER_MAGIC_SPECIFIC :
1036 lmu = calloc(1, lmu_size);
1040 dirpath = strdup(name);
1046 namepath = strdup(name);
1053 param2lmu(lmu, param);
1055 filename = basename(namepath);
1056 dir = dirname(dirpath);
1058 data.ioc_inlbuf1 = (char *)filename;
1059 data.ioc_inllen1 = strlen(filename) + 1;
1060 data.ioc_inlbuf2 = (char *)lmu;
1061 data.ioc_inllen2 = lmu_size;
1062 data.ioc_type = mode;
1063 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1065 llapi_error(LLAPI_MSG_ERROR, rc,
1066 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1071 fd = open(dir, O_DIRECTORY | O_RDONLY);
1074 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1078 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1079 char *errmsg = "stripe already set";
1082 if (errno != EEXIST && errno != EALREADY)
1083 errmsg = strerror(errno);
1085 llapi_err_noerrno(LLAPI_MSG_ERROR,
1086 "dirstripe error on '%s': %s", name, errmsg);
1097 * Create a foreign directory.
1099 * \param name the name of the directory to be created
1100 * \param mode permission of the file if it is created, see mode in open(2)
1101 * \param type foreign type to be set in LMV EA
1102 * \param flags foreign flags to be set in LMV EA
1103 * \param value foreign pattern to be set in LMV EA
1105 * \retval 0 on success
1106 * \retval negative errno on failure
1108 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1109 __u32 flags, const char *value)
1111 struct lmv_foreign_md *lfm = NULL;
1112 size_t lfm_size, len;
1113 struct obd_ioctl_data data = { 0 };
1116 char *dirpath = NULL;
1117 char *namepath = NULL;
1122 len = strlen(value);
1123 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1126 llapi_error(LLAPI_MSG_ERROR, rc, "invalid LOV EA length %zu "
1127 "(must be 0 < len < %zu)\n", len,
1129 offsetof(struct lmv_foreign_md, lfm_value));
1132 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1133 lfm = calloc(1, lfm_size);
1137 dirpath = strdup(name);
1143 namepath = strdup(name);
1150 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1151 lfm->lfm_length = len;
1152 lfm->lfm_type = type;
1153 lfm->lfm_flags = flags;
1154 memcpy(lfm->lfm_value, value, len);
1156 filename = basename(namepath);
1157 dir = dirname(dirpath);
1159 data.ioc_inlbuf1 = (char *)filename;
1160 data.ioc_inllen1 = strlen(filename) + 1;
1161 data.ioc_inlbuf2 = (char *)lfm;
1162 data.ioc_inllen2 = lfm_size;
1163 data.ioc_type = mode;
1164 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1166 llapi_error(LLAPI_MSG_ERROR, rc,
1167 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1172 fd = open(dir, O_DIRECTORY | O_RDONLY);
1175 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1179 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1180 char *errmsg = "stripe already set";
1183 if (errno != EEXIST && errno != EALREADY)
1184 errmsg = strerror(errno);
1186 llapi_err_noerrno(LLAPI_MSG_ERROR,
1187 "dirstripe error on '%s': %s", name, errmsg);
1197 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1198 int stripe_count, int stripe_pattern,
1199 const char *pool_name)
1201 const struct llapi_stripe_param param = {
1202 .lsp_stripe_count = stripe_count,
1203 .lsp_stripe_offset = stripe_offset,
1204 .lsp_stripe_pattern = stripe_pattern,
1205 .lsp_pool = (char *)pool_name
1208 return llapi_dir_create(name, mode, ¶m);
1211 int llapi_direntry_remove(char *dname)
1213 char *dirpath = NULL;
1214 char *namepath = NULL;
1220 dirpath = strdup(dname);
1221 namepath = strdup(dname);
1222 if (!dirpath || !namepath)
1225 filename = basename(namepath);
1227 dir = dirname(dirpath);
1229 fd = open(dir, O_DIRECTORY | O_RDONLY);
1232 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
1237 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
1238 char *errmsg = strerror(errno);
1239 llapi_err_noerrno(LLAPI_MSG_ERROR,
1240 "error on ioctl %#jx for '%s' (%d): %s",
1241 (uintmax_t)LL_IOC_LMV_SETSTRIPE, filename,
1253 * Find the fsname, the full path, and/or an open fd.
1254 * Either the fsname or path must not be NULL
1256 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1259 char buf[PATH_MAX], mntdir[PATH_MAX];
1260 char *ptr, *ptr_end;
1262 int idx = 0, len = 0, mntlen, fd;
1265 /* get the mount point */
1266 fp = setmntent(PROC_MOUNTS, "r");
1269 llapi_error(LLAPI_MSG_ERROR, rc,
1270 "cannot retrieve filesystem mount point");
1274 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1277 if (!llapi_is_lustre_mnt(&mnt))
1280 if ((want & WANT_INDEX) && (idx++ != index))
1283 mntlen = strlen(mnt.mnt_dir);
1284 ptr = strchr(mnt.mnt_fsname, '/');
1285 while (ptr && *ptr == '/')
1287 /* thanks to the call to llapi_is_lustre_mnt() above,
1288 * we are sure that mnt.mnt_fsname contains ":/",
1289 * so ptr should never be NULL */
1293 while (*ptr_end != '/' && *ptr_end != '\0')
1296 /* Check the fsname for a match, if given */
1297 if (!(want & WANT_FSNAME) && fsname != NULL &&
1298 (strlen(fsname) > 0) &&
1299 (strncmp(ptr, fsname, ptr_end - ptr) != 0))
1302 /* If the path isn't set return the first one we find */
1303 if (path == NULL || strlen(path) == 0) {
1304 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1305 mntdir[sizeof(mntdir) - 1] = '\0';
1306 if ((want & WANT_FSNAME) && fsname != NULL) {
1307 strncpy(fsname, ptr, ptr_end - ptr);
1308 fsname[ptr_end - ptr] = '\0';
1312 /* Otherwise find the longest matching path */
1313 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1314 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1315 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1316 mntdir[sizeof(mntdir) - 1] = '\0';
1318 if ((want & WANT_FSNAME) && fsname != NULL) {
1319 strncpy(fsname, ptr, ptr_end - ptr);
1320 fsname[ptr_end - ptr] = '\0';
1329 if ((want & WANT_PATH) && path != NULL) {
1330 strncpy(path, mntdir, PATH_MAX);
1331 path[strlen(mntdir)] = '\0';
1333 if (want & WANT_FD) {
1334 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1337 llapi_error(LLAPI_MSG_ERROR, rc,
1338 "cannot open '%s': %s", mntdir,
1345 } else if (want & WANT_ERROR)
1346 llapi_err_noerrno(LLAPI_MSG_ERROR,
1347 "'%s' not on a mounted Lustre filesystem",
1348 (want & WANT_PATH) ? fsname : path);
1353 * search lustre mounts
1355 * Calling this function will return to the user the mount point, mntdir, and
1356 * the file system name, fsname, if the user passed a buffer to this routine.
1358 * The user inputs are pathname and index. If the pathname is supplied then
1359 * the value of the index will be ignored. The pathname will return data if
1360 * the pathname is located on a lustre mount. Index is used to pick which
1361 * mount point you want in the case of multiple mounted lustre file systems.
1362 * See function lfs_osts in lfs.c for an example of the index use.
1364 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1367 int want = WANT_PATH, idx = -1;
1369 if (!pathname || pathname[0] == '\0') {
1373 strcpy(mntdir, pathname);
1376 want |= WANT_FSNAME;
1377 return get_root_path(want, fsname, NULL, mntdir, idx);
1380 /* Given a path, find the corresponding Lustre fsname */
1381 int llapi_search_fsname(const char *pathname, char *fsname)
1386 path = realpath(pathname, NULL);
1388 char tmp[PATH_MAX - 1];
1394 if (pathname[0] != '/') {
1395 /* Need an absolute path, but realpath() only works for
1396 * pathnames that actually exist. We go through the
1397 * extra hurdle of dirname(getcwd() + pathname) in
1398 * case the relative pathname contains ".." in it. */
1399 char realpath[PATH_MAX - 1];
1401 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1403 llapi_error(LLAPI_MSG_ERROR, rc,
1404 "cannot get current working directory");
1408 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1409 if (rc >= sizeof(tmp)) {
1411 llapi_error(LLAPI_MSG_ERROR, rc,
1412 "invalid parent path '%s'",
1418 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1419 if (rc >= sizeof(buf)) {
1421 llapi_error(LLAPI_MSG_ERROR, rc,
1422 "invalid path '%s'", pathname);
1425 path = realpath(buf, NULL);
1427 ptr = strrchr(buf, '/');
1429 llapi_error(LLAPI_MSG_ERROR |
1430 LLAPI_MSG_NO_ERRNO, 0,
1431 "cannot resolve path '%s'",
1436 path = realpath(buf, NULL);
1439 llapi_error(LLAPI_MSG_ERROR, rc,
1440 "cannot resolve path '%s'",
1446 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1451 int llapi_search_rootpath(char *pathname, const char *fsname)
1453 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1456 int llapi_getname(const char *path, char *buf, size_t size)
1458 struct obd_uuid uuid_buf;
1459 char *uuid = uuid_buf.uuid;
1461 int rc, len, fsname_len;
1463 memset(&uuid_buf, 0, sizeof(uuid_buf));
1464 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1469 * We want to turn testfs-clilov-ffff88002738bc00 into
1470 * testfs-ffff88002738bc00 in a portable way that doesn't depend
1471 * on what is after "-clilov-" as it may change in the future.
1472 * Unfortunately, the "fsname" part may contain a dash, so we
1473 * can't just skip to the first dash, and the "instance" may be a
1474 * UUID in the future, so we can't necessarily go to the last dash.
1476 cfg_instance = strstr(uuid, "-clilov-");
1480 fsname_len = cfg_instance - uuid;
1481 cfg_instance += strlen("-clilov-");
1482 len = snprintf(buf, size, "%.*s-%s", fsname_len, uuid, cfg_instance);
1491 * Get the list of pool members.
1492 * \param poolname string of format \<fsname\>.\<poolname\>
1493 * \param members caller-allocated array of char*
1494 * \param list_size size of the members array
1495 * \param buffer caller-allocated buffer for storing OST names
1496 * \param buffer_size size of the buffer
1498 * \return number of members retrieved for this pool
1499 * \retval -error failure
1501 int llapi_get_poolmembers(const char *poolname, char **members,
1502 int list_size, char *buffer, int buffer_size)
1504 char fsname[PATH_MAX];
1513 /* name is FSNAME.POOLNAME */
1514 if (strlen(poolname) >= sizeof(fsname))
1517 snprintf(fsname, sizeof(fsname), "%s", poolname);
1518 pool = strchr(fsname, '.');
1525 rc = poolpath(&pathname, fsname, NULL);
1527 llapi_error(LLAPI_MSG_ERROR, rc,
1528 "Lustre filesystem '%s' not found",
1533 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1534 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1535 cfs_free_param_data(&pathname);
1536 if (rc >= sizeof(buf))
1538 fd = fopen(buf, "r");
1541 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1546 while (fgets(buf, sizeof(buf), fd) != NULL) {
1547 if (nb_entries >= list_size) {
1551 buf[sizeof(buf) - 1] = '\0';
1553 tmp = strchr(buf, '\n');
1556 if (used + strlen(buf) + 1 > buffer_size) {
1561 strcpy(buffer + used, buf);
1562 members[nb_entries] = buffer + used;
1563 used += strlen(buf) + 1;
1573 * Get the list of pools in a filesystem.
1574 * \param name filesystem name or path
1575 * \param poollist caller-allocated array of char*
1576 * \param list_size size of the poollist array
1577 * \param buffer caller-allocated buffer for storing pool names
1578 * \param buffer_size size of the buffer
1580 * \return number of pools retrieved for this filesystem
1581 * \retval -error failure
1583 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1584 char *buffer, int buffer_size)
1590 struct dirent *pool;
1592 unsigned int nb_entries = 0;
1593 unsigned int used = 0;
1596 /* initialize output array */
1597 for (i = 0; i < list_size; i++)
1600 /* is name a pathname ? */
1601 ptr = strchr(name, '/');
1603 char fsname_buf[MAXNAMLEN];
1605 /* We will need fsname for printing later */
1606 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1610 ptr = strrchr(fsname_buf, '-');
1614 fsname = strdup(fsname_buf);
1618 /* name is FSNAME */
1619 fsname = strdup(name);
1624 rc = poolpath(&pathname, fsname, NULL);
1626 llapi_error(LLAPI_MSG_ERROR, rc,
1627 "Lustre filesystem '%s' not found", name);
1631 dir = opendir(pathname.gl_pathv[0]);
1634 llapi_error(LLAPI_MSG_ERROR, rc,
1635 "Could not open pool list for '%s'",
1642 pool = readdir(dir);
1648 /* ignore . and .. */
1649 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1652 /* check output bounds */
1653 if (nb_entries >= list_size) {
1655 goto free_dir_no_msg;
1658 /* +2 for '.' and final '\0' */
1659 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1662 goto free_dir_no_msg;
1665 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1666 poollist[nb_entries] = buffer + used;
1667 used += strlen(pool->d_name) + strlen(fsname) + 2;
1673 llapi_error(LLAPI_MSG_ERROR, rc,
1674 "Error reading pool list for '%s'", name);
1676 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1681 cfs_free_param_data(&pathname);
1684 return rc != 0 ? rc : nb_entries;
1687 /* wrapper for lfs.c and obd.c */
1688 int llapi_poollist(const char *name)
1690 /* list of pool names (assume that pool count is smaller
1692 char **list, *buffer = NULL, *fsname = (char *)name;
1693 char *poolname = NULL, *tmp = NULL, data[16];
1694 enum param_filter type = FILTER_BY_PATH;
1695 int obdcount, bufsize, rc, nb, i;
1700 if (name[0] != '/') {
1701 fsname = strdup(name);
1705 poolname = strchr(fsname, '.');
1708 type = FILTER_BY_FS_NAME;
1711 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1712 data, sizeof(data));
1715 obdcount = atoi(data);
1717 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1718 * and also an array to store the pointers for all that
1719 * allocated space. */
1721 bufsize = sizeof(struct obd_uuid) * obdcount;
1722 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1723 if (buffer == NULL) {
1727 list = (char **) (buffer + bufsize);
1730 /* name is a path or fsname */
1731 nb = llapi_get_poollist(name, list, obdcount,
1734 /* name is a pool name (<fsname>.<poolname>) */
1735 nb = llapi_get_poolmembers(name, list, obdcount,
1739 if (nb == -EOVERFLOW) {
1742 goto retry_get_pools;
1745 for (i = 0; i < nb; i++)
1746 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1747 rc = (nb < 0 ? nb : 0);
1751 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1756 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1757 void *data, struct dirent64 *de);
1759 #define OBD_NOT_FOUND (-1)
1761 static void find_param_fini(struct find_param *param)
1763 if (param->fp_migrate)
1766 if (param->fp_obd_indexes) {
1767 free(param->fp_obd_indexes);
1768 param->fp_obd_indexes = NULL;
1771 if (param->fp_lmd) {
1772 free(param->fp_lmd);
1773 param->fp_lmd = NULL;
1776 if (param->fp_lmv_md) {
1777 free(param->fp_lmv_md);
1778 param->fp_lmv_md = NULL;
1782 static int common_param_init(struct find_param *param, char *path)
1784 int lum_size = get_mds_md_size(path);
1789 /* migrate has fp_lmv_md initialized outside */
1790 if (param->fp_migrate)
1793 if (lum_size < PATH_MAX + 1)
1794 lum_size = PATH_MAX + 1;
1796 param->fp_lum_size = lum_size;
1797 param->fp_lmd = calloc(1, sizeof(lstat_t) + lum_size);
1798 if (param->fp_lmd == NULL) {
1799 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1800 "error: allocation of %zu bytes for ioctl",
1801 sizeof(lstat_t) + param->fp_lum_size);
1805 param->fp_lmv_stripe_count = 256;
1806 param->fp_lmv_md = calloc(1,
1807 lmv_user_md_size(param->fp_lmv_stripe_count,
1808 LMV_USER_MAGIC_SPECIFIC));
1809 if (param->fp_lmv_md == NULL) {
1810 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1811 "error: allocation of %d bytes for ioctl",
1812 lmv_user_md_size(param->fp_lmv_stripe_count,
1813 LMV_USER_MAGIC_SPECIFIC));
1814 find_param_fini(param);
1818 param->fp_got_uuids = 0;
1819 param->fp_obd_indexes = NULL;
1820 param->fp_obd_index = OBD_NOT_FOUND;
1821 param->fp_mdt_index = OBD_NOT_FOUND;
1825 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1826 struct dirent64 *de)
1828 struct find_param *param = data;
1834 /* set errno upon failure */
1835 static DIR *opendir_parent(const char *path)
1841 path_copy = strdup(path);
1842 if (path_copy == NULL)
1845 parent_path = dirname(path_copy);
1846 parent = opendir(parent_path);
1852 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1857 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1858 if (param->fp_get_default_lmv)
1859 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1861 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1863 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1864 if (errno == E2BIG && ret != 0) {
1868 /* if foreign LMV case, fake stripes number */
1869 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
1870 struct lmv_foreign_md *lfm;
1872 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1873 if (lfm->lfm_length < XATTR_SIZE_MAX -
1874 offsetof(typeof(*lfm), lfm_value)) {
1875 uint32_t size = lfm->lfm_length +
1876 offsetof(typeof(*lfm), lfm_value);
1878 stripe_count = lmv_foreign_to_md_stripes(size);
1880 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1881 "error: invalid %d foreign size "
1882 "returned from ioctl",
1887 stripe_count = param->fp_lmv_md->lum_stripe_count;
1889 if (stripe_count <= param->fp_lmv_stripe_count)
1892 free(param->fp_lmv_md);
1893 param->fp_lmv_stripe_count = stripe_count;
1894 lmv_size = lmv_user_md_size(stripe_count,
1895 LMV_USER_MAGIC_SPECIFIC);
1896 param->fp_lmv_md = malloc(lmv_size);
1897 if (param->fp_lmv_md == NULL) {
1898 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1899 "error: allocation of %d bytes for ioctl",
1900 lmv_user_md_size(param->fp_lmv_stripe_count,
1901 LMV_USER_MAGIC_SPECIFIC));
1909 int get_lmd_info_fd(char *path, int parent_fd, int dir_fd,
1910 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1912 struct lov_user_mds_data *lmd = lmdbuf;
1913 lstat_t *st = &lmd->lmd_st;
1916 if (parent_fd < 0 && dir_fd < 0)
1918 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1922 /* LL_IOC_MDC_GETINFO operates on the current directory inode
1923 * and returns struct lov_user_mds_data, while
1924 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
1926 ret = ioctl(dir_fd, type == GET_LMD_INFO ? LL_IOC_MDC_GETINFO :
1927 LL_IOC_LOV_GETSTRIPE,
1929 } else if (parent_fd >= 0) {
1930 char *fname = strrchr(path, '/');
1932 /* IOC_MDC_GETFILEINFO takes as input the filename (relative to
1933 * the parent directory) and returns struct lov_user_mds_data,
1934 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
1936 * This avoids opening, locking, and closing each file on the
1937 * client if that is not needed. Multiple of these ioctl() can
1938 * be done on the parent dir with a single open for all
1939 * files in that directory, and it also doesn't pollute the
1940 * client dcache with millions of dentries when traversing
1941 * a large filesystem.
1943 fname = (fname == NULL ? path : fname + 1);
1945 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
1948 else if (ret >= lmdlen || ret++ == 0)
1951 ret = ioctl(parent_fd, type == GET_LMD_INFO ?
1952 IOC_MDC_GETFILEINFO :
1953 IOC_MDC_GETFILESTRIPE, lmdbuf);
1956 if (ret && type == GET_LMD_INFO) {
1957 if (errno == ENOTTY) {
1958 /* ioctl is not supported, it is not a lustre fs.
1959 * Do the regular lstat(2) instead.
1961 ret = lstat_f(path, st);
1964 llapi_error(LLAPI_MSG_ERROR, ret,
1965 "error: %s: lstat failed for %s",
1968 } else if (errno == ENOENT) {
1970 llapi_error(LLAPI_MSG_WARN, ret,
1971 "warning: %s does not exist", path);
1972 } else if (errno != EISDIR && errno != ENODATA) {
1974 llapi_error(LLAPI_MSG_ERROR, ret,
1975 "%s ioctl failed for %s.",
1976 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
1977 "IOC_MDC_GETFILEINFO", path);
1984 static int get_lmd_info(char *path, DIR *parent, DIR *dir, void *lmdbuf,
1985 int lmdlen, enum get_lmd_info_type type)
1991 parent_fd = dirfd(parent);
1993 dir_fd = dirfd(dir);
1995 return get_lmd_info_fd(path, parent_fd, dir_fd, lmdbuf, lmdlen, type);
1998 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1999 semantic_func_t sem_init,
2000 semantic_func_t sem_fini, void *data,
2001 struct dirent64 *de)
2003 struct find_param *param = (struct find_param *)data;
2004 struct dirent64 *dent;
2012 if (!d && errno != ENOTDIR) {
2014 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2017 } else if (!d && !parent) {
2018 /* ENOTDIR. Open the parent dir. */
2019 p = opendir_parent(path);
2026 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
2032 while ((dent = readdir64(d)) != NULL) {
2035 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2039 if ((len + dent->d_reclen + 2) > size) {
2040 llapi_err_noerrno(LLAPI_MSG_ERROR,
2041 "error: %s: string buffer too small",
2046 strcat(path, dent->d_name);
2048 if (dent->d_type == DT_UNKNOWN) {
2049 lstat_t *st = ¶m->fp_lmd->lmd_st;
2051 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
2052 param->fp_lum_size, GET_LMD_INFO);
2054 dent->d_type = IFTODT(st->st_mode);
2061 switch (dent->d_type) {
2063 llapi_err_noerrno(LLAPI_MSG_ERROR,
2064 "error: %s: '%s' is UNKNOWN type %d",
2065 __func__, dent->d_name, dent->d_type);
2068 rc = llapi_semantic_traverse(path, size, d, sem_init,
2069 sem_fini, data, dent);
2070 if (rc != 0 && ret == 0)
2076 rc = sem_init(path, d, NULL, data, dent);
2077 if (rc < 0 && ret == 0) {
2082 if (sem_fini && rc == 0)
2083 sem_fini(path, d, NULL, data, dent);
2091 sem_fini(path, parent, &d, data, de);
2100 static int param_callback(char *path, semantic_func_t sem_init,
2101 semantic_func_t sem_fini, struct find_param *param)
2103 int ret, len = strlen(path);
2106 if (len > PATH_MAX) {
2108 llapi_error(LLAPI_MSG_ERROR, ret,
2109 "Path name '%s' is too long", path);
2113 buf = (char *)malloc(PATH_MAX + 1);
2117 snprintf(buf, PATH_MAX + 1, "%s", path);
2118 ret = common_param_init(param, buf);
2122 param->fp_depth = 0;
2124 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
2125 sem_fini, param, NULL);
2127 find_param_fini(param);
2129 return ret < 0 ? ret : 0;
2132 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2134 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
2138 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2143 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2145 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2148 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2153 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2157 fd = open(path, O_RDONLY | O_NONBLOCK);
2160 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
2164 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2170 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2174 fd = open(path, O_RDONLY | O_NONBLOCK);
2177 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2181 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2193 * If uuidp is NULL, return the number of available obd uuids.
2194 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2195 * there are more OSTs than allocated to uuidp, then an error is returned with
2196 * the ost_count set to number of available obd uuids.
2198 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2199 int *ost_count, enum tgt_type type)
2201 char buf[PATH_MAX], format[32];
2202 int rc = 0, index = 0;
2203 struct obd_uuid name;
2207 /* Get the lov name */
2208 if (type == LOV_TYPE)
2209 rc = llapi_file_fget_lov_uuid(fd, &name);
2211 rc = llapi_file_fget_lmv_uuid(fd, &name);
2215 /* Now get the ost uuids */
2216 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2217 FILTER_BY_EXACT, "target_obd", ¶m);
2221 fp = fopen(param.gl_pathv[0], "r");
2224 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2229 snprintf(format, sizeof(format),
2230 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2231 while (fgets(buf, sizeof(buf), fp) != NULL) {
2232 if (uuidp && (index < *ost_count)) {
2233 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2241 if (uuidp && (index > *ost_count))
2246 cfs_free_param_data(¶m);
2250 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2252 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2255 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2260 root = open(mnt, O_RDONLY | O_DIRECTORY);
2263 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2268 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2276 /* Check if user specified value matches a real uuid. Ignore _UUID,
2277 * -osc-4ba41334, other trailing gunk in comparison.
2278 * @param real_uuid ends in "_UUID"
2279 * @param search_uuid may or may not end in "_UUID"
2281 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2283 int cmplen = strlen(real_uuid);
2284 int searchlen = strlen(search_uuid);
2286 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2288 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2291 /* The UUIDs may legitimately be different lengths, if
2292 * the system was upgraded from an older version. */
2293 if (cmplen != searchlen)
2296 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2299 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2300 * returned in param->fp_obd_index */
2301 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2303 struct obd_uuid obd_uuid;
2310 if (param->fp_got_uuids)
2313 /* Get the lov/lmv name */
2314 if (param->fp_get_lmv)
2315 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2317 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2319 if (rc != -ENOTTY) {
2320 llapi_error(LLAPI_MSG_ERROR, rc,
2321 "error: can't get %s name: %s",
2322 param->fp_get_lmv ? "lmv" : "lov",
2330 param->fp_got_uuids = 1;
2332 /* Now get the ost uuids */
2333 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2334 obd_uuid.uuid, FILTER_BY_EXACT,
2335 "target_obd", ¶m_data);
2339 fp = fopen(param_data.gl_pathv[0], "r");
2342 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2343 param_data.gl_pathv[0]);
2347 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2348 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2349 param->fp_get_lmv ? "MDTS" : "OBDS");
2351 snprintf(format, sizeof(format),
2352 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2353 while (fgets(buf, sizeof(buf), fp) != NULL) {
2356 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2359 if (param->fp_obd_uuid) {
2360 if (llapi_uuid_match(obd_uuid.uuid,
2361 param->fp_obd_uuid->uuid)) {
2362 param->fp_obd_index = index;
2365 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2366 /* Print everything */
2367 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2370 param->fp_obds_printed = 1;
2374 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2375 llapi_err_noerrno(LLAPI_MSG_ERROR,
2376 "error: %s: unknown obduuid: %s",
2377 __func__, param->fp_obd_uuid->uuid);
2381 cfs_free_param_data(¶m_data);
2385 /* In this case, param->fp_obd_uuid will be an array of obduuids and
2386 * obd index for all these obduuids will be returned in
2387 * param->fp_obd_indexes */
2388 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2389 int num_obds, int **obdindexes, int *obdindex,
2392 int ret, obdcount, obd_valid = 0, obdnum;
2394 struct obd_uuid *uuids = NULL;
2398 if (type == LOV_TYPE)
2399 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2401 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2405 obdcount = atoi(buf);
2406 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2411 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2413 if (ret == -EOVERFLOW) {
2414 struct obd_uuid *uuids_temp;
2416 uuids_temp = realloc(uuids, obdcount *
2417 sizeof(struct obd_uuid));
2418 if (uuids_temp != NULL) {
2420 goto retry_get_uuids;
2425 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2429 indexes = malloc(num_obds * sizeof(*obdindex));
2430 if (indexes == NULL) {
2435 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2438 /* The user may have specified a simple index */
2439 i = strtol(obduuids[obdnum].uuid, &end, 0);
2440 if (end && *end == '\0' && i < obdcount) {
2441 indexes[obdnum] = i;
2444 for (i = 0; i < obdcount; i++) {
2445 if (llapi_uuid_match(uuids[i].uuid,
2446 obduuids[obdnum].uuid)) {
2447 indexes[obdnum] = i;
2453 if (i >= obdcount) {
2454 indexes[obdnum] = OBD_NOT_FOUND;
2455 llapi_err_noerrno(LLAPI_MSG_ERROR,
2456 "invalid obduuid '%s'",
2457 obduuids[obdnum].uuid);
2463 *obdindex = OBD_NOT_FOUND;
2465 *obdindex = obd_valid;
2467 *obdindexes = indexes;
2475 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2479 if (param->fp_mdt_uuid) {
2480 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2482 ¶m->fp_mdt_indexes,
2483 ¶m->fp_mdt_index, LMV_TYPE);
2488 if (param->fp_obd_uuid) {
2489 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2491 ¶m->fp_obd_indexes,
2492 ¶m->fp_obd_index, LOV_TYPE);
2497 param->fp_got_uuids = 1;
2502 int llapi_ostlist(char *path, struct find_param *param)
2507 fd = open(path, O_RDONLY | O_DIRECTORY);
2511 ret = setup_obd_uuid(fd, path, param);
2518 * Tries to determine the default stripe attributes for a given filesystem. The
2519 * filesystem to check should be specified by fsname, or will be determined
2522 static int sattr_get_defaults(const char *const fsname,
2523 unsigned int *scount,
2524 unsigned int *ssize,
2525 unsigned int *soffset)
2531 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2532 "stripecount", val, sizeof(val));
2535 *scount = atoi(val);
2539 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2540 "stripesize", val, sizeof(val));
2547 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2548 "stripeoffset", val, sizeof(val));
2551 *soffset = atoi(val);
2558 * Tries to gather the default stripe attributes for a given filesystem. If
2559 * the attributes can be determined, they are cached for easy retreival the
2560 * next time they are needed. Only a single filesystem's attributes are
2563 int sattr_cache_get_defaults(const char *const fsname,
2564 const char *const pathname, unsigned int *scount,
2565 unsigned int *ssize, unsigned int *soffset)
2568 char fsname[PATH_MAX + 1];
2569 unsigned int stripecount;
2570 unsigned int stripesize;
2571 unsigned int stripeoffset;
2577 char fsname_buf[PATH_MAX + 1];
2578 unsigned int tmp[3];
2580 if (fsname == NULL) {
2581 rc = llapi_search_fsname(pathname, fsname_buf);
2585 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2588 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2590 * Ensure all 3 sattrs (count, size, and offset) are
2591 * successfully retrieved and stored in tmp before writing to
2594 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2598 cache.stripecount = tmp[0];
2599 cache.stripesize = tmp[1];
2600 cache.stripeoffset = tmp[2];
2601 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2605 *scount = cache.stripecount;
2607 *ssize = cache.stripesize;
2609 *soffset = cache.stripeoffset;
2614 static char *layout2name(__u32 layout_pattern)
2616 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2618 else if (layout_pattern == LOV_PATTERN_MDT)
2620 else if (layout_pattern == LOV_PATTERN_RAID0)
2622 else if (layout_pattern ==
2623 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2624 return "raid0,overstriped";
2629 enum lov_dump_flags {
2630 LDF_IS_DIR = 0x0001,
2631 LDF_IS_RAW = 0x0002,
2632 LDF_INDENT = 0x0004,
2633 LDF_SKIP_OBJS = 0x0008,
2635 LDF_EXTENSION = 0x0020,
2638 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2639 struct lov_user_ost_data_v1 *objects,
2640 enum llapi_layout_verbose verbose,
2641 int depth, char *pool_name,
2642 enum lov_dump_flags flags)
2644 bool is_dir = flags & LDF_IS_DIR;
2645 bool is_raw = flags & LDF_IS_RAW;
2646 bool indent = flags & LDF_INDENT;
2647 bool yaml = flags & LDF_YAML;
2648 bool skip_objs = flags & LDF_SKIP_OBJS;
2649 bool extension = flags & LDF_EXTENSION;
2650 char *prefix = is_dir ? "" : "lmm_";
2651 char *separator = "";
2652 char *space = indent ? " " : "";
2655 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2656 lmm_oi_set_seq(&lum->lmm_oi, 0);
2657 if (!indent && (verbose & VERBOSE_DETAIL))
2658 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2661 if (!yaml && !indent && depth && path &&
2662 ((verbose != VERBOSE_OBJID) || !is_dir))
2663 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2665 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2666 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2667 space, prefix, lum->lmm_magic);
2668 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2670 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2671 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2673 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2675 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2680 if (verbose & ~VERBOSE_DFID)
2681 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2683 /* This needs a bit of hand-holding since old 1.x lmm_oi
2684 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2685 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2686 * a real FID. Ideally the 2.x code would have stored this
2687 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2688 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2689 * worked properly (especially since IGIF FIDs use mds_inum as
2690 * the FID SEQ), but unfortunately that didn't happen.
2692 * Print it to look like an IGIF FID, even though the fields
2693 * are reversed on disk, so that it makes sense to userspace.
2695 * Don't use ostid_id() and ostid_seq(), since they assume the
2696 * oi_fid fields are in the right order. This is why there are
2697 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2699 * For newer layout types hopefully this will be a real FID. */
2700 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2701 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2702 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2703 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2704 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2706 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2707 (unsigned long long)seq, oid, ver);
2709 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2710 (unsigned long long)seq, oid, ver);
2713 if (verbose & VERBOSE_STRIPE_COUNT) {
2714 if (verbose & ~VERBOSE_STRIPE_COUNT)
2715 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2718 if (!is_raw && lum->lmm_stripe_count == 0 &&
2719 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2720 unsigned int scount;
2721 rc = sattr_cache_get_defaults(NULL, path,
2725 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2728 llapi_error(LLAPI_MSG_ERROR, rc,
2729 "Cannot determine default"
2732 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2734 (__s16)lum->lmm_stripe_count);
2737 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2739 (__s16)lum->lmm_stripe_count);
2741 if (!yaml && is_dir)
2747 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
2748 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
2749 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2750 if (verbose & ~VERBOSE_EXT_SIZE && extension)
2751 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
2753 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
2754 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2756 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2758 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2761 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2763 llapi_error(LLAPI_MSG_ERROR, rc,
2764 "Cannot determine default"
2767 /* Extension size is in KiB */
2768 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2770 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
2771 (unsigned long long)lum->lmm_stripe_size);
2773 if (!yaml && is_dir)
2779 if ((verbose & VERBOSE_PATTERN)) {
2780 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2781 if (verbose & ~VERBOSE_PATTERN)
2782 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2784 if (lov_pattern_supported(lum->lmm_pattern))
2785 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2786 layout2name(lum->lmm_pattern));
2788 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2789 separator = (!yaml && is_dir) ? " " : "\n";
2792 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2793 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2794 if (verbose & ~VERBOSE_GENERATION)
2795 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2797 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2798 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2802 if (verbose & VERBOSE_STRIPE_OFFSET) {
2803 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2804 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2805 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2807 if (is_dir || skip_objs)
2808 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2809 lum->lmm_stripe_offset ==
2810 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2811 lum->lmm_stripe_offset);
2812 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
2813 llapi_printf(LLAPI_MSG_NORMAL, "0");
2815 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2816 objects[0].l_ost_idx);
2817 if (!yaml && is_dir)
2823 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2824 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2825 if (verbose & ~VERBOSE_POOL)
2826 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2828 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2829 if (!yaml && is_dir)
2835 if (strlen(separator) != 0)
2836 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2839 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2840 struct lov_user_ost_data_v1 *objects,
2841 char *path, int obdindex, int depth,
2842 enum llapi_layout_verbose verbose,
2843 enum lov_dump_flags flags)
2845 bool is_dir = flags & LDF_IS_DIR;
2846 bool indent = flags & LDF_INDENT;
2847 bool skip_objs = flags & LDF_SKIP_OBJS;
2848 bool yaml = flags & LDF_YAML;
2849 bool obdstripe = obdindex == OBD_NOT_FOUND;
2852 if (!obdstripe && !skip_objs) {
2853 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2854 if (obdindex == objects[i].l_ost_idx) {
2864 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2867 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
2868 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2869 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
2870 char *space = " - ";
2873 llapi_printf(LLAPI_MSG_NORMAL,
2874 "%6slmm_objects:\n", " ");
2876 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2878 llapi_printf(LLAPI_MSG_NORMAL,
2879 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2881 for (i = 0; i < lum->lmm_stripe_count; i++) {
2882 int idx = objects[i].l_ost_idx;
2883 long long oid = ostid_id(&objects[i].l_ost_oi);
2884 long long gr = ostid_seq(&objects[i].l_ost_oi);
2886 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2890 struct lu_fid fid = { 0 };
2892 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2893 llapi_printf(LLAPI_MSG_NORMAL,
2894 "%sl_ost_idx: %d\n", space, idx);
2895 llapi_printf(LLAPI_MSG_NORMAL,
2896 "%8sl_fid: "DFID_NOBRACE"\n",
2898 } else if (indent) {
2899 struct lu_fid fid = { 0 };
2901 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2902 llapi_printf(LLAPI_MSG_NORMAL,
2903 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
2904 space, i, idx, PFID(&fid));
2907 sprintf(fmt, "%s%s%s\n",
2908 "\t%6u\t%14llu\t%#13llx\t",
2909 (fid_seq_is_rsvd(gr) ||
2910 fid_seq_is_mdt0(gr)) ?
2911 "%14llu" : "%#14llx", "%s");
2912 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2914 obdindex == idx ? " *" : "");
2918 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2921 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2922 char *path, int obdindex, int depth,
2923 enum llapi_layout_verbose verbose,
2924 enum lov_dump_flags flags)
2926 struct lmv_user_mds_data *objects = lum->lum_objects;
2927 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2928 char *separator = "";
2929 bool yaml = flags & LDF_YAML;
2930 bool obdstripe = false;
2933 if (obdindex != OBD_NOT_FOUND) {
2934 if (lum->lum_stripe_count == 0) {
2935 if (obdindex == lum->lum_stripe_offset)
2938 for (i = 0; i < lum->lum_stripe_count; i++) {
2939 if (obdindex == objects[i].lum_mds) {
2940 llapi_printf(LLAPI_MSG_NORMAL,
2955 /* show all information default */
2957 if (lum->lum_magic == LMV_USER_MAGIC)
2958 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
2959 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
2961 verbose = VERBOSE_OBJID;
2964 if (depth && path && ((verbose != VERBOSE_OBJID)))
2965 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2967 if (verbose & VERBOSE_STRIPE_COUNT) {
2968 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2969 if (verbose & ~VERBOSE_STRIPE_COUNT)
2970 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2971 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2972 (int)lum->lum_stripe_count);
2973 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
2979 if (verbose & VERBOSE_STRIPE_OFFSET) {
2980 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2981 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2982 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2983 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2984 (int)lum->lum_stripe_offset);
2985 if (verbose & VERBOSE_HASH_TYPE && !yaml)
2991 if (verbose & VERBOSE_HASH_TYPE) {
2992 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
2993 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
2995 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2996 if (verbose & ~VERBOSE_HASH_TYPE)
2997 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
2998 if (type < LMV_HASH_TYPE_MAX)
2999 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3000 mdt_hash_name[type]);
3002 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3004 if (flags & LMV_HASH_FLAG_SPACE)
3005 llapi_printf(LLAPI_MSG_NORMAL, ",space");
3006 if (flags & LMV_HASH_FLAG_MIGRATION)
3007 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3008 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3009 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3010 if (flags & LMV_HASH_FLAG_LOST_LMV)
3011 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3016 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3017 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3018 if (lum->lum_stripe_count > 0)
3019 llapi_printf(LLAPI_MSG_NORMAL,
3020 "mdtidx\t\t FID[seq:oid:ver]\n");
3021 for (i = 0; i < lum->lum_stripe_count; i++) {
3022 int idx = objects[i].lum_mds;
3023 struct lu_fid *fid = &objects[i].lum_fid;
3024 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3025 llapi_printf(LLAPI_MSG_NORMAL,
3026 "%6u\t\t "DFID"\t\t%s\n",
3028 obdindex == idx ? " *" : "");
3033 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3034 pool_name[0] != '\0') {
3035 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3036 if (verbose & ~VERBOSE_POOL)
3037 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3039 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3043 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3044 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3047 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3048 enum lov_dump_flags flags)
3050 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3051 int depth = param->fp_max_depth;
3052 enum llapi_layout_verbose verbose = param->fp_verbose;
3053 bool yaml = flags & LDF_YAML;
3055 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3056 !(flags & LDF_IS_DIR)) && !yaml)
3057 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3059 if (verbose & VERBOSE_DETAIL) {
3060 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3061 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3062 " ", comp_v1->lcm_magic);
3063 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3064 " ", comp_v1->lcm_size);
3065 if (flags & LDF_IS_DIR)
3066 llapi_printf(LLAPI_MSG_NORMAL,
3067 "%2slcm_flags: %s\n", " ",
3068 comp_v1->lcm_mirror_count > 0 ?
3071 llapi_printf(LLAPI_MSG_NORMAL,
3072 "%2slcm_flags: %s\n", " ",
3073 llapi_layout_flags_string(comp_v1->lcm_flags));
3076 if (verbose & VERBOSE_GENERATION) {
3077 if (verbose & ~VERBOSE_GENERATION)
3078 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3080 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3083 if (verbose & VERBOSE_MIRROR_COUNT) {
3084 if (verbose & ~VERBOSE_MIRROR_COUNT)
3085 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3087 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3088 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3089 comp_v1->lcm_mirror_count + 1 : 1);
3092 if (verbose & VERBOSE_COMP_COUNT) {
3093 if (verbose & ~VERBOSE_COMP_COUNT)
3094 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3096 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3097 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3098 comp_v1->lcm_entry_count : 0);
3101 if (verbose & VERBOSE_DETAIL && !yaml)
3102 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3105 static void lcme_flags2str(__u32 comp_flags)
3111 llapi_printf(LLAPI_MSG_NORMAL, "0");
3114 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3115 if (comp_flags & comp_flags_table[i].cfn_flag) {
3117 llapi_printf(LLAPI_MSG_NORMAL, ",");
3118 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3119 comp_flags_table[i].cfn_name);
3120 comp_flags &= ~comp_flags_table[i].cfn_flag;
3126 llapi_printf(LLAPI_MSG_NORMAL, ",");
3127 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3131 static void lov_dump_comp_v1_entry(struct find_param *param,
3132 enum lov_dump_flags flags, int index)
3134 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3135 struct lov_comp_md_entry_v1 *entry;
3136 char *separator = "";
3137 enum llapi_layout_verbose verbose = param->fp_verbose;
3138 bool yaml = flags & LDF_YAML;
3140 entry = &comp_v1->lcm_entries[index];
3143 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3145 if (verbose & VERBOSE_COMP_ID) {
3146 if (verbose & VERBOSE_DETAIL && !yaml)
3147 llapi_printf(LLAPI_MSG_NORMAL,
3148 "%slcme_id: ", " - ");
3149 else if (verbose & ~VERBOSE_COMP_ID)
3150 llapi_printf(LLAPI_MSG_NORMAL,
3151 "%4slcme_id: ", " ");
3152 if (entry->lcme_id != LCME_ID_INVAL)
3153 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3155 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3159 if (verbose & VERBOSE_MIRROR_ID) {
3160 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3161 if (verbose & ~VERBOSE_MIRROR_ID)
3162 llapi_printf(LLAPI_MSG_NORMAL,
3163 "%4slcme_mirror_id: ", " ");
3164 if (entry->lcme_id != LCME_ID_INVAL)
3165 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3166 mirror_id_of(entry->lcme_id));
3168 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3172 if (verbose & VERBOSE_COMP_FLAGS) {
3173 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3174 if (verbose & ~VERBOSE_COMP_FLAGS)
3175 llapi_printf(LLAPI_MSG_NORMAL,
3176 "%4slcme_flags: ", " ");
3177 lcme_flags2str(entry->lcme_flags);
3180 /* print snapshot timestamp if its a nosync comp */
3181 if ((verbose & VERBOSE_COMP_FLAGS) &&
3182 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3183 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3184 if (verbose & ~VERBOSE_COMP_FLAGS)
3185 llapi_printf(LLAPI_MSG_NORMAL,
3186 "%4slcme_timestamp: ", " ");
3188 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3189 (unsigned long long)entry->lcme_timestamp);
3191 time_t stamp = entry->lcme_timestamp;
3192 char *date_str = asctime(localtime(&stamp));
3194 date_str[strlen(date_str) - 1] = '\0';
3195 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3201 if (verbose & VERBOSE_COMP_START) {
3202 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3203 if (verbose & ~VERBOSE_COMP_START)
3204 llapi_printf(LLAPI_MSG_NORMAL,
3205 "%4slcme_extent.e_start: ", " ");
3206 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3207 (unsigned long long)entry->lcme_extent.e_start);
3211 if (verbose & VERBOSE_COMP_END) {
3212 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3213 if (verbose & ~VERBOSE_COMP_END)
3214 llapi_printf(LLAPI_MSG_NORMAL,
3215 "%4slcme_extent.e_end: ", " ");
3216 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3217 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3219 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3220 (unsigned long long)entry->lcme_extent.e_end);
3225 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3226 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3227 } else if (verbose & VERBOSE_DETAIL) {
3228 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3229 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3230 " ", entry->lcme_offset);
3231 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3232 " ", entry->lcme_size);
3233 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3235 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3239 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3240 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3242 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3243 * The table below gives the answers for the specified parameters (value and
3244 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3245 * --------------------------------------
3246 * 1 | file > limit; sign > 0 | -1 / -1 |
3247 * 2 | file = limit; sign > 0 | -1 / -1 |
3248 * 3 | file < limit; sign > 0 | ? / 1 |
3249 * 4 | file > limit; sign = 0 | -1 / -1 |
3250 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3251 * 6 | file < limit; sign = 0 | ? / -1 |
3252 * 7 | file > limit; sign < 0 | 1 / 1 |
3253 * 8 | file = limit; sign < 0 | ? / -1 |
3254 * 9 | file < limit; sign < 0 | ? / -1 |
3255 * --------------------------------------
3256 * Note: 5th actually means that the value is within the interval
3257 * (limit - margin, limit]. */
3258 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3259 int sign, int negopt, unsigned long long margin,
3265 /* Drop the fraction of margin (of days or size). */
3266 if (file + margin <= limit)
3268 } else if (sign == 0) {
3269 if (file <= limit && file + margin > limit)
3271 else if (file + margin <= limit)
3273 } else if (sign < 0) {
3280 return negopt ? ~ret + 1 : ret;
3283 static inline struct lov_user_md *
3284 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3286 return (struct lov_user_md *)((char *)comp_v1 +
3287 comp_v1->lcm_entries[ent_idx].lcme_offset);
3290 static inline struct lov_user_ost_data_v1 *
3291 lov_v1v3_objects(struct lov_user_md *v1)
3293 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3294 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3296 return v1->lmm_objects;
3300 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3302 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3303 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3304 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3306 pool_name[0] = '\0';
3310 print_last_init_comp(struct find_param *param)
3312 /* print all component info */
3313 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3316 /* print specific component info */
3317 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3318 param->fp_check_comp_start || param->fp_check_comp_end ||
3319 param->fp_check_mirror_id || param->fp_check_mirror_index)
3325 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3329 if (param->fp_comp_end == LUSTRE_EOF) {
3330 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3331 match = end == LUSTRE_EOF ? 1 : -1;
3332 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3333 match = end == LUSTRE_EOF ? -1 : 1;
3334 else /* at least EOF */
3336 if (param->fp_exclude_comp_end)
3339 unsigned long long margin;
3341 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3342 match = find_value_cmp(end, param->fp_comp_end,
3343 param->fp_comp_end_sign,
3344 param->fp_exclude_comp_end, margin, 0);
3351 * An example of "getstripe -v" for a two components PFL file:
3354 * lcm_magic: 0x0BD60BD0
3358 * lcm_entry_count: 2
3362 * lcme_extent.e_start: 0
3363 * lcme_extent.e_end: 1048576
3367 * lmm_magic: 0x0BD10BD0
3368 * lmm_seq: 0x200000401
3369 * lmm_object_id: 0x1
3370 * lmm_fid: [0x200000401:0x1:0x0]
3371 * lmm_stripe_count: 1
3372 * lmm_stripe_size: 1048576
3373 * lmm_pattern: raid0
3375 * lmm_stripe_offset: 0
3377 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3381 * lcme_extent.e_start: 1048576
3382 * lcme_extent.e_end: EOF
3386 * lmm_magic: 0x0BD10BD0
3387 * lmm_seq: 0x200000401
3388 * lmm_object_id: 0x1
3389 * lmm_fid: [0x200000401:0x1:0x0]
3390 * lmm_stripe_count: 2
3391 * lmm_stripe_size: 1048576
3392 * lmm_pattern: raid0
3394 * lmm_stripe_offset: 1
3396 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3397 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3399 static void lov_dump_comp_v1(struct find_param *param, char *path,
3400 enum lov_dump_flags flags)
3402 struct lov_comp_md_entry_v1 *entry;
3403 struct lov_user_ost_data_v1 *objects;
3404 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3405 struct lov_user_md_v1 *v1;
3406 char pool_name[LOV_MAXPOOLNAME + 1];
3407 int obdindex = param->fp_obd_index;
3408 int i, j, match, ext;
3409 bool obdstripe = false;
3410 __u16 mirror_index = 0;
3411 __u16 mirror_id = 0;
3413 if (obdindex != OBD_NOT_FOUND) {
3414 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3415 i < comp_v1->lcm_entry_count; i++) {
3416 if (!(comp_v1->lcm_entries[i].lcme_flags &
3420 v1 = lov_comp_entry(comp_v1, i);
3421 objects = lov_v1v3_objects(v1);
3423 for (j = 0; j < v1->lmm_stripe_count; j++) {
3424 if (obdindex == objects[j].l_ost_idx) {
3437 lov_dump_comp_v1_header(param, path, flags);
3439 flags |= LDF_INDENT;
3441 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3442 entry = &comp_v1->lcm_entries[i];
3444 if (param->fp_check_comp_flags) {
3445 if (((param->fp_comp_flags & entry->lcme_flags) !=
3446 param->fp_comp_flags) ||
3447 (param->fp_comp_neg_flags & entry->lcme_flags))
3451 if (param->fp_check_comp_id &&
3452 param->fp_comp_id != entry->lcme_id)
3455 if (param->fp_check_comp_start) {
3456 match = find_value_cmp(entry->lcme_extent.e_start,
3457 param->fp_comp_start,
3458 param->fp_comp_start_sign,
3460 param->fp_comp_start_units, 0);
3465 if (param->fp_check_comp_end) {
3466 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3472 if (param->fp_check_mirror_index) {
3473 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3475 mirror_id = mirror_id_of(entry->lcme_id);
3478 match = find_value_cmp(mirror_index,
3479 param->fp_mirror_index,
3480 param->fp_mirror_index_sign,
3481 param->fp_exclude_mirror_index,
3485 } else if (param->fp_check_mirror_id) {
3486 if (mirror_id != mirror_id_of(entry->lcme_id))
3487 mirror_id = mirror_id_of(entry->lcme_id);
3489 match = find_value_cmp(mirror_id,
3490 param->fp_mirror_id,
3491 param->fp_mirror_id_sign,
3492 param->fp_exclude_mirror_id,
3498 if (print_last_init_comp(param)) {
3500 * if part of stripe info is needed, we'd print only
3501 * the last instantiated component info.
3503 if (entry->lcme_flags & LCME_FL_INIT)
3506 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3507 if (entry->lcme_flags & LCME_FL_EXTENSION)
3508 /* moved back below */
3516 if (entry->lcme_flags & LCME_FL_INIT) {
3517 if (obdindex != OBD_NOT_FOUND) {
3518 flags |= LDF_SKIP_OBJS;
3519 v1 = lov_comp_entry(comp_v1, i);
3520 objects = lov_v1v3_objects(v1);
3522 for (j = 0; j < v1->lmm_stripe_count; j++) {
3523 if (obdindex == objects[j].l_ost_idx) {
3524 flags &= ~LDF_SKIP_OBJS;
3529 flags &= ~LDF_SKIP_OBJS;
3532 flags |= LDF_SKIP_OBJS;
3535 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3537 lov_dump_comp_v1_entry(param, flags, i);
3539 v1 = lov_comp_entry(comp_v1, i);
3540 objects = lov_v1v3_objects(v1);
3541 lov_v1v3_pool_name(v1, pool_name);
3543 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3544 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3545 param->fp_max_depth, param->fp_verbose,
3548 if (print_last_init_comp(param)) {
3550 * directory layout contains only layout template, print the
3554 i = comp_v1->lcm_entry_count - 1;
3557 flags &= ~LDF_SKIP_OBJS;
3559 lov_dump_comp_v1_entry(param, flags, i);
3561 v1 = lov_comp_entry(comp_v1, i);
3562 objects = lov_v1v3_objects(v1);
3563 lov_v1v3_pool_name(v1, pool_name);
3565 entry = &comp_v1->lcm_entries[i];
3566 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3567 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3568 param->fp_max_depth, param->fp_verbose,
3573 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3574 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3577 static inline bool has_any_comp_options(struct find_param *param)
3579 enum llapi_layout_verbose verbose = param->fp_verbose;
3581 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3582 param->fp_check_comp_start || param->fp_check_comp_end ||
3583 param->fp_check_comp_flags)
3586 /* show full layout information, not component specific */
3587 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3590 return verbose & VERBOSE_COMP_OPTS;
3593 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3596 struct lov_user_md *lum = &orig->lmd_lmm;
3597 struct lov_user_mds_data *new;
3598 struct lov_comp_md_v1 *comp_v1;
3599 struct lov_comp_md_entry_v1 *ent;
3600 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3601 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3604 new = malloc(sizeof(lstat_t) + lum_off + lum_size);
3606 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3610 memcpy(new, orig, sizeof(lstat_t));
3612 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3613 comp_v1->lcm_magic = lum->lmm_magic;
3614 comp_v1->lcm_size = lum_off + lum_size;
3615 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3616 comp_v1->lcm_flags = 0;
3617 comp_v1->lcm_entry_count = 1;
3619 ent = &comp_v1->lcm_entries[0];
3621 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3622 ent->lcme_extent.e_start = 0;
3623 ent->lcme_extent.e_end = LUSTRE_EOF;
3624 ent->lcme_offset = lum_off;
3625 ent->lcme_size = lum_size;
3627 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3632 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3633 enum lov_dump_flags flags)
3635 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3637 if (has_any_comp_options(param)) {
3638 struct lov_user_mds_data *new_lmd, *orig_lmd;
3640 orig_lmd = param->fp_lmd;
3641 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3642 if (new_lmd != NULL) {
3643 param->fp_lmd = new_lmd;
3644 lov_dump_comp_v1(param, path, flags);
3645 param->fp_lmd = orig_lmd;
3651 if (magic == LOV_USER_MAGIC_V1) {
3652 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3653 param->fp_lmd->lmd_lmm.lmm_objects,
3654 path, param->fp_obd_index,
3655 param->fp_max_depth, param->fp_verbose,
3658 char pool_name[LOV_MAXPOOLNAME + 1];
3659 struct lov_user_ost_data_v1 *objects;
3660 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3662 snprintf(pool_name, sizeof(pool_name), "%s",
3663 lmmv3->lmm_pool_name);
3664 objects = lmmv3->lmm_objects;
3665 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3666 objects, path, param->fp_obd_index,
3667 param->fp_max_depth, param->fp_verbose,
3672 static uint32_t check_foreign_type(uint32_t foreign_type)
3676 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3677 if (lu_foreign_types[i].lft_name == NULL)
3679 if (foreign_type == lu_foreign_types[i].lft_type)
3683 return LU_FOREIGN_TYPE_UNKNOWN;
3686 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3687 enum lov_dump_flags flags)
3689 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3690 bool yaml = flags & LDF_YAML;
3692 if (!yaml && param->fp_depth && path)
3693 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3695 if (param->fp_verbose & VERBOSE_DETAIL) {
3696 uint32_t type = check_foreign_type(lfm->lfm_type);
3698 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3700 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3702 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3704 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3705 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3706 lu_foreign_types[type].lft_name);
3708 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3710 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3713 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3714 lfm->lfm_length, lfm->lfm_value);
3715 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3718 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3719 enum lov_dump_flags flags)
3721 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3722 bool yaml = flags & LDF_YAML;
3724 if (!yaml && param->fp_depth && path)
3725 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3727 if (param->fp_verbose & VERBOSE_DETAIL) {
3728 uint32_t type = check_foreign_type(lfm->lfm_type);
3730 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3732 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3734 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3736 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3737 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3738 lu_foreign_types[type].lft_name);
3740 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3742 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3745 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3746 lfm->lfm_length, lfm->lfm_value);
3747 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3750 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3751 enum lov_dump_flags flags)
3755 if (param->fp_get_lmv || param->fp_get_default_lmv)
3756 magic = (__u32)param->fp_lmv_md->lum_magic;
3758 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3761 flags |= LDF_IS_RAW;
3766 case LOV_USER_MAGIC_V1:
3767 case LOV_USER_MAGIC_V3:
3768 case LOV_USER_MAGIC_SPECIFIC:
3769 lov_dump_plain_user_lmm(param, path, flags);
3771 case LOV_USER_MAGIC_FOREIGN:
3772 lov_dump_foreign_lmm(param, path, flags);
3775 case LMV_USER_MAGIC: {
3776 char pool_name[LOV_MAXPOOLNAME + 1];
3777 struct lmv_user_md *lum;
3779 lum = (struct lmv_user_md *)param->fp_lmv_md;
3780 snprintf(pool_name, sizeof(pool_name), "%s",
3781 lum->lum_pool_name);
3782 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3783 param->fp_max_depth, param->fp_verbose,
3787 case LOV_USER_MAGIC_COMP_V1:
3788 lov_dump_comp_v1(param, path, flags);
3790 case LMV_MAGIC_FOREIGN:
3791 lmv_dump_foreign_lmm(param, path, flags);
3794 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
3795 "(expecting one of %#x %#x %#x %#x)\n",
3796 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3797 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3798 LMV_USER_MAGIC, LMV_MAGIC_V1);
3803 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3809 fname = strrchr(path, '/');
3811 /* It should be a file (or other non-directory) */
3812 if (fname == NULL) {
3813 dname = (char *)malloc(2);
3817 fname = (char *)path;
3819 dname = (char *)malloc(fname - path + 1);
3822 strncpy(dname, path, fname - path);
3823 dname[fname - path] = '\0';
3827 fd = open(dname, O_RDONLY | O_NONBLOCK);
3833 strcpy((char *)lum, fname);
3834 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3837 if (close(fd) == -1 && rc == 0)
3845 int llapi_file_lookup(int dirfd, const char *name)
3847 struct obd_ioctl_data data = { 0 };
3852 if (dirfd < 0 || name == NULL)
3855 data.ioc_version = OBD_IOCTL_VERSION;
3856 data.ioc_len = sizeof(data);
3857 data.ioc_inlbuf1 = (char *)name;
3858 data.ioc_inllen1 = strlen(name) + 1;
3860 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3862 llapi_error(LLAPI_MSG_ERROR, rc,
3863 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3868 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
3874 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
3875 * Return -1 or 1 if file timestamp does not or does match the given criteria
3876 * correspondingly. Return 0 if the MDS time is being checked and there are
3877 * attributes on OSTs and it is not yet clear if the timespamp matches.
3879 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
3880 * updated timestamps. */
3881 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
3886 /* Check if file is accepted. */
3887 if (param->fp_atime) {
3888 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
3889 param->fp_asign, param->fp_exclude_atime,
3890 param->fp_time_margin, mds);
3896 if (param->fp_mtime) {
3897 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
3898 param->fp_msign, param->fp_exclude_mtime,
3899 param->fp_time_margin, mds);
3903 /* If the previous check matches, but this one is not yet clear,
3904 * we should return 0 to do an RPC on OSTs. */
3909 if (param->fp_ctime) {
3910 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
3911 param->fp_csign, param->fp_exclude_ctime,
3912 param->fp_time_margin, mds);
3916 /* If the previous check matches, but this one is not yet clear,
3917 * we should return 0 to do an RPC on OSTs. */
3926 * Check whether the stripes matches the indexes user provided
3930 static int check_obd_match(struct find_param *param)
3932 struct lov_user_ost_data_v1 *objects;
3933 struct lov_comp_md_v1 *comp_v1 = NULL;
3934 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3935 lstat_t *st = ¶m->fp_lmd->lmd_st;
3936 int i, j, k, count = 1;
3938 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
3941 if (!S_ISREG(st->st_mode))
3944 /* exclude foreign */
3945 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
3946 return param->fp_exclude_obd;
3948 /* Only those files should be accepted, which have a
3949 * stripe on the specified OST. */
3950 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3951 comp_v1 = (struct lov_comp_md_v1 *)v1;
3952 count = comp_v1->lcm_entry_count;
3955 for (i = 0; i < count; i++) {
3957 v1 = lov_comp_entry(comp_v1, i);
3959 objects = lov_v1v3_objects(v1);
3961 for (j = 0; j < v1->lmm_stripe_count; j++) {
3962 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
3965 for (k = 0; k < param->fp_num_obds; k++) {
3966 if (param->fp_obd_indexes[k] ==
3967 objects[j].l_ost_idx)
3968 return !param->fp_exclude_obd;
3973 return param->fp_exclude_obd;
3976 static int check_mdt_match(struct find_param *param)
3980 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
3983 /* FIXME: For striped dir, we should get stripe information and check */
3984 for (i = 0; i < param->fp_num_mdts; i++) {
3985 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
3986 return !param->fp_exclude_mdt;
3989 if (param->fp_exclude_mdt)
3996 * Check whether the obd is active or not, if it is
3997 * not active, just print the object affected by this
4000 static int print_failed_tgt(struct find_param *param, char *path, int type)
4002 struct obd_statfs stat_buf;
4003 struct obd_uuid uuid_buf;
4006 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV)
4009 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4010 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4011 ret = llapi_obd_statfs(path, type,
4012 param->fp_obd_index, &stat_buf,
4015 llapi_printf(LLAPI_MSG_NORMAL,
4016 "obd_uuid: %s failed %s ",
4017 param->fp_obd_uuid->uuid,
4024 static int find_check_stripe_size(struct find_param *param)
4026 struct lov_comp_md_v1 *comp_v1 = NULL;
4027 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4028 __u32 stripe_size = 0;
4029 int ret, i, count = 1;
4031 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4032 return param->fp_exclude_stripe_size ? 1 : -1;
4034 ret = param->fp_exclude_stripe_size ? 1 : -1;
4035 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4036 comp_v1 = (struct lov_comp_md_v1 *)v1;
4037 count = comp_v1->lcm_entry_count;
4040 for (i = 0; i < count; i++) {
4041 struct lov_comp_md_entry_v1 *ent;
4044 v1 = lov_comp_entry(comp_v1, i);
4046 ent = &comp_v1->lcm_entries[i];
4047 if (ent->lcme_flags & LCME_FL_EXTENSION)
4049 if (!(ent->lcme_flags & LCME_FL_INIT))
4052 stripe_size = v1->lmm_stripe_size;
4055 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4056 param->fp_stripe_size_sign,
4057 param->fp_exclude_stripe_size,
4058 param->fp_stripe_size_units, 0);
4063 static int find_check_ext_size(struct find_param *param)
4065 struct lov_comp_md_v1 *comp_v1;
4066 struct lov_user_md_v1 *v1;
4069 ret = param->fp_exclude_ext_size ? 1 : -1;
4070 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4071 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4074 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4075 struct lov_comp_md_entry_v1 *ent;
4077 v1 = lov_comp_entry(comp_v1, i);
4079 ent = &comp_v1->lcm_entries[i];
4080 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4083 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4084 param->fp_ext_size_sign,
4085 param->fp_exclude_ext_size,
4086 param->fp_ext_size_units, 0);
4087 /* If any ext_size matches */
4095 static __u32 find_get_stripe_count(struct find_param *param)
4097 struct lov_comp_md_v1 *comp_v1 = NULL;
4098 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4100 __u32 stripe_count = 0;
4102 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4105 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4106 comp_v1 = (struct lov_comp_md_v1 *)v1;
4107 count = comp_v1->lcm_entry_count;
4110 for (i = 0; i < count; i++) {
4112 struct lov_comp_md_entry_v1 *ent;
4114 v1 = lov_comp_entry(comp_v1, i);
4116 ent = &comp_v1->lcm_entries[i];
4117 if (!(ent->lcme_flags & LCME_FL_INIT))
4120 if (ent->lcme_flags & LCME_FL_EXTENSION)
4123 stripe_count = v1->lmm_stripe_count;
4126 return stripe_count;
4129 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4131 static int find_check_layout(struct find_param *param)
4133 struct lov_comp_md_v1 *comp_v1 = NULL;
4134 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4136 bool found = false, valid = false;
4138 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4139 comp_v1 = (struct lov_comp_md_v1 *)v1;
4140 count = comp_v1->lcm_entry_count;
4143 for (i = 0; i < count; i++) {
4145 v1 = lov_comp_entry(comp_v1, i);
4147 /* foreign file have a special magic but no pattern field */
4148 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4151 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4155 if (v1->lmm_pattern & param->fp_layout) {
4164 if ((found && !param->fp_exclude_layout) ||
4165 (!found && param->fp_exclude_layout))
4171 /* if no type specified, check/exclude all foreign
4172 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4174 static int find_check_foreign(struct find_param *param)
4176 if (S_ISREG(param->fp_lmd->lmd_st.st_mode)) {
4177 struct lov_foreign_md *lfm;
4179 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4180 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4181 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4182 return param->fp_exclude_foreign ? 1 : -1;
4185 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4186 lfm->lfm_type == param->fp_foreign_type)
4187 return param->fp_exclude_foreign ? -1 : 1;
4188 return param->fp_exclude_foreign ? 1 : -1;
4192 if (S_ISDIR(param->fp_lmd->lmd_st.st_mode)) {
4193 struct lmv_foreign_md *lfm;
4195 lfm = (void *)param->fp_lmv_md;
4196 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
4197 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4198 return param->fp_exclude_foreign ? 1 : -1;
4201 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4202 lfm->lfm_type == param->fp_foreign_type)
4203 return param->fp_exclude_foreign ? -1 : 1;
4204 return param->fp_exclude_foreign ? 1 : -1;
4210 static int find_check_pool(struct find_param *param)
4212 struct lov_comp_md_v1 *comp_v1 = NULL;
4213 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4214 struct lov_user_md_v3 *v3 = (void *)v1;
4218 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4219 comp_v1 = (struct lov_comp_md_v1 *)v1;
4220 count = comp_v1->lcm_entry_count;
4221 /* empty requested pool is taken as no pool search */
4222 if (count == 0 && param->fp_poolname[0] == '\0')
4226 for (i = 0; i < count; i++) {
4227 if (comp_v1 != NULL)
4228 v1 = lov_comp_entry(comp_v1, i);
4230 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4233 if (((v1->lmm_magic == LOV_USER_MAGIC_V1) &&
4234 (param->fp_poolname[0] == '\0')) ||
4235 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4236 (strncmp(v3->lmm_pool_name,
4237 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4238 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4239 (strcmp(param->fp_poolname, "*") == 0))) {
4245 if ((found && !param->fp_exclude_pool) ||
4246 (!found && param->fp_exclude_pool))
4252 static int find_check_comp_options(struct find_param *param)
4254 lstat_t *st = ¶m->fp_lmd->lmd_st;
4255 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4256 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4257 struct lov_comp_md_entry_v1 *entry;
4260 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4263 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4264 comp_v1 = (struct lov_comp_md_v1 *)v1;
4266 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4267 if (forged_v1 == NULL)
4269 comp_v1 = forged_v1;
4270 comp_v1->lcm_entry_count = 1;
4271 entry = &comp_v1->lcm_entries[0];
4272 entry->lcme_flags = S_ISDIR(st->st_mode) ? 0 : LCME_FL_INIT;
4273 entry->lcme_extent.e_start = 0;
4274 entry->lcme_extent.e_end = LUSTRE_EOF;
4277 /* invalid case, don't match for any kind of search. */
4278 if (comp_v1->lcm_entry_count == 0) {
4283 if (param->fp_check_comp_count) {
4284 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4285 param->fp_comp_count,
4286 param->fp_comp_count_sign,
4287 param->fp_exclude_comp_count, 1, 0);
4293 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4294 entry = &comp_v1->lcm_entries[i];
4296 if (param->fp_check_comp_flags) {
4298 if (((param->fp_comp_flags & entry->lcme_flags) !=
4299 param->fp_comp_flags) ||
4300 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4306 if (param->fp_check_comp_start) {
4307 ret = find_value_cmp(entry->lcme_extent.e_start,
4308 param->fp_comp_start,
4309 param->fp_comp_start_sign,
4310 param->fp_exclude_comp_start,
4311 param->fp_comp_start_units, 0);
4316 if (param->fp_check_comp_end) {
4317 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4323 /* the component matches all criteria */
4332 static int find_check_mirror_options(struct find_param *param)
4334 struct lov_comp_md_v1 *comp_v1;
4335 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4338 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4341 comp_v1 = (struct lov_comp_md_v1 *)v1;
4343 if (param->fp_check_mirror_count) {
4344 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4345 param->fp_mirror_count,
4346 param->fp_mirror_count_sign,
4347 param->fp_exclude_mirror_count, 1, 0);
4352 if (param->fp_check_mirror_state) {
4354 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4356 if ((param->fp_mirror_state != 0 &&
4357 file_state != param->fp_mirror_state) ||
4358 file_state == param->fp_mirror_neg_state)
4365 static bool find_check_lmm_info(struct find_param *param)
4367 return param->fp_check_pool || param->fp_check_stripe_count ||
4368 param->fp_check_stripe_size || param->fp_check_layout ||
4369 param->fp_check_comp_count || param->fp_check_comp_end ||
4370 param->fp_check_comp_start || param->fp_check_comp_flags ||
4371 param->fp_check_mirror_count || param->fp_check_foreign ||
4372 param->fp_check_mirror_state || param->fp_check_ext_size ||
4373 param->fp_check_projid;
4377 * Get file/directory project id.
4378 * by the open fd resides on.
4379 * Return 0 and project id on success, or -ve errno.
4381 static int fget_projid(int fd, int *projid)
4386 rc = ioctl(fd, LL_IOC_FSGETXATTR, &fsx);
4390 *projid = fsx.fsx_projid;
4394 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
4395 void *data, struct dirent64 *de)
4397 struct find_param *param = (struct find_param *)data;
4398 DIR *dir = dirp == NULL ? NULL : *dirp;
4399 int decision = 1; /* 1 is accepted; -1 is rejected. */
4400 lstat_t *st = ¶m->fp_lmd->lmd_st;
4402 int checked_type = 0;
4404 __u32 stripe_count = 0;
4407 if (parent == NULL && dir == NULL)
4410 /* If a regular expression is presented, make the initial decision */
4411 if (param->fp_pattern != NULL) {
4412 char *fname = strrchr(path, '/');
4413 fname = (fname == NULL ? path : fname + 1);
4414 ret = fnmatch(param->fp_pattern, fname, 0);
4415 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4416 (ret == 0 && param->fp_exclude_pattern))
4420 /* See if we can check the file type from the dirent. */
4421 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
4424 if (DTTOIF(de->d_type) == param->fp_type) {
4425 if (param->fp_exclude_type)
4428 if (!param->fp_exclude_type)
4435 /* Request MDS for the stat info if some of these parameters need
4436 * to be compared. */
4437 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4438 param->fp_check_uid || param->fp_check_gid ||
4439 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4440 param->fp_check_size || param->fp_check_blocks ||
4441 find_check_lmm_info(param) ||
4442 param->fp_check_mdt_count || param->fp_check_hash_type)
4445 if (param->fp_type != 0 && checked_type == 0)
4448 if (decision == 0) {
4449 if (dir && (param->fp_check_mdt_count ||
4450 param->fp_check_hash_type || param->fp_check_foreign)) {
4451 param->fp_get_lmv = 1;
4452 ret = cb_get_dirstripe(path, dir, param);
4454 /* XXX this works to decide for foreign
4457 if (errno == ENODATA &&
4458 param->fp_check_foreign) {
4459 if (param->fp_exclude_foreign)
4467 param->fp_lmd->lmd_lmm.lmm_magic = 0;
4468 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
4469 param->fp_lum_size, GET_LMD_INFO);
4470 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
4471 find_check_lmm_info(param)) {
4472 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
4474 /* We need to "fake" the "use the default" values
4475 * since the lmm struct is zeroed out at this point. */
4476 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4477 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
4479 ostid_set_seq(&lmm->lmm_oi,
4480 FID_SEQ_LOV_DEFAULT);
4481 lmm->lmm_stripe_size = 0;
4482 lmm->lmm_stripe_count = 0;
4483 lmm->lmm_stripe_offset = -1;
4485 if (ret == 0 && param->fp_mdt_uuid != NULL) {
4487 ret = llapi_file_fget_mdtidx(dirfd(dir),
4488 ¶m->fp_file_mdt_index);
4489 } else if (S_ISREG(st->st_mode)) {
4490 /* FIXME: we could get the MDT index from the
4491 * file's FID in lmd->lmd_lmm.lmm_oi without
4492 * opening the file, once we are sure that
4493 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
4494 * That would still be an ioctl() to map the
4495 * FID to the MDT, but not an open RPC. */
4496 fd = open(path, O_RDONLY);
4498 ret = llapi_file_fget_mdtidx(fd,
4499 ¶m->fp_file_mdt_index);
4504 /* For a special file, we assume it resides on
4505 * the same MDT as the parent directory. */
4506 ret = llapi_file_fget_mdtidx(dirfd(parent),
4507 ¶m->fp_file_mdt_index);
4518 stripe_count = find_get_stripe_count(param);
4522 if (param->fp_type && !checked_type) {
4523 if ((st->st_mode & S_IFMT) == param->fp_type) {
4524 if (param->fp_exclude_type)
4527 if (!param->fp_exclude_type)
4533 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
4534 if (lustre_fs && param->fp_got_uuids &&
4535 param->fp_dev != st->st_dev) {
4536 /* A lustre/lustre mount point is crossed. */
4537 param->fp_got_uuids = 0;
4538 param->fp_obds_printed = 0;
4539 param->fp_mdt_index = OBD_NOT_FOUND;
4540 param->fp_obd_index = OBD_NOT_FOUND;
4543 if (lustre_fs && !param->fp_got_uuids) {
4544 ret = setup_target_indexes(dir ? dir : parent, path,
4549 param->fp_dev = st->st_dev;
4550 } else if (!lustre_fs && param->fp_got_uuids) {
4551 /* A lustre/non-lustre mount point is crossed. */
4552 param->fp_got_uuids = 0;
4553 param->fp_mdt_index = OBD_NOT_FOUND;
4554 param->fp_obd_index = OBD_NOT_FOUND;
4558 if (param->fp_check_foreign) {
4559 decision = find_check_foreign(param);
4564 if (param->fp_check_stripe_size) {
4565 decision = find_check_stripe_size(param);
4570 if (param->fp_check_ext_size) {
4571 decision = find_check_ext_size(param);
4576 if (param->fp_check_stripe_count) {
4577 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
4578 param->fp_stripe_count_sign,
4579 param->fp_exclude_stripe_count, 1, 0);
4584 if (param->fp_check_mdt_count) {
4585 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4590 decision = find_value_cmp(
4591 param->fp_lmv_md->lum_stripe_count,
4592 param->fp_mdt_count,
4593 param->fp_mdt_count_sign,
4594 param->fp_exclude_mdt_count, 1, 0);
4599 if (param->fp_check_layout) {
4600 decision = find_check_layout(param);
4605 if (param->fp_check_hash_type) {
4608 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4613 found = param->fp_lmv_md->lum_hash_type & param->fp_hash_type;
4614 if ((found && param->fp_exclude_hash_type) ||
4615 (!found && !param->fp_exclude_hash_type)) {
4621 /* If an OBD UUID is specified but none matches, skip this file. */
4622 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
4623 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
4626 /* If an OST or MDT UUID is given, and some OST matches,
4628 if (param->fp_obd_index != OBD_NOT_FOUND ||
4629 param->fp_mdt_index != OBD_NOT_FOUND) {
4630 if (param->fp_obd_uuid) {
4631 if (check_obd_match(param)) {
4632 /* If no mdtuuid is given, we are done.
4633 * Otherwise, fall through to the mdtuuid
4635 if (!param->fp_mdt_uuid)
4642 if (param->fp_mdt_uuid) {
4643 if (check_mdt_match(param))
4650 if (param->fp_check_uid) {
4651 if (st->st_uid == param->fp_uid) {
4652 if (param->fp_exclude_uid)
4655 if (!param->fp_exclude_uid)
4660 if (param->fp_check_gid) {
4661 if (st->st_gid == param->fp_gid) {
4662 if (param->fp_exclude_gid)
4665 if (!param->fp_exclude_gid)
4670 if (param->fp_check_projid) {
4674 fd = open(path, O_RDONLY);
4677 ret = fget_projid(fd, &projid);
4682 if (projid == param->fp_projid) {
4683 if (param->fp_exclude_projid)
4686 if (!param->fp_exclude_projid)
4691 if (param->fp_check_pool) {
4692 decision = find_check_pool(param);
4697 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
4698 param->fp_check_comp_start || param->fp_check_comp_end) {
4699 decision = find_check_comp_options(param);
4704 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
4705 decision = find_check_mirror_options(param);
4710 /* Check the time on mds. */
4712 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
4715 for_mds = lustre_fs ?
4716 (S_ISREG(st->st_mode) && stripe_count) : 0;
4717 decision = find_time_check(st, param, for_mds);
4722 /* If file still fits the request, ask ost for updated info.
4723 The regular stat is almost of the same speed as some new
4724 'glimpse-size-ioctl'. */
4726 if ((param->fp_check_size || param->fp_check_blocks) &&
4727 ((S_ISREG(st->st_mode) && stripe_count) || S_ISDIR(st->st_mode)))
4731 /* For regular files with the stripe the decision may have not
4732 * been taken yet if *time or size is to be checked. */
4733 if (param->fp_obd_index != OBD_NOT_FOUND)
4734 print_failed_tgt(param, path, LL_STATFS_LOV);
4736 if (param->fp_mdt_index != OBD_NOT_FOUND)
4737 print_failed_tgt(param, path, LL_STATFS_LMV);
4740 ret = fstat_f(dirfd(dir), st);
4741 else if (de != NULL)
4742 ret = fstatat_f(dirfd(parent), de->d_name, st,
4743 AT_SYMLINK_NOFOLLOW);
4745 ret = lstat_f(path, st);
4748 if (errno == ENOENT) {
4749 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
4750 "warning: %s: %s does not exist",
4755 llapi_error(LLAPI_MSG_ERROR, ret,
4756 "%s: IOC_LOV_GETINFO on %s failed",
4762 /* Check the time on osc. */
4763 decision = find_time_check(st, param, 0);
4768 if (param->fp_check_size) {
4769 decision = find_value_cmp(st->st_size, param->fp_size,
4770 param->fp_size_sign,
4771 param->fp_exclude_size,
4772 param->fp_size_units, 0);
4777 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
4778 decision = find_value_cmp(st->st_blocks * 512, param->fp_blocks,
4779 param->fp_blocks_sign,
4780 param->fp_exclude_blocks,
4781 param->fp_blocks_units, 0);
4787 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
4788 if (param->fp_zero_end)
4789 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
4791 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4795 /* Do not get down anymore? */
4796 if (param->fp_depth == param->fp_max_depth) {
4807 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
4808 void *param_data, struct dirent64 *de)
4810 struct find_param *param = (struct find_param *)param_data;
4811 struct lmv_user_md *lmu = param->fp_lmv_md;
4812 DIR *tmp_parent = parent;
4813 char raw[MAX_IOC_BUFLEN] = {'\0'};
4815 struct obd_ioctl_data data = { 0 };
4822 if (parent == NULL && dirp == NULL)
4831 if (parent == NULL) {
4832 tmp_parent = opendir_parent(path);
4833 if (tmp_parent == NULL) {
4836 llapi_error(LLAPI_MSG_ERROR, ret,
4837 "can not open %s", path);
4842 fd = dirfd(tmp_parent);
4844 path_copy = strdup(path);
4845 filename = basename(path_copy);
4847 data.ioc_inlbuf1 = (char *)filename;
4848 data.ioc_inllen1 = strlen(filename) + 1;
4849 data.ioc_inlbuf2 = (char *)lmu;
4850 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
4852 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
4854 llapi_error(LLAPI_MSG_ERROR, ret,
4855 "llapi_obd_statfs: error packing ioctl data");
4860 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
4862 if (errno == EBUSY && !retry) {
4863 /* because migrate may not be able to lock all involved
4864 * objects in order, for some of them it try lock, while
4865 * there may be conflicting COS locks and cause migrate
4866 * fail with EBUSY, hope a sync() could cause
4867 * transaction commit and release these COS locks. */
4871 } else if (errno == EALREADY) {
4872 if (param->fp_verbose & VERBOSE_DETAIL)
4873 llapi_printf(LLAPI_MSG_NORMAL,
4874 "%s was migrated to MDT%d already\n",
4875 path, lmu->lum_stripe_offset);
4879 llapi_error(LLAPI_MSG_ERROR, ret,
4880 "%s migrate failed: %s (%d)\n",
4881 path, strerror(-ret), ret);
4884 } else if (param->fp_verbose & VERBOSE_DETAIL) {
4885 llapi_printf(LLAPI_MSG_NORMAL,
4886 "migrate %s to MDT%d stripe count %d\n",
4887 path, lmu->lum_stripe_offset,
4888 lmu->lum_stripe_count);
4893 /* If the directory is being migration, we need
4894 * close the directory after migration,
4895 * so the old directory cache will be cleanup
4896 * on the client side, and re-open to get the
4897 * new directory handle */
4898 *dirp = opendir(path);
4899 if (*dirp == NULL) {
4901 llapi_error(LLAPI_MSG_ERROR, ret,
4902 "%s: Failed to open '%s'", __func__, path);
4907 closedir(tmp_parent);
4914 /* dir migration finished, shrink its stripes */
4915 static int cb_migrate_mdt_fini(char *path, DIR *parent, DIR **dirp, void *data,
4916 struct dirent64 *de)
4918 struct find_param *param = data;
4919 struct lmv_user_md *lmu = param->fp_lmv_md;
4920 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
4923 if (de && de->d_type != DT_DIR)
4928 * close it before setxattr because the latter may destroy the
4929 * original object, and cause close fail.
4931 ret = closedir(*dirp);
4937 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
4938 if (ret == -EALREADY)
4941 cb_common_fini(path, parent, dirp, data, de);
4945 int llapi_migrate_mdt(char *path, struct find_param *param)
4947 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
4951 int llapi_mv(char *path, struct find_param *param)
4953 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
4954 static bool printed;
4957 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
4958 "llapi_mv() is deprecated, use llapi_migrate_mdt()\n");
4962 return llapi_migrate_mdt(path, param);
4965 int llapi_find(char *path, struct find_param *param)
4967 return param_callback(path, cb_find_init, cb_common_fini, param);
4971 * Get MDT number that the file/directory inode referenced
4972 * by the open fd resides on.
4973 * Return 0 and mdtidx on success, or -ve errno.
4975 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
4977 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
4982 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
4983 struct dirent64 *de)
4985 struct find_param *param = (struct find_param *)data;
4986 DIR *d = dirp == NULL ? NULL : *dirp;
4990 if (parent == NULL && d == NULL)
4994 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
4995 } else /* if (parent) */ {
4998 fd = open(path, O_RDONLY | O_NOCTTY);
5000 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5008 if (ret == -ENODATA) {
5009 if (!param->fp_obd_uuid)
5010 llapi_printf(LLAPI_MSG_NORMAL,
5011 "'%s' has no stripe info\n", path);
5013 } else if (ret == -ENOENT) {
5014 llapi_error(LLAPI_MSG_WARN, ret,
5015 "warning: %s: '%s' does not exist",
5018 } else if (ret == -ENOTTY) {
5019 llapi_error(LLAPI_MSG_ERROR, ret,
5020 "%s: '%s' not on a Lustre fs",
5023 llapi_error(LLAPI_MSG_ERROR, ret,
5024 "error: %s: '%s' failed get_mdtidx",
5030 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5031 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5033 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5037 /* Do not go down anymore? */
5038 if (param->fp_depth == param->fp_max_depth)
5046 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
5047 struct dirent64 *de)
5049 struct find_param *param = (struct find_param *)data;
5050 DIR *d = dirp == NULL ? NULL : *dirp;
5053 if (parent == NULL && d == NULL)
5056 if (param->fp_obd_uuid) {
5057 param->fp_quiet = 1;
5058 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
5063 if (d && (param->fp_get_lmv || param->fp_get_default_lmv))
5064 ret = cb_get_dirstripe(path, d, param);
5066 (parent && !param->fp_get_lmv && !param->fp_get_default_lmv))
5067 ret = get_lmd_info(path, parent, d, ¶m->fp_lmd->lmd_lmm,
5068 param->fp_lum_size, GET_LMD_STRIPE);
5073 if (errno == ENODATA && d != NULL) {
5074 /* We need to "fake" the "use the default" values
5075 * since the lmm struct is zeroed out at this point.
5076 * The magic needs to be set in order to satisfy
5077 * a check later on in the code path.
5078 * The object_seq needs to be set for the "(Default)"
5079 * prefix to be displayed. */
5080 if (param->fp_get_default_lmv) {
5081 struct lmv_user_md *lum = param->fp_lmv_md;
5083 lum->lum_magic = LMV_USER_MAGIC;
5084 lum->lum_stripe_count = 0;
5085 lum->lum_stripe_offset = -1;
5087 } else if (param->fp_get_lmv) {
5088 struct lmv_user_md *lum = param->fp_lmv_md;
5091 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5094 lum->lum_magic = LMV_MAGIC_V1;
5095 lum->lum_stripe_count = 0;
5096 lum->lum_stripe_offset = mdtidx;
5099 struct lov_user_md *lmm =
5100 ¶m->fp_lmd->lmd_lmm;
5102 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5104 ostid_set_seq(&lmm->lmm_oi,
5105 FID_SEQ_LOV_DEFAULT);
5106 lmm->lmm_stripe_count = 0;
5107 lmm->lmm_stripe_size = 0;
5108 lmm->lmm_stripe_offset = -1;
5111 } else if (errno == ENODATA && parent != NULL) {
5112 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5113 llapi_printf(LLAPI_MSG_NORMAL,
5114 "%s has no stripe info\n", path);
5116 } else if (errno == ENOENT) {
5117 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5118 "warning: %s: %s does not exist",
5121 } else if (errno == ENOTTY) {
5123 llapi_error(LLAPI_MSG_ERROR, ret,
5124 "%s: '%s' not on a Lustre fs?",
5129 llapi_error(LLAPI_MSG_ERROR, ret,
5130 "error: %s: %s failed for %s",
5131 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
5132 "IOC_MDC_GETFILESTRIPE", path);
5139 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5140 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
5143 /* Do not get down anymore? */
5144 if (param->fp_depth == param->fp_max_depth)
5152 int llapi_getstripe(char *path, struct find_param *param)
5154 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5155 cb_get_mdt_index : cb_getstripe,
5156 cb_common_fini, param);
5159 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5160 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5162 char raw[MAX_IOC_BUFLEN] = {'\0'};
5164 struct obd_ioctl_data data = { 0 };
5167 data.ioc_inlbuf1 = (char *)&type;
5168 data.ioc_inllen1 = sizeof(__u32);
5169 data.ioc_inlbuf2 = (char *)&index;
5170 data.ioc_inllen2 = sizeof(__u32);
5171 data.ioc_pbuf1 = (char *)stat_buf;
5172 data.ioc_plen1 = sizeof(struct obd_statfs);
5173 data.ioc_pbuf2 = (char *)uuid_buf;
5174 data.ioc_plen2 = sizeof(struct obd_uuid);
5176 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5178 llapi_error(LLAPI_MSG_ERROR, rc,
5179 "llapi_obd_statfs: error packing ioctl data");
5183 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5185 return rc < 0 ? -errno : 0;
5188 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5189 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5194 fd = open(path, O_RDONLY);
5197 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5199 /* If we can't even open a file on the filesystem (e.g. with
5200 * -ESHUTDOWN), force caller to exit or it will loop forever. */
5204 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5211 #define MAX_STRING_SIZE 128
5213 int llapi_ping(char *obd_type, char *obd_name)
5215 int flags = O_RDONLY;
5216 char buf[1] = { 0 };
5220 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5221 obd_type, obd_name);
5225 fd = open(path.gl_pathv[0], flags);
5227 if (errno == EACCES && flags == O_RDONLY) {
5232 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5237 if (flags == O_RDONLY)
5238 rc = read(fd, buf, sizeof(buf));
5240 rc = write(fd, buf, sizeof(buf));
5248 cfs_free_param_data(&path);
5252 int llapi_target_iterate(int type_num, char **obd_type,
5253 void *args, llapi_cb_t cb)
5259 for (i = 0; i < type_num; i++) {
5262 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5266 for (j = 0; j < param.gl_pathc; j++) {
5267 char obd_uuid[UUID_MAX + 1];
5271 fp = fopen(param.gl_pathv[j], "r");
5274 llapi_error(LLAPI_MSG_ERROR, rc,
5275 "error: opening '%s'",
5280 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5282 llapi_error(LLAPI_MSG_ERROR, rc,
5283 "error: reading '%s'",
5288 /* Extract the obd_name from the sysfs path.
5289 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5291 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5297 /* skip /fs/lustre/'obd_type'/ */
5298 obd_name += strlen(obd_type[i]) + 12;
5299 /* chop off after obd_name */
5300 ptr = strrchr(obd_name, '/');
5304 cb(obd_type[i], obd_name, obd_uuid, args);
5313 cfs_free_param_data(¶m);
5317 static void do_target_check(char *obd_type_name, char *obd_name,
5318 char *obd_uuid, void *args)
5322 rc = llapi_ping(obd_type_name, obd_name);
5323 if (rc == ENOTCONN) {
5324 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5326 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5328 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5332 int llapi_target_check(int type_num, char **obd_type, char *dir)
5334 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5337 #undef MAX_STRING_SIZE
5339 /* Is this a lustre fs? */
5340 int llapi_is_lustre_mnttype(const char *type)
5342 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
5345 /* Is this a lustre client fs? */
5346 int llapi_is_lustre_mnt(struct mntent *mnt)
5348 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5349 strstr(mnt->mnt_fsname, ":/") != NULL);
5352 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5354 char fsname[PATH_MAX + 1];
5358 rc = llapi_search_fsname(mnt, fsname);
5362 root = open(mnt, O_RDONLY | O_DIRECTORY);
5365 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
5369 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
5377 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
5378 * format must have %s%s, buf must be > 16
5379 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
5380 * then buf = "lustre-MDT0000"
5382 static int get_mdtname(char *name, char *format, char *buf)
5384 char suffix[]="-MDT0000";
5385 int len = strlen(name);
5387 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
5388 name[len - 5] = '\0';
5393 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
5396 /* Not enough room to add suffix */
5397 llapi_err_noerrno(LLAPI_MSG_ERROR,
5398 "Invalid MDT name |%s|", name);
5403 return sprintf(buf, format, name, suffix);
5406 /** ioctl on filsystem root, with mdtindex sent as data
5407 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
5408 * \param mdtidxp pointer to integer within data to be filled in with the
5409 * mdt index (0 if no mdt is specified). NULL won't be filled.
5411 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
5419 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
5420 Open root and parse mdt index. */
5421 if (mdtname[0] == '/') {
5423 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
5424 (char *)mdtname, -1);
5426 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
5428 ptr = fsname + strlen(fsname) - 8;
5430 index = strtol(ptr + 4, NULL, 16);
5431 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
5435 llapi_err_noerrno(LLAPI_MSG_ERROR,
5436 "Can't open %s: %d\n", mdtname, rc);
5443 rc = ioctl(fd, opc, data);
5452 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
5453 int buflen, long long *recno, int *linkno)
5455 const char *fidstr_orig = fidstr;
5457 struct getinfo_fid2path *gf;
5462 while (*fidstr == '[')
5465 sscanf(fidstr, SFID, RFID(&fid));
5466 if (!fid_is_sane(&fid)) {
5467 llapi_err_noerrno(LLAPI_MSG_ERROR,
5468 "bad FID format '%s', should be [seq:oid:ver]"
5469 " (e.g. "DFID")\n", fidstr_orig,
5470 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
5474 gf = malloc(sizeof(*gf) + buflen);
5479 gf->gf_recno = *recno;
5480 gf->gf_linkno = *linkno;
5481 gf->gf_pathlen = buflen;
5483 /* Take path or fsname */
5484 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
5489 /* strip out instances of // */
5490 for (a = gf->gf_u.gf_path; *a != '\0'; a++) {
5491 if ((*a == '/') && (*(a + 1) == '/'))
5498 if (buf[0] == '\0') { /* ROOT path */
5503 *recno = gf->gf_recno;
5504 *linkno = gf->gf_linkno;
5511 static int fid_from_lma(const char *path, int fd, struct lu_fid *fid)
5514 struct lustre_mdt_attrs *lma;
5518 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
5520 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
5523 lma = (struct lustre_mdt_attrs *)buf;
5524 fid_le_to_cpu(fid, &lma->lma_self_fid);
5528 int llapi_get_mdt_index_by_fid(int fd, const struct lu_fid *fid,
5533 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
5542 int llapi_fd2fid(int fd, struct lu_fid *fid)
5546 memset(fid, 0, sizeof(*fid));
5548 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
5549 if (rc == -EINVAL || rc == -ENOTTY)
5550 rc = fid_from_lma(NULL, fd, fid);
5555 int llapi_path2fid(const char *path, struct lu_fid *fid)
5559 memset(fid, 0, sizeof(*fid));
5560 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5562 if (errno == ELOOP || errno == ENXIO)
5563 return fid_from_lma(path, -1, fid);
5567 rc = llapi_fd2fid(fd, fid);
5568 if (rc == -EINVAL || rc == -ENOTTY)
5569 rc = fid_from_lma(path, -1, fid);
5575 int llapi_fd2parent(int fd, unsigned int linkno, struct lu_fid *parent_fid,
5576 char *name, size_t name_size)
5578 struct getparent *gp;
5581 gp = malloc(sizeof(*gp) + name_size);
5585 gp->gp_linkno = linkno;
5586 gp->gp_name_size = name_size;
5588 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
5594 *parent_fid = gp->gp_fid;
5596 strncpy(name, gp->gp_name, name_size);
5597 name[name_size - 1] = '\0';
5604 int llapi_path2parent(const char *path, unsigned int linkno,
5605 struct lu_fid *parent_fid, char *name, size_t name_size)
5610 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5614 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
5619 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
5624 root = open(mnt, O_RDONLY | O_DIRECTORY);
5627 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
5631 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
5634 llapi_error(LLAPI_MSG_ERROR, rc,
5635 "ioctl on %s for getting connect flags failed", mnt);
5642 * Get a 64-bit value representing the version of file data pointed by fd.
5644 * Each write or truncate, flushed on OST, will change this value. You can use
5645 * this value to verify if file data was modified. This only checks the file
5646 * data, not metadata.
5648 * \param flags 0: no flush pages, usually used it the process has already
5650 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
5652 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
5653 * pages from clients.
5655 * \retval 0 on success.
5656 * \retval -errno on error.
5658 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
5661 struct ioc_data_version idv;
5663 idv.idv_flags = (__u32)flags;
5665 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5669 *data_version = idv.idv_version;
5675 * Flush cached pages from all clients.
5677 * \param fd File descriptor
5681 int llapi_file_flush(int fd)
5685 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);
5689 * Fetch layout version from OST objects. Layout version on OST objects are
5690 * only set when the file is a mirrored file AND after the file has been
5691 * written at least once.
5693 * It actually fetches the least layout version from the objects.
5695 int llapi_get_ost_layout_version(int fd, __u32 *layout_version)
5698 struct ioc_data_version idv = { 0 };
5700 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5704 *layout_version = idv.idv_layout_version;
5710 * Create a file without any name and open it for read/write
5712 * - file is created as if it were a standard file in the given \a directory
5713 * - file does not appear in \a directory and mtime does not change because
5714 * the filename is handled specially by the Lustre MDS.
5715 * - file is destroyed at final close
5717 * \param[in] directory directory from which to inherit layout/MDT idx
5718 * \param[in] mdt_idx MDT index on which the file is created,
5719 * \a idx == -1 means no specific MDT is requested
5720 * \param[in] mode standard open(2) mode
5721 * \param[in] stripe_param stripe parameters. May be NULL.
5723 * \retval a file descriptor on success.
5724 * \retval -errno on error.
5726 int llapi_create_volatile_param(const char *directory, int mdt_idx,
5727 int open_flags, mode_t mode,
5728 const struct llapi_stripe_param *stripe_param)
5730 char file_path[PATH_MAX];
5731 int saved_errno = errno;
5733 unsigned int rnumber;
5739 rc = snprintf(file_path, sizeof(file_path),
5740 "%s/" LUSTRE_VOLATILE_HDR "::%.4X",
5741 directory, rnumber);
5743 rc = snprintf(file_path, sizeof(file_path),
5744 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X",
5745 directory, mdt_idx, rnumber);
5747 if (rc < 0 || rc >= sizeof(file_path))
5748 return -ENAMETOOLONG;
5751 * Either open O_WRONLY or O_RDWR, creating RDONLY
5752 * is non-sensical here
5754 if ((open_flags & O_ACCMODE) == O_RDONLY)
5755 open_flags = O_RDWR | (open_flags & ~O_ACCMODE);
5757 open_flags |= O_CREAT | O_EXCL | O_NOFOLLOW;
5759 if (stripe_param != NULL) {
5760 fd = llapi_file_open_param(file_path, open_flags,
5761 mode, stripe_param);
5765 fd = open(file_path, open_flags, mode);
5769 } while (fd < 0 && rc == -EEXIST);
5772 llapi_error(LLAPI_MSG_ERROR, rc,
5773 "Cannot create volatile file '%s' in '%s'",
5774 file_path + strlen(directory) + 1 +
5775 LUSTRE_VOLATILE_HDR_LEN,
5781 * Unlink file in case this wasn't a Lustre filesystem and the magic
5782 * volatile filename wasn't handled as intended. The effect is the
5783 * same. If volatile open was supported then we expect unlink() to
5786 (void)unlink(file_path);
5789 * Since we are returning successfully we restore errno (and
5790 * mask out possible EEXIST from open() and ENOENT from unlink().
5792 errno = saved_errno;
5798 * Create a file without any name open it for read/write
5800 * - file is created as if it were a standard file in the given \a directory
5801 * - file does not appear in \a directory and mtime does not change because
5802 * the filename is handled specially by the Lustre MDS.
5803 * - file is removed at final close
5804 * - file modes are rw------- since it doesn't make sense to have a read-only
5805 * or write-only file that cannot be opened again.
5806 * - if user wants another mode it must use fchmod() on the open file, no
5807 * security problems arise because it cannot be opened by another process.
5809 * \param[in] directory directory from which to inherit layout/MDT idx
5810 * \param[in] idx MDT index on which the file is created,
5811 * \a idx == -1 means no specific MDT is requested
5812 * \param[in] open_flags standard open(2) flags
5814 * \retval a file descriptor on success.
5815 * \retval -errno on error.
5817 int llapi_create_volatile_idx(const char *directory, int mdt_idx,
5820 return llapi_create_volatile_param(directory, mdt_idx, open_flags,
5821 S_IRUSR | S_IWUSR, NULL);
5825 * Swap the layouts between 2 file descriptors
5826 * the 2 files must be open for writing
5827 * first fd received the ioctl, second fd is passed as arg
5828 * this is assymetric but avoid use of root path for ioctl
5830 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
5831 int gid, __u64 flags)
5833 struct lustre_swap_layouts lsl;
5838 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5839 rc = fstat(fd1, &st1);
5843 rc = fstat(fd2, &st2);
5848 lsl.sl_flags = flags;
5852 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
5856 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5857 struct timeval tv1[2];
5858 struct timeval tv2[2];
5860 memset(tv1, 0, sizeof(tv1));
5861 memset(tv2, 0, sizeof(tv2));
5863 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
5864 tv1[0].tv_sec = st1.st_atime;
5865 tv2[0].tv_sec = st2.st_atime;
5867 tv1[0].tv_sec = st2.st_atime;
5868 tv2[0].tv_sec = st1.st_atime;
5871 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
5872 tv1[1].tv_sec = st1.st_mtime;
5873 tv2[1].tv_sec = st2.st_mtime;
5875 tv1[1].tv_sec = st2.st_mtime;
5876 tv2[1].tv_sec = st1.st_mtime;
5879 rc = futimes(fd1, tv1);
5883 rc = futimes(fd2, tv2);
5891 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
5898 while (grp_id == 0);
5900 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
5908 * Swap the layouts between 2 files
5909 * the 2 files are open in write
5911 int llapi_swap_layouts(const char *path1, const char *path2,
5912 __u64 dv1, __u64 dv2, __u64 flags)
5916 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
5919 llapi_error(LLAPI_MSG_ERROR, rc,
5920 "error: cannot open '%s' for write", path1);
5924 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
5927 llapi_error(LLAPI_MSG_ERROR, rc,
5928 "error: cannot open '%s' for write", path2);
5932 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
5934 llapi_error(LLAPI_MSG_ERROR, rc,
5935 "error: cannot swap layout between '%s' and '%s'",
5946 * Attempt to open a file with Lustre file identifier \a fid
5947 * and return an open file descriptor.
5949 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
5950 * \param[in] fid Lustre file identifier of file to open
5951 * \param[in] flags open() flags
5953 * \retval non-negative file descriptor on successful open
5954 * \retval -1 if an error occurred
5956 int llapi_open_by_fid(const char *lustre_dir, const struct lu_fid *fid,
5959 char mntdir[PATH_MAX];
5960 char path[PATH_MAX + 64];
5963 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
5967 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
5968 return open(path, flags);
5974 * \param fd File to lock.
5975 * \param gid Group Identifier.
5977 * \retval 0 on success.
5978 * \retval -errno on failure.
5980 int llapi_group_lock(int fd, int gid)
5984 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
5987 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
5995 * \param fd File to unlock.
5996 * \param gid Group Identifier.
5998 * \retval 0 on success.
5999 * \retval -errno on failure.
6001 int llapi_group_unlock(int fd, int gid)
6005 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
6008 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");