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");
148 * Remove trailing linefeed so error string can be appended.
149 * @fmt is a const string, so we can't modify it directly.
151 if (has_nl && (newfmt = strdup(fmt)))
152 *strrchr(newfmt, '\n') = '\0';
154 newfmt = (char *)fmt;
156 vfprintf(stderr, newfmt, ap);
159 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
163 static void info_callback_default(enum llapi_message_level level, int err,
164 const char *fmt, va_list ap)
167 if (liblustreapi_cmd != NULL) {
168 fprintf(stdout, "%s %s: ",
169 program_invocation_short_name,
172 fprintf(stdout, "%s: ", program_invocation_short_name);
175 vfprintf(stdout, fmt, ap);
178 static llapi_log_callback_t llapi_error_callback = error_callback_default;
179 static llapi_log_callback_t llapi_info_callback = info_callback_default;
182 /* llapi_error will preserve errno */
183 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
186 int tmp_errno = errno;
188 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
192 llapi_error_callback(level, abs(err), fmt, args);
197 /* llapi_printf will preserve errno */
198 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
201 int tmp_errno = errno;
203 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
207 llapi_info_callback(level, 0, fmt, args);
213 * Set a custom error logging function. Passing in NULL will reset the logging
214 * callback to its default value.
216 * This function returns the value of the old callback.
218 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
220 llapi_log_callback_t old = llapi_error_callback;
223 llapi_error_callback = cb;
225 llapi_error_callback = error_callback_default;
231 * Set a custom info logging function. Passing in NULL will reset the logging
232 * callback to its default value.
234 * This function returns the value of the old callback.
236 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
238 llapi_log_callback_t old = llapi_info_callback;
241 llapi_info_callback = cb;
243 llapi_info_callback = info_callback_default;
249 * size_units is to be initialized (or zeroed) by caller.
251 int llapi_parse_size(const char *optarg, unsigned long long *size,
252 unsigned long long *size_units, int bytes_spec)
255 char *argbuf = (char *)optarg;
256 unsigned long long frac = 0, frac_d = 1;
258 if (strncmp(optarg, "-", 1) == 0)
261 if (*size_units == 0)
264 *size = strtoull(argbuf, &end, 0);
265 if (end != NULL && *end == '.') {
269 frac = strtoull(argbuf, &end, 10);
270 /* count decimal places */
271 for (i = 0; i < (end - argbuf); i++)
276 if ((*end == 'b') && *(end + 1) == '\0' &&
277 (*size & (~0ULL << (64 - 9))) == 0 &&
279 *size_units = 1 << 9;
280 } else if ((*end == 'b') &&
281 *(end + 1) == '\0' &&
284 } else if ((*end == 'k' || *end == 'K') &&
285 *(end + 1) == '\0' &&
286 (*size & (~0ULL << (64 - 10))) == 0) {
287 *size_units = 1 << 10;
288 } else if ((*end == 'm' || *end == 'M') &&
289 *(end + 1) == '\0' &&
290 (*size & (~0ULL << (64 - 20))) == 0) {
291 *size_units = 1 << 20;
292 } else if ((*end == 'g' || *end == 'G') &&
293 *(end + 1) == '\0' &&
294 (*size & (~0ULL << (64 - 30))) == 0) {
295 *size_units = 1 << 30;
296 } else if ((*end == 't' || *end == 'T') &&
297 *(end + 1) == '\0' &&
298 (*size & (~0ULL << (64 - 40))) == 0) {
299 *size_units = 1ULL << 40;
300 } else if ((*end == 'p' || *end == 'P') &&
301 *(end + 1) == '\0' &&
302 (*size & (~0ULL << (64 - 50))) == 0) {
303 *size_units = 1ULL << 50;
304 } else if ((*end == 'e' || *end == 'E') &&
305 *(end + 1) == '\0' &&
306 (*size & (~0ULL << (64 - 60))) == 0) {
307 *size_units = 1ULL << 60;
312 *size = *size * *size_units + frac * *size_units / frac_d;
317 int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
319 struct obd_ioctl_data *overlay;
322 data->ioc_len = obd_ioctl_packlen(data);
323 data->ioc_version = OBD_IOCTL_VERSION;
325 if (*pbuf != NULL && data->ioc_len > max_len) {
326 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
327 "pbuf = %p, ioc_len = %u, max_len = %d",
328 *pbuf, data->ioc_len, max_len);
333 *pbuf = malloc(data->ioc_len);
338 overlay = (struct obd_ioctl_data *)*pbuf;
339 memcpy(*pbuf, data, sizeof(*data));
341 ptr = overlay->ioc_bulk;
342 if (data->ioc_inlbuf1) {
343 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
344 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
347 if (data->ioc_inlbuf2) {
348 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
349 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
352 if (data->ioc_inlbuf3) {
353 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
354 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
357 if (data->ioc_inlbuf4) {
358 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
359 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
365 int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
367 struct obd_ioctl_data *overlay;
373 overlay = (struct obd_ioctl_data *)pbuf;
375 /* Preserve the caller's buffer pointers */
376 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
377 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
378 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
379 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
381 memcpy(data, pbuf, sizeof(*data));
383 ptr = overlay->ioc_bulk;
384 if (data->ioc_inlbuf1) {
385 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
386 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
389 if (data->ioc_inlbuf2) {
390 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
391 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
394 if (data->ioc_inlbuf3) {
395 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
396 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
399 if (data->ioc_inlbuf4) {
400 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
401 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
407 /* XXX: llapi_xxx() functions return negative values upon failure */
409 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
410 int stripe_count, int stripe_pattern)
412 static int page_size;
415 if (page_size == 0) {
417 * 64 KB is the largest common page size (on ia64/PPC/ARM),
418 * but check the local page size just in case. The page_size
419 * will not change for the lifetime of this process at least.
421 page_size = LOV_MIN_STRIPE_SIZE;
422 if (getpagesize() > page_size) {
423 page_size = getpagesize();
424 llapi_err_noerrno(LLAPI_MSG_WARN,
425 "warning: page size (%u) larger than expected (%u)",
426 page_size, LOV_MIN_STRIPE_SIZE);
429 if (!llapi_stripe_size_is_aligned(stripe_size)) {
431 llapi_error(LLAPI_MSG_ERROR, rc,
432 "error: bad stripe_size %llu, must be an even multiple of %d bytes",
433 (unsigned long long)stripe_size, page_size);
436 if (!llapi_stripe_index_is_valid(stripe_offset)) {
438 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
442 if (!llapi_stripe_count_is_valid(stripe_count)) {
444 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
448 if (llapi_stripe_size_is_too_big(stripe_size)) {
450 llapi_error(LLAPI_MSG_ERROR, rc,
451 "error: stripe size '%llu' over 4GB limit",
452 (unsigned long long)stripe_size);
461 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
466 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
468 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
472 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
474 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
479 if (!llapi_dir_hash_type_is_valid(hash_type)) {
481 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
489 * Trim a trailing newline from a string, if it exists.
491 int llapi_chomp_string(char *buf)
507 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
509 static int get_param_lov(const char *path, const char *param,
510 char *buf, size_t buf_size)
512 struct obd_uuid uuid;
515 rc = llapi_file_get_lov_uuid(path, &uuid);
519 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
524 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
526 static int get_param_lmv(const char *path, const char *param,
527 char *buf, size_t buf_size)
529 struct obd_uuid uuid;
532 rc = llapi_file_get_lmv_uuid(path, &uuid);
536 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
540 static int get_mds_md_size(const char *path)
542 char buf[PATH_MAX], inst[PATH_MAX];
543 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
546 rc = llapi_getname(path, inst, sizeof(inst));
550 /* Get the max ea size from llite parameters. */
551 rc = get_lustre_param_value("llite", inst, FILTER_BY_EXACT,
552 "max_easize", buf, sizeof(buf));
558 return rc > 0 ? rc : md_size;
561 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
563 return get_param_lmv(path, "uuid", buf, bufsize);
567 * Open a Lustre file.
569 * \param name the name of the file to be opened
570 * \param flags access mode, see flags in open(2)
571 * \param mode permission of the file if it is created, see mode in open(2)
572 * \param param stripe pattern of the newly created file
574 * \retval file descriptor of opened file
575 * \retval negative errno on failure
577 int llapi_file_open_param(const char *name, int flags, mode_t mode,
578 const struct llapi_stripe_param *param)
580 char fsname[MAX_OBD_NAME + 1] = { 0 };
581 char *pool_name = param->lsp_pool;
582 struct lov_user_md *lum = NULL;
583 size_t lum_size = sizeof(*lum);
586 /* Make sure we are on a Lustre file system */
587 rc = llapi_search_fsname(name, fsname);
589 llapi_error(LLAPI_MSG_ERROR, rc,
590 "'%s' is not on a Lustre filesystem",
595 /* Check if the stripe pattern is sane. */
596 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
597 param->lsp_stripe_offset,
598 param->lsp_stripe_count,
599 param->lsp_stripe_pattern);
603 /* Make sure we have a good pool */
604 if (pool_name != NULL) {
606 * in case user gives the full pool name <fsname>.<poolname>,
609 char *ptr = strchr(pool_name, '.');
613 if (strcmp(pool_name, fsname) != 0) {
615 llapi_err_noerrno(LLAPI_MSG_ERROR,
616 "Pool '%s' is not on filesystem '%s'",
623 /* Make sure the pool exists and is non-empty */
624 rc = llapi_search_ost(fsname, pool_name, NULL);
626 char *err = rc == 0 ? "has no OSTs" : "does not exist";
628 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
629 fsname, pool_name, err);
633 lum_size = sizeof(struct lov_user_md_v3);
636 /* sanity check of target list */
637 if (param->lsp_is_specific) {
638 char ostname[MAX_OBD_NAME + 64];
642 for (i = 0; i < param->lsp_stripe_count; i++) {
643 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
644 fsname, param->lsp_osts[i]);
645 rc = llapi_search_ost(fsname, pool_name, ostname);
650 llapi_error(LLAPI_MSG_ERROR, rc,
651 "%s: cannot find OST %s in %s",
658 /* Make sure stripe offset is in OST list. */
659 if (param->lsp_osts[i] == param->lsp_stripe_offset)
663 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
664 "%s: stripe offset '%d' is not in the target list",
665 __func__, param->lsp_stripe_offset);
669 lum_size = lov_user_md_size(param->lsp_stripe_count,
670 LOV_USER_MAGIC_SPECIFIC);
673 lum = calloc(1, lum_size);
678 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
680 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
681 flags = O_DIRECTORY | O_RDONLY;
688 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
693 /* Initialize IOCTL striping pattern structure */
694 lum->lmm_magic = LOV_USER_MAGIC_V1;
695 lum->lmm_pattern = param->lsp_stripe_pattern;
696 lum->lmm_stripe_size = param->lsp_stripe_size;
697 lum->lmm_stripe_count = param->lsp_stripe_count;
698 lum->lmm_stripe_offset = param->lsp_stripe_offset;
699 if (pool_name != NULL) {
700 struct lov_user_md_v3 *lumv3 = (void *)lum;
702 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
703 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
705 if (param->lsp_is_specific) {
706 struct lov_user_md_v3 *lumv3 = (void *)lum;
709 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
710 if (pool_name == NULL) {
712 * LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
713 * OST list, therefore if pool is not specified we have
714 * to pack a null pool name for placeholder.
716 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
719 for (i = 0; i < param->lsp_stripe_count; i++)
720 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
723 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
724 char errmsg[512] = "stripe already set";
727 if (errno != EEXIST && errno != EALREADY)
728 strncpy(errmsg, strerror(errno), sizeof(errmsg) - 1);
729 if (rc == -EREMOTEIO)
730 snprintf(errmsg, sizeof(errmsg),
731 "inactive OST among your specified %d OST(s)",
732 param->lsp_stripe_count);
734 llapi_err_noerrno(LLAPI_MSG_ERROR,
735 "setstripe error for '%s': %s", name, errmsg);
746 int llapi_file_open_pool(const char *name, int flags, int mode,
747 unsigned long long stripe_size, int stripe_offset,
748 int stripe_count, int stripe_pattern, char *pool_name)
750 const struct llapi_stripe_param param = {
751 .lsp_stripe_size = stripe_size,
752 .lsp_stripe_count = stripe_count,
753 .lsp_stripe_pattern = stripe_pattern,
754 .lsp_stripe_offset = stripe_offset,
755 .lsp_pool = pool_name
757 return llapi_file_open_param(name, flags, mode, ¶m);
760 int llapi_file_open(const char *name, int flags, int mode,
761 unsigned long long stripe_size, int stripe_offset,
762 int stripe_count, int stripe_pattern)
764 return llapi_file_open_pool(name, flags, mode, stripe_size,
765 stripe_offset, stripe_count,
766 stripe_pattern, NULL);
769 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
770 __u32 flags, char *foreign_lov)
773 struct lov_foreign_md *lfm;
776 if (foreign_lov == NULL) {
778 llapi_error(LLAPI_MSG_ERROR, rc,
779 "foreign LOV EA content must be provided");
783 len = strlen(foreign_lov);
784 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
787 llapi_error(LLAPI_MSG_ERROR, rc,
788 "foreign LOV EA size %zu (must be 0 < len < %zu)",
789 len, XATTR_SIZE_MAX -
790 offsetof(struct lov_foreign_md, lfm_value));
794 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
797 llapi_error(LLAPI_MSG_ERROR, rc,
798 "failed to allocate lov_foreign_md");
802 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
805 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
809 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
810 lfm->lfm_length = len;
811 lfm->lfm_type = type;
812 lfm->lfm_flags = flags;
813 memcpy(lfm->lfm_value, foreign_lov, len);
815 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
816 char *errmsg = "stripe already set";
820 errmsg = "not on a Lustre filesystem";
821 else if (errno == EEXIST || errno == EALREADY)
822 errmsg = "stripe already set";
824 errmsg = strerror(errno);
826 llapi_err_noerrno(LLAPI_MSG_ERROR,
827 "setstripe error for '%s': %s", name, errmsg);
843 int llapi_file_create(const char *name, unsigned long long stripe_size,
844 int stripe_offset, int stripe_count, int stripe_pattern)
848 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
849 stripe_offset, stripe_count, stripe_pattern,
858 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
859 int stripe_offset, int stripe_count,
860 int stripe_pattern, char *pool_name)
864 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
865 stripe_offset, stripe_count, stripe_pattern,
874 static int verify_dir_param(const char *name,
875 const struct llapi_stripe_param *param)
877 char fsname[MAX_OBD_NAME + 1] = { 0 };
878 char *pool_name = param->lsp_pool;
881 /* Make sure we are on a Lustre file system */
882 rc = llapi_search_fsname(name, fsname);
884 llapi_error(LLAPI_MSG_ERROR, rc,
885 "'%s' is not on a Lustre filesystem",
890 /* Check if the stripe pattern is sane. */
891 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
892 param->lsp_stripe_count,
893 param->lsp_stripe_pattern);
897 /* Make sure we have a good pool */
898 if (pool_name != NULL) {
900 * in case user gives the full pool name <fsname>.<poolname>,
903 char *ptr = strchr(pool_name, '.');
907 if (strcmp(pool_name, fsname) != 0) {
909 llapi_err_noerrno(LLAPI_MSG_ERROR,
910 "Pool '%s' is not on filesystem '%s'",
917 /* Make sure the pool exists and is non-empty */
918 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
920 char *err = rc == 0 ? "has no OSTs" : "does not exist";
922 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
923 fsname, pool_name, err);
928 /* sanity check of target list */
929 if (param->lsp_is_specific) {
930 char mdtname[MAX_OBD_NAME + 64];
934 for (i = 0; i < param->lsp_stripe_count; i++) {
935 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
936 fsname, param->lsp_tgts[i]);
937 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
942 llapi_error(LLAPI_MSG_ERROR, rc,
943 "%s: cannot find MDT %s in %s",
950 /* Make sure stripe offset is in MDT list. */
951 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
955 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
956 "%s: stripe offset '%d' is not in the target list",
957 __func__, param->lsp_stripe_offset);
965 static inline void param2lmu(struct lmv_user_md *lmu,
966 const struct llapi_stripe_param *param)
968 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
970 lmu->lum_stripe_count = param->lsp_stripe_count;
971 lmu->lum_stripe_offset = param->lsp_stripe_offset;
972 lmu->lum_hash_type = param->lsp_stripe_pattern;
973 if (param->lsp_pool != NULL)
974 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
975 if (param->lsp_is_specific) {
978 for (i = 0; i < param->lsp_stripe_count; i++)
979 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
983 int llapi_dir_set_default_lmv(const char *name,
984 const struct llapi_stripe_param *param)
986 struct lmv_user_md lmu = { 0 };
990 rc = verify_dir_param(name, param);
994 /* TODO: default lmv doesn't support specific targets yet */
995 if (param->lsp_is_specific)
998 param2lmu(&lmu, param);
1000 fd = open(name, O_DIRECTORY | O_RDONLY);
1003 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1007 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
1009 char *errmsg = "stripe already set";
1012 if (errno != EEXIST && errno != EALREADY)
1013 errmsg = strerror(errno);
1015 llapi_err_noerrno(LLAPI_MSG_ERROR,
1016 "default dirstripe error on '%s': %s",
1023 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
1024 int stripe_count, int stripe_pattern,
1025 const char *pool_name)
1027 const struct llapi_stripe_param param = {
1028 .lsp_stripe_count = stripe_count,
1029 .lsp_stripe_offset = stripe_offset,
1030 .lsp_stripe_pattern = stripe_pattern,
1031 .lsp_pool = (char *)pool_name
1034 return llapi_dir_set_default_lmv(name, ¶m);
1038 * Create a Lustre directory.
1040 * \param name the name of the directory to be created
1041 * \param mode permission of the file if it is created, see mode in open(2)
1042 * \param param stripe pattern of the newly created directory
1044 * \retval 0 on success
1045 * \retval negative errno on failure
1047 int llapi_dir_create(const char *name, mode_t mode,
1048 const struct llapi_stripe_param *param)
1050 struct lmv_user_md *lmu = NULL;
1052 struct obd_ioctl_data data = { 0 };
1055 char *dirpath = NULL;
1056 char *namepath = NULL;
1061 rc = verify_dir_param(name, param);
1065 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1066 param->lsp_is_specific ?
1067 LMV_USER_MAGIC_SPECIFIC :
1070 lmu = calloc(1, lmu_size);
1074 dirpath = strdup(name);
1080 namepath = strdup(name);
1087 param2lmu(lmu, param);
1089 filename = basename(namepath);
1090 dir = dirname(dirpath);
1092 data.ioc_inlbuf1 = (char *)filename;
1093 data.ioc_inllen1 = strlen(filename) + 1;
1094 data.ioc_inlbuf2 = (char *)lmu;
1095 data.ioc_inllen2 = lmu_size;
1096 data.ioc_type = mode;
1097 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1099 llapi_error(LLAPI_MSG_ERROR, rc,
1100 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1105 fd = open(dir, O_DIRECTORY | O_RDONLY);
1108 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1112 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1113 char *errmsg = "stripe already set";
1116 if (errno != EEXIST && errno != EALREADY)
1117 errmsg = strerror(errno);
1119 llapi_err_noerrno(LLAPI_MSG_ERROR,
1120 "dirstripe error on '%s': %s", name, errmsg);
1131 * Create a foreign directory.
1133 * \param name the name of the directory to be created
1134 * \param mode permission of the file if it is created, see mode in open(2)
1135 * \param type foreign type to be set in LMV EA
1136 * \param flags foreign flags to be set in LMV EA
1137 * \param value foreign pattern to be set in LMV EA
1139 * \retval 0 on success
1140 * \retval negative errno on failure
1142 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1143 __u32 flags, const char *value)
1145 struct lmv_foreign_md *lfm = NULL;
1146 size_t lfm_size, len;
1147 struct obd_ioctl_data data = { 0 };
1150 char *dirpath = NULL;
1151 char *namepath = NULL;
1156 len = strlen(value);
1157 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1160 llapi_error(LLAPI_MSG_ERROR, rc,
1161 "invalid LOV EA length %zu (must be 0 < len < %zu)",
1162 len, XATTR_SIZE_MAX -
1163 offsetof(struct lmv_foreign_md, lfm_value));
1166 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1167 lfm = calloc(1, lfm_size);
1171 dirpath = strdup(name);
1177 namepath = strdup(name);
1184 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1185 lfm->lfm_length = len;
1186 lfm->lfm_type = type;
1187 lfm->lfm_flags = flags;
1188 memcpy(lfm->lfm_value, value, len);
1190 filename = basename(namepath);
1191 dir = dirname(dirpath);
1193 data.ioc_inlbuf1 = (char *)filename;
1194 data.ioc_inllen1 = strlen(filename) + 1;
1195 data.ioc_inlbuf2 = (char *)lfm;
1196 data.ioc_inllen2 = lfm_size;
1197 data.ioc_type = mode;
1198 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1200 llapi_error(LLAPI_MSG_ERROR, rc,
1201 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1206 fd = open(dir, O_DIRECTORY | O_RDONLY);
1209 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1213 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1214 char *errmsg = "stripe already set";
1217 if (errno != EEXIST && errno != EALREADY)
1218 errmsg = strerror(errno);
1220 llapi_err_noerrno(LLAPI_MSG_ERROR,
1221 "dirstripe error on '%s': %s", name, errmsg);
1231 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1232 int stripe_count, int stripe_pattern,
1233 const char *pool_name)
1235 const struct llapi_stripe_param param = {
1236 .lsp_stripe_count = stripe_count,
1237 .lsp_stripe_offset = stripe_offset,
1238 .lsp_stripe_pattern = stripe_pattern,
1239 .lsp_pool = (char *)pool_name
1242 return llapi_dir_create(name, mode, ¶m);
1245 int llapi_direntry_remove(char *dname)
1247 char *dirpath = NULL;
1248 char *namepath = NULL;
1254 dirpath = strdup(dname);
1255 namepath = strdup(dname);
1256 if (!dirpath || !namepath)
1259 filename = basename(namepath);
1261 dir = dirname(dirpath);
1263 fd = open(dir, O_DIRECTORY | O_RDONLY);
1266 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
1271 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename))
1272 llapi_error(LLAPI_MSG_ERROR, errno,
1273 "error on ioctl %#lx for '%s' (%d)",
1274 (long)LL_IOC_LMV_SETSTRIPE, filename, fd);
1284 * Find the fsname, the full path, and/or an open fd.
1285 * Either the fsname or path must not be NULL
1287 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1290 char buf[PATH_MAX], mntdir[PATH_MAX];
1291 char *ptr, *ptr_end;
1293 int idx = 0, len = 0, mntlen, fd;
1295 int fsnamelen, mountlen;
1297 /* get the mount point */
1298 fp = setmntent(PROC_MOUNTS, "r");
1301 llapi_error(LLAPI_MSG_ERROR, rc,
1302 "cannot retrieve filesystem mount point");
1306 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1309 if (!llapi_is_lustre_mnt(&mnt))
1312 if ((want & WANT_INDEX) && (idx++ != index))
1315 mntlen = strlen(mnt.mnt_dir);
1316 ptr = strchr(mnt.mnt_fsname, '/');
1317 while (ptr && *ptr == '/')
1320 * thanks to the call to llapi_is_lustre_mnt() above,
1321 * we are sure that mnt.mnt_fsname contains ":/",
1322 * so ptr should never be NULL
1327 while (*ptr_end != '/' && *ptr_end != '\0')
1330 /* Check the fsname for a match, if given */
1331 mountlen = ptr_end - ptr;
1332 if (!(want & WANT_FSNAME) && fsname != NULL &&
1333 (fsnamelen = strlen(fsname)) > 0 &&
1334 (fsnamelen != mountlen ||
1335 (strncmp(ptr, fsname, mountlen) != 0)))
1338 /* If the path isn't set return the first one we find */
1339 if (path == NULL || strlen(path) == 0) {
1340 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1341 mntdir[sizeof(mntdir) - 1] = '\0';
1342 if ((want & WANT_FSNAME) && fsname != NULL) {
1343 strncpy(fsname, ptr, mountlen);
1344 fsname[mountlen] = '\0';
1348 /* Otherwise find the longest matching path */
1349 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1350 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1351 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1352 mntdir[sizeof(mntdir) - 1] = '\0';
1354 if ((want & WANT_FSNAME) && fsname != NULL) {
1355 strncpy(fsname, ptr, mountlen);
1356 fsname[mountlen] = '\0';
1365 if ((want & WANT_PATH) && path != NULL) {
1366 strncpy(path, mntdir, PATH_MAX);
1367 path[strlen(mntdir)] = '\0';
1369 if (want & WANT_FD) {
1370 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1373 llapi_error(LLAPI_MSG_ERROR, rc,
1374 "cannot open '%s'", mntdir);
1380 } else if (want & WANT_ERROR)
1381 llapi_err_noerrno(LLAPI_MSG_ERROR,
1382 "'%s' not on a mounted Lustre filesystem",
1383 (want & WANT_PATH) ? fsname : path);
1388 * search lustre mounts
1390 * Calling this function will return to the user the mount point, mntdir, and
1391 * the file system name, fsname, if the user passed a buffer to this routine.
1393 * The user inputs are pathname and index. If the pathname is supplied then
1394 * the value of the index will be ignored. The pathname will return data if
1395 * the pathname is located on a lustre mount. Index is used to pick which
1396 * mount point you want in the case of multiple mounted lustre file systems.
1397 * See function lfs_osts in lfs.c for an example of the index use.
1399 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1402 int want = WANT_PATH, idx = -1;
1404 if (!pathname || pathname[0] == '\0') {
1408 strcpy(mntdir, pathname);
1412 want |= WANT_FSNAME;
1413 return get_root_path(want, fsname, NULL, mntdir, idx);
1416 /* Given a path, find the corresponding Lustre fsname */
1417 int llapi_search_fsname(const char *pathname, char *fsname)
1422 path = realpath(pathname, NULL);
1424 char tmp[PATH_MAX - 1];
1430 if (pathname[0] != '/') {
1432 * Need an absolute path, but realpath() only works for
1433 * pathnames that actually exist. We go through the
1434 * extra hurdle of dirname(getcwd() + pathname) in
1435 * case the relative pathname contains ".." in it.
1437 char realpath[PATH_MAX - 1];
1439 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1441 llapi_error(LLAPI_MSG_ERROR, rc,
1442 "cannot get current working directory");
1446 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1447 if (rc >= sizeof(tmp)) {
1449 llapi_error(LLAPI_MSG_ERROR, rc,
1450 "invalid parent path '%s'",
1456 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1457 if (rc >= sizeof(buf)) {
1459 llapi_error(LLAPI_MSG_ERROR, rc,
1460 "invalid path '%s'", pathname);
1463 path = realpath(buf, NULL);
1465 ptr = strrchr(buf, '/');
1467 llapi_error(LLAPI_MSG_ERROR |
1468 LLAPI_MSG_NO_ERRNO, 0,
1469 "cannot resolve path '%s'",
1474 path = realpath(buf, NULL);
1477 llapi_error(LLAPI_MSG_ERROR, rc,
1478 "cannot resolve path '%s'",
1484 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1489 int llapi_search_rootpath(char *pathname, const char *fsname)
1492 * pathname can be used as an argument by get_root_path(),
1493 * clear it for safety
1496 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1500 * Get the list of pool members.
1501 * \param poolname string of format \<fsname\>.\<poolname\>
1502 * \param members caller-allocated array of char*
1503 * \param list_size size of the members array
1504 * \param buffer caller-allocated buffer for storing OST names
1505 * \param buffer_size size of the buffer
1507 * \return number of members retrieved for this pool
1508 * \retval -error failure
1510 int llapi_get_poolmembers(const char *poolname, char **members,
1511 int list_size, char *buffer, int buffer_size)
1513 char fsname[PATH_MAX];
1522 /* name is FSNAME.POOLNAME */
1523 if (strlen(poolname) >= sizeof(fsname))
1526 snprintf(fsname, sizeof(fsname), "%s", poolname);
1527 pool = strchr(fsname, '.');
1534 rc = poolpath(&pathname, fsname, NULL);
1536 llapi_error(LLAPI_MSG_ERROR, rc,
1537 "Lustre filesystem '%s' not found",
1542 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1543 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1544 cfs_free_param_data(&pathname);
1545 if (rc >= sizeof(buf))
1547 fd = fopen(buf, "r");
1550 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1555 while (fgets(buf, sizeof(buf), fd) != NULL) {
1556 if (nb_entries >= list_size) {
1560 buf[sizeof(buf) - 1] = '\0';
1562 tmp = strchr(buf, '\n');
1565 if (used + strlen(buf) + 1 > buffer_size) {
1570 strcpy(buffer + used, buf);
1571 members[nb_entries] = buffer + used;
1572 used += strlen(buf) + 1;
1582 * Get the list of pools in a filesystem.
1583 * \param name filesystem name or path
1584 * \param poollist caller-allocated array of char*
1585 * \param list_size size of the poollist array
1586 * \param buffer caller-allocated buffer for storing pool names
1587 * \param buffer_size size of the buffer
1589 * \return number of pools retrieved for this filesystem
1590 * \retval -error failure
1592 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1593 char *buffer, int buffer_size)
1599 struct dirent *pool;
1601 unsigned int nb_entries = 0;
1602 unsigned int used = 0;
1605 /* initialize output array */
1606 for (i = 0; i < list_size; i++)
1609 /* is name a pathname ? */
1610 ptr = strchr(name, '/');
1612 char fsname_buf[MAXNAMLEN];
1614 /* We will need fsname for printing later */
1615 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1619 ptr = strrchr(fsname_buf, '-');
1623 fsname = strdup(fsname_buf);
1627 /* name is FSNAME */
1628 fsname = strdup(name);
1633 rc = poolpath(&pathname, fsname, NULL);
1635 llapi_error(LLAPI_MSG_ERROR, rc,
1636 "Lustre filesystem '%s' not found", name);
1640 dir = opendir(pathname.gl_pathv[0]);
1643 llapi_error(LLAPI_MSG_ERROR, rc,
1644 "Could not open pool list for '%s'",
1651 pool = readdir(dir);
1657 /* ignore . and .. */
1658 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1661 /* check output bounds */
1662 if (nb_entries >= list_size) {
1664 goto free_dir_no_msg;
1667 /* +2 for '.' and final '\0' */
1668 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1671 goto free_dir_no_msg;
1674 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1675 poollist[nb_entries] = buffer + used;
1676 used += strlen(pool->d_name) + strlen(fsname) + 2;
1682 llapi_error(LLAPI_MSG_ERROR, rc,
1683 "Error reading pool list for '%s'", name);
1685 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1690 cfs_free_param_data(&pathname);
1693 return rc != 0 ? rc : nb_entries;
1696 /* wrapper for lfs.c and obd.c */
1697 int llapi_poollist(const char *name)
1700 * list of pool names (assume that pool count is smaller
1703 char **list, *buffer = NULL, *fsname = (char *)name;
1704 char *poolname = NULL, *tmp = NULL, data[16];
1705 enum param_filter type = FILTER_BY_PATH;
1706 int obdcount, bufsize, rc, nb, i;
1711 if (name[0] != '/') {
1712 fsname = strdup(name);
1716 poolname = strchr(fsname, '.');
1719 type = FILTER_BY_FS_NAME;
1722 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1723 data, sizeof(data));
1726 obdcount = atoi(data);
1729 * Allocate space for each fsname-OST0000_UUID, 1 per OST,
1730 * and also an array to store the pointers for all that
1734 bufsize = sizeof(struct obd_uuid) * obdcount;
1735 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1736 if (buffer == NULL) {
1740 list = (char **) (buffer + bufsize);
1743 /* name is a path or fsname */
1744 nb = llapi_get_poollist(name, list, obdcount,
1747 /* name is a pool name (<fsname>.<poolname>) */
1748 nb = llapi_get_poolmembers(name, list, obdcount,
1752 if (nb == -EOVERFLOW) {
1755 goto retry_get_pools;
1758 for (i = 0; i < nb; i++)
1759 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1760 rc = (nb < 0 ? nb : 0);
1764 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1769 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1770 void *data, struct dirent64 *de);
1772 #define OBD_NOT_FOUND (-1)
1774 static void find_param_fini(struct find_param *param)
1776 if (param->fp_migrate)
1779 if (param->fp_obd_indexes) {
1780 free(param->fp_obd_indexes);
1781 param->fp_obd_indexes = NULL;
1784 if (param->fp_lmd) {
1785 free(param->fp_lmd);
1786 param->fp_lmd = NULL;
1789 if (param->fp_lmv_md) {
1790 free(param->fp_lmv_md);
1791 param->fp_lmv_md = NULL;
1795 static int common_param_init(struct find_param *param, char *path)
1797 int lum_size = get_mds_md_size(path);
1802 /* migrate has fp_lmv_md initialized outside */
1803 if (param->fp_migrate)
1806 if (lum_size < PATH_MAX + 1)
1807 lum_size = PATH_MAX + 1;
1809 param->fp_lum_size = lum_size;
1810 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1812 if (param->fp_lmd == NULL) {
1813 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1814 "error: allocate %zu bytes for layout failed",
1815 sizeof(lstat_t) + param->fp_lum_size);
1819 param->fp_lmv_stripe_count = 256;
1820 param->fp_lmv_md = calloc(1,
1821 lmv_user_md_size(param->fp_lmv_stripe_count,
1822 LMV_USER_MAGIC_SPECIFIC));
1823 if (param->fp_lmv_md == NULL) {
1824 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1825 "error: allocation of %d bytes for ioctl",
1826 lmv_user_md_size(param->fp_lmv_stripe_count,
1827 LMV_USER_MAGIC_SPECIFIC));
1828 find_param_fini(param);
1832 param->fp_got_uuids = 0;
1833 param->fp_obd_indexes = NULL;
1834 param->fp_obd_index = OBD_NOT_FOUND;
1835 param->fp_mdt_index = OBD_NOT_FOUND;
1839 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1840 struct dirent64 *de)
1842 struct find_param *param = data;
1848 /* set errno upon failure */
1849 static DIR *opendir_parent(const char *path)
1855 path_copy = strdup(path);
1856 if (path_copy == NULL)
1859 parent_path = dirname(path_copy);
1860 parent = opendir(parent_path);
1866 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1871 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1872 if (param->fp_get_default_lmv)
1873 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1875 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1877 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1878 if (errno == E2BIG && ret != 0) {
1882 /* if foreign LMV case, fake stripes number */
1883 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
1884 struct lmv_foreign_md *lfm;
1886 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1887 if (lfm->lfm_length < XATTR_SIZE_MAX -
1888 offsetof(typeof(*lfm), lfm_value)) {
1889 uint32_t size = lfm->lfm_length +
1890 offsetof(typeof(*lfm), lfm_value);
1892 stripe_count = lmv_foreign_to_md_stripes(size);
1894 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1895 "error: invalid %d foreign size returned from ioctl",
1900 stripe_count = param->fp_lmv_md->lum_stripe_count;
1902 if (stripe_count <= param->fp_lmv_stripe_count)
1905 free(param->fp_lmv_md);
1906 param->fp_lmv_stripe_count = stripe_count;
1907 lmv_size = lmv_user_md_size(stripe_count,
1908 LMV_USER_MAGIC_SPECIFIC);
1909 param->fp_lmv_md = malloc(lmv_size);
1910 if (param->fp_lmv_md == NULL) {
1911 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1912 "error: allocation of %d bytes for ioctl",
1913 lmv_user_md_size(param->fp_lmv_stripe_count,
1914 LMV_USER_MAGIC_SPECIFIC));
1922 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1925 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
1926 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
1927 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
1928 lmd_v2->lmd_stx.stx_uid = st->st_uid;
1929 lmd_v2->lmd_stx.stx_gid = st->st_gid;
1930 lmd_v2->lmd_stx.stx_mode = st->st_mode;
1931 lmd_v2->lmd_stx.stx_ino = st->st_ino;
1932 lmd_v2->lmd_stx.stx_size = st->st_size;
1933 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
1934 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
1935 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
1936 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
1937 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
1938 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
1939 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
1940 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
1941 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
1943 lmd_v2->lmd_flags = 0;
1945 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1947 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
1948 if (lmd_v2->lmd_stx.stx_size)
1949 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
1950 if (lmd_v2->lmd_stx.stx_blocks)
1951 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
1953 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1954 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
1955 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
1960 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
1962 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
1963 struct lov_user_mds_data *lmd_v2 = lmdbuf;
1967 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
1971 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
1974 st = lmd_v1->lmd_st;
1975 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
1976 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
1977 convert_lmd_statx(lmd_v2, &st, false);
1978 lmd_v2->lmd_lmmsize = 0;
1979 lmd_v2->lmd_padding = 0;
1984 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
1985 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1987 struct lov_user_mds_data *lmd = lmdbuf;
1988 static bool use_old_ioctl;
1992 if (parent_fd < 0 && dir_fd < 0)
1994 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1999 * LL_IOC_MDC_GETINFO operates on the current directory inode
2000 * and returns struct lov_user_mds_data, while
2001 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
2003 if (type == GET_LMD_INFO)
2004 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_OLD :
2007 cmd = LL_IOC_LOV_GETSTRIPE;
2010 ret = ioctl(dir_fd, cmd, lmdbuf);
2011 if (ret < 0 && errno == ENOTTY && cmd == LL_IOC_MDC_GETINFO) {
2012 cmd = LL_IOC_MDC_GETINFO_OLD;
2013 use_old_ioctl = true;
2017 if (cmd == LL_IOC_MDC_GETINFO_OLD && !ret)
2018 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2019 } else if (parent_fd >= 0) {
2020 const char *fname = strrchr(path, '/');
2023 * IOC_MDC_GETFILEINFO takes as input the filename (relative to
2024 * the parent directory) and returns struct lov_user_mds_data,
2025 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2027 * This avoids opening, locking, and closing each file on the
2028 * client if that is not needed. Multiple of these ioctl() can
2029 * be done on the parent dir with a single open for all
2030 * files in that directory, and it also doesn't pollute the
2031 * client dcache with millions of dentries when traversing
2032 * a large filesystem.
2034 fname = (fname == NULL ? path : fname + 1);
2036 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2039 else if (ret >= lmdlen || ret++ == 0)
2042 if (type == GET_LMD_INFO)
2043 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_OLD :
2044 IOC_MDC_GETFILEINFO;
2046 cmd = IOC_MDC_GETFILESTRIPE;
2049 ret = ioctl(parent_fd, cmd, lmdbuf);
2050 if (ret < 0 && errno == ENOTTY &&
2051 cmd == IOC_MDC_GETFILEINFO) {
2052 cmd = IOC_MDC_GETFILEINFO_OLD;
2053 use_old_ioctl = true;
2054 goto retry_getfileinfo;
2057 if (cmd == IOC_MDC_GETFILEINFO_OLD && !ret)
2058 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2062 if (ret && type == GET_LMD_INFO) {
2063 if (errno == ENOTTY) {
2067 * ioctl is not supported, it is not a lustre fs.
2068 * Do the regular lstat(2) instead.
2070 ret = lstat_f(path, &st);
2073 llapi_error(LLAPI_MSG_ERROR, ret,
2074 "error: %s: lstat failed for %s",
2078 convert_lmd_statx(lmd, &st, true);
2080 * It may be wrong to set use_old_ioctl with true as
2081 * the file is not a lustre fs. So reset it with false
2084 use_old_ioctl = false;
2085 } else if (errno == ENOENT) {
2087 llapi_error(LLAPI_MSG_WARN, ret,
2088 "warning: %s does not exist", path);
2089 } else if (errno != EISDIR && errno != ENODATA) {
2091 llapi_error(LLAPI_MSG_ERROR, ret,
2092 "%s ioctl failed for %s.",
2093 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2094 "IOC_MDC_GETFILEINFO", path);
2101 static int get_lmd_info(char *path, DIR *parent, DIR *dir, void *lmdbuf,
2102 int lmdlen, enum get_lmd_info_type type)
2108 parent_fd = dirfd(parent);
2110 dir_fd = dirfd(dir);
2112 return get_lmd_info_fd(path, parent_fd, dir_fd, lmdbuf, lmdlen, type);
2115 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
2116 semantic_func_t sem_init,
2117 semantic_func_t sem_fini, void *data,
2118 struct dirent64 *de)
2120 struct find_param *param = (struct find_param *)data;
2121 struct dirent64 *dent;
2129 if (!d && errno != ENOTDIR) {
2131 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2134 } else if (!d && !parent) {
2135 /* ENOTDIR. Open the parent dir. */
2136 p = opendir_parent(path);
2143 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
2149 while ((dent = readdir64(d)) != NULL) {
2152 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2156 if ((len + dent->d_reclen + 2) > size) {
2157 llapi_err_noerrno(LLAPI_MSG_ERROR,
2158 "error: %s: string buffer too small",
2163 strcat(path, dent->d_name);
2165 if (dent->d_type == DT_UNKNOWN) {
2166 struct lov_user_mds_data *lmd = param->fp_lmd;
2168 rc = get_lmd_info(path, d, NULL, lmd,
2169 param->fp_lum_size, GET_LMD_INFO);
2171 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2178 switch (dent->d_type) {
2180 llapi_err_noerrno(LLAPI_MSG_ERROR,
2181 "error: %s: '%s' is UNKNOWN type %d",
2182 __func__, dent->d_name, dent->d_type);
2185 rc = llapi_semantic_traverse(path, size, d, sem_init,
2186 sem_fini, data, dent);
2187 if (rc != 0 && ret == 0)
2193 rc = sem_init(path, d, NULL, data, dent);
2194 if (rc < 0 && ret == 0) {
2199 if (sem_fini && rc == 0)
2200 sem_fini(path, d, NULL, data, dent);
2208 sem_fini(path, parent, &d, data, de);
2217 static int param_callback(char *path, semantic_func_t sem_init,
2218 semantic_func_t sem_fini, struct find_param *param)
2220 int ret, len = strlen(path);
2223 if (len > PATH_MAX) {
2225 llapi_error(LLAPI_MSG_ERROR, ret,
2226 "Path name '%s' is too long", path);
2230 buf = (char *)malloc(PATH_MAX + 1);
2234 snprintf(buf, PATH_MAX + 1, "%s", path);
2235 ret = common_param_init(param, buf);
2239 param->fp_depth = 0;
2241 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
2242 sem_fini, param, NULL);
2244 find_param_fini(param);
2246 return ret < 0 ? ret : 0;
2249 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2251 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
2255 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2260 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2262 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2266 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2271 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2275 fd = open(path, O_RDONLY | O_NONBLOCK);
2278 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
2282 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2288 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2292 fd = open(path, O_RDONLY | O_NONBLOCK);
2295 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2299 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2311 * If uuidp is NULL, return the number of available obd uuids.
2312 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2313 * there are more OSTs than allocated to uuidp, then an error is returned with
2314 * the ost_count set to number of available obd uuids.
2316 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2317 int *ost_count, enum tgt_type type)
2319 char buf[PATH_MAX], format[32];
2320 int rc = 0, index = 0;
2321 struct obd_uuid name;
2325 /* Get the lov name */
2326 if (type == LOV_TYPE)
2327 rc = llapi_file_fget_lov_uuid(fd, &name);
2329 rc = llapi_file_fget_lmv_uuid(fd, &name);
2333 /* Now get the ost uuids */
2334 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2335 FILTER_BY_EXACT, "target_obd", ¶m);
2339 fp = fopen(param.gl_pathv[0], "r");
2342 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2347 snprintf(format, sizeof(format),
2348 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2349 while (fgets(buf, sizeof(buf), fp) != NULL) {
2350 if (uuidp && (index < *ost_count)) {
2351 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2359 if (uuidp && (index > *ost_count))
2364 cfs_free_param_data(¶m);
2368 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2370 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2373 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2378 root = open(mnt, O_RDONLY | O_DIRECTORY);
2381 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2386 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2395 * Check if user specified value matches a real uuid. Ignore _UUID,
2396 * -osc-4ba41334, other trailing gunk in comparison.
2397 * @param real_uuid ends in "_UUID"
2398 * @param search_uuid may or may not end in "_UUID"
2400 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2402 int cmplen = strlen(real_uuid);
2403 int searchlen = strlen(search_uuid);
2405 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2407 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2411 * The UUIDs may legitimately be different lengths, if
2412 * the system was upgraded from an older version.
2414 if (cmplen != searchlen)
2417 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2421 * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2422 * returned in param->fp_obd_index
2424 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2426 struct obd_uuid obd_uuid;
2433 if (param->fp_got_uuids)
2436 /* Get the lov/lmv name */
2437 if (param->fp_get_lmv)
2438 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2440 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2442 if (rc != -ENOTTY) {
2443 llapi_error(LLAPI_MSG_ERROR, rc,
2444 "error: can't get %s name: %s",
2445 param->fp_get_lmv ? "lmv" : "lov",
2453 param->fp_got_uuids = 1;
2455 /* Now get the ost uuids */
2456 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2457 obd_uuid.uuid, FILTER_BY_EXACT,
2458 "target_obd", ¶m_data);
2462 fp = fopen(param_data.gl_pathv[0], "r");
2465 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2466 param_data.gl_pathv[0]);
2470 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2471 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2472 param->fp_get_lmv ? "MDTS" : "OBDS");
2474 snprintf(format, sizeof(format),
2475 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2476 while (fgets(buf, sizeof(buf), fp) != NULL) {
2479 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2482 if (param->fp_obd_uuid) {
2483 if (llapi_uuid_match(obd_uuid.uuid,
2484 param->fp_obd_uuid->uuid)) {
2485 param->fp_obd_index = index;
2488 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2489 /* Print everything */
2490 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2493 param->fp_obds_printed = 1;
2497 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2498 llapi_err_noerrno(LLAPI_MSG_ERROR,
2499 "error: %s: unknown obduuid: %s",
2500 __func__, param->fp_obd_uuid->uuid);
2504 cfs_free_param_data(¶m_data);
2509 * In this case, param->fp_obd_uuid will be an array of obduuids and
2510 * obd index for all these obduuids will be returned in
2511 * param->fp_obd_indexes
2513 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2514 int num_obds, int **obdindexes, int *obdindex,
2517 int ret, obdcount, obd_valid = 0, obdnum;
2519 struct obd_uuid *uuids = NULL;
2523 if (type == LOV_TYPE)
2524 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2526 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2530 obdcount = atoi(buf);
2531 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2536 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2538 if (ret == -EOVERFLOW) {
2539 struct obd_uuid *uuids_temp;
2541 uuids_temp = realloc(uuids, obdcount *
2542 sizeof(struct obd_uuid));
2543 if (uuids_temp != NULL) {
2545 goto retry_get_uuids;
2550 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2554 indexes = malloc(num_obds * sizeof(*obdindex));
2555 if (indexes == NULL) {
2560 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2563 /* The user may have specified a simple index */
2564 i = strtol(obduuids[obdnum].uuid, &end, 0);
2565 if (end && *end == '\0' && i < obdcount) {
2566 indexes[obdnum] = i;
2569 for (i = 0; i < obdcount; i++) {
2570 if (llapi_uuid_match(uuids[i].uuid,
2571 obduuids[obdnum].uuid)) {
2572 indexes[obdnum] = i;
2578 if (i >= obdcount) {
2579 indexes[obdnum] = OBD_NOT_FOUND;
2580 llapi_err_noerrno(LLAPI_MSG_ERROR,
2581 "invalid obduuid '%s'",
2582 obduuids[obdnum].uuid);
2588 *obdindex = OBD_NOT_FOUND;
2590 *obdindex = obd_valid;
2592 *obdindexes = indexes;
2600 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2604 if (param->fp_mdt_uuid) {
2605 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2607 ¶m->fp_mdt_indexes,
2608 ¶m->fp_mdt_index, LMV_TYPE);
2613 if (param->fp_obd_uuid) {
2614 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2616 ¶m->fp_obd_indexes,
2617 ¶m->fp_obd_index, LOV_TYPE);
2622 param->fp_got_uuids = 1;
2627 int llapi_ostlist(char *path, struct find_param *param)
2632 fd = open(path, O_RDONLY | O_DIRECTORY);
2636 ret = setup_obd_uuid(fd, path, param);
2643 * Tries to determine the default stripe attributes for a given filesystem. The
2644 * filesystem to check should be specified by fsname, or will be determined
2647 static int sattr_get_defaults(const char *const fsname,
2648 unsigned int *scount,
2649 unsigned int *ssize,
2650 unsigned int *soffset)
2656 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2657 "stripecount", val, sizeof(val));
2660 *scount = atoi(val);
2664 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2665 "stripesize", val, sizeof(val));
2672 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2673 "stripeoffset", val, sizeof(val));
2676 *soffset = atoi(val);
2683 * Tries to gather the default stripe attributes for a given filesystem. If
2684 * the attributes can be determined, they are cached for easy retreival the
2685 * next time they are needed. Only a single filesystem's attributes are
2688 int sattr_cache_get_defaults(const char *const fsname,
2689 const char *const pathname, unsigned int *scount,
2690 unsigned int *ssize, unsigned int *soffset)
2693 char fsname[PATH_MAX + 1];
2694 unsigned int stripecount;
2695 unsigned int stripesize;
2696 unsigned int stripeoffset;
2702 char fsname_buf[PATH_MAX + 1];
2703 unsigned int tmp[3];
2705 if (fsname == NULL) {
2706 rc = llapi_search_fsname(pathname, fsname_buf);
2710 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2713 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2715 * Ensure all 3 sattrs (count, size, and offset) are
2716 * successfully retrieved and stored in tmp before writing to
2719 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2723 cache.stripecount = tmp[0];
2724 cache.stripesize = tmp[1];
2725 cache.stripeoffset = tmp[2];
2726 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2730 *scount = cache.stripecount;
2732 *ssize = cache.stripesize;
2734 *soffset = cache.stripeoffset;
2739 static char *layout2name(__u32 layout_pattern)
2741 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2743 else if (layout_pattern == LOV_PATTERN_MDT)
2745 else if (layout_pattern == LOV_PATTERN_RAID0)
2747 else if (layout_pattern ==
2748 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2749 return "raid0,overstriped";
2754 enum lov_dump_flags {
2755 LDF_IS_DIR = 0x0001,
2756 LDF_IS_RAW = 0x0002,
2757 LDF_INDENT = 0x0004,
2758 LDF_SKIP_OBJS = 0x0008,
2760 LDF_EXTENSION = 0x0020,
2763 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2764 struct lov_user_ost_data_v1 *objects,
2765 enum llapi_layout_verbose verbose,
2766 int depth, char *pool_name,
2767 enum lov_dump_flags flags)
2769 bool is_dir = flags & LDF_IS_DIR;
2770 bool is_raw = flags & LDF_IS_RAW;
2771 bool indent = flags & LDF_INDENT;
2772 bool yaml = flags & LDF_YAML;
2773 bool skip_objs = flags & LDF_SKIP_OBJS;
2774 bool extension = flags & LDF_EXTENSION;
2775 char *prefix = is_dir ? "" : "lmm_";
2776 char *separator = "";
2777 char *space = indent ? " " : "";
2780 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2781 lmm_oi_set_seq(&lum->lmm_oi, 0);
2782 if (!indent && (verbose & VERBOSE_DETAIL))
2783 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2786 if (!yaml && !indent && depth && path &&
2787 ((verbose != VERBOSE_OBJID) || !is_dir))
2788 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2790 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2791 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2792 space, prefix, lum->lmm_magic);
2793 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2795 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2796 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2798 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2800 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2805 if (verbose & ~VERBOSE_DFID)
2806 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2809 * This needs a bit of hand-holding since old 1.x lmm_oi
2810 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2811 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2812 * a real FID. Ideally the 2.x code would have stored this
2813 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2814 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2815 * worked properly (especially since IGIF FIDs use mds_inum as
2816 * the FID SEQ), but unfortunately that didn't happen.
2818 * Print it to look like an IGIF FID, even though the fields
2819 * are reversed on disk, so that it makes sense to userspace.
2821 * Don't use ostid_id() and ostid_seq(), since they assume the
2822 * oi_fid fields are in the right order. This is why there are
2823 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2825 * For newer layout types hopefully this will be a real FID.
2827 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2828 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2829 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2830 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2831 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2833 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2834 (unsigned long long)seq, oid, ver);
2836 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2837 (unsigned long long)seq, oid, ver);
2840 if (verbose & VERBOSE_STRIPE_COUNT) {
2841 if (verbose & ~VERBOSE_STRIPE_COUNT)
2842 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2845 if (!is_raw && lum->lmm_stripe_count == 0 &&
2846 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2847 unsigned int scount;
2849 rc = sattr_cache_get_defaults(NULL, path,
2853 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2856 llapi_error(LLAPI_MSG_ERROR, rc,
2857 "Cannot determine default stripe count.");
2859 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2861 (__s16)lum->lmm_stripe_count);
2864 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2866 (__s16)lum->lmm_stripe_count);
2868 if (!yaml && is_dir)
2874 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
2875 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
2876 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2877 if (verbose & ~VERBOSE_EXT_SIZE && extension)
2878 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
2880 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
2881 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2883 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2886 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2889 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2891 llapi_error(LLAPI_MSG_ERROR, rc,
2892 "Cannot determine default stripe size.");
2894 /* Extension size is in KiB */
2895 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2897 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
2898 (unsigned long long)lum->lmm_stripe_size);
2900 if (!yaml && is_dir)
2906 if ((verbose & VERBOSE_PATTERN)) {
2907 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2908 if (verbose & ~VERBOSE_PATTERN)
2909 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2911 if (lov_pattern_supported(lum->lmm_pattern))
2912 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2913 layout2name(lum->lmm_pattern));
2915 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2916 separator = (!yaml && is_dir) ? " " : "\n";
2919 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2920 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2921 if (verbose & ~VERBOSE_GENERATION)
2922 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2924 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2925 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2929 if (verbose & VERBOSE_STRIPE_OFFSET) {
2930 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2931 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2932 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2934 if (is_dir || skip_objs)
2935 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2936 lum->lmm_stripe_offset ==
2937 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2938 lum->lmm_stripe_offset);
2939 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
2940 llapi_printf(LLAPI_MSG_NORMAL, "0");
2942 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2943 objects[0].l_ost_idx);
2944 if (!yaml && is_dir)
2950 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2951 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2952 if (verbose & ~VERBOSE_POOL)
2953 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2955 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2956 if (!yaml && is_dir)
2962 if (strlen(separator) != 0)
2963 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2966 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2967 struct lov_user_ost_data_v1 *objects,
2968 char *path, int obdindex, int depth,
2969 enum llapi_layout_verbose verbose,
2970 enum lov_dump_flags flags)
2972 bool is_dir = flags & LDF_IS_DIR;
2973 bool indent = flags & LDF_INDENT;
2974 bool skip_objs = flags & LDF_SKIP_OBJS;
2975 bool yaml = flags & LDF_YAML;
2976 bool obdstripe = obdindex == OBD_NOT_FOUND;
2979 if (!obdstripe && !skip_objs) {
2980 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2981 if (obdindex == objects[i].l_ost_idx) {
2991 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2994 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
2995 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2996 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
2997 char *space = " - ";
3000 llapi_printf(LLAPI_MSG_NORMAL,
3001 "%6slmm_objects:\n", " ");
3003 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
3005 llapi_printf(LLAPI_MSG_NORMAL,
3006 "\tobdidx\t\t objid\t\t objid\t\t group\n");
3008 for (i = 0; i < lum->lmm_stripe_count; i++) {
3009 int idx = objects[i].l_ost_idx;
3010 long long oid = ostid_id(&objects[i].l_ost_oi);
3011 long long gr = ostid_seq(&objects[i].l_ost_oi);
3013 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
3017 struct lu_fid fid = { 0 };
3019 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3020 llapi_printf(LLAPI_MSG_NORMAL,
3021 "%sl_ost_idx: %d\n", space, idx);
3022 llapi_printf(LLAPI_MSG_NORMAL,
3023 "%8sl_fid: "DFID_NOBRACE"\n",
3025 } else if (indent) {
3026 struct lu_fid fid = { 0 };
3028 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3029 llapi_printf(LLAPI_MSG_NORMAL,
3030 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
3031 space, i, idx, PFID(&fid));
3035 sprintf(fmt, "%s%s%s\n",
3036 "\t%6u\t%14llu\t%#13llx\t",
3037 (fid_seq_is_rsvd(gr) ||
3038 fid_seq_is_mdt0(gr)) ?
3039 "%14llu" : "%#14llx", "%s");
3040 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
3042 obdindex == idx ? " *" : "");
3046 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3049 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3050 char *path, int obdindex, int depth,
3051 enum llapi_layout_verbose verbose,
3052 enum lov_dump_flags flags)
3054 struct lmv_user_mds_data *objects = lum->lum_objects;
3055 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3056 char *separator = "";
3057 bool yaml = flags & LDF_YAML;
3058 bool obdstripe = false;
3061 if (obdindex != OBD_NOT_FOUND) {
3062 if (lum->lum_stripe_count == 0) {
3063 if (obdindex == lum->lum_stripe_offset)
3066 for (i = 0; i < lum->lum_stripe_count; i++) {
3067 if (obdindex == objects[i].lum_mds) {
3068 llapi_printf(LLAPI_MSG_NORMAL,
3083 /* show all information default */
3085 if (lum->lum_magic == LMV_USER_MAGIC)
3086 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3087 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3089 verbose = VERBOSE_OBJID;
3092 if (depth && path && ((verbose != VERBOSE_OBJID)))
3093 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3095 if (verbose & VERBOSE_STRIPE_COUNT) {
3096 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3097 if (verbose & ~VERBOSE_STRIPE_COUNT)
3098 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3099 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3100 (int)lum->lum_stripe_count);
3101 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3107 if (verbose & VERBOSE_STRIPE_OFFSET) {
3108 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3109 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3110 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3111 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3112 (int)lum->lum_stripe_offset);
3113 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3119 if (verbose & VERBOSE_HASH_TYPE) {
3120 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3121 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3123 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3124 if (verbose & ~VERBOSE_HASH_TYPE)
3125 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3126 if (type < LMV_HASH_TYPE_MAX)
3127 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3128 mdt_hash_name[type]);
3130 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3132 if (flags & LMV_HASH_FLAG_MIGRATION)
3133 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3134 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3135 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3136 if (flags & LMV_HASH_FLAG_LOST_LMV)
3137 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3142 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3143 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3144 if (lum->lum_stripe_count > 0)
3145 llapi_printf(LLAPI_MSG_NORMAL,
3146 "mdtidx\t\t FID[seq:oid:ver]\n");
3147 for (i = 0; i < lum->lum_stripe_count; i++) {
3148 int idx = objects[i].lum_mds;
3149 struct lu_fid *fid = &objects[i].lum_fid;
3151 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3152 llapi_printf(LLAPI_MSG_NORMAL,
3153 "%6u\t\t "DFID"\t\t%s\n",
3155 obdindex == idx ? " *" : "");
3159 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3160 pool_name[0] != '\0') {
3161 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3162 if (verbose & ~VERBOSE_POOL)
3163 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3165 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3169 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3170 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3173 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3174 enum lov_dump_flags flags)
3176 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3177 int depth = param->fp_max_depth;
3178 enum llapi_layout_verbose verbose = param->fp_verbose;
3179 bool yaml = flags & LDF_YAML;
3181 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3182 !(flags & LDF_IS_DIR)) && !yaml)
3183 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3185 if (verbose & VERBOSE_DETAIL) {
3186 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3187 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3188 " ", comp_v1->lcm_magic);
3189 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3190 " ", comp_v1->lcm_size);
3191 if (flags & LDF_IS_DIR)
3192 llapi_printf(LLAPI_MSG_NORMAL,
3193 "%2slcm_flags: %s\n", " ",
3194 comp_v1->lcm_mirror_count > 0 ?
3197 llapi_printf(LLAPI_MSG_NORMAL,
3198 "%2slcm_flags: %s\n", " ",
3199 llapi_layout_flags_string(comp_v1->lcm_flags));
3202 if (verbose & VERBOSE_GENERATION) {
3203 if (verbose & ~VERBOSE_GENERATION)
3204 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3206 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3209 if (verbose & VERBOSE_MIRROR_COUNT) {
3210 if (verbose & ~VERBOSE_MIRROR_COUNT)
3211 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3213 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3214 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3215 comp_v1->lcm_mirror_count + 1 : 1);
3218 if (verbose & VERBOSE_COMP_COUNT) {
3219 if (verbose & ~VERBOSE_COMP_COUNT)
3220 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3222 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3223 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3224 comp_v1->lcm_entry_count : 0);
3227 if (verbose & VERBOSE_DETAIL && !yaml)
3228 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3231 static void lcme_flags2str(__u32 comp_flags)
3237 llapi_printf(LLAPI_MSG_NORMAL, "0");
3240 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3241 if (comp_flags & comp_flags_table[i].cfn_flag) {
3243 llapi_printf(LLAPI_MSG_NORMAL, ",");
3244 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3245 comp_flags_table[i].cfn_name);
3246 comp_flags &= ~comp_flags_table[i].cfn_flag;
3252 llapi_printf(LLAPI_MSG_NORMAL, ",");
3253 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3257 static void lov_dump_comp_v1_entry(struct find_param *param,
3258 enum lov_dump_flags flags, int index)
3260 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3261 struct lov_comp_md_entry_v1 *entry;
3262 char *separator = "";
3263 enum llapi_layout_verbose verbose = param->fp_verbose;
3264 bool yaml = flags & LDF_YAML;
3266 entry = &comp_v1->lcm_entries[index];
3269 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3271 if (verbose & VERBOSE_COMP_ID) {
3272 if (verbose & VERBOSE_DETAIL && !yaml)
3273 llapi_printf(LLAPI_MSG_NORMAL,
3274 "%slcme_id: ", " - ");
3275 else if (verbose & ~VERBOSE_COMP_ID)
3276 llapi_printf(LLAPI_MSG_NORMAL,
3277 "%4slcme_id: ", " ");
3278 if (entry->lcme_id != LCME_ID_INVAL)
3279 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3281 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3285 if (verbose & VERBOSE_MIRROR_ID) {
3286 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3287 if (verbose & ~VERBOSE_MIRROR_ID)
3288 llapi_printf(LLAPI_MSG_NORMAL,
3289 "%4slcme_mirror_id: ", " ");
3290 if (entry->lcme_id != LCME_ID_INVAL)
3291 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3292 mirror_id_of(entry->lcme_id));
3294 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3298 if (verbose & VERBOSE_COMP_FLAGS) {
3299 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3300 if (verbose & ~VERBOSE_COMP_FLAGS)
3301 llapi_printf(LLAPI_MSG_NORMAL,
3302 "%4slcme_flags: ", " ");
3303 lcme_flags2str(entry->lcme_flags);
3306 /* print snapshot timestamp if its a nosync comp */
3307 if ((verbose & VERBOSE_COMP_FLAGS) &&
3308 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3309 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3310 if (verbose & ~VERBOSE_COMP_FLAGS)
3311 llapi_printf(LLAPI_MSG_NORMAL,
3312 "%4slcme_timestamp: ", " ");
3314 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3315 (unsigned long long)entry->lcme_timestamp);
3317 time_t stamp = entry->lcme_timestamp;
3318 char *date_str = asctime(localtime(&stamp));
3320 date_str[strlen(date_str) - 1] = '\0';
3321 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3327 if (verbose & VERBOSE_COMP_START) {
3328 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3329 if (verbose & ~VERBOSE_COMP_START)
3330 llapi_printf(LLAPI_MSG_NORMAL,
3331 "%4slcme_extent.e_start: ", " ");
3332 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3333 (unsigned long long)entry->lcme_extent.e_start);
3337 if (verbose & VERBOSE_COMP_END) {
3338 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3339 if (verbose & ~VERBOSE_COMP_END)
3340 llapi_printf(LLAPI_MSG_NORMAL,
3341 "%4slcme_extent.e_end: ", " ");
3342 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3343 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3345 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3346 (unsigned long long)entry->lcme_extent.e_end);
3351 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3352 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3353 } else if (verbose & VERBOSE_DETAIL) {
3354 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3355 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3356 " ", entry->lcme_offset);
3357 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3358 " ", entry->lcme_size);
3359 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3361 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3366 * Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3367 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3369 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3370 * The table below gives the answers for the specified parameters (value and
3371 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3372 * --------------------------------------
3373 * 1 | file > limit; sign > 0 | -1 / -1 |
3374 * 2 | file = limit; sign > 0 | -1 / -1 |
3375 * 3 | file < limit; sign > 0 | ? / 1 |
3376 * 4 | file > limit; sign = 0 | -1 / -1 |
3377 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3378 * 6 | file < limit; sign = 0 | ? / -1 |
3379 * 7 | file > limit; sign < 0 | 1 / 1 |
3380 * 8 | file = limit; sign < 0 | ? / -1 |
3381 * 9 | file < limit; sign < 0 | ? / -1 |
3382 * --------------------------------------
3383 * Note: 5th actually means that the value is within the interval
3384 * (limit - margin, limit].
3386 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3387 int sign, int negopt, unsigned long long margin,
3393 /* Drop the fraction of margin (of days or size). */
3394 if (file + margin <= limit)
3396 } else if (sign == 0) {
3397 if (file <= limit && file + margin > limit)
3399 else if (file + margin <= limit)
3401 } else if (sign < 0) {
3408 return negopt ? ~ret + 1 : ret;
3411 static inline struct lov_user_md *
3412 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3414 return (struct lov_user_md *)((char *)comp_v1 +
3415 comp_v1->lcm_entries[ent_idx].lcme_offset);
3418 static inline struct lov_user_ost_data_v1 *
3419 lov_v1v3_objects(struct lov_user_md *v1)
3421 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3422 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3424 return v1->lmm_objects;
3428 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3430 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3431 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3432 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3434 pool_name[0] = '\0';
3438 print_last_init_comp(struct find_param *param)
3440 /* print all component info */
3441 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3444 /* print specific component info */
3445 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3446 param->fp_check_comp_start || param->fp_check_comp_end ||
3447 param->fp_check_mirror_id || param->fp_check_mirror_index)
3453 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3457 if (param->fp_comp_end == LUSTRE_EOF) {
3458 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3459 match = end == LUSTRE_EOF ? 1 : -1;
3460 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3461 match = end == LUSTRE_EOF ? -1 : 1;
3462 else /* at least EOF */
3464 if (param->fp_exclude_comp_end)
3467 unsigned long long margin;
3469 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3470 match = find_value_cmp(end, param->fp_comp_end,
3471 param->fp_comp_end_sign,
3472 param->fp_exclude_comp_end, margin, 0);
3479 * An example of "getstripe -v" for a two components PFL file:
3482 * lcm_magic: 0x0BD60BD0
3486 * lcm_entry_count: 2
3490 * lcme_extent.e_start: 0
3491 * lcme_extent.e_end: 1048576
3495 * lmm_magic: 0x0BD10BD0
3496 * lmm_seq: 0x200000401
3497 * lmm_object_id: 0x1
3498 * lmm_fid: [0x200000401:0x1:0x0]
3499 * lmm_stripe_count: 1
3500 * lmm_stripe_size: 1048576
3501 * lmm_pattern: raid0
3503 * lmm_stripe_offset: 0
3505 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3509 * lcme_extent.e_start: 1048576
3510 * lcme_extent.e_end: EOF
3514 * lmm_magic: 0x0BD10BD0
3515 * lmm_seq: 0x200000401
3516 * lmm_object_id: 0x1
3517 * lmm_fid: [0x200000401:0x1:0x0]
3518 * lmm_stripe_count: 2
3519 * lmm_stripe_size: 1048576
3520 * lmm_pattern: raid0
3522 * lmm_stripe_offset: 1
3524 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3525 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3527 static void lov_dump_comp_v1(struct find_param *param, char *path,
3528 enum lov_dump_flags flags)
3530 struct lov_comp_md_entry_v1 *entry;
3531 struct lov_user_ost_data_v1 *objects;
3532 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3533 struct lov_user_md_v1 *v1;
3534 char pool_name[LOV_MAXPOOLNAME + 1];
3535 int obdindex = param->fp_obd_index;
3536 int i, j, match, ext;
3537 bool obdstripe = false;
3538 __u16 mirror_index = 0;
3539 __u16 mirror_id = 0;
3541 if (obdindex != OBD_NOT_FOUND) {
3542 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3543 i < comp_v1->lcm_entry_count; i++) {
3544 if (!(comp_v1->lcm_entries[i].lcme_flags &
3548 v1 = lov_comp_entry(comp_v1, i);
3549 objects = lov_v1v3_objects(v1);
3551 for (j = 0; j < v1->lmm_stripe_count; j++) {
3552 if (obdindex == objects[j].l_ost_idx) {
3565 lov_dump_comp_v1_header(param, path, flags);
3567 flags |= LDF_INDENT;
3569 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3570 entry = &comp_v1->lcm_entries[i];
3572 if (param->fp_check_comp_flags) {
3573 if (((param->fp_comp_flags & entry->lcme_flags) !=
3574 param->fp_comp_flags) ||
3575 (param->fp_comp_neg_flags & entry->lcme_flags))
3579 if (param->fp_check_comp_id &&
3580 param->fp_comp_id != entry->lcme_id)
3583 if (param->fp_check_comp_start) {
3584 match = find_value_cmp(entry->lcme_extent.e_start,
3585 param->fp_comp_start,
3586 param->fp_comp_start_sign,
3588 param->fp_comp_start_units, 0);
3593 if (param->fp_check_comp_end) {
3594 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3600 if (param->fp_check_mirror_index) {
3601 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3603 mirror_id = mirror_id_of(entry->lcme_id);
3606 match = find_value_cmp(mirror_index,
3607 param->fp_mirror_index,
3608 param->fp_mirror_index_sign,
3609 param->fp_exclude_mirror_index,
3613 } else if (param->fp_check_mirror_id) {
3614 if (mirror_id != mirror_id_of(entry->lcme_id))
3615 mirror_id = mirror_id_of(entry->lcme_id);
3617 match = find_value_cmp(mirror_id,
3618 param->fp_mirror_id,
3619 param->fp_mirror_id_sign,
3620 param->fp_exclude_mirror_id,
3626 if (print_last_init_comp(param)) {
3628 * if part of stripe info is needed, we'd print only
3629 * the last instantiated component info.
3631 if (entry->lcme_flags & LCME_FL_INIT)
3634 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3635 if (entry->lcme_flags & LCME_FL_EXTENSION)
3636 /* moved back below */
3644 if (entry->lcme_flags & LCME_FL_INIT) {
3645 if (obdindex != OBD_NOT_FOUND) {
3646 flags |= LDF_SKIP_OBJS;
3647 v1 = lov_comp_entry(comp_v1, i);
3648 objects = lov_v1v3_objects(v1);
3650 for (j = 0; j < v1->lmm_stripe_count; j++) {
3651 if (obdindex == objects[j].l_ost_idx) {
3652 flags &= ~LDF_SKIP_OBJS;
3657 flags &= ~LDF_SKIP_OBJS;
3660 flags |= LDF_SKIP_OBJS;
3663 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3665 lov_dump_comp_v1_entry(param, flags, i);
3667 v1 = lov_comp_entry(comp_v1, i);
3668 objects = lov_v1v3_objects(v1);
3669 lov_v1v3_pool_name(v1, pool_name);
3671 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3672 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3673 param->fp_max_depth, param->fp_verbose,
3676 if (print_last_init_comp(param)) {
3678 * directory layout contains only layout template, print the
3682 i = comp_v1->lcm_entry_count - 1;
3685 flags &= ~LDF_SKIP_OBJS;
3687 lov_dump_comp_v1_entry(param, flags, i);
3689 v1 = lov_comp_entry(comp_v1, i);
3690 objects = lov_v1v3_objects(v1);
3691 lov_v1v3_pool_name(v1, pool_name);
3693 entry = &comp_v1->lcm_entries[i];
3694 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3695 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3696 param->fp_max_depth, param->fp_verbose,
3701 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3702 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3705 static inline bool has_any_comp_options(struct find_param *param)
3707 enum llapi_layout_verbose verbose = param->fp_verbose;
3709 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3710 param->fp_check_comp_start || param->fp_check_comp_end ||
3711 param->fp_check_comp_flags)
3714 /* show full layout information, not component specific */
3715 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3718 return verbose & VERBOSE_COMP_OPTS;
3721 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3724 struct lov_user_md *lum = &orig->lmd_lmm;
3725 struct lov_user_mds_data *new;
3726 struct lov_comp_md_v1 *comp_v1;
3727 struct lov_comp_md_entry_v1 *ent;
3728 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3729 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3732 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3734 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3738 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3739 + sizeof(new->lmd_lmmsize));
3741 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3742 comp_v1->lcm_magic = lum->lmm_magic;
3743 comp_v1->lcm_size = lum_off + lum_size;
3744 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3745 comp_v1->lcm_flags = 0;
3746 comp_v1->lcm_entry_count = 1;
3748 ent = &comp_v1->lcm_entries[0];
3750 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3751 ent->lcme_extent.e_start = 0;
3752 ent->lcme_extent.e_end = LUSTRE_EOF;
3753 ent->lcme_offset = lum_off;
3754 ent->lcme_size = lum_size;
3756 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3761 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3762 enum lov_dump_flags flags)
3764 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3766 if (has_any_comp_options(param)) {
3767 struct lov_user_mds_data *new_lmd, *orig_lmd;
3769 orig_lmd = param->fp_lmd;
3770 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3771 if (new_lmd != NULL) {
3772 param->fp_lmd = new_lmd;
3773 lov_dump_comp_v1(param, path, flags);
3774 param->fp_lmd = orig_lmd;
3780 if (magic == LOV_USER_MAGIC_V1) {
3781 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3782 param->fp_lmd->lmd_lmm.lmm_objects,
3783 path, param->fp_obd_index,
3784 param->fp_max_depth, param->fp_verbose,
3787 char pool_name[LOV_MAXPOOLNAME + 1];
3788 struct lov_user_ost_data_v1 *objects;
3789 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3791 snprintf(pool_name, sizeof(pool_name), "%s",
3792 lmmv3->lmm_pool_name);
3793 objects = lmmv3->lmm_objects;
3794 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3795 objects, path, param->fp_obd_index,
3796 param->fp_max_depth, param->fp_verbose,
3801 static uint32_t check_foreign_type(uint32_t foreign_type)
3805 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3806 if (lu_foreign_types[i].lft_name == NULL)
3808 if (foreign_type == lu_foreign_types[i].lft_type)
3812 return LU_FOREIGN_TYPE_UNKNOWN;
3815 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3816 enum lov_dump_flags flags)
3818 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3819 bool yaml = flags & LDF_YAML;
3821 if (!yaml && param->fp_depth && path)
3822 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3824 if (param->fp_verbose & VERBOSE_DETAIL) {
3825 uint32_t type = check_foreign_type(lfm->lfm_type);
3827 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3829 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3831 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3833 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3834 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3835 lu_foreign_types[type].lft_name);
3837 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3839 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3842 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3843 lfm->lfm_length, lfm->lfm_value);
3844 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3847 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3848 enum lov_dump_flags flags)
3850 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3851 bool yaml = flags & LDF_YAML;
3853 if (!yaml && param->fp_depth && path)
3854 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3856 if (param->fp_verbose & VERBOSE_DETAIL) {
3857 uint32_t type = check_foreign_type(lfm->lfm_type);
3859 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3861 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3863 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3865 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3866 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3867 lu_foreign_types[type].lft_name);
3869 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3871 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3874 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3875 lfm->lfm_length, lfm->lfm_value);
3876 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3879 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3880 enum lov_dump_flags flags)
3884 if (param->fp_get_lmv || param->fp_get_default_lmv)
3885 magic = (__u32)param->fp_lmv_md->lum_magic;
3887 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3890 flags |= LDF_IS_RAW;
3895 case LOV_USER_MAGIC_V1:
3896 case LOV_USER_MAGIC_V3:
3897 case LOV_USER_MAGIC_SPECIFIC:
3898 lov_dump_plain_user_lmm(param, path, flags);
3900 case LOV_USER_MAGIC_FOREIGN:
3901 lov_dump_foreign_lmm(param, path, flags);
3904 case LMV_USER_MAGIC: {
3905 char pool_name[LOV_MAXPOOLNAME + 1];
3906 struct lmv_user_md *lum;
3908 lum = (struct lmv_user_md *)param->fp_lmv_md;
3909 snprintf(pool_name, sizeof(pool_name), "%s",
3910 lum->lum_pool_name);
3911 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3912 param->fp_max_depth, param->fp_verbose,
3916 case LOV_USER_MAGIC_COMP_V1:
3917 lov_dump_comp_v1(param, path, flags);
3919 case LMV_MAGIC_FOREIGN:
3920 lmv_dump_foreign_lmm(param, path, flags);
3923 llapi_printf(LLAPI_MSG_NORMAL,
3924 "unknown lmm_magic: %#x (expecting one of %#x %#x %#x %#x)\n",
3925 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3926 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3927 LMV_USER_MAGIC, LMV_MAGIC_V1);
3932 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3938 fname = strrchr(path, '/');
3940 /* It should be a file (or other non-directory) */
3941 if (fname == NULL) {
3942 dname = (char *)malloc(2);
3946 fname = (char *)path;
3948 dname = (char *)malloc(fname - path + 1);
3951 strncpy(dname, path, fname - path);
3952 dname[fname - path] = '\0';
3956 fd = open(dname, O_RDONLY | O_NONBLOCK);
3962 strcpy((char *)lum, fname);
3963 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3966 if (close(fd) == -1 && rc == 0)
3974 int llapi_file_lookup(int dirfd, const char *name)
3976 struct obd_ioctl_data data = { 0 };
3981 if (dirfd < 0 || name == NULL)
3984 data.ioc_version = OBD_IOCTL_VERSION;
3985 data.ioc_len = sizeof(data);
3986 data.ioc_inlbuf1 = (char *)name;
3987 data.ioc_inllen1 = strlen(name) + 1;
3989 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3991 llapi_error(LLAPI_MSG_ERROR, rc,
3992 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3997 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
4004 * Check if the file time matches all the given criteria (e.g. --atime +/-N).
4005 * Return -1 or 1 if file timestamp does not or does match the given criteria
4006 * correspondingly. Return 0 if the MDS time is being checked and there are
4007 * attributes on OSTs and it is not yet clear if the timespamp matches.
4009 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
4010 * updated timestamps.
4012 static int find_time_check(struct find_param *param, int mds)
4014 struct lov_user_mds_data *lmd = param->fp_lmd;
4018 /* Check if file is accepted. */
4019 if (param->fp_atime) {
4020 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4021 param->fp_atime, param->fp_asign,
4022 param->fp_exclude_atime,
4023 param->fp_time_margin, mds);
4029 if (param->fp_mtime) {
4030 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4031 param->fp_mtime, param->fp_msign,
4032 param->fp_exclude_mtime,
4033 param->fp_time_margin, mds);
4038 * If the previous check matches, but this one is not yet clear,
4039 * we should return 0 to do an RPC on OSTs.
4045 if (param->fp_ctime) {
4046 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4047 param->fp_ctime, param->fp_csign,
4048 param->fp_exclude_ctime,
4049 param->fp_time_margin, mds);
4054 * If the previous check matches, but this one is not yet clear,
4055 * we should return 0 to do an RPC on OSTs.
4064 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4066 struct lov_user_mds_data *lmd = param->fp_lmd;
4071 for (i = 0; i < 2; i++) {
4072 /* Check if file is accepted. */
4073 if (param->fp_newery[NEWERXY_ATIME][i]) {
4074 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4075 param->fp_newery[NEWERXY_ATIME][i],
4082 if (param->fp_newery[NEWERXY_MTIME][i]) {
4083 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4084 param->fp_newery[NEWERXY_MTIME][i],
4090 * If the previous check matches, but this one is not
4091 * yet clear, we should return 0 to do an RPC on OSTs.
4097 if (param->fp_newery[NEWERXY_CTIME][i]) {
4098 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4099 param->fp_newery[NEWERXY_CTIME][i],
4105 * If the previous check matches, but this one is not
4106 * yet clear, we should return 0 to do an RPC on OSTs.
4113 * File birth time (btime) can get from MDT directly.
4114 * if @from_mdt is true, it means the input file attributs are
4115 * obtained directly from MDT.
4116 * Thus, if @from_mdt is false, we should skip the following
4122 if (param->fp_newery[NEWERXY_BTIME][i]) {
4123 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4126 rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4127 param->fp_newery[NEWERXY_BTIME][i],
4138 * Check whether the stripes matches the indexes user provided
4142 static int check_obd_match(struct find_param *param)
4144 struct lov_user_ost_data_v1 *objects;
4145 struct lov_comp_md_v1 *comp_v1 = NULL;
4146 struct lov_user_mds_data *lmd = param->fp_lmd;
4147 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4148 int i, j, k, count = 1;
4150 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4153 if (!S_ISREG(lmd->lmd_stx.stx_mode))
4156 /* exclude foreign */
4157 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4158 return param->fp_exclude_obd;
4161 * Only those files should be accepted, which have a
4162 * stripe on the specified OST.
4164 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4165 comp_v1 = (struct lov_comp_md_v1 *)v1;
4166 count = comp_v1->lcm_entry_count;
4169 for (i = 0; i < count; i++) {
4171 v1 = lov_comp_entry(comp_v1, i);
4173 objects = lov_v1v3_objects(v1);
4175 for (j = 0; j < v1->lmm_stripe_count; j++) {
4176 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4179 for (k = 0; k < param->fp_num_obds; k++) {
4180 if (param->fp_obd_indexes[k] ==
4181 objects[j].l_ost_idx)
4182 return !param->fp_exclude_obd;
4187 return param->fp_exclude_obd;
4190 static int check_mdt_match(struct find_param *param)
4194 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4197 /* FIXME: For striped dir, we should get stripe information and check */
4198 for (i = 0; i < param->fp_num_mdts; i++) {
4199 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4200 return !param->fp_exclude_mdt;
4203 if (param->fp_exclude_mdt)
4210 * Check whether the obd is active or not, if it is
4211 * not active, just print the object affected by this
4214 static int print_failed_tgt(struct find_param *param, char *path, int type)
4216 struct obd_statfs stat_buf;
4217 struct obd_uuid uuid_buf;
4220 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV)
4223 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4224 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4225 ret = llapi_obd_statfs(path, type,
4226 type == LL_STATFS_LOV ? param->fp_obd_index :
4227 param->fp_mdt_index, &stat_buf,
4230 llapi_error(LLAPI_MSG_NORMAL, ret, "obd_uuid: %s failed",
4231 param->fp_obd_uuid->uuid);
4236 static int find_check_stripe_size(struct find_param *param)
4238 struct lov_comp_md_v1 *comp_v1 = NULL;
4239 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4240 __u32 stripe_size = 0;
4241 int ret, i, count = 1;
4243 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4244 return param->fp_exclude_stripe_size ? 1 : -1;
4246 ret = param->fp_exclude_stripe_size ? 1 : -1;
4247 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4248 comp_v1 = (struct lov_comp_md_v1 *)v1;
4249 count = comp_v1->lcm_entry_count;
4252 for (i = 0; i < count; i++) {
4253 struct lov_comp_md_entry_v1 *ent;
4256 v1 = lov_comp_entry(comp_v1, i);
4258 ent = &comp_v1->lcm_entries[i];
4259 if (ent->lcme_flags & LCME_FL_EXTENSION)
4261 if (!(ent->lcme_flags & LCME_FL_INIT))
4264 stripe_size = v1->lmm_stripe_size;
4267 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4268 param->fp_stripe_size_sign,
4269 param->fp_exclude_stripe_size,
4270 param->fp_stripe_size_units, 0);
4275 static int find_check_ext_size(struct find_param *param)
4277 struct lov_comp_md_v1 *comp_v1;
4278 struct lov_user_md_v1 *v1;
4281 ret = param->fp_exclude_ext_size ? 1 : -1;
4282 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4283 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4286 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4287 struct lov_comp_md_entry_v1 *ent;
4289 v1 = lov_comp_entry(comp_v1, i);
4291 ent = &comp_v1->lcm_entries[i];
4292 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4295 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4296 param->fp_ext_size_sign,
4297 param->fp_exclude_ext_size,
4298 param->fp_ext_size_units, 0);
4299 /* If any ext_size matches */
4307 static __u32 find_get_stripe_count(struct find_param *param)
4309 struct lov_comp_md_v1 *comp_v1 = NULL;
4310 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4312 __u32 stripe_count = 0;
4314 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4317 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4318 comp_v1 = (struct lov_comp_md_v1 *)v1;
4319 count = comp_v1->lcm_entry_count;
4322 for (i = 0; i < count; i++) {
4324 struct lov_comp_md_entry_v1 *ent;
4326 v1 = lov_comp_entry(comp_v1, i);
4328 ent = &comp_v1->lcm_entries[i];
4329 if (!(ent->lcme_flags & LCME_FL_INIT))
4332 if (ent->lcme_flags & LCME_FL_EXTENSION)
4335 stripe_count = v1->lmm_stripe_count;
4338 return stripe_count;
4341 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4343 static int find_check_layout(struct find_param *param)
4345 struct lov_comp_md_v1 *comp_v1 = NULL;
4346 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4348 bool found = false, valid = false;
4350 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4351 comp_v1 = (struct lov_comp_md_v1 *)v1;
4352 count = comp_v1->lcm_entry_count;
4355 for (i = 0; i < count; i++) {
4357 v1 = lov_comp_entry(comp_v1, i);
4359 /* foreign file have a special magic but no pattern field */
4360 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4363 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4367 if (v1->lmm_pattern & param->fp_layout) {
4376 if ((found && !param->fp_exclude_layout) ||
4377 (!found && param->fp_exclude_layout))
4384 * if no type specified, check/exclude all foreign
4385 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4387 static int find_check_foreign(struct find_param *param)
4389 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4390 struct lov_foreign_md *lfm;
4392 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4393 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4394 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4395 return param->fp_exclude_foreign ? 1 : -1;
4399 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4400 lfm->lfm_type == param->fp_foreign_type)
4401 return param->fp_exclude_foreign ? -1 : 1;
4402 return param->fp_exclude_foreign ? 1 : -1;
4405 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4406 struct lmv_foreign_md *lfm;
4408 lfm = (void *)param->fp_lmv_md;
4409 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
4410 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4411 return param->fp_exclude_foreign ? 1 : -1;
4415 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4416 lfm->lfm_type == param->fp_foreign_type)
4417 return param->fp_exclude_foreign ? -1 : 1;
4418 return param->fp_exclude_foreign ? 1 : -1;
4423 static int find_check_pool(struct find_param *param)
4425 struct lov_comp_md_v1 *comp_v1 = NULL;
4426 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4427 struct lov_user_md_v3 *v3 = (void *)v1;
4431 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4432 comp_v1 = (struct lov_comp_md_v1 *)v1;
4433 count = comp_v1->lcm_entry_count;
4434 /* empty requested pool is taken as no pool search */
4435 if (count == 0 && param->fp_poolname[0] == '\0')
4439 for (i = 0; i < count; i++) {
4440 if (comp_v1 != NULL)
4441 v1 = lov_comp_entry(comp_v1, i);
4443 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4446 if (((v1->lmm_magic == LOV_USER_MAGIC_V1) &&
4447 (param->fp_poolname[0] == '\0')) ||
4448 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4449 (strncmp(v3->lmm_pool_name,
4450 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4451 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4452 (strcmp(param->fp_poolname, "*") == 0))) {
4458 if ((found && !param->fp_exclude_pool) ||
4459 (!found && param->fp_exclude_pool))
4465 static int find_check_comp_options(struct find_param *param)
4467 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4468 struct lov_user_mds_data *lmd = param->fp_lmd;
4469 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4470 struct lov_comp_md_entry_v1 *entry;
4473 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4476 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4477 comp_v1 = (struct lov_comp_md_v1 *)v1;
4479 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4480 if (forged_v1 == NULL)
4482 comp_v1 = forged_v1;
4483 comp_v1->lcm_entry_count = 1;
4484 entry = &comp_v1->lcm_entries[0];
4485 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4487 entry->lcme_extent.e_start = 0;
4488 entry->lcme_extent.e_end = LUSTRE_EOF;
4491 /* invalid case, don't match for any kind of search. */
4492 if (comp_v1->lcm_entry_count == 0) {
4497 if (param->fp_check_comp_count) {
4498 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4499 param->fp_comp_count,
4500 param->fp_comp_count_sign,
4501 param->fp_exclude_comp_count, 1, 0);
4507 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4508 entry = &comp_v1->lcm_entries[i];
4510 if (param->fp_check_comp_flags) {
4512 if (((param->fp_comp_flags & entry->lcme_flags) !=
4513 param->fp_comp_flags) ||
4514 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4520 if (param->fp_check_comp_start) {
4521 ret = find_value_cmp(entry->lcme_extent.e_start,
4522 param->fp_comp_start,
4523 param->fp_comp_start_sign,
4524 param->fp_exclude_comp_start,
4525 param->fp_comp_start_units, 0);
4530 if (param->fp_check_comp_end) {
4531 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4537 /* the component matches all criteria */
4546 static int find_check_mirror_options(struct find_param *param)
4548 struct lov_comp_md_v1 *comp_v1;
4549 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4552 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4555 comp_v1 = (struct lov_comp_md_v1 *)v1;
4557 if (param->fp_check_mirror_count) {
4558 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4559 param->fp_mirror_count,
4560 param->fp_mirror_count_sign,
4561 param->fp_exclude_mirror_count, 1, 0);
4566 if (param->fp_check_mirror_state) {
4568 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4570 if ((param->fp_mirror_state != 0 &&
4571 file_state != param->fp_mirror_state) ||
4572 file_state == param->fp_mirror_neg_state)
4579 static bool find_check_lmm_info(struct find_param *param)
4581 return param->fp_check_pool || param->fp_check_stripe_count ||
4582 param->fp_check_stripe_size || param->fp_check_layout ||
4583 param->fp_check_comp_count || param->fp_check_comp_end ||
4584 param->fp_check_comp_start || param->fp_check_comp_flags ||
4585 param->fp_check_mirror_count || param->fp_check_foreign ||
4586 param->fp_check_mirror_state || param->fp_check_ext_size ||
4587 param->fp_check_projid;
4591 * Get file/directory project id.
4592 * by the open fd resides on.
4593 * Return 0 and project id on success, or -ve errno.
4595 static int fget_projid(int fd, int *projid)
4600 rc = ioctl(fd, LL_IOC_FSGETXATTR, &fsx);
4604 *projid = fsx.fsx_projid;
4608 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
4609 void *data, struct dirent64 *de)
4611 struct find_param *param = (struct find_param *)data;
4612 struct lov_user_mds_data *lmd = param->fp_lmd;
4613 DIR *dir = dirp == NULL ? NULL : *dirp;
4614 int decision = 1; /* 1 is accepted; -1 is rejected. */
4616 int checked_type = 0;
4618 __u32 stripe_count = 0;
4622 if (parent == NULL && dir == NULL)
4625 /* If a regular expression is presented, make the initial decision */
4626 if (param->fp_pattern != NULL) {
4627 char *fname = strrchr(path, '/');
4629 fname = (fname == NULL ? path : fname + 1);
4630 ret = fnmatch(param->fp_pattern, fname, 0);
4631 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4632 (ret == 0 && param->fp_exclude_pattern))
4636 /* See if we can check the file type from the dirent. */
4637 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
4640 if (DTTOIF(de->d_type) == param->fp_type) {
4641 if (param->fp_exclude_type)
4644 if (!param->fp_exclude_type)
4652 * Request MDS for the stat info if some of these parameters need
4655 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4656 param->fp_check_uid || param->fp_check_gid ||
4657 param->fp_newerxy || param->fp_btime ||
4658 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4659 param->fp_check_size || param->fp_check_blocks ||
4660 find_check_lmm_info(param) ||
4661 param->fp_check_mdt_count || param->fp_check_hash_type)
4664 if (param->fp_type != 0 && checked_type == 0)
4667 if (decision == 0) {
4668 if (dir && (param->fp_check_mdt_count ||
4669 param->fp_check_hash_type || param->fp_check_foreign)) {
4670 param->fp_get_lmv = 1;
4671 ret = cb_get_dirstripe(path, dir, param);
4674 * XXX this works to decide for foreign
4677 if (errno == ENODATA &&
4678 param->fp_check_foreign) {
4679 if (param->fp_exclude_foreign)
4687 param->fp_lmd->lmd_lmm.lmm_magic = 0;
4688 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
4689 param->fp_lum_size, GET_LMD_INFO);
4690 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
4691 find_check_lmm_info(param)) {
4692 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
4695 * We need to "fake" the "use the default" values
4696 * since the lmm struct is zeroed out at this point.
4698 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4699 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
4701 ostid_set_seq(&lmm->lmm_oi,
4702 FID_SEQ_LOV_DEFAULT);
4703 lmm->lmm_stripe_size = 0;
4704 lmm->lmm_stripe_count = 0;
4705 lmm->lmm_stripe_offset = -1;
4707 if (ret == 0 && param->fp_mdt_uuid != NULL) {
4709 ret = llapi_file_fget_mdtidx(dirfd(dir),
4710 ¶m->fp_file_mdt_index);
4711 } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
4713 * FIXME: we could get the MDT index from the
4714 * file's FID in lmd->lmd_lmm.lmm_oi without
4715 * opening the file, once we are sure that
4716 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
4717 * That would still be an ioctl() to map the
4718 * FID to the MDT, but not an open RPC.
4720 fd = open(path, O_RDONLY);
4722 ret = llapi_file_fget_mdtidx(fd,
4723 ¶m->fp_file_mdt_index);
4729 * For a special file, we assume it resides on
4730 * the same MDT as the parent directory.
4732 ret = llapi_file_fget_mdtidx(dirfd(parent),
4733 ¶m->fp_file_mdt_index);
4744 stripe_count = find_get_stripe_count(param);
4748 if (param->fp_type && !checked_type) {
4749 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
4750 if (param->fp_exclude_type)
4753 if (!param->fp_exclude_type)
4759 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
4760 if (lustre_fs && param->fp_got_uuids &&
4761 param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
4762 lmd->lmd_stx.stx_dev_minor)) {
4763 /* A lustre/lustre mount point is crossed. */
4764 param->fp_got_uuids = 0;
4765 param->fp_obds_printed = 0;
4766 param->fp_mdt_index = OBD_NOT_FOUND;
4767 param->fp_obd_index = OBD_NOT_FOUND;
4770 if (lustre_fs && !param->fp_got_uuids) {
4771 ret = setup_target_indexes(dir ? dir : parent, path,
4776 param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
4777 lmd->lmd_stx.stx_dev_minor);
4778 } else if (!lustre_fs && param->fp_got_uuids) {
4779 /* A lustre/non-lustre mount point is crossed. */
4780 param->fp_got_uuids = 0;
4781 param->fp_mdt_index = OBD_NOT_FOUND;
4782 param->fp_obd_index = OBD_NOT_FOUND;
4786 if (param->fp_check_foreign) {
4787 decision = find_check_foreign(param);
4792 if (param->fp_check_stripe_size) {
4793 decision = find_check_stripe_size(param);
4798 if (param->fp_check_ext_size) {
4799 decision = find_check_ext_size(param);
4804 if (param->fp_check_stripe_count) {
4805 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
4806 param->fp_stripe_count_sign,
4807 param->fp_exclude_stripe_count, 1, 0);
4812 if (param->fp_check_mdt_count) {
4813 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4818 decision = find_value_cmp(
4819 param->fp_lmv_md->lum_stripe_count,
4820 param->fp_mdt_count,
4821 param->fp_mdt_count_sign,
4822 param->fp_exclude_mdt_count, 1, 0);
4827 if (param->fp_check_layout) {
4828 decision = find_check_layout(param);
4833 if (param->fp_check_hash_type) {
4836 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4841 found = param->fp_lmv_md->lum_hash_type & param->fp_hash_type;
4842 if ((found && param->fp_exclude_hash_type) ||
4843 (!found && !param->fp_exclude_hash_type)) {
4849 /* If an OBD UUID is specified but none matches, skip this file. */
4850 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
4851 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
4855 * If an OST or MDT UUID is given, and some OST matches,
4858 if (param->fp_obd_index != OBD_NOT_FOUND ||
4859 param->fp_mdt_index != OBD_NOT_FOUND) {
4860 if (param->fp_obd_uuid) {
4861 if (check_obd_match(param)) {
4863 * If no mdtuuid is given, we are done.
4864 * Otherwise, fall through to the mdtuuid
4867 if (!param->fp_mdt_uuid)
4874 if (param->fp_mdt_uuid) {
4875 if (check_mdt_match(param))
4882 if (param->fp_check_uid) {
4883 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
4884 if (param->fp_exclude_uid)
4887 if (!param->fp_exclude_uid)
4892 if (param->fp_check_gid) {
4893 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
4894 if (param->fp_exclude_gid)
4897 if (!param->fp_exclude_gid)
4902 if (param->fp_check_projid) {
4906 fd = open(path, O_RDONLY);
4909 ret = fget_projid(fd, &projid);
4914 if (projid == param->fp_projid) {
4915 if (param->fp_exclude_projid)
4918 if (!param->fp_exclude_projid)
4923 if (param->fp_check_pool) {
4924 decision = find_check_pool(param);
4929 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
4930 param->fp_check_comp_start || param->fp_check_comp_end) {
4931 decision = find_check_comp_options(param);
4936 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
4937 decision = find_check_mirror_options(param);
4942 /* Check the time on mds. */
4944 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
4947 for_mds = lustre_fs ?
4948 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
4949 decision = find_time_check(param, for_mds);
4954 if (param->fp_btime) {
4955 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
4960 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4961 param->fp_btime, param->fp_bsign,
4962 param->fp_exclude_btime,
4963 param->fp_time_margin, 0);
4968 if (param->fp_newerxy) {
4971 for_mds = lustre_fs ?
4972 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
4973 decision = find_newerxy_check(param, for_mds, true);
4982 flags = param->fp_lmd->lmd_flags;
4983 if (param->fp_check_size &&
4984 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
4985 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
4986 !(flags & OBD_MD_FLSIZE ||
4987 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
4990 if (param->fp_check_blocks &&
4991 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
4992 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
4993 !(flags & OBD_MD_FLBLOCKS ||
4994 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
4998 * If file still fits the request, ask ost for updated info.
4999 * The regular stat is almost of the same speed as some new
5000 * 'glimpse-size-ioctl'.
5006 * For regular files with the stripe the decision may have not
5007 * been taken yet if *time or size is to be checked.
5009 if (param->fp_obd_index != OBD_NOT_FOUND)
5010 print_failed_tgt(param, path, LL_STATFS_LOV);
5012 if (param->fp_mdt_index != OBD_NOT_FOUND)
5013 print_failed_tgt(param, path, LL_STATFS_LMV);
5016 ret = fstat_f(dirfd(dir), &st);
5017 else if (de != NULL)
5018 ret = fstatat_f(dirfd(parent), de->d_name, &st,
5019 AT_SYMLINK_NOFOLLOW);
5021 ret = lstat_f(path, &st);
5024 if (errno == ENOENT) {
5025 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
5026 "warning: %s: %s does not exist",
5031 llapi_error(LLAPI_MSG_ERROR, ret,
5032 "%s: stat on %s failed",
5038 convert_lmd_statx(param->fp_lmd, &st, true);
5039 /* Check the time on osc. */
5040 decision = find_time_check(param, 0);
5044 if (param->fp_newerxy) {
5045 decision = find_newerxy_check(param, 0, false);
5055 if (param->fp_check_size) {
5056 decision = find_value_cmp(lmd->lmd_stx.stx_size,
5058 param->fp_size_sign,
5059 param->fp_exclude_size,
5060 param->fp_size_units, 0);
5065 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
5066 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
5068 param->fp_blocks_sign,
5069 param->fp_exclude_blocks,
5070 param->fp_blocks_units, 0);
5076 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
5077 if (param->fp_zero_end)
5078 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
5080 llapi_printf(LLAPI_MSG_NORMAL, "\n");
5084 /* Do not get down anymore? */
5085 if (param->fp_depth == param->fp_max_depth) {
5096 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
5097 void *param_data, struct dirent64 *de)
5099 struct find_param *param = (struct find_param *)param_data;
5100 struct lmv_user_md *lmu = param->fp_lmv_md;
5101 DIR *tmp_parent = parent;
5102 char raw[MAX_IOC_BUFLEN] = {'\0'};
5104 struct obd_ioctl_data data = { 0 };
5111 if (parent == NULL && dirp == NULL)
5120 if (parent == NULL) {
5121 tmp_parent = opendir_parent(path);
5122 if (tmp_parent == NULL) {
5125 llapi_error(LLAPI_MSG_ERROR, ret,
5126 "can not open %s", path);
5131 fd = dirfd(tmp_parent);
5133 path_copy = strdup(path);
5134 filename = basename(path_copy);
5136 data.ioc_inlbuf1 = (char *)filename;
5137 data.ioc_inllen1 = strlen(filename) + 1;
5138 data.ioc_inlbuf2 = (char *)lmu;
5139 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
5141 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5143 llapi_error(LLAPI_MSG_ERROR, ret,
5144 "llapi_obd_statfs: error packing ioctl data");
5149 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
5151 if (errno == EBUSY && !retry) {
5153 * because migrate may not be able to lock all involved
5154 * objects in order, for some of them it try lock, while
5155 * there may be conflicting COS locks and cause migrate
5156 * fail with EBUSY, hope a sync() could cause
5157 * transaction commit and release these COS locks.
5162 } else if (errno == EALREADY) {
5163 if (param->fp_verbose & VERBOSE_DETAIL)
5164 llapi_printf(LLAPI_MSG_NORMAL,
5165 "%s migrated to MDT%d already\n",
5166 path, lmu->lum_stripe_offset);
5170 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
5174 } else if (param->fp_verbose & VERBOSE_DETAIL) {
5175 llapi_printf(LLAPI_MSG_NORMAL,
5176 "migrate %s to MDT%d stripe count %d\n",
5177 path, lmu->lum_stripe_offset,
5178 lmu->lum_stripe_count);
5184 * If the directory is being migration, we need
5185 * close the directory after migration,
5186 * so the old directory cache will be cleanup
5187 * on the client side, and re-open to get the
5188 * new directory handle
5190 *dirp = opendir(path);
5191 if (*dirp == NULL) {
5193 llapi_error(LLAPI_MSG_ERROR, ret,
5194 "%s: Failed to open '%s'", __func__, path);
5199 closedir(tmp_parent);
5206 /* dir migration finished, shrink its stripes */
5207 static int cb_migrate_mdt_fini(char *path, DIR *parent, DIR **dirp, void *data,
5208 struct dirent64 *de)
5210 struct find_param *param = data;
5211 struct lmv_user_md *lmu = param->fp_lmv_md;
5212 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
5215 if (de && de->d_type != DT_DIR)
5220 * close it before setxattr because the latter may destroy the
5221 * original object, and cause close fail.
5223 ret = closedir(*dirp);
5229 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
5230 if (ret == -EALREADY)
5233 cb_common_fini(path, parent, dirp, data, de);
5237 int llapi_migrate_mdt(char *path, struct find_param *param)
5239 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
5243 int llapi_mv(char *path, struct find_param *param)
5245 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
5246 static bool printed;
5249 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
5250 "%s() is deprecated, use llapi_migrate_mdt() instead",
5255 return llapi_migrate_mdt(path, param);
5258 int llapi_find(char *path, struct find_param *param)
5260 return param_callback(path, cb_find_init, cb_common_fini, param);
5264 * Get MDT number that the file/directory inode referenced
5265 * by the open fd resides on.
5266 * Return 0 and mdtidx on success, or -ve errno.
5268 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
5270 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
5275 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
5276 struct dirent64 *de)
5278 struct find_param *param = (struct find_param *)data;
5279 DIR *d = dirp == NULL ? NULL : *dirp;
5283 if (parent == NULL && d == NULL)
5287 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5288 } else /* if (parent) */ {
5291 fd = open(path, O_RDONLY | O_NOCTTY);
5293 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5301 if (ret == -ENODATA) {
5302 if (!param->fp_obd_uuid)
5303 llapi_printf(LLAPI_MSG_NORMAL,
5304 "'%s' has no stripe info\n", path);
5306 } else if (ret == -ENOENT) {
5307 llapi_error(LLAPI_MSG_WARN, ret,
5308 "warning: %s: '%s' does not exist",
5311 } else if (ret == -ENOTTY) {
5312 llapi_error(LLAPI_MSG_ERROR, ret,
5313 "%s: '%s' not on a Lustre fs",
5316 llapi_error(LLAPI_MSG_ERROR, ret,
5317 "error: %s: '%s' failed get_mdtidx",
5323 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5324 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5326 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5330 /* Do not go down anymore? */
5331 if (param->fp_depth == param->fp_max_depth)
5339 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
5340 struct dirent64 *de)
5342 struct find_param *param = (struct find_param *)data;
5343 DIR *d = dirp == NULL ? NULL : *dirp;
5346 if (parent == NULL && d == NULL)
5349 if (param->fp_obd_uuid) {
5350 param->fp_quiet = 1;
5351 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
5356 if (d && (param->fp_get_lmv || param->fp_get_default_lmv))
5357 ret = cb_get_dirstripe(path, d, param);
5359 (parent && !param->fp_get_lmv && !param->fp_get_default_lmv))
5360 ret = get_lmd_info(path, parent, d, ¶m->fp_lmd->lmd_lmm,
5361 param->fp_lum_size, GET_LMD_STRIPE);
5366 if (errno == ENODATA && d != NULL) {
5368 * We need to "fake" the "use the default" values
5369 * since the lmm struct is zeroed out at this point.
5370 * The magic needs to be set in order to satisfy
5371 * a check later on in the code path.
5372 * The object_seq needs to be set for the "(Default)"
5373 * prefix to be displayed.
5375 if (param->fp_get_default_lmv) {
5376 struct lmv_user_md *lum = param->fp_lmv_md;
5378 lum->lum_magic = LMV_USER_MAGIC;
5379 lum->lum_stripe_count = 0;
5380 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
5382 } else if (param->fp_get_lmv) {
5383 struct lmv_user_md *lum = param->fp_lmv_md;
5386 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5389 lum->lum_magic = LMV_MAGIC_V1;
5390 lum->lum_stripe_count = 0;
5391 lum->lum_stripe_offset = mdtidx;
5394 struct lov_user_md *lmm =
5395 ¶m->fp_lmd->lmd_lmm;
5397 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5399 ostid_set_seq(&lmm->lmm_oi,
5400 FID_SEQ_LOV_DEFAULT);
5401 lmm->lmm_stripe_count = 0;
5402 lmm->lmm_stripe_size = 0;
5403 lmm->lmm_stripe_offset = -1;
5406 } else if (errno == ENODATA && parent != NULL) {
5407 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5408 llapi_printf(LLAPI_MSG_NORMAL,
5409 "%s has no stripe info\n", path);
5411 } else if (errno == ENOENT) {
5412 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5413 "warning: %s: %s does not exist",
5416 } else if (errno == ENOTTY) {
5418 llapi_error(LLAPI_MSG_ERROR, ret,
5419 "%s: '%s' not on a Lustre fs?",
5424 llapi_error(LLAPI_MSG_ERROR, ret,
5425 "error: %s: %s failed for %s",
5426 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
5427 "IOC_MDC_GETFILESTRIPE", path);
5434 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5435 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
5438 /* Do not get down anymore? */
5439 if (param->fp_depth == param->fp_max_depth)
5447 int llapi_getstripe(char *path, struct find_param *param)
5449 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5450 cb_get_mdt_index : cb_getstripe,
5451 cb_common_fini, param);
5454 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5455 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5457 char raw[MAX_IOC_BUFLEN] = {'\0'};
5459 struct obd_ioctl_data data = { 0 };
5462 data.ioc_inlbuf1 = (char *)&type;
5463 data.ioc_inllen1 = sizeof(__u32);
5464 data.ioc_inlbuf2 = (char *)&index;
5465 data.ioc_inllen2 = sizeof(__u32);
5466 data.ioc_pbuf1 = (char *)stat_buf;
5467 data.ioc_plen1 = sizeof(struct obd_statfs);
5468 data.ioc_pbuf2 = (char *)uuid_buf;
5469 data.ioc_plen2 = sizeof(struct obd_uuid);
5471 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5473 llapi_error(LLAPI_MSG_ERROR, rc,
5474 "llapi_obd_statfs: error packing ioctl data");
5478 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5480 return rc < 0 ? -errno : 0;
5483 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5484 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5489 fd = open(path, O_RDONLY);
5492 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5495 * If we can't even open a file on the filesystem (e.g. with
5496 * -ESHUTDOWN), force caller to exit or it will loop forever.
5501 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5508 #define MAX_STRING_SIZE 128
5510 int llapi_ping(char *obd_type, char *obd_name)
5512 int flags = O_RDONLY;
5513 char buf[1] = { 0 };
5517 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5518 obd_type, obd_name);
5522 fd = open(path.gl_pathv[0], flags);
5524 if (errno == EACCES && flags == O_RDONLY) {
5529 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5534 if (flags == O_RDONLY)
5535 rc = read(fd, buf, sizeof(buf));
5537 rc = write(fd, buf, sizeof(buf));
5545 cfs_free_param_data(&path);
5549 int llapi_target_iterate(int type_num, char **obd_type,
5550 void *args, llapi_cb_t cb)
5556 for (i = 0; i < type_num; i++) {
5559 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5563 for (j = 0; j < param.gl_pathc; j++) {
5564 char obd_uuid[UUID_MAX + 1];
5568 fp = fopen(param.gl_pathv[j], "r");
5571 llapi_error(LLAPI_MSG_ERROR, rc,
5572 "error: opening '%s'",
5577 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5579 llapi_error(LLAPI_MSG_ERROR, rc,
5580 "error: reading '%s'",
5585 /* Extract the obd_name from the sysfs path.
5586 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5588 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5594 /* skip /fs/lustre/'obd_type'/ */
5595 obd_name += strlen(obd_type[i]) + 12;
5596 /* chop off after obd_name */
5597 ptr = strrchr(obd_name, '/');
5601 cb(obd_type[i], obd_name, obd_uuid, args);
5610 cfs_free_param_data(¶m);
5614 static void do_target_check(char *obd_type_name, char *obd_name,
5615 char *obd_uuid, void *args)
5619 rc = llapi_ping(obd_type_name, obd_name);
5621 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5623 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5625 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5628 int llapi_target_check(int type_num, char **obd_type, char *dir)
5630 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5633 #undef MAX_STRING_SIZE
5635 /* Is this a lustre fs? */
5636 int llapi_is_lustre_mnttype(const char *type)
5638 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
5641 /* Is this a lustre client fs? */
5642 int llapi_is_lustre_mnt(struct mntent *mnt)
5644 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5645 strstr(mnt->mnt_fsname, ":/") != NULL);
5648 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5650 char fsname[PATH_MAX + 1];
5654 rc = llapi_search_fsname(mnt, fsname);
5658 root = open(mnt, O_RDONLY | O_DIRECTORY);
5661 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
5665 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
5673 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
5678 root = open(mnt, O_RDONLY | O_DIRECTORY);
5681 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
5685 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
5688 llapi_error(LLAPI_MSG_ERROR, rc,
5689 "ioctl on %s for getting connect flags failed", mnt);
5696 * Get a 64-bit value representing the version of file data pointed by fd.
5698 * Each write or truncate, flushed on OST, will change this value. You can use
5699 * this value to verify if file data was modified. This only checks the file
5700 * data, not metadata.
5702 * \param flags 0: no flush pages, usually used it the process has already
5704 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
5706 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
5707 * pages from clients.
5709 * \retval 0 on success.
5710 * \retval -errno on error.
5712 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
5715 struct ioc_data_version idv;
5717 idv.idv_flags = (__u32)flags;
5719 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5723 *data_version = idv.idv_version;
5729 * Flush cached pages from all clients.
5731 * \param fd File descriptor
5735 int llapi_file_flush(int fd)
5739 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);
5743 * Fetch layout version from OST objects. Layout version on OST objects are
5744 * only set when the file is a mirrored file AND after the file has been
5745 * written at least once.
5747 * It actually fetches the least layout version from the objects.
5749 int llapi_get_ost_layout_version(int fd, __u32 *layout_version)
5752 struct ioc_data_version idv = { 0 };
5754 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5758 *layout_version = idv.idv_layout_version;
5764 * Create a file without any name and open it for read/write
5766 * - file is created as if it were a standard file in the given \a directory
5767 * - file does not appear in \a directory and mtime does not change because
5768 * the filename is handled specially by the Lustre MDS.
5769 * - file is destroyed at final close
5771 * \param[in] directory directory from which to inherit layout/MDT idx
5772 * \param[in] mdt_idx MDT index on which the file is created,
5773 * \a idx == -1 means no specific MDT is requested
5774 * \param[in] mode standard open(2) mode
5775 * \param[in] stripe_param stripe parameters. May be NULL.
5777 * \retval a file descriptor on success.
5778 * \retval -errno on error.
5780 int llapi_create_volatile_param(const char *directory, int mdt_idx,
5781 int open_flags, mode_t mode,
5782 const struct llapi_stripe_param *stripe_param)
5784 char file_path[PATH_MAX];
5785 int saved_errno = errno;
5787 unsigned int rnumber;
5793 rc = snprintf(file_path, sizeof(file_path),
5794 "%s/" LUSTRE_VOLATILE_HDR "::%.4X",
5795 directory, rnumber);
5797 rc = snprintf(file_path, sizeof(file_path),
5798 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X",
5799 directory, mdt_idx, rnumber);
5801 if (rc < 0 || rc >= sizeof(file_path))
5802 return -ENAMETOOLONG;
5805 * Either open O_WRONLY or O_RDWR, creating RDONLY
5806 * is non-sensical here
5808 if ((open_flags & O_ACCMODE) == O_RDONLY)
5809 open_flags = O_RDWR | (open_flags & ~O_ACCMODE);
5811 open_flags |= O_CREAT | O_EXCL | O_NOFOLLOW;
5813 if (stripe_param != NULL) {
5814 fd = llapi_file_open_param(file_path, open_flags,
5815 mode, stripe_param);
5819 fd = open(file_path, open_flags, mode);
5823 } while (fd < 0 && rc == -EEXIST);
5826 llapi_error(LLAPI_MSG_ERROR, rc,
5827 "Cannot create volatile file '%s' in '%s'",
5828 file_path + strlen(directory) + 1 +
5829 LUSTRE_VOLATILE_HDR_LEN,
5835 * Unlink file in case this wasn't a Lustre filesystem and the magic
5836 * volatile filename wasn't handled as intended. The effect is the
5837 * same. If volatile open was supported then we expect unlink() to
5840 (void)unlink(file_path);
5843 * Since we are returning successfully we restore errno (and
5844 * mask out possible EEXIST from open() and ENOENT from unlink().
5846 errno = saved_errno;
5852 * Create a file without any name open it for read/write
5854 * - file is created as if it were a standard file in the given \a directory
5855 * - file does not appear in \a directory and mtime does not change because
5856 * the filename is handled specially by the Lustre MDS.
5857 * - file is removed at final close
5858 * - file modes are rw------- since it doesn't make sense to have a read-only
5859 * or write-only file that cannot be opened again.
5860 * - if user wants another mode it must use fchmod() on the open file, no
5861 * security problems arise because it cannot be opened by another process.
5863 * \param[in] directory directory from which to inherit layout/MDT idx
5864 * \param[in] idx MDT index on which the file is created,
5865 * \a idx == -1 means no specific MDT is requested
5866 * \param[in] open_flags standard open(2) flags
5868 * \retval a file descriptor on success.
5869 * \retval -errno on error.
5871 int llapi_create_volatile_idx(const char *directory, int mdt_idx,
5874 return llapi_create_volatile_param(directory, mdt_idx, open_flags,
5875 S_IRUSR | S_IWUSR, NULL);
5879 * Swap the layouts between 2 file descriptors
5880 * the 2 files must be open for writing
5881 * first fd received the ioctl, second fd is passed as arg
5882 * this is assymetric but avoid use of root path for ioctl
5884 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
5885 int gid, __u64 flags)
5887 struct lustre_swap_layouts lsl;
5892 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5893 rc = fstat(fd1, &st1);
5897 rc = fstat(fd2, &st2);
5902 lsl.sl_flags = flags;
5906 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
5910 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5911 struct timeval tv1[2];
5912 struct timeval tv2[2];
5914 memset(tv1, 0, sizeof(tv1));
5915 memset(tv2, 0, sizeof(tv2));
5917 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
5918 tv1[0].tv_sec = st1.st_atime;
5919 tv2[0].tv_sec = st2.st_atime;
5921 tv1[0].tv_sec = st2.st_atime;
5922 tv2[0].tv_sec = st1.st_atime;
5925 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
5926 tv1[1].tv_sec = st1.st_mtime;
5927 tv2[1].tv_sec = st2.st_mtime;
5929 tv1[1].tv_sec = st2.st_mtime;
5930 tv2[1].tv_sec = st1.st_mtime;
5933 rc = futimes(fd1, tv1);
5937 rc = futimes(fd2, tv2);
5945 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
5952 while (grp_id == 0);
5954 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
5962 * Swap the layouts between 2 files
5963 * the 2 files are open in write
5965 int llapi_swap_layouts(const char *path1, const char *path2,
5966 __u64 dv1, __u64 dv2, __u64 flags)
5970 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
5973 llapi_error(LLAPI_MSG_ERROR, rc,
5974 "error: cannot open '%s' for write", path1);
5978 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
5981 llapi_error(LLAPI_MSG_ERROR, rc,
5982 "error: cannot open '%s' for write", path2);
5986 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
5988 llapi_error(LLAPI_MSG_ERROR, rc,
5989 "error: cannot swap layout between '%s' and '%s'",
6002 * \param fd File to lock.
6003 * \param gid Group Identifier.
6005 * \retval 0 on success.
6006 * \retval -errno on failure.
6008 int llapi_group_lock(int fd, int gid)
6012 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
6015 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
6023 * \param fd File to unlock.
6024 * \param gid Group Identifier.
6026 * \retval 0 on success.
6027 * \retval -errno on failure.
6029 int llapi_group_unlock(int fd, int gid)
6033 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
6036 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");