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, mntlen = 0, 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) &&
1349 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1350 /* check the path format */
1351 if (strlen(path) > mntlen && path[mntlen] != '/')
1353 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1354 mntdir[sizeof(mntdir) - 1] = '\0';
1355 if ((want & WANT_FSNAME) && fsname != NULL) {
1356 strncpy(fsname, ptr, mountlen);
1357 fsname[mountlen] = '\0';
1367 if ((want & WANT_PATH) && path != NULL) {
1368 strncpy(path, mntdir, mntlen);
1369 path[mntlen] = '\0';
1371 if (want & WANT_FD) {
1372 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1375 llapi_error(LLAPI_MSG_ERROR, rc,
1376 "cannot open '%s'", mntdir);
1382 } else if (want & WANT_ERROR)
1383 llapi_err_noerrno(LLAPI_MSG_ERROR,
1384 "'%s' not on a mounted Lustre filesystem",
1385 (want & WANT_PATH) ? fsname : path);
1390 * search lustre mounts
1392 * Calling this function will return to the user the mount point, mntdir, and
1393 * the file system name, fsname, if the user passed a buffer to this routine.
1395 * The user inputs are pathname and index. If the pathname is supplied then
1396 * the value of the index will be ignored. The pathname will return data if
1397 * the pathname is located on a lustre mount. Index is used to pick which
1398 * mount point you want in the case of multiple mounted lustre file systems.
1399 * See function lfs_osts in lfs.c for an example of the index use.
1401 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1404 int want = WANT_PATH, idx = -1;
1406 if (!pathname || pathname[0] == '\0') {
1410 strcpy(mntdir, pathname);
1414 want |= WANT_FSNAME;
1415 return get_root_path(want, fsname, NULL, mntdir, idx);
1418 /* Given a path, find the corresponding Lustre fsname */
1419 int llapi_search_fsname(const char *pathname, char *fsname)
1424 path = realpath(pathname, NULL);
1426 char tmp[PATH_MAX - 1];
1432 if (pathname[0] != '/') {
1434 * Need an absolute path, but realpath() only works for
1435 * pathnames that actually exist. We go through the
1436 * extra hurdle of dirname(getcwd() + pathname) in
1437 * case the relative pathname contains ".." in it.
1439 char realpath[PATH_MAX - 1];
1441 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1443 llapi_error(LLAPI_MSG_ERROR, rc,
1444 "cannot get current working directory");
1448 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1449 if (rc >= sizeof(tmp)) {
1451 llapi_error(LLAPI_MSG_ERROR, rc,
1452 "invalid parent path '%s'",
1458 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1459 if (rc >= sizeof(buf)) {
1461 llapi_error(LLAPI_MSG_ERROR, rc,
1462 "invalid path '%s'", pathname);
1465 path = realpath(buf, NULL);
1467 ptr = strrchr(buf, '/');
1469 llapi_error(LLAPI_MSG_ERROR |
1470 LLAPI_MSG_NO_ERRNO, 0,
1471 "cannot resolve path '%s'",
1476 path = realpath(buf, NULL);
1479 llapi_error(LLAPI_MSG_ERROR, rc,
1480 "cannot resolve path '%s'",
1486 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1491 int llapi_search_rootpath(char *pathname, const char *fsname)
1494 * pathname can be used as an argument by get_root_path(),
1495 * clear it for safety
1498 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1502 * Get the list of pool members.
1503 * \param poolname string of format \<fsname\>.\<poolname\>
1504 * \param members caller-allocated array of char*
1505 * \param list_size size of the members array
1506 * \param buffer caller-allocated buffer for storing OST names
1507 * \param buffer_size size of the buffer
1509 * \return number of members retrieved for this pool
1510 * \retval -error failure
1512 int llapi_get_poolmembers(const char *poolname, char **members,
1513 int list_size, char *buffer, int buffer_size)
1515 char fsname[PATH_MAX];
1524 /* name is FSNAME.POOLNAME */
1525 if (strlen(poolname) >= sizeof(fsname))
1528 snprintf(fsname, sizeof(fsname), "%s", poolname);
1529 pool = strchr(fsname, '.');
1536 rc = poolpath(&pathname, fsname, NULL);
1538 llapi_error(LLAPI_MSG_ERROR, rc,
1539 "Lustre filesystem '%s' not found",
1544 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1545 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1546 cfs_free_param_data(&pathname);
1547 if (rc >= sizeof(buf))
1549 fd = fopen(buf, "r");
1552 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1557 while (fgets(buf, sizeof(buf), fd) != NULL) {
1558 if (nb_entries >= list_size) {
1562 buf[sizeof(buf) - 1] = '\0';
1564 tmp = strchr(buf, '\n');
1567 if (used + strlen(buf) + 1 > buffer_size) {
1572 strcpy(buffer + used, buf);
1573 members[nb_entries] = buffer + used;
1574 used += strlen(buf) + 1;
1584 * Get the list of pools in a filesystem.
1585 * \param name filesystem name or path
1586 * \param poollist caller-allocated array of char*
1587 * \param list_size size of the poollist array
1588 * \param buffer caller-allocated buffer for storing pool names
1589 * \param buffer_size size of the buffer
1591 * \return number of pools retrieved for this filesystem
1592 * \retval -error failure
1594 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1595 char *buffer, int buffer_size)
1601 struct dirent *pool;
1603 unsigned int nb_entries = 0;
1604 unsigned int used = 0;
1607 /* initialize output array */
1608 for (i = 0; i < list_size; i++)
1611 /* is name a pathname ? */
1612 ptr = strchr(name, '/');
1614 char fsname_buf[MAXNAMLEN];
1616 /* We will need fsname for printing later */
1617 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1621 ptr = strrchr(fsname_buf, '-');
1625 fsname = strdup(fsname_buf);
1629 /* name is FSNAME */
1630 fsname = strdup(name);
1635 rc = poolpath(&pathname, fsname, NULL);
1637 llapi_error(LLAPI_MSG_ERROR, rc,
1638 "Lustre filesystem '%s' not found", name);
1642 dir = opendir(pathname.gl_pathv[0]);
1645 llapi_error(LLAPI_MSG_ERROR, rc,
1646 "Could not open pool list for '%s'",
1653 pool = readdir(dir);
1659 /* ignore . and .. */
1660 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1663 /* check output bounds */
1664 if (nb_entries >= list_size) {
1666 goto free_dir_no_msg;
1669 /* +2 for '.' and final '\0' */
1670 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1673 goto free_dir_no_msg;
1676 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1677 poollist[nb_entries] = buffer + used;
1678 used += strlen(pool->d_name) + strlen(fsname) + 2;
1684 llapi_error(LLAPI_MSG_ERROR, rc,
1685 "Error reading pool list for '%s'", name);
1687 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1692 cfs_free_param_data(&pathname);
1695 return rc != 0 ? rc : nb_entries;
1698 /* wrapper for lfs.c and obd.c */
1699 int llapi_poollist(const char *name)
1702 * list of pool names (assume that pool count is smaller
1705 char **list, *buffer = NULL, *fsname = (char *)name;
1706 char *poolname = NULL, *tmp = NULL, data[16];
1707 enum param_filter type = FILTER_BY_PATH;
1708 int obdcount, bufsize, rc, nb, i;
1713 if (name[0] != '/') {
1714 fsname = strdup(name);
1718 poolname = strchr(fsname, '.');
1721 type = FILTER_BY_FS_NAME;
1724 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1725 data, sizeof(data));
1728 obdcount = atoi(data);
1731 * Allocate space for each fsname-OST0000_UUID, 1 per OST,
1732 * and also an array to store the pointers for all that
1736 bufsize = sizeof(struct obd_uuid) * obdcount;
1737 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1738 if (buffer == NULL) {
1742 list = (char **) (buffer + bufsize);
1745 /* name is a path or fsname */
1746 nb = llapi_get_poollist(name, list, obdcount,
1749 /* name is a pool name (<fsname>.<poolname>) */
1750 nb = llapi_get_poolmembers(name, list, obdcount,
1754 if (nb == -EOVERFLOW) {
1757 goto retry_get_pools;
1760 for (i = 0; i < nb; i++)
1761 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1762 rc = (nb < 0 ? nb : 0);
1766 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1771 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1772 void *data, struct dirent64 *de);
1774 #define OBD_NOT_FOUND (-1)
1776 static void find_param_fini(struct find_param *param)
1778 if (param->fp_migrate)
1781 if (param->fp_obd_indexes) {
1782 free(param->fp_obd_indexes);
1783 param->fp_obd_indexes = NULL;
1786 if (param->fp_lmd) {
1787 free(param->fp_lmd);
1788 param->fp_lmd = NULL;
1791 if (param->fp_lmv_md) {
1792 free(param->fp_lmv_md);
1793 param->fp_lmv_md = NULL;
1797 static int common_param_init(struct find_param *param, char *path)
1799 int lum_size = get_mds_md_size(path);
1804 /* migrate has fp_lmv_md initialized outside */
1805 if (param->fp_migrate)
1808 if (lum_size < PATH_MAX + 1)
1809 lum_size = PATH_MAX + 1;
1811 param->fp_lum_size = lum_size;
1812 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1814 if (param->fp_lmd == NULL) {
1815 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1816 "error: allocate %zu bytes for layout failed",
1817 sizeof(lstat_t) + param->fp_lum_size);
1821 param->fp_lmv_stripe_count = 256;
1822 param->fp_lmv_md = calloc(1,
1823 lmv_user_md_size(param->fp_lmv_stripe_count,
1824 LMV_USER_MAGIC_SPECIFIC));
1825 if (param->fp_lmv_md == NULL) {
1826 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1827 "error: allocation of %d bytes for ioctl",
1828 lmv_user_md_size(param->fp_lmv_stripe_count,
1829 LMV_USER_MAGIC_SPECIFIC));
1830 find_param_fini(param);
1834 param->fp_got_uuids = 0;
1835 param->fp_obd_indexes = NULL;
1836 param->fp_obd_index = OBD_NOT_FOUND;
1837 param->fp_mdt_index = OBD_NOT_FOUND;
1841 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1842 struct dirent64 *de)
1844 struct find_param *param = data;
1850 /* set errno upon failure */
1851 static DIR *opendir_parent(const char *path)
1857 path_copy = strdup(path);
1858 if (path_copy == NULL)
1861 parent_path = dirname(path_copy);
1862 parent = opendir(parent_path);
1868 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1873 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1874 if (param->fp_get_default_lmv)
1875 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1877 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1879 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1880 if (errno == E2BIG && ret != 0) {
1884 /* if foreign LMV case, fake stripes number */
1885 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
1886 struct lmv_foreign_md *lfm;
1888 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1889 if (lfm->lfm_length < XATTR_SIZE_MAX -
1890 offsetof(typeof(*lfm), lfm_value)) {
1891 uint32_t size = lfm->lfm_length +
1892 offsetof(typeof(*lfm), lfm_value);
1894 stripe_count = lmv_foreign_to_md_stripes(size);
1896 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1897 "error: invalid %d foreign size returned from ioctl",
1902 stripe_count = param->fp_lmv_md->lum_stripe_count;
1904 if (stripe_count <= param->fp_lmv_stripe_count)
1907 free(param->fp_lmv_md);
1908 param->fp_lmv_stripe_count = stripe_count;
1909 lmv_size = lmv_user_md_size(stripe_count,
1910 LMV_USER_MAGIC_SPECIFIC);
1911 param->fp_lmv_md = malloc(lmv_size);
1912 if (param->fp_lmv_md == NULL) {
1913 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1914 "error: allocation of %d bytes for ioctl",
1915 lmv_user_md_size(param->fp_lmv_stripe_count,
1916 LMV_USER_MAGIC_SPECIFIC));
1924 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1927 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
1928 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
1929 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
1930 lmd_v2->lmd_stx.stx_uid = st->st_uid;
1931 lmd_v2->lmd_stx.stx_gid = st->st_gid;
1932 lmd_v2->lmd_stx.stx_mode = st->st_mode;
1933 lmd_v2->lmd_stx.stx_ino = st->st_ino;
1934 lmd_v2->lmd_stx.stx_size = st->st_size;
1935 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
1936 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
1937 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
1938 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
1939 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
1940 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
1941 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
1942 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
1943 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
1945 lmd_v2->lmd_flags = 0;
1947 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1949 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
1950 if (lmd_v2->lmd_stx.stx_size)
1951 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
1952 if (lmd_v2->lmd_stx.stx_blocks)
1953 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
1955 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1956 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
1957 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
1962 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
1964 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
1965 struct lov_user_mds_data *lmd_v2 = lmdbuf;
1969 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
1973 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
1976 st = lmd_v1->lmd_st;
1977 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
1978 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
1979 convert_lmd_statx(lmd_v2, &st, false);
1980 lmd_v2->lmd_lmmsize = 0;
1981 lmd_v2->lmd_padding = 0;
1986 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
1987 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1989 struct lov_user_mds_data *lmd = lmdbuf;
1990 static bool use_old_ioctl;
1994 if (parent_fd < 0 && dir_fd < 0)
1996 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
2001 * LL_IOC_MDC_GETINFO operates on the current directory inode
2002 * and returns struct lov_user_mds_data, while
2003 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
2005 if (type == GET_LMD_INFO)
2006 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_OLD :
2009 cmd = LL_IOC_LOV_GETSTRIPE;
2012 ret = ioctl(dir_fd, cmd, lmdbuf);
2013 if (ret < 0 && errno == ENOTTY && cmd == LL_IOC_MDC_GETINFO) {
2014 cmd = LL_IOC_MDC_GETINFO_OLD;
2015 use_old_ioctl = true;
2019 if (cmd == LL_IOC_MDC_GETINFO_OLD && !ret)
2020 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2021 } else if (parent_fd >= 0) {
2022 const char *fname = strrchr(path, '/');
2025 * IOC_MDC_GETFILEINFO takes as input the filename (relative to
2026 * the parent directory) and returns struct lov_user_mds_data,
2027 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2029 * This avoids opening, locking, and closing each file on the
2030 * client if that is not needed. Multiple of these ioctl() can
2031 * be done on the parent dir with a single open for all
2032 * files in that directory, and it also doesn't pollute the
2033 * client dcache with millions of dentries when traversing
2034 * a large filesystem.
2036 fname = (fname == NULL ? path : fname + 1);
2038 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2041 else if (ret >= lmdlen || ret++ == 0)
2044 if (type == GET_LMD_INFO)
2045 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_OLD :
2046 IOC_MDC_GETFILEINFO;
2048 cmd = IOC_MDC_GETFILESTRIPE;
2051 ret = ioctl(parent_fd, cmd, lmdbuf);
2052 if (ret < 0 && errno == ENOTTY &&
2053 cmd == IOC_MDC_GETFILEINFO) {
2054 cmd = IOC_MDC_GETFILEINFO_OLD;
2055 use_old_ioctl = true;
2056 goto retry_getfileinfo;
2059 if (cmd == IOC_MDC_GETFILEINFO_OLD && !ret)
2060 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2064 if (ret && type == GET_LMD_INFO) {
2065 if (errno == ENOTTY) {
2069 * ioctl is not supported, it is not a lustre fs.
2070 * Do the regular lstat(2) instead.
2072 ret = lstat_f(path, &st);
2075 llapi_error(LLAPI_MSG_ERROR, ret,
2076 "error: %s: lstat failed for %s",
2080 convert_lmd_statx(lmd, &st, true);
2082 * It may be wrong to set use_old_ioctl with true as
2083 * the file is not a lustre fs. So reset it with false
2086 use_old_ioctl = false;
2087 } else if (errno == ENOENT) {
2089 llapi_error(LLAPI_MSG_WARN, ret,
2090 "warning: %s does not exist", path);
2091 } else if (errno != EISDIR && errno != ENODATA) {
2093 llapi_error(LLAPI_MSG_ERROR, ret,
2094 "%s ioctl failed for %s.",
2095 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2096 "IOC_MDC_GETFILEINFO", path);
2103 static int get_lmd_info(char *path, DIR *parent, DIR *dir, void *lmdbuf,
2104 int lmdlen, enum get_lmd_info_type type)
2110 parent_fd = dirfd(parent);
2112 dir_fd = dirfd(dir);
2114 return get_lmd_info_fd(path, parent_fd, dir_fd, lmdbuf, lmdlen, type);
2117 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
2118 semantic_func_t sem_init,
2119 semantic_func_t sem_fini, void *data,
2120 struct dirent64 *de)
2122 struct find_param *param = (struct find_param *)data;
2123 struct dirent64 *dent;
2131 if (!d && errno != ENOTDIR) {
2133 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2136 } else if (!d && !parent) {
2137 /* ENOTDIR. Open the parent dir. */
2138 p = opendir_parent(path);
2145 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
2151 while ((dent = readdir64(d)) != NULL) {
2154 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2158 if ((len + dent->d_reclen + 2) > size) {
2159 llapi_err_noerrno(LLAPI_MSG_ERROR,
2160 "error: %s: string buffer too small",
2165 strcat(path, dent->d_name);
2167 if (dent->d_type == DT_UNKNOWN) {
2168 struct lov_user_mds_data *lmd = param->fp_lmd;
2170 rc = get_lmd_info(path, d, NULL, lmd,
2171 param->fp_lum_size, GET_LMD_INFO);
2173 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2180 switch (dent->d_type) {
2182 llapi_err_noerrno(LLAPI_MSG_ERROR,
2183 "error: %s: '%s' is UNKNOWN type %d",
2184 __func__, dent->d_name, dent->d_type);
2187 rc = llapi_semantic_traverse(path, size, d, sem_init,
2188 sem_fini, data, dent);
2189 if (rc != 0 && ret == 0)
2195 rc = sem_init(path, d, NULL, data, dent);
2196 if (rc < 0 && ret == 0) {
2201 if (sem_fini && rc == 0)
2202 sem_fini(path, d, NULL, data, dent);
2210 sem_fini(path, parent, &d, data, de);
2219 static int param_callback(char *path, semantic_func_t sem_init,
2220 semantic_func_t sem_fini, struct find_param *param)
2222 int ret, len = strlen(path);
2225 if (len > PATH_MAX) {
2227 llapi_error(LLAPI_MSG_ERROR, ret,
2228 "Path name '%s' is too long", path);
2232 buf = (char *)malloc(PATH_MAX + 1);
2236 snprintf(buf, PATH_MAX + 1, "%s", path);
2237 ret = common_param_init(param, buf);
2241 param->fp_depth = 0;
2243 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
2244 sem_fini, param, NULL);
2246 find_param_fini(param);
2248 return ret < 0 ? ret : 0;
2251 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2253 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
2257 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2262 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2264 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2268 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2273 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2277 fd = open(path, O_RDONLY | O_NONBLOCK);
2280 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
2284 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2290 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2294 fd = open(path, O_RDONLY | O_NONBLOCK);
2297 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2301 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2313 * If uuidp is NULL, return the number of available obd uuids.
2314 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2315 * there are more OSTs than allocated to uuidp, then an error is returned with
2316 * the ost_count set to number of available obd uuids.
2318 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2319 int *ost_count, enum tgt_type type)
2321 char buf[PATH_MAX], format[32];
2322 int rc = 0, index = 0;
2323 struct obd_uuid name;
2327 /* Get the lov name */
2328 if (type == LOV_TYPE)
2329 rc = llapi_file_fget_lov_uuid(fd, &name);
2331 rc = llapi_file_fget_lmv_uuid(fd, &name);
2335 /* Now get the ost uuids */
2336 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2337 FILTER_BY_EXACT, "target_obd", ¶m);
2341 fp = fopen(param.gl_pathv[0], "r");
2344 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2349 snprintf(format, sizeof(format),
2350 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2351 while (fgets(buf, sizeof(buf), fp) != NULL) {
2352 if (uuidp && (index < *ost_count)) {
2353 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2361 if (uuidp && (index > *ost_count))
2366 cfs_free_param_data(¶m);
2370 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2372 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2375 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2380 root = open(mnt, O_RDONLY | O_DIRECTORY);
2383 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2388 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2397 * Check if user specified value matches a real uuid. Ignore _UUID,
2398 * -osc-4ba41334, other trailing gunk in comparison.
2399 * @param real_uuid ends in "_UUID"
2400 * @param search_uuid may or may not end in "_UUID"
2402 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2404 int cmplen = strlen(real_uuid);
2405 int searchlen = strlen(search_uuid);
2407 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2409 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2413 * The UUIDs may legitimately be different lengths, if
2414 * the system was upgraded from an older version.
2416 if (cmplen != searchlen)
2419 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2423 * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2424 * returned in param->fp_obd_index
2426 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2428 struct obd_uuid obd_uuid;
2435 if (param->fp_got_uuids)
2438 /* Get the lov/lmv name */
2439 if (param->fp_get_lmv)
2440 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2442 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2444 if (rc != -ENOTTY) {
2445 llapi_error(LLAPI_MSG_ERROR, rc,
2446 "error: can't get %s name: %s",
2447 param->fp_get_lmv ? "lmv" : "lov",
2455 param->fp_got_uuids = 1;
2457 /* Now get the ost uuids */
2458 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2459 obd_uuid.uuid, FILTER_BY_EXACT,
2460 "target_obd", ¶m_data);
2464 fp = fopen(param_data.gl_pathv[0], "r");
2467 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2468 param_data.gl_pathv[0]);
2472 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2473 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2474 param->fp_get_lmv ? "MDTS" : "OBDS");
2476 snprintf(format, sizeof(format),
2477 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2478 while (fgets(buf, sizeof(buf), fp) != NULL) {
2481 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2484 if (param->fp_obd_uuid) {
2485 if (llapi_uuid_match(obd_uuid.uuid,
2486 param->fp_obd_uuid->uuid)) {
2487 param->fp_obd_index = index;
2490 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2491 /* Print everything */
2492 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2495 param->fp_obds_printed = 1;
2499 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2500 llapi_err_noerrno(LLAPI_MSG_ERROR,
2501 "error: %s: unknown obduuid: %s",
2502 __func__, param->fp_obd_uuid->uuid);
2506 cfs_free_param_data(¶m_data);
2511 * In this case, param->fp_obd_uuid will be an array of obduuids and
2512 * obd index for all these obduuids will be returned in
2513 * param->fp_obd_indexes
2515 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2516 int num_obds, int **obdindexes, int *obdindex,
2519 int ret, obdcount, obd_valid = 0, obdnum;
2521 struct obd_uuid *uuids = NULL;
2525 if (type == LOV_TYPE)
2526 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2528 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2532 obdcount = atoi(buf);
2533 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2538 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2540 if (ret == -EOVERFLOW) {
2541 struct obd_uuid *uuids_temp;
2543 uuids_temp = realloc(uuids, obdcount *
2544 sizeof(struct obd_uuid));
2545 if (uuids_temp != NULL) {
2547 goto retry_get_uuids;
2552 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2556 indexes = malloc(num_obds * sizeof(*obdindex));
2557 if (indexes == NULL) {
2562 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2565 /* The user may have specified a simple index */
2566 i = strtol(obduuids[obdnum].uuid, &end, 0);
2567 if (end && *end == '\0' && i < obdcount) {
2568 indexes[obdnum] = i;
2571 for (i = 0; i < obdcount; i++) {
2572 if (llapi_uuid_match(uuids[i].uuid,
2573 obduuids[obdnum].uuid)) {
2574 indexes[obdnum] = i;
2580 if (i >= obdcount) {
2581 indexes[obdnum] = OBD_NOT_FOUND;
2582 llapi_err_noerrno(LLAPI_MSG_ERROR,
2583 "invalid obduuid '%s'",
2584 obduuids[obdnum].uuid);
2590 *obdindex = OBD_NOT_FOUND;
2592 *obdindex = obd_valid;
2594 *obdindexes = indexes;
2602 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2606 if (param->fp_mdt_uuid) {
2607 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2609 ¶m->fp_mdt_indexes,
2610 ¶m->fp_mdt_index, LMV_TYPE);
2615 if (param->fp_obd_uuid) {
2616 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2618 ¶m->fp_obd_indexes,
2619 ¶m->fp_obd_index, LOV_TYPE);
2624 param->fp_got_uuids = 1;
2629 int llapi_ostlist(char *path, struct find_param *param)
2634 fd = open(path, O_RDONLY | O_DIRECTORY);
2638 ret = setup_obd_uuid(fd, path, param);
2645 * Tries to determine the default stripe attributes for a given filesystem. The
2646 * filesystem to check should be specified by fsname, or will be determined
2649 static int sattr_get_defaults(const char *const fsname,
2650 unsigned int *scount,
2651 unsigned int *ssize,
2652 unsigned int *soffset)
2658 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2659 "stripecount", val, sizeof(val));
2662 *scount = atoi(val);
2666 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2667 "stripesize", val, sizeof(val));
2674 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2675 "stripeoffset", val, sizeof(val));
2678 *soffset = atoi(val);
2685 * Tries to gather the default stripe attributes for a given filesystem. If
2686 * the attributes can be determined, they are cached for easy retreival the
2687 * next time they are needed. Only a single filesystem's attributes are
2690 int sattr_cache_get_defaults(const char *const fsname,
2691 const char *const pathname, unsigned int *scount,
2692 unsigned int *ssize, unsigned int *soffset)
2695 char fsname[PATH_MAX + 1];
2696 unsigned int stripecount;
2697 unsigned int stripesize;
2698 unsigned int stripeoffset;
2704 char fsname_buf[PATH_MAX + 1];
2705 unsigned int tmp[3];
2707 if (fsname == NULL) {
2708 rc = llapi_search_fsname(pathname, fsname_buf);
2712 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2715 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2717 * Ensure all 3 sattrs (count, size, and offset) are
2718 * successfully retrieved and stored in tmp before writing to
2721 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2725 cache.stripecount = tmp[0];
2726 cache.stripesize = tmp[1];
2727 cache.stripeoffset = tmp[2];
2728 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2732 *scount = cache.stripecount;
2734 *ssize = cache.stripesize;
2736 *soffset = cache.stripeoffset;
2741 static char *layout2name(__u32 layout_pattern)
2743 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2745 else if (layout_pattern == LOV_PATTERN_MDT)
2747 else if (layout_pattern == LOV_PATTERN_RAID0)
2749 else if (layout_pattern ==
2750 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2751 return "raid0,overstriped";
2756 enum lov_dump_flags {
2757 LDF_IS_DIR = 0x0001,
2758 LDF_IS_RAW = 0x0002,
2759 LDF_INDENT = 0x0004,
2760 LDF_SKIP_OBJS = 0x0008,
2762 LDF_EXTENSION = 0x0020,
2765 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2766 struct lov_user_ost_data_v1 *objects,
2767 enum llapi_layout_verbose verbose,
2768 int depth, char *pool_name,
2769 enum lov_dump_flags flags)
2771 bool is_dir = flags & LDF_IS_DIR;
2772 bool is_raw = flags & LDF_IS_RAW;
2773 bool indent = flags & LDF_INDENT;
2774 bool yaml = flags & LDF_YAML;
2775 bool skip_objs = flags & LDF_SKIP_OBJS;
2776 bool extension = flags & LDF_EXTENSION;
2777 char *prefix = is_dir ? "" : "lmm_";
2778 char *separator = "";
2779 char *space = indent ? " " : "";
2782 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2783 lmm_oi_set_seq(&lum->lmm_oi, 0);
2784 if (!indent && (verbose & VERBOSE_DETAIL))
2785 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2788 if (!yaml && !indent && depth && path &&
2789 ((verbose != VERBOSE_OBJID) || !is_dir))
2790 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2792 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2793 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2794 space, prefix, lum->lmm_magic);
2795 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2797 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2798 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2800 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2802 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2807 if (verbose & ~VERBOSE_DFID)
2808 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2811 * This needs a bit of hand-holding since old 1.x lmm_oi
2812 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2813 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2814 * a real FID. Ideally the 2.x code would have stored this
2815 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2816 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2817 * worked properly (especially since IGIF FIDs use mds_inum as
2818 * the FID SEQ), but unfortunately that didn't happen.
2820 * Print it to look like an IGIF FID, even though the fields
2821 * are reversed on disk, so that it makes sense to userspace.
2823 * Don't use ostid_id() and ostid_seq(), since they assume the
2824 * oi_fid fields are in the right order. This is why there are
2825 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2827 * For newer layout types hopefully this will be a real FID.
2829 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2830 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2831 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2832 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2833 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2835 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2836 (unsigned long long)seq, oid, ver);
2838 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2839 (unsigned long long)seq, oid, ver);
2842 if (verbose & VERBOSE_STRIPE_COUNT) {
2843 if (verbose & ~VERBOSE_STRIPE_COUNT)
2844 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2847 if (!is_raw && lum->lmm_stripe_count == 0 &&
2848 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2849 unsigned int scount;
2851 rc = sattr_cache_get_defaults(NULL, path,
2855 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2858 llapi_error(LLAPI_MSG_ERROR, rc,
2859 "Cannot determine default stripe count.");
2861 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2863 (__s16)lum->lmm_stripe_count);
2866 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2868 (__s16)lum->lmm_stripe_count);
2870 if (!yaml && is_dir)
2876 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
2877 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
2878 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2879 if (verbose & ~VERBOSE_EXT_SIZE && extension)
2880 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
2882 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
2883 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2885 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2888 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2891 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2893 llapi_error(LLAPI_MSG_ERROR, rc,
2894 "Cannot determine default stripe size.");
2896 /* Extension size is in KiB */
2897 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2899 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
2900 (unsigned long long)lum->lmm_stripe_size);
2902 if (!yaml && is_dir)
2908 if ((verbose & VERBOSE_PATTERN)) {
2909 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2910 if (verbose & ~VERBOSE_PATTERN)
2911 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2913 if (lov_pattern_supported(lum->lmm_pattern))
2914 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2915 layout2name(lum->lmm_pattern));
2917 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2918 separator = (!yaml && is_dir) ? " " : "\n";
2921 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2922 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2923 if (verbose & ~VERBOSE_GENERATION)
2924 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2926 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2927 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2931 if (verbose & VERBOSE_STRIPE_OFFSET) {
2932 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2933 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2934 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2936 if (is_dir || skip_objs)
2937 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2938 lum->lmm_stripe_offset ==
2939 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2940 lum->lmm_stripe_offset);
2941 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
2942 llapi_printf(LLAPI_MSG_NORMAL, "0");
2944 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2945 objects[0].l_ost_idx);
2946 if (!yaml && is_dir)
2952 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2953 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2954 if (verbose & ~VERBOSE_POOL)
2955 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2957 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2958 if (!yaml && is_dir)
2964 if (strlen(separator) != 0)
2965 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2968 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2969 struct lov_user_ost_data_v1 *objects,
2970 char *path, int obdindex, int depth,
2971 enum llapi_layout_verbose verbose,
2972 enum lov_dump_flags flags)
2974 bool is_dir = flags & LDF_IS_DIR;
2975 bool indent = flags & LDF_INDENT;
2976 bool skip_objs = flags & LDF_SKIP_OBJS;
2977 bool yaml = flags & LDF_YAML;
2978 bool obdstripe = obdindex == OBD_NOT_FOUND;
2981 if (!obdstripe && !skip_objs) {
2982 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2983 if (obdindex == objects[i].l_ost_idx) {
2993 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2996 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
2997 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2998 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
2999 char *space = " - ";
3002 llapi_printf(LLAPI_MSG_NORMAL,
3003 "%6slmm_objects:\n", " ");
3005 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
3007 llapi_printf(LLAPI_MSG_NORMAL,
3008 "\tobdidx\t\t objid\t\t objid\t\t group\n");
3010 for (i = 0; i < lum->lmm_stripe_count; i++) {
3011 int idx = objects[i].l_ost_idx;
3012 long long oid = ostid_id(&objects[i].l_ost_oi);
3013 long long gr = ostid_seq(&objects[i].l_ost_oi);
3015 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
3019 struct lu_fid fid = { 0 };
3021 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3022 llapi_printf(LLAPI_MSG_NORMAL,
3023 "%sl_ost_idx: %d\n", space, idx);
3024 llapi_printf(LLAPI_MSG_NORMAL,
3025 "%8sl_fid: "DFID_NOBRACE"\n",
3027 } else if (indent) {
3028 struct lu_fid fid = { 0 };
3030 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3031 llapi_printf(LLAPI_MSG_NORMAL,
3032 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
3033 space, i, idx, PFID(&fid));
3037 sprintf(fmt, "%s%s%s\n",
3038 "\t%6u\t%14llu\t%#13llx\t",
3039 (fid_seq_is_rsvd(gr) ||
3040 fid_seq_is_mdt0(gr)) ?
3041 "%14llu" : "%#14llx", "%s");
3042 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
3044 obdindex == idx ? " *" : "");
3048 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3051 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3052 char *path, int obdindex, int depth,
3053 enum llapi_layout_verbose verbose,
3054 enum lov_dump_flags flags)
3056 struct lmv_user_mds_data *objects = lum->lum_objects;
3057 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3058 char *separator = "";
3059 bool yaml = flags & LDF_YAML;
3060 bool obdstripe = false;
3063 if (obdindex != OBD_NOT_FOUND) {
3064 if (lum->lum_stripe_count == 0) {
3065 if (obdindex == lum->lum_stripe_offset)
3068 for (i = 0; i < lum->lum_stripe_count; i++) {
3069 if (obdindex == objects[i].lum_mds) {
3070 llapi_printf(LLAPI_MSG_NORMAL,
3085 /* show all information default */
3087 if (lum->lum_magic == LMV_USER_MAGIC)
3088 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3089 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3091 verbose = VERBOSE_OBJID;
3094 if (depth && path && ((verbose != VERBOSE_OBJID)))
3095 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3097 if (verbose & VERBOSE_STRIPE_COUNT) {
3098 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3099 if (verbose & ~VERBOSE_STRIPE_COUNT)
3100 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3101 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3102 (int)lum->lum_stripe_count);
3103 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3109 if (verbose & VERBOSE_STRIPE_OFFSET) {
3110 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3111 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3112 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3113 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3114 (int)lum->lum_stripe_offset);
3115 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3121 if (verbose & VERBOSE_HASH_TYPE) {
3122 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3123 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3125 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3126 if (verbose & ~VERBOSE_HASH_TYPE)
3127 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3128 if (type < LMV_HASH_TYPE_MAX)
3129 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3130 mdt_hash_name[type]);
3132 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3134 if (flags & LMV_HASH_FLAG_MIGRATION)
3135 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3136 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3137 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3138 if (flags & LMV_HASH_FLAG_LOST_LMV)
3139 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3144 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3145 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3146 if (lum->lum_stripe_count > 0)
3147 llapi_printf(LLAPI_MSG_NORMAL,
3148 "mdtidx\t\t FID[seq:oid:ver]\n");
3149 for (i = 0; i < lum->lum_stripe_count; i++) {
3150 int idx = objects[i].lum_mds;
3151 struct lu_fid *fid = &objects[i].lum_fid;
3153 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3154 llapi_printf(LLAPI_MSG_NORMAL,
3155 "%6u\t\t "DFID"\t\t%s\n",
3157 obdindex == idx ? " *" : "");
3161 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3162 pool_name[0] != '\0') {
3163 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3164 if (verbose & ~VERBOSE_POOL)
3165 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3167 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3171 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3172 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3175 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3176 enum lov_dump_flags flags)
3178 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3179 int depth = param->fp_max_depth;
3180 enum llapi_layout_verbose verbose = param->fp_verbose;
3181 bool yaml = flags & LDF_YAML;
3183 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3184 !(flags & LDF_IS_DIR)) && !yaml)
3185 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3187 if (verbose & VERBOSE_DETAIL) {
3188 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3189 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3190 " ", comp_v1->lcm_magic);
3191 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3192 " ", comp_v1->lcm_size);
3193 if (flags & LDF_IS_DIR)
3194 llapi_printf(LLAPI_MSG_NORMAL,
3195 "%2slcm_flags: %s\n", " ",
3196 comp_v1->lcm_mirror_count > 0 ?
3199 llapi_printf(LLAPI_MSG_NORMAL,
3200 "%2slcm_flags: %s\n", " ",
3201 llapi_layout_flags_string(comp_v1->lcm_flags));
3204 if (verbose & VERBOSE_GENERATION) {
3205 if (verbose & ~VERBOSE_GENERATION)
3206 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3208 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3211 if (verbose & VERBOSE_MIRROR_COUNT) {
3212 if (verbose & ~VERBOSE_MIRROR_COUNT)
3213 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3215 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3216 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3217 comp_v1->lcm_mirror_count + 1 : 1);
3220 if (verbose & VERBOSE_COMP_COUNT) {
3221 if (verbose & ~VERBOSE_COMP_COUNT)
3222 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3224 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3225 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3226 comp_v1->lcm_entry_count : 0);
3229 if (verbose & VERBOSE_DETAIL && !yaml)
3230 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3233 static void lcme_flags2str(__u32 comp_flags)
3239 llapi_printf(LLAPI_MSG_NORMAL, "0");
3242 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3243 if (comp_flags & comp_flags_table[i].cfn_flag) {
3245 llapi_printf(LLAPI_MSG_NORMAL, ",");
3246 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3247 comp_flags_table[i].cfn_name);
3248 comp_flags &= ~comp_flags_table[i].cfn_flag;
3254 llapi_printf(LLAPI_MSG_NORMAL, ",");
3255 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3259 static void lov_dump_comp_v1_entry(struct find_param *param,
3260 enum lov_dump_flags flags, int index)
3262 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3263 struct lov_comp_md_entry_v1 *entry;
3264 char *separator = "";
3265 enum llapi_layout_verbose verbose = param->fp_verbose;
3266 bool yaml = flags & LDF_YAML;
3268 entry = &comp_v1->lcm_entries[index];
3271 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3273 if (verbose & VERBOSE_COMP_ID) {
3274 if (verbose & VERBOSE_DETAIL && !yaml)
3275 llapi_printf(LLAPI_MSG_NORMAL,
3276 "%slcme_id: ", " - ");
3277 else if (verbose & ~VERBOSE_COMP_ID)
3278 llapi_printf(LLAPI_MSG_NORMAL,
3279 "%4slcme_id: ", " ");
3280 if (entry->lcme_id != LCME_ID_INVAL)
3281 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3283 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3287 if (verbose & VERBOSE_MIRROR_ID) {
3288 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3289 if (verbose & ~VERBOSE_MIRROR_ID)
3290 llapi_printf(LLAPI_MSG_NORMAL,
3291 "%4slcme_mirror_id: ", " ");
3292 if (entry->lcme_id != LCME_ID_INVAL)
3293 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3294 mirror_id_of(entry->lcme_id));
3296 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3300 if (verbose & VERBOSE_COMP_FLAGS) {
3301 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3302 if (verbose & ~VERBOSE_COMP_FLAGS)
3303 llapi_printf(LLAPI_MSG_NORMAL,
3304 "%4slcme_flags: ", " ");
3305 lcme_flags2str(entry->lcme_flags);
3308 /* print snapshot timestamp if its a nosync comp */
3309 if ((verbose & VERBOSE_COMP_FLAGS) &&
3310 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3311 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3312 if (verbose & ~VERBOSE_COMP_FLAGS)
3313 llapi_printf(LLAPI_MSG_NORMAL,
3314 "%4slcme_timestamp: ", " ");
3316 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3317 (unsigned long long)entry->lcme_timestamp);
3319 time_t stamp = entry->lcme_timestamp;
3320 char *date_str = asctime(localtime(&stamp));
3322 date_str[strlen(date_str) - 1] = '\0';
3323 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3329 if (verbose & VERBOSE_COMP_START) {
3330 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3331 if (verbose & ~VERBOSE_COMP_START)
3332 llapi_printf(LLAPI_MSG_NORMAL,
3333 "%4slcme_extent.e_start: ", " ");
3334 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3335 (unsigned long long)entry->lcme_extent.e_start);
3339 if (verbose & VERBOSE_COMP_END) {
3340 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3341 if (verbose & ~VERBOSE_COMP_END)
3342 llapi_printf(LLAPI_MSG_NORMAL,
3343 "%4slcme_extent.e_end: ", " ");
3344 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3345 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3347 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3348 (unsigned long long)entry->lcme_extent.e_end);
3353 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3354 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3355 } else if (verbose & VERBOSE_DETAIL) {
3356 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3357 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3358 " ", entry->lcme_offset);
3359 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3360 " ", entry->lcme_size);
3361 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3363 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3368 * Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3369 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3371 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3372 * The table below gives the answers for the specified parameters (value and
3373 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3374 * --------------------------------------
3375 * 1 | file > limit; sign > 0 | -1 / -1 |
3376 * 2 | file = limit; sign > 0 | -1 / -1 |
3377 * 3 | file < limit; sign > 0 | ? / 1 |
3378 * 4 | file > limit; sign = 0 | -1 / -1 |
3379 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3380 * 6 | file < limit; sign = 0 | ? / -1 |
3381 * 7 | file > limit; sign < 0 | 1 / 1 |
3382 * 8 | file = limit; sign < 0 | ? / -1 |
3383 * 9 | file < limit; sign < 0 | ? / -1 |
3384 * --------------------------------------
3385 * Note: 5th actually means that the value is within the interval
3386 * (limit - margin, limit].
3388 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3389 int sign, int negopt, unsigned long long margin,
3395 /* Drop the fraction of margin (of days or size). */
3396 if (file + margin <= limit)
3398 } else if (sign == 0) {
3399 if (file <= limit && file + margin > limit)
3401 else if (file + margin <= limit)
3403 } else if (sign < 0) {
3410 return negopt ? ~ret + 1 : ret;
3413 static inline struct lov_user_md *
3414 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3416 return (struct lov_user_md *)((char *)comp_v1 +
3417 comp_v1->lcm_entries[ent_idx].lcme_offset);
3420 static inline struct lov_user_ost_data_v1 *
3421 lov_v1v3_objects(struct lov_user_md *v1)
3423 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3424 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3426 return v1->lmm_objects;
3430 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3432 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3433 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3434 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3436 pool_name[0] = '\0';
3440 print_last_init_comp(struct find_param *param)
3442 /* print all component info */
3443 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3446 /* print specific component info */
3447 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3448 param->fp_check_comp_start || param->fp_check_comp_end ||
3449 param->fp_check_mirror_id || param->fp_check_mirror_index)
3455 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3459 if (param->fp_comp_end == LUSTRE_EOF) {
3460 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3461 match = end == LUSTRE_EOF ? 1 : -1;
3462 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3463 match = end == LUSTRE_EOF ? -1 : 1;
3464 else /* at least EOF */
3466 if (param->fp_exclude_comp_end)
3469 unsigned long long margin;
3471 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3472 match = find_value_cmp(end, param->fp_comp_end,
3473 param->fp_comp_end_sign,
3474 param->fp_exclude_comp_end, margin, 0);
3481 * An example of "getstripe -v" for a two components PFL file:
3484 * lcm_magic: 0x0BD60BD0
3488 * lcm_entry_count: 2
3492 * lcme_extent.e_start: 0
3493 * lcme_extent.e_end: 1048576
3497 * lmm_magic: 0x0BD10BD0
3498 * lmm_seq: 0x200000401
3499 * lmm_object_id: 0x1
3500 * lmm_fid: [0x200000401:0x1:0x0]
3501 * lmm_stripe_count: 1
3502 * lmm_stripe_size: 1048576
3503 * lmm_pattern: raid0
3505 * lmm_stripe_offset: 0
3507 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3511 * lcme_extent.e_start: 1048576
3512 * lcme_extent.e_end: EOF
3516 * lmm_magic: 0x0BD10BD0
3517 * lmm_seq: 0x200000401
3518 * lmm_object_id: 0x1
3519 * lmm_fid: [0x200000401:0x1:0x0]
3520 * lmm_stripe_count: 2
3521 * lmm_stripe_size: 1048576
3522 * lmm_pattern: raid0
3524 * lmm_stripe_offset: 1
3526 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3527 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3529 static void lov_dump_comp_v1(struct find_param *param, char *path,
3530 enum lov_dump_flags flags)
3532 struct lov_comp_md_entry_v1 *entry;
3533 struct lov_user_ost_data_v1 *objects;
3534 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3535 struct lov_user_md_v1 *v1;
3536 char pool_name[LOV_MAXPOOLNAME + 1];
3537 int obdindex = param->fp_obd_index;
3538 int i, j, match, ext;
3539 bool obdstripe = false;
3540 __u16 mirror_index = 0;
3541 __u16 mirror_id = 0;
3543 if (obdindex != OBD_NOT_FOUND) {
3544 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3545 i < comp_v1->lcm_entry_count; i++) {
3546 if (!(comp_v1->lcm_entries[i].lcme_flags &
3550 v1 = lov_comp_entry(comp_v1, i);
3551 objects = lov_v1v3_objects(v1);
3553 for (j = 0; j < v1->lmm_stripe_count; j++) {
3554 if (obdindex == objects[j].l_ost_idx) {
3567 lov_dump_comp_v1_header(param, path, flags);
3569 flags |= LDF_INDENT;
3571 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3572 entry = &comp_v1->lcm_entries[i];
3574 if (param->fp_check_comp_flags) {
3575 if (((param->fp_comp_flags & entry->lcme_flags) !=
3576 param->fp_comp_flags) ||
3577 (param->fp_comp_neg_flags & entry->lcme_flags))
3581 if (param->fp_check_comp_id &&
3582 param->fp_comp_id != entry->lcme_id)
3585 if (param->fp_check_comp_start) {
3586 match = find_value_cmp(entry->lcme_extent.e_start,
3587 param->fp_comp_start,
3588 param->fp_comp_start_sign,
3590 param->fp_comp_start_units, 0);
3595 if (param->fp_check_comp_end) {
3596 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3602 if (param->fp_check_mirror_index) {
3603 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3605 mirror_id = mirror_id_of(entry->lcme_id);
3608 match = find_value_cmp(mirror_index,
3609 param->fp_mirror_index,
3610 param->fp_mirror_index_sign,
3611 param->fp_exclude_mirror_index,
3615 } else if (param->fp_check_mirror_id) {
3616 if (mirror_id != mirror_id_of(entry->lcme_id))
3617 mirror_id = mirror_id_of(entry->lcme_id);
3619 match = find_value_cmp(mirror_id,
3620 param->fp_mirror_id,
3621 param->fp_mirror_id_sign,
3622 param->fp_exclude_mirror_id,
3628 if (print_last_init_comp(param)) {
3630 * if part of stripe info is needed, we'd print only
3631 * the last instantiated component info.
3633 if (entry->lcme_flags & LCME_FL_INIT)
3636 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3637 if (entry->lcme_flags & LCME_FL_EXTENSION)
3638 /* moved back below */
3646 if (entry->lcme_flags & LCME_FL_INIT) {
3647 if (obdindex != OBD_NOT_FOUND) {
3648 flags |= LDF_SKIP_OBJS;
3649 v1 = lov_comp_entry(comp_v1, i);
3650 objects = lov_v1v3_objects(v1);
3652 for (j = 0; j < v1->lmm_stripe_count; j++) {
3653 if (obdindex == objects[j].l_ost_idx) {
3654 flags &= ~LDF_SKIP_OBJS;
3659 flags &= ~LDF_SKIP_OBJS;
3662 flags |= LDF_SKIP_OBJS;
3665 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3667 lov_dump_comp_v1_entry(param, flags, i);
3669 v1 = lov_comp_entry(comp_v1, i);
3670 objects = lov_v1v3_objects(v1);
3671 lov_v1v3_pool_name(v1, pool_name);
3673 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3674 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3675 param->fp_max_depth, param->fp_verbose,
3678 if (print_last_init_comp(param)) {
3680 * directory layout contains only layout template, print the
3684 i = comp_v1->lcm_entry_count - 1;
3687 flags &= ~LDF_SKIP_OBJS;
3689 lov_dump_comp_v1_entry(param, flags, i);
3691 v1 = lov_comp_entry(comp_v1, i);
3692 objects = lov_v1v3_objects(v1);
3693 lov_v1v3_pool_name(v1, pool_name);
3695 entry = &comp_v1->lcm_entries[i];
3696 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3697 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3698 param->fp_max_depth, param->fp_verbose,
3703 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3704 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3707 static inline bool has_any_comp_options(struct find_param *param)
3709 enum llapi_layout_verbose verbose = param->fp_verbose;
3711 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3712 param->fp_check_comp_start || param->fp_check_comp_end ||
3713 param->fp_check_comp_flags)
3716 /* show full layout information, not component specific */
3717 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3720 return verbose & VERBOSE_COMP_OPTS;
3723 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3726 struct lov_user_md *lum = &orig->lmd_lmm;
3727 struct lov_user_mds_data *new;
3728 struct lov_comp_md_v1 *comp_v1;
3729 struct lov_comp_md_entry_v1 *ent;
3730 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3731 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3734 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3736 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3740 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3741 + sizeof(new->lmd_lmmsize));
3743 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3744 comp_v1->lcm_magic = lum->lmm_magic;
3745 comp_v1->lcm_size = lum_off + lum_size;
3746 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3747 comp_v1->lcm_flags = 0;
3748 comp_v1->lcm_entry_count = 1;
3750 ent = &comp_v1->lcm_entries[0];
3752 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3753 ent->lcme_extent.e_start = 0;
3754 ent->lcme_extent.e_end = LUSTRE_EOF;
3755 ent->lcme_offset = lum_off;
3756 ent->lcme_size = lum_size;
3758 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3763 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3764 enum lov_dump_flags flags)
3766 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3768 if (has_any_comp_options(param)) {
3769 struct lov_user_mds_data *new_lmd, *orig_lmd;
3771 orig_lmd = param->fp_lmd;
3772 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3773 if (new_lmd != NULL) {
3774 param->fp_lmd = new_lmd;
3775 lov_dump_comp_v1(param, path, flags);
3776 param->fp_lmd = orig_lmd;
3782 if (magic == LOV_USER_MAGIC_V1) {
3783 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3784 param->fp_lmd->lmd_lmm.lmm_objects,
3785 path, param->fp_obd_index,
3786 param->fp_max_depth, param->fp_verbose,
3789 char pool_name[LOV_MAXPOOLNAME + 1];
3790 struct lov_user_ost_data_v1 *objects;
3791 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3793 snprintf(pool_name, sizeof(pool_name), "%s",
3794 lmmv3->lmm_pool_name);
3795 objects = lmmv3->lmm_objects;
3796 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3797 objects, path, param->fp_obd_index,
3798 param->fp_max_depth, param->fp_verbose,
3803 static uint32_t check_foreign_type(uint32_t foreign_type)
3807 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3808 if (lu_foreign_types[i].lft_name == NULL)
3810 if (foreign_type == lu_foreign_types[i].lft_type)
3814 return LU_FOREIGN_TYPE_UNKNOWN;
3817 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3818 enum lov_dump_flags flags)
3820 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3821 bool yaml = flags & LDF_YAML;
3823 if (!yaml && param->fp_depth && path)
3824 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3826 if (param->fp_verbose & VERBOSE_DETAIL) {
3827 uint32_t type = check_foreign_type(lfm->lfm_type);
3829 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3831 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3833 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3835 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3836 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3837 lu_foreign_types[type].lft_name);
3839 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3841 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3844 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3845 lfm->lfm_length, lfm->lfm_value);
3846 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3849 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3850 enum lov_dump_flags flags)
3852 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3853 bool yaml = flags & LDF_YAML;
3855 if (!yaml && param->fp_depth && path)
3856 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3858 if (param->fp_verbose & VERBOSE_DETAIL) {
3859 uint32_t type = check_foreign_type(lfm->lfm_type);
3861 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3863 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3865 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3867 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3868 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3869 lu_foreign_types[type].lft_name);
3871 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3873 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3876 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3877 lfm->lfm_length, lfm->lfm_value);
3878 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3881 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3882 enum lov_dump_flags flags)
3886 if (param->fp_get_lmv || param->fp_get_default_lmv)
3887 magic = (__u32)param->fp_lmv_md->lum_magic;
3889 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3892 flags |= LDF_IS_RAW;
3897 case LOV_USER_MAGIC_V1:
3898 case LOV_USER_MAGIC_V3:
3899 case LOV_USER_MAGIC_SPECIFIC:
3900 lov_dump_plain_user_lmm(param, path, flags);
3902 case LOV_USER_MAGIC_FOREIGN:
3903 lov_dump_foreign_lmm(param, path, flags);
3906 case LMV_USER_MAGIC: {
3907 char pool_name[LOV_MAXPOOLNAME + 1];
3908 struct lmv_user_md *lum;
3910 lum = (struct lmv_user_md *)param->fp_lmv_md;
3911 snprintf(pool_name, sizeof(pool_name), "%s",
3912 lum->lum_pool_name);
3913 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3914 param->fp_max_depth, param->fp_verbose,
3918 case LOV_USER_MAGIC_COMP_V1:
3919 lov_dump_comp_v1(param, path, flags);
3921 case LMV_MAGIC_FOREIGN:
3922 lmv_dump_foreign_lmm(param, path, flags);
3925 llapi_printf(LLAPI_MSG_NORMAL,
3926 "unknown lmm_magic: %#x (expecting one of %#x %#x %#x %#x)\n",
3927 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3928 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3929 LMV_USER_MAGIC, LMV_MAGIC_V1);
3934 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3940 fname = strrchr(path, '/');
3942 /* It should be a file (or other non-directory) */
3943 if (fname == NULL) {
3944 dname = (char *)malloc(2);
3948 fname = (char *)path;
3950 dname = (char *)malloc(fname - path + 1);
3953 strncpy(dname, path, fname - path);
3954 dname[fname - path] = '\0';
3958 fd = open(dname, O_RDONLY | O_NONBLOCK);
3964 strcpy((char *)lum, fname);
3965 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3968 if (close(fd) == -1 && rc == 0)
3976 int llapi_file_lookup(int dirfd, const char *name)
3978 struct obd_ioctl_data data = { 0 };
3983 if (dirfd < 0 || name == NULL)
3986 data.ioc_version = OBD_IOCTL_VERSION;
3987 data.ioc_len = sizeof(data);
3988 data.ioc_inlbuf1 = (char *)name;
3989 data.ioc_inllen1 = strlen(name) + 1;
3991 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3993 llapi_error(LLAPI_MSG_ERROR, rc,
3994 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3999 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
4006 * Check if the file time matches all the given criteria (e.g. --atime +/-N).
4007 * Return -1 or 1 if file timestamp does not or does match the given criteria
4008 * correspondingly. Return 0 if the MDS time is being checked and there are
4009 * attributes on OSTs and it is not yet clear if the timespamp matches.
4011 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
4012 * updated timestamps.
4014 static int find_time_check(struct find_param *param, int mds)
4016 struct lov_user_mds_data *lmd = param->fp_lmd;
4020 /* Check if file is accepted. */
4021 if (param->fp_atime) {
4022 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4023 param->fp_atime, param->fp_asign,
4024 param->fp_exclude_atime,
4025 param->fp_time_margin, mds);
4031 if (param->fp_mtime) {
4032 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4033 param->fp_mtime, param->fp_msign,
4034 param->fp_exclude_mtime,
4035 param->fp_time_margin, mds);
4040 * If the previous check matches, but this one is not yet clear,
4041 * we should return 0 to do an RPC on OSTs.
4047 if (param->fp_ctime) {
4048 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4049 param->fp_ctime, param->fp_csign,
4050 param->fp_exclude_ctime,
4051 param->fp_time_margin, mds);
4056 * If the previous check matches, but this one is not yet clear,
4057 * we should return 0 to do an RPC on OSTs.
4066 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4068 struct lov_user_mds_data *lmd = param->fp_lmd;
4073 for (i = 0; i < 2; i++) {
4074 /* Check if file is accepted. */
4075 if (param->fp_newery[NEWERXY_ATIME][i]) {
4076 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4077 param->fp_newery[NEWERXY_ATIME][i],
4084 if (param->fp_newery[NEWERXY_MTIME][i]) {
4085 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4086 param->fp_newery[NEWERXY_MTIME][i],
4092 * If the previous check matches, but this one is not
4093 * yet clear, we should return 0 to do an RPC on OSTs.
4099 if (param->fp_newery[NEWERXY_CTIME][i]) {
4100 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4101 param->fp_newery[NEWERXY_CTIME][i],
4107 * If the previous check matches, but this one is not
4108 * yet clear, we should return 0 to do an RPC on OSTs.
4115 * File birth time (btime) can get from MDT directly.
4116 * if @from_mdt is true, it means the input file attributs are
4117 * obtained directly from MDT.
4118 * Thus, if @from_mdt is false, we should skip the following
4124 if (param->fp_newery[NEWERXY_BTIME][i]) {
4125 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4128 rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4129 param->fp_newery[NEWERXY_BTIME][i],
4140 * Check whether the stripes matches the indexes user provided
4144 static int check_obd_match(struct find_param *param)
4146 struct lov_user_ost_data_v1 *objects;
4147 struct lov_comp_md_v1 *comp_v1 = NULL;
4148 struct lov_user_mds_data *lmd = param->fp_lmd;
4149 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4150 int i, j, k, count = 1;
4152 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4155 if (!S_ISREG(lmd->lmd_stx.stx_mode))
4158 /* exclude foreign */
4159 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4160 return param->fp_exclude_obd;
4163 * Only those files should be accepted, which have a
4164 * stripe on the specified OST.
4166 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4167 comp_v1 = (struct lov_comp_md_v1 *)v1;
4168 count = comp_v1->lcm_entry_count;
4171 for (i = 0; i < count; i++) {
4173 v1 = lov_comp_entry(comp_v1, i);
4175 objects = lov_v1v3_objects(v1);
4177 for (j = 0; j < v1->lmm_stripe_count; j++) {
4178 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4181 for (k = 0; k < param->fp_num_obds; k++) {
4182 if (param->fp_obd_indexes[k] ==
4183 objects[j].l_ost_idx)
4184 return !param->fp_exclude_obd;
4189 return param->fp_exclude_obd;
4192 static int check_mdt_match(struct find_param *param)
4196 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4199 /* FIXME: For striped dir, we should get stripe information and check */
4200 for (i = 0; i < param->fp_num_mdts; i++) {
4201 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4202 return !param->fp_exclude_mdt;
4205 if (param->fp_exclude_mdt)
4212 * Check whether the obd is active or not, if it is
4213 * not active, just print the object affected by this
4216 static void print_failed_tgt(struct find_param *param, char *path, int type)
4218 struct obd_statfs stat_buf;
4219 struct obd_uuid uuid_buf;
4220 int tgt_nr, i, *indexes;
4223 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV) {
4224 llapi_error(LLAPI_MSG_NORMAL, ret, "%s: wrong statfs type(%d)",
4229 tgt_nr = (type == LL_STATFS_LOV) ? param->fp_obd_index :
4230 param->fp_mdt_index;
4231 indexes = (type == LL_STATFS_LOV) ? param->fp_obd_indexes :
4232 param->fp_mdt_indexes;
4234 for (i = 0; i < tgt_nr; i++) {
4235 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4236 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4238 ret = llapi_obd_statfs(path, type, indexes[i], &stat_buf,
4241 llapi_error(LLAPI_MSG_NORMAL, ret,
4242 "%s: obd_uuid: %s failed",
4243 __func__, param->fp_obd_uuid->uuid);
4247 static int find_check_stripe_size(struct find_param *param)
4249 struct lov_comp_md_v1 *comp_v1 = NULL;
4250 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4251 __u32 stripe_size = 0;
4252 int ret, i, count = 1;
4254 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4255 return param->fp_exclude_stripe_size ? 1 : -1;
4257 ret = param->fp_exclude_stripe_size ? 1 : -1;
4258 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4259 comp_v1 = (struct lov_comp_md_v1 *)v1;
4260 count = comp_v1->lcm_entry_count;
4263 for (i = 0; i < count; i++) {
4264 struct lov_comp_md_entry_v1 *ent;
4267 v1 = lov_comp_entry(comp_v1, i);
4269 ent = &comp_v1->lcm_entries[i];
4270 if (ent->lcme_flags & LCME_FL_EXTENSION)
4272 if (!(ent->lcme_flags & LCME_FL_INIT))
4275 stripe_size = v1->lmm_stripe_size;
4278 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4279 param->fp_stripe_size_sign,
4280 param->fp_exclude_stripe_size,
4281 param->fp_stripe_size_units, 0);
4286 static int find_check_ext_size(struct find_param *param)
4288 struct lov_comp_md_v1 *comp_v1;
4289 struct lov_user_md_v1 *v1;
4292 ret = param->fp_exclude_ext_size ? 1 : -1;
4293 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4294 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4297 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4298 struct lov_comp_md_entry_v1 *ent;
4300 v1 = lov_comp_entry(comp_v1, i);
4302 ent = &comp_v1->lcm_entries[i];
4303 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4306 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4307 param->fp_ext_size_sign,
4308 param->fp_exclude_ext_size,
4309 param->fp_ext_size_units, 0);
4310 /* If any ext_size matches */
4318 static __u32 find_get_stripe_count(struct find_param *param)
4320 struct lov_comp_md_v1 *comp_v1 = NULL;
4321 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4323 __u32 stripe_count = 0;
4325 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4328 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4329 comp_v1 = (struct lov_comp_md_v1 *)v1;
4330 count = comp_v1->lcm_entry_count;
4333 for (i = 0; i < count; i++) {
4335 struct lov_comp_md_entry_v1 *ent;
4337 v1 = lov_comp_entry(comp_v1, i);
4339 ent = &comp_v1->lcm_entries[i];
4340 if (!(ent->lcme_flags & LCME_FL_INIT))
4343 if (ent->lcme_flags & LCME_FL_EXTENSION)
4346 stripe_count = v1->lmm_stripe_count;
4349 return stripe_count;
4352 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4354 static int find_check_layout(struct find_param *param)
4356 struct lov_comp_md_v1 *comp_v1 = NULL;
4357 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4359 bool found = false, valid = false;
4361 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4362 comp_v1 = (struct lov_comp_md_v1 *)v1;
4363 count = comp_v1->lcm_entry_count;
4366 for (i = 0; i < count; i++) {
4368 v1 = lov_comp_entry(comp_v1, i);
4370 /* foreign file have a special magic but no pattern field */
4371 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4374 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4378 if (v1->lmm_pattern & param->fp_layout) {
4387 if ((found && !param->fp_exclude_layout) ||
4388 (!found && param->fp_exclude_layout))
4395 * if no type specified, check/exclude all foreign
4396 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4398 static int find_check_foreign(struct find_param *param)
4400 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4401 struct lov_foreign_md *lfm;
4403 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4404 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4405 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4406 return param->fp_exclude_foreign ? 1 : -1;
4410 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4411 lfm->lfm_type == param->fp_foreign_type)
4412 return param->fp_exclude_foreign ? -1 : 1;
4413 return param->fp_exclude_foreign ? 1 : -1;
4416 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4417 struct lmv_foreign_md *lfm;
4419 lfm = (void *)param->fp_lmv_md;
4420 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
4421 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4422 return param->fp_exclude_foreign ? 1 : -1;
4426 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4427 lfm->lfm_type == param->fp_foreign_type)
4428 return param->fp_exclude_foreign ? -1 : 1;
4429 return param->fp_exclude_foreign ? 1 : -1;
4434 static int find_check_pool(struct find_param *param)
4436 struct lov_comp_md_v1 *comp_v1 = NULL;
4437 struct lov_user_md_v3 *v3 = (void *)¶m->fp_lmd->lmd_lmm;
4441 if (v3->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4442 comp_v1 = (struct lov_comp_md_v1 *)v3;
4443 count = comp_v1->lcm_entry_count;
4444 /* empty requested pool is taken as no pool search */
4445 if (count == 0 && param->fp_poolname[0] == '\0') {
4451 for (i = 0; i < count; i++) {
4452 if (comp_v1 != NULL) {
4453 if (!(comp_v1->lcm_entries[i].lcme_flags &
4457 v3 = (void *)lov_comp_entry(comp_v1, i);
4460 if (v3->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4463 if (((v3->lmm_magic == LOV_USER_MAGIC_V1) &&
4464 (param->fp_poolname[0] == '\0')) ||
4465 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4466 (strncmp(v3->lmm_pool_name,
4467 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4468 ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4469 (strcmp(param->fp_poolname, "*") == 0))) {
4476 if ((found && !param->fp_exclude_pool) ||
4477 (!found && param->fp_exclude_pool))
4483 static int find_check_comp_options(struct find_param *param)
4485 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4486 struct lov_user_mds_data *lmd = param->fp_lmd;
4487 struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4488 struct lov_comp_md_entry_v1 *entry;
4491 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4494 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4495 comp_v1 = (struct lov_comp_md_v1 *)v1;
4497 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4498 if (forged_v1 == NULL)
4500 comp_v1 = forged_v1;
4501 comp_v1->lcm_entry_count = 1;
4502 entry = &comp_v1->lcm_entries[0];
4503 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4505 entry->lcme_extent.e_start = 0;
4506 entry->lcme_extent.e_end = LUSTRE_EOF;
4509 /* invalid case, don't match for any kind of search. */
4510 if (comp_v1->lcm_entry_count == 0) {
4515 if (param->fp_check_comp_count) {
4516 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4517 param->fp_comp_count,
4518 param->fp_comp_count_sign,
4519 param->fp_exclude_comp_count, 1, 0);
4525 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4526 entry = &comp_v1->lcm_entries[i];
4528 if (param->fp_check_comp_flags) {
4530 if (((param->fp_comp_flags & entry->lcme_flags) !=
4531 param->fp_comp_flags) ||
4532 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4538 if (param->fp_check_comp_start) {
4539 ret = find_value_cmp(entry->lcme_extent.e_start,
4540 param->fp_comp_start,
4541 param->fp_comp_start_sign,
4542 param->fp_exclude_comp_start,
4543 param->fp_comp_start_units, 0);
4548 if (param->fp_check_comp_end) {
4549 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4555 /* the component matches all criteria */
4564 static int find_check_mirror_options(struct find_param *param)
4566 struct lov_comp_md_v1 *comp_v1;
4567 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4570 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4573 comp_v1 = (struct lov_comp_md_v1 *)v1;
4575 if (param->fp_check_mirror_count) {
4576 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4577 param->fp_mirror_count,
4578 param->fp_mirror_count_sign,
4579 param->fp_exclude_mirror_count, 1, 0);
4584 if (param->fp_check_mirror_state) {
4586 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4588 if ((param->fp_mirror_state != 0 &&
4589 file_state != param->fp_mirror_state) ||
4590 file_state == param->fp_mirror_neg_state)
4597 static bool find_check_lmm_info(struct find_param *param)
4599 return param->fp_check_pool || param->fp_check_stripe_count ||
4600 param->fp_check_stripe_size || param->fp_check_layout ||
4601 param->fp_check_comp_count || param->fp_check_comp_end ||
4602 param->fp_check_comp_start || param->fp_check_comp_flags ||
4603 param->fp_check_mirror_count || param->fp_check_foreign ||
4604 param->fp_check_mirror_state || param->fp_check_ext_size ||
4605 param->fp_check_projid;
4609 * Get file/directory project id.
4610 * by the open fd resides on.
4611 * Return 0 and project id on success, or -ve errno.
4613 static int fget_projid(int fd, int *projid)
4618 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
4622 *projid = fsx.fsx_projid;
4626 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
4627 void *data, struct dirent64 *de)
4629 struct find_param *param = (struct find_param *)data;
4630 struct lov_user_mds_data *lmd = param->fp_lmd;
4631 DIR *dir = dirp == NULL ? NULL : *dirp;
4632 int decision = 1; /* 1 is accepted; -1 is rejected. */
4634 int checked_type = 0;
4636 __u32 stripe_count = 0;
4640 if (parent == NULL && dir == NULL)
4643 /* If a regular expression is presented, make the initial decision */
4644 if (param->fp_pattern != NULL) {
4645 char *fname = strrchr(path, '/');
4647 fname = (fname == NULL ? path : fname + 1);
4648 ret = fnmatch(param->fp_pattern, fname, 0);
4649 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4650 (ret == 0 && param->fp_exclude_pattern))
4654 /* See if we can check the file type from the dirent. */
4655 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
4658 if (DTTOIF(de->d_type) == param->fp_type) {
4659 if (param->fp_exclude_type)
4662 if (!param->fp_exclude_type)
4670 * Request MDS for the stat info if some of these parameters need
4673 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4674 param->fp_check_uid || param->fp_check_gid ||
4675 param->fp_newerxy || param->fp_btime ||
4676 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4677 param->fp_check_size || param->fp_check_blocks ||
4678 find_check_lmm_info(param) ||
4679 param->fp_check_mdt_count || param->fp_check_hash_type)
4682 if (param->fp_type != 0 && checked_type == 0)
4685 if (decision == 0) {
4686 if (dir && (param->fp_check_mdt_count ||
4687 param->fp_check_hash_type || param->fp_check_foreign)) {
4688 param->fp_get_lmv = 1;
4689 ret = cb_get_dirstripe(path, dir, param);
4692 * XXX this works to decide for foreign
4695 if (errno == ENODATA &&
4696 param->fp_check_foreign) {
4697 if (param->fp_exclude_foreign)
4705 param->fp_lmd->lmd_lmm.lmm_magic = 0;
4706 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
4707 param->fp_lum_size, GET_LMD_INFO);
4708 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
4709 find_check_lmm_info(param)) {
4710 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
4713 * We need to "fake" the "use the default" values
4714 * since the lmm struct is zeroed out at this point.
4716 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4717 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
4719 ostid_set_seq(&lmm->lmm_oi,
4720 FID_SEQ_LOV_DEFAULT);
4721 lmm->lmm_stripe_size = 0;
4722 lmm->lmm_stripe_count = 0;
4723 lmm->lmm_stripe_offset = -1;
4725 if (ret == 0 && param->fp_mdt_uuid != NULL) {
4727 ret = llapi_file_fget_mdtidx(dirfd(dir),
4728 ¶m->fp_file_mdt_index);
4729 } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
4731 * FIXME: we could get the MDT index from the
4732 * file's FID in lmd->lmd_lmm.lmm_oi without
4733 * opening the file, once we are sure that
4734 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
4735 * That would still be an ioctl() to map the
4736 * FID to the MDT, but not an open RPC.
4738 fd = open(path, O_RDONLY);
4740 ret = llapi_file_fget_mdtidx(fd,
4741 ¶m->fp_file_mdt_index);
4747 * For a special file, we assume it resides on
4748 * the same MDT as the parent directory.
4750 ret = llapi_file_fget_mdtidx(dirfd(parent),
4751 ¶m->fp_file_mdt_index);
4762 stripe_count = find_get_stripe_count(param);
4766 if (param->fp_type && !checked_type) {
4767 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
4768 if (param->fp_exclude_type)
4771 if (!param->fp_exclude_type)
4777 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
4778 if (lustre_fs && param->fp_got_uuids &&
4779 param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
4780 lmd->lmd_stx.stx_dev_minor)) {
4781 /* A lustre/lustre mount point is crossed. */
4782 param->fp_got_uuids = 0;
4783 param->fp_obds_printed = 0;
4784 param->fp_mdt_index = OBD_NOT_FOUND;
4785 param->fp_obd_index = OBD_NOT_FOUND;
4788 if (lustre_fs && !param->fp_got_uuids) {
4789 ret = setup_target_indexes(dir ? dir : parent, path,
4794 param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
4795 lmd->lmd_stx.stx_dev_minor);
4796 } else if (!lustre_fs && param->fp_got_uuids) {
4797 /* A lustre/non-lustre mount point is crossed. */
4798 param->fp_got_uuids = 0;
4799 param->fp_mdt_index = OBD_NOT_FOUND;
4800 param->fp_obd_index = OBD_NOT_FOUND;
4804 if (param->fp_check_foreign) {
4805 decision = find_check_foreign(param);
4810 if (param->fp_check_stripe_size) {
4811 decision = find_check_stripe_size(param);
4816 if (param->fp_check_ext_size) {
4817 decision = find_check_ext_size(param);
4822 if (param->fp_check_stripe_count) {
4823 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
4824 param->fp_stripe_count_sign,
4825 param->fp_exclude_stripe_count, 1, 0);
4830 if (param->fp_check_mdt_count) {
4831 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4836 decision = find_value_cmp(
4837 param->fp_lmv_md->lum_stripe_count,
4838 param->fp_mdt_count,
4839 param->fp_mdt_count_sign,
4840 param->fp_exclude_mdt_count, 1, 0);
4845 if (param->fp_check_layout) {
4846 decision = find_check_layout(param);
4851 if (param->fp_check_hash_type) {
4854 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4859 found = param->fp_lmv_md->lum_hash_type & param->fp_hash_type;
4860 if ((found && param->fp_exclude_hash_type) ||
4861 (!found && !param->fp_exclude_hash_type)) {
4867 /* If an OBD UUID is specified but none matches, skip this file. */
4868 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
4869 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
4873 * If an OST or MDT UUID is given, and some OST matches,
4876 if (param->fp_obd_index != OBD_NOT_FOUND ||
4877 param->fp_mdt_index != OBD_NOT_FOUND) {
4878 if (param->fp_obd_uuid) {
4879 if (check_obd_match(param)) {
4881 * If no mdtuuid is given, we are done.
4882 * Otherwise, fall through to the mdtuuid
4885 if (!param->fp_mdt_uuid)
4892 if (param->fp_mdt_uuid) {
4893 if (check_mdt_match(param))
4900 if (param->fp_check_uid) {
4901 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
4902 if (param->fp_exclude_uid)
4905 if (!param->fp_exclude_uid)
4910 if (param->fp_check_gid) {
4911 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
4912 if (param->fp_exclude_gid)
4915 if (!param->fp_exclude_gid)
4920 if (param->fp_check_projid) {
4924 fd = open(path, O_RDONLY);
4927 ret = fget_projid(fd, &projid);
4932 if (projid == param->fp_projid) {
4933 if (param->fp_exclude_projid)
4936 if (!param->fp_exclude_projid)
4941 if (param->fp_check_pool) {
4942 decision = find_check_pool(param);
4947 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
4948 param->fp_check_comp_start || param->fp_check_comp_end) {
4949 decision = find_check_comp_options(param);
4954 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
4955 decision = find_check_mirror_options(param);
4960 /* Check the time on mds. */
4962 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
4965 for_mds = lustre_fs ?
4966 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
4967 decision = find_time_check(param, for_mds);
4972 if (param->fp_btime) {
4973 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
4978 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4979 param->fp_btime, param->fp_bsign,
4980 param->fp_exclude_btime,
4981 param->fp_time_margin, 0);
4986 if (param->fp_newerxy) {
4989 for_mds = lustre_fs ?
4990 (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
4991 decision = find_newerxy_check(param, for_mds, true);
5000 flags = param->fp_lmd->lmd_flags;
5001 if (param->fp_check_size &&
5002 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5003 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5004 !(flags & OBD_MD_FLSIZE ||
5005 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
5008 if (param->fp_check_blocks &&
5009 ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5010 S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5011 !(flags & OBD_MD_FLBLOCKS ||
5012 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
5016 * If file still fits the request, ask ost for updated info.
5017 * The regular stat is almost of the same speed as some new
5018 * 'glimpse-size-ioctl'.
5024 * For regular files with the stripe the decision may have not
5025 * been taken yet if *time or size is to be checked.
5027 if (param->fp_obd_index != OBD_NOT_FOUND)
5028 print_failed_tgt(param, path, LL_STATFS_LOV);
5030 if (param->fp_mdt_index != OBD_NOT_FOUND)
5031 print_failed_tgt(param, path, LL_STATFS_LMV);
5034 ret = fstat_f(dirfd(dir), &st);
5035 else if (de != NULL)
5036 ret = fstatat_f(dirfd(parent), de->d_name, &st,
5037 AT_SYMLINK_NOFOLLOW);
5039 ret = lstat_f(path, &st);
5042 if (errno == ENOENT) {
5043 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
5044 "warning: %s: %s does not exist",
5049 llapi_error(LLAPI_MSG_ERROR, ret,
5050 "%s: stat on %s failed",
5056 convert_lmd_statx(param->fp_lmd, &st, true);
5057 /* Check the time on osc. */
5058 decision = find_time_check(param, 0);
5062 if (param->fp_newerxy) {
5063 decision = find_newerxy_check(param, 0, false);
5073 if (param->fp_check_size) {
5074 decision = find_value_cmp(lmd->lmd_stx.stx_size,
5076 param->fp_size_sign,
5077 param->fp_exclude_size,
5078 param->fp_size_units, 0);
5083 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
5084 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
5086 param->fp_blocks_sign,
5087 param->fp_exclude_blocks,
5088 param->fp_blocks_units, 0);
5094 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
5095 if (param->fp_zero_end)
5096 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
5098 llapi_printf(LLAPI_MSG_NORMAL, "\n");
5102 /* Do not get down anymore? */
5103 if (param->fp_depth == param->fp_max_depth) {
5114 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
5115 void *param_data, struct dirent64 *de)
5117 struct find_param *param = (struct find_param *)param_data;
5118 struct lmv_user_md *lmu = param->fp_lmv_md;
5119 DIR *tmp_parent = parent;
5120 char raw[MAX_IOC_BUFLEN] = {'\0'};
5122 struct obd_ioctl_data data = { 0 };
5129 if (parent == NULL && dirp == NULL)
5138 if (parent == NULL) {
5139 tmp_parent = opendir_parent(path);
5140 if (tmp_parent == NULL) {
5143 llapi_error(LLAPI_MSG_ERROR, ret,
5144 "can not open %s", path);
5149 fd = dirfd(tmp_parent);
5151 path_copy = strdup(path);
5152 filename = basename(path_copy);
5154 data.ioc_inlbuf1 = (char *)filename;
5155 data.ioc_inllen1 = strlen(filename) + 1;
5156 data.ioc_inlbuf2 = (char *)lmu;
5157 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
5159 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5161 llapi_error(LLAPI_MSG_ERROR, ret,
5162 "%s: error packing ioctl data", __func__);
5167 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
5169 if (errno == EBUSY && !retry) {
5171 * because migrate may not be able to lock all involved
5172 * objects in order, for some of them it try lock, while
5173 * there may be conflicting COS locks and cause migrate
5174 * fail with EBUSY, hope a sync() could cause
5175 * transaction commit and release these COS locks.
5180 } else if (errno == EALREADY) {
5181 if (param->fp_verbose & VERBOSE_DETAIL)
5182 llapi_printf(LLAPI_MSG_NORMAL,
5183 "%s migrated to MDT%d already\n",
5184 path, lmu->lum_stripe_offset);
5188 llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
5192 } else if (param->fp_verbose & VERBOSE_DETAIL) {
5193 llapi_printf(LLAPI_MSG_NORMAL,
5194 "migrate %s to MDT%d stripe count %d\n",
5195 path, lmu->lum_stripe_offset,
5196 lmu->lum_stripe_count);
5202 * If the directory is being migration, we need
5203 * close the directory after migration,
5204 * so the old directory cache will be cleanup
5205 * on the client side, and re-open to get the
5206 * new directory handle
5208 *dirp = opendir(path);
5209 if (*dirp == NULL) {
5211 llapi_error(LLAPI_MSG_ERROR, ret,
5212 "%s: Failed to open '%s'", __func__, path);
5217 closedir(tmp_parent);
5224 /* dir migration finished, shrink its stripes */
5225 static int cb_migrate_mdt_fini(char *path, DIR *parent, DIR **dirp, void *data,
5226 struct dirent64 *de)
5228 struct find_param *param = data;
5229 struct lmv_user_md *lmu = param->fp_lmv_md;
5230 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
5233 if (de && de->d_type != DT_DIR)
5238 * close it before setxattr because the latter may destroy the
5239 * original object, and cause close fail.
5241 ret = closedir(*dirp);
5247 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
5248 if (ret == -EALREADY)
5251 cb_common_fini(path, parent, dirp, data, de);
5255 int llapi_migrate_mdt(char *path, struct find_param *param)
5257 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
5261 int llapi_mv(char *path, struct find_param *param)
5263 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
5264 static bool printed;
5267 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
5268 "%s() is deprecated, use llapi_migrate_mdt() instead",
5273 return llapi_migrate_mdt(path, param);
5276 int llapi_find(char *path, struct find_param *param)
5278 return param_callback(path, cb_find_init, cb_common_fini, param);
5282 * Get MDT number that the file/directory inode referenced
5283 * by the open fd resides on.
5284 * Return 0 and mdtidx on success, or -ve errno.
5286 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
5288 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
5293 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
5294 struct dirent64 *de)
5296 struct find_param *param = (struct find_param *)data;
5297 DIR *d = dirp == NULL ? NULL : *dirp;
5301 if (parent == NULL && d == NULL)
5305 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5306 } else /* if (parent) */ {
5309 fd = open(path, O_RDONLY | O_NOCTTY);
5311 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5319 if (ret == -ENODATA) {
5320 if (!param->fp_obd_uuid)
5321 llapi_printf(LLAPI_MSG_NORMAL,
5322 "'%s' has no stripe info\n", path);
5324 } else if (ret == -ENOENT) {
5325 llapi_error(LLAPI_MSG_WARN, ret,
5326 "warning: %s: '%s' does not exist",
5329 } else if (ret == -ENOTTY) {
5330 llapi_error(LLAPI_MSG_ERROR, ret,
5331 "%s: '%s' not on a Lustre fs",
5334 llapi_error(LLAPI_MSG_ERROR, ret,
5335 "error: %s: '%s' failed get_mdtidx",
5341 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5342 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5344 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5348 /* Do not go down anymore? */
5349 if (param->fp_depth == param->fp_max_depth)
5357 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
5358 struct dirent64 *de)
5360 struct find_param *param = (struct find_param *)data;
5361 DIR *d = dirp == NULL ? NULL : *dirp;
5364 if (parent == NULL && d == NULL)
5367 if (param->fp_obd_uuid) {
5368 param->fp_quiet = 1;
5369 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
5374 if (d && (param->fp_get_lmv || param->fp_get_default_lmv))
5375 ret = cb_get_dirstripe(path, d, param);
5377 (parent && !param->fp_get_lmv && !param->fp_get_default_lmv))
5378 ret = get_lmd_info(path, parent, d, ¶m->fp_lmd->lmd_lmm,
5379 param->fp_lum_size, GET_LMD_STRIPE);
5384 if (errno == ENODATA && d != NULL) {
5386 * We need to "fake" the "use the default" values
5387 * since the lmm struct is zeroed out at this point.
5388 * The magic needs to be set in order to satisfy
5389 * a check later on in the code path.
5390 * The object_seq needs to be set for the "(Default)"
5391 * prefix to be displayed.
5393 if (param->fp_get_default_lmv) {
5394 struct lmv_user_md *lum = param->fp_lmv_md;
5396 lum->lum_magic = LMV_USER_MAGIC;
5397 lum->lum_stripe_count = 0;
5398 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
5400 } else if (param->fp_get_lmv) {
5401 struct lmv_user_md *lum = param->fp_lmv_md;
5404 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5407 lum->lum_magic = LMV_MAGIC_V1;
5408 lum->lum_stripe_count = 0;
5409 lum->lum_stripe_offset = mdtidx;
5412 struct lov_user_md *lmm =
5413 ¶m->fp_lmd->lmd_lmm;
5415 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5417 ostid_set_seq(&lmm->lmm_oi,
5418 FID_SEQ_LOV_DEFAULT);
5419 lmm->lmm_stripe_count = 0;
5420 lmm->lmm_stripe_size = 0;
5421 lmm->lmm_stripe_offset = -1;
5424 } else if (errno == ENODATA && parent != NULL) {
5425 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5426 llapi_printf(LLAPI_MSG_NORMAL,
5427 "%s has no stripe info\n", path);
5429 } else if (errno == ENOENT) {
5430 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5431 "warning: %s: %s does not exist",
5434 } else if (errno == ENOTTY) {
5436 llapi_error(LLAPI_MSG_ERROR, ret,
5437 "%s: '%s' not on a Lustre fs?",
5442 llapi_error(LLAPI_MSG_ERROR, ret,
5443 "error: %s: %s failed for %s",
5444 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
5445 "IOC_MDC_GETFILESTRIPE", path);
5452 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5453 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
5456 /* Do not get down anymore? */
5457 if (param->fp_depth == param->fp_max_depth)
5465 int llapi_getstripe(char *path, struct find_param *param)
5467 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5468 cb_get_mdt_index : cb_getstripe,
5469 cb_common_fini, param);
5472 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5473 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5475 char raw[MAX_IOC_BUFLEN] = {'\0'};
5477 struct obd_ioctl_data data = { 0 };
5480 data.ioc_inlbuf1 = (char *)&type;
5481 data.ioc_inllen1 = sizeof(__u32);
5482 data.ioc_inlbuf2 = (char *)&index;
5483 data.ioc_inllen2 = sizeof(__u32);
5484 data.ioc_pbuf1 = (char *)stat_buf;
5485 data.ioc_plen1 = sizeof(struct obd_statfs);
5486 data.ioc_pbuf2 = (char *)uuid_buf;
5487 data.ioc_plen2 = sizeof(struct obd_uuid);
5489 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5491 llapi_error(LLAPI_MSG_ERROR, rc,
5492 "%s: error packing ioctl data", __func__);
5496 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5498 return rc < 0 ? -errno : 0;
5501 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5502 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5507 fd = open(path, O_RDONLY);
5510 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5513 * If we can't even open a file on the filesystem (e.g. with
5514 * -ESHUTDOWN), force caller to exit or it will loop forever.
5519 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5526 #define MAX_STRING_SIZE 128
5528 int llapi_ping(char *obd_type, char *obd_name)
5530 int flags = O_RDONLY;
5531 char buf[1] = { 0 };
5535 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5536 obd_type, obd_name);
5540 fd = open(path.gl_pathv[0], flags);
5542 if (errno == EACCES && flags == O_RDONLY) {
5547 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5552 if (flags == O_RDONLY)
5553 rc = read(fd, buf, sizeof(buf));
5555 rc = write(fd, buf, sizeof(buf));
5563 cfs_free_param_data(&path);
5567 int llapi_target_iterate(int type_num, char **obd_type,
5568 void *args, llapi_cb_t cb)
5574 for (i = 0; i < type_num; i++) {
5577 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5581 for (j = 0; j < param.gl_pathc; j++) {
5582 char obd_uuid[UUID_MAX + 1];
5586 fp = fopen(param.gl_pathv[j], "r");
5589 llapi_error(LLAPI_MSG_ERROR, rc,
5590 "error: opening '%s'",
5595 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5597 llapi_error(LLAPI_MSG_ERROR, rc,
5598 "error: reading '%s'",
5603 /* Extract the obd_name from the sysfs path.
5604 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5606 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5612 /* skip /fs/lustre/'obd_type'/ */
5613 obd_name += strlen(obd_type[i]) + 12;
5614 /* chop off after obd_name */
5615 ptr = strrchr(obd_name, '/');
5619 cb(obd_type[i], obd_name, obd_uuid, args);
5628 cfs_free_param_data(¶m);
5632 static void do_target_check(char *obd_type_name, char *obd_name,
5633 char *obd_uuid, void *args)
5637 rc = llapi_ping(obd_type_name, obd_name);
5639 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5641 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5643 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5646 int llapi_target_check(int type_num, char **obd_type, char *dir)
5648 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5651 #undef MAX_STRING_SIZE
5653 /* Is this a lustre fs? */
5654 int llapi_is_lustre_mnttype(const char *type)
5656 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
5659 /* Is this a lustre client fs? */
5660 int llapi_is_lustre_mnt(struct mntent *mnt)
5662 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5663 strstr(mnt->mnt_fsname, ":/") != NULL);
5666 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5668 char fsname[PATH_MAX + 1];
5672 rc = llapi_search_fsname(mnt, fsname);
5676 root = open(mnt, O_RDONLY | O_DIRECTORY);
5679 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
5683 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
5686 if (rc == -ENOENT && LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd))
5687 llapi_error(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, rc,
5688 "Cannot find pool '%s'", qctl->qc_poolname);
5694 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
5699 root = open(mnt, O_RDONLY | O_DIRECTORY);
5702 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
5706 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
5709 llapi_error(LLAPI_MSG_ERROR, rc,
5710 "ioctl on %s for getting connect flags failed", mnt);
5717 * Get a 64-bit value representing the version of file data pointed by fd.
5719 * Each write or truncate, flushed on OST, will change this value. You can use
5720 * this value to verify if file data was modified. This only checks the file
5721 * data, not metadata.
5723 * \param flags 0: no flush pages, usually used it the process has already
5725 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
5727 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
5728 * pages from clients.
5730 * \retval 0 on success.
5731 * \retval -errno on error.
5733 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
5736 struct ioc_data_version idv;
5738 idv.idv_flags = (__u32)flags;
5740 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5744 *data_version = idv.idv_version;
5750 * Flush cached pages from all clients.
5752 * \param fd File descriptor
5756 int llapi_file_flush(int fd)
5760 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);
5764 * Fetch layout version from OST objects. Layout version on OST objects are
5765 * only set when the file is a mirrored file AND after the file has been
5766 * written at least once.
5768 * It actually fetches the least layout version from the objects.
5770 int llapi_get_ost_layout_version(int fd, __u32 *layout_version)
5773 struct ioc_data_version idv = { 0 };
5775 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5779 *layout_version = idv.idv_layout_version;
5785 * Create a file without any name and open it for read/write
5787 * - file is created as if it were a standard file in the given \a directory
5788 * - file does not appear in \a directory and mtime does not change because
5789 * the filename is handled specially by the Lustre MDS.
5790 * - file is destroyed at final close
5792 * \param[in] directory directory from which to inherit layout/MDT idx
5793 * \param[in] mdt_idx MDT index on which the file is created,
5794 * \a idx == -1 means no specific MDT is requested
5795 * \param[in] mode standard open(2) mode
5796 * \param[in] stripe_param stripe parameters. May be NULL.
5798 * \retval a file descriptor on success.
5799 * \retval -errno on error.
5801 int llapi_create_volatile_param(const char *directory, int mdt_idx,
5802 int open_flags, mode_t mode,
5803 const struct llapi_stripe_param *stripe_param)
5805 char file_path[PATH_MAX];
5806 int saved_errno = errno;
5808 unsigned int rnumber;
5814 rc = snprintf(file_path, sizeof(file_path),
5815 "%s/" LUSTRE_VOLATILE_HDR "::%.4X",
5816 directory, rnumber);
5818 rc = snprintf(file_path, sizeof(file_path),
5819 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X",
5820 directory, mdt_idx, rnumber);
5822 if (rc < 0 || rc >= sizeof(file_path))
5823 return -ENAMETOOLONG;
5826 * Either open O_WRONLY or O_RDWR, creating RDONLY
5827 * is non-sensical here
5829 if ((open_flags & O_ACCMODE) == O_RDONLY)
5830 open_flags = O_RDWR | (open_flags & ~O_ACCMODE);
5832 open_flags |= O_CREAT | O_EXCL | O_NOFOLLOW;
5834 if (stripe_param != NULL) {
5835 fd = llapi_file_open_param(file_path, open_flags,
5836 mode, stripe_param);
5840 fd = open(file_path, open_flags, mode);
5844 } while (fd < 0 && rc == -EEXIST);
5847 llapi_error(LLAPI_MSG_ERROR, rc,
5848 "Cannot create volatile file '%s' in '%s'",
5849 file_path + strlen(directory) + 1 +
5850 LUSTRE_VOLATILE_HDR_LEN,
5856 * Unlink file in case this wasn't a Lustre filesystem and the magic
5857 * volatile filename wasn't handled as intended. The effect is the
5858 * same. If volatile open was supported then we expect unlink() to
5861 (void)unlink(file_path);
5864 * Since we are returning successfully we restore errno (and
5865 * mask out possible EEXIST from open() and ENOENT from unlink().
5867 errno = saved_errno;
5873 * Create a file without any name open it for read/write
5875 * - file is created as if it were a standard file in the given \a directory
5876 * - file does not appear in \a directory and mtime does not change because
5877 * the filename is handled specially by the Lustre MDS.
5878 * - file is removed at final close
5879 * - file modes are rw------- since it doesn't make sense to have a read-only
5880 * or write-only file that cannot be opened again.
5881 * - if user wants another mode it must use fchmod() on the open file, no
5882 * security problems arise because it cannot be opened by another process.
5884 * \param[in] directory directory from which to inherit layout/MDT idx
5885 * \param[in] idx MDT index on which the file is created,
5886 * \a idx == -1 means no specific MDT is requested
5887 * \param[in] open_flags standard open(2) flags
5889 * \retval a file descriptor on success.
5890 * \retval -errno on error.
5892 int llapi_create_volatile_idx(const char *directory, int mdt_idx,
5895 return llapi_create_volatile_param(directory, mdt_idx, open_flags,
5896 S_IRUSR | S_IWUSR, NULL);
5900 * Swap the layouts between 2 file descriptors
5901 * the 2 files must be open for writing
5902 * first fd received the ioctl, second fd is passed as arg
5903 * this is assymetric but avoid use of root path for ioctl
5905 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
5906 int gid, __u64 flags)
5908 struct lustre_swap_layouts lsl;
5913 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5914 rc = fstat(fd1, &st1);
5918 rc = fstat(fd2, &st2);
5923 lsl.sl_flags = flags;
5927 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
5931 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5932 struct timeval tv1[2];
5933 struct timeval tv2[2];
5935 memset(tv1, 0, sizeof(tv1));
5936 memset(tv2, 0, sizeof(tv2));
5938 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
5939 tv1[0].tv_sec = st1.st_atime;
5940 tv2[0].tv_sec = st2.st_atime;
5942 tv1[0].tv_sec = st2.st_atime;
5943 tv2[0].tv_sec = st1.st_atime;
5946 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
5947 tv1[1].tv_sec = st1.st_mtime;
5948 tv2[1].tv_sec = st2.st_mtime;
5950 tv1[1].tv_sec = st2.st_mtime;
5951 tv2[1].tv_sec = st1.st_mtime;
5954 rc = futimes(fd1, tv1);
5958 rc = futimes(fd2, tv2);
5966 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
5973 while (grp_id == 0);
5975 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
5983 * Swap the layouts between 2 files
5984 * the 2 files are open in write
5986 int llapi_swap_layouts(const char *path1, const char *path2,
5987 __u64 dv1, __u64 dv2, __u64 flags)
5991 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
5994 llapi_error(LLAPI_MSG_ERROR, rc,
5995 "error: cannot open '%s' for write", path1);
5999 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
6002 llapi_error(LLAPI_MSG_ERROR, rc,
6003 "error: cannot open '%s' for write", path2);
6007 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
6009 llapi_error(LLAPI_MSG_ERROR, rc,
6010 "error: cannot swap layout between '%s' and '%s'",
6023 * \param fd File to lock.
6024 * \param gid Group Identifier.
6026 * \retval 0 on success.
6027 * \retval -errno on failure.
6029 int llapi_group_lock(int fd, int gid)
6033 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
6036 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
6044 * \param fd File to unlock.
6045 * \param gid Group Identifier.
6047 * \retval 0 on success.
6048 * \retval -errno on failure.
6050 int llapi_group_unlock(int fd, int gid)
6054 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
6057 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");