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;
1264 int fsnamelen, mountlen;
1266 /* get the mount point */
1267 fp = setmntent(PROC_MOUNTS, "r");
1270 llapi_error(LLAPI_MSG_ERROR, rc,
1271 "cannot retrieve filesystem mount point");
1275 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1278 if (!llapi_is_lustre_mnt(&mnt))
1281 if ((want & WANT_INDEX) && (idx++ != index))
1284 mntlen = strlen(mnt.mnt_dir);
1285 ptr = strchr(mnt.mnt_fsname, '/');
1286 while (ptr && *ptr == '/')
1288 /* thanks to the call to llapi_is_lustre_mnt() above,
1289 * we are sure that mnt.mnt_fsname contains ":/",
1290 * so ptr should never be NULL */
1294 while (*ptr_end != '/' && *ptr_end != '\0')
1297 /* Check the fsname for a match, if given */
1298 mountlen = ptr_end - ptr;
1299 if (!(want & WANT_FSNAME) && fsname != NULL &&
1300 (fsnamelen = strlen(fsname)) > 0 &&
1301 (fsnamelen != mountlen ||
1302 (strncmp(ptr, fsname, mountlen) != 0)))
1305 /* If the path isn't set return the first one we find */
1306 if (path == NULL || strlen(path) == 0) {
1307 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1308 mntdir[sizeof(mntdir) - 1] = '\0';
1309 if ((want & WANT_FSNAME) && fsname != NULL) {
1310 strncpy(fsname, ptr, mountlen);
1311 fsname[mountlen] = '\0';
1315 /* Otherwise find the longest matching path */
1316 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1317 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1318 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1319 mntdir[sizeof(mntdir) - 1] = '\0';
1321 if ((want & WANT_FSNAME) && fsname != NULL) {
1322 strncpy(fsname, ptr, mountlen);
1323 fsname[mountlen] = '\0';
1332 if ((want & WANT_PATH) && path != NULL) {
1333 strncpy(path, mntdir, PATH_MAX);
1334 path[strlen(mntdir)] = '\0';
1336 if (want & WANT_FD) {
1337 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1340 llapi_error(LLAPI_MSG_ERROR, rc,
1341 "cannot open '%s': %s", mntdir,
1348 } else if (want & WANT_ERROR)
1349 llapi_err_noerrno(LLAPI_MSG_ERROR,
1350 "'%s' not on a mounted Lustre filesystem",
1351 (want & WANT_PATH) ? fsname : path);
1356 * search lustre mounts
1358 * Calling this function will return to the user the mount point, mntdir, and
1359 * the file system name, fsname, if the user passed a buffer to this routine.
1361 * The user inputs are pathname and index. If the pathname is supplied then
1362 * the value of the index will be ignored. The pathname will return data if
1363 * the pathname is located on a lustre mount. Index is used to pick which
1364 * mount point you want in the case of multiple mounted lustre file systems.
1365 * See function lfs_osts in lfs.c for an example of the index use.
1367 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1370 int want = WANT_PATH, idx = -1;
1372 if (!pathname || pathname[0] == '\0') {
1376 strcpy(mntdir, pathname);
1379 want |= WANT_FSNAME;
1380 return get_root_path(want, fsname, NULL, mntdir, idx);
1383 /* Given a path, find the corresponding Lustre fsname */
1384 int llapi_search_fsname(const char *pathname, char *fsname)
1389 path = realpath(pathname, NULL);
1391 char tmp[PATH_MAX - 1];
1397 if (pathname[0] != '/') {
1398 /* Need an absolute path, but realpath() only works for
1399 * pathnames that actually exist. We go through the
1400 * extra hurdle of dirname(getcwd() + pathname) in
1401 * case the relative pathname contains ".." in it. */
1402 char realpath[PATH_MAX - 1];
1404 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1406 llapi_error(LLAPI_MSG_ERROR, rc,
1407 "cannot get current working directory");
1411 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1412 if (rc >= sizeof(tmp)) {
1414 llapi_error(LLAPI_MSG_ERROR, rc,
1415 "invalid parent path '%s'",
1421 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1422 if (rc >= sizeof(buf)) {
1424 llapi_error(LLAPI_MSG_ERROR, rc,
1425 "invalid path '%s'", pathname);
1428 path = realpath(buf, NULL);
1430 ptr = strrchr(buf, '/');
1432 llapi_error(LLAPI_MSG_ERROR |
1433 LLAPI_MSG_NO_ERRNO, 0,
1434 "cannot resolve path '%s'",
1439 path = realpath(buf, NULL);
1442 llapi_error(LLAPI_MSG_ERROR, rc,
1443 "cannot resolve path '%s'",
1449 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1454 int llapi_search_rootpath(char *pathname, const char *fsname)
1456 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1459 int llapi_getname(const char *path, char *buf, size_t size)
1461 struct obd_uuid uuid_buf;
1462 char *uuid = uuid_buf.uuid;
1464 int rc, len, fsname_len;
1466 memset(&uuid_buf, 0, sizeof(uuid_buf));
1467 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1472 * We want to turn testfs-clilov-ffff88002738bc00 into
1473 * testfs-ffff88002738bc00 in a portable way that doesn't depend
1474 * on what is after "-clilov-" as it may change in the future.
1475 * Unfortunately, the "fsname" part may contain a dash, so we
1476 * can't just skip to the first dash, and the "instance" may be a
1477 * UUID in the future, so we can't necessarily go to the last dash.
1479 cfg_instance = strstr(uuid, "-clilov-");
1483 fsname_len = cfg_instance - uuid;
1484 cfg_instance += strlen("-clilov-");
1485 len = snprintf(buf, size, "%.*s-%s", fsname_len, uuid, cfg_instance);
1494 * Get the list of pool members.
1495 * \param poolname string of format \<fsname\>.\<poolname\>
1496 * \param members caller-allocated array of char*
1497 * \param list_size size of the members array
1498 * \param buffer caller-allocated buffer for storing OST names
1499 * \param buffer_size size of the buffer
1501 * \return number of members retrieved for this pool
1502 * \retval -error failure
1504 int llapi_get_poolmembers(const char *poolname, char **members,
1505 int list_size, char *buffer, int buffer_size)
1507 char fsname[PATH_MAX];
1516 /* name is FSNAME.POOLNAME */
1517 if (strlen(poolname) >= sizeof(fsname))
1520 snprintf(fsname, sizeof(fsname), "%s", poolname);
1521 pool = strchr(fsname, '.');
1528 rc = poolpath(&pathname, fsname, NULL);
1530 llapi_error(LLAPI_MSG_ERROR, rc,
1531 "Lustre filesystem '%s' not found",
1536 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1537 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1538 cfs_free_param_data(&pathname);
1539 if (rc >= sizeof(buf))
1541 fd = fopen(buf, "r");
1544 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1549 while (fgets(buf, sizeof(buf), fd) != NULL) {
1550 if (nb_entries >= list_size) {
1554 buf[sizeof(buf) - 1] = '\0';
1556 tmp = strchr(buf, '\n');
1559 if (used + strlen(buf) + 1 > buffer_size) {
1564 strcpy(buffer + used, buf);
1565 members[nb_entries] = buffer + used;
1566 used += strlen(buf) + 1;
1576 * Get the list of pools in a filesystem.
1577 * \param name filesystem name or path
1578 * \param poollist caller-allocated array of char*
1579 * \param list_size size of the poollist array
1580 * \param buffer caller-allocated buffer for storing pool names
1581 * \param buffer_size size of the buffer
1583 * \return number of pools retrieved for this filesystem
1584 * \retval -error failure
1586 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1587 char *buffer, int buffer_size)
1593 struct dirent *pool;
1595 unsigned int nb_entries = 0;
1596 unsigned int used = 0;
1599 /* initialize output array */
1600 for (i = 0; i < list_size; i++)
1603 /* is name a pathname ? */
1604 ptr = strchr(name, '/');
1606 char fsname_buf[MAXNAMLEN];
1608 /* We will need fsname for printing later */
1609 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1613 ptr = strrchr(fsname_buf, '-');
1617 fsname = strdup(fsname_buf);
1621 /* name is FSNAME */
1622 fsname = strdup(name);
1627 rc = poolpath(&pathname, fsname, NULL);
1629 llapi_error(LLAPI_MSG_ERROR, rc,
1630 "Lustre filesystem '%s' not found", name);
1634 dir = opendir(pathname.gl_pathv[0]);
1637 llapi_error(LLAPI_MSG_ERROR, rc,
1638 "Could not open pool list for '%s'",
1645 pool = readdir(dir);
1651 /* ignore . and .. */
1652 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1655 /* check output bounds */
1656 if (nb_entries >= list_size) {
1658 goto free_dir_no_msg;
1661 /* +2 for '.' and final '\0' */
1662 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1665 goto free_dir_no_msg;
1668 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1669 poollist[nb_entries] = buffer + used;
1670 used += strlen(pool->d_name) + strlen(fsname) + 2;
1676 llapi_error(LLAPI_MSG_ERROR, rc,
1677 "Error reading pool list for '%s'", name);
1679 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1684 cfs_free_param_data(&pathname);
1687 return rc != 0 ? rc : nb_entries;
1690 /* wrapper for lfs.c and obd.c */
1691 int llapi_poollist(const char *name)
1693 /* list of pool names (assume that pool count is smaller
1695 char **list, *buffer = NULL, *fsname = (char *)name;
1696 char *poolname = NULL, *tmp = NULL, data[16];
1697 enum param_filter type = FILTER_BY_PATH;
1698 int obdcount, bufsize, rc, nb, i;
1703 if (name[0] != '/') {
1704 fsname = strdup(name);
1708 poolname = strchr(fsname, '.');
1711 type = FILTER_BY_FS_NAME;
1714 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1715 data, sizeof(data));
1718 obdcount = atoi(data);
1720 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1721 * and also an array to store the pointers for all that
1722 * allocated space. */
1724 bufsize = sizeof(struct obd_uuid) * obdcount;
1725 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1726 if (buffer == NULL) {
1730 list = (char **) (buffer + bufsize);
1733 /* name is a path or fsname */
1734 nb = llapi_get_poollist(name, list, obdcount,
1737 /* name is a pool name (<fsname>.<poolname>) */
1738 nb = llapi_get_poolmembers(name, list, obdcount,
1742 if (nb == -EOVERFLOW) {
1745 goto retry_get_pools;
1748 for (i = 0; i < nb; i++)
1749 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1750 rc = (nb < 0 ? nb : 0);
1754 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1759 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1760 void *data, struct dirent64 *de);
1762 #define OBD_NOT_FOUND (-1)
1764 static void find_param_fini(struct find_param *param)
1766 if (param->fp_migrate)
1769 if (param->fp_obd_indexes) {
1770 free(param->fp_obd_indexes);
1771 param->fp_obd_indexes = NULL;
1774 if (param->fp_lmd) {
1775 free(param->fp_lmd);
1776 param->fp_lmd = NULL;
1779 if (param->fp_lmv_md) {
1780 free(param->fp_lmv_md);
1781 param->fp_lmv_md = NULL;
1785 static int common_param_init(struct find_param *param, char *path)
1787 int lum_size = get_mds_md_size(path);
1792 /* migrate has fp_lmv_md initialized outside */
1793 if (param->fp_migrate)
1796 if (lum_size < PATH_MAX + 1)
1797 lum_size = PATH_MAX + 1;
1799 param->fp_lum_size = lum_size;
1800 param->fp_lmd = calloc(1, sizeof(lstat_t) + lum_size);
1801 if (param->fp_lmd == NULL) {
1802 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1803 "error: allocation of %zu bytes for ioctl",
1804 sizeof(lstat_t) + param->fp_lum_size);
1808 param->fp_lmv_stripe_count = 256;
1809 param->fp_lmv_md = calloc(1,
1810 lmv_user_md_size(param->fp_lmv_stripe_count,
1811 LMV_USER_MAGIC_SPECIFIC));
1812 if (param->fp_lmv_md == NULL) {
1813 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1814 "error: allocation of %d bytes for ioctl",
1815 lmv_user_md_size(param->fp_lmv_stripe_count,
1816 LMV_USER_MAGIC_SPECIFIC));
1817 find_param_fini(param);
1821 param->fp_got_uuids = 0;
1822 param->fp_obd_indexes = NULL;
1823 param->fp_obd_index = OBD_NOT_FOUND;
1824 param->fp_mdt_index = OBD_NOT_FOUND;
1828 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1829 struct dirent64 *de)
1831 struct find_param *param = data;
1837 /* set errno upon failure */
1838 static DIR *opendir_parent(const char *path)
1844 path_copy = strdup(path);
1845 if (path_copy == NULL)
1848 parent_path = dirname(path_copy);
1849 parent = opendir(parent_path);
1855 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1860 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1861 if (param->fp_get_default_lmv)
1862 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1864 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1866 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1867 if (errno == E2BIG && ret != 0) {
1871 /* if foreign LMV case, fake stripes number */
1872 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
1873 struct lmv_foreign_md *lfm;
1875 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1876 if (lfm->lfm_length < XATTR_SIZE_MAX -
1877 offsetof(typeof(*lfm), lfm_value)) {
1878 uint32_t size = lfm->lfm_length +
1879 offsetof(typeof(*lfm), lfm_value);
1881 stripe_count = lmv_foreign_to_md_stripes(size);
1883 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1884 "error: invalid %d foreign size "
1885 "returned from ioctl",
1890 stripe_count = param->fp_lmv_md->lum_stripe_count;
1892 if (stripe_count <= param->fp_lmv_stripe_count)
1895 free(param->fp_lmv_md);
1896 param->fp_lmv_stripe_count = stripe_count;
1897 lmv_size = lmv_user_md_size(stripe_count,
1898 LMV_USER_MAGIC_SPECIFIC);
1899 param->fp_lmv_md = malloc(lmv_size);
1900 if (param->fp_lmv_md == NULL) {
1901 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1902 "error: allocation of %d bytes for ioctl",
1903 lmv_user_md_size(param->fp_lmv_stripe_count,
1904 LMV_USER_MAGIC_SPECIFIC));
1912 int get_lmd_info_fd(char *path, int parent_fd, int dir_fd,
1913 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1915 struct lov_user_mds_data *lmd = lmdbuf;
1916 lstat_t *st = &lmd->lmd_st;
1919 if (parent_fd < 0 && dir_fd < 0)
1921 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1925 /* LL_IOC_MDC_GETINFO operates on the current directory inode
1926 * and returns struct lov_user_mds_data, while
1927 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
1929 ret = ioctl(dir_fd, type == GET_LMD_INFO ? LL_IOC_MDC_GETINFO :
1930 LL_IOC_LOV_GETSTRIPE,
1932 } else if (parent_fd >= 0) {
1933 char *fname = strrchr(path, '/');
1935 /* IOC_MDC_GETFILEINFO takes as input the filename (relative to
1936 * the parent directory) and returns struct lov_user_mds_data,
1937 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
1939 * This avoids opening, locking, and closing each file on the
1940 * client if that is not needed. Multiple of these ioctl() can
1941 * be done on the parent dir with a single open for all
1942 * files in that directory, and it also doesn't pollute the
1943 * client dcache with millions of dentries when traversing
1944 * a large filesystem.
1946 fname = (fname == NULL ? path : fname + 1);
1948 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
1951 else if (ret >= lmdlen || ret++ == 0)
1954 ret = ioctl(parent_fd, type == GET_LMD_INFO ?
1955 IOC_MDC_GETFILEINFO :
1956 IOC_MDC_GETFILESTRIPE, lmdbuf);
1959 if (ret && type == GET_LMD_INFO) {
1960 if (errno == ENOTTY) {
1961 /* ioctl is not supported, it is not a lustre fs.
1962 * Do the regular lstat(2) instead.
1964 ret = lstat_f(path, st);
1967 llapi_error(LLAPI_MSG_ERROR, ret,
1968 "error: %s: lstat failed for %s",
1971 } else if (errno == ENOENT) {
1973 llapi_error(LLAPI_MSG_WARN, ret,
1974 "warning: %s does not exist", path);
1975 } else if (errno != EISDIR && errno != ENODATA) {
1977 llapi_error(LLAPI_MSG_ERROR, ret,
1978 "%s ioctl failed for %s.",
1979 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
1980 "IOC_MDC_GETFILEINFO", path);
1987 static int get_lmd_info(char *path, DIR *parent, DIR *dir, void *lmdbuf,
1988 int lmdlen, enum get_lmd_info_type type)
1994 parent_fd = dirfd(parent);
1996 dir_fd = dirfd(dir);
1998 return get_lmd_info_fd(path, parent_fd, dir_fd, lmdbuf, lmdlen, type);
2001 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
2002 semantic_func_t sem_init,
2003 semantic_func_t sem_fini, void *data,
2004 struct dirent64 *de)
2006 struct find_param *param = (struct find_param *)data;
2007 struct dirent64 *dent;
2015 if (!d && errno != ENOTDIR) {
2017 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2020 } else if (!d && !parent) {
2021 /* ENOTDIR. Open the parent dir. */
2022 p = opendir_parent(path);
2029 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
2035 while ((dent = readdir64(d)) != NULL) {
2038 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2042 if ((len + dent->d_reclen + 2) > size) {
2043 llapi_err_noerrno(LLAPI_MSG_ERROR,
2044 "error: %s: string buffer too small",
2049 strcat(path, dent->d_name);
2051 if (dent->d_type == DT_UNKNOWN) {
2052 lstat_t *st = ¶m->fp_lmd->lmd_st;
2054 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
2055 param->fp_lum_size, GET_LMD_INFO);
2057 dent->d_type = IFTODT(st->st_mode);
2064 switch (dent->d_type) {
2066 llapi_err_noerrno(LLAPI_MSG_ERROR,
2067 "error: %s: '%s' is UNKNOWN type %d",
2068 __func__, dent->d_name, dent->d_type);
2071 rc = llapi_semantic_traverse(path, size, d, sem_init,
2072 sem_fini, data, dent);
2073 if (rc != 0 && ret == 0)
2079 rc = sem_init(path, d, NULL, data, dent);
2080 if (rc < 0 && ret == 0) {
2085 if (sem_fini && rc == 0)
2086 sem_fini(path, d, NULL, data, dent);
2094 sem_fini(path, parent, &d, data, de);
2103 static int param_callback(char *path, semantic_func_t sem_init,
2104 semantic_func_t sem_fini, struct find_param *param)
2106 int ret, len = strlen(path);
2109 if (len > PATH_MAX) {
2111 llapi_error(LLAPI_MSG_ERROR, ret,
2112 "Path name '%s' is too long", path);
2116 buf = (char *)malloc(PATH_MAX + 1);
2120 snprintf(buf, PATH_MAX + 1, "%s", path);
2121 ret = common_param_init(param, buf);
2125 param->fp_depth = 0;
2127 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
2128 sem_fini, param, NULL);
2130 find_param_fini(param);
2132 return ret < 0 ? ret : 0;
2135 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2137 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
2141 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2146 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2148 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2151 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2156 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2160 fd = open(path, O_RDONLY | O_NONBLOCK);
2163 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
2167 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2173 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2177 fd = open(path, O_RDONLY | O_NONBLOCK);
2180 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2184 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2196 * If uuidp is NULL, return the number of available obd uuids.
2197 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2198 * there are more OSTs than allocated to uuidp, then an error is returned with
2199 * the ost_count set to number of available obd uuids.
2201 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2202 int *ost_count, enum tgt_type type)
2204 char buf[PATH_MAX], format[32];
2205 int rc = 0, index = 0;
2206 struct obd_uuid name;
2210 /* Get the lov name */
2211 if (type == LOV_TYPE)
2212 rc = llapi_file_fget_lov_uuid(fd, &name);
2214 rc = llapi_file_fget_lmv_uuid(fd, &name);
2218 /* Now get the ost uuids */
2219 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2220 FILTER_BY_EXACT, "target_obd", ¶m);
2224 fp = fopen(param.gl_pathv[0], "r");
2227 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2232 snprintf(format, sizeof(format),
2233 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2234 while (fgets(buf, sizeof(buf), fp) != NULL) {
2235 if (uuidp && (index < *ost_count)) {
2236 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2244 if (uuidp && (index > *ost_count))
2249 cfs_free_param_data(¶m);
2253 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2255 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2258 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2263 root = open(mnt, O_RDONLY | O_DIRECTORY);
2266 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2271 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2279 /* Check if user specified value matches a real uuid. Ignore _UUID,
2280 * -osc-4ba41334, other trailing gunk in comparison.
2281 * @param real_uuid ends in "_UUID"
2282 * @param search_uuid may or may not end in "_UUID"
2284 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2286 int cmplen = strlen(real_uuid);
2287 int searchlen = strlen(search_uuid);
2289 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2291 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2294 /* The UUIDs may legitimately be different lengths, if
2295 * the system was upgraded from an older version. */
2296 if (cmplen != searchlen)
2299 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2302 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2303 * returned in param->fp_obd_index */
2304 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2306 struct obd_uuid obd_uuid;
2313 if (param->fp_got_uuids)
2316 /* Get the lov/lmv name */
2317 if (param->fp_get_lmv)
2318 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2320 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2322 if (rc != -ENOTTY) {
2323 llapi_error(LLAPI_MSG_ERROR, rc,
2324 "error: can't get %s name: %s",
2325 param->fp_get_lmv ? "lmv" : "lov",
2333 param->fp_got_uuids = 1;
2335 /* Now get the ost uuids */
2336 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2337 obd_uuid.uuid, FILTER_BY_EXACT,
2338 "target_obd", ¶m_data);
2342 fp = fopen(param_data.gl_pathv[0], "r");
2345 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2346 param_data.gl_pathv[0]);
2350 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2351 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2352 param->fp_get_lmv ? "MDTS" : "OBDS");
2354 snprintf(format, sizeof(format),
2355 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2356 while (fgets(buf, sizeof(buf), fp) != NULL) {
2359 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2362 if (param->fp_obd_uuid) {
2363 if (llapi_uuid_match(obd_uuid.uuid,
2364 param->fp_obd_uuid->uuid)) {
2365 param->fp_obd_index = index;
2368 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2369 /* Print everything */
2370 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2373 param->fp_obds_printed = 1;
2377 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2378 llapi_err_noerrno(LLAPI_MSG_ERROR,
2379 "error: %s: unknown obduuid: %s",
2380 __func__, param->fp_obd_uuid->uuid);
2384 cfs_free_param_data(¶m_data);
2388 /* In this case, param->fp_obd_uuid will be an array of obduuids and
2389 * obd index for all these obduuids will be returned in
2390 * param->fp_obd_indexes */
2391 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2392 int num_obds, int **obdindexes, int *obdindex,
2395 int ret, obdcount, obd_valid = 0, obdnum;
2397 struct obd_uuid *uuids = NULL;
2401 if (type == LOV_TYPE)
2402 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2404 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2408 obdcount = atoi(buf);
2409 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2414 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2416 if (ret == -EOVERFLOW) {
2417 struct obd_uuid *uuids_temp;
2419 uuids_temp = realloc(uuids, obdcount *
2420 sizeof(struct obd_uuid));
2421 if (uuids_temp != NULL) {
2423 goto retry_get_uuids;
2428 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2432 indexes = malloc(num_obds * sizeof(*obdindex));
2433 if (indexes == NULL) {
2438 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2441 /* The user may have specified a simple index */
2442 i = strtol(obduuids[obdnum].uuid, &end, 0);
2443 if (end && *end == '\0' && i < obdcount) {
2444 indexes[obdnum] = i;
2447 for (i = 0; i < obdcount; i++) {
2448 if (llapi_uuid_match(uuids[i].uuid,
2449 obduuids[obdnum].uuid)) {
2450 indexes[obdnum] = i;
2456 if (i >= obdcount) {
2457 indexes[obdnum] = OBD_NOT_FOUND;
2458 llapi_err_noerrno(LLAPI_MSG_ERROR,
2459 "invalid obduuid '%s'",
2460 obduuids[obdnum].uuid);
2466 *obdindex = OBD_NOT_FOUND;
2468 *obdindex = obd_valid;
2470 *obdindexes = indexes;
2478 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2482 if (param->fp_mdt_uuid) {
2483 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2485 ¶m->fp_mdt_indexes,
2486 ¶m->fp_mdt_index, LMV_TYPE);
2491 if (param->fp_obd_uuid) {
2492 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2494 ¶m->fp_obd_indexes,
2495 ¶m->fp_obd_index, LOV_TYPE);
2500 param->fp_got_uuids = 1;
2505 int llapi_ostlist(char *path, struct find_param *param)
2510 fd = open(path, O_RDONLY | O_DIRECTORY);
2514 ret = setup_obd_uuid(fd, path, param);
2521 * Tries to determine the default stripe attributes for a given filesystem. The
2522 * filesystem to check should be specified by fsname, or will be determined
2525 static int sattr_get_defaults(const char *const fsname,
2526 unsigned int *scount,
2527 unsigned int *ssize,
2528 unsigned int *soffset)
2534 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2535 "stripecount", val, sizeof(val));
2538 *scount = atoi(val);
2542 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2543 "stripesize", val, sizeof(val));
2550 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2551 "stripeoffset", val, sizeof(val));
2554 *soffset = atoi(val);
2561 * Tries to gather the default stripe attributes for a given filesystem. If
2562 * the attributes can be determined, they are cached for easy retreival the
2563 * next time they are needed. Only a single filesystem's attributes are
2566 int sattr_cache_get_defaults(const char *const fsname,
2567 const char *const pathname, unsigned int *scount,
2568 unsigned int *ssize, unsigned int *soffset)
2571 char fsname[PATH_MAX + 1];
2572 unsigned int stripecount;
2573 unsigned int stripesize;
2574 unsigned int stripeoffset;
2580 char fsname_buf[PATH_MAX + 1];
2581 unsigned int tmp[3];
2583 if (fsname == NULL) {
2584 rc = llapi_search_fsname(pathname, fsname_buf);
2588 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2591 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2593 * Ensure all 3 sattrs (count, size, and offset) are
2594 * successfully retrieved and stored in tmp before writing to
2597 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2601 cache.stripecount = tmp[0];
2602 cache.stripesize = tmp[1];
2603 cache.stripeoffset = tmp[2];
2604 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2608 *scount = cache.stripecount;
2610 *ssize = cache.stripesize;
2612 *soffset = cache.stripeoffset;
2617 static char *layout2name(__u32 layout_pattern)
2619 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2621 else if (layout_pattern == LOV_PATTERN_MDT)
2623 else if (layout_pattern == LOV_PATTERN_RAID0)
2625 else if (layout_pattern ==
2626 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2627 return "raid0,overstriped";
2632 enum lov_dump_flags {
2633 LDF_IS_DIR = 0x0001,
2634 LDF_IS_RAW = 0x0002,
2635 LDF_INDENT = 0x0004,
2636 LDF_SKIP_OBJS = 0x0008,
2638 LDF_EXTENSION = 0x0020,
2641 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2642 struct lov_user_ost_data_v1 *objects,
2643 enum llapi_layout_verbose verbose,
2644 int depth, char *pool_name,
2645 enum lov_dump_flags flags)
2647 bool is_dir = flags & LDF_IS_DIR;
2648 bool is_raw = flags & LDF_IS_RAW;
2649 bool indent = flags & LDF_INDENT;
2650 bool yaml = flags & LDF_YAML;
2651 bool skip_objs = flags & LDF_SKIP_OBJS;
2652 bool extension = flags & LDF_EXTENSION;
2653 char *prefix = is_dir ? "" : "lmm_";
2654 char *separator = "";
2655 char *space = indent ? " " : "";
2658 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2659 lmm_oi_set_seq(&lum->lmm_oi, 0);
2660 if (!indent && (verbose & VERBOSE_DETAIL))
2661 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2664 if (!yaml && !indent && depth && path &&
2665 ((verbose != VERBOSE_OBJID) || !is_dir))
2666 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2668 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2669 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2670 space, prefix, lum->lmm_magic);
2671 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2673 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2674 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2676 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2678 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2683 if (verbose & ~VERBOSE_DFID)
2684 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2686 /* This needs a bit of hand-holding since old 1.x lmm_oi
2687 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2688 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2689 * a real FID. Ideally the 2.x code would have stored this
2690 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2691 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2692 * worked properly (especially since IGIF FIDs use mds_inum as
2693 * the FID SEQ), but unfortunately that didn't happen.
2695 * Print it to look like an IGIF FID, even though the fields
2696 * are reversed on disk, so that it makes sense to userspace.
2698 * Don't use ostid_id() and ostid_seq(), since they assume the
2699 * oi_fid fields are in the right order. This is why there are
2700 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2702 * For newer layout types hopefully this will be a real FID. */
2703 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2704 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2705 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2706 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2707 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2709 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2710 (unsigned long long)seq, oid, ver);
2712 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2713 (unsigned long long)seq, oid, ver);
2716 if (verbose & VERBOSE_STRIPE_COUNT) {
2717 if (verbose & ~VERBOSE_STRIPE_COUNT)
2718 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2721 if (!is_raw && lum->lmm_stripe_count == 0 &&
2722 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2723 unsigned int scount;
2724 rc = sattr_cache_get_defaults(NULL, path,
2728 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2731 llapi_error(LLAPI_MSG_ERROR, rc,
2732 "Cannot determine default"
2735 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2737 (__s16)lum->lmm_stripe_count);
2740 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2742 (__s16)lum->lmm_stripe_count);
2744 if (!yaml && is_dir)
2750 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
2751 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
2752 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2753 if (verbose & ~VERBOSE_EXT_SIZE && extension)
2754 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
2756 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
2757 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2759 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2761 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2764 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2766 llapi_error(LLAPI_MSG_ERROR, rc,
2767 "Cannot determine default"
2770 /* Extension size is in KiB */
2771 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2773 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
2774 (unsigned long long)lum->lmm_stripe_size);
2776 if (!yaml && is_dir)
2782 if ((verbose & VERBOSE_PATTERN)) {
2783 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2784 if (verbose & ~VERBOSE_PATTERN)
2785 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2787 if (lov_pattern_supported(lum->lmm_pattern))
2788 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2789 layout2name(lum->lmm_pattern));
2791 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2792 separator = (!yaml && is_dir) ? " " : "\n";
2795 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2796 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2797 if (verbose & ~VERBOSE_GENERATION)
2798 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2800 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2801 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2805 if (verbose & VERBOSE_STRIPE_OFFSET) {
2806 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2807 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2808 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2810 if (is_dir || skip_objs)
2811 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2812 lum->lmm_stripe_offset ==
2813 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2814 lum->lmm_stripe_offset);
2815 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
2816 llapi_printf(LLAPI_MSG_NORMAL, "0");
2818 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2819 objects[0].l_ost_idx);
2820 if (!yaml && is_dir)
2826 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2827 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2828 if (verbose & ~VERBOSE_POOL)
2829 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2831 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2832 if (!yaml && is_dir)
2838 if (strlen(separator) != 0)
2839 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2842 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2843 struct lov_user_ost_data_v1 *objects,
2844 char *path, int obdindex, int depth,
2845 enum llapi_layout_verbose verbose,
2846 enum lov_dump_flags flags)
2848 bool is_dir = flags & LDF_IS_DIR;
2849 bool indent = flags & LDF_INDENT;
2850 bool skip_objs = flags & LDF_SKIP_OBJS;
2851 bool yaml = flags & LDF_YAML;
2852 bool obdstripe = obdindex == OBD_NOT_FOUND;
2855 if (!obdstripe && !skip_objs) {
2856 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2857 if (obdindex == objects[i].l_ost_idx) {
2867 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2870 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
2871 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2872 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
2873 char *space = " - ";
2876 llapi_printf(LLAPI_MSG_NORMAL,
2877 "%6slmm_objects:\n", " ");
2879 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2881 llapi_printf(LLAPI_MSG_NORMAL,
2882 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2884 for (i = 0; i < lum->lmm_stripe_count; i++) {
2885 int idx = objects[i].l_ost_idx;
2886 long long oid = ostid_id(&objects[i].l_ost_oi);
2887 long long gr = ostid_seq(&objects[i].l_ost_oi);
2889 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2893 struct lu_fid fid = { 0 };
2895 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2896 llapi_printf(LLAPI_MSG_NORMAL,
2897 "%sl_ost_idx: %d\n", space, idx);
2898 llapi_printf(LLAPI_MSG_NORMAL,
2899 "%8sl_fid: "DFID_NOBRACE"\n",
2901 } else if (indent) {
2902 struct lu_fid fid = { 0 };
2904 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2905 llapi_printf(LLAPI_MSG_NORMAL,
2906 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
2907 space, i, idx, PFID(&fid));
2910 sprintf(fmt, "%s%s%s\n",
2911 "\t%6u\t%14llu\t%#13llx\t",
2912 (fid_seq_is_rsvd(gr) ||
2913 fid_seq_is_mdt0(gr)) ?
2914 "%14llu" : "%#14llx", "%s");
2915 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2917 obdindex == idx ? " *" : "");
2921 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2924 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2925 char *path, int obdindex, int depth,
2926 enum llapi_layout_verbose verbose,
2927 enum lov_dump_flags flags)
2929 struct lmv_user_mds_data *objects = lum->lum_objects;
2930 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2931 char *separator = "";
2932 bool yaml = flags & LDF_YAML;
2933 bool obdstripe = false;
2936 if (obdindex != OBD_NOT_FOUND) {
2937 if (lum->lum_stripe_count == 0) {
2938 if (obdindex == lum->lum_stripe_offset)
2941 for (i = 0; i < lum->lum_stripe_count; i++) {
2942 if (obdindex == objects[i].lum_mds) {
2943 llapi_printf(LLAPI_MSG_NORMAL,
2958 /* show all information default */
2960 if (lum->lum_magic == LMV_USER_MAGIC)
2961 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
2962 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
2964 verbose = VERBOSE_OBJID;
2967 if (depth && path && ((verbose != VERBOSE_OBJID)))
2968 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2970 if (verbose & VERBOSE_STRIPE_COUNT) {
2971 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2972 if (verbose & ~VERBOSE_STRIPE_COUNT)
2973 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2974 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2975 (int)lum->lum_stripe_count);
2976 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
2982 if (verbose & VERBOSE_STRIPE_OFFSET) {
2983 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2984 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2985 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2986 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2987 (int)lum->lum_stripe_offset);
2988 if (verbose & VERBOSE_HASH_TYPE && !yaml)
2994 if (verbose & VERBOSE_HASH_TYPE) {
2995 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
2996 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
2998 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2999 if (verbose & ~VERBOSE_HASH_TYPE)
3000 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3001 if (type < LMV_HASH_TYPE_MAX)
3002 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3003 mdt_hash_name[type]);
3005 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3007 if (flags & LMV_HASH_FLAG_SPACE)
3008 llapi_printf(LLAPI_MSG_NORMAL, ",space");
3009 if (flags & LMV_HASH_FLAG_MIGRATION)
3010 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3011 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3012 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3013 if (flags & LMV_HASH_FLAG_LOST_LMV)
3014 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3019 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3020 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3021 if (lum->lum_stripe_count > 0)
3022 llapi_printf(LLAPI_MSG_NORMAL,
3023 "mdtidx\t\t FID[seq:oid:ver]\n");
3024 for (i = 0; i < lum->lum_stripe_count; i++) {
3025 int idx = objects[i].lum_mds;
3026 struct lu_fid *fid = &objects[i].lum_fid;
3027 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3028 llapi_printf(LLAPI_MSG_NORMAL,
3029 "%6u\t\t "DFID"\t\t%s\n",
3031 obdindex == idx ? " *" : "");
3036 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3037 pool_name[0] != '\0') {
3038 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3039 if (verbose & ~VERBOSE_POOL)
3040 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3042 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3046 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3047 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3050 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3051 enum lov_dump_flags flags)
3053 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3054 int depth = param->fp_max_depth;
3055 enum llapi_layout_verbose verbose = param->fp_verbose;
3056 bool yaml = flags & LDF_YAML;
3058 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3059 !(flags & LDF_IS_DIR)) && !yaml)
3060 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3062 if (verbose & VERBOSE_DETAIL) {
3063 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3064 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3065 " ", comp_v1->lcm_magic);
3066 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3067 " ", comp_v1->lcm_size);
3068 if (flags & LDF_IS_DIR)
3069 llapi_printf(LLAPI_MSG_NORMAL,
3070 "%2slcm_flags: %s\n", " ",
3071 comp_v1->lcm_mirror_count > 0 ?
3074 llapi_printf(LLAPI_MSG_NORMAL,
3075 "%2slcm_flags: %s\n", " ",
3076 llapi_layout_flags_string(comp_v1->lcm_flags));
3079 if (verbose & VERBOSE_GENERATION) {
3080 if (verbose & ~VERBOSE_GENERATION)
3081 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3083 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3086 if (verbose & VERBOSE_MIRROR_COUNT) {
3087 if (verbose & ~VERBOSE_MIRROR_COUNT)
3088 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3090 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3091 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3092 comp_v1->lcm_mirror_count + 1 : 1);
3095 if (verbose & VERBOSE_COMP_COUNT) {
3096 if (verbose & ~VERBOSE_COMP_COUNT)
3097 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3099 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3100 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3101 comp_v1->lcm_entry_count : 0);
3104 if (verbose & VERBOSE_DETAIL && !yaml)
3105 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3108 static void lcme_flags2str(__u32 comp_flags)
3114 llapi_printf(LLAPI_MSG_NORMAL, "0");
3117 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3118 if (comp_flags & comp_flags_table[i].cfn_flag) {
3120 llapi_printf(LLAPI_MSG_NORMAL, ",");
3121 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3122 comp_flags_table[i].cfn_name);
3123 comp_flags &= ~comp_flags_table[i].cfn_flag;
3129 llapi_printf(LLAPI_MSG_NORMAL, ",");
3130 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3134 static void lov_dump_comp_v1_entry(struct find_param *param,
3135 enum lov_dump_flags flags, int index)
3137 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3138 struct lov_comp_md_entry_v1 *entry;
3139 char *separator = "";
3140 enum llapi_layout_verbose verbose = param->fp_verbose;
3141 bool yaml = flags & LDF_YAML;
3143 entry = &comp_v1->lcm_entries[index];
3146 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3148 if (verbose & VERBOSE_COMP_ID) {
3149 if (verbose & VERBOSE_DETAIL && !yaml)
3150 llapi_printf(LLAPI_MSG_NORMAL,
3151 "%slcme_id: ", " - ");
3152 else if (verbose & ~VERBOSE_COMP_ID)
3153 llapi_printf(LLAPI_MSG_NORMAL,
3154 "%4slcme_id: ", " ");
3155 if (entry->lcme_id != LCME_ID_INVAL)
3156 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3158 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3162 if (verbose & VERBOSE_MIRROR_ID) {
3163 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3164 if (verbose & ~VERBOSE_MIRROR_ID)
3165 llapi_printf(LLAPI_MSG_NORMAL,
3166 "%4slcme_mirror_id: ", " ");
3167 if (entry->lcme_id != LCME_ID_INVAL)
3168 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3169 mirror_id_of(entry->lcme_id));
3171 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3175 if (verbose & VERBOSE_COMP_FLAGS) {
3176 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3177 if (verbose & ~VERBOSE_COMP_FLAGS)
3178 llapi_printf(LLAPI_MSG_NORMAL,
3179 "%4slcme_flags: ", " ");
3180 lcme_flags2str(entry->lcme_flags);
3183 /* print snapshot timestamp if its a nosync comp */
3184 if ((verbose & VERBOSE_COMP_FLAGS) &&
3185 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3186 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3187 if (verbose & ~VERBOSE_COMP_FLAGS)
3188 llapi_printf(LLAPI_MSG_NORMAL,
3189 "%4slcme_timestamp: ", " ");
3191 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3192 (unsigned long long)entry->lcme_timestamp);
3194 time_t stamp = entry->lcme_timestamp;
3195 char *date_str = asctime(localtime(&stamp));
3197 date_str[strlen(date_str) - 1] = '\0';
3198 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3204 if (verbose & VERBOSE_COMP_START) {
3205 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3206 if (verbose & ~VERBOSE_COMP_START)
3207 llapi_printf(LLAPI_MSG_NORMAL,
3208 "%4slcme_extent.e_start: ", " ");
3209 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3210 (unsigned long long)entry->lcme_extent.e_start);
3214 if (verbose & VERBOSE_COMP_END) {
3215 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3216 if (verbose & ~VERBOSE_COMP_END)
3217 llapi_printf(LLAPI_MSG_NORMAL,
3218 "%4slcme_extent.e_end: ", " ");
3219 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3220 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3222 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3223 (unsigned long long)entry->lcme_extent.e_end);
3228 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3229 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3230 } else if (verbose & VERBOSE_DETAIL) {
3231 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3232 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3233 " ", entry->lcme_offset);
3234 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3235 " ", entry->lcme_size);
3236 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3238 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3242 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3243 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3245 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3246 * The table below gives the answers for the specified parameters (value and
3247 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3248 * --------------------------------------
3249 * 1 | file > limit; sign > 0 | -1 / -1 |
3250 * 2 | file = limit; sign > 0 | -1 / -1 |
3251 * 3 | file < limit; sign > 0 | ? / 1 |
3252 * 4 | file > limit; sign = 0 | -1 / -1 |
3253 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3254 * 6 | file < limit; sign = 0 | ? / -1 |
3255 * 7 | file > limit; sign < 0 | 1 / 1 |
3256 * 8 | file = limit; sign < 0 | ? / -1 |
3257 * 9 | file < limit; sign < 0 | ? / -1 |
3258 * --------------------------------------
3259 * Note: 5th actually means that the value is within the interval
3260 * (limit - margin, limit]. */
3261 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3262 int sign, int negopt, unsigned long long margin,
3268 /* Drop the fraction of margin (of days or size). */
3269 if (file + margin <= limit)
3271 } else if (sign == 0) {
3272 if (file <= limit && file + margin > limit)
3274 else if (file + margin <= limit)
3276 } else if (sign < 0) {
3283 return negopt ? ~ret + 1 : ret;
3286 static inline struct lov_user_md *
3287 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3289 return (struct lov_user_md *)((char *)comp_v1 +
3290 comp_v1->lcm_entries[ent_idx].lcme_offset);
3293 static inline struct lov_user_ost_data_v1 *
3294 lov_v1v3_objects(struct lov_user_md *v1)
3296 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3297 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3299 return v1->lmm_objects;
3303 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3305 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3306 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3307 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3309 pool_name[0] = '\0';
3313 print_last_init_comp(struct find_param *param)
3315 /* print all component info */
3316 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3319 /* print specific component info */
3320 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3321 param->fp_check_comp_start || param->fp_check_comp_end ||
3322 param->fp_check_mirror_id || param->fp_check_mirror_index)
3328 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3332 if (param->fp_comp_end == LUSTRE_EOF) {
3333 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3334 match = end == LUSTRE_EOF ? 1 : -1;
3335 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3336 match = end == LUSTRE_EOF ? -1 : 1;
3337 else /* at least EOF */
3339 if (param->fp_exclude_comp_end)
3342 unsigned long long margin;
3344 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3345 match = find_value_cmp(end, param->fp_comp_end,
3346 param->fp_comp_end_sign,
3347 param->fp_exclude_comp_end, margin, 0);
3354 * An example of "getstripe -v" for a two components PFL file:
3357 * lcm_magic: 0x0BD60BD0
3361 * lcm_entry_count: 2
3365 * lcme_extent.e_start: 0
3366 * lcme_extent.e_end: 1048576
3370 * lmm_magic: 0x0BD10BD0
3371 * lmm_seq: 0x200000401
3372 * lmm_object_id: 0x1
3373 * lmm_fid: [0x200000401:0x1:0x0]
3374 * lmm_stripe_count: 1
3375 * lmm_stripe_size: 1048576
3376 * lmm_pattern: raid0
3378 * lmm_stripe_offset: 0
3380 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3384 * lcme_extent.e_start: 1048576
3385 * lcme_extent.e_end: EOF
3389 * lmm_magic: 0x0BD10BD0
3390 * lmm_seq: 0x200000401
3391 * lmm_object_id: 0x1
3392 * lmm_fid: [0x200000401:0x1:0x0]
3393 * lmm_stripe_count: 2
3394 * lmm_stripe_size: 1048576
3395 * lmm_pattern: raid0
3397 * lmm_stripe_offset: 1
3399 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3400 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3402 static void lov_dump_comp_v1(struct find_param *param, char *path,
3403 enum lov_dump_flags flags)
3405 struct lov_comp_md_entry_v1 *entry;
3406 struct lov_user_ost_data_v1 *objects;
3407 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3408 struct lov_user_md_v1 *v1;
3409 char pool_name[LOV_MAXPOOLNAME + 1];
3410 int obdindex = param->fp_obd_index;
3411 int i, j, match, ext;
3412 bool obdstripe = false;
3413 __u16 mirror_index = 0;
3414 __u16 mirror_id = 0;
3416 if (obdindex != OBD_NOT_FOUND) {
3417 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3418 i < comp_v1->lcm_entry_count; i++) {
3419 if (!(comp_v1->lcm_entries[i].lcme_flags &
3423 v1 = lov_comp_entry(comp_v1, i);
3424 objects = lov_v1v3_objects(v1);
3426 for (j = 0; j < v1->lmm_stripe_count; j++) {
3427 if (obdindex == objects[j].l_ost_idx) {
3440 lov_dump_comp_v1_header(param, path, flags);
3442 flags |= LDF_INDENT;
3444 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3445 entry = &comp_v1->lcm_entries[i];
3447 if (param->fp_check_comp_flags) {
3448 if (((param->fp_comp_flags & entry->lcme_flags) !=
3449 param->fp_comp_flags) ||
3450 (param->fp_comp_neg_flags & entry->lcme_flags))
3454 if (param->fp_check_comp_id &&
3455 param->fp_comp_id != entry->lcme_id)
3458 if (param->fp_check_comp_start) {
3459 match = find_value_cmp(entry->lcme_extent.e_start,
3460 param->fp_comp_start,
3461 param->fp_comp_start_sign,
3463 param->fp_comp_start_units, 0);
3468 if (param->fp_check_comp_end) {
3469 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3475 if (param->fp_check_mirror_index) {
3476 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3478 mirror_id = mirror_id_of(entry->lcme_id);
3481 match = find_value_cmp(mirror_index,
3482 param->fp_mirror_index,
3483 param->fp_mirror_index_sign,
3484 param->fp_exclude_mirror_index,
3488 } else if (param->fp_check_mirror_id) {
3489 if (mirror_id != mirror_id_of(entry->lcme_id))
3490 mirror_id = mirror_id_of(entry->lcme_id);
3492 match = find_value_cmp(mirror_id,
3493 param->fp_mirror_id,
3494 param->fp_mirror_id_sign,
3495 param->fp_exclude_mirror_id,
3501 if (print_last_init_comp(param)) {
3503 * if part of stripe info is needed, we'd print only
3504 * the last instantiated component info.
3506 if (entry->lcme_flags & LCME_FL_INIT)
3509 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3510 if (entry->lcme_flags & LCME_FL_EXTENSION)
3511 /* moved back below */
3519 if (entry->lcme_flags & LCME_FL_INIT) {
3520 if (obdindex != OBD_NOT_FOUND) {
3521 flags |= LDF_SKIP_OBJS;
3522 v1 = lov_comp_entry(comp_v1, i);
3523 objects = lov_v1v3_objects(v1);
3525 for (j = 0; j < v1->lmm_stripe_count; j++) {
3526 if (obdindex == objects[j].l_ost_idx) {
3527 flags &= ~LDF_SKIP_OBJS;
3532 flags &= ~LDF_SKIP_OBJS;
3535 flags |= LDF_SKIP_OBJS;
3538 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3540 lov_dump_comp_v1_entry(param, flags, i);
3542 v1 = lov_comp_entry(comp_v1, i);
3543 objects = lov_v1v3_objects(v1);
3544 lov_v1v3_pool_name(v1, pool_name);
3546 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3547 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3548 param->fp_max_depth, param->fp_verbose,
3551 if (print_last_init_comp(param)) {
3553 * directory layout contains only layout template, print the
3557 i = comp_v1->lcm_entry_count - 1;
3560 flags &= ~LDF_SKIP_OBJS;
3562 lov_dump_comp_v1_entry(param, flags, i);
3564 v1 = lov_comp_entry(comp_v1, i);
3565 objects = lov_v1v3_objects(v1);
3566 lov_v1v3_pool_name(v1, pool_name);
3568 entry = &comp_v1->lcm_entries[i];
3569 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3570 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3571 param->fp_max_depth, param->fp_verbose,
3576 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3577 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3580 static inline bool has_any_comp_options(struct find_param *param)
3582 enum llapi_layout_verbose verbose = param->fp_verbose;
3584 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3585 param->fp_check_comp_start || param->fp_check_comp_end ||
3586 param->fp_check_comp_flags)
3589 /* show full layout information, not component specific */
3590 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3593 return verbose & VERBOSE_COMP_OPTS;
3596 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3599 struct lov_user_md *lum = &orig->lmd_lmm;
3600 struct lov_user_mds_data *new;
3601 struct lov_comp_md_v1 *comp_v1;
3602 struct lov_comp_md_entry_v1 *ent;
3603 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3604 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3607 new = malloc(sizeof(lstat_t) + lum_off + lum_size);
3609 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3613 memcpy(new, orig, sizeof(lstat_t));
3615 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3616 comp_v1->lcm_magic = lum->lmm_magic;
3617 comp_v1->lcm_size = lum_off + lum_size;
3618 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3619 comp_v1->lcm_flags = 0;
3620 comp_v1->lcm_entry_count = 1;
3622 ent = &comp_v1->lcm_entries[0];
3624 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3625 ent->lcme_extent.e_start = 0;
3626 ent->lcme_extent.e_end = LUSTRE_EOF;
3627 ent->lcme_offset = lum_off;
3628 ent->lcme_size = lum_size;
3630 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3635 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3636 enum lov_dump_flags flags)
3638 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3640 if (has_any_comp_options(param)) {
3641 struct lov_user_mds_data *new_lmd, *orig_lmd;
3643 orig_lmd = param->fp_lmd;
3644 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3645 if (new_lmd != NULL) {
3646 param->fp_lmd = new_lmd;
3647 lov_dump_comp_v1(param, path, flags);
3648 param->fp_lmd = orig_lmd;
3654 if (magic == LOV_USER_MAGIC_V1) {
3655 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3656 param->fp_lmd->lmd_lmm.lmm_objects,
3657 path, param->fp_obd_index,
3658 param->fp_max_depth, param->fp_verbose,
3661 char pool_name[LOV_MAXPOOLNAME + 1];
3662 struct lov_user_ost_data_v1 *objects;
3663 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3665 snprintf(pool_name, sizeof(pool_name), "%s",
3666 lmmv3->lmm_pool_name);
3667 objects = lmmv3->lmm_objects;
3668 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3669 objects, path, param->fp_obd_index,
3670 param->fp_max_depth, param->fp_verbose,
3675 static uint32_t check_foreign_type(uint32_t foreign_type)
3679 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3680 if (lu_foreign_types[i].lft_name == NULL)
3682 if (foreign_type == lu_foreign_types[i].lft_type)
3686 return LU_FOREIGN_TYPE_UNKNOWN;
3689 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3690 enum lov_dump_flags flags)
3692 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3693 bool yaml = flags & LDF_YAML;
3695 if (!yaml && param->fp_depth && path)
3696 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3698 if (param->fp_verbose & VERBOSE_DETAIL) {
3699 uint32_t type = check_foreign_type(lfm->lfm_type);
3701 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3703 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3705 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3707 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3708 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3709 lu_foreign_types[type].lft_name);
3711 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3713 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3716 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3717 lfm->lfm_length, lfm->lfm_value);
3718 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3721 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3722 enum lov_dump_flags flags)
3724 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3725 bool yaml = flags & LDF_YAML;
3727 if (!yaml && param->fp_depth && path)
3728 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3730 if (param->fp_verbose & VERBOSE_DETAIL) {
3731 uint32_t type = check_foreign_type(lfm->lfm_type);
3733 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3735 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3737 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3739 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3740 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3741 lu_foreign_types[type].lft_name);
3743 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3745 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3748 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3749 lfm->lfm_length, lfm->lfm_value);
3750 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3753 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3754 enum lov_dump_flags flags)
3758 if (param->fp_get_lmv || param->fp_get_default_lmv)
3759 magic = (__u32)param->fp_lmv_md->lum_magic;
3761 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3764 flags |= LDF_IS_RAW;
3769 case LOV_USER_MAGIC_V1:
3770 case LOV_USER_MAGIC_V3:
3771 case LOV_USER_MAGIC_SPECIFIC:
3772 lov_dump_plain_user_lmm(param, path, flags);
3774 case LOV_USER_MAGIC_FOREIGN:
3775 lov_dump_foreign_lmm(param, path, flags);
3778 case LMV_USER_MAGIC: {
3779 char pool_name[LOV_MAXPOOLNAME + 1];
3780 struct lmv_user_md *lum;
3782 lum = (struct lmv_user_md *)param->fp_lmv_md;
3783 snprintf(pool_name, sizeof(pool_name), "%s",
3784 lum->lum_pool_name);
3785 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3786 param->fp_max_depth, param->fp_verbose,
3790 case LOV_USER_MAGIC_COMP_V1:
3791 lov_dump_comp_v1(param, path, flags);
3793 case LMV_MAGIC_FOREIGN:
3794 lmv_dump_foreign_lmm(param, path, flags);
3797 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
3798 "(expecting one of %#x %#x %#x %#x)\n",
3799 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3800 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3801 LMV_USER_MAGIC, LMV_MAGIC_V1);
3806 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3812 fname = strrchr(path, '/');
3814 /* It should be a file (or other non-directory) */
3815 if (fname == NULL) {
3816 dname = (char *)malloc(2);
3820 fname = (char *)path;
3822 dname = (char *)malloc(fname - path + 1);
3825 strncpy(dname, path, fname - path);
3826 dname[fname - path] = '\0';
3830 fd = open(dname, O_RDONLY | O_NONBLOCK);
3836 strcpy((char *)lum, fname);
3837 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3840 if (close(fd) == -1 && rc == 0)
3848 int llapi_file_lookup(int dirfd, const char *name)
3850 struct obd_ioctl_data data = { 0 };
3855 if (dirfd < 0 || name == NULL)
3858 data.ioc_version = OBD_IOCTL_VERSION;
3859 data.ioc_len = sizeof(data);
3860 data.ioc_inlbuf1 = (char *)name;
3861 data.ioc_inllen1 = strlen(name) + 1;
3863 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3865 llapi_error(LLAPI_MSG_ERROR, rc,
3866 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3871 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
3877 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
3878 * Return -1 or 1 if file timestamp does not or does match the given criteria
3879 * correspondingly. Return 0 if the MDS time is being checked and there are
3880 * attributes on OSTs and it is not yet clear if the timespamp matches.
3882 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
3883 * updated timestamps. */
3884 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
3889 /* Check if file is accepted. */
3890 if (param->fp_atime) {
3891 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
3892 param->fp_asign, param->fp_exclude_atime,
3893 param->fp_time_margin, mds);
3899 if (param->fp_mtime) {
3900 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
3901 param->fp_msign, param->fp_exclude_mtime,
3902 param->fp_time_margin, mds);
3906 /* If the previous check matches, but this one is not yet clear,
3907 * we should return 0 to do an RPC on OSTs. */
3912 if (param->fp_ctime) {
3913 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
3914 param->fp_csign, param->fp_exclude_ctime,
3915 param->fp_time_margin, mds);
3919 /* If the previous check matches, but this one is not yet clear,
3920 * we should return 0 to do an RPC on OSTs. */
3929 * Check whether the stripes matches the indexes user provided
3933 static int check_obd_match(struct find_param *param)
3935 struct lov_user_ost_data_v1 *objects;
3936 struct lov_comp_md_v1 *comp_v1 = NULL;
3937 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3938 lstat_t *st = ¶m->fp_lmd->lmd_st;
3939 int i, j, k, count = 1;
3941 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
3944 if (!S_ISREG(st->st_mode))
3947 /* exclude foreign */
3948 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
3949 return param->fp_exclude_obd;
3951 /* Only those files should be accepted, which have a
3952 * stripe on the specified OST. */
3953 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3954 comp_v1 = (struct lov_comp_md_v1 *)v1;
3955 count = comp_v1->lcm_entry_count;
3958 for (i = 0; i < count; i++) {
3960 v1 = lov_comp_entry(comp_v1, i);
3962 objects = lov_v1v3_objects(v1);
3964 for (j = 0; j < v1->lmm_stripe_count; j++) {
3965 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
3968 for (k = 0; k < param->fp_num_obds; k++) {
3969 if (param->fp_obd_indexes[k] ==
3970 objects[j].l_ost_idx)
3971 return !param->fp_exclude_obd;
3976 return param->fp_exclude_obd;
3979 static int check_mdt_match(struct find_param *param)
3983 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
3986 /* FIXME: For striped dir, we should get stripe information and check */
3987 for (i = 0; i < param->fp_num_mdts; i++) {
3988 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
3989 return !param->fp_exclude_mdt;
3992 if (param->fp_exclude_mdt)
3999 * Check whether the obd is active or not, if it is
4000 * not active, just print the object affected by this
4003 static int print_failed_tgt(struct find_param *param, char *path, int type)
4005 struct obd_statfs stat_buf;
4006 struct obd_uuid uuid_buf;
4009 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV)
4012 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4013 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4014 ret = llapi_obd_statfs(path, type,
4015 param->fp_obd_index, &stat_buf,
4018 llapi_printf(LLAPI_MSG_NORMAL,
4019 "obd_uuid: %s failed %s ",
4020 param->fp_obd_uuid->uuid,
4027 static int find_check_stripe_size(struct find_param *param)
4029 struct lov_comp_md_v1 *comp_v1 = NULL;
4030 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4031 __u32 stripe_size = 0;
4032 int ret, i, count = 1;
4034 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4035 return param->fp_exclude_stripe_size ? 1 : -1;
4037 ret = param->fp_exclude_stripe_size ? 1 : -1;
4038 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4039 comp_v1 = (struct lov_comp_md_v1 *)v1;
4040 count = comp_v1->lcm_entry_count;
4043 for (i = 0; i < count; i++) {
4044 struct lov_comp_md_entry_v1 *ent;
4047 v1 = lov_comp_entry(comp_v1, i);
4049 ent = &comp_v1->lcm_entries[i];
4050 if (ent->lcme_flags & LCME_FL_EXTENSION)
4052 if (!(ent->lcme_flags & LCME_FL_INIT))
4055 stripe_size = v1->lmm_stripe_size;
4058 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4059 param->fp_stripe_size_sign,
4060 param->fp_exclude_stripe_size,
4061 param->fp_stripe_size_units, 0);
4066 static int find_check_ext_size(struct find_param *param)
4068 struct lov_comp_md_v1 *comp_v1;
4069 struct lov_user_md_v1 *v1;
4072 ret = param->fp_exclude_ext_size ? 1 : -1;
4073 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4074 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4077 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4078 struct lov_comp_md_entry_v1 *ent;
4080 v1 = lov_comp_entry(comp_v1, i);
4082 ent = &comp_v1->lcm_entries[i];
4083 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4086 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4087 param->fp_ext_size_sign,
4088 param->fp_exclude_ext_size,
4089 param->fp_ext_size_units, 0);
4090 /* If any ext_size matches */
4098 static __u32 find_get_stripe_count(struct find_param *param)
4100 struct lov_comp_md_v1 *comp_v1 = NULL;
4101 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4103 __u32 stripe_count = 0;
4105 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4108 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4109 comp_v1 = (struct lov_comp_md_v1 *)v1;
4110 count = comp_v1->lcm_entry_count;
4113 for (i = 0; i < count; i++) {
4115 struct lov_comp_md_entry_v1 *ent;
4117 v1 = lov_comp_entry(comp_v1, i);
4119 ent = &comp_v1->lcm_entries[i];
4120 if (!(ent->lcme_flags & LCME_FL_INIT))
4123 if (ent->lcme_flags & LCME_FL_EXTENSION)
4126 stripe_count = v1->lmm_stripe_count;
4129 return stripe_count;
4132 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4134 static int find_check_layout(struct find_param *param)
4136 struct lov_comp_md_v1 *comp_v1 = NULL;
4137 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4139 bool found = false, valid = false;
4141 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4142 comp_v1 = (struct lov_comp_md_v1 *)v1;
4143 count = comp_v1->lcm_entry_count;
4146 for (i = 0; i < count; i++) {
4148 v1 = lov_comp_entry(comp_v1, i);
4150 /* foreign file have a special magic but no pattern field */
4151 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4154 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4158 if (v1->lmm_pattern & param->fp_layout) {
4167 if ((found && !param->fp_exclude_layout) ||
4168 (!found && param->fp_exclude_layout))
4174 /* if no type specified, check/exclude all foreign
4175 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4177 static int find_check_foreign(struct find_param *param)
4179 if (S_ISREG(param->fp_lmd->lmd_st.st_mode)) {
4180 struct lov_foreign_md *lfm;
4182 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4183 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4184 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4185 return param->fp_exclude_foreign ? 1 : -1;
4188 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4189 lfm->lfm_type == param->fp_foreign_type)
4190 return param->fp_exclude_foreign ? -1 : 1;
4191 return param->fp_exclude_foreign ? 1 : -1;
4195 if (S_ISDIR(param->fp_lmd->lmd_st.st_mode)) {
4196 struct lmv_foreign_md *lfm;
4198 lfm = (void *)param->fp_lmv_md;
4199 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
4200 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4201 return param->fp_exclude_foreign ? 1 : -1;
4204 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4205 lfm->lfm_type == param->fp_foreign_type)
4206 return param->fp_exclude_foreign ? -1 : 1;
4207 return param->fp_exclude_foreign ? 1 : -1;
4213 static int find_check_pool(struct find_param *param)
4215 struct lov_comp_md_v1 *comp_v1 = NULL;
4216 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4217 struct lov_user_md_v3 *v3 = (void *)v1;
4221 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4222 comp_v1 = (struct lov_comp_md_v1 *)v1;
4223 count = comp_v1->lcm_entry_count;
4224 /* empty requested pool is taken as no pool search */
4225 if (count == 0 && param->fp_poolname[0] == '\0')
4229 for (i = 0; i < count; i++) {
4230 if (comp_v1 != NULL)
4231 v1 = lov_comp_entry(comp_v1, i);
4233 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4236 if (((v1->lmm_magic == LOV_USER_MAGIC_V1) &&
4237 (param->fp_poolname[0] == '\0')) ||
4238 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4239 (strncmp(v3->lmm_pool_name,
4240 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4241 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4242 (strcmp(param->fp_poolname, "*") == 0))) {
4248 if ((found && !param->fp_exclude_pool) ||
4249 (!found && param->fp_exclude_pool))
4255 static int find_check_comp_options(struct find_param *param)
4257 lstat_t *st = ¶m->fp_lmd->lmd_st;
4258 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4259 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4260 struct lov_comp_md_entry_v1 *entry;
4263 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4266 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4267 comp_v1 = (struct lov_comp_md_v1 *)v1;
4269 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4270 if (forged_v1 == NULL)
4272 comp_v1 = forged_v1;
4273 comp_v1->lcm_entry_count = 1;
4274 entry = &comp_v1->lcm_entries[0];
4275 entry->lcme_flags = S_ISDIR(st->st_mode) ? 0 : LCME_FL_INIT;
4276 entry->lcme_extent.e_start = 0;
4277 entry->lcme_extent.e_end = LUSTRE_EOF;
4280 /* invalid case, don't match for any kind of search. */
4281 if (comp_v1->lcm_entry_count == 0) {
4286 if (param->fp_check_comp_count) {
4287 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4288 param->fp_comp_count,
4289 param->fp_comp_count_sign,
4290 param->fp_exclude_comp_count, 1, 0);
4296 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4297 entry = &comp_v1->lcm_entries[i];
4299 if (param->fp_check_comp_flags) {
4301 if (((param->fp_comp_flags & entry->lcme_flags) !=
4302 param->fp_comp_flags) ||
4303 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4309 if (param->fp_check_comp_start) {
4310 ret = find_value_cmp(entry->lcme_extent.e_start,
4311 param->fp_comp_start,
4312 param->fp_comp_start_sign,
4313 param->fp_exclude_comp_start,
4314 param->fp_comp_start_units, 0);
4319 if (param->fp_check_comp_end) {
4320 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4326 /* the component matches all criteria */
4335 static int find_check_mirror_options(struct find_param *param)
4337 struct lov_comp_md_v1 *comp_v1;
4338 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4341 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4344 comp_v1 = (struct lov_comp_md_v1 *)v1;
4346 if (param->fp_check_mirror_count) {
4347 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4348 param->fp_mirror_count,
4349 param->fp_mirror_count_sign,
4350 param->fp_exclude_mirror_count, 1, 0);
4355 if (param->fp_check_mirror_state) {
4357 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4359 if ((param->fp_mirror_state != 0 &&
4360 file_state != param->fp_mirror_state) ||
4361 file_state == param->fp_mirror_neg_state)
4368 static bool find_check_lmm_info(struct find_param *param)
4370 return param->fp_check_pool || param->fp_check_stripe_count ||
4371 param->fp_check_stripe_size || param->fp_check_layout ||
4372 param->fp_check_comp_count || param->fp_check_comp_end ||
4373 param->fp_check_comp_start || param->fp_check_comp_flags ||
4374 param->fp_check_mirror_count || param->fp_check_foreign ||
4375 param->fp_check_mirror_state || param->fp_check_ext_size ||
4376 param->fp_check_projid;
4380 * Get file/directory project id.
4381 * by the open fd resides on.
4382 * Return 0 and project id on success, or -ve errno.
4384 static int fget_projid(int fd, int *projid)
4389 rc = ioctl(fd, LL_IOC_FSGETXATTR, &fsx);
4393 *projid = fsx.fsx_projid;
4397 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
4398 void *data, struct dirent64 *de)
4400 struct find_param *param = (struct find_param *)data;
4401 DIR *dir = dirp == NULL ? NULL : *dirp;
4402 int decision = 1; /* 1 is accepted; -1 is rejected. */
4403 lstat_t *st = ¶m->fp_lmd->lmd_st;
4405 int checked_type = 0;
4407 __u32 stripe_count = 0;
4410 if (parent == NULL && dir == NULL)
4413 /* If a regular expression is presented, make the initial decision */
4414 if (param->fp_pattern != NULL) {
4415 char *fname = strrchr(path, '/');
4416 fname = (fname == NULL ? path : fname + 1);
4417 ret = fnmatch(param->fp_pattern, fname, 0);
4418 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4419 (ret == 0 && param->fp_exclude_pattern))
4423 /* See if we can check the file type from the dirent. */
4424 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
4427 if (DTTOIF(de->d_type) == param->fp_type) {
4428 if (param->fp_exclude_type)
4431 if (!param->fp_exclude_type)
4438 /* Request MDS for the stat info if some of these parameters need
4439 * to be compared. */
4440 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4441 param->fp_check_uid || param->fp_check_gid ||
4442 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4443 param->fp_check_size || param->fp_check_blocks ||
4444 find_check_lmm_info(param) ||
4445 param->fp_check_mdt_count || param->fp_check_hash_type)
4448 if (param->fp_type != 0 && checked_type == 0)
4451 if (decision == 0) {
4452 if (dir && (param->fp_check_mdt_count ||
4453 param->fp_check_hash_type || param->fp_check_foreign)) {
4454 param->fp_get_lmv = 1;
4455 ret = cb_get_dirstripe(path, dir, param);
4457 /* XXX this works to decide for foreign
4460 if (errno == ENODATA &&
4461 param->fp_check_foreign) {
4462 if (param->fp_exclude_foreign)
4470 param->fp_lmd->lmd_lmm.lmm_magic = 0;
4471 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
4472 param->fp_lum_size, GET_LMD_INFO);
4473 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
4474 find_check_lmm_info(param)) {
4475 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
4477 /* We need to "fake" the "use the default" values
4478 * since the lmm struct is zeroed out at this point. */
4479 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4480 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
4482 ostid_set_seq(&lmm->lmm_oi,
4483 FID_SEQ_LOV_DEFAULT);
4484 lmm->lmm_stripe_size = 0;
4485 lmm->lmm_stripe_count = 0;
4486 lmm->lmm_stripe_offset = -1;
4488 if (ret == 0 && param->fp_mdt_uuid != NULL) {
4490 ret = llapi_file_fget_mdtidx(dirfd(dir),
4491 ¶m->fp_file_mdt_index);
4492 } else if (S_ISREG(st->st_mode)) {
4493 /* FIXME: we could get the MDT index from the
4494 * file's FID in lmd->lmd_lmm.lmm_oi without
4495 * opening the file, once we are sure that
4496 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
4497 * That would still be an ioctl() to map the
4498 * FID to the MDT, but not an open RPC. */
4499 fd = open(path, O_RDONLY);
4501 ret = llapi_file_fget_mdtidx(fd,
4502 ¶m->fp_file_mdt_index);
4507 /* For a special file, we assume it resides on
4508 * the same MDT as the parent directory. */
4509 ret = llapi_file_fget_mdtidx(dirfd(parent),
4510 ¶m->fp_file_mdt_index);
4521 stripe_count = find_get_stripe_count(param);
4525 if (param->fp_type && !checked_type) {
4526 if ((st->st_mode & S_IFMT) == param->fp_type) {
4527 if (param->fp_exclude_type)
4530 if (!param->fp_exclude_type)
4536 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
4537 if (lustre_fs && param->fp_got_uuids &&
4538 param->fp_dev != st->st_dev) {
4539 /* A lustre/lustre mount point is crossed. */
4540 param->fp_got_uuids = 0;
4541 param->fp_obds_printed = 0;
4542 param->fp_mdt_index = OBD_NOT_FOUND;
4543 param->fp_obd_index = OBD_NOT_FOUND;
4546 if (lustre_fs && !param->fp_got_uuids) {
4547 ret = setup_target_indexes(dir ? dir : parent, path,
4552 param->fp_dev = st->st_dev;
4553 } else if (!lustre_fs && param->fp_got_uuids) {
4554 /* A lustre/non-lustre mount point is crossed. */
4555 param->fp_got_uuids = 0;
4556 param->fp_mdt_index = OBD_NOT_FOUND;
4557 param->fp_obd_index = OBD_NOT_FOUND;
4561 if (param->fp_check_foreign) {
4562 decision = find_check_foreign(param);
4567 if (param->fp_check_stripe_size) {
4568 decision = find_check_stripe_size(param);
4573 if (param->fp_check_ext_size) {
4574 decision = find_check_ext_size(param);
4579 if (param->fp_check_stripe_count) {
4580 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
4581 param->fp_stripe_count_sign,
4582 param->fp_exclude_stripe_count, 1, 0);
4587 if (param->fp_check_mdt_count) {
4588 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4593 decision = find_value_cmp(
4594 param->fp_lmv_md->lum_stripe_count,
4595 param->fp_mdt_count,
4596 param->fp_mdt_count_sign,
4597 param->fp_exclude_mdt_count, 1, 0);
4602 if (param->fp_check_layout) {
4603 decision = find_check_layout(param);
4608 if (param->fp_check_hash_type) {
4611 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4616 found = param->fp_lmv_md->lum_hash_type & param->fp_hash_type;
4617 if ((found && param->fp_exclude_hash_type) ||
4618 (!found && !param->fp_exclude_hash_type)) {
4624 /* If an OBD UUID is specified but none matches, skip this file. */
4625 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
4626 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
4629 /* If an OST or MDT UUID is given, and some OST matches,
4631 if (param->fp_obd_index != OBD_NOT_FOUND ||
4632 param->fp_mdt_index != OBD_NOT_FOUND) {
4633 if (param->fp_obd_uuid) {
4634 if (check_obd_match(param)) {
4635 /* If no mdtuuid is given, we are done.
4636 * Otherwise, fall through to the mdtuuid
4638 if (!param->fp_mdt_uuid)
4645 if (param->fp_mdt_uuid) {
4646 if (check_mdt_match(param))
4653 if (param->fp_check_uid) {
4654 if (st->st_uid == param->fp_uid) {
4655 if (param->fp_exclude_uid)
4658 if (!param->fp_exclude_uid)
4663 if (param->fp_check_gid) {
4664 if (st->st_gid == param->fp_gid) {
4665 if (param->fp_exclude_gid)
4668 if (!param->fp_exclude_gid)
4673 if (param->fp_check_projid) {
4677 fd = open(path, O_RDONLY);
4680 ret = fget_projid(fd, &projid);
4685 if (projid == param->fp_projid) {
4686 if (param->fp_exclude_projid)
4689 if (!param->fp_exclude_projid)
4694 if (param->fp_check_pool) {
4695 decision = find_check_pool(param);
4700 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
4701 param->fp_check_comp_start || param->fp_check_comp_end) {
4702 decision = find_check_comp_options(param);
4707 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
4708 decision = find_check_mirror_options(param);
4713 /* Check the time on mds. */
4715 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
4718 for_mds = lustre_fs ?
4719 (S_ISREG(st->st_mode) && stripe_count) : 0;
4720 decision = find_time_check(st, param, for_mds);
4725 /* If file still fits the request, ask ost for updated info.
4726 The regular stat is almost of the same speed as some new
4727 'glimpse-size-ioctl'. */
4729 if ((param->fp_check_size || param->fp_check_blocks) &&
4730 ((S_ISREG(st->st_mode) && stripe_count) || S_ISDIR(st->st_mode)))
4734 /* For regular files with the stripe the decision may have not
4735 * been taken yet if *time or size is to be checked. */
4736 if (param->fp_obd_index != OBD_NOT_FOUND)
4737 print_failed_tgt(param, path, LL_STATFS_LOV);
4739 if (param->fp_mdt_index != OBD_NOT_FOUND)
4740 print_failed_tgt(param, path, LL_STATFS_LMV);
4743 ret = fstat_f(dirfd(dir), st);
4744 else if (de != NULL)
4745 ret = fstatat_f(dirfd(parent), de->d_name, st,
4746 AT_SYMLINK_NOFOLLOW);
4748 ret = lstat_f(path, st);
4751 if (errno == ENOENT) {
4752 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
4753 "warning: %s: %s does not exist",
4758 llapi_error(LLAPI_MSG_ERROR, ret,
4759 "%s: IOC_LOV_GETINFO on %s failed",
4765 /* Check the time on osc. */
4766 decision = find_time_check(st, param, 0);
4771 if (param->fp_check_size) {
4772 decision = find_value_cmp(st->st_size, param->fp_size,
4773 param->fp_size_sign,
4774 param->fp_exclude_size,
4775 param->fp_size_units, 0);
4780 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
4781 decision = find_value_cmp(st->st_blocks * 512, param->fp_blocks,
4782 param->fp_blocks_sign,
4783 param->fp_exclude_blocks,
4784 param->fp_blocks_units, 0);
4790 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
4791 if (param->fp_zero_end)
4792 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
4794 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4798 /* Do not get down anymore? */
4799 if (param->fp_depth == param->fp_max_depth) {
4810 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
4811 void *param_data, struct dirent64 *de)
4813 struct find_param *param = (struct find_param *)param_data;
4814 struct lmv_user_md *lmu = param->fp_lmv_md;
4815 DIR *tmp_parent = parent;
4816 char raw[MAX_IOC_BUFLEN] = {'\0'};
4818 struct obd_ioctl_data data = { 0 };
4825 if (parent == NULL && dirp == NULL)
4834 if (parent == NULL) {
4835 tmp_parent = opendir_parent(path);
4836 if (tmp_parent == NULL) {
4839 llapi_error(LLAPI_MSG_ERROR, ret,
4840 "can not open %s", path);
4845 fd = dirfd(tmp_parent);
4847 path_copy = strdup(path);
4848 filename = basename(path_copy);
4850 data.ioc_inlbuf1 = (char *)filename;
4851 data.ioc_inllen1 = strlen(filename) + 1;
4852 data.ioc_inlbuf2 = (char *)lmu;
4853 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
4855 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
4857 llapi_error(LLAPI_MSG_ERROR, ret,
4858 "llapi_obd_statfs: error packing ioctl data");
4863 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
4865 if (errno == EBUSY && !retry) {
4866 /* because migrate may not be able to lock all involved
4867 * objects in order, for some of them it try lock, while
4868 * there may be conflicting COS locks and cause migrate
4869 * fail with EBUSY, hope a sync() could cause
4870 * transaction commit and release these COS locks. */
4874 } else if (errno == EALREADY) {
4875 if (param->fp_verbose & VERBOSE_DETAIL)
4876 llapi_printf(LLAPI_MSG_NORMAL,
4877 "%s was migrated to MDT%d already\n",
4878 path, lmu->lum_stripe_offset);
4882 llapi_error(LLAPI_MSG_ERROR, ret,
4883 "%s migrate failed: %s (%d)\n",
4884 path, strerror(-ret), ret);
4887 } else if (param->fp_verbose & VERBOSE_DETAIL) {
4888 llapi_printf(LLAPI_MSG_NORMAL,
4889 "migrate %s to MDT%d stripe count %d\n",
4890 path, lmu->lum_stripe_offset,
4891 lmu->lum_stripe_count);
4896 /* If the directory is being migration, we need
4897 * close the directory after migration,
4898 * so the old directory cache will be cleanup
4899 * on the client side, and re-open to get the
4900 * new directory handle */
4901 *dirp = opendir(path);
4902 if (*dirp == NULL) {
4904 llapi_error(LLAPI_MSG_ERROR, ret,
4905 "%s: Failed to open '%s'", __func__, path);
4910 closedir(tmp_parent);
4917 /* dir migration finished, shrink its stripes */
4918 static int cb_migrate_mdt_fini(char *path, DIR *parent, DIR **dirp, void *data,
4919 struct dirent64 *de)
4921 struct find_param *param = data;
4922 struct lmv_user_md *lmu = param->fp_lmv_md;
4923 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
4926 if (de && de->d_type != DT_DIR)
4931 * close it before setxattr because the latter may destroy the
4932 * original object, and cause close fail.
4934 ret = closedir(*dirp);
4940 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
4941 if (ret == -EALREADY)
4944 cb_common_fini(path, parent, dirp, data, de);
4948 int llapi_migrate_mdt(char *path, struct find_param *param)
4950 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
4954 int llapi_mv(char *path, struct find_param *param)
4956 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
4957 static bool printed;
4960 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
4961 "llapi_mv() is deprecated, use llapi_migrate_mdt()\n");
4965 return llapi_migrate_mdt(path, param);
4968 int llapi_find(char *path, struct find_param *param)
4970 return param_callback(path, cb_find_init, cb_common_fini, param);
4974 * Get MDT number that the file/directory inode referenced
4975 * by the open fd resides on.
4976 * Return 0 and mdtidx on success, or -ve errno.
4978 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
4980 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
4985 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
4986 struct dirent64 *de)
4988 struct find_param *param = (struct find_param *)data;
4989 DIR *d = dirp == NULL ? NULL : *dirp;
4993 if (parent == NULL && d == NULL)
4997 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
4998 } else /* if (parent) */ {
5001 fd = open(path, O_RDONLY | O_NOCTTY);
5003 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5011 if (ret == -ENODATA) {
5012 if (!param->fp_obd_uuid)
5013 llapi_printf(LLAPI_MSG_NORMAL,
5014 "'%s' has no stripe info\n", path);
5016 } else if (ret == -ENOENT) {
5017 llapi_error(LLAPI_MSG_WARN, ret,
5018 "warning: %s: '%s' does not exist",
5021 } else if (ret == -ENOTTY) {
5022 llapi_error(LLAPI_MSG_ERROR, ret,
5023 "%s: '%s' not on a Lustre fs",
5026 llapi_error(LLAPI_MSG_ERROR, ret,
5027 "error: %s: '%s' failed get_mdtidx",
5033 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5034 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5036 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5040 /* Do not go down anymore? */
5041 if (param->fp_depth == param->fp_max_depth)
5049 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
5050 struct dirent64 *de)
5052 struct find_param *param = (struct find_param *)data;
5053 DIR *d = dirp == NULL ? NULL : *dirp;
5056 if (parent == NULL && d == NULL)
5059 if (param->fp_obd_uuid) {
5060 param->fp_quiet = 1;
5061 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
5066 if (d && (param->fp_get_lmv || param->fp_get_default_lmv))
5067 ret = cb_get_dirstripe(path, d, param);
5069 (parent && !param->fp_get_lmv && !param->fp_get_default_lmv))
5070 ret = get_lmd_info(path, parent, d, ¶m->fp_lmd->lmd_lmm,
5071 param->fp_lum_size, GET_LMD_STRIPE);
5076 if (errno == ENODATA && d != NULL) {
5077 /* We need to "fake" the "use the default" values
5078 * since the lmm struct is zeroed out at this point.
5079 * The magic needs to be set in order to satisfy
5080 * a check later on in the code path.
5081 * The object_seq needs to be set for the "(Default)"
5082 * prefix to be displayed. */
5083 if (param->fp_get_default_lmv) {
5084 struct lmv_user_md *lum = param->fp_lmv_md;
5086 lum->lum_magic = LMV_USER_MAGIC;
5087 lum->lum_stripe_count = 0;
5088 lum->lum_stripe_offset = -1;
5090 } else if (param->fp_get_lmv) {
5091 struct lmv_user_md *lum = param->fp_lmv_md;
5094 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5097 lum->lum_magic = LMV_MAGIC_V1;
5098 lum->lum_stripe_count = 0;
5099 lum->lum_stripe_offset = mdtidx;
5102 struct lov_user_md *lmm =
5103 ¶m->fp_lmd->lmd_lmm;
5105 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5107 ostid_set_seq(&lmm->lmm_oi,
5108 FID_SEQ_LOV_DEFAULT);
5109 lmm->lmm_stripe_count = 0;
5110 lmm->lmm_stripe_size = 0;
5111 lmm->lmm_stripe_offset = -1;
5114 } else if (errno == ENODATA && parent != NULL) {
5115 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5116 llapi_printf(LLAPI_MSG_NORMAL,
5117 "%s has no stripe info\n", path);
5119 } else if (errno == ENOENT) {
5120 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5121 "warning: %s: %s does not exist",
5124 } else if (errno == ENOTTY) {
5126 llapi_error(LLAPI_MSG_ERROR, ret,
5127 "%s: '%s' not on a Lustre fs?",
5132 llapi_error(LLAPI_MSG_ERROR, ret,
5133 "error: %s: %s failed for %s",
5134 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
5135 "IOC_MDC_GETFILESTRIPE", path);
5142 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5143 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
5146 /* Do not get down anymore? */
5147 if (param->fp_depth == param->fp_max_depth)
5155 int llapi_getstripe(char *path, struct find_param *param)
5157 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5158 cb_get_mdt_index : cb_getstripe,
5159 cb_common_fini, param);
5162 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5163 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5165 char raw[MAX_IOC_BUFLEN] = {'\0'};
5167 struct obd_ioctl_data data = { 0 };
5170 data.ioc_inlbuf1 = (char *)&type;
5171 data.ioc_inllen1 = sizeof(__u32);
5172 data.ioc_inlbuf2 = (char *)&index;
5173 data.ioc_inllen2 = sizeof(__u32);
5174 data.ioc_pbuf1 = (char *)stat_buf;
5175 data.ioc_plen1 = sizeof(struct obd_statfs);
5176 data.ioc_pbuf2 = (char *)uuid_buf;
5177 data.ioc_plen2 = sizeof(struct obd_uuid);
5179 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5181 llapi_error(LLAPI_MSG_ERROR, rc,
5182 "llapi_obd_statfs: error packing ioctl data");
5186 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5188 return rc < 0 ? -errno : 0;
5191 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5192 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5197 fd = open(path, O_RDONLY);
5200 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5202 /* If we can't even open a file on the filesystem (e.g. with
5203 * -ESHUTDOWN), force caller to exit or it will loop forever. */
5207 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5214 #define MAX_STRING_SIZE 128
5216 int llapi_ping(char *obd_type, char *obd_name)
5218 int flags = O_RDONLY;
5219 char buf[1] = { 0 };
5223 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5224 obd_type, obd_name);
5228 fd = open(path.gl_pathv[0], flags);
5230 if (errno == EACCES && flags == O_RDONLY) {
5235 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5240 if (flags == O_RDONLY)
5241 rc = read(fd, buf, sizeof(buf));
5243 rc = write(fd, buf, sizeof(buf));
5251 cfs_free_param_data(&path);
5255 int llapi_target_iterate(int type_num, char **obd_type,
5256 void *args, llapi_cb_t cb)
5262 for (i = 0; i < type_num; i++) {
5265 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5269 for (j = 0; j < param.gl_pathc; j++) {
5270 char obd_uuid[UUID_MAX + 1];
5274 fp = fopen(param.gl_pathv[j], "r");
5277 llapi_error(LLAPI_MSG_ERROR, rc,
5278 "error: opening '%s'",
5283 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5285 llapi_error(LLAPI_MSG_ERROR, rc,
5286 "error: reading '%s'",
5291 /* Extract the obd_name from the sysfs path.
5292 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5294 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5300 /* skip /fs/lustre/'obd_type'/ */
5301 obd_name += strlen(obd_type[i]) + 12;
5302 /* chop off after obd_name */
5303 ptr = strrchr(obd_name, '/');
5307 cb(obd_type[i], obd_name, obd_uuid, args);
5316 cfs_free_param_data(¶m);
5320 static void do_target_check(char *obd_type_name, char *obd_name,
5321 char *obd_uuid, void *args)
5325 rc = llapi_ping(obd_type_name, obd_name);
5326 if (rc == ENOTCONN) {
5327 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5329 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5331 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5335 int llapi_target_check(int type_num, char **obd_type, char *dir)
5337 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5340 #undef MAX_STRING_SIZE
5342 /* Is this a lustre fs? */
5343 int llapi_is_lustre_mnttype(const char *type)
5345 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
5348 /* Is this a lustre client fs? */
5349 int llapi_is_lustre_mnt(struct mntent *mnt)
5351 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5352 strstr(mnt->mnt_fsname, ":/") != NULL);
5355 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5357 char fsname[PATH_MAX + 1];
5361 rc = llapi_search_fsname(mnt, fsname);
5365 root = open(mnt, O_RDONLY | O_DIRECTORY);
5368 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
5372 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
5380 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
5381 * format must have %s%s, buf must be > 16
5382 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
5383 * then buf = "lustre-MDT0000"
5385 static int get_mdtname(char *name, char *format, char *buf)
5387 char suffix[]="-MDT0000";
5388 int len = strlen(name);
5390 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
5391 name[len - 5] = '\0';
5396 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
5399 /* Not enough room to add suffix */
5400 llapi_err_noerrno(LLAPI_MSG_ERROR,
5401 "Invalid MDT name |%s|", name);
5406 return sprintf(buf, format, name, suffix);
5409 /** ioctl on filsystem root, with mdtindex sent as data
5410 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
5411 * \param mdtidxp pointer to integer within data to be filled in with the
5412 * mdt index (0 if no mdt is specified). NULL won't be filled.
5414 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
5422 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
5423 Open root and parse mdt index. */
5424 if (mdtname[0] == '/') {
5426 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
5427 (char *)mdtname, -1);
5429 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
5431 ptr = fsname + strlen(fsname) - 8;
5433 index = strtol(ptr + 4, NULL, 16);
5434 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
5438 llapi_err_noerrno(LLAPI_MSG_ERROR,
5439 "Can't open %s: %d\n", mdtname, rc);
5446 rc = ioctl(fd, opc, data);
5455 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
5456 int buflen, long long *recno, int *linkno)
5458 const char *fidstr_orig = fidstr;
5460 struct getinfo_fid2path *gf;
5465 while (*fidstr == '[')
5468 sscanf(fidstr, SFID, RFID(&fid));
5469 if (!fid_is_sane(&fid)) {
5470 llapi_err_noerrno(LLAPI_MSG_ERROR,
5471 "bad FID format '%s', should be [seq:oid:ver]"
5472 " (e.g. "DFID")\n", fidstr_orig,
5473 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
5477 gf = malloc(sizeof(*gf) + buflen);
5482 gf->gf_recno = *recno;
5483 gf->gf_linkno = *linkno;
5484 gf->gf_pathlen = buflen;
5486 /* Take path or fsname */
5487 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
5492 /* strip out instances of // */
5493 for (a = gf->gf_u.gf_path; *a != '\0'; a++) {
5494 if ((*a == '/') && (*(a + 1) == '/'))
5501 if (buf[0] == '\0') { /* ROOT path */
5506 *recno = gf->gf_recno;
5507 *linkno = gf->gf_linkno;
5514 static int fid_from_lma(const char *path, int fd, struct lu_fid *fid)
5517 struct lustre_mdt_attrs *lma;
5521 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
5523 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
5526 lma = (struct lustre_mdt_attrs *)buf;
5527 fid_le_to_cpu(fid, &lma->lma_self_fid);
5531 int llapi_get_mdt_index_by_fid(int fd, const struct lu_fid *fid,
5536 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
5545 int llapi_fd2fid(int fd, struct lu_fid *fid)
5549 memset(fid, 0, sizeof(*fid));
5551 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
5552 if (rc == -EINVAL || rc == -ENOTTY)
5553 rc = fid_from_lma(NULL, fd, fid);
5558 int llapi_path2fid(const char *path, struct lu_fid *fid)
5562 memset(fid, 0, sizeof(*fid));
5563 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5565 if (errno == ELOOP || errno == ENXIO)
5566 return fid_from_lma(path, -1, fid);
5570 rc = llapi_fd2fid(fd, fid);
5571 if (rc == -EINVAL || rc == -ENOTTY)
5572 rc = fid_from_lma(path, -1, fid);
5578 int llapi_fd2parent(int fd, unsigned int linkno, struct lu_fid *parent_fid,
5579 char *name, size_t name_size)
5581 struct getparent *gp;
5584 gp = malloc(sizeof(*gp) + name_size);
5588 gp->gp_linkno = linkno;
5589 gp->gp_name_size = name_size;
5591 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
5597 *parent_fid = gp->gp_fid;
5599 strncpy(name, gp->gp_name, name_size);
5600 name[name_size - 1] = '\0';
5607 int llapi_path2parent(const char *path, unsigned int linkno,
5608 struct lu_fid *parent_fid, char *name, size_t name_size)
5613 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5617 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
5622 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
5627 root = open(mnt, O_RDONLY | O_DIRECTORY);
5630 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
5634 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
5637 llapi_error(LLAPI_MSG_ERROR, rc,
5638 "ioctl on %s for getting connect flags failed", mnt);
5645 * Get a 64-bit value representing the version of file data pointed by fd.
5647 * Each write or truncate, flushed on OST, will change this value. You can use
5648 * this value to verify if file data was modified. This only checks the file
5649 * data, not metadata.
5651 * \param flags 0: no flush pages, usually used it the process has already
5653 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
5655 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
5656 * pages from clients.
5658 * \retval 0 on success.
5659 * \retval -errno on error.
5661 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
5664 struct ioc_data_version idv;
5666 idv.idv_flags = (__u32)flags;
5668 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5672 *data_version = idv.idv_version;
5678 * Flush cached pages from all clients.
5680 * \param fd File descriptor
5684 int llapi_file_flush(int fd)
5688 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);
5692 * Fetch layout version from OST objects. Layout version on OST objects are
5693 * only set when the file is a mirrored file AND after the file has been
5694 * written at least once.
5696 * It actually fetches the least layout version from the objects.
5698 int llapi_get_ost_layout_version(int fd, __u32 *layout_version)
5701 struct ioc_data_version idv = { 0 };
5703 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5707 *layout_version = idv.idv_layout_version;
5713 * Create a file without any name and open it for read/write
5715 * - file is created as if it were a standard file in the given \a directory
5716 * - file does not appear in \a directory and mtime does not change because
5717 * the filename is handled specially by the Lustre MDS.
5718 * - file is destroyed at final close
5720 * \param[in] directory directory from which to inherit layout/MDT idx
5721 * \param[in] mdt_idx MDT index on which the file is created,
5722 * \a idx == -1 means no specific MDT is requested
5723 * \param[in] mode standard open(2) mode
5724 * \param[in] stripe_param stripe parameters. May be NULL.
5726 * \retval a file descriptor on success.
5727 * \retval -errno on error.
5729 int llapi_create_volatile_param(const char *directory, int mdt_idx,
5730 int open_flags, mode_t mode,
5731 const struct llapi_stripe_param *stripe_param)
5733 char file_path[PATH_MAX];
5734 int saved_errno = errno;
5736 unsigned int rnumber;
5742 rc = snprintf(file_path, sizeof(file_path),
5743 "%s/" LUSTRE_VOLATILE_HDR "::%.4X",
5744 directory, rnumber);
5746 rc = snprintf(file_path, sizeof(file_path),
5747 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X",
5748 directory, mdt_idx, rnumber);
5750 if (rc < 0 || rc >= sizeof(file_path))
5751 return -ENAMETOOLONG;
5754 * Either open O_WRONLY or O_RDWR, creating RDONLY
5755 * is non-sensical here
5757 if ((open_flags & O_ACCMODE) == O_RDONLY)
5758 open_flags = O_RDWR | (open_flags & ~O_ACCMODE);
5760 open_flags |= O_CREAT | O_EXCL | O_NOFOLLOW;
5762 if (stripe_param != NULL) {
5763 fd = llapi_file_open_param(file_path, open_flags,
5764 mode, stripe_param);
5768 fd = open(file_path, open_flags, mode);
5772 } while (fd < 0 && rc == -EEXIST);
5775 llapi_error(LLAPI_MSG_ERROR, rc,
5776 "Cannot create volatile file '%s' in '%s'",
5777 file_path + strlen(directory) + 1 +
5778 LUSTRE_VOLATILE_HDR_LEN,
5784 * Unlink file in case this wasn't a Lustre filesystem and the magic
5785 * volatile filename wasn't handled as intended. The effect is the
5786 * same. If volatile open was supported then we expect unlink() to
5789 (void)unlink(file_path);
5792 * Since we are returning successfully we restore errno (and
5793 * mask out possible EEXIST from open() and ENOENT from unlink().
5795 errno = saved_errno;
5801 * Create a file without any name open it for read/write
5803 * - file is created as if it were a standard file in the given \a directory
5804 * - file does not appear in \a directory and mtime does not change because
5805 * the filename is handled specially by the Lustre MDS.
5806 * - file is removed at final close
5807 * - file modes are rw------- since it doesn't make sense to have a read-only
5808 * or write-only file that cannot be opened again.
5809 * - if user wants another mode it must use fchmod() on the open file, no
5810 * security problems arise because it cannot be opened by another process.
5812 * \param[in] directory directory from which to inherit layout/MDT idx
5813 * \param[in] idx MDT index on which the file is created,
5814 * \a idx == -1 means no specific MDT is requested
5815 * \param[in] open_flags standard open(2) flags
5817 * \retval a file descriptor on success.
5818 * \retval -errno on error.
5820 int llapi_create_volatile_idx(const char *directory, int mdt_idx,
5823 return llapi_create_volatile_param(directory, mdt_idx, open_flags,
5824 S_IRUSR | S_IWUSR, NULL);
5828 * Swap the layouts between 2 file descriptors
5829 * the 2 files must be open for writing
5830 * first fd received the ioctl, second fd is passed as arg
5831 * this is assymetric but avoid use of root path for ioctl
5833 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
5834 int gid, __u64 flags)
5836 struct lustre_swap_layouts lsl;
5841 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5842 rc = fstat(fd1, &st1);
5846 rc = fstat(fd2, &st2);
5851 lsl.sl_flags = flags;
5855 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
5859 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5860 struct timeval tv1[2];
5861 struct timeval tv2[2];
5863 memset(tv1, 0, sizeof(tv1));
5864 memset(tv2, 0, sizeof(tv2));
5866 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
5867 tv1[0].tv_sec = st1.st_atime;
5868 tv2[0].tv_sec = st2.st_atime;
5870 tv1[0].tv_sec = st2.st_atime;
5871 tv2[0].tv_sec = st1.st_atime;
5874 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
5875 tv1[1].tv_sec = st1.st_mtime;
5876 tv2[1].tv_sec = st2.st_mtime;
5878 tv1[1].tv_sec = st2.st_mtime;
5879 tv2[1].tv_sec = st1.st_mtime;
5882 rc = futimes(fd1, tv1);
5886 rc = futimes(fd2, tv2);
5894 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
5901 while (grp_id == 0);
5903 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
5911 * Swap the layouts between 2 files
5912 * the 2 files are open in write
5914 int llapi_swap_layouts(const char *path1, const char *path2,
5915 __u64 dv1, __u64 dv2, __u64 flags)
5919 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
5922 llapi_error(LLAPI_MSG_ERROR, rc,
5923 "error: cannot open '%s' for write", path1);
5927 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
5930 llapi_error(LLAPI_MSG_ERROR, rc,
5931 "error: cannot open '%s' for write", path2);
5935 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
5937 llapi_error(LLAPI_MSG_ERROR, rc,
5938 "error: cannot swap layout between '%s' and '%s'",
5949 * Attempt to open a file with Lustre file identifier \a fid
5950 * and return an open file descriptor.
5952 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
5953 * \param[in] fid Lustre file identifier of file to open
5954 * \param[in] flags open() flags
5956 * \retval non-negative file descriptor on successful open
5957 * \retval -1 if an error occurred
5959 int llapi_open_by_fid(const char *lustre_dir, const struct lu_fid *fid,
5962 char mntdir[PATH_MAX];
5963 char path[PATH_MAX + 64];
5966 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
5970 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
5971 return open(path, flags);
5977 * \param fd File to lock.
5978 * \param gid Group Identifier.
5980 * \retval 0 on success.
5981 * \retval -errno on failure.
5983 int llapi_group_lock(int fd, int gid)
5987 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
5990 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
5998 * \param fd File to unlock.
5999 * \param gid Group Identifier.
6001 * \retval 0 on success.
6002 * \retval -errno on failure.
6004 int llapi_group_unlock(int fd, int gid)
6008 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
6011 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");