4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/utils/liblustreapi.c
34 * Author: Peter J. Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Robert Read <rread@clusterfs.com>
50 #include <sys/ioctl.h>
57 #include <sys/statfs.h>
58 #include <sys/syscall.h>
60 #include <sys/types.h>
61 #include <sys/xattr.h>
62 #include <sys/sysmacros.h>
65 #include <libgen.h> /* for dirname() */
66 #ifdef HAVE_LINUX_UNISTD_H
67 #include <linux/unistd.h>
75 #include <libcfs/util/ioctl.h>
76 #include <libcfs/util/param.h>
77 #include <libcfs/util/string.h>
78 #include <linux/lnet/lnetctl.h>
79 #include <lustre/lustreapi.h>
80 #include <linux/lustre/lustre_ostid.h>
81 #include <linux/lustre/lustre_ioctl.h>
82 #include "lustreapi_internal.h"
84 static int llapi_msg_level = LLAPI_MSG_MAX;
85 const char *liblustreapi_cmd;
87 char *mdt_hash_name[] = { "none",
88 LMV_HASH_NAME_ALL_CHARS,
89 LMV_HASH_NAME_FNV_1A_64,
92 struct lustre_foreign_type lu_foreign_types[] = {
93 {.lft_type = LU_FOREIGN_TYPE_NONE, .lft_name = "none"},
94 {.lft_type = LU_FOREIGN_TYPE_DAOS, .lft_name = "daos"},
95 /* must be the last element */
96 {.lft_type = LU_FOREIGN_TYPE_UNKNOWN, .lft_name = NULL}
97 /* array max dimension must be <= UINT32_MAX */
100 void llapi_msg_set_level(int level)
102 /* ensure level is in the good range */
103 if (level < LLAPI_MSG_OFF)
104 llapi_msg_level = LLAPI_MSG_OFF;
105 else if (level > LLAPI_MSG_MAX)
106 llapi_msg_level = LLAPI_MSG_MAX;
108 llapi_msg_level = level;
111 int llapi_msg_get_level(void)
113 return llapi_msg_level;
116 void llapi_set_command_name(const char *cmd)
118 liblustreapi_cmd = cmd;
121 void llapi_clear_command_name(void)
123 liblustreapi_cmd = NULL;
126 static void error_callback_default(enum llapi_message_level level, int err,
127 const char *fmt, va_list ap)
129 if (liblustreapi_cmd != NULL)
130 fprintf(stderr, "%s %s: ", program_invocation_short_name,
133 fprintf(stderr, "%s: ", program_invocation_short_name);
134 vfprintf(stderr, fmt, ap);
135 if (level & LLAPI_MSG_NO_ERRNO)
136 fprintf(stderr, "\n");
138 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
141 static void info_callback_default(enum llapi_message_level level, int err,
142 const char *fmt, va_list ap)
145 if (liblustreapi_cmd != NULL) {
146 fprintf(stdout, "%s %s: ",
147 program_invocation_short_name,
150 fprintf(stdout, "%s: ", program_invocation_short_name);
153 vfprintf(stdout, fmt, ap);
156 static llapi_log_callback_t llapi_error_callback = error_callback_default;
157 static llapi_log_callback_t llapi_info_callback = info_callback_default;
160 /* llapi_error will preserve errno */
161 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
164 int tmp_errno = errno;
166 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
170 llapi_error_callback(level, abs(err), fmt, args);
175 /* llapi_printf will preserve errno */
176 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
179 int tmp_errno = errno;
181 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
185 llapi_info_callback(level, 0, fmt, args);
191 * Set a custom error logging function. Passing in NULL will reset the logging
192 * callback to its default value.
194 * This function returns the value of the old callback.
196 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
198 llapi_log_callback_t old = llapi_error_callback;
201 llapi_error_callback = cb;
203 llapi_error_callback = error_callback_default;
209 * Set a custom info logging function. Passing in NULL will reset the logging
210 * callback to its default value.
212 * This function returns the value of the old callback.
214 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
216 llapi_log_callback_t old = llapi_info_callback;
219 llapi_info_callback = cb;
221 llapi_info_callback = info_callback_default;
227 * size_units is to be initialized (or zeroed) by caller.
229 int llapi_parse_size(const char *optarg, unsigned long long *size,
230 unsigned long long *size_units, int bytes_spec)
233 char *argbuf = (char *)optarg;
234 unsigned long long frac = 0, frac_d = 1;
236 if (strncmp(optarg, "-", 1) == 0)
239 if (*size_units == 0)
242 *size = strtoull(argbuf, &end, 0);
243 if (end != NULL && *end == '.') {
247 frac = strtoull(argbuf, &end, 10);
248 /* count decimal places */
249 for (i = 0; i < (end - argbuf); i++)
254 if ((*end == 'b') && *(end + 1) == '\0' &&
255 (*size & (~0ULL << (64 - 9))) == 0 &&
257 *size_units = 1 << 9;
258 } else if ((*end == 'b') &&
259 *(end + 1) == '\0' &&
262 } else if ((*end == 'k' || *end == 'K') &&
263 *(end + 1) == '\0' &&
264 (*size & (~0ULL << (64 - 10))) == 0) {
265 *size_units = 1 << 10;
266 } else if ((*end == 'm' || *end == 'M') &&
267 *(end + 1) == '\0' &&
268 (*size & (~0ULL << (64 - 20))) == 0) {
269 *size_units = 1 << 20;
270 } else if ((*end == 'g' || *end == 'G') &&
271 *(end + 1) == '\0' &&
272 (*size & (~0ULL << (64 - 30))) == 0) {
273 *size_units = 1 << 30;
274 } else if ((*end == 't' || *end == 'T') &&
275 *(end + 1) == '\0' &&
276 (*size & (~0ULL << (64 - 40))) == 0) {
277 *size_units = 1ULL << 40;
278 } else if ((*end == 'p' || *end == 'P') &&
279 *(end + 1) == '\0' &&
280 (*size & (~0ULL << (64 - 50))) == 0) {
281 *size_units = 1ULL << 50;
282 } else if ((*end == 'e' || *end == 'E') &&
283 *(end + 1) == '\0' &&
284 (*size & (~0ULL << (64 - 60))) == 0) {
285 *size_units = 1ULL << 60;
290 *size = *size * *size_units + frac * *size_units / frac_d;
295 int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
297 struct obd_ioctl_data *overlay;
300 data->ioc_len = obd_ioctl_packlen(data);
301 data->ioc_version = OBD_IOCTL_VERSION;
303 if (*pbuf != NULL && data->ioc_len > max_len) {
304 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
305 "pbuf = %p, ioc_len = %u, max_len = %d\n",
306 *pbuf, data->ioc_len, max_len);
311 *pbuf = malloc(data->ioc_len);
316 overlay = (struct obd_ioctl_data *)*pbuf;
317 memcpy(*pbuf, data, sizeof(*data));
319 ptr = overlay->ioc_bulk;
320 if (data->ioc_inlbuf1) {
321 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
322 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
325 if (data->ioc_inlbuf2) {
326 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
327 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
330 if (data->ioc_inlbuf3) {
331 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
332 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
335 if (data->ioc_inlbuf4) {
336 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
337 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
343 int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
345 struct obd_ioctl_data *overlay;
351 overlay = (struct obd_ioctl_data *)pbuf;
353 /* Preserve the caller's buffer pointers */
354 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
355 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
356 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
357 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
359 memcpy(data, pbuf, sizeof(*data));
361 ptr = overlay->ioc_bulk;
362 if (data->ioc_inlbuf1) {
363 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
364 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
367 if (data->ioc_inlbuf2) {
368 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
369 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
372 if (data->ioc_inlbuf3) {
373 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
374 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
377 if (data->ioc_inlbuf4) {
378 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
379 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
385 /* XXX: llapi_xxx() functions return negative values upon failure */
387 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
388 int stripe_count, int stripe_pattern)
390 static int page_size;
393 if (page_size == 0) {
394 /* 64 KB is the largest common page size (on ia64/PPC/ARM),
395 * but check the local page size just in case. The page_size
396 * will not change for the lifetime of this process at least.
398 page_size = LOV_MIN_STRIPE_SIZE;
399 if (getpagesize() > page_size) {
400 page_size = getpagesize();
401 llapi_err_noerrno(LLAPI_MSG_WARN,
402 "warning: page size (%u) larger than expected (%u)",
403 page_size, LOV_MIN_STRIPE_SIZE);
406 if (!llapi_stripe_size_is_aligned(stripe_size)) {
408 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
409 "must be an even multiple of %d bytes",
410 (unsigned long long)stripe_size, page_size);
413 if (!llapi_stripe_index_is_valid(stripe_offset)) {
415 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
419 if (!llapi_stripe_count_is_valid(stripe_count)) {
421 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
425 if (llapi_stripe_size_is_too_big(stripe_size)) {
427 llapi_error(LLAPI_MSG_ERROR, rc,
428 "error: stripe size '%llu' over 4GB limit",
429 (unsigned long long)stripe_size);
438 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
443 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
445 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
449 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
451 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
456 if (!llapi_dir_hash_type_is_valid(hash_type)) {
458 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
466 * Trim a trailing newline from a string, if it exists.
468 int llapi_chomp_string(char *buf)
484 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
486 static int get_param_lov(const char *path, const char *param,
487 char *buf, size_t buf_size)
489 struct obd_uuid uuid;
492 rc = llapi_file_get_lov_uuid(path, &uuid);
496 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
501 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
503 static int get_param_lmv(const char *path, const char *param,
504 char *buf, size_t buf_size)
506 struct obd_uuid uuid;
509 rc = llapi_file_get_lmv_uuid(path, &uuid);
513 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
517 static int get_mds_md_size(const char *path)
519 char buf[PATH_MAX], inst[PATH_MAX];
520 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
523 rc = llapi_getname(path, inst, sizeof(inst));
527 /* Get the max ea size from llite parameters. */
528 rc = get_lustre_param_value("llite", inst, FILTER_BY_EXACT,
529 "max_easize", buf, sizeof(buf));
535 return rc > 0 ? rc : md_size;
538 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
540 return get_param_lmv(path, "uuid", buf, bufsize);
544 * Open a Lustre file.
546 * \param name the name of the file to be opened
547 * \param flags access mode, see flags in open(2)
548 * \param mode permission of the file if it is created, see mode in open(2)
549 * \param param stripe pattern of the newly created file
551 * \retval file descriptor of opened file
552 * \retval negative errno on failure
554 int llapi_file_open_param(const char *name, int flags, mode_t mode,
555 const struct llapi_stripe_param *param)
557 char fsname[MAX_OBD_NAME + 1] = { 0 };
558 char *pool_name = param->lsp_pool;
559 struct lov_user_md *lum = NULL;
560 size_t lum_size = sizeof(*lum);
563 /* Make sure we are on a Lustre file system */
564 rc = llapi_search_fsname(name, fsname);
566 llapi_error(LLAPI_MSG_ERROR, rc,
567 "'%s' is not on a Lustre filesystem",
572 /* Check if the stripe pattern is sane. */
573 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
574 param->lsp_stripe_offset,
575 param->lsp_stripe_count,
576 param->lsp_stripe_pattern);
580 /* Make sure we have a good pool */
581 if (pool_name != NULL) {
582 /* in case user gives the full pool name <fsname>.<poolname>,
583 * strip the fsname */
584 char *ptr = strchr(pool_name, '.');
587 if (strcmp(pool_name, fsname) != 0) {
589 llapi_err_noerrno(LLAPI_MSG_ERROR,
590 "Pool '%s' is not on filesystem '%s'",
597 /* Make sure the pool exists and is non-empty */
598 rc = llapi_search_ost(fsname, pool_name, NULL);
600 char *err = rc == 0 ? "has no OSTs" : "does not exist";
602 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
603 fsname, pool_name, err);
607 lum_size = sizeof(struct lov_user_md_v3);
610 /* sanity check of target list */
611 if (param->lsp_is_specific) {
612 char ostname[MAX_OBD_NAME + 64];
616 for (i = 0; i < param->lsp_stripe_count; i++) {
617 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
618 fsname, param->lsp_osts[i]);
619 rc = llapi_search_ost(fsname, pool_name, ostname);
624 llapi_error(LLAPI_MSG_ERROR, rc,
625 "%s: cannot find OST %s in %s",
632 /* Make sure stripe offset is in OST list. */
633 if (param->lsp_osts[i] == param->lsp_stripe_offset)
637 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
638 "%s: stripe offset '%d' is not in the "
640 __func__, param->lsp_stripe_offset);
644 lum_size = lov_user_md_size(param->lsp_stripe_count,
645 LOV_USER_MAGIC_SPECIFIC);
648 lum = calloc(1, lum_size);
653 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
655 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
656 flags = O_DIRECTORY | O_RDONLY;
663 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
668 /* Initialize IOCTL striping pattern structure */
669 lum->lmm_magic = LOV_USER_MAGIC_V1;
670 lum->lmm_pattern = param->lsp_stripe_pattern;
671 lum->lmm_stripe_size = param->lsp_stripe_size;
672 lum->lmm_stripe_count = param->lsp_stripe_count;
673 lum->lmm_stripe_offset = param->lsp_stripe_offset;
674 if (pool_name != NULL) {
675 struct lov_user_md_v3 *lumv3 = (void *)lum;
677 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
678 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
680 if (param->lsp_is_specific) {
681 struct lov_user_md_v3 *lumv3 = (void *)lum;
684 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
685 if (pool_name == NULL) {
686 /* LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
687 * OST list, therefore if pool is not specified we have
688 * to pack a null pool name for placeholder. */
689 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
692 for (i = 0; i < param->lsp_stripe_count; i++)
693 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
696 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
697 char *errmsg = "stripe already set";
700 if (errno != EEXIST && errno != EALREADY)
701 errmsg = strerror(errno);
703 llapi_err_noerrno(LLAPI_MSG_ERROR,
704 "setstripe error for '%s': %s", name, errmsg);
715 int llapi_file_open_pool(const char *name, int flags, int mode,
716 unsigned long long stripe_size, int stripe_offset,
717 int stripe_count, int stripe_pattern, char *pool_name)
719 const struct llapi_stripe_param param = {
720 .lsp_stripe_size = stripe_size,
721 .lsp_stripe_count = stripe_count,
722 .lsp_stripe_pattern = stripe_pattern,
723 .lsp_stripe_offset = stripe_offset,
724 .lsp_pool = pool_name
726 return llapi_file_open_param(name, flags, mode, ¶m);
729 int llapi_file_open(const char *name, int flags, int mode,
730 unsigned long long stripe_size, int stripe_offset,
731 int stripe_count, int stripe_pattern)
733 return llapi_file_open_pool(name, flags, mode, stripe_size,
734 stripe_offset, stripe_count,
735 stripe_pattern, NULL);
738 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
739 __u32 flags, char *foreign_lov)
742 struct lov_foreign_md *lfm;
745 if (foreign_lov == NULL) {
747 llapi_error(LLAPI_MSG_ERROR, rc,
748 "foreign LOV EA content must be provided");
752 len = strlen(foreign_lov);
753 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
756 llapi_error(LLAPI_MSG_ERROR, rc,
757 "foreign LOV EA size %zu (must be 0 < len < %zu)",
758 len, XATTR_SIZE_MAX -
759 offsetof(struct lov_foreign_md, lfm_value));
763 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
766 llapi_error(LLAPI_MSG_ERROR, rc,
767 "failed to allocate lov_foreign_md");
771 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
774 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
778 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
779 lfm->lfm_length = len;
780 lfm->lfm_type = type;
781 lfm->lfm_flags = flags;
782 memcpy(lfm->lfm_value, foreign_lov, len);
784 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
785 char *errmsg = "stripe already set";
789 errmsg = "not on a Lustre filesystem";
790 else if (errno == EEXIST || errno == EALREADY)
791 errmsg = "stripe already set";
793 errmsg = strerror(errno);
795 llapi_err_noerrno(LLAPI_MSG_ERROR,
796 "setstripe error for '%s': %s", name, errmsg);
812 int llapi_file_create(const char *name, unsigned long long stripe_size,
813 int stripe_offset, int stripe_count, int stripe_pattern)
817 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
818 stripe_offset, stripe_count, stripe_pattern,
827 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
828 int stripe_offset, int stripe_count,
829 int stripe_pattern, char *pool_name)
833 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
834 stripe_offset, stripe_count, stripe_pattern,
843 static int verify_dir_param(const char *name,
844 const struct llapi_stripe_param *param)
846 char fsname[MAX_OBD_NAME + 1] = { 0 };
847 char *pool_name = param->lsp_pool;
850 /* Make sure we are on a Lustre file system */
851 rc = llapi_search_fsname(name, fsname);
853 llapi_error(LLAPI_MSG_ERROR, rc,
854 "'%s' is not on a Lustre filesystem",
859 /* Check if the stripe pattern is sane. */
860 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
861 param->lsp_stripe_count,
862 param->lsp_stripe_pattern);
866 /* Make sure we have a good pool */
867 if (pool_name != NULL) {
868 /* in case user gives the full pool name <fsname>.<poolname>,
869 * strip the fsname */
870 char *ptr = strchr(pool_name, '.');
874 if (strcmp(pool_name, fsname) != 0) {
876 llapi_err_noerrno(LLAPI_MSG_ERROR,
877 "Pool '%s' is not on filesystem '%s'",
884 /* Make sure the pool exists and is non-empty */
885 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
887 char *err = rc == 0 ? "has no OSTs" : "does not exist";
889 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
890 fsname, pool_name, err);
895 /* sanity check of target list */
896 if (param->lsp_is_specific) {
897 char mdtname[MAX_OBD_NAME + 64];
901 for (i = 0; i < param->lsp_stripe_count; i++) {
902 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
903 fsname, param->lsp_tgts[i]);
904 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
909 llapi_error(LLAPI_MSG_ERROR, rc,
910 "%s: cannot find MDT %s in %s",
917 /* Make sure stripe offset is in MDT list. */
918 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
922 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
923 "%s: stripe offset '%d' is not in the "
925 __func__, param->lsp_stripe_offset);
933 static inline void param2lmu(struct lmv_user_md *lmu,
934 const struct llapi_stripe_param *param)
936 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
938 lmu->lum_stripe_count = param->lsp_stripe_count;
939 lmu->lum_stripe_offset = param->lsp_stripe_offset;
940 lmu->lum_hash_type = param->lsp_stripe_pattern;
941 if (param->lsp_pool != NULL)
942 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
943 if (param->lsp_is_specific) {
946 for (i = 0; i < param->lsp_stripe_count; i++)
947 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
951 int llapi_dir_set_default_lmv(const char *name,
952 const struct llapi_stripe_param *param)
954 struct lmv_user_md lmu = { 0 };
958 rc = verify_dir_param(name, param);
962 /* TODO: default lmv doesn't support specific targets yet */
963 if (param->lsp_is_specific)
966 param2lmu(&lmu, param);
968 fd = open(name, O_DIRECTORY | O_RDONLY);
971 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
975 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
977 char *errmsg = "stripe already set";
979 if (errno != EEXIST && errno != EALREADY)
980 errmsg = strerror(errno);
982 llapi_err_noerrno(LLAPI_MSG_ERROR,
983 "default dirstripe error on '%s': %s",
990 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
991 int stripe_count, int stripe_pattern,
992 const char *pool_name)
994 const struct llapi_stripe_param param = {
995 .lsp_stripe_count = stripe_count,
996 .lsp_stripe_offset = stripe_offset,
997 .lsp_stripe_pattern = stripe_pattern,
998 .lsp_pool = (char *)pool_name
1001 return llapi_dir_set_default_lmv(name, ¶m);
1005 * Create a Lustre directory.
1007 * \param name the name of the directory to be created
1008 * \param mode permission of the file if it is created, see mode in open(2)
1009 * \param param stripe pattern of the newly created directory
1011 * \retval 0 on success
1012 * \retval negative errno on failure
1014 int llapi_dir_create(const char *name, mode_t mode,
1015 const struct llapi_stripe_param *param)
1017 struct lmv_user_md *lmu = NULL;
1019 struct obd_ioctl_data data = { 0 };
1022 char *dirpath = NULL;
1023 char *namepath = NULL;
1028 rc = verify_dir_param(name, param);
1032 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1033 param->lsp_is_specific ?
1034 LMV_USER_MAGIC_SPECIFIC :
1037 lmu = calloc(1, lmu_size);
1041 dirpath = strdup(name);
1047 namepath = strdup(name);
1054 param2lmu(lmu, param);
1056 filename = basename(namepath);
1057 dir = dirname(dirpath);
1059 data.ioc_inlbuf1 = (char *)filename;
1060 data.ioc_inllen1 = strlen(filename) + 1;
1061 data.ioc_inlbuf2 = (char *)lmu;
1062 data.ioc_inllen2 = lmu_size;
1063 data.ioc_type = mode;
1064 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1066 llapi_error(LLAPI_MSG_ERROR, rc,
1067 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1072 fd = open(dir, O_DIRECTORY | O_RDONLY);
1075 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1079 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1080 char *errmsg = "stripe already set";
1083 if (errno != EEXIST && errno != EALREADY)
1084 errmsg = strerror(errno);
1086 llapi_err_noerrno(LLAPI_MSG_ERROR,
1087 "dirstripe error on '%s': %s", name, errmsg);
1098 * Create a foreign directory.
1100 * \param name the name of the directory to be created
1101 * \param mode permission of the file if it is created, see mode in open(2)
1102 * \param type foreign type to be set in LMV EA
1103 * \param flags foreign flags to be set in LMV EA
1104 * \param value foreign pattern to be set in LMV EA
1106 * \retval 0 on success
1107 * \retval negative errno on failure
1109 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1110 __u32 flags, const char *value)
1112 struct lmv_foreign_md *lfm = NULL;
1113 size_t lfm_size, len;
1114 struct obd_ioctl_data data = { 0 };
1117 char *dirpath = NULL;
1118 char *namepath = NULL;
1123 len = strlen(value);
1124 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1127 llapi_error(LLAPI_MSG_ERROR, rc, "invalid LOV EA length %zu "
1128 "(must be 0 < len < %zu)\n", len,
1130 offsetof(struct lmv_foreign_md, lfm_value));
1133 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1134 lfm = calloc(1, lfm_size);
1138 dirpath = strdup(name);
1144 namepath = strdup(name);
1151 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1152 lfm->lfm_length = len;
1153 lfm->lfm_type = type;
1154 lfm->lfm_flags = flags;
1155 memcpy(lfm->lfm_value, value, len);
1157 filename = basename(namepath);
1158 dir = dirname(dirpath);
1160 data.ioc_inlbuf1 = (char *)filename;
1161 data.ioc_inllen1 = strlen(filename) + 1;
1162 data.ioc_inlbuf2 = (char *)lfm;
1163 data.ioc_inllen2 = lfm_size;
1164 data.ioc_type = mode;
1165 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1167 llapi_error(LLAPI_MSG_ERROR, rc,
1168 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1173 fd = open(dir, O_DIRECTORY | O_RDONLY);
1176 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1180 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1181 char *errmsg = "stripe already set";
1184 if (errno != EEXIST && errno != EALREADY)
1185 errmsg = strerror(errno);
1187 llapi_err_noerrno(LLAPI_MSG_ERROR,
1188 "dirstripe error on '%s': %s", name, errmsg);
1198 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1199 int stripe_count, int stripe_pattern,
1200 const char *pool_name)
1202 const struct llapi_stripe_param param = {
1203 .lsp_stripe_count = stripe_count,
1204 .lsp_stripe_offset = stripe_offset,
1205 .lsp_stripe_pattern = stripe_pattern,
1206 .lsp_pool = (char *)pool_name
1209 return llapi_dir_create(name, mode, ¶m);
1212 int llapi_direntry_remove(char *dname)
1214 char *dirpath = NULL;
1215 char *namepath = NULL;
1221 dirpath = strdup(dname);
1222 namepath = strdup(dname);
1223 if (!dirpath || !namepath)
1226 filename = basename(namepath);
1228 dir = dirname(dirpath);
1230 fd = open(dir, O_DIRECTORY | O_RDONLY);
1233 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
1238 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
1239 char *errmsg = strerror(errno);
1240 llapi_err_noerrno(LLAPI_MSG_ERROR,
1241 "error on ioctl %#jx for '%s' (%d): %s",
1242 (uintmax_t)LL_IOC_LMV_SETSTRIPE, filename,
1254 * Find the fsname, the full path, and/or an open fd.
1255 * Either the fsname or path must not be NULL
1257 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1260 char buf[PATH_MAX], mntdir[PATH_MAX];
1261 char *ptr, *ptr_end;
1263 int idx = 0, len = 0, mntlen, fd;
1265 int fsnamelen, mountlen;
1267 /* get the mount point */
1268 fp = setmntent(PROC_MOUNTS, "r");
1271 llapi_error(LLAPI_MSG_ERROR, rc,
1272 "cannot retrieve filesystem mount point");
1276 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1279 if (!llapi_is_lustre_mnt(&mnt))
1282 if ((want & WANT_INDEX) && (idx++ != index))
1285 mntlen = strlen(mnt.mnt_dir);
1286 ptr = strchr(mnt.mnt_fsname, '/');
1287 while (ptr && *ptr == '/')
1289 /* thanks to the call to llapi_is_lustre_mnt() above,
1290 * we are sure that mnt.mnt_fsname contains ":/",
1291 * so ptr should never be NULL */
1295 while (*ptr_end != '/' && *ptr_end != '\0')
1298 /* Check the fsname for a match, if given */
1299 mountlen = ptr_end - ptr;
1300 if (!(want & WANT_FSNAME) && fsname != NULL &&
1301 (fsnamelen = strlen(fsname)) > 0 &&
1302 (fsnamelen != mountlen ||
1303 (strncmp(ptr, fsname, mountlen) != 0)))
1306 /* If the path isn't set return the first one we find */
1307 if (path == NULL || strlen(path) == 0) {
1308 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1309 mntdir[sizeof(mntdir) - 1] = '\0';
1310 if ((want & WANT_FSNAME) && fsname != NULL) {
1311 strncpy(fsname, ptr, mountlen);
1312 fsname[mountlen] = '\0';
1316 /* Otherwise find the longest matching path */
1317 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1318 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1319 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1320 mntdir[sizeof(mntdir) - 1] = '\0';
1322 if ((want & WANT_FSNAME) && fsname != NULL) {
1323 strncpy(fsname, ptr, mountlen);
1324 fsname[mountlen] = '\0';
1333 if ((want & WANT_PATH) && path != NULL) {
1334 strncpy(path, mntdir, PATH_MAX);
1335 path[strlen(mntdir)] = '\0';
1337 if (want & WANT_FD) {
1338 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1341 llapi_error(LLAPI_MSG_ERROR, rc,
1342 "cannot open '%s': %s", mntdir,
1349 } else if (want & WANT_ERROR)
1350 llapi_err_noerrno(LLAPI_MSG_ERROR,
1351 "'%s' not on a mounted Lustre filesystem",
1352 (want & WANT_PATH) ? fsname : path);
1357 * search lustre mounts
1359 * Calling this function will return to the user the mount point, mntdir, and
1360 * the file system name, fsname, if the user passed a buffer to this routine.
1362 * The user inputs are pathname and index. If the pathname is supplied then
1363 * the value of the index will be ignored. The pathname will return data if
1364 * the pathname is located on a lustre mount. Index is used to pick which
1365 * mount point you want in the case of multiple mounted lustre file systems.
1366 * See function lfs_osts in lfs.c for an example of the index use.
1368 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1371 int want = WANT_PATH, idx = -1;
1373 if (!pathname || pathname[0] == '\0') {
1377 strcpy(mntdir, pathname);
1380 want |= WANT_FSNAME;
1381 return get_root_path(want, fsname, NULL, mntdir, idx);
1384 /* Given a path, find the corresponding Lustre fsname */
1385 int llapi_search_fsname(const char *pathname, char *fsname)
1390 path = realpath(pathname, NULL);
1392 char tmp[PATH_MAX - 1];
1398 if (pathname[0] != '/') {
1399 /* Need an absolute path, but realpath() only works for
1400 * pathnames that actually exist. We go through the
1401 * extra hurdle of dirname(getcwd() + pathname) in
1402 * case the relative pathname contains ".." in it. */
1403 char realpath[PATH_MAX - 1];
1405 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1407 llapi_error(LLAPI_MSG_ERROR, rc,
1408 "cannot get current working directory");
1412 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1413 if (rc >= sizeof(tmp)) {
1415 llapi_error(LLAPI_MSG_ERROR, rc,
1416 "invalid parent path '%s'",
1422 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1423 if (rc >= sizeof(buf)) {
1425 llapi_error(LLAPI_MSG_ERROR, rc,
1426 "invalid path '%s'", pathname);
1429 path = realpath(buf, NULL);
1431 ptr = strrchr(buf, '/');
1433 llapi_error(LLAPI_MSG_ERROR |
1434 LLAPI_MSG_NO_ERRNO, 0,
1435 "cannot resolve path '%s'",
1440 path = realpath(buf, NULL);
1443 llapi_error(LLAPI_MSG_ERROR, rc,
1444 "cannot resolve path '%s'",
1450 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1455 int llapi_search_rootpath(char *pathname, const char *fsname)
1457 /* pathname can be used as an argument by get_root_path(),
1458 * clear it for safety */
1460 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1463 int llapi_getname(const char *path, char *buf, size_t size)
1465 struct obd_uuid uuid_buf;
1466 char *uuid = uuid_buf.uuid;
1468 int rc, len, fsname_len;
1470 memset(&uuid_buf, 0, sizeof(uuid_buf));
1471 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1476 * We want to turn testfs-clilov-ffff88002738bc00 into
1477 * testfs-ffff88002738bc00 in a portable way that doesn't depend
1478 * on what is after "-clilov-" as it may change in the future.
1479 * Unfortunately, the "fsname" part may contain a dash, so we
1480 * can't just skip to the first dash, and the "instance" may be a
1481 * UUID in the future, so we can't necessarily go to the last dash.
1483 cfg_instance = strstr(uuid, "-clilov-");
1487 fsname_len = cfg_instance - uuid;
1488 cfg_instance += strlen("-clilov-");
1489 len = snprintf(buf, size, "%.*s-%s", fsname_len, uuid, cfg_instance);
1498 * Get the list of pool members.
1499 * \param poolname string of format \<fsname\>.\<poolname\>
1500 * \param members caller-allocated array of char*
1501 * \param list_size size of the members array
1502 * \param buffer caller-allocated buffer for storing OST names
1503 * \param buffer_size size of the buffer
1505 * \return number of members retrieved for this pool
1506 * \retval -error failure
1508 int llapi_get_poolmembers(const char *poolname, char **members,
1509 int list_size, char *buffer, int buffer_size)
1511 char fsname[PATH_MAX];
1520 /* name is FSNAME.POOLNAME */
1521 if (strlen(poolname) >= sizeof(fsname))
1524 snprintf(fsname, sizeof(fsname), "%s", poolname);
1525 pool = strchr(fsname, '.');
1532 rc = poolpath(&pathname, fsname, NULL);
1534 llapi_error(LLAPI_MSG_ERROR, rc,
1535 "Lustre filesystem '%s' not found",
1540 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1541 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1542 cfs_free_param_data(&pathname);
1543 if (rc >= sizeof(buf))
1545 fd = fopen(buf, "r");
1548 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1553 while (fgets(buf, sizeof(buf), fd) != NULL) {
1554 if (nb_entries >= list_size) {
1558 buf[sizeof(buf) - 1] = '\0';
1560 tmp = strchr(buf, '\n');
1563 if (used + strlen(buf) + 1 > buffer_size) {
1568 strcpy(buffer + used, buf);
1569 members[nb_entries] = buffer + used;
1570 used += strlen(buf) + 1;
1580 * Get the list of pools in a filesystem.
1581 * \param name filesystem name or path
1582 * \param poollist caller-allocated array of char*
1583 * \param list_size size of the poollist array
1584 * \param buffer caller-allocated buffer for storing pool names
1585 * \param buffer_size size of the buffer
1587 * \return number of pools retrieved for this filesystem
1588 * \retval -error failure
1590 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1591 char *buffer, int buffer_size)
1597 struct dirent *pool;
1599 unsigned int nb_entries = 0;
1600 unsigned int used = 0;
1603 /* initialize output array */
1604 for (i = 0; i < list_size; i++)
1607 /* is name a pathname ? */
1608 ptr = strchr(name, '/');
1610 char fsname_buf[MAXNAMLEN];
1612 /* We will need fsname for printing later */
1613 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1617 ptr = strrchr(fsname_buf, '-');
1621 fsname = strdup(fsname_buf);
1625 /* name is FSNAME */
1626 fsname = strdup(name);
1631 rc = poolpath(&pathname, fsname, NULL);
1633 llapi_error(LLAPI_MSG_ERROR, rc,
1634 "Lustre filesystem '%s' not found", name);
1638 dir = opendir(pathname.gl_pathv[0]);
1641 llapi_error(LLAPI_MSG_ERROR, rc,
1642 "Could not open pool list for '%s'",
1649 pool = readdir(dir);
1655 /* ignore . and .. */
1656 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1659 /* check output bounds */
1660 if (nb_entries >= list_size) {
1662 goto free_dir_no_msg;
1665 /* +2 for '.' and final '\0' */
1666 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1669 goto free_dir_no_msg;
1672 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1673 poollist[nb_entries] = buffer + used;
1674 used += strlen(pool->d_name) + strlen(fsname) + 2;
1680 llapi_error(LLAPI_MSG_ERROR, rc,
1681 "Error reading pool list for '%s'", name);
1683 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1688 cfs_free_param_data(&pathname);
1691 return rc != 0 ? rc : nb_entries;
1694 /* wrapper for lfs.c and obd.c */
1695 int llapi_poollist(const char *name)
1697 /* list of pool names (assume that pool count is smaller
1699 char **list, *buffer = NULL, *fsname = (char *)name;
1700 char *poolname = NULL, *tmp = NULL, data[16];
1701 enum param_filter type = FILTER_BY_PATH;
1702 int obdcount, bufsize, rc, nb, i;
1707 if (name[0] != '/') {
1708 fsname = strdup(name);
1712 poolname = strchr(fsname, '.');
1715 type = FILTER_BY_FS_NAME;
1718 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1719 data, sizeof(data));
1722 obdcount = atoi(data);
1724 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1725 * and also an array to store the pointers for all that
1726 * allocated space. */
1728 bufsize = sizeof(struct obd_uuid) * obdcount;
1729 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1730 if (buffer == NULL) {
1734 list = (char **) (buffer + bufsize);
1737 /* name is a path or fsname */
1738 nb = llapi_get_poollist(name, list, obdcount,
1741 /* name is a pool name (<fsname>.<poolname>) */
1742 nb = llapi_get_poolmembers(name, list, obdcount,
1746 if (nb == -EOVERFLOW) {
1749 goto retry_get_pools;
1752 for (i = 0; i < nb; i++)
1753 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1754 rc = (nb < 0 ? nb : 0);
1758 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1763 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1764 void *data, struct dirent64 *de);
1766 #define OBD_NOT_FOUND (-1)
1768 static void find_param_fini(struct find_param *param)
1770 if (param->fp_migrate)
1773 if (param->fp_obd_indexes) {
1774 free(param->fp_obd_indexes);
1775 param->fp_obd_indexes = NULL;
1778 if (param->fp_lmd) {
1779 free(param->fp_lmd);
1780 param->fp_lmd = NULL;
1783 if (param->fp_lmv_md) {
1784 free(param->fp_lmv_md);
1785 param->fp_lmv_md = NULL;
1789 static int common_param_init(struct find_param *param, char *path)
1791 int lum_size = get_mds_md_size(path);
1796 /* migrate has fp_lmv_md initialized outside */
1797 if (param->fp_migrate)
1800 if (lum_size < PATH_MAX + 1)
1801 lum_size = PATH_MAX + 1;
1803 param->fp_lum_size = lum_size;
1804 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1806 if (param->fp_lmd == NULL) {
1807 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1808 "error: allocate %zu bytes for layout failed",
1809 sizeof(lstat_t) + param->fp_lum_size);
1813 param->fp_lmv_stripe_count = 256;
1814 param->fp_lmv_md = calloc(1,
1815 lmv_user_md_size(param->fp_lmv_stripe_count,
1816 LMV_USER_MAGIC_SPECIFIC));
1817 if (param->fp_lmv_md == NULL) {
1818 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1819 "error: allocation of %d bytes for ioctl",
1820 lmv_user_md_size(param->fp_lmv_stripe_count,
1821 LMV_USER_MAGIC_SPECIFIC));
1822 find_param_fini(param);
1826 param->fp_got_uuids = 0;
1827 param->fp_obd_indexes = NULL;
1828 param->fp_obd_index = OBD_NOT_FOUND;
1829 param->fp_mdt_index = OBD_NOT_FOUND;
1833 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1834 struct dirent64 *de)
1836 struct find_param *param = data;
1842 /* set errno upon failure */
1843 static DIR *opendir_parent(const char *path)
1849 path_copy = strdup(path);
1850 if (path_copy == NULL)
1853 parent_path = dirname(path_copy);
1854 parent = opendir(parent_path);
1860 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1865 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1866 if (param->fp_get_default_lmv)
1867 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1869 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1871 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1872 if (errno == E2BIG && ret != 0) {
1876 /* if foreign LMV case, fake stripes number */
1877 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
1878 struct lmv_foreign_md *lfm;
1880 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1881 if (lfm->lfm_length < XATTR_SIZE_MAX -
1882 offsetof(typeof(*lfm), lfm_value)) {
1883 uint32_t size = lfm->lfm_length +
1884 offsetof(typeof(*lfm), lfm_value);
1886 stripe_count = lmv_foreign_to_md_stripes(size);
1888 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1889 "error: invalid %d foreign size "
1890 "returned from ioctl",
1895 stripe_count = param->fp_lmv_md->lum_stripe_count;
1897 if (stripe_count <= param->fp_lmv_stripe_count)
1900 free(param->fp_lmv_md);
1901 param->fp_lmv_stripe_count = stripe_count;
1902 lmv_size = lmv_user_md_size(stripe_count,
1903 LMV_USER_MAGIC_SPECIFIC);
1904 param->fp_lmv_md = malloc(lmv_size);
1905 if (param->fp_lmv_md == NULL) {
1906 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1907 "error: allocation of %d bytes for ioctl",
1908 lmv_user_md_size(param->fp_lmv_stripe_count,
1909 LMV_USER_MAGIC_SPECIFIC));
1917 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1920 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
1921 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
1922 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
1923 lmd_v2->lmd_stx.stx_uid = st->st_uid;
1924 lmd_v2->lmd_stx.stx_gid = st->st_gid;
1925 lmd_v2->lmd_stx.stx_mode = st->st_mode;
1926 lmd_v2->lmd_stx.stx_ino = st->st_ino;
1927 lmd_v2->lmd_stx.stx_size = st->st_size;
1928 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
1929 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
1930 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
1931 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
1932 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
1933 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
1934 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
1935 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
1936 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
1938 lmd_v2->lmd_flags = 0;
1940 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1942 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
1943 if (lmd_v2->lmd_stx.stx_size)
1944 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
1945 if (lmd_v2->lmd_stx.stx_blocks)
1946 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
1948 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1949 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
1950 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
1955 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
1957 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
1958 struct lov_user_mds_data *lmd_v2 = lmdbuf;
1962 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
1966 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
1969 st = lmd_v1->lmd_st;
1970 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
1971 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
1972 convert_lmd_statx(lmd_v2, &st, false);
1973 lmd_v2->lmd_lmmsize = 0;
1974 lmd_v2->lmd_padding = 0;
1979 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
1980 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1982 struct lov_user_mds_data *lmd = lmdbuf;
1983 static bool use_old_ioctl;
1987 if (parent_fd < 0 && dir_fd < 0)
1989 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1993 /* LL_IOC_MDC_GETINFO operates on the current directory inode
1994 * and returns struct lov_user_mds_data, while
1995 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
1997 if (type == GET_LMD_INFO)
1998 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_OLD :
2001 cmd = LL_IOC_LOV_GETSTRIPE;
2004 ret = ioctl(dir_fd, cmd, lmdbuf);
2005 if (ret < 0 && errno == ENOTTY && cmd == LL_IOC_MDC_GETINFO) {
2006 cmd = LL_IOC_MDC_GETINFO_OLD;
2007 use_old_ioctl = true;
2011 if (cmd == LL_IOC_MDC_GETINFO_OLD && !ret)
2012 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2013 } else if (parent_fd >= 0) {
2014 const char *fname = strrchr(path, '/');
2016 /* IOC_MDC_GETFILEINFO takes as input the filename (relative to
2017 * the parent directory) and returns struct lov_user_mds_data,
2018 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2020 * This avoids opening, locking, and closing each file on the
2021 * client if that is not needed. Multiple of these ioctl() can
2022 * be done on the parent dir with a single open for all
2023 * files in that directory, and it also doesn't pollute the
2024 * client dcache with millions of dentries when traversing
2025 * a large filesystem.
2027 fname = (fname == NULL ? path : fname + 1);
2029 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2032 else if (ret >= lmdlen || ret++ == 0)
2035 if (type == GET_LMD_INFO)
2036 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_OLD :
2037 IOC_MDC_GETFILEINFO;
2039 cmd = IOC_MDC_GETFILESTRIPE;
2042 ret = ioctl(parent_fd, cmd, lmdbuf);
2043 if (ret < 0 && errno == ENOTTY &&
2044 cmd == IOC_MDC_GETFILEINFO) {
2045 cmd = IOC_MDC_GETFILEINFO_OLD;
2046 use_old_ioctl = true;
2047 goto retry_getfileinfo;
2050 if (cmd == IOC_MDC_GETFILEINFO_OLD && !ret)
2051 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2055 if (ret && type == GET_LMD_INFO) {
2056 if (errno == ENOTTY) {
2059 /* ioctl is not supported, it is not a lustre fs.
2060 * Do the regular lstat(2) instead.
2062 ret = lstat_f(path, &st);
2065 llapi_error(LLAPI_MSG_ERROR, ret,
2066 "error: %s: lstat failed for %s",
2070 convert_lmd_statx(lmd, &st, true);
2071 /* It may be wrong to set use_old_ioctl with true as
2072 * the file is not a lustre fs. So reset it with false
2075 use_old_ioctl = false;
2076 } else if (errno == ENOENT) {
2078 llapi_error(LLAPI_MSG_WARN, ret,
2079 "warning: %s does not exist", path);
2080 } else if (errno != EISDIR && errno != ENODATA) {
2082 llapi_error(LLAPI_MSG_ERROR, ret,
2083 "%s ioctl failed for %s.",
2084 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2085 "IOC_MDC_GETFILEINFO", path);
2092 static int get_lmd_info(char *path, DIR *parent, DIR *dir, void *lmdbuf,
2093 int lmdlen, enum get_lmd_info_type type)
2099 parent_fd = dirfd(parent);
2101 dir_fd = dirfd(dir);
2103 return get_lmd_info_fd(path, parent_fd, dir_fd, lmdbuf, lmdlen, type);
2106 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
2107 semantic_func_t sem_init,
2108 semantic_func_t sem_fini, void *data,
2109 struct dirent64 *de)
2111 struct find_param *param = (struct find_param *)data;
2112 struct dirent64 *dent;
2120 if (!d && errno != ENOTDIR) {
2122 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2125 } else if (!d && !parent) {
2126 /* ENOTDIR. Open the parent dir. */
2127 p = opendir_parent(path);
2134 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
2140 while ((dent = readdir64(d)) != NULL) {
2143 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2147 if ((len + dent->d_reclen + 2) > size) {
2148 llapi_err_noerrno(LLAPI_MSG_ERROR,
2149 "error: %s: string buffer too small",
2154 strcat(path, dent->d_name);
2156 if (dent->d_type == DT_UNKNOWN) {
2157 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
2159 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
2160 param->fp_lum_size, GET_LMD_INFO);
2162 dent->d_type = IFTODT(stx->stx_mode);
2169 switch (dent->d_type) {
2171 llapi_err_noerrno(LLAPI_MSG_ERROR,
2172 "error: %s: '%s' is UNKNOWN type %d",
2173 __func__, dent->d_name, dent->d_type);
2176 rc = llapi_semantic_traverse(path, size, d, sem_init,
2177 sem_fini, data, dent);
2178 if (rc != 0 && ret == 0)
2184 rc = sem_init(path, d, NULL, data, dent);
2185 if (rc < 0 && ret == 0) {
2190 if (sem_fini && rc == 0)
2191 sem_fini(path, d, NULL, data, dent);
2199 sem_fini(path, parent, &d, data, de);
2208 static int param_callback(char *path, semantic_func_t sem_init,
2209 semantic_func_t sem_fini, struct find_param *param)
2211 int ret, len = strlen(path);
2214 if (len > PATH_MAX) {
2216 llapi_error(LLAPI_MSG_ERROR, ret,
2217 "Path name '%s' is too long", path);
2221 buf = (char *)malloc(PATH_MAX + 1);
2225 snprintf(buf, PATH_MAX + 1, "%s", path);
2226 ret = common_param_init(param, buf);
2230 param->fp_depth = 0;
2232 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
2233 sem_fini, param, NULL);
2235 find_param_fini(param);
2237 return ret < 0 ? ret : 0;
2240 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2242 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
2246 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2251 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2253 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2256 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2261 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2265 fd = open(path, O_RDONLY | O_NONBLOCK);
2268 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
2272 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2278 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2282 fd = open(path, O_RDONLY | O_NONBLOCK);
2285 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2289 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2301 * If uuidp is NULL, return the number of available obd uuids.
2302 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2303 * there are more OSTs than allocated to uuidp, then an error is returned with
2304 * the ost_count set to number of available obd uuids.
2306 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2307 int *ost_count, enum tgt_type type)
2309 char buf[PATH_MAX], format[32];
2310 int rc = 0, index = 0;
2311 struct obd_uuid name;
2315 /* Get the lov name */
2316 if (type == LOV_TYPE)
2317 rc = llapi_file_fget_lov_uuid(fd, &name);
2319 rc = llapi_file_fget_lmv_uuid(fd, &name);
2323 /* Now get the ost uuids */
2324 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2325 FILTER_BY_EXACT, "target_obd", ¶m);
2329 fp = fopen(param.gl_pathv[0], "r");
2332 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2337 snprintf(format, sizeof(format),
2338 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2339 while (fgets(buf, sizeof(buf), fp) != NULL) {
2340 if (uuidp && (index < *ost_count)) {
2341 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2349 if (uuidp && (index > *ost_count))
2354 cfs_free_param_data(¶m);
2358 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2360 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2363 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2368 root = open(mnt, O_RDONLY | O_DIRECTORY);
2371 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2376 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2384 /* Check if user specified value matches a real uuid. Ignore _UUID,
2385 * -osc-4ba41334, other trailing gunk in comparison.
2386 * @param real_uuid ends in "_UUID"
2387 * @param search_uuid may or may not end in "_UUID"
2389 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2391 int cmplen = strlen(real_uuid);
2392 int searchlen = strlen(search_uuid);
2394 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2396 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2399 /* The UUIDs may legitimately be different lengths, if
2400 * the system was upgraded from an older version. */
2401 if (cmplen != searchlen)
2404 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2407 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2408 * returned in param->fp_obd_index */
2409 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2411 struct obd_uuid obd_uuid;
2418 if (param->fp_got_uuids)
2421 /* Get the lov/lmv name */
2422 if (param->fp_get_lmv)
2423 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2425 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2427 if (rc != -ENOTTY) {
2428 llapi_error(LLAPI_MSG_ERROR, rc,
2429 "error: can't get %s name: %s",
2430 param->fp_get_lmv ? "lmv" : "lov",
2438 param->fp_got_uuids = 1;
2440 /* Now get the ost uuids */
2441 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2442 obd_uuid.uuid, FILTER_BY_EXACT,
2443 "target_obd", ¶m_data);
2447 fp = fopen(param_data.gl_pathv[0], "r");
2450 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2451 param_data.gl_pathv[0]);
2455 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2456 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2457 param->fp_get_lmv ? "MDTS" : "OBDS");
2459 snprintf(format, sizeof(format),
2460 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2461 while (fgets(buf, sizeof(buf), fp) != NULL) {
2464 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2467 if (param->fp_obd_uuid) {
2468 if (llapi_uuid_match(obd_uuid.uuid,
2469 param->fp_obd_uuid->uuid)) {
2470 param->fp_obd_index = index;
2473 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2474 /* Print everything */
2475 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2478 param->fp_obds_printed = 1;
2482 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2483 llapi_err_noerrno(LLAPI_MSG_ERROR,
2484 "error: %s: unknown obduuid: %s",
2485 __func__, param->fp_obd_uuid->uuid);
2489 cfs_free_param_data(¶m_data);
2493 /* In this case, param->fp_obd_uuid will be an array of obduuids and
2494 * obd index for all these obduuids will be returned in
2495 * param->fp_obd_indexes */
2496 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2497 int num_obds, int **obdindexes, int *obdindex,
2500 int ret, obdcount, obd_valid = 0, obdnum;
2502 struct obd_uuid *uuids = NULL;
2506 if (type == LOV_TYPE)
2507 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2509 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2513 obdcount = atoi(buf);
2514 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2519 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2521 if (ret == -EOVERFLOW) {
2522 struct obd_uuid *uuids_temp;
2524 uuids_temp = realloc(uuids, obdcount *
2525 sizeof(struct obd_uuid));
2526 if (uuids_temp != NULL) {
2528 goto retry_get_uuids;
2533 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2537 indexes = malloc(num_obds * sizeof(*obdindex));
2538 if (indexes == NULL) {
2543 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2546 /* The user may have specified a simple index */
2547 i = strtol(obduuids[obdnum].uuid, &end, 0);
2548 if (end && *end == '\0' && i < obdcount) {
2549 indexes[obdnum] = i;
2552 for (i = 0; i < obdcount; i++) {
2553 if (llapi_uuid_match(uuids[i].uuid,
2554 obduuids[obdnum].uuid)) {
2555 indexes[obdnum] = i;
2561 if (i >= obdcount) {
2562 indexes[obdnum] = OBD_NOT_FOUND;
2563 llapi_err_noerrno(LLAPI_MSG_ERROR,
2564 "invalid obduuid '%s'",
2565 obduuids[obdnum].uuid);
2571 *obdindex = OBD_NOT_FOUND;
2573 *obdindex = obd_valid;
2575 *obdindexes = indexes;
2583 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2587 if (param->fp_mdt_uuid) {
2588 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2590 ¶m->fp_mdt_indexes,
2591 ¶m->fp_mdt_index, LMV_TYPE);
2596 if (param->fp_obd_uuid) {
2597 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2599 ¶m->fp_obd_indexes,
2600 ¶m->fp_obd_index, LOV_TYPE);
2605 param->fp_got_uuids = 1;
2610 int llapi_ostlist(char *path, struct find_param *param)
2615 fd = open(path, O_RDONLY | O_DIRECTORY);
2619 ret = setup_obd_uuid(fd, path, param);
2626 * Tries to determine the default stripe attributes for a given filesystem. The
2627 * filesystem to check should be specified by fsname, or will be determined
2630 static int sattr_get_defaults(const char *const fsname,
2631 unsigned int *scount,
2632 unsigned int *ssize,
2633 unsigned int *soffset)
2639 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2640 "stripecount", val, sizeof(val));
2643 *scount = atoi(val);
2647 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2648 "stripesize", val, sizeof(val));
2655 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2656 "stripeoffset", val, sizeof(val));
2659 *soffset = atoi(val);
2666 * Tries to gather the default stripe attributes for a given filesystem. If
2667 * the attributes can be determined, they are cached for easy retreival the
2668 * next time they are needed. Only a single filesystem's attributes are
2671 int sattr_cache_get_defaults(const char *const fsname,
2672 const char *const pathname, unsigned int *scount,
2673 unsigned int *ssize, unsigned int *soffset)
2676 char fsname[PATH_MAX + 1];
2677 unsigned int stripecount;
2678 unsigned int stripesize;
2679 unsigned int stripeoffset;
2685 char fsname_buf[PATH_MAX + 1];
2686 unsigned int tmp[3];
2688 if (fsname == NULL) {
2689 rc = llapi_search_fsname(pathname, fsname_buf);
2693 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2696 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2698 * Ensure all 3 sattrs (count, size, and offset) are
2699 * successfully retrieved and stored in tmp before writing to
2702 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2706 cache.stripecount = tmp[0];
2707 cache.stripesize = tmp[1];
2708 cache.stripeoffset = tmp[2];
2709 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2713 *scount = cache.stripecount;
2715 *ssize = cache.stripesize;
2717 *soffset = cache.stripeoffset;
2722 static char *layout2name(__u32 layout_pattern)
2724 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2726 else if (layout_pattern == LOV_PATTERN_MDT)
2728 else if (layout_pattern == LOV_PATTERN_RAID0)
2730 else if (layout_pattern ==
2731 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2732 return "raid0,overstriped";
2737 enum lov_dump_flags {
2738 LDF_IS_DIR = 0x0001,
2739 LDF_IS_RAW = 0x0002,
2740 LDF_INDENT = 0x0004,
2741 LDF_SKIP_OBJS = 0x0008,
2743 LDF_EXTENSION = 0x0020,
2746 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2747 struct lov_user_ost_data_v1 *objects,
2748 enum llapi_layout_verbose verbose,
2749 int depth, char *pool_name,
2750 enum lov_dump_flags flags)
2752 bool is_dir = flags & LDF_IS_DIR;
2753 bool is_raw = flags & LDF_IS_RAW;
2754 bool indent = flags & LDF_INDENT;
2755 bool yaml = flags & LDF_YAML;
2756 bool skip_objs = flags & LDF_SKIP_OBJS;
2757 bool extension = flags & LDF_EXTENSION;
2758 char *prefix = is_dir ? "" : "lmm_";
2759 char *separator = "";
2760 char *space = indent ? " " : "";
2763 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2764 lmm_oi_set_seq(&lum->lmm_oi, 0);
2765 if (!indent && (verbose & VERBOSE_DETAIL))
2766 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2769 if (!yaml && !indent && depth && path &&
2770 ((verbose != VERBOSE_OBJID) || !is_dir))
2771 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2773 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2774 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2775 space, prefix, lum->lmm_magic);
2776 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2778 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2779 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2781 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2783 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2788 if (verbose & ~VERBOSE_DFID)
2789 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2791 /* This needs a bit of hand-holding since old 1.x lmm_oi
2792 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2793 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2794 * a real FID. Ideally the 2.x code would have stored this
2795 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2796 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2797 * worked properly (especially since IGIF FIDs use mds_inum as
2798 * the FID SEQ), but unfortunately that didn't happen.
2800 * Print it to look like an IGIF FID, even though the fields
2801 * are reversed on disk, so that it makes sense to userspace.
2803 * Don't use ostid_id() and ostid_seq(), since they assume the
2804 * oi_fid fields are in the right order. This is why there are
2805 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2807 * For newer layout types hopefully this will be a real FID. */
2808 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2809 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2810 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2811 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2812 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2814 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2815 (unsigned long long)seq, oid, ver);
2817 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2818 (unsigned long long)seq, oid, ver);
2821 if (verbose & VERBOSE_STRIPE_COUNT) {
2822 if (verbose & ~VERBOSE_STRIPE_COUNT)
2823 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2826 if (!is_raw && lum->lmm_stripe_count == 0 &&
2827 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2828 unsigned int scount;
2829 rc = sattr_cache_get_defaults(NULL, path,
2833 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2836 llapi_error(LLAPI_MSG_ERROR, rc,
2837 "Cannot determine default"
2840 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2842 (__s16)lum->lmm_stripe_count);
2845 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2847 (__s16)lum->lmm_stripe_count);
2849 if (!yaml && is_dir)
2855 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
2856 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
2857 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2858 if (verbose & ~VERBOSE_EXT_SIZE && extension)
2859 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
2861 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
2862 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2864 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2866 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2869 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2871 llapi_error(LLAPI_MSG_ERROR, rc,
2872 "Cannot determine default"
2875 /* Extension size is in KiB */
2876 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2878 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
2879 (unsigned long long)lum->lmm_stripe_size);
2881 if (!yaml && is_dir)
2887 if ((verbose & VERBOSE_PATTERN)) {
2888 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2889 if (verbose & ~VERBOSE_PATTERN)
2890 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2892 if (lov_pattern_supported(lum->lmm_pattern))
2893 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2894 layout2name(lum->lmm_pattern));
2896 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2897 separator = (!yaml && is_dir) ? " " : "\n";
2900 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2901 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2902 if (verbose & ~VERBOSE_GENERATION)
2903 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2905 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2906 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2910 if (verbose & VERBOSE_STRIPE_OFFSET) {
2911 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2912 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2913 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2915 if (is_dir || skip_objs)
2916 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2917 lum->lmm_stripe_offset ==
2918 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2919 lum->lmm_stripe_offset);
2920 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
2921 llapi_printf(LLAPI_MSG_NORMAL, "0");
2923 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2924 objects[0].l_ost_idx);
2925 if (!yaml && is_dir)
2931 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2932 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2933 if (verbose & ~VERBOSE_POOL)
2934 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2936 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2937 if (!yaml && is_dir)
2943 if (strlen(separator) != 0)
2944 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2947 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2948 struct lov_user_ost_data_v1 *objects,
2949 char *path, int obdindex, int depth,
2950 enum llapi_layout_verbose verbose,
2951 enum lov_dump_flags flags)
2953 bool is_dir = flags & LDF_IS_DIR;
2954 bool indent = flags & LDF_INDENT;
2955 bool skip_objs = flags & LDF_SKIP_OBJS;
2956 bool yaml = flags & LDF_YAML;
2957 bool obdstripe = obdindex == OBD_NOT_FOUND;
2960 if (!obdstripe && !skip_objs) {
2961 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2962 if (obdindex == objects[i].l_ost_idx) {
2972 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2975 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
2976 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2977 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
2978 char *space = " - ";
2981 llapi_printf(LLAPI_MSG_NORMAL,
2982 "%6slmm_objects:\n", " ");
2984 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2986 llapi_printf(LLAPI_MSG_NORMAL,
2987 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2989 for (i = 0; i < lum->lmm_stripe_count; i++) {
2990 int idx = objects[i].l_ost_idx;
2991 long long oid = ostid_id(&objects[i].l_ost_oi);
2992 long long gr = ostid_seq(&objects[i].l_ost_oi);
2994 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2998 struct lu_fid fid = { 0 };
3000 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3001 llapi_printf(LLAPI_MSG_NORMAL,
3002 "%sl_ost_idx: %d\n", space, idx);
3003 llapi_printf(LLAPI_MSG_NORMAL,
3004 "%8sl_fid: "DFID_NOBRACE"\n",
3006 } else if (indent) {
3007 struct lu_fid fid = { 0 };
3009 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3010 llapi_printf(LLAPI_MSG_NORMAL,
3011 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
3012 space, i, idx, PFID(&fid));
3015 sprintf(fmt, "%s%s%s\n",
3016 "\t%6u\t%14llu\t%#13llx\t",
3017 (fid_seq_is_rsvd(gr) ||
3018 fid_seq_is_mdt0(gr)) ?
3019 "%14llu" : "%#14llx", "%s");
3020 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
3022 obdindex == idx ? " *" : "");
3026 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3029 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3030 char *path, int obdindex, int depth,
3031 enum llapi_layout_verbose verbose,
3032 enum lov_dump_flags flags)
3034 struct lmv_user_mds_data *objects = lum->lum_objects;
3035 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3036 char *separator = "";
3037 bool yaml = flags & LDF_YAML;
3038 bool obdstripe = false;
3041 if (obdindex != OBD_NOT_FOUND) {
3042 if (lum->lum_stripe_count == 0) {
3043 if (obdindex == lum->lum_stripe_offset)
3046 for (i = 0; i < lum->lum_stripe_count; i++) {
3047 if (obdindex == objects[i].lum_mds) {
3048 llapi_printf(LLAPI_MSG_NORMAL,
3063 /* show all information default */
3065 if (lum->lum_magic == LMV_USER_MAGIC)
3066 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3067 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3069 verbose = VERBOSE_OBJID;
3072 if (depth && path && ((verbose != VERBOSE_OBJID)))
3073 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3075 if (verbose & VERBOSE_STRIPE_COUNT) {
3076 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3077 if (verbose & ~VERBOSE_STRIPE_COUNT)
3078 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3079 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3080 (int)lum->lum_stripe_count);
3081 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3087 if (verbose & VERBOSE_STRIPE_OFFSET) {
3088 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3089 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3090 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3091 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3092 (int)lum->lum_stripe_offset);
3093 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3099 if (verbose & VERBOSE_HASH_TYPE) {
3100 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3101 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3103 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3104 if (verbose & ~VERBOSE_HASH_TYPE)
3105 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3106 if (type < LMV_HASH_TYPE_MAX)
3107 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3108 mdt_hash_name[type]);
3110 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3112 if (flags & LMV_HASH_FLAG_SPACE)
3113 llapi_printf(LLAPI_MSG_NORMAL, ",space");
3114 if (flags & LMV_HASH_FLAG_MIGRATION)
3115 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3116 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3117 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3118 if (flags & LMV_HASH_FLAG_LOST_LMV)
3119 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3124 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3125 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3126 if (lum->lum_stripe_count > 0)
3127 llapi_printf(LLAPI_MSG_NORMAL,
3128 "mdtidx\t\t FID[seq:oid:ver]\n");
3129 for (i = 0; i < lum->lum_stripe_count; i++) {
3130 int idx = objects[i].lum_mds;
3131 struct lu_fid *fid = &objects[i].lum_fid;
3132 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3133 llapi_printf(LLAPI_MSG_NORMAL,
3134 "%6u\t\t "DFID"\t\t%s\n",
3136 obdindex == idx ? " *" : "");
3141 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3142 pool_name[0] != '\0') {
3143 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3144 if (verbose & ~VERBOSE_POOL)
3145 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3147 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3151 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3152 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3155 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3156 enum lov_dump_flags flags)
3158 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3159 int depth = param->fp_max_depth;
3160 enum llapi_layout_verbose verbose = param->fp_verbose;
3161 bool yaml = flags & LDF_YAML;
3163 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3164 !(flags & LDF_IS_DIR)) && !yaml)
3165 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3167 if (verbose & VERBOSE_DETAIL) {
3168 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3169 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3170 " ", comp_v1->lcm_magic);
3171 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3172 " ", comp_v1->lcm_size);
3173 if (flags & LDF_IS_DIR)
3174 llapi_printf(LLAPI_MSG_NORMAL,
3175 "%2slcm_flags: %s\n", " ",
3176 comp_v1->lcm_mirror_count > 0 ?
3179 llapi_printf(LLAPI_MSG_NORMAL,
3180 "%2slcm_flags: %s\n", " ",
3181 llapi_layout_flags_string(comp_v1->lcm_flags));
3184 if (verbose & VERBOSE_GENERATION) {
3185 if (verbose & ~VERBOSE_GENERATION)
3186 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3188 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3191 if (verbose & VERBOSE_MIRROR_COUNT) {
3192 if (verbose & ~VERBOSE_MIRROR_COUNT)
3193 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3195 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3196 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3197 comp_v1->lcm_mirror_count + 1 : 1);
3200 if (verbose & VERBOSE_COMP_COUNT) {
3201 if (verbose & ~VERBOSE_COMP_COUNT)
3202 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3204 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3205 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3206 comp_v1->lcm_entry_count : 0);
3209 if (verbose & VERBOSE_DETAIL && !yaml)
3210 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3213 static void lcme_flags2str(__u32 comp_flags)
3219 llapi_printf(LLAPI_MSG_NORMAL, "0");
3222 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3223 if (comp_flags & comp_flags_table[i].cfn_flag) {
3225 llapi_printf(LLAPI_MSG_NORMAL, ",");
3226 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3227 comp_flags_table[i].cfn_name);
3228 comp_flags &= ~comp_flags_table[i].cfn_flag;
3234 llapi_printf(LLAPI_MSG_NORMAL, ",");
3235 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3239 static void lov_dump_comp_v1_entry(struct find_param *param,
3240 enum lov_dump_flags flags, int index)
3242 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3243 struct lov_comp_md_entry_v1 *entry;
3244 char *separator = "";
3245 enum llapi_layout_verbose verbose = param->fp_verbose;
3246 bool yaml = flags & LDF_YAML;
3248 entry = &comp_v1->lcm_entries[index];
3251 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3253 if (verbose & VERBOSE_COMP_ID) {
3254 if (verbose & VERBOSE_DETAIL && !yaml)
3255 llapi_printf(LLAPI_MSG_NORMAL,
3256 "%slcme_id: ", " - ");
3257 else if (verbose & ~VERBOSE_COMP_ID)
3258 llapi_printf(LLAPI_MSG_NORMAL,
3259 "%4slcme_id: ", " ");
3260 if (entry->lcme_id != LCME_ID_INVAL)
3261 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3263 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3267 if (verbose & VERBOSE_MIRROR_ID) {
3268 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3269 if (verbose & ~VERBOSE_MIRROR_ID)
3270 llapi_printf(LLAPI_MSG_NORMAL,
3271 "%4slcme_mirror_id: ", " ");
3272 if (entry->lcme_id != LCME_ID_INVAL)
3273 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3274 mirror_id_of(entry->lcme_id));
3276 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3280 if (verbose & VERBOSE_COMP_FLAGS) {
3281 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3282 if (verbose & ~VERBOSE_COMP_FLAGS)
3283 llapi_printf(LLAPI_MSG_NORMAL,
3284 "%4slcme_flags: ", " ");
3285 lcme_flags2str(entry->lcme_flags);
3288 /* print snapshot timestamp if its a nosync comp */
3289 if ((verbose & VERBOSE_COMP_FLAGS) &&
3290 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3291 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3292 if (verbose & ~VERBOSE_COMP_FLAGS)
3293 llapi_printf(LLAPI_MSG_NORMAL,
3294 "%4slcme_timestamp: ", " ");
3296 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3297 (unsigned long long)entry->lcme_timestamp);
3299 time_t stamp = entry->lcme_timestamp;
3300 char *date_str = asctime(localtime(&stamp));
3302 date_str[strlen(date_str) - 1] = '\0';
3303 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3309 if (verbose & VERBOSE_COMP_START) {
3310 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3311 if (verbose & ~VERBOSE_COMP_START)
3312 llapi_printf(LLAPI_MSG_NORMAL,
3313 "%4slcme_extent.e_start: ", " ");
3314 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3315 (unsigned long long)entry->lcme_extent.e_start);
3319 if (verbose & VERBOSE_COMP_END) {
3320 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3321 if (verbose & ~VERBOSE_COMP_END)
3322 llapi_printf(LLAPI_MSG_NORMAL,
3323 "%4slcme_extent.e_end: ", " ");
3324 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3325 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3327 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3328 (unsigned long long)entry->lcme_extent.e_end);
3333 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3334 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3335 } else if (verbose & VERBOSE_DETAIL) {
3336 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3337 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3338 " ", entry->lcme_offset);
3339 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3340 " ", entry->lcme_size);
3341 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3343 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3347 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3348 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3350 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3351 * The table below gives the answers for the specified parameters (value and
3352 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3353 * --------------------------------------
3354 * 1 | file > limit; sign > 0 | -1 / -1 |
3355 * 2 | file = limit; sign > 0 | -1 / -1 |
3356 * 3 | file < limit; sign > 0 | ? / 1 |
3357 * 4 | file > limit; sign = 0 | -1 / -1 |
3358 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3359 * 6 | file < limit; sign = 0 | ? / -1 |
3360 * 7 | file > limit; sign < 0 | 1 / 1 |
3361 * 8 | file = limit; sign < 0 | ? / -1 |
3362 * 9 | file < limit; sign < 0 | ? / -1 |
3363 * --------------------------------------
3364 * Note: 5th actually means that the value is within the interval
3365 * (limit - margin, limit]. */
3366 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3367 int sign, int negopt, unsigned long long margin,
3373 /* Drop the fraction of margin (of days or size). */
3374 if (file + margin <= limit)
3376 } else if (sign == 0) {
3377 if (file <= limit && file + margin > limit)
3379 else if (file + margin <= limit)
3381 } else if (sign < 0) {
3388 return negopt ? ~ret + 1 : ret;
3391 static inline struct lov_user_md *
3392 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3394 return (struct lov_user_md *)((char *)comp_v1 +
3395 comp_v1->lcm_entries[ent_idx].lcme_offset);
3398 static inline struct lov_user_ost_data_v1 *
3399 lov_v1v3_objects(struct lov_user_md *v1)
3401 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3402 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3404 return v1->lmm_objects;
3408 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3410 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3411 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3412 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3414 pool_name[0] = '\0';
3418 print_last_init_comp(struct find_param *param)
3420 /* print all component info */
3421 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3424 /* print specific component info */
3425 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3426 param->fp_check_comp_start || param->fp_check_comp_end ||
3427 param->fp_check_mirror_id || param->fp_check_mirror_index)
3433 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3437 if (param->fp_comp_end == LUSTRE_EOF) {
3438 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3439 match = end == LUSTRE_EOF ? 1 : -1;
3440 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3441 match = end == LUSTRE_EOF ? -1 : 1;
3442 else /* at least EOF */
3444 if (param->fp_exclude_comp_end)
3447 unsigned long long margin;
3449 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3450 match = find_value_cmp(end, param->fp_comp_end,
3451 param->fp_comp_end_sign,
3452 param->fp_exclude_comp_end, margin, 0);
3459 * An example of "getstripe -v" for a two components PFL file:
3462 * lcm_magic: 0x0BD60BD0
3466 * lcm_entry_count: 2
3470 * lcme_extent.e_start: 0
3471 * lcme_extent.e_end: 1048576
3475 * lmm_magic: 0x0BD10BD0
3476 * lmm_seq: 0x200000401
3477 * lmm_object_id: 0x1
3478 * lmm_fid: [0x200000401:0x1:0x0]
3479 * lmm_stripe_count: 1
3480 * lmm_stripe_size: 1048576
3481 * lmm_pattern: raid0
3483 * lmm_stripe_offset: 0
3485 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3489 * lcme_extent.e_start: 1048576
3490 * lcme_extent.e_end: EOF
3494 * lmm_magic: 0x0BD10BD0
3495 * lmm_seq: 0x200000401
3496 * lmm_object_id: 0x1
3497 * lmm_fid: [0x200000401:0x1:0x0]
3498 * lmm_stripe_count: 2
3499 * lmm_stripe_size: 1048576
3500 * lmm_pattern: raid0
3502 * lmm_stripe_offset: 1
3504 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3505 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3507 static void lov_dump_comp_v1(struct find_param *param, char *path,
3508 enum lov_dump_flags flags)
3510 struct lov_comp_md_entry_v1 *entry;
3511 struct lov_user_ost_data_v1 *objects;
3512 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3513 struct lov_user_md_v1 *v1;
3514 char pool_name[LOV_MAXPOOLNAME + 1];
3515 int obdindex = param->fp_obd_index;
3516 int i, j, match, ext;
3517 bool obdstripe = false;
3518 __u16 mirror_index = 0;
3519 __u16 mirror_id = 0;
3521 if (obdindex != OBD_NOT_FOUND) {
3522 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3523 i < comp_v1->lcm_entry_count; i++) {
3524 if (!(comp_v1->lcm_entries[i].lcme_flags &
3528 v1 = lov_comp_entry(comp_v1, i);
3529 objects = lov_v1v3_objects(v1);
3531 for (j = 0; j < v1->lmm_stripe_count; j++) {
3532 if (obdindex == objects[j].l_ost_idx) {
3545 lov_dump_comp_v1_header(param, path, flags);
3547 flags |= LDF_INDENT;
3549 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3550 entry = &comp_v1->lcm_entries[i];
3552 if (param->fp_check_comp_flags) {
3553 if (((param->fp_comp_flags & entry->lcme_flags) !=
3554 param->fp_comp_flags) ||
3555 (param->fp_comp_neg_flags & entry->lcme_flags))
3559 if (param->fp_check_comp_id &&
3560 param->fp_comp_id != entry->lcme_id)
3563 if (param->fp_check_comp_start) {
3564 match = find_value_cmp(entry->lcme_extent.e_start,
3565 param->fp_comp_start,
3566 param->fp_comp_start_sign,
3568 param->fp_comp_start_units, 0);
3573 if (param->fp_check_comp_end) {
3574 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3580 if (param->fp_check_mirror_index) {
3581 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3583 mirror_id = mirror_id_of(entry->lcme_id);
3586 match = find_value_cmp(mirror_index,
3587 param->fp_mirror_index,
3588 param->fp_mirror_index_sign,
3589 param->fp_exclude_mirror_index,
3593 } else if (param->fp_check_mirror_id) {
3594 if (mirror_id != mirror_id_of(entry->lcme_id))
3595 mirror_id = mirror_id_of(entry->lcme_id);
3597 match = find_value_cmp(mirror_id,
3598 param->fp_mirror_id,
3599 param->fp_mirror_id_sign,
3600 param->fp_exclude_mirror_id,
3606 if (print_last_init_comp(param)) {
3608 * if part of stripe info is needed, we'd print only
3609 * the last instantiated component info.
3611 if (entry->lcme_flags & LCME_FL_INIT)
3614 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3615 if (entry->lcme_flags & LCME_FL_EXTENSION)
3616 /* moved back below */
3624 if (entry->lcme_flags & LCME_FL_INIT) {
3625 if (obdindex != OBD_NOT_FOUND) {
3626 flags |= LDF_SKIP_OBJS;
3627 v1 = lov_comp_entry(comp_v1, i);
3628 objects = lov_v1v3_objects(v1);
3630 for (j = 0; j < v1->lmm_stripe_count; j++) {
3631 if (obdindex == objects[j].l_ost_idx) {
3632 flags &= ~LDF_SKIP_OBJS;
3637 flags &= ~LDF_SKIP_OBJS;
3640 flags |= LDF_SKIP_OBJS;
3643 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3645 lov_dump_comp_v1_entry(param, flags, i);
3647 v1 = lov_comp_entry(comp_v1, i);
3648 objects = lov_v1v3_objects(v1);
3649 lov_v1v3_pool_name(v1, pool_name);
3651 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3652 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3653 param->fp_max_depth, param->fp_verbose,
3656 if (print_last_init_comp(param)) {
3658 * directory layout contains only layout template, print the
3662 i = comp_v1->lcm_entry_count - 1;
3665 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 entry = &comp_v1->lcm_entries[i];
3674 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3675 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3676 param->fp_max_depth, param->fp_verbose,
3681 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3682 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3685 static inline bool has_any_comp_options(struct find_param *param)
3687 enum llapi_layout_verbose verbose = param->fp_verbose;
3689 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3690 param->fp_check_comp_start || param->fp_check_comp_end ||
3691 param->fp_check_comp_flags)
3694 /* show full layout information, not component specific */
3695 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3698 return verbose & VERBOSE_COMP_OPTS;
3701 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3704 struct lov_user_md *lum = &orig->lmd_lmm;
3705 struct lov_user_mds_data *new;
3706 struct lov_comp_md_v1 *comp_v1;
3707 struct lov_comp_md_entry_v1 *ent;
3708 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3709 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3712 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3714 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3718 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3719 + sizeof(new->lmd_lmmsize));
3721 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3722 comp_v1->lcm_magic = lum->lmm_magic;
3723 comp_v1->lcm_size = lum_off + lum_size;
3724 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3725 comp_v1->lcm_flags = 0;
3726 comp_v1->lcm_entry_count = 1;
3728 ent = &comp_v1->lcm_entries[0];
3730 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3731 ent->lcme_extent.e_start = 0;
3732 ent->lcme_extent.e_end = LUSTRE_EOF;
3733 ent->lcme_offset = lum_off;
3734 ent->lcme_size = lum_size;
3736 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3741 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3742 enum lov_dump_flags flags)
3744 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3746 if (has_any_comp_options(param)) {
3747 struct lov_user_mds_data *new_lmd, *orig_lmd;
3749 orig_lmd = param->fp_lmd;
3750 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3751 if (new_lmd != NULL) {
3752 param->fp_lmd = new_lmd;
3753 lov_dump_comp_v1(param, path, flags);
3754 param->fp_lmd = orig_lmd;
3760 if (magic == LOV_USER_MAGIC_V1) {
3761 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3762 param->fp_lmd->lmd_lmm.lmm_objects,
3763 path, param->fp_obd_index,
3764 param->fp_max_depth, param->fp_verbose,
3767 char pool_name[LOV_MAXPOOLNAME + 1];
3768 struct lov_user_ost_data_v1 *objects;
3769 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3771 snprintf(pool_name, sizeof(pool_name), "%s",
3772 lmmv3->lmm_pool_name);
3773 objects = lmmv3->lmm_objects;
3774 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3775 objects, path, param->fp_obd_index,
3776 param->fp_max_depth, param->fp_verbose,
3781 static uint32_t check_foreign_type(uint32_t foreign_type)
3785 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3786 if (lu_foreign_types[i].lft_name == NULL)
3788 if (foreign_type == lu_foreign_types[i].lft_type)
3792 return LU_FOREIGN_TYPE_UNKNOWN;
3795 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3796 enum lov_dump_flags flags)
3798 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3799 bool yaml = flags & LDF_YAML;
3801 if (!yaml && param->fp_depth && path)
3802 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3804 if (param->fp_verbose & VERBOSE_DETAIL) {
3805 uint32_t type = check_foreign_type(lfm->lfm_type);
3807 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3809 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3811 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3813 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3814 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3815 lu_foreign_types[type].lft_name);
3817 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3819 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3822 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3823 lfm->lfm_length, lfm->lfm_value);
3824 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3827 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3828 enum lov_dump_flags flags)
3830 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3831 bool yaml = flags & LDF_YAML;
3833 if (!yaml && param->fp_depth && path)
3834 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3836 if (param->fp_verbose & VERBOSE_DETAIL) {
3837 uint32_t type = check_foreign_type(lfm->lfm_type);
3839 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3841 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3843 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3845 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3846 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3847 lu_foreign_types[type].lft_name);
3849 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3851 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3854 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3855 lfm->lfm_length, lfm->lfm_value);
3856 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3859 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3860 enum lov_dump_flags flags)
3864 if (param->fp_get_lmv || param->fp_get_default_lmv)
3865 magic = (__u32)param->fp_lmv_md->lum_magic;
3867 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3870 flags |= LDF_IS_RAW;
3875 case LOV_USER_MAGIC_V1:
3876 case LOV_USER_MAGIC_V3:
3877 case LOV_USER_MAGIC_SPECIFIC:
3878 lov_dump_plain_user_lmm(param, path, flags);
3880 case LOV_USER_MAGIC_FOREIGN:
3881 lov_dump_foreign_lmm(param, path, flags);
3884 case LMV_USER_MAGIC: {
3885 char pool_name[LOV_MAXPOOLNAME + 1];
3886 struct lmv_user_md *lum;
3888 lum = (struct lmv_user_md *)param->fp_lmv_md;
3889 snprintf(pool_name, sizeof(pool_name), "%s",
3890 lum->lum_pool_name);
3891 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3892 param->fp_max_depth, param->fp_verbose,
3896 case LOV_USER_MAGIC_COMP_V1:
3897 lov_dump_comp_v1(param, path, flags);
3899 case LMV_MAGIC_FOREIGN:
3900 lmv_dump_foreign_lmm(param, path, flags);
3903 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
3904 "(expecting one of %#x %#x %#x %#x)\n",
3905 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3906 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3907 LMV_USER_MAGIC, LMV_MAGIC_V1);
3912 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3918 fname = strrchr(path, '/');
3920 /* It should be a file (or other non-directory) */
3921 if (fname == NULL) {
3922 dname = (char *)malloc(2);
3926 fname = (char *)path;
3928 dname = (char *)malloc(fname - path + 1);
3931 strncpy(dname, path, fname - path);
3932 dname[fname - path] = '\0';
3936 fd = open(dname, O_RDONLY | O_NONBLOCK);
3942 strcpy((char *)lum, fname);
3943 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3946 if (close(fd) == -1 && rc == 0)
3954 int llapi_file_lookup(int dirfd, const char *name)
3956 struct obd_ioctl_data data = { 0 };
3961 if (dirfd < 0 || name == NULL)
3964 data.ioc_version = OBD_IOCTL_VERSION;
3965 data.ioc_len = sizeof(data);
3966 data.ioc_inlbuf1 = (char *)name;
3967 data.ioc_inllen1 = strlen(name) + 1;
3969 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3971 llapi_error(LLAPI_MSG_ERROR, rc,
3972 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3977 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
3983 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
3984 * Return -1 or 1 if file timestamp does not or does match the given criteria
3985 * correspondingly. Return 0 if the MDS time is being checked and there are
3986 * attributes on OSTs and it is not yet clear if the timespamp matches.
3988 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
3989 * updated timestamps. */
3990 static int find_time_check(lstatx_t *stx, struct find_param *param, int mds)
3995 /* Check if file is accepted. */
3996 if (param->fp_atime) {
3997 rc2 = find_value_cmp(stx->stx_atime.tv_sec, param->fp_atime,
3998 param->fp_asign, param->fp_exclude_atime,
3999 param->fp_time_margin, mds);
4005 if (param->fp_mtime) {
4006 rc2 = find_value_cmp(stx->stx_mtime.tv_sec, param->fp_mtime,
4007 param->fp_msign, param->fp_exclude_mtime,
4008 param->fp_time_margin, mds);
4012 /* If the previous check matches, but this one is not yet clear,
4013 * we should return 0 to do an RPC on OSTs. */
4018 if (param->fp_ctime) {
4019 rc2 = find_value_cmp(stx->stx_ctime.tv_sec, param->fp_ctime,
4020 param->fp_csign, param->fp_exclude_ctime,
4021 param->fp_time_margin, mds);
4025 /* If the previous check matches, but this one is not yet clear,
4026 * we should return 0 to do an RPC on OSTs. */
4035 * Check whether the stripes matches the indexes user provided
4039 static int check_obd_match(struct find_param *param)
4041 struct lov_user_ost_data_v1 *objects;
4042 struct lov_comp_md_v1 *comp_v1 = NULL;
4043 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4044 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
4045 int i, j, k, count = 1;
4047 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4050 if (!S_ISREG(stx->stx_mode))
4053 /* exclude foreign */
4054 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4055 return param->fp_exclude_obd;
4057 /* Only those files should be accepted, which have a
4058 * stripe on the specified OST. */
4059 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4060 comp_v1 = (struct lov_comp_md_v1 *)v1;
4061 count = comp_v1->lcm_entry_count;
4064 for (i = 0; i < count; i++) {
4066 v1 = lov_comp_entry(comp_v1, i);
4068 objects = lov_v1v3_objects(v1);
4070 for (j = 0; j < v1->lmm_stripe_count; j++) {
4071 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4074 for (k = 0; k < param->fp_num_obds; k++) {
4075 if (param->fp_obd_indexes[k] ==
4076 objects[j].l_ost_idx)
4077 return !param->fp_exclude_obd;
4082 return param->fp_exclude_obd;
4085 static int check_mdt_match(struct find_param *param)
4089 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4092 /* FIXME: For striped dir, we should get stripe information and check */
4093 for (i = 0; i < param->fp_num_mdts; i++) {
4094 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4095 return !param->fp_exclude_mdt;
4098 if (param->fp_exclude_mdt)
4105 * Check whether the obd is active or not, if it is
4106 * not active, just print the object affected by this
4109 static int print_failed_tgt(struct find_param *param, char *path, int type)
4111 struct obd_statfs stat_buf;
4112 struct obd_uuid uuid_buf;
4115 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV)
4118 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4119 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4120 ret = llapi_obd_statfs(path, type,
4121 param->fp_obd_index, &stat_buf,
4124 llapi_printf(LLAPI_MSG_NORMAL,
4125 "obd_uuid: %s failed %s ",
4126 param->fp_obd_uuid->uuid,
4133 static int find_check_stripe_size(struct find_param *param)
4135 struct lov_comp_md_v1 *comp_v1 = NULL;
4136 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4137 __u32 stripe_size = 0;
4138 int ret, i, count = 1;
4140 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4141 return param->fp_exclude_stripe_size ? 1 : -1;
4143 ret = param->fp_exclude_stripe_size ? 1 : -1;
4144 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4145 comp_v1 = (struct lov_comp_md_v1 *)v1;
4146 count = comp_v1->lcm_entry_count;
4149 for (i = 0; i < count; i++) {
4150 struct lov_comp_md_entry_v1 *ent;
4153 v1 = lov_comp_entry(comp_v1, i);
4155 ent = &comp_v1->lcm_entries[i];
4156 if (ent->lcme_flags & LCME_FL_EXTENSION)
4158 if (!(ent->lcme_flags & LCME_FL_INIT))
4161 stripe_size = v1->lmm_stripe_size;
4164 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4165 param->fp_stripe_size_sign,
4166 param->fp_exclude_stripe_size,
4167 param->fp_stripe_size_units, 0);
4172 static int find_check_ext_size(struct find_param *param)
4174 struct lov_comp_md_v1 *comp_v1;
4175 struct lov_user_md_v1 *v1;
4178 ret = param->fp_exclude_ext_size ? 1 : -1;
4179 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4180 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4183 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4184 struct lov_comp_md_entry_v1 *ent;
4186 v1 = lov_comp_entry(comp_v1, i);
4188 ent = &comp_v1->lcm_entries[i];
4189 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4192 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4193 param->fp_ext_size_sign,
4194 param->fp_exclude_ext_size,
4195 param->fp_ext_size_units, 0);
4196 /* If any ext_size matches */
4204 static __u32 find_get_stripe_count(struct find_param *param)
4206 struct lov_comp_md_v1 *comp_v1 = NULL;
4207 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4209 __u32 stripe_count = 0;
4211 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4214 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4215 comp_v1 = (struct lov_comp_md_v1 *)v1;
4216 count = comp_v1->lcm_entry_count;
4219 for (i = 0; i < count; i++) {
4221 struct lov_comp_md_entry_v1 *ent;
4223 v1 = lov_comp_entry(comp_v1, i);
4225 ent = &comp_v1->lcm_entries[i];
4226 if (!(ent->lcme_flags & LCME_FL_INIT))
4229 if (ent->lcme_flags & LCME_FL_EXTENSION)
4232 stripe_count = v1->lmm_stripe_count;
4235 return stripe_count;
4238 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4240 static int find_check_layout(struct find_param *param)
4242 struct lov_comp_md_v1 *comp_v1 = NULL;
4243 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4245 bool found = false, valid = false;
4247 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4248 comp_v1 = (struct lov_comp_md_v1 *)v1;
4249 count = comp_v1->lcm_entry_count;
4252 for (i = 0; i < count; i++) {
4254 v1 = lov_comp_entry(comp_v1, i);
4256 /* foreign file have a special magic but no pattern field */
4257 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4260 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4264 if (v1->lmm_pattern & param->fp_layout) {
4273 if ((found && !param->fp_exclude_layout) ||
4274 (!found && param->fp_exclude_layout))
4280 /* if no type specified, check/exclude all foreign
4281 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4283 static int find_check_foreign(struct find_param *param)
4285 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4286 struct lov_foreign_md *lfm;
4288 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4289 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4290 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4291 return param->fp_exclude_foreign ? 1 : -1;
4294 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4295 lfm->lfm_type == param->fp_foreign_type)
4296 return param->fp_exclude_foreign ? -1 : 1;
4297 return param->fp_exclude_foreign ? 1 : -1;
4301 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4302 struct lmv_foreign_md *lfm;
4304 lfm = (void *)param->fp_lmv_md;
4305 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
4306 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4307 return param->fp_exclude_foreign ? 1 : -1;
4310 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4311 lfm->lfm_type == param->fp_foreign_type)
4312 return param->fp_exclude_foreign ? -1 : 1;
4313 return param->fp_exclude_foreign ? 1 : -1;
4319 static int find_check_pool(struct find_param *param)
4321 struct lov_comp_md_v1 *comp_v1 = NULL;
4322 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4323 struct lov_user_md_v3 *v3 = (void *)v1;
4327 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4328 comp_v1 = (struct lov_comp_md_v1 *)v1;
4329 count = comp_v1->lcm_entry_count;
4330 /* empty requested pool is taken as no pool search */
4331 if (count == 0 && param->fp_poolname[0] == '\0')
4335 for (i = 0; i < count; i++) {
4336 if (comp_v1 != NULL)
4337 v1 = lov_comp_entry(comp_v1, i);
4339 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4342 if (((v1->lmm_magic == LOV_USER_MAGIC_V1) &&
4343 (param->fp_poolname[0] == '\0')) ||
4344 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4345 (strncmp(v3->lmm_pool_name,
4346 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4347 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4348 (strcmp(param->fp_poolname, "*") == 0))) {
4354 if ((found && !param->fp_exclude_pool) ||
4355 (!found && param->fp_exclude_pool))
4361 static int find_check_comp_options(struct find_param *param)
4363 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
4364 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4365 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4366 struct lov_comp_md_entry_v1 *entry;
4369 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4372 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4373 comp_v1 = (struct lov_comp_md_v1 *)v1;
4375 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4376 if (forged_v1 == NULL)
4378 comp_v1 = forged_v1;
4379 comp_v1->lcm_entry_count = 1;
4380 entry = &comp_v1->lcm_entries[0];
4381 entry->lcme_flags = S_ISDIR(stx->stx_mode) ? 0 : LCME_FL_INIT;
4382 entry->lcme_extent.e_start = 0;
4383 entry->lcme_extent.e_end = LUSTRE_EOF;
4386 /* invalid case, don't match for any kind of search. */
4387 if (comp_v1->lcm_entry_count == 0) {
4392 if (param->fp_check_comp_count) {
4393 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4394 param->fp_comp_count,
4395 param->fp_comp_count_sign,
4396 param->fp_exclude_comp_count, 1, 0);
4402 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4403 entry = &comp_v1->lcm_entries[i];
4405 if (param->fp_check_comp_flags) {
4407 if (((param->fp_comp_flags & entry->lcme_flags) !=
4408 param->fp_comp_flags) ||
4409 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4415 if (param->fp_check_comp_start) {
4416 ret = find_value_cmp(entry->lcme_extent.e_start,
4417 param->fp_comp_start,
4418 param->fp_comp_start_sign,
4419 param->fp_exclude_comp_start,
4420 param->fp_comp_start_units, 0);
4425 if (param->fp_check_comp_end) {
4426 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4432 /* the component matches all criteria */
4441 static int find_check_mirror_options(struct find_param *param)
4443 struct lov_comp_md_v1 *comp_v1;
4444 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4447 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4450 comp_v1 = (struct lov_comp_md_v1 *)v1;
4452 if (param->fp_check_mirror_count) {
4453 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4454 param->fp_mirror_count,
4455 param->fp_mirror_count_sign,
4456 param->fp_exclude_mirror_count, 1, 0);
4461 if (param->fp_check_mirror_state) {
4463 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4465 if ((param->fp_mirror_state != 0 &&
4466 file_state != param->fp_mirror_state) ||
4467 file_state == param->fp_mirror_neg_state)
4474 static bool find_check_lmm_info(struct find_param *param)
4476 return param->fp_check_pool || param->fp_check_stripe_count ||
4477 param->fp_check_stripe_size || param->fp_check_layout ||
4478 param->fp_check_comp_count || param->fp_check_comp_end ||
4479 param->fp_check_comp_start || param->fp_check_comp_flags ||
4480 param->fp_check_mirror_count || param->fp_check_foreign ||
4481 param->fp_check_mirror_state || param->fp_check_ext_size ||
4482 param->fp_check_projid;
4486 * Get file/directory project id.
4487 * by the open fd resides on.
4488 * Return 0 and project id on success, or -ve errno.
4490 static int fget_projid(int fd, int *projid)
4495 rc = ioctl(fd, LL_IOC_FSGETXATTR, &fsx);
4499 *projid = fsx.fsx_projid;
4503 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
4504 void *data, struct dirent64 *de)
4506 struct find_param *param = (struct find_param *)data;
4507 DIR *dir = dirp == NULL ? NULL : *dirp;
4508 int decision = 1; /* 1 is accepted; -1 is rejected. */
4509 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
4511 int checked_type = 0;
4513 __u32 stripe_count = 0;
4517 if (parent == NULL && dir == NULL)
4520 /* If a regular expression is presented, make the initial decision */
4521 if (param->fp_pattern != NULL) {
4522 char *fname = strrchr(path, '/');
4523 fname = (fname == NULL ? path : fname + 1);
4524 ret = fnmatch(param->fp_pattern, fname, 0);
4525 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4526 (ret == 0 && param->fp_exclude_pattern))
4530 /* See if we can check the file type from the dirent. */
4531 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
4534 if (DTTOIF(de->d_type) == param->fp_type) {
4535 if (param->fp_exclude_type)
4538 if (!param->fp_exclude_type)
4545 /* Request MDS for the stat info if some of these parameters need
4546 * to be compared. */
4547 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4548 param->fp_check_uid || param->fp_check_gid ||
4549 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4550 param->fp_check_size || param->fp_check_blocks ||
4551 find_check_lmm_info(param) ||
4552 param->fp_check_mdt_count || param->fp_check_hash_type)
4555 if (param->fp_type != 0 && checked_type == 0)
4558 if (decision == 0) {
4559 if (dir && (param->fp_check_mdt_count ||
4560 param->fp_check_hash_type || param->fp_check_foreign)) {
4561 param->fp_get_lmv = 1;
4562 ret = cb_get_dirstripe(path, dir, param);
4564 /* XXX this works to decide for foreign
4567 if (errno == ENODATA &&
4568 param->fp_check_foreign) {
4569 if (param->fp_exclude_foreign)
4577 param->fp_lmd->lmd_lmm.lmm_magic = 0;
4578 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
4579 param->fp_lum_size, GET_LMD_INFO);
4580 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
4581 find_check_lmm_info(param)) {
4582 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
4584 /* We need to "fake" the "use the default" values
4585 * since the lmm struct is zeroed out at this point. */
4586 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4587 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
4589 ostid_set_seq(&lmm->lmm_oi,
4590 FID_SEQ_LOV_DEFAULT);
4591 lmm->lmm_stripe_size = 0;
4592 lmm->lmm_stripe_count = 0;
4593 lmm->lmm_stripe_offset = -1;
4595 if (ret == 0 && param->fp_mdt_uuid != NULL) {
4597 ret = llapi_file_fget_mdtidx(dirfd(dir),
4598 ¶m->fp_file_mdt_index);
4599 } else if (S_ISREG(stx->stx_mode)) {
4600 /* FIXME: we could get the MDT index from the
4601 * file's FID in lmd->lmd_lmm.lmm_oi without
4602 * opening the file, once we are sure that
4603 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
4604 * That would still be an ioctl() to map the
4605 * FID to the MDT, but not an open RPC. */
4606 fd = open(path, O_RDONLY);
4608 ret = llapi_file_fget_mdtidx(fd,
4609 ¶m->fp_file_mdt_index);
4614 /* For a special file, we assume it resides on
4615 * the same MDT as the parent directory. */
4616 ret = llapi_file_fget_mdtidx(dirfd(parent),
4617 ¶m->fp_file_mdt_index);
4628 stripe_count = find_get_stripe_count(param);
4632 if (param->fp_type && !checked_type) {
4633 if ((stx->stx_mode & S_IFMT) == param->fp_type) {
4634 if (param->fp_exclude_type)
4637 if (!param->fp_exclude_type)
4643 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
4644 if (lustre_fs && param->fp_got_uuids &&
4645 param->fp_dev != makedev(stx->stx_dev_major,
4646 stx->stx_dev_minor)) {
4647 /* A lustre/lustre mount point is crossed. */
4648 param->fp_got_uuids = 0;
4649 param->fp_obds_printed = 0;
4650 param->fp_mdt_index = OBD_NOT_FOUND;
4651 param->fp_obd_index = OBD_NOT_FOUND;
4654 if (lustre_fs && !param->fp_got_uuids) {
4655 ret = setup_target_indexes(dir ? dir : parent, path,
4660 param->fp_dev = makedev(stx->stx_dev_major,
4661 stx->stx_dev_minor);
4662 } else if (!lustre_fs && param->fp_got_uuids) {
4663 /* A lustre/non-lustre mount point is crossed. */
4664 param->fp_got_uuids = 0;
4665 param->fp_mdt_index = OBD_NOT_FOUND;
4666 param->fp_obd_index = OBD_NOT_FOUND;
4670 if (param->fp_check_foreign) {
4671 decision = find_check_foreign(param);
4676 if (param->fp_check_stripe_size) {
4677 decision = find_check_stripe_size(param);
4682 if (param->fp_check_ext_size) {
4683 decision = find_check_ext_size(param);
4688 if (param->fp_check_stripe_count) {
4689 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
4690 param->fp_stripe_count_sign,
4691 param->fp_exclude_stripe_count, 1, 0);
4696 if (param->fp_check_mdt_count) {
4697 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4702 decision = find_value_cmp(
4703 param->fp_lmv_md->lum_stripe_count,
4704 param->fp_mdt_count,
4705 param->fp_mdt_count_sign,
4706 param->fp_exclude_mdt_count, 1, 0);
4711 if (param->fp_check_layout) {
4712 decision = find_check_layout(param);
4717 if (param->fp_check_hash_type) {
4720 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4725 found = param->fp_lmv_md->lum_hash_type & param->fp_hash_type;
4726 if ((found && param->fp_exclude_hash_type) ||
4727 (!found && !param->fp_exclude_hash_type)) {
4733 /* If an OBD UUID is specified but none matches, skip this file. */
4734 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
4735 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
4738 /* If an OST or MDT UUID is given, and some OST matches,
4740 if (param->fp_obd_index != OBD_NOT_FOUND ||
4741 param->fp_mdt_index != OBD_NOT_FOUND) {
4742 if (param->fp_obd_uuid) {
4743 if (check_obd_match(param)) {
4744 /* If no mdtuuid is given, we are done.
4745 * Otherwise, fall through to the mdtuuid
4747 if (!param->fp_mdt_uuid)
4754 if (param->fp_mdt_uuid) {
4755 if (check_mdt_match(param))
4762 if (param->fp_check_uid) {
4763 if (stx->stx_uid == param->fp_uid) {
4764 if (param->fp_exclude_uid)
4767 if (!param->fp_exclude_uid)
4772 if (param->fp_check_gid) {
4773 if (stx->stx_gid == param->fp_gid) {
4774 if (param->fp_exclude_gid)
4777 if (!param->fp_exclude_gid)
4782 if (param->fp_check_projid) {
4786 fd = open(path, O_RDONLY);
4789 ret = fget_projid(fd, &projid);
4794 if (projid == param->fp_projid) {
4795 if (param->fp_exclude_projid)
4798 if (!param->fp_exclude_projid)
4803 if (param->fp_check_pool) {
4804 decision = find_check_pool(param);
4809 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
4810 param->fp_check_comp_start || param->fp_check_comp_end) {
4811 decision = find_check_comp_options(param);
4816 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
4817 decision = find_check_mirror_options(param);
4822 /* Check the time on mds. */
4824 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
4827 for_mds = lustre_fs ?
4828 (S_ISREG(stx->stx_mode) && stripe_count) : 0;
4829 decision = find_time_check(stx, param, for_mds);
4834 flags = param->fp_lmd->lmd_flags;
4835 if (param->fp_check_size &&
4836 ((S_ISREG(stx->stx_mode) && stripe_count) ||
4837 S_ISDIR(stx->stx_mode)) &&
4838 !(flags & OBD_MD_FLSIZE ||
4839 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
4842 if (param->fp_check_blocks &&
4843 ((S_ISREG(stx->stx_mode) && stripe_count) ||
4844 S_ISDIR(stx->stx_mode)) &&
4845 !(flags & OBD_MD_FLBLOCKS ||
4846 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
4849 /* If file still fits the request, ask ost for updated info.
4850 * The regular stat is almost of the same speed as some new
4851 * 'glimpse-size-ioctl'.
4856 /* For regular files with the stripe the decision may have not
4857 * been taken yet if *time or size is to be checked. */
4858 if (param->fp_obd_index != OBD_NOT_FOUND)
4859 print_failed_tgt(param, path, LL_STATFS_LOV);
4861 if (param->fp_mdt_index != OBD_NOT_FOUND)
4862 print_failed_tgt(param, path, LL_STATFS_LMV);
4865 ret = fstat_f(dirfd(dir), &st);
4866 else if (de != NULL)
4867 ret = fstatat_f(dirfd(parent), de->d_name, &st,
4868 AT_SYMLINK_NOFOLLOW);
4870 ret = lstat_f(path, &st);
4873 if (errno == ENOENT) {
4874 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
4875 "warning: %s: %s does not exist",
4880 llapi_error(LLAPI_MSG_ERROR, ret,
4881 "%s: stat on %s failed",
4887 convert_lmd_statx(param->fp_lmd, &st, true);
4888 /* Check the time on osc. */
4889 decision = find_time_check(stx, param, 0);
4894 if (param->fp_check_size) {
4895 decision = find_value_cmp(stx->stx_size, param->fp_size,
4896 param->fp_size_sign,
4897 param->fp_exclude_size,
4898 param->fp_size_units, 0);
4903 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
4904 decision = find_value_cmp(stx->stx_blocks * 512,
4906 param->fp_blocks_sign,
4907 param->fp_exclude_blocks,
4908 param->fp_blocks_units, 0);
4914 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
4915 if (param->fp_zero_end)
4916 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
4918 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4922 /* Do not get down anymore? */
4923 if (param->fp_depth == param->fp_max_depth) {
4934 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
4935 void *param_data, struct dirent64 *de)
4937 struct find_param *param = (struct find_param *)param_data;
4938 struct lmv_user_md *lmu = param->fp_lmv_md;
4939 DIR *tmp_parent = parent;
4940 char raw[MAX_IOC_BUFLEN] = {'\0'};
4942 struct obd_ioctl_data data = { 0 };
4949 if (parent == NULL && dirp == NULL)
4958 if (parent == NULL) {
4959 tmp_parent = opendir_parent(path);
4960 if (tmp_parent == NULL) {
4963 llapi_error(LLAPI_MSG_ERROR, ret,
4964 "can not open %s", path);
4969 fd = dirfd(tmp_parent);
4971 path_copy = strdup(path);
4972 filename = basename(path_copy);
4974 data.ioc_inlbuf1 = (char *)filename;
4975 data.ioc_inllen1 = strlen(filename) + 1;
4976 data.ioc_inlbuf2 = (char *)lmu;
4977 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
4979 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
4981 llapi_error(LLAPI_MSG_ERROR, ret,
4982 "llapi_obd_statfs: error packing ioctl data");
4987 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
4989 if (errno == EBUSY && !retry) {
4990 /* because migrate may not be able to lock all involved
4991 * objects in order, for some of them it try lock, while
4992 * there may be conflicting COS locks and cause migrate
4993 * fail with EBUSY, hope a sync() could cause
4994 * transaction commit and release these COS locks. */
4998 } else if (errno == EALREADY) {
4999 if (param->fp_verbose & VERBOSE_DETAIL)
5000 llapi_printf(LLAPI_MSG_NORMAL,
5001 "%s was migrated to MDT%d already\n",
5002 path, lmu->lum_stripe_offset);
5006 llapi_error(LLAPI_MSG_ERROR, ret,
5007 "%s migrate failed: %s (%d)\n",
5008 path, strerror(-ret), ret);
5011 } else if (param->fp_verbose & VERBOSE_DETAIL) {
5012 llapi_printf(LLAPI_MSG_NORMAL,
5013 "migrate %s to MDT%d stripe count %d\n",
5014 path, lmu->lum_stripe_offset,
5015 lmu->lum_stripe_count);
5020 /* If the directory is being migration, we need
5021 * close the directory after migration,
5022 * so the old directory cache will be cleanup
5023 * on the client side, and re-open to get the
5024 * new directory handle */
5025 *dirp = opendir(path);
5026 if (*dirp == NULL) {
5028 llapi_error(LLAPI_MSG_ERROR, ret,
5029 "%s: Failed to open '%s'", __func__, path);
5034 closedir(tmp_parent);
5041 /* dir migration finished, shrink its stripes */
5042 static int cb_migrate_mdt_fini(char *path, DIR *parent, DIR **dirp, void *data,
5043 struct dirent64 *de)
5045 struct find_param *param = data;
5046 struct lmv_user_md *lmu = param->fp_lmv_md;
5047 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
5050 if (de && de->d_type != DT_DIR)
5055 * close it before setxattr because the latter may destroy the
5056 * original object, and cause close fail.
5058 ret = closedir(*dirp);
5064 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
5065 if (ret == -EALREADY)
5068 cb_common_fini(path, parent, dirp, data, de);
5072 int llapi_migrate_mdt(char *path, struct find_param *param)
5074 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
5078 int llapi_mv(char *path, struct find_param *param)
5080 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
5081 static bool printed;
5084 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
5085 "llapi_mv() is deprecated, use llapi_migrate_mdt()\n");
5089 return llapi_migrate_mdt(path, param);
5092 int llapi_find(char *path, struct find_param *param)
5094 return param_callback(path, cb_find_init, cb_common_fini, param);
5098 * Get MDT number that the file/directory inode referenced
5099 * by the open fd resides on.
5100 * Return 0 and mdtidx on success, or -ve errno.
5102 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
5104 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
5109 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
5110 struct dirent64 *de)
5112 struct find_param *param = (struct find_param *)data;
5113 DIR *d = dirp == NULL ? NULL : *dirp;
5117 if (parent == NULL && d == NULL)
5121 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5122 } else /* if (parent) */ {
5125 fd = open(path, O_RDONLY | O_NOCTTY);
5127 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5135 if (ret == -ENODATA) {
5136 if (!param->fp_obd_uuid)
5137 llapi_printf(LLAPI_MSG_NORMAL,
5138 "'%s' has no stripe info\n", path);
5140 } else if (ret == -ENOENT) {
5141 llapi_error(LLAPI_MSG_WARN, ret,
5142 "warning: %s: '%s' does not exist",
5145 } else if (ret == -ENOTTY) {
5146 llapi_error(LLAPI_MSG_ERROR, ret,
5147 "%s: '%s' not on a Lustre fs",
5150 llapi_error(LLAPI_MSG_ERROR, ret,
5151 "error: %s: '%s' failed get_mdtidx",
5157 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5158 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5160 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5164 /* Do not go down anymore? */
5165 if (param->fp_depth == param->fp_max_depth)
5173 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
5174 struct dirent64 *de)
5176 struct find_param *param = (struct find_param *)data;
5177 DIR *d = dirp == NULL ? NULL : *dirp;
5180 if (parent == NULL && d == NULL)
5183 if (param->fp_obd_uuid) {
5184 param->fp_quiet = 1;
5185 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
5190 if (d && (param->fp_get_lmv || param->fp_get_default_lmv))
5191 ret = cb_get_dirstripe(path, d, param);
5193 (parent && !param->fp_get_lmv && !param->fp_get_default_lmv))
5194 ret = get_lmd_info(path, parent, d, ¶m->fp_lmd->lmd_lmm,
5195 param->fp_lum_size, GET_LMD_STRIPE);
5200 if (errno == ENODATA && d != NULL) {
5201 /* We need to "fake" the "use the default" values
5202 * since the lmm struct is zeroed out at this point.
5203 * The magic needs to be set in order to satisfy
5204 * a check later on in the code path.
5205 * The object_seq needs to be set for the "(Default)"
5206 * prefix to be displayed. */
5207 if (param->fp_get_default_lmv) {
5208 struct lmv_user_md *lum = param->fp_lmv_md;
5210 lum->lum_magic = LMV_USER_MAGIC;
5211 lum->lum_stripe_count = 0;
5212 lum->lum_stripe_offset = -1;
5214 } else if (param->fp_get_lmv) {
5215 struct lmv_user_md *lum = param->fp_lmv_md;
5218 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5221 lum->lum_magic = LMV_MAGIC_V1;
5222 lum->lum_stripe_count = 0;
5223 lum->lum_stripe_offset = mdtidx;
5226 struct lov_user_md *lmm =
5227 ¶m->fp_lmd->lmd_lmm;
5229 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5231 ostid_set_seq(&lmm->lmm_oi,
5232 FID_SEQ_LOV_DEFAULT);
5233 lmm->lmm_stripe_count = 0;
5234 lmm->lmm_stripe_size = 0;
5235 lmm->lmm_stripe_offset = -1;
5238 } else if (errno == ENODATA && parent != NULL) {
5239 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5240 llapi_printf(LLAPI_MSG_NORMAL,
5241 "%s has no stripe info\n", path);
5243 } else if (errno == ENOENT) {
5244 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5245 "warning: %s: %s does not exist",
5248 } else if (errno == ENOTTY) {
5250 llapi_error(LLAPI_MSG_ERROR, ret,
5251 "%s: '%s' not on a Lustre fs?",
5256 llapi_error(LLAPI_MSG_ERROR, ret,
5257 "error: %s: %s failed for %s",
5258 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
5259 "IOC_MDC_GETFILESTRIPE", path);
5266 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5267 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
5270 /* Do not get down anymore? */
5271 if (param->fp_depth == param->fp_max_depth)
5279 int llapi_getstripe(char *path, struct find_param *param)
5281 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5282 cb_get_mdt_index : cb_getstripe,
5283 cb_common_fini, param);
5286 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5287 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5289 char raw[MAX_IOC_BUFLEN] = {'\0'};
5291 struct obd_ioctl_data data = { 0 };
5294 data.ioc_inlbuf1 = (char *)&type;
5295 data.ioc_inllen1 = sizeof(__u32);
5296 data.ioc_inlbuf2 = (char *)&index;
5297 data.ioc_inllen2 = sizeof(__u32);
5298 data.ioc_pbuf1 = (char *)stat_buf;
5299 data.ioc_plen1 = sizeof(struct obd_statfs);
5300 data.ioc_pbuf2 = (char *)uuid_buf;
5301 data.ioc_plen2 = sizeof(struct obd_uuid);
5303 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5305 llapi_error(LLAPI_MSG_ERROR, rc,
5306 "llapi_obd_statfs: error packing ioctl data");
5310 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5312 return rc < 0 ? -errno : 0;
5315 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5316 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5321 fd = open(path, O_RDONLY);
5324 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5326 /* If we can't even open a file on the filesystem (e.g. with
5327 * -ESHUTDOWN), force caller to exit or it will loop forever. */
5331 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5338 #define MAX_STRING_SIZE 128
5340 int llapi_ping(char *obd_type, char *obd_name)
5342 int flags = O_RDONLY;
5343 char buf[1] = { 0 };
5347 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5348 obd_type, obd_name);
5352 fd = open(path.gl_pathv[0], flags);
5354 if (errno == EACCES && flags == O_RDONLY) {
5359 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5364 if (flags == O_RDONLY)
5365 rc = read(fd, buf, sizeof(buf));
5367 rc = write(fd, buf, sizeof(buf));
5375 cfs_free_param_data(&path);
5379 int llapi_target_iterate(int type_num, char **obd_type,
5380 void *args, llapi_cb_t cb)
5386 for (i = 0; i < type_num; i++) {
5389 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5393 for (j = 0; j < param.gl_pathc; j++) {
5394 char obd_uuid[UUID_MAX + 1];
5398 fp = fopen(param.gl_pathv[j], "r");
5401 llapi_error(LLAPI_MSG_ERROR, rc,
5402 "error: opening '%s'",
5407 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5409 llapi_error(LLAPI_MSG_ERROR, rc,
5410 "error: reading '%s'",
5415 /* Extract the obd_name from the sysfs path.
5416 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5418 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5424 /* skip /fs/lustre/'obd_type'/ */
5425 obd_name += strlen(obd_type[i]) + 12;
5426 /* chop off after obd_name */
5427 ptr = strrchr(obd_name, '/');
5431 cb(obd_type[i], obd_name, obd_uuid, args);
5440 cfs_free_param_data(¶m);
5444 static void do_target_check(char *obd_type_name, char *obd_name,
5445 char *obd_uuid, void *args)
5449 rc = llapi_ping(obd_type_name, obd_name);
5450 if (rc == ENOTCONN) {
5451 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5453 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5455 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5459 int llapi_target_check(int type_num, char **obd_type, char *dir)
5461 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5464 #undef MAX_STRING_SIZE
5466 /* Is this a lustre fs? */
5467 int llapi_is_lustre_mnttype(const char *type)
5469 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
5472 /* Is this a lustre client fs? */
5473 int llapi_is_lustre_mnt(struct mntent *mnt)
5475 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5476 strstr(mnt->mnt_fsname, ":/") != NULL);
5479 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5481 char fsname[PATH_MAX + 1];
5485 rc = llapi_search_fsname(mnt, fsname);
5489 root = open(mnt, O_RDONLY | O_DIRECTORY);
5492 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
5496 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
5504 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
5505 * format must have %s%s, buf must be > 16
5506 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
5507 * then buf = "lustre-MDT0000"
5509 static int get_mdtname(char *name, char *format, char *buf)
5511 char suffix[]="-MDT0000";
5512 int len = strlen(name);
5514 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
5515 name[len - 5] = '\0';
5520 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
5523 /* Not enough room to add suffix */
5524 llapi_err_noerrno(LLAPI_MSG_ERROR,
5525 "Invalid MDT name |%s|", name);
5530 return sprintf(buf, format, name, suffix);
5533 /** ioctl on filsystem root, with mdtindex sent as data
5534 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
5535 * \param mdtidxp pointer to integer within data to be filled in with the
5536 * mdt index (0 if no mdt is specified). NULL won't be filled.
5538 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
5546 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
5547 Open root and parse mdt index. */
5548 if (mdtname[0] == '/') {
5550 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
5551 (char *)mdtname, -1);
5553 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
5555 ptr = fsname + strlen(fsname) - 8;
5557 index = strtol(ptr + 4, NULL, 16);
5558 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
5562 llapi_err_noerrno(LLAPI_MSG_ERROR,
5563 "Can't open %s: %d\n", mdtname, rc);
5570 rc = ioctl(fd, opc, data);
5579 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
5580 int buflen, long long *recno, int *linkno)
5582 const char *fidstr_orig = fidstr;
5584 struct getinfo_fid2path *gf;
5589 while (*fidstr == '[')
5592 sscanf(fidstr, SFID, RFID(&fid));
5593 if (!fid_is_sane(&fid)) {
5594 llapi_err_noerrno(LLAPI_MSG_ERROR,
5595 "bad FID format '%s', should be [seq:oid:ver]"
5596 " (e.g. "DFID")\n", fidstr_orig,
5597 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
5601 gf = malloc(sizeof(*gf) + buflen);
5606 gf->gf_recno = *recno;
5607 gf->gf_linkno = *linkno;
5608 gf->gf_pathlen = buflen;
5610 /* Take path or fsname */
5611 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
5616 /* strip out instances of // */
5617 for (a = gf->gf_u.gf_path; *a != '\0'; a++) {
5618 if ((*a == '/') && (*(a + 1) == '/'))
5625 if (buf[0] == '\0') { /* ROOT path */
5630 *recno = gf->gf_recno;
5631 *linkno = gf->gf_linkno;
5638 static int fid_from_lma(const char *path, int fd, struct lu_fid *fid)
5641 struct lustre_mdt_attrs *lma;
5645 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
5647 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
5650 lma = (struct lustre_mdt_attrs *)buf;
5651 fid_le_to_cpu(fid, &lma->lma_self_fid);
5655 int llapi_get_mdt_index_by_fid(int fd, const struct lu_fid *fid,
5660 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
5669 int llapi_fd2fid(int fd, struct lu_fid *fid)
5673 memset(fid, 0, sizeof(*fid));
5675 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
5676 if (rc == -EINVAL || rc == -ENOTTY)
5677 rc = fid_from_lma(NULL, fd, fid);
5682 int llapi_path2fid(const char *path, struct lu_fid *fid)
5686 memset(fid, 0, sizeof(*fid));
5687 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5689 if (errno == ELOOP || errno == ENXIO)
5690 return fid_from_lma(path, -1, fid);
5694 rc = llapi_fd2fid(fd, fid);
5695 if (rc == -EINVAL || rc == -ENOTTY)
5696 rc = fid_from_lma(path, -1, fid);
5702 int llapi_fd2parent(int fd, unsigned int linkno, struct lu_fid *parent_fid,
5703 char *name, size_t name_size)
5705 struct getparent *gp;
5708 gp = malloc(sizeof(*gp) + name_size);
5712 gp->gp_linkno = linkno;
5713 gp->gp_name_size = name_size;
5715 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
5721 *parent_fid = gp->gp_fid;
5723 strncpy(name, gp->gp_name, name_size);
5724 name[name_size - 1] = '\0';
5731 int llapi_path2parent(const char *path, unsigned int linkno,
5732 struct lu_fid *parent_fid, char *name, size_t name_size)
5737 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5741 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
5746 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
5751 root = open(mnt, O_RDONLY | O_DIRECTORY);
5754 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
5758 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
5761 llapi_error(LLAPI_MSG_ERROR, rc,
5762 "ioctl on %s for getting connect flags failed", mnt);
5769 * Get a 64-bit value representing the version of file data pointed by fd.
5771 * Each write or truncate, flushed on OST, will change this value. You can use
5772 * this value to verify if file data was modified. This only checks the file
5773 * data, not metadata.
5775 * \param flags 0: no flush pages, usually used it the process has already
5777 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
5779 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
5780 * pages from clients.
5782 * \retval 0 on success.
5783 * \retval -errno on error.
5785 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
5788 struct ioc_data_version idv;
5790 idv.idv_flags = (__u32)flags;
5792 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5796 *data_version = idv.idv_version;
5802 * Flush cached pages from all clients.
5804 * \param fd File descriptor
5808 int llapi_file_flush(int fd)
5812 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);
5816 * Fetch layout version from OST objects. Layout version on OST objects are
5817 * only set when the file is a mirrored file AND after the file has been
5818 * written at least once.
5820 * It actually fetches the least layout version from the objects.
5822 int llapi_get_ost_layout_version(int fd, __u32 *layout_version)
5825 struct ioc_data_version idv = { 0 };
5827 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5831 *layout_version = idv.idv_layout_version;
5837 * Create a file without any name and open it for read/write
5839 * - file is created as if it were a standard file in the given \a directory
5840 * - file does not appear in \a directory and mtime does not change because
5841 * the filename is handled specially by the Lustre MDS.
5842 * - file is destroyed at final close
5844 * \param[in] directory directory from which to inherit layout/MDT idx
5845 * \param[in] mdt_idx MDT index on which the file is created,
5846 * \a idx == -1 means no specific MDT is requested
5847 * \param[in] mode standard open(2) mode
5848 * \param[in] stripe_param stripe parameters. May be NULL.
5850 * \retval a file descriptor on success.
5851 * \retval -errno on error.
5853 int llapi_create_volatile_param(const char *directory, int mdt_idx,
5854 int open_flags, mode_t mode,
5855 const struct llapi_stripe_param *stripe_param)
5857 char file_path[PATH_MAX];
5858 int saved_errno = errno;
5860 unsigned int rnumber;
5866 rc = snprintf(file_path, sizeof(file_path),
5867 "%s/" LUSTRE_VOLATILE_HDR "::%.4X",
5868 directory, rnumber);
5870 rc = snprintf(file_path, sizeof(file_path),
5871 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X",
5872 directory, mdt_idx, rnumber);
5874 if (rc < 0 || rc >= sizeof(file_path))
5875 return -ENAMETOOLONG;
5878 * Either open O_WRONLY or O_RDWR, creating RDONLY
5879 * is non-sensical here
5881 if ((open_flags & O_ACCMODE) == O_RDONLY)
5882 open_flags = O_RDWR | (open_flags & ~O_ACCMODE);
5884 open_flags |= O_CREAT | O_EXCL | O_NOFOLLOW;
5886 if (stripe_param != NULL) {
5887 fd = llapi_file_open_param(file_path, open_flags,
5888 mode, stripe_param);
5892 fd = open(file_path, open_flags, mode);
5896 } while (fd < 0 && rc == -EEXIST);
5899 llapi_error(LLAPI_MSG_ERROR, rc,
5900 "Cannot create volatile file '%s' in '%s'",
5901 file_path + strlen(directory) + 1 +
5902 LUSTRE_VOLATILE_HDR_LEN,
5908 * Unlink file in case this wasn't a Lustre filesystem and the magic
5909 * volatile filename wasn't handled as intended. The effect is the
5910 * same. If volatile open was supported then we expect unlink() to
5913 (void)unlink(file_path);
5916 * Since we are returning successfully we restore errno (and
5917 * mask out possible EEXIST from open() and ENOENT from unlink().
5919 errno = saved_errno;
5925 * Create a file without any name open it for read/write
5927 * - file is created as if it were a standard file in the given \a directory
5928 * - file does not appear in \a directory and mtime does not change because
5929 * the filename is handled specially by the Lustre MDS.
5930 * - file is removed at final close
5931 * - file modes are rw------- since it doesn't make sense to have a read-only
5932 * or write-only file that cannot be opened again.
5933 * - if user wants another mode it must use fchmod() on the open file, no
5934 * security problems arise because it cannot be opened by another process.
5936 * \param[in] directory directory from which to inherit layout/MDT idx
5937 * \param[in] idx MDT index on which the file is created,
5938 * \a idx == -1 means no specific MDT is requested
5939 * \param[in] open_flags standard open(2) flags
5941 * \retval a file descriptor on success.
5942 * \retval -errno on error.
5944 int llapi_create_volatile_idx(const char *directory, int mdt_idx,
5947 return llapi_create_volatile_param(directory, mdt_idx, open_flags,
5948 S_IRUSR | S_IWUSR, NULL);
5952 * Swap the layouts between 2 file descriptors
5953 * the 2 files must be open for writing
5954 * first fd received the ioctl, second fd is passed as arg
5955 * this is assymetric but avoid use of root path for ioctl
5957 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
5958 int gid, __u64 flags)
5960 struct lustre_swap_layouts lsl;
5965 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5966 rc = fstat(fd1, &st1);
5970 rc = fstat(fd2, &st2);
5975 lsl.sl_flags = flags;
5979 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
5983 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5984 struct timeval tv1[2];
5985 struct timeval tv2[2];
5987 memset(tv1, 0, sizeof(tv1));
5988 memset(tv2, 0, sizeof(tv2));
5990 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
5991 tv1[0].tv_sec = st1.st_atime;
5992 tv2[0].tv_sec = st2.st_atime;
5994 tv1[0].tv_sec = st2.st_atime;
5995 tv2[0].tv_sec = st1.st_atime;
5998 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
5999 tv1[1].tv_sec = st1.st_mtime;
6000 tv2[1].tv_sec = st2.st_mtime;
6002 tv1[1].tv_sec = st2.st_mtime;
6003 tv2[1].tv_sec = st1.st_mtime;
6006 rc = futimes(fd1, tv1);
6010 rc = futimes(fd2, tv2);
6018 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
6025 while (grp_id == 0);
6027 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
6035 * Swap the layouts between 2 files
6036 * the 2 files are open in write
6038 int llapi_swap_layouts(const char *path1, const char *path2,
6039 __u64 dv1, __u64 dv2, __u64 flags)
6043 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
6046 llapi_error(LLAPI_MSG_ERROR, rc,
6047 "error: cannot open '%s' for write", path1);
6051 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
6054 llapi_error(LLAPI_MSG_ERROR, rc,
6055 "error: cannot open '%s' for write", path2);
6059 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
6061 llapi_error(LLAPI_MSG_ERROR, rc,
6062 "error: cannot swap layout between '%s' and '%s'",
6073 * Attempt to open a file with Lustre file identifier \a fid
6074 * and return an open file descriptor.
6076 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
6077 * \param[in] fid Lustre file identifier of file to open
6078 * \param[in] flags open() flags
6080 * \retval non-negative file descriptor on successful open
6081 * \retval -1 if an error occurred
6083 int llapi_open_by_fid(const char *lustre_dir, const struct lu_fid *fid,
6086 char mntdir[PATH_MAX];
6087 char path[PATH_MAX + 64];
6090 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
6094 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
6095 return open(path, flags);
6101 * \param fd File to lock.
6102 * \param gid Group Identifier.
6104 * \retval 0 on success.
6105 * \retval -errno on failure.
6107 int llapi_group_lock(int fd, int gid)
6111 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
6114 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
6122 * \param fd File to unlock.
6123 * \param gid Group Identifier.
6125 * \retval 0 on success.
6126 * \retval -errno on failure.
6128 int llapi_group_unlock(int fd, int gid)
6132 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
6135 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");