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>
62 #include <sys/sysmacros.h>
65 #include <libgen.h> /* for dirname() */
66 #ifdef HAVE_LINUX_UNISTD_H
67 #include <linux/unistd.h>
75 #include <libcfs/util/ioctl.h>
76 #include <libcfs/util/param.h>
77 #include <libcfs/util/string.h>
78 #include <linux/lnet/lnetctl.h>
79 #include <lustre/lustreapi.h>
80 #include <linux/lustre/lustre_ostid.h>
81 #include <linux/lustre/lustre_ioctl.h>
82 #include "lustreapi_internal.h"
85 static int llapi_msg_level = LLAPI_MSG_MAX;
86 const char *liblustreapi_cmd;
88 char *mdt_hash_name[] = { "none",
89 LMV_HASH_NAME_ALL_CHARS,
90 LMV_HASH_NAME_FNV_1A_64,
94 struct lustre_foreign_type lu_foreign_types[] = {
95 {.lft_type = LU_FOREIGN_TYPE_NONE, .lft_name = "none"},
96 {.lft_type = LU_FOREIGN_TYPE_DAOS, .lft_name = "daos"},
97 /* must be the last element */
98 {.lft_type = LU_FOREIGN_TYPE_UNKNOWN, .lft_name = NULL}
99 /* array max dimension must be <= UINT32_MAX */
102 void llapi_msg_set_level(int level)
104 /* ensure level is in the good range */
105 if (level < LLAPI_MSG_OFF)
106 llapi_msg_level = LLAPI_MSG_OFF;
107 else if (level > LLAPI_MSG_MAX)
108 llapi_msg_level = LLAPI_MSG_MAX;
110 llapi_msg_level = level;
113 int llapi_msg_get_level(void)
115 return llapi_msg_level;
118 void llapi_set_command_name(const char *cmd)
120 liblustreapi_cmd = cmd;
123 void llapi_clear_command_name(void)
125 liblustreapi_cmd = NULL;
128 static void error_callback_default(enum llapi_message_level level, int err,
129 const char *fmt, va_list ap)
131 bool has_nl = strchr(fmt, '\n') != NULL;
133 if (liblustreapi_cmd != NULL)
134 fprintf(stderr, "%s %s: ", program_invocation_short_name,
137 fprintf(stderr, "%s: ", program_invocation_short_name);
140 if (level & LLAPI_MSG_NO_ERRNO) {
141 vfprintf(stderr, fmt, ap);
143 fprintf(stderr, "\n");
147 /* Remove trailing linefeed so error string can be appended.
148 * @fmt is a const string, so we can't modify it directly.
150 if (has_nl && (newfmt = strdup(fmt)))
151 *strrchr(newfmt, '\n') = '\0';
153 newfmt = (char *)fmt;
155 vfprintf(stderr, newfmt, ap);
158 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
162 static void info_callback_default(enum llapi_message_level level, int err,
163 const char *fmt, va_list ap)
166 if (liblustreapi_cmd != NULL) {
167 fprintf(stdout, "%s %s: ",
168 program_invocation_short_name,
171 fprintf(stdout, "%s: ", program_invocation_short_name);
174 vfprintf(stdout, fmt, ap);
177 static llapi_log_callback_t llapi_error_callback = error_callback_default;
178 static llapi_log_callback_t llapi_info_callback = info_callback_default;
181 /* llapi_error will preserve errno */
182 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
185 int tmp_errno = errno;
187 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
191 llapi_error_callback(level, abs(err), fmt, args);
196 /* llapi_printf will preserve errno */
197 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
200 int tmp_errno = errno;
202 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
206 llapi_info_callback(level, 0, fmt, args);
212 * Set a custom error logging function. Passing in NULL will reset the logging
213 * callback to its default value.
215 * This function returns the value of the old callback.
217 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
219 llapi_log_callback_t old = llapi_error_callback;
222 llapi_error_callback = cb;
224 llapi_error_callback = error_callback_default;
230 * Set a custom info logging function. Passing in NULL will reset the logging
231 * callback to its default value.
233 * This function returns the value of the old callback.
235 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
237 llapi_log_callback_t old = llapi_info_callback;
240 llapi_info_callback = cb;
242 llapi_info_callback = info_callback_default;
248 * size_units is to be initialized (or zeroed) by caller.
250 int llapi_parse_size(const char *optarg, unsigned long long *size,
251 unsigned long long *size_units, int bytes_spec)
254 char *argbuf = (char *)optarg;
255 unsigned long long frac = 0, frac_d = 1;
257 if (strncmp(optarg, "-", 1) == 0)
260 if (*size_units == 0)
263 *size = strtoull(argbuf, &end, 0);
264 if (end != NULL && *end == '.') {
268 frac = strtoull(argbuf, &end, 10);
269 /* count decimal places */
270 for (i = 0; i < (end - argbuf); i++)
275 if ((*end == 'b') && *(end + 1) == '\0' &&
276 (*size & (~0ULL << (64 - 9))) == 0 &&
278 *size_units = 1 << 9;
279 } else if ((*end == 'b') &&
280 *(end + 1) == '\0' &&
283 } else if ((*end == 'k' || *end == 'K') &&
284 *(end + 1) == '\0' &&
285 (*size & (~0ULL << (64 - 10))) == 0) {
286 *size_units = 1 << 10;
287 } else if ((*end == 'm' || *end == 'M') &&
288 *(end + 1) == '\0' &&
289 (*size & (~0ULL << (64 - 20))) == 0) {
290 *size_units = 1 << 20;
291 } else if ((*end == 'g' || *end == 'G') &&
292 *(end + 1) == '\0' &&
293 (*size & (~0ULL << (64 - 30))) == 0) {
294 *size_units = 1 << 30;
295 } else if ((*end == 't' || *end == 'T') &&
296 *(end + 1) == '\0' &&
297 (*size & (~0ULL << (64 - 40))) == 0) {
298 *size_units = 1ULL << 40;
299 } else if ((*end == 'p' || *end == 'P') &&
300 *(end + 1) == '\0' &&
301 (*size & (~0ULL << (64 - 50))) == 0) {
302 *size_units = 1ULL << 50;
303 } else if ((*end == 'e' || *end == 'E') &&
304 *(end + 1) == '\0' &&
305 (*size & (~0ULL << (64 - 60))) == 0) {
306 *size_units = 1ULL << 60;
311 *size = *size * *size_units + frac * *size_units / frac_d;
316 int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
318 struct obd_ioctl_data *overlay;
321 data->ioc_len = obd_ioctl_packlen(data);
322 data->ioc_version = OBD_IOCTL_VERSION;
324 if (*pbuf != NULL && data->ioc_len > max_len) {
325 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
326 "pbuf = %p, ioc_len = %u, max_len = %d",
327 *pbuf, data->ioc_len, max_len);
332 *pbuf = malloc(data->ioc_len);
337 overlay = (struct obd_ioctl_data *)*pbuf;
338 memcpy(*pbuf, data, sizeof(*data));
340 ptr = overlay->ioc_bulk;
341 if (data->ioc_inlbuf1) {
342 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
343 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
346 if (data->ioc_inlbuf2) {
347 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
348 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
351 if (data->ioc_inlbuf3) {
352 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
353 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
356 if (data->ioc_inlbuf4) {
357 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
358 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
364 int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
366 struct obd_ioctl_data *overlay;
372 overlay = (struct obd_ioctl_data *)pbuf;
374 /* Preserve the caller's buffer pointers */
375 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
376 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
377 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
378 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
380 memcpy(data, pbuf, sizeof(*data));
382 ptr = overlay->ioc_bulk;
383 if (data->ioc_inlbuf1) {
384 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
385 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
388 if (data->ioc_inlbuf2) {
389 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
390 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
393 if (data->ioc_inlbuf3) {
394 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
395 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
398 if (data->ioc_inlbuf4) {
399 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
400 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
406 /* XXX: llapi_xxx() functions return negative values upon failure */
408 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
409 int stripe_count, int stripe_pattern)
411 static int page_size;
414 if (page_size == 0) {
415 /* 64 KB is the largest common page size (on ia64/PPC/ARM),
416 * but check the local page size just in case. The page_size
417 * will not change for the lifetime of this process at least.
419 page_size = LOV_MIN_STRIPE_SIZE;
420 if (getpagesize() > page_size) {
421 page_size = getpagesize();
422 llapi_err_noerrno(LLAPI_MSG_WARN,
423 "warning: page size (%u) larger than expected (%u)",
424 page_size, LOV_MIN_STRIPE_SIZE);
427 if (!llapi_stripe_size_is_aligned(stripe_size)) {
429 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
430 "must be an even multiple of %d bytes",
431 (unsigned long long)stripe_size, page_size);
434 if (!llapi_stripe_index_is_valid(stripe_offset)) {
436 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
440 if (!llapi_stripe_count_is_valid(stripe_count)) {
442 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
446 if (llapi_stripe_size_is_too_big(stripe_size)) {
448 llapi_error(LLAPI_MSG_ERROR, rc,
449 "error: stripe size '%llu' over 4GB limit",
450 (unsigned long long)stripe_size);
459 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
464 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
466 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
470 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
472 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
477 if (!llapi_dir_hash_type_is_valid(hash_type)) {
479 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
487 * Trim a trailing newline from a string, if it exists.
489 int llapi_chomp_string(char *buf)
505 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
507 static int get_param_lov(const char *path, const char *param,
508 char *buf, size_t buf_size)
510 struct obd_uuid uuid;
513 rc = llapi_file_get_lov_uuid(path, &uuid);
517 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
522 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
524 static int get_param_lmv(const char *path, const char *param,
525 char *buf, size_t buf_size)
527 struct obd_uuid uuid;
530 rc = llapi_file_get_lmv_uuid(path, &uuid);
534 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
538 static int get_mds_md_size(const char *path)
540 char buf[PATH_MAX], inst[PATH_MAX];
541 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
544 rc = llapi_getname(path, inst, sizeof(inst));
548 /* Get the max ea size from llite parameters. */
549 rc = get_lustre_param_value("llite", inst, FILTER_BY_EXACT,
550 "max_easize", buf, sizeof(buf));
556 return rc > 0 ? rc : md_size;
559 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
561 return get_param_lmv(path, "uuid", buf, bufsize);
565 * Open a Lustre file.
567 * \param name the name of the file to be opened
568 * \param flags access mode, see flags in open(2)
569 * \param mode permission of the file if it is created, see mode in open(2)
570 * \param param stripe pattern of the newly created file
572 * \retval file descriptor of opened file
573 * \retval negative errno on failure
575 int llapi_file_open_param(const char *name, int flags, mode_t mode,
576 const struct llapi_stripe_param *param)
578 char fsname[MAX_OBD_NAME + 1] = { 0 };
579 char *pool_name = param->lsp_pool;
580 struct lov_user_md *lum = NULL;
581 size_t lum_size = sizeof(*lum);
584 /* Make sure we are on a Lustre file system */
585 rc = llapi_search_fsname(name, fsname);
587 llapi_error(LLAPI_MSG_ERROR, rc,
588 "'%s' is not on a Lustre filesystem",
593 /* Check if the stripe pattern is sane. */
594 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
595 param->lsp_stripe_offset,
596 param->lsp_stripe_count,
597 param->lsp_stripe_pattern);
601 /* Make sure we have a good pool */
602 if (pool_name != NULL) {
603 /* in case user gives the full pool name <fsname>.<poolname>,
604 * strip the fsname */
605 char *ptr = strchr(pool_name, '.');
608 if (strcmp(pool_name, fsname) != 0) {
610 llapi_err_noerrno(LLAPI_MSG_ERROR,
611 "Pool '%s' is not on filesystem '%s'",
618 /* Make sure the pool exists and is non-empty */
619 rc = llapi_search_ost(fsname, pool_name, NULL);
621 char *err = rc == 0 ? "has no OSTs" : "does not exist";
623 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
624 fsname, pool_name, err);
628 lum_size = sizeof(struct lov_user_md_v3);
631 /* sanity check of target list */
632 if (param->lsp_is_specific) {
633 char ostname[MAX_OBD_NAME + 64];
637 for (i = 0; i < param->lsp_stripe_count; i++) {
638 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
639 fsname, param->lsp_osts[i]);
640 rc = llapi_search_ost(fsname, pool_name, ostname);
645 llapi_error(LLAPI_MSG_ERROR, rc,
646 "%s: cannot find OST %s in %s",
653 /* Make sure stripe offset is in OST list. */
654 if (param->lsp_osts[i] == param->lsp_stripe_offset)
658 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
659 "%s: stripe offset '%d' is not in the "
661 __func__, param->lsp_stripe_offset);
665 lum_size = lov_user_md_size(param->lsp_stripe_count,
666 LOV_USER_MAGIC_SPECIFIC);
669 lum = calloc(1, lum_size);
674 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
676 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
677 flags = O_DIRECTORY | O_RDONLY;
684 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
689 /* Initialize IOCTL striping pattern structure */
690 lum->lmm_magic = LOV_USER_MAGIC_V1;
691 lum->lmm_pattern = param->lsp_stripe_pattern;
692 lum->lmm_stripe_size = param->lsp_stripe_size;
693 lum->lmm_stripe_count = param->lsp_stripe_count;
694 lum->lmm_stripe_offset = param->lsp_stripe_offset;
695 if (pool_name != NULL) {
696 struct lov_user_md_v3 *lumv3 = (void *)lum;
698 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
699 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
701 if (param->lsp_is_specific) {
702 struct lov_user_md_v3 *lumv3 = (void *)lum;
705 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
706 if (pool_name == NULL) {
707 /* LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
708 * OST list, therefore if pool is not specified we have
709 * to pack a null pool name for placeholder. */
710 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
713 for (i = 0; i < param->lsp_stripe_count; i++)
714 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
717 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
718 char errmsg[512] = "stripe already set";
721 if (errno != EEXIST && errno != EALREADY)
722 strncpy(errmsg, strerror(errno), sizeof(errmsg) - 1);
723 if (rc == -EREMOTEIO)
724 snprintf(errmsg, sizeof(errmsg),
725 "inactive OST among your specified %d OST(s)",
726 param->lsp_stripe_count);
728 llapi_err_noerrno(LLAPI_MSG_ERROR,
729 "setstripe error for '%s': %s", name, errmsg);
740 int llapi_file_open_pool(const char *name, int flags, int mode,
741 unsigned long long stripe_size, int stripe_offset,
742 int stripe_count, int stripe_pattern, char *pool_name)
744 const struct llapi_stripe_param param = {
745 .lsp_stripe_size = stripe_size,
746 .lsp_stripe_count = stripe_count,
747 .lsp_stripe_pattern = stripe_pattern,
748 .lsp_stripe_offset = stripe_offset,
749 .lsp_pool = pool_name
751 return llapi_file_open_param(name, flags, mode, ¶m);
754 int llapi_file_open(const char *name, int flags, int mode,
755 unsigned long long stripe_size, int stripe_offset,
756 int stripe_count, int stripe_pattern)
758 return llapi_file_open_pool(name, flags, mode, stripe_size,
759 stripe_offset, stripe_count,
760 stripe_pattern, NULL);
763 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
764 __u32 flags, char *foreign_lov)
767 struct lov_foreign_md *lfm;
770 if (foreign_lov == NULL) {
772 llapi_error(LLAPI_MSG_ERROR, rc,
773 "foreign LOV EA content must be provided");
777 len = strlen(foreign_lov);
778 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
781 llapi_error(LLAPI_MSG_ERROR, rc,
782 "foreign LOV EA size %zu (must be 0 < len < %zu)",
783 len, XATTR_SIZE_MAX -
784 offsetof(struct lov_foreign_md, lfm_value));
788 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
791 llapi_error(LLAPI_MSG_ERROR, rc,
792 "failed to allocate lov_foreign_md");
796 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
799 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
803 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
804 lfm->lfm_length = len;
805 lfm->lfm_type = type;
806 lfm->lfm_flags = flags;
807 memcpy(lfm->lfm_value, foreign_lov, len);
809 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
810 char *errmsg = "stripe already set";
814 errmsg = "not on a Lustre filesystem";
815 else if (errno == EEXIST || errno == EALREADY)
816 errmsg = "stripe already set";
818 errmsg = strerror(errno);
820 llapi_err_noerrno(LLAPI_MSG_ERROR,
821 "setstripe error for '%s': %s", name, errmsg);
837 int llapi_file_create(const char *name, unsigned long long stripe_size,
838 int stripe_offset, int stripe_count, int stripe_pattern)
842 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
843 stripe_offset, stripe_count, stripe_pattern,
852 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
853 int stripe_offset, int stripe_count,
854 int stripe_pattern, char *pool_name)
858 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
859 stripe_offset, stripe_count, stripe_pattern,
868 static int verify_dir_param(const char *name,
869 const struct llapi_stripe_param *param)
871 char fsname[MAX_OBD_NAME + 1] = { 0 };
872 char *pool_name = param->lsp_pool;
875 /* Make sure we are on a Lustre file system */
876 rc = llapi_search_fsname(name, fsname);
878 llapi_error(LLAPI_MSG_ERROR, rc,
879 "'%s' is not on a Lustre filesystem",
884 /* Check if the stripe pattern is sane. */
885 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
886 param->lsp_stripe_count,
887 param->lsp_stripe_pattern);
891 /* Make sure we have a good pool */
892 if (pool_name != NULL) {
893 /* in case user gives the full pool name <fsname>.<poolname>,
894 * strip the fsname */
895 char *ptr = strchr(pool_name, '.');
899 if (strcmp(pool_name, fsname) != 0) {
901 llapi_err_noerrno(LLAPI_MSG_ERROR,
902 "Pool '%s' is not on filesystem '%s'",
909 /* Make sure the pool exists and is non-empty */
910 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
912 char *err = rc == 0 ? "has no OSTs" : "does not exist";
914 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
915 fsname, pool_name, err);
920 /* sanity check of target list */
921 if (param->lsp_is_specific) {
922 char mdtname[MAX_OBD_NAME + 64];
926 for (i = 0; i < param->lsp_stripe_count; i++) {
927 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
928 fsname, param->lsp_tgts[i]);
929 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
934 llapi_error(LLAPI_MSG_ERROR, rc,
935 "%s: cannot find MDT %s in %s",
942 /* Make sure stripe offset is in MDT list. */
943 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
947 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
948 "%s: stripe offset '%d' is not in the "
950 __func__, param->lsp_stripe_offset);
958 static inline void param2lmu(struct lmv_user_md *lmu,
959 const struct llapi_stripe_param *param)
961 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
963 lmu->lum_stripe_count = param->lsp_stripe_count;
964 lmu->lum_stripe_offset = param->lsp_stripe_offset;
965 lmu->lum_hash_type = param->lsp_stripe_pattern;
966 if (param->lsp_pool != NULL)
967 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
968 if (param->lsp_is_specific) {
971 for (i = 0; i < param->lsp_stripe_count; i++)
972 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
976 int llapi_dir_set_default_lmv(const char *name,
977 const struct llapi_stripe_param *param)
979 struct lmv_user_md lmu = { 0 };
983 rc = verify_dir_param(name, param);
987 /* TODO: default lmv doesn't support specific targets yet */
988 if (param->lsp_is_specific)
991 param2lmu(&lmu, param);
993 fd = open(name, O_DIRECTORY | O_RDONLY);
996 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1000 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
1002 char *errmsg = "stripe already set";
1004 if (errno != EEXIST && errno != EALREADY)
1005 errmsg = strerror(errno);
1007 llapi_err_noerrno(LLAPI_MSG_ERROR,
1008 "default dirstripe error on '%s': %s",
1015 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
1016 int stripe_count, int stripe_pattern,
1017 const char *pool_name)
1019 const struct llapi_stripe_param param = {
1020 .lsp_stripe_count = stripe_count,
1021 .lsp_stripe_offset = stripe_offset,
1022 .lsp_stripe_pattern = stripe_pattern,
1023 .lsp_pool = (char *)pool_name
1026 return llapi_dir_set_default_lmv(name, ¶m);
1030 * Create a Lustre directory.
1032 * \param name the name of the directory to be created
1033 * \param mode permission of the file if it is created, see mode in open(2)
1034 * \param param stripe pattern of the newly created directory
1036 * \retval 0 on success
1037 * \retval negative errno on failure
1039 int llapi_dir_create(const char *name, mode_t mode,
1040 const struct llapi_stripe_param *param)
1042 struct lmv_user_md *lmu = NULL;
1044 struct obd_ioctl_data data = { 0 };
1047 char *dirpath = NULL;
1048 char *namepath = NULL;
1053 rc = verify_dir_param(name, param);
1057 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1058 param->lsp_is_specific ?
1059 LMV_USER_MAGIC_SPECIFIC :
1062 lmu = calloc(1, lmu_size);
1066 dirpath = strdup(name);
1072 namepath = strdup(name);
1079 param2lmu(lmu, param);
1081 filename = basename(namepath);
1082 dir = dirname(dirpath);
1084 data.ioc_inlbuf1 = (char *)filename;
1085 data.ioc_inllen1 = strlen(filename) + 1;
1086 data.ioc_inlbuf2 = (char *)lmu;
1087 data.ioc_inllen2 = lmu_size;
1088 data.ioc_type = mode;
1089 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1091 llapi_error(LLAPI_MSG_ERROR, rc,
1092 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1097 fd = open(dir, O_DIRECTORY | O_RDONLY);
1100 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1104 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1105 char *errmsg = "stripe already set";
1108 if (errno != EEXIST && errno != EALREADY)
1109 errmsg = strerror(errno);
1111 llapi_err_noerrno(LLAPI_MSG_ERROR,
1112 "dirstripe error on '%s': %s", name, errmsg);
1123 * Create a foreign directory.
1125 * \param name the name of the directory to be created
1126 * \param mode permission of the file if it is created, see mode in open(2)
1127 * \param type foreign type to be set in LMV EA
1128 * \param flags foreign flags to be set in LMV EA
1129 * \param value foreign pattern to be set in LMV EA
1131 * \retval 0 on success
1132 * \retval negative errno on failure
1134 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1135 __u32 flags, const char *value)
1137 struct lmv_foreign_md *lfm = NULL;
1138 size_t lfm_size, len;
1139 struct obd_ioctl_data data = { 0 };
1142 char *dirpath = NULL;
1143 char *namepath = NULL;
1148 len = strlen(value);
1149 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1152 llapi_error(LLAPI_MSG_ERROR, rc,
1153 "invalid LOV EA length %zu (must be 0 < len < %zu)",
1154 len, XATTR_SIZE_MAX -
1155 offsetof(struct lmv_foreign_md, lfm_value));
1158 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1159 lfm = calloc(1, lfm_size);
1163 dirpath = strdup(name);
1169 namepath = strdup(name);
1176 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1177 lfm->lfm_length = len;
1178 lfm->lfm_type = type;
1179 lfm->lfm_flags = flags;
1180 memcpy(lfm->lfm_value, value, len);
1182 filename = basename(namepath);
1183 dir = dirname(dirpath);
1185 data.ioc_inlbuf1 = (char *)filename;
1186 data.ioc_inllen1 = strlen(filename) + 1;
1187 data.ioc_inlbuf2 = (char *)lfm;
1188 data.ioc_inllen2 = lfm_size;
1189 data.ioc_type = mode;
1190 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1192 llapi_error(LLAPI_MSG_ERROR, rc,
1193 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1198 fd = open(dir, O_DIRECTORY | O_RDONLY);
1201 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1205 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1206 char *errmsg = "stripe already set";
1209 if (errno != EEXIST && errno != EALREADY)
1210 errmsg = strerror(errno);
1212 llapi_err_noerrno(LLAPI_MSG_ERROR,
1213 "dirstripe error on '%s': %s", name, errmsg);
1223 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1224 int stripe_count, int stripe_pattern,
1225 const char *pool_name)
1227 const struct llapi_stripe_param param = {
1228 .lsp_stripe_count = stripe_count,
1229 .lsp_stripe_offset = stripe_offset,
1230 .lsp_stripe_pattern = stripe_pattern,
1231 .lsp_pool = (char *)pool_name
1234 return llapi_dir_create(name, mode, ¶m);
1237 int llapi_direntry_remove(char *dname)
1239 char *dirpath = NULL;
1240 char *namepath = NULL;
1246 dirpath = strdup(dname);
1247 namepath = strdup(dname);
1248 if (!dirpath || !namepath)
1251 filename = basename(namepath);
1253 dir = dirname(dirpath);
1255 fd = open(dir, O_DIRECTORY | O_RDONLY);
1258 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
1263 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename))
1264 llapi_error(LLAPI_MSG_ERROR, errno,
1265 "error on ioctl %#lx for '%s' (%d)",
1266 (long)LL_IOC_LMV_SETSTRIPE, filename, fd);
1276 * Find the fsname, the full path, and/or an open fd.
1277 * Either the fsname or path must not be NULL
1279 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1282 char buf[PATH_MAX], mntdir[PATH_MAX];
1283 char *ptr, *ptr_end;
1285 int idx = 0, len = 0, mntlen, fd;
1287 int fsnamelen, mountlen;
1289 /* get the mount point */
1290 fp = setmntent(PROC_MOUNTS, "r");
1293 llapi_error(LLAPI_MSG_ERROR, rc,
1294 "cannot retrieve filesystem mount point");
1298 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1301 if (!llapi_is_lustre_mnt(&mnt))
1304 if ((want & WANT_INDEX) && (idx++ != index))
1307 mntlen = strlen(mnt.mnt_dir);
1308 ptr = strchr(mnt.mnt_fsname, '/');
1309 while (ptr && *ptr == '/')
1311 /* thanks to the call to llapi_is_lustre_mnt() above,
1312 * we are sure that mnt.mnt_fsname contains ":/",
1313 * so ptr should never be NULL */
1317 while (*ptr_end != '/' && *ptr_end != '\0')
1320 /* Check the fsname for a match, if given */
1321 mountlen = ptr_end - ptr;
1322 if (!(want & WANT_FSNAME) && fsname != NULL &&
1323 (fsnamelen = strlen(fsname)) > 0 &&
1324 (fsnamelen != mountlen ||
1325 (strncmp(ptr, fsname, mountlen) != 0)))
1328 /* If the path isn't set return the first one we find */
1329 if (path == NULL || strlen(path) == 0) {
1330 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1331 mntdir[sizeof(mntdir) - 1] = '\0';
1332 if ((want & WANT_FSNAME) && fsname != NULL) {
1333 strncpy(fsname, ptr, mountlen);
1334 fsname[mountlen] = '\0';
1338 /* Otherwise find the longest matching path */
1339 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1340 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1341 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1342 mntdir[sizeof(mntdir) - 1] = '\0';
1344 if ((want & WANT_FSNAME) && fsname != NULL) {
1345 strncpy(fsname, ptr, mountlen);
1346 fsname[mountlen] = '\0';
1355 if ((want & WANT_PATH) && path != NULL) {
1356 strncpy(path, mntdir, PATH_MAX);
1357 path[strlen(mntdir)] = '\0';
1359 if (want & WANT_FD) {
1360 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1363 llapi_error(LLAPI_MSG_ERROR, rc,
1364 "cannot open '%s'", mntdir);
1370 } else if (want & WANT_ERROR)
1371 llapi_err_noerrno(LLAPI_MSG_ERROR,
1372 "'%s' not on a mounted Lustre filesystem",
1373 (want & WANT_PATH) ? fsname : path);
1378 * search lustre mounts
1380 * Calling this function will return to the user the mount point, mntdir, and
1381 * the file system name, fsname, if the user passed a buffer to this routine.
1383 * The user inputs are pathname and index. If the pathname is supplied then
1384 * the value of the index will be ignored. The pathname will return data if
1385 * the pathname is located on a lustre mount. Index is used to pick which
1386 * mount point you want in the case of multiple mounted lustre file systems.
1387 * See function lfs_osts in lfs.c for an example of the index use.
1389 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1392 int want = WANT_PATH, idx = -1;
1394 if (!pathname || pathname[0] == '\0') {
1398 strcpy(mntdir, pathname);
1401 want |= WANT_FSNAME;
1402 return get_root_path(want, fsname, NULL, mntdir, idx);
1405 /* Given a path, find the corresponding Lustre fsname */
1406 int llapi_search_fsname(const char *pathname, char *fsname)
1411 path = realpath(pathname, NULL);
1413 char tmp[PATH_MAX - 1];
1419 if (pathname[0] != '/') {
1420 /* Need an absolute path, but realpath() only works for
1421 * pathnames that actually exist. We go through the
1422 * extra hurdle of dirname(getcwd() + pathname) in
1423 * case the relative pathname contains ".." in it. */
1424 char realpath[PATH_MAX - 1];
1426 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1428 llapi_error(LLAPI_MSG_ERROR, rc,
1429 "cannot get current working directory");
1433 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1434 if (rc >= sizeof(tmp)) {
1436 llapi_error(LLAPI_MSG_ERROR, rc,
1437 "invalid parent path '%s'",
1443 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1444 if (rc >= sizeof(buf)) {
1446 llapi_error(LLAPI_MSG_ERROR, rc,
1447 "invalid path '%s'", pathname);
1450 path = realpath(buf, NULL);
1452 ptr = strrchr(buf, '/');
1454 llapi_error(LLAPI_MSG_ERROR |
1455 LLAPI_MSG_NO_ERRNO, 0,
1456 "cannot resolve path '%s'",
1461 path = realpath(buf, NULL);
1464 llapi_error(LLAPI_MSG_ERROR, rc,
1465 "cannot resolve path '%s'",
1471 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1476 int llapi_search_rootpath(char *pathname, const char *fsname)
1478 /* pathname can be used as an argument by get_root_path(),
1479 * clear it for safety */
1481 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1485 * Get the list of pool members.
1486 * \param poolname string of format \<fsname\>.\<poolname\>
1487 * \param members caller-allocated array of char*
1488 * \param list_size size of the members array
1489 * \param buffer caller-allocated buffer for storing OST names
1490 * \param buffer_size size of the buffer
1492 * \return number of members retrieved for this pool
1493 * \retval -error failure
1495 int llapi_get_poolmembers(const char *poolname, char **members,
1496 int list_size, char *buffer, int buffer_size)
1498 char fsname[PATH_MAX];
1507 /* name is FSNAME.POOLNAME */
1508 if (strlen(poolname) >= sizeof(fsname))
1511 snprintf(fsname, sizeof(fsname), "%s", poolname);
1512 pool = strchr(fsname, '.');
1519 rc = poolpath(&pathname, fsname, NULL);
1521 llapi_error(LLAPI_MSG_ERROR, rc,
1522 "Lustre filesystem '%s' not found",
1527 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1528 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1529 cfs_free_param_data(&pathname);
1530 if (rc >= sizeof(buf))
1532 fd = fopen(buf, "r");
1535 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1540 while (fgets(buf, sizeof(buf), fd) != NULL) {
1541 if (nb_entries >= list_size) {
1545 buf[sizeof(buf) - 1] = '\0';
1547 tmp = strchr(buf, '\n');
1550 if (used + strlen(buf) + 1 > buffer_size) {
1555 strcpy(buffer + used, buf);
1556 members[nb_entries] = buffer + used;
1557 used += strlen(buf) + 1;
1567 * Get the list of pools in a filesystem.
1568 * \param name filesystem name or path
1569 * \param poollist caller-allocated array of char*
1570 * \param list_size size of the poollist array
1571 * \param buffer caller-allocated buffer for storing pool names
1572 * \param buffer_size size of the buffer
1574 * \return number of pools retrieved for this filesystem
1575 * \retval -error failure
1577 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1578 char *buffer, int buffer_size)
1584 struct dirent *pool;
1586 unsigned int nb_entries = 0;
1587 unsigned int used = 0;
1590 /* initialize output array */
1591 for (i = 0; i < list_size; i++)
1594 /* is name a pathname ? */
1595 ptr = strchr(name, '/');
1597 char fsname_buf[MAXNAMLEN];
1599 /* We will need fsname for printing later */
1600 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1604 ptr = strrchr(fsname_buf, '-');
1608 fsname = strdup(fsname_buf);
1612 /* name is FSNAME */
1613 fsname = strdup(name);
1618 rc = poolpath(&pathname, fsname, NULL);
1620 llapi_error(LLAPI_MSG_ERROR, rc,
1621 "Lustre filesystem '%s' not found", name);
1625 dir = opendir(pathname.gl_pathv[0]);
1628 llapi_error(LLAPI_MSG_ERROR, rc,
1629 "Could not open pool list for '%s'",
1636 pool = readdir(dir);
1642 /* ignore . and .. */
1643 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1646 /* check output bounds */
1647 if (nb_entries >= list_size) {
1649 goto free_dir_no_msg;
1652 /* +2 for '.' and final '\0' */
1653 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1656 goto free_dir_no_msg;
1659 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1660 poollist[nb_entries] = buffer + used;
1661 used += strlen(pool->d_name) + strlen(fsname) + 2;
1667 llapi_error(LLAPI_MSG_ERROR, rc,
1668 "Error reading pool list for '%s'", name);
1670 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1675 cfs_free_param_data(&pathname);
1678 return rc != 0 ? rc : nb_entries;
1681 /* wrapper for lfs.c and obd.c */
1682 int llapi_poollist(const char *name)
1684 /* list of pool names (assume that pool count is smaller
1686 char **list, *buffer = NULL, *fsname = (char *)name;
1687 char *poolname = NULL, *tmp = NULL, data[16];
1688 enum param_filter type = FILTER_BY_PATH;
1689 int obdcount, bufsize, rc, nb, i;
1694 if (name[0] != '/') {
1695 fsname = strdup(name);
1699 poolname = strchr(fsname, '.');
1702 type = FILTER_BY_FS_NAME;
1705 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1706 data, sizeof(data));
1709 obdcount = atoi(data);
1711 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1712 * and also an array to store the pointers for all that
1713 * allocated space. */
1715 bufsize = sizeof(struct obd_uuid) * obdcount;
1716 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1717 if (buffer == NULL) {
1721 list = (char **) (buffer + bufsize);
1724 /* name is a path or fsname */
1725 nb = llapi_get_poollist(name, list, obdcount,
1728 /* name is a pool name (<fsname>.<poolname>) */
1729 nb = llapi_get_poolmembers(name, list, obdcount,
1733 if (nb == -EOVERFLOW) {
1736 goto retry_get_pools;
1739 for (i = 0; i < nb; i++)
1740 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1741 rc = (nb < 0 ? nb : 0);
1745 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1750 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1751 void *data, struct dirent64 *de);
1753 #define OBD_NOT_FOUND (-1)
1755 static void find_param_fini(struct find_param *param)
1757 if (param->fp_migrate)
1760 if (param->fp_obd_indexes) {
1761 free(param->fp_obd_indexes);
1762 param->fp_obd_indexes = NULL;
1765 if (param->fp_lmd) {
1766 free(param->fp_lmd);
1767 param->fp_lmd = NULL;
1770 if (param->fp_lmv_md) {
1771 free(param->fp_lmv_md);
1772 param->fp_lmv_md = NULL;
1776 static int common_param_init(struct find_param *param, char *path)
1778 int lum_size = get_mds_md_size(path);
1783 /* migrate has fp_lmv_md initialized outside */
1784 if (param->fp_migrate)
1787 if (lum_size < PATH_MAX + 1)
1788 lum_size = PATH_MAX + 1;
1790 param->fp_lum_size = lum_size;
1791 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1793 if (param->fp_lmd == NULL) {
1794 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1795 "error: allocate %zu bytes for layout failed",
1796 sizeof(lstat_t) + param->fp_lum_size);
1800 param->fp_lmv_stripe_count = 256;
1801 param->fp_lmv_md = calloc(1,
1802 lmv_user_md_size(param->fp_lmv_stripe_count,
1803 LMV_USER_MAGIC_SPECIFIC));
1804 if (param->fp_lmv_md == NULL) {
1805 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1806 "error: allocation of %d bytes for ioctl",
1807 lmv_user_md_size(param->fp_lmv_stripe_count,
1808 LMV_USER_MAGIC_SPECIFIC));
1809 find_param_fini(param);
1813 param->fp_got_uuids = 0;
1814 param->fp_obd_indexes = NULL;
1815 param->fp_obd_index = OBD_NOT_FOUND;
1816 param->fp_mdt_index = OBD_NOT_FOUND;
1820 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1821 struct dirent64 *de)
1823 struct find_param *param = data;
1829 /* set errno upon failure */
1830 static DIR *opendir_parent(const char *path)
1836 path_copy = strdup(path);
1837 if (path_copy == NULL)
1840 parent_path = dirname(path_copy);
1841 parent = opendir(parent_path);
1847 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1852 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1853 if (param->fp_get_default_lmv)
1854 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1856 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1858 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1859 if (errno == E2BIG && ret != 0) {
1863 /* if foreign LMV case, fake stripes number */
1864 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
1865 struct lmv_foreign_md *lfm;
1867 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1868 if (lfm->lfm_length < XATTR_SIZE_MAX -
1869 offsetof(typeof(*lfm), lfm_value)) {
1870 uint32_t size = lfm->lfm_length +
1871 offsetof(typeof(*lfm), lfm_value);
1873 stripe_count = lmv_foreign_to_md_stripes(size);
1875 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1876 "error: invalid %d foreign size "
1877 "returned from ioctl",
1882 stripe_count = param->fp_lmv_md->lum_stripe_count;
1884 if (stripe_count <= param->fp_lmv_stripe_count)
1887 free(param->fp_lmv_md);
1888 param->fp_lmv_stripe_count = stripe_count;
1889 lmv_size = lmv_user_md_size(stripe_count,
1890 LMV_USER_MAGIC_SPECIFIC);
1891 param->fp_lmv_md = malloc(lmv_size);
1892 if (param->fp_lmv_md == NULL) {
1893 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1894 "error: allocation of %d bytes for ioctl",
1895 lmv_user_md_size(param->fp_lmv_stripe_count,
1896 LMV_USER_MAGIC_SPECIFIC));
1904 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1907 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
1908 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
1909 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
1910 lmd_v2->lmd_stx.stx_uid = st->st_uid;
1911 lmd_v2->lmd_stx.stx_gid = st->st_gid;
1912 lmd_v2->lmd_stx.stx_mode = st->st_mode;
1913 lmd_v2->lmd_stx.stx_ino = st->st_ino;
1914 lmd_v2->lmd_stx.stx_size = st->st_size;
1915 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
1916 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
1917 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
1918 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
1919 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
1920 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
1921 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
1922 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
1923 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
1925 lmd_v2->lmd_flags = 0;
1927 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1929 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
1930 if (lmd_v2->lmd_stx.stx_size)
1931 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
1932 if (lmd_v2->lmd_stx.stx_blocks)
1933 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
1935 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1936 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
1937 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
1942 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
1944 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
1945 struct lov_user_mds_data *lmd_v2 = lmdbuf;
1949 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
1953 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
1956 st = lmd_v1->lmd_st;
1957 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
1958 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
1959 convert_lmd_statx(lmd_v2, &st, false);
1960 lmd_v2->lmd_lmmsize = 0;
1961 lmd_v2->lmd_padding = 0;
1966 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
1967 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1969 struct lov_user_mds_data *lmd = lmdbuf;
1970 static bool use_old_ioctl;
1974 if (parent_fd < 0 && dir_fd < 0)
1976 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1980 /* LL_IOC_MDC_GETINFO operates on the current directory inode
1981 * and returns struct lov_user_mds_data, while
1982 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
1984 if (type == GET_LMD_INFO)
1985 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_OLD :
1988 cmd = LL_IOC_LOV_GETSTRIPE;
1991 ret = ioctl(dir_fd, cmd, lmdbuf);
1992 if (ret < 0 && errno == ENOTTY && cmd == LL_IOC_MDC_GETINFO) {
1993 cmd = LL_IOC_MDC_GETINFO_OLD;
1994 use_old_ioctl = true;
1998 if (cmd == LL_IOC_MDC_GETINFO_OLD && !ret)
1999 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2000 } else if (parent_fd >= 0) {
2001 const char *fname = strrchr(path, '/');
2003 /* IOC_MDC_GETFILEINFO takes as input the filename (relative to
2004 * the parent directory) and returns struct lov_user_mds_data,
2005 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2007 * This avoids opening, locking, and closing each file on the
2008 * client if that is not needed. Multiple of these ioctl() can
2009 * be done on the parent dir with a single open for all
2010 * files in that directory, and it also doesn't pollute the
2011 * client dcache with millions of dentries when traversing
2012 * a large filesystem.
2014 fname = (fname == NULL ? path : fname + 1);
2016 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2019 else if (ret >= lmdlen || ret++ == 0)
2022 if (type == GET_LMD_INFO)
2023 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_OLD :
2024 IOC_MDC_GETFILEINFO;
2026 cmd = IOC_MDC_GETFILESTRIPE;
2029 ret = ioctl(parent_fd, cmd, lmdbuf);
2030 if (ret < 0 && errno == ENOTTY &&
2031 cmd == IOC_MDC_GETFILEINFO) {
2032 cmd = IOC_MDC_GETFILEINFO_OLD;
2033 use_old_ioctl = true;
2034 goto retry_getfileinfo;
2037 if (cmd == IOC_MDC_GETFILEINFO_OLD && !ret)
2038 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2042 if (ret && type == GET_LMD_INFO) {
2043 if (errno == ENOTTY) {
2046 /* ioctl is not supported, it is not a lustre fs.
2047 * Do the regular lstat(2) instead.
2049 ret = lstat_f(path, &st);
2052 llapi_error(LLAPI_MSG_ERROR, ret,
2053 "error: %s: lstat failed for %s",
2057 convert_lmd_statx(lmd, &st, true);
2058 /* It may be wrong to set use_old_ioctl with true as
2059 * the file is not a lustre fs. So reset it with false
2062 use_old_ioctl = false;
2063 } else if (errno == ENOENT) {
2065 llapi_error(LLAPI_MSG_WARN, ret,
2066 "warning: %s does not exist", path);
2067 } else if (errno != EISDIR && errno != ENODATA) {
2069 llapi_error(LLAPI_MSG_ERROR, ret,
2070 "%s ioctl failed for %s.",
2071 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2072 "IOC_MDC_GETFILEINFO", path);
2079 static int get_lmd_info(char *path, DIR *parent, DIR *dir, void *lmdbuf,
2080 int lmdlen, enum get_lmd_info_type type)
2086 parent_fd = dirfd(parent);
2088 dir_fd = dirfd(dir);
2090 return get_lmd_info_fd(path, parent_fd, dir_fd, lmdbuf, lmdlen, type);
2093 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
2094 semantic_func_t sem_init,
2095 semantic_func_t sem_fini, void *data,
2096 struct dirent64 *de)
2098 struct find_param *param = (struct find_param *)data;
2099 struct dirent64 *dent;
2107 if (!d && errno != ENOTDIR) {
2109 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2112 } else if (!d && !parent) {
2113 /* ENOTDIR. Open the parent dir. */
2114 p = opendir_parent(path);
2121 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
2127 while ((dent = readdir64(d)) != NULL) {
2130 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2134 if ((len + dent->d_reclen + 2) > size) {
2135 llapi_err_noerrno(LLAPI_MSG_ERROR,
2136 "error: %s: string buffer too small",
2141 strcat(path, dent->d_name);
2143 if (dent->d_type == DT_UNKNOWN) {
2144 struct lov_user_mds_data *lmd = param->fp_lmd;
2146 rc = get_lmd_info(path, d, NULL, lmd,
2147 param->fp_lum_size, GET_LMD_INFO);
2149 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2156 switch (dent->d_type) {
2158 llapi_err_noerrno(LLAPI_MSG_ERROR,
2159 "error: %s: '%s' is UNKNOWN type %d",
2160 __func__, dent->d_name, dent->d_type);
2163 rc = llapi_semantic_traverse(path, size, d, sem_init,
2164 sem_fini, data, dent);
2165 if (rc != 0 && ret == 0)
2171 rc = sem_init(path, d, NULL, data, dent);
2172 if (rc < 0 && ret == 0) {
2177 if (sem_fini && rc == 0)
2178 sem_fini(path, d, NULL, data, dent);
2186 sem_fini(path, parent, &d, data, de);
2195 static int param_callback(char *path, semantic_func_t sem_init,
2196 semantic_func_t sem_fini, struct find_param *param)
2198 int ret, len = strlen(path);
2201 if (len > PATH_MAX) {
2203 llapi_error(LLAPI_MSG_ERROR, ret,
2204 "Path name '%s' is too long", path);
2208 buf = (char *)malloc(PATH_MAX + 1);
2212 snprintf(buf, PATH_MAX + 1, "%s", path);
2213 ret = common_param_init(param, buf);
2217 param->fp_depth = 0;
2219 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
2220 sem_fini, param, NULL);
2222 find_param_fini(param);
2224 return ret < 0 ? ret : 0;
2227 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2229 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
2233 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2238 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2240 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2243 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2248 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2252 fd = open(path, O_RDONLY | O_NONBLOCK);
2255 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
2259 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2265 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2269 fd = open(path, O_RDONLY | O_NONBLOCK);
2272 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2276 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2288 * If uuidp is NULL, return the number of available obd uuids.
2289 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2290 * there are more OSTs than allocated to uuidp, then an error is returned with
2291 * the ost_count set to number of available obd uuids.
2293 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2294 int *ost_count, enum tgt_type type)
2296 char buf[PATH_MAX], format[32];
2297 int rc = 0, index = 0;
2298 struct obd_uuid name;
2302 /* Get the lov name */
2303 if (type == LOV_TYPE)
2304 rc = llapi_file_fget_lov_uuid(fd, &name);
2306 rc = llapi_file_fget_lmv_uuid(fd, &name);
2310 /* Now get the ost uuids */
2311 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2312 FILTER_BY_EXACT, "target_obd", ¶m);
2316 fp = fopen(param.gl_pathv[0], "r");
2319 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2324 snprintf(format, sizeof(format),
2325 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2326 while (fgets(buf, sizeof(buf), fp) != NULL) {
2327 if (uuidp && (index < *ost_count)) {
2328 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2336 if (uuidp && (index > *ost_count))
2341 cfs_free_param_data(¶m);
2345 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2347 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2350 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2355 root = open(mnt, O_RDONLY | O_DIRECTORY);
2358 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2363 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2371 /* Check if user specified value matches a real uuid. Ignore _UUID,
2372 * -osc-4ba41334, other trailing gunk in comparison.
2373 * @param real_uuid ends in "_UUID"
2374 * @param search_uuid may or may not end in "_UUID"
2376 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2378 int cmplen = strlen(real_uuid);
2379 int searchlen = strlen(search_uuid);
2381 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2383 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2386 /* The UUIDs may legitimately be different lengths, if
2387 * the system was upgraded from an older version. */
2388 if (cmplen != searchlen)
2391 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2394 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2395 * returned in param->fp_obd_index */
2396 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2398 struct obd_uuid obd_uuid;
2405 if (param->fp_got_uuids)
2408 /* Get the lov/lmv name */
2409 if (param->fp_get_lmv)
2410 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2412 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2414 if (rc != -ENOTTY) {
2415 llapi_error(LLAPI_MSG_ERROR, rc,
2416 "error: can't get %s name: %s",
2417 param->fp_get_lmv ? "lmv" : "lov",
2425 param->fp_got_uuids = 1;
2427 /* Now get the ost uuids */
2428 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2429 obd_uuid.uuid, FILTER_BY_EXACT,
2430 "target_obd", ¶m_data);
2434 fp = fopen(param_data.gl_pathv[0], "r");
2437 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2438 param_data.gl_pathv[0]);
2442 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2443 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2444 param->fp_get_lmv ? "MDTS" : "OBDS");
2446 snprintf(format, sizeof(format),
2447 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2448 while (fgets(buf, sizeof(buf), fp) != NULL) {
2451 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2454 if (param->fp_obd_uuid) {
2455 if (llapi_uuid_match(obd_uuid.uuid,
2456 param->fp_obd_uuid->uuid)) {
2457 param->fp_obd_index = index;
2460 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2461 /* Print everything */
2462 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2465 param->fp_obds_printed = 1;
2469 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2470 llapi_err_noerrno(LLAPI_MSG_ERROR,
2471 "error: %s: unknown obduuid: %s",
2472 __func__, param->fp_obd_uuid->uuid);
2476 cfs_free_param_data(¶m_data);
2480 /* In this case, param->fp_obd_uuid will be an array of obduuids and
2481 * obd index for all these obduuids will be returned in
2482 * param->fp_obd_indexes */
2483 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2484 int num_obds, int **obdindexes, int *obdindex,
2487 int ret, obdcount, obd_valid = 0, obdnum;
2489 struct obd_uuid *uuids = NULL;
2493 if (type == LOV_TYPE)
2494 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2496 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2500 obdcount = atoi(buf);
2501 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2506 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2508 if (ret == -EOVERFLOW) {
2509 struct obd_uuid *uuids_temp;
2511 uuids_temp = realloc(uuids, obdcount *
2512 sizeof(struct obd_uuid));
2513 if (uuids_temp != NULL) {
2515 goto retry_get_uuids;
2520 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2524 indexes = malloc(num_obds * sizeof(*obdindex));
2525 if (indexes == NULL) {
2530 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2533 /* The user may have specified a simple index */
2534 i = strtol(obduuids[obdnum].uuid, &end, 0);
2535 if (end && *end == '\0' && i < obdcount) {
2536 indexes[obdnum] = i;
2539 for (i = 0; i < obdcount; i++) {
2540 if (llapi_uuid_match(uuids[i].uuid,
2541 obduuids[obdnum].uuid)) {
2542 indexes[obdnum] = i;
2548 if (i >= obdcount) {
2549 indexes[obdnum] = OBD_NOT_FOUND;
2550 llapi_err_noerrno(LLAPI_MSG_ERROR,
2551 "invalid obduuid '%s'",
2552 obduuids[obdnum].uuid);
2558 *obdindex = OBD_NOT_FOUND;
2560 *obdindex = obd_valid;
2562 *obdindexes = indexes;
2570 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2574 if (param->fp_mdt_uuid) {
2575 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2577 ¶m->fp_mdt_indexes,
2578 ¶m->fp_mdt_index, LMV_TYPE);
2583 if (param->fp_obd_uuid) {
2584 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2586 ¶m->fp_obd_indexes,
2587 ¶m->fp_obd_index, LOV_TYPE);
2592 param->fp_got_uuids = 1;
2597 int llapi_ostlist(char *path, struct find_param *param)
2602 fd = open(path, O_RDONLY | O_DIRECTORY);
2606 ret = setup_obd_uuid(fd, path, param);
2613 * Tries to determine the default stripe attributes for a given filesystem. The
2614 * filesystem to check should be specified by fsname, or will be determined
2617 static int sattr_get_defaults(const char *const fsname,
2618 unsigned int *scount,
2619 unsigned int *ssize,
2620 unsigned int *soffset)
2626 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2627 "stripecount", val, sizeof(val));
2630 *scount = atoi(val);
2634 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2635 "stripesize", val, sizeof(val));
2642 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2643 "stripeoffset", val, sizeof(val));
2646 *soffset = atoi(val);
2653 * Tries to gather the default stripe attributes for a given filesystem. If
2654 * the attributes can be determined, they are cached for easy retreival the
2655 * next time they are needed. Only a single filesystem's attributes are
2658 int sattr_cache_get_defaults(const char *const fsname,
2659 const char *const pathname, unsigned int *scount,
2660 unsigned int *ssize, unsigned int *soffset)
2663 char fsname[PATH_MAX + 1];
2664 unsigned int stripecount;
2665 unsigned int stripesize;
2666 unsigned int stripeoffset;
2672 char fsname_buf[PATH_MAX + 1];
2673 unsigned int tmp[3];
2675 if (fsname == NULL) {
2676 rc = llapi_search_fsname(pathname, fsname_buf);
2680 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2683 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2685 * Ensure all 3 sattrs (count, size, and offset) are
2686 * successfully retrieved and stored in tmp before writing to
2689 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2693 cache.stripecount = tmp[0];
2694 cache.stripesize = tmp[1];
2695 cache.stripeoffset = tmp[2];
2696 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2700 *scount = cache.stripecount;
2702 *ssize = cache.stripesize;
2704 *soffset = cache.stripeoffset;
2709 static char *layout2name(__u32 layout_pattern)
2711 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2713 else if (layout_pattern == LOV_PATTERN_MDT)
2715 else if (layout_pattern == LOV_PATTERN_RAID0)
2717 else if (layout_pattern ==
2718 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2719 return "raid0,overstriped";
2724 enum lov_dump_flags {
2725 LDF_IS_DIR = 0x0001,
2726 LDF_IS_RAW = 0x0002,
2727 LDF_INDENT = 0x0004,
2728 LDF_SKIP_OBJS = 0x0008,
2730 LDF_EXTENSION = 0x0020,
2733 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2734 struct lov_user_ost_data_v1 *objects,
2735 enum llapi_layout_verbose verbose,
2736 int depth, char *pool_name,
2737 enum lov_dump_flags flags)
2739 bool is_dir = flags & LDF_IS_DIR;
2740 bool is_raw = flags & LDF_IS_RAW;
2741 bool indent = flags & LDF_INDENT;
2742 bool yaml = flags & LDF_YAML;
2743 bool skip_objs = flags & LDF_SKIP_OBJS;
2744 bool extension = flags & LDF_EXTENSION;
2745 char *prefix = is_dir ? "" : "lmm_";
2746 char *separator = "";
2747 char *space = indent ? " " : "";
2750 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2751 lmm_oi_set_seq(&lum->lmm_oi, 0);
2752 if (!indent && (verbose & VERBOSE_DETAIL))
2753 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2756 if (!yaml && !indent && depth && path &&
2757 ((verbose != VERBOSE_OBJID) || !is_dir))
2758 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2760 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2761 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2762 space, prefix, lum->lmm_magic);
2763 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2765 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2766 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2768 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2770 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2775 if (verbose & ~VERBOSE_DFID)
2776 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2778 /* This needs a bit of hand-holding since old 1.x lmm_oi
2779 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2780 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2781 * a real FID. Ideally the 2.x code would have stored this
2782 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2783 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2784 * worked properly (especially since IGIF FIDs use mds_inum as
2785 * the FID SEQ), but unfortunately that didn't happen.
2787 * Print it to look like an IGIF FID, even though the fields
2788 * are reversed on disk, so that it makes sense to userspace.
2790 * Don't use ostid_id() and ostid_seq(), since they assume the
2791 * oi_fid fields are in the right order. This is why there are
2792 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2794 * For newer layout types hopefully this will be a real FID. */
2795 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2796 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2797 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2798 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2799 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2801 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2802 (unsigned long long)seq, oid, ver);
2804 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2805 (unsigned long long)seq, oid, ver);
2808 if (verbose & VERBOSE_STRIPE_COUNT) {
2809 if (verbose & ~VERBOSE_STRIPE_COUNT)
2810 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2813 if (!is_raw && lum->lmm_stripe_count == 0 &&
2814 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2815 unsigned int scount;
2816 rc = sattr_cache_get_defaults(NULL, path,
2820 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2823 llapi_error(LLAPI_MSG_ERROR, rc,
2824 "Cannot determine default"
2827 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2829 (__s16)lum->lmm_stripe_count);
2832 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2834 (__s16)lum->lmm_stripe_count);
2836 if (!yaml && is_dir)
2842 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
2843 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
2844 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2845 if (verbose & ~VERBOSE_EXT_SIZE && extension)
2846 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
2848 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
2849 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2851 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2853 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2856 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2858 llapi_error(LLAPI_MSG_ERROR, rc,
2859 "Cannot determine default"
2862 /* Extension size is in KiB */
2863 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2865 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
2866 (unsigned long long)lum->lmm_stripe_size);
2868 if (!yaml && is_dir)
2874 if ((verbose & VERBOSE_PATTERN)) {
2875 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2876 if (verbose & ~VERBOSE_PATTERN)
2877 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2879 if (lov_pattern_supported(lum->lmm_pattern))
2880 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2881 layout2name(lum->lmm_pattern));
2883 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2884 separator = (!yaml && is_dir) ? " " : "\n";
2887 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2888 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2889 if (verbose & ~VERBOSE_GENERATION)
2890 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2892 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2893 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2897 if (verbose & VERBOSE_STRIPE_OFFSET) {
2898 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2899 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2900 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2902 if (is_dir || skip_objs)
2903 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2904 lum->lmm_stripe_offset ==
2905 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2906 lum->lmm_stripe_offset);
2907 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
2908 llapi_printf(LLAPI_MSG_NORMAL, "0");
2910 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2911 objects[0].l_ost_idx);
2912 if (!yaml && is_dir)
2918 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2919 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2920 if (verbose & ~VERBOSE_POOL)
2921 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2923 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2924 if (!yaml && is_dir)
2930 if (strlen(separator) != 0)
2931 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2934 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2935 struct lov_user_ost_data_v1 *objects,
2936 char *path, int obdindex, int depth,
2937 enum llapi_layout_verbose verbose,
2938 enum lov_dump_flags flags)
2940 bool is_dir = flags & LDF_IS_DIR;
2941 bool indent = flags & LDF_INDENT;
2942 bool skip_objs = flags & LDF_SKIP_OBJS;
2943 bool yaml = flags & LDF_YAML;
2944 bool obdstripe = obdindex == OBD_NOT_FOUND;
2947 if (!obdstripe && !skip_objs) {
2948 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2949 if (obdindex == objects[i].l_ost_idx) {
2959 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2962 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
2963 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2964 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
2965 char *space = " - ";
2968 llapi_printf(LLAPI_MSG_NORMAL,
2969 "%6slmm_objects:\n", " ");
2971 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2973 llapi_printf(LLAPI_MSG_NORMAL,
2974 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2976 for (i = 0; i < lum->lmm_stripe_count; i++) {
2977 int idx = objects[i].l_ost_idx;
2978 long long oid = ostid_id(&objects[i].l_ost_oi);
2979 long long gr = ostid_seq(&objects[i].l_ost_oi);
2981 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2985 struct lu_fid fid = { 0 };
2987 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2988 llapi_printf(LLAPI_MSG_NORMAL,
2989 "%sl_ost_idx: %d\n", space, idx);
2990 llapi_printf(LLAPI_MSG_NORMAL,
2991 "%8sl_fid: "DFID_NOBRACE"\n",
2993 } else if (indent) {
2994 struct lu_fid fid = { 0 };
2996 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2997 llapi_printf(LLAPI_MSG_NORMAL,
2998 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
2999 space, i, idx, PFID(&fid));
3002 sprintf(fmt, "%s%s%s\n",
3003 "\t%6u\t%14llu\t%#13llx\t",
3004 (fid_seq_is_rsvd(gr) ||
3005 fid_seq_is_mdt0(gr)) ?
3006 "%14llu" : "%#14llx", "%s");
3007 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
3009 obdindex == idx ? " *" : "");
3013 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3016 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3017 char *path, int obdindex, int depth,
3018 enum llapi_layout_verbose verbose,
3019 enum lov_dump_flags flags)
3021 struct lmv_user_mds_data *objects = lum->lum_objects;
3022 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3023 char *separator = "";
3024 bool yaml = flags & LDF_YAML;
3025 bool obdstripe = false;
3028 if (obdindex != OBD_NOT_FOUND) {
3029 if (lum->lum_stripe_count == 0) {
3030 if (obdindex == lum->lum_stripe_offset)
3033 for (i = 0; i < lum->lum_stripe_count; i++) {
3034 if (obdindex == objects[i].lum_mds) {
3035 llapi_printf(LLAPI_MSG_NORMAL,
3050 /* show all information default */
3052 if (lum->lum_magic == LMV_USER_MAGIC)
3053 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3054 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3056 verbose = VERBOSE_OBJID;
3059 if (depth && path && ((verbose != VERBOSE_OBJID)))
3060 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3062 if (verbose & VERBOSE_STRIPE_COUNT) {
3063 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3064 if (verbose & ~VERBOSE_STRIPE_COUNT)
3065 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3066 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3067 (int)lum->lum_stripe_count);
3068 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3074 if (verbose & VERBOSE_STRIPE_OFFSET) {
3075 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3076 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3077 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3078 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3079 (int)lum->lum_stripe_offset);
3080 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3086 if (verbose & VERBOSE_HASH_TYPE) {
3087 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3088 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3090 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3091 if (verbose & ~VERBOSE_HASH_TYPE)
3092 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3093 if (type < LMV_HASH_TYPE_MAX)
3094 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3095 mdt_hash_name[type]);
3097 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3099 if (flags & LMV_HASH_FLAG_MIGRATION)
3100 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3101 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3102 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3103 if (flags & LMV_HASH_FLAG_LOST_LMV)
3104 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3109 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3110 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3111 if (lum->lum_stripe_count > 0)
3112 llapi_printf(LLAPI_MSG_NORMAL,
3113 "mdtidx\t\t FID[seq:oid:ver]\n");
3114 for (i = 0; i < lum->lum_stripe_count; i++) {
3115 int idx = objects[i].lum_mds;
3116 struct lu_fid *fid = &objects[i].lum_fid;
3117 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3118 llapi_printf(LLAPI_MSG_NORMAL,
3119 "%6u\t\t "DFID"\t\t%s\n",
3121 obdindex == idx ? " *" : "");
3126 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3127 pool_name[0] != '\0') {
3128 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3129 if (verbose & ~VERBOSE_POOL)
3130 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3132 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3136 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3137 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3140 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3141 enum lov_dump_flags flags)
3143 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3144 int depth = param->fp_max_depth;
3145 enum llapi_layout_verbose verbose = param->fp_verbose;
3146 bool yaml = flags & LDF_YAML;
3148 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3149 !(flags & LDF_IS_DIR)) && !yaml)
3150 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3152 if (verbose & VERBOSE_DETAIL) {
3153 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3154 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3155 " ", comp_v1->lcm_magic);
3156 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3157 " ", comp_v1->lcm_size);
3158 if (flags & LDF_IS_DIR)
3159 llapi_printf(LLAPI_MSG_NORMAL,
3160 "%2slcm_flags: %s\n", " ",
3161 comp_v1->lcm_mirror_count > 0 ?
3164 llapi_printf(LLAPI_MSG_NORMAL,
3165 "%2slcm_flags: %s\n", " ",
3166 llapi_layout_flags_string(comp_v1->lcm_flags));
3169 if (verbose & VERBOSE_GENERATION) {
3170 if (verbose & ~VERBOSE_GENERATION)
3171 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3173 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3176 if (verbose & VERBOSE_MIRROR_COUNT) {
3177 if (verbose & ~VERBOSE_MIRROR_COUNT)
3178 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3180 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3181 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3182 comp_v1->lcm_mirror_count + 1 : 1);
3185 if (verbose & VERBOSE_COMP_COUNT) {
3186 if (verbose & ~VERBOSE_COMP_COUNT)
3187 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3189 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3190 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3191 comp_v1->lcm_entry_count : 0);
3194 if (verbose & VERBOSE_DETAIL && !yaml)
3195 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3198 static void lcme_flags2str(__u32 comp_flags)
3204 llapi_printf(LLAPI_MSG_NORMAL, "0");
3207 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3208 if (comp_flags & comp_flags_table[i].cfn_flag) {
3210 llapi_printf(LLAPI_MSG_NORMAL, ",");
3211 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3212 comp_flags_table[i].cfn_name);
3213 comp_flags &= ~comp_flags_table[i].cfn_flag;
3219 llapi_printf(LLAPI_MSG_NORMAL, ",");
3220 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3224 static void lov_dump_comp_v1_entry(struct find_param *param,
3225 enum lov_dump_flags flags, int index)
3227 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3228 struct lov_comp_md_entry_v1 *entry;
3229 char *separator = "";
3230 enum llapi_layout_verbose verbose = param->fp_verbose;
3231 bool yaml = flags & LDF_YAML;
3233 entry = &comp_v1->lcm_entries[index];
3236 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3238 if (verbose & VERBOSE_COMP_ID) {
3239 if (verbose & VERBOSE_DETAIL && !yaml)
3240 llapi_printf(LLAPI_MSG_NORMAL,
3241 "%slcme_id: ", " - ");
3242 else if (verbose & ~VERBOSE_COMP_ID)
3243 llapi_printf(LLAPI_MSG_NORMAL,
3244 "%4slcme_id: ", " ");
3245 if (entry->lcme_id != LCME_ID_INVAL)
3246 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3248 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3252 if (verbose & VERBOSE_MIRROR_ID) {
3253 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3254 if (verbose & ~VERBOSE_MIRROR_ID)
3255 llapi_printf(LLAPI_MSG_NORMAL,
3256 "%4slcme_mirror_id: ", " ");
3257 if (entry->lcme_id != LCME_ID_INVAL)
3258 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3259 mirror_id_of(entry->lcme_id));
3261 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3265 if (verbose & VERBOSE_COMP_FLAGS) {
3266 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3267 if (verbose & ~VERBOSE_COMP_FLAGS)
3268 llapi_printf(LLAPI_MSG_NORMAL,
3269 "%4slcme_flags: ", " ");
3270 lcme_flags2str(entry->lcme_flags);
3273 /* print snapshot timestamp if its a nosync comp */
3274 if ((verbose & VERBOSE_COMP_FLAGS) &&
3275 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3276 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3277 if (verbose & ~VERBOSE_COMP_FLAGS)
3278 llapi_printf(LLAPI_MSG_NORMAL,
3279 "%4slcme_timestamp: ", " ");
3281 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3282 (unsigned long long)entry->lcme_timestamp);
3284 time_t stamp = entry->lcme_timestamp;
3285 char *date_str = asctime(localtime(&stamp));
3287 date_str[strlen(date_str) - 1] = '\0';
3288 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3294 if (verbose & VERBOSE_COMP_START) {
3295 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3296 if (verbose & ~VERBOSE_COMP_START)
3297 llapi_printf(LLAPI_MSG_NORMAL,
3298 "%4slcme_extent.e_start: ", " ");
3299 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3300 (unsigned long long)entry->lcme_extent.e_start);
3304 if (verbose & VERBOSE_COMP_END) {
3305 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3306 if (verbose & ~VERBOSE_COMP_END)
3307 llapi_printf(LLAPI_MSG_NORMAL,
3308 "%4slcme_extent.e_end: ", " ");
3309 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3310 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3312 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3313 (unsigned long long)entry->lcme_extent.e_end);
3318 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3319 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3320 } else if (verbose & VERBOSE_DETAIL) {
3321 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3322 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3323 " ", entry->lcme_offset);
3324 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3325 " ", entry->lcme_size);
3326 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3328 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3332 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3333 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3335 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3336 * The table below gives the answers for the specified parameters (value and
3337 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3338 * --------------------------------------
3339 * 1 | file > limit; sign > 0 | -1 / -1 |
3340 * 2 | file = limit; sign > 0 | -1 / -1 |
3341 * 3 | file < limit; sign > 0 | ? / 1 |
3342 * 4 | file > limit; sign = 0 | -1 / -1 |
3343 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3344 * 6 | file < limit; sign = 0 | ? / -1 |
3345 * 7 | file > limit; sign < 0 | 1 / 1 |
3346 * 8 | file = limit; sign < 0 | ? / -1 |
3347 * 9 | file < limit; sign < 0 | ? / -1 |
3348 * --------------------------------------
3349 * Note: 5th actually means that the value is within the interval
3350 * (limit - margin, limit]. */
3351 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3352 int sign, int negopt, unsigned long long margin,
3358 /* Drop the fraction of margin (of days or size). */
3359 if (file + margin <= limit)
3361 } else if (sign == 0) {
3362 if (file <= limit && file + margin > limit)
3364 else if (file + margin <= limit)
3366 } else if (sign < 0) {
3373 return negopt ? ~ret + 1 : ret;
3376 static inline struct lov_user_md *
3377 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3379 return (struct lov_user_md *)((char *)comp_v1 +
3380 comp_v1->lcm_entries[ent_idx].lcme_offset);
3383 static inline struct lov_user_ost_data_v1 *
3384 lov_v1v3_objects(struct lov_user_md *v1)
3386 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3387 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3389 return v1->lmm_objects;
3393 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3395 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3396 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3397 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3399 pool_name[0] = '\0';
3403 print_last_init_comp(struct find_param *param)
3405 /* print all component info */
3406 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3409 /* print specific component info */
3410 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3411 param->fp_check_comp_start || param->fp_check_comp_end ||
3412 param->fp_check_mirror_id || param->fp_check_mirror_index)
3418 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3422 if (param->fp_comp_end == LUSTRE_EOF) {
3423 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3424 match = end == LUSTRE_EOF ? 1 : -1;
3425 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3426 match = end == LUSTRE_EOF ? -1 : 1;
3427 else /* at least EOF */
3429 if (param->fp_exclude_comp_end)
3432 unsigned long long margin;
3434 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3435 match = find_value_cmp(end, param->fp_comp_end,
3436 param->fp_comp_end_sign,
3437 param->fp_exclude_comp_end, margin, 0);
3444 * An example of "getstripe -v" for a two components PFL file:
3447 * lcm_magic: 0x0BD60BD0
3451 * lcm_entry_count: 2
3455 * lcme_extent.e_start: 0
3456 * lcme_extent.e_end: 1048576
3460 * lmm_magic: 0x0BD10BD0
3461 * lmm_seq: 0x200000401
3462 * lmm_object_id: 0x1
3463 * lmm_fid: [0x200000401:0x1:0x0]
3464 * lmm_stripe_count: 1
3465 * lmm_stripe_size: 1048576
3466 * lmm_pattern: raid0
3468 * lmm_stripe_offset: 0
3470 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3474 * lcme_extent.e_start: 1048576
3475 * lcme_extent.e_end: EOF
3479 * lmm_magic: 0x0BD10BD0
3480 * lmm_seq: 0x200000401
3481 * lmm_object_id: 0x1
3482 * lmm_fid: [0x200000401:0x1:0x0]
3483 * lmm_stripe_count: 2
3484 * lmm_stripe_size: 1048576
3485 * lmm_pattern: raid0
3487 * lmm_stripe_offset: 1
3489 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3490 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3492 static void lov_dump_comp_v1(struct find_param *param, char *path,
3493 enum lov_dump_flags flags)
3495 struct lov_comp_md_entry_v1 *entry;
3496 struct lov_user_ost_data_v1 *objects;
3497 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3498 struct lov_user_md_v1 *v1;
3499 char pool_name[LOV_MAXPOOLNAME + 1];
3500 int obdindex = param->fp_obd_index;
3501 int i, j, match, ext;
3502 bool obdstripe = false;
3503 __u16 mirror_index = 0;
3504 __u16 mirror_id = 0;
3506 if (obdindex != OBD_NOT_FOUND) {
3507 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3508 i < comp_v1->lcm_entry_count; i++) {
3509 if (!(comp_v1->lcm_entries[i].lcme_flags &
3513 v1 = lov_comp_entry(comp_v1, i);
3514 objects = lov_v1v3_objects(v1);
3516 for (j = 0; j < v1->lmm_stripe_count; j++) {
3517 if (obdindex == objects[j].l_ost_idx) {
3530 lov_dump_comp_v1_header(param, path, flags);
3532 flags |= LDF_INDENT;
3534 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3535 entry = &comp_v1->lcm_entries[i];
3537 if (param->fp_check_comp_flags) {
3538 if (((param->fp_comp_flags & entry->lcme_flags) !=
3539 param->fp_comp_flags) ||
3540 (param->fp_comp_neg_flags & entry->lcme_flags))
3544 if (param->fp_check_comp_id &&
3545 param->fp_comp_id != entry->lcme_id)
3548 if (param->fp_check_comp_start) {
3549 match = find_value_cmp(entry->lcme_extent.e_start,
3550 param->fp_comp_start,
3551 param->fp_comp_start_sign,
3553 param->fp_comp_start_units, 0);
3558 if (param->fp_check_comp_end) {
3559 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3565 if (param->fp_check_mirror_index) {
3566 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3568 mirror_id = mirror_id_of(entry->lcme_id);
3571 match = find_value_cmp(mirror_index,
3572 param->fp_mirror_index,
3573 param->fp_mirror_index_sign,
3574 param->fp_exclude_mirror_index,
3578 } else if (param->fp_check_mirror_id) {
3579 if (mirror_id != mirror_id_of(entry->lcme_id))
3580 mirror_id = mirror_id_of(entry->lcme_id);
3582 match = find_value_cmp(mirror_id,
3583 param->fp_mirror_id,
3584 param->fp_mirror_id_sign,
3585 param->fp_exclude_mirror_id,
3591 if (print_last_init_comp(param)) {
3593 * if part of stripe info is needed, we'd print only
3594 * the last instantiated component info.
3596 if (entry->lcme_flags & LCME_FL_INIT)
3599 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3600 if (entry->lcme_flags & LCME_FL_EXTENSION)
3601 /* moved back below */
3609 if (entry->lcme_flags & LCME_FL_INIT) {
3610 if (obdindex != OBD_NOT_FOUND) {
3611 flags |= LDF_SKIP_OBJS;
3612 v1 = lov_comp_entry(comp_v1, i);
3613 objects = lov_v1v3_objects(v1);
3615 for (j = 0; j < v1->lmm_stripe_count; j++) {
3616 if (obdindex == objects[j].l_ost_idx) {
3617 flags &= ~LDF_SKIP_OBJS;
3622 flags &= ~LDF_SKIP_OBJS;
3625 flags |= LDF_SKIP_OBJS;
3628 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3630 lov_dump_comp_v1_entry(param, flags, i);
3632 v1 = lov_comp_entry(comp_v1, i);
3633 objects = lov_v1v3_objects(v1);
3634 lov_v1v3_pool_name(v1, pool_name);
3636 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3637 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3638 param->fp_max_depth, param->fp_verbose,
3641 if (print_last_init_comp(param)) {
3643 * directory layout contains only layout template, print the
3647 i = comp_v1->lcm_entry_count - 1;
3650 flags &= ~LDF_SKIP_OBJS;
3652 lov_dump_comp_v1_entry(param, flags, i);
3654 v1 = lov_comp_entry(comp_v1, i);
3655 objects = lov_v1v3_objects(v1);
3656 lov_v1v3_pool_name(v1, pool_name);
3658 entry = &comp_v1->lcm_entries[i];
3659 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3660 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3661 param->fp_max_depth, param->fp_verbose,
3666 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3667 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3670 static inline bool has_any_comp_options(struct find_param *param)
3672 enum llapi_layout_verbose verbose = param->fp_verbose;
3674 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3675 param->fp_check_comp_start || param->fp_check_comp_end ||
3676 param->fp_check_comp_flags)
3679 /* show full layout information, not component specific */
3680 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3683 return verbose & VERBOSE_COMP_OPTS;
3686 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3689 struct lov_user_md *lum = &orig->lmd_lmm;
3690 struct lov_user_mds_data *new;
3691 struct lov_comp_md_v1 *comp_v1;
3692 struct lov_comp_md_entry_v1 *ent;
3693 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3694 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3697 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3699 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3703 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3704 + sizeof(new->lmd_lmmsize));
3706 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3707 comp_v1->lcm_magic = lum->lmm_magic;
3708 comp_v1->lcm_size = lum_off + lum_size;
3709 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3710 comp_v1->lcm_flags = 0;
3711 comp_v1->lcm_entry_count = 1;
3713 ent = &comp_v1->lcm_entries[0];
3715 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3716 ent->lcme_extent.e_start = 0;
3717 ent->lcme_extent.e_end = LUSTRE_EOF;
3718 ent->lcme_offset = lum_off;
3719 ent->lcme_size = lum_size;
3721 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3726 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3727 enum lov_dump_flags flags)
3729 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3731 if (has_any_comp_options(param)) {
3732 struct lov_user_mds_data *new_lmd, *orig_lmd;
3734 orig_lmd = param->fp_lmd;
3735 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3736 if (new_lmd != NULL) {
3737 param->fp_lmd = new_lmd;
3738 lov_dump_comp_v1(param, path, flags);
3739 param->fp_lmd = orig_lmd;
3745 if (magic == LOV_USER_MAGIC_V1) {
3746 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3747 param->fp_lmd->lmd_lmm.lmm_objects,
3748 path, param->fp_obd_index,
3749 param->fp_max_depth, param->fp_verbose,
3752 char pool_name[LOV_MAXPOOLNAME + 1];
3753 struct lov_user_ost_data_v1 *objects;
3754 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3756 snprintf(pool_name, sizeof(pool_name), "%s",
3757 lmmv3->lmm_pool_name);
3758 objects = lmmv3->lmm_objects;
3759 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3760 objects, path, param->fp_obd_index,
3761 param->fp_max_depth, param->fp_verbose,
3766 static uint32_t check_foreign_type(uint32_t foreign_type)
3770 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3771 if (lu_foreign_types[i].lft_name == NULL)
3773 if (foreign_type == lu_foreign_types[i].lft_type)
3777 return LU_FOREIGN_TYPE_UNKNOWN;
3780 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3781 enum lov_dump_flags flags)
3783 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3784 bool yaml = flags & LDF_YAML;
3786 if (!yaml && param->fp_depth && path)
3787 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3789 if (param->fp_verbose & VERBOSE_DETAIL) {
3790 uint32_t type = check_foreign_type(lfm->lfm_type);
3792 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3794 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3796 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3798 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3799 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3800 lu_foreign_types[type].lft_name);
3802 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3804 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3807 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3808 lfm->lfm_length, lfm->lfm_value);
3809 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3812 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3813 enum lov_dump_flags flags)
3815 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3816 bool yaml = flags & LDF_YAML;
3818 if (!yaml && param->fp_depth && path)
3819 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3821 if (param->fp_verbose & VERBOSE_DETAIL) {
3822 uint32_t type = check_foreign_type(lfm->lfm_type);
3824 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3826 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3828 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3830 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3831 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3832 lu_foreign_types[type].lft_name);
3834 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3836 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3839 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3840 lfm->lfm_length, lfm->lfm_value);
3841 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3844 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3845 enum lov_dump_flags flags)
3849 if (param->fp_get_lmv || param->fp_get_default_lmv)
3850 magic = (__u32)param->fp_lmv_md->lum_magic;
3852 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3855 flags |= LDF_IS_RAW;
3860 case LOV_USER_MAGIC_V1:
3861 case LOV_USER_MAGIC_V3:
3862 case LOV_USER_MAGIC_SPECIFIC:
3863 lov_dump_plain_user_lmm(param, path, flags);
3865 case LOV_USER_MAGIC_FOREIGN:
3866 lov_dump_foreign_lmm(param, path, flags);
3869 case LMV_USER_MAGIC: {
3870 char pool_name[LOV_MAXPOOLNAME + 1];
3871 struct lmv_user_md *lum;
3873 lum = (struct lmv_user_md *)param->fp_lmv_md;
3874 snprintf(pool_name, sizeof(pool_name), "%s",
3875 lum->lum_pool_name);
3876 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3877 param->fp_max_depth, param->fp_verbose,
3881 case LOV_USER_MAGIC_COMP_V1:
3882 lov_dump_comp_v1(param, path, flags);
3884 case LMV_MAGIC_FOREIGN:
3885 lmv_dump_foreign_lmm(param, path, flags);
3888 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
3889 "(expecting one of %#x %#x %#x %#x)\n",
3890 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3891 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3892 LMV_USER_MAGIC, LMV_MAGIC_V1);
3897 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3903 fname = strrchr(path, '/');
3905 /* It should be a file (or other non-directory) */
3906 if (fname == NULL) {
3907 dname = (char *)malloc(2);
3911 fname = (char *)path;
3913 dname = (char *)malloc(fname - path + 1);
3916 strncpy(dname, path, fname - path);
3917 dname[fname - path] = '\0';
3921 fd = open(dname, O_RDONLY | O_NONBLOCK);
3927 strcpy((char *)lum, fname);
3928 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3931 if (close(fd) == -1 && rc == 0)
3939 int llapi_file_lookup(int dirfd, const char *name)
3941 struct obd_ioctl_data data = { 0 };
3946 if (dirfd < 0 || name == NULL)
3949 data.ioc_version = OBD_IOCTL_VERSION;
3950 data.ioc_len = sizeof(data);
3951 data.ioc_inlbuf1 = (char *)name;
3952 data.ioc_inllen1 = strlen(name) + 1;
3954 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3956 llapi_error(LLAPI_MSG_ERROR, rc,
3957 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3962 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
3968 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
3969 * Return -1 or 1 if file timestamp does not or does match the given criteria
3970 * correspondingly. Return 0 if the MDS time is being checked and there are
3971 * attributes on OSTs and it is not yet clear if the timespamp matches.
3973 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
3974 * updated timestamps. */
3975 static int find_time_check(struct find_param *param, int mds)
3977 struct lov_user_mds_data *lmd = param->fp_lmd;
3981 /* Check if file is accepted. */
3982 if (param->fp_atime) {
3983 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
3984 param->fp_atime, param->fp_asign,
3985 param->fp_exclude_atime,
3986 param->fp_time_margin, mds);
3992 if (param->fp_mtime) {
3993 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
3994 param->fp_mtime, param->fp_msign,
3995 param->fp_exclude_mtime,
3996 param->fp_time_margin, mds);
4000 /* If the previous check matches, but this one is not yet clear,
4001 * we should return 0 to do an RPC on OSTs. */
4006 if (param->fp_ctime) {
4007 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4008 param->fp_ctime, param->fp_csign,
4009 param->fp_exclude_ctime,
4010 param->fp_time_margin, mds);
4014 /* If the previous check matches, but this one is not yet clear,
4015 * we should return 0 to do an RPC on OSTs. */
4023 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4025 struct lov_user_mds_data *lmd = param->fp_lmd;
4030 for (i = 0; i < 2; i++) {
4031 /* Check if file is accepted. */
4032 if (param->fp_newery[NEWERXY_ATIME][i]) {
4033 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4034 param->fp_newery[NEWERXY_ATIME][i],
4041 if (param->fp_newery[NEWERXY_MTIME][i]) {
4042 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4043 param->fp_newery[NEWERXY_MTIME][i],
4049 * If the previous check matches, but this one is not
4050 * yet clear, we should return 0 to do an RPC on OSTs.
4056 if (param->fp_newery[NEWERXY_CTIME][i]) {
4057 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4058 param->fp_newery[NEWERXY_CTIME][i],
4064 * If the previous check matches, but this one is not
4065 * yet clear, we should return 0 to do an RPC on OSTs.
4072 * File birth time (btime) can get from MDT directly.
4073 * if @from_mdt is true, it means the input file attributs are
4074 * obtained directly from MDT.
4075 * Thus, if @from_mdt is false, we should skip the following
4081 if (param->fp_newery[NEWERXY_BTIME][i]) {
4082 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4085 rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4086 param->fp_newery[NEWERXY_BTIME][i],
4097 * Check whether the stripes matches the indexes user provided
4101 static int check_obd_match(struct find_param *param)
4103 struct lov_user_ost_data_v1 *objects;
4104 struct lov_comp_md_v1 *comp_v1 = NULL;
4105 struct lov_user_mds_data *lmd = param->fp_lmd;
4106 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4107 int i, j, k, count = 1;
4109 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4112 if (!S_ISREG(lmd->lmd_stx.stx_mode))
4115 /* exclude foreign */
4116 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4117 return param->fp_exclude_obd;
4119 /* Only those files should be accepted, which have a
4120 * stripe on the specified OST. */
4121 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4122 comp_v1 = (struct lov_comp_md_v1 *)v1;
4123 count = comp_v1->lcm_entry_count;
4126 for (i = 0; i < count; i++) {
4128 v1 = lov_comp_entry(comp_v1, i);
4130 objects = lov_v1v3_objects(v1);
4132 for (j = 0; j < v1->lmm_stripe_count; j++) {
4133 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4136 for (k = 0; k < param->fp_num_obds; k++) {
4137 if (param->fp_obd_indexes[k] ==
4138 objects[j].l_ost_idx)
4139 return !param->fp_exclude_obd;
4144 return param->fp_exclude_obd;
4147 static int check_mdt_match(struct find_param *param)
4151 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4154 /* FIXME: For striped dir, we should get stripe information and check */
4155 for (i = 0; i < param->fp_num_mdts; i++) {
4156 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4157 return !param->fp_exclude_mdt;
4160 if (param->fp_exclude_mdt)
4167 * Check whether the obd is active or not, if it is
4168 * not active, just print the object affected by this
4171 static int print_failed_tgt(struct find_param *param, char *path, int type)
4173 struct obd_statfs stat_buf;
4174 struct obd_uuid uuid_buf;
4177 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV)
4180 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4181 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4182 ret = llapi_obd_statfs(path, type,
4183 type == LL_STATFS_LOV ? param->fp_obd_index :
4184 param->fp_mdt_index, &stat_buf,
4187 llapi_error(LLAPI_MSG_NORMAL, ret, "obd_uuid: %s failed",
4188 param->fp_obd_uuid->uuid);
4193 static int find_check_stripe_size(struct find_param *param)
4195 struct lov_comp_md_v1 *comp_v1 = NULL;
4196 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4197 __u32 stripe_size = 0;
4198 int ret, i, count = 1;
4200 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4201 return param->fp_exclude_stripe_size ? 1 : -1;
4203 ret = param->fp_exclude_stripe_size ? 1 : -1;
4204 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4205 comp_v1 = (struct lov_comp_md_v1 *)v1;
4206 count = comp_v1->lcm_entry_count;
4209 for (i = 0; i < count; i++) {
4210 struct lov_comp_md_entry_v1 *ent;
4213 v1 = lov_comp_entry(comp_v1, i);
4215 ent = &comp_v1->lcm_entries[i];
4216 if (ent->lcme_flags & LCME_FL_EXTENSION)
4218 if (!(ent->lcme_flags & LCME_FL_INIT))
4221 stripe_size = v1->lmm_stripe_size;
4224 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4225 param->fp_stripe_size_sign,
4226 param->fp_exclude_stripe_size,
4227 param->fp_stripe_size_units, 0);
4232 static int find_check_ext_size(struct find_param *param)
4234 struct lov_comp_md_v1 *comp_v1;
4235 struct lov_user_md_v1 *v1;
4238 ret = param->fp_exclude_ext_size ? 1 : -1;
4239 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4240 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4243 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4244 struct lov_comp_md_entry_v1 *ent;
4246 v1 = lov_comp_entry(comp_v1, i);
4248 ent = &comp_v1->lcm_entries[i];
4249 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4252 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4253 param->fp_ext_size_sign,
4254 param->fp_exclude_ext_size,
4255 param->fp_ext_size_units, 0);
4256 /* If any ext_size matches */
4264 static __u32 find_get_stripe_count(struct find_param *param)
4266 struct lov_comp_md_v1 *comp_v1 = NULL;
4267 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4269 __u32 stripe_count = 0;
4271 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4274 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4275 comp_v1 = (struct lov_comp_md_v1 *)v1;
4276 count = comp_v1->lcm_entry_count;
4279 for (i = 0; i < count; i++) {
4281 struct lov_comp_md_entry_v1 *ent;
4283 v1 = lov_comp_entry(comp_v1, i);
4285 ent = &comp_v1->lcm_entries[i];
4286 if (!(ent->lcme_flags & LCME_FL_INIT))
4289 if (ent->lcme_flags & LCME_FL_EXTENSION)
4292 stripe_count = v1->lmm_stripe_count;
4295 return stripe_count;
4298 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4300 static int find_check_layout(struct find_param *param)
4302 struct lov_comp_md_v1 *comp_v1 = NULL;
4303 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4305 bool found = false, valid = false;
4307 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4308 comp_v1 = (struct lov_comp_md_v1 *)v1;
4309 count = comp_v1->lcm_entry_count;
4312 for (i = 0; i < count; i++) {
4314 v1 = lov_comp_entry(comp_v1, i);
4316 /* foreign file have a special magic but no pattern field */
4317 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4320 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4324 if (v1->lmm_pattern & param->fp_layout) {
4333 if ((found && !param->fp_exclude_layout) ||
4334 (!found && param->fp_exclude_layout))
4340 /* if no type specified, check/exclude all foreign
4341 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4343 static int find_check_foreign(struct find_param *param)
4345 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4346 struct lov_foreign_md *lfm;
4348 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4349 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4350 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4351 return param->fp_exclude_foreign ? 1 : -1;
4354 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4355 lfm->lfm_type == param->fp_foreign_type)
4356 return param->fp_exclude_foreign ? -1 : 1;
4357 return param->fp_exclude_foreign ? 1 : -1;
4361 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4362 struct lmv_foreign_md *lfm;
4364 lfm = (void *)param->fp_lmv_md;
4365 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
4366 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4367 return param->fp_exclude_foreign ? 1 : -1;
4370 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4371 lfm->lfm_type == param->fp_foreign_type)
4372 return param->fp_exclude_foreign ? -1 : 1;
4373 return param->fp_exclude_foreign ? 1 : -1;
4379 static int find_check_pool(struct find_param *param)
4381 struct lov_comp_md_v1 *comp_v1 = NULL;
4382 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4383 struct lov_user_md_v3 *v3 = (void *)v1;
4387 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4388 comp_v1 = (struct lov_comp_md_v1 *)v1;
4389 count = comp_v1->lcm_entry_count;
4390 /* empty requested pool is taken as no pool search */
4391 if (count == 0 && param->fp_poolname[0] == '\0')
4395 for (i = 0; i < count; i++) {
4396 if (comp_v1 != NULL)
4397 v1 = lov_comp_entry(comp_v1, i);
4399 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4402 if (((v1->lmm_magic == LOV_USER_MAGIC_V1) &&
4403 (param->fp_poolname[0] == '\0')) ||
4404 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4405 (strncmp(v3->lmm_pool_name,
4406 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4407 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4408 (strcmp(param->fp_poolname, "*") == 0))) {
4414 if ((found && !param->fp_exclude_pool) ||
4415 (!found && param->fp_exclude_pool))
4421 static int find_check_comp_options(struct find_param *param)
4423 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4424 struct lov_user_mds_data *lmd = param->fp_lmd;
4425 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4426 struct lov_comp_md_entry_v1 *entry;
4429 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4432 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4433 comp_v1 = (struct lov_comp_md_v1 *)v1;
4435 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4436 if (forged_v1 == NULL)
4438 comp_v1 = forged_v1;
4439 comp_v1->lcm_entry_count = 1;
4440 entry = &comp_v1->lcm_entries[0];
4441 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4443 entry->lcme_extent.e_start = 0;
4444 entry->lcme_extent.e_end = LUSTRE_EOF;
4447 /* invalid case, don't match for any kind of search. */
4448 if (comp_v1->lcm_entry_count == 0) {
4453 if (param->fp_check_comp_count) {
4454 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4455 param->fp_comp_count,
4456 param->fp_comp_count_sign,
4457 param->fp_exclude_comp_count, 1, 0);
4463 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4464 entry = &comp_v1->lcm_entries[i];
4466 if (param->fp_check_comp_flags) {
4468 if (((param->fp_comp_flags & entry->lcme_flags) !=
4469 param->fp_comp_flags) ||
4470 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4476 if (param->fp_check_comp_start) {
4477 ret = find_value_cmp(entry->lcme_extent.e_start,
4478 param->fp_comp_start,
4479 param->fp_comp_start_sign,
4480 param->fp_exclude_comp_start,
4481 param->fp_comp_start_units, 0);
4486 if (param->fp_check_comp_end) {
4487 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4493 /* the component matches all criteria */
4502 static int find_check_mirror_options(struct find_param *param)
4504 struct lov_comp_md_v1 *comp_v1;
4505 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4508 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4511 comp_v1 = (struct lov_comp_md_v1 *)v1;
4513 if (param->fp_check_mirror_count) {
4514 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4515 param->fp_mirror_count,
4516 param->fp_mirror_count_sign,
4517 param->fp_exclude_mirror_count, 1, 0);
4522 if (param->fp_check_mirror_state) {
4524 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4526 if ((param->fp_mirror_state != 0 &&
4527 file_state != param->fp_mirror_state) ||
4528 file_state == param->fp_mirror_neg_state)
4535 static bool find_check_lmm_info(struct find_param *param)
4537 return param->fp_check_pool || param->fp_check_stripe_count ||
4538 param->fp_check_stripe_size || param->fp_check_layout ||
4539 param->fp_check_comp_count || param->fp_check_comp_end ||
4540 param->fp_check_comp_start || param->fp_check_comp_flags ||
4541 param->fp_check_mirror_count || param->fp_check_foreign ||
4542 param->fp_check_mirror_state || param->fp_check_ext_size ||
4543 param->fp_check_projid;
4547 * Get file/directory project id.
4548 * by the open fd resides on.
4549 * Return 0 and project id on success, or -ve errno.
4551 static int fget_projid(int fd, int *projid)
4556 rc = ioctl(fd, LL_IOC_FSGETXATTR, &fsx);
4560 *projid = fsx.fsx_projid;
4564 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
4565 void *data, struct dirent64 *de)
4567 struct find_param *param = (struct find_param *)data;
4568 struct lov_user_mds_data *lmd = param->fp_lmd;
4569 DIR *dir = dirp == NULL ? NULL : *dirp;
4570 int decision = 1; /* 1 is accepted; -1 is rejected. */
4572 int checked_type = 0;
4574 __u32 stripe_count = 0;
4578 if (parent == NULL && dir == NULL)
4581 /* If a regular expression is presented, make the initial decision */
4582 if (param->fp_pattern != NULL) {
4583 char *fname = strrchr(path, '/');
4584 fname = (fname == NULL ? path : fname + 1);
4585 ret = fnmatch(param->fp_pattern, fname, 0);
4586 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4587 (ret == 0 && param->fp_exclude_pattern))
4591 /* See if we can check the file type from the dirent. */
4592 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
4595 if (DTTOIF(de->d_type) == param->fp_type) {
4596 if (param->fp_exclude_type)
4599 if (!param->fp_exclude_type)
4606 /* Request MDS for the stat info if some of these parameters need
4607 * to be compared. */
4608 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4609 param->fp_check_uid || param->fp_check_gid ||
4610 param->fp_newerxy || param->fp_btime ||
4611 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4612 param->fp_check_size || param->fp_check_blocks ||
4613 find_check_lmm_info(param) ||
4614 param->fp_check_mdt_count || param->fp_check_hash_type)
4617 if (param->fp_type != 0 && checked_type == 0)
4620 if (decision == 0) {
4621 if (dir && (param->fp_check_mdt_count ||
4622 param->fp_check_hash_type || param->fp_check_foreign)) {
4623 param->fp_get_lmv = 1;
4624 ret = cb_get_dirstripe(path, dir, param);
4626 /* XXX this works to decide for foreign
4629 if (errno == ENODATA &&
4630 param->fp_check_foreign) {
4631 if (param->fp_exclude_foreign)
4639 param->fp_lmd->lmd_lmm.lmm_magic = 0;
4640 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
4641 param->fp_lum_size, GET_LMD_INFO);
4642 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
4643 find_check_lmm_info(param)) {
4644 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
4646 /* We need to "fake" the "use the default" values
4647 * since the lmm struct is zeroed out at this point. */
4648 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4649 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
4651 ostid_set_seq(&lmm->lmm_oi,
4652 FID_SEQ_LOV_DEFAULT);
4653 lmm->lmm_stripe_size = 0;
4654 lmm->lmm_stripe_count = 0;
4655 lmm->lmm_stripe_offset = -1;
4657 if (ret == 0 && param->fp_mdt_uuid != NULL) {
4659 ret = llapi_file_fget_mdtidx(dirfd(dir),
4660 ¶m->fp_file_mdt_index);
4661 } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
4662 /* FIXME: we could get the MDT index from the
4663 * file's FID in lmd->lmd_lmm.lmm_oi without
4664 * opening the file, once we are sure that
4665 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
4666 * That would still be an ioctl() to map the
4667 * FID to the MDT, but not an open RPC. */
4668 fd = open(path, O_RDONLY);
4670 ret = llapi_file_fget_mdtidx(fd,
4671 ¶m->fp_file_mdt_index);
4676 /* For a special file, we assume it resides on
4677 * the same MDT as the parent directory. */
4678 ret = llapi_file_fget_mdtidx(dirfd(parent),
4679 ¶m->fp_file_mdt_index);
4690 stripe_count = find_get_stripe_count(param);
4694 if (param->fp_type && !checked_type) {
4695 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
4696 if (param->fp_exclude_type)
4699 if (!param->fp_exclude_type)
4705 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
4706 if (lustre_fs && param->fp_got_uuids &&
4707 param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
4708 lmd->lmd_stx.stx_dev_minor)) {
4709 /* A lustre/lustre mount point is crossed. */
4710 param->fp_got_uuids = 0;
4711 param->fp_obds_printed = 0;
4712 param->fp_mdt_index = OBD_NOT_FOUND;
4713 param->fp_obd_index = OBD_NOT_FOUND;
4716 if (lustre_fs && !param->fp_got_uuids) {
4717 ret = setup_target_indexes(dir ? dir : parent, path,
4722 param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
4723 lmd->lmd_stx.stx_dev_minor);
4724 } else if (!lustre_fs && param->fp_got_uuids) {
4725 /* A lustre/non-lustre mount point is crossed. */
4726 param->fp_got_uuids = 0;
4727 param->fp_mdt_index = OBD_NOT_FOUND;
4728 param->fp_obd_index = OBD_NOT_FOUND;
4732 if (param->fp_check_foreign) {
4733 decision = find_check_foreign(param);
4738 if (param->fp_check_stripe_size) {
4739 decision = find_check_stripe_size(param);
4744 if (param->fp_check_ext_size) {
4745 decision = find_check_ext_size(param);
4750 if (param->fp_check_stripe_count) {
4751 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
4752 param->fp_stripe_count_sign,
4753 param->fp_exclude_stripe_count, 1, 0);
4758 if (param->fp_check_mdt_count) {
4759 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4764 decision = find_value_cmp(
4765 param->fp_lmv_md->lum_stripe_count,
4766 param->fp_mdt_count,
4767 param->fp_mdt_count_sign,
4768 param->fp_exclude_mdt_count, 1, 0);
4773 if (param->fp_check_layout) {
4774 decision = find_check_layout(param);
4779 if (param->fp_check_hash_type) {
4782 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4787 found = param->fp_lmv_md->lum_hash_type & param->fp_hash_type;
4788 if ((found && param->fp_exclude_hash_type) ||
4789 (!found && !param->fp_exclude_hash_type)) {
4795 /* If an OBD UUID is specified but none matches, skip this file. */
4796 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
4797 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
4800 /* If an OST or MDT UUID is given, and some OST matches,
4802 if (param->fp_obd_index != OBD_NOT_FOUND ||
4803 param->fp_mdt_index != OBD_NOT_FOUND) {
4804 if (param->fp_obd_uuid) {
4805 if (check_obd_match(param)) {
4806 /* If no mdtuuid is given, we are done.
4807 * Otherwise, fall through to the mdtuuid
4809 if (!param->fp_mdt_uuid)
4816 if (param->fp_mdt_uuid) {
4817 if (check_mdt_match(param))
4824 if (param->fp_check_uid) {
4825 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
4826 if (param->fp_exclude_uid)
4829 if (!param->fp_exclude_uid)
4834 if (param->fp_check_gid) {
4835 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
4836 if (param->fp_exclude_gid)
4839 if (!param->fp_exclude_gid)
4844 if (param->fp_check_projid) {
4848 fd = open(path, O_RDONLY);
4851 ret = fget_projid(fd, &projid);
4856 if (projid == param->fp_projid) {
4857 if (param->fp_exclude_projid)
4860 if (!param->fp_exclude_projid)
4865 if (param->fp_check_pool) {
4866 decision = find_check_pool(param);
4871 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
4872 param->fp_check_comp_start || param->fp_check_comp_end) {
4873 decision = find_check_comp_options(param);
4878 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
4879 decision = find_check_mirror_options(param);
4884 /* Check the time on mds. */
4886 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
4889 for_mds = lustre_fs ?
4890 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
4891 decision = find_time_check(param, for_mds);
4896 if (param->fp_btime) {
4897 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
4902 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4903 param->fp_btime, param->fp_bsign,
4904 param->fp_exclude_btime,
4905 param->fp_time_margin, 0);
4910 if (param->fp_newerxy) {
4913 for_mds = lustre_fs ?
4914 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
4915 decision = find_newerxy_check(param, for_mds, true);
4924 flags = param->fp_lmd->lmd_flags;
4925 if (param->fp_check_size &&
4926 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
4927 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
4928 !(flags & OBD_MD_FLSIZE ||
4929 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
4932 if (param->fp_check_blocks &&
4933 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
4934 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
4935 !(flags & OBD_MD_FLBLOCKS ||
4936 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
4939 /* If file still fits the request, ask ost for updated info.
4940 * The regular stat is almost of the same speed as some new
4941 * 'glimpse-size-ioctl'.
4946 /* For regular files with the stripe the decision may have not
4947 * been taken yet if *time or size is to be checked. */
4948 if (param->fp_obd_index != OBD_NOT_FOUND)
4949 print_failed_tgt(param, path, LL_STATFS_LOV);
4951 if (param->fp_mdt_index != OBD_NOT_FOUND)
4952 print_failed_tgt(param, path, LL_STATFS_LMV);
4955 ret = fstat_f(dirfd(dir), &st);
4956 else if (de != NULL)
4957 ret = fstatat_f(dirfd(parent), de->d_name, &st,
4958 AT_SYMLINK_NOFOLLOW);
4960 ret = lstat_f(path, &st);
4963 if (errno == ENOENT) {
4964 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
4965 "warning: %s: %s does not exist",
4970 llapi_error(LLAPI_MSG_ERROR, ret,
4971 "%s: stat on %s failed",
4977 convert_lmd_statx(param->fp_lmd, &st, true);
4978 /* Check the time on osc. */
4979 decision = find_time_check(param, 0);
4983 if (param->fp_newerxy) {
4984 decision = find_newerxy_check(param, 0, false);
4994 if (param->fp_check_size) {
4995 decision = find_value_cmp(lmd->lmd_stx.stx_size,
4997 param->fp_size_sign,
4998 param->fp_exclude_size,
4999 param->fp_size_units, 0);
5004 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
5005 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
5007 param->fp_blocks_sign,
5008 param->fp_exclude_blocks,
5009 param->fp_blocks_units, 0);
5015 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
5016 if (param->fp_zero_end)
5017 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
5019 llapi_printf(LLAPI_MSG_NORMAL, "\n");
5023 /* Do not get down anymore? */
5024 if (param->fp_depth == param->fp_max_depth) {
5035 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
5036 void *param_data, struct dirent64 *de)
5038 struct find_param *param = (struct find_param *)param_data;
5039 struct lmv_user_md *lmu = param->fp_lmv_md;
5040 DIR *tmp_parent = parent;
5041 char raw[MAX_IOC_BUFLEN] = {'\0'};
5043 struct obd_ioctl_data data = { 0 };
5050 if (parent == NULL && dirp == NULL)
5059 if (parent == NULL) {
5060 tmp_parent = opendir_parent(path);
5061 if (tmp_parent == NULL) {
5064 llapi_error(LLAPI_MSG_ERROR, ret,
5065 "can not open %s", path);
5070 fd = dirfd(tmp_parent);
5072 path_copy = strdup(path);
5073 filename = basename(path_copy);
5075 data.ioc_inlbuf1 = (char *)filename;
5076 data.ioc_inllen1 = strlen(filename) + 1;
5077 data.ioc_inlbuf2 = (char *)lmu;
5078 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
5080 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5082 llapi_error(LLAPI_MSG_ERROR, ret,
5083 "llapi_obd_statfs: error packing ioctl data");
5088 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
5090 if (errno == EBUSY && !retry) {
5091 /* because migrate may not be able to lock all involved
5092 * objects in order, for some of them it try lock, while
5093 * there may be conflicting COS locks and cause migrate
5094 * fail with EBUSY, hope a sync() could cause
5095 * transaction commit and release these COS locks. */
5099 } else if (errno == EALREADY) {
5100 if (param->fp_verbose & VERBOSE_DETAIL)
5101 llapi_printf(LLAPI_MSG_NORMAL,
5102 "%s migrated to MDT%d already\n",
5103 path, lmu->lum_stripe_offset);
5107 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
5111 } else if (param->fp_verbose & VERBOSE_DETAIL) {
5112 llapi_printf(LLAPI_MSG_NORMAL,
5113 "migrate %s to MDT%d stripe count %d\n",
5114 path, lmu->lum_stripe_offset,
5115 lmu->lum_stripe_count);
5120 /* If the directory is being migration, we need
5121 * close the directory after migration,
5122 * so the old directory cache will be cleanup
5123 * on the client side, and re-open to get the
5124 * new directory handle */
5125 *dirp = opendir(path);
5126 if (*dirp == NULL) {
5128 llapi_error(LLAPI_MSG_ERROR, ret,
5129 "%s: Failed to open '%s'", __func__, path);
5134 closedir(tmp_parent);
5141 /* dir migration finished, shrink its stripes */
5142 static int cb_migrate_mdt_fini(char *path, DIR *parent, DIR **dirp, void *data,
5143 struct dirent64 *de)
5145 struct find_param *param = data;
5146 struct lmv_user_md *lmu = param->fp_lmv_md;
5147 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
5150 if (de && de->d_type != DT_DIR)
5155 * close it before setxattr because the latter may destroy the
5156 * original object, and cause close fail.
5158 ret = closedir(*dirp);
5164 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
5165 if (ret == -EALREADY)
5168 cb_common_fini(path, parent, dirp, data, de);
5172 int llapi_migrate_mdt(char *path, struct find_param *param)
5174 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
5178 int llapi_mv(char *path, struct find_param *param)
5180 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
5181 static bool printed;
5184 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
5185 "%s() is deprecated, use llapi_migrate_mdt() instead",
5190 return llapi_migrate_mdt(path, param);
5193 int llapi_find(char *path, struct find_param *param)
5195 return param_callback(path, cb_find_init, cb_common_fini, param);
5199 * Get MDT number that the file/directory inode referenced
5200 * by the open fd resides on.
5201 * Return 0 and mdtidx on success, or -ve errno.
5203 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
5205 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
5210 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
5211 struct dirent64 *de)
5213 struct find_param *param = (struct find_param *)data;
5214 DIR *d = dirp == NULL ? NULL : *dirp;
5218 if (parent == NULL && d == NULL)
5222 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5223 } else /* if (parent) */ {
5226 fd = open(path, O_RDONLY | O_NOCTTY);
5228 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5236 if (ret == -ENODATA) {
5237 if (!param->fp_obd_uuid)
5238 llapi_printf(LLAPI_MSG_NORMAL,
5239 "'%s' has no stripe info\n", path);
5241 } else if (ret == -ENOENT) {
5242 llapi_error(LLAPI_MSG_WARN, ret,
5243 "warning: %s: '%s' does not exist",
5246 } else if (ret == -ENOTTY) {
5247 llapi_error(LLAPI_MSG_ERROR, ret,
5248 "%s: '%s' not on a Lustre fs",
5251 llapi_error(LLAPI_MSG_ERROR, ret,
5252 "error: %s: '%s' failed get_mdtidx",
5258 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5259 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5261 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5265 /* Do not go down anymore? */
5266 if (param->fp_depth == param->fp_max_depth)
5274 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
5275 struct dirent64 *de)
5277 struct find_param *param = (struct find_param *)data;
5278 DIR *d = dirp == NULL ? NULL : *dirp;
5281 if (parent == NULL && d == NULL)
5284 if (param->fp_obd_uuid) {
5285 param->fp_quiet = 1;
5286 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
5291 if (d && (param->fp_get_lmv || param->fp_get_default_lmv))
5292 ret = cb_get_dirstripe(path, d, param);
5294 (parent && !param->fp_get_lmv && !param->fp_get_default_lmv))
5295 ret = get_lmd_info(path, parent, d, ¶m->fp_lmd->lmd_lmm,
5296 param->fp_lum_size, GET_LMD_STRIPE);
5301 if (errno == ENODATA && d != NULL) {
5302 /* We need to "fake" the "use the default" values
5303 * since the lmm struct is zeroed out at this point.
5304 * The magic needs to be set in order to satisfy
5305 * a check later on in the code path.
5306 * The object_seq needs to be set for the "(Default)"
5307 * prefix to be displayed. */
5308 if (param->fp_get_default_lmv) {
5309 struct lmv_user_md *lum = param->fp_lmv_md;
5311 lum->lum_magic = LMV_USER_MAGIC;
5312 lum->lum_stripe_count = 0;
5313 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
5315 } else if (param->fp_get_lmv) {
5316 struct lmv_user_md *lum = param->fp_lmv_md;
5319 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5322 lum->lum_magic = LMV_MAGIC_V1;
5323 lum->lum_stripe_count = 0;
5324 lum->lum_stripe_offset = mdtidx;
5327 struct lov_user_md *lmm =
5328 ¶m->fp_lmd->lmd_lmm;
5330 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5332 ostid_set_seq(&lmm->lmm_oi,
5333 FID_SEQ_LOV_DEFAULT);
5334 lmm->lmm_stripe_count = 0;
5335 lmm->lmm_stripe_size = 0;
5336 lmm->lmm_stripe_offset = -1;
5339 } else if (errno == ENODATA && parent != NULL) {
5340 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5341 llapi_printf(LLAPI_MSG_NORMAL,
5342 "%s has no stripe info\n", path);
5344 } else if (errno == ENOENT) {
5345 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5346 "warning: %s: %s does not exist",
5349 } else if (errno == ENOTTY) {
5351 llapi_error(LLAPI_MSG_ERROR, ret,
5352 "%s: '%s' not on a Lustre fs?",
5357 llapi_error(LLAPI_MSG_ERROR, ret,
5358 "error: %s: %s failed for %s",
5359 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
5360 "IOC_MDC_GETFILESTRIPE", path);
5367 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5368 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
5371 /* Do not get down anymore? */
5372 if (param->fp_depth == param->fp_max_depth)
5380 int llapi_getstripe(char *path, struct find_param *param)
5382 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5383 cb_get_mdt_index : cb_getstripe,
5384 cb_common_fini, param);
5387 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5388 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5390 char raw[MAX_IOC_BUFLEN] = {'\0'};
5392 struct obd_ioctl_data data = { 0 };
5395 data.ioc_inlbuf1 = (char *)&type;
5396 data.ioc_inllen1 = sizeof(__u32);
5397 data.ioc_inlbuf2 = (char *)&index;
5398 data.ioc_inllen2 = sizeof(__u32);
5399 data.ioc_pbuf1 = (char *)stat_buf;
5400 data.ioc_plen1 = sizeof(struct obd_statfs);
5401 data.ioc_pbuf2 = (char *)uuid_buf;
5402 data.ioc_plen2 = sizeof(struct obd_uuid);
5404 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5406 llapi_error(LLAPI_MSG_ERROR, rc,
5407 "llapi_obd_statfs: error packing ioctl data");
5411 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5413 return rc < 0 ? -errno : 0;
5416 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5417 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5422 fd = open(path, O_RDONLY);
5425 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5427 /* If we can't even open a file on the filesystem (e.g. with
5428 * -ESHUTDOWN), force caller to exit or it will loop forever. */
5432 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5439 #define MAX_STRING_SIZE 128
5441 int llapi_ping(char *obd_type, char *obd_name)
5443 int flags = O_RDONLY;
5444 char buf[1] = { 0 };
5448 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5449 obd_type, obd_name);
5453 fd = open(path.gl_pathv[0], flags);
5455 if (errno == EACCES && flags == O_RDONLY) {
5460 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5465 if (flags == O_RDONLY)
5466 rc = read(fd, buf, sizeof(buf));
5468 rc = write(fd, buf, sizeof(buf));
5476 cfs_free_param_data(&path);
5480 int llapi_target_iterate(int type_num, char **obd_type,
5481 void *args, llapi_cb_t cb)
5487 for (i = 0; i < type_num; i++) {
5490 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5494 for (j = 0; j < param.gl_pathc; j++) {
5495 char obd_uuid[UUID_MAX + 1];
5499 fp = fopen(param.gl_pathv[j], "r");
5502 llapi_error(LLAPI_MSG_ERROR, rc,
5503 "error: opening '%s'",
5508 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5510 llapi_error(LLAPI_MSG_ERROR, rc,
5511 "error: reading '%s'",
5516 /* Extract the obd_name from the sysfs path.
5517 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5519 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5525 /* skip /fs/lustre/'obd_type'/ */
5526 obd_name += strlen(obd_type[i]) + 12;
5527 /* chop off after obd_name */
5528 ptr = strrchr(obd_name, '/');
5532 cb(obd_type[i], obd_name, obd_uuid, args);
5541 cfs_free_param_data(¶m);
5545 static void do_target_check(char *obd_type_name, char *obd_name,
5546 char *obd_uuid, void *args)
5550 rc = llapi_ping(obd_type_name, obd_name);
5551 if (rc == ENOTCONN) {
5552 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5554 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5556 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5560 int llapi_target_check(int type_num, char **obd_type, char *dir)
5562 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5565 #undef MAX_STRING_SIZE
5567 /* Is this a lustre fs? */
5568 int llapi_is_lustre_mnttype(const char *type)
5570 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
5573 /* Is this a lustre client fs? */
5574 int llapi_is_lustre_mnt(struct mntent *mnt)
5576 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5577 strstr(mnt->mnt_fsname, ":/") != NULL);
5580 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5582 char fsname[PATH_MAX + 1];
5586 rc = llapi_search_fsname(mnt, fsname);
5590 root = open(mnt, O_RDONLY | O_DIRECTORY);
5593 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
5597 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
5605 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
5610 root = open(mnt, O_RDONLY | O_DIRECTORY);
5613 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
5617 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
5620 llapi_error(LLAPI_MSG_ERROR, rc,
5621 "ioctl on %s for getting connect flags failed", mnt);
5628 * Get a 64-bit value representing the version of file data pointed by fd.
5630 * Each write or truncate, flushed on OST, will change this value. You can use
5631 * this value to verify if file data was modified. This only checks the file
5632 * data, not metadata.
5634 * \param flags 0: no flush pages, usually used it the process has already
5636 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
5638 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
5639 * pages from clients.
5641 * \retval 0 on success.
5642 * \retval -errno on error.
5644 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
5647 struct ioc_data_version idv;
5649 idv.idv_flags = (__u32)flags;
5651 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5655 *data_version = idv.idv_version;
5661 * Flush cached pages from all clients.
5663 * \param fd File descriptor
5667 int llapi_file_flush(int fd)
5671 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);
5675 * Fetch layout version from OST objects. Layout version on OST objects are
5676 * only set when the file is a mirrored file AND after the file has been
5677 * written at least once.
5679 * It actually fetches the least layout version from the objects.
5681 int llapi_get_ost_layout_version(int fd, __u32 *layout_version)
5684 struct ioc_data_version idv = { 0 };
5686 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5690 *layout_version = idv.idv_layout_version;
5696 * Create a file without any name and open it for read/write
5698 * - file is created as if it were a standard file in the given \a directory
5699 * - file does not appear in \a directory and mtime does not change because
5700 * the filename is handled specially by the Lustre MDS.
5701 * - file is destroyed at final close
5703 * \param[in] directory directory from which to inherit layout/MDT idx
5704 * \param[in] mdt_idx MDT index on which the file is created,
5705 * \a idx == -1 means no specific MDT is requested
5706 * \param[in] mode standard open(2) mode
5707 * \param[in] stripe_param stripe parameters. May be NULL.
5709 * \retval a file descriptor on success.
5710 * \retval -errno on error.
5712 int llapi_create_volatile_param(const char *directory, int mdt_idx,
5713 int open_flags, mode_t mode,
5714 const struct llapi_stripe_param *stripe_param)
5716 char file_path[PATH_MAX];
5717 int saved_errno = errno;
5719 unsigned int rnumber;
5725 rc = snprintf(file_path, sizeof(file_path),
5726 "%s/" LUSTRE_VOLATILE_HDR "::%.4X",
5727 directory, rnumber);
5729 rc = snprintf(file_path, sizeof(file_path),
5730 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X",
5731 directory, mdt_idx, rnumber);
5733 if (rc < 0 || rc >= sizeof(file_path))
5734 return -ENAMETOOLONG;
5737 * Either open O_WRONLY or O_RDWR, creating RDONLY
5738 * is non-sensical here
5740 if ((open_flags & O_ACCMODE) == O_RDONLY)
5741 open_flags = O_RDWR | (open_flags & ~O_ACCMODE);
5743 open_flags |= O_CREAT | O_EXCL | O_NOFOLLOW;
5745 if (stripe_param != NULL) {
5746 fd = llapi_file_open_param(file_path, open_flags,
5747 mode, stripe_param);
5751 fd = open(file_path, open_flags, mode);
5755 } while (fd < 0 && rc == -EEXIST);
5758 llapi_error(LLAPI_MSG_ERROR, rc,
5759 "Cannot create volatile file '%s' in '%s'",
5760 file_path + strlen(directory) + 1 +
5761 LUSTRE_VOLATILE_HDR_LEN,
5767 * Unlink file in case this wasn't a Lustre filesystem and the magic
5768 * volatile filename wasn't handled as intended. The effect is the
5769 * same. If volatile open was supported then we expect unlink() to
5772 (void)unlink(file_path);
5775 * Since we are returning successfully we restore errno (and
5776 * mask out possible EEXIST from open() and ENOENT from unlink().
5778 errno = saved_errno;
5784 * Create a file without any name open it for read/write
5786 * - file is created as if it were a standard file in the given \a directory
5787 * - file does not appear in \a directory and mtime does not change because
5788 * the filename is handled specially by the Lustre MDS.
5789 * - file is removed at final close
5790 * - file modes are rw------- since it doesn't make sense to have a read-only
5791 * or write-only file that cannot be opened again.
5792 * - if user wants another mode it must use fchmod() on the open file, no
5793 * security problems arise because it cannot be opened by another process.
5795 * \param[in] directory directory from which to inherit layout/MDT idx
5796 * \param[in] idx MDT index on which the file is created,
5797 * \a idx == -1 means no specific MDT is requested
5798 * \param[in] open_flags standard open(2) flags
5800 * \retval a file descriptor on success.
5801 * \retval -errno on error.
5803 int llapi_create_volatile_idx(const char *directory, int mdt_idx,
5806 return llapi_create_volatile_param(directory, mdt_idx, open_flags,
5807 S_IRUSR | S_IWUSR, NULL);
5811 * Swap the layouts between 2 file descriptors
5812 * the 2 files must be open for writing
5813 * first fd received the ioctl, second fd is passed as arg
5814 * this is assymetric but avoid use of root path for ioctl
5816 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
5817 int gid, __u64 flags)
5819 struct lustre_swap_layouts lsl;
5824 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5825 rc = fstat(fd1, &st1);
5829 rc = fstat(fd2, &st2);
5834 lsl.sl_flags = flags;
5838 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
5842 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5843 struct timeval tv1[2];
5844 struct timeval tv2[2];
5846 memset(tv1, 0, sizeof(tv1));
5847 memset(tv2, 0, sizeof(tv2));
5849 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
5850 tv1[0].tv_sec = st1.st_atime;
5851 tv2[0].tv_sec = st2.st_atime;
5853 tv1[0].tv_sec = st2.st_atime;
5854 tv2[0].tv_sec = st1.st_atime;
5857 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
5858 tv1[1].tv_sec = st1.st_mtime;
5859 tv2[1].tv_sec = st2.st_mtime;
5861 tv1[1].tv_sec = st2.st_mtime;
5862 tv2[1].tv_sec = st1.st_mtime;
5865 rc = futimes(fd1, tv1);
5869 rc = futimes(fd2, tv2);
5877 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
5884 while (grp_id == 0);
5886 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
5894 * Swap the layouts between 2 files
5895 * the 2 files are open in write
5897 int llapi_swap_layouts(const char *path1, const char *path2,
5898 __u64 dv1, __u64 dv2, __u64 flags)
5902 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
5905 llapi_error(LLAPI_MSG_ERROR, rc,
5906 "error: cannot open '%s' for write", path1);
5910 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
5913 llapi_error(LLAPI_MSG_ERROR, rc,
5914 "error: cannot open '%s' for write", path2);
5918 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
5920 llapi_error(LLAPI_MSG_ERROR, rc,
5921 "error: cannot swap layout between '%s' and '%s'",
5934 * \param fd File to lock.
5935 * \param gid Group Identifier.
5937 * \retval 0 on success.
5938 * \retval -errno on failure.
5940 int llapi_group_lock(int fd, int gid)
5944 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
5947 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
5955 * \param fd File to unlock.
5956 * \param gid Group Identifier.
5958 * \retval 0 on success.
5959 * \retval -errno on failure.
5961 int llapi_group_unlock(int fd, int gid)
5965 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
5968 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");