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 #include <linux/limits.h>
67 #ifdef HAVE_LINUX_UNISTD_H
68 #include <linux/unistd.h>
76 #include <libcfs/util/ioctl.h>
77 #include <libcfs/util/param.h>
78 #include <libcfs/util/string.h>
79 #include <linux/lnet/lnetctl.h>
80 #include <lustre/lustreapi.h>
81 #include <linux/lustre/lustre_ostid.h>
82 #include <linux/lustre/lustre_ioctl.h>
83 #include "lustreapi_internal.h"
86 static int llapi_msg_level = LLAPI_MSG_MAX;
87 const char *liblustreapi_cmd;
89 char *mdt_hash_name[] = { "none",
90 LMV_HASH_NAME_ALL_CHARS,
91 LMV_HASH_NAME_FNV_1A_64,
95 struct lustre_foreign_type lu_foreign_types[] = {
96 {.lft_type = LU_FOREIGN_TYPE_NONE, .lft_name = "none"},
97 {.lft_type = LU_FOREIGN_TYPE_DAOS, .lft_name = "daos"},
98 /* must be the last element */
99 {.lft_type = LU_FOREIGN_TYPE_UNKNOWN, .lft_name = NULL}
100 /* array max dimension must be <= UINT32_MAX */
103 void llapi_msg_set_level(int level)
105 /* ensure level is in the good range */
106 if (level < LLAPI_MSG_OFF)
107 llapi_msg_level = LLAPI_MSG_OFF;
108 else if (level > LLAPI_MSG_MAX)
109 llapi_msg_level = LLAPI_MSG_MAX;
111 llapi_msg_level = level;
114 int llapi_msg_get_level(void)
116 return llapi_msg_level;
119 void llapi_set_command_name(const char *cmd)
121 liblustreapi_cmd = cmd;
124 void llapi_clear_command_name(void)
126 liblustreapi_cmd = NULL;
129 static void error_callback_default(enum llapi_message_level level, int err,
130 const char *fmt, va_list ap)
132 bool has_nl = strchr(fmt, '\n') != NULL;
134 if (liblustreapi_cmd != NULL)
135 fprintf(stderr, "%s %s: ", program_invocation_short_name,
138 fprintf(stderr, "%s: ", program_invocation_short_name);
141 if (level & LLAPI_MSG_NO_ERRNO) {
142 vfprintf(stderr, fmt, ap);
144 fprintf(stderr, "\n");
149 * Remove trailing linefeed so error string can be appended.
150 * @fmt is a const string, so we can't modify it directly.
152 if (has_nl && (newfmt = strdup(fmt)))
153 *strrchr(newfmt, '\n') = '\0';
155 newfmt = (char *)fmt;
157 vfprintf(stderr, newfmt, ap);
160 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
164 static void info_callback_default(enum llapi_message_level level, int err,
165 const char *fmt, va_list ap)
168 if (liblustreapi_cmd != NULL) {
169 fprintf(stdout, "%s %s: ",
170 program_invocation_short_name,
173 fprintf(stdout, "%s: ", program_invocation_short_name);
176 vfprintf(stdout, fmt, ap);
179 static llapi_log_callback_t llapi_error_callback = error_callback_default;
180 static llapi_log_callback_t llapi_info_callback = info_callback_default;
183 /* llapi_error will preserve errno */
184 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
187 int tmp_errno = errno;
189 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
193 llapi_error_callback(level, abs(err), fmt, args);
198 /* llapi_printf will preserve errno */
199 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
202 int tmp_errno = errno;
204 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
208 llapi_info_callback(level, 0, fmt, args);
214 * Set a custom error logging function. Passing in NULL will reset the logging
215 * callback to its default value.
217 * This function returns the value of the old callback.
219 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
221 llapi_log_callback_t old = llapi_error_callback;
224 llapi_error_callback = cb;
226 llapi_error_callback = error_callback_default;
232 * Set a custom info logging function. Passing in NULL will reset the logging
233 * callback to its default value.
235 * This function returns the value of the old callback.
237 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
239 llapi_log_callback_t old = llapi_info_callback;
242 llapi_info_callback = cb;
244 llapi_info_callback = info_callback_default;
250 * size_units is to be initialized (or zeroed) by caller.
252 int llapi_parse_size(const char *optarg, unsigned long long *size,
253 unsigned long long *size_units, int bytes_spec)
256 char *argbuf = (char *)optarg;
257 unsigned long long frac = 0, frac_d = 1;
259 if (strncmp(optarg, "-", 1) == 0)
262 if (*size_units == 0)
265 *size = strtoull(argbuf, &end, 0);
266 if (end != NULL && *end == '.') {
270 frac = strtoull(argbuf, &end, 10);
271 /* count decimal places */
272 for (i = 0; i < (end - argbuf); i++)
277 if ((*end == 'b') && *(end + 1) == '\0' &&
278 (*size & (~0ULL << (64 - 9))) == 0 &&
280 *size_units = 1 << 9;
281 } else if ((*end == 'b') &&
282 *(end + 1) == '\0' &&
285 } else if ((*end == 'k' || *end == 'K') &&
286 *(end + 1) == '\0' &&
287 (*size & (~0ULL << (64 - 10))) == 0) {
288 *size_units = 1 << 10;
289 } else if ((*end == 'm' || *end == 'M') &&
290 *(end + 1) == '\0' &&
291 (*size & (~0ULL << (64 - 20))) == 0) {
292 *size_units = 1 << 20;
293 } else if ((*end == 'g' || *end == 'G') &&
294 *(end + 1) == '\0' &&
295 (*size & (~0ULL << (64 - 30))) == 0) {
296 *size_units = 1 << 30;
297 } else if ((*end == 't' || *end == 'T') &&
298 *(end + 1) == '\0' &&
299 (*size & (~0ULL << (64 - 40))) == 0) {
300 *size_units = 1ULL << 40;
301 } else if ((*end == 'p' || *end == 'P') &&
302 *(end + 1) == '\0' &&
303 (*size & (~0ULL << (64 - 50))) == 0) {
304 *size_units = 1ULL << 50;
305 } else if ((*end == 'e' || *end == 'E') &&
306 *(end + 1) == '\0' &&
307 (*size & (~0ULL << (64 - 60))) == 0) {
308 *size_units = 1ULL << 60;
313 *size = *size * *size_units + frac * *size_units / frac_d;
318 int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
320 struct obd_ioctl_data *overlay;
323 data->ioc_len = obd_ioctl_packlen(data);
324 data->ioc_version = OBD_IOCTL_VERSION;
326 if (*pbuf != NULL && data->ioc_len > max_len) {
327 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
328 "pbuf = %p, ioc_len = %u, max_len = %d",
329 *pbuf, data->ioc_len, max_len);
334 *pbuf = malloc(data->ioc_len);
339 overlay = (struct obd_ioctl_data *)*pbuf;
340 memcpy(*pbuf, data, sizeof(*data));
342 ptr = overlay->ioc_bulk;
343 if (data->ioc_inlbuf1) {
344 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
345 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
348 if (data->ioc_inlbuf2) {
349 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
350 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
353 if (data->ioc_inlbuf3) {
354 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
355 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
358 if (data->ioc_inlbuf4) {
359 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
360 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
366 int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
368 struct obd_ioctl_data *overlay;
374 overlay = (struct obd_ioctl_data *)pbuf;
376 /* Preserve the caller's buffer pointers */
377 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
378 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
379 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
380 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
382 memcpy(data, pbuf, sizeof(*data));
384 ptr = overlay->ioc_bulk;
385 if (data->ioc_inlbuf1) {
386 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
387 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
390 if (data->ioc_inlbuf2) {
391 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
392 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
395 if (data->ioc_inlbuf3) {
396 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
397 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
400 if (data->ioc_inlbuf4) {
401 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
402 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
408 /* XXX: llapi_xxx() functions return negative values upon failure */
410 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
411 int stripe_count, int stripe_pattern)
413 static int page_size;
416 if (page_size == 0) {
418 * 64 KB is the largest common page size (on ia64/PPC/ARM),
419 * but check the local page size just in case. The page_size
420 * will not change for the lifetime of this process at least.
422 page_size = LOV_MIN_STRIPE_SIZE;
423 if (getpagesize() > page_size) {
424 page_size = getpagesize();
425 llapi_err_noerrno(LLAPI_MSG_WARN,
426 "warning: page size (%u) larger than expected (%u)",
427 page_size, LOV_MIN_STRIPE_SIZE);
430 if (!llapi_stripe_size_is_aligned(stripe_size)) {
432 llapi_error(LLAPI_MSG_ERROR, rc,
433 "error: bad stripe_size %llu, must be an even multiple of %d bytes",
434 (unsigned long long)stripe_size, page_size);
437 if (!llapi_stripe_index_is_valid(stripe_offset)) {
439 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
443 if (!llapi_stripe_count_is_valid(stripe_count)) {
445 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
449 if (llapi_stripe_size_is_too_big(stripe_size)) {
451 llapi_error(LLAPI_MSG_ERROR, rc,
452 "error: stripe size '%llu' over 4GB limit",
453 (unsigned long long)stripe_size);
462 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
467 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
469 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
473 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
475 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
480 if (!llapi_dir_hash_type_is_valid(hash_type)) {
482 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
490 * Trim a trailing newline from a string, if it exists.
492 int llapi_chomp_string(char *buf)
508 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
510 static int get_param_lov(const char *path, const char *param,
511 char *buf, size_t buf_size)
513 struct obd_uuid uuid;
516 rc = llapi_file_get_lov_uuid(path, &uuid);
520 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
525 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
527 static int get_param_lmv(const char *path, const char *param,
528 char *buf, size_t buf_size)
530 struct obd_uuid uuid;
533 rc = llapi_file_get_lmv_uuid(path, &uuid);
537 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
541 static int get_mds_md_size(const char *path)
543 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
546 * Rather than open the file and do the ioctl to get the
547 * instance name and close the file and search for the param
548 * file and open the param file and read the param file and
549 * parse the value and close the param file, let's just return
550 * a large enough value. It's 2020, RAM is cheap and this is
554 if (md_size < XATTR_SIZE_MAX)
555 md_size = XATTR_SIZE_MAX;
560 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
562 return get_param_lmv(path, "uuid", buf, bufsize);
566 * Open a Lustre file.
568 * \param name the name of the file to be opened
569 * \param flags access mode, see flags in open(2)
570 * \param mode permission of the file if it is created, see mode in open(2)
571 * \param param stripe pattern of the newly created file
573 * \retval file descriptor of opened file
574 * \retval negative errno on failure
576 int llapi_file_open_param(const char *name, int flags, mode_t mode,
577 const struct llapi_stripe_param *param)
579 char fsname[MAX_OBD_NAME + 1] = { 0 };
580 char *pool_name = param->lsp_pool;
581 struct lov_user_md *lum = NULL;
582 size_t lum_size = sizeof(*lum);
585 /* Make sure we are on a Lustre file system */
586 rc = llapi_search_fsname(name, fsname);
588 llapi_error(LLAPI_MSG_ERROR, rc,
589 "'%s' is not on a Lustre filesystem",
594 /* Check if the stripe pattern is sane. */
595 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
596 param->lsp_stripe_offset,
597 param->lsp_stripe_count,
598 param->lsp_stripe_pattern);
602 /* Make sure we have a good pool */
603 if (pool_name != NULL) {
605 * in case user gives the full pool name <fsname>.<poolname>,
608 char *ptr = strchr(pool_name, '.');
612 if (strcmp(pool_name, fsname) != 0) {
614 llapi_err_noerrno(LLAPI_MSG_ERROR,
615 "Pool '%s' is not on filesystem '%s'",
622 /* Make sure the pool exists and is non-empty */
623 rc = llapi_search_ost(fsname, pool_name, NULL);
625 char *err = rc == 0 ? "has no OSTs" : "does not exist";
627 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
628 fsname, pool_name, err);
632 lum_size = sizeof(struct lov_user_md_v3);
635 /* sanity check of target list */
636 if (param->lsp_is_specific) {
637 char ostname[MAX_OBD_NAME + 64];
641 for (i = 0; i < param->lsp_stripe_count; i++) {
642 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
643 fsname, param->lsp_osts[i]);
644 rc = llapi_search_ost(fsname, pool_name, ostname);
649 llapi_error(LLAPI_MSG_ERROR, rc,
650 "%s: cannot find OST %s in %s",
657 /* Make sure stripe offset is in OST list. */
658 if (param->lsp_osts[i] == param->lsp_stripe_offset)
662 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
663 "%s: stripe offset '%d' is not in the target list",
664 __func__, param->lsp_stripe_offset);
668 lum_size = lov_user_md_size(param->lsp_stripe_count,
669 LOV_USER_MAGIC_SPECIFIC);
672 lum = calloc(1, lum_size);
677 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
679 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
680 flags = O_DIRECTORY | O_RDONLY;
687 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
692 /* Initialize IOCTL striping pattern structure */
693 lum->lmm_magic = LOV_USER_MAGIC_V1;
694 lum->lmm_pattern = param->lsp_stripe_pattern;
695 lum->lmm_stripe_size = param->lsp_stripe_size;
696 lum->lmm_stripe_count = param->lsp_stripe_count;
697 lum->lmm_stripe_offset = param->lsp_stripe_offset;
698 if (pool_name != NULL) {
699 struct lov_user_md_v3 *lumv3 = (void *)lum;
701 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
702 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
704 if (param->lsp_is_specific) {
705 struct lov_user_md_v3 *lumv3 = (void *)lum;
708 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
709 if (pool_name == NULL) {
711 * LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
712 * OST list, therefore if pool is not specified we have
713 * to pack a null pool name for placeholder.
715 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
718 for (i = 0; i < param->lsp_stripe_count; i++)
719 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
722 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
723 char errmsg[512] = "stripe already set";
726 if (errno != EEXIST && errno != EALREADY)
727 strncpy(errmsg, strerror(errno), sizeof(errmsg) - 1);
728 if (rc == -EREMOTEIO)
729 snprintf(errmsg, sizeof(errmsg),
730 "inactive OST among your specified %d OST(s)",
731 param->lsp_stripe_count);
733 llapi_err_noerrno(LLAPI_MSG_ERROR,
734 "setstripe error for '%s': %s", name, errmsg);
745 int llapi_file_open_pool(const char *name, int flags, int mode,
746 unsigned long long stripe_size, int stripe_offset,
747 int stripe_count, int stripe_pattern, char *pool_name)
749 const struct llapi_stripe_param param = {
750 .lsp_stripe_size = stripe_size,
751 .lsp_stripe_count = stripe_count,
752 .lsp_stripe_pattern = stripe_pattern,
753 .lsp_stripe_offset = stripe_offset,
754 .lsp_pool = pool_name
756 return llapi_file_open_param(name, flags, mode, ¶m);
759 int llapi_file_open(const char *name, int flags, int mode,
760 unsigned long long stripe_size, int stripe_offset,
761 int stripe_count, int stripe_pattern)
763 return llapi_file_open_pool(name, flags, mode, stripe_size,
764 stripe_offset, stripe_count,
765 stripe_pattern, NULL);
768 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
769 __u32 flags, char *foreign_lov)
772 struct lov_foreign_md *lfm;
775 if (foreign_lov == NULL) {
777 llapi_error(LLAPI_MSG_ERROR, rc,
778 "foreign LOV EA content must be provided");
782 len = strlen(foreign_lov);
783 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
786 llapi_error(LLAPI_MSG_ERROR, rc,
787 "foreign LOV EA size %zu (must be 0 < len < %zu)",
788 len, XATTR_SIZE_MAX -
789 offsetof(struct lov_foreign_md, lfm_value));
793 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
796 llapi_error(LLAPI_MSG_ERROR, rc,
797 "failed to allocate lov_foreign_md");
801 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
804 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
808 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
809 lfm->lfm_length = len;
810 lfm->lfm_type = type;
811 lfm->lfm_flags = flags;
812 memcpy(lfm->lfm_value, foreign_lov, len);
814 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
815 char *errmsg = "stripe already set";
819 errmsg = "not on a Lustre filesystem";
820 else if (errno == EEXIST || errno == EALREADY)
821 errmsg = "stripe already set";
823 errmsg = strerror(errno);
825 llapi_err_noerrno(LLAPI_MSG_ERROR,
826 "setstripe error for '%s': %s", name, errmsg);
842 int llapi_file_create(const char *name, unsigned long long stripe_size,
843 int stripe_offset, int stripe_count, int stripe_pattern)
847 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
848 stripe_offset, stripe_count, stripe_pattern,
857 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
858 int stripe_offset, int stripe_count,
859 int stripe_pattern, char *pool_name)
863 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
864 stripe_offset, stripe_count, stripe_pattern,
873 static int verify_dir_param(const char *name,
874 const struct llapi_stripe_param *param)
876 char fsname[MAX_OBD_NAME + 1] = { 0 };
877 char *pool_name = param->lsp_pool;
880 /* Make sure we are on a Lustre file system */
881 rc = llapi_search_fsname(name, fsname);
883 llapi_error(LLAPI_MSG_ERROR, rc,
884 "'%s' is not on a Lustre filesystem",
889 /* Check if the stripe pattern is sane. */
890 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
891 param->lsp_stripe_count,
892 param->lsp_stripe_pattern);
896 /* Make sure we have a good pool */
897 if (pool_name != NULL) {
899 * in case user gives the full pool name <fsname>.<poolname>,
902 char *ptr = strchr(pool_name, '.');
906 if (strcmp(pool_name, fsname) != 0) {
908 llapi_err_noerrno(LLAPI_MSG_ERROR,
909 "Pool '%s' is not on filesystem '%s'",
916 /* Make sure the pool exists and is non-empty */
917 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
919 char *err = rc == 0 ? "has no OSTs" : "does not exist";
921 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
922 fsname, pool_name, err);
927 /* sanity check of target list */
928 if (param->lsp_is_specific) {
929 char mdtname[MAX_OBD_NAME + 64];
933 for (i = 0; i < param->lsp_stripe_count; i++) {
934 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
935 fsname, param->lsp_tgts[i]);
936 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
941 llapi_error(LLAPI_MSG_ERROR, rc,
942 "%s: cannot find MDT %s in %s",
949 /* Make sure stripe offset is in MDT list. */
950 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
954 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
955 "%s: stripe offset '%d' is not in the target list",
956 __func__, param->lsp_stripe_offset);
964 static inline void param2lmu(struct lmv_user_md *lmu,
965 const struct llapi_stripe_param *param)
967 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
969 lmu->lum_stripe_count = param->lsp_stripe_count;
970 lmu->lum_stripe_offset = param->lsp_stripe_offset;
971 lmu->lum_hash_type = param->lsp_stripe_pattern;
972 if (param->lsp_pool != NULL)
973 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
974 if (param->lsp_is_specific) {
977 for (i = 0; i < param->lsp_stripe_count; i++)
978 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
982 int llapi_dir_set_default_lmv(const char *name,
983 const struct llapi_stripe_param *param)
985 struct lmv_user_md lmu = { 0 };
989 rc = verify_dir_param(name, param);
993 /* TODO: default lmv doesn't support specific targets yet */
994 if (param->lsp_is_specific)
997 param2lmu(&lmu, param);
999 fd = open(name, O_DIRECTORY | O_RDONLY);
1002 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1006 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
1008 char *errmsg = "stripe already set";
1011 if (errno != EEXIST && errno != EALREADY)
1012 errmsg = strerror(errno);
1014 llapi_err_noerrno(LLAPI_MSG_ERROR,
1015 "default dirstripe error on '%s': %s",
1022 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
1023 int stripe_count, int stripe_pattern,
1024 const char *pool_name)
1026 const struct llapi_stripe_param param = {
1027 .lsp_stripe_count = stripe_count,
1028 .lsp_stripe_offset = stripe_offset,
1029 .lsp_stripe_pattern = stripe_pattern,
1030 .lsp_pool = (char *)pool_name
1033 return llapi_dir_set_default_lmv(name, ¶m);
1037 * Create a Lustre directory.
1039 * \param name the name of the directory to be created
1040 * \param mode permission of the file if it is created, see mode in open(2)
1041 * \param param stripe pattern of the newly created directory
1043 * \retval 0 on success
1044 * \retval negative errno on failure
1046 int llapi_dir_create(const char *name, mode_t mode,
1047 const struct llapi_stripe_param *param)
1049 struct lmv_user_md *lmu = NULL;
1051 struct obd_ioctl_data data = { 0 };
1054 char *dirpath = NULL;
1055 char *namepath = NULL;
1060 rc = verify_dir_param(name, param);
1064 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1065 param->lsp_is_specific ?
1066 LMV_USER_MAGIC_SPECIFIC :
1069 lmu = calloc(1, lmu_size);
1073 dirpath = strdup(name);
1079 namepath = strdup(name);
1086 param2lmu(lmu, param);
1088 filename = basename(namepath);
1089 dir = dirname(dirpath);
1091 data.ioc_inlbuf1 = (char *)filename;
1092 data.ioc_inllen1 = strlen(filename) + 1;
1093 data.ioc_inlbuf2 = (char *)lmu;
1094 data.ioc_inllen2 = lmu_size;
1095 data.ioc_type = mode;
1096 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1098 llapi_error(LLAPI_MSG_ERROR, rc,
1099 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1104 fd = open(dir, O_DIRECTORY | O_RDONLY);
1107 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1111 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1112 char *errmsg = "stripe already set";
1115 if (errno != EEXIST && errno != EALREADY)
1116 errmsg = strerror(errno);
1118 llapi_err_noerrno(LLAPI_MSG_ERROR,
1119 "dirstripe error on '%s': %s", name, errmsg);
1130 * Create a foreign directory.
1132 * \param name the name of the directory to be created
1133 * \param mode permission of the file if it is created, see mode in open(2)
1134 * \param type foreign type to be set in LMV EA
1135 * \param flags foreign flags to be set in LMV EA
1136 * \param value foreign pattern to be set in LMV EA
1138 * \retval 0 on success
1139 * \retval negative errno on failure
1141 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1142 __u32 flags, const char *value)
1144 struct lmv_foreign_md *lfm = NULL;
1145 size_t lfm_size, len;
1146 struct obd_ioctl_data data = { 0 };
1149 char *dirpath = NULL;
1150 char *namepath = NULL;
1155 len = strlen(value);
1156 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1159 llapi_error(LLAPI_MSG_ERROR, rc,
1160 "invalid LOV EA length %zu (must be 0 < len < %zu)",
1161 len, XATTR_SIZE_MAX -
1162 offsetof(struct lmv_foreign_md, lfm_value));
1165 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1166 lfm = calloc(1, lfm_size);
1170 dirpath = strdup(name);
1176 namepath = strdup(name);
1183 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1184 lfm->lfm_length = len;
1185 lfm->lfm_type = type;
1186 lfm->lfm_flags = flags;
1187 memcpy(lfm->lfm_value, value, len);
1189 filename = basename(namepath);
1190 dir = dirname(dirpath);
1192 data.ioc_inlbuf1 = (char *)filename;
1193 data.ioc_inllen1 = strlen(filename) + 1;
1194 data.ioc_inlbuf2 = (char *)lfm;
1195 data.ioc_inllen2 = lfm_size;
1196 data.ioc_type = mode;
1197 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1199 llapi_error(LLAPI_MSG_ERROR, rc,
1200 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1205 fd = open(dir, O_DIRECTORY | O_RDONLY);
1208 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1212 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1213 char *errmsg = "stripe already set";
1216 if (errno != EEXIST && errno != EALREADY)
1217 errmsg = strerror(errno);
1219 llapi_err_noerrno(LLAPI_MSG_ERROR,
1220 "dirstripe error on '%s': %s", name, errmsg);
1230 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1231 int stripe_count, int stripe_pattern,
1232 const char *pool_name)
1234 const struct llapi_stripe_param param = {
1235 .lsp_stripe_count = stripe_count,
1236 .lsp_stripe_offset = stripe_offset,
1237 .lsp_stripe_pattern = stripe_pattern,
1238 .lsp_pool = (char *)pool_name
1241 return llapi_dir_create(name, mode, ¶m);
1244 int llapi_direntry_remove(char *dname)
1246 char *dirpath = NULL;
1247 char *namepath = NULL;
1253 dirpath = strdup(dname);
1254 namepath = strdup(dname);
1255 if (!dirpath || !namepath)
1258 filename = basename(namepath);
1260 dir = dirname(dirpath);
1262 fd = open(dir, O_DIRECTORY | O_RDONLY);
1265 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
1270 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename))
1271 llapi_error(LLAPI_MSG_ERROR, errno,
1272 "error on ioctl %#lx for '%s' (%d)",
1273 (long)LL_IOC_LMV_SETSTRIPE, filename, fd);
1283 * Find the fsname, the full path, and/or an open fd.
1284 * Either the fsname or path must not be NULL
1286 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1289 char buf[PATH_MAX], mntdir[PATH_MAX];
1290 char *ptr, *ptr_end;
1292 int idx = 0, len = 0, mntlen, fd;
1294 int fsnamelen, mountlen;
1296 /* get the mount point */
1297 fp = setmntent(PROC_MOUNTS, "r");
1300 llapi_error(LLAPI_MSG_ERROR, rc,
1301 "cannot retrieve filesystem mount point");
1305 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1308 if (!llapi_is_lustre_mnt(&mnt))
1311 if ((want & WANT_INDEX) && (idx++ != index))
1314 mntlen = strlen(mnt.mnt_dir);
1315 ptr = strchr(mnt.mnt_fsname, '/');
1316 while (ptr && *ptr == '/')
1319 * thanks to the call to llapi_is_lustre_mnt() above,
1320 * we are sure that mnt.mnt_fsname contains ":/",
1321 * so ptr should never be NULL
1326 while (*ptr_end != '/' && *ptr_end != '\0')
1329 /* Check the fsname for a match, if given */
1330 mountlen = ptr_end - ptr;
1331 if (!(want & WANT_FSNAME) && fsname != NULL &&
1332 (fsnamelen = strlen(fsname)) > 0 &&
1333 (fsnamelen != mountlen ||
1334 (strncmp(ptr, fsname, mountlen) != 0)))
1337 /* If the path isn't set return the first one we find */
1338 if (path == NULL || strlen(path) == 0) {
1339 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1340 mntdir[sizeof(mntdir) - 1] = '\0';
1341 if ((want & WANT_FSNAME) && fsname != NULL) {
1342 strncpy(fsname, ptr, mountlen);
1343 fsname[mountlen] = '\0';
1347 /* Otherwise find the longest matching path */
1348 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1349 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1350 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1351 mntdir[sizeof(mntdir) - 1] = '\0';
1353 if ((want & WANT_FSNAME) && fsname != NULL) {
1354 strncpy(fsname, ptr, mountlen);
1355 fsname[mountlen] = '\0';
1364 if ((want & WANT_PATH) && path != NULL) {
1365 strncpy(path, mntdir, PATH_MAX);
1366 path[strlen(mntdir)] = '\0';
1368 if (want & WANT_FD) {
1369 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1372 llapi_error(LLAPI_MSG_ERROR, rc,
1373 "cannot open '%s'", mntdir);
1379 } else if (want & WANT_ERROR)
1380 llapi_err_noerrno(LLAPI_MSG_ERROR,
1381 "'%s' not on a mounted Lustre filesystem",
1382 (want & WANT_PATH) ? fsname : path);
1387 * search lustre mounts
1389 * Calling this function will return to the user the mount point, mntdir, and
1390 * the file system name, fsname, if the user passed a buffer to this routine.
1392 * The user inputs are pathname and index. If the pathname is supplied then
1393 * the value of the index will be ignored. The pathname will return data if
1394 * the pathname is located on a lustre mount. Index is used to pick which
1395 * mount point you want in the case of multiple mounted lustre file systems.
1396 * See function lfs_osts in lfs.c for an example of the index use.
1398 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1401 int want = WANT_PATH, idx = -1;
1403 if (!pathname || pathname[0] == '\0') {
1407 strcpy(mntdir, pathname);
1411 want |= WANT_FSNAME;
1412 return get_root_path(want, fsname, NULL, mntdir, idx);
1415 /* Given a path, find the corresponding Lustre fsname */
1416 int llapi_search_fsname(const char *pathname, char *fsname)
1421 path = realpath(pathname, NULL);
1423 char tmp[PATH_MAX - 1];
1429 if (pathname[0] != '/') {
1431 * Need an absolute path, but realpath() only works for
1432 * pathnames that actually exist. We go through the
1433 * extra hurdle of dirname(getcwd() + pathname) in
1434 * case the relative pathname contains ".." in it.
1436 char realpath[PATH_MAX - 1];
1438 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1440 llapi_error(LLAPI_MSG_ERROR, rc,
1441 "cannot get current working directory");
1445 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1446 if (rc >= sizeof(tmp)) {
1448 llapi_error(LLAPI_MSG_ERROR, rc,
1449 "invalid parent path '%s'",
1455 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1456 if (rc >= sizeof(buf)) {
1458 llapi_error(LLAPI_MSG_ERROR, rc,
1459 "invalid path '%s'", pathname);
1462 path = realpath(buf, NULL);
1464 ptr = strrchr(buf, '/');
1466 llapi_error(LLAPI_MSG_ERROR |
1467 LLAPI_MSG_NO_ERRNO, 0,
1468 "cannot resolve path '%s'",
1473 path = realpath(buf, NULL);
1476 llapi_error(LLAPI_MSG_ERROR, rc,
1477 "cannot resolve path '%s'",
1483 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1488 int llapi_search_rootpath(char *pathname, const char *fsname)
1491 * pathname can be used as an argument by get_root_path(),
1492 * clear it for safety
1495 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1499 * Get the list of pool members.
1500 * \param poolname string of format \<fsname\>.\<poolname\>
1501 * \param members caller-allocated array of char*
1502 * \param list_size size of the members array
1503 * \param buffer caller-allocated buffer for storing OST names
1504 * \param buffer_size size of the buffer
1506 * \return number of members retrieved for this pool
1507 * \retval -error failure
1509 int llapi_get_poolmembers(const char *poolname, char **members,
1510 int list_size, char *buffer, int buffer_size)
1512 char fsname[PATH_MAX];
1521 /* name is FSNAME.POOLNAME */
1522 if (strlen(poolname) >= sizeof(fsname))
1525 snprintf(fsname, sizeof(fsname), "%s", poolname);
1526 pool = strchr(fsname, '.');
1533 rc = poolpath(&pathname, fsname, NULL);
1535 llapi_error(LLAPI_MSG_ERROR, rc,
1536 "Lustre filesystem '%s' not found",
1541 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1542 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1543 cfs_free_param_data(&pathname);
1544 if (rc >= sizeof(buf))
1546 fd = fopen(buf, "r");
1549 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1554 while (fgets(buf, sizeof(buf), fd) != NULL) {
1555 if (nb_entries >= list_size) {
1559 buf[sizeof(buf) - 1] = '\0';
1561 tmp = strchr(buf, '\n');
1564 if (used + strlen(buf) + 1 > buffer_size) {
1569 strcpy(buffer + used, buf);
1570 members[nb_entries] = buffer + used;
1571 used += strlen(buf) + 1;
1581 * Get the list of pools in a filesystem.
1582 * \param name filesystem name or path
1583 * \param poollist caller-allocated array of char*
1584 * \param list_size size of the poollist array
1585 * \param buffer caller-allocated buffer for storing pool names
1586 * \param buffer_size size of the buffer
1588 * \return number of pools retrieved for this filesystem
1589 * \retval -error failure
1591 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1592 char *buffer, int buffer_size)
1598 struct dirent *pool;
1600 unsigned int nb_entries = 0;
1601 unsigned int used = 0;
1604 /* initialize output array */
1605 for (i = 0; i < list_size; i++)
1608 /* is name a pathname ? */
1609 ptr = strchr(name, '/');
1611 char fsname_buf[MAXNAMLEN];
1613 /* We will need fsname for printing later */
1614 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1618 ptr = strrchr(fsname_buf, '-');
1622 fsname = strdup(fsname_buf);
1626 /* name is FSNAME */
1627 fsname = strdup(name);
1632 rc = poolpath(&pathname, fsname, NULL);
1634 llapi_error(LLAPI_MSG_ERROR, rc,
1635 "Lustre filesystem '%s' not found", name);
1639 dir = opendir(pathname.gl_pathv[0]);
1642 llapi_error(LLAPI_MSG_ERROR, rc,
1643 "Could not open pool list for '%s'",
1650 pool = readdir(dir);
1656 /* ignore . and .. */
1657 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1660 /* check output bounds */
1661 if (nb_entries >= list_size) {
1663 goto free_dir_no_msg;
1666 /* +2 for '.' and final '\0' */
1667 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1670 goto free_dir_no_msg;
1673 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1674 poollist[nb_entries] = buffer + used;
1675 used += strlen(pool->d_name) + strlen(fsname) + 2;
1681 llapi_error(LLAPI_MSG_ERROR, rc,
1682 "Error reading pool list for '%s'", name);
1684 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1689 cfs_free_param_data(&pathname);
1692 return rc != 0 ? rc : nb_entries;
1695 /* wrapper for lfs.c and obd.c */
1696 int llapi_poollist(const char *name)
1699 * list of pool names (assume that pool count is smaller
1702 char **list, *buffer = NULL, *fsname = (char *)name;
1703 char *poolname = NULL, *tmp = NULL, data[16];
1704 enum param_filter type = FILTER_BY_PATH;
1705 int obdcount, bufsize, rc, nb, i;
1710 if (name[0] != '/') {
1711 fsname = strdup(name);
1715 poolname = strchr(fsname, '.');
1718 type = FILTER_BY_FS_NAME;
1721 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1722 data, sizeof(data));
1725 obdcount = atoi(data);
1728 * Allocate space for each fsname-OST0000_UUID, 1 per OST,
1729 * and also an array to store the pointers for all that
1733 bufsize = sizeof(struct obd_uuid) * obdcount;
1734 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1735 if (buffer == NULL) {
1739 list = (char **) (buffer + bufsize);
1742 /* name is a path or fsname */
1743 nb = llapi_get_poollist(name, list, obdcount,
1746 /* name is a pool name (<fsname>.<poolname>) */
1747 nb = llapi_get_poolmembers(name, list, obdcount,
1751 if (nb == -EOVERFLOW) {
1754 goto retry_get_pools;
1757 for (i = 0; i < nb; i++)
1758 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1759 rc = (nb < 0 ? nb : 0);
1763 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1768 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1769 void *data, struct dirent64 *de);
1771 #define OBD_NOT_FOUND (-1)
1773 static void find_param_fini(struct find_param *param)
1775 if (param->fp_migrate)
1778 if (param->fp_obd_indexes) {
1779 free(param->fp_obd_indexes);
1780 param->fp_obd_indexes = NULL;
1783 if (param->fp_lmd) {
1784 free(param->fp_lmd);
1785 param->fp_lmd = NULL;
1788 if (param->fp_lmv_md) {
1789 free(param->fp_lmv_md);
1790 param->fp_lmv_md = NULL;
1794 static int common_param_init(struct find_param *param, char *path)
1796 int lum_size = get_mds_md_size(path);
1801 /* migrate has fp_lmv_md initialized outside */
1802 if (param->fp_migrate)
1805 if (lum_size < PATH_MAX + 1)
1806 lum_size = PATH_MAX + 1;
1808 param->fp_lum_size = lum_size;
1809 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1811 if (param->fp_lmd == NULL) {
1812 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1813 "error: allocate %zu bytes for layout failed",
1814 sizeof(lstat_t) + param->fp_lum_size);
1818 param->fp_lmv_stripe_count = 256;
1819 param->fp_lmv_md = calloc(1,
1820 lmv_user_md_size(param->fp_lmv_stripe_count,
1821 LMV_USER_MAGIC_SPECIFIC));
1822 if (param->fp_lmv_md == NULL) {
1823 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1824 "error: allocation of %d bytes for ioctl",
1825 lmv_user_md_size(param->fp_lmv_stripe_count,
1826 LMV_USER_MAGIC_SPECIFIC));
1827 find_param_fini(param);
1831 param->fp_got_uuids = 0;
1832 param->fp_obd_indexes = NULL;
1833 param->fp_obd_index = OBD_NOT_FOUND;
1834 param->fp_mdt_index = OBD_NOT_FOUND;
1838 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1839 struct dirent64 *de)
1841 struct find_param *param = data;
1847 /* set errno upon failure */
1848 static DIR *opendir_parent(const char *path)
1854 path_copy = strdup(path);
1855 if (path_copy == NULL)
1858 parent_path = dirname(path_copy);
1859 parent = opendir(parent_path);
1865 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1870 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1871 if (param->fp_get_default_lmv)
1872 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1874 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1876 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1877 if (errno == E2BIG && ret != 0) {
1881 /* if foreign LMV case, fake stripes number */
1882 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
1883 struct lmv_foreign_md *lfm;
1885 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1886 if (lfm->lfm_length < XATTR_SIZE_MAX -
1887 offsetof(typeof(*lfm), lfm_value)) {
1888 uint32_t size = lfm->lfm_length +
1889 offsetof(typeof(*lfm), lfm_value);
1891 stripe_count = lmv_foreign_to_md_stripes(size);
1893 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1894 "error: invalid %d foreign size returned from ioctl",
1899 stripe_count = param->fp_lmv_md->lum_stripe_count;
1901 if (stripe_count <= param->fp_lmv_stripe_count)
1904 free(param->fp_lmv_md);
1905 param->fp_lmv_stripe_count = stripe_count;
1906 lmv_size = lmv_user_md_size(stripe_count,
1907 LMV_USER_MAGIC_SPECIFIC);
1908 param->fp_lmv_md = malloc(lmv_size);
1909 if (param->fp_lmv_md == NULL) {
1910 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1911 "error: allocation of %d bytes for ioctl",
1912 lmv_user_md_size(param->fp_lmv_stripe_count,
1913 LMV_USER_MAGIC_SPECIFIC));
1921 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1924 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
1925 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
1926 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
1927 lmd_v2->lmd_stx.stx_uid = st->st_uid;
1928 lmd_v2->lmd_stx.stx_gid = st->st_gid;
1929 lmd_v2->lmd_stx.stx_mode = st->st_mode;
1930 lmd_v2->lmd_stx.stx_ino = st->st_ino;
1931 lmd_v2->lmd_stx.stx_size = st->st_size;
1932 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
1933 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
1934 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
1935 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
1936 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
1937 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
1938 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
1939 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
1940 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
1942 lmd_v2->lmd_flags = 0;
1944 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1946 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
1947 if (lmd_v2->lmd_stx.stx_size)
1948 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
1949 if (lmd_v2->lmd_stx.stx_blocks)
1950 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
1952 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1953 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
1954 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
1959 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
1961 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
1962 struct lov_user_mds_data *lmd_v2 = lmdbuf;
1966 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
1970 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
1973 st = lmd_v1->lmd_st;
1974 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
1975 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
1976 convert_lmd_statx(lmd_v2, &st, false);
1977 lmd_v2->lmd_lmmsize = 0;
1978 lmd_v2->lmd_padding = 0;
1983 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
1984 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1986 struct lov_user_mds_data *lmd = lmdbuf;
1987 static bool use_old_ioctl;
1991 if (parent_fd < 0 && dir_fd < 0)
1993 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1998 * LL_IOC_MDC_GETINFO operates on the current directory inode
1999 * and returns struct lov_user_mds_data, while
2000 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
2002 if (type == GET_LMD_INFO)
2003 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_OLD :
2006 cmd = LL_IOC_LOV_GETSTRIPE;
2009 ret = ioctl(dir_fd, cmd, lmdbuf);
2010 if (ret < 0 && errno == ENOTTY && cmd == LL_IOC_MDC_GETINFO) {
2011 cmd = LL_IOC_MDC_GETINFO_OLD;
2012 use_old_ioctl = true;
2016 if (cmd == LL_IOC_MDC_GETINFO_OLD && !ret)
2017 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2018 } else if (parent_fd >= 0) {
2019 const char *fname = strrchr(path, '/');
2022 * IOC_MDC_GETFILEINFO takes as input the filename (relative to
2023 * the parent directory) and returns struct lov_user_mds_data,
2024 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2026 * This avoids opening, locking, and closing each file on the
2027 * client if that is not needed. Multiple of these ioctl() can
2028 * be done on the parent dir with a single open for all
2029 * files in that directory, and it also doesn't pollute the
2030 * client dcache with millions of dentries when traversing
2031 * a large filesystem.
2033 fname = (fname == NULL ? path : fname + 1);
2035 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2038 else if (ret >= lmdlen || ret++ == 0)
2041 if (type == GET_LMD_INFO)
2042 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_OLD :
2043 IOC_MDC_GETFILEINFO;
2045 cmd = IOC_MDC_GETFILESTRIPE;
2048 ret = ioctl(parent_fd, cmd, lmdbuf);
2049 if (ret < 0 && errno == ENOTTY &&
2050 cmd == IOC_MDC_GETFILEINFO) {
2051 cmd = IOC_MDC_GETFILEINFO_OLD;
2052 use_old_ioctl = true;
2053 goto retry_getfileinfo;
2056 if (cmd == IOC_MDC_GETFILEINFO_OLD && !ret)
2057 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2061 if (ret && type == GET_LMD_INFO) {
2062 if (errno == ENOTTY) {
2066 * ioctl is not supported, it is not a lustre fs.
2067 * Do the regular lstat(2) instead.
2069 ret = lstat_f(path, &st);
2072 llapi_error(LLAPI_MSG_ERROR, ret,
2073 "error: %s: lstat failed for %s",
2077 convert_lmd_statx(lmd, &st, true);
2079 * It may be wrong to set use_old_ioctl with true as
2080 * the file is not a lustre fs. So reset it with false
2083 use_old_ioctl = false;
2084 } else if (errno == ENOENT) {
2086 llapi_error(LLAPI_MSG_WARN, ret,
2087 "warning: %s does not exist", path);
2088 } else if (errno != EISDIR && errno != ENODATA) {
2090 llapi_error(LLAPI_MSG_ERROR, ret,
2091 "%s ioctl failed for %s.",
2092 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2093 "IOC_MDC_GETFILEINFO", path);
2100 static int get_lmd_info(char *path, DIR *parent, DIR *dir, void *lmdbuf,
2101 int lmdlen, enum get_lmd_info_type type)
2107 parent_fd = dirfd(parent);
2109 dir_fd = dirfd(dir);
2111 return get_lmd_info_fd(path, parent_fd, dir_fd, lmdbuf, lmdlen, type);
2114 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
2115 semantic_func_t sem_init,
2116 semantic_func_t sem_fini, void *data,
2117 struct dirent64 *de)
2119 struct find_param *param = (struct find_param *)data;
2120 struct dirent64 *dent;
2128 if (!d && errno != ENOTDIR) {
2130 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2133 } else if (!d && !parent) {
2134 /* ENOTDIR. Open the parent dir. */
2135 p = opendir_parent(path);
2142 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
2148 while ((dent = readdir64(d)) != NULL) {
2151 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2155 if ((len + dent->d_reclen + 2) > size) {
2156 llapi_err_noerrno(LLAPI_MSG_ERROR,
2157 "error: %s: string buffer too small",
2162 strcat(path, dent->d_name);
2164 if (dent->d_type == DT_UNKNOWN) {
2165 struct lov_user_mds_data *lmd = param->fp_lmd;
2167 rc = get_lmd_info(path, d, NULL, lmd,
2168 param->fp_lum_size, GET_LMD_INFO);
2170 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2177 switch (dent->d_type) {
2179 llapi_err_noerrno(LLAPI_MSG_ERROR,
2180 "error: %s: '%s' is UNKNOWN type %d",
2181 __func__, dent->d_name, dent->d_type);
2184 rc = llapi_semantic_traverse(path, size, d, sem_init,
2185 sem_fini, data, dent);
2186 if (rc != 0 && ret == 0)
2192 rc = sem_init(path, d, NULL, data, dent);
2193 if (rc < 0 && ret == 0) {
2198 if (sem_fini && rc == 0)
2199 sem_fini(path, d, NULL, data, dent);
2207 sem_fini(path, parent, &d, data, de);
2216 static int param_callback(char *path, semantic_func_t sem_init,
2217 semantic_func_t sem_fini, struct find_param *param)
2219 int ret, len = strlen(path);
2222 if (len > PATH_MAX) {
2224 llapi_error(LLAPI_MSG_ERROR, ret,
2225 "Path name '%s' is too long", path);
2229 buf = (char *)malloc(PATH_MAX + 1);
2233 snprintf(buf, PATH_MAX + 1, "%s", path);
2234 ret = common_param_init(param, buf);
2238 param->fp_depth = 0;
2240 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
2241 sem_fini, param, NULL);
2243 find_param_fini(param);
2245 return ret < 0 ? ret : 0;
2248 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2250 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
2254 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2259 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2261 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2265 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2270 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2274 fd = open(path, O_RDONLY | O_NONBLOCK);
2277 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
2281 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2287 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2291 fd = open(path, O_RDONLY | O_NONBLOCK);
2294 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2298 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2310 * If uuidp is NULL, return the number of available obd uuids.
2311 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2312 * there are more OSTs than allocated to uuidp, then an error is returned with
2313 * the ost_count set to number of available obd uuids.
2315 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2316 int *ost_count, enum tgt_type type)
2318 char buf[PATH_MAX], format[32];
2319 int rc = 0, index = 0;
2320 struct obd_uuid name;
2324 /* Get the lov name */
2325 if (type == LOV_TYPE)
2326 rc = llapi_file_fget_lov_uuid(fd, &name);
2328 rc = llapi_file_fget_lmv_uuid(fd, &name);
2332 /* Now get the ost uuids */
2333 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2334 FILTER_BY_EXACT, "target_obd", ¶m);
2338 fp = fopen(param.gl_pathv[0], "r");
2341 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2346 snprintf(format, sizeof(format),
2347 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2348 while (fgets(buf, sizeof(buf), fp) != NULL) {
2349 if (uuidp && (index < *ost_count)) {
2350 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2358 if (uuidp && (index > *ost_count))
2363 cfs_free_param_data(¶m);
2367 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2369 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2372 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2377 root = open(mnt, O_RDONLY | O_DIRECTORY);
2380 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2385 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2394 * Check if user specified value matches a real uuid. Ignore _UUID,
2395 * -osc-4ba41334, other trailing gunk in comparison.
2396 * @param real_uuid ends in "_UUID"
2397 * @param search_uuid may or may not end in "_UUID"
2399 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2401 int cmplen = strlen(real_uuid);
2402 int searchlen = strlen(search_uuid);
2404 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2406 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2410 * The UUIDs may legitimately be different lengths, if
2411 * the system was upgraded from an older version.
2413 if (cmplen != searchlen)
2416 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2420 * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2421 * returned in param->fp_obd_index
2423 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2425 struct obd_uuid obd_uuid;
2432 if (param->fp_got_uuids)
2435 /* Get the lov/lmv name */
2436 if (param->fp_get_lmv)
2437 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2439 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2441 if (rc != -ENOTTY) {
2442 llapi_error(LLAPI_MSG_ERROR, rc,
2443 "error: can't get %s name: %s",
2444 param->fp_get_lmv ? "lmv" : "lov",
2452 param->fp_got_uuids = 1;
2454 /* Now get the ost uuids */
2455 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2456 obd_uuid.uuid, FILTER_BY_EXACT,
2457 "target_obd", ¶m_data);
2461 fp = fopen(param_data.gl_pathv[0], "r");
2464 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2465 param_data.gl_pathv[0]);
2469 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2470 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2471 param->fp_get_lmv ? "MDTS" : "OBDS");
2473 snprintf(format, sizeof(format),
2474 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2475 while (fgets(buf, sizeof(buf), fp) != NULL) {
2478 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2481 if (param->fp_obd_uuid) {
2482 if (llapi_uuid_match(obd_uuid.uuid,
2483 param->fp_obd_uuid->uuid)) {
2484 param->fp_obd_index = index;
2487 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2488 /* Print everything */
2489 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2492 param->fp_obds_printed = 1;
2496 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2497 llapi_err_noerrno(LLAPI_MSG_ERROR,
2498 "error: %s: unknown obduuid: %s",
2499 __func__, param->fp_obd_uuid->uuid);
2503 cfs_free_param_data(¶m_data);
2508 * In this case, param->fp_obd_uuid will be an array of obduuids and
2509 * obd index for all these obduuids will be returned in
2510 * param->fp_obd_indexes
2512 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2513 int num_obds, int **obdindexes, int *obdindex,
2516 int ret, obdcount, obd_valid = 0, obdnum;
2518 struct obd_uuid *uuids = NULL;
2522 if (type == LOV_TYPE)
2523 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2525 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2529 obdcount = atoi(buf);
2530 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2535 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2537 if (ret == -EOVERFLOW) {
2538 struct obd_uuid *uuids_temp;
2540 uuids_temp = realloc(uuids, obdcount *
2541 sizeof(struct obd_uuid));
2542 if (uuids_temp != NULL) {
2544 goto retry_get_uuids;
2549 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2553 indexes = malloc(num_obds * sizeof(*obdindex));
2554 if (indexes == NULL) {
2559 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2562 /* The user may have specified a simple index */
2563 i = strtol(obduuids[obdnum].uuid, &end, 0);
2564 if (end && *end == '\0' && i < obdcount) {
2565 indexes[obdnum] = i;
2568 for (i = 0; i < obdcount; i++) {
2569 if (llapi_uuid_match(uuids[i].uuid,
2570 obduuids[obdnum].uuid)) {
2571 indexes[obdnum] = i;
2577 if (i >= obdcount) {
2578 indexes[obdnum] = OBD_NOT_FOUND;
2579 llapi_err_noerrno(LLAPI_MSG_ERROR,
2580 "invalid obduuid '%s'",
2581 obduuids[obdnum].uuid);
2587 *obdindex = OBD_NOT_FOUND;
2589 *obdindex = obd_valid;
2591 *obdindexes = indexes;
2599 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2603 if (param->fp_mdt_uuid) {
2604 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2606 ¶m->fp_mdt_indexes,
2607 ¶m->fp_mdt_index, LMV_TYPE);
2612 if (param->fp_obd_uuid) {
2613 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2615 ¶m->fp_obd_indexes,
2616 ¶m->fp_obd_index, LOV_TYPE);
2621 param->fp_got_uuids = 1;
2626 int llapi_ostlist(char *path, struct find_param *param)
2631 fd = open(path, O_RDONLY | O_DIRECTORY);
2635 ret = setup_obd_uuid(fd, path, param);
2642 * Tries to determine the default stripe attributes for a given filesystem. The
2643 * filesystem to check should be specified by fsname, or will be determined
2646 static int sattr_get_defaults(const char *const fsname,
2647 unsigned int *scount,
2648 unsigned int *ssize,
2649 unsigned int *soffset)
2655 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2656 "stripecount", val, sizeof(val));
2659 *scount = atoi(val);
2663 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2664 "stripesize", val, sizeof(val));
2671 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2672 "stripeoffset", val, sizeof(val));
2675 *soffset = atoi(val);
2682 * Tries to gather the default stripe attributes for a given filesystem. If
2683 * the attributes can be determined, they are cached for easy retreival the
2684 * next time they are needed. Only a single filesystem's attributes are
2687 int sattr_cache_get_defaults(const char *const fsname,
2688 const char *const pathname, unsigned int *scount,
2689 unsigned int *ssize, unsigned int *soffset)
2692 char fsname[PATH_MAX + 1];
2693 unsigned int stripecount;
2694 unsigned int stripesize;
2695 unsigned int stripeoffset;
2701 char fsname_buf[PATH_MAX + 1];
2702 unsigned int tmp[3];
2704 if (fsname == NULL) {
2705 rc = llapi_search_fsname(pathname, fsname_buf);
2709 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2712 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2714 * Ensure all 3 sattrs (count, size, and offset) are
2715 * successfully retrieved and stored in tmp before writing to
2718 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2722 cache.stripecount = tmp[0];
2723 cache.stripesize = tmp[1];
2724 cache.stripeoffset = tmp[2];
2725 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2729 *scount = cache.stripecount;
2731 *ssize = cache.stripesize;
2733 *soffset = cache.stripeoffset;
2738 static char *layout2name(__u32 layout_pattern)
2740 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2742 else if (layout_pattern == LOV_PATTERN_MDT)
2744 else if (layout_pattern == LOV_PATTERN_RAID0)
2746 else if (layout_pattern ==
2747 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2748 return "raid0,overstriped";
2753 enum lov_dump_flags {
2754 LDF_IS_DIR = 0x0001,
2755 LDF_IS_RAW = 0x0002,
2756 LDF_INDENT = 0x0004,
2757 LDF_SKIP_OBJS = 0x0008,
2759 LDF_EXTENSION = 0x0020,
2762 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2763 struct lov_user_ost_data_v1 *objects,
2764 enum llapi_layout_verbose verbose,
2765 int depth, char *pool_name,
2766 enum lov_dump_flags flags)
2768 bool is_dir = flags & LDF_IS_DIR;
2769 bool is_raw = flags & LDF_IS_RAW;
2770 bool indent = flags & LDF_INDENT;
2771 bool yaml = flags & LDF_YAML;
2772 bool skip_objs = flags & LDF_SKIP_OBJS;
2773 bool extension = flags & LDF_EXTENSION;
2774 char *prefix = is_dir ? "" : "lmm_";
2775 char *separator = "";
2776 char *space = indent ? " " : "";
2779 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2780 lmm_oi_set_seq(&lum->lmm_oi, 0);
2781 if (!indent && (verbose & VERBOSE_DETAIL))
2782 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2785 if (!yaml && !indent && depth && path &&
2786 ((verbose != VERBOSE_OBJID) || !is_dir))
2787 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2789 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2790 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2791 space, prefix, lum->lmm_magic);
2792 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2794 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2795 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2797 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2799 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2804 if (verbose & ~VERBOSE_DFID)
2805 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2808 * This needs a bit of hand-holding since old 1.x lmm_oi
2809 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2810 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2811 * a real FID. Ideally the 2.x code would have stored this
2812 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2813 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2814 * worked properly (especially since IGIF FIDs use mds_inum as
2815 * the FID SEQ), but unfortunately that didn't happen.
2817 * Print it to look like an IGIF FID, even though the fields
2818 * are reversed on disk, so that it makes sense to userspace.
2820 * Don't use ostid_id() and ostid_seq(), since they assume the
2821 * oi_fid fields are in the right order. This is why there are
2822 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2824 * For newer layout types hopefully this will be a real FID.
2826 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2827 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2828 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2829 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2830 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2832 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2833 (unsigned long long)seq, oid, ver);
2835 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2836 (unsigned long long)seq, oid, ver);
2839 if (verbose & VERBOSE_STRIPE_COUNT) {
2840 if (verbose & ~VERBOSE_STRIPE_COUNT)
2841 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2844 if (!is_raw && lum->lmm_stripe_count == 0 &&
2845 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2846 unsigned int scount;
2848 rc = sattr_cache_get_defaults(NULL, path,
2852 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2855 llapi_error(LLAPI_MSG_ERROR, rc,
2856 "Cannot determine default stripe count.");
2858 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2860 (__s16)lum->lmm_stripe_count);
2863 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2865 (__s16)lum->lmm_stripe_count);
2867 if (!yaml && is_dir)
2873 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
2874 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
2875 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2876 if (verbose & ~VERBOSE_EXT_SIZE && extension)
2877 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
2879 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
2880 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2882 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2885 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2888 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2890 llapi_error(LLAPI_MSG_ERROR, rc,
2891 "Cannot determine default stripe size.");
2893 /* Extension size is in KiB */
2894 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2896 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
2897 (unsigned long long)lum->lmm_stripe_size);
2899 if (!yaml && is_dir)
2905 if ((verbose & VERBOSE_PATTERN)) {
2906 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2907 if (verbose & ~VERBOSE_PATTERN)
2908 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2910 if (lov_pattern_supported(lum->lmm_pattern))
2911 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2912 layout2name(lum->lmm_pattern));
2914 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2915 separator = (!yaml && is_dir) ? " " : "\n";
2918 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2919 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2920 if (verbose & ~VERBOSE_GENERATION)
2921 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2923 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2924 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2928 if (verbose & VERBOSE_STRIPE_OFFSET) {
2929 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2930 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2931 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2933 if (is_dir || skip_objs)
2934 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2935 lum->lmm_stripe_offset ==
2936 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2937 lum->lmm_stripe_offset);
2938 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
2939 llapi_printf(LLAPI_MSG_NORMAL, "0");
2941 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2942 objects[0].l_ost_idx);
2943 if (!yaml && is_dir)
2949 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2950 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2951 if (verbose & ~VERBOSE_POOL)
2952 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2954 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2955 if (!yaml && is_dir)
2961 if (strlen(separator) != 0)
2962 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2965 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2966 struct lov_user_ost_data_v1 *objects,
2967 char *path, int obdindex, int depth,
2968 enum llapi_layout_verbose verbose,
2969 enum lov_dump_flags flags)
2971 bool is_dir = flags & LDF_IS_DIR;
2972 bool indent = flags & LDF_INDENT;
2973 bool skip_objs = flags & LDF_SKIP_OBJS;
2974 bool yaml = flags & LDF_YAML;
2975 bool obdstripe = obdindex == OBD_NOT_FOUND;
2978 if (!obdstripe && !skip_objs) {
2979 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2980 if (obdindex == objects[i].l_ost_idx) {
2990 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2993 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
2994 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2995 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
2996 char *space = " - ";
2999 llapi_printf(LLAPI_MSG_NORMAL,
3000 "%6slmm_objects:\n", " ");
3002 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
3004 llapi_printf(LLAPI_MSG_NORMAL,
3005 "\tobdidx\t\t objid\t\t objid\t\t group\n");
3007 for (i = 0; i < lum->lmm_stripe_count; i++) {
3008 int idx = objects[i].l_ost_idx;
3009 long long oid = ostid_id(&objects[i].l_ost_oi);
3010 long long gr = ostid_seq(&objects[i].l_ost_oi);
3012 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
3016 struct lu_fid fid = { 0 };
3018 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3019 llapi_printf(LLAPI_MSG_NORMAL,
3020 "%sl_ost_idx: %d\n", space, idx);
3021 llapi_printf(LLAPI_MSG_NORMAL,
3022 "%8sl_fid: "DFID_NOBRACE"\n",
3024 } else if (indent) {
3025 struct lu_fid fid = { 0 };
3027 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3028 llapi_printf(LLAPI_MSG_NORMAL,
3029 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
3030 space, i, idx, PFID(&fid));
3034 sprintf(fmt, "%s%s%s\n",
3035 "\t%6u\t%14llu\t%#13llx\t",
3036 (fid_seq_is_rsvd(gr) ||
3037 fid_seq_is_mdt0(gr)) ?
3038 "%14llu" : "%#14llx", "%s");
3039 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
3041 obdindex == idx ? " *" : "");
3045 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3048 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3049 char *path, int obdindex, int depth,
3050 enum llapi_layout_verbose verbose,
3051 enum lov_dump_flags flags)
3053 struct lmv_user_mds_data *objects = lum->lum_objects;
3054 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3055 char *separator = "";
3056 bool yaml = flags & LDF_YAML;
3057 bool obdstripe = false;
3060 if (obdindex != OBD_NOT_FOUND) {
3061 if (lum->lum_stripe_count == 0) {
3062 if (obdindex == lum->lum_stripe_offset)
3065 for (i = 0; i < lum->lum_stripe_count; i++) {
3066 if (obdindex == objects[i].lum_mds) {
3067 llapi_printf(LLAPI_MSG_NORMAL,
3082 /* show all information default */
3084 if (lum->lum_magic == LMV_USER_MAGIC)
3085 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3086 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3088 verbose = VERBOSE_OBJID;
3091 if (depth && path && ((verbose != VERBOSE_OBJID)))
3092 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3094 if (verbose & VERBOSE_STRIPE_COUNT) {
3095 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3096 if (verbose & ~VERBOSE_STRIPE_COUNT)
3097 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3098 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3099 (int)lum->lum_stripe_count);
3100 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3106 if (verbose & VERBOSE_STRIPE_OFFSET) {
3107 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3108 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3109 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3110 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3111 (int)lum->lum_stripe_offset);
3112 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3118 if (verbose & VERBOSE_HASH_TYPE) {
3119 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3120 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3122 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3123 if (verbose & ~VERBOSE_HASH_TYPE)
3124 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3125 if (type < LMV_HASH_TYPE_MAX)
3126 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3127 mdt_hash_name[type]);
3129 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3131 if (flags & LMV_HASH_FLAG_MIGRATION)
3132 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3133 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3134 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3135 if (flags & LMV_HASH_FLAG_LOST_LMV)
3136 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3141 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3142 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3143 if (lum->lum_stripe_count > 0)
3144 llapi_printf(LLAPI_MSG_NORMAL,
3145 "mdtidx\t\t FID[seq:oid:ver]\n");
3146 for (i = 0; i < lum->lum_stripe_count; i++) {
3147 int idx = objects[i].lum_mds;
3148 struct lu_fid *fid = &objects[i].lum_fid;
3150 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3151 llapi_printf(LLAPI_MSG_NORMAL,
3152 "%6u\t\t "DFID"\t\t%s\n",
3154 obdindex == idx ? " *" : "");
3158 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3159 pool_name[0] != '\0') {
3160 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3161 if (verbose & ~VERBOSE_POOL)
3162 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3164 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3168 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3169 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3172 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3173 enum lov_dump_flags flags)
3175 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3176 int depth = param->fp_max_depth;
3177 enum llapi_layout_verbose verbose = param->fp_verbose;
3178 bool yaml = flags & LDF_YAML;
3180 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3181 !(flags & LDF_IS_DIR)) && !yaml)
3182 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3184 if (verbose & VERBOSE_DETAIL) {
3185 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3186 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3187 " ", comp_v1->lcm_magic);
3188 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3189 " ", comp_v1->lcm_size);
3190 if (flags & LDF_IS_DIR)
3191 llapi_printf(LLAPI_MSG_NORMAL,
3192 "%2slcm_flags: %s\n", " ",
3193 comp_v1->lcm_mirror_count > 0 ?
3196 llapi_printf(LLAPI_MSG_NORMAL,
3197 "%2slcm_flags: %s\n", " ",
3198 llapi_layout_flags_string(comp_v1->lcm_flags));
3201 if (verbose & VERBOSE_GENERATION) {
3202 if (verbose & ~VERBOSE_GENERATION)
3203 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3205 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3208 if (verbose & VERBOSE_MIRROR_COUNT) {
3209 if (verbose & ~VERBOSE_MIRROR_COUNT)
3210 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3212 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3213 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3214 comp_v1->lcm_mirror_count + 1 : 1);
3217 if (verbose & VERBOSE_COMP_COUNT) {
3218 if (verbose & ~VERBOSE_COMP_COUNT)
3219 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3221 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3222 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3223 comp_v1->lcm_entry_count : 0);
3226 if (verbose & VERBOSE_DETAIL && !yaml)
3227 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3230 static void lcme_flags2str(__u32 comp_flags)
3236 llapi_printf(LLAPI_MSG_NORMAL, "0");
3239 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3240 if (comp_flags & comp_flags_table[i].cfn_flag) {
3242 llapi_printf(LLAPI_MSG_NORMAL, ",");
3243 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3244 comp_flags_table[i].cfn_name);
3245 comp_flags &= ~comp_flags_table[i].cfn_flag;
3251 llapi_printf(LLAPI_MSG_NORMAL, ",");
3252 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3256 static void lov_dump_comp_v1_entry(struct find_param *param,
3257 enum lov_dump_flags flags, int index)
3259 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3260 struct lov_comp_md_entry_v1 *entry;
3261 char *separator = "";
3262 enum llapi_layout_verbose verbose = param->fp_verbose;
3263 bool yaml = flags & LDF_YAML;
3265 entry = &comp_v1->lcm_entries[index];
3268 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3270 if (verbose & VERBOSE_COMP_ID) {
3271 if (verbose & VERBOSE_DETAIL && !yaml)
3272 llapi_printf(LLAPI_MSG_NORMAL,
3273 "%slcme_id: ", " - ");
3274 else if (verbose & ~VERBOSE_COMP_ID)
3275 llapi_printf(LLAPI_MSG_NORMAL,
3276 "%4slcme_id: ", " ");
3277 if (entry->lcme_id != LCME_ID_INVAL)
3278 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3280 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3284 if (verbose & VERBOSE_MIRROR_ID) {
3285 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3286 if (verbose & ~VERBOSE_MIRROR_ID)
3287 llapi_printf(LLAPI_MSG_NORMAL,
3288 "%4slcme_mirror_id: ", " ");
3289 if (entry->lcme_id != LCME_ID_INVAL)
3290 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3291 mirror_id_of(entry->lcme_id));
3293 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3297 if (verbose & VERBOSE_COMP_FLAGS) {
3298 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3299 if (verbose & ~VERBOSE_COMP_FLAGS)
3300 llapi_printf(LLAPI_MSG_NORMAL,
3301 "%4slcme_flags: ", " ");
3302 lcme_flags2str(entry->lcme_flags);
3305 /* print snapshot timestamp if its a nosync comp */
3306 if ((verbose & VERBOSE_COMP_FLAGS) &&
3307 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3308 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3309 if (verbose & ~VERBOSE_COMP_FLAGS)
3310 llapi_printf(LLAPI_MSG_NORMAL,
3311 "%4slcme_timestamp: ", " ");
3313 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3314 (unsigned long long)entry->lcme_timestamp);
3316 time_t stamp = entry->lcme_timestamp;
3317 char *date_str = asctime(localtime(&stamp));
3319 date_str[strlen(date_str) - 1] = '\0';
3320 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3326 if (verbose & VERBOSE_COMP_START) {
3327 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3328 if (verbose & ~VERBOSE_COMP_START)
3329 llapi_printf(LLAPI_MSG_NORMAL,
3330 "%4slcme_extent.e_start: ", " ");
3331 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3332 (unsigned long long)entry->lcme_extent.e_start);
3336 if (verbose & VERBOSE_COMP_END) {
3337 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3338 if (verbose & ~VERBOSE_COMP_END)
3339 llapi_printf(LLAPI_MSG_NORMAL,
3340 "%4slcme_extent.e_end: ", " ");
3341 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3342 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3344 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3345 (unsigned long long)entry->lcme_extent.e_end);
3350 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3351 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3352 } else if (verbose & VERBOSE_DETAIL) {
3353 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3354 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3355 " ", entry->lcme_offset);
3356 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3357 " ", entry->lcme_size);
3358 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3360 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3365 * Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3366 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3368 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3369 * The table below gives the answers for the specified parameters (value and
3370 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3371 * --------------------------------------
3372 * 1 | file > limit; sign > 0 | -1 / -1 |
3373 * 2 | file = limit; sign > 0 | -1 / -1 |
3374 * 3 | file < limit; sign > 0 | ? / 1 |
3375 * 4 | file > limit; sign = 0 | -1 / -1 |
3376 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3377 * 6 | file < limit; sign = 0 | ? / -1 |
3378 * 7 | file > limit; sign < 0 | 1 / 1 |
3379 * 8 | file = limit; sign < 0 | ? / -1 |
3380 * 9 | file < limit; sign < 0 | ? / -1 |
3381 * --------------------------------------
3382 * Note: 5th actually means that the value is within the interval
3383 * (limit - margin, limit].
3385 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3386 int sign, int negopt, unsigned long long margin,
3392 /* Drop the fraction of margin (of days or size). */
3393 if (file + margin <= limit)
3395 } else if (sign == 0) {
3396 if (file <= limit && file + margin > limit)
3398 else if (file + margin <= limit)
3400 } else if (sign < 0) {
3407 return negopt ? ~ret + 1 : ret;
3410 static inline struct lov_user_md *
3411 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3413 return (struct lov_user_md *)((char *)comp_v1 +
3414 comp_v1->lcm_entries[ent_idx].lcme_offset);
3417 static inline struct lov_user_ost_data_v1 *
3418 lov_v1v3_objects(struct lov_user_md *v1)
3420 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3421 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3423 return v1->lmm_objects;
3427 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3429 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3430 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3431 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3433 pool_name[0] = '\0';
3437 print_last_init_comp(struct find_param *param)
3439 /* print all component info */
3440 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3443 /* print specific component info */
3444 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3445 param->fp_check_comp_start || param->fp_check_comp_end ||
3446 param->fp_check_mirror_id || param->fp_check_mirror_index)
3452 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3456 if (param->fp_comp_end == LUSTRE_EOF) {
3457 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3458 match = end == LUSTRE_EOF ? 1 : -1;
3459 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3460 match = end == LUSTRE_EOF ? -1 : 1;
3461 else /* at least EOF */
3463 if (param->fp_exclude_comp_end)
3466 unsigned long long margin;
3468 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3469 match = find_value_cmp(end, param->fp_comp_end,
3470 param->fp_comp_end_sign,
3471 param->fp_exclude_comp_end, margin, 0);
3478 * An example of "getstripe -v" for a two components PFL file:
3481 * lcm_magic: 0x0BD60BD0
3485 * lcm_entry_count: 2
3489 * lcme_extent.e_start: 0
3490 * lcme_extent.e_end: 1048576
3494 * lmm_magic: 0x0BD10BD0
3495 * lmm_seq: 0x200000401
3496 * lmm_object_id: 0x1
3497 * lmm_fid: [0x200000401:0x1:0x0]
3498 * lmm_stripe_count: 1
3499 * lmm_stripe_size: 1048576
3500 * lmm_pattern: raid0
3502 * lmm_stripe_offset: 0
3504 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3508 * lcme_extent.e_start: 1048576
3509 * lcme_extent.e_end: EOF
3513 * lmm_magic: 0x0BD10BD0
3514 * lmm_seq: 0x200000401
3515 * lmm_object_id: 0x1
3516 * lmm_fid: [0x200000401:0x1:0x0]
3517 * lmm_stripe_count: 2
3518 * lmm_stripe_size: 1048576
3519 * lmm_pattern: raid0
3521 * lmm_stripe_offset: 1
3523 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3524 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3526 static void lov_dump_comp_v1(struct find_param *param, char *path,
3527 enum lov_dump_flags flags)
3529 struct lov_comp_md_entry_v1 *entry;
3530 struct lov_user_ost_data_v1 *objects;
3531 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3532 struct lov_user_md_v1 *v1;
3533 char pool_name[LOV_MAXPOOLNAME + 1];
3534 int obdindex = param->fp_obd_index;
3535 int i, j, match, ext;
3536 bool obdstripe = false;
3537 __u16 mirror_index = 0;
3538 __u16 mirror_id = 0;
3540 if (obdindex != OBD_NOT_FOUND) {
3541 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3542 i < comp_v1->lcm_entry_count; i++) {
3543 if (!(comp_v1->lcm_entries[i].lcme_flags &
3547 v1 = lov_comp_entry(comp_v1, i);
3548 objects = lov_v1v3_objects(v1);
3550 for (j = 0; j < v1->lmm_stripe_count; j++) {
3551 if (obdindex == objects[j].l_ost_idx) {
3564 lov_dump_comp_v1_header(param, path, flags);
3566 flags |= LDF_INDENT;
3568 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3569 entry = &comp_v1->lcm_entries[i];
3571 if (param->fp_check_comp_flags) {
3572 if (((param->fp_comp_flags & entry->lcme_flags) !=
3573 param->fp_comp_flags) ||
3574 (param->fp_comp_neg_flags & entry->lcme_flags))
3578 if (param->fp_check_comp_id &&
3579 param->fp_comp_id != entry->lcme_id)
3582 if (param->fp_check_comp_start) {
3583 match = find_value_cmp(entry->lcme_extent.e_start,
3584 param->fp_comp_start,
3585 param->fp_comp_start_sign,
3587 param->fp_comp_start_units, 0);
3592 if (param->fp_check_comp_end) {
3593 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3599 if (param->fp_check_mirror_index) {
3600 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3602 mirror_id = mirror_id_of(entry->lcme_id);
3605 match = find_value_cmp(mirror_index,
3606 param->fp_mirror_index,
3607 param->fp_mirror_index_sign,
3608 param->fp_exclude_mirror_index,
3612 } else if (param->fp_check_mirror_id) {
3613 if (mirror_id != mirror_id_of(entry->lcme_id))
3614 mirror_id = mirror_id_of(entry->lcme_id);
3616 match = find_value_cmp(mirror_id,
3617 param->fp_mirror_id,
3618 param->fp_mirror_id_sign,
3619 param->fp_exclude_mirror_id,
3625 if (print_last_init_comp(param)) {
3627 * if part of stripe info is needed, we'd print only
3628 * the last instantiated component info.
3630 if (entry->lcme_flags & LCME_FL_INIT)
3633 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3634 if (entry->lcme_flags & LCME_FL_EXTENSION)
3635 /* moved back below */
3643 if (entry->lcme_flags & LCME_FL_INIT) {
3644 if (obdindex != OBD_NOT_FOUND) {
3645 flags |= LDF_SKIP_OBJS;
3646 v1 = lov_comp_entry(comp_v1, i);
3647 objects = lov_v1v3_objects(v1);
3649 for (j = 0; j < v1->lmm_stripe_count; j++) {
3650 if (obdindex == objects[j].l_ost_idx) {
3651 flags &= ~LDF_SKIP_OBJS;
3656 flags &= ~LDF_SKIP_OBJS;
3659 flags |= LDF_SKIP_OBJS;
3662 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3664 lov_dump_comp_v1_entry(param, flags, i);
3666 v1 = lov_comp_entry(comp_v1, i);
3667 objects = lov_v1v3_objects(v1);
3668 lov_v1v3_pool_name(v1, pool_name);
3670 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3671 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3672 param->fp_max_depth, param->fp_verbose,
3675 if (print_last_init_comp(param)) {
3677 * directory layout contains only layout template, print the
3681 i = comp_v1->lcm_entry_count - 1;
3684 flags &= ~LDF_SKIP_OBJS;
3686 lov_dump_comp_v1_entry(param, flags, i);
3688 v1 = lov_comp_entry(comp_v1, i);
3689 objects = lov_v1v3_objects(v1);
3690 lov_v1v3_pool_name(v1, pool_name);
3692 entry = &comp_v1->lcm_entries[i];
3693 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3694 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3695 param->fp_max_depth, param->fp_verbose,
3700 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3701 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3704 static inline bool has_any_comp_options(struct find_param *param)
3706 enum llapi_layout_verbose verbose = param->fp_verbose;
3708 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3709 param->fp_check_comp_start || param->fp_check_comp_end ||
3710 param->fp_check_comp_flags)
3713 /* show full layout information, not component specific */
3714 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3717 return verbose & VERBOSE_COMP_OPTS;
3720 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3723 struct lov_user_md *lum = &orig->lmd_lmm;
3724 struct lov_user_mds_data *new;
3725 struct lov_comp_md_v1 *comp_v1;
3726 struct lov_comp_md_entry_v1 *ent;
3727 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3728 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3731 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3733 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3737 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3738 + sizeof(new->lmd_lmmsize));
3740 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3741 comp_v1->lcm_magic = lum->lmm_magic;
3742 comp_v1->lcm_size = lum_off + lum_size;
3743 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3744 comp_v1->lcm_flags = 0;
3745 comp_v1->lcm_entry_count = 1;
3747 ent = &comp_v1->lcm_entries[0];
3749 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3750 ent->lcme_extent.e_start = 0;
3751 ent->lcme_extent.e_end = LUSTRE_EOF;
3752 ent->lcme_offset = lum_off;
3753 ent->lcme_size = lum_size;
3755 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3760 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3761 enum lov_dump_flags flags)
3763 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3765 if (has_any_comp_options(param)) {
3766 struct lov_user_mds_data *new_lmd, *orig_lmd;
3768 orig_lmd = param->fp_lmd;
3769 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3770 if (new_lmd != NULL) {
3771 param->fp_lmd = new_lmd;
3772 lov_dump_comp_v1(param, path, flags);
3773 param->fp_lmd = orig_lmd;
3779 if (magic == LOV_USER_MAGIC_V1) {
3780 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3781 param->fp_lmd->lmd_lmm.lmm_objects,
3782 path, param->fp_obd_index,
3783 param->fp_max_depth, param->fp_verbose,
3786 char pool_name[LOV_MAXPOOLNAME + 1];
3787 struct lov_user_ost_data_v1 *objects;
3788 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3790 snprintf(pool_name, sizeof(pool_name), "%s",
3791 lmmv3->lmm_pool_name);
3792 objects = lmmv3->lmm_objects;
3793 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3794 objects, path, param->fp_obd_index,
3795 param->fp_max_depth, param->fp_verbose,
3800 static uint32_t check_foreign_type(uint32_t foreign_type)
3804 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3805 if (lu_foreign_types[i].lft_name == NULL)
3807 if (foreign_type == lu_foreign_types[i].lft_type)
3811 return LU_FOREIGN_TYPE_UNKNOWN;
3814 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3815 enum lov_dump_flags flags)
3817 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3818 bool yaml = flags & LDF_YAML;
3820 if (!yaml && param->fp_depth && path)
3821 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3823 if (param->fp_verbose & VERBOSE_DETAIL) {
3824 uint32_t type = check_foreign_type(lfm->lfm_type);
3826 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3828 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3830 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3832 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3833 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3834 lu_foreign_types[type].lft_name);
3836 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3838 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3841 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3842 lfm->lfm_length, lfm->lfm_value);
3843 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3846 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3847 enum lov_dump_flags flags)
3849 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3850 bool yaml = flags & LDF_YAML;
3852 if (!yaml && param->fp_depth && path)
3853 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3855 if (param->fp_verbose & VERBOSE_DETAIL) {
3856 uint32_t type = check_foreign_type(lfm->lfm_type);
3858 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3860 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3862 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3864 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3865 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3866 lu_foreign_types[type].lft_name);
3868 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3870 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3873 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3874 lfm->lfm_length, lfm->lfm_value);
3875 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3878 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3879 enum lov_dump_flags flags)
3883 if (param->fp_get_lmv || param->fp_get_default_lmv)
3884 magic = (__u32)param->fp_lmv_md->lum_magic;
3886 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3889 flags |= LDF_IS_RAW;
3894 case LOV_USER_MAGIC_V1:
3895 case LOV_USER_MAGIC_V3:
3896 case LOV_USER_MAGIC_SPECIFIC:
3897 lov_dump_plain_user_lmm(param, path, flags);
3899 case LOV_USER_MAGIC_FOREIGN:
3900 lov_dump_foreign_lmm(param, path, flags);
3903 case LMV_USER_MAGIC: {
3904 char pool_name[LOV_MAXPOOLNAME + 1];
3905 struct lmv_user_md *lum;
3907 lum = (struct lmv_user_md *)param->fp_lmv_md;
3908 snprintf(pool_name, sizeof(pool_name), "%s",
3909 lum->lum_pool_name);
3910 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3911 param->fp_max_depth, param->fp_verbose,
3915 case LOV_USER_MAGIC_COMP_V1:
3916 lov_dump_comp_v1(param, path, flags);
3918 case LMV_MAGIC_FOREIGN:
3919 lmv_dump_foreign_lmm(param, path, flags);
3922 llapi_printf(LLAPI_MSG_NORMAL,
3923 "unknown lmm_magic: %#x (expecting one of %#x %#x %#x %#x)\n",
3924 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3925 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3926 LMV_USER_MAGIC, LMV_MAGIC_V1);
3931 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3937 fname = strrchr(path, '/');
3939 /* It should be a file (or other non-directory) */
3940 if (fname == NULL) {
3941 dname = (char *)malloc(2);
3945 fname = (char *)path;
3947 dname = (char *)malloc(fname - path + 1);
3950 strncpy(dname, path, fname - path);
3951 dname[fname - path] = '\0';
3955 fd = open(dname, O_RDONLY | O_NONBLOCK);
3961 strcpy((char *)lum, fname);
3962 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3965 if (close(fd) == -1 && rc == 0)
3973 int llapi_file_lookup(int dirfd, const char *name)
3975 struct obd_ioctl_data data = { 0 };
3980 if (dirfd < 0 || name == NULL)
3983 data.ioc_version = OBD_IOCTL_VERSION;
3984 data.ioc_len = sizeof(data);
3985 data.ioc_inlbuf1 = (char *)name;
3986 data.ioc_inllen1 = strlen(name) + 1;
3988 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3990 llapi_error(LLAPI_MSG_ERROR, rc,
3991 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3996 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
4003 * Check if the file time matches all the given criteria (e.g. --atime +/-N).
4004 * Return -1 or 1 if file timestamp does not or does match the given criteria
4005 * correspondingly. Return 0 if the MDS time is being checked and there are
4006 * attributes on OSTs and it is not yet clear if the timespamp matches.
4008 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
4009 * updated timestamps.
4011 static int find_time_check(struct find_param *param, int mds)
4013 struct lov_user_mds_data *lmd = param->fp_lmd;
4017 /* Check if file is accepted. */
4018 if (param->fp_atime) {
4019 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4020 param->fp_atime, param->fp_asign,
4021 param->fp_exclude_atime,
4022 param->fp_time_margin, mds);
4028 if (param->fp_mtime) {
4029 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4030 param->fp_mtime, param->fp_msign,
4031 param->fp_exclude_mtime,
4032 param->fp_time_margin, mds);
4037 * If the previous check matches, but this one is not yet clear,
4038 * we should return 0 to do an RPC on OSTs.
4044 if (param->fp_ctime) {
4045 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4046 param->fp_ctime, param->fp_csign,
4047 param->fp_exclude_ctime,
4048 param->fp_time_margin, mds);
4053 * If the previous check matches, but this one is not yet clear,
4054 * we should return 0 to do an RPC on OSTs.
4063 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4065 struct lov_user_mds_data *lmd = param->fp_lmd;
4070 for (i = 0; i < 2; i++) {
4071 /* Check if file is accepted. */
4072 if (param->fp_newery[NEWERXY_ATIME][i]) {
4073 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4074 param->fp_newery[NEWERXY_ATIME][i],
4081 if (param->fp_newery[NEWERXY_MTIME][i]) {
4082 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4083 param->fp_newery[NEWERXY_MTIME][i],
4089 * If the previous check matches, but this one is not
4090 * yet clear, we should return 0 to do an RPC on OSTs.
4096 if (param->fp_newery[NEWERXY_CTIME][i]) {
4097 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4098 param->fp_newery[NEWERXY_CTIME][i],
4104 * If the previous check matches, but this one is not
4105 * yet clear, we should return 0 to do an RPC on OSTs.
4112 * File birth time (btime) can get from MDT directly.
4113 * if @from_mdt is true, it means the input file attributs are
4114 * obtained directly from MDT.
4115 * Thus, if @from_mdt is false, we should skip the following
4121 if (param->fp_newery[NEWERXY_BTIME][i]) {
4122 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4125 rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4126 param->fp_newery[NEWERXY_BTIME][i],
4137 * Check whether the stripes matches the indexes user provided
4141 static int check_obd_match(struct find_param *param)
4143 struct lov_user_ost_data_v1 *objects;
4144 struct lov_comp_md_v1 *comp_v1 = NULL;
4145 struct lov_user_mds_data *lmd = param->fp_lmd;
4146 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4147 int i, j, k, count = 1;
4149 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4152 if (!S_ISREG(lmd->lmd_stx.stx_mode))
4155 /* exclude foreign */
4156 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4157 return param->fp_exclude_obd;
4160 * Only those files should be accepted, which have a
4161 * stripe on the specified OST.
4163 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4164 comp_v1 = (struct lov_comp_md_v1 *)v1;
4165 count = comp_v1->lcm_entry_count;
4168 for (i = 0; i < count; i++) {
4170 v1 = lov_comp_entry(comp_v1, i);
4172 objects = lov_v1v3_objects(v1);
4174 for (j = 0; j < v1->lmm_stripe_count; j++) {
4175 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4178 for (k = 0; k < param->fp_num_obds; k++) {
4179 if (param->fp_obd_indexes[k] ==
4180 objects[j].l_ost_idx)
4181 return !param->fp_exclude_obd;
4186 return param->fp_exclude_obd;
4189 static int check_mdt_match(struct find_param *param)
4193 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4196 /* FIXME: For striped dir, we should get stripe information and check */
4197 for (i = 0; i < param->fp_num_mdts; i++) {
4198 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4199 return !param->fp_exclude_mdt;
4202 if (param->fp_exclude_mdt)
4209 * Check whether the obd is active or not, if it is
4210 * not active, just print the object affected by this
4213 static int print_failed_tgt(struct find_param *param, char *path, int type)
4215 struct obd_statfs stat_buf;
4216 struct obd_uuid uuid_buf;
4219 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV)
4222 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4223 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4224 ret = llapi_obd_statfs(path, type,
4225 type == LL_STATFS_LOV ? param->fp_obd_index :
4226 param->fp_mdt_index, &stat_buf,
4229 llapi_error(LLAPI_MSG_NORMAL, ret, "obd_uuid: %s failed",
4230 param->fp_obd_uuid->uuid);
4235 static int find_check_stripe_size(struct find_param *param)
4237 struct lov_comp_md_v1 *comp_v1 = NULL;
4238 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4239 __u32 stripe_size = 0;
4240 int ret, i, count = 1;
4242 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4243 return param->fp_exclude_stripe_size ? 1 : -1;
4245 ret = param->fp_exclude_stripe_size ? 1 : -1;
4246 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4247 comp_v1 = (struct lov_comp_md_v1 *)v1;
4248 count = comp_v1->lcm_entry_count;
4251 for (i = 0; i < count; i++) {
4252 struct lov_comp_md_entry_v1 *ent;
4255 v1 = lov_comp_entry(comp_v1, i);
4257 ent = &comp_v1->lcm_entries[i];
4258 if (ent->lcme_flags & LCME_FL_EXTENSION)
4260 if (!(ent->lcme_flags & LCME_FL_INIT))
4263 stripe_size = v1->lmm_stripe_size;
4266 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4267 param->fp_stripe_size_sign,
4268 param->fp_exclude_stripe_size,
4269 param->fp_stripe_size_units, 0);
4274 static int find_check_ext_size(struct find_param *param)
4276 struct lov_comp_md_v1 *comp_v1;
4277 struct lov_user_md_v1 *v1;
4280 ret = param->fp_exclude_ext_size ? 1 : -1;
4281 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4282 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4285 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4286 struct lov_comp_md_entry_v1 *ent;
4288 v1 = lov_comp_entry(comp_v1, i);
4290 ent = &comp_v1->lcm_entries[i];
4291 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4294 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4295 param->fp_ext_size_sign,
4296 param->fp_exclude_ext_size,
4297 param->fp_ext_size_units, 0);
4298 /* If any ext_size matches */
4306 static __u32 find_get_stripe_count(struct find_param *param)
4308 struct lov_comp_md_v1 *comp_v1 = NULL;
4309 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4311 __u32 stripe_count = 0;
4313 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4316 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4317 comp_v1 = (struct lov_comp_md_v1 *)v1;
4318 count = comp_v1->lcm_entry_count;
4321 for (i = 0; i < count; i++) {
4323 struct lov_comp_md_entry_v1 *ent;
4325 v1 = lov_comp_entry(comp_v1, i);
4327 ent = &comp_v1->lcm_entries[i];
4328 if (!(ent->lcme_flags & LCME_FL_INIT))
4331 if (ent->lcme_flags & LCME_FL_EXTENSION)
4334 stripe_count = v1->lmm_stripe_count;
4337 return stripe_count;
4340 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4342 static int find_check_layout(struct find_param *param)
4344 struct lov_comp_md_v1 *comp_v1 = NULL;
4345 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4347 bool found = false, valid = false;
4349 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4350 comp_v1 = (struct lov_comp_md_v1 *)v1;
4351 count = comp_v1->lcm_entry_count;
4354 for (i = 0; i < count; i++) {
4356 v1 = lov_comp_entry(comp_v1, i);
4358 /* foreign file have a special magic but no pattern field */
4359 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4362 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4366 if (v1->lmm_pattern & param->fp_layout) {
4375 if ((found && !param->fp_exclude_layout) ||
4376 (!found && param->fp_exclude_layout))
4383 * if no type specified, check/exclude all foreign
4384 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4386 static int find_check_foreign(struct find_param *param)
4388 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4389 struct lov_foreign_md *lfm;
4391 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4392 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4393 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4394 return param->fp_exclude_foreign ? 1 : -1;
4398 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4399 lfm->lfm_type == param->fp_foreign_type)
4400 return param->fp_exclude_foreign ? -1 : 1;
4401 return param->fp_exclude_foreign ? 1 : -1;
4404 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4405 struct lmv_foreign_md *lfm;
4407 lfm = (void *)param->fp_lmv_md;
4408 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
4409 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4410 return param->fp_exclude_foreign ? 1 : -1;
4414 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4415 lfm->lfm_type == param->fp_foreign_type)
4416 return param->fp_exclude_foreign ? -1 : 1;
4417 return param->fp_exclude_foreign ? 1 : -1;
4422 static int find_check_pool(struct find_param *param)
4424 struct lov_comp_md_v1 *comp_v1 = NULL;
4425 struct lov_user_md_v3 *v3 = (void *)¶m->fp_lmd->lmd_lmm;
4429 if (v3->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4430 comp_v1 = (struct lov_comp_md_v1 *)v3;
4431 count = comp_v1->lcm_entry_count;
4432 /* empty requested pool is taken as no pool search */
4433 if (count == 0 && param->fp_poolname[0] == '\0') {
4439 for (i = 0; i < count; i++) {
4440 if (comp_v1 != NULL) {
4441 if (!(comp_v1->lcm_entries[i].lcme_flags &
4445 v3 = (void *)lov_comp_entry(comp_v1, i);
4448 if (v3->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4451 if (((v3->lmm_magic == LOV_USER_MAGIC_V1) &&
4452 (param->fp_poolname[0] == '\0')) ||
4453 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4454 (strncmp(v3->lmm_pool_name,
4455 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4456 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4457 (strcmp(param->fp_poolname, "*") == 0))) {
4464 if ((found && !param->fp_exclude_pool) ||
4465 (!found && param->fp_exclude_pool))
4471 static int find_check_comp_options(struct find_param *param)
4473 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4474 struct lov_user_mds_data *lmd = param->fp_lmd;
4475 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4476 struct lov_comp_md_entry_v1 *entry;
4479 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4482 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4483 comp_v1 = (struct lov_comp_md_v1 *)v1;
4485 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4486 if (forged_v1 == NULL)
4488 comp_v1 = forged_v1;
4489 comp_v1->lcm_entry_count = 1;
4490 entry = &comp_v1->lcm_entries[0];
4491 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4493 entry->lcme_extent.e_start = 0;
4494 entry->lcme_extent.e_end = LUSTRE_EOF;
4497 /* invalid case, don't match for any kind of search. */
4498 if (comp_v1->lcm_entry_count == 0) {
4503 if (param->fp_check_comp_count) {
4504 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4505 param->fp_comp_count,
4506 param->fp_comp_count_sign,
4507 param->fp_exclude_comp_count, 1, 0);
4513 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4514 entry = &comp_v1->lcm_entries[i];
4516 if (param->fp_check_comp_flags) {
4518 if (((param->fp_comp_flags & entry->lcme_flags) !=
4519 param->fp_comp_flags) ||
4520 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4526 if (param->fp_check_comp_start) {
4527 ret = find_value_cmp(entry->lcme_extent.e_start,
4528 param->fp_comp_start,
4529 param->fp_comp_start_sign,
4530 param->fp_exclude_comp_start,
4531 param->fp_comp_start_units, 0);
4536 if (param->fp_check_comp_end) {
4537 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4543 /* the component matches all criteria */
4552 static int find_check_mirror_options(struct find_param *param)
4554 struct lov_comp_md_v1 *comp_v1;
4555 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4558 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4561 comp_v1 = (struct lov_comp_md_v1 *)v1;
4563 if (param->fp_check_mirror_count) {
4564 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4565 param->fp_mirror_count,
4566 param->fp_mirror_count_sign,
4567 param->fp_exclude_mirror_count, 1, 0);
4572 if (param->fp_check_mirror_state) {
4574 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4576 if ((param->fp_mirror_state != 0 &&
4577 file_state != param->fp_mirror_state) ||
4578 file_state == param->fp_mirror_neg_state)
4585 static bool find_check_lmm_info(struct find_param *param)
4587 return param->fp_check_pool || param->fp_check_stripe_count ||
4588 param->fp_check_stripe_size || param->fp_check_layout ||
4589 param->fp_check_comp_count || param->fp_check_comp_end ||
4590 param->fp_check_comp_start || param->fp_check_comp_flags ||
4591 param->fp_check_mirror_count || param->fp_check_foreign ||
4592 param->fp_check_mirror_state || param->fp_check_ext_size ||
4593 param->fp_check_projid;
4597 * Get file/directory project id.
4598 * by the open fd resides on.
4599 * Return 0 and project id on success, or -ve errno.
4601 static int fget_projid(int fd, int *projid)
4606 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
4610 *projid = fsx.fsx_projid;
4614 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
4615 void *data, struct dirent64 *de)
4617 struct find_param *param = (struct find_param *)data;
4618 struct lov_user_mds_data *lmd = param->fp_lmd;
4619 DIR *dir = dirp == NULL ? NULL : *dirp;
4620 int decision = 1; /* 1 is accepted; -1 is rejected. */
4622 int checked_type = 0;
4624 __u32 stripe_count = 0;
4628 if (parent == NULL && dir == NULL)
4631 /* If a regular expression is presented, make the initial decision */
4632 if (param->fp_pattern != NULL) {
4633 char *fname = strrchr(path, '/');
4635 fname = (fname == NULL ? path : fname + 1);
4636 ret = fnmatch(param->fp_pattern, fname, 0);
4637 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4638 (ret == 0 && param->fp_exclude_pattern))
4642 /* See if we can check the file type from the dirent. */
4643 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
4646 if (DTTOIF(de->d_type) == param->fp_type) {
4647 if (param->fp_exclude_type)
4650 if (!param->fp_exclude_type)
4658 * Request MDS for the stat info if some of these parameters need
4661 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4662 param->fp_check_uid || param->fp_check_gid ||
4663 param->fp_newerxy || param->fp_btime ||
4664 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4665 param->fp_check_size || param->fp_check_blocks ||
4666 find_check_lmm_info(param) ||
4667 param->fp_check_mdt_count || param->fp_check_hash_type)
4670 if (param->fp_type != 0 && checked_type == 0)
4673 if (decision == 0) {
4674 if (dir && (param->fp_check_mdt_count ||
4675 param->fp_check_hash_type || param->fp_check_foreign)) {
4676 param->fp_get_lmv = 1;
4677 ret = cb_get_dirstripe(path, dir, param);
4680 * XXX this works to decide for foreign
4683 if (errno == ENODATA &&
4684 param->fp_check_foreign) {
4685 if (param->fp_exclude_foreign)
4693 param->fp_lmd->lmd_lmm.lmm_magic = 0;
4694 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
4695 param->fp_lum_size, GET_LMD_INFO);
4696 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
4697 find_check_lmm_info(param)) {
4698 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
4701 * We need to "fake" the "use the default" values
4702 * since the lmm struct is zeroed out at this point.
4704 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4705 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
4707 ostid_set_seq(&lmm->lmm_oi,
4708 FID_SEQ_LOV_DEFAULT);
4709 lmm->lmm_stripe_size = 0;
4710 lmm->lmm_stripe_count = 0;
4711 lmm->lmm_stripe_offset = -1;
4713 if (ret == 0 && param->fp_mdt_uuid != NULL) {
4715 ret = llapi_file_fget_mdtidx(dirfd(dir),
4716 ¶m->fp_file_mdt_index);
4717 } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
4719 * FIXME: we could get the MDT index from the
4720 * file's FID in lmd->lmd_lmm.lmm_oi without
4721 * opening the file, once we are sure that
4722 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
4723 * That would still be an ioctl() to map the
4724 * FID to the MDT, but not an open RPC.
4726 fd = open(path, O_RDONLY);
4728 ret = llapi_file_fget_mdtidx(fd,
4729 ¶m->fp_file_mdt_index);
4735 * For a special file, we assume it resides on
4736 * the same MDT as the parent directory.
4738 ret = llapi_file_fget_mdtidx(dirfd(parent),
4739 ¶m->fp_file_mdt_index);
4750 stripe_count = find_get_stripe_count(param);
4754 if (param->fp_type && !checked_type) {
4755 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
4756 if (param->fp_exclude_type)
4759 if (!param->fp_exclude_type)
4765 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
4766 if (lustre_fs && param->fp_got_uuids &&
4767 param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
4768 lmd->lmd_stx.stx_dev_minor)) {
4769 /* A lustre/lustre mount point is crossed. */
4770 param->fp_got_uuids = 0;
4771 param->fp_obds_printed = 0;
4772 param->fp_mdt_index = OBD_NOT_FOUND;
4773 param->fp_obd_index = OBD_NOT_FOUND;
4776 if (lustre_fs && !param->fp_got_uuids) {
4777 ret = setup_target_indexes(dir ? dir : parent, path,
4782 param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
4783 lmd->lmd_stx.stx_dev_minor);
4784 } else if (!lustre_fs && param->fp_got_uuids) {
4785 /* A lustre/non-lustre mount point is crossed. */
4786 param->fp_got_uuids = 0;
4787 param->fp_mdt_index = OBD_NOT_FOUND;
4788 param->fp_obd_index = OBD_NOT_FOUND;
4792 if (param->fp_check_foreign) {
4793 decision = find_check_foreign(param);
4798 if (param->fp_check_stripe_size) {
4799 decision = find_check_stripe_size(param);
4804 if (param->fp_check_ext_size) {
4805 decision = find_check_ext_size(param);
4810 if (param->fp_check_stripe_count) {
4811 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
4812 param->fp_stripe_count_sign,
4813 param->fp_exclude_stripe_count, 1, 0);
4818 if (param->fp_check_mdt_count) {
4819 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4824 decision = find_value_cmp(
4825 param->fp_lmv_md->lum_stripe_count,
4826 param->fp_mdt_count,
4827 param->fp_mdt_count_sign,
4828 param->fp_exclude_mdt_count, 1, 0);
4833 if (param->fp_check_layout) {
4834 decision = find_check_layout(param);
4839 if (param->fp_check_hash_type) {
4842 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4847 found = param->fp_lmv_md->lum_hash_type & param->fp_hash_type;
4848 if ((found && param->fp_exclude_hash_type) ||
4849 (!found && !param->fp_exclude_hash_type)) {
4855 /* If an OBD UUID is specified but none matches, skip this file. */
4856 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
4857 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
4861 * If an OST or MDT UUID is given, and some OST matches,
4864 if (param->fp_obd_index != OBD_NOT_FOUND ||
4865 param->fp_mdt_index != OBD_NOT_FOUND) {
4866 if (param->fp_obd_uuid) {
4867 if (check_obd_match(param)) {
4869 * If no mdtuuid is given, we are done.
4870 * Otherwise, fall through to the mdtuuid
4873 if (!param->fp_mdt_uuid)
4880 if (param->fp_mdt_uuid) {
4881 if (check_mdt_match(param))
4888 if (param->fp_check_uid) {
4889 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
4890 if (param->fp_exclude_uid)
4893 if (!param->fp_exclude_uid)
4898 if (param->fp_check_gid) {
4899 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
4900 if (param->fp_exclude_gid)
4903 if (!param->fp_exclude_gid)
4908 if (param->fp_check_projid) {
4912 fd = open(path, O_RDONLY);
4915 ret = fget_projid(fd, &projid);
4920 if (projid == param->fp_projid) {
4921 if (param->fp_exclude_projid)
4924 if (!param->fp_exclude_projid)
4929 if (param->fp_check_pool) {
4930 decision = find_check_pool(param);
4935 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
4936 param->fp_check_comp_start || param->fp_check_comp_end) {
4937 decision = find_check_comp_options(param);
4942 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
4943 decision = find_check_mirror_options(param);
4948 /* Check the time on mds. */
4950 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
4953 for_mds = lustre_fs ?
4954 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
4955 decision = find_time_check(param, for_mds);
4960 if (param->fp_btime) {
4961 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
4966 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4967 param->fp_btime, param->fp_bsign,
4968 param->fp_exclude_btime,
4969 param->fp_time_margin, 0);
4974 if (param->fp_newerxy) {
4977 for_mds = lustre_fs ?
4978 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
4979 decision = find_newerxy_check(param, for_mds, true);
4988 flags = param->fp_lmd->lmd_flags;
4989 if (param->fp_check_size &&
4990 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
4991 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
4992 !(flags & OBD_MD_FLSIZE ||
4993 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
4996 if (param->fp_check_blocks &&
4997 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
4998 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
4999 !(flags & OBD_MD_FLBLOCKS ||
5000 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
5004 * If file still fits the request, ask ost for updated info.
5005 * The regular stat is almost of the same speed as some new
5006 * 'glimpse-size-ioctl'.
5012 * For regular files with the stripe the decision may have not
5013 * been taken yet if *time or size is to be checked.
5015 if (param->fp_obd_index != OBD_NOT_FOUND)
5016 print_failed_tgt(param, path, LL_STATFS_LOV);
5018 if (param->fp_mdt_index != OBD_NOT_FOUND)
5019 print_failed_tgt(param, path, LL_STATFS_LMV);
5022 ret = fstat_f(dirfd(dir), &st);
5023 else if (de != NULL)
5024 ret = fstatat_f(dirfd(parent), de->d_name, &st,
5025 AT_SYMLINK_NOFOLLOW);
5027 ret = lstat_f(path, &st);
5030 if (errno == ENOENT) {
5031 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
5032 "warning: %s: %s does not exist",
5037 llapi_error(LLAPI_MSG_ERROR, ret,
5038 "%s: stat on %s failed",
5044 convert_lmd_statx(param->fp_lmd, &st, true);
5045 /* Check the time on osc. */
5046 decision = find_time_check(param, 0);
5050 if (param->fp_newerxy) {
5051 decision = find_newerxy_check(param, 0, false);
5061 if (param->fp_check_size) {
5062 decision = find_value_cmp(lmd->lmd_stx.stx_size,
5064 param->fp_size_sign,
5065 param->fp_exclude_size,
5066 param->fp_size_units, 0);
5071 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
5072 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
5074 param->fp_blocks_sign,
5075 param->fp_exclude_blocks,
5076 param->fp_blocks_units, 0);
5082 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
5083 if (param->fp_zero_end)
5084 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
5086 llapi_printf(LLAPI_MSG_NORMAL, "\n");
5090 /* Do not get down anymore? */
5091 if (param->fp_depth == param->fp_max_depth) {
5102 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
5103 void *param_data, struct dirent64 *de)
5105 struct find_param *param = (struct find_param *)param_data;
5106 struct lmv_user_md *lmu = param->fp_lmv_md;
5107 DIR *tmp_parent = parent;
5108 char raw[MAX_IOC_BUFLEN] = {'\0'};
5110 struct obd_ioctl_data data = { 0 };
5117 if (parent == NULL && dirp == NULL)
5126 if (parent == NULL) {
5127 tmp_parent = opendir_parent(path);
5128 if (tmp_parent == NULL) {
5131 llapi_error(LLAPI_MSG_ERROR, ret,
5132 "can not open %s", path);
5137 fd = dirfd(tmp_parent);
5139 path_copy = strdup(path);
5140 filename = basename(path_copy);
5142 data.ioc_inlbuf1 = (char *)filename;
5143 data.ioc_inllen1 = strlen(filename) + 1;
5144 data.ioc_inlbuf2 = (char *)lmu;
5145 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
5147 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5149 llapi_error(LLAPI_MSG_ERROR, ret,
5150 "llapi_obd_statfs: error packing ioctl data");
5155 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
5157 if (errno == EBUSY && !retry) {
5159 * because migrate may not be able to lock all involved
5160 * objects in order, for some of them it try lock, while
5161 * there may be conflicting COS locks and cause migrate
5162 * fail with EBUSY, hope a sync() could cause
5163 * transaction commit and release these COS locks.
5168 } else if (errno == EALREADY) {
5169 if (param->fp_verbose & VERBOSE_DETAIL)
5170 llapi_printf(LLAPI_MSG_NORMAL,
5171 "%s migrated to MDT%d already\n",
5172 path, lmu->lum_stripe_offset);
5176 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
5180 } else if (param->fp_verbose & VERBOSE_DETAIL) {
5181 llapi_printf(LLAPI_MSG_NORMAL,
5182 "migrate %s to MDT%d stripe count %d\n",
5183 path, lmu->lum_stripe_offset,
5184 lmu->lum_stripe_count);
5190 * If the directory is being migration, we need
5191 * close the directory after migration,
5192 * so the old directory cache will be cleanup
5193 * on the client side, and re-open to get the
5194 * new directory handle
5196 *dirp = opendir(path);
5197 if (*dirp == NULL) {
5199 llapi_error(LLAPI_MSG_ERROR, ret,
5200 "%s: Failed to open '%s'", __func__, path);
5205 closedir(tmp_parent);
5212 /* dir migration finished, shrink its stripes */
5213 static int cb_migrate_mdt_fini(char *path, DIR *parent, DIR **dirp, void *data,
5214 struct dirent64 *de)
5216 struct find_param *param = data;
5217 struct lmv_user_md *lmu = param->fp_lmv_md;
5218 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
5221 if (de && de->d_type != DT_DIR)
5226 * close it before setxattr because the latter may destroy the
5227 * original object, and cause close fail.
5229 ret = closedir(*dirp);
5235 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
5236 if (ret == -EALREADY)
5239 cb_common_fini(path, parent, dirp, data, de);
5243 int llapi_migrate_mdt(char *path, struct find_param *param)
5245 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
5249 int llapi_mv(char *path, struct find_param *param)
5251 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
5252 static bool printed;
5255 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
5256 "%s() is deprecated, use llapi_migrate_mdt() instead",
5261 return llapi_migrate_mdt(path, param);
5264 int llapi_find(char *path, struct find_param *param)
5266 return param_callback(path, cb_find_init, cb_common_fini, param);
5270 * Get MDT number that the file/directory inode referenced
5271 * by the open fd resides on.
5272 * Return 0 and mdtidx on success, or -ve errno.
5274 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
5276 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
5281 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
5282 struct dirent64 *de)
5284 struct find_param *param = (struct find_param *)data;
5285 DIR *d = dirp == NULL ? NULL : *dirp;
5289 if (parent == NULL && d == NULL)
5293 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5294 } else /* if (parent) */ {
5297 fd = open(path, O_RDONLY | O_NOCTTY);
5299 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5307 if (ret == -ENODATA) {
5308 if (!param->fp_obd_uuid)
5309 llapi_printf(LLAPI_MSG_NORMAL,
5310 "'%s' has no stripe info\n", path);
5312 } else if (ret == -ENOENT) {
5313 llapi_error(LLAPI_MSG_WARN, ret,
5314 "warning: %s: '%s' does not exist",
5317 } else if (ret == -ENOTTY) {
5318 llapi_error(LLAPI_MSG_ERROR, ret,
5319 "%s: '%s' not on a Lustre fs",
5322 llapi_error(LLAPI_MSG_ERROR, ret,
5323 "error: %s: '%s' failed get_mdtidx",
5329 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5330 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5332 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5336 /* Do not go down anymore? */
5337 if (param->fp_depth == param->fp_max_depth)
5345 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
5346 struct dirent64 *de)
5348 struct find_param *param = (struct find_param *)data;
5349 DIR *d = dirp == NULL ? NULL : *dirp;
5352 if (parent == NULL && d == NULL)
5355 if (param->fp_obd_uuid) {
5356 param->fp_quiet = 1;
5357 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
5362 if (d && (param->fp_get_lmv || param->fp_get_default_lmv))
5363 ret = cb_get_dirstripe(path, d, param);
5365 (parent && !param->fp_get_lmv && !param->fp_get_default_lmv))
5366 ret = get_lmd_info(path, parent, d, ¶m->fp_lmd->lmd_lmm,
5367 param->fp_lum_size, GET_LMD_STRIPE);
5372 if (errno == ENODATA && d != NULL) {
5374 * We need to "fake" the "use the default" values
5375 * since the lmm struct is zeroed out at this point.
5376 * The magic needs to be set in order to satisfy
5377 * a check later on in the code path.
5378 * The object_seq needs to be set for the "(Default)"
5379 * prefix to be displayed.
5381 if (param->fp_get_default_lmv) {
5382 struct lmv_user_md *lum = param->fp_lmv_md;
5384 lum->lum_magic = LMV_USER_MAGIC;
5385 lum->lum_stripe_count = 0;
5386 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
5388 } else if (param->fp_get_lmv) {
5389 struct lmv_user_md *lum = param->fp_lmv_md;
5392 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5395 lum->lum_magic = LMV_MAGIC_V1;
5396 lum->lum_stripe_count = 0;
5397 lum->lum_stripe_offset = mdtidx;
5400 struct lov_user_md *lmm =
5401 ¶m->fp_lmd->lmd_lmm;
5403 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5405 ostid_set_seq(&lmm->lmm_oi,
5406 FID_SEQ_LOV_DEFAULT);
5407 lmm->lmm_stripe_count = 0;
5408 lmm->lmm_stripe_size = 0;
5409 lmm->lmm_stripe_offset = -1;
5412 } else if (errno == ENODATA && parent != NULL) {
5413 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5414 llapi_printf(LLAPI_MSG_NORMAL,
5415 "%s has no stripe info\n", path);
5417 } else if (errno == ENOENT) {
5418 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5419 "warning: %s: %s does not exist",
5422 } else if (errno == ENOTTY) {
5424 llapi_error(LLAPI_MSG_ERROR, ret,
5425 "%s: '%s' not on a Lustre fs?",
5430 llapi_error(LLAPI_MSG_ERROR, ret,
5431 "error: %s: %s failed for %s",
5432 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
5433 "IOC_MDC_GETFILESTRIPE", path);
5440 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5441 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
5444 /* Do not get down anymore? */
5445 if (param->fp_depth == param->fp_max_depth)
5453 int llapi_getstripe(char *path, struct find_param *param)
5455 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5456 cb_get_mdt_index : cb_getstripe,
5457 cb_common_fini, param);
5460 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5461 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5463 char raw[MAX_IOC_BUFLEN] = {'\0'};
5465 struct obd_ioctl_data data = { 0 };
5468 data.ioc_inlbuf1 = (char *)&type;
5469 data.ioc_inllen1 = sizeof(__u32);
5470 data.ioc_inlbuf2 = (char *)&index;
5471 data.ioc_inllen2 = sizeof(__u32);
5472 data.ioc_pbuf1 = (char *)stat_buf;
5473 data.ioc_plen1 = sizeof(struct obd_statfs);
5474 data.ioc_pbuf2 = (char *)uuid_buf;
5475 data.ioc_plen2 = sizeof(struct obd_uuid);
5477 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5479 llapi_error(LLAPI_MSG_ERROR, rc,
5480 "llapi_obd_statfs: error packing ioctl data");
5484 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5486 return rc < 0 ? -errno : 0;
5489 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5490 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5495 fd = open(path, O_RDONLY);
5498 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5501 * If we can't even open a file on the filesystem (e.g. with
5502 * -ESHUTDOWN), force caller to exit or it will loop forever.
5507 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5514 #define MAX_STRING_SIZE 128
5516 int llapi_ping(char *obd_type, char *obd_name)
5518 int flags = O_RDONLY;
5519 char buf[1] = { 0 };
5523 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5524 obd_type, obd_name);
5528 fd = open(path.gl_pathv[0], flags);
5530 if (errno == EACCES && flags == O_RDONLY) {
5535 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5540 if (flags == O_RDONLY)
5541 rc = read(fd, buf, sizeof(buf));
5543 rc = write(fd, buf, sizeof(buf));
5551 cfs_free_param_data(&path);
5555 int llapi_target_iterate(int type_num, char **obd_type,
5556 void *args, llapi_cb_t cb)
5562 for (i = 0; i < type_num; i++) {
5565 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5569 for (j = 0; j < param.gl_pathc; j++) {
5570 char obd_uuid[UUID_MAX + 1];
5574 fp = fopen(param.gl_pathv[j], "r");
5577 llapi_error(LLAPI_MSG_ERROR, rc,
5578 "error: opening '%s'",
5583 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5585 llapi_error(LLAPI_MSG_ERROR, rc,
5586 "error: reading '%s'",
5591 /* Extract the obd_name from the sysfs path.
5592 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5594 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5600 /* skip /fs/lustre/'obd_type'/ */
5601 obd_name += strlen(obd_type[i]) + 12;
5602 /* chop off after obd_name */
5603 ptr = strrchr(obd_name, '/');
5607 cb(obd_type[i], obd_name, obd_uuid, args);
5616 cfs_free_param_data(¶m);
5620 static void do_target_check(char *obd_type_name, char *obd_name,
5621 char *obd_uuid, void *args)
5625 rc = llapi_ping(obd_type_name, obd_name);
5627 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5629 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5631 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5634 int llapi_target_check(int type_num, char **obd_type, char *dir)
5636 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5639 #undef MAX_STRING_SIZE
5641 /* Is this a lustre fs? */
5642 int llapi_is_lustre_mnttype(const char *type)
5644 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
5647 /* Is this a lustre client fs? */
5648 int llapi_is_lustre_mnt(struct mntent *mnt)
5650 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5651 strstr(mnt->mnt_fsname, ":/") != NULL);
5654 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5656 char fsname[PATH_MAX + 1];
5660 rc = llapi_search_fsname(mnt, fsname);
5664 root = open(mnt, O_RDONLY | O_DIRECTORY);
5667 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
5671 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
5679 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
5684 root = open(mnt, O_RDONLY | O_DIRECTORY);
5687 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
5691 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
5694 llapi_error(LLAPI_MSG_ERROR, rc,
5695 "ioctl on %s for getting connect flags failed", mnt);
5702 * Get a 64-bit value representing the version of file data pointed by fd.
5704 * Each write or truncate, flushed on OST, will change this value. You can use
5705 * this value to verify if file data was modified. This only checks the file
5706 * data, not metadata.
5708 * \param flags 0: no flush pages, usually used it the process has already
5710 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
5712 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
5713 * pages from clients.
5715 * \retval 0 on success.
5716 * \retval -errno on error.
5718 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
5721 struct ioc_data_version idv;
5723 idv.idv_flags = (__u32)flags;
5725 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5729 *data_version = idv.idv_version;
5735 * Flush cached pages from all clients.
5737 * \param fd File descriptor
5741 int llapi_file_flush(int fd)
5745 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);
5749 * Fetch layout version from OST objects. Layout version on OST objects are
5750 * only set when the file is a mirrored file AND after the file has been
5751 * written at least once.
5753 * It actually fetches the least layout version from the objects.
5755 int llapi_get_ost_layout_version(int fd, __u32 *layout_version)
5758 struct ioc_data_version idv = { 0 };
5760 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5764 *layout_version = idv.idv_layout_version;
5770 * Create a file without any name and open it for read/write
5772 * - file is created as if it were a standard file in the given \a directory
5773 * - file does not appear in \a directory and mtime does not change because
5774 * the filename is handled specially by the Lustre MDS.
5775 * - file is destroyed at final close
5777 * \param[in] directory directory from which to inherit layout/MDT idx
5778 * \param[in] mdt_idx MDT index on which the file is created,
5779 * \a idx == -1 means no specific MDT is requested
5780 * \param[in] mode standard open(2) mode
5781 * \param[in] stripe_param stripe parameters. May be NULL.
5783 * \retval a file descriptor on success.
5784 * \retval -errno on error.
5786 int llapi_create_volatile_param(const char *directory, int mdt_idx,
5787 int open_flags, mode_t mode,
5788 const struct llapi_stripe_param *stripe_param)
5790 char file_path[PATH_MAX];
5791 int saved_errno = errno;
5793 unsigned int rnumber;
5799 rc = snprintf(file_path, sizeof(file_path),
5800 "%s/" LUSTRE_VOLATILE_HDR "::%.4X",
5801 directory, rnumber);
5803 rc = snprintf(file_path, sizeof(file_path),
5804 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X",
5805 directory, mdt_idx, rnumber);
5807 if (rc < 0 || rc >= sizeof(file_path))
5808 return -ENAMETOOLONG;
5811 * Either open O_WRONLY or O_RDWR, creating RDONLY
5812 * is non-sensical here
5814 if ((open_flags & O_ACCMODE) == O_RDONLY)
5815 open_flags = O_RDWR | (open_flags & ~O_ACCMODE);
5817 open_flags |= O_CREAT | O_EXCL | O_NOFOLLOW;
5819 if (stripe_param != NULL) {
5820 fd = llapi_file_open_param(file_path, open_flags,
5821 mode, stripe_param);
5825 fd = open(file_path, open_flags, mode);
5829 } while (fd < 0 && rc == -EEXIST);
5832 llapi_error(LLAPI_MSG_ERROR, rc,
5833 "Cannot create volatile file '%s' in '%s'",
5834 file_path + strlen(directory) + 1 +
5835 LUSTRE_VOLATILE_HDR_LEN,
5841 * Unlink file in case this wasn't a Lustre filesystem and the magic
5842 * volatile filename wasn't handled as intended. The effect is the
5843 * same. If volatile open was supported then we expect unlink() to
5846 (void)unlink(file_path);
5849 * Since we are returning successfully we restore errno (and
5850 * mask out possible EEXIST from open() and ENOENT from unlink().
5852 errno = saved_errno;
5858 * Create a file without any name open it for read/write
5860 * - file is created as if it were a standard file in the given \a directory
5861 * - file does not appear in \a directory and mtime does not change because
5862 * the filename is handled specially by the Lustre MDS.
5863 * - file is removed at final close
5864 * - file modes are rw------- since it doesn't make sense to have a read-only
5865 * or write-only file that cannot be opened again.
5866 * - if user wants another mode it must use fchmod() on the open file, no
5867 * security problems arise because it cannot be opened by another process.
5869 * \param[in] directory directory from which to inherit layout/MDT idx
5870 * \param[in] idx MDT index on which the file is created,
5871 * \a idx == -1 means no specific MDT is requested
5872 * \param[in] open_flags standard open(2) flags
5874 * \retval a file descriptor on success.
5875 * \retval -errno on error.
5877 int llapi_create_volatile_idx(const char *directory, int mdt_idx,
5880 return llapi_create_volatile_param(directory, mdt_idx, open_flags,
5881 S_IRUSR | S_IWUSR, NULL);
5885 * Swap the layouts between 2 file descriptors
5886 * the 2 files must be open for writing
5887 * first fd received the ioctl, second fd is passed as arg
5888 * this is assymetric but avoid use of root path for ioctl
5890 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
5891 int gid, __u64 flags)
5893 struct lustre_swap_layouts lsl;
5898 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5899 rc = fstat(fd1, &st1);
5903 rc = fstat(fd2, &st2);
5908 lsl.sl_flags = flags;
5912 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
5916 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5917 struct timeval tv1[2];
5918 struct timeval tv2[2];
5920 memset(tv1, 0, sizeof(tv1));
5921 memset(tv2, 0, sizeof(tv2));
5923 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
5924 tv1[0].tv_sec = st1.st_atime;
5925 tv2[0].tv_sec = st2.st_atime;
5927 tv1[0].tv_sec = st2.st_atime;
5928 tv2[0].tv_sec = st1.st_atime;
5931 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
5932 tv1[1].tv_sec = st1.st_mtime;
5933 tv2[1].tv_sec = st2.st_mtime;
5935 tv1[1].tv_sec = st2.st_mtime;
5936 tv2[1].tv_sec = st1.st_mtime;
5939 rc = futimes(fd1, tv1);
5943 rc = futimes(fd2, tv2);
5951 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
5958 while (grp_id == 0);
5960 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
5968 * Swap the layouts between 2 files
5969 * the 2 files are open in write
5971 int llapi_swap_layouts(const char *path1, const char *path2,
5972 __u64 dv1, __u64 dv2, __u64 flags)
5976 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
5979 llapi_error(LLAPI_MSG_ERROR, rc,
5980 "error: cannot open '%s' for write", path1);
5984 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
5987 llapi_error(LLAPI_MSG_ERROR, rc,
5988 "error: cannot open '%s' for write", path2);
5992 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
5994 llapi_error(LLAPI_MSG_ERROR, rc,
5995 "error: cannot swap layout between '%s' and '%s'",
6008 * \param fd File to lock.
6009 * \param gid Group Identifier.
6011 * \retval 0 on success.
6012 * \retval -errno on failure.
6014 int llapi_group_lock(int fd, int gid)
6018 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
6021 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
6029 * \param fd File to unlock.
6030 * \param gid Group Identifier.
6032 * \retval 0 on success.
6033 * \retval -errno on failure.
6035 int llapi_group_unlock(int fd, int gid)
6039 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
6042 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");