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 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1460 int llapi_getname(const char *path, char *buf, size_t size)
1462 struct obd_uuid uuid_buf;
1463 char *uuid = uuid_buf.uuid;
1465 int rc, len, fsname_len;
1467 memset(&uuid_buf, 0, sizeof(uuid_buf));
1468 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1473 * We want to turn testfs-clilov-ffff88002738bc00 into
1474 * testfs-ffff88002738bc00 in a portable way that doesn't depend
1475 * on what is after "-clilov-" as it may change in the future.
1476 * Unfortunately, the "fsname" part may contain a dash, so we
1477 * can't just skip to the first dash, and the "instance" may be a
1478 * UUID in the future, so we can't necessarily go to the last dash.
1480 cfg_instance = strstr(uuid, "-clilov-");
1484 fsname_len = cfg_instance - uuid;
1485 cfg_instance += strlen("-clilov-");
1486 len = snprintf(buf, size, "%.*s-%s", fsname_len, uuid, cfg_instance);
1495 * Get the list of pool members.
1496 * \param poolname string of format \<fsname\>.\<poolname\>
1497 * \param members caller-allocated array of char*
1498 * \param list_size size of the members array
1499 * \param buffer caller-allocated buffer for storing OST names
1500 * \param buffer_size size of the buffer
1502 * \return number of members retrieved for this pool
1503 * \retval -error failure
1505 int llapi_get_poolmembers(const char *poolname, char **members,
1506 int list_size, char *buffer, int buffer_size)
1508 char fsname[PATH_MAX];
1517 /* name is FSNAME.POOLNAME */
1518 if (strlen(poolname) >= sizeof(fsname))
1521 snprintf(fsname, sizeof(fsname), "%s", poolname);
1522 pool = strchr(fsname, '.');
1529 rc = poolpath(&pathname, fsname, NULL);
1531 llapi_error(LLAPI_MSG_ERROR, rc,
1532 "Lustre filesystem '%s' not found",
1537 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1538 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1539 cfs_free_param_data(&pathname);
1540 if (rc >= sizeof(buf))
1542 fd = fopen(buf, "r");
1545 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1550 while (fgets(buf, sizeof(buf), fd) != NULL) {
1551 if (nb_entries >= list_size) {
1555 buf[sizeof(buf) - 1] = '\0';
1557 tmp = strchr(buf, '\n');
1560 if (used + strlen(buf) + 1 > buffer_size) {
1565 strcpy(buffer + used, buf);
1566 members[nb_entries] = buffer + used;
1567 used += strlen(buf) + 1;
1577 * Get the list of pools in a filesystem.
1578 * \param name filesystem name or path
1579 * \param poollist caller-allocated array of char*
1580 * \param list_size size of the poollist array
1581 * \param buffer caller-allocated buffer for storing pool names
1582 * \param buffer_size size of the buffer
1584 * \return number of pools retrieved for this filesystem
1585 * \retval -error failure
1587 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1588 char *buffer, int buffer_size)
1594 struct dirent *pool;
1596 unsigned int nb_entries = 0;
1597 unsigned int used = 0;
1600 /* initialize output array */
1601 for (i = 0; i < list_size; i++)
1604 /* is name a pathname ? */
1605 ptr = strchr(name, '/');
1607 char fsname_buf[MAXNAMLEN];
1609 /* We will need fsname for printing later */
1610 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1614 ptr = strrchr(fsname_buf, '-');
1618 fsname = strdup(fsname_buf);
1622 /* name is FSNAME */
1623 fsname = strdup(name);
1628 rc = poolpath(&pathname, fsname, NULL);
1630 llapi_error(LLAPI_MSG_ERROR, rc,
1631 "Lustre filesystem '%s' not found", name);
1635 dir = opendir(pathname.gl_pathv[0]);
1638 llapi_error(LLAPI_MSG_ERROR, rc,
1639 "Could not open pool list for '%s'",
1646 pool = readdir(dir);
1652 /* ignore . and .. */
1653 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1656 /* check output bounds */
1657 if (nb_entries >= list_size) {
1659 goto free_dir_no_msg;
1662 /* +2 for '.' and final '\0' */
1663 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1666 goto free_dir_no_msg;
1669 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1670 poollist[nb_entries] = buffer + used;
1671 used += strlen(pool->d_name) + strlen(fsname) + 2;
1677 llapi_error(LLAPI_MSG_ERROR, rc,
1678 "Error reading pool list for '%s'", name);
1680 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1685 cfs_free_param_data(&pathname);
1688 return rc != 0 ? rc : nb_entries;
1691 /* wrapper for lfs.c and obd.c */
1692 int llapi_poollist(const char *name)
1694 /* list of pool names (assume that pool count is smaller
1696 char **list, *buffer = NULL, *fsname = (char *)name;
1697 char *poolname = NULL, *tmp = NULL, data[16];
1698 enum param_filter type = FILTER_BY_PATH;
1699 int obdcount, bufsize, rc, nb, i;
1704 if (name[0] != '/') {
1705 fsname = strdup(name);
1709 poolname = strchr(fsname, '.');
1712 type = FILTER_BY_FS_NAME;
1715 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1716 data, sizeof(data));
1719 obdcount = atoi(data);
1721 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1722 * and also an array to store the pointers for all that
1723 * allocated space. */
1725 bufsize = sizeof(struct obd_uuid) * obdcount;
1726 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1727 if (buffer == NULL) {
1731 list = (char **) (buffer + bufsize);
1734 /* name is a path or fsname */
1735 nb = llapi_get_poollist(name, list, obdcount,
1738 /* name is a pool name (<fsname>.<poolname>) */
1739 nb = llapi_get_poolmembers(name, list, obdcount,
1743 if (nb == -EOVERFLOW) {
1746 goto retry_get_pools;
1749 for (i = 0; i < nb; i++)
1750 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1751 rc = (nb < 0 ? nb : 0);
1755 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1760 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1761 void *data, struct dirent64 *de);
1763 #define OBD_NOT_FOUND (-1)
1765 static void find_param_fini(struct find_param *param)
1767 if (param->fp_migrate)
1770 if (param->fp_obd_indexes) {
1771 free(param->fp_obd_indexes);
1772 param->fp_obd_indexes = NULL;
1775 if (param->fp_lmd) {
1776 free(param->fp_lmd);
1777 param->fp_lmd = NULL;
1780 if (param->fp_lmv_md) {
1781 free(param->fp_lmv_md);
1782 param->fp_lmv_md = NULL;
1786 static int common_param_init(struct find_param *param, char *path)
1788 int lum_size = get_mds_md_size(path);
1793 /* migrate has fp_lmv_md initialized outside */
1794 if (param->fp_migrate)
1797 if (lum_size < PATH_MAX + 1)
1798 lum_size = PATH_MAX + 1;
1800 param->fp_lum_size = lum_size;
1801 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1803 if (param->fp_lmd == NULL) {
1804 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1805 "error: allocate %zu bytes for layout failed",
1806 sizeof(lstat_t) + param->fp_lum_size);
1810 param->fp_lmv_stripe_count = 256;
1811 param->fp_lmv_md = calloc(1,
1812 lmv_user_md_size(param->fp_lmv_stripe_count,
1813 LMV_USER_MAGIC_SPECIFIC));
1814 if (param->fp_lmv_md == NULL) {
1815 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1816 "error: allocation of %d bytes for ioctl",
1817 lmv_user_md_size(param->fp_lmv_stripe_count,
1818 LMV_USER_MAGIC_SPECIFIC));
1819 find_param_fini(param);
1823 param->fp_got_uuids = 0;
1824 param->fp_obd_indexes = NULL;
1825 param->fp_obd_index = OBD_NOT_FOUND;
1826 param->fp_mdt_index = OBD_NOT_FOUND;
1830 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1831 struct dirent64 *de)
1833 struct find_param *param = data;
1839 /* set errno upon failure */
1840 static DIR *opendir_parent(const char *path)
1846 path_copy = strdup(path);
1847 if (path_copy == NULL)
1850 parent_path = dirname(path_copy);
1851 parent = opendir(parent_path);
1857 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1862 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1863 if (param->fp_get_default_lmv)
1864 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1866 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1868 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1869 if (errno == E2BIG && ret != 0) {
1873 /* if foreign LMV case, fake stripes number */
1874 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
1875 struct lmv_foreign_md *lfm;
1877 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1878 if (lfm->lfm_length < XATTR_SIZE_MAX -
1879 offsetof(typeof(*lfm), lfm_value)) {
1880 uint32_t size = lfm->lfm_length +
1881 offsetof(typeof(*lfm), lfm_value);
1883 stripe_count = lmv_foreign_to_md_stripes(size);
1885 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1886 "error: invalid %d foreign size "
1887 "returned from ioctl",
1892 stripe_count = param->fp_lmv_md->lum_stripe_count;
1894 if (stripe_count <= param->fp_lmv_stripe_count)
1897 free(param->fp_lmv_md);
1898 param->fp_lmv_stripe_count = stripe_count;
1899 lmv_size = lmv_user_md_size(stripe_count,
1900 LMV_USER_MAGIC_SPECIFIC);
1901 param->fp_lmv_md = malloc(lmv_size);
1902 if (param->fp_lmv_md == NULL) {
1903 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1904 "error: allocation of %d bytes for ioctl",
1905 lmv_user_md_size(param->fp_lmv_stripe_count,
1906 LMV_USER_MAGIC_SPECIFIC));
1914 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1917 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
1918 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
1919 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
1920 lmd_v2->lmd_stx.stx_uid = st->st_uid;
1921 lmd_v2->lmd_stx.stx_gid = st->st_gid;
1922 lmd_v2->lmd_stx.stx_mode = st->st_mode;
1923 lmd_v2->lmd_stx.stx_ino = st->st_ino;
1924 lmd_v2->lmd_stx.stx_size = st->st_size;
1925 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
1926 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
1927 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
1928 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
1929 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
1930 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
1931 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
1932 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
1933 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
1935 lmd_v2->lmd_flags = 0;
1937 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1939 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
1940 if (lmd_v2->lmd_stx.stx_size)
1941 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
1942 if (lmd_v2->lmd_stx.stx_blocks)
1943 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
1945 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1946 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
1947 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
1952 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
1954 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
1955 struct lov_user_mds_data *lmd_v2 = lmdbuf;
1959 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
1963 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
1966 st = lmd_v1->lmd_st;
1967 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
1968 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
1969 convert_lmd_statx(lmd_v2, &st, false);
1970 lmd_v2->lmd_lmmsize = 0;
1971 lmd_v2->lmd_padding = 0;
1976 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
1977 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1979 struct lov_user_mds_data *lmd = lmdbuf;
1980 static bool use_old_ioctl;
1984 if (parent_fd < 0 && dir_fd < 0)
1986 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1990 /* LL_IOC_MDC_GETINFO operates on the current directory inode
1991 * and returns struct lov_user_mds_data, while
1992 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
1994 if (type == GET_LMD_INFO)
1995 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_OLD :
1998 cmd = LL_IOC_LOV_GETSTRIPE;
2001 ret = ioctl(dir_fd, cmd, lmdbuf);
2002 if (ret < 0 && errno == ENOTTY && cmd == LL_IOC_MDC_GETINFO) {
2003 cmd = LL_IOC_MDC_GETINFO_OLD;
2004 use_old_ioctl = true;
2008 if (cmd == LL_IOC_MDC_GETINFO_OLD && !ret)
2009 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2010 } else if (parent_fd >= 0) {
2011 const char *fname = strrchr(path, '/');
2013 /* IOC_MDC_GETFILEINFO takes as input the filename (relative to
2014 * the parent directory) and returns struct lov_user_mds_data,
2015 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2017 * This avoids opening, locking, and closing each file on the
2018 * client if that is not needed. Multiple of these ioctl() can
2019 * be done on the parent dir with a single open for all
2020 * files in that directory, and it also doesn't pollute the
2021 * client dcache with millions of dentries when traversing
2022 * a large filesystem.
2024 fname = (fname == NULL ? path : fname + 1);
2026 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2029 else if (ret >= lmdlen || ret++ == 0)
2032 if (type == GET_LMD_INFO)
2033 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_OLD :
2034 IOC_MDC_GETFILEINFO;
2036 cmd = IOC_MDC_GETFILESTRIPE;
2039 ret = ioctl(parent_fd, cmd, lmdbuf);
2040 if (ret < 0 && errno == ENOTTY &&
2041 cmd == IOC_MDC_GETFILEINFO) {
2042 cmd = IOC_MDC_GETFILEINFO_OLD;
2043 use_old_ioctl = true;
2044 goto retry_getfileinfo;
2047 if (cmd == IOC_MDC_GETFILEINFO_OLD && !ret)
2048 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2052 if (ret && type == GET_LMD_INFO) {
2053 if (errno == ENOTTY) {
2056 /* ioctl is not supported, it is not a lustre fs.
2057 * Do the regular lstat(2) instead.
2059 ret = lstat_f(path, &st);
2062 llapi_error(LLAPI_MSG_ERROR, ret,
2063 "error: %s: lstat failed for %s",
2067 convert_lmd_statx(lmd, &st, true);
2068 /* It may be wrong to set use_old_ioctl with true as
2069 * the file is not a lustre fs. So reset it with false
2072 use_old_ioctl = false;
2073 } else if (errno == ENOENT) {
2075 llapi_error(LLAPI_MSG_WARN, ret,
2076 "warning: %s does not exist", path);
2077 } else if (errno != EISDIR && errno != ENODATA) {
2079 llapi_error(LLAPI_MSG_ERROR, ret,
2080 "%s ioctl failed for %s.",
2081 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2082 "IOC_MDC_GETFILEINFO", path);
2089 static int get_lmd_info(char *path, DIR *parent, DIR *dir, void *lmdbuf,
2090 int lmdlen, enum get_lmd_info_type type)
2096 parent_fd = dirfd(parent);
2098 dir_fd = dirfd(dir);
2100 return get_lmd_info_fd(path, parent_fd, dir_fd, lmdbuf, lmdlen, type);
2103 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
2104 semantic_func_t sem_init,
2105 semantic_func_t sem_fini, void *data,
2106 struct dirent64 *de)
2108 struct find_param *param = (struct find_param *)data;
2109 struct dirent64 *dent;
2117 if (!d && errno != ENOTDIR) {
2119 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2122 } else if (!d && !parent) {
2123 /* ENOTDIR. Open the parent dir. */
2124 p = opendir_parent(path);
2131 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
2137 while ((dent = readdir64(d)) != NULL) {
2140 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2144 if ((len + dent->d_reclen + 2) > size) {
2145 llapi_err_noerrno(LLAPI_MSG_ERROR,
2146 "error: %s: string buffer too small",
2151 strcat(path, dent->d_name);
2153 if (dent->d_type == DT_UNKNOWN) {
2154 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
2156 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
2157 param->fp_lum_size, GET_LMD_INFO);
2159 dent->d_type = IFTODT(stx->stx_mode);
2166 switch (dent->d_type) {
2168 llapi_err_noerrno(LLAPI_MSG_ERROR,
2169 "error: %s: '%s' is UNKNOWN type %d",
2170 __func__, dent->d_name, dent->d_type);
2173 rc = llapi_semantic_traverse(path, size, d, sem_init,
2174 sem_fini, data, dent);
2175 if (rc != 0 && ret == 0)
2181 rc = sem_init(path, d, NULL, data, dent);
2182 if (rc < 0 && ret == 0) {
2187 if (sem_fini && rc == 0)
2188 sem_fini(path, d, NULL, data, dent);
2196 sem_fini(path, parent, &d, data, de);
2205 static int param_callback(char *path, semantic_func_t sem_init,
2206 semantic_func_t sem_fini, struct find_param *param)
2208 int ret, len = strlen(path);
2211 if (len > PATH_MAX) {
2213 llapi_error(LLAPI_MSG_ERROR, ret,
2214 "Path name '%s' is too long", path);
2218 buf = (char *)malloc(PATH_MAX + 1);
2222 snprintf(buf, PATH_MAX + 1, "%s", path);
2223 ret = common_param_init(param, buf);
2227 param->fp_depth = 0;
2229 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
2230 sem_fini, param, NULL);
2232 find_param_fini(param);
2234 return ret < 0 ? ret : 0;
2237 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2239 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
2243 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2248 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2250 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2253 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2258 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2262 fd = open(path, O_RDONLY | O_NONBLOCK);
2265 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
2269 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2275 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2279 fd = open(path, O_RDONLY | O_NONBLOCK);
2282 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2286 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2298 * If uuidp is NULL, return the number of available obd uuids.
2299 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2300 * there are more OSTs than allocated to uuidp, then an error is returned with
2301 * the ost_count set to number of available obd uuids.
2303 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2304 int *ost_count, enum tgt_type type)
2306 char buf[PATH_MAX], format[32];
2307 int rc = 0, index = 0;
2308 struct obd_uuid name;
2312 /* Get the lov name */
2313 if (type == LOV_TYPE)
2314 rc = llapi_file_fget_lov_uuid(fd, &name);
2316 rc = llapi_file_fget_lmv_uuid(fd, &name);
2320 /* Now get the ost uuids */
2321 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2322 FILTER_BY_EXACT, "target_obd", ¶m);
2326 fp = fopen(param.gl_pathv[0], "r");
2329 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2334 snprintf(format, sizeof(format),
2335 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2336 while (fgets(buf, sizeof(buf), fp) != NULL) {
2337 if (uuidp && (index < *ost_count)) {
2338 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2346 if (uuidp && (index > *ost_count))
2351 cfs_free_param_data(¶m);
2355 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2357 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2360 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2365 root = open(mnt, O_RDONLY | O_DIRECTORY);
2368 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2373 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2381 /* Check if user specified value matches a real uuid. Ignore _UUID,
2382 * -osc-4ba41334, other trailing gunk in comparison.
2383 * @param real_uuid ends in "_UUID"
2384 * @param search_uuid may or may not end in "_UUID"
2386 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2388 int cmplen = strlen(real_uuid);
2389 int searchlen = strlen(search_uuid);
2391 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2393 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2396 /* The UUIDs may legitimately be different lengths, if
2397 * the system was upgraded from an older version. */
2398 if (cmplen != searchlen)
2401 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2404 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2405 * returned in param->fp_obd_index */
2406 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2408 struct obd_uuid obd_uuid;
2415 if (param->fp_got_uuids)
2418 /* Get the lov/lmv name */
2419 if (param->fp_get_lmv)
2420 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2422 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2424 if (rc != -ENOTTY) {
2425 llapi_error(LLAPI_MSG_ERROR, rc,
2426 "error: can't get %s name: %s",
2427 param->fp_get_lmv ? "lmv" : "lov",
2435 param->fp_got_uuids = 1;
2437 /* Now get the ost uuids */
2438 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2439 obd_uuid.uuid, FILTER_BY_EXACT,
2440 "target_obd", ¶m_data);
2444 fp = fopen(param_data.gl_pathv[0], "r");
2447 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2448 param_data.gl_pathv[0]);
2452 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2453 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2454 param->fp_get_lmv ? "MDTS" : "OBDS");
2456 snprintf(format, sizeof(format),
2457 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2458 while (fgets(buf, sizeof(buf), fp) != NULL) {
2461 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2464 if (param->fp_obd_uuid) {
2465 if (llapi_uuid_match(obd_uuid.uuid,
2466 param->fp_obd_uuid->uuid)) {
2467 param->fp_obd_index = index;
2470 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2471 /* Print everything */
2472 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2475 param->fp_obds_printed = 1;
2479 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2480 llapi_err_noerrno(LLAPI_MSG_ERROR,
2481 "error: %s: unknown obduuid: %s",
2482 __func__, param->fp_obd_uuid->uuid);
2486 cfs_free_param_data(¶m_data);
2490 /* In this case, param->fp_obd_uuid will be an array of obduuids and
2491 * obd index for all these obduuids will be returned in
2492 * param->fp_obd_indexes */
2493 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2494 int num_obds, int **obdindexes, int *obdindex,
2497 int ret, obdcount, obd_valid = 0, obdnum;
2499 struct obd_uuid *uuids = NULL;
2503 if (type == LOV_TYPE)
2504 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2506 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2510 obdcount = atoi(buf);
2511 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2516 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2518 if (ret == -EOVERFLOW) {
2519 struct obd_uuid *uuids_temp;
2521 uuids_temp = realloc(uuids, obdcount *
2522 sizeof(struct obd_uuid));
2523 if (uuids_temp != NULL) {
2525 goto retry_get_uuids;
2530 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2534 indexes = malloc(num_obds * sizeof(*obdindex));
2535 if (indexes == NULL) {
2540 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2543 /* The user may have specified a simple index */
2544 i = strtol(obduuids[obdnum].uuid, &end, 0);
2545 if (end && *end == '\0' && i < obdcount) {
2546 indexes[obdnum] = i;
2549 for (i = 0; i < obdcount; i++) {
2550 if (llapi_uuid_match(uuids[i].uuid,
2551 obduuids[obdnum].uuid)) {
2552 indexes[obdnum] = i;
2558 if (i >= obdcount) {
2559 indexes[obdnum] = OBD_NOT_FOUND;
2560 llapi_err_noerrno(LLAPI_MSG_ERROR,
2561 "invalid obduuid '%s'",
2562 obduuids[obdnum].uuid);
2568 *obdindex = OBD_NOT_FOUND;
2570 *obdindex = obd_valid;
2572 *obdindexes = indexes;
2580 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2584 if (param->fp_mdt_uuid) {
2585 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2587 ¶m->fp_mdt_indexes,
2588 ¶m->fp_mdt_index, LMV_TYPE);
2593 if (param->fp_obd_uuid) {
2594 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2596 ¶m->fp_obd_indexes,
2597 ¶m->fp_obd_index, LOV_TYPE);
2602 param->fp_got_uuids = 1;
2607 int llapi_ostlist(char *path, struct find_param *param)
2612 fd = open(path, O_RDONLY | O_DIRECTORY);
2616 ret = setup_obd_uuid(fd, path, param);
2623 * Tries to determine the default stripe attributes for a given filesystem. The
2624 * filesystem to check should be specified by fsname, or will be determined
2627 static int sattr_get_defaults(const char *const fsname,
2628 unsigned int *scount,
2629 unsigned int *ssize,
2630 unsigned int *soffset)
2636 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2637 "stripecount", val, sizeof(val));
2640 *scount = atoi(val);
2644 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2645 "stripesize", val, sizeof(val));
2652 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2653 "stripeoffset", val, sizeof(val));
2656 *soffset = atoi(val);
2663 * Tries to gather the default stripe attributes for a given filesystem. If
2664 * the attributes can be determined, they are cached for easy retreival the
2665 * next time they are needed. Only a single filesystem's attributes are
2668 int sattr_cache_get_defaults(const char *const fsname,
2669 const char *const pathname, unsigned int *scount,
2670 unsigned int *ssize, unsigned int *soffset)
2673 char fsname[PATH_MAX + 1];
2674 unsigned int stripecount;
2675 unsigned int stripesize;
2676 unsigned int stripeoffset;
2682 char fsname_buf[PATH_MAX + 1];
2683 unsigned int tmp[3];
2685 if (fsname == NULL) {
2686 rc = llapi_search_fsname(pathname, fsname_buf);
2690 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2693 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2695 * Ensure all 3 sattrs (count, size, and offset) are
2696 * successfully retrieved and stored in tmp before writing to
2699 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2703 cache.stripecount = tmp[0];
2704 cache.stripesize = tmp[1];
2705 cache.stripeoffset = tmp[2];
2706 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2710 *scount = cache.stripecount;
2712 *ssize = cache.stripesize;
2714 *soffset = cache.stripeoffset;
2719 static char *layout2name(__u32 layout_pattern)
2721 if (layout_pattern & LOV_PATTERN_F_RELEASED)
2723 else if (layout_pattern == LOV_PATTERN_MDT)
2725 else if (layout_pattern == LOV_PATTERN_RAID0)
2727 else if (layout_pattern ==
2728 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2729 return "raid0,overstriped";
2734 enum lov_dump_flags {
2735 LDF_IS_DIR = 0x0001,
2736 LDF_IS_RAW = 0x0002,
2737 LDF_INDENT = 0x0004,
2738 LDF_SKIP_OBJS = 0x0008,
2740 LDF_EXTENSION = 0x0020,
2743 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2744 struct lov_user_ost_data_v1 *objects,
2745 enum llapi_layout_verbose verbose,
2746 int depth, char *pool_name,
2747 enum lov_dump_flags flags)
2749 bool is_dir = flags & LDF_IS_DIR;
2750 bool is_raw = flags & LDF_IS_RAW;
2751 bool indent = flags & LDF_INDENT;
2752 bool yaml = flags & LDF_YAML;
2753 bool skip_objs = flags & LDF_SKIP_OBJS;
2754 bool extension = flags & LDF_EXTENSION;
2755 char *prefix = is_dir ? "" : "lmm_";
2756 char *separator = "";
2757 char *space = indent ? " " : "";
2760 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2761 lmm_oi_set_seq(&lum->lmm_oi, 0);
2762 if (!indent && (verbose & VERBOSE_DETAIL))
2763 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2766 if (!yaml && !indent && depth && path &&
2767 ((verbose != VERBOSE_OBJID) || !is_dir))
2768 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2770 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2771 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2772 space, prefix, lum->lmm_magic);
2773 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2775 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2776 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2778 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2780 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2785 if (verbose & ~VERBOSE_DFID)
2786 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2788 /* This needs a bit of hand-holding since old 1.x lmm_oi
2789 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2790 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2791 * a real FID. Ideally the 2.x code would have stored this
2792 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2793 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2794 * worked properly (especially since IGIF FIDs use mds_inum as
2795 * the FID SEQ), but unfortunately that didn't happen.
2797 * Print it to look like an IGIF FID, even though the fields
2798 * are reversed on disk, so that it makes sense to userspace.
2800 * Don't use ostid_id() and ostid_seq(), since they assume the
2801 * oi_fid fields are in the right order. This is why there are
2802 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2804 * For newer layout types hopefully this will be a real FID. */
2805 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2806 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2807 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2808 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2809 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2811 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2812 (unsigned long long)seq, oid, ver);
2814 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2815 (unsigned long long)seq, oid, ver);
2818 if (verbose & VERBOSE_STRIPE_COUNT) {
2819 if (verbose & ~VERBOSE_STRIPE_COUNT)
2820 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2823 if (!is_raw && lum->lmm_stripe_count == 0 &&
2824 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2825 unsigned int scount;
2826 rc = sattr_cache_get_defaults(NULL, path,
2830 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2833 llapi_error(LLAPI_MSG_ERROR, rc,
2834 "Cannot determine default"
2837 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2839 (__s16)lum->lmm_stripe_count);
2842 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2844 (__s16)lum->lmm_stripe_count);
2846 if (!yaml && is_dir)
2852 if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
2853 ((verbose & VERBOSE_EXT_SIZE) && extension)) {
2854 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2855 if (verbose & ~VERBOSE_EXT_SIZE && extension)
2856 llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
2858 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
2859 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2861 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2863 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2866 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2868 llapi_error(LLAPI_MSG_ERROR, rc,
2869 "Cannot determine default"
2872 /* Extension size is in KiB */
2873 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2875 (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
2876 (unsigned long long)lum->lmm_stripe_size);
2878 if (!yaml && is_dir)
2884 if ((verbose & VERBOSE_PATTERN)) {
2885 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2886 if (verbose & ~VERBOSE_PATTERN)
2887 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2889 if (lov_pattern_supported(lum->lmm_pattern))
2890 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2891 layout2name(lum->lmm_pattern));
2893 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2894 separator = (!yaml && is_dir) ? " " : "\n";
2897 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2898 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2899 if (verbose & ~VERBOSE_GENERATION)
2900 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2902 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2903 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2907 if (verbose & VERBOSE_STRIPE_OFFSET) {
2908 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2909 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2910 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2912 if (is_dir || skip_objs)
2913 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2914 lum->lmm_stripe_offset ==
2915 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2916 lum->lmm_stripe_offset);
2917 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
2918 llapi_printf(LLAPI_MSG_NORMAL, "0");
2920 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2921 objects[0].l_ost_idx);
2922 if (!yaml && is_dir)
2928 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2929 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2930 if (verbose & ~VERBOSE_POOL)
2931 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2933 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2934 if (!yaml && is_dir)
2940 if (strlen(separator) != 0)
2941 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2944 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2945 struct lov_user_ost_data_v1 *objects,
2946 char *path, int obdindex, int depth,
2947 enum llapi_layout_verbose verbose,
2948 enum lov_dump_flags flags)
2950 bool is_dir = flags & LDF_IS_DIR;
2951 bool indent = flags & LDF_INDENT;
2952 bool skip_objs = flags & LDF_SKIP_OBJS;
2953 bool yaml = flags & LDF_YAML;
2954 bool obdstripe = obdindex == OBD_NOT_FOUND;
2957 if (!obdstripe && !skip_objs) {
2958 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2959 if (obdindex == objects[i].l_ost_idx) {
2969 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2972 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
2973 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2974 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
2975 char *space = " - ";
2978 llapi_printf(LLAPI_MSG_NORMAL,
2979 "%6slmm_objects:\n", " ");
2981 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2983 llapi_printf(LLAPI_MSG_NORMAL,
2984 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2986 for (i = 0; i < lum->lmm_stripe_count; i++) {
2987 int idx = objects[i].l_ost_idx;
2988 long long oid = ostid_id(&objects[i].l_ost_oi);
2989 long long gr = ostid_seq(&objects[i].l_ost_oi);
2991 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2995 struct lu_fid fid = { 0 };
2997 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2998 llapi_printf(LLAPI_MSG_NORMAL,
2999 "%sl_ost_idx: %d\n", space, idx);
3000 llapi_printf(LLAPI_MSG_NORMAL,
3001 "%8sl_fid: "DFID_NOBRACE"\n",
3003 } else if (indent) {
3004 struct lu_fid fid = { 0 };
3006 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3007 llapi_printf(LLAPI_MSG_NORMAL,
3008 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
3009 space, i, idx, PFID(&fid));
3012 sprintf(fmt, "%s%s%s\n",
3013 "\t%6u\t%14llu\t%#13llx\t",
3014 (fid_seq_is_rsvd(gr) ||
3015 fid_seq_is_mdt0(gr)) ?
3016 "%14llu" : "%#14llx", "%s");
3017 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
3019 obdindex == idx ? " *" : "");
3023 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3026 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3027 char *path, int obdindex, int depth,
3028 enum llapi_layout_verbose verbose,
3029 enum lov_dump_flags flags)
3031 struct lmv_user_mds_data *objects = lum->lum_objects;
3032 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3033 char *separator = "";
3034 bool yaml = flags & LDF_YAML;
3035 bool obdstripe = false;
3038 if (obdindex != OBD_NOT_FOUND) {
3039 if (lum->lum_stripe_count == 0) {
3040 if (obdindex == lum->lum_stripe_offset)
3043 for (i = 0; i < lum->lum_stripe_count; i++) {
3044 if (obdindex == objects[i].lum_mds) {
3045 llapi_printf(LLAPI_MSG_NORMAL,
3060 /* show all information default */
3062 if (lum->lum_magic == LMV_USER_MAGIC)
3063 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3064 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3066 verbose = VERBOSE_OBJID;
3069 if (depth && path && ((verbose != VERBOSE_OBJID)))
3070 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3072 if (verbose & VERBOSE_STRIPE_COUNT) {
3073 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3074 if (verbose & ~VERBOSE_STRIPE_COUNT)
3075 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3076 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3077 (int)lum->lum_stripe_count);
3078 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3084 if (verbose & VERBOSE_STRIPE_OFFSET) {
3085 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3086 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3087 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3088 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3089 (int)lum->lum_stripe_offset);
3090 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3096 if (verbose & VERBOSE_HASH_TYPE) {
3097 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3098 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3100 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3101 if (verbose & ~VERBOSE_HASH_TYPE)
3102 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3103 if (type < LMV_HASH_TYPE_MAX)
3104 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3105 mdt_hash_name[type]);
3107 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3109 if (flags & LMV_HASH_FLAG_SPACE)
3110 llapi_printf(LLAPI_MSG_NORMAL, ",space");
3111 if (flags & LMV_HASH_FLAG_MIGRATION)
3112 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3113 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3114 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3115 if (flags & LMV_HASH_FLAG_LOST_LMV)
3116 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3121 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3122 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3123 if (lum->lum_stripe_count > 0)
3124 llapi_printf(LLAPI_MSG_NORMAL,
3125 "mdtidx\t\t FID[seq:oid:ver]\n");
3126 for (i = 0; i < lum->lum_stripe_count; i++) {
3127 int idx = objects[i].lum_mds;
3128 struct lu_fid *fid = &objects[i].lum_fid;
3129 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3130 llapi_printf(LLAPI_MSG_NORMAL,
3131 "%6u\t\t "DFID"\t\t%s\n",
3133 obdindex == idx ? " *" : "");
3138 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3139 pool_name[0] != '\0') {
3140 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3141 if (verbose & ~VERBOSE_POOL)
3142 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3144 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3148 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3149 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3152 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3153 enum lov_dump_flags flags)
3155 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3156 int depth = param->fp_max_depth;
3157 enum llapi_layout_verbose verbose = param->fp_verbose;
3158 bool yaml = flags & LDF_YAML;
3160 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3161 !(flags & LDF_IS_DIR)) && !yaml)
3162 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3164 if (verbose & VERBOSE_DETAIL) {
3165 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3166 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3167 " ", comp_v1->lcm_magic);
3168 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3169 " ", comp_v1->lcm_size);
3170 if (flags & LDF_IS_DIR)
3171 llapi_printf(LLAPI_MSG_NORMAL,
3172 "%2slcm_flags: %s\n", " ",
3173 comp_v1->lcm_mirror_count > 0 ?
3176 llapi_printf(LLAPI_MSG_NORMAL,
3177 "%2slcm_flags: %s\n", " ",
3178 llapi_layout_flags_string(comp_v1->lcm_flags));
3181 if (verbose & VERBOSE_GENERATION) {
3182 if (verbose & ~VERBOSE_GENERATION)
3183 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3185 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3188 if (verbose & VERBOSE_MIRROR_COUNT) {
3189 if (verbose & ~VERBOSE_MIRROR_COUNT)
3190 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3192 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3193 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3194 comp_v1->lcm_mirror_count + 1 : 1);
3197 if (verbose & VERBOSE_COMP_COUNT) {
3198 if (verbose & ~VERBOSE_COMP_COUNT)
3199 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3201 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3202 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3203 comp_v1->lcm_entry_count : 0);
3206 if (verbose & VERBOSE_DETAIL && !yaml)
3207 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3210 static void lcme_flags2str(__u32 comp_flags)
3216 llapi_printf(LLAPI_MSG_NORMAL, "0");
3219 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3220 if (comp_flags & comp_flags_table[i].cfn_flag) {
3222 llapi_printf(LLAPI_MSG_NORMAL, ",");
3223 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3224 comp_flags_table[i].cfn_name);
3225 comp_flags &= ~comp_flags_table[i].cfn_flag;
3231 llapi_printf(LLAPI_MSG_NORMAL, ",");
3232 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3236 static void lov_dump_comp_v1_entry(struct find_param *param,
3237 enum lov_dump_flags flags, int index)
3239 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3240 struct lov_comp_md_entry_v1 *entry;
3241 char *separator = "";
3242 enum llapi_layout_verbose verbose = param->fp_verbose;
3243 bool yaml = flags & LDF_YAML;
3245 entry = &comp_v1->lcm_entries[index];
3248 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3250 if (verbose & VERBOSE_COMP_ID) {
3251 if (verbose & VERBOSE_DETAIL && !yaml)
3252 llapi_printf(LLAPI_MSG_NORMAL,
3253 "%slcme_id: ", " - ");
3254 else if (verbose & ~VERBOSE_COMP_ID)
3255 llapi_printf(LLAPI_MSG_NORMAL,
3256 "%4slcme_id: ", " ");
3257 if (entry->lcme_id != LCME_ID_INVAL)
3258 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3260 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3264 if (verbose & VERBOSE_MIRROR_ID) {
3265 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3266 if (verbose & ~VERBOSE_MIRROR_ID)
3267 llapi_printf(LLAPI_MSG_NORMAL,
3268 "%4slcme_mirror_id: ", " ");
3269 if (entry->lcme_id != LCME_ID_INVAL)
3270 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3271 mirror_id_of(entry->lcme_id));
3273 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3277 if (verbose & VERBOSE_COMP_FLAGS) {
3278 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3279 if (verbose & ~VERBOSE_COMP_FLAGS)
3280 llapi_printf(LLAPI_MSG_NORMAL,
3281 "%4slcme_flags: ", " ");
3282 lcme_flags2str(entry->lcme_flags);
3285 /* print snapshot timestamp if its a nosync comp */
3286 if ((verbose & VERBOSE_COMP_FLAGS) &&
3287 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3288 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3289 if (verbose & ~VERBOSE_COMP_FLAGS)
3290 llapi_printf(LLAPI_MSG_NORMAL,
3291 "%4slcme_timestamp: ", " ");
3293 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3294 (unsigned long long)entry->lcme_timestamp);
3296 time_t stamp = entry->lcme_timestamp;
3297 char *date_str = asctime(localtime(&stamp));
3299 date_str[strlen(date_str) - 1] = '\0';
3300 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3306 if (verbose & VERBOSE_COMP_START) {
3307 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3308 if (verbose & ~VERBOSE_COMP_START)
3309 llapi_printf(LLAPI_MSG_NORMAL,
3310 "%4slcme_extent.e_start: ", " ");
3311 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3312 (unsigned long long)entry->lcme_extent.e_start);
3316 if (verbose & VERBOSE_COMP_END) {
3317 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3318 if (verbose & ~VERBOSE_COMP_END)
3319 llapi_printf(LLAPI_MSG_NORMAL,
3320 "%4slcme_extent.e_end: ", " ");
3321 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3322 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3324 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3325 (unsigned long long)entry->lcme_extent.e_end);
3330 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3331 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3332 } else if (verbose & VERBOSE_DETAIL) {
3333 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3334 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3335 " ", entry->lcme_offset);
3336 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3337 " ", entry->lcme_size);
3338 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3340 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3344 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3345 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3347 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3348 * The table below gives the answers for the specified parameters (value and
3349 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3350 * --------------------------------------
3351 * 1 | file > limit; sign > 0 | -1 / -1 |
3352 * 2 | file = limit; sign > 0 | -1 / -1 |
3353 * 3 | file < limit; sign > 0 | ? / 1 |
3354 * 4 | file > limit; sign = 0 | -1 / -1 |
3355 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3356 * 6 | file < limit; sign = 0 | ? / -1 |
3357 * 7 | file > limit; sign < 0 | 1 / 1 |
3358 * 8 | file = limit; sign < 0 | ? / -1 |
3359 * 9 | file < limit; sign < 0 | ? / -1 |
3360 * --------------------------------------
3361 * Note: 5th actually means that the value is within the interval
3362 * (limit - margin, limit]. */
3363 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3364 int sign, int negopt, unsigned long long margin,
3370 /* Drop the fraction of margin (of days or size). */
3371 if (file + margin <= limit)
3373 } else if (sign == 0) {
3374 if (file <= limit && file + margin > limit)
3376 else if (file + margin <= limit)
3378 } else if (sign < 0) {
3385 return negopt ? ~ret + 1 : ret;
3388 static inline struct lov_user_md *
3389 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3391 return (struct lov_user_md *)((char *)comp_v1 +
3392 comp_v1->lcm_entries[ent_idx].lcme_offset);
3395 static inline struct lov_user_ost_data_v1 *
3396 lov_v1v3_objects(struct lov_user_md *v1)
3398 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3399 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3401 return v1->lmm_objects;
3405 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3407 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3408 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3409 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3411 pool_name[0] = '\0';
3415 print_last_init_comp(struct find_param *param)
3417 /* print all component info */
3418 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3421 /* print specific component info */
3422 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3423 param->fp_check_comp_start || param->fp_check_comp_end ||
3424 param->fp_check_mirror_id || param->fp_check_mirror_index)
3430 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3434 if (param->fp_comp_end == LUSTRE_EOF) {
3435 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3436 match = end == LUSTRE_EOF ? 1 : -1;
3437 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3438 match = end == LUSTRE_EOF ? -1 : 1;
3439 else /* at least EOF */
3441 if (param->fp_exclude_comp_end)
3444 unsigned long long margin;
3446 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3447 match = find_value_cmp(end, param->fp_comp_end,
3448 param->fp_comp_end_sign,
3449 param->fp_exclude_comp_end, margin, 0);
3456 * An example of "getstripe -v" for a two components PFL file:
3459 * lcm_magic: 0x0BD60BD0
3463 * lcm_entry_count: 2
3467 * lcme_extent.e_start: 0
3468 * lcme_extent.e_end: 1048576
3472 * lmm_magic: 0x0BD10BD0
3473 * lmm_seq: 0x200000401
3474 * lmm_object_id: 0x1
3475 * lmm_fid: [0x200000401:0x1:0x0]
3476 * lmm_stripe_count: 1
3477 * lmm_stripe_size: 1048576
3478 * lmm_pattern: raid0
3480 * lmm_stripe_offset: 0
3482 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3486 * lcme_extent.e_start: 1048576
3487 * lcme_extent.e_end: EOF
3491 * lmm_magic: 0x0BD10BD0
3492 * lmm_seq: 0x200000401
3493 * lmm_object_id: 0x1
3494 * lmm_fid: [0x200000401:0x1:0x0]
3495 * lmm_stripe_count: 2
3496 * lmm_stripe_size: 1048576
3497 * lmm_pattern: raid0
3499 * lmm_stripe_offset: 1
3501 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3502 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3504 static void lov_dump_comp_v1(struct find_param *param, char *path,
3505 enum lov_dump_flags flags)
3507 struct lov_comp_md_entry_v1 *entry;
3508 struct lov_user_ost_data_v1 *objects;
3509 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3510 struct lov_user_md_v1 *v1;
3511 char pool_name[LOV_MAXPOOLNAME + 1];
3512 int obdindex = param->fp_obd_index;
3513 int i, j, match, ext;
3514 bool obdstripe = false;
3515 __u16 mirror_index = 0;
3516 __u16 mirror_id = 0;
3518 if (obdindex != OBD_NOT_FOUND) {
3519 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3520 i < comp_v1->lcm_entry_count; i++) {
3521 if (!(comp_v1->lcm_entries[i].lcme_flags &
3525 v1 = lov_comp_entry(comp_v1, i);
3526 objects = lov_v1v3_objects(v1);
3528 for (j = 0; j < v1->lmm_stripe_count; j++) {
3529 if (obdindex == objects[j].l_ost_idx) {
3542 lov_dump_comp_v1_header(param, path, flags);
3544 flags |= LDF_INDENT;
3546 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3547 entry = &comp_v1->lcm_entries[i];
3549 if (param->fp_check_comp_flags) {
3550 if (((param->fp_comp_flags & entry->lcme_flags) !=
3551 param->fp_comp_flags) ||
3552 (param->fp_comp_neg_flags & entry->lcme_flags))
3556 if (param->fp_check_comp_id &&
3557 param->fp_comp_id != entry->lcme_id)
3560 if (param->fp_check_comp_start) {
3561 match = find_value_cmp(entry->lcme_extent.e_start,
3562 param->fp_comp_start,
3563 param->fp_comp_start_sign,
3565 param->fp_comp_start_units, 0);
3570 if (param->fp_check_comp_end) {
3571 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3577 if (param->fp_check_mirror_index) {
3578 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3580 mirror_id = mirror_id_of(entry->lcme_id);
3583 match = find_value_cmp(mirror_index,
3584 param->fp_mirror_index,
3585 param->fp_mirror_index_sign,
3586 param->fp_exclude_mirror_index,
3590 } else if (param->fp_check_mirror_id) {
3591 if (mirror_id != mirror_id_of(entry->lcme_id))
3592 mirror_id = mirror_id_of(entry->lcme_id);
3594 match = find_value_cmp(mirror_id,
3595 param->fp_mirror_id,
3596 param->fp_mirror_id_sign,
3597 param->fp_exclude_mirror_id,
3603 if (print_last_init_comp(param)) {
3605 * if part of stripe info is needed, we'd print only
3606 * the last instantiated component info.
3608 if (entry->lcme_flags & LCME_FL_INIT)
3611 if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3612 if (entry->lcme_flags & LCME_FL_EXTENSION)
3613 /* moved back below */
3621 if (entry->lcme_flags & LCME_FL_INIT) {
3622 if (obdindex != OBD_NOT_FOUND) {
3623 flags |= LDF_SKIP_OBJS;
3624 v1 = lov_comp_entry(comp_v1, i);
3625 objects = lov_v1v3_objects(v1);
3627 for (j = 0; j < v1->lmm_stripe_count; j++) {
3628 if (obdindex == objects[j].l_ost_idx) {
3629 flags &= ~LDF_SKIP_OBJS;
3634 flags &= ~LDF_SKIP_OBJS;
3637 flags |= LDF_SKIP_OBJS;
3640 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3642 lov_dump_comp_v1_entry(param, flags, i);
3644 v1 = lov_comp_entry(comp_v1, i);
3645 objects = lov_v1v3_objects(v1);
3646 lov_v1v3_pool_name(v1, pool_name);
3648 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3649 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3650 param->fp_max_depth, param->fp_verbose,
3653 if (print_last_init_comp(param)) {
3655 * directory layout contains only layout template, print the
3659 i = comp_v1->lcm_entry_count - 1;
3662 flags &= ~LDF_SKIP_OBJS;
3664 lov_dump_comp_v1_entry(param, flags, i);
3666 v1 = lov_comp_entry(comp_v1, i);
3667 objects = lov_v1v3_objects(v1);
3668 lov_v1v3_pool_name(v1, pool_name);
3670 entry = &comp_v1->lcm_entries[i];
3671 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3672 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3673 param->fp_max_depth, param->fp_verbose,
3678 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3679 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3682 static inline bool has_any_comp_options(struct find_param *param)
3684 enum llapi_layout_verbose verbose = param->fp_verbose;
3686 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3687 param->fp_check_comp_start || param->fp_check_comp_end ||
3688 param->fp_check_comp_flags)
3691 /* show full layout information, not component specific */
3692 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3695 return verbose & VERBOSE_COMP_OPTS;
3698 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3701 struct lov_user_md *lum = &orig->lmd_lmm;
3702 struct lov_user_mds_data *new;
3703 struct lov_comp_md_v1 *comp_v1;
3704 struct lov_comp_md_entry_v1 *ent;
3705 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3706 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3709 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3711 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3715 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3716 + sizeof(new->lmd_lmmsize));
3718 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3719 comp_v1->lcm_magic = lum->lmm_magic;
3720 comp_v1->lcm_size = lum_off + lum_size;
3721 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3722 comp_v1->lcm_flags = 0;
3723 comp_v1->lcm_entry_count = 1;
3725 ent = &comp_v1->lcm_entries[0];
3727 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3728 ent->lcme_extent.e_start = 0;
3729 ent->lcme_extent.e_end = LUSTRE_EOF;
3730 ent->lcme_offset = lum_off;
3731 ent->lcme_size = lum_size;
3733 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3738 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3739 enum lov_dump_flags flags)
3741 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3743 if (has_any_comp_options(param)) {
3744 struct lov_user_mds_data *new_lmd, *orig_lmd;
3746 orig_lmd = param->fp_lmd;
3747 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3748 if (new_lmd != NULL) {
3749 param->fp_lmd = new_lmd;
3750 lov_dump_comp_v1(param, path, flags);
3751 param->fp_lmd = orig_lmd;
3757 if (magic == LOV_USER_MAGIC_V1) {
3758 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3759 param->fp_lmd->lmd_lmm.lmm_objects,
3760 path, param->fp_obd_index,
3761 param->fp_max_depth, param->fp_verbose,
3764 char pool_name[LOV_MAXPOOLNAME + 1];
3765 struct lov_user_ost_data_v1 *objects;
3766 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3768 snprintf(pool_name, sizeof(pool_name), "%s",
3769 lmmv3->lmm_pool_name);
3770 objects = lmmv3->lmm_objects;
3771 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3772 objects, path, param->fp_obd_index,
3773 param->fp_max_depth, param->fp_verbose,
3778 static uint32_t check_foreign_type(uint32_t foreign_type)
3782 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3783 if (lu_foreign_types[i].lft_name == NULL)
3785 if (foreign_type == lu_foreign_types[i].lft_type)
3789 return LU_FOREIGN_TYPE_UNKNOWN;
3792 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3793 enum lov_dump_flags flags)
3795 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3796 bool yaml = flags & LDF_YAML;
3798 if (!yaml && param->fp_depth && path)
3799 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3801 if (param->fp_verbose & VERBOSE_DETAIL) {
3802 uint32_t type = check_foreign_type(lfm->lfm_type);
3804 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3806 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3808 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3810 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3811 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3812 lu_foreign_types[type].lft_name);
3814 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3816 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3819 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3820 lfm->lfm_length, lfm->lfm_value);
3821 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3824 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3825 enum lov_dump_flags flags)
3827 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3828 bool yaml = flags & LDF_YAML;
3830 if (!yaml && param->fp_depth && path)
3831 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3833 if (param->fp_verbose & VERBOSE_DETAIL) {
3834 uint32_t type = check_foreign_type(lfm->lfm_type);
3836 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3838 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3840 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3842 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3843 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3844 lu_foreign_types[type].lft_name);
3846 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3848 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3851 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3852 lfm->lfm_length, lfm->lfm_value);
3853 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3856 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3857 enum lov_dump_flags flags)
3861 if (param->fp_get_lmv || param->fp_get_default_lmv)
3862 magic = (__u32)param->fp_lmv_md->lum_magic;
3864 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3867 flags |= LDF_IS_RAW;
3872 case LOV_USER_MAGIC_V1:
3873 case LOV_USER_MAGIC_V3:
3874 case LOV_USER_MAGIC_SPECIFIC:
3875 lov_dump_plain_user_lmm(param, path, flags);
3877 case LOV_USER_MAGIC_FOREIGN:
3878 lov_dump_foreign_lmm(param, path, flags);
3881 case LMV_USER_MAGIC: {
3882 char pool_name[LOV_MAXPOOLNAME + 1];
3883 struct lmv_user_md *lum;
3885 lum = (struct lmv_user_md *)param->fp_lmv_md;
3886 snprintf(pool_name, sizeof(pool_name), "%s",
3887 lum->lum_pool_name);
3888 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3889 param->fp_max_depth, param->fp_verbose,
3893 case LOV_USER_MAGIC_COMP_V1:
3894 lov_dump_comp_v1(param, path, flags);
3896 case LMV_MAGIC_FOREIGN:
3897 lmv_dump_foreign_lmm(param, path, flags);
3900 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
3901 "(expecting one of %#x %#x %#x %#x)\n",
3902 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3903 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3904 LMV_USER_MAGIC, LMV_MAGIC_V1);
3909 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3915 fname = strrchr(path, '/');
3917 /* It should be a file (or other non-directory) */
3918 if (fname == NULL) {
3919 dname = (char *)malloc(2);
3923 fname = (char *)path;
3925 dname = (char *)malloc(fname - path + 1);
3928 strncpy(dname, path, fname - path);
3929 dname[fname - path] = '\0';
3933 fd = open(dname, O_RDONLY | O_NONBLOCK);
3939 strcpy((char *)lum, fname);
3940 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3943 if (close(fd) == -1 && rc == 0)
3951 int llapi_file_lookup(int dirfd, const char *name)
3953 struct obd_ioctl_data data = { 0 };
3958 if (dirfd < 0 || name == NULL)
3961 data.ioc_version = OBD_IOCTL_VERSION;
3962 data.ioc_len = sizeof(data);
3963 data.ioc_inlbuf1 = (char *)name;
3964 data.ioc_inllen1 = strlen(name) + 1;
3966 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3968 llapi_error(LLAPI_MSG_ERROR, rc,
3969 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3974 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
3980 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
3981 * Return -1 or 1 if file timestamp does not or does match the given criteria
3982 * correspondingly. Return 0 if the MDS time is being checked and there are
3983 * attributes on OSTs and it is not yet clear if the timespamp matches.
3985 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
3986 * updated timestamps. */
3987 static int find_time_check(lstatx_t *stx, struct find_param *param, int mds)
3992 /* Check if file is accepted. */
3993 if (param->fp_atime) {
3994 rc2 = find_value_cmp(stx->stx_atime.tv_sec, param->fp_atime,
3995 param->fp_asign, param->fp_exclude_atime,
3996 param->fp_time_margin, mds);
4002 if (param->fp_mtime) {
4003 rc2 = find_value_cmp(stx->stx_mtime.tv_sec, param->fp_mtime,
4004 param->fp_msign, param->fp_exclude_mtime,
4005 param->fp_time_margin, mds);
4009 /* If the previous check matches, but this one is not yet clear,
4010 * we should return 0 to do an RPC on OSTs. */
4015 if (param->fp_ctime) {
4016 rc2 = find_value_cmp(stx->stx_ctime.tv_sec, param->fp_ctime,
4017 param->fp_csign, param->fp_exclude_ctime,
4018 param->fp_time_margin, mds);
4022 /* If the previous check matches, but this one is not yet clear,
4023 * we should return 0 to do an RPC on OSTs. */
4032 * Check whether the stripes matches the indexes user provided
4036 static int check_obd_match(struct find_param *param)
4038 struct lov_user_ost_data_v1 *objects;
4039 struct lov_comp_md_v1 *comp_v1 = NULL;
4040 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4041 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
4042 int i, j, k, count = 1;
4044 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4047 if (!S_ISREG(stx->stx_mode))
4050 /* exclude foreign */
4051 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4052 return param->fp_exclude_obd;
4054 /* Only those files should be accepted, which have a
4055 * stripe on the specified OST. */
4056 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4057 comp_v1 = (struct lov_comp_md_v1 *)v1;
4058 count = comp_v1->lcm_entry_count;
4061 for (i = 0; i < count; i++) {
4063 v1 = lov_comp_entry(comp_v1, i);
4065 objects = lov_v1v3_objects(v1);
4067 for (j = 0; j < v1->lmm_stripe_count; j++) {
4068 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4071 for (k = 0; k < param->fp_num_obds; k++) {
4072 if (param->fp_obd_indexes[k] ==
4073 objects[j].l_ost_idx)
4074 return !param->fp_exclude_obd;
4079 return param->fp_exclude_obd;
4082 static int check_mdt_match(struct find_param *param)
4086 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4089 /* FIXME: For striped dir, we should get stripe information and check */
4090 for (i = 0; i < param->fp_num_mdts; i++) {
4091 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4092 return !param->fp_exclude_mdt;
4095 if (param->fp_exclude_mdt)
4102 * Check whether the obd is active or not, if it is
4103 * not active, just print the object affected by this
4106 static int print_failed_tgt(struct find_param *param, char *path, int type)
4108 struct obd_statfs stat_buf;
4109 struct obd_uuid uuid_buf;
4112 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV)
4115 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4116 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4117 ret = llapi_obd_statfs(path, type,
4118 param->fp_obd_index, &stat_buf,
4121 llapi_printf(LLAPI_MSG_NORMAL,
4122 "obd_uuid: %s failed %s ",
4123 param->fp_obd_uuid->uuid,
4130 static int find_check_stripe_size(struct find_param *param)
4132 struct lov_comp_md_v1 *comp_v1 = NULL;
4133 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4134 __u32 stripe_size = 0;
4135 int ret, i, count = 1;
4137 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4138 return param->fp_exclude_stripe_size ? 1 : -1;
4140 ret = param->fp_exclude_stripe_size ? 1 : -1;
4141 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4142 comp_v1 = (struct lov_comp_md_v1 *)v1;
4143 count = comp_v1->lcm_entry_count;
4146 for (i = 0; i < count; i++) {
4147 struct lov_comp_md_entry_v1 *ent;
4150 v1 = lov_comp_entry(comp_v1, i);
4152 ent = &comp_v1->lcm_entries[i];
4153 if (ent->lcme_flags & LCME_FL_EXTENSION)
4155 if (!(ent->lcme_flags & LCME_FL_INIT))
4158 stripe_size = v1->lmm_stripe_size;
4161 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4162 param->fp_stripe_size_sign,
4163 param->fp_exclude_stripe_size,
4164 param->fp_stripe_size_units, 0);
4169 static int find_check_ext_size(struct find_param *param)
4171 struct lov_comp_md_v1 *comp_v1;
4172 struct lov_user_md_v1 *v1;
4175 ret = param->fp_exclude_ext_size ? 1 : -1;
4176 comp_v1 = (struct lov_comp_md_v1 *)¶m->fp_lmd->lmd_lmm;
4177 if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4180 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4181 struct lov_comp_md_entry_v1 *ent;
4183 v1 = lov_comp_entry(comp_v1, i);
4185 ent = &comp_v1->lcm_entries[i];
4186 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4189 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4190 param->fp_ext_size_sign,
4191 param->fp_exclude_ext_size,
4192 param->fp_ext_size_units, 0);
4193 /* If any ext_size matches */
4201 static __u32 find_get_stripe_count(struct find_param *param)
4203 struct lov_comp_md_v1 *comp_v1 = NULL;
4204 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4206 __u32 stripe_count = 0;
4208 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4211 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4212 comp_v1 = (struct lov_comp_md_v1 *)v1;
4213 count = comp_v1->lcm_entry_count;
4216 for (i = 0; i < count; i++) {
4218 struct lov_comp_md_entry_v1 *ent;
4220 v1 = lov_comp_entry(comp_v1, i);
4222 ent = &comp_v1->lcm_entries[i];
4223 if (!(ent->lcme_flags & LCME_FL_INIT))
4226 if (ent->lcme_flags & LCME_FL_EXTENSION)
4229 stripe_count = v1->lmm_stripe_count;
4232 return stripe_count;
4235 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4237 static int find_check_layout(struct find_param *param)
4239 struct lov_comp_md_v1 *comp_v1 = NULL;
4240 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4242 bool found = false, valid = false;
4244 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4245 comp_v1 = (struct lov_comp_md_v1 *)v1;
4246 count = comp_v1->lcm_entry_count;
4249 for (i = 0; i < count; i++) {
4251 v1 = lov_comp_entry(comp_v1, i);
4253 /* foreign file have a special magic but no pattern field */
4254 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4257 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4261 if (v1->lmm_pattern & param->fp_layout) {
4270 if ((found && !param->fp_exclude_layout) ||
4271 (!found && param->fp_exclude_layout))
4277 /* if no type specified, check/exclude all foreign
4278 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4280 static int find_check_foreign(struct find_param *param)
4282 if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4283 struct lov_foreign_md *lfm;
4285 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4286 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4287 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4288 return param->fp_exclude_foreign ? 1 : -1;
4291 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4292 lfm->lfm_type == param->fp_foreign_type)
4293 return param->fp_exclude_foreign ? -1 : 1;
4294 return param->fp_exclude_foreign ? 1 : -1;
4298 if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4299 struct lmv_foreign_md *lfm;
4301 lfm = (void *)param->fp_lmv_md;
4302 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
4303 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4304 return param->fp_exclude_foreign ? 1 : -1;
4307 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4308 lfm->lfm_type == param->fp_foreign_type)
4309 return param->fp_exclude_foreign ? -1 : 1;
4310 return param->fp_exclude_foreign ? 1 : -1;
4316 static int find_check_pool(struct find_param *param)
4318 struct lov_comp_md_v1 *comp_v1 = NULL;
4319 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4320 struct lov_user_md_v3 *v3 = (void *)v1;
4324 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4325 comp_v1 = (struct lov_comp_md_v1 *)v1;
4326 count = comp_v1->lcm_entry_count;
4327 /* empty requested pool is taken as no pool search */
4328 if (count == 0 && param->fp_poolname[0] == '\0')
4332 for (i = 0; i < count; i++) {
4333 if (comp_v1 != NULL)
4334 v1 = lov_comp_entry(comp_v1, i);
4336 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4339 if (((v1->lmm_magic == LOV_USER_MAGIC_V1) &&
4340 (param->fp_poolname[0] == '\0')) ||
4341 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4342 (strncmp(v3->lmm_pool_name,
4343 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4344 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4345 (strcmp(param->fp_poolname, "*") == 0))) {
4351 if ((found && !param->fp_exclude_pool) ||
4352 (!found && param->fp_exclude_pool))
4358 static int find_check_comp_options(struct find_param *param)
4360 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
4361 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4362 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4363 struct lov_comp_md_entry_v1 *entry;
4366 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4369 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4370 comp_v1 = (struct lov_comp_md_v1 *)v1;
4372 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4373 if (forged_v1 == NULL)
4375 comp_v1 = forged_v1;
4376 comp_v1->lcm_entry_count = 1;
4377 entry = &comp_v1->lcm_entries[0];
4378 entry->lcme_flags = S_ISDIR(stx->stx_mode) ? 0 : LCME_FL_INIT;
4379 entry->lcme_extent.e_start = 0;
4380 entry->lcme_extent.e_end = LUSTRE_EOF;
4383 /* invalid case, don't match for any kind of search. */
4384 if (comp_v1->lcm_entry_count == 0) {
4389 if (param->fp_check_comp_count) {
4390 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4391 param->fp_comp_count,
4392 param->fp_comp_count_sign,
4393 param->fp_exclude_comp_count, 1, 0);
4399 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4400 entry = &comp_v1->lcm_entries[i];
4402 if (param->fp_check_comp_flags) {
4404 if (((param->fp_comp_flags & entry->lcme_flags) !=
4405 param->fp_comp_flags) ||
4406 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4412 if (param->fp_check_comp_start) {
4413 ret = find_value_cmp(entry->lcme_extent.e_start,
4414 param->fp_comp_start,
4415 param->fp_comp_start_sign,
4416 param->fp_exclude_comp_start,
4417 param->fp_comp_start_units, 0);
4422 if (param->fp_check_comp_end) {
4423 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4429 /* the component matches all criteria */
4438 static int find_check_mirror_options(struct find_param *param)
4440 struct lov_comp_md_v1 *comp_v1;
4441 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4444 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4447 comp_v1 = (struct lov_comp_md_v1 *)v1;
4449 if (param->fp_check_mirror_count) {
4450 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4451 param->fp_mirror_count,
4452 param->fp_mirror_count_sign,
4453 param->fp_exclude_mirror_count, 1, 0);
4458 if (param->fp_check_mirror_state) {
4460 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4462 if ((param->fp_mirror_state != 0 &&
4463 file_state != param->fp_mirror_state) ||
4464 file_state == param->fp_mirror_neg_state)
4471 static bool find_check_lmm_info(struct find_param *param)
4473 return param->fp_check_pool || param->fp_check_stripe_count ||
4474 param->fp_check_stripe_size || param->fp_check_layout ||
4475 param->fp_check_comp_count || param->fp_check_comp_end ||
4476 param->fp_check_comp_start || param->fp_check_comp_flags ||
4477 param->fp_check_mirror_count || param->fp_check_foreign ||
4478 param->fp_check_mirror_state || param->fp_check_ext_size ||
4479 param->fp_check_projid;
4483 * Get file/directory project id.
4484 * by the open fd resides on.
4485 * Return 0 and project id on success, or -ve errno.
4487 static int fget_projid(int fd, int *projid)
4492 rc = ioctl(fd, LL_IOC_FSGETXATTR, &fsx);
4496 *projid = fsx.fsx_projid;
4500 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
4501 void *data, struct dirent64 *de)
4503 struct find_param *param = (struct find_param *)data;
4504 DIR *dir = dirp == NULL ? NULL : *dirp;
4505 int decision = 1; /* 1 is accepted; -1 is rejected. */
4506 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
4508 int checked_type = 0;
4510 __u32 stripe_count = 0;
4514 if (parent == NULL && dir == NULL)
4517 /* If a regular expression is presented, make the initial decision */
4518 if (param->fp_pattern != NULL) {
4519 char *fname = strrchr(path, '/');
4520 fname = (fname == NULL ? path : fname + 1);
4521 ret = fnmatch(param->fp_pattern, fname, 0);
4522 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4523 (ret == 0 && param->fp_exclude_pattern))
4527 /* See if we can check the file type from the dirent. */
4528 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
4531 if (DTTOIF(de->d_type) == param->fp_type) {
4532 if (param->fp_exclude_type)
4535 if (!param->fp_exclude_type)
4542 /* Request MDS for the stat info if some of these parameters need
4543 * to be compared. */
4544 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4545 param->fp_check_uid || param->fp_check_gid ||
4546 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4547 param->fp_check_size || param->fp_check_blocks ||
4548 find_check_lmm_info(param) ||
4549 param->fp_check_mdt_count || param->fp_check_hash_type)
4552 if (param->fp_type != 0 && checked_type == 0)
4555 if (decision == 0) {
4556 if (dir && (param->fp_check_mdt_count ||
4557 param->fp_check_hash_type || param->fp_check_foreign)) {
4558 param->fp_get_lmv = 1;
4559 ret = cb_get_dirstripe(path, dir, param);
4561 /* XXX this works to decide for foreign
4564 if (errno == ENODATA &&
4565 param->fp_check_foreign) {
4566 if (param->fp_exclude_foreign)
4574 param->fp_lmd->lmd_lmm.lmm_magic = 0;
4575 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
4576 param->fp_lum_size, GET_LMD_INFO);
4577 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
4578 find_check_lmm_info(param)) {
4579 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
4581 /* We need to "fake" the "use the default" values
4582 * since the lmm struct is zeroed out at this point. */
4583 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4584 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
4586 ostid_set_seq(&lmm->lmm_oi,
4587 FID_SEQ_LOV_DEFAULT);
4588 lmm->lmm_stripe_size = 0;
4589 lmm->lmm_stripe_count = 0;
4590 lmm->lmm_stripe_offset = -1;
4592 if (ret == 0 && param->fp_mdt_uuid != NULL) {
4594 ret = llapi_file_fget_mdtidx(dirfd(dir),
4595 ¶m->fp_file_mdt_index);
4596 } else if (S_ISREG(stx->stx_mode)) {
4597 /* FIXME: we could get the MDT index from the
4598 * file's FID in lmd->lmd_lmm.lmm_oi without
4599 * opening the file, once we are sure that
4600 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
4601 * That would still be an ioctl() to map the
4602 * FID to the MDT, but not an open RPC. */
4603 fd = open(path, O_RDONLY);
4605 ret = llapi_file_fget_mdtidx(fd,
4606 ¶m->fp_file_mdt_index);
4611 /* For a special file, we assume it resides on
4612 * the same MDT as the parent directory. */
4613 ret = llapi_file_fget_mdtidx(dirfd(parent),
4614 ¶m->fp_file_mdt_index);
4625 stripe_count = find_get_stripe_count(param);
4629 if (param->fp_type && !checked_type) {
4630 if ((stx->stx_mode & S_IFMT) == param->fp_type) {
4631 if (param->fp_exclude_type)
4634 if (!param->fp_exclude_type)
4640 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
4641 if (lustre_fs && param->fp_got_uuids &&
4642 param->fp_dev != makedev(stx->stx_dev_major,
4643 stx->stx_dev_minor)) {
4644 /* A lustre/lustre mount point is crossed. */
4645 param->fp_got_uuids = 0;
4646 param->fp_obds_printed = 0;
4647 param->fp_mdt_index = OBD_NOT_FOUND;
4648 param->fp_obd_index = OBD_NOT_FOUND;
4651 if (lustre_fs && !param->fp_got_uuids) {
4652 ret = setup_target_indexes(dir ? dir : parent, path,
4657 param->fp_dev = makedev(stx->stx_dev_major,
4658 stx->stx_dev_minor);
4659 } else if (!lustre_fs && param->fp_got_uuids) {
4660 /* A lustre/non-lustre mount point is crossed. */
4661 param->fp_got_uuids = 0;
4662 param->fp_mdt_index = OBD_NOT_FOUND;
4663 param->fp_obd_index = OBD_NOT_FOUND;
4667 if (param->fp_check_foreign) {
4668 decision = find_check_foreign(param);
4673 if (param->fp_check_stripe_size) {
4674 decision = find_check_stripe_size(param);
4679 if (param->fp_check_ext_size) {
4680 decision = find_check_ext_size(param);
4685 if (param->fp_check_stripe_count) {
4686 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
4687 param->fp_stripe_count_sign,
4688 param->fp_exclude_stripe_count, 1, 0);
4693 if (param->fp_check_mdt_count) {
4694 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4699 decision = find_value_cmp(
4700 param->fp_lmv_md->lum_stripe_count,
4701 param->fp_mdt_count,
4702 param->fp_mdt_count_sign,
4703 param->fp_exclude_mdt_count, 1, 0);
4708 if (param->fp_check_layout) {
4709 decision = find_check_layout(param);
4714 if (param->fp_check_hash_type) {
4717 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4722 found = param->fp_lmv_md->lum_hash_type & param->fp_hash_type;
4723 if ((found && param->fp_exclude_hash_type) ||
4724 (!found && !param->fp_exclude_hash_type)) {
4730 /* If an OBD UUID is specified but none matches, skip this file. */
4731 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
4732 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
4735 /* If an OST or MDT UUID is given, and some OST matches,
4737 if (param->fp_obd_index != OBD_NOT_FOUND ||
4738 param->fp_mdt_index != OBD_NOT_FOUND) {
4739 if (param->fp_obd_uuid) {
4740 if (check_obd_match(param)) {
4741 /* If no mdtuuid is given, we are done.
4742 * Otherwise, fall through to the mdtuuid
4744 if (!param->fp_mdt_uuid)
4751 if (param->fp_mdt_uuid) {
4752 if (check_mdt_match(param))
4759 if (param->fp_check_uid) {
4760 if (stx->stx_uid == param->fp_uid) {
4761 if (param->fp_exclude_uid)
4764 if (!param->fp_exclude_uid)
4769 if (param->fp_check_gid) {
4770 if (stx->stx_gid == param->fp_gid) {
4771 if (param->fp_exclude_gid)
4774 if (!param->fp_exclude_gid)
4779 if (param->fp_check_projid) {
4783 fd = open(path, O_RDONLY);
4786 ret = fget_projid(fd, &projid);
4791 if (projid == param->fp_projid) {
4792 if (param->fp_exclude_projid)
4795 if (!param->fp_exclude_projid)
4800 if (param->fp_check_pool) {
4801 decision = find_check_pool(param);
4806 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
4807 param->fp_check_comp_start || param->fp_check_comp_end) {
4808 decision = find_check_comp_options(param);
4813 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
4814 decision = find_check_mirror_options(param);
4819 /* Check the time on mds. */
4821 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
4824 for_mds = lustre_fs ?
4825 (S_ISREG(stx->stx_mode) && stripe_count) : 0;
4826 decision = find_time_check(stx, param, for_mds);
4831 flags = param->fp_lmd->lmd_flags;
4832 if (param->fp_check_size &&
4833 ((S_ISREG(stx->stx_mode) && stripe_count) ||
4834 S_ISDIR(stx->stx_mode)) &&
4835 !(flags & OBD_MD_FLSIZE ||
4836 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
4839 if (param->fp_check_blocks &&
4840 ((S_ISREG(stx->stx_mode) && stripe_count) ||
4841 S_ISDIR(stx->stx_mode)) &&
4842 !(flags & OBD_MD_FLBLOCKS ||
4843 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
4846 /* If file still fits the request, ask ost for updated info.
4847 * The regular stat is almost of the same speed as some new
4848 * 'glimpse-size-ioctl'.
4853 /* For regular files with the stripe the decision may have not
4854 * been taken yet if *time or size is to be checked. */
4855 if (param->fp_obd_index != OBD_NOT_FOUND)
4856 print_failed_tgt(param, path, LL_STATFS_LOV);
4858 if (param->fp_mdt_index != OBD_NOT_FOUND)
4859 print_failed_tgt(param, path, LL_STATFS_LMV);
4862 ret = fstat_f(dirfd(dir), &st);
4863 else if (de != NULL)
4864 ret = fstatat_f(dirfd(parent), de->d_name, &st,
4865 AT_SYMLINK_NOFOLLOW);
4867 ret = lstat_f(path, &st);
4870 if (errno == ENOENT) {
4871 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
4872 "warning: %s: %s does not exist",
4877 llapi_error(LLAPI_MSG_ERROR, ret,
4878 "%s: stat on %s failed",
4884 convert_lmd_statx(param->fp_lmd, &st, true);
4885 /* Check the time on osc. */
4886 decision = find_time_check(stx, param, 0);
4891 if (param->fp_check_size) {
4892 decision = find_value_cmp(stx->stx_size, param->fp_size,
4893 param->fp_size_sign,
4894 param->fp_exclude_size,
4895 param->fp_size_units, 0);
4900 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
4901 decision = find_value_cmp(stx->stx_blocks * 512,
4903 param->fp_blocks_sign,
4904 param->fp_exclude_blocks,
4905 param->fp_blocks_units, 0);
4911 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
4912 if (param->fp_zero_end)
4913 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
4915 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4919 /* Do not get down anymore? */
4920 if (param->fp_depth == param->fp_max_depth) {
4931 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
4932 void *param_data, struct dirent64 *de)
4934 struct find_param *param = (struct find_param *)param_data;
4935 struct lmv_user_md *lmu = param->fp_lmv_md;
4936 DIR *tmp_parent = parent;
4937 char raw[MAX_IOC_BUFLEN] = {'\0'};
4939 struct obd_ioctl_data data = { 0 };
4946 if (parent == NULL && dirp == NULL)
4955 if (parent == NULL) {
4956 tmp_parent = opendir_parent(path);
4957 if (tmp_parent == NULL) {
4960 llapi_error(LLAPI_MSG_ERROR, ret,
4961 "can not open %s", path);
4966 fd = dirfd(tmp_parent);
4968 path_copy = strdup(path);
4969 filename = basename(path_copy);
4971 data.ioc_inlbuf1 = (char *)filename;
4972 data.ioc_inllen1 = strlen(filename) + 1;
4973 data.ioc_inlbuf2 = (char *)lmu;
4974 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
4976 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
4978 llapi_error(LLAPI_MSG_ERROR, ret,
4979 "llapi_obd_statfs: error packing ioctl data");
4984 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
4986 if (errno == EBUSY && !retry) {
4987 /* because migrate may not be able to lock all involved
4988 * objects in order, for some of them it try lock, while
4989 * there may be conflicting COS locks and cause migrate
4990 * fail with EBUSY, hope a sync() could cause
4991 * transaction commit and release these COS locks. */
4995 } else if (errno == EALREADY) {
4996 if (param->fp_verbose & VERBOSE_DETAIL)
4997 llapi_printf(LLAPI_MSG_NORMAL,
4998 "%s was migrated to MDT%d already\n",
4999 path, lmu->lum_stripe_offset);
5003 llapi_error(LLAPI_MSG_ERROR, ret,
5004 "%s migrate failed: %s (%d)\n",
5005 path, strerror(-ret), ret);
5008 } else if (param->fp_verbose & VERBOSE_DETAIL) {
5009 llapi_printf(LLAPI_MSG_NORMAL,
5010 "migrate %s to MDT%d stripe count %d\n",
5011 path, lmu->lum_stripe_offset,
5012 lmu->lum_stripe_count);
5017 /* If the directory is being migration, we need
5018 * close the directory after migration,
5019 * so the old directory cache will be cleanup
5020 * on the client side, and re-open to get the
5021 * new directory handle */
5022 *dirp = opendir(path);
5023 if (*dirp == NULL) {
5025 llapi_error(LLAPI_MSG_ERROR, ret,
5026 "%s: Failed to open '%s'", __func__, path);
5031 closedir(tmp_parent);
5038 /* dir migration finished, shrink its stripes */
5039 static int cb_migrate_mdt_fini(char *path, DIR *parent, DIR **dirp, void *data,
5040 struct dirent64 *de)
5042 struct find_param *param = data;
5043 struct lmv_user_md *lmu = param->fp_lmv_md;
5044 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
5047 if (de && de->d_type != DT_DIR)
5052 * close it before setxattr because the latter may destroy the
5053 * original object, and cause close fail.
5055 ret = closedir(*dirp);
5061 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
5062 if (ret == -EALREADY)
5065 cb_common_fini(path, parent, dirp, data, de);
5069 int llapi_migrate_mdt(char *path, struct find_param *param)
5071 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
5075 int llapi_mv(char *path, struct find_param *param)
5077 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
5078 static bool printed;
5081 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
5082 "llapi_mv() is deprecated, use llapi_migrate_mdt()\n");
5086 return llapi_migrate_mdt(path, param);
5089 int llapi_find(char *path, struct find_param *param)
5091 return param_callback(path, cb_find_init, cb_common_fini, param);
5095 * Get MDT number that the file/directory inode referenced
5096 * by the open fd resides on.
5097 * Return 0 and mdtidx on success, or -ve errno.
5099 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
5101 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
5106 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
5107 struct dirent64 *de)
5109 struct find_param *param = (struct find_param *)data;
5110 DIR *d = dirp == NULL ? NULL : *dirp;
5114 if (parent == NULL && d == NULL)
5118 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5119 } else /* if (parent) */ {
5122 fd = open(path, O_RDONLY | O_NOCTTY);
5124 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5132 if (ret == -ENODATA) {
5133 if (!param->fp_obd_uuid)
5134 llapi_printf(LLAPI_MSG_NORMAL,
5135 "'%s' has no stripe info\n", path);
5137 } else if (ret == -ENOENT) {
5138 llapi_error(LLAPI_MSG_WARN, ret,
5139 "warning: %s: '%s' does not exist",
5142 } else if (ret == -ENOTTY) {
5143 llapi_error(LLAPI_MSG_ERROR, ret,
5144 "%s: '%s' not on a Lustre fs",
5147 llapi_error(LLAPI_MSG_ERROR, ret,
5148 "error: %s: '%s' failed get_mdtidx",
5154 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5155 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5157 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5161 /* Do not go down anymore? */
5162 if (param->fp_depth == param->fp_max_depth)
5170 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
5171 struct dirent64 *de)
5173 struct find_param *param = (struct find_param *)data;
5174 DIR *d = dirp == NULL ? NULL : *dirp;
5177 if (parent == NULL && d == NULL)
5180 if (param->fp_obd_uuid) {
5181 param->fp_quiet = 1;
5182 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
5187 if (d && (param->fp_get_lmv || param->fp_get_default_lmv))
5188 ret = cb_get_dirstripe(path, d, param);
5190 (parent && !param->fp_get_lmv && !param->fp_get_default_lmv))
5191 ret = get_lmd_info(path, parent, d, ¶m->fp_lmd->lmd_lmm,
5192 param->fp_lum_size, GET_LMD_STRIPE);
5197 if (errno == ENODATA && d != NULL) {
5198 /* We need to "fake" the "use the default" values
5199 * since the lmm struct is zeroed out at this point.
5200 * The magic needs to be set in order to satisfy
5201 * a check later on in the code path.
5202 * The object_seq needs to be set for the "(Default)"
5203 * prefix to be displayed. */
5204 if (param->fp_get_default_lmv) {
5205 struct lmv_user_md *lum = param->fp_lmv_md;
5207 lum->lum_magic = LMV_USER_MAGIC;
5208 lum->lum_stripe_count = 0;
5209 lum->lum_stripe_offset = -1;
5211 } else if (param->fp_get_lmv) {
5212 struct lmv_user_md *lum = param->fp_lmv_md;
5215 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5218 lum->lum_magic = LMV_MAGIC_V1;
5219 lum->lum_stripe_count = 0;
5220 lum->lum_stripe_offset = mdtidx;
5223 struct lov_user_md *lmm =
5224 ¶m->fp_lmd->lmd_lmm;
5226 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5228 ostid_set_seq(&lmm->lmm_oi,
5229 FID_SEQ_LOV_DEFAULT);
5230 lmm->lmm_stripe_count = 0;
5231 lmm->lmm_stripe_size = 0;
5232 lmm->lmm_stripe_offset = -1;
5235 } else if (errno == ENODATA && parent != NULL) {
5236 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5237 llapi_printf(LLAPI_MSG_NORMAL,
5238 "%s has no stripe info\n", path);
5240 } else if (errno == ENOENT) {
5241 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5242 "warning: %s: %s does not exist",
5245 } else if (errno == ENOTTY) {
5247 llapi_error(LLAPI_MSG_ERROR, ret,
5248 "%s: '%s' not on a Lustre fs?",
5253 llapi_error(LLAPI_MSG_ERROR, ret,
5254 "error: %s: %s failed for %s",
5255 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
5256 "IOC_MDC_GETFILESTRIPE", path);
5263 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5264 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
5267 /* Do not get down anymore? */
5268 if (param->fp_depth == param->fp_max_depth)
5276 int llapi_getstripe(char *path, struct find_param *param)
5278 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5279 cb_get_mdt_index : cb_getstripe,
5280 cb_common_fini, param);
5283 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5284 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5286 char raw[MAX_IOC_BUFLEN] = {'\0'};
5288 struct obd_ioctl_data data = { 0 };
5291 data.ioc_inlbuf1 = (char *)&type;
5292 data.ioc_inllen1 = sizeof(__u32);
5293 data.ioc_inlbuf2 = (char *)&index;
5294 data.ioc_inllen2 = sizeof(__u32);
5295 data.ioc_pbuf1 = (char *)stat_buf;
5296 data.ioc_plen1 = sizeof(struct obd_statfs);
5297 data.ioc_pbuf2 = (char *)uuid_buf;
5298 data.ioc_plen2 = sizeof(struct obd_uuid);
5300 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5302 llapi_error(LLAPI_MSG_ERROR, rc,
5303 "llapi_obd_statfs: error packing ioctl data");
5307 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5309 return rc < 0 ? -errno : 0;
5312 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5313 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5318 fd = open(path, O_RDONLY);
5321 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5323 /* If we can't even open a file on the filesystem (e.g. with
5324 * -ESHUTDOWN), force caller to exit or it will loop forever. */
5328 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5335 #define MAX_STRING_SIZE 128
5337 int llapi_ping(char *obd_type, char *obd_name)
5339 int flags = O_RDONLY;
5340 char buf[1] = { 0 };
5344 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5345 obd_type, obd_name);
5349 fd = open(path.gl_pathv[0], flags);
5351 if (errno == EACCES && flags == O_RDONLY) {
5356 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5361 if (flags == O_RDONLY)
5362 rc = read(fd, buf, sizeof(buf));
5364 rc = write(fd, buf, sizeof(buf));
5372 cfs_free_param_data(&path);
5376 int llapi_target_iterate(int type_num, char **obd_type,
5377 void *args, llapi_cb_t cb)
5383 for (i = 0; i < type_num; i++) {
5386 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5390 for (j = 0; j < param.gl_pathc; j++) {
5391 char obd_uuid[UUID_MAX + 1];
5395 fp = fopen(param.gl_pathv[j], "r");
5398 llapi_error(LLAPI_MSG_ERROR, rc,
5399 "error: opening '%s'",
5404 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5406 llapi_error(LLAPI_MSG_ERROR, rc,
5407 "error: reading '%s'",
5412 /* Extract the obd_name from the sysfs path.
5413 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5415 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5421 /* skip /fs/lustre/'obd_type'/ */
5422 obd_name += strlen(obd_type[i]) + 12;
5423 /* chop off after obd_name */
5424 ptr = strrchr(obd_name, '/');
5428 cb(obd_type[i], obd_name, obd_uuid, args);
5437 cfs_free_param_data(¶m);
5441 static void do_target_check(char *obd_type_name, char *obd_name,
5442 char *obd_uuid, void *args)
5446 rc = llapi_ping(obd_type_name, obd_name);
5447 if (rc == ENOTCONN) {
5448 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5450 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5452 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5456 int llapi_target_check(int type_num, char **obd_type, char *dir)
5458 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5461 #undef MAX_STRING_SIZE
5463 /* Is this a lustre fs? */
5464 int llapi_is_lustre_mnttype(const char *type)
5466 return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
5469 /* Is this a lustre client fs? */
5470 int llapi_is_lustre_mnt(struct mntent *mnt)
5472 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5473 strstr(mnt->mnt_fsname, ":/") != NULL);
5476 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5478 char fsname[PATH_MAX + 1];
5482 rc = llapi_search_fsname(mnt, fsname);
5486 root = open(mnt, O_RDONLY | O_DIRECTORY);
5489 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
5493 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
5501 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
5502 * format must have %s%s, buf must be > 16
5503 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
5504 * then buf = "lustre-MDT0000"
5506 static int get_mdtname(char *name, char *format, char *buf)
5508 char suffix[]="-MDT0000";
5509 int len = strlen(name);
5511 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
5512 name[len - 5] = '\0';
5517 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
5520 /* Not enough room to add suffix */
5521 llapi_err_noerrno(LLAPI_MSG_ERROR,
5522 "Invalid MDT name |%s|", name);
5527 return sprintf(buf, format, name, suffix);
5530 /** ioctl on filsystem root, with mdtindex sent as data
5531 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
5532 * \param mdtidxp pointer to integer within data to be filled in with the
5533 * mdt index (0 if no mdt is specified). NULL won't be filled.
5535 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
5543 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
5544 Open root and parse mdt index. */
5545 if (mdtname[0] == '/') {
5547 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
5548 (char *)mdtname, -1);
5550 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
5552 ptr = fsname + strlen(fsname) - 8;
5554 index = strtol(ptr + 4, NULL, 16);
5555 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
5559 llapi_err_noerrno(LLAPI_MSG_ERROR,
5560 "Can't open %s: %d\n", mdtname, rc);
5567 rc = ioctl(fd, opc, data);
5576 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
5577 int buflen, long long *recno, int *linkno)
5579 const char *fidstr_orig = fidstr;
5581 struct getinfo_fid2path *gf;
5586 while (*fidstr == '[')
5589 sscanf(fidstr, SFID, RFID(&fid));
5590 if (!fid_is_sane(&fid)) {
5591 llapi_err_noerrno(LLAPI_MSG_ERROR,
5592 "bad FID format '%s', should be [seq:oid:ver]"
5593 " (e.g. "DFID")\n", fidstr_orig,
5594 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
5598 gf = malloc(sizeof(*gf) + buflen);
5603 gf->gf_recno = *recno;
5604 gf->gf_linkno = *linkno;
5605 gf->gf_pathlen = buflen;
5607 /* Take path or fsname */
5608 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
5613 /* strip out instances of // */
5614 for (a = gf->gf_u.gf_path; *a != '\0'; a++) {
5615 if ((*a == '/') && (*(a + 1) == '/'))
5622 if (buf[0] == '\0') { /* ROOT path */
5627 *recno = gf->gf_recno;
5628 *linkno = gf->gf_linkno;
5635 static int fid_from_lma(const char *path, int fd, struct lu_fid *fid)
5638 struct lustre_mdt_attrs *lma;
5642 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
5644 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
5647 lma = (struct lustre_mdt_attrs *)buf;
5648 fid_le_to_cpu(fid, &lma->lma_self_fid);
5652 int llapi_get_mdt_index_by_fid(int fd, const struct lu_fid *fid,
5657 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
5666 int llapi_fd2fid(int fd, struct lu_fid *fid)
5670 memset(fid, 0, sizeof(*fid));
5672 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
5673 if (rc == -EINVAL || rc == -ENOTTY)
5674 rc = fid_from_lma(NULL, fd, fid);
5679 int llapi_path2fid(const char *path, struct lu_fid *fid)
5683 memset(fid, 0, sizeof(*fid));
5684 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5686 if (errno == ELOOP || errno == ENXIO)
5687 return fid_from_lma(path, -1, fid);
5691 rc = llapi_fd2fid(fd, fid);
5692 if (rc == -EINVAL || rc == -ENOTTY)
5693 rc = fid_from_lma(path, -1, fid);
5699 int llapi_fd2parent(int fd, unsigned int linkno, struct lu_fid *parent_fid,
5700 char *name, size_t name_size)
5702 struct getparent *gp;
5705 gp = malloc(sizeof(*gp) + name_size);
5709 gp->gp_linkno = linkno;
5710 gp->gp_name_size = name_size;
5712 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
5718 *parent_fid = gp->gp_fid;
5720 strncpy(name, gp->gp_name, name_size);
5721 name[name_size - 1] = '\0';
5728 int llapi_path2parent(const char *path, unsigned int linkno,
5729 struct lu_fid *parent_fid, char *name, size_t name_size)
5734 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5738 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
5743 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
5748 root = open(mnt, O_RDONLY | O_DIRECTORY);
5751 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
5755 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
5758 llapi_error(LLAPI_MSG_ERROR, rc,
5759 "ioctl on %s for getting connect flags failed", mnt);
5766 * Get a 64-bit value representing the version of file data pointed by fd.
5768 * Each write or truncate, flushed on OST, will change this value. You can use
5769 * this value to verify if file data was modified. This only checks the file
5770 * data, not metadata.
5772 * \param flags 0: no flush pages, usually used it the process has already
5774 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
5776 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
5777 * pages from clients.
5779 * \retval 0 on success.
5780 * \retval -errno on error.
5782 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
5785 struct ioc_data_version idv;
5787 idv.idv_flags = (__u32)flags;
5789 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5793 *data_version = idv.idv_version;
5799 * Flush cached pages from all clients.
5801 * \param fd File descriptor
5805 int llapi_file_flush(int fd)
5809 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);
5813 * Fetch layout version from OST objects. Layout version on OST objects are
5814 * only set when the file is a mirrored file AND after the file has been
5815 * written at least once.
5817 * It actually fetches the least layout version from the objects.
5819 int llapi_get_ost_layout_version(int fd, __u32 *layout_version)
5822 struct ioc_data_version idv = { 0 };
5824 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5828 *layout_version = idv.idv_layout_version;
5834 * Create a file without any name and open it for read/write
5836 * - file is created as if it were a standard file in the given \a directory
5837 * - file does not appear in \a directory and mtime does not change because
5838 * the filename is handled specially by the Lustre MDS.
5839 * - file is destroyed at final close
5841 * \param[in] directory directory from which to inherit layout/MDT idx
5842 * \param[in] mdt_idx MDT index on which the file is created,
5843 * \a idx == -1 means no specific MDT is requested
5844 * \param[in] mode standard open(2) mode
5845 * \param[in] stripe_param stripe parameters. May be NULL.
5847 * \retval a file descriptor on success.
5848 * \retval -errno on error.
5850 int llapi_create_volatile_param(const char *directory, int mdt_idx,
5851 int open_flags, mode_t mode,
5852 const struct llapi_stripe_param *stripe_param)
5854 char file_path[PATH_MAX];
5855 int saved_errno = errno;
5857 unsigned int rnumber;
5863 rc = snprintf(file_path, sizeof(file_path),
5864 "%s/" LUSTRE_VOLATILE_HDR "::%.4X",
5865 directory, rnumber);
5867 rc = snprintf(file_path, sizeof(file_path),
5868 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X",
5869 directory, mdt_idx, rnumber);
5871 if (rc < 0 || rc >= sizeof(file_path))
5872 return -ENAMETOOLONG;
5875 * Either open O_WRONLY or O_RDWR, creating RDONLY
5876 * is non-sensical here
5878 if ((open_flags & O_ACCMODE) == O_RDONLY)
5879 open_flags = O_RDWR | (open_flags & ~O_ACCMODE);
5881 open_flags |= O_CREAT | O_EXCL | O_NOFOLLOW;
5883 if (stripe_param != NULL) {
5884 fd = llapi_file_open_param(file_path, open_flags,
5885 mode, stripe_param);
5889 fd = open(file_path, open_flags, mode);
5893 } while (fd < 0 && rc == -EEXIST);
5896 llapi_error(LLAPI_MSG_ERROR, rc,
5897 "Cannot create volatile file '%s' in '%s'",
5898 file_path + strlen(directory) + 1 +
5899 LUSTRE_VOLATILE_HDR_LEN,
5905 * Unlink file in case this wasn't a Lustre filesystem and the magic
5906 * volatile filename wasn't handled as intended. The effect is the
5907 * same. If volatile open was supported then we expect unlink() to
5910 (void)unlink(file_path);
5913 * Since we are returning successfully we restore errno (and
5914 * mask out possible EEXIST from open() and ENOENT from unlink().
5916 errno = saved_errno;
5922 * Create a file without any name open it for read/write
5924 * - file is created as if it were a standard file in the given \a directory
5925 * - file does not appear in \a directory and mtime does not change because
5926 * the filename is handled specially by the Lustre MDS.
5927 * - file is removed at final close
5928 * - file modes are rw------- since it doesn't make sense to have a read-only
5929 * or write-only file that cannot be opened again.
5930 * - if user wants another mode it must use fchmod() on the open file, no
5931 * security problems arise because it cannot be opened by another process.
5933 * \param[in] directory directory from which to inherit layout/MDT idx
5934 * \param[in] idx MDT index on which the file is created,
5935 * \a idx == -1 means no specific MDT is requested
5936 * \param[in] open_flags standard open(2) flags
5938 * \retval a file descriptor on success.
5939 * \retval -errno on error.
5941 int llapi_create_volatile_idx(const char *directory, int mdt_idx,
5944 return llapi_create_volatile_param(directory, mdt_idx, open_flags,
5945 S_IRUSR | S_IWUSR, NULL);
5949 * Swap the layouts between 2 file descriptors
5950 * the 2 files must be open for writing
5951 * first fd received the ioctl, second fd is passed as arg
5952 * this is assymetric but avoid use of root path for ioctl
5954 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
5955 int gid, __u64 flags)
5957 struct lustre_swap_layouts lsl;
5962 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5963 rc = fstat(fd1, &st1);
5967 rc = fstat(fd2, &st2);
5972 lsl.sl_flags = flags;
5976 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
5980 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5981 struct timeval tv1[2];
5982 struct timeval tv2[2];
5984 memset(tv1, 0, sizeof(tv1));
5985 memset(tv2, 0, sizeof(tv2));
5987 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
5988 tv1[0].tv_sec = st1.st_atime;
5989 tv2[0].tv_sec = st2.st_atime;
5991 tv1[0].tv_sec = st2.st_atime;
5992 tv2[0].tv_sec = st1.st_atime;
5995 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
5996 tv1[1].tv_sec = st1.st_mtime;
5997 tv2[1].tv_sec = st2.st_mtime;
5999 tv1[1].tv_sec = st2.st_mtime;
6000 tv2[1].tv_sec = st1.st_mtime;
6003 rc = futimes(fd1, tv1);
6007 rc = futimes(fd2, tv2);
6015 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
6022 while (grp_id == 0);
6024 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
6032 * Swap the layouts between 2 files
6033 * the 2 files are open in write
6035 int llapi_swap_layouts(const char *path1, const char *path2,
6036 __u64 dv1, __u64 dv2, __u64 flags)
6040 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
6043 llapi_error(LLAPI_MSG_ERROR, rc,
6044 "error: cannot open '%s' for write", path1);
6048 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
6051 llapi_error(LLAPI_MSG_ERROR, rc,
6052 "error: cannot open '%s' for write", path2);
6056 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
6058 llapi_error(LLAPI_MSG_ERROR, rc,
6059 "error: cannot swap layout between '%s' and '%s'",
6070 * Attempt to open a file with Lustre file identifier \a fid
6071 * and return an open file descriptor.
6073 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
6074 * \param[in] fid Lustre file identifier of file to open
6075 * \param[in] flags open() flags
6077 * \retval non-negative file descriptor on successful open
6078 * \retval -1 if an error occurred
6080 int llapi_open_by_fid(const char *lustre_dir, const struct lu_fid *fid,
6083 char mntdir[PATH_MAX];
6084 char path[PATH_MAX + 64];
6087 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
6091 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
6092 return open(path, flags);
6098 * \param fd File to lock.
6099 * \param gid Group Identifier.
6101 * \retval 0 on success.
6102 * \retval -errno on failure.
6104 int llapi_group_lock(int fd, int gid)
6108 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
6111 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
6119 * \param fd File to unlock.
6120 * \param gid Group Identifier.
6122 * \retval 0 on success.
6123 * \retval -errno on failure.
6125 int llapi_group_unlock(int fd, int gid)
6129 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
6132 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");